[med-svn] [python-pysam] 01/02: Imported Upstream version 0.8.1

Jorge Soares jssoares-guest at moszumanska.debian.org
Wed Nov 26 11:06:53 UTC 2014


This is an automated email from the git hooks/post-receive script.

jssoares-guest pushed a commit to branch master
in repository python-pysam.

commit 91878cb6cda2aedc6e5e70177280bfa0c2fb4849
Author: Jorge Soares <j.s.soares at gmail.com>
Date:   Wed Nov 26 11:06:32 2014 +0000

    Imported Upstream version 0.8.1
---
 INSTALL                                          |     4 +-
 PKG-INFO                                         |     4 +-
 README.rst                                       |     4 +-
 debian/changelog                                 |    57 -
 debian/compat                                    |     1 -
 debian/control                                   |    77 -
 debian/copyright                                 |   109 -
 debian/gbp.conf                                  |     5 -
 debian/patches/do_not_use_distribute_setup.patch |    17 -
 debian/patches/fix_cleanup_tests.patch           |    19 -
 debian/patches/offline-tests.patch               |  1824 -
 debian/patches/series                            |     4 -
 debian/patches/use_external_htslib.patch         |    23 -
 debian/python-pysam-tests.README.Debian          |    12 -
 debian/python-pysam-tests.docs                   |     1 -
 debian/python-pysam-tests.install                |     1 -
 debian/rules                                     |    49 -
 debian/source/format                             |     1 -
 debian/tests/control                             |     3 -
 debian/tests/run-unit-test                       |    14 -
 debian/watch                                     |     3 -
 doc/api.rst                                      |   166 +-
 doc/conf.py                                      |    14 +
 doc/developer.rst                                |    40 +-
 doc/faq.rst                                      |    47 +-
 doc/glossary.rst                                 |    18 +-
 doc/index.rst                                    |    42 +-
 doc/out                                          |    30 +
 doc/release.rst                                  |   208 +-
 doc/usage.rst                                    |   308 +-
 htslib/__init__.py                               |     0
 htslib/bgzf.c                                    |   975 +-
 htslib/bgzip.c                                   |   291 +-
 htslib/cram/cram_decode.c                        |     2 +-
 htslib/cram/cram_index.c                         |     2 +
 htslib/cram/cram_io.c                            |     4 +-
 htslib/cram/cram_io.h                            |     2 +-
 htslib/cram/open_trace_file.c                    |   179 +-
 htslib/faidx.c                                   |   552 +-
 htslib/hfile.c                                   |     2 +-
 htslib/hfile_internal.h                          |     2 +-
 htslib/hfile_net.c                               |     2 +-
 htslib/hts.c                                     |  1874 +-
 htslib/htslib/__init__.py                        |     0
 htslib/htslib/bgzf.h                             |   415 +-
 htslib/htslib/faidx.h                            |   139 +-
 htslib/htslib/hfile.h                            |     2 +-
 htslib/htslib/hts.h                              |   173 +-
 htslib/htslib/hts_defs.h                         |     2 +-
 htslib/htslib/kfunc.h                            |    30 +-
 htslib/htslib/khash_str2int.h                    |    40 +-
 htslib/htslib/knetfile.h                         |    26 +
 htslib/htslib/kstdint.h                          |    64 -
 htslib/htslib/kstring.h                          |     2 +-
 htslib/htslib/sam.h                              |   243 +-
 htslib/htslib/synced_bcf_reader.h                |    95 +-
 htslib/htslib/tbx.h                              |    67 +-
 htslib/htslib/vcf.h                              |   489 +-
 htslib/htslib/vcf_sweep.h                        |    28 +-
 htslib/htslib/vcfutils.h                         |    60 +-
 htslib/kfunc.c                                   |    26 +
 htslib/knetfile.c                                |    17 +-
 htslib/kstring.c                                 |    25 +
 htslib/sam.c                                     |  2352 +-
 htslib/synced_bcf_reader.c                       |   130 +-
 htslib/tabix.c                                   |    61 +-
 htslib/tbx.c                                     |   411 +-
 htslib/vcf.c                                     |  1370 +-
 htslib/vcf_sweep.c                               |    24 +
 htslib/vcfutils.c                                |   174 +-
 htslib/version.h                                 |     2 +-
 pysam.egg-info/PKG-INFO                          |     4 +-
 pysam.egg-info/SOURCES.txt                       |    57 +-
 pysam.egg-info/requires.txt                      |     1 -
 pysam.py                                         |     2 +-
 pysam/TabProxies.c                               |  5037 ++-
 pysam/TabProxies.pxd                             |    33 +-
 pysam/TabProxies.pyx                             |   369 +-
 pysam/__init__.py                                |    26 +-
 pysam/{csamfile.c => calignmentfile.c}           | 33519 +++++++++-------
 pysam/{csamfile.pxd => calignmentfile.pxd}       |    36 +-
 pysam/{csamfile.pyx => calignmentfile.pyx}       |  2301 +-
 pysam/cfaidx.c                                   |  1344 +-
 pysam/cfaidx.pxd                                 |    11 +-
 pysam/cfaidx.pyx                                 |    53 +-
 pysam/chtslib.c                                  |     2 +-
 pysam/chtslib.pxd                                |    29 +-
 pysam/csamfile.c                                 | 44112 +--------------------
 pysam/csamfile.pxd                               |   187 +-
 pysam/csamfile.pyx                               |  3441 +-
 pysam/csamtools.c                                |     2 +-
 pysam/ctabix.c                                   |  5593 +--
 pysam/ctabix.pxd                                 |    26 +-
 pysam/ctabix.pyx                                 |   473 +-
 pysam/cvcf.c                                     |  7693 ++--
 pysam/cvcf.pyx                                   |    79 +-
 pysam/htslib_util.h                              |    61 +-
 pysam/pysam_util.c                               |    26 +-
 pysam/samfile_util.c                             |     2 +
 pysam/samfile_util.h                             |     1 -
 pysam/version.py                                 |     8 +-
 samtools/__init__.py                             |     0
 samtools/bam.c.pysam.c                           |   590 +-
 samtools/bam.h                                   |  1019 +-
 samtools/bam2bcf.c.pysam.c                       |  1069 +-
 samtools/bam2bcf.h                               |   137 +-
 samtools/bam2bcf_indel.c.pysam.c                 |   907 +-
 samtools/bam2depth.c.pysam.c                     |   238 +-
 samtools/bam_aux.c.pysam.c                       |   254 +-
 samtools/bam_cat.c.pysam.c                       |   341 +-
 samtools/bam_color.c.pysam.c                     |   226 +-
 samtools/bam_endian.h                            |    54 +-
 samtools/bam_flags.c.pysam.c                     |    70 +
 samtools/bam_import.c.pysam.c                    |   528 +-
 samtools/bam_index.c.pysam.c                     |   802 +-
 samtools/bam_lpileup.c.pysam.c                   |   313 +-
 samtools/bam_lpileup.h                           |    57 +
 samtools/bam_mate.c.pysam.c                      |   423 +-
 samtools/bam_md.c.pysam.c                        |   712 +-
 samtools/bam_pileup.c.pysam.c                    |   439 -
 samtools/bam_plbuf.c.pysam.c                     |    68 +
 htslib/htslib/hts_defs.h => samtools/bam_plbuf.h |    50 +-
 samtools/bam_plcmd.c.pysam.c                     |  1223 +-
 samtools/bam_reheader.c.pysam.c                  |   117 +-
 samtools/bam_rmdup.c.pysam.c                     |   329 +-
 samtools/bam_rmdupse.c.pysam.c                   |   259 +-
 samtools/bam_sort.c.pysam.c                      |  1554 +-
 samtools/bam_split.c.pysam.c                     |   510 +
 samtools/bam_stat.c.pysam.c                      |   162 +-
 samtools/bam_tview.c.pysam.c                     |   683 +-
 samtools/bam_tview.h                             |    70 +-
 samtools/bam_tview_curses.c.pysam.c              |   423 +-
 samtools/bam_tview_html.c.pysam.c                |   496 +-
 samtools/bamshuf.c.pysam.c                       |   254 +-
 samtools/bcftools/__init__.py                    |     0
 samtools/bcftools/bcf.c.pysam.c                  |   398 -
 samtools/bcftools/bcf.h                          |   197 -
 samtools/bcftools/bcf2qcall.c.pysam.c            |    93 -
 samtools/bcftools/bcfutils.c.pysam.c             |   506 -
 samtools/bcftools/call1.c.pysam.c                |   635 -
 samtools/bcftools/em.c.pysam.c                   |   312 -
 samtools/bcftools/fet.c.pysam.c                  |   114 -
 samtools/bcftools/index.c.pysam.c                |   338 -
 samtools/bcftools/kfunc.c.pysam.c                |   164 -
 samtools/bcftools/kmin.c.pysam.c                 |   211 -
 samtools/bcftools/kmin.h                         |    46 -
 samtools/bcftools/mut.c.pysam.c                  |   129 -
 samtools/bcftools/prob1.c.pysam.c                |   990 -
 samtools/bcftools/prob1.h                        |    49 -
 samtools/bcftools/vcf.c.pysam.c                  |   251 -
 samtools/bedcov.c.pysam.c                        |   241 +-
 samtools/bedidx.c.pysam.c                        |   328 +-
 samtools/bgzf.c.pysam.c                          |   696 -
 samtools/bgzf.h                                  |   207 -
 samtools/cut_target.c.pysam.c                    |   351 +-
 samtools/errmod.c.pysam.c                        |   277 +-
 samtools/errmod.h                                |    39 +-
 samtools/faidx.c.pysam.c                         |   486 +-
 samtools/faidx.h                                 |   103 -
 samtools/kaln.c.pysam.c                          |     2 +-
 samtools/kaln.h                                  |     2 +-
 samtools/khash.h                                 |   528 -
 samtools/klist.h                                 |    96 -
 samtools/knetfile.c.pysam.c                      |   634 -
 samtools/knetfile.h                              |    75 -
 samtools/kprobaln.c                              |     6 +-
 samtools/kprobaln.c.pysam.c                      |     6 +-
 samtools/kprobaln.h                              |     2 +-
 samtools/kseq.h                                  |   235 -
 samtools/ksort.h                                 |   285 -
 samtools/kstring.c.pysam.c                       |   214 -
 samtools/kstring.h                               |   169 -
 samtools/misc/ace2sam.c.pysam.c                  |   388 +-
 samtools/misc/md5.c.pysam.c                      |     4 +-
 samtools/padding.c.pysam.c                       |   866 +-
 samtools/phase.c.pysam.c                         |  1217 +-
 samtools/razf.c.pysam.c                          |   855 -
 samtools/razf.h                                  |   134 -
 samtools/sam.c.pysam.c                           |   246 +-
 samtools/sam.h                                   |   147 +-
 samtools/sam_header.c.pysam.c                    |   156 +-
 samtools/sam_header.h                            |    52 +-
 samtools/sam_view.c.pysam.c                      |  1037 +-
 samtools/sample.c.pysam.c                        |   173 +-
 samtools/sample.h                                |    32 +-
 htslib/htslib/hts_defs.h => samtools/samtools.h  |    30 +-
 samtools/stats.c.pysam.c                         |  1540 +
 samtools/stats_isize.c.pysam.c                   |   221 +
 samtools/stats_isize.h                           |    83 +
 samtools/test/test.c.pysam.c                     |    55 +
 htslib/htslib/hts_defs.h => samtools/test/test.h |    32 +-
 samtools/version.h                               |     1 +
 samtools/win32/__init__.py                       |     0
 setup.py                                         |   156 +-
 tests/example.gtf.gz.tbi                         |   Bin 260 -> 0 bytes
 tests/pysam_test.py                              |  1885 -
 tests/tabix_test.py                              |   908 -
 197 files changed, 51391 insertions(+), 100606 deletions(-)

diff --git a/INSTALL b/INSTALL
index 4ef01e0..2ce7add 100644
--- a/INSTALL
+++ b/INSTALL
@@ -55,9 +55,9 @@ The documentation will be put into build/sphinx.
 Architecture specific options
 =============================
 
-Pysam has been compiled on various linux systems and works
+Pysam has been compiled on various Linux systems and works
 with python 2.6 and python 2.5.
 
 Python 2.7 and Python 3 have not been tested.
 
-Windows support does not work yet
+Windows support does not work yet.
diff --git a/PKG-INFO b/PKG-INFO
index 41920ad..354479b 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,8 +1,8 @@
 Metadata-Version: 1.1
 Name: pysam
-Version: 0.8.0
+Version: 0.8.1
 Summary: pysam
-Home-page: http://code.google.com/p/pysam/
+Home-page: https://github.com/pysam-developers/pysam
 Author: Andreas Heger
 Author-email: andreas.heger at gmail.com
 License: MIT
diff --git a/README.rst b/README.rst
index 2f85ccb..cbe09d2 100644
--- a/README.rst
+++ b/README.rst
@@ -4,10 +4,10 @@ Pysam
 
 Pysam is a python module for reading and manipulating files in the
 SAM/BAM format. The SAM/BAM format is a way to store efficiently large
-numbers of alignments (`Li 2009`_), such as those routinely are created by
+numbers of alignments (`Li 2009`_), such as those routinely created by
 next-generation sequencing methods.
 
-pysam lightweight wrapper of the samtools_ C-API. Pysam also includes an
+Pysam is a lightweight wrapper of the samtools_ C-API. Pysam also includes an
 interface for tabix_.
 
 The latest version is available through 
diff --git a/debian/changelog b/debian/changelog
deleted file mode 100644
index 8f47519..0000000
--- a/debian/changelog
+++ /dev/null
@@ -1,57 +0,0 @@
-python-pysam (0.8.0-1) UNRELEASED; urgency=medium
-
-  [ Charles Plessy ]
-  * Requires Python 2.7 or higher. 
-
-  [ Andreas Tille ]
-  * New upstream version
-  * Link against htslib
-
- -- Andreas Tille <tille at debian.org>  Tue, 19 Aug 2014 21:26:37 +0200
-
-python-pysam (0.7.7-1) unstable; urgency=medium
-
-  * New upstream releases.
-  * Upstream source code moved to GitHub.
-  * Watch the Python Package Index since there are no relevant tags on GitHub.
-  * Added a git-buildpackage configuration file to mark its usage.
-  * Build-depend samtools (>= 0.1.19); this is needed for the regression tests
-    in Wheezy.
-  * debian/patches/offline-tests.patch: correction from a later release.
-
- -- Charles Plessy <plessy at debian.org>  Sat, 19 Apr 2014 14:17:42 +0900
-
-python-pysam (0.7.5-5) unstable; urgency=medium
-
-  * Add make to autopkgtest dependencies
-    Closes: #741274
-
- -- Andreas Tille <tille at debian.org>  Wed, 19 Mar 2014 13:30:15 +0100
-
-python-pysam (0.7.5-4) unstable; urgency=medium
-
-  * Fix autotest
-    Closes: #741274
-
- -- Andreas Tille <tille at debian.org>  Tue, 11 Mar 2014 20:08:15 +0100
-
-python-pysam (0.7.5-3) unstable; urgency=medium
-
-  * Do not install tests in world writable dir
-    Closes: #739575
-
- -- Andreas Tille <tille at debian.org>  Sat, 01 Mar 2014 23:40:21 +0100
-
-python-pysam (0.7.5-2) unstable; urgency=medium
-
-  * debian/rules: Set PYTHONPATH correctly using dh_python
-    (thanks to Piotr Ożarowski <piotr at debian.org> for the patch)
-    Closes: #739631
-
- -- Andreas Tille <tille at debian.org>  Thu, 20 Feb 2014 19:01:46 +0100
-
-python-pysam (0.7.5-1) unstable; urgency=low
-
-  * Initial release (Closes: #738665)
-
- -- Andreas Tille <tille at debian.org>  Fri, 07 Feb 2014 18:29:40 +0100
diff --git a/debian/compat b/debian/compat
deleted file mode 100644
index ec63514..0000000
--- a/debian/compat
+++ /dev/null
@@ -1 +0,0 @@
-9
diff --git a/debian/control b/debian/control
deleted file mode 100644
index 14f7647..0000000
--- a/debian/control
+++ /dev/null
@@ -1,77 +0,0 @@
-Source: python-pysam
-Maintainer: Debian Med Packaging Team <debian-med-packaging at lists.alioth.debian.org>
-Uploaders: Charles Plessy <plessy at debian.org>,
-           Andreas Tille <tille at debian.org>
-Section: python
-XS-Testsuite: autopkgtest
-Priority: optional
-Build-Depends: debhelper (>= 9),
-	       dh-python,
-	       python-all-dev,
-	       python-setuptools,
-	       cython,
-	       dh-python3,
-	       python3-all-dev,
-	       python3-setuptools,
-	       cython3,
-               zlib1g-dev,
-               samtools (>= 0.1.19),
-               libhts-dev
-Standards-Version: 3.9.5
-Vcs-Browser: http://anonscm.debian.org/gitweb/?p=debian-med/python-pysam.git
-Vcs-Git: git://anonscm.debian.org/debian-med/python-pysam.git
-Homepage: https://github.com/pysam-developers/pysam
-X-Python-Version: >= 2.7
-
-Package: python-pysam
-Architecture: any
-Depends: ${shlibs:Depends},
-         ${misc:Depends},
-         ${python:Depends},
-Description: interface for the SAM/BAM sequence alignment and mapping format
- Pysam is a Python module for reading and manipulating Samfiles. It's a
- lightweight wrapper of the samtools C-API. 
-
-Package: python3-pysam
-Architecture: any
-Depends: ${shlibs:Depends},
-         ${misc:Depends},
-         ${python3:Depends},
-Description: interface for the SAM/BAM sequence alignment and mapping format
- Pysam is a Python module for reading and manipulating Samfiles. It's a
- lightweight wrapper of the samtools C-API. 
-
-
-Package: python-pysam-tests
-Architecture: all
-Priority: extra
-Enhances: python-pysam
-Depends: ${shlibs:Depends},
-         ${misc:Depends},
-         ${python:Depends},
-         python,
-         python-pysam,
-         python-pyrex
-Description: interface for the SAM/BAM sequence alignment and mapping format (test data)
- Pysam is a Python module for reading and manipulating Samfiles. It's a
- lightweight wrapper of the samtools C-API. 
- .
- This package contains the data provided by upstream to run the pysam
- test suite.
-
-Package: python3-pysam-tests
-Architecture: all
-Priority: extra
-Enhances: python3-pysam
-Depends: ${shlibs:Depends},
-         ${misc:Depends},
-         ${python3:Depends},
-         python3,
-         python3-pysam,
-	 cython3
-Description: interface for the SAM/BAM sequence alignment and mapping format (test data)
- Pysam is a Python module for reading and manipulating Samfiles. It's a
- lightweight wrapper of the samtools C-API. 
- .
- This package contains the data provided by upstream to run the pysam
- test suite.
diff --git a/debian/copyright b/debian/copyright
deleted file mode 100644
index 95c3301..0000000
--- a/debian/copyright
+++ /dev/null
@@ -1,109 +0,0 @@
-Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
-Upstream-Name: pysam
-Upstream-Contact: Andreas Heger <andreas.heger at gmail.com>
-Source: https://pypi.python.org/packages/source/p/pysam/pysam-0.7.7.tar.gz
-
-Files: *
-Copyright: 2008-2010 Genome Research Ltd.
-License: MIT
-
-Files: samtools/khash.h samtools/kseq.h
-Copyright: 2008-2011 by Attractive Chaos <attractor at live.co.uk>
-License: MIT
-
-Files: samtools/kprobaln.c.pysam.c samtools/kaln.c.pysam.c samtools/kaln.h samtools/kprobaln.h
-Copyright: 2003-2006, 2008-2010, by Heng Li <lh3lh3 at live.co.uk>
-License: MIT
-
-Files: tabix/tabix.h
-Copyright: 2009 Genome Research Ltd (GRL), 2010 Broad Institute
-License: MIT
-
-Files: samtools/bcftools/bcf.h
-Copyright: 2010 Broad Institute
-License: MIT
-
-Files: samtools/bgzf.c.pysam.c samtools/bgzf.h tabix/bgzf.c.pysam.c tabix/bgzf.h samtools/knetfile.c.pysam.c
-Copyright: 2008 Broad Institute / Massachusetts Institute of Technology
-           2010-2012 Attractive Chaos <attractor at live.co.uk>
-License: MIT
-
-Files: tabix/bgzip.c.pysam.c
-Copyright: 2008 Broad Institute / Massachusetts Institute of Technology
-License: MIT
-
-Files: samtools/razf.c.pysam.c samtools/razf.h
-Copyright: 2008 Jue Ruan <ruanjue at gmail.com>, Heng Li <lh3 at sanger.ac.uk>
-License: BSDlike1
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
-   1. Redistributions of source code must retain the above copyright
-      notice, this list of conditions and the following disclaimer.
-   2. Redistributions in binary form must reproduce the above copyright
-      notice, this list of conditions and the following disclaimer in the
-      documentation and/or other materials provided with the distribution.
- .
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- SUCH DAMAGE.
-
-Files: samtools/win32/zconf.h samtools/win32/zlib.h
-Copyright: 1995-2005 Jean-loup Gailly <jloup at gzip.org> and Mark Adler <madler at alumni.caltech.edu>
-License: BSDlike2
-  This software is provided 'as-is', without any express or implied
-  warranty.  In no event will the authors be held liable for any damages
-  arising from the use of this software.
- .
-  Permission is granted to anyone to use this software for any purpose,
-  including commercial applications, and to alter it and redistribute it
-  freely, subject to the following restrictions:
- .
-  1. The origin of this software must not be misrepresented; you must not
-     claim that you wrote the original software. If you use this software
-     in a product, an acknowledgment in the product documentation would be
-     appreciated but is not required.
-  2. Altered source versions must be plainly marked as such, and must not be
-     misrepresented as being the original software.
-  3. This notice may not be removed or altered from any source distribution.
-Comment: These files are not used and could be stripped from the source
-
-Files: samtools/win32/xcurses.h
-Copyright: 2008 wmcbrine
-License: PublicDomain
- This file is in public domain.
-Comment: These files are not used and could be stripped from the source
-
-Files: samtools/misc/md5.*
-Copyright: 1993 Colin Plumb
-License: PublicDomain
- No copyright is claimed.
- This code is in the public domain; do with it what you wish.
-
-License: MIT
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- .
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- .
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-
diff --git a/debian/gbp.conf b/debian/gbp.conf
deleted file mode 100644
index bf424bb..0000000
--- a/debian/gbp.conf
+++ /dev/null
@@ -1,5 +0,0 @@
-# This source package is managed with git-buildpackage and pristine-tar.
-
-[DEFAULT]
-# use pristine-tar:
-pristine-tar = True
diff --git a/debian/patches/do_not_use_distribute_setup.patch b/debian/patches/do_not_use_distribute_setup.patch
deleted file mode 100644
index 7506fe2..0000000
--- a/debian/patches/do_not_use_distribute_setup.patch
+++ /dev/null
@@ -1,17 +0,0 @@
-Author: Olivier Sallou <olivier.sallou at irisa.fr>
-Last-Updated: Fri, 07 Feb 2014 18:29:40 +0100
-Description: Prevent downloading distribute_setup
-
---- a/setup.py
-+++ b/setup.py
-@@ -205,8 +205,8 @@ if len(sys.argv) == 2 and sys.argv[1] ==
- try:
-     from setuptools import Extension, setup
- except ImportError:
--    from ez_setup import use_setuptools
--    use_setuptools()
-+    #from ez_setup import use_setuptools
-+    #use_setuptools()
-     from setuptools import Extension, setup
- 
- #######################################################
diff --git a/debian/patches/fix_cleanup_tests.patch b/debian/patches/fix_cleanup_tests.patch
deleted file mode 100644
index 852dd09..0000000
--- a/debian/patches/fix_cleanup_tests.patch
+++ /dev/null
@@ -1,19 +0,0 @@
-Author: Andreas Tille <tille at debian.org>
-Last-Changed: Mon, 10 Feb 2014 11:29:40 +0100
-Description: Prevent tests makefile from deleting files which
- are contained inside the upstream source
-
---- a/tests/Makefile
-+++ b/tests/Makefile
-@@ -47,6 +47,11 @@
- 	samtools view -bS $< > $@
- 
- clean:
-+	mkdir keep_bam_files
-+	mv ex9_fail.bam ex9_nofail.bam example_btag.bam example_empty_header.bam issue100.bam tag_bug.bam test_unaligned.bam keep_bam_files
- 	rm -fr *.bam *.bai *.fai *.pileup* \
- 		*~ calDepth *.dSYM pysam_*.sam \
- 	ex2.sam ex2.sam.gz ex1.sam
-+	mv keep_bam_files/* .
-+	rmdir keep_bam_files
-+	rm -rf pysam_test_work/
diff --git a/debian/patches/offline-tests.patch b/debian/patches/offline-tests.patch
deleted file mode 100644
index 022dd10..0000000
--- a/debian/patches/offline-tests.patch
+++ /dev/null
@@ -1,1824 +0,0 @@
-Author: Andreas Tille <tille at debian.org>
-Last-Changed: Mon, 10 Feb 2014 11:29:40 +0100
-Description: Create a copy of the test suite script and remove those
- tests that try to fetch files from network
-
---- /dev/null
-+++ b/tests/pysam_test_offline.py
-@@ -0,0 +1,1816 @@
-+#!/usr/bin/env python
-+'''unit testing code for pysam.
-+
-+Execute in the :file:`tests` directory as it requires the Makefile
-+and data files located there.
-+'''
-+
-+import pysam
-+import unittest
-+import os, re, sys
-+import itertools
-+import collections
-+import subprocess
-+import shutil
-+import logging
-+
-+IS_PYTHON3 = sys.version_info[0] >= 3
-+
-+if IS_PYTHON3:
-+    from itertools import zip_longest
-+else:
-+    from itertools import izip as zip_longest
-+
-+
-+SAMTOOLS="samtools"
-+WORKDIR="pysam_test_work"
-+
-+def checkBinaryEqual( filename1, filename2 ):
-+    '''return true if the two files are binary equal.'''
-+    if os.path.getsize( filename1 ) !=  os.path.getsize( filename2 ):
-+        return False
-+
-+    infile1 = open(filename1, "rb")
-+    infile2 = open(filename2, "rb")
-+
-+    def chariter( infile ):
-+        while 1:
-+            c = infile.read(1)
-+            if c == b"": break
-+            yield c
-+
-+    found = False
-+    for c1,c2 in zip_longest( chariter( infile1), chariter( infile2) ):
-+        if c1 != c2: break
-+    else:
-+        found = True
-+
-+    infile1.close()
-+    infile2.close()
-+    return found
-+
-+def runSamtools( cmd ):
-+    '''run a samtools command'''
-+
-+    try:
-+        retcode = subprocess.call(cmd, shell=True,
-+                                  stderr = subprocess.PIPE)
-+        if retcode < 0:
-+            print("Child was terminated by signal", -retcode)
-+    except OSError as e:
-+        print("Execution failed:", e)
-+
-+def getSamtoolsVersion():
-+    '''return samtools version'''
-+
-+    with subprocess.Popen(SAMTOOLS, shell=True, stderr=subprocess.PIPE).stderr as pipe:
-+        lines = b"".join(pipe.readlines())
-+
-+    if IS_PYTHON3:
-+        lines = lines.decode('ascii')
-+    return re.search( "Version:\s+(\S+)", lines).groups()[0]
-+
-+class BinaryTest(unittest.TestCase):
-+    '''test samtools command line commands and compare
-+    against pysam commands.
-+
-+    Tests fail, if the output is not binary identical.
-+    '''
-+
-+    first_time = True
-+
-+    # a dictionary of commands to test
-+    # first entry: (samtools output file, samtools command)
-+    # second entry: (pysam output file, (pysam function, pysam options) )
-+    commands = \
-+        { 
-+          "view" :
-+              (
-+                ("ex1.view", "view ex1.bam > ex1.view"),
-+                ("pysam_ex1.view", (pysam.view, "ex1.bam" ) ),
-+                ),
-+          "view2" :
-+              (
-+                ("ex1.view", "view -bT ex1.fa -o ex1.view2 ex1.sam"),
-+                # note that -o ex1.view2 throws exception.
-+                ("pysam_ex1.view", (pysam.view, "-bT ex1.fa -oex1.view2 ex1.sam" ) ),
-+                ),
-+          "sort" :
-+              (
-+                ( "ex1.sort.bam", "sort ex1.bam ex1.sort" ),
-+                ( "pysam_ex1.sort.bam", (pysam.sort, "ex1.bam pysam_ex1.sort" ) ),
-+                ),
-+          "mpileup" :
-+              (
-+                ("ex1.pileup", "mpileup ex1.bam > ex1.pileup" ),
-+                ("pysam_ex1.mpileup", (pysam.mpileup, "ex1.bam" ) ),
-+                ),
-+          "depth" :
-+              (
-+                ("ex1.depth", "depth ex1.bam > ex1.depth" ),
-+                ("pysam_ex1.depth", (pysam.depth, "ex1.bam" ) ),
-+                ),
-+          "faidx" : 
-+              ( 
-+                ("ex1.fa.fai", "faidx ex1.fa"), 
-+                ("pysam_ex1.fa.fai", (pysam.faidx, "ex1.fa") ),
-+                ),
-+          "index":
-+              (
-+                ("ex1.bam.bai", "index ex1.bam" ),
-+                ("pysam_ex1.bam.bai", (pysam.index, "pysam_ex1.bam" ) ),
-+                ),
-+          "idxstats" :
-+              ( 
-+                ("ex1.idxstats", "idxstats ex1.bam > ex1.idxstats" ),
-+                ("pysam_ex1.idxstats", (pysam.idxstats, "pysam_ex1.bam" ) ),
-+                ),
-+          "fixmate" :
-+              (
-+                ("ex1.fixmate", "fixmate ex1.bam ex1.fixmate" ),
-+                ("pysam_ex1.fixmate", (pysam.fixmate, "pysam_ex1.bam pysam_ex1.fixmate") ),
-+                ),
-+          "flagstat" :
-+              (
-+                ("ex1.flagstat", "flagstat ex1.bam > ex1.flagstat" ),
-+                ("pysam_ex1.flagstat", (pysam.flagstat, "pysam_ex1.bam") ),
-+                ),
-+          "calmd" :
-+              (
-+                ("ex1.calmd", "calmd ex1.bam ex1.fa > ex1.calmd" ),
-+                ("pysam_ex1.calmd", (pysam.calmd, "pysam_ex1.bam ex1.fa") ),
-+                ),
-+          "merge" :
-+              (
-+                ("ex1.merge", "merge -f ex1.merge ex1.bam ex1.bam" ),
-+                # -f option does not work - following command will cause the subsequent
-+                # command to fail
-+                ("pysam_ex1.merge", (pysam.merge, "pysam_ex1.merge pysam_ex1.bam pysam_ex1.bam") ),
-+                ),
-+          "rmdup" :
-+              (
-+                ("ex1.rmdup", "rmdup ex1.bam ex1.rmdup" ),
-+                ("pysam_ex1.rmdup", (pysam.rmdup, "pysam_ex1.bam pysam_ex1.rmdup" )),
-+                ),
-+          "reheader" :
-+              (
-+                ( "ex1.reheader", "reheader ex1.bam ex1.bam > ex1.reheader"),
-+                ( "pysam_ex1.reheader", (pysam.reheader, "ex1.bam ex1.bam" ) ),
-+                ),
-+          "cat":
-+              (
-+                ( "ex1.cat", "cat ex1.bam ex1.bam > ex1.cat"),
-+                ( "pysam_ex1.cat", (pysam.cat, "ex1.bam ex1.bam" ) ),
-+                ),
-+          "targetcut":
-+              (
-+                ("ex1.targetcut", "targetcut ex1.bam > ex1.targetcut" ),
-+                ("pysam_ex1.targetcut", (pysam.targetcut, "pysam_ex1.bam") ),
-+                ),
-+          "phase":
-+              (
-+                ("ex1.phase", "phase ex1.bam > ex1.phase" ),
-+                ("pysam_ex1.phase", (pysam.phase, "pysam_ex1.bam") ),
-+                ),
-+          "import" :
-+              (
-+                ("ex1.bam", "import ex1.fa.fai ex1.sam.gz ex1.bam" ),
-+                ("pysam_ex1.bam", (pysam.samimport, "ex1.fa.fai ex1.sam.gz pysam_ex1.bam") ),
-+                ),
-+          "bam2fq":
-+              (
-+                ("ex1.bam2fq", "bam2fq ex1.bam > ex1.bam2fq" ),
-+                ("pysam_ex1.bam2fq", (pysam.bam2fq, "pysam_ex1.bam") ),
-+                ),
-+          "pad2unpad":
-+              (
-+                ("ex2.unpad", "pad2unpad -T ex1.fa ex2.bam > ex2.unpad" ),
-+                ("pysam_ex2.unpad", (pysam.pad2unpad, "-T ex1.fa ex2.bam") ),
-+                ),
-+          "bamshuf":
-+              (
-+                ("ex1.bamshuf.bam", "bamshuf ex1.bam ex1.bamshuf" ),
-+                ("pysam_ex1.bamshuf.bam", (pysam.bamshuf, "ex1.bam pysam_ex1.bamshuf") ),
-+                ),
-+          "bedcov":
-+              (
-+                ("ex1.bedcov", "bedcov ex1.bed ex1.bam > ex1.bedcov" ),
-+                ("pysam_ex1.bedcov", (pysam.bedcov, "ex1.bed ex1.bam") ),
-+                ),
-+        }
-+
-+    # some tests depend on others. The order specifies in which order
-+    # the samtools commands are executed.
-+    # The first three (faidx, import, index) need to be in that order, 
-+    # the rest is arbitrary.
-+    order = ('faidx', 'import', 'index', 
-+             # 'pileup1', 'pileup2', deprecated
-+             # 'glfview', deprecated
-+             'view', 'view2',
-+             'sort',
-+             'mpileup',
-+             'depth',
-+             'idxstats',
-+             'fixmate',
-+             'flagstat',
-+              ## 'calmd',
-+             'merge',
-+             'rmdup',
-+             'reheader',
-+             'cat',
-+             'bedcov',
-+             'targetcut',
-+             'phase',
-+             'bamshuf',
-+             'bam2fq',
-+              'pad2unpad',
-+              )
-+
-+    def setUp( self ):
-+        '''setup tests. 
-+
-+        For setup, all commands will be run before the first test is
-+        executed. Individual tests will then just compare the output
-+        files.
-+        '''
-+        if BinaryTest.first_time:
-+
-+            # remove previous files
-+            if os.path.exists( WORKDIR ):
-+                shutil.rmtree( WORKDIR )
-+                pass
-+
-+            # copy the source files to WORKDIR
-+            os.makedirs( WORKDIR )
-+
-+            shutil.copy( "ex1.fa", os.path.join( WORKDIR, "pysam_ex1.fa" ) )
-+            shutil.copy( "ex1.fa", os.path.join( WORKDIR, "ex1.fa" ) )
-+            shutil.copy( "ex1.sam.gz", os.path.join( WORKDIR, "ex1.sam.gz" ) )
-+            shutil.copy( "ex1.sam", os.path.join( WORKDIR, "ex1.sam" ) )
-+            shutil.copy( "ex2.bam", os.path.join( WORKDIR, "ex2.bam" ) )
-+
-+            # cd to workdir
-+            savedir = os.getcwd()
-+            os.chdir( WORKDIR )
-+            
-+            for label in self.order:
-+                # print ("command=", label)
-+                command = self.commands[label]
-+                # build samtools command and target and run
-+                samtools_target, samtools_command = command[0]
-+                runSamtools( " ".join( (SAMTOOLS, samtools_command )))
-+
-+                # get pysam command and run
-+                try:
-+                    pysam_target, pysam_command = command[1]
-+                except ValueError as msg:
-+                    raise ValueError( "error while setting up %s=%s: %s" %\
-+                                          (label, command, msg) )
-+
-+                pysam_method, pysam_options = pysam_command
-+                try:
-+                    output = pysam_method( *pysam_options.split(" "), raw=True)
-+                except pysam.SamtoolsError as msg:
-+                    raise pysam.SamtoolsError( "error while executing %s: options=%s: msg=%s" %\
-+                                                   (label, pysam_options, msg) )
-+
-+                
-+                if ">" in samtools_command:
-+                    with open( pysam_target, "wb" ) as outfile:
-+                        if type(output) == list:
-+                            if IS_PYTHON3:
-+                                for line in output: 
-+                                    outfile.write( line.encode('ascii') )
-+                            else:
-+                                for line in output: outfile.write( line )
-+                        else:
-+                            outfile.write(output)
-+
-+            os.chdir( savedir )
-+            BinaryTest.first_time = False
-+
-+        samtools_version = getSamtoolsVersion()
-+
-+        
-+        def _r( s ):
-+            # patch - remove any of the alpha/beta suffixes, i.e., 0.1.12a -> 0.1.12
-+            if s.count('-') > 0: s = s[0:s.find('-')]
-+            return re.sub( "[^0-9.]", "", s )
-+
-+        if _r(samtools_version) != _r( pysam.__samtools_version__):
-+            raise ValueError("versions of pysam/samtools and samtools differ: %s != %s" % \
-+                                 (pysam.__samtools_version__,
-+                                  samtools_version ))
-+
-+    def checkCommand( self, command ):
-+        if command:
-+            samtools_target, pysam_target = self.commands[command][0][0], self.commands[command][1][0]
-+            samtools_target = os.path.join( WORKDIR, samtools_target )
-+            pysam_target = os.path.join( WORKDIR, pysam_target )
-+            self.assertTrue( checkBinaryEqual( samtools_target, pysam_target ), 
-+                             "%s failed: files %s and %s are not the same" % (command, samtools_target, pysam_target) )
-+            
-+    def testImport( self ):
-+        self.checkCommand( "import" )
-+
-+    def testIndex( self ):
-+        self.checkCommand( "index" )
-+
-+    def testSort( self ):
-+        self.checkCommand( "sort" )
-+
-+    def testMpileup( self ):
-+        self.checkCommand( "mpileup" )
-+
-+    def testDepth( self ):
-+        self.checkCommand( "depth" )
-+
-+    def testIdxstats( self ):
-+        self.checkCommand( "idxstats" )
-+
-+    def testFixmate( self ):
-+        self.checkCommand( "fixmate" )
-+
-+    def testFlagstat( self ):
-+        self.checkCommand( "flagstat" )
-+        
-+    def testMerge( self ):
-+        self.checkCommand( "merge" )
-+
-+    def testRmdup( self ):
-+        self.checkCommand( "rmdup" )
-+
-+    def testReheader( self ):
-+        self.checkCommand( "reheader" )
-+
-+    def testCat( self ):
-+        self.checkCommand( "cat" )
-+
-+    def testTargetcut( self ):
-+        self.checkCommand( "targetcut" )
-+
-+    def testPhase( self ):
-+        self.checkCommand( "phase" )
-+
-+    def testBam2fq( self ):
-+        self.checkCommand( "bam2fq" )
-+
-+    def testBedcov( self ):
-+        self.checkCommand( "bedcov" )
-+
-+    def testBamshuf( self ):
-+        self.checkCommand( "bamshuf" )
-+
-+    def testPad2Unpad( self ):
-+        self.checkCommand( "pad2unpad" )
-+
-+    # def testPileup1( self ):
-+    #     self.checkCommand( "pileup1" )
-+    
-+    # def testPileup2( self ):
-+    #     self.checkCommand( "pileup2" )
-+
-+    # deprecated
-+    # def testGLFView( self ):
-+    #     self.checkCommand( "glfview" )
-+
-+    def testView( self ):
-+        self.checkCommand( "view" )
-+
-+    def testEmptyIndex( self ):
-+        self.assertRaises( IOError, pysam.index, "exdoesntexist.bam" )
-+
-+    def __del__(self):
-+        if os.path.exists( WORKDIR ):
-+            pass
-+        # shutil.rmtree( WORKDIR )
-+
-+class IOTest(unittest.TestCase):
-+    '''check if reading samfile and writing a samfile are consistent.'''
-+
-+    def checkEcho( self, input_filename, 
-+                   reference_filename, 
-+                   output_filename, 
-+                   input_mode, output_mode, use_template = True ):
-+        '''iterate through *input_filename* writing to *output_filename* and
-+        comparing the output to *reference_filename*. 
-+        
-+        The files are opened according to the *input_mode* and *output_mode*.
-+
-+        If *use_template* is set, the header is copied from infile using the
-+        template mechanism, otherwise target names and lengths are passed 
-+        explicitely. 
-+
-+        '''
-+
-+        infile = pysam.Samfile( input_filename, input_mode )
-+        if use_template:
-+            outfile = pysam.Samfile( output_filename, output_mode, template = infile )
-+        else:
-+            outfile = pysam.Samfile( output_filename, output_mode, 
-+                                     referencenames = infile.references,
-+                                     referencelengths = infile.lengths,
-+                                     add_sq_text = False )
-+            
-+        iter = infile.fetch()
-+
-+        for x in iter: outfile.write( x )
-+        infile.close()
-+        outfile.close()
-+
-+        self.assertTrue( checkBinaryEqual( reference_filename, output_filename), 
-+                         "files %s and %s are not the same" % (reference_filename, output_filename) )
-+
-+
-+    def testReadWriteBam( self ):
-+        
-+        input_filename = "ex1.bam"
-+        output_filename = "pysam_ex1.bam"
-+        reference_filename = "ex1.bam"
-+
-+        self.checkEcho( input_filename, reference_filename, output_filename,
-+                        "rb", "wb" )
-+
-+    def testReadWriteBamWithTargetNames( self ):
-+        
-+        input_filename = "ex1.bam"
-+        output_filename = "pysam_ex1.bam"
-+        reference_filename = "ex1.bam"
-+
-+        self.checkEcho( input_filename, reference_filename, output_filename,
-+                        "rb", "wb", use_template = False )
-+
-+    def testReadWriteSamWithHeader( self ):
-+        
-+        input_filename = "ex2.sam"
-+        output_filename = "pysam_ex2.sam"
-+        reference_filename = "ex2.sam"
-+
-+        self.checkEcho( input_filename, reference_filename, output_filename,
-+                        "r", "wh" )
-+
-+    def testReadWriteSamWithoutHeader( self ):
-+        
-+        input_filename = "ex2.sam"
-+        output_filename = "pysam_ex2.sam"
-+        reference_filename = "ex1.sam"
-+
-+        self.checkEcho( input_filename, reference_filename, output_filename,
-+                        "r", "w" )
-+
-+    def testReadSamWithoutTargetNames( self ):
-+        '''see issue 104.'''
-+        input_filename = "example_unmapped_reads_no_sq.sam"
-+
-+        # raise exception in default mode
-+        self.assertRaises( ValueError, pysam.Samfile, input_filename, "r" )
-+
-+        # raise exception if no SQ files
-+        self.assertRaises( ValueError, pysam.Samfile, input_filename, "r",
-+                           check_header = True)
-+
-+        infile = pysam.Samfile( input_filename, check_header = False, check_sq = False )
-+        result = list(infile.fetch())
-+
-+    def testReadBamWithoutTargetNames( self ):
-+        '''see issue 104.'''
-+        input_filename = "example_unmapped_reads_no_sq.bam"
-+
-+        # raise exception in default mode
-+        self.assertRaises( ValueError, pysam.Samfile, input_filename, "r" )
-+
-+        # raise exception if no SQ files
-+        self.assertRaises( ValueError, pysam.Samfile, input_filename, "r",
-+                           check_header = True)
-+
-+
-+        infile = pysam.Samfile( input_filename, check_header = False, check_sq = False )
-+        result = list(infile.fetch( until_eof = True))
-+
-+    def testReadSamWithoutHeader( self ):
-+        input_filename = "ex1.sam"
-+        output_filename = "pysam_ex1.sam"
-+        reference_filename = "ex1.sam"
-+
-+        # reading from a samfile without header is not implemented.
-+        self.assertRaises( ValueError, pysam.Samfile, input_filename, "r" )
-+
-+        self.assertRaises( ValueError, pysam.Samfile, input_filename, "r",
-+                           check_header = False )
-+
-+    def testReadUnformattedFile( self ):
-+        '''test reading from a file that is not bam/sam formatted'''
-+        input_filename = "example.vcf40"
-+
-+        # bam - file raise error
-+        self.assertRaises( ValueError, pysam.Samfile, input_filename, "rb" )
-+
-+        # sam - file error, but can't fetch
-+        self.assertRaises( ValueError, pysam.Samfile, input_filename, "r" )
-+        
-+        self.assertRaises( ValueError, pysam.Samfile, input_filename, "r", 
-+                           check_header = False)
-+
-+    def testBAMWithoutAlignedReads( self ):
-+        '''see issue 117'''
-+        input_filename = "test_unaligned.bam"
-+        samfile = pysam.Samfile( input_filename, "rb", check_sq = False )
-+        samfile.fetch( until_eof = True )
-+
-+    def testBAMWithShortBAI( self ):
-+        '''see issue 116'''
-+        input_filename = "example_bai.bam"
-+        samfile = pysam.Samfile( input_filename, "rb", check_sq = False )
-+        samfile.fetch( 'chr2' )
-+
-+    def testFetchFromClosedFile( self ):
-+
-+        samfile = pysam.Samfile( "ex1.bam", "rb" )
-+        samfile.close()
-+        self.assertRaises( ValueError, samfile.fetch, 'chr1', 100, 120)
-+
-+    def testClosedFile( self ):
-+        '''test that access to a closed samfile raises ValueError.'''
-+
-+        samfile = pysam.Samfile( "ex1.bam", "rb" )
-+        samfile.close()
-+        self.assertRaises( ValueError, samfile.fetch, 'chr1', 100, 120)
-+        self.assertRaises( ValueError, samfile.pileup, 'chr1', 100, 120)
-+        self.assertRaises( ValueError, samfile.getrname, 0 )
-+        self.assertRaises( ValueError, samfile.tell )
-+        self.assertRaises( ValueError, samfile.seek, 0 )
-+        self.assertRaises( ValueError, getattr, samfile, "nreferences" )
-+        self.assertRaises( ValueError, getattr, samfile, "references" )
-+        self.assertRaises( ValueError, getattr, samfile, "lengths" )
-+        self.assertRaises( ValueError, getattr, samfile, "text" )
-+        self.assertRaises( ValueError, getattr, samfile, "header" )
-+
-+        # write on closed file 
-+        self.assertEqual( 0, samfile.write(None) )
-+
-+    def testAutoDetection( self ):
-+        '''test if autodetection works.'''
-+
-+        samfile = pysam.Samfile( "ex3.sam" )
-+        self.assertRaises( ValueError, samfile.fetch, 'chr1' )
-+        samfile.close()
-+
-+        samfile = pysam.Samfile( "ex3.bam" )
-+        samfile.fetch('chr1')
-+        samfile.close()
-+
-+    def testReadingFromSamFileWithoutHeader( self ):
-+        '''read from samfile without header.
-+        '''
-+        samfile = pysam.Samfile( "ex7.sam", check_header = False, check_sq = False )
-+        self.assertRaises( NotImplementedError, samfile.__iter__ )
-+
-+    def testReadingFromFileWithoutIndex( self ):
-+        '''read from bam file without index.'''
-+
-+        assert not os.path.exists( "ex2.bam.bai" )
-+        samfile = pysam.Samfile( "ex2.bam", "rb" )
-+        self.assertRaises( ValueError, samfile.fetch )
-+        self.assertEqual( len(list( samfile.fetch(until_eof = True) )), 3270 )
-+
-+    def testReadingUniversalFileMode( self ):
-+        '''read from samfile without header.
-+        '''
-+
-+        input_filename = "ex2.sam"
-+        output_filename = "pysam_ex2.sam"
-+        reference_filename = "ex1.sam"
-+
-+        self.checkEcho( input_filename, reference_filename, output_filename,
-+                        "rU", "w" )
-+
-+class TestFloatTagBug( unittest.TestCase ):
-+    '''see issue 71'''
-+
-+    def testFloatTagBug( self ): 
-+        '''a float tag before another exposed a parsing bug in bam_aux_get.
-+
-+        Fixed in 0.1.19
-+        '''
-+        samfile = pysam.Samfile("tag_bug.bam")
-+        read = next(samfile.fetch(until_eof=True))
-+        self.assertTrue( ('XC',1) in read.tags )
-+        self.assertEqual(read.opt('XC'), 1)
-+
-+class TestLargeFieldBug( unittest.TestCase ):
-+    '''see issue 100'''
-+
-+    def testLargeFileBug( self ): 
-+        '''when creating a read with a large entry in the tag field
-+        causes an errror:
-+            NotImplementedError: tags field too large
-+        '''
-+        samfile = pysam.Samfile("issue100.bam")
-+        read = next(samfile.fetch(until_eof=True))
-+        new_read = pysam.AlignedRead()
-+        new_read.tags = read.tags
-+        self.assertEqual( new_read.tags, read.tags )
-+
-+class TestTagParsing( unittest.TestCase ):
-+    '''tests checking the accuracy of tag setting and retrieval.'''
-+
-+    def makeRead( self ):
-+        a = pysam.AlignedRead()
-+        a.qname = "read_12345"
-+        a.tid = 0
-+        a.seq="ACGT" * 3
-+        a.flag = 0
-+        a.rname = 0
-+        a.pos = 1
-+        a.mapq = 20
-+        a.cigar = ( (0,10), (2,1), (0,25) )
-+        a.mrnm = 0
-+        a.mpos=200
-+        a.isize = 0
-+        a.qual ="1234" * 3
-+        # todo: create tags
-+        return a
-+
-+    def testNegativeIntegers( self ):
-+        x = -2
-+        aligned_read = self.makeRead()
-+        aligned_read.tags = [("XD", int(x) ) ]
-+        # print (aligned_read.tags)
-+
-+    def testNegativeIntegers2( self ):
-+        x = -2
-+        r = self.makeRead()
-+        r.tags = [("XD", int(x) ) ]
-+        outfile = pysam.Samfile( "test.bam",
-+                                 "wb",
-+                                 referencenames = ("chr1",),
-+                                 referencelengths = (1000,) )
-+        outfile.write (r )
-+        outfile.close()
-+
-+    def testCigarString( self ):
-+        r = self.makeRead()
-+        self.assertEqual( r.cigarstring, "10M1D25M" )
-+        r.cigarstring = "20M10D20M"
-+        self.assertEqual( r.cigar, [(0,20), (2,10), (0,20)])
-+
-+    def testLongTags( self ):
-+        '''see issue 115'''
-+        
-+        r = self.makeRead()
-+        rg = 'HS2000-899_199.L3'
-+        tags = [('XC', 85), ('XT', 'M'), ('NM', 5), ('SM', 29), ('AM', 29), ('XM', 1), ('XO', 1), ('XG', 4), ('MD', '37^ACCC29T18'), ('XA','5,+11707,36M1I48M,2;21,-48119779,46M1I38M,2;hs37d5,-10060835,40M1D45M,3;5,+11508,36M1I48M,3;hs37d5,+6743812,36M1I48M,3;19,-59118894,46M1I38M,3;4,-191044002,6M1I78M,3;')]
-+
-+        r.tags = tags
-+        r.tags += [("RG",rg)] * 100
-+        tags += [("RG",rg)] * 100
-+        
-+        self.assertEqual( tags, r.tags )
-+
-+class TestIteratorRow(unittest.TestCase):
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex1.bam","rb" )
-+
-+    def checkRange( self, rnge ):
-+        '''compare results from iterator with those from samtools.'''
-+        ps = list(self.samfile.fetch(region=rnge))
-+        sa = list(pysam.view( "ex1.bam", rnge, raw = True) )
-+        self.assertEqual( len(ps), len(sa), "unequal number of results for range %s: %i != %i" % (rnge, len(ps), len(sa) ))
-+        # check if the same reads are returned and in the same order
-+        for line, (a, b) in enumerate( list(zip( ps, sa )) ):
-+            d = b.split("\t")
-+            self.assertEqual( a.qname, d[0], "line %i: read id mismatch: %s != %s" % (line, a.rname, d[0]) )
-+            self.assertEqual( a.pos, int(d[3])-1, "line %i: read position mismatch: %s != %s, \n%s\n%s\n" % \
-+                                  (line, a.pos, int(d[3])-1,
-+                                   str(a), str(d) ) )
-+            if sys.version_info[0] < 3:
-+                qual = d[10]
-+            else:
-+                qual = d[10].encode('ascii')
-+            self.assertEqual( a.qual, qual, "line %i: quality mismatch: %s != %s, \n%s\n%s\n" % \
-+                                  (line, a.qual, qual,
-+                                   str(a), str(d) ) )
-+
-+    def testIteratePerContig(self):
-+        '''check random access per contig'''
-+        for contig in self.samfile.references:
-+            self.checkRange( contig )
-+
-+    def testIterateRanges(self):
-+        '''check random access per range'''
-+        for contig, length in zip(self.samfile.references, self.samfile.lengths):
-+            for start in range( 1, length, 90):
-+                self.checkRange( "%s:%i-%i" % (contig, start, start + 90) ) # this includes empty ranges
-+
-+    def tearDown(self):
-+        self.samfile.close()
-+
-+
-+class TestIteratorRowAll(unittest.TestCase):
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex1.bam","rb" )
-+
-+    def testIterate(self):
-+        '''compare results from iterator with those from samtools.'''
-+        ps = list(self.samfile.fetch())
-+        sa = list(pysam.view( "ex1.bam", raw = True) )
-+        self.assertEqual( len(ps), len(sa), "unequal number of results: %i != %i" % (len(ps), len(sa) ))
-+        # check if the same reads are returned
-+        for line, pair in enumerate( list(zip( ps, sa )) ):
-+            data = pair[1].split("\t")
-+            self.assertEqual( pair[0].qname, data[0], "read id mismatch in line %i: %s != %s" % (line, pair[0].rname, data[0]) )
-+
-+    def tearDown(self):
-+        self.samfile.close()
-+
-+class TestIteratorColumn(unittest.TestCase):
-+    '''test iterator column against contents of ex4.bam.'''
-+    
-+    # note that samfile contains 1-based coordinates
-+    # 1D means deletion with respect to reference sequence
-+    # 
-+    mCoverages = { 'chr1' : [ 0 ] * 20 + [1] * 36 + [0] * (100 - 20 -35 ),
-+                   'chr2' : [ 0 ] * 20 + [1] * 35 + [0] * (100 - 20 -35 ),
-+                   }
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex4.bam","rb" )
-+
-+    def checkRange( self, contig, start = None, end = None, truncate = False ):
-+        '''compare results from iterator with those from samtools.'''
-+        # check if the same reads are returned and in the same order
-+        for column in self.samfile.pileup(contig, start, end, truncate = truncate):
-+            if truncate:
-+                self.assertGreaterEqual( column.pos, start )
-+                self.assertLess( column.pos, end )
-+            thiscov = len(column.pileups)
-+            refcov = self.mCoverages[self.samfile.getrname(column.tid)][column.pos]
-+            self.assertEqual( thiscov, refcov, "wrong coverage at pos %s:%i %i should be %i" % (self.samfile.getrname(column.tid), column.pos, thiscov, refcov))
-+
-+    def testIterateAll(self):
-+        '''check random access per contig'''
-+        self.checkRange( None )
-+
-+    def testIteratePerContig(self):
-+        '''check random access per contig'''
-+        for contig in self.samfile.references:
-+            self.checkRange( contig )
-+
-+    def testIterateRanges(self):
-+        '''check random access per range'''
-+        for contig, length in zip(self.samfile.references, self.samfile.lengths):
-+            for start in range( 1, length, 90):
-+                self.checkRange( contig, start, start + 90 ) # this includes empty ranges
-+
-+    def testInverse( self ):
-+        '''test the inverse, is point-wise pileup accurate.'''
-+        for contig, refseq in list(self.mCoverages.items()):
-+            refcolumns = sum(refseq)
-+            for pos, refcov in enumerate( refseq ):
-+                columns = list(self.samfile.pileup( contig, pos, pos+1) )
-+                if refcov == 0:
-+                    # if no read, no coverage
-+                    self.assertEqual( len(columns), refcov, "wrong number of pileup columns returned for position %s:%i, %i should be %i" %(contig,pos,len(columns), refcov) )
-+                elif refcov == 1:
-+                    # one read, all columns of the read are returned
-+                    self.assertEqual( len(columns), refcolumns, "pileup incomplete at position %i: got %i, expected %i " %\
-+                                          (pos, len(columns), refcolumns))
-+
-+    def testIterateTruncate( self ):
-+        '''check random access per range'''
-+        for contig, length in zip(self.samfile.references, self.samfile.lengths):
-+            for start in range( 1, length, 90):
-+                self.checkRange( contig, start, start + 90, truncate = True ) # this includes empty ranges
-+                
-+    def tearDown(self):
-+        self.samfile.close()
-+
-+class TestIteratorColumn2(unittest.TestCase):
-+    '''test iterator column against contents of ex1.bam.'''
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex1.bam","rb" )
-+
-+    def testStart( self ):
-+        #print self.samfile.fetch().next().pos
-+        #print self.samfile.pileup().next().pos
-+        pass
-+
-+    def testTruncate( self ):
-+        '''see issue 107.'''
-+        # note that ranges in regions start from 1
-+        p = self.samfile.pileup(region='chr1:170:172', truncate=True)
-+        columns = [ x.pos for x in p ]
-+        self.assertEqual( len(columns), 3)
-+        self.assertEqual( columns, [169,170,171] )
-+
-+        p = self.samfile.pileup( 'chr1', 169, 172, truncate=True)
-+        columns = [ x.pos for x in p ]
-+    
-+        self.assertEqual( len(columns), 3)
-+        self.assertEqual( columns, [169,170,171] )
-+
-+class TestAlignedReadFromBam(unittest.TestCase):
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex3.bam","rb" )
-+        self.reads=list(self.samfile.fetch())
-+
-+    def testARqname(self):
-+        self.assertEqual( self.reads[0].qname, "read_28833_29006_6945", "read name mismatch in read 1: %s != %s" % (self.reads[0].qname, "read_28833_29006_6945") )
-+        self.assertEqual( self.reads[1].qname, "read_28701_28881_323b", "read name mismatch in read 2: %s != %s" % (self.reads[1].qname, "read_28701_28881_323b") )
-+
-+    def testARflag(self):
-+        self.assertEqual( self.reads[0].flag, 99, "flag mismatch in read 1: %s != %s" % (self.reads[0].flag, 99) )
-+        self.assertEqual( self.reads[1].flag, 147, "flag mismatch in read 2: %s != %s" % (self.reads[1].flag, 147) )
-+
-+    def testARrname(self):
-+        self.assertEqual( self.reads[0].rname, 0, "chromosome/target id mismatch in read 1: %s != %s" % (self.reads[0].rname, 0) )
-+        self.assertEqual( self.reads[1].rname, 1, "chromosome/target id mismatch in read 2: %s != %s" % (self.reads[1].rname, 1) )
-+
-+    def testARpos(self):
-+        self.assertEqual( self.reads[0].pos, 33-1, "mapping position mismatch in read 1: %s != %s" % (self.reads[0].pos, 33-1) )
-+        self.assertEqual( self.reads[1].pos, 88-1, "mapping position mismatch in read 2: %s != %s" % (self.reads[1].pos, 88-1) )
-+
-+    def testARmapq(self):
-+        self.assertEqual( self.reads[0].mapq, 20, "mapping quality mismatch in read 1: %s != %s" % (self.reads[0].mapq, 20) )
-+        self.assertEqual( self.reads[1].mapq, 30, "mapping quality mismatch in read 2: %s != %s" % (self.reads[1].mapq, 30) )
-+
-+    def testARcigar(self):
-+        self.assertEqual( self.reads[0].cigar, [(0, 10), (2, 1), (0, 25)], "read name length mismatch in read 1: %s != %s" % (self.reads[0].cigar, [(0, 10), (2, 1), (0, 25)]) )
-+        self.assertEqual( self.reads[1].cigar, [(0, 35)], "read name length mismatch in read 2: %s != %s" % (self.reads[1].cigar, [(0, 35)]) )
-+
-+    def testARcigarstring(self):
-+        self.assertEqual( self.reads[0].cigarstring, '10M1D25M' )
-+        self.assertEqual( self.reads[1].cigarstring, '35M' )
-+
-+    def testARmrnm(self):
-+        self.assertEqual( self.reads[0].mrnm, 0, "mate reference sequence name mismatch in read 1: %s != %s" % (self.reads[0].mrnm, 0) )
-+        self.assertEqual( self.reads[1].mrnm, 1, "mate reference sequence name mismatch in read 2: %s != %s" % (self.reads[1].mrnm, 1) )
-+        self.assertEqual( self.reads[0].rnext, 0, "mate reference sequence name mismatch in read 1: %s != %s" % (self.reads[0].rnext, 0) )
-+        self.assertEqual( self.reads[1].rnext, 1, "mate reference sequence name mismatch in read 2: %s != %s" % (self.reads[1].rnext, 1) )
-+
-+    def testARmpos(self):
-+        self.assertEqual( self.reads[0].mpos, 200-1, "mate mapping position mismatch in read 1: %s != %s" % (self.reads[0].mpos, 200-1) )
-+        self.assertEqual( self.reads[1].mpos, 500-1, "mate mapping position mismatch in read 2: %s != %s" % (self.reads[1].mpos, 500-1) )
-+        self.assertEqual( self.reads[0].pnext, 200-1, "mate mapping position mismatch in read 1: %s != %s" % (self.reads[0].pnext, 200-1) )
-+        self.assertEqual( self.reads[1].pnext, 500-1, "mate mapping position mismatch in read 2: %s != %s" % (self.reads[1].pnext, 500-1) )
-+
-+    def testARisize(self):
-+        self.assertEqual( self.reads[0].isize, 167, "insert size mismatch in read 1: %s != %s" % (self.reads[0].isize, 167) )
-+        self.assertEqual( self.reads[1].isize, 412, "insert size mismatch in read 2: %s != %s" % (self.reads[1].isize, 412) )
-+        self.assertEqual( self.reads[0].tlen, 167, "insert size mismatch in read 1: %s != %s" % (self.reads[0].tlen, 167) )
-+        self.assertEqual( self.reads[1].tlen, 412, "insert size mismatch in read 2: %s != %s" % (self.reads[1].tlen, 412) )
-+
-+    def testARseq(self):
-+        self.assertEqual( self.reads[0].seq, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG", "sequence mismatch in read 1: %s != %s" % (self.reads[0].seq, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG") )
-+        self.assertEqual( self.reads[1].seq, b"ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA", "sequence size mismatch in read 2: %s != %s" % (self.reads[1].seq, b"ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA") )
-+        self.assertEqual( self.reads[3].seq, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG", "sequence mismatch in read 4: %s != %s" % (self.reads[3].seq, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG") )
-+
-+    def testARqual(self):
-+        self.assertEqual( self.reads[0].qual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<", "quality string mismatch in read 1: %s != %s" % (self.reads[0].qual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<") )
-+        self.assertEqual( self.reads[1].qual, b"<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<", "quality string mismatch in read 2: %s != %s" % (self.reads[1].qual, b"<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<") )
-+        self.assertEqual( self.reads[3].qual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<", "quality string mismatch in read 3: %s != %s" % (self.reads[3].qual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<") )
-+
-+    def testARquery(self):
-+        self.assertEqual( self.reads[0].query, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG", "query mismatch in read 1: %s != %s" % (self.reads[0].query, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG") )
-+        self.assertEqual( self.reads[1].query, b"ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA", "query size mismatch in read 2: %s != %s" % (self.reads[1].query, b"ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA") )
-+        self.assertEqual( self.reads[3].query, b"TAGCTAGCTACCTATATCTTGGTCTT", "query mismatch in read 4: %s != %s" % (self.reads[3].query, b"TAGCTAGCTACCTATATCTTGGTCTT") )
-+
-+    def testARqqual(self):
-+        self.assertEqual( self.reads[0].qqual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<", "qquality string mismatch in read 1: %s != %s" % (self.reads[0].qqual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<") )
-+        self.assertEqual( self.reads[1].qqual, b"<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<", "qquality string mismatch in read 2: %s != %s" % (self.reads[1].qqual, b"<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<") )
-+        self.assertEqual( self.reads[3].qqual, b"<<<<<<<<<<<<<<<<<:<9/,&,22", "qquality string mismatch in read 3: %s != %s" % (self.reads[3].qqual, b"<<<<<<<<<<<<<<<<<:<9/,&,22") )
-+
-+    def testPresentOptionalFields(self):
-+        self.assertEqual( self.reads[0].opt('NM'), 1, "optional field mismatch in read 1, NM: %s != %s" % (self.reads[0].opt('NM'), 1) )
-+        self.assertEqual( self.reads[0].opt('RG'), 'L1', "optional field mismatch in read 1, RG: %s != %s" % (self.reads[0].opt('RG'), 'L1') )
-+        self.assertEqual( self.reads[1].opt('RG'), 'L2', "optional field mismatch in read 2, RG: %s != %s" % (self.reads[1].opt('RG'), 'L2') )
-+        self.assertEqual( self.reads[1].opt('MF'), 18, "optional field mismatch in read 2, MF: %s != %s" % (self.reads[1].opt('MF'), 18) )
-+
-+    def testPairedBools(self):
-+        self.assertEqual( self.reads[0].is_paired, True, "is paired mismatch in read 1: %s != %s" % (self.reads[0].is_paired, True) )
-+        self.assertEqual( self.reads[1].is_paired, True, "is paired mismatch in read 2: %s != %s" % (self.reads[1].is_paired, True) )
-+        self.assertEqual( self.reads[0].is_proper_pair, True, "is proper pair mismatch in read 1: %s != %s" % (self.reads[0].is_proper_pair, True) )
-+        self.assertEqual( self.reads[1].is_proper_pair, True, "is proper pair mismatch in read 2: %s != %s" % (self.reads[1].is_proper_pair, True) )
-+
-+    def testTags( self ):
-+        self.assertEqual( self.reads[0].tags, 
-+                          [('NM', 1), ('RG', 'L1'), 
-+                           ('PG', 'P1'), ('XT', 'U')] )
-+        self.assertEqual( self.reads[1].tags, 
-+                          [('MF', 18), ('RG', 'L2'), 
-+                           ('PG', 'P2'),('XT', 'R') ] )
-+
-+    def testOpt( self ):
-+        self.assertEqual( self.reads[0].opt("XT"), "U" )
-+        self.assertEqual( self.reads[1].opt("XT"), "R" )
-+
-+    def testMissingOpt( self ):
-+        self.assertRaises( KeyError, self.reads[0].opt, "XP" )
-+
-+    def testEmptyOpt( self ):
-+        self.assertRaises( KeyError, self.reads[2].opt, "XT" )
-+
-+    def tearDown(self):
-+        self.samfile.close()
-+
-+class TestAlignedReadFromSam(TestAlignedReadFromBam):
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex3.sam","r" )
-+        self.reads=list(self.samfile.fetch())
-+
-+# needs to be implemented 
-+# class TestAlignedReadFromSamWithoutHeader(TestAlignedReadFromBam):
-+#
-+#     def setUp(self):
-+#         self.samfile=pysam.Samfile( "ex7.sam","r" )
-+#         self.reads=list(self.samfile.fetch())
-+
-+class TestHeaderSam(unittest.TestCase):
-+
-+    header = {'SQ': [{'LN': 1575, 'SN': 'chr1'}, 
-+                     {'LN': 1584, 'SN': 'chr2'}], 
-+              'RG': [{'LB': 'SC_1', 'ID': 'L1', 'SM': 'NA12891', 'PU': 'SC_1_10', "CN":"name:with:colon"}, 
-+                     {'LB': 'SC_2', 'ID': 'L2', 'SM': 'NA12891', 'PU': 'SC_2_12', "CN":"name:with:colon"}],
-+              'PG': [{'ID': 'P1', 'VN': '1.0'}, {'ID': 'P2', 'VN': '1.1'}], 
-+              'HD': {'VN': '1.0'},
-+              'CO' : [ 'this is a comment', 'this is another comment'],
-+              }
-+
-+    def compareHeaders( self, a, b ):
-+        '''compare two headers a and b.'''
-+        for ak,av in a.items():
-+            self.assertTrue( ak in b, "key '%s' not in '%s' " % (ak,b) )
-+            self.assertEqual( av, b[ak] )
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex3.sam","r" )
-+
-+    def testHeaders(self):
-+        self.compareHeaders( self.header, self.samfile.header )
-+        self.compareHeaders( self.samfile.header, self.header )
-+
-+    def testNameMapping( self ):
-+        for x, y in enumerate( ("chr1", "chr2")):
-+            tid = self.samfile.gettid( y )
-+            ref = self.samfile.getrname( x )
-+            self.assertEqual( tid, x )
-+            self.assertEqual( ref, y )
-+
-+        self.assertEqual( self.samfile.gettid("chr?"), -1 )
-+        self.assertRaises( ValueError, self.samfile.getrname, 2 )
-+
-+    def tearDown(self):
-+        self.samfile.close()
-+
-+class TestHeaderBam(TestHeaderSam):
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex3.bam","rb" )
-+
-+
-+class TestHeader1000Genomes( unittest.TestCase ):
-+
-+    # bamfile = "http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/technical/phase2b_alignment/data/NA07048/exome_alignment/NA07048.unmapped.ILLUMINA.bwa.CEU.exome.20120522_p2b.bam"
-+    bamfile = "http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/technical/phase3_EX_or_LC_only_alignment/data/HG00104/alignment/HG00104.chrom11.ILLUMINA.bwa.GBR.low_coverage.20130415.bam"
-+        
-+    def testRead( self ):
-+
-+	# Skip fetching files from web when building the package
-+        #f = pysam.Samfile( self.bamfile, "rb" )
-+        #data = f.header.copy()
-+        #self.assertTrue( data )
-+        self.assertTrue( True )
-+
-+class TestUnmappedReads(unittest.TestCase):
-+
-+    def testSAM(self):
-+        samfile=pysam.Samfile( "ex5.sam","r" )
-+        self.assertEqual( len(list(samfile.fetch( until_eof = True))), 2 ) 
-+        samfile.close()
-+
-+    def testBAM(self):
-+        samfile=pysam.Samfile( "ex5.bam","rb" )
-+        self.assertEqual( len(list(samfile.fetch( until_eof = True))), 2 ) 
-+        samfile.close()
-+
-+class TestPileupObjects(unittest.TestCase):
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex1.bam","rb" )
-+
-+    def testPileupColumn(self):
-+        for pcolumn1 in self.samfile.pileup( region="chr1:105" ):
-+            if pcolumn1.pos == 104:
-+                self.assertEqual( pcolumn1.tid, 0, "chromosome/target id mismatch in position 1: %s != %s" % (pcolumn1.tid, 0) )
-+                self.assertEqual( pcolumn1.pos, 105-1, "position mismatch in position 1: %s != %s" % (pcolumn1.pos, 105-1) )
-+                self.assertEqual( pcolumn1.n, 2, "# reads mismatch in position 1: %s != %s" % (pcolumn1.n, 2) )
-+        for pcolumn2 in self.samfile.pileup( region="chr2:1480" ):
-+            if pcolumn2.pos == 1479:
-+                self.assertEqual( pcolumn2.tid, 1, "chromosome/target id mismatch in position 1: %s != %s" % (pcolumn2.tid, 1) )
-+                self.assertEqual( pcolumn2.pos, 1480-1, "position mismatch in position 1: %s != %s" % (pcolumn2.pos, 1480-1) )
-+                self.assertEqual( pcolumn2.n, 12, "# reads mismatch in position 1: %s != %s" % (pcolumn2.n, 12) )
-+
-+    def testPileupRead(self):
-+        for pcolumn1 in self.samfile.pileup( region="chr1:105" ):
-+            if pcolumn1.pos == 104:
-+                self.assertEqual( len(pcolumn1.pileups), 2, "# reads aligned to column mismatch in position 1: %s != %s" % (len(pcolumn1.pileups), 2) )
-+#                self.assertEqual( pcolumn1.pileups[0]  # need to test additional properties here
-+
-+    def tearDown(self):
-+        self.samfile.close()
-+
-+    def testIteratorOutOfScope( self ):
-+        '''test if exception is raised if pileup col is accessed after iterator is exhausted.'''
-+
-+        for pileupcol in self.samfile.pileup():
-+            pass
-+        
-+        self.assertRaises( ValueError, getattr, pileupcol, "pileups" )
-+
-+class TestContextManager(unittest.TestCase):
-+
-+    def testManager( self ):
-+        with pysam.Samfile('ex1.bam', 'rb') as samfile:
-+            samfile.fetch()
-+        self.assertEqual( samfile._isOpen(), False )
-+
-+class TestExceptions(unittest.TestCase):
-+
-+    def setUp(self):
-+        self.samfile=pysam.Samfile( "ex1.bam","rb" )
-+
-+    def testMissingFile(self):
-+
-+        self.assertRaises( IOError, pysam.Samfile, "exdoesntexist.bam", "rb" )
-+        self.assertRaises( IOError, pysam.Samfile, "exdoesntexist.sam", "r" )
-+        self.assertRaises( IOError, pysam.Samfile, "exdoesntexist.bam", "r" )
-+        self.assertRaises( IOError, pysam.Samfile, "exdoesntexist.sam", "rb" )
-+
-+    def testBadContig(self):
-+        self.assertRaises( ValueError, self.samfile.fetch, "chr88" )
-+
-+    def testMeaninglessCrap(self):
-+        self.assertRaises( ValueError, self.samfile.fetch, "skljf" )
-+
-+    def testBackwardsOrderNewFormat(self):
-+        self.assertRaises( ValueError, self.samfile.fetch, 'chr1', 100, 10 )
-+
-+    def testBackwardsOrderOldFormat(self):
-+        self.assertRaises( ValueError, self.samfile.fetch, region="chr1:100-10")
-+        
-+    def testOutOfRangeNegativeNewFormat(self):
-+        self.assertRaises( ValueError, self.samfile.fetch, "chr1", 5, -10 )
-+        self.assertRaises( ValueError, self.samfile.fetch, "chr1", 5, 0 )
-+        self.assertRaises( ValueError, self.samfile.fetch, "chr1", -5, -10 )
-+
-+        self.assertRaises( ValueError, self.samfile.count, "chr1", 5, -10 )
-+        self.assertRaises( ValueError, self.samfile.count, "chr1", 5, 0 )        
-+        self.assertRaises( ValueError, self.samfile.count, "chr1", -5, -10 )
-+
-+    def testOutOfRangeNegativeOldFormat(self):
-+        self.assertRaises( ValueError, self.samfile.fetch, region="chr1:-5-10" )
-+        self.assertRaises( ValueError, self.samfile.fetch, region="chr1:-5-0" )
-+        self.assertRaises( ValueError, self.samfile.fetch, region="chr1:-5--10" )
-+
-+        self.assertRaises( ValueError, self.samfile.count, region="chr1:-5-10" )
-+        self.assertRaises( ValueError, self.samfile.count, region="chr1:-5-0" )
-+        self.assertRaises( ValueError, self.samfile.count, region="chr1:-5--10" )
-+
-+    def testOutOfRangNewFormat(self):
-+        self.assertRaises( ValueError, self.samfile.fetch, "chr1", 9999999999, 99999999999 )
-+        self.assertRaises( ValueError, self.samfile.count, "chr1", 9999999999, 99999999999 )
-+
-+    def testOutOfRangeLargeNewFormat(self):
-+        self.assertRaises( ValueError, self.samfile.fetch, "chr1", 9999999999999999999999999999999, 9999999999999999999999999999999999999999 )
-+        self.assertRaises( ValueError, self.samfile.count, "chr1", 9999999999999999999999999999999, 9999999999999999999999999999999999999999 )
-+
-+    def testOutOfRangeLargeOldFormat(self):
-+        self.assertRaises( ValueError, self.samfile.fetch, "chr1:99999999999999999-999999999999999999" )
-+        self.assertRaises( ValueError, self.samfile.count, "chr1:99999999999999999-999999999999999999" )
-+
-+    def testZeroToZero(self):        
-+        '''see issue 44'''
-+        self.assertEqual( len(list(self.samfile.fetch('chr1', 0, 0))), 0)
-+
-+    def tearDown(self):
-+        self.samfile.close()
-+
-+class TestWrongFormat(unittest.TestCase):
-+    '''test cases for opening files not in bam/sam format.'''
-+
-+    def testOpenSamAsBam( self ):
-+        self.assertRaises( ValueError, pysam.Samfile, 'ex1.sam', 'rb' )
-+
-+    def testOpenBamAsSam( self ):
-+        # test fails, needs to be implemented.
-+        # sam.fetch() fails on reading, not on opening
-+        # self.assertRaises( ValueError, pysam.Samfile, 'ex1.bam', 'r' )
-+        pass
-+
-+    def testOpenFastaAsSam( self ):
-+        # test fails, needs to be implemented.
-+        # sam.fetch() fails on reading, not on opening
-+        # self.assertRaises( ValueError, pysam.Samfile, 'ex1.fa', 'r' )
-+        pass
-+
-+    def testOpenFastaAsBam( self ):
-+        self.assertRaises( ValueError, pysam.Samfile, 'ex1.fa', 'rb' )
-+
-+class TestFastaFile(unittest.TestCase):
-+
-+    mSequences = { 'chr1' :
-+                       b"CACTAGTGGCTCATTGTAAATGTGTGGTTTAACTCGTCCATGGCCCAGCATTAGGGAGCTGTGGACCCTGCAGCCTGGCTGTGGGGGCCGCAGTGGCTGAGGGGTGCAGAGCCGAGTCACGGGGTTGCCAGCACAGGGGCTTAACCTCTGGTGACTGCCAGAGCTGCTGGCAAGCTAGAGTCCCATTTGGAGCCCCTCTAAGCCGTTCTATTTGTAATGAAAACTATATTTATGCTATTCAGTTCTAAATATAGAAATTGAAACAGCTGTGTTTAGTGCCTTTGTTCAACCCCCTTGCAACAACCTTGAGAACCCCAGGGAATTTGTCAATGTCAGGGAAGGAGCATTTTGTCAGTTACCAAATGTGTTTATTACCAGAGGGATGGAGGGAAGAGGGACGCTGAAGAACTTTGATGCCCTCTTCTTCCAAAGATGAAACGCGTAACTGCGCTCTCATTCACTCCAG [...]
-+                   'chr2' :
-+                       b"TTCAAATGAACTTCTGTAATTGAAAAATTCATTTAAGAAATTACAAAATATAGTTGAAAGCTCTAACAATAGACTAAACCAAGCAGAAGAAAGAGGTTCAGAACTTGAAGACAAGTCTCTTATGAATTAACCCAGTCAGACAAAAATAAAGAAAAAAATTTTAAAAATGAACAGAGCTTTCAAGAAGTATGAGATTATGTAAAGTAACTGAACCTATGAGTCACAGGTATTCCTGAGGAAAAAGAAAAAGTGAGAAGTTTGGAAAAACTATTTGAGGAAGTAATTGGGGAAAACCTCTTTAGTCTTGCTAGAGATTTAGACATCTAAATGAAAGAGGCTCAAAGAATGCCAGGAAGATACATTGCAAGACAGACTTCATCAAGATATGTAGTCATCAGACTATCTAAAGTCAACATGAAGGAAAAAAATTCTAAAATCAGCAAGAGAAAAGCATACAGTCATCTAT [...]
-+                   }
-+
-+    def setUp(self):
-+        self.file=pysam.Fastafile( "ex1.fa" )
-+
-+    def testFetch(self):
-+        for id, seq in list(self.mSequences.items()):
-+            self.assertEqual( seq, self.file.fetch( id ) )
-+            for x in range( 0, len(seq), 10):
-+                self.assertEqual( seq[x:x+10], self.file.fetch( id, x, x+10) )
-+                # test x:end
-+                self.assertEqual( seq[x:], self.file.fetch( id, x) )
-+                # test 0:x
-+                self.assertEqual( seq[:x], self.file.fetch( id, None, x) )
-+
-+        
-+        # unknown sequence returns ""
-+        self.assertEqual( b"", self.file.fetch("chr12") )
-+
-+    def testOutOfRangeAccess( self ):
-+        '''test out of range access.'''
-+        # out of range access returns an empty string
-+        for contig, s in self.mSequences.items():
-+            self.assertEqual( self.file.fetch( contig, len(s), len(s)+1), b"" )
-+
-+        self.assertEqual( self.file.fetch( "chr3", 0 , 100), b"" ) 
-+
-+    def testFetchErrors( self ):
-+        self.assertRaises( ValueError, self.file.fetch )
-+        self.assertRaises( IndexError, self.file.fetch, "chr1", -1, 10 )
-+        self.assertRaises( ValueError, self.file.fetch, "chr1", 20, 10 )
-+
-+    def testLength( self ):
-+        self.assertEqual( len(self.file), 2 )
-+        
-+    def tearDown(self):
-+        self.file.close()
-+
-+class TestAlignedRead(unittest.TestCase):
-+    '''tests to check if aligned read can be constructed
-+    and manipulated.
-+    '''
-+
-+    def checkFieldEqual( self, read1, read2, exclude = []):
-+        '''check if two reads are equal by comparing each field.'''
-+
-+        for x in ("qname", "seq", "flag",
-+                  "rname", "pos", "mapq", "cigar",
-+                  "mrnm", "mpos", "isize", "qual",
-+                  "is_paired", "is_proper_pair",
-+                  "is_unmapped", "mate_is_unmapped",
-+                  "is_reverse", "mate_is_reverse",
-+                  "is_read1", "is_read2",
-+                  "is_secondary", "is_qcfail",
-+                  "is_duplicate", "bin"):
-+            if x in exclude: continue
-+            self.assertEqual( getattr(read1, x), getattr(read2,x), "attribute mismatch for %s: %s != %s" % 
-+                              (x, getattr(read1, x), getattr(read2,x)))
-+    
-+    def testEmpty( self ):
-+        a = pysam.AlignedRead()
-+        self.assertEqual( a.qname, None )
-+        self.assertEqual( a.seq, None )
-+        self.assertEqual( a.qual, None )
-+        self.assertEqual( a.flag, 0 )
-+        self.assertEqual( a.rname, 0 )
-+        self.assertEqual( a.mapq, 0 )
-+        self.assertEqual( a.cigar, None )
-+        self.assertEqual( a.tags, [] )
-+        self.assertEqual( a.mrnm, 0 )
-+        self.assertEqual( a.mpos, 0 )
-+        self.assertEqual( a.isize, 0 )
-+
-+    def buildRead( self ):
-+        '''build an example read.'''
-+        
-+        a = pysam.AlignedRead()
-+        a.qname = "read_12345"
-+        a.seq="ACGT" * 10
-+        a.flag = 0
-+        a.rname = 0
-+        a.pos = 20
-+        a.mapq = 20
-+        a.cigar = ( (0,10), (2,1), (0,9), (1,1), (0,20) )
-+        a.mrnm = 0
-+        a.mpos=200
-+        a.isize=167
-+        a.qual="1234" * 10
-+        # todo: create tags
-+        return a
-+
-+    def testUpdate( self ):
-+        '''check if updating fields affects other variable length data
-+        '''
-+        a = self.buildRead()
-+        b = self.buildRead()
-+
-+        # check qname
-+        b.qname = "read_123"
-+        self.checkFieldEqual( a, b, "qname" )
-+        b.qname = "read_12345678"
-+        self.checkFieldEqual( a, b, "qname" )
-+        b.qname = "read_12345"
-+        self.checkFieldEqual( a, b)
-+
-+        # check cigar
-+        b.cigar = ( (0,10), )
-+        self.checkFieldEqual( a, b, "cigar" )
-+        b.cigar = ( (0,10), (2,1), (0,10) )
-+        self.checkFieldEqual( a, b, "cigar" )
-+        b.cigar = ( (0,10), (2,1), (0,9), (1,1), (0,20) )
-+        self.checkFieldEqual( a, b)
-+
-+        # check seq 
-+        b.seq = "ACGT"
-+        self.checkFieldEqual( a, b, ("seq", "qual") )
-+        b.seq = "ACGT" * 3
-+        self.checkFieldEqual( a, b, ("seq", "qual") )
-+        b.seq = "ACGT" * 10
-+        self.checkFieldEqual( a, b, ("qual",))
-+
-+        # reset qual
-+        b = self.buildRead()
-+
-+        # check flags:
-+        for x in (
-+            "is_paired", "is_proper_pair",
-+            "is_unmapped", "mate_is_unmapped",
-+            "is_reverse", "mate_is_reverse",
-+            "is_read1", "is_read2",
-+            "is_secondary", "is_qcfail",
-+            "is_duplicate"):
-+            setattr( b, x, True )
-+            self.assertEqual( getattr(b, x), True )
-+            self.checkFieldEqual( a, b, ("flag", x,) )
-+            setattr( b, x, False )
-+            self.assertEqual( getattr(b, x), False )
-+            self.checkFieldEqual( a, b )
-+
-+    def testLargeRead( self ):
-+        '''build an example read.'''
-+        
-+        a = pysam.AlignedRead()
-+        a.qname = "read_12345"
-+        a.seq="ACGT" * 200
-+        a.flag = 0
-+        a.rname = 0
-+        a.pos = 20
-+        a.mapq = 20
-+        a.cigar = ( (0, 4 * 200), )
-+        a.mrnm = 0
-+        a.mpos=200
-+        a.isize=167
-+        a.qual="1234" * 200
-+
-+        return a
-+
-+    def testTagParsing( self ):
-+        '''test for tag parsing
-+
-+        see http://groups.google.com/group/pysam-user-group/browse_thread/thread/67ca204059ea465a
-+        '''
-+        samfile=pysam.Samfile( "ex8.bam","rb" )
-+
-+        for entry in samfile:
-+            before = entry.tags
-+            entry.tags = entry.tags
-+            after = entry.tags
-+            self.assertEqual( after, before )
-+
-+    def testUpdateTlen( self ):
-+        '''check if updating tlen works'''
-+        a = self.buildRead()
-+        oldlen = a.tlen
-+        oldlen *= 2
-+        a.tlen = oldlen
-+        self.assertEqual( a.tlen, oldlen )
-+
-+    def testPositions( self ):
-+        a = self.buildRead()
-+        self.assertEqual( a.positions,
-+                          [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
-+                                31, 32, 33, 34, 35, 36, 37, 38, 39, 
-+                           40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
-+                           50, 51, 52, 53, 54, 55, 56, 57, 58, 59] )
-+
-+        self.assertEqual( a.aligned_pairs,
-+                          [(0, 20), (1, 21), (2, 22), (3, 23), (4, 24), 
-+                           (5, 25), (6, 26), (7, 27), (8, 28), (9, 29), 
-+                           (None, 30), 
-+                           (10, 31), (11, 32), (12, 33), (13, 34), (14, 35), 
-+                           (15, 36), (16, 37), (17, 38), (18, 39), (19, None), 
-+                           (20, 40), (21, 41), (22, 42), (23, 43), (24, 44), 
-+                           (25, 45), (26, 46), (27, 47), (28, 48), (29, 49), 
-+                           (30, 50), (31, 51), (32, 52), (33, 53), (34, 54), 
-+                           (35, 55), (36, 56), (37, 57), (38, 58), (39, 59)] )
-+
-+        self.assertEqual( a.positions, [x[1] for x in a.aligned_pairs if x[0] != None and x[1] != None] )
-+        # alen is the length of the aligned read in genome
-+        self.assertEqual( a.alen, a.aligned_pairs[-1][0] + 1 )
-+        # aend points to one beyond last aligned base in ref
-+        self.assertEqual( a.positions[-1], a.aend - 1 )
-+
-+class TestDeNovoConstruction(unittest.TestCase):
-+    '''check BAM/SAM file construction using ex6.sam
-+    
-+    (note these are +1 coordinates):
-+    
-+    read_28833_29006_6945	99	chr1	33	20	10M1D25M	=	200	167	AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG	<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<	NM:i:1	RG:Z:L1
-+    read_28701_28881_323b	147	chr2	88	30	35M	=	500	412	ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA	<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<	MF:i:18	RG:Z:L2
-+    '''
-+
-+    header = { 'HD': {'VN': '1.0'},
-+               'SQ': [{'LN': 1575, 'SN': 'chr1'}, 
-+                      {'LN': 1584, 'SN': 'chr2'}], }
-+
-+    bamfile = "ex6.bam"
-+    samfile = "ex6.sam"
-+
-+    def checkFieldEqual( self, read1, read2, exclude = []):
-+        '''check if two reads are equal by comparing each field.'''
-+
-+        for x in ("qname", "seq", "flag",
-+                  "rname", "pos", "mapq", "cigar",
-+                  "mrnm", "mpos", "isize", "qual",
-+                  "bin",
-+                  "is_paired", "is_proper_pair",
-+                  "is_unmapped", "mate_is_unmapped",
-+                  "is_reverse", "mate_is_reverse",
-+                  "is_read1", "is_read2",
-+                  "is_secondary", "is_qcfail",
-+                  "is_duplicate"):
-+            if x in exclude: continue
-+            self.assertEqual( getattr(read1, x), getattr(read2,x), "attribute mismatch for %s: %s != %s" % 
-+                              (x, getattr(read1, x), getattr(read2,x)))
-+
-+    def setUp( self ):
-+
-+        a = pysam.AlignedRead()
-+        a.qname = "read_28833_29006_6945"
-+        a.seq="AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG"
-+        a.flag = 99
-+        a.rname = 0
-+        a.pos = 32
-+        a.mapq = 20
-+        a.cigar = ( (0,10), (2,1), (0,25) )
-+        a.mrnm = 0
-+        a.mpos=199
-+        a.isize=167
-+        a.qual="<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<"
-+        a.tags = ( ("NM", 1),
-+                   ("RG", "L1") )
-+
-+        b = pysam.AlignedRead()
-+        b.qname = "read_28701_28881_323b"
-+        b.seq="ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA"
-+        b.flag = 147
-+        b.rname = 1
-+        b.pos = 87
-+        b.mapq = 30
-+        b.cigar = ( (0,35), )
-+        b.mrnm = 1
-+        b.mpos=499
-+        b.isize=412
-+        b.qual="<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<"
-+        b.tags = ( ("MF", 18),
-+                   ("RG", "L2") )
-+
-+        self.reads = (a,b)
-+
-+    def testSAMWholeFile( self ):
-+        
-+        tmpfilename = "tmp_%i.sam" % id(self)
-+
-+        outfile = pysam.Samfile( tmpfilename, "wh", header = self.header )
-+
-+        for x in self.reads: outfile.write( x )
-+        outfile.close()
-+        self.assertTrue( checkBinaryEqual( tmpfilename, self.samfile ),
-+                         "mismatch when construction SAM file, see %s %s" % (tmpfilename, self.samfile))
-+        
-+        os.unlink( tmpfilename )
-+
-+    def testBAMPerRead( self ):
-+        '''check if individual reads are binary equal.'''
-+        infile = pysam.Samfile( self.bamfile, "rb")
-+
-+        others = list(infile)
-+        for denovo, other in zip( others, self.reads):
-+            self.checkFieldEqual( other, denovo )
-+            self.assertEqual( other.compare( denovo ), 0 )
-+
-+    def testSAMPerRead( self ):
-+        '''check if individual reads are binary equal.'''
-+        infile = pysam.Samfile( self.samfile, "r")
-+
-+        others = list(infile)
-+        for denovo, other in zip( others, self.reads):
-+            self.checkFieldEqual( other, denovo )
-+            self.assertEqual( other.compare( denovo), 0 )
-+            
-+    def testBAMWholeFile( self ):
-+        
-+        tmpfilename = "tmp_%i.bam" % id(self)
-+
-+        outfile = pysam.Samfile( tmpfilename, "wb", header = self.header )
-+
-+        for x in self.reads: outfile.write( x )
-+        outfile.close()
-+        
-+        self.assertTrue( checkBinaryEqual( tmpfilename, self.bamfile ),
-+                         "mismatch when construction BAM file, see %s %s" % (tmpfilename, self.bamfile))
-+        
-+        os.unlink( tmpfilename )
-+
-+class TestDeNovoConstructionUserTags(TestDeNovoConstruction):
-+    '''test de novo construction with a header that contains lower-case tags.'''
-+
-+    header = { 'HD': {'VN': '1.0'},
-+               'SQ': [{'LN': 1575, 'SN': 'chr1'}, 
-+                      {'LN': 1584, 'SN': 'chr2'}],
-+               'x1': {'A': 2, 'B': 5 },
-+               'x3': {'A': 6, 'B': 5 },
-+               'x2': {'A': 4, 'B': 5 } }
-+
-+    bamfile = "example_user_header.bam"
-+    samfile = "example_user_header.sam"
-+
-+class TestEmptyHeader( unittest.TestCase ):
-+    '''see issue 84.'''
-+    
-+    def testEmptyHeader( self ):
-+
-+        s = pysam.Samfile('example_empty_header.bam')
-+        self.assertEqual( s.header, {'SQ': [{'LN': 1000, 'SN': 'chr1'}]} )
-+
-+class TestBTagSam( unittest.TestCase ):
-+    '''see issue 81.'''
-+    
-+    compare = [ [100, 1, 91, 0, 7, 101, 0, 201, 96, 204, 0, 0, 87, 109, 0, 7, 97, 112, 1, 12, 78, 197, 0, 7, 100, 95, 101, 202, 0, 6, 0, 1, 186, 0, 84, 0, 244, 0, 0, 324, 0, 107, 195, 101, 113, 0, 102, 0, 104, 3, 0, 101, 1, 0, 212, 6, 0, 0, 1, 0, 74, 1, 11, 0, 196, 2, 197, 103, 0, 108, 98, 2, 7, 0, 1, 2, 194, 0, 180, 0, 108, 0, 203, 104, 16, 5, 205, 0, 0, 0, 1, 1, 100, 98, 0, 0, 204, 6, 0, 79, 0, 0, 101, 7, 109, 90, 265, 1, 27, 10, 109, 102, 9, 0, 292, 0, 110, 0, 0, 102, 112, 0, 0, 84,  [...]
-+                [-100,200,-300,-400],
-+                [-100,12],
-+                [12,15],
-+                [-1.0,5.0,2.5] ]
-+            
-+    filename = 'example_btag.sam'
-+
-+    def testRead( self ):
-+
-+        s = pysam.Samfile(self.filename)
-+        for x, read in enumerate(s):
-+            if x == 0:
-+                self.assertEqual( read.tags, [('RG', 'QW85I'), ('PG', 'tmap'), ('MD', '140'), ('NM', 0), ('AS', 140), ('FZ', [100, 1, 91, 0, 7, 101, 0, 201, 96, 204, 0, 0, 87, 109, 0, 7, 97, 112, 1, 12, 78, 197, 0, 7, 100, 95, 101, 202, 0, 6, 0, 1, 186, 0, 84, 0, 244, 0, 0, 324, 0, 107, 195, 101, 113, 0, 102, 0, 104, 3, 0, 101, 1, 0, 212, 6, 0, 0, 1, 0, 74, 1, 11, 0, 196, 2, 197, 103, 0, 108, 98, 2, 7, 0, 1, 2, 194, 0, 180, 0, 108, 0, 203, 104, 16, 5, 205, 0, 0, 0, 1, 1, 100, 98, 0, 0,  [...]
-+                                  )
-+                         
-+            fz = dict(read.tags)["FZ"]
-+            self.assertEqual( fz, self.compare[x] )
-+            self.assertEqual( read.opt("FZ"), self.compare[x])
-+
-+    def testWrite( self ):
-+        
-+        s = pysam.Samfile(self.filename)
-+        for read in s:
-+            before = read.tags
-+            read.tags = read.tags
-+            after = read.tags
-+            self.assertEqual( after, before )
-+
-+class TestBTagBam( TestBTagSam ):
-+    filename = 'example_btag.bam'
-+
-+class TestDoubleFetch(unittest.TestCase):
-+    '''check if two iterators on the same bamfile are independent.'''
-+    
-+    def testDoubleFetch( self ):
-+
-+        samfile1 = pysam.Samfile('ex1.bam', 'rb')
-+
-+        for a,b in zip(samfile1.fetch(), samfile1.fetch()):
-+            self.assertEqual( a.compare( b ), 0 )
-+
-+    def testDoubleFetchWithRegion( self ):
-+
-+        samfile1 = pysam.Samfile('ex1.bam', 'rb')
-+        chr, start, stop = 'chr1', 200, 3000000
-+        self.assertTrue(len(list(samfile1.fetch ( chr, start, stop))) > 0) #just making sure the test has something to catch
-+
-+        for a,b in zip(samfile1.fetch( chr, start, stop), samfile1.fetch( chr, start, stop)):
-+            self.assertEqual( a.compare( b ), 0 ) 
-+
-+    def testDoubleFetchUntilEOF( self ):
-+
-+        samfile1 = pysam.Samfile('ex1.bam', 'rb')
-+
-+        for a,b in zip(samfile1.fetch( until_eof = True), 
-+                       samfile1.fetch( until_eof = True )):
-+            self.assertEqual( a.compare( b), 0 )
-+
-+class TestRemoteFileFTP(unittest.TestCase):
-+    '''test remote access.
-+
-+    '''
-+
-+    # Need to find an ftp server without password on standard
-+    # port.
-+
-+    url = "ftp://ftp.sanger.ac.uk/pub/rd/humanSequences/CV.bam"
-+    region = "1:1-1000"
-+
-+    def testFTPView( self ):
-+        return
-+        result = pysam.view( self.url, self.region )
-+        self.assertEqual( len(result), 36 )
-+        
-+    def testFTPFetch( self ):
-+        return
-+        samfile = pysam.Samfile(self.url, "rb")  
-+        result = list(samfile.fetch( region = self.region ))
-+        self.assertEqual( len(result), 36 )
-+
-+class TestLargeOptValues( unittest.TestCase ):
-+
-+    ints = ( 65536, 214748, 2147484, 2147483647 )
-+    floats = ( 65536.0, 214748.0, 2147484.0 )
-+
-+    def check( self, samfile ):
-+        
-+        i = samfile.fetch()
-+        for exp in self.ints:
-+            rr = next(i)
-+            obs = rr.opt("ZP")
-+            self.assertEqual( exp, obs, "expected %s, got %s\n%s" % (str(exp), str(obs), str(rr)))
-+
-+        for exp in [ -x for x in self.ints ]:
-+            rr = next(i)
-+            obs = rr.opt("ZP")
-+            self.assertEqual( exp, obs, "expected %s, got %s\n%s" % (str(exp), str(obs), str(rr)))
-+
-+        for exp in self.floats:
-+            rr = next(i)
-+            obs = rr.opt("ZP")
-+            self.assertEqual( exp, obs, "expected %s, got %s\n%s" % (str(exp), str(obs), str(rr)))
-+
-+        for exp in [ -x for x in self.floats ]:
-+            rr = next(i)
-+            obs = rr.opt("ZP")
-+            self.assertEqual( exp, obs, "expected %s, got %s\n%s" % (str(exp), str(obs), str(rr)))
-+
-+    def testSAM( self ):
-+        samfile = pysam.Samfile("ex10.sam", "r")
-+        self.check( samfile )
-+
-+    def testBAM( self ):
-+        samfile = pysam.Samfile("ex10.bam", "rb")
-+        self.check( samfile )
-+
-+# class TestSNPCalls( unittest.TestCase ):
-+#     '''test pysam SNP calling ability.'''
-+
-+#     def checkEqual( self, a, b ):
-+#         for x in ("reference_base", 
-+#                   "pos",
-+#                   "genotype",
-+#                   "consensus_quality",
-+#                   "snp_quality",
-+#                   "mapping_quality",
-+#                   "coverage" ):
-+#             self.assertEqual( getattr(a, x), getattr(b,x), "%s mismatch: %s != %s\n%s\n%s" % 
-+#                               (x, getattr(a, x), getattr(b,x), str(a), str(b)))
-+
-+#     def testAllPositionsViaIterator( self ):
-+#         samfile = pysam.Samfile( "ex1.bam", "rb")  
-+#         fastafile = pysam.Fastafile( "ex1.fa" )
-+#         try: 
-+#             refs = [ x for x in pysam.pileup( "-c", "-f", "ex1.fa", "ex1.bam" ) if x.reference_base != "*"]
-+#         except pysam.SamtoolsError:
-+#             pass
-+
-+#         i = samfile.pileup( stepper = "samtools", fastafile = fastafile )
-+#         calls = list(pysam.IteratorSNPCalls(i))
-+#         for x,y in zip( refs, calls ):
-+#             self.checkEqual( x, y )
-+
-+#     def testPerPositionViaIterator( self ):
-+#         # test pileup for each position. This is a slow operation
-+#         # so this test is disabled 
-+#         return
-+#         samfile = pysam.Samfile( "ex1.bam", "rb")  
-+#         fastafile = pysam.Fastafile( "ex1.fa" )
-+#         for x in pysam.pileup( "-c", "-f", "ex1.fa", "ex1.bam" ):
-+#             if x.reference_base == "*": continue
-+#             i = samfile.pileup( x.chromosome, x.pos, x.pos+1,
-+#                                 fastafile = fastafile,
-+#                                 stepper = "samtools" )
-+#             z = [ zz for zz in pysam.IteratorSamtools(i) if zz.pos == x.pos ]
-+#             self.assertEqual( len(z), 1 )
-+#             self.checkEqual( x, z[0] )
-+
-+#     def testPerPositionViaCaller( self ):
-+#         # test pileup for each position. This is a fast operation
-+#         samfile = pysam.Samfile( "ex1.bam", "rb")  
-+#         fastafile = pysam.Fastafile( "ex1.fa" )
-+#         i = samfile.pileup( stepper = "samtools", fastafile = fastafile )
-+#         caller = pysam.SNPCaller( i )
-+
-+#         for x in pysam.pileup( "-c", "-f", "ex1.fa", "ex1.bam" ):
-+#             if x.reference_base == "*": continue
-+#             call = caller.call( x.chromosome, x.pos )
-+#             self.checkEqual( x, call )
-+
-+# class TestIndelCalls( unittest.TestCase ):
-+#     '''test pysam indel calling.'''
-+
-+#     def checkEqual( self, a, b ):
-+
-+#         for x in ("pos",
-+#                   "genotype",
-+#                   "consensus_quality",
-+#                   "snp_quality",
-+#                   "mapping_quality",
-+#                   "coverage",
-+#                   "first_allele",
-+#                   "second_allele",
-+#                   "reads_first",
-+#                   "reads_second",
-+#                   "reads_diff"):
-+#             if b.genotype == "*/*" and x == "second_allele":
-+#                 # ignore test for second allele (positions chr2:439 and chr2:1512)
-+#                 continue
-+#             self.assertEqual( getattr(a, x), getattr(b,x), "%s mismatch: %s != %s\n%s\n%s" % 
-+#                               (x, getattr(a, x), getattr(b,x), str(a), str(b)))
-+
-+#     def testAllPositionsViaIterator( self ):
-+
-+#         samfile = pysam.Samfile( "ex1.bam", "rb")  
-+#         fastafile = pysam.Fastafile( "ex1.fa" )
-+#         try: 
-+#             refs = [ x for x in pysam.pileup( "-c", "-f", "ex1.fa", "ex1.bam" ) if x.reference_base == "*"]
-+#         except pysam.SamtoolsError:
-+#             pass
-+
-+#         i = samfile.pileup( stepper = "samtools", fastafile = fastafile )
-+#         calls = [ x for x in list(pysam.IteratorIndelCalls(i)) if x != None ]
-+#         for x,y in zip( refs, calls ):
-+#             self.checkEqual( x, y )
-+
-+#     def testPerPositionViaCaller( self ):
-+#         # test pileup for each position. This is a fast operation
-+#         samfile = pysam.Samfile( "ex1.bam", "rb")  
-+#         fastafile = pysam.Fastafile( "ex1.fa" )
-+#         i = samfile.pileup( stepper = "samtools", fastafile = fastafile )
-+#         caller = pysam.IndelCaller( i )
-+
-+#         for x in pysam.pileup( "-c", "-f", "ex1.fa", "ex1.bam" ):
-+#             if x.reference_base != "*": continue
-+#             call = caller.call( x.chromosome, x.pos )
-+#             self.checkEqual( x, call )
-+
-+class TestLogging( unittest.TestCase ):
-+    '''test around bug issue 42,
-+
-+    failed in versions < 0.4
-+    '''
-+
-+    def check( self, bamfile, log ):
-+
-+        if log:
-+            logger = logging.getLogger('franklin')
-+            logger.setLevel(logging.INFO)
-+            formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
-+            log_hand  = logging.FileHandler('log.txt')
-+            log_hand.setFormatter(formatter)
-+            logger.addHandler(log_hand)
-+
-+        bam  = pysam.Samfile(bamfile, 'rb')
-+        cols = bam.pileup()
-+        self.assertTrue( True )
-+
-+    def testFail1( self ):
-+        self.check( "ex9_fail.bam", False )
-+        self.check( "ex9_fail.bam", True )
-+
-+    def testNoFail1( self ):
-+        self.check( "ex9_nofail.bam", False )
-+        self.check( "ex9_nofail.bam", True )
-+
-+    def testNoFail2( self ):
-+        self.check( "ex9_nofail.bam", True )
-+        self.check( "ex9_nofail.bam", True )
-+        
-+# TODOS
-+# 1. finish testing all properties within pileup objects
-+# 2. check exceptions and bad input problems (missing files, optional fields that aren't present, etc...)
-+# 3. check: presence of sequence
-+
-+class TestSamfileUtilityFunctions( unittest.TestCase ):
-+
-+    def testCount( self ):
-+
-+        samfile = pysam.Samfile( "ex1.bam", "rb" )
-+
-+        for contig in ("chr1", "chr2" ):
-+            for start in range( 0, 2000, 100 ):
-+                end = start + 1
-+                self.assertEqual( len( list( samfile.fetch( contig, start, end ) ) ),
-+                                  samfile.count( contig, start, end ) )
-+
-+                # test empty intervals
-+                self.assertEqual( len( list( samfile.fetch( contig, start, start ) ) ),
-+                                  samfile.count( contig, start, start ) )
-+
-+                # test half empty intervals
-+                self.assertEqual( len( list( samfile.fetch( contig, start ) ) ),
-+                                  samfile.count( contig, start ) )
-+
-+    def testMate( self ):
-+        '''test mate access.'''
-+
-+        with open( "ex1.sam", "rb" ) as inf:
-+            readnames = [ x.split(b"\t")[0] for x in inf.readlines() ]
-+        if sys.version_info[0] >= 3:
-+            readnames = [ name.decode('ascii') for name in readnames ]
-+            
-+        counts = collections.defaultdict( int )
-+        for x in readnames: counts[x] += 1
-+
-+        samfile = pysam.Samfile( "ex1.bam", "rb" )
-+        for read in samfile.fetch():
-+            if not read.is_paired:
-+                self.assertRaises( ValueError, samfile.mate, read )
-+            elif read.mate_is_unmapped:
-+                self.assertRaises( ValueError, samfile.mate, read )
-+            else:
-+                if counts[read.qname] == 1:
-+                    self.assertRaises( ValueError, samfile.mate, read )
-+                else:
-+                    mate = samfile.mate( read )
-+                    self.assertEqual( read.qname, mate.qname )
-+                    self.assertEqual( read.is_read1, mate.is_read2 )
-+                    self.assertEqual( read.is_read2, mate.is_read1 )
-+                    self.assertEqual( read.pos, mate.mpos )
-+                    self.assertEqual( read.mpos, mate.pos )
-+
-+    def testIndexStats( self ):
-+        '''test if total number of mapped/unmapped reads is correct.'''
-+
-+        samfile = pysam.Samfile( "ex1.bam", "rb" )
-+        self.assertEqual( samfile.mapped, 3235 )
-+        self.assertEqual( samfile.unmapped, 35 )
-+
-+class TestSamtoolsProxy( unittest.TestCase ):
-+    '''tests for sanity checking access to samtools functions.'''
-+
-+    def testIndex( self ):
-+        self.assertRaises( IOError, pysam.index, "missing_file" )
-+
-+    def testView( self ):
-+        # note that view still echos "open: No such file or directory"
-+        self.assertRaises( pysam.SamtoolsError, pysam.view, "missing_file" )
-+
-+    def testSort( self ):
-+        self.assertRaises( pysam.SamtoolsError, pysam.sort, "missing_file" )
-+
-+class TestSamfileIndex( unittest.TestCase):
-+    
-+    def testIndex( self ):
-+        samfile = pysam.Samfile( "ex1.bam", "rb" )
-+        index = pysam.IndexedReads( samfile )
-+        index.build()
-+
-+        reads = collections.defaultdict( int )
-+
-+        for read in samfile: reads[read.qname] += 1
-+            
-+        for qname, counts in reads.items():
-+            found = list(index.find( qname ))
-+            self.assertEqual( len(found), counts )
-+            for x in found: self.assertEqual( x.qname, qname )
-+            
-+
-+if __name__ == "__main__":
-+    # build data files
-+    print ("building data files")
-+    subprocess.call( "make", shell=True)
-+    print ("starting tests")
-+    unittest.main()
-+    print ("completed tests")
diff --git a/debian/patches/series b/debian/patches/series
deleted file mode 100644
index 797745b..0000000
--- a/debian/patches/series
+++ /dev/null
@@ -1,4 +0,0 @@
-# fix_cleanup_tests.patch
-do_not_use_distribute_setup.patch
-# offline-tests.patch
-use_external_htslib.patch
diff --git a/debian/patches/use_external_htslib.patch b/debian/patches/use_external_htslib.patch
deleted file mode 100644
index 31bd62f..0000000
--- a/debian/patches/use_external_htslib.patch
+++ /dev/null
@@ -1,23 +0,0 @@
-Author: Andreas Tille <tille at debian.org>
-Date: Tue, 19 Aug 2014 21:26:37 +0200
-Description: setup.py allows to use external htslib which we do hereby
-
---- a/setup.py
-+++ b/setup.py
-@@ -32,7 +32,7 @@ IS_PYTHON3 = sys.version_info[0] >= 3
- #         pysam. 
- # external: use shared libhts.so compiled outside of 
- #           pysam
--HTSLIB = "separate"
-+HTSLIB = "external"
- HTSLIB_DIR = []
- 
- # collect pysam version
-@@ -55,6 +55,7 @@ tabix_dest = os.path.abspath("tabix")
- if HTSLIB == 'external':
-     htslib_sources = []
-     chtslib_sources = []
-+    shared_htslib_sources = htslib_sources
-     htslib_library_dirs = HTSLIB_DIR
-     htslib_libraries = ['hts']
- elif HTSLIB == 'separate':
diff --git a/debian/python-pysam-tests.README.Debian b/debian/python-pysam-tests.README.Debian
deleted file mode 100644
index ed86b39..0000000
--- a/debian/python-pysam-tests.README.Debian
+++ /dev/null
@@ -1,12 +0,0 @@
-Pysam for Debian
-================
-
-To verify whether your python-pysam modules are working correctly
-you can run the test suite manually by running the script
-
-    run-unit-test
-
-in this directory.
-
- -- Andreas Tille <tille at debian.org>  Fri, 07 Feb 2014 18:29:40 +0100
-
diff --git a/debian/python-pysam-tests.docs b/debian/python-pysam-tests.docs
deleted file mode 100644
index 760739f..0000000
--- a/debian/python-pysam-tests.docs
+++ /dev/null
@@ -1 +0,0 @@
-debian/tests/run-unit-test
diff --git a/debian/python-pysam-tests.install b/debian/python-pysam-tests.install
deleted file mode 100644
index a99a578..0000000
--- a/debian/python-pysam-tests.install
+++ /dev/null
@@ -1 +0,0 @@
-tests	usr/share/doc/python-pysam
diff --git a/debian/rules b/debian/rules
deleted file mode 100755
index 550e772..0000000
--- a/debian/rules
+++ /dev/null
@@ -1,49 +0,0 @@
-#!/usr/bin/make -f
-
-export PYBUILD_NAME=pysam
-
-DEBPKGNAME := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}')
-TESTPKG    := $(DEBPKGNAME)-tests
-
-HTSLIBDIR  := /usr/lib/$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
-
-# DEB_BUILD_OPTIONS := nocheck
-
-%:
-	dh $@ --with python2,python3 --buildsystem=pybuild
-
-# Make sure Cython is recreating some c-files.  To enable building twice in a
-# row these will be saved in advance and restored afterwards
-debian/savefiles:
-	if grep -q -l "Generated by Cython" pysam/*.c ; then \
-	    mkdir -p debian/savefiles ; \
-	    mv `grep -l "Generated by Cython" pysam/*.c` debian/savefiles ; \
-	fi
-
-override_dh_clean:
-	dh_clean
-	# restore cython generated files
-	if [ -d debian/savefiles ] ; then \
-	    mv debian/savefiles/* pysam ; \
-	    rm -rf debian/savefiles ; \
-	fi
-
-override_dh_auto_build: debian/savefiles
-	HTSLIB_LIBRARY_DIR=$(HTSLIBDIR) HTSLIB_INCLUDE_DIR=/usr/include dh_auto_build
-
-override_dh_auto_test:
-ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
-	LC_ALL=C.UTF-8 dh_auto_test -- --test --system=custom \
-		--test-args='set -e; \
-			     cp -a $(CURDIR)/tests {build_dir}/tests ; \
-			     cd {build_dir}/tests && HTSLIB_LIBRARY_DIR=$(HTSLIBDIR) HTSLIB_INCLUDE_DIR=/usr/include PYTHONPATH={build_dir} {interpreter} ./pysam_test.py \
-			                          && HTSLIB_LIBRARY_DIR=$(HTSLIBDIR) HTSLIB_INCLUDE_DIR=/usr/include PYTHONPATH={build_dir} {interpreter} ./tabix_test.py '
-endif
-
-override_dh_install-indep:
-	dh_install -p $(TESTPKG)
-	cd debian/$(TESTPKG)/usr/share/doc/python-pysam/tests; \
-	    make clean; \
-	    rm -f log.txt ; \
-	    chmod a+x tabix_test.py
-
diff --git a/debian/source/format b/debian/source/format
deleted file mode 100644
index 163aaf8..0000000
--- a/debian/source/format
+++ /dev/null
@@ -1 +0,0 @@
-3.0 (quilt)
diff --git a/debian/tests/control b/debian/tests/control
deleted file mode 100644
index f1a4bf7..0000000
--- a/debian/tests/control
+++ /dev/null
@@ -1,3 +0,0 @@
-Tests: run-unit-test
-Depends: @, python-pysam-tests, samtools, make
-Restrictions: allow-stderr
diff --git a/debian/tests/run-unit-test b/debian/tests/run-unit-test
deleted file mode 100644
index 44e94b4..0000000
--- a/debian/tests/run-unit-test
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/sh -e
-
-if [ "$ADTTMP" = "" ] ; then
-  ADTTMP=`mktemp -d /tmp/python-pysam-test.XXXXXX`
-fi
-cd $ADTTMP
-cp -a /usr/share/doc/python-pysam/tests/* $ADTTMP
-gunzip -r *.py.gz \
-        ex9_fail.bam.gz \
-        ex9_nofail.bam.gz \
-        example_empty_header.bam.gz \
-        issue100.bam.gz
-chmod u+x ./pysam_test_offline.py
-./pysam_test_offline.py
diff --git a/debian/watch b/debian/watch
deleted file mode 100644
index d964da4..0000000
--- a/debian/watch
+++ /dev/null
@@ -1,3 +0,0 @@
-version=3
-
-https://pypi.python.org/packages/source/p/pysam/pysam-(\d+.*)\.tar\.gz
diff --git a/doc/api.rst b/doc/api.rst
index dbb6ecb..38b7b7e 100644
--- a/doc/api.rst
+++ b/doc/api.rst
@@ -2,23 +2,29 @@
 pysam - An interface for reading and writing SAM files
 ======================================================
 
-Pysam is a python module that makes it easy to read and manipulate mapped short read sequence data stored in SAM/BAM files. 
-It is a lightweight wrapper of the samtools_ C-API.
+Introduction
+============
 
-This page provides a quick introduction in using pysam followed by the API. 
-See :ref:`usage` for more detailed usage instructions.
+Pysam is a python module that makes it easy to read and manipulate
+mapped short read sequence data stored in SAM/BAM files.  It is a
+lightweight wrapper of the htslib_ C-API.
 
-To use the module to read a file in BAM format, create a :class:`~pysam.Samfile` object::
+This page provides a quick introduction in using pysam followed by the
+API. See :ref:`usage` for more detailed usage instructions.
+
+To use the module to read a file in BAM format, create a
+:class:`~pysam.AlignmentFile` object::
 
    import pysam
-   samfile = pysam.Samfile( "ex1.bam", "rb" )
-   
+   samfile = pysam.AlignmentFile("ex1.bam", "rb")
 
-Once a file is opened you can iterate over all of the read mapping to a specified region using :meth:`~pysam.Samfile.fetch`.
-Each iteration returns a :class:`~pysam.AlignedRead` object which represents a single read along with its fields and optional tags::
+Once a file is opened you can iterate over all of the read mapping to
+a specified region using :meth:`~pysam.AlignmentFile.fetch`.  Each
+iteration returns a :class:`~pysam.AlignedSegment` object which
+represents a single read along with its fields and optional tags::
 
-   for alignedread in samfile.fetch('chr1', 100, 120):
-	print alignedread
+   for read in samfile.fetch('chr1', 100, 120):
+	print read
 
    samfile.close()
 
@@ -29,11 +35,11 @@ To give::
     EAS51_64:3:190:727:308	0	102	<<<<<<<<<<<<<<<<<<<<<<<<<<<::<<<844	99	GGTGCAGAGCCGAGTCACGGGGTTGCCAGCACAGG	99	18	1
     ...
 
-You can also write to a :class:`~pysam.Samfile`::
+You can also write to a :class:`~pysam.AlignmentFile`::
 
    import pysam
-   samfile = pysam.Samfile("ex1.bam", "rb")
-   pairedreads = pysam.Samfile("allpaired.bam", "wb", template=samfile)
+   samfile = pysam.AlignmentFile("ex1.bam", "rb")
+   pairedreads = pysam.AlignmentFile("allpaired.bam", "wb", template=samfile)
    for read in samfile.fetch():
 	if read.is_paired:
 		pairedreads.write(read)
@@ -41,20 +47,23 @@ You can also write to a :class:`~pysam.Samfile`::
    pairedreads.close()
    samfile.close()
 
-An alternative way of accessing the data in a SAM file is by iterating 
-over each base of a specified region using the :meth:`~pysam.Samfile.pileup` 
-method. Each iteration returns a :class:`~pysam.PileupColumn` which 
-represents all the reads in the SAM file that map to a single base in the 
-reference sequence. The list of reads are represented as :class:`~pysam.PileupRead` 
-objects in the :attr:`PileupColumn.pileups <pysam.PileupColumn.pileups>` property::
+An alternative way of accessing the data in a SAM file is by iterating
+over each base of a specified region using the
+:meth:`~pysam.AlignmentFile.pileup` method. Each iteration returns a
+:class:`~pysam.PileupColumn` which represents all the reads in the SAM
+file that map to a single base in the reference sequence. The list of
+reads are represented as :class:`~pysam.PileupRead` objects in the
+:attr:`PileupColumn.pileups <pysam.PileupColumn.pileups>` property::
 
     import pysam
-    samfile = pysam.Samfile("ex1.bam", "rb" )
-    for pileupcolumn in samfile.pileup( 'chr1', 100, 120):
-	print
-	print 'coverage at base %s = %s' % (pileupcolumn.pos , pileupcolumn.n)
+    samfile = pysam.AlignmentFile("ex1.bam", "rb" )
+    for pileupcolumn in samfile.pileup("chr1", 100, 120):
+	print ("\ncoverage at base %s = %s" %
+	        (pileupcolumn.pos, pileupcolumn.n))
 	for pileupread in pileupcolumn.pileups:
-	    print '\tbase in read %s = %s' % (pileupread.alignment.qname, pileupread.alignment.seq[pileupread.qpos])
+	    print ('\tbase in read %s = %s' %
+	            (pileupread.alignment.query_name,
+                     pileupread.alignment.query_sequence[pileupread.query_position]))
 
     samfile.close()
 
@@ -74,50 +83,121 @@ The above code outputs::
         base in read EAS51_64:3:190:727:308 = G
     ...
 
-Commands available in :term:`csamtools` are available as simple function calls. For example::
+Commands available in :term:`csamtools` are available as simple
+function calls. For example::
 
-   pysam.sort( "ex1.bam", "output" )
+   pysam.sort("ex1.bam", "output")
 
 corresponds to the command line::
 
    samtools sort ex1.bam output
 
-Analogous to :class:`~pysam.Samfile`, a :class:`~pysam.Tabixfile` allows fast random access to compressed
-and tabix indexed tab-separated file formats with genomic data::
+Analogous to :class:`~pysam.AlignmentFile`, a
+:class:`~pysam.TabixFile` allows fast random access to compressed and
+tabix indexed tab-separated file formats with genomic data::
 
    import pysam
-   tabixfile = pysam.Tabixfile( "example.gtf.gz" )
+   tabixfile = pysam.TabixFile("example.gtf.gz")
    
-   for gtf in tabixfile.fetch('chr1', 1000, 2000):
-       print gtf.contig, gtf.start, gtf.end, gtf.gene_id
+   for gtf in tabixfile.fetch("chr1", 1000, 2000):
+       print (gtf.contig, gtf.start, gtf.end, gtf.gene_id)
 
-:class:`~pysam.Tabixfile` implements lazy parsing in order to iterate over large tables efficiently.
+:class:`~pysam.TabixFile` implements lazy parsing in order to iterate
+over large tables efficiently.
 
 More detailed usage instructions is at :ref:`usage`.
 
 .. note::
-    Coordinates in pysam are always 0-based (following the python convention). SAM text files use 1-based coordinates.
+
+   Coordinates in pysam are always 0-based (following the python
+   convention). SAM text files use 1-based coordinates.
 
 .. note::
-    The above examples can be run in the :file:`tests` directory of the installation directory. Type 'make' before 
-    running them.
+
+   The above examples can be run in the :file:`tests` directory of the
+    installation directory. Type 'make' before running them.
 
 .. seealso::
 
-    http://code.google.com/p/pysam/
-	The pysam Google code page, containing source code and download instructions
+   https://github.com/pysam-developers/pysam
 
-    http://wwwfgu.anat.ox.ac.uk/~andreas/documentation/samtools/contents.html
-	The pysam website containing documentation
+       The pysam code repository, containing source code and download
+       instructions
 
-.. _samtools: http://samtools.sourceforge.net/ 
+   http://pysam.readthedocs.org/en/latest/
 
+       The pysam website containing documentation
 
 API
 ===
 
-.. automodule:: pysam
+SAM/BAM files
+-------------
+
+Objects of type :class:`~pysam.AlignmentFile` allow working with
+BAM/SAM formatted files.
+
+.. autoclass:: pysam.AlignmentFile
+   :members:
+
+An :class:`~pysam.AlignedSegment` represents an aligned segment within
+a SAM/BAM file.
+
+.. autoclass:: pysam.AlignedSegment
+   :members:
+
+.. autoclass:: pysam.PileupColumn
    :members:
-   :undoc-members:
+
+.. autoclass:: pysam.PileupRead
+   :members:
+
+.. autoclass:: pysam.IndexedReads
+   :members:
+
+
+Tabix files
+-----------
+
+:class:`~pysam.TabixFile` opens tabular files that have been
+indexed with tabix_.
+
+.. autoclass:: pysam.TabixFile
+   :members:
+
+To iterate over tabix files, use :func:`~pysam.tabix_iterator`:
+
+.. autofunction:: pysam.tabix_iterator
+
+.. autofunction:: pysam.tabix_compress
+
+.. autofunction:: pysam.tabix_index
+
+.. autoclass:: pysam.asTuple
+   :members:
+
+.. autoclass:: pysam.asVCF
+   :members:
+
+.. autoclass:: pysam.asBed
+   :members:
+
+.. autoclass:: pysam.asGTF
+   :members:
+
+
+Fasta files
+-----------
+
+.. autoclass:: pysam.FastaFile
+   :members:
+
+Fastq files
+-----------
+
+.. autoclass:: pysam.FastqFile
+   :members:
+
+
 
 
diff --git a/doc/conf.py b/doc/conf.py
index 7c7c097..c9b8ac6 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -47,6 +47,20 @@ master_doc = 'index'
 project = u'pysam'
 copyright = u'2009, Andreas Heger, Tildon Grant Belgrad, Martin Goodson, Kevin Jacobs'
 
+# Included at the end of each rst file
+rst_epilog = '''
+.. _CGAT Training Programme: http://www.cgat.org
+.. _pysam: https://github.com/pysam-developers/pysam
+.. _samtools: http://samtools.sourceforge.net/
+.. _htslib: http://www.htslib.org/
+.. _tabix: http://samtools.sourceforge.net/tabix.shtml/
+.. _Galaxy: https://main.g2.bx.psu.edu/
+.. _cython: http://cython.org/
+.. _python: http://python.org/
+.. _pyximport: http://www.prescod.net/pyximport/
+
+'''
+
 # The version info for the project you're documenting, acts as replacement for
 # |version| and |release|, also used in various other places throughout the
 # built documents.
diff --git a/doc/developer.rst b/doc/developer.rst
index d7383c7..6abd22c 100644
--- a/doc/developer.rst
+++ b/doc/developer.rst
@@ -8,36 +8,42 @@ Code organization
 The top level directory is organized in the following 
 directories:
 
-pysam
+:file:`pysam`
    Code specific to pysam
 
-samtools
-   Original and unmodified source code from :term:`csamtools`. Use 
-   :file:`setup.py` to obtain the latest code.
+:file:`doc`
+   The documentation. To build the latest documention type::
 
-tests
-   Examples and data for testing
+       make -C doc html
 
-Importing :term:`csamtools`
-===========================
+:file:`tests`
+   Code and data for testing
 
-Running :file:`setup.py` will import the csamtools source code. 
-The command::
+:file:`htslib`
+   Source code from :term:`htslib` shipped with pysam. See
+   :file:`setup.py` about importing.
 
-   python setup.py import PATH
+:file:`samtools`
+   Source code from :term:`csamtools` shipped with pysam. See
+   :file:`setup.py` about importing.
 
-where ``PATH`` points to a :term:`csamtools` source directory. For example::
 
-   python setup.py import ~/samtools-0.1.6
+Importing new versions of htslib and samtools
+=============================================
 
-Note that files will not be overwritten. To import all anew, 
-delete all :file:`*.c` and :file:`*.h` files in the :file:`samtools`
-directory first. 
+See instructions in :file:`setup.py` to import the latest
+version of htslib_ and samtools_.
 
 Unit testing
 ============
 
-Unit tests are in the scripts :file:`pysam_test.py` and :file:`tabix_test.py`. 
+Unit tests are in the :file:`tests` directory. To run all unit tests,
+run::
+
+   nosetests -s -v tests
+
+Note to use the ``-s/--nocapture`` option to prevent nosetests from
+captpuring standard output.
 
 Contributors
 ============
diff --git a/doc/faq.rst b/doc/faq.rst
index 8e07287..5d2a048 100644
--- a/doc/faq.rst
+++ b/doc/faq.rst
@@ -15,11 +15,12 @@ format is 0-based. It is important to remember that pysam will always
 conform to the python convention and translate to/from the file format
 automatically.
 
-The only exception is the :term:`region string` in the :meth:`Samfile.fetch`
-and :meth:`Samfile.pileup` methods. This string follows the convention
-of the samtools command line utilities. The same is true for any
-coordinates passed to the samtools command utilities directly, such
-as :meth:`pysam.mpileup`.
+The only exception is the :term:`region` string in the
+:meth:`~pysam.AlignmentFile.fetch` and
+:meth:`~pysam.AlignmentFile.pileup` methods. This string follows the
+convention of the samtools command line utilities. The same is true
+for any coordinates passed to the samtools command utilities directly,
+such as :meth:`pysam.mpileup`.
 
 BAM files with a large number of reference sequences is slow
 ============================================================
@@ -27,7 +28,7 @@ BAM files with a large number of reference sequences is slow
 If you have many reference sequences in a bam file, the following
 might be slow::
 
-      track = pysam.Samfile(fname, "rb")
+      track = pysam.AlignmentFile(fname, "rb")
       for aln in track.fetch():
       	  pass
 	  
@@ -36,7 +37,7 @@ for each reference sequence in the order as it is defined in the
 header. This might require a lot of jumping around in the file. To
 avoid this, use::
 
-      track = pysam.Samfile(fname, "rb")
+      track = pysam.AlignmentFile(fname, "rb")
       for aln in track.fetch( until_eof = True ):
       	  pass
  
@@ -84,7 +85,7 @@ the C-samtools package. Thus, some attention must be paid at the
 lifetime of objects. The following to code snippets will cause an
 error::
 
-    s = Samfile('ex1.bam')
+    s = AlignmentFile('ex1.bam')
     for p in s.pileup('chr1', 1000,1010):
         pass
     
@@ -94,7 +95,7 @@ error::
 The iteration has finished, thus the contents of p are invalid. A
 variation of this::
 
-    p = next(Samfile('ex1.bam').pileup('chr1', 1000, 1010))
+    p = next(AlignmentFile('ex1.bam').pileup('chr1', 1000, 1010))
     for pp in p.pileups:
         print pp
 
@@ -102,11 +103,37 @@ Again, the iteration finishes as the temporary iterator created
 by pileup goes out of scope. The solution is to keep a handle
 to the iterator that remains alive::
 
-    i = Samfile( 'ex1.bam' ).pileup( 'chr1', 1000, 1010)
+    i = AlignmentFile('ex1.bam').pileup( 'chr1', 1000, 1010)
     p = next(i)
     for pp in p.pileups:
         print pp
 
+Psyam won't compile
+===================
+
+Compiling pysam can be tricky as there are numerous variables that
+differ between build environments such as OS, version, python version,
+and compiler. It is difficult to build software that build cleanly
+on all systems and the process might fail. Please see the 
+`pysam user group
+<https://groups.google.com/forum/#!forum/pysam-user-group>`_
+for common issues.
+
+If there is a build issue, read the generated output carefully -
+generally the cause of the problem is among the first errors to be
+reported. For example, you will need to have the development version
+of python installed that includes the header files such as
+:file:`Python.h`. If that file is missing, the compiler will report
+this at the very top of its error messages but will follow it 
+with any unknown function or variable definition it encounters later
+on.
+
+A general advice is to always use the latest version on python_ and
+cython_ when building pysam. There are some known incompatibilities:
+
+* Python 3.4 requires cython 0.20.2 or later (see `here
+  <https://github.com/pysam-developers/pysam/issues/37>`_)
+
 
 
 
diff --git a/doc/glossary.rst b/doc/glossary.rst
index aa61c40..e5c69c2 100644
--- a/doc/glossary.rst
+++ b/doc/glossary.rst
@@ -68,6 +68,20 @@ Glossary
    tabix row
       A row in a :term:`tabix file`. Fields within a row are 
       tab-separated. 
-      
 
-.. _samtools: http://samtools.sourceforge.net
+   soft clipping
+   soft clipped
+
+      In alignments with soft clipping part of the query sequence
+      are not aligned. The unaligned query sequence is still part
+      of the alignment record. This is in difference to hard clipped reads.
+
+   hard clipping
+   hard clipped
+
+      In hard clipped reads, part of the sequence has been removed
+      prior to alignment. That only a subsequence is aligend might be
+      recorded in the :term:`cigar` alignment, but the removed
+      sequence will not be part of the alignment record, in contrast
+      to :term:`soft clipped` reads.
+     
diff --git a/doc/index.rst b/doc/index.rst
index 9f2b9c0..10aadb9 100644
--- a/doc/index.rst
+++ b/doc/index.rst
@@ -1,25 +1,21 @@
-.. samtools documentation master file, created by
-   sphinx-quickstart on Wed Aug 12 14:43:42 2009.
-   You can adapt this file completely to your liking, but it should at least
-   contain the root `toctree` directive.
-
-pysam: samtools interface for python
-====================================
+pysam: htslib interface for python
+==================================
 
 :Author: Andreas Heger and contributors
 :Date: |today|
 :Version: |version|
 
-The *SAM/BAM* format is a way to store efficiently large numbers of alignments
-[Li2009]_, such as those routinely are created by next-generation sequencing 
-methods.
+The *SAM/BAM* format is a way to store efficiently large numbers of
+alignments [Li2009]_, such as those routinely are created by
+next-generation sequencing methods.
 
-This module provides a low-level wrapper around the samtools_ C-API using `cython`_
-and a high-level API for convenient access to the data in *SAM/BAM* formatted files.
-Also included is an interface to the tabix_ C-API for reading compressed and
-indexed tabular data.
+This module provides a low-level wrapper around the htslib_ C-API as
+using `cython`_ and a high-level API for convenient access to the data
+in *SAM/BAM* formatted files. Also included is an interface to the
+samtools_ command line utilities and the tabix_ C-API for reading
+compressed and indexed tabular data.
 
-The current version wraps *samtools-0.1.19* and *tabix-0.2.6*.
+The current version wraps *htslib-1.1* and *samtools-1.1*.
 
 Contents
 --------
@@ -29,11 +25,10 @@ Contents
 
    api.rst
    usage.rst
-   tabix.rst
    faq.rst
-   glossary.rst
    developer.rst
    release.rst
+   glossary.rst
 
 Indices and tables
 ------------------
@@ -52,6 +47,9 @@ References
 	    `PMID: 19505943 <http://www.ncbi.nlm.nih.gov/pubmed/19505943?dopt=Abstract>`_
 
 .. seealso::
+ 
+   Information about htslib
+      http://www.htslib.org
 
    The samtools homepage
       http://samtools.sourceforge.net
@@ -61,13 +59,3 @@ References
 
    The python language
       http://www.python.org
-
-.. _samtools: http://samtools.sourceforge.net
-
-.. _tabix: http://samtools.sourceforge.net/tabix.shtml
-
-.. _cython: http://cython.org/
-
-.. _python: http://www.python.org
-
-
diff --git a/doc/out b/doc/out
new file mode 100644
index 0000000..8e2f6d1
--- /dev/null
+++ b/doc/out
@@ -0,0 +1,30 @@
+sphinx-build -b html -d _build/doctrees   . _build/html
+Running Sphinx v1.1.3
+loading pickled environment... done
+building [html]: targets for 0 source files that are out of date
+updating environment: 0 added, 1 changed, 0 removed
+reading sources... [100%] index
+
+/home/andreas/devel/pysam/doc/index.rst:23: WARNING: toctree contains reference to nonexisting document u'tabix'
+/home/andreas/devel/pysam/doc/index.rst:73: WARNING: Duplicate explicit target name: "samtools".
+/home/andreas/devel/pysam/doc/index.rst:74: WARNING: Duplicate explicit target name: "htslib".
+/home/andreas/devel/pysam/doc/index.rst:75: WARNING: Duplicate explicit target name: "tabix".
+/home/andreas/devel/pysam/doc/index.rst:77: WARNING: Duplicate explicit target name: "cython".
+/home/andreas/devel/pysam/doc/index.rst:78: WARNING: Duplicate explicit target name: "python".
+/home/andreas/devel/pysam/doc/index.rst:12: ERROR: Duplicate target name, cannot be used as a unique reference: "htslib".
+/home/andreas/devel/pysam/doc/index.rst:12: ERROR: Duplicate target name, cannot be used as a unique reference: "cython".
+/home/andreas/devel/pysam/doc/index.rst:12: ERROR: Duplicate target name, cannot be used as a unique reference: "samtools".
+/home/andreas/devel/pysam/doc/index.rst:12: ERROR: Duplicate target name, cannot be used as a unique reference: "tabix".
+looking for now-outdated files... none found
+pickling environment... done
+checking consistency... done
+preparing documents... done
+writing output... [100%] index
+
+writing additional files... genindex py-modindex search
+copying static files... done
+dumping search index... done
+dumping object inventory... done
+build succeeded, 10 warnings.
+
+Build finished. The HTML pages are in _build/html.
diff --git a/doc/release.rst b/doc/release.rst
index d42f13a..eefbf04 100644
--- a/doc/release.rst
+++ b/doc/release.rst
@@ -2,124 +2,204 @@
 Release notes
 =============
 
+Release 0.8.1
+=============
+
+* Pysam now wraps htslib and samtools versions 1.1
+
+* Bugfixes, most notable:
+  * issue #43: uncompressed BAM output
+  * issue #42: skip tests requiring network if none available
+  * issue #19: multiple iterators can now be made to work on the same tabix file
+  * issue #24: All strings returned from/passed to the pysam API are now unicode in python 3
+  * issue #5:  type guessing for lists of integers fixed    
+    
+* API changes for consistency. The old API is still present,
+  but deprecated.
+  In particular:
+
+  * Tabixfile -> TabixFile
+  * Fastafile -> FastaFile
+  * Fastqfile -> FastqFile
+  * Samfile -> AlignmentFile
+  * AlignedRead -> AlignedSegment
+     * qname -> query_name
+     * tid -> reference_id
+     * pos -> reference_start
+     * mapq -> mapping_quality
+     * rnext -> next_reference_id
+     * pnext -> next_reference_start
+     * cigar = alignment
+     * cigarstring = cigarstring
+     * tlen -> query_length
+     * seq -> query_sequence
+     * qual -> query_qualities, now returns array
+     * qqual -> query_alignment_qualities, now returns array
+     * tags = tags
+     * alen -> reference_length, reference is always "alignment", so removed
+     * aend -> reference_end
+     * rlen -> query_length
+     * query -> query_alignment_sequence
+     * qstart -> query_alignment_start
+     * qend -> query_alignment_end
+     * qlen -> query_alignment_length
+     * mrnm -> next_reference_id   
+     * mpos -> next_reference_start
+     * rname -> reference_id
+     * isize -> template_length
+     * blocks -> get_blocks()
+     * aligned_pairs -> get_aligned_pairs()
+     * inferred_length -> infer_query_length()
+     * positions -> get_reference_positions()
+     * overlap() -> get_overlap()
+
+  * All strings are now passed to or received from the pysam API
+    as strings, no more bytes.
+
+Other changes:
+  * AlignmentFile.fetch(reopen) option is now multiple_iterators. The
+    default changed to not reopen a file unless requested by the user.
+  * FastaFile.getReferenceLength is now FastaFile.get_reference_length
+
+Backwards incompatible changes
+
+* Empty cigarstring now returns None (intstead of '')
+* Empty cigar now returns None (instead of [])
+* When using the extension classes in cython modules, AlignedRead
+  needs to be substituted with AlignedSegment. 
+* fancy_str() has been removed
+* qual, qqual now return arrays
+
+
+
+
 Release 0.8.0
 =============
 
-  * Disabled features
-     * IteratorColumn.setMask() disabled as
-       htslib does not implement this functionality?
+* Disabled features
+  * IteratorColumn.setMask() disabled as htslib does not implement
+    this functionality?
 
-  * Not implemented yet:
-     * reading SAM files without header
-     * samtools stepper
+* Not implemented yet:
+  * reading SAM files without header
+
+Tabix files between version 0.7.8 and 0.8.0 are
+not compatible and need to be re-indexed.
+
+While version 0.7.8 and 0.8.0 should be mostly
+compatible, there are some notable exceptions:
+
+* tabix iterators will fail if there are comments
+  in the middle or the end of a file.
+
+* tabix raises always ValueError for invalid intervals.
+  Previously, different types of errors were raised
+  (KeyError, IndexError, ValueError) depending on
+  the type of invalid intervals (missing chromosome,
+  out-of-range, malformatted interval).
 
-  * tabix needs to be re-indexed
-  * tabix iterators will not return comments
-    (TODO: full iterator)
-  * tabix raises always ValueError for invalid intervals
 
 Release 0.7.8
 =============
 
-   * added AlignedRead.setTag method
-   * added AlignedRead.blocks
-   * unsetting CIGAR strings is now possible
-   * empty CIGAR string returns empty list
-   * added reopen flag to Samfile.fetch()
-   * various bugfixes
+* added AlignedRead.setTag method
+* added AlignedRead.blocks
+* unsetting CIGAR strings is now possible
+* empty CIGAR string returns empty list
+* added reopen flag to Samfile.fetch()
+* various bugfixes
 
 Release 0.7.7
 =============
 
-   * added Fastafile.references, .nreferences and .lengths
-   * tabix_iterator now uses kseq.h for python 2.7
+* added Fastafile.references, .nreferences and .lengths
+* tabix_iterator now uses kseq.h for python 2.7
 
 Release 0.7.6
 =============
 
-   * added inferred_length property
-   * issue 122: MACOSX getline missing, now it works?
-   * seq and qual can be set None
-   * added Fastqfile
+* added inferred_length property
+* issue 122: MACOSX getline missing, now it works?
+* seq and qual can be set None
+* added Fastqfile
 
 Release 0.7.5
 =============
 
-   * switch to samtools 0.1.19
-   * issue 122: MACOSX getline missing
-   * issue 130: clean up tempfiles
-   * various other bugfixes
+* switch to samtools 0.1.19
+* issue 122: MACOSX getline missing
+* issue 130: clean up tempfiles
+* various other bugfixes
 
 Release 0.7.4
 =============
 	
-   * further bugfixes to setup.py and package layout
+* further bugfixes to setup.py and package layout
 
 Release 0.7.3
 =============
 	
-   * further bugfixes to setup.py
-   * upgraded distribute_setup.py to 0.6.34
+* further bugfixes to setup.py
+* upgraded distribute_setup.py to 0.6.34
 
 Release 0.7.2
 =============
   
-   * bugfix in installer - failed when cython not present
-   * changed installation locations of shared libraries
+* bugfix in installer - failed when cython not present
+* changed installation locations of shared libraries
 
 Release 0.7.1
 =============
 
-   * bugfix: missing PP tag PG records in header
-   * added pre-built .c files to distribution
+* bugfix: missing PP tag PG records in header
+* added pre-built .c files to distribution
 
 Release 0.7
 ===========
 
-   * switch to tabix 0.2.6
-   * added cigarstring field
-   * python3 compatibility
-   * added B tag handling
-   * added check_sq and check_header options to Samfile.__init__
-   * added lazy GTF parsing to tabix
-   * reworked support for VCF format parsing
-   * bugfixes
+* switch to tabix 0.2.6
+* added cigarstring field
+* python3 compatibility
+* added B tag handling
+* added check_sq and check_header options to Samfile.__init__
+* added lazy GTF parsing to tabix
+* reworked support for VCF format parsing
+* bugfixes
 
 Release 0.6
 ===========
 
-   * switch to samtools 0.1.18
-   * various bugfixes
-   * removed references to deprecated 'samtools pileup' functionality
-   * AlignedRead.tags now returns an empty list if there are no tags.
-   * added pnext, rnext and tlen
+* switch to samtools 0.1.18
+* various bugfixes
+* removed references to deprecated 'samtools pileup' functionality
+* AlignedRead.tags now returns an empty list if there are no tags.
+* added pnext, rnext and tlen
 
 Release 0.5
 ===========
 
-   * switch to samtools 0.1.16 and tabix 0.2.5
-   * improved tabix parsing, added vcf support
-   * re-organized code to permit linking againts pysam
-   * various bugfixes
-   * added Samfile.positions and Samfile.overlap
+* switch to samtools 0.1.16 and tabix 0.2.5
+* improved tabix parsing, added vcf support
+* re-organized code to permit linking against pysam
+* various bugfixes
+* added Samfile.positions and Samfile.overlap
 
 Release 0.4
 ===========
 
-   * switch to samtools 0.1.12a and tabix 0.2.3
-   * added snp and indel calling.
-   * switch from pyrex to cython
-   * changed handling of samtools stderr
-   * various bugfixes
-   * added Samfile.count and Samfile.mate
-   * deprecated AlignedRead.rname, added AlignedRead.tid
+* switch to samtools 0.1.12a and tabix 0.2.3
+* added snp and indel calling.
+* switch from pyrex to cython
+* changed handling of samtools stderr
+* various bugfixes
+* added Samfile.count and Samfile.mate
+* deprecated AlignedRead.rname, added AlignedRead.tid
 
 Release 0.3
 ===========
 
-   * switch to samtools 0.1.8
-   * added support for tabix files
-   * numerous bugfixes including
-       * permit simultaneous iterators on the same file
-       * working access to remote files
-
+* switch to samtools 0.1.8
+* added support for tabix files
+* numerous bugfixes including
+* permit simultaneous iterators on the same file
+* working access to remote files
diff --git a/doc/usage.rst b/doc/usage.rst
index 1a5e79c..7e79298 100644
--- a/doc/usage.rst
+++ b/doc/usage.rst
@@ -4,103 +4,157 @@
 Working with BAM/SAM-formatted files
 ====================================
 
-In order to follow the examples below, execute
-   
-   make
-
-in the :file:`test` directory.
-
 Opening a samfile
 =================
 
-The first operation is to import the pysam module and open a 
-:class:`pysam.Samfile`::
+To begin with, import the pysam module and open a
+:class:`pysam.AlignmentFile`::
 
    import pysam
-   samfile = pysam.Samfile( "ex1.bam", "rb" )
+   samfile = pysam.AlignmentFile("ex1.bam", "rb")
 
 The above command opens the file :file:`ex1.bam` for reading.
 The ``b`` qualifier indicates that this is a :term:`BAM` file. 
 To open a :term:`SAM` file, type::
 
    import pysam
-   samfile = pysam.Samfile( "ex1.bam", "r" )
+   samfile = pysam.AlignmentFile("ex1.sam", "r")
 
 Fetching reads mapped to a :term:`region`
 =========================================
 
-There are two ways to obtain the reads mapped to a genomic region. The
-first method follows the :term:`csamtools` API and  works 
-via a callback function. The callback will be executed for each 
-alignment in a :term:`region`::
+Reads are obtained through a call to the
+:meth:`pysam.AlignmentFile.fetch` method which returns an iterator.
+Each call to the iterator will returns a :class:`pysam.AlignedSegment`
+object::
 
-   def my_fetch_callback( alignment ):
-       print str(alignment)
+   iter = samfile.fetch("seq1", 10, 20)
+   for x in iter:
+       print (str(x))
 
-   samfile.fetch( 'seq1', 10, 20, callback = my_fetch_callback )
+:meth:`pysam.AlignmentFile.fetch` returns all reads overlapping a
+region sorted by the first aligned base in the :term:`reference`
+sequence.  Note that it will also return reads that are only partially
+overlapping with the :term:`region`. Thus the reads returned might
+span a region that is larger than the one queried.
 
-Using a function object, work can be done on the alignments. The
-code below simply counts aligned reads::
+..
+   The
+   first method follows the :term:`csamtools` API and  works 
+   via a callback function. The callback will be executed for each 
+   alignment in a :term:`region`::
 
-   class Counter:
-       def __init__(self):
-           self.counts = 0
-       def __call__(self, alignment):
-           self.counts += 1
-   
-   c = Counter()
-   samfile.fetch( 'seq1', 10, 20, callback = c )
-   print "counts=", c.counts
+      def my_fetch_callback(alignment):
+	  print str(alignment)
 
-The second method uses python iterators. If you call :meth:`pysam.Samfile.fetch`
-without a callback, an iterator of the type :class:`pysam.IteratorRow` is returned.
-It will iterate through mapped reads
-and return a :class:`pysam.AlignedRead` object for each::
+      samfile.fetch('seq1', 10, 20, callback = my_fetch_callback)
 
-   iter = samfile.fetch( 'seq1', 10, 20)
-   for x in iter: print str(x)
+   Using a function object, work can be done on the alignments. The
+   code below simply counts aligned reads::
 
-Note that both methods iterate through a :term:`BAM` file
-on a read-by-read basis. 
+      class Counter:
+	  def __init__(self):
+	      self.counts = 0
+	  def __call__(self, alignment):
+	      self.counts += 1
 
-:meth:`pysam.Samfile.fetch` returns all reads overlapping a region sorted
-by the first aligned base in the :term:`reference` sequence.
-Note that it will also return reads that are only partially
-overlapping with the :term:`region`. Thus the reads returned
-might span a region that is larger than the one queried.
+      c = Counter()
+      samfile.fetch( 'seq1', 10, 20, callback = c )
+      print "counts=", c.counts
 
 Using the pileup-engine
 =======================
 
-The :term:`pileup` engine of :term:`csamtools` iterates
-over all reads that are aligned to a :term:`region`. In
-contrast to :term:`fetching`, the :term:`pileup` engine 
-returns for each base in the :term:`reference` sequence the reads that
-map to that particular position.
+In contrast to :term:`fetching`, the :term:`pileup` engine returns for
+each base in the :term:`reference` sequence the reads that map to that
+particular position. In the typical view of reads stacking vertically
+on top of the reference sequence similar to a multiple alignment,
+:term:`fetching` iterates over the rows of this implied multiple
+alignment while a :term:`pileup` iterates over the :term:`columns`.
+
+..
+   Again, there are two principal methods to iterate.
+   The first works via a callback function::
+
+      def my_pileup_callback( pileups ):
+	  print str(pileups)
+      samfile.pileup( 'seq1', 10, 20, callback = my_pileup_callback )
+
+Calling :meth:`~pysam.AlignmentFile.pileup` will return an iterator
+over each :term:`column` (reference base) of a specified
+:term:`region`. Each call to the iterator returns an object of the
+type :class:`pysam.PileupColumn` that provides access to all the
+reads aligned to that particular reference position as well as
+some additional information::
+
+   iter = samfile.pileup('seq1', 10, 20)
+   for x in iter:
+      print (str(x))
+ 
+
+Creating SAM/BAM files from scratch
+===================================
+
+The following example shows how a new BAM file is constructed from
+scratch.  The important part here is that the
+:class:`pysam.AlignmentFile` class needs to receive the sequence
+identifiers. These can be given either as a dictionary in a header
+structure, as lists of names and sizes, or from a template file.
+Here, we use a header dictionary::
+
+   header = { 'HD': {'VN': '1.0'},
+               'SQ': [{'LN': 1575, 'SN': 'chr1'}, 
+                      {'LN': 1584, 'SN': 'chr2'}] }
+
+   outfile = pysam.AlignmentFile(tmpfilename, "wh", header=header)
+   a = pysam.AlignedSegment()
+   a.query_name = "read_28833_29006_6945"
+   a.query_sequence="AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG"
+   a.flag = 99
+   a.reference_id = 0
+   a.reference_start = 32
+   a.mapping_quality = 20
+   a.cigar = ((0,10), (2,1), (0,25))
+   a.next_reference_id = 0
+   a.next_reference_start=199
+   a.template_length=167
+   a.query_qualities="<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<"
+   a.tags = (("NM", 1),
+	     ("RG", "L1"))
+   outfile.write(a)
+   outfile.close()
+
+Using streams
+=============
 
-Again, there are two principal methods to iterate.
-The first works via a callback function::
+Pysam does not support reading and writing from true python file
+objects, but it does support reading and writing from stdin and
+stdout. The following example reads from stdin and writes to stdout::
 
-   def my_pileup_callback( pileups ):
-       print str(pileups)
-   samfile.pileup( 'seq1', 10, 20, callback = my_pileup_callback )
+   infile = pysam.AlignmentFile("-", "r")
+   outfile = pysam.AlignmentFile("-", "w", template=infile)
+   for s in infile:
+       outfile.write(s)
 
-The second method uses python iterators. The iterator
-:class:`pysam.IteratorColumn` will iterate through each :term:`column`
-(reference bases) and return a list of aligned reads::
+It will also work with :term:`BAM` files. The following script
+converts a :term:`BAM` formatted file on stdin to a :term:`SAM`
+formatted file on stdout::
 
-   iter = samfile.pileup( 'seq1', 10, 20 )
-   for x in iter: print str(x)
+   infile = pysam.AlignmentFile("-", "rb")
+   outfile = pysam.AlignmentFile("-", "w", template=infile)
+   for s in infile:
+       outfile.write(s)
 
-Aligned reads are returned as a :class:`pysam.PileupColumn`.
+Note, only the file open mode needs to changed from ``r`` to ``rb``.
 
+=====================================
 Using samtools commands within python
 =====================================
 
 Commands available in :term:`csamtools` are available
 as simple function calls. For example::
 
-   pysam.sort( "ex1.bam", "output" )
+   pysam.sort("ex1.bam", "output")
 
 corresponds to the command line::
 
@@ -108,11 +162,11 @@ corresponds to the command line::
 
 Command line options can be provided as arguments::
    
-   pysam.sort( "-n", "ex1.bam", "output" )
+   pysam.sort("-n", "ex1.bam", "output")
 
 or::
 
-   pysam.sort( "-m", "1000000", "ex1.bam", "output" )
+   pysam.sort("-m", "1000000", "ex1.bam", "output")
 
 In order to get usage information, try::
 
@@ -125,7 +179,7 @@ Argument errors raise a :class:`pysam.SamtoolsError`::
    Traceback (most recent call last):
    File "x.py", line 12, in <module>
      pysam.sort()
-   File "/home/andreas/pysam/build/lib.linux-x86_64-2.6/pysam/__init__.py", line 37, in __call__
+   File "/build/lib.linux-x86_64-2.6/pysam/__init__.py", line 37, in __call__
      if retval: raise SamtoolsError( "\n".join( stderr ) )
    pysam.SamtoolsError: 'Usage: samtools sort [-n] [-m <maxMem>] <in.bam> <out.prefix>\n'
 
@@ -137,56 +191,34 @@ available using the :meth:`getMessages` method::
 Note that only the output from the last invocation of a command
 is stored.
 
-Creating SAM/BAM files from scratch
-===================================
+================================
+Working with tabix-indexed files
+================================
 
-The following example shows how a new BAM file is constructed from scratch.
-The important part here is that the :class:`pysam.Samfile` class needs to receive
-the sequence identifiers. These can be given either as a dictionary in a
-header structure, as lists of names and sizes, or from a template file.
-Here, we use a header dictionary::
+To open a tabular file that has been indexed with tabix_, use
+:class:`~pysam.TabixFile`::
 
-   header = { 'HD': {'VN': '1.0'},
-               'SQ': [{'LN': 1575, 'SN': 'chr1'}, 
-                      {'LN': 1584, 'SN': 'chr2'}] }
+    import pysam
+    tbx = pysam.TabixFile("example.bed.gz")
 
-   outfile = pysam.Samfile( tmpfilename, "wh", header = header )
-   a = pysam.AlignedRead()
-   a.qname = "read_28833_29006_6945"
-   a.seq="AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG"
-   a.flag = 99
-   a.rname = 0
-   a.pos = 32
-   a.mapq = 20
-   a.cigar = ( (0,10), (2,1), (0,25) )
-   a.mrnm = 0
-   a.mpos=199
-   a.isize=167
-   a.qual="<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<"
-   a.tags = ( ("NM", 1),
-	      ("RG", "L1") )
-   outfile.write(a)
-   outfile.close()
+Similar to :class:`~pysam.AlignmentFile.fetch`, intervals within a
+region can be retrieved by calling :meth:`~pysam.TabixFile.fetch()`::
 
-Using streams
-=============
+    for row in tbx.fetch("chr1", 1000, 2000):
+         print (str(row))
 
-Pysam does not support reading and writing from true python file objects, but
-it does support reading and writing from stdin and stdout. The following example reads 
-from stdin and writes to stdout::
+This will return a tuple-like data structure in which columns can
+be retrieved by numeric index:
 
-   infile = pysam.Samfile( "-", "r" )
-   outfile = pysam.Samfile( "-", "w", template = infile )
-   for s in infile: outfile.write(s)
+    for row in tbx.fetch("chr1", 1000, 2000):
+         print ("chromosome is", row[0])
 
-It will also work with :term:`BAM` files. The following script converts a :term:`BAM` formatted file
-on stdin to a :term:`SAM` formatted file on stdout::
+By providing a parser argument to :class:`~pysam.AlignmentFile.fetch`
+or :class:`~pysam.TabixFile`, the data will we presented in parsed
+form:
 
-   infile = pysam.Samfile( "-", "rb" )
-   outfile = pysam.Samfile( "-", "w", template = infile )
-   for s in infile: outfile.write(s)
-
-Note, only the file open mode needs to changed from ``r`` to ``rb``.
+    for row in tbx.fetch("chr1", 1000, 2000, parser=pysam.asTuple()):
+         print ("chromosome is", row.contig)
 
 .. Currently inactivated as pileup deprecated
 .. Using the samtools SNP caller
@@ -199,7 +231,7 @@ Note, only the file open mode needs to changed from ``r`` to ``rb``.
 
 .. To use :class:`pysam.IteratorSNPCalls`, associate it with a :class:`pysam.IteratorColumn`::
 
-..     samfile = pysam.Samfile( "ex1.bam", "rb")  
+..     samfile = pysam.AlignmentFile( "ex1.bam", "rb")  
 ..     fastafile = pysam.Fastafile( "ex1.fa" )
 ..     pileup_iter = samfile.pileup( stepper = "samtools", fastafile = fastafile )
 ..     sncpall_iter = pysam.IteratorSNPCalls(pileup_iter)
@@ -208,7 +240,7 @@ Note, only the file open mode needs to changed from ``r`` to ``rb``.
 
 .. Usage of :class:`pysam.SNPCaller` is similar::
 
-..     samfile = pysam.Samfile( "ex1.bam", "rb")  
+..     samfile = pysam.AlignmentFile( "ex1.bam", "rb")  
 ..     fastafile = pysam.Fastafile( "ex1.fa" )
 ..     pileup_iter = samfile.pileup( stepper = "samtools", fastafile = fastafile )
 ..     snpcaller = pysam.SNPCaller.call(pileup_iter)
@@ -221,16 +253,17 @@ Note, only the file open mode needs to changed from ``r`` to ``rb``.
 .. Calling indels works along the same lines, using the :class:`pysam.IteratorIndelCalls`
 .. and :class:`pysam.IteratorIndelCaller`.
 
+===============
 Extending pysam
 ===============
 
 Using pyximport_, it is (relatively) straight-forward to access pysam
 internals and the underlying samtools library. An example is provided
-in the :file:`test` directory. The example emulates the samtools flagstat command
-and consists of three files:
+in the :file:`tests` directory. The example emulates the samtools
+flagstat command and consists of three files:
 
-1. The main script :file:`pysam_flagstat.py`. The important lines in this script
-   are::
+1. The main script :file:`pysam_flagstat.py`. The important lines in
+   this script are::
 
       import pyximport
       pyximport.install()
@@ -238,46 +271,47 @@ and consists of three files:
 
       ...
    
-      flag_counts = _pysam_flagstat.count( pysam_in )
+      flag_counts = _pysam_flagstat.count(pysam_in)
 
-   The first part imports, sets up pyximport_ and imports the cython module :file:`_pysam_flagstat`. 
-   The second part calls the ``count`` method in :file:`_pysam_flagstat`. 
+   The first part imports, sets up pyximport_ and imports the cython
+   module :file:`_pysam_flagstat`.  The second part calls the
+   ``count`` method in :file:`_pysam_flagstat`.
  
-2. The cython implementation :file:`_pysam_flagstat.pyx`. This script imports the pysam API via::
+2. The cython implementation :file:`_pysam_flagstat.pyx`. This script
+   imports the pysam API via::
 
-      from csamtools cimport *
+      from pysam.calignmentfile cimport AlignementFile, AlignedSegment
 
-   This statement imports, amongst others, :class:`AlignedRead` into the namespace. Speed can be
-   gained from declaring variables. For example, to efficiently iterate
-   over a file, an :class:`AlignedRead` object is declared::
+   This statement imports, amongst others, :class:`AlignedSegment`
+   into the namespace. Speed can be gained from declaring
+   variables. For example, to efficiently iterate over a file, an
+   :class:`AlignedSegment` object is declared::
 
       # loop over samfile
-      cdef AlignedRead read
+      cdef AlignedSegment read
       for read in samfile:
           ...
 
-3. A :file:`pyxbld` providing pyximport_ with build information. 
-   Required are the locations of the samtools and pysam header libraries 
-   of a source installation of pysam plus the :file:`csamtools.so` 
-   shared library. For example::
+3. A :file:`pyxbld` providing pyximport_ with build information.
+   Required are the locations of the samtools and pysam header
+   libraries of a source installation of pysam plus the
+   :file:`csamtools.so` shared library. For example::
 
      def make_ext(modname, pyxfilename):
 	 from distutils.extension import Extension
-	 import pysam, os
-	 dirname = os.path.dirname( pysam.__file__ )[:-len("pysam")]
-	 return Extension(name = modname,
-			  sources=[pyxfilename],
-			  extra_link_args=[ os.path.join( dirname, "csamtools.so")],
-			  include_dirs =  pysam.get_include(),
-			  define_macros = pysam.get_defines() )
-
-If the script :file:`pysam_flagstat.py` is called the first time, pyximport_ will 
-compile the cython_ extension :file:`_pysam_flagstat.pyx` and make it available 
-to the script. Compilation requires a working compiler and cython_ installation.
-Each time :file:`_pysam_flagstat.pyx` is modified, a new compilation will take place.
+	 import pysam
+	 return Extension(name=modname,
+               sources=[pyxfilename],
+               extra_link_args=pysam.get_libraries(),
+	       include_dirs=pysam.get_include(),
+	       define_macros=pysam.get_defines())
+
+If the script :file:`pysam_flagstat.py` is called the first time,
+pyximport_ will compile the cython_ extension
+:file:`_pysam_flagstat.pyx` and make it available to the
+script. Compilation requires a working compiler and cython_
+installation.  Each time :file:`_pysam_flagstat.pyx` is modified, a
+new compilation will take place.
 
 pyximport_ comes with cython_.
 
-.. _cython: http://cython.org/
-
-.. _pyximport: http://www.prescod.net/pyximport/
diff --git a/htslib/__init__.py b/htslib/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/htslib/bgzf.c b/htslib/bgzf.c
index 1bf799b..090bec7 100644
--- a/htslib/bgzf.c
+++ b/htslib/bgzf.c
@@ -1,7 +1,8 @@
 /* The MIT License
 
    Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
-                 2011 Attractive Chaos <attractor at live.co.uk>
+                 2011, 2012 Attractive Chaos <attractor at live.co.uk>
+   Copyright (C) 2009, 2013, 2014 Genome Research Ltd
 
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
@@ -47,7 +48,7 @@
  | 31|139|  8|  4|              0|  0|255|      6| 66| 67|      2|BLK_LEN|
  +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
   BGZF extension:
-                ^                              ^   ^   ^ 
+                ^                              ^   ^   ^
                 |                              |   |   |
                FLG.EXTRA                     XLEN  B   C
 
@@ -60,15 +61,15 @@ static const uint8_t g_magic[19] = "\037\213\010\4\0\0\0\0\0\377\6\0\102\103\2\0
 
 #ifdef BGZF_CACHE
 typedef struct {
-	int size;
-	uint8_t *block;
-	int64_t end_offset;
+    int size;
+    uint8_t *block;
+    int64_t end_offset;
 } cache_t;
 #include "htslib/khash.h"
 KHASH_MAP_INIT_INT64(cache, cache_t)
 #endif
 
-typedef struct 
+typedef struct
 {
     uint64_t uaddr;  // offset w.r.t. uncompressed data
     uint64_t caddr;  // offset w.r.t. compressed data
@@ -77,7 +78,7 @@ bgzidx1_t;
 
 struct __bgzidx_t
 {
-    int noffs, moffs;       // the size of the index, n:used, m:allocated 
+    int noffs, moffs;       // the size of the index, n:used, m:allocated
     bgzidx1_t *offs;        // offsets
     uint64_t ublock_addr;   // offset of the current block (uncompressed data)
 };
@@ -87,201 +88,229 @@ int bgzf_index_add_block(BGZF *fp);
 
 static inline void packInt16(uint8_t *buffer, uint16_t value)
 {
-	buffer[0] = value;
-	buffer[1] = value >> 8;
+    buffer[0] = value;
+    buffer[1] = value >> 8;
 }
 
 static inline int unpackInt16(const uint8_t *buffer)
 {
-	return buffer[0] | buffer[1] << 8;
+    return buffer[0] | buffer[1] << 8;
 }
 
 static inline void packInt32(uint8_t *buffer, uint32_t value)
 {
-	buffer[0] = value;
-	buffer[1] = value >> 8;
-	buffer[2] = value >> 16;
-	buffer[3] = value >> 24;
+    buffer[0] = value;
+    buffer[1] = value >> 8;
+    buffer[2] = value >> 16;
+    buffer[3] = value >> 24;
 }
 
 static BGZF *bgzf_read_init(hFILE *hfpr)
 {
-	BGZF *fp;
-	uint8_t magic[2];
-	ssize_t n = hpeek(hfpr, magic, 2);
-	if (n < 0) return NULL;
-
-	fp = (BGZF*)calloc(1, sizeof(BGZF));
-	if (fp == NULL) return NULL;
-
-	fp->is_write = 0;
-	fp->is_compressed = (n==2 && magic[0]==0x1f && magic[1]==0x8b);
-	fp->uncompressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
-	fp->compressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
+    BGZF *fp;
+    uint8_t magic[2];
+    ssize_t n = hpeek(hfpr, magic, 2);
+    if (n < 0) return NULL;
+
+    fp = (BGZF*)calloc(1, sizeof(BGZF));
+    if (fp == NULL) return NULL;
+
+    fp->is_write = 0;
+    fp->is_compressed = (n==2 && magic[0]==0x1f && magic[1]==0x8b);
+    fp->uncompressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
+    fp->compressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
 #ifdef BGZF_CACHE
-	fp->cache = kh_init(cache);
+    fp->cache = kh_init(cache);
 #endif
-	return fp;
+    return fp;
 }
 
 static BGZF *bgzf_write_init(int compress_level) // compress_level==-1 for the default level, -2 plain uncompressed
 {
-	BGZF *fp;
-	fp = (BGZF*)calloc(1, sizeof(BGZF));
-	fp->is_write = 1;
+    BGZF *fp;
+    fp = (BGZF*)calloc(1, sizeof(BGZF));
+    fp->is_write = 1;
     if ( compress_level==-2 )
     {
         fp->is_compressed = 0;
         return fp;
     }
     fp->is_compressed = 1;
-	fp->uncompressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
-	fp->compressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
-	fp->compress_level = compress_level < 0? Z_DEFAULT_COMPRESSION : compress_level; // Z_DEFAULT_COMPRESSION==-1
-	if (fp->compress_level > 9) fp->compress_level = Z_DEFAULT_COMPRESSION;
-	return fp;
+    fp->uncompressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
+    fp->compressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
+    fp->compress_level = compress_level < 0? Z_DEFAULT_COMPRESSION : compress_level; // Z_DEFAULT_COMPRESSION==-1
+    if (fp->compress_level > 9) fp->compress_level = Z_DEFAULT_COMPRESSION;
+    return fp;
 }
 // get the compress level from the mode string
 static int mode2level(const char *__restrict mode)
 {
-	int i, compress_level = -1;
-	for (i = 0; mode[i]; ++i)
-		if (mode[i] >= '0' && mode[i] <= '9') break;
-	if (mode[i]) compress_level = (int)mode[i] - '0';
-	if (strchr(mode, 'u')) compress_level = -2;
-	return compress_level;
+    int i, compress_level = -1;
+    for (i = 0; mode[i]; ++i)
+        if (mode[i] >= '0' && mode[i] <= '9') break;
+    if (mode[i]) compress_level = (int)mode[i] - '0';
+    if (strchr(mode, 'u')) compress_level = -2;
+    return compress_level;
 }
 
 BGZF *bgzf_open(const char *path, const char *mode)
 {
-	BGZF *fp = 0;
-	assert(compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE);
-	if (strchr(mode, 'r')) {
-		hFILE *fpr;
-		if ((fpr = hopen(path, mode)) == 0) return 0;
-		fp = bgzf_read_init(fpr);
-		if (fp == 0) { hclose_abruptly(fpr); return NULL; }
-		fp->fp = fpr;
-	} else if (strchr(mode, 'w') || strchr(mode, 'a')) {
-		hFILE *fpw;
-		if ((fpw = hopen(path, mode)) == 0) return 0;
-		fp = bgzf_write_init(mode2level(mode));
-		fp->fp = fpw;
-	}
-	else { errno = EINVAL; return 0; }
-
-	fp->is_be = ed_is_big();
-	return fp;
+    BGZF *fp = 0;
+    assert(compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE);
+    if (strchr(mode, 'r')) {
+        hFILE *fpr;
+        if ((fpr = hopen(path, mode)) == 0) return 0;
+        fp = bgzf_read_init(fpr);
+        if (fp == 0) { hclose_abruptly(fpr); return NULL; }
+        fp->fp = fpr;
+    } else if (strchr(mode, 'w') || strchr(mode, 'a')) {
+        hFILE *fpw;
+        if ((fpw = hopen(path, mode)) == 0) return 0;
+        fp = bgzf_write_init(mode2level(mode));
+        fp->fp = fpw;
+    }
+    else { errno = EINVAL; return 0; }
+
+    fp->is_be = ed_is_big();
+    return fp;
 }
 
 BGZF *bgzf_dopen(int fd, const char *mode)
 {
-	BGZF *fp = 0;
-	assert(compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE);
-	if (strchr(mode, 'r')) {
-		hFILE *fpr;
-		if ((fpr = hdopen(fd, mode)) == 0) return 0;
-		fp = bgzf_read_init(fpr);
-		if (fp == 0) { hclose_abruptly(fpr); return NULL; } // FIXME this closes fd
-		fp->fp = fpr;
-	} else if (strchr(mode, 'w') || strchr(mode, 'a')) {
-		hFILE *fpw;
-		if ((fpw = hdopen(fd, mode)) == 0) return 0;
-		fp = bgzf_write_init(mode2level(mode));
-		fp->fp = fpw;
-	}
-	else { errno = EINVAL; return 0; }
-
-	fp->is_be = ed_is_big();
-	return fp;
+    BGZF *fp = 0;
+    assert(compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE);
+    if (strchr(mode, 'r')) {
+        hFILE *fpr;
+        if ((fpr = hdopen(fd, mode)) == 0) return 0;
+        fp = bgzf_read_init(fpr);
+        if (fp == 0) { hclose_abruptly(fpr); return NULL; } // FIXME this closes fd
+        fp->fp = fpr;
+    } else if (strchr(mode, 'w') || strchr(mode, 'a')) {
+        hFILE *fpw;
+        if ((fpw = hdopen(fd, mode)) == 0) return 0;
+        fp = bgzf_write_init(mode2level(mode));
+        fp->fp = fpw;
+    }
+    else { errno = EINVAL; return 0; }
+
+    fp->is_be = ed_is_big();
+    return fp;
 }
 
 BGZF *bgzf_hopen(hFILE *hfp, const char *mode)
 {
-	BGZF *fp = NULL;
-	assert(compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE);
-	if (strchr(mode, 'r')) {
-		fp = bgzf_read_init(hfp);
-		if (fp == NULL) return NULL;
-	} else if (strchr(mode, 'w') || strchr(mode, 'a')) {
-		fp = bgzf_write_init(mode2level(mode));
-	}
-	else { errno = EINVAL; return 0; }
-
-	fp->fp = hfp;
-	fp->is_be = ed_is_big();
-	return fp;
+    BGZF *fp = NULL;
+    assert(compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE);
+    if (strchr(mode, 'r')) {
+        fp = bgzf_read_init(hfp);
+        if (fp == NULL) return NULL;
+    } else if (strchr(mode, 'w') || strchr(mode, 'a')) {
+        fp = bgzf_write_init(mode2level(mode));
+    }
+    else { errno = EINVAL; return 0; }
+
+    fp->fp = hfp;
+    fp->is_be = ed_is_big();
+    return fp;
 }
 
 static int bgzf_compress(void *_dst, int *dlen, void *src, int slen, int level)
 {
-	uint32_t crc;
-	z_stream zs;
-	uint8_t *dst = (uint8_t*)_dst;
-
-	// compress the body
-	zs.zalloc = NULL; zs.zfree = NULL;
-	zs.next_in  = (Bytef*)src;
-	zs.avail_in = slen;
-	zs.next_out = dst + BLOCK_HEADER_LENGTH;
-	zs.avail_out = *dlen - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;
-	if (deflateInit2(&zs, level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1; // -15 to disable zlib header/footer
-	if (deflate(&zs, Z_FINISH) != Z_STREAM_END) return -1;
-	if (deflateEnd(&zs) != Z_OK) return -1;
-	*dlen = zs.total_out + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;
-	// write the header
-	memcpy(dst, g_magic, BLOCK_HEADER_LENGTH); // the last two bytes are a place holder for the length of the block
-	packInt16(&dst[16], *dlen - 1); // write the compressed length; -1 to fit 2 bytes
-	// write the footer
-	crc = crc32(crc32(0L, NULL, 0L), (Bytef*)src, slen);
-	packInt32((uint8_t*)&dst[*dlen - 8], crc);
-	packInt32((uint8_t*)&dst[*dlen - 4], slen);
-	return 0;
+    uint32_t crc;
+    z_stream zs;
+    uint8_t *dst = (uint8_t*)_dst;
+
+    // compress the body
+    zs.zalloc = NULL; zs.zfree = NULL;
+    zs.next_in  = (Bytef*)src;
+    zs.avail_in = slen;
+    zs.next_out = dst + BLOCK_HEADER_LENGTH;
+    zs.avail_out = *dlen - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;
+    if (deflateInit2(&zs, level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1; // -15 to disable zlib header/footer
+    if (deflate(&zs, Z_FINISH) != Z_STREAM_END) return -1;
+    if (deflateEnd(&zs) != Z_OK) return -1;
+    *dlen = zs.total_out + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;
+    // write the header
+    memcpy(dst, g_magic, BLOCK_HEADER_LENGTH); // the last two bytes are a place holder for the length of the block
+    packInt16(&dst[16], *dlen - 1); // write the compressed length; -1 to fit 2 bytes
+    // write the footer
+    crc = crc32(crc32(0L, NULL, 0L), (Bytef*)src, slen);
+    packInt32((uint8_t*)&dst[*dlen - 8], crc);
+    packInt32((uint8_t*)&dst[*dlen - 4], slen);
+    return 0;
+}
+
+static int bgzf_gzip_compress(BGZF *fp, void *_dst, int *dlen, void *src, int slen, int level)
+{
+    uint8_t *dst = (uint8_t*)_dst;
+    z_stream *zs = fp->gz_stream;
+    if ( !zs )
+    {
+        zs = fp->gz_stream = (z_stream*)calloc(1,sizeof(z_stream));
+        zs->zalloc = NULL;
+        zs->zfree  = NULL;
+        if ( deflateInit2(zs, level, Z_DEFLATED, 15|16, 8, Z_DEFAULT_STRATEGY)!=Z_OK ) return -1;   // gzip output
+    }
+    int flush = slen ? Z_NO_FLUSH : Z_FINISH;
+    zs->next_in   = (Bytef*)src;
+    zs->avail_in  = slen;
+    zs->next_out  = dst;
+    zs->avail_out = *dlen;
+    if ( deflate(zs, flush) == Z_STREAM_ERROR ) return -1;
+    *dlen = *dlen - zs->avail_out;
+    return 0;
 }
 
 // Deflate the block in fp->uncompressed_block into fp->compressed_block. Also adds an extra field that stores the compressed block length.
 static int deflate_block(BGZF *fp, int block_length)
 {
-	int comp_size = BGZF_MAX_BLOCK_SIZE;
-	if (bgzf_compress(fp->compressed_block, &comp_size, fp->uncompressed_block, block_length, fp->compress_level) != 0) {
-		fp->errcode |= BGZF_ERR_ZLIB;
-		return -1;
-	}
-	fp->block_offset = 0;
-	return comp_size;
+    int comp_size = BGZF_MAX_BLOCK_SIZE;
+    int ret;
+    if ( !fp->is_gzip )
+        ret = bgzf_compress(fp->compressed_block, &comp_size, fp->uncompressed_block, block_length, fp->compress_level);
+    else
+        ret = bgzf_gzip_compress(fp, fp->compressed_block, &comp_size, fp->uncompressed_block, block_length, fp->compress_level);
+
+    if ( ret != 0 )
+    {
+        fp->errcode |= BGZF_ERR_ZLIB;
+        return -1;
+    }
+    fp->block_offset = 0;
+    return comp_size;
 }
 
 // Inflate the block in fp->compressed_block into fp->uncompressed_block
 static int inflate_block(BGZF* fp, int block_length)
 {
-	z_stream zs;
-	zs.zalloc = NULL;
-	zs.zfree = NULL;
-	zs.next_in = (Bytef*)fp->compressed_block + 18;
-	zs.avail_in = block_length - 16;
-	zs.next_out = (Bytef*)fp->uncompressed_block;
-	zs.avail_out = BGZF_MAX_BLOCK_SIZE;
-
-	if (inflateInit2(&zs, -15) != Z_OK) {
-		fp->errcode |= BGZF_ERR_ZLIB;
-		return -1;
-	}
-	if (inflate(&zs, Z_FINISH) != Z_STREAM_END) {
-		inflateEnd(&zs);
-		fp->errcode |= BGZF_ERR_ZLIB;
-		return -1;
-	}
-	if (inflateEnd(&zs) != Z_OK) {
-		fp->errcode |= BGZF_ERR_ZLIB;
-		return -1;
-	}
-	return zs.total_out;
+    z_stream zs;
+    zs.zalloc = NULL;
+    zs.zfree = NULL;
+    zs.next_in = (Bytef*)fp->compressed_block + 18;
+    zs.avail_in = block_length - 16;
+    zs.next_out = (Bytef*)fp->uncompressed_block;
+    zs.avail_out = BGZF_MAX_BLOCK_SIZE;
+
+    if (inflateInit2(&zs, -15) != Z_OK) {
+        fp->errcode |= BGZF_ERR_ZLIB;
+        return -1;
+    }
+    if (inflate(&zs, Z_FINISH) != Z_STREAM_END) {
+        inflateEnd(&zs);
+        fp->errcode |= BGZF_ERR_ZLIB;
+        return -1;
+    }
+    if (inflateEnd(&zs) != Z_OK) {
+        fp->errcode |= BGZF_ERR_ZLIB;
+        return -1;
+    }
+    return zs.total_out;
 }
 
 static int inflate_gzip_block(BGZF *fp, int cached)
 {
     int ret = Z_OK;
-    do 
+    do
     {
         if ( !cached && fp->gz_stream->avail_out!=0 )
         {
@@ -296,9 +325,10 @@ static int inflate_gzip_block(BGZF *fp, int cached)
             fp->gz_stream->next_out = (Bytef*)fp->uncompressed_block + fp->block_offset;
             fp->gz_stream->avail_out = BGZF_MAX_BLOCK_SIZE - fp->block_offset;
             ret = inflate(fp->gz_stream, Z_NO_FLUSH);
+            if ( ret==Z_BUF_ERROR ) continue;   // non-critical error
             if ( ret<0 ) return -1;
             unsigned int have = BGZF_MAX_BLOCK_SIZE - fp->gz_stream->avail_out;
-            if ( have ) return have; 
+            if ( have ) return have;
         }
         while ( fp->gz_stream->avail_out == 0 );
     }
@@ -319,60 +349,60 @@ static int check_header(const uint8_t *header)
 #ifdef BGZF_CACHE
 static void free_cache(BGZF *fp)
 {
-	khint_t k;
-	khash_t(cache) *h = (khash_t(cache)*)fp->cache;
-	if (fp->is_write) return;
-	for (k = kh_begin(h); k < kh_end(h); ++k)
-		if (kh_exist(h, k)) free(kh_val(h, k).block);
-	kh_destroy(cache, h);
+    khint_t k;
+    khash_t(cache) *h = (khash_t(cache)*)fp->cache;
+    if (fp->is_write) return;
+    for (k = kh_begin(h); k < kh_end(h); ++k)
+        if (kh_exist(h, k)) free(kh_val(h, k).block);
+    kh_destroy(cache, h);
 }
 
 static int load_block_from_cache(BGZF *fp, int64_t block_address)
 {
-	khint_t k;
-	cache_t *p;
-	khash_t(cache) *h = (khash_t(cache)*)fp->cache;
-	k = kh_get(cache, h, block_address);
-	if (k == kh_end(h)) return 0;
-	p = &kh_val(h, k);
-	if (fp->block_length != 0) fp->block_offset = 0;
-	fp->block_address = block_address;
-	fp->block_length = p->size;
-	memcpy(fp->uncompressed_block, p->block, BGZF_MAX_BLOCK_SIZE);
-	if ( hseek(fp->fp, p->end_offset, SEEK_SET) < 0 ) 
+    khint_t k;
+    cache_t *p;
+    khash_t(cache) *h = (khash_t(cache)*)fp->cache;
+    k = kh_get(cache, h, block_address);
+    if (k == kh_end(h)) return 0;
+    p = &kh_val(h, k);
+    if (fp->block_length != 0) fp->block_offset = 0;
+    fp->block_address = block_address;
+    fp->block_length = p->size;
+    memcpy(fp->uncompressed_block, p->block, BGZF_MAX_BLOCK_SIZE);
+    if ( hseek(fp->fp, p->end_offset, SEEK_SET) < 0 )
     {
         // todo: move the error up
         fprintf(stderr,"Could not hseek to %"PRId64"\n", p->end_offset);
         exit(1);
     }
-	return p->size;
+    return p->size;
 }
 
 static void cache_block(BGZF *fp, int size)
 {
-	int ret;
-	khint_t k;
-	cache_t *p;
-	khash_t(cache) *h = (khash_t(cache)*)fp->cache;
-	if (BGZF_MAX_BLOCK_SIZE >= fp->cache_size) return;
-	if ((kh_size(h) + 1) * BGZF_MAX_BLOCK_SIZE > (uint32_t)fp->cache_size) {
-		/* A better way would be to remove the oldest block in the
-		 * cache, but here we remove a random one for simplicity. This
-		 * should not have a big impact on performance. */
-		for (k = kh_begin(h); k < kh_end(h); ++k)
-			if (kh_exist(h, k)) break;
-		if (k < kh_end(h)) {
-			free(kh_val(h, k).block);
-			kh_del(cache, h, k);
-		}
-	}
-	k = kh_put(cache, h, fp->block_address, &ret);
-	if (ret == 0) return; // if this happens, a bug!
-	p = &kh_val(h, k);
-	p->size = fp->block_length;
-	p->end_offset = fp->block_address + size;
-	p->block = (uint8_t*)malloc(BGZF_MAX_BLOCK_SIZE);
-	memcpy(kh_val(h, k).block, fp->uncompressed_block, BGZF_MAX_BLOCK_SIZE);
+    int ret;
+    khint_t k;
+    cache_t *p;
+    khash_t(cache) *h = (khash_t(cache)*)fp->cache;
+    if (BGZF_MAX_BLOCK_SIZE >= fp->cache_size) return;
+    if ((kh_size(h) + 1) * BGZF_MAX_BLOCK_SIZE > (uint32_t)fp->cache_size) {
+        /* A better way would be to remove the oldest block in the
+         * cache, but here we remove a random one for simplicity. This
+         * should not have a big impact on performance. */
+        for (k = kh_begin(h); k < kh_end(h); ++k)
+            if (kh_exist(h, k)) break;
+        if (k < kh_end(h)) {
+            free(kh_val(h, k).block);
+            kh_del(cache, h, k);
+        }
+    }
+    k = kh_put(cache, h, fp->block_address, &ret);
+    if (ret == 0) return; // if this happens, a bug!
+    p = &kh_val(h, k);
+    p->size = fp->block_length;
+    p->end_offset = fp->block_address + size;
+    p->block = (uint8_t*)malloc(BGZF_MAX_BLOCK_SIZE);
+    memcpy(kh_val(h, k).block, fp->uncompressed_block, BGZF_MAX_BLOCK_SIZE);
 }
 #else
 static void free_cache(BGZF *fp) {}
@@ -382,14 +412,14 @@ static void cache_block(BGZF *fp, int size) {}
 
 int bgzf_read_block(BGZF *fp)
 {
-	uint8_t header[BLOCK_HEADER_LENGTH], *compressed_block;
-	int count, size = 0, block_length, remaining;
+    uint8_t header[BLOCK_HEADER_LENGTH], *compressed_block;
+    int count, size = 0, block_length, remaining;
 
     // Reading an uncompressed file
     if ( !fp->is_compressed )
     {
         count = hread(fp->fp, fp->uncompressed_block, BGZF_MAX_BLOCK_SIZE);
-        if ( count==0 ) 
+        if ( count==0 )
         {
             fp->block_length = 0;
             return 0;
@@ -401,8 +431,8 @@ int bgzf_read_block(BGZF *fp)
     }
 
     // Reading compressed file
-	int64_t block_address;
-	block_address = htell(fp->fp);
+    int64_t block_address;
+    block_address = htell(fp->fp);
     if ( fp->is_gzip )
     {
         count = inflate_gzip_block(fp, 0);
@@ -415,14 +445,14 @@ int bgzf_read_block(BGZF *fp)
         fp->block_address = block_address;
         return 0;
     }
-	if (fp->cache_size && load_block_from_cache(fp, block_address)) return 0;
+    if (fp->cache_size && load_block_from_cache(fp, block_address)) return 0;
     count = hread(fp->fp, header, sizeof(header));
     if (count == 0) { // no data read
         fp->block_length = 0;
         return 0;
     }
     int ret;
-    if ( count != sizeof(header) || (ret=check_header(header))==-2 ) 
+    if ( count != sizeof(header) || (ret=check_header(header))==-2 )
     {
         fp->errcode |= BGZF_ERR_HEADER;
         return -1;
@@ -444,7 +474,7 @@ int bgzf_read_block(BGZF *fp)
         if ( header[3] & 0x8 ) // FLG.FNAME
         {
             while ( nskip<BGZF_BLOCK_SIZE && cblock[nskip] ) nskip++;
-            if ( nskip==BGZF_BLOCK_SIZE ) 
+            if ( nskip==BGZF_BLOCK_SIZE )
             {
                 fp->errcode |= BGZF_ERR_HEADER;
                 return -1;
@@ -454,7 +484,7 @@ int bgzf_read_block(BGZF *fp)
         if ( header[3] & 0x10 ) // FLG.FCOMMENT
         {
             while ( nskip<BGZF_BLOCK_SIZE && cblock[nskip] ) nskip++;
-            if ( nskip==BGZF_BLOCK_SIZE ) 
+            if ( nskip==BGZF_BLOCK_SIZE )
             {
                 fp->errcode |= BGZF_ERR_HEADER;
                 return -1;
@@ -466,7 +496,7 @@ int bgzf_read_block(BGZF *fp)
         fp->is_gzip = 1;
         fp->gz_stream = (z_stream*) calloc(1,sizeof(z_stream));
         int ret = inflateInit2(fp->gz_stream, -15);
-        if (ret != Z_OK) 
+        if (ret != Z_OK)
         {
             fp->errcode |= BGZF_ERR_ZLIB;
             return -1;
@@ -481,251 +511,252 @@ int bgzf_read_block(BGZF *fp)
         }
         fp->block_length = count;
         fp->block_address = block_address;
+        if ( fp->idx_build_otf ) return -1; // cannot build index for gzip
         return 0;
     }
-	size = count;
-	block_length = unpackInt16((uint8_t*)&header[16]) + 1; // +1 because when writing this number, we used "-1"
-	compressed_block = (uint8_t*)fp->compressed_block;
-	memcpy(compressed_block, header, BLOCK_HEADER_LENGTH);
-	remaining = block_length - BLOCK_HEADER_LENGTH;
-	count = hread(fp->fp, &compressed_block[BLOCK_HEADER_LENGTH], remaining);
-	if (count != remaining) {
-		fp->errcode |= BGZF_ERR_IO;
-		return -1;
-	}
-	size += count;
-	if ((count = inflate_block(fp, block_length)) < 0) return -1;
-	if (fp->block_length != 0) fp->block_offset = 0; // Do not reset offset if this read follows a seek.
-	fp->block_address = block_address;
-	fp->block_length = count;
-    if ( fp->idx_build_otf ) 
+    size = count;
+    block_length = unpackInt16((uint8_t*)&header[16]) + 1; // +1 because when writing this number, we used "-1"
+    compressed_block = (uint8_t*)fp->compressed_block;
+    memcpy(compressed_block, header, BLOCK_HEADER_LENGTH);
+    remaining = block_length - BLOCK_HEADER_LENGTH;
+    count = hread(fp->fp, &compressed_block[BLOCK_HEADER_LENGTH], remaining);
+    if (count != remaining) {
+        fp->errcode |= BGZF_ERR_IO;
+        return -1;
+    }
+    size += count;
+    if ((count = inflate_block(fp, block_length)) < 0) return -1;
+    if (fp->block_length != 0) fp->block_offset = 0; // Do not reset offset if this read follows a seek.
+    fp->block_address = block_address;
+    fp->block_length = count;
+    if ( fp->idx_build_otf )
     {
         bgzf_index_add_block(fp);
         fp->idx->ublock_addr += count;
     }
-	cache_block(fp, size);
-	return 0;
+    cache_block(fp, size);
+    return 0;
 }
 
 ssize_t bgzf_read(BGZF *fp, void *data, size_t length)
 {
-	ssize_t bytes_read = 0;
-	uint8_t *output = (uint8_t*)data;
-	if (length <= 0) return 0;
-	assert(fp->is_write == 0);
-	while (bytes_read < length) {
-		int copy_length, available = fp->block_length - fp->block_offset;
-		uint8_t *buffer;
-		if (available <= 0) {
-			if (bgzf_read_block(fp) != 0) return -1;
-			available = fp->block_length - fp->block_offset;
-			if (available <= 0) break;
-		}
-		copy_length = length - bytes_read < available? length - bytes_read : available;
-		buffer = (uint8_t*)fp->uncompressed_block;
-		memcpy(output, buffer + fp->block_offset, copy_length);
-		fp->block_offset += copy_length;
-		output += copy_length;
-		bytes_read += copy_length;
-	}
-	if (fp->block_offset == fp->block_length) {
-		fp->block_address = htell(fp->fp);
-		fp->block_offset = fp->block_length = 0;
-	}
+    ssize_t bytes_read = 0;
+    uint8_t *output = (uint8_t*)data;
+    if (length <= 0) return 0;
+    assert(fp->is_write == 0);
+    while (bytes_read < length) {
+        int copy_length, available = fp->block_length - fp->block_offset;
+        uint8_t *buffer;
+        if (available <= 0) {
+            if (bgzf_read_block(fp) != 0) return -1;
+            available = fp->block_length - fp->block_offset;
+            if (available <= 0) break;
+        }
+        copy_length = length - bytes_read < available? length - bytes_read : available;
+        buffer = (uint8_t*)fp->uncompressed_block;
+        memcpy(output, buffer + fp->block_offset, copy_length);
+        fp->block_offset += copy_length;
+        output += copy_length;
+        bytes_read += copy_length;
+    }
+    if (fp->block_offset == fp->block_length) {
+        fp->block_address = htell(fp->fp);
+        fp->block_offset = fp->block_length = 0;
+    }
     fp->uncompressed_address += bytes_read;
-	return bytes_read;
+    return bytes_read;
 }
 
 ssize_t bgzf_raw_read(BGZF *fp, void *data, size_t length)
 {
-	return hread(fp->fp, data, length);
+    return hread(fp->fp, data, length);
 }
 
 #ifdef BGZF_MT
 
 typedef struct {
-	struct bgzf_mtaux_t *mt;
-	void *buf;
-	int i, errcode, toproc, compress_level;
+    struct bgzf_mtaux_t *mt;
+    void *buf;
+    int i, errcode, toproc, compress_level;
 } worker_t;
 
 typedef struct bgzf_mtaux_t {
-	int n_threads, n_blks, curr, done;
-	volatile int proc_cnt;
-	void **blk;
-	int *len;
-	worker_t *w;
-	pthread_t *tid;
-	pthread_mutex_t lock;
-	pthread_cond_t cv;
+    int n_threads, n_blks, curr, done;
+    volatile int proc_cnt;
+    void **blk;
+    int *len;
+    worker_t *w;
+    pthread_t *tid;
+    pthread_mutex_t lock;
+    pthread_cond_t cv;
 } mtaux_t;
 
 static int worker_aux(worker_t *w)
 {
-	int i, stop = 0;
-	// wait for condition: to process or all done
-	pthread_mutex_lock(&w->mt->lock);
-	while (!w->toproc && !w->mt->done)
-		pthread_cond_wait(&w->mt->cv, &w->mt->lock);
-	if (w->mt->done) stop = 1;
-	w->toproc = 0;
-	pthread_mutex_unlock(&w->mt->lock);
-	if (stop) return 1; // to quit the thread
-	w->errcode = 0;
-	for (i = w->i; i < w->mt->curr; i += w->mt->n_threads) {
-		int clen = BGZF_MAX_BLOCK_SIZE;
-		if (bgzf_compress(w->buf, &clen, w->mt->blk[i], w->mt->len[i], w->compress_level) != 0)
-			w->errcode |= BGZF_ERR_ZLIB;
-		memcpy(w->mt->blk[i], w->buf, clen);
-		w->mt->len[i] = clen;
-	}
-	__sync_fetch_and_add(&w->mt->proc_cnt, 1);
-	return 0;
+    int i, stop = 0;
+    // wait for condition: to process or all done
+    pthread_mutex_lock(&w->mt->lock);
+    while (!w->toproc && !w->mt->done)
+        pthread_cond_wait(&w->mt->cv, &w->mt->lock);
+    if (w->mt->done) stop = 1;
+    w->toproc = 0;
+    pthread_mutex_unlock(&w->mt->lock);
+    if (stop) return 1; // to quit the thread
+    w->errcode = 0;
+    for (i = w->i; i < w->mt->curr; i += w->mt->n_threads) {
+        int clen = BGZF_MAX_BLOCK_SIZE;
+        if (bgzf_compress(w->buf, &clen, w->mt->blk[i], w->mt->len[i], w->compress_level) != 0)
+            w->errcode |= BGZF_ERR_ZLIB;
+        memcpy(w->mt->blk[i], w->buf, clen);
+        w->mt->len[i] = clen;
+    }
+    __sync_fetch_and_add(&w->mt->proc_cnt, 1);
+    return 0;
 }
 
 static void *mt_worker(void *data)
 {
-	while (worker_aux((worker_t*)data) == 0);
-	return 0;
+    while (worker_aux((worker_t*)data) == 0);
+    return 0;
 }
 
 int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks)
 {
-	int i;
-	mtaux_t *mt;
-	pthread_attr_t attr;
-	if (!fp->is_write || fp->mt || n_threads <= 1) return -1;
-	mt = (mtaux_t*)calloc(1, sizeof(mtaux_t));
-	mt->n_threads = n_threads;
-	mt->n_blks = n_threads * n_sub_blks;
-	mt->len = (int*)calloc(mt->n_blks, sizeof(int));
-	mt->blk = (void**)calloc(mt->n_blks, sizeof(void*));
-	for (i = 0; i < mt->n_blks; ++i)
-		mt->blk[i] = malloc(BGZF_MAX_BLOCK_SIZE);
-	mt->tid = (pthread_t*)calloc(mt->n_threads, sizeof(pthread_t)); // tid[0] is not used, as the worker 0 is launched by the master
-	mt->w = (worker_t*)calloc(mt->n_threads, sizeof(worker_t));
-	for (i = 0; i < mt->n_threads; ++i) {
-		mt->w[i].i = i;
-		mt->w[i].mt = mt;
-		mt->w[i].compress_level = fp->compress_level;
-		mt->w[i].buf = malloc(BGZF_MAX_BLOCK_SIZE);
-	}
-	pthread_attr_init(&attr);
-	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
-	pthread_mutex_init(&mt->lock, 0);
-	pthread_cond_init(&mt->cv, 0);
-	for (i = 1; i < mt->n_threads; ++i) // worker 0 is effectively launched by the master thread
-		pthread_create(&mt->tid[i], &attr, mt_worker, &mt->w[i]);
-	fp->mt = mt;
-	return 0;
+    int i;
+    mtaux_t *mt;
+    pthread_attr_t attr;
+    if (!fp->is_write || fp->mt || n_threads <= 1) return -1;
+    mt = (mtaux_t*)calloc(1, sizeof(mtaux_t));
+    mt->n_threads = n_threads;
+    mt->n_blks = n_threads * n_sub_blks;
+    mt->len = (int*)calloc(mt->n_blks, sizeof(int));
+    mt->blk = (void**)calloc(mt->n_blks, sizeof(void*));
+    for (i = 0; i < mt->n_blks; ++i)
+        mt->blk[i] = malloc(BGZF_MAX_BLOCK_SIZE);
+    mt->tid = (pthread_t*)calloc(mt->n_threads, sizeof(pthread_t)); // tid[0] is not used, as the worker 0 is launched by the master
+    mt->w = (worker_t*)calloc(mt->n_threads, sizeof(worker_t));
+    for (i = 0; i < mt->n_threads; ++i) {
+        mt->w[i].i = i;
+        mt->w[i].mt = mt;
+        mt->w[i].compress_level = fp->compress_level;
+        mt->w[i].buf = malloc(BGZF_MAX_BLOCK_SIZE);
+    }
+    pthread_attr_init(&attr);
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
+    pthread_mutex_init(&mt->lock, 0);
+    pthread_cond_init(&mt->cv, 0);
+    for (i = 1; i < mt->n_threads; ++i) // worker 0 is effectively launched by the master thread
+        pthread_create(&mt->tid[i], &attr, mt_worker, &mt->w[i]);
+    fp->mt = mt;
+    return 0;
 }
 
 static void mt_destroy(mtaux_t *mt)
 {
-	int i;
-	// signal all workers to quit
-	pthread_mutex_lock(&mt->lock);
-	mt->done = 1; mt->proc_cnt = 0;
-	pthread_cond_broadcast(&mt->cv);
-	pthread_mutex_unlock(&mt->lock);
-	for (i = 1; i < mt->n_threads; ++i) pthread_join(mt->tid[i], 0); // worker 0 is effectively launched by the master thread
-	// free other data allocated on heap
-	for (i = 0; i < mt->n_blks; ++i) free(mt->blk[i]);
-	for (i = 0; i < mt->n_threads; ++i) free(mt->w[i].buf);
-	free(mt->blk); free(mt->len); free(mt->w); free(mt->tid);
-	pthread_cond_destroy(&mt->cv);
-	pthread_mutex_destroy(&mt->lock);
-	free(mt);
+    int i;
+    // signal all workers to quit
+    pthread_mutex_lock(&mt->lock);
+    mt->done = 1; mt->proc_cnt = 0;
+    pthread_cond_broadcast(&mt->cv);
+    pthread_mutex_unlock(&mt->lock);
+    for (i = 1; i < mt->n_threads; ++i) pthread_join(mt->tid[i], 0); // worker 0 is effectively launched by the master thread
+    // free other data allocated on heap
+    for (i = 0; i < mt->n_blks; ++i) free(mt->blk[i]);
+    for (i = 0; i < mt->n_threads; ++i) free(mt->w[i].buf);
+    free(mt->blk); free(mt->len); free(mt->w); free(mt->tid);
+    pthread_cond_destroy(&mt->cv);
+    pthread_mutex_destroy(&mt->lock);
+    free(mt);
 }
 
 static void mt_queue(BGZF *fp)
 {
-	mtaux_t *mt = fp->mt;
-	assert(mt->curr < mt->n_blks); // guaranteed by the caller
-	memcpy(mt->blk[mt->curr], fp->uncompressed_block, fp->block_offset);
-	mt->len[mt->curr] = fp->block_offset;
-	fp->block_offset = 0;
-	++mt->curr;
+    mtaux_t *mt = fp->mt;
+    assert(mt->curr < mt->n_blks); // guaranteed by the caller
+    memcpy(mt->blk[mt->curr], fp->uncompressed_block, fp->block_offset);
+    mt->len[mt->curr] = fp->block_offset;
+    fp->block_offset = 0;
+    ++mt->curr;
 }
 
 static int mt_flush_queue(BGZF *fp)
 {
-	int i;
-	mtaux_t *mt = fp->mt;
-	// signal all the workers to compress
-	pthread_mutex_lock(&mt->lock);
-	for (i = 0; i < mt->n_threads; ++i) mt->w[i].toproc = 1;
-	mt->proc_cnt = 0;
-	pthread_cond_broadcast(&mt->cv);
-	pthread_mutex_unlock(&mt->lock);
-	// worker 0 is doing things here
-	worker_aux(&mt->w[0]);
-	// wait for all the threads to complete
-	while (mt->proc_cnt < mt->n_threads);
-	// dump data to disk
-	for (i = 0; i < mt->n_threads; ++i) fp->errcode |= mt->w[i].errcode;
-	for (i = 0; i < mt->curr; ++i)
-		if (hwrite(fp->fp, mt->blk[i], mt->len[i]) != mt->len[i]) {
-			fp->errcode |= BGZF_ERR_IO;
-			break;
-		}
-	mt->curr = 0;
-	return (fp->errcode == 0)? 0 : -1;
+    int i;
+    mtaux_t *mt = fp->mt;
+    // signal all the workers to compress
+    pthread_mutex_lock(&mt->lock);
+    for (i = 0; i < mt->n_threads; ++i) mt->w[i].toproc = 1;
+    mt->proc_cnt = 0;
+    pthread_cond_broadcast(&mt->cv);
+    pthread_mutex_unlock(&mt->lock);
+    // worker 0 is doing things here
+    worker_aux(&mt->w[0]);
+    // wait for all the threads to complete
+    while (mt->proc_cnt < mt->n_threads);
+    // dump data to disk
+    for (i = 0; i < mt->n_threads; ++i) fp->errcode |= mt->w[i].errcode;
+    for (i = 0; i < mt->curr; ++i)
+        if (hwrite(fp->fp, mt->blk[i], mt->len[i]) != mt->len[i]) {
+            fp->errcode |= BGZF_ERR_IO;
+            break;
+        }
+    mt->curr = 0;
+    return (fp->errcode == 0)? 0 : -1;
 }
 
 static int lazy_flush(BGZF *fp)
 {
-	if (fp->mt) {
-		if (fp->block_offset) mt_queue(fp);
-		return (fp->mt->curr < fp->mt->n_blks)? 0 : mt_flush_queue(fp);
-	}
-	else return bgzf_flush(fp);
+    if (fp->mt) {
+        if (fp->block_offset) mt_queue(fp);
+        return (fp->mt->curr < fp->mt->n_blks)? 0 : mt_flush_queue(fp);
+    }
+    else return bgzf_flush(fp);
 }
 
 #else  // ~ #ifdef BGZF_MT
 
 int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks)
 {
-	return 0;
+    return 0;
 }
 
 static inline int lazy_flush(BGZF *fp)
 {
-	return bgzf_flush(fp);
+    return bgzf_flush(fp);
 }
 
 #endif // ~ #ifdef BGZF_MT
 
 int bgzf_flush(BGZF *fp)
 {
-	if (!fp->is_write) return 0;
+    if (!fp->is_write) return 0;
 #ifdef BGZF_MT
-	if (fp->mt) {
-		if (fp->block_offset) mt_queue(fp); // guaranteed that assertion does not fail
-		return mt_flush_queue(fp);
-	}
+    if (fp->mt) {
+        if (fp->block_offset) mt_queue(fp); // guaranteed that assertion does not fail
+        return mt_flush_queue(fp);
+    }
 #endif
-	while (fp->block_offset > 0) {
-        if ( fp->idx_build_otf ) 
+    while (fp->block_offset > 0) {
+        if ( fp->idx_build_otf )
         {
             bgzf_index_add_block(fp);
             fp->idx->ublock_addr += fp->block_offset;
         }
-		int block_length = deflate_block(fp, fp->block_offset);
-		if (block_length < 0) return -1;
-		if (hwrite(fp->fp, fp->compressed_block, block_length) != block_length) {
-			fp->errcode |= BGZF_ERR_IO; // possibly truncated file
-			return -1;
-		}
-		fp->block_address += block_length;
-	}
-	return 0;
+        int block_length = deflate_block(fp, fp->block_offset);
+        if (block_length < 0) return -1;
+        if (hwrite(fp->fp, fp->compressed_block, block_length) != block_length) {
+            fp->errcode |= BGZF_ERR_IO; // possibly truncated file
+            return -1;
+        }
+        fp->block_address += block_length;
+    }
+    return 0;
 }
 
 int bgzf_flush_try(BGZF *fp, ssize_t size)
 {
-	if (fp->block_offset + size > BGZF_BLOCK_SIZE) return lazy_flush(fp);
-	return 0;
+    if (fp->block_offset + size > BGZF_BLOCK_SIZE) return lazy_flush(fp);
+    return 0;
 }
 
 ssize_t bgzf_write(BGZF *fp, const void *data, size_t length)
@@ -733,127 +764,128 @@ ssize_t bgzf_write(BGZF *fp, const void *data, size_t length)
     if ( !fp->is_compressed )
         return hwrite(fp->fp, data, length);
 
-	const uint8_t *input = (const uint8_t*)data;
-	ssize_t remaining = length;
-	assert(fp->is_write);
-	while (remaining > 0) {
-		uint8_t* buffer = (uint8_t*)fp->uncompressed_block;
-		int copy_length = BGZF_BLOCK_SIZE - fp->block_offset;
-		if (copy_length > remaining) copy_length = remaining;
-		memcpy(buffer + fp->block_offset, input, copy_length);
-		fp->block_offset += copy_length;
-		input += copy_length;
-		remaining -= copy_length;
-		if (fp->block_offset == BGZF_BLOCK_SIZE) {
-			if (lazy_flush(fp) != 0) return -1;
-		}
-	}
-	return length - remaining;
+    const uint8_t *input = (const uint8_t*)data;
+    ssize_t remaining = length;
+    assert(fp->is_write);
+    while (remaining > 0) {
+        uint8_t* buffer = (uint8_t*)fp->uncompressed_block;
+        int copy_length = BGZF_BLOCK_SIZE - fp->block_offset;
+        if (copy_length > remaining) copy_length = remaining;
+        memcpy(buffer + fp->block_offset, input, copy_length);
+        fp->block_offset += copy_length;
+        input += copy_length;
+        remaining -= copy_length;
+        if (fp->block_offset == BGZF_BLOCK_SIZE) {
+            if (lazy_flush(fp) != 0) return -1;
+        }
+    }
+    return length - remaining;
 }
 
 ssize_t bgzf_raw_write(BGZF *fp, const void *data, size_t length)
 {
-	return hwrite(fp->fp, data, length);
+    return hwrite(fp->fp, data, length);
 }
 
 int bgzf_close(BGZF* fp)
 {
-	int ret, block_length;
-	if (fp == 0) return -1;
-	if (fp->is_write && fp->is_compressed) {
-		if (bgzf_flush(fp) != 0) return -1;
-		fp->compress_level = -1;
-		block_length = deflate_block(fp, 0); // write an empty block
-		if (hwrite(fp->fp, fp->compressed_block, block_length) < 0
-			|| hflush(fp->fp) != 0) {
-			fp->errcode |= BGZF_ERR_IO;
-			return -1;
-		}
+    int ret, block_length;
+    if (fp == 0) return -1;
+    if (fp->is_write && fp->is_compressed) {
+        if (bgzf_flush(fp) != 0) return -1;
+        fp->compress_level = -1;
+        block_length = deflate_block(fp, 0); // write an empty block
+        if (hwrite(fp->fp, fp->compressed_block, block_length) < 0
+            || hflush(fp->fp) != 0) {
+            fp->errcode |= BGZF_ERR_IO;
+            return -1;
+        }
 #ifdef BGZF_MT
-		if (fp->mt) mt_destroy(fp->mt);
+        if (fp->mt) mt_destroy(fp->mt);
 #endif
-	}
+    }
     if ( fp->is_gzip )
     {
-        (void)inflateEnd(fp->gz_stream);
+        if (!fp->is_write) (void)inflateEnd(fp->gz_stream);
+        else (void)deflateEnd(fp->gz_stream);
         free(fp->gz_stream);
     }
-	ret = hclose(fp->fp);
-	if (ret != 0) return -1;
+    ret = hclose(fp->fp);
+    if (ret != 0) return -1;
     bgzf_index_destroy(fp);
-	free(fp->uncompressed_block);
-	free(fp->compressed_block);
-	free_cache(fp);
-	free(fp);
-	return 0;
+    free(fp->uncompressed_block);
+    free(fp->compressed_block);
+    free_cache(fp);
+    free(fp);
+    return 0;
 }
 
 void bgzf_set_cache_size(BGZF *fp, int cache_size)
 {
-	if (fp) fp->cache_size = cache_size;
+    if (fp) fp->cache_size = cache_size;
 }
 
 int bgzf_check_EOF(BGZF *fp)
 {
-	uint8_t buf[28];
-	off_t offset = htell(fp->fp);
-	if (hseek(fp->fp, -28, SEEK_END) < 0) {
-		if (errno == ESPIPE) { hclearerr(fp->fp); return 2; }
-		else return -1;
-	}
-	if ( hread(fp->fp, buf, 28) != 28 ) return -1;
-	if ( hseek(fp->fp, offset, SEEK_SET) < 0 ) return -1;
-	return (memcmp("\037\213\010\4\0\0\0\0\0\377\6\0\102\103\2\0\033\0\3\0\0\0\0\0\0\0\0\0", buf, 28) == 0)? 1 : 0;
+    uint8_t buf[28];
+    off_t offset = htell(fp->fp);
+    if (hseek(fp->fp, -28, SEEK_END) < 0) {
+        if (errno == ESPIPE) { hclearerr(fp->fp); return 2; }
+        else return -1;
+    }
+    if ( hread(fp->fp, buf, 28) != 28 ) return -1;
+    if ( hseek(fp->fp, offset, SEEK_SET) < 0 ) return -1;
+    return (memcmp("\037\213\010\4\0\0\0\0\0\377\6\0\102\103\2\0\033\0\3\0\0\0\0\0\0\0\0\0", buf, 28) == 0)? 1 : 0;
 }
 
 int64_t bgzf_seek(BGZF* fp, int64_t pos, int where)
 {
-	int block_offset;
-	int64_t block_address;
-
-	if (fp->is_write || where != SEEK_SET) {
-		fp->errcode |= BGZF_ERR_MISUSE;
-		return -1;
-	}
-	block_offset = pos & 0xFFFF;
-	block_address = pos >> 16;
-	if (hseek(fp->fp, block_address, SEEK_SET) < 0) {
-		fp->errcode |= BGZF_ERR_IO;
-		return -1;
-	}
-	fp->block_length = 0;  // indicates current block has not been loaded
-	fp->block_address = block_address << 16;
-	fp->block_offset = block_offset;
-	return 0;
+    int block_offset;
+    int64_t block_address;
+
+    if (fp->is_write || where != SEEK_SET) {
+        fp->errcode |= BGZF_ERR_MISUSE;
+        return -1;
+    }
+    block_offset = pos & 0xFFFF;
+    block_address = pos >> 16;
+    if (hseek(fp->fp, block_address, SEEK_SET) < 0) {
+        fp->errcode |= BGZF_ERR_IO;
+        return -1;
+    }
+    fp->block_length = 0;  // indicates current block has not been loaded
+    fp->block_address = block_address;
+    fp->block_offset = block_offset;
+    return 0;
 }
 
 int bgzf_is_bgzf(const char *fn)
 {
-	uint8_t buf[16];
-	int n;
-	hFILE *fp;
-	if ((fp = hopen(fn, "r")) == 0) return 0;
-	n = hread(fp, buf, 16);
-	if ( hclose(fp) < 0 ) return -1;
-	if (n != 16) return 0;
-	return memcmp(g_magic, buf, 16) == 0? 1 : 0;
+    uint8_t buf[16];
+    int n;
+    hFILE *fp;
+    if ((fp = hopen(fn, "r")) == 0) return 0;
+    n = hread(fp, buf, 16);
+    if ( hclose(fp) < 0 ) return -1;
+    if (n != 16) return 0;
+    return memcmp(g_magic, buf, 16) == 0? 1 : 0;
 }
 
 int bgzf_getc(BGZF *fp)
 {
-	int c;
-	if (fp->block_offset >= fp->block_length) {
-		if (bgzf_read_block(fp) != 0) return -2; /* error */
-		if (fp->block_length == 0) return -1; /* end-of-file */
-	}
-	c = ((unsigned char*)fp->uncompressed_block)[fp->block_offset++];
+    int c;
+    if (fp->block_offset >= fp->block_length) {
+        if (bgzf_read_block(fp) != 0) return -2; /* error */
+        if (fp->block_length == 0) return -1; /* end-of-file */
+    }
+    c = ((unsigned char*)fp->uncompressed_block)[fp->block_offset++];
     if (fp->block_offset == fp->block_length) {
         fp->block_address = htell(fp->fp);
         fp->block_offset = 0;
         fp->block_length = 0;
     }
     fp->uncompressed_address++;
-	return c;
+    return c;
 }
 
 #ifndef kroundup32
@@ -862,35 +894,36 @@ int bgzf_getc(BGZF *fp)
 
 int bgzf_getline(BGZF *fp, int delim, kstring_t *str)
 {
-	int l, state = 0;
-	unsigned char *buf = (unsigned char*)fp->uncompressed_block;
-	str->l = 0;
-	do {
-		if (fp->block_offset >= fp->block_length) {
-			if (bgzf_read_block(fp) != 0) { state = -2; break; }
-			if (fp->block_length == 0) { state = -1; break; }
-		}
-		for (l = fp->block_offset; l < fp->block_length && buf[l] != delim; ++l);
-		if (l < fp->block_length) state = 1;
-		l -= fp->block_offset;
-		if (str->l + l + 1 >= str->m) {
-			str->m = str->l + l + 2;
-			kroundup32(str->m);
-			str->s = (char*)realloc(str->s, str->m);
-		}
-		memcpy(str->s + str->l, buf + fp->block_offset, l);
-		str->l += l;
-		fp->block_offset += l + 1;
-		if (fp->block_offset >= fp->block_length) {
-			fp->block_address = htell(fp->fp);
-			fp->block_offset = 0;
-			fp->block_length = 0;
-		} 
-	} while (state == 0);
-	if (str->l == 0 && state < 0) return state;
+    int l, state = 0;
+    unsigned char *buf = (unsigned char*)fp->uncompressed_block;
+    str->l = 0;
+    do {
+        if (fp->block_offset >= fp->block_length) {
+            if (bgzf_read_block(fp) != 0) { state = -2; break; }
+            if (fp->block_length == 0) { state = -1; break; }
+        }
+        for (l = fp->block_offset; l < fp->block_length && buf[l] != delim; ++l);
+        if (l < fp->block_length) state = 1;
+        l -= fp->block_offset;
+        if (str->l + l + 1 >= str->m) {
+            str->m = str->l + l + 2;
+            kroundup32(str->m);
+            str->s = (char*)realloc(str->s, str->m);
+        }
+        memcpy(str->s + str->l, buf + fp->block_offset, l);
+        str->l += l;
+        fp->block_offset += l + 1;
+        if (fp->block_offset >= fp->block_length) {
+            fp->block_address = htell(fp->fp);
+            fp->block_offset = 0;
+            fp->block_length = 0;
+        }
+    } while (state == 0);
+    if (str->l == 0 && state < 0) return state;
     fp->uncompressed_address += str->l;
-	str->s[str->l] = 0;
-	return str->l;
+    if ( delim=='\n' && str->l>0 && str->s[str->l-1]=='\r' ) str->l--;
+    str->s[str->l] = 0;
+    return str->l;
 }
 
 void bgzf_index_destroy(BGZF *fp)
diff --git a/htslib/bgzip.c b/htslib/bgzip.c
index bcd81f8..a8a88af 100644
--- a/htslib/bgzip.c
+++ b/htslib/bgzip.c
@@ -1,6 +1,7 @@
-/* The MIT License
+/* bgzip.c -- Block compression/decompression utility.
 
-   Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
+   Copyright (C) 2008, 2009 Broad Institute / Massachusetts Institute of Technology
+   Copyright (C) 2010, 2013, 2014 Genome Research Ltd.
 
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
@@ -9,7 +10,7 @@
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
 
-   The above copyright notice and this permission notice shall be included in
+   The above copyright notices and this permission notice shall be included in
    all copies or substantial portions of the Software.
 
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -47,55 +48,55 @@ static void error(const char *format, ...)
 
 static int write_open(const char *fn, int is_forced)
 {
-	int fd = -1;
-	char c;
-	if (!is_forced) {
-		if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0666)) < 0 && errno == EEXIST) {
-			fprintf(stderr, "[bgzip] %s already exists; do you wish to overwrite (y or n)? ", fn);
-			if ( scanf("%c", &c) != 1 ) c = 'n';
-			if (c != 'Y' && c != 'y') {
-				fprintf(stderr, "[bgzip] not overwritten\n");
-				exit(EXIT_FAILURE);
-			}
-		}
-	}
-	if (fd < 0) {
-		if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
-			fprintf(stderr, "[bgzip] %s: Fail to write\n", fn);
-			exit(EXIT_FAILURE);
-		}
-	}
-	return fd;
+    int fd = -1;
+    char c;
+    if (!is_forced) {
+        if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0666)) < 0 && errno == EEXIST) {
+            fprintf(stderr, "[bgzip] %s already exists; do you wish to overwrite (y or n)? ", fn);
+            if ( scanf("%c", &c) != 1 ) c = 'n';
+            if (c != 'Y' && c != 'y') {
+                fprintf(stderr, "[bgzip] not overwritten\n");
+                exit(EXIT_FAILURE);
+            }
+        }
+    }
+    if (fd < 0) {
+        if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
+            fprintf(stderr, "[bgzip] %s: Fail to write\n", fn);
+            exit(EXIT_FAILURE);
+        }
+    }
+    return fd;
 }
 
 static int bgzip_main_usage(void)
 {
-	fprintf(stderr, "\n");
-	fprintf(stderr, "Version: %s\n", hts_version());
-	fprintf(stderr, "Usage:   bgzip [OPTIONS] [FILE] ...\n");
-	fprintf(stderr, "Options:\n");
-	fprintf(stderr, "   -b, --offset INT        decompress at virtual file pointer (0-based uncompressed offset)\n");
+    fprintf(stderr, "\n");
+    fprintf(stderr, "Version: %s\n", hts_version());
+    fprintf(stderr, "Usage:   bgzip [OPTIONS] [FILE] ...\n");
+    fprintf(stderr, "Options:\n");
+    fprintf(stderr, "   -b, --offset INT        decompress at virtual file pointer (0-based uncompressed offset)\n");
     fprintf(stderr, "   -c, --stdout            write on standard output, keep original files unchanged\n");
-	fprintf(stderr, "   -d, --decompress        decompress\n");
-	fprintf(stderr, "   -f, --force             overwrite files without asking\n");
-	fprintf(stderr, "   -h, --help              give this help\n");
-	fprintf(stderr, "   -i, --index             compress and create BGZF index\n");
-	fprintf(stderr, "   -I, --index-name FILE   name of BGZF index file [file.gz.gzi]\n");
-	fprintf(stderr, "   -r, --reindex           (re)index compressed file\n");
-	fprintf(stderr, "   -s, --size INT          decompress INT bytes (uncompressed size)\n");
-	fprintf(stderr, "\n");
-	return 1;
+    fprintf(stderr, "   -d, --decompress        decompress\n");
+    fprintf(stderr, "   -f, --force             overwrite files without asking\n");
+    fprintf(stderr, "   -h, --help              give this help\n");
+    fprintf(stderr, "   -i, --index             compress and create BGZF index\n");
+    fprintf(stderr, "   -I, --index-name FILE   name of BGZF index file [file.gz.gzi]\n");
+    fprintf(stderr, "   -r, --reindex           (re)index compressed file\n");
+    fprintf(stderr, "   -s, --size INT          decompress INT bytes (uncompressed size)\n");
+    fprintf(stderr, "\n");
+    return 1;
 }
 
 int main(int argc, char **argv)
 {
-	int c, compress, pstdout, is_forced, index = 0, reindex = 0;
-	BGZF *fp;
-	void *buffer;
-	long start, end, size;
+    int c, compress, pstdout, is_forced, index = 0, reindex = 0;
+    BGZF *fp;
+    void *buffer;
+    long start, end, size;
     char *index_fname = NULL;
 
-    static struct option loptions[] = 
+    static struct option loptions[] =
     {
         {"help",0,0,'h'},
         {"offset",1,0,'b'},
@@ -109,93 +110,93 @@ int main(int argc, char **argv)
         {0,0,0,0}
     };
 
-	compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0;
-	while((c  = getopt_long(argc, argv, "cdh?fb:s:iI:r",loptions,NULL)) >= 0){
-		switch(c){
-		case 'd': compress = 0; break;
-		case 'c': pstdout = 1; break;
-		case 'b': start = atol(optarg); compress = 0; pstdout = 1; break;
-		case 's': size = atol(optarg); pstdout = 1; break;
-		case 'f': is_forced = 1; break;
+    compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0;
+    while((c  = getopt_long(argc, argv, "cdh?fb:s:iI:r",loptions,NULL)) >= 0){
+        switch(c){
+        case 'd': compress = 0; break;
+        case 'c': pstdout = 1; break;
+        case 'b': start = atol(optarg); compress = 0; pstdout = 1; break;
+        case 's': size = atol(optarg); pstdout = 1; break;
+        case 'f': is_forced = 1; break;
         case 'i': index = 1; break;
         case 'I': index_fname = optarg; break;
         case 'r': reindex = 1; compress = 0; break;
-		case 'h': 
+        case 'h':
         case '?': return bgzip_main_usage();
-		}
-	}
-	if (size >= 0) end = start + size;
-	if (end >= 0 && end < start) {
-		fprintf(stderr, "[bgzip] Illegal region: [%ld, %ld]\n", start, end);
-		return 1;
-	}
-	if (compress == 1) {
-		struct stat sbuf;
-		int f_src = fileno(stdin);
-		int f_dst = fileno(stdout);
+        }
+    }
+    if (size >= 0) end = start + size;
+    if (end >= 0 && end < start) {
+        fprintf(stderr, "[bgzip] Illegal region: [%ld, %ld]\n", start, end);
+        return 1;
+    }
+    if (compress == 1) {
+        struct stat sbuf;
+        int f_src = fileno(stdin);
+        int f_dst = fileno(stdout);
 
-		if ( argc>optind )
-		{
-			if ( stat(argv[optind],&sbuf)<0 ) 
-			{ 
-				fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
-				return 1; 
-			}
+        if ( argc>optind )
+        {
+            if ( stat(argv[optind],&sbuf)<0 )
+            {
+                fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
+                return 1;
+            }
 
-			if ((f_src = open(argv[optind], O_RDONLY)) < 0) {
-				fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
-				return 1;
-			}
+            if ((f_src = open(argv[optind], O_RDONLY)) < 0) {
+                fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
+                return 1;
+            }
 
-			if (pstdout)
-				f_dst = fileno(stdout);
-			else
-			{
-				char *name = malloc(strlen(argv[optind]) + 5);
-				strcpy(name, argv[optind]);
-				strcat(name, ".gz");
-				f_dst = write_open(name, is_forced);
-				if (f_dst < 0) return 1;
-				free(name);
-			}
-		}
-		else if (!pstdout && isatty(fileno((FILE *)stdout)) )
-			return bgzip_main_usage();
+            if (pstdout)
+                f_dst = fileno(stdout);
+            else
+            {
+                char *name = malloc(strlen(argv[optind]) + 5);
+                strcpy(name, argv[optind]);
+                strcat(name, ".gz");
+                f_dst = write_open(name, is_forced);
+                if (f_dst < 0) return 1;
+                free(name);
+            }
+        }
+        else if (!pstdout && isatty(fileno((FILE *)stdout)) )
+            return bgzip_main_usage();
         else if ( index && !index_fname )
         {
             fprintf(stderr, "[bgzip] Index file name expected when writing to stdout\n");
             return 1;
         }
 
-		fp = bgzf_fdopen(f_dst, "w");
+        fp = bgzf_fdopen(f_dst, "w");
         if ( index ) bgzf_index_build_init(fp);
-		buffer = malloc(WINDOW_SIZE);
-		while ((c = read(f_src, buffer, WINDOW_SIZE)) > 0)
-			if (bgzf_write(fp, buffer, c) < 0) error("Could not write %d bytes: Error %d\n", c, fp->errcode);
-		// f_dst will be closed here
-        if ( index ) 
+        buffer = malloc(WINDOW_SIZE);
+        while ((c = read(f_src, buffer, WINDOW_SIZE)) > 0)
+            if (bgzf_write(fp, buffer, c) < 0) error("Could not write %d bytes: Error %d\n", c, fp->errcode);
+        // f_dst will be closed here
+        if ( index )
         {
             if ( index_fname ) bgzf_index_dump(fp, index_fname, NULL);
             else bgzf_index_dump(fp, argv[optind], ".gz.gzi");
         }
-		if (bgzf_close(fp) < 0) error("Close failed: Error %d", fp->errcode);
-		if (argc > optind && !pstdout) unlink(argv[optind]);
-		free(buffer);
-		close(f_src);
-		return 0;
-	}
+        if (bgzf_close(fp) < 0) error("Close failed: Error %d", fp->errcode);
+        if (argc > optind && !pstdout) unlink(argv[optind]);
+        free(buffer);
+        close(f_src);
+        return 0;
+    }
     else if ( reindex )
     {
         if ( argc>optind )
         {
-			fp = bgzf_open(argv[optind], "r");
+            fp = bgzf_open(argv[optind], "r");
             if ( !fp ) error("[bgzip] Could not open file: %s\n", argv[optind]);
         }
         else
         {
             if ( !index_fname ) error("[bgzip] Index file name expected when reading from stdin\n");
             fp = bgzf_fdopen(fileno(stdin), "r");
-        	if ( !fp ) error("[bgzip] Could not read from stdin: %s\n", strerror(errno));
+            if ( !fp ) error("[bgzip] Could not read from stdin: %s\n", strerror(errno));
         }
 
         buffer = malloc(BGZF_BLOCK_SIZE);
@@ -204,10 +205,10 @@ int main(int argc, char **argv)
         while ( (ret=bgzf_read(fp, buffer, BGZF_BLOCK_SIZE))>0 ) ;
         free(buffer);
         if ( ret<0 ) error("Is the file gzipped or bgzipped? The latter is required for indexing.\n");
- 
+
         if ( index_fname )
             bgzf_index_dump(fp, index_fname, NULL);
-        else 
+        else
             bgzf_index_dump(fp, argv[optind], ".gzi");
 
         if ( bgzf_close(fp)<0 ) error("Close failed: Error %d\n",fp->errcode);
@@ -215,50 +216,50 @@ int main(int argc, char **argv)
     }
     else
     {
-		struct stat sbuf;
-		int f_dst;
+        struct stat sbuf;
+        int f_dst;
 
-		if ( argc>optind )
-		{
-			if ( stat(argv[optind],&sbuf)<0 )
-			{
-				fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
-				return 1;
-			}
-			char *name;
-			int len = strlen(argv[optind]);
-			if ( strcmp(argv[optind]+len-3,".gz") )
-			{
-				fprintf(stderr, "[bgzip] %s: unknown suffix -- ignored\n", argv[optind]);
-				return 1;
-			}
-			fp = bgzf_open(argv[optind], "r");
-			if (fp == NULL) {
-				fprintf(stderr, "[bgzip] Could not open file: %s\n", argv[optind]);
-				return 1;
-			}
+        if ( argc>optind )
+        {
+            if ( stat(argv[optind],&sbuf)<0 )
+            {
+                fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
+                return 1;
+            }
+            char *name;
+            int len = strlen(argv[optind]);
+            if ( strcmp(argv[optind]+len-3,".gz") )
+            {
+                fprintf(stderr, "[bgzip] %s: unknown suffix -- ignored\n", argv[optind]);
+                return 1;
+            }
+            fp = bgzf_open(argv[optind], "r");
+            if (fp == NULL) {
+                fprintf(stderr, "[bgzip] Could not open file: %s\n", argv[optind]);
+                return 1;
+            }
 
-			if (pstdout) {
-				f_dst = fileno(stdout);
-			}
-			else {
-				name = strdup(argv[optind]);
-				name[strlen(name) - 3] = '\0';
-				f_dst = write_open(name, is_forced);
-				free(name);
-			}
-		}
-		else if (!pstdout && isatty(fileno((FILE *)stdin)) )
-			return bgzip_main_usage();
-		else
-		{
-			f_dst = fileno(stdout);
-			fp = bgzf_fdopen(fileno(stdin), "r");
-			if (fp == NULL) {
-				fprintf(stderr, "[bgzip] Could not read from stdin: %s\n", strerror(errno));
-				return 1;
-			}
-		}
+            if (pstdout) {
+                f_dst = fileno(stdout);
+            }
+            else {
+                name = strdup(argv[optind]);
+                name[strlen(name) - 3] = '\0';
+                f_dst = write_open(name, is_forced);
+                free(name);
+            }
+        }
+        else if (!pstdout && isatty(fileno((FILE *)stdin)) )
+            return bgzip_main_usage();
+        else
+        {
+            f_dst = fileno(stdout);
+            fp = bgzf_fdopen(fileno(stdin), "r");
+            if (fp == NULL) {
+                fprintf(stderr, "[bgzip] Could not read from stdin: %s\n", strerror(errno));
+                return 1;
+            }
+        }
         buffer = malloc(WINDOW_SIZE);
         if ( start>0 )
         {
@@ -278,6 +279,6 @@ int main(int argc, char **argv)
         if (bgzf_close(fp) < 0) error("Close failed: Error %d\n",fp->errcode);
         if (!pstdout) unlink(argv[optind]);
         return 0;
-	}
+    }
     return 0;
 }
diff --git a/htslib/cram/cram_decode.c b/htslib/cram/cram_decode.c
index 83fdaee..e002ac9 100644
--- a/htslib/cram/cram_decode.c
+++ b/htslib/cram/cram_decode.c
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2012-2013 Genome Research Ltd.
+Copyright (c) 2012-2014 Genome Research Ltd.
 Author: James Bonfield <jkb at sanger.ac.uk>
 
 Redistribution and use in source and binary forms, with or without 
diff --git a/htslib/cram/cram_index.c b/htslib/cram/cram_index.c
index d78b3e0..d16f601 100644
--- a/htslib/cram/cram_index.c
+++ b/htslib/cram/cram_index.c
@@ -155,6 +155,8 @@ int cram_index_load(cram_fd *fd, const char *fn) {
 	}
 	kstr.s = s;
 	kstr.l = l;
+	kstr.m = l; // conservative estimate of the size allocated
+	kputsn("", 0, &kstr); // ensure kstr.s is NUL-terminated
     }
 
 
diff --git a/htslib/cram/cram_io.c b/htslib/cram/cram_io.c
index 8abc59d..c5a4c4e 100644
--- a/htslib/cram/cram_io.c
+++ b/htslib/cram/cram_io.c
@@ -1433,8 +1433,8 @@ static int cram_populate_ref(cram_fd *fd, int id, ref_entry *r) {
     if (fd->verbose)
 	fprintf(stderr, "cram_populate_ref on fd %p, id %d\n", fd, id);
 
-    if (!ref_path)
-	ref_path = ".";
+    if (!ref_path || *ref_path == 0)
+	ref_path = "http://www.ebi.ac.uk:80/ena/cram/md5/%s";
 
     if (!r->name)
 	return -1;
diff --git a/htslib/cram/cram_io.h b/htslib/cram/cram_io.h
index 165dc5e..49073f7 100644
--- a/htslib/cram/cram_io.h
+++ b/htslib/cram/cram_io.h
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2012-2013 Genome Research Ltd.
+Copyright (c) 2012-2014 Genome Research Ltd.
 Author: James Bonfield <jkb at sanger.ac.uk>
 
 Redistribution and use in source and binary forms, with or without 
diff --git a/htslib/cram/open_trace_file.c b/htslib/cram/open_trace_file.c
index d89815d..edad293 100644
--- a/htslib/cram/open_trace_file.c
+++ b/htslib/cram/open_trace_file.c
@@ -32,7 +32,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 /*
-Copyright (c) 2008, 2009, 2013 Genome Research Ltd.
+Copyright (c) 2008, 2009, 2013, 2014 Genome Research Ltd.
 Author: James Bonfield <jkb at sanger.ac.uk>
 
 Redistribution and use in source and binary forms, with or without 
@@ -73,12 +73,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #ifndef PATH_MAX
 #  define PATH_MAX 1024
 #endif
-#ifdef HAVE_LIBCURL
-#  include <curl/curl.h>
-#endif
 
 #include "cram/open_trace_file.h"
 #include "cram/misc.h"
+#include "htslib/hfile.h"
 
 /*
  * Tokenises the search path splitting on colons (unix) or semicolons
@@ -117,6 +115,34 @@ char *tokenise_search_path(char *searchpath) {
 	    continue;
 	}
 
+	/* Handle http:// and ftp:// too without :: */
+	if (path_sep == ':') {
+	    if ((i == 0 || (i > 0 && searchpath[i-1] == ':')) &&
+		(!strncmp(&searchpath[i], "http:",     5) ||
+		 !strncmp(&searchpath[i], "ftp:",      4) ||
+		 !strncmp(&searchpath[i], "|http:",    6) ||
+		 !strncmp(&searchpath[i], "|ftp:",     5) ||
+		 !strncmp(&searchpath[i], "URL=http:", 9) ||
+		 !strncmp(&searchpath[i], "URL=ftp:",  8))) {
+		do {
+		    newsearch[j++] = searchpath[i];
+		} while (i<len && searchpath[i++] != ':');
+		if (searchpath[i] == ':')
+		    i++;
+		if (searchpath[i]=='/')
+		    newsearch[j++] = searchpath[i++];
+		if (searchpath[i]=='/')
+		    newsearch[j++] = searchpath[i++];
+		// Look for host:port
+		do {
+		    newsearch[j++] = searchpath[i++];
+		} while (i<len && searchpath[i] != ':' && searchpath[i] != '/');
+		newsearch[j++] = searchpath[i++];
+		if (searchpath[i] == ':')
+		    i++;
+	    }
+	}
+
 	if (searchpath[i] == path_sep) {
 	    /* Skip blank path components */
 	    if (j && newsearch[j-1] != 0)
@@ -136,26 +162,11 @@ char *tokenise_search_path(char *searchpath) {
     return newsearch;
 }
 
-#ifdef HAVE_LIBCURL
 mFILE *find_file_url(char *file, char *url) {
     char buf[8192], *cp;
-    mFILE *mf = NULL, *headers = NULL;
-    int maxlen = 8190 - strlen(file);
-    static CURL *handle = NULL;
-    static int curl_init = 0;
-    char errbuf[CURL_ERROR_SIZE];
-
-    *errbuf = 0;
-
-    if (!curl_init) {
-	if (curl_global_init(CURL_GLOBAL_ALL))
-	    return NULL;
-
-	if (NULL == (handle = curl_easy_init()))
-	    goto error;
-
-	curl_init = 1;
-    }
+    mFILE *mf = NULL;
+    int maxlen = 8190 - strlen(file), len;
+    hFILE *hf;
 
     /* Expand %s for the trace name */
     for (cp = buf; *url && cp - buf < maxlen; url++) {
@@ -168,68 +179,26 @@ mFILE *find_file_url(char *file, char *url) {
     }
     *cp++ = 0;
 
-    /* Setup the curl */
-    if (NULL == (mf = mfcreate(NULL, 0)) ||
-	NULL == (headers = mfcreate(NULL, 0)))
+    if (!(hf = hopen(buf, "r")))
 	return NULL;
 
-    if (0 != curl_easy_setopt(handle, CURLOPT_URL, buf))
-	goto error;
-    if (0 != curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 60L))
-	goto error;
-    if (0 != curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION,
-			      (curl_write_callback)mfwrite))
-	goto error;
-    if (0 != curl_easy_setopt(handle, CURLOPT_WRITEDATA, mf))
-	goto error;
-    if (0 != curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION,
-			      (curl_write_callback)mfwrite))
-	goto error;
-    if (0 != curl_easy_setopt(handle, CURLOPT_WRITEHEADER, headers))
-	goto error;
-    if (0 != curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errbuf))
-	goto error;
-    
-    /* Fetch! */
-    if (0 != curl_easy_perform(handle))
-	goto error;
-    
-    /* Report errors is approproate. 404 is silent as it may have just been
-     * a search via RAWDATA path, everything else is worth reporting.
-     */
-    {
-	float version;
-	int response;
-	char nul = 0;
-	mfwrite(&nul, 1, 1, headers);
-	if (2 == sscanf(headers->data, "HTTP/%f %d", &version, &response)) {
-	    if (response != 200) {
-		if (response != 404)
-		    fprintf(stderr, "%.*s\n",
-			    (int)headers->size, headers->data);
-		goto error;
-	    }
+    if (NULL == (mf = mfcreate(NULL, 0)))
+	return NULL;
+    while ((len = hread(hf, buf, 8192)) > 0) {
+	if (mfwrite(buf, len, 1, mf) <= 0) {
+	    hclose_abruptly(hf);
+	    mfdestroy(mf);
+	    return NULL;
 	}
     }
-
-    if (mftell(mf) == 0)
-	goto error;
-
-    mfdestroy(headers);
+    if (hclose(hf) < 0) {
+	mfdestroy(mf);
+	return NULL;
+    }
 
     mrewind(mf);
     return mf;
-
- error:
-    if (mf)
-	mfdestroy(mf);
-    if (headers)
-	mfdestroy(headers);
-    if (*errbuf)
-	fprintf(stderr, "%s\n", errbuf);
-    return NULL;
 }
-#endif
 
 /*
  * Searches for file in the directory 'dirname'. If it finds it, it opens
@@ -333,41 +302,37 @@ mFILE *open_path_mfile(char *file, char *path, char *relative_to) {
      * special, otherwise we treat the element as a directory.
      */
     for (ele = newsearch; *ele; ele += strlen(ele)+1) {
-	int i;
-	char *suffix[6] = {"", ".gz", ".bz2", ".sz", ".Z", ".bz2"};
-	for (i = 0; i < 6; i++) {
-	    char file2[1024];
-	    char *ele2;
-	    int valid = 1;
-
-	    /*
-	     * '|' prefixing a path component indicates that we do not
-	     * wish to perform the compression extension searching in that
-	     * location.
-	     */
-	    if (*ele == '|') {
-		ele2 = ele+1;
-		valid = (i == 0);
-	    } else {
-		ele2 = ele;
-	    }
-
-	    sprintf(file2, "%s%s", file, suffix[i]);
+	char *ele2;
+
+	/*
+	 * '|' prefixing a path component indicates that we do not
+	 * wish to perform the compression extension searching in that
+	 * location.
+	 *
+	 * NB: this has been removed from the htslib implementation.
+	 */
+	if (*ele == '|') {
+	    ele2 = ele+1;
+	} else {
+	    ele2 = ele;
+	}
 
-#if defined(HAVE_LIBCURL)
-	    if (0 == strncmp(ele2, "URL=", 4)) {
-		if (valid && (fp = find_file_url(file2, ele2+4))) {
-		    free(newsearch);
-		    return fp;
-		}
-	    } else
-#endif
-	    if (valid && (fp = find_file_dir(file2, ele2))) {
+	if (0 == strncmp(ele2, "URL=", 4)) {
+	    if ((fp = find_file_url(file, ele2+4))) {
 		free(newsearch);
 		return fp;
 	    }
-	}
-    }
+	} else if (!strncmp(ele2, "http:", 5) ||
+		   !strncmp(ele2, "ftp:", 4)) {
+	    if ((fp = find_file_url(file, ele2))) {
+		free(newsearch);
+		return fp;
+	    }
+	} else if ((fp = find_file_dir(file, ele2))) {
+	    free(newsearch);
+	    return fp;
+	} 
+   }
 
     free(newsearch);
 
diff --git a/htslib/faidx.c b/htslib/faidx.c
index 05dff83..75ec84c 100644
--- a/htslib/faidx.c
+++ b/htslib/faidx.c
@@ -1,3 +1,28 @@
+/*  faidx.c -- FASTA random access.
+
+    Copyright (C) 2008, 2009, 2013, 2014 Genome Research Ltd.
+    Portions copyright (C) 2011 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include "config.h"
 
 #include <ctype.h>
@@ -14,17 +39,17 @@
 #endif
 
 typedef struct {
-	int32_t line_len, line_blen;
-	int64_t len;
-	uint64_t offset;
+    int32_t line_len, line_blen;
+    int64_t len;
+    uint64_t offset;
 } faidx1_t;
 KHASH_MAP_INIT_STR(s, faidx1_t)
 
 struct __faidx_t {
-	BGZF *bgzf;
-	int n, m;
-	char **name;
-	khash_t(s) *hash;
+    BGZF *bgzf;
+    int n, m;
+    char **name;
+    khash_t(s) *hash;
 };
 
 #ifndef kroundup32
@@ -33,173 +58,184 @@ struct __faidx_t {
 
 static inline void fai_insert_index(faidx_t *idx, const char *name, int len, int line_len, int line_blen, uint64_t offset)
 {
-	khint_t k;
-	int ret;
-	faidx1_t t;
-	if (idx->n == idx->m) {
-		idx->m = idx->m? idx->m<<1 : 16;
-		idx->name = (char**)realloc(idx->name, sizeof(char*) * idx->m);
-	}
-	idx->name[idx->n] = strdup(name);
-	k = kh_put(s, idx->hash, idx->name[idx->n], &ret);
-	t.len = len; t.line_len = line_len; t.line_blen = line_blen; t.offset = offset;
-	kh_value(idx->hash, k) = t;
-	++idx->n;
+    khint_t k;
+    int ret;
+    faidx1_t t;
+    if (idx->n == idx->m) {
+        idx->m = idx->m? idx->m<<1 : 16;
+        idx->name = (char**)realloc(idx->name, sizeof(char*) * idx->m);
+    }
+    idx->name[idx->n] = strdup(name);
+    k = kh_put(s, idx->hash, idx->name[idx->n], &ret);
+    t.len = len; t.line_len = line_len; t.line_blen = line_blen; t.offset = offset;
+    kh_value(idx->hash, k) = t;
+    ++idx->n;
 }
 
 faidx_t *fai_build_core(BGZF *bgzf)
 {
-	char c, *name;
-	int l_name, m_name;
-	int line_len, line_blen, state;
-	int l1, l2;
-	faidx_t *idx;
-	uint64_t offset;
-	int64_t len;
-
-	idx = (faidx_t*)calloc(1, sizeof(faidx_t));
-	idx->hash = kh_init(s);
-	name = 0; l_name = m_name = 0;
-	len = line_len = line_blen = -1; state = 0; l1 = l2 = -1; offset = 0;
-	while ( (c=bgzf_getc(bgzf))>=0 ) {
-		if (c == '\n') { // an empty line
-			if (state == 1) {
-				offset = bgzf_utell(bgzf);
-				continue;
-			} else if ((state == 0 && len < 0) || state == 2) continue;
-		}
-		if (c == '>') { // fasta header
-			if (len >= 0)
-				fai_insert_index(idx, name, len, line_len, line_blen, offset);
-			l_name = 0;
-			while ( (c=bgzf_getc(bgzf))>=0 && !isspace(c)) {
-				if (m_name < l_name + 2) {
-					m_name = l_name + 2;
-					kroundup32(m_name);
-					name = (char*)realloc(name, m_name);
-				}
-				name[l_name++] = c;
-			}
-			name[l_name] = '\0';
-			if ( c<0 ) {
-				fprintf(stderr, "[fai_build_core] the last entry has no sequence\n");
-				free(name); fai_destroy(idx);
-				return 0;
-			}
-			if (c != '\n') while ( (c=bgzf_getc(bgzf))>=0 && c != '\n');
-			state = 1; len = 0;
-			offset = bgzf_utell(bgzf);
-		} else {
-			if (state == 3) {
-				fprintf(stderr, "[fai_build_core] inlined empty line is not allowed in sequence '%s'.\n", name);
-				free(name); fai_destroy(idx);
-				return 0;
-			}
-			if (state == 2) state = 3;
-			l1 = l2 = 0;
-			do {
-				++l1;
-				if (isgraph(c)) ++l2;
-			} while ( (c=bgzf_getc(bgzf))>=0 && c != '\n');
-			if (state == 3 && l2) {
-				fprintf(stderr, "[fai_build_core] different line length in sequence '%s'.\n", name);
-				free(name); fai_destroy(idx);
-				return 0;
-			}
-			++l1; len += l2;
-			if (state == 1) line_len = l1, line_blen = l2, state = 0;
-			else if (state == 0) {
-				if (l1 != line_len || l2 != line_blen) state = 2;
-			}
-		}
-	}
-	fai_insert_index(idx, name, len, line_len, line_blen, offset);
-	free(name);
-	return idx;
+    char c, *name;
+    int l_name, m_name;
+    int line_len, line_blen, state;
+    int l1, l2;
+    faidx_t *idx;
+    uint64_t offset;
+    int64_t len;
+
+    idx = (faidx_t*)calloc(1, sizeof(faidx_t));
+    idx->hash = kh_init(s);
+    name = 0; l_name = m_name = 0;
+    len = line_len = line_blen = -1; state = 0; l1 = l2 = -1; offset = 0;
+    while ( (c=bgzf_getc(bgzf))>=0 ) {
+        if (c == '\n') { // an empty line
+            if (state == 1) {
+                offset = bgzf_utell(bgzf);
+                continue;
+            } else if ((state == 0 && len < 0) || state == 2) continue;
+        }
+        if (c == '>') { // fasta header
+            if (len >= 0)
+                fai_insert_index(idx, name, len, line_len, line_blen, offset);
+            l_name = 0;
+            while ( (c=bgzf_getc(bgzf))>=0 && !isspace(c)) {
+                if (m_name < l_name + 2) {
+                    m_name = l_name + 2;
+                    kroundup32(m_name);
+                    name = (char*)realloc(name, m_name);
+                }
+                name[l_name++] = c;
+            }
+            name[l_name] = '\0';
+            if ( c<0 ) {
+                fprintf(stderr, "[fai_build_core] the last entry has no sequence\n");
+                free(name); fai_destroy(idx);
+                return 0;
+            }
+            if (c != '\n') while ( (c=bgzf_getc(bgzf))>=0 && c != '\n');
+            state = 1; len = 0;
+            offset = bgzf_utell(bgzf);
+        } else {
+            if (state == 3) {
+                fprintf(stderr, "[fai_build_core] inlined empty line is not allowed in sequence '%s'.\n", name);
+                free(name); fai_destroy(idx);
+                return 0;
+            }
+            if (state == 2) state = 3;
+            l1 = l2 = 0;
+            do {
+                ++l1;
+                if (isgraph(c)) ++l2;
+            } while ( (c=bgzf_getc(bgzf))>=0 && c != '\n');
+            if (state == 3 && l2) {
+                fprintf(stderr, "[fai_build_core] different line length in sequence '%s'.\n", name);
+                free(name); fai_destroy(idx);
+                return 0;
+            }
+            ++l1; len += l2;
+            if (state == 1) line_len = l1, line_blen = l2, state = 0;
+            else if (state == 0) {
+                if (l1 != line_len || l2 != line_blen) state = 2;
+            }
+        }
+    }
+    if ( name )
+        fai_insert_index(idx, name, len, line_len, line_blen, offset);
+    else
+    {
+        free(idx);
+        return NULL;
+    }
+    free(name);
+    return idx;
 }
 
 void fai_save(const faidx_t *fai, FILE *fp)
 {
-	khint_t k;
-	int i;
-	for (i = 0; i < fai->n; ++i) {
-		faidx1_t x;
-		k = kh_get(s, fai->hash, fai->name[i]);
-		x = kh_value(fai->hash, k);
+    khint_t k;
+    int i;
+    for (i = 0; i < fai->n; ++i) {
+        faidx1_t x;
+        k = kh_get(s, fai->hash, fai->name[i]);
+        x = kh_value(fai->hash, k);
 #ifdef _WIN32
-		fprintf(fp, "%s\t%d\t%ld\t%d\t%d\n", fai->name[i], (int)x.len, (long)x.offset, (int)x.line_blen, (int)x.line_len);
+        fprintf(fp, "%s\t%d\t%ld\t%d\t%d\n", fai->name[i], (int)x.len, (long)x.offset, (int)x.line_blen, (int)x.line_len);
 #else
-		fprintf(fp, "%s\t%d\t%lld\t%d\t%d\n", fai->name[i], (int)x.len, (long long)x.offset, (int)x.line_blen, (int)x.line_len);
+        fprintf(fp, "%s\t%d\t%lld\t%d\t%d\n", fai->name[i], (int)x.len, (long long)x.offset, (int)x.line_blen, (int)x.line_len);
 #endif
-	}
+    }
 }
 
 faidx_t *fai_read(FILE *fp)
 {
-	faidx_t *fai;
-	char *buf, *p;
-	int len, line_len, line_blen;
+    faidx_t *fai;
+    char *buf, *p;
+    int len, line_len, line_blen;
 #ifdef _WIN32
-	long offset;
+    long offset;
 #else
-	long long offset;
+    long long offset;
 #endif
-	fai = (faidx_t*)calloc(1, sizeof(faidx_t));
-	fai->hash = kh_init(s);
-	buf = (char*)calloc(0x10000, 1);
-	while (!feof(fp) && fgets(buf, 0x10000, fp)) {
-		for (p = buf; *p && isgraph(*p); ++p);
-		*p = 0; ++p;
+    fai = (faidx_t*)calloc(1, sizeof(faidx_t));
+    fai->hash = kh_init(s);
+    buf = (char*)calloc(0x10000, 1);
+    while (!feof(fp) && fgets(buf, 0x10000, fp)) {
+        for (p = buf; *p && isgraph(*p); ++p);
+        *p = 0; ++p;
 #ifdef _WIN32
-		sscanf(p, "%d%ld%d%d", &len, &offset, &line_blen, &line_len);
+        sscanf(p, "%d%ld%d%d", &len, &offset, &line_blen, &line_len);
 #else
-		sscanf(p, "%d%lld%d%d", &len, &offset, &line_blen, &line_len);
+        sscanf(p, "%d%lld%d%d", &len, &offset, &line_blen, &line_len);
 #endif
-		fai_insert_index(fai, buf, len, line_len, line_blen, offset);
-	}
-	free(buf);
-	return fai;
+        fai_insert_index(fai, buf, len, line_len, line_blen, offset);
+    }
+    free(buf);
+    return fai;
 }
 
 void fai_destroy(faidx_t *fai)
 {
-	int i;
-	for (i = 0; i < fai->n; ++i) free(fai->name[i]);
-	free(fai->name);
-	kh_destroy(s, fai->hash);
-	if (fai->bgzf) bgzf_close(fai->bgzf);
-	free(fai);
+    int i;
+    for (i = 0; i < fai->n; ++i) free(fai->name[i]);
+    free(fai->name);
+    kh_destroy(s, fai->hash);
+    if (fai->bgzf) bgzf_close(fai->bgzf);
+    free(fai);
 }
 
 int fai_build(const char *fn)
 {
-	char *str;
-	BGZF *bgzf;
-	FILE *fp;
-	faidx_t *fai;
-	str = (char*)calloc(strlen(fn) + 5, 1);
-	sprintf(str, "%s.fai", fn);
-	bgzf = bgzf_open(fn, "r");
-	if ( !bgzf ) {
-		fprintf(stderr, "[fai_build] fail to open the FASTA file %s\n",fn);
-		free(str);
-		return -1;
-	}
+    char *str;
+    BGZF *bgzf;
+    FILE *fp;
+    faidx_t *fai;
+    str = (char*)calloc(strlen(fn) + 5, 1);
+    sprintf(str, "%s.fai", fn);
+    bgzf = bgzf_open(fn, "r");
+    if ( !bgzf ) {
+        fprintf(stderr, "[fai_build] fail to open the FASTA file %s\n",fn);
+        free(str);
+        return -1;
+    }
     if ( bgzf->is_compressed ) bgzf_index_build_init(bgzf);
-	fai = fai_build_core(bgzf);
+    fai = fai_build_core(bgzf);
+    if ( !fai )
+    {
+        if ( bgzf->is_compressed && bgzf->is_gzip ) fprintf(stderr,"Cannot index files compressed with gzip, please use bgzip\n");
+        return -1;
+    }
     if ( bgzf->is_compressed ) bgzf_index_dump(bgzf, fn, ".gzi");
-	bgzf_close(bgzf);
-	fp = fopen(str, "wb");
-	if ( !fp ) {
-		fprintf(stderr, "[fai_build] fail to write FASTA index %s\n",str);
-		fai_destroy(fai); free(str);
-		return -1;
-	}
-	fai_save(fai, fp);
-	fclose(fp);
-	free(str);
-	fai_destroy(fai);
-	return 0;
+    bgzf_close(bgzf);
+    fp = fopen(str, "wb");
+    if ( !fp ) {
+        fprintf(stderr, "[fai_build] fail to write FASTA index %s\n",str);
+        fai_destroy(fai); free(str);
+        return -1;
+    }
+    fai_save(fai, fp);
+    fclose(fp);
+    free(str);
+    fai_destroy(fai);
+    return 0;
 }
 
 #ifdef _USE_KNETFILE
@@ -245,11 +281,11 @@ FILE *download_and_open(const char *fn)
 
 faidx_t *fai_load(const char *fn)
 {
-	char *str;
-	FILE *fp;
-	faidx_t *fai;
-	str = (char*)calloc(strlen(fn) + 5, 1);
-	sprintf(str, "%s.fai", fn);
+    char *str;
+    FILE *fp;
+    faidx_t *fai;
+    str = (char*)calloc(strlen(fn) + 5, 1);
+    sprintf(str, "%s.fai", fn);
 
 #ifdef _USE_KNETFILE
     if (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn)
@@ -265,26 +301,26 @@ faidx_t *fai_load(const char *fn)
     else
 #endif
         fp = fopen(str, "rb");
-	if (fp == 0) {
-		fprintf(stderr, "[fai_load] build FASTA index.\n");
-		fai_build(fn);
-		fp = fopen(str, "rb");
-		if (fp == 0) {
-			fprintf(stderr, "[fai_load] fail to open FASTA index.\n");
-			free(str);
-			return 0;
-		}
-	}
-
-	fai = fai_read(fp);
-	fclose(fp);
-
-	fai->bgzf = bgzf_open(fn, "rb");
-	free(str);
-	if (fai->bgzf == 0) {
-		fprintf(stderr, "[fai_load] fail to open FASTA file.\n");
-		return 0;
-	}
+    if (fp == 0) {
+        fprintf(stderr, "[fai_load] build FASTA index.\n");
+        fai_build(fn);
+        fp = fopen(str, "rb");
+        if (fp == 0) {
+            fprintf(stderr, "[fai_load] fail to open FASTA index.\n");
+            free(str);
+            return 0;
+        }
+    }
+
+    fai = fai_read(fp);
+    fclose(fp);
+
+    fai->bgzf = bgzf_open(fn, "rb");
+    free(str);
+    if (fai->bgzf == 0) {
+        fprintf(stderr, "[fai_load] fail to open FASTA file.\n");
+        return 0;
+    }
     if ( fai->bgzf->is_compressed==1 )
     {
         if ( bgzf_index_load(fai->bgzf, fn, ".gzi") < 0 )
@@ -294,128 +330,144 @@ faidx_t *fai_load(const char *fn)
             return NULL;
         }
     }
-	return fai;
+    return fai;
 }
 
 char *fai_fetch(const faidx_t *fai, const char *str, int *len)
 {
-	char *s, c;
-	int i, l, k, name_end;
-	khiter_t iter;
-	faidx1_t val;
-	khash_t(s) *h;
-	int beg, end;
-
-	beg = end = -1;
-	h = fai->hash;
-	name_end = l = strlen(str);
-	s = (char*)malloc(l+1);
-	// remove space
-	for (i = k = 0; i < l; ++i)
-		if (!isspace(str[i])) s[k++] = str[i];
-	s[k] = 0; l = k;
-	// determine the sequence name
-	for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
-	if (i >= 0) name_end = i;
-	if (name_end < l) { // check if this is really the end
-		int n_hyphen = 0;
-		for (i = name_end + 1; i < l; ++i) {
-			if (s[i] == '-') ++n_hyphen;
-			else if (!isdigit(s[i]) && s[i] != ',') break;
-		}
-		if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
-		s[name_end] = 0;
-		iter = kh_get(s, h, s);
-		if (iter == kh_end(h)) { // cannot find the sequence name
-			iter = kh_get(s, h, str); // try str as the name
-			if (iter == kh_end(h)) {
-				*len = 0;
-			free(s); return 0;
-			} else s[name_end] = ':', name_end = l;
-		}
-	} else iter = kh_get(s, h, str);
-	if(iter == kh_end(h)) {
-		fprintf(stderr, "[fai_fetch] Warning - Reference %s not found in FASTA file, returning empty sequence\n", str);
-		free(s);
+    char *s, c;
+    int i, l, k, name_end;
+    khiter_t iter;
+    faidx1_t val;
+    khash_t(s) *h;
+    int beg, end;
+
+    beg = end = -1;
+    h = fai->hash;
+    name_end = l = strlen(str);
+    s = (char*)malloc(l+1);
+    // remove space
+    for (i = k = 0; i < l; ++i)
+        if (!isspace(str[i])) s[k++] = str[i];
+    s[k] = 0; l = k;
+    // determine the sequence name
+    for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
+    if (i >= 0) name_end = i;
+    if (name_end < l) { // check if this is really the end
+        int n_hyphen = 0;
+        for (i = name_end + 1; i < l; ++i) {
+            if (s[i] == '-') ++n_hyphen;
+            else if (!isdigit(s[i]) && s[i] != ',') break;
+        }
+        if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
+        s[name_end] = 0;
+        iter = kh_get(s, h, s);
+        if (iter == kh_end(h)) { // cannot find the sequence name
+            iter = kh_get(s, h, str); // try str as the name
+            if (iter == kh_end(h)) {
+                *len = 0;
+            free(s); return 0;
+            } else s[name_end] = ':', name_end = l;
+        }
+    } else iter = kh_get(s, h, str);
+    if(iter == kh_end(h)) {
+        fprintf(stderr, "[fai_fetch] Warning - Reference %s not found in FASTA file, returning empty sequence\n", str);
+        free(s);
         *len = -2;
-		return 0;
-	};
-	val = kh_value(h, iter);
-	// parse the interval
-	if (name_end < l) {
-		for (i = k = name_end + 1; i < l; ++i)
-			if (s[i] != ',') s[k++] = s[i];
-		s[k] = 0;
-		beg = atoi(s + name_end + 1);
-		for (i = name_end + 1; i != k; ++i) if (s[i] == '-') break;
-		end = i < k? atoi(s + i + 1) : val.len;
-		if (beg > 0) --beg;
-	} else beg = 0, end = val.len;
-	if (beg >= val.len) beg = val.len;
-	if (end >= val.len) end = val.len;
-	if (beg > end) beg = end;
-	free(s);
-
-	// now retrieve the sequence
-	int ret = bgzf_useek(fai->bgzf, val.offset + beg / val.line_blen * val.line_len + beg % val.line_blen, SEEK_SET);
+        return 0;
+    };
+    val = kh_value(h, iter);
+    // parse the interval
+    if (name_end < l) {
+        for (i = k = name_end + 1; i < l; ++i)
+            if (s[i] != ',') s[k++] = s[i];
+        s[k] = 0;
+        beg = atoi(s + name_end + 1);
+        for (i = name_end + 1; i != k; ++i) if (s[i] == '-') break;
+        end = i < k? atoi(s + i + 1) : val.len;
+        if (beg > 0) --beg;
+    } else beg = 0, end = val.len;
+    if (beg >= val.len) beg = val.len;
+    if (end >= val.len) end = val.len;
+    if (beg > end) beg = end;
+    free(s);
+
+    // now retrieve the sequence
+    int ret = bgzf_useek(fai->bgzf, val.offset + beg / val.line_blen * val.line_len + beg % val.line_blen, SEEK_SET);
     if ( ret<0 )
     {
         *len = -1;
         fprintf(stderr, "[fai_fetch] Error: fai_fetch failed. (Seeking in a compressed, .gzi unindexed, file?)\n");
         return NULL;
     }
-	l = 0;
-	s = (char*)malloc(end - beg + 2);
-	while ( (c=bgzf_getc(fai->bgzf))>=0 && l < end - beg )
-		if (isgraph(c)) s[l++] = c;
-	s[l] = '\0';
-	*len = l;
-	return s;
+    l = 0;
+    s = (char*)malloc(end - beg + 2);
+    while ( (c=bgzf_getc(fai->bgzf))>=0 && l < end - beg )
+        if (isgraph(c)) s[l++] = c;
+    s[l] = '\0';
+    *len = l;
+    return s;
 }
 
-int faidx_fetch_nseq(const faidx_t *fai) 
+int faidx_nseq(const faidx_t *fai)
 {
-	return fai->n;
+    return fai->n;
+}
+const char *faidx_iseq(const faidx_t *fai, int i)
+{
+    return fai->name[i];
+}
+int faidx_seq_len(const faidx_t *fai, const char *seq)
+{
+    khint_t k = kh_get(s, fai->hash, seq);
+    if ( k == kh_end(fai->hash) ) return -1;
+    return kh_val(fai->hash, k).len;
 }
 
 char *faidx_fetch_seq(const faidx_t *fai, const char *c_name, int p_beg_i, int p_end_i, int *len)
 {
-	int l;
-	char c;
+    int l;
+    char c;
     khiter_t iter;
     faidx1_t val;
-	char *seq=NULL;
+    char *seq=NULL;
 
     // Adjust position
     iter = kh_get(s, fai->hash, c_name);
-    if (iter == kh_end(fai->hash)) 
+    if (iter == kh_end(fai->hash))
     {
         *len = -2;
         fprintf(stderr, "[fai_fetch_seq] The sequence \"%s\" not found\n", c_name);
         return NULL;
     }
     val = kh_value(fai->hash, iter);
-	if(p_end_i < p_beg_i) p_beg_i = p_end_i;
+    if(p_end_i < p_beg_i) p_beg_i = p_end_i;
     if(p_beg_i < 0) p_beg_i = 0;
     else if(val.len <= p_beg_i) p_beg_i = val.len - 1;
     if(p_end_i < 0) p_end_i = 0;
     else if(val.len <= p_end_i) p_end_i = val.len - 1;
 
-    // Now retrieve the sequence 
-	int ret = bgzf_useek(fai->bgzf, val.offset + p_beg_i / val.line_blen * val.line_len + p_beg_i % val.line_blen, SEEK_SET);
+    // Now retrieve the sequence
+    int ret = bgzf_useek(fai->bgzf, val.offset + p_beg_i / val.line_blen * val.line_len + p_beg_i % val.line_blen, SEEK_SET);
     if ( ret<0 )
     {
         *len = -1;
         fprintf(stderr, "[fai_fetch_seq] Error: fai_fetch failed. (Seeking in a compressed, .gzi unindexed, file?)\n");
         return NULL;
     }
-	l = 0;
-	seq = (char*)malloc(p_end_i - p_beg_i + 2);
-	while ( (c=bgzf_getc(fai->bgzf))>=0 && l < p_end_i - p_beg_i + 1)
-		if (isgraph(c)) seq[l++] = c;
-	seq[l] = '\0';
-	*len = l;
-	return seq;
+    l = 0;
+    seq = (char*)malloc(p_end_i - p_beg_i + 2);
+    while ( (c=bgzf_getc(fai->bgzf))>=0 && l < p_end_i - p_beg_i + 1)
+        if (isgraph(c)) seq[l++] = c;
+    seq[l] = '\0';
+    *len = l;
+    return seq;
 }
 
+int faidx_has_seq(const faidx_t *fai, const char *seq)
+{
+    khiter_t iter = kh_get(s, fai->hash, seq);
+    if (iter == kh_end(fai->hash)) return 0;
+    return 1;
+}
 
diff --git a/htslib/hfile.c b/htslib/hfile.c
index 0188410..3f33bf6 100644
--- a/htslib/hfile.c
+++ b/htslib/hfile.c
@@ -11,7 +11,7 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
-The above copyright notices and this permission notice shall be included in
+The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
diff --git a/htslib/hfile_internal.h b/htslib/hfile_internal.h
index 88b0c8e..7ac06ba 100644
--- a/htslib/hfile_internal.h
+++ b/htslib/hfile_internal.h
@@ -11,7 +11,7 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
-The above copyright notices and this permission notice shall be included in
+The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
diff --git a/htslib/hfile_net.c b/htslib/hfile_net.c
index 53eda94..9506634 100644
--- a/htslib/hfile_net.c
+++ b/htslib/hfile_net.c
@@ -11,7 +11,7 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
-The above copyright notices and this permission notice shall be included in
+The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
diff --git a/htslib/hts.c b/htslib/hts.c
index 87f88a9..5fab4ba 100644
--- a/htslib/hts.c
+++ b/htslib/hts.c
@@ -1,3 +1,28 @@
+/*  hts.c -- format-neutral I/O, indexing, and iterator API functions.
+
+    Copyright (C) 2008, 2009, 2012-2014 Genome Research Ltd.
+    Copyright (C) 2012, 2013 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <zlib.h>
 #include <ctype.h>
 #include <stdio.h>
@@ -28,27 +53,27 @@ int hts_verbose = 3;
 
 const char *hts_version()
 {
-	return HTS_VERSION;
+    return HTS_VERSION;
 }
 
 const unsigned char seq_nt16_table[256] = {
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	 1, 2, 4, 8, 15,15,15,15, 15,15,15,15, 15, 0 /*=*/,15,15,
-	15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
-	15,15, 5, 6,  8,15, 7, 9, 15,10,15,15, 15,15,15,15,
-	15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
-	15,15, 5, 6,  8,15, 7, 9, 15,10,15,15, 15,15,15,15,
-
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+     1, 2, 4, 8, 15,15,15,15, 15,15,15,15, 15, 0 /*=*/,15,15,
+    15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
+    15,15, 5, 6,  8,15, 7, 9, 15,10,15,15, 15,15,15,15,
+    15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
+    15,15, 5, 6,  8,15, 7, 9, 15,10,15,15, 15,15,15,15,
+
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
+    15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15
 };
 
 const char seq_nt16_str[] = "=ACMGRSVTWYHKDBN";
@@ -61,197 +86,197 @@ const char seq_nt16_str[] = "=ACMGRSVTWYHKDBN";
 // positioned at the start of a GZIP block.
 static size_t decompress_peek(hFILE *fp, unsigned char *dest, size_t destsize)
 {
-	// Typically at most a couple of hundred bytes of input are required
-	// to get a few bytes of output from inflate(), so hopefully this buffer
-	// size suffices in general.
-	unsigned char buffer[512];
-	z_stream zs;
-	ssize_t npeek = hpeek(fp, buffer, sizeof buffer);
-
-	if (npeek < 0) return 0;
-
-	zs.zalloc = NULL;
-	zs.zfree = NULL;
-	zs.next_in = buffer;
-	zs.avail_in = npeek;
-	zs.next_out = dest;
-	zs.avail_out = destsize;
-	if (inflateInit2(&zs, 31) != Z_OK) return 0;
-
-	while (zs.total_out < destsize)
-		if (inflate(&zs, Z_SYNC_FLUSH) != Z_OK) break;
-
-	destsize = zs.total_out;
-	inflateEnd(&zs);
-
-	return destsize;
+    // Typically at most a couple of hundred bytes of input are required
+    // to get a few bytes of output from inflate(), so hopefully this buffer
+    // size suffices in general.
+    unsigned char buffer[512];
+    z_stream zs;
+    ssize_t npeek = hpeek(fp, buffer, sizeof buffer);
+
+    if (npeek < 0) return 0;
+
+    zs.zalloc = NULL;
+    zs.zfree = NULL;
+    zs.next_in = buffer;
+    zs.avail_in = npeek;
+    zs.next_out = dest;
+    zs.avail_out = destsize;
+    if (inflateInit2(&zs, 31) != Z_OK) return 0;
+
+    while (zs.total_out < destsize)
+        if (inflate(&zs, Z_SYNC_FLUSH) != Z_OK) break;
+
+    destsize = zs.total_out;
+    inflateEnd(&zs);
+
+    return destsize;
 }
 
 // Returns whether the block contains any control characters, i.e.,
 // characters less than SPACE other than whitespace etc (ASCII BEL..CR).
 static int is_binary(unsigned char *s, size_t n)
 {
-	size_t i;
-	for (i = 0; i < n; i++)
-		if (s[i] < 0x07 || (s[i] >= 0x0e && s[i] < 0x20)) return 1;
-	return 0;
+    size_t i;
+    for (i = 0; i < n; i++)
+        if (s[i] < 0x07 || (s[i] >= 0x0e && s[i] < 0x20)) return 1;
+    return 0;
 }
 
 htsFile *hts_open(const char *fn, const char *mode)
 {
-	htsFile *fp = NULL;
-	hFILE *hfile = hopen(fn, mode);
-	if (hfile == NULL) goto error;
-
-	fp = (htsFile*)calloc(1, sizeof(htsFile));
-	if (fp == NULL) goto error;
-
-	fp->fn = strdup(fn);
-	fp->is_be = ed_is_big();
-
-	if (strchr(mode, 'r')) {
-		unsigned char s[18];
-		if (hpeek(hfile, s, 6) == 6 && memcmp(s, "CRAM", 4) == 0 &&
-			s[4] >= 1 && s[4] <= 2 && s[5] <= 1) {
-			fp->is_cram = 1;
-		}
-		else if (hpeek(hfile, s, 18) == 18 && s[0] == 0x1f && s[1] == 0x8b &&
-				 (s[3] & 4) && memcmp(&s[12], "BC\2\0", 4) == 0) {
-			// The stream is BGZF-compressed.  Decompress a few bytes to see
-			// whether it's in a binary format (e.g., BAM or BCF, starting
-			// with four bytes of magic including a control character) or is
-			// a bgzipped SAM or VCF text file.
-			fp->is_compressed = 1;
-			if (is_binary(s, decompress_peek(hfile, s, 4))) fp->is_bin = 1;
-			else fp->is_kstream = 1;
-		}
-		else if (hpeek(hfile, s, 2) == 2 && s[0] == 0x1f && s[1] == 0x8b) {
-			// Plain GZIP header... so a gzipped text file.
-			fp->is_compressed = 1;
-			fp->is_kstream = 1;
-		}
-		else if (hpeek(hfile, s, 4) == 4 && is_binary(s, 4)) {
-			// Binary format, but in a raw non-compressed form.
-			fp->is_bin = 1;
-		}
-		else {
-			fp->is_kstream = 1;
-		}
-	}
-	else if (strchr(mode, 'w') || strchr(mode, 'a')) {
-		fp->is_write = 1;
-		if (strchr(mode, 'b')) fp->is_bin = 1;
-		if (strchr(mode, 'c')) fp->is_cram = 1;
-		if (strchr(mode, 'z')) fp->is_compressed = 1;
-		else if (strchr(mode, 'u')) fp->is_compressed = 0;
-		else fp->is_compressed = 2;    // not set, default behaviour
-	}
-	else goto error;
-
-	if (fp->is_bin || (fp->is_write && fp->is_compressed==1)) {
-		fp->fp.bgzf = bgzf_hopen(hfile, mode);
-		if (fp->fp.bgzf == NULL) goto error;
-	}
-	else if (fp->is_cram) {
-		fp->fp.cram = cram_dopen(hfile, fn, mode);
-		if (fp->fp.cram == NULL) goto error;
-		if (!fp->is_write)
-		    cram_set_option(fp->fp.cram, CRAM_OPT_DECODE_MD, 1);
-
-	}
-	else if (fp->is_kstream) {
-	#if KS_BGZF
-		BGZF *gzfp = bgzf_hopen(hfile, mode);
-	#else
-		// TODO Implement gzip hFILE adaptor
-		hclose(hfile); // This won't work, especially for stdin
-		gzFile gzfp = strcmp(fn, "-")? gzopen(fn, "rb") : gzdopen(fileno(stdin), "rb");
-	#endif
-		if (gzfp) fp->fp.voidp = ks_init(gzfp);
-		else goto error;
-	}
-	else {
-		fp->fp.hfile = hfile;
-	}
-
-	return fp;
+    htsFile *fp = NULL;
+    hFILE *hfile = hopen(fn, mode);
+    if (hfile == NULL) goto error;
+
+    fp = (htsFile*)calloc(1, sizeof(htsFile));
+    if (fp == NULL) goto error;
+
+    fp->fn = strdup(fn);
+    fp->is_be = ed_is_big();
+
+    if (strchr(mode, 'r')) {
+        unsigned char s[18];
+        if (hpeek(hfile, s, 6) == 6 && memcmp(s, "CRAM", 4) == 0 &&
+            s[4] >= 1 && s[4] <= 2 && s[5] <= 1) {
+            fp->is_cram = 1;
+        }
+        else if (hpeek(hfile, s, 18) == 18 && s[0] == 0x1f && s[1] == 0x8b &&
+                 (s[3] & 4) && memcmp(&s[12], "BC\2\0", 4) == 0) {
+            // The stream is BGZF-compressed.  Decompress a few bytes to see
+            // whether it's in a binary format (e.g., BAM or BCF, starting
+            // with four bytes of magic including a control character) or is
+            // a bgzipped SAM or VCF text file.
+            fp->is_compressed = 1;
+            if (is_binary(s, decompress_peek(hfile, s, 4))) fp->is_bin = 1;
+            else fp->is_kstream = 1;
+        }
+        else if (hpeek(hfile, s, 2) == 2 && s[0] == 0x1f && s[1] == 0x8b) {
+            // Plain GZIP header... so a gzipped text file.
+            fp->is_compressed = 1;
+            fp->is_kstream = 1;
+        }
+        else if (hpeek(hfile, s, 4) == 4 && is_binary(s, 4)) {
+            // Binary format, but in a raw non-compressed form.
+            fp->is_bin = 1;
+        }
+        else {
+            fp->is_kstream = 1;
+        }
+    }
+    else if (strchr(mode, 'w') || strchr(mode, 'a')) {
+        fp->is_write = 1;
+        if (strchr(mode, 'b')) fp->is_bin = 1;
+        if (strchr(mode, 'c')) fp->is_cram = 1;
+        if (strchr(mode, 'z')) fp->is_compressed = 1;
+        else if (strchr(mode, 'u')) fp->is_compressed = 0;
+        else fp->is_compressed = 2;    // not set, default behaviour
+    }
+    else goto error;
+
+    if (fp->is_bin || (fp->is_write && fp->is_compressed==1)) {
+        fp->fp.bgzf = bgzf_hopen(hfile, mode);
+        if (fp->fp.bgzf == NULL) goto error;
+    }
+    else if (fp->is_cram) {
+        fp->fp.cram = cram_dopen(hfile, fn, mode);
+        if (fp->fp.cram == NULL) goto error;
+        if (!fp->is_write)
+            cram_set_option(fp->fp.cram, CRAM_OPT_DECODE_MD, 1);
+
+    }
+    else if (fp->is_kstream) {
+    #if KS_BGZF
+        BGZF *gzfp = bgzf_hopen(hfile, mode);
+    #else
+        // TODO Implement gzip hFILE adaptor
+        hclose(hfile); // This won't work, especially for stdin
+        gzFile gzfp = strcmp(fn, "-")? gzopen(fn, "rb") : gzdopen(fileno(stdin), "rb");
+    #endif
+        if (gzfp) fp->fp.voidp = ks_init(gzfp);
+        else goto error;
+    }
+    else {
+        fp->fp.hfile = hfile;
+    }
+
+    return fp;
 
 error:
-	if (hts_verbose >= 2)
-		fprintf(stderr, "[E::%s] fail to open file '%s'\n", __func__, fn);
-
-	if (hfile)
-		hclose_abruptly(hfile);
-
-	if (fp) {
-		free(fp->fn);
-		free(fp->fn_aux);
-		free(fp);
-	}
-	return NULL;
+    if (hts_verbose >= 2)
+        fprintf(stderr, "[E::%s] fail to open file '%s'\n", __func__, fn);
+
+    if (hfile)
+        hclose_abruptly(hfile);
+
+    if (fp) {
+        free(fp->fn);
+        free(fp->fn_aux);
+        free(fp);
+    }
+    return NULL;
 }
 
 int hts_close(htsFile *fp)
 {
-	int ret, save;
-
-	if (fp->is_bin || (fp->is_write && fp->is_compressed==1)) {
-		ret = bgzf_close(fp->fp.bgzf);
-	} else if (fp->is_cram) {
-		if (!fp->is_write) {
-			switch (cram_eof(fp->fp.cram)) {
-			case 0:
-				fprintf(stderr, "[E::%s] Failed to decode sequence.\n", __func__);
-				return -1;
-			case 2:
-				fprintf(stderr, "[W::%s] EOF marker is absent. The input is probably truncated.\n", __func__);
-				break;
-			default: /* case 1, expected EOF */
-				break;
-			}
-		}
-		ret = cram_close(fp->fp.cram);
-	} else if (fp->is_kstream) {
-	#if KS_BGZF
-		BGZF *gzfp = ((kstream_t*)fp->fp.voidp)->f;
-		ret = bgzf_close(gzfp);
-	#else
-		gzFile gzfp = ((kstream_t*)fp->fp.voidp)->f;
-		ret = gzclose(gzfp);
-	#endif
-		ks_destroy((kstream_t*)fp->fp.voidp);
-	} else {
-		ret = hclose(fp->fp.hfile);
-	}
-
-	save = errno;
-	free(fp->fn);
-	free(fp->fn_aux);
+    int ret, save;
+
+    if (fp->is_bin || (fp->is_write && fp->is_compressed==1)) {
+        ret = bgzf_close(fp->fp.bgzf);
+    } else if (fp->is_cram) {
+        if (!fp->is_write) {
+            switch (cram_eof(fp->fp.cram)) {
+            case 0:
+                fprintf(stderr, "[E::%s] Failed to decode sequence.\n", __func__);
+                return -1;
+            case 2:
+                fprintf(stderr, "[W::%s] EOF marker is absent. The input is probably truncated.\n", __func__);
+                break;
+            default: /* case 1, expected EOF */
+                break;
+            }
+        }
+        ret = cram_close(fp->fp.cram);
+    } else if (fp->is_kstream) {
+    #if KS_BGZF
+        BGZF *gzfp = ((kstream_t*)fp->fp.voidp)->f;
+        ret = bgzf_close(gzfp);
+    #else
+        gzFile gzfp = ((kstream_t*)fp->fp.voidp)->f;
+        ret = gzclose(gzfp);
+    #endif
+        ks_destroy((kstream_t*)fp->fp.voidp);
+    } else {
+        ret = hclose(fp->fp.hfile);
+    }
+
+    save = errno;
+    free(fp->fn);
+    free(fp->fn_aux);
     free(fp->line.s);
-	free(fp);
-	errno = save;
-	return ret;
+    free(fp);
+    errno = save;
+    return ret;
 }
 
 int hts_set_threads(htsFile *fp, int n)
 {
-	// TODO Plug in CRAM and other threading
-	if (fp->is_bin) {
-		return bgzf_mt(fp->fp.bgzf, n, 256);
-	}
-	else return 0;
+    // TODO Plug in CRAM and other threading
+    if (fp->is_bin) {
+        return bgzf_mt(fp->fp.bgzf, n, 256);
+    }
+    else return 0;
 }
 
 int hts_set_fai_filename(htsFile *fp, const char *fn_aux)
 {
-	free(fp->fn_aux);
-	if (fn_aux) {
-		fp->fn_aux = strdup(fn_aux);
-		if (fp->fn_aux == NULL) return -1;
-	}
-	else fp->fn_aux = NULL;
-
-	return 0;
+    free(fp->fn_aux);
+    if (fn_aux) {
+        fp->fn_aux = strdup(fn_aux);
+        if (fp->fn_aux == NULL) return -1;
+    }
+    else fp->fn_aux = NULL;
+
+    return 0;
 }
 
 // For VCF/BCF backward sweeper. Not exposing these functions because their
@@ -284,10 +309,10 @@ long hts_utell(htsFile *fp)
 
 int hts_getline(htsFile *fp, int delimiter, kstring_t *str)
 {
-	int ret, dret;
-	ret = ks_getuntil((kstream_t*)fp->fp.voidp, delimiter, str, &dret);
-	++fp->lineno;
-	return ret;
+    int ret, dret;
+    ret = ks_getuntil((kstream_t*)fp->fp.voidp, delimiter, str, &dret);
+    ++fp->lineno;
+    return ret;
 }
 
 char **hts_readlist(const char *string, int is_file, int *_n)
@@ -307,7 +332,7 @@ char **hts_readlist(const char *string, int is_file, int *_n)
         kstring_t str;
         str.s = 0; str.l = str.m = 0;
         ks = ks_init(fp);
-        while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) 
+        while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0)
         {
             if (str.l == 0) continue;
             n++;
@@ -320,14 +345,14 @@ char **hts_readlist(const char *string, int is_file, int *_n)
 #else
         gzclose(fp);
 #endif
-        free(str.s);        
+        free(str.s);
     }
     else
     {
         const char *q = string, *p = string;
         while ( 1 )
         {
-            if (*p == ',' || *p == 0) 
+            if (*p == ',' || *p == 0)
             {
                 n++;
                 hts_expand(char*,n,m,s);
@@ -346,51 +371,51 @@ char **hts_readlist(const char *string, int is_file, int *_n)
 
 char **hts_readlines(const char *fn, int *_n)
 {
-	int m = 0, n = 0, dret;
-	char **s = 0;
+    int m = 0, n = 0, dret;
+    char **s = 0;
 #if KS_BGZF
-	BGZF *fp = bgzf_open(fn, "r");
+    BGZF *fp = bgzf_open(fn, "r");
 #else
-	gzFile fp = gzopen(fn, "r");
+    gzFile fp = gzopen(fn, "r");
 #endif
-	if ( fp ) { // read from file
-		kstream_t *ks;
-		kstring_t str;
-		str.s = 0; str.l = str.m = 0;
-		ks = ks_init(fp);
-		while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) {
-			if (str.l == 0) continue;
-			if (m == n) {
-				m = m? m<<1 : 16;
-				s = (char**)realloc(s, m * sizeof(char*));
-			}
-			s[n++] = strdup(str.s);
-		}
-		ks_destroy(ks);
+    if ( fp ) { // read from file
+        kstream_t *ks;
+        kstring_t str;
+        str.s = 0; str.l = str.m = 0;
+        ks = ks_init(fp);
+        while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) {
+            if (str.l == 0) continue;
+            if (m == n) {
+                m = m? m<<1 : 16;
+                s = (char**)realloc(s, m * sizeof(char*));
+            }
+            s[n++] = strdup(str.s);
+        }
+        ks_destroy(ks);
         #if KS_BGZF
             bgzf_close(fp);
         #else
-		    gzclose(fp);
+            gzclose(fp);
         #endif
-		s = (char**)realloc(s, n * sizeof(char*));
-		free(str.s);
-	} else if (*fn == ':') { // read from string
-		const char *q, *p;
-		for (q = p = fn + 1;; ++p)
-			if (*p == ',' || *p == 0) {
-				if (m == n) {
-					m = m? m<<1 : 16;
-					s = (char**)realloc(s, m * sizeof(char*));
-				}
-				s[n] = (char*)calloc(p - q + 1, 1);
-				strncpy(s[n++], q, p - q);
-				q = p + 1;
-				if (*p == 0) break;
-			}
-	} else return 0;
-	s = (char**)realloc(s, n * sizeof(char*));
-	*_n = n;
-	return s;
+        s = (char**)realloc(s, n * sizeof(char*));
+        free(str.s);
+    } else if (*fn == ':') { // read from string
+        const char *q, *p;
+        for (q = p = fn + 1;; ++p)
+            if (*p == ',' || *p == 0) {
+                if (m == n) {
+                    m = m? m<<1 : 16;
+                    s = (char**)realloc(s, m * sizeof(char*));
+                }
+                s[n] = (char*)calloc(p - q + 1, 1);
+                strncpy(s[n++], q, p - q);
+                q = p + 1;
+                if (*p == 0) break;
+            }
+    } else return 0;
+    s = (char**)realloc(s, n * sizeof(char*));
+    *_n = n;
+    return s;
 }
 
 int hts_file_type(const char *fname)
@@ -417,7 +442,7 @@ int hts_file_type(const char *fname)
         if ( !fp ) return 0;
         if ( bgzf_read(fp, magic, 3)!=3 ) { bgzf_close(fp); return 0; }
         bgzf_close(fp);
-        if ( !strncmp((char*)magic,"##",2) ) return FT_VCF;
+        if ( !strncmp((char*)magic,"##",2) ) return FT_VCF_GZ;
         if ( !strncmp((char*)magic,"BCF",3) ) return FT_BCF_GZ;
     }
     return 0;
@@ -439,9 +464,9 @@ int hts_file_type(const char *fname)
 KSORT_INIT(_off, hts_pair64_t, pair64_lt)
 
 typedef struct {
-	int32_t m, n;
-	uint64_t loff;
-	hts_pair64_t *list;
+    int32_t m, n;
+    uint64_t loff;
+    hts_pair64_t *list;
 } bins_t;
 
 #include "htslib/khash.h"
@@ -449,194 +474,203 @@ KHASH_MAP_INIT_INT(bin, bins_t)
 typedef khash_t(bin) bidx_t;
 
 typedef struct {
-	int32_t n, m;
-	uint64_t *offset;
+    int32_t n, m;
+    uint64_t *offset;
 } lidx_t;
 
 struct __hts_idx_t {
-	int fmt, min_shift, n_lvls, n_bins;
-	uint32_t l_meta;
-	int32_t n, m;
-	uint64_t n_no_coor;
-	bidx_t **bidx;
-	lidx_t *lidx;
-	uint8_t *meta;
-	struct {
-		uint32_t last_bin, save_bin;
-		int last_coor, last_tid, save_tid, finished;
-		uint64_t last_off, save_off;
-		uint64_t off_beg, off_end;
-		uint64_t n_mapped, n_unmapped;
-	} z; // keep internal states
+    int fmt, min_shift, n_lvls, n_bins;
+    uint32_t l_meta;
+    int32_t n, m;
+    uint64_t n_no_coor;
+    bidx_t **bidx;
+    lidx_t *lidx;
+    uint8_t *meta;
+    struct {
+        uint32_t last_bin, save_bin;
+        int last_coor, last_tid, save_tid, finished;
+        uint64_t last_off, save_off;
+        uint64_t off_beg, off_end;
+        uint64_t n_mapped, n_unmapped;
+    } z; // keep internal states
 };
 
 static inline void insert_to_b(bidx_t *b, int bin, uint64_t beg, uint64_t end)
 {
-	khint_t k;
-	bins_t *l;
-	int absent;
-	k = kh_put(bin, b, bin, &absent);
-	l = &kh_value(b, k);
-	if (absent) {
-		l->m = 1; l->n = 0;
-		l->list = (hts_pair64_t*)calloc(l->m, 16);
-	}
-	if (l->n == l->m) {
-		l->m <<= 1;
-		l->list = (hts_pair64_t*)realloc(l->list, l->m * 16);
-	}
-	l->list[l->n].u = beg;
-	l->list[l->n++].v = end;
+    khint_t k;
+    bins_t *l;
+    int absent;
+    k = kh_put(bin, b, bin, &absent);
+    l = &kh_value(b, k);
+    if (absent) {
+        l->m = 1; l->n = 0;
+        l->list = (hts_pair64_t*)calloc(l->m, 16);
+    }
+    if (l->n == l->m) {
+        l->m <<= 1;
+        l->list = (hts_pair64_t*)realloc(l->list, l->m * 16);
+    }
+    l->list[l->n].u = beg;
+    l->list[l->n++].v = end;
 }
 
 static inline void insert_to_l(lidx_t *l, int64_t _beg, int64_t _end, uint64_t offset, int min_shift)
 {
-	int i, beg, end;
-	beg = _beg >> min_shift;
-	end = (_end - 1) >> min_shift;
-	if (l->m < end + 1) {
-		int old_m = l->m;
-		l->m = end + 1;
-		kroundup32(l->m);
-		l->offset = (uint64_t*)realloc(l->offset, l->m * 8);
-		memset(l->offset + old_m, 0xff, 8 * (l->m - old_m)); // fill l->offset with (uint64_t)-1
-	}
-	if (beg == end) { // to save a loop in this case
-		if (l->offset[beg] == (uint64_t)-1) l->offset[beg] = offset;
-	} else {
-		for (i = beg; i <= end; ++i)
-			if (l->offset[i] == (uint64_t)-1) l->offset[i] = offset;
-	}
-	if (l->n < end + 1) l->n = end + 1;
+    int i, beg, end;
+    beg = _beg >> min_shift;
+    end = (_end - 1) >> min_shift;
+    if (l->m < end + 1) {
+        int old_m = l->m;
+        l->m = end + 1;
+        kroundup32(l->m);
+        l->offset = (uint64_t*)realloc(l->offset, l->m * 8);
+        memset(l->offset + old_m, 0xff, 8 * (l->m - old_m)); // fill l->offset with (uint64_t)-1
+    }
+    if (beg == end) { // to save a loop in this case
+        if (l->offset[beg] == (uint64_t)-1) l->offset[beg] = offset;
+    } else {
+        for (i = beg; i <= end; ++i)
+            if (l->offset[i] == (uint64_t)-1) l->offset[i] = offset;
+    }
+    if (l->n < end + 1) l->n = end + 1;
 }
 
 hts_idx_t *hts_idx_init(int n, int fmt, uint64_t offset0, int min_shift, int n_lvls)
 {
-	hts_idx_t *idx;
-	idx = (hts_idx_t*)calloc(1, sizeof(hts_idx_t));
-	if (idx == NULL) return NULL;
-	idx->fmt = fmt;
-	idx->min_shift = min_shift;
-	idx->n_lvls = n_lvls;
-	idx->n_bins = ((1<<(3 * n_lvls + 3)) - 1) / 7;
-	idx->z.save_bin = idx->z.save_tid = idx->z.last_tid = idx->z.last_bin = 0xffffffffu;
-	idx->z.save_off = idx->z.last_off = idx->z.off_beg = idx->z.off_end = offset0;
-	idx->z.last_coor = 0xffffffffu;
-	if (n) {
-		idx->n = idx->m = n;
-		idx->bidx = (bidx_t**)calloc(n, sizeof(bidx_t*));
-		if (idx->bidx == NULL) { free(idx); return NULL; }
-		idx->lidx = (lidx_t*) calloc(n, sizeof(lidx_t));
-		if (idx->lidx == NULL) { free(idx->bidx); free(idx); return NULL; }
-	}
-	return idx;
+    hts_idx_t *idx;
+    idx = (hts_idx_t*)calloc(1, sizeof(hts_idx_t));
+    if (idx == NULL) return NULL;
+    idx->fmt = fmt;
+    idx->min_shift = min_shift;
+    idx->n_lvls = n_lvls;
+    idx->n_bins = ((1<<(3 * n_lvls + 3)) - 1) / 7;
+    idx->z.save_bin = idx->z.save_tid = idx->z.last_tid = idx->z.last_bin = 0xffffffffu;
+    idx->z.save_off = idx->z.last_off = idx->z.off_beg = idx->z.off_end = offset0;
+    idx->z.last_coor = 0xffffffffu;
+    if (n) {
+        idx->n = idx->m = n;
+        idx->bidx = (bidx_t**)calloc(n, sizeof(bidx_t*));
+        if (idx->bidx == NULL) { free(idx); return NULL; }
+        idx->lidx = (lidx_t*) calloc(n, sizeof(lidx_t));
+        if (idx->lidx == NULL) { free(idx->bidx); free(idx); return NULL; }
+    }
+    return idx;
 }
 
 static void update_loff(hts_idx_t *idx, int i, int free_lidx)
 {
-	bidx_t *bidx = idx->bidx[i];
-	lidx_t *lidx = &idx->lidx[i];
-	khint_t k;
-	int l;
-	uint64_t offset0 = 0;
-	if (bidx) {
-		k = kh_get(bin, bidx, META_BIN(idx));
-		if (k != kh_end(bidx))
-			offset0 = kh_val(bidx, k).list[0].u;
-		for (l = 0; l < lidx->n && lidx->offset[l] == (uint64_t)-1; ++l)
-			lidx->offset[l] = offset0;
-	} else l = 1;
-	for (; l < lidx->n; ++l) // fill missing values
-		if (lidx->offset[l] == (uint64_t)-1)
-			lidx->offset[l] = lidx->offset[l-1];
-	if (bidx == 0) return;
-	for (k = kh_begin(bidx); k != kh_end(bidx); ++k) // set loff
-		if (kh_exist(bidx, k))
-			kh_val(bidx, k).loff = kh_key(bidx, k) < idx->n_bins? lidx->offset[hts_bin_bot(kh_key(bidx, k), idx->n_lvls)] : 0;
-	if (free_lidx) {
-		free(lidx->offset);
-		lidx->m = lidx->n = 0;
-		lidx->offset = 0;
-	}
+    bidx_t *bidx = idx->bidx[i];
+    lidx_t *lidx = &idx->lidx[i];
+    khint_t k;
+    int l;
+    uint64_t offset0 = 0;
+    if (bidx) {
+        k = kh_get(bin, bidx, META_BIN(idx));
+        if (k != kh_end(bidx))
+            offset0 = kh_val(bidx, k).list[0].u;
+        for (l = 0; l < lidx->n && lidx->offset[l] == (uint64_t)-1; ++l)
+            lidx->offset[l] = offset0;
+    } else l = 1;
+    for (; l < lidx->n; ++l) // fill missing values
+        if (lidx->offset[l] == (uint64_t)-1)
+            lidx->offset[l] = lidx->offset[l-1];
+    if (bidx == 0) return;
+    for (k = kh_begin(bidx); k != kh_end(bidx); ++k) // set loff
+        if (kh_exist(bidx, k))
+        {
+            if ( kh_key(bidx, k) < idx->n_bins )
+            {
+                int bot_bin = hts_bin_bot(kh_key(bidx, k), idx->n_lvls);
+                // disable linear index if bot_bin out of bounds
+                kh_val(bidx, k).loff = bot_bin < lidx->n ? lidx->offset[bot_bin] : 0;
+            }
+            else
+                kh_val(bidx, k).loff = 0;
+        }
+    if (free_lidx) {
+        free(lidx->offset);
+        lidx->m = lidx->n = 0;
+        lidx->offset = 0;
+    }
 }
 
 static void compress_binning(hts_idx_t *idx, int i)
 {
-	bidx_t *bidx = idx->bidx[i];
-	khint_t k;
-	int l, m;
-	if (bidx == 0) return;
-	// merge a bin to its parent if the bin is too small
-	for (l = idx->n_lvls; l > 0; --l) {
-		unsigned start = hts_bin_first(l);
-		for (k = kh_begin(bidx); k != kh_end(bidx); ++k) {
-			bins_t *p, *q;
-			if (!kh_exist(bidx, k) || kh_key(bidx, k) >= idx->n_bins || kh_key(bidx, k) < start) continue;
-			p = &kh_value(bidx, k);
-			if (l < idx->n_lvls && p->n > 1) ks_introsort(_off, p->n, p->list);
-			if ((p->list[p->n - 1].v>>16) - (p->list[0].u>>16) < HTS_MIN_MARKER_DIST) {
-				khint_t kp;
-				kp = kh_get(bin, bidx, hts_bin_parent(kh_key(bidx, k)));
-				if (kp == kh_end(bidx)) continue;
-				q = &kh_val(bidx, kp);
-				if (q->n + p->n > q->m) {
-					q->m = q->n + p->n;
-					kroundup32(q->m);
-					q->list = (hts_pair64_t*)realloc(q->list, q->m * 16);
-				}
-				memcpy(q->list + q->n, p->list, p->n * 16);
-				q->n += p->n;
-				free(p->list);
-				kh_del(bin, bidx, k);
-			}
-		}
-	}
-	k = kh_get(bin, bidx, 0);
-	if (k != kh_end(bidx)) ks_introsort(_off, kh_val(bidx, k).n, kh_val(bidx, k).list);
-	// merge adjacent chunks that start from the same BGZF block
-	for (k = kh_begin(bidx); k != kh_end(bidx); ++k) {
-		bins_t *p;
-		if (!kh_exist(bidx, k) || kh_key(bidx, k) >= idx->n_bins) continue;
-		p = &kh_value(bidx, k);
-		for (l = 1, m = 0; l < p->n; ++l) {
-			if (p->list[m].v>>16 >= p->list[l].u>>16) {
-				if (p->list[m].v < p->list[l].v) p->list[m].v = p->list[l].v;
-			} else p->list[++m] = p->list[l];
-		}
-		p->n = m + 1;
-	}
+    bidx_t *bidx = idx->bidx[i];
+    khint_t k;
+    int l, m;
+    if (bidx == 0) return;
+    // merge a bin to its parent if the bin is too small
+    for (l = idx->n_lvls; l > 0; --l) {
+        unsigned start = hts_bin_first(l);
+        for (k = kh_begin(bidx); k != kh_end(bidx); ++k) {
+            bins_t *p, *q;
+            if (!kh_exist(bidx, k) || kh_key(bidx, k) >= idx->n_bins || kh_key(bidx, k) < start) continue;
+            p = &kh_value(bidx, k);
+            if (l < idx->n_lvls && p->n > 1) ks_introsort(_off, p->n, p->list);
+            if ((p->list[p->n - 1].v>>16) - (p->list[0].u>>16) < HTS_MIN_MARKER_DIST) {
+                khint_t kp;
+                kp = kh_get(bin, bidx, hts_bin_parent(kh_key(bidx, k)));
+                if (kp == kh_end(bidx)) continue;
+                q = &kh_val(bidx, kp);
+                if (q->n + p->n > q->m) {
+                    q->m = q->n + p->n;
+                    kroundup32(q->m);
+                    q->list = (hts_pair64_t*)realloc(q->list, q->m * 16);
+                }
+                memcpy(q->list + q->n, p->list, p->n * 16);
+                q->n += p->n;
+                free(p->list);
+                kh_del(bin, bidx, k);
+            }
+        }
+    }
+    k = kh_get(bin, bidx, 0);
+    if (k != kh_end(bidx)) ks_introsort(_off, kh_val(bidx, k).n, kh_val(bidx, k).list);
+    // merge adjacent chunks that start from the same BGZF block
+    for (k = kh_begin(bidx); k != kh_end(bidx); ++k) {
+        bins_t *p;
+        if (!kh_exist(bidx, k) || kh_key(bidx, k) >= idx->n_bins) continue;
+        p = &kh_value(bidx, k);
+        for (l = 1, m = 0; l < p->n; ++l) {
+            if (p->list[m].v>>16 >= p->list[l].u>>16) {
+                if (p->list[m].v < p->list[l].v) p->list[m].v = p->list[l].v;
+            } else p->list[++m] = p->list[l];
+        }
+        p->n = m + 1;
+    }
 }
 
 void hts_idx_finish(hts_idx_t *idx, uint64_t final_offset)
 {
-	int i;
-	if (idx == NULL || idx->z.finished) return; // do not run this function on an empty index or multiple times
-	if (idx->z.save_tid >= 0) {
-		insert_to_b(idx->bidx[idx->z.save_tid], idx->z.save_bin, idx->z.save_off, final_offset);
-		insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.off_beg, final_offset);
-		insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.n_mapped, idx->z.n_unmapped);
-	}
-	for (i = 0; i < idx->n; ++i) {
-		update_loff(idx, i, (idx->fmt == HTS_FMT_CSI));
-		compress_binning(idx, i);
-	}
-	idx->z.finished = 1;
+    int i;
+    if (idx == NULL || idx->z.finished) return; // do not run this function on an empty index or multiple times
+    if (idx->z.save_tid >= 0) {
+        insert_to_b(idx->bidx[idx->z.save_tid], idx->z.save_bin, idx->z.save_off, final_offset);
+        insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.off_beg, final_offset);
+        insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.n_mapped, idx->z.n_unmapped);
+    }
+    for (i = 0; i < idx->n; ++i) {
+        update_loff(idx, i, (idx->fmt == HTS_FMT_CSI));
+        compress_binning(idx, i);
+    }
+    idx->z.finished = 1;
 }
 
 int hts_idx_push(hts_idx_t *idx, int tid, int beg, int end, uint64_t offset, int is_mapped)
 {
-	int bin;
-	if (tid >= idx->m) { // enlarge the index
-		int32_t oldm = idx->m;
-		idx->m = idx->m? idx->m<<1 : 2;
-		idx->bidx = (bidx_t**)realloc(idx->bidx, idx->m * sizeof(bidx_t*));
-		idx->lidx = (lidx_t*) realloc(idx->lidx, idx->m * sizeof(lidx_t));
-		memset(&idx->bidx[oldm], 0, (idx->m - oldm) * sizeof(bidx_t*));
-		memset(&idx->lidx[oldm], 0, (idx->m - oldm) * sizeof(lidx_t));
-	}
-	if (idx->n < tid + 1) idx->n = tid + 1;
-	if (idx->z.finished) return 0;
-	if (idx->z.last_tid != tid || (idx->z.last_tid >= 0 && tid < 0)) { // change of chromosome
+    int bin;
+    if (tid >= idx->m) { // enlarge the index
+        int32_t oldm = idx->m;
+        idx->m = idx->m? idx->m<<1 : 2;
+        idx->bidx = (bidx_t**)realloc(idx->bidx, idx->m * sizeof(bidx_t*));
+        idx->lidx = (lidx_t*) realloc(idx->lidx, idx->m * sizeof(lidx_t));
+        memset(&idx->bidx[oldm], 0, (idx->m - oldm) * sizeof(bidx_t*));
+        memset(&idx->lidx[oldm], 0, (idx->m - oldm) * sizeof(lidx_t));
+    }
+    if (idx->n < tid + 1) idx->n = tid + 1;
+    if (idx->z.finished) return 0;
+    if (idx->z.last_tid != tid || (idx->z.last_tid >= 0 && tid < 0)) { // change of chromosome
         if ( tid>=0 && idx->n_no_coor )
         {
             if (hts_verbose >= 1) fprintf(stderr,"[E::%s] NO_COOR reads not in a single block at the end %d %d\n", __func__, tid,idx->z.last_tid);
@@ -647,332 +681,328 @@ int hts_idx_push(hts_idx_t *idx, int tid, int beg, int end, uint64_t offset, int
             if (hts_verbose >= 1) fprintf(stderr, "[E::%s] chromosome blocks not continuous\n", __func__);
             return -1;
         }
-		idx->z.last_tid = tid;
-		idx->z.last_bin = 0xffffffffu;
-	} else if (tid >= 0 && idx->z.last_coor > beg) { // test if positions are out of order
-		if (hts_verbose >= 1) fprintf(stderr, "[E::%s] unsorted positions\n", __func__);
-		return -1;
-	}
+        idx->z.last_tid = tid;
+        idx->z.last_bin = 0xffffffffu;
+    } else if (tid >= 0 && idx->z.last_coor > beg) { // test if positions are out of order
+        if (hts_verbose >= 1) fprintf(stderr, "[E::%s] unsorted positions\n", __func__);
+        return -1;
+    }
     if ( tid>=0 )
     {
-	    if (idx->bidx[tid] == 0) idx->bidx[tid] = kh_init(bin);
+        if (idx->bidx[tid] == 0) idx->bidx[tid] = kh_init(bin);
         if ( is_mapped)
             insert_to_l(&idx->lidx[tid], beg, end, idx->z.last_off, idx->min_shift); // last_off points to the start of the current record
     }
-	else idx->n_no_coor++;
-	bin = hts_reg2bin(beg, end, idx->min_shift, idx->n_lvls);
-	if ((int)idx->z.last_bin != bin) { // then possibly write the binning index
-		if (idx->z.save_bin != 0xffffffffu) // save_bin==0xffffffffu only happens to the first record
-			insert_to_b(idx->bidx[idx->z.save_tid], idx->z.save_bin, idx->z.save_off, idx->z.last_off);
-		if (idx->z.last_bin == 0xffffffffu && idx->z.save_bin != 0xffffffffu) { // change of chr; keep meta information
-			idx->z.off_end = idx->z.last_off;
-			insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.off_beg, idx->z.off_end);
-			insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.n_mapped, idx->z.n_unmapped);
-			idx->z.n_mapped = idx->z.n_unmapped = 0;
-			idx->z.off_beg = idx->z.off_end;
-		}
-		idx->z.save_off = idx->z.last_off;
-		idx->z.save_bin = idx->z.last_bin = bin;
-		idx->z.save_tid = tid;
-		if (tid < 0) { // come to the end of the records having coordinates
-			hts_idx_finish(idx, offset);
-			return 0;
-		}
-	}
-	if (is_mapped) ++idx->z.n_mapped;
-	else ++idx->z.n_unmapped;
-	idx->z.last_off = offset;
-	idx->z.last_coor = beg;
-	return 0;
+    else idx->n_no_coor++;
+    bin = hts_reg2bin(beg, end, idx->min_shift, idx->n_lvls);
+    if ((int)idx->z.last_bin != bin) { // then possibly write the binning index
+        if (idx->z.save_bin != 0xffffffffu) // save_bin==0xffffffffu only happens to the first record
+            insert_to_b(idx->bidx[idx->z.save_tid], idx->z.save_bin, idx->z.save_off, idx->z.last_off);
+        if (idx->z.last_bin == 0xffffffffu && idx->z.save_bin != 0xffffffffu) { // change of chr; keep meta information
+            idx->z.off_end = idx->z.last_off;
+            insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.off_beg, idx->z.off_end);
+            insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.n_mapped, idx->z.n_unmapped);
+            idx->z.n_mapped = idx->z.n_unmapped = 0;
+            idx->z.off_beg = idx->z.off_end;
+        }
+        idx->z.save_off = idx->z.last_off;
+        idx->z.save_bin = idx->z.last_bin = bin;
+        idx->z.save_tid = tid;
+    }
+    if (is_mapped) ++idx->z.n_mapped;
+    else ++idx->z.n_unmapped;
+    idx->z.last_off = offset;
+    idx->z.last_coor = beg;
+    return 0;
 }
 
 void hts_idx_destroy(hts_idx_t *idx)
 {
-	khint_t k;
-	int i;
-	if (idx == 0) return;
-	// For HTS_FMT_CRAI, idx actually points to a different type -- see sam.c
-	if (idx->fmt == HTS_FMT_CRAI) { free(idx); return; }
-
-	for (i = 0; i < idx->m; ++i) {
-		bidx_t *bidx = idx->bidx[i];
-		free(idx->lidx[i].offset);
-		if (bidx == 0) continue;
-		for (k = kh_begin(bidx); k != kh_end(bidx); ++k)
-			if (kh_exist(bidx, k))
-				free(kh_value(bidx, k).list);
-		kh_destroy(bin, bidx);
-	}
-	free(idx->bidx); free(idx->lidx); free(idx->meta);
-	free(idx);
+    khint_t k;
+    int i;
+    if (idx == 0) return;
+    // For HTS_FMT_CRAI, idx actually points to a different type -- see sam.c
+    if (idx->fmt == HTS_FMT_CRAI) { free(idx); return; }
+
+    for (i = 0; i < idx->m; ++i) {
+        bidx_t *bidx = idx->bidx[i];
+        free(idx->lidx[i].offset);
+        if (bidx == 0) continue;
+        for (k = kh_begin(bidx); k != kh_end(bidx); ++k)
+            if (kh_exist(bidx, k))
+                free(kh_value(bidx, k).list);
+        kh_destroy(bin, bidx);
+    }
+    free(idx->bidx); free(idx->lidx); free(idx->meta);
+    free(idx);
 }
 
 static inline long idx_read(int is_bgzf, void *fp, void *buf, long l)
 {
-	if (is_bgzf) return bgzf_read((BGZF*)fp, buf, l);
-	else return (long)fread(buf, 1, l, (FILE*)fp);
+    if (is_bgzf) return bgzf_read((BGZF*)fp, buf, l);
+    else return (long)fread(buf, 1, l, (FILE*)fp);
 }
 
 static inline long idx_write(int is_bgzf, void *fp, const void *buf, long l)
 {
-	if (is_bgzf) return bgzf_write((BGZF*)fp, buf, l);
-	else return (long)fwrite(buf, 1, l, (FILE*)fp);
+    if (is_bgzf) return bgzf_write((BGZF*)fp, buf, l);
+    else return (long)fwrite(buf, 1, l, (FILE*)fp);
 }
 
 static inline void swap_bins(bins_t *p)
 {
-	int i;
-	for (i = 0; i < p->n; ++i) {
-		ed_swap_8p(&p->list[i].u);
-		ed_swap_8p(&p->list[i].v);
-	}
+    int i;
+    for (i = 0; i < p->n; ++i) {
+        ed_swap_8p(&p->list[i].u);
+        ed_swap_8p(&p->list[i].v);
+    }
 }
 
 static void hts_idx_save_core(const hts_idx_t *idx, void *fp, int fmt)
 {
-	int32_t i, size, is_be;
-	int is_bgzf = (fmt != HTS_FMT_BAI);
-	is_be = ed_is_big();
-	if (is_be) {
-		uint32_t x = idx->n;
-		idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
-	} else idx_write(is_bgzf, fp, &idx->n, 4);
-	if (fmt == HTS_FMT_TBI && idx->l_meta) idx_write(is_bgzf, fp, idx->meta, idx->l_meta);
-	for (i = 0; i < idx->n; ++i) {
-		khint_t k;
-		bidx_t *bidx = idx->bidx[i];
-		lidx_t *lidx = &idx->lidx[i];
-		// write binning index
-		size = bidx? kh_size(bidx) : 0;
-		if (is_be) { // big endian
-			uint32_t x = size;
-			idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
-		} else idx_write(is_bgzf, fp, &size, 4);
-		if (bidx == 0) goto write_lidx;
-		for (k = kh_begin(bidx); k != kh_end(bidx); ++k) {
-			bins_t *p;
-			if (!kh_exist(bidx, k)) continue;
-			p = &kh_value(bidx, k);
-			if (is_be) { // big endian
-				uint32_t x;
-				x = kh_key(bidx, k); idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
-				if (fmt == HTS_FMT_CSI) {
-					uint64_t y = kh_val(bidx, k).loff;
-					idx_write(is_bgzf, fp, ed_swap_4p(&y), 8);
-				}
-				x = p->n; idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
-				swap_bins(p);
-				idx_write(is_bgzf, fp, p->list, 16 * p->n);
-				swap_bins(p);
-			} else {
-				idx_write(is_bgzf, fp, &kh_key(bidx, k), 4);
-				if (fmt == HTS_FMT_CSI) idx_write(is_bgzf, fp, &kh_val(bidx, k).loff, 8);
-				//int j;for(j=0;j<p->n;++j)fprintf(stderr,"%d,%llx,%d,%llx:%llx\n",kh_key(bidx,k),kh_val(bidx, k).loff,j,p->list[j].u,p->list[j].v);
-				idx_write(is_bgzf, fp, &p->n, 4);
-				idx_write(is_bgzf, fp, p->list, p->n << 4);
-			}
-		}
+    int32_t i, size, is_be;
+    int is_bgzf = (fmt != HTS_FMT_BAI);
+    is_be = ed_is_big();
+    if (is_be) {
+        uint32_t x = idx->n;
+        idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
+    } else idx_write(is_bgzf, fp, &idx->n, 4);
+    if (fmt == HTS_FMT_TBI && idx->l_meta) idx_write(is_bgzf, fp, idx->meta, idx->l_meta);
+    for (i = 0; i < idx->n; ++i) {
+        khint_t k;
+        bidx_t *bidx = idx->bidx[i];
+        lidx_t *lidx = &idx->lidx[i];
+        // write binning index
+        size = bidx? kh_size(bidx) : 0;
+        if (is_be) { // big endian
+            uint32_t x = size;
+            idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
+        } else idx_write(is_bgzf, fp, &size, 4);
+        if (bidx == 0) goto write_lidx;
+        for (k = kh_begin(bidx); k != kh_end(bidx); ++k) {
+            bins_t *p;
+            if (!kh_exist(bidx, k)) continue;
+            p = &kh_value(bidx, k);
+            if (is_be) { // big endian
+                uint32_t x;
+                x = kh_key(bidx, k); idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
+                if (fmt == HTS_FMT_CSI) {
+                    uint64_t y = kh_val(bidx, k).loff;
+                    idx_write(is_bgzf, fp, ed_swap_4p(&y), 8);
+                }
+                x = p->n; idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
+                swap_bins(p);
+                idx_write(is_bgzf, fp, p->list, 16 * p->n);
+                swap_bins(p);
+            } else {
+                idx_write(is_bgzf, fp, &kh_key(bidx, k), 4);
+                if (fmt == HTS_FMT_CSI) idx_write(is_bgzf, fp, &kh_val(bidx, k).loff, 8);
+                //int j;for(j=0;j<p->n;++j)fprintf(stderr,"%d,%llx,%d,%llx:%llx\n",kh_key(bidx,k),kh_val(bidx, k).loff,j,p->list[j].u,p->list[j].v);
+                idx_write(is_bgzf, fp, &p->n, 4);
+                idx_write(is_bgzf, fp, p->list, p->n << 4);
+            }
+        }
 write_lidx:
-		if (fmt != HTS_FMT_CSI) {
-			if (is_be) {
-				int32_t x = lidx->n;
-				idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
-				for (x = 0; x < lidx->n; ++x) ed_swap_8p(&lidx->offset[x]);
-				idx_write(is_bgzf, fp, lidx->offset, lidx->n << 3);
-				for (x = 0; x < lidx->n; ++x) ed_swap_8p(&lidx->offset[x]);
-			} else {
-				idx_write(is_bgzf, fp, &lidx->n, 4);
-				idx_write(is_bgzf, fp, lidx->offset, lidx->n << 3);
-			}
-		}
-	}
-	if (is_be) { // write the number of reads without coordinates
-		uint64_t x = idx->n_no_coor;
-		idx_write(is_bgzf, fp, &x, 8);
-	} else idx_write(is_bgzf, fp, &idx->n_no_coor, 8);
+        if (fmt != HTS_FMT_CSI) {
+            if (is_be) {
+                int32_t x = lidx->n;
+                idx_write(is_bgzf, fp, ed_swap_4p(&x), 4);
+                for (x = 0; x < lidx->n; ++x) ed_swap_8p(&lidx->offset[x]);
+                idx_write(is_bgzf, fp, lidx->offset, lidx->n << 3);
+                for (x = 0; x < lidx->n; ++x) ed_swap_8p(&lidx->offset[x]);
+            } else {
+                idx_write(is_bgzf, fp, &lidx->n, 4);
+                idx_write(is_bgzf, fp, lidx->offset, lidx->n << 3);
+            }
+        }
+    }
+    if (is_be) { // write the number of reads without coordinates
+        uint64_t x = idx->n_no_coor;
+        idx_write(is_bgzf, fp, &x, 8);
+    } else idx_write(is_bgzf, fp, &idx->n_no_coor, 8);
 }
 
 void hts_idx_save(const hts_idx_t *idx, const char *fn, int fmt)
 {
-	char *fnidx;
-	fnidx = (char*)calloc(1, strlen(fn) + 5);
-	strcpy(fnidx, fn);
-	if (fmt == HTS_FMT_CSI) {
-		BGZF *fp;
-		uint32_t x[3];
-		int is_be, i;
-		is_be = ed_is_big();
-		fp = bgzf_open(strcat(fnidx, ".csi"), "w");
-		bgzf_write(fp, "CSI\1", 4);
-		x[0] = idx->min_shift; x[1] = idx->n_lvls; x[2] = idx->l_meta;
-		if (is_be) {
-			for (i = 0; i < 3; ++i)
-				bgzf_write(fp, ed_swap_4p(&x[i]), 4);
-		} else bgzf_write(fp, &x, 12);
-		if (idx->l_meta) bgzf_write(fp, idx->meta, idx->l_meta);
-		hts_idx_save_core(idx, fp, HTS_FMT_CSI);
-		bgzf_close(fp);
-	} else if (fmt == HTS_FMT_TBI) {
-		BGZF *fp;
-		fp = bgzf_open(strcat(fnidx, ".tbi"), "w");
-		bgzf_write(fp, "TBI\1", 4);
-		hts_idx_save_core(idx, fp, HTS_FMT_TBI);
-		bgzf_close(fp);
-	} else if (fmt == HTS_FMT_BAI) {
-		FILE *fp;
-		fp = fopen(strcat(fnidx, ".bai"), "w");
-		fwrite("BAI\1", 1, 4, fp);
-		hts_idx_save_core(idx, fp, HTS_FMT_BAI);
-		fclose(fp);
-	} else abort();
-	free(fnidx);
+    char *fnidx;
+    fnidx = (char*)calloc(1, strlen(fn) + 5);
+    strcpy(fnidx, fn);
+    if (fmt == HTS_FMT_CSI) {
+        BGZF *fp;
+        uint32_t x[3];
+        int is_be, i;
+        is_be = ed_is_big();
+        fp = bgzf_open(strcat(fnidx, ".csi"), "w");
+        bgzf_write(fp, "CSI\1", 4);
+        x[0] = idx->min_shift; x[1] = idx->n_lvls; x[2] = idx->l_meta;
+        if (is_be) {
+            for (i = 0; i < 3; ++i)
+                bgzf_write(fp, ed_swap_4p(&x[i]), 4);
+        } else bgzf_write(fp, &x, 12);
+        if (idx->l_meta) bgzf_write(fp, idx->meta, idx->l_meta);
+        hts_idx_save_core(idx, fp, HTS_FMT_CSI);
+        bgzf_close(fp);
+    } else if (fmt == HTS_FMT_TBI) {
+        BGZF *fp;
+        fp = bgzf_open(strcat(fnidx, ".tbi"), "w");
+        bgzf_write(fp, "TBI\1", 4);
+        hts_idx_save_core(idx, fp, HTS_FMT_TBI);
+        bgzf_close(fp);
+    } else if (fmt == HTS_FMT_BAI) {
+        FILE *fp;
+        fp = fopen(strcat(fnidx, ".bai"), "w");
+        fwrite("BAI\1", 1, 4, fp);
+        hts_idx_save_core(idx, fp, HTS_FMT_BAI);
+        fclose(fp);
+    } else abort();
+    free(fnidx);
 }
 
 static int hts_idx_load_core(hts_idx_t *idx, void *fp, int fmt)
 {
-	int32_t i, n, is_be;
-	int is_bgzf = (fmt != HTS_FMT_BAI);
-	is_be = ed_is_big();
-	if (idx == NULL) return -4;
-	for (i = 0; i < idx->n; ++i) {
-		bidx_t *h;
-		lidx_t *l = &idx->lidx[i];
-		uint32_t key;
-		int j, absent;
-		bins_t *p;
-		h = idx->bidx[i] = kh_init(bin);
-		if (idx_read(is_bgzf, fp, &n, 4) != 4) return -1;
-		if (is_be) ed_swap_4p(&n);
-		for (j = 0; j < n; ++j) {
-			khint_t k;
-			if (idx_read(is_bgzf, fp, &key, 4) != 4) return -1;
-			if (is_be) ed_swap_4p(&key);
-			k = kh_put(bin, h, key, &absent);
-			if (absent <= 0) return -3; // Duplicate bin number
-			p = &kh_val(h, k);
-			if (fmt == HTS_FMT_CSI) {
-				if (idx_read(is_bgzf, fp, &p->loff, 8) != 8) return -1;
-				if (is_be) ed_swap_8p(&p->loff);
-			} else p->loff = 0;
-			if (idx_read(is_bgzf, fp, &p->n, 4) != 4) return -1;
-			if (is_be) ed_swap_4p(&p->n);
-			p->m = p->n;
-			p->list = (hts_pair64_t*)malloc(p->m * 16);
-			if (p->list == NULL) return -2;
-			if (idx_read(is_bgzf, fp, p->list, p->n<<4) != p->n<<4) return -1;
-			if (is_be) swap_bins(p);
-		}
-		if (fmt != HTS_FMT_CSI) { // load linear index
-			int j;
-			if (idx_read(is_bgzf, fp, &l->n, 4) != 4) return -1;
-			if (is_be) ed_swap_4p(&l->n);
-			l->m = l->n;
-			l->offset = (uint64_t*)malloc(l->n << 3);
-			if (l->offset == NULL) return -2;
-			if (idx_read(is_bgzf, fp, l->offset, l->n << 3) != l->n << 3) return -1;
-			if (is_be) for (j = 0; j < l->n; ++j) ed_swap_8p(&l->offset[j]);
-			for (j = 1; j < l->n; ++j) // fill missing values; may happen given older samtools and tabix
-				if (l->offset[j] == 0) l->offset[j] = l->offset[j-1];
-			update_loff(idx, i, 1);
-		}
-	}
-	if (idx_read(is_bgzf, fp, &idx->n_no_coor, 8) != 8) idx->n_no_coor = 0;
-	if (is_be) ed_swap_8p(&idx->n_no_coor);
-	return 0;
+    int32_t i, n, is_be;
+    int is_bgzf = (fmt != HTS_FMT_BAI);
+    is_be = ed_is_big();
+    if (idx == NULL) return -4;
+    for (i = 0; i < idx->n; ++i) {
+        bidx_t *h;
+        lidx_t *l = &idx->lidx[i];
+        uint32_t key;
+        int j, absent;
+        bins_t *p;
+        h = idx->bidx[i] = kh_init(bin);
+        if (idx_read(is_bgzf, fp, &n, 4) != 4) return -1;
+        if (is_be) ed_swap_4p(&n);
+        for (j = 0; j < n; ++j) {
+            khint_t k;
+            if (idx_read(is_bgzf, fp, &key, 4) != 4) return -1;
+            if (is_be) ed_swap_4p(&key);
+            k = kh_put(bin, h, key, &absent);
+            if (absent <= 0) return -3; // Duplicate bin number
+            p = &kh_val(h, k);
+            if (fmt == HTS_FMT_CSI) {
+                if (idx_read(is_bgzf, fp, &p->loff, 8) != 8) return -1;
+                if (is_be) ed_swap_8p(&p->loff);
+            } else p->loff = 0;
+            if (idx_read(is_bgzf, fp, &p->n, 4) != 4) return -1;
+            if (is_be) ed_swap_4p(&p->n);
+            p->m = p->n;
+            p->list = (hts_pair64_t*)malloc(p->m * 16);
+            if (p->list == NULL) return -2;
+            if (idx_read(is_bgzf, fp, p->list, p->n<<4) != p->n<<4) return -1;
+            if (is_be) swap_bins(p);
+        }
+        if (fmt != HTS_FMT_CSI) { // load linear index
+            int j;
+            if (idx_read(is_bgzf, fp, &l->n, 4) != 4) return -1;
+            if (is_be) ed_swap_4p(&l->n);
+            l->m = l->n;
+            l->offset = (uint64_t*)malloc(l->n << 3);
+            if (l->offset == NULL) return -2;
+            if (idx_read(is_bgzf, fp, l->offset, l->n << 3) != l->n << 3) return -1;
+            if (is_be) for (j = 0; j < l->n; ++j) ed_swap_8p(&l->offset[j]);
+            for (j = 1; j < l->n; ++j) // fill missing values; may happen given older samtools and tabix
+                if (l->offset[j] == 0) l->offset[j] = l->offset[j-1];
+            update_loff(idx, i, 1);
+        }
+    }
+    if (idx_read(is_bgzf, fp, &idx->n_no_coor, 8) != 8) idx->n_no_coor = 0;
+    if (is_be) ed_swap_8p(&idx->n_no_coor);
+    return 0;
 }
 
 hts_idx_t *hts_idx_load_local(const char *fn, int fmt)
 {
-	uint8_t magic[4];
-	int i, is_be;
-	hts_idx_t *idx = NULL;
-	is_be = ed_is_big();
-	if (fmt == HTS_FMT_CSI) {
-		BGZF *fp;
-		uint32_t x[3], n;
-		uint8_t *meta = 0;
-		if ((fp = bgzf_open(fn, "r")) == 0) return NULL;
-		if (bgzf_read(fp, magic, 4) != 4) goto csi_fail;
-		if (memcmp(magic, "CSI\1", 4) != 0) goto csi_fail;
-		if (bgzf_read(fp, x, 12) != 12) goto csi_fail;
-		if (is_be) for (i = 0; i < 3; ++i) ed_swap_4p(&x[i]);
-		if (x[2]) {
-			if ((meta = (uint8_t*)malloc(x[2])) == NULL) goto csi_fail;
-			if (bgzf_read(fp, meta, x[2]) != x[2]) goto csi_fail;
-		}
-		if (bgzf_read(fp, &n, 4) != 4) goto csi_fail;
-		if (is_be) ed_swap_4p(&n);
-		if ((idx = hts_idx_init(n, fmt, 0, x[0], x[1])) == NULL) goto csi_fail;
-		idx->l_meta = x[2];
-		idx->meta = meta;
-		meta = NULL;
-		if (hts_idx_load_core(idx, fp, HTS_FMT_CSI) < 0) goto csi_fail;
-		bgzf_close(fp);
-		return idx;
-
-	csi_fail:
-		bgzf_close(fp);
-		hts_idx_destroy(idx);
-		free(meta);
-		return NULL;
-
-	} else if (fmt == HTS_FMT_TBI) {
-		BGZF *fp;
-		uint32_t x[8];
-		if ((fp = bgzf_open(fn, "r")) == 0) return NULL;
-		if (bgzf_read(fp, magic, 4) != 4) goto tbi_fail;
-		if (memcmp(magic, "TBI\1", 4) != 0) goto tbi_fail;
-		if (bgzf_read(fp, x, 32) != 32) goto tbi_fail;
-		if (is_be) for (i = 0; i < 8; ++i) ed_swap_4p(&x[i]);
-		if ((idx = hts_idx_init(x[0], fmt, 0, 14, 5)) == NULL) goto tbi_fail;
-		idx->l_meta = 28 + x[7];
-		if ((idx->meta = (uint8_t*)malloc(idx->l_meta)) == NULL) goto tbi_fail;
-		memcpy(idx->meta, &x[1], 28);
-		if (bgzf_read(fp, idx->meta + 28, x[7]) != x[7]) goto tbi_fail;
-		if (hts_idx_load_core(idx, fp, HTS_FMT_TBI) < 0) goto tbi_fail;
-		bgzf_close(fp);
-		return idx;
-
-	tbi_fail:
-		bgzf_close(fp);
-		hts_idx_destroy(idx);
-		return NULL;
-
-	} else if (fmt == HTS_FMT_BAI) {
-		uint32_t n;
-		FILE *fp;
-		if ((fp = fopen(fn, "rb")) == 0) return NULL;
-		if (fread(magic, 1, 4, fp) != 4) goto bai_fail;
-		if (memcmp(magic, "BAI\1", 4) != 0) goto bai_fail;
-		if (fread(&n, 4, 1, fp) != 1) goto bai_fail;
-		if (is_be) ed_swap_4p(&n);
-		idx = hts_idx_init(n, fmt, 0, 14, 5);
-		if (hts_idx_load_core(idx, fp, HTS_FMT_BAI) < 0) goto bai_fail;
-		fclose(fp);
-		return idx;
-
-	bai_fail:
-		fclose(fp);
-		hts_idx_destroy(idx);
-		return NULL;
-
-	} else abort();
+    uint8_t magic[4];
+    int i, is_be;
+    hts_idx_t *idx = NULL;
+    is_be = ed_is_big();
+    if (fmt == HTS_FMT_CSI) {
+        BGZF *fp;
+        uint32_t x[3], n;
+        uint8_t *meta = 0;
+        if ((fp = bgzf_open(fn, "r")) == 0) return NULL;
+        if (bgzf_read(fp, magic, 4) != 4) goto csi_fail;
+        if (memcmp(magic, "CSI\1", 4) != 0) goto csi_fail;
+        if (bgzf_read(fp, x, 12) != 12) goto csi_fail;
+        if (is_be) for (i = 0; i < 3; ++i) ed_swap_4p(&x[i]);
+        if (x[2]) {
+            if ((meta = (uint8_t*)malloc(x[2])) == NULL) goto csi_fail;
+            if (bgzf_read(fp, meta, x[2]) != x[2]) goto csi_fail;
+        }
+        if (bgzf_read(fp, &n, 4) != 4) goto csi_fail;
+        if (is_be) ed_swap_4p(&n);
+        if ((idx = hts_idx_init(n, fmt, 0, x[0], x[1])) == NULL) goto csi_fail;
+        idx->l_meta = x[2];
+        idx->meta = meta;
+        meta = NULL;
+        if (hts_idx_load_core(idx, fp, HTS_FMT_CSI) < 0) goto csi_fail;
+        bgzf_close(fp);
+        return idx;
+
+    csi_fail:
+        bgzf_close(fp);
+        hts_idx_destroy(idx);
+        free(meta);
+        return NULL;
+
+    } else if (fmt == HTS_FMT_TBI) {
+        BGZF *fp;
+        uint32_t x[8];
+        if ((fp = bgzf_open(fn, "r")) == 0) return NULL;
+        if (bgzf_read(fp, magic, 4) != 4) goto tbi_fail;
+        if (memcmp(magic, "TBI\1", 4) != 0) goto tbi_fail;
+        if (bgzf_read(fp, x, 32) != 32) goto tbi_fail;
+        if (is_be) for (i = 0; i < 8; ++i) ed_swap_4p(&x[i]);
+        if ((idx = hts_idx_init(x[0], fmt, 0, 14, 5)) == NULL) goto tbi_fail;
+        idx->l_meta = 28 + x[7];
+        if ((idx->meta = (uint8_t*)malloc(idx->l_meta)) == NULL) goto tbi_fail;
+        memcpy(idx->meta, &x[1], 28);
+        if (bgzf_read(fp, idx->meta + 28, x[7]) != x[7]) goto tbi_fail;
+        if (hts_idx_load_core(idx, fp, HTS_FMT_TBI) < 0) goto tbi_fail;
+        bgzf_close(fp);
+        return idx;
+
+    tbi_fail:
+        bgzf_close(fp);
+        hts_idx_destroy(idx);
+        return NULL;
+
+    } else if (fmt == HTS_FMT_BAI) {
+        uint32_t n;
+        FILE *fp;
+        if ((fp = fopen(fn, "rb")) == 0) return NULL;
+        if (fread(magic, 1, 4, fp) != 4) goto bai_fail;
+        if (memcmp(magic, "BAI\1", 4) != 0) goto bai_fail;
+        if (fread(&n, 4, 1, fp) != 1) goto bai_fail;
+        if (is_be) ed_swap_4p(&n);
+        idx = hts_idx_init(n, fmt, 0, 14, 5);
+        if (hts_idx_load_core(idx, fp, HTS_FMT_BAI) < 0) goto bai_fail;
+        fclose(fp);
+        return idx;
+
+    bai_fail:
+        fclose(fp);
+        hts_idx_destroy(idx);
+        return NULL;
+
+    } else abort();
 }
 
 void hts_idx_set_meta(hts_idx_t *idx, int l_meta, uint8_t *meta, int is_copy)
 {
-	if (idx->meta) free(idx->meta);
-	idx->l_meta = l_meta;
-	if (is_copy) {
-		idx->meta = (uint8_t*)malloc(l_meta);
-		memcpy(idx->meta, meta, l_meta);
-	} else idx->meta = meta;
+    if (idx->meta) free(idx->meta);
+    idx->l_meta = l_meta;
+    if (is_copy) {
+        idx->meta = (uint8_t*)malloc(l_meta);
+        memcpy(idx->meta, meta, l_meta);
+    } else idx->meta = meta;
 }
 
 uint8_t *hts_idx_get_meta(hts_idx_t *idx, int *l_meta)
 {
-	*l_meta = idx->l_meta;
-	return idx->meta;
+    *l_meta = idx->l_meta;
+    return idx->meta;
 }
 
 const char **hts_idx_seqnames(const hts_idx_t *idx, int *n, hts_id2name_f getid, void *hdr)
@@ -997,26 +1027,26 @@ const char **hts_idx_seqnames(const hts_idx_t *idx, int *n, hts_id2name_f getid,
 
 int hts_idx_get_stat(const hts_idx_t* idx, int tid, uint64_t* mapped, uint64_t* unmapped)
 {
-	if ( idx->fmt == HTS_FMT_CRAI ) {
-		*mapped = 0; *unmapped = 0;
-		return -1;
-	}
-
-	bidx_t *h = idx->bidx[tid];
-	khint_t k = kh_get(bin, h, META_BIN(idx));
-	if (k != kh_end(h)) {
-		*mapped = kh_val(h, k).list[1].u;
-		*unmapped = kh_val(h, k).list[1].v;
-		return 0;
-	} else {
-		*mapped = 0; *unmapped = 0;
-		return -1;
-	}
+    if ( idx->fmt == HTS_FMT_CRAI ) {
+        *mapped = 0; *unmapped = 0;
+        return -1;
+    }
+
+    bidx_t *h = idx->bidx[tid];
+    khint_t k = kh_get(bin, h, META_BIN(idx));
+    if (k != kh_end(h)) {
+        *mapped = kh_val(h, k).list[1].u;
+        *unmapped = kh_val(h, k).list[1].v;
+        return 0;
+    } else {
+        *mapped = 0; *unmapped = 0;
+        return -1;
+    }
 }
 
 uint64_t hts_idx_get_n_no_coor(const hts_idx_t* idx)
 {
-	return idx->n_no_coor;
+    return idx->n_no_coor;
 }
 
 /****************
@@ -1025,36 +1055,36 @@ uint64_t hts_idx_get_n_no_coor(const hts_idx_t* idx)
 
 static inline int reg2bins(int64_t beg, int64_t end, hts_itr_t *itr, int min_shift, int n_lvls)
 {
-	int l, t, s = min_shift + (n_lvls<<1) + n_lvls;
-	if (beg >= end) return 0;
-	if (end >= 1LL<<s) end = 1LL<<s;
-	for (--end, l = 0, t = 0; l <= n_lvls; s -= 3, t += 1<<((l<<1)+l), ++l) {
-		int b, e, n, i;
-		b = t + (beg>>s); e = t + (end>>s); n = e - b + 1;
-		if (itr->bins.n + n > itr->bins.m) {
-			itr->bins.m = itr->bins.n + n;
-			kroundup32(itr->bins.m);
-			itr->bins.a = (int*)realloc(itr->bins.a, sizeof(int) * itr->bins.m);
-		}
-		for (i = b; i <= e; ++i) itr->bins.a[itr->bins.n++] = i;
-	}
-	return itr->bins.n;
+    int l, t, s = min_shift + (n_lvls<<1) + n_lvls;
+    if (beg >= end) return 0;
+    if (end >= 1LL<<s) end = 1LL<<s;
+    for (--end, l = 0, t = 0; l <= n_lvls; s -= 3, t += 1<<((l<<1)+l), ++l) {
+        int b, e, n, i;
+        b = t + (beg>>s); e = t + (end>>s); n = e - b + 1;
+        if (itr->bins.n + n > itr->bins.m) {
+            itr->bins.m = itr->bins.n + n;
+            kroundup32(itr->bins.m);
+            itr->bins.a = (int*)realloc(itr->bins.a, sizeof(int) * itr->bins.m);
+        }
+        for (i = b; i <= e; ++i) itr->bins.a[itr->bins.n++] = i;
+    }
+    return itr->bins.n;
 }
 
 hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec)
 {
-	int i, n_off, l, bin;
-	hts_pair64_t *off;
-	khint_t k;
-	bidx_t *bidx;
-	uint64_t min_off;
-	hts_itr_t *iter = 0;
-	if (tid < 0) {
-		int finished0 = 0;
-		uint64_t off0 = (uint64_t)-1;
-		khint_t k;
-		switch (tid) {
-		case HTS_IDX_START:
+    int i, n_off, l, bin;
+    hts_pair64_t *off;
+    khint_t k;
+    bidx_t *bidx;
+    uint64_t min_off;
+    hts_itr_t *iter = 0;
+    if (tid < 0) {
+        int finished0 = 0;
+        uint64_t off0 = (uint64_t)-1;
+        khint_t k;
+        switch (tid) {
+        case HTS_IDX_START:
             // Find the smallest offset, note that sequence ids may not be ordered sequentially
             for (i=0; i<idx->n; i++)
             {
@@ -1066,7 +1096,7 @@ hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_re
             if ( off0==(uint64_t)-1 && idx->n_no_coor ) off0 = 0; // only no-coor reads in this bam
             break;
 
-		case HTS_IDX_NOCOOR:
+        case HTS_IDX_NOCOOR:
             if ( idx->n>0 )
             {
                 bidx = idx->bidx[idx->n - 1];
@@ -1074,172 +1104,172 @@ hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_re
                 if (k != kh_end(bidx)) off0 = kh_val(bidx, k).list[0].v;
             }
             if ( off0==(uint64_t)-1 && idx->n_no_coor ) off0 = 0; // only no-coor reads in this bam
-			break;
-
-		case HTS_IDX_REST:
-			off0 = 0;
-			break;
-
-		case HTS_IDX_NONE:
-			finished0 = 1;
-			off0 = 0;
-			break;
-
-		default:
-			return 0;
-		}
-		if (off0 != (uint64_t)-1) {
-			iter = (hts_itr_t*)calloc(1, sizeof(hts_itr_t));
-			iter->read_rest = 1;
-			iter->finished = finished0;
-			iter->curr_off = off0;
-			iter->readrec = readrec;
-			return iter;
-		} else return 0;
-	}
-
-	if (beg < 0) beg = 0;
-	if (end < beg) return 0;
-	if ((bidx = idx->bidx[tid]) == 0) return 0;
-
-	iter = (hts_itr_t*)calloc(1, sizeof(hts_itr_t));
-	iter->tid = tid, iter->beg = beg, iter->end = end; iter->i = -1;
-	iter->readrec = readrec;
-
-	// compute min_off
-	bin = hts_bin_first(idx->n_lvls) + (beg>>idx->min_shift);
-	do {
-		int first;
-		k = kh_get(bin, bidx, bin);
-		if (k != kh_end(bidx)) break;
-		first = (hts_bin_parent(bin)<<3) + 1;
-		if (bin > first) --bin;
-		else bin = hts_bin_parent(bin);
-	} while (bin);
-	if (bin == 0) k = kh_get(bin, bidx, bin);
-	min_off = k != kh_end(bidx)? kh_val(bidx, k).loff : 0;
-	// retrieve bins
-	reg2bins(beg, end, iter, idx->min_shift, idx->n_lvls);
-	for (i = n_off = 0; i < iter->bins.n; ++i)
-		if ((k = kh_get(bin, bidx, iter->bins.a[i])) != kh_end(bidx))
-			n_off += kh_value(bidx, k).n;
-	if (n_off == 0) return iter;
-	off = (hts_pair64_t*)calloc(n_off, 16);
-	for (i = n_off = 0; i < iter->bins.n; ++i) {
-		if ((k = kh_get(bin, bidx, iter->bins.a[i])) != kh_end(bidx)) {
-			int j;
-			bins_t *p = &kh_value(bidx, k);
-			for (j = 0; j < p->n; ++j)
-				if (p->list[j].v > min_off) off[n_off++] = p->list[j];
-		}
-	}
-	if (n_off == 0) {
-		free(off); return iter;
-	}
-	ks_introsort(_off, n_off, off);
-	// resolve completely contained adjacent blocks
-	for (i = 1, l = 0; i < n_off; ++i)
-		if (off[l].v < off[i].v) off[++l] = off[i];
-	n_off = l + 1;
-	// resolve overlaps between adjacent blocks; this may happen due to the merge in indexing
-	for (i = 1; i < n_off; ++i)
-		if (off[i-1].v >= off[i].u) off[i-1].v = off[i].u;
-	// merge adjacent blocks
-	for (i = 1, l = 0; i < n_off; ++i) {
-		if (off[l].v>>16 == off[i].u>>16) off[l].v = off[i].v;
-		else off[++l] = off[i];
-	}
-	n_off = l + 1;
-	iter->n_off = n_off; iter->off = off;
-	return iter;
+            break;
+
+        case HTS_IDX_REST:
+            off0 = 0;
+            break;
+
+        case HTS_IDX_NONE:
+            finished0 = 1;
+            off0 = 0;
+            break;
+
+        default:
+            return 0;
+        }
+        if (off0 != (uint64_t)-1) {
+            iter = (hts_itr_t*)calloc(1, sizeof(hts_itr_t));
+            iter->read_rest = 1;
+            iter->finished = finished0;
+            iter->curr_off = off0;
+            iter->readrec = readrec;
+            return iter;
+        } else return 0;
+    }
+
+    if (beg < 0) beg = 0;
+    if (end < beg) return 0;
+    if ((bidx = idx->bidx[tid]) == 0) return 0;
+
+    iter = (hts_itr_t*)calloc(1, sizeof(hts_itr_t));
+    iter->tid = tid, iter->beg = beg, iter->end = end; iter->i = -1;
+    iter->readrec = readrec;
+
+    // compute min_off
+    bin = hts_bin_first(idx->n_lvls) + (beg>>idx->min_shift);
+    do {
+        int first;
+        k = kh_get(bin, bidx, bin);
+        if (k != kh_end(bidx)) break;
+        first = (hts_bin_parent(bin)<<3) + 1;
+        if (bin > first) --bin;
+        else bin = hts_bin_parent(bin);
+    } while (bin);
+    if (bin == 0) k = kh_get(bin, bidx, bin);
+    min_off = k != kh_end(bidx)? kh_val(bidx, k).loff : 0;
+    // retrieve bins
+    reg2bins(beg, end, iter, idx->min_shift, idx->n_lvls);
+    for (i = n_off = 0; i < iter->bins.n; ++i)
+        if ((k = kh_get(bin, bidx, iter->bins.a[i])) != kh_end(bidx))
+            n_off += kh_value(bidx, k).n;
+    if (n_off == 0) return iter;
+    off = (hts_pair64_t*)calloc(n_off, 16);
+    for (i = n_off = 0; i < iter->bins.n; ++i) {
+        if ((k = kh_get(bin, bidx, iter->bins.a[i])) != kh_end(bidx)) {
+            int j;
+            bins_t *p = &kh_value(bidx, k);
+            for (j = 0; j < p->n; ++j)
+                if (p->list[j].v > min_off) off[n_off++] = p->list[j];
+        }
+    }
+    if (n_off == 0) {
+        free(off); return iter;
+    }
+    ks_introsort(_off, n_off, off);
+    // resolve completely contained adjacent blocks
+    for (i = 1, l = 0; i < n_off; ++i)
+        if (off[l].v < off[i].v) off[++l] = off[i];
+    n_off = l + 1;
+    // resolve overlaps between adjacent blocks; this may happen due to the merge in indexing
+    for (i = 1; i < n_off; ++i)
+        if (off[i-1].v >= off[i].u) off[i-1].v = off[i].u;
+    // merge adjacent blocks
+    for (i = 1, l = 0; i < n_off; ++i) {
+        if (off[l].v>>16 == off[i].u>>16) off[l].v = off[i].v;
+        else off[++l] = off[i];
+    }
+    n_off = l + 1;
+    iter->n_off = n_off; iter->off = off;
+    return iter;
 }
 
 void hts_itr_destroy(hts_itr_t *iter)
 {
-	if (iter) { free(iter->off); free(iter->bins.a); free(iter); }
+    if (iter) { free(iter->off); free(iter->bins.a); free(iter); }
 }
 
 const char *hts_parse_reg(const char *s, int *beg, int *end)
 {
-	int i, k, l, name_end;
-	*beg = *end = -1;
-	name_end = l = strlen(s);
-	// determine the sequence name
-	for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
-	if (i >= 0) name_end = i;
-	if (name_end < l) { // check if this is really the end
-		int n_hyphen = 0;
-		for (i = name_end + 1; i < l; ++i) {
-			if (s[i] == '-') ++n_hyphen;
-			else if (!isdigit(s[i]) && s[i] != ',') break;
-		}
-		if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
-	}
-	// parse the interval
-	if (name_end < l) {
-		char *tmp;
-		tmp = (char*)alloca(l - name_end + 1);
-		for (i = name_end + 1, k = 0; i < l; ++i)
-			if (s[i] != ',') tmp[k++] = s[i];
-		tmp[k] = 0;
-		if ((*beg = strtol(tmp, &tmp, 10) - 1) < 0) *beg = 0;
-		*end = *tmp? strtol(tmp + 1, &tmp, 10) : 1<<29;
-		if (*beg > *end) name_end = l;
-	}
-	if (name_end == l) *beg = 0, *end = 1<<29;
-	return s + name_end;
+    int i, k, l, name_end;
+    *beg = *end = -1;
+    name_end = l = strlen(s);
+    // determine the sequence name
+    for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
+    if (i >= 0) name_end = i;
+    if (name_end < l) { // check if this is really the end
+        int n_hyphen = 0;
+        for (i = name_end + 1; i < l; ++i) {
+            if (s[i] == '-') ++n_hyphen;
+            else if (!isdigit(s[i]) && s[i] != ',') break;
+        }
+        if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
+    }
+    // parse the interval
+    if (name_end < l) {
+        char *tmp;
+        tmp = (char*)alloca(l - name_end + 1);
+        for (i = name_end + 1, k = 0; i < l; ++i)
+            if (s[i] != ',') tmp[k++] = s[i];
+        tmp[k] = 0;
+        if ((*beg = strtol(tmp, &tmp, 10) - 1) < 0) *beg = 0;
+        *end = *tmp? strtol(tmp + 1, &tmp, 10) : 1<<29;
+        if (*beg > *end) name_end = l;
+    }
+    if (name_end == l) *beg = 0, *end = 1<<29;
+    return s + name_end;
 }
 
 hts_itr_t *hts_itr_querys(const hts_idx_t *idx, const char *reg, hts_name2id_f getid, void *hdr, hts_itr_query_func *itr_query, hts_readrec_func *readrec)
 {
-	int tid, beg, end;
-	char *q, *tmp;
-	if (strcmp(reg, ".") == 0)
-		return itr_query(idx, HTS_IDX_START, 0, 1<<29, readrec);
-	else if (strcmp(reg, "*") != 0) {
-		q = (char*)hts_parse_reg(reg, &beg, &end);
-		tmp = (char*)alloca(q - reg + 1);
-		strncpy(tmp, reg, q - reg);
-		tmp[q - reg] = 0;
-		if ((tid = getid(hdr, tmp)) < 0)
-			tid = getid(hdr, reg);
-		if (tid < 0) return 0;
-		return itr_query(idx, tid, beg, end, readrec);
-	} else return itr_query(idx, HTS_IDX_NOCOOR, 0, 0, readrec);
+    int tid, beg, end;
+    char *q, *tmp;
+    if (strcmp(reg, ".") == 0)
+        return itr_query(idx, HTS_IDX_START, 0, 1<<29, readrec);
+    else if (strcmp(reg, "*") != 0) {
+        q = (char*)hts_parse_reg(reg, &beg, &end);
+        tmp = (char*)alloca(q - reg + 1);
+        strncpy(tmp, reg, q - reg);
+        tmp[q - reg] = 0;
+        if ((tid = getid(hdr, tmp)) < 0)
+            tid = getid(hdr, reg);
+        if (tid < 0) return 0;
+        return itr_query(idx, tid, beg, end, readrec);
+    } else return itr_query(idx, HTS_IDX_NOCOOR, 0, 0, readrec);
 }
 
 int hts_itr_next(BGZF *fp, hts_itr_t *iter, void *r, void *data)
 {
-	int ret, tid, beg, end;
-	if (iter == NULL || iter->finished) return -1;
-	if (iter->read_rest) {
-		if (iter->curr_off) { // seek to the start
-			bgzf_seek(fp, iter->curr_off, SEEK_SET);
-			iter->curr_off = 0; // only seek once
-		}
-		ret = iter->readrec(fp, data, r, &tid, &beg, &end);
-		if (ret < 0) iter->finished = 1;
-		return ret;
-	}
-	if (iter->off == 0) return -1;
-	for (;;) {
-		if (iter->curr_off == 0 || iter->curr_off >= iter->off[iter->i].v) { // then jump to the next chunk
-			if (iter->i == iter->n_off - 1) { ret = -1; break; } // no more chunks
-			if (iter->i < 0 || iter->off[iter->i].v != iter->off[iter->i+1].u) { // not adjacent chunks; then seek
-				bgzf_seek(fp, iter->off[iter->i+1].u, SEEK_SET);
-				iter->curr_off = bgzf_tell(fp);
-			}
-			++iter->i;
-		}
-		if ((ret = iter->readrec(fp, data, r, &tid, &beg, &end)) >= 0) {
-			iter->curr_off = bgzf_tell(fp);
-			if (tid != iter->tid || beg >= iter->end) { // no need to proceed
-				ret = -1; break;
-			} else if (end > iter->beg && iter->end > beg) return ret;
-		} else break; // end of file or error
-	}
-	iter->finished = 1;
-	return ret;
+    int ret, tid, beg, end;
+    if (iter == NULL || iter->finished) return -1;
+    if (iter->read_rest) {
+        if (iter->curr_off) { // seek to the start
+            bgzf_seek(fp, iter->curr_off, SEEK_SET);
+            iter->curr_off = 0; // only seek once
+        }
+        ret = iter->readrec(fp, data, r, &tid, &beg, &end);
+        if (ret < 0) iter->finished = 1;
+        return ret;
+    }
+    if (iter->off == 0) return -1;
+    for (;;) {
+        if (iter->curr_off == 0 || iter->curr_off >= iter->off[iter->i].v) { // then jump to the next chunk
+            if (iter->i == iter->n_off - 1) { ret = -1; break; } // no more chunks
+            if (iter->i < 0 || iter->off[iter->i].v != iter->off[iter->i+1].u) { // not adjacent chunks; then seek
+                bgzf_seek(fp, iter->off[iter->i+1].u, SEEK_SET);
+                iter->curr_off = bgzf_tell(fp);
+            }
+            ++iter->i;
+        }
+        if ((ret = iter->readrec(fp, data, r, &tid, &beg, &end)) >= 0) {
+            iter->curr_off = bgzf_tell(fp);
+            if (tid != iter->tid || beg >= iter->end) { // no need to proceed
+                ret = -1; break;
+            } else if (end > iter->beg && iter->end > beg) return ret;
+        } else break; // end of file or error
+    }
+    iter->finished = 1;
+    return ret;
 }
 
 /**********************
@@ -1248,70 +1278,70 @@ int hts_itr_next(BGZF *fp, hts_itr_t *iter, void *r, void *data)
 
 static char *test_and_fetch(const char *fn)
 {
-	FILE *fp;
-	// FIXME Use is_remote_scheme() helper that's true for ftp/http/irods/etc
-	if (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn) {
-		const int buf_size = 1 * 1024 * 1024;
-		hFILE *fp_remote;
-		uint8_t *buf;
-		int l;
-		const char *p;
-		for (p = fn + strlen(fn) - 1; p >= fn; --p)
-			if (*p == '/') break;
-		++p; // p now points to the local file name
-		if ((fp_remote = hopen(fn, "r")) == 0) {
-			if (hts_verbose >= 1) fprintf(stderr, "[E::%s] fail to open remote file '%s'\n", __func__, fn);
-			return 0;
-		}
-		if ((fp = fopen(p, "w")) == 0) {
-			if (hts_verbose >= 1) fprintf(stderr, "[E::%s] fail to create file '%s' in the working directory\n", __func__, p);
-			hclose_abruptly(fp_remote);
-			return 0;
-		}
-		if (hts_verbose >= 3) fprintf(stderr, "[M::%s] downloading file '%s' to local directory\n", __func__, fn);
-		buf = (uint8_t*)calloc(buf_size, 1);
-		while ((l = hread(fp_remote, buf, buf_size)) > 0) fwrite(buf, 1, l, fp);
-		free(buf);
-		fclose(fp);
-		if (hclose(fp_remote) != 0) fprintf(stderr, "[E::%s] fail to close remote file '%s'\n", __func__, fn);
-		return (char*)p;
-	} else {
-		if ((fp = fopen(fn, "rb")) == 0) return 0;
-		fclose(fp);
-		return (char*)fn;
-	}
+    FILE *fp;
+    // FIXME Use is_remote_scheme() helper that's true for ftp/http/irods/etc
+    if (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn) {
+        const int buf_size = 1 * 1024 * 1024;
+        hFILE *fp_remote;
+        uint8_t *buf;
+        int l;
+        const char *p;
+        for (p = fn + strlen(fn) - 1; p >= fn; --p)
+            if (*p == '/') break;
+        ++p; // p now points to the local file name
+        if ((fp_remote = hopen(fn, "r")) == 0) {
+            if (hts_verbose >= 1) fprintf(stderr, "[E::%s] fail to open remote file '%s'\n", __func__, fn);
+            return 0;
+        }
+        if ((fp = fopen(p, "w")) == 0) {
+            if (hts_verbose >= 1) fprintf(stderr, "[E::%s] fail to create file '%s' in the working directory\n", __func__, p);
+            hclose_abruptly(fp_remote);
+            return 0;
+        }
+        if (hts_verbose >= 3) fprintf(stderr, "[M::%s] downloading file '%s' to local directory\n", __func__, fn);
+        buf = (uint8_t*)calloc(buf_size, 1);
+        while ((l = hread(fp_remote, buf, buf_size)) > 0) fwrite(buf, 1, l, fp);
+        free(buf);
+        fclose(fp);
+        if (hclose(fp_remote) != 0) fprintf(stderr, "[E::%s] fail to close remote file '%s'\n", __func__, fn);
+        return (char*)p;
+    } else {
+        if ((fp = fopen(fn, "rb")) == 0) return 0;
+        fclose(fp);
+        return (char*)fn;
+    }
 }
 
 char *hts_idx_getfn(const char *fn, const char *ext)
 {
-	int i, l_fn, l_ext;
-	char *fnidx, *ret;
-	l_fn = strlen(fn); l_ext = strlen(ext);
-	fnidx = (char*)calloc(l_fn + l_ext + 1, 1);
-	strcpy(fnidx, fn); strcpy(fnidx + l_fn, ext);
-	if ((ret = test_and_fetch(fnidx)) == 0) {
-		for (i = l_fn - 1; i > 0; --i)
-			if (fnidx[i] == '.') break;
-		strcpy(fnidx + i, ext);
-		ret = test_and_fetch(fnidx);
-	}
-	if (ret == 0) {
-		free(fnidx);
-		return 0;
-	}
-	l_fn = strlen(ret);
-	memmove(fnidx, ret, l_fn + 1);
-	return fnidx;
+    int i, l_fn, l_ext;
+    char *fnidx, *ret;
+    l_fn = strlen(fn); l_ext = strlen(ext);
+    fnidx = (char*)calloc(l_fn + l_ext + 1, 1);
+    strcpy(fnidx, fn); strcpy(fnidx + l_fn, ext);
+    if ((ret = test_and_fetch(fnidx)) == 0) {
+        for (i = l_fn - 1; i > 0; --i)
+            if (fnidx[i] == '.') break;
+        strcpy(fnidx + i, ext);
+        ret = test_and_fetch(fnidx);
+    }
+    if (ret == 0) {
+        free(fnidx);
+        return 0;
+    }
+    l_fn = strlen(ret);
+    memmove(fnidx, ret, l_fn + 1);
+    return fnidx;
 }
 
 hts_idx_t *hts_idx_load(const char *fn, int fmt)
 {
-	char *fnidx;
-	hts_idx_t *idx;
-	fnidx = hts_idx_getfn(fn, ".csi");
-	if (fnidx) fmt = HTS_FMT_CSI;
-	else fnidx = hts_idx_getfn(fn, fmt == HTS_FMT_BAI? ".bai" : ".tbi");
-	if (fnidx == 0) return 0;
+    char *fnidx;
+    hts_idx_t *idx;
+    fnidx = hts_idx_getfn(fn, ".csi");
+    if (fnidx) fmt = HTS_FMT_CSI;
+    else fnidx = hts_idx_getfn(fn, fmt == HTS_FMT_BAI? ".bai" : ".tbi");
+    if (fnidx == 0) return 0;
 
     // Check that the index file is up to date, the main file might have changed
     struct stat stat_idx,stat_main;
@@ -1320,7 +1350,7 @@ hts_idx_t *hts_idx_load(const char *fn, int fmt)
         if ( stat_idx.st_mtime < stat_main.st_mtime )
             fprintf(stderr, "Warning: The index file is older than the data file: %s\n", fnidx);
     }
-	idx = hts_idx_load_local(fnidx, fmt);
-	free(fnidx);
-	return idx;
+    idx = hts_idx_load_local(fnidx, fmt);
+    free(fnidx);
+    return idx;
 }
diff --git a/htslib/htslib/__init__.py b/htslib/htslib/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/htslib/htslib/bgzf.h b/htslib/htslib/bgzf.h
index 1edce41..31b8d5e 100644
--- a/htslib/htslib/bgzf.h
+++ b/htslib/htslib/bgzf.h
@@ -2,6 +2,7 @@
 
    Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
                  2011, 2012 Attractive Chaos <attractor at live.co.uk>
+   Copyright (C) 2009, 2013, 2014 Genome Research Ltd
 
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
@@ -25,8 +26,8 @@
 /* The BGZF library was originally written by Bob Handsaker from the Broad
  * Institute. It was later improved by the SAMtools developers. */
 
-#ifndef __BGZF_H
-#define __BGZF_H
+#ifndef HTSLIB_BGZF_H
+#define HTSLIB_BGZF_H
 
 #include <stdint.h>
 #include <stdio.h>
@@ -46,12 +47,12 @@ struct bgzf_mtaux_t;
 typedef struct __bgzidx_t bgzidx_t;
 
 struct BGZF {
-	int errcode:16, is_write:2, is_be:2, compress_level:9, is_compressed:2, is_gzip:1;
-	int cache_size;
+    int errcode:16, is_write:2, is_be:2, compress_level:9, is_compressed:2, is_gzip:1;
+    int cache_size;
     int block_length, block_offset;
     int64_t block_address, uncompressed_address;
     void *uncompressed_block, *compressed_block;
-	void *cache; // a pointer to a hash table
+    void *cache; // a pointer to a hash table
     struct hFILE *fp; // actual file handle
     struct bgzf_mtaux_t *mt; // only used for multi-threading
     bgzidx_t *idx;      // BGZF index
@@ -66,8 +67,8 @@ typedef struct BGZF BGZF;
 #ifndef KSTRING_T
 #define KSTRING_T kstring_t
 typedef struct __kstring_t {
-	size_t l, m;
-	char *s;
+    size_t l, m;
+    char *s;
 } kstring_t;
 #endif
 
@@ -75,235 +76,235 @@ typedef struct __kstring_t {
 extern "C" {
 #endif
 
-	/******************
-	 * Basic routines *
-	 ******************/
-
-	/**
-	 * Open an existing file descriptor for reading or writing.
-	 *
-	 * @param fd    file descriptor
-	 * @param mode  mode matching /[rwa][u0-9]+/: 'r' for reading, 'w' for
-	 *              writing, or 'a' for appending, while a digit specifies
-	 *              the zlib compression level.
+    /******************
+     * Basic routines *
+     ******************/
+
+    /**
+     * Open an existing file descriptor for reading or writing.
+     *
+     * @param fd    file descriptor
+     * @param mode  mode matching /[rwa][u0-9]+/: 'r' for reading, 'w' for
+     *              writing, or 'a' for appending, while a digit specifies
+     *              the zlib compression level.
      *              Note that there is a distinction between 'u' and '0': the
      *              first yields plain uncompressed output whereas the latter
      *              outputs uncompressed data wrapped in the zlib format.
-	 * @return      BGZF file handler; 0 on error
-	 */
-	BGZF* bgzf_dopen(int fd, const char *mode);
-
-	#define bgzf_fdopen(fd, mode) bgzf_dopen((fd), (mode)) // for backward compatibility
-
-	/**
-	 * Open the specified file for reading or writing.
-	 */
-	BGZF* bgzf_open(const char* path, const char *mode);
-
-	/**
-	 * Open an existing hFILE stream for reading or writing.
-	 */
-	BGZF* bgzf_hopen(struct hFILE *fp, const char *mode);
-
-	/**
-	 * Close the BGZF and free all associated resources.
-	 *
-	 * @param fp    BGZF file handler
-	 * @return      0 on success and -1 on error
-	 */
-	int bgzf_close(BGZF *fp);
-
-	/**
-	 * Read up to _length_ bytes from the file storing into _data_.
-	 *
-	 * @param fp     BGZF file handler
-	 * @param data   data array to read into
-	 * @param length size of data to read
-	 * @return       number of bytes actually read; 0 on end-of-file and -1 on error
-	 */
-	ssize_t bgzf_read(BGZF *fp, void *data, size_t length);
-
-	/**
-	 * Write _length_ bytes from _data_ to the file.  If no I/O errors occur,
-	 * the complete _length_ bytes will be written (or queued for writing).
-	 *
-	 * @param fp     BGZF file handler
-	 * @param data   data array to write
-	 * @param length size of data to write
-	 * @return       number of bytes written (i.e., _length_); negative on error
-	 */
-	ssize_t bgzf_write(BGZF *fp, const void *data, size_t length);
-
-	/**
-	 * Read up to _length_ bytes directly from the underlying stream without
-	 * decompressing.  Bypasses BGZF blocking, so must be used with care in
-	 * specialised circumstances only.
-	 *
-	 * @param fp     BGZF file handler
-	 * @param data   data array to read into
-	 * @param length number of raw bytes to read
-	 * @return       number of bytes actually read; 0 on end-of-file and -1 on error
-	 */
-	ssize_t bgzf_raw_read(BGZF *fp, void *data, size_t length);
-
-	/**
-	 * Write _length_ bytes directly to the underlying stream without
-	 * compressing.  Bypasses BGZF blocking, so must be used with care
-	 * in specialised circumstances only.
-	 *
-	 * @param fp     BGZF file handler
-	 * @param data   data array to write
-	 * @param length number of raw bytes to write
-	 * @return       number of bytes actually written; -1 on error
-	 */
-	ssize_t bgzf_raw_write(BGZF *fp, const void *data, size_t length);
-
-	/**
-	 * Write the data in the buffer to the file.
-	 */
-	int bgzf_flush(BGZF *fp);
-
-	/**
-	 * Return a virtual file pointer to the current location in the file.
-	 * No interpetation of the value should be made, other than a subsequent
-	 * call to bgzf_seek can be used to position the file at the same point.
-	 * Return value is non-negative on success.
-	 */
-	#define bgzf_tell(fp) (((fp)->block_address << 16) | ((fp)->block_offset & 0xFFFF))
-
-	/**
-	 * Set the file to read from the location specified by _pos_.
-	 *
-	 * @param fp     BGZF file handler
-	 * @param pos    virtual file offset returned by bgzf_tell()
-	 * @param whence must be SEEK_SET
-	 * @return       0 on success and -1 on error
-	 */
-	int64_t bgzf_seek(BGZF *fp, int64_t pos, int whence);
-
-	/**
-	 * Check if the BGZF end-of-file (EOF) marker is present
-	 *
-	 * @param fp    BGZF file handler opened for reading
-	 * @return      1 if the EOF marker is present and correct;
-	 *              2 if it can't be checked, e.g., because fp isn't seekable;
-	 *              0 if the EOF marker is absent;
-	 *              -1 (with errno set) on error
-	 */
-	int bgzf_check_EOF(BGZF *fp);
-
-	/**
-	 * Check if a file is in the BGZF format
-	 *
-	 * @param fn    file name
-	 * @return      1 if _fn_ is BGZF; 0 if not or on I/O error
-	 */
-	 int bgzf_is_bgzf(const char *fn);
-
-	/*********************
-	 * Advanced routines *
-	 *********************/
-
-	/**
-	 * Set the cache size. Only effective when compiled with -DBGZF_CACHE.
-	 *
-	 * @param fp    BGZF file handler
-	 * @param size  size of cache in bytes; 0 to disable caching (default)
-	 */
-	void bgzf_set_cache_size(BGZF *fp, int size);
-
-	/**
-	 * Flush the file if the remaining buffer size is smaller than _size_ 
-	 * @return      0 if flushing succeeded or was not needed; negative on error
-	 */
-	int bgzf_flush_try(BGZF *fp, ssize_t size);
-
-	/**
-	 * Read one byte from a BGZF file. It is faster than bgzf_read()
-	 * @param fp     BGZF file handler
-	 * @return       byte read; -1 on end-of-file or error
-	 */
-	int bgzf_getc(BGZF *fp);
-
-	/**
-	 * Read one line from a BGZF file. It is faster than bgzf_getc()
-	 *
-	 * @param fp     BGZF file handler
-	 * @param delim  delimitor
-	 * @param str    string to write to; must be initialized
-	 * @return       length of the string; 0 on end-of-file; negative on error
-	 */
-	int bgzf_getline(BGZF *fp, int delim, kstring_t *str);
-
-	/**
-	 * Read the next BGZF block.
-	 */
-	int bgzf_read_block(BGZF *fp);
-
-	/**
-	 * Enable multi-threading (only effective on writing and when the
-	 * library was compiled with -DBGZF_MT)
-	 *
-	 * @param fp          BGZF file handler; must be opened for writing
-	 * @param n_threads   #threads used for writing
-	 * @param n_sub_blks  #blocks processed by each thread; a value 64-256 is recommended
-	 */
-	int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks);
-
-
-	/*******************
-	 * bgzidx routines *
-	 *******************/
-
-	/**
-     *  Position BGZF at the uncompressed offset 
+     * @return      BGZF file handler; 0 on error
+     */
+    BGZF* bgzf_dopen(int fd, const char *mode);
+
+    #define bgzf_fdopen(fd, mode) bgzf_dopen((fd), (mode)) // for backward compatibility
+
+    /**
+     * Open the specified file for reading or writing.
+     */
+    BGZF* bgzf_open(const char* path, const char *mode);
+
+    /**
+     * Open an existing hFILE stream for reading or writing.
+     */
+    BGZF* bgzf_hopen(struct hFILE *fp, const char *mode);
+
+    /**
+     * Close the BGZF and free all associated resources.
+     *
+     * @param fp    BGZF file handler
+     * @return      0 on success and -1 on error
+     */
+    int bgzf_close(BGZF *fp);
+
+    /**
+     * Read up to _length_ bytes from the file storing into _data_.
+     *
+     * @param fp     BGZF file handler
+     * @param data   data array to read into
+     * @param length size of data to read
+     * @return       number of bytes actually read; 0 on end-of-file and -1 on error
+     */
+    ssize_t bgzf_read(BGZF *fp, void *data, size_t length);
+
+    /**
+     * Write _length_ bytes from _data_ to the file.  If no I/O errors occur,
+     * the complete _length_ bytes will be written (or queued for writing).
+     *
+     * @param fp     BGZF file handler
+     * @param data   data array to write
+     * @param length size of data to write
+     * @return       number of bytes written (i.e., _length_); negative on error
+     */
+    ssize_t bgzf_write(BGZF *fp, const void *data, size_t length);
+
+    /**
+     * Read up to _length_ bytes directly from the underlying stream without
+     * decompressing.  Bypasses BGZF blocking, so must be used with care in
+     * specialised circumstances only.
+     *
+     * @param fp     BGZF file handler
+     * @param data   data array to read into
+     * @param length number of raw bytes to read
+     * @return       number of bytes actually read; 0 on end-of-file and -1 on error
+     */
+    ssize_t bgzf_raw_read(BGZF *fp, void *data, size_t length);
+
+    /**
+     * Write _length_ bytes directly to the underlying stream without
+     * compressing.  Bypasses BGZF blocking, so must be used with care
+     * in specialised circumstances only.
+     *
+     * @param fp     BGZF file handler
+     * @param data   data array to write
+     * @param length number of raw bytes to write
+     * @return       number of bytes actually written; -1 on error
+     */
+    ssize_t bgzf_raw_write(BGZF *fp, const void *data, size_t length);
+
+    /**
+     * Write the data in the buffer to the file.
+     */
+    int bgzf_flush(BGZF *fp);
+
+    /**
+     * Return a virtual file pointer to the current location in the file.
+     * No interpetation of the value should be made, other than a subsequent
+     * call to bgzf_seek can be used to position the file at the same point.
+     * Return value is non-negative on success.
+     */
+    #define bgzf_tell(fp) (((fp)->block_address << 16) | ((fp)->block_offset & 0xFFFF))
+
+    /**
+     * Set the file to read from the location specified by _pos_.
+     *
+     * @param fp     BGZF file handler
+     * @param pos    virtual file offset returned by bgzf_tell()
+     * @param whence must be SEEK_SET
+     * @return       0 on success and -1 on error
+     */
+    int64_t bgzf_seek(BGZF *fp, int64_t pos, int whence);
+
+    /**
+     * Check if the BGZF end-of-file (EOF) marker is present
+     *
+     * @param fp    BGZF file handler opened for reading
+     * @return      1 if the EOF marker is present and correct;
+     *              2 if it can't be checked, e.g., because fp isn't seekable;
+     *              0 if the EOF marker is absent;
+     *              -1 (with errno set) on error
+     */
+    int bgzf_check_EOF(BGZF *fp);
+
+    /**
+     * Check if a file is in the BGZF format
+     *
+     * @param fn    file name
+     * @return      1 if _fn_ is BGZF; 0 if not or on I/O error
+     */
+     int bgzf_is_bgzf(const char *fn);
+
+    /*********************
+     * Advanced routines *
+     *********************/
+
+    /**
+     * Set the cache size. Only effective when compiled with -DBGZF_CACHE.
+     *
+     * @param fp    BGZF file handler
+     * @param size  size of cache in bytes; 0 to disable caching (default)
+     */
+    void bgzf_set_cache_size(BGZF *fp, int size);
+
+    /**
+     * Flush the file if the remaining buffer size is smaller than _size_
+     * @return      0 if flushing succeeded or was not needed; negative on error
+     */
+    int bgzf_flush_try(BGZF *fp, ssize_t size);
+
+    /**
+     * Read one byte from a BGZF file. It is faster than bgzf_read()
+     * @param fp     BGZF file handler
+     * @return       byte read; -1 on end-of-file or error
+     */
+    int bgzf_getc(BGZF *fp);
+
+    /**
+     * Read one line from a BGZF file. It is faster than bgzf_getc()
+     *
+     * @param fp     BGZF file handler
+     * @param delim  delimitor
+     * @param str    string to write to; must be initialized
+     * @return       length of the string; 0 on end-of-file; negative on error
+     */
+    int bgzf_getline(BGZF *fp, int delim, kstring_t *str);
+
+    /**
+     * Read the next BGZF block.
+     */
+    int bgzf_read_block(BGZF *fp);
+
+    /**
+     * Enable multi-threading (only effective on writing and when the
+     * library was compiled with -DBGZF_MT)
+     *
+     * @param fp          BGZF file handler; must be opened for writing
+     * @param n_threads   #threads used for writing
+     * @param n_sub_blks  #blocks processed by each thread; a value 64-256 is recommended
+     */
+    int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks);
+
+
+    /*******************
+     * bgzidx routines *
+     *******************/
+
+    /**
+     *  Position BGZF at the uncompressed offset
      *
      *  @param fp           BGZF file handler; must be opened for reading
      *  @param uoffset      file offset in the uncompressed data
      *  @param where        SEEK_SET supported atm
      *
      *  Returns 0 on success and -1 on error.
-	 */
+     */
     int bgzf_useek(BGZF *fp, long uoffset, int where);
 
-	/**
+    /**
      *  Position in uncompressed BGZF
      *
      *  @param fp           BGZF file handler; must be opened for reading
      *
      *  Returns the current offset on success and -1 on error.
-	 */
+     */
     long bgzf_utell(BGZF *fp);
 
-	/**
-	 * Tell BGZF to build index while compressing.
+    /**
+     * Tell BGZF to build index while compressing.
      *
-	 * @param fp          BGZF file handler; can be opened for reading or writing.
+     * @param fp          BGZF file handler; can be opened for reading or writing.
      *
      * Returns 0 on success and -1 on error.
-	 */
-	int bgzf_index_build_init(BGZF *fp);
-
-   	/**
-	 * Load BGZF index
-	 *
-	 * @param fp          BGZF file handler
-	 * @param bname       base name
+     */
+    int bgzf_index_build_init(BGZF *fp);
+
+    /**
+     * Load BGZF index
+     *
+     * @param fp          BGZF file handler
+     * @param bname       base name
      * @param suffix      suffix to add to bname (can be NULL)
      *
      * Returns 0 on success and -1 on error.
-	 */
+     */
     int bgzf_index_load(BGZF *fp, const char *bname, const char *suffix);
 
-   	/**
-	 * Save BGZF index
-	 *
-	 * @param fp          BGZF file handler
-	 * @param bname       base name
+    /**
+     * Save BGZF index
+     *
+     * @param fp          BGZF file handler
+     * @param bname       base name
      * @param suffix      suffix to add to bname (can be NULL)
      *
      * Returns 0 on success and -1 on error.
-	 */
+     */
     int bgzf_index_dump(BGZF *fp, const char *bname, const char *suffix);
 
 #ifdef __cplusplus
diff --git a/htslib/htslib/faidx.h b/htslib/htslib/faidx.h
index 3153b98..24a30e2 100644
--- a/htslib/htslib/faidx.h
+++ b/htslib/htslib/faidx.h
@@ -1,6 +1,8 @@
-/* The MIT License
+/* faidx.h -- FASTA random access.
 
-   Copyright (c) 2008 Genome Research Ltd (GRL).
+   Copyright (C) 2008, 2009, 2013, 2014 Genome Research Ltd.
+
+   Author: Heng Li <lh3 at sanger.ac.uk>
 
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
@@ -23,10 +25,8 @@
    SOFTWARE.
 */
 
-/* Contact: Heng Li <lh3 at sanger.ac.uk>, Petr Danecek <pd3 at sanger> */
-
-#ifndef FAIDX_H
-#define FAIDX_H
+#ifndef HTSLIB_FAIDX_H
+#define HTSLIB_FAIDX_H
 
 /*!
   @header
@@ -52,58 +52,81 @@ typedef struct __faidx_t faidx_t;
 extern "C" {
 #endif
 
-	/*!
-	  @abstract   Build index for a FASTA or bgzip-compressed FASTA file.
-	  @param  fn  FASTA file name
-	  @return     0 on success; or -1 on failure
-	  @discussion File "fn.fai" will be generated.
-	 */
-	int fai_build(const char *fn);
-
-	/*!
-	  @abstract    Distroy a faidx_t struct.
-	  @param  fai  Pointer to the struct to be destroyed
-	 */
-	void fai_destroy(faidx_t *fai);
-
-	/*!
-	  @abstract   Load index from "fn.fai".
-	  @param  fn  File name of the FASTA file
-	 */
-	faidx_t *fai_load(const char *fn);
-
-	/*!
-	  @abstract    Fetch the sequence in a region.
-	  @param  fai  Pointer to the faidx_t struct
-	  @param  reg  Region in the format "chr2:20,000-30,000"
-	  @param  len  Length of the region; -2 if seq not present, -1 general error
-	  @return      Pointer to the sequence; null on failure
-
-	  @discussion The returned sequence is allocated by malloc family
-	  and should be destroyed by end users by calling free() on it.
-	 */
-	char *fai_fetch(const faidx_t *fai, const char *reg, int *len);
-
-	/*!
-	  @abstract	   Fetch the number of sequences. 
-	  @param  fai  Pointer to the faidx_t struct
-	  @return	   The number of sequences
-	 */
-	int faidx_fetch_nseq(const faidx_t *fai);
-
-	/*!
-	  @abstract    Fetch the sequence in a region.
-	  @param  fai  Pointer to the faidx_t struct
-	  @param  c_name Region name
-	  @param  p_beg_i  Beginning position number (zero-based)
-	  @param  p_end_i  End position number (zero-based)
-	  @param  len  Length of the region; -2 if c_name not present, -1 general error
-	  @return      Pointer to the sequence; null on failure
-
-	  @discussion The returned sequence is allocated by malloc family
-	  and should be destroyed by end users by calling free() on it.
-	 */
-	char *faidx_fetch_seq(const faidx_t *fai, const char *c_name, int p_beg_i, int p_end_i, int *len);
+    /*!
+      @abstract   Build index for a FASTA or bgzip-compressed FASTA file.
+      @param  fn  FASTA file name
+      @return     0 on success; or -1 on failure
+      @discussion File "fn.fai" will be generated.
+     */
+    int fai_build(const char *fn);
+
+    /*!
+      @abstract    Distroy a faidx_t struct.
+      @param  fai  Pointer to the struct to be destroyed
+     */
+    void fai_destroy(faidx_t *fai);
+
+    /*!
+      @abstract   Load index from "fn.fai".
+      @param  fn  File name of the FASTA file
+     */
+    faidx_t *fai_load(const char *fn);
+
+    /*!
+      @abstract    Fetch the sequence in a region.
+      @param  fai  Pointer to the faidx_t struct
+      @param  reg  Region in the format "chr2:20,000-30,000"
+      @param  len  Length of the region; -2 if seq not present, -1 general error
+      @return      Pointer to the sequence; null on failure
+
+      @discussion The returned sequence is allocated by malloc family
+      and should be destroyed by end users by calling free() on it.
+     */
+    char *fai_fetch(const faidx_t *fai, const char *reg, int *len);
+
+    /*!
+      @abstract    Fetch the number of sequences.
+      @param  fai  Pointer to the faidx_t struct
+      @return      The number of sequences
+     */
+    int faidx_fetch_nseq(const faidx_t *fai);
+
+    /*!
+      @abstract    Fetch the sequence in a region.
+      @param  fai  Pointer to the faidx_t struct
+      @param  c_name Region name
+      @param  p_beg_i  Beginning position number (zero-based)
+      @param  p_end_i  End position number (zero-based)
+      @param  len  Length of the region; -2 if c_name not present, -1 general error
+      @return      Pointer to the sequence; null on failure
+
+      @discussion The returned sequence is allocated by malloc family
+      and should be destroyed by end users by calling free() on it.
+     */
+    char *faidx_fetch_seq(const faidx_t *fai, const char *c_name, int p_beg_i, int p_end_i, int *len);
+
+    /*!
+      @abstract    Query if sequence is present
+      @param  fai  Pointer to the faidx_t struct
+      @param  seq  Sequence name
+      @return      1 if present or 0 if absent
+     */
+    int faidx_has_seq(const faidx_t *fai, const char *seq);
+
+    /*!
+      @abstract    Return number of sequences in fai index
+     */
+    int faidx_nseq(const faidx_t *fai);
+
+    /*!
+      @abstract    Return name of i-th sequence
+     */
+    const char *faidx_iseq(const faidx_t *fai, int i);
+
+    /*!
+      @abstract    Return sequence length, -1 if not present
+     */
+    int faidx_seq_len(const faidx_t *fai, const char *seq);
 
 #ifdef __cplusplus
 }
diff --git a/htslib/htslib/hfile.h b/htslib/htslib/hfile.h
index 68175e7..1b1a8a9 100644
--- a/htslib/htslib/hfile.h
+++ b/htslib/htslib/hfile.h
@@ -11,7 +11,7 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
-The above copyright notices and this permission notice shall be included in
+The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
diff --git a/htslib/htslib/hts.h b/htslib/htslib/hts.h
index d6fca3b..d020751 100644
--- a/htslib/htslib/hts.h
+++ b/htslib/htslib/hts.h
@@ -1,5 +1,30 @@
-#ifndef HTS_H
-#define HTS_H
+/*  hts.h -- format-neutral I/O, indexing, and iterator API functions.
+
+    Copyright (C) 2012-2014 Genome Research Ltd.
+    Copyright (C) 2012 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#ifndef HTSLIB_HTS_H
+#define HTSLIB_HTS_H
 
 #include <stddef.h>
 #include <stdint.h>
@@ -14,17 +39,11 @@ struct hFILE;
 #ifndef KSTRING_T
 #define KSTRING_T kstring_t
 typedef struct __kstring_t {
-	size_t l, m;
-	char *s;
+    size_t l, m;
+    char *s;
 } kstring_t;
 #endif
 
-#if defined(__GNUC__)
-# define val_unused __attribute__((unused))
-#else
-# define val_unused
-#endif
-
 #ifndef kroundup32
 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
 #endif
@@ -37,30 +56,30 @@ typedef struct __kstring_t {
  * @param m     size of memory allocated
  */
 #define hts_expand(type_t, n, m, ptr) if ((n) > (m)) { \
-		(m) = (n); kroundup32(m); \
-		(ptr) = (type_t*)realloc((ptr), (m) * sizeof(type_t)); \
-	}
+        (m) = (n); kroundup32(m); \
+        (ptr) = (type_t*)realloc((ptr), (m) * sizeof(type_t)); \
+    }
 #define hts_expand0(type_t, n, m, ptr) if ((n) > (m)) { \
-		int t = (m); (m) = (n); kroundup32(m); \
-		(ptr) = (type_t*)realloc((ptr), (m) * sizeof(type_t)); \
+        int t = (m); (m) = (n); kroundup32(m); \
+        (ptr) = (type_t*)realloc((ptr), (m) * sizeof(type_t)); \
         memset(((type_t*)ptr)+t,0,sizeof(type_t)*((m)-t)); \
-	}
+    }
 
 /************
  * File I/O *
  ************/
 
 typedef struct {
-	uint32_t is_bin:1, is_write:1, is_be:1, is_cram:1, is_compressed:2, is_kstream:1, dummy:25;
-	int64_t lineno;
-	kstring_t line;
-	char *fn, *fn_aux;
-	union {
-		BGZF *bgzf;
-		struct cram_fd *cram;
-		struct hFILE *hfile;
-		void *voidp;
-	} fp;
+    uint32_t is_bin:1, is_write:1, is_be:1, is_cram:1, is_compressed:2, is_kstream:1, dummy:25;
+    int64_t lineno;
+    kstring_t line;
+    char *fn, *fn_aux;
+    union {
+        BGZF *bgzf;
+        struct cram_fd *cram;
+        struct hFILE *hfile;
+        void *voidp;
+    } fp;
 } htsFile;
 
 /**********************
@@ -180,54 +199,54 @@ struct __hts_idx_t;
 typedef struct __hts_idx_t hts_idx_t;
 
 typedef struct {
-	uint64_t u, v;
+    uint64_t u, v;
 } hts_pair64_t;
 
 typedef int hts_readrec_func(BGZF *fp, void *data, void *r, int *tid, int *beg, int *end);
 
 typedef struct {
-	uint32_t read_rest:1, finished:1, dummy:29;
-	int tid, beg, end, n_off, i;
-	uint64_t curr_off;
-	hts_pair64_t *off;
-	hts_readrec_func *readrec;
-	struct {
-		int n, m;
-		int *a;
-	} bins;
+    uint32_t read_rest:1, finished:1, dummy:29;
+    int tid, beg, end, n_off, i;
+    uint64_t curr_off;
+    hts_pair64_t *off;
+    hts_readrec_func *readrec;
+    struct {
+        int n, m;
+        int *a;
+    } bins;
 } hts_itr_t;
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-	#define hts_bin_first(l) (((1<<(((l)<<1) + (l))) - 1) / 7)
-	#define hts_bin_parent(l) (((l) - 1) >> 3)
+    #define hts_bin_first(l) (((1<<(((l)<<1) + (l))) - 1) / 7)
+    #define hts_bin_parent(l) (((l) - 1) >> 3)
 
-	hts_idx_t *hts_idx_init(int n, int fmt, uint64_t offset0, int min_shift, int n_lvls);
-	void hts_idx_destroy(hts_idx_t *idx);
-	int hts_idx_push(hts_idx_t *idx, int tid, int beg, int end, uint64_t offset, int is_mapped);
-	void hts_idx_finish(hts_idx_t *idx, uint64_t final_offset);
+    hts_idx_t *hts_idx_init(int n, int fmt, uint64_t offset0, int min_shift, int n_lvls);
+    void hts_idx_destroy(hts_idx_t *idx);
+    int hts_idx_push(hts_idx_t *idx, int tid, int beg, int end, uint64_t offset, int is_mapped);
+    void hts_idx_finish(hts_idx_t *idx, uint64_t final_offset);
 
-	void hts_idx_save(const hts_idx_t *idx, const char *fn, int fmt);
-	hts_idx_t *hts_idx_load(const char *fn, int fmt);
+    void hts_idx_save(const hts_idx_t *idx, const char *fn, int fmt);
+    hts_idx_t *hts_idx_load(const char *fn, int fmt);
 
-	uint8_t *hts_idx_get_meta(hts_idx_t *idx, int *l_meta);
-	void hts_idx_set_meta(hts_idx_t *idx, int l_meta, uint8_t *meta, int is_copy);
+    uint8_t *hts_idx_get_meta(hts_idx_t *idx, int *l_meta);
+    void hts_idx_set_meta(hts_idx_t *idx, int l_meta, uint8_t *meta, int is_copy);
 
-	int hts_idx_get_stat(const hts_idx_t* idx, int tid, uint64_t* mapped, uint64_t* unmapped);
-	uint64_t hts_idx_get_n_no_coor(const hts_idx_t* idx);
+    int hts_idx_get_stat(const hts_idx_t* idx, int tid, uint64_t* mapped, uint64_t* unmapped);
+    uint64_t hts_idx_get_n_no_coor(const hts_idx_t* idx);
 
-	const char *hts_parse_reg(const char *s, int *beg, int *end);
-	hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec);
-	void hts_itr_destroy(hts_itr_t *iter);
+    const char *hts_parse_reg(const char *s, int *beg, int *end);
+    hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec);
+    void hts_itr_destroy(hts_itr_t *iter);
 
-	typedef int (*hts_name2id_f)(void*, const char*);
-	typedef const char *(*hts_id2name_f)(void*, int);
-	typedef hts_itr_t *hts_itr_query_func(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec);
+    typedef int (*hts_name2id_f)(void*, const char*);
+    typedef const char *(*hts_id2name_f)(void*, int);
+    typedef hts_itr_t *hts_itr_query_func(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec);
 
-	hts_itr_t *hts_itr_querys(const hts_idx_t *idx, const char *reg, hts_name2id_f getid, void *hdr, hts_itr_query_func *itr_query, hts_readrec_func *readrec);
-	int hts_itr_next(BGZF *fp, hts_itr_t *iter, void *r, void *data);
+    hts_itr_t *hts_itr_querys(const hts_idx_t *idx, const char *reg, hts_name2id_f getid, void *hdr, hts_itr_query_func *itr_query, hts_readrec_func *readrec);
+    int hts_itr_next(BGZF *fp, hts_itr_t *iter, void *r, void *data);
     const char **hts_idx_seqnames(const hts_idx_t *idx, int *n, hts_id2name_f getid, void *hdr); // free only the array, not the values
 
     /**
@@ -255,17 +274,17 @@ extern "C" {
 
 static inline int hts_reg2bin(int64_t beg, int64_t end, int min_shift, int n_lvls)
 {
-	int l, s = min_shift, t = ((1<<((n_lvls<<1) + n_lvls)) - 1) / 7;
-	for (--end, l = n_lvls; l > 0; --l, s += 3, t -= 1<<((l<<1)+l))
-		if (beg>>s == end>>s) return t + (beg>>s);
-	return 0;
+    int l, s = min_shift, t = ((1<<((n_lvls<<1) + n_lvls)) - 1) / 7;
+    for (--end, l = n_lvls; l > 0; --l, s += 3, t -= 1<<((l<<1)+l))
+        if (beg>>s == end>>s) return t + (beg>>s);
+    return 0;
 }
 
 static inline int hts_bin_bot(int bin, int n_lvls)
 {
-	int l, b;
-	for (l = 0, b = bin; b; ++l, b = hts_bin_parent(b)); // compute the level of bin
-	return (bin - hts_bin_first(l)) << (n_lvls - l) * 3;
+    int l, b;
+    for (l = 0, b = bin; b; ++l, b = hts_bin_parent(b)); // compute the level of bin
+    return (bin - hts_bin_first(l)) << (n_lvls - l) * 3;
 }
 
 /**************
@@ -274,38 +293,38 @@ static inline int hts_bin_bot(int bin, int n_lvls)
 
 static inline int ed_is_big(void)
 {
-	long one= 1;
-	return !(*((char *)(&one)));
+    long one= 1;
+    return !(*((char *)(&one)));
 }
 static inline uint16_t ed_swap_2(uint16_t v)
 {
-	return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8));
+    return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8));
 }
 static inline void *ed_swap_2p(void *x)
 {
-	*(uint16_t*)x = ed_swap_2(*(uint16_t*)x);
-	return x;
+    *(uint16_t*)x = ed_swap_2(*(uint16_t*)x);
+    return x;
 }
 static inline uint32_t ed_swap_4(uint32_t v)
 {
-	v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
-	return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
+    v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
+    return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
 }
 static inline void *ed_swap_4p(void *x)
 {
-	*(uint32_t*)x = ed_swap_4(*(uint32_t*)x);
-	return x;
+    *(uint32_t*)x = ed_swap_4(*(uint32_t*)x);
+    return x;
 }
 static inline uint64_t ed_swap_8(uint64_t v)
 {
-	v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
-	v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
-	return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
+    v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
+    v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
+    return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
 }
 static inline void *ed_swap_8p(void *x)
 {
-	*(uint64_t*)x = ed_swap_8(*(uint64_t*)x);
-	return x;
+    *(uint64_t*)x = ed_swap_8(*(uint64_t*)x);
+    return x;
 }
 
 #endif
diff --git a/htslib/htslib/hts_defs.h b/htslib/htslib/hts_defs.h
index efc4f6c..c9efbb9 100644
--- a/htslib/htslib/hts_defs.h
+++ b/htslib/htslib/hts_defs.h
@@ -11,7 +11,7 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
-The above copyright notices and this permission notice shall be included in
+The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
diff --git a/htslib/htslib/kfunc.h b/htslib/htslib/kfunc.h
index 0a6ad92..19e4da6 100644
--- a/htslib/htslib/kfunc.h
+++ b/htslib/htslib/kfunc.h
@@ -1,5 +1,31 @@
-#ifndef __KFUNC_H__
-#define __KFUNC_H__
+/* The MIT License
+
+   Copyright (C) 2010, 2013 Genome Research Ltd.
+   Copyright (C) 2011 Attractive Chaos <attractor at live.co.uk>
+
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   "Software"), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+
+   The above copyright notice and this permission notice shall be
+   included in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+   SOFTWARE.
+*/
+
+#ifndef HTSLIB_KFUNC_H
+#define HTSLIB_KFUNC_H
 
 /* Log gamma function
  * \log{\Gamma(z)}
diff --git a/htslib/htslib/khash_str2int.h b/htslib/htslib/khash_str2int.h
index db26d72..8c4f5a6 100644
--- a/htslib/htslib/khash_str2int.h
+++ b/htslib/htslib/khash_str2int.h
@@ -1,12 +1,36 @@
-#ifndef __KHASH_UTILS_H__
-#define __KHASH_UTILS_H__
+/*  khash_str2int.h -- C-string to integer hash table.
+
+    Copyright (C) 2013 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#ifndef HTSLIB_KHASH_STR2INT_H
+#define HTSLIB_KHASH_STR2INT_H
 
 #include <htslib/khash.h>
 
 KHASH_MAP_INIT_STR(str2int, int)
 
 /*
- *  Wrappers for khash dictionaries used by mpileup. 
+ *  Wrappers for khash dictionaries used by mpileup.
  */
 
 static inline void *khash_str2int_init(void)
@@ -16,7 +40,7 @@ static inline void *khash_str2int_init(void)
 
 /*
  *  Destroy the hash structure, but not the keys
- */ 
+ */
 static inline void khash_str2int_destroy(void *_hash)
 {
     khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
@@ -25,7 +49,7 @@ static inline void khash_str2int_destroy(void *_hash)
 
 /*
  *  Destroys both the hash structure and the keys
- */ 
+ */
 static inline void khash_str2int_destroy_free(void *_hash)
 {
     khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
@@ -66,7 +90,8 @@ static inline int khash_str2int_get(void *_hash, const char *str, int *value)
 /*
  *  Add a new string to the dictionary, auto-incrementing the value.
  *  On success returns the newly inserted integer id, on error -1
- *  is returned.
+ *  is returned. Note that the key must continue to exist throughout
+ *  the whole life of _hash.
  */
 static inline int khash_str2int_inc(void *_hash, const char *str)
 {
@@ -82,7 +107,8 @@ static inline int khash_str2int_inc(void *_hash, const char *str)
 
 /*
  *  Set a new key,value pair. On success returns the bin index, on
- *  error -1 is returned.
+ *  error -1 is returned. Note that the key must contnue to exist
+ *  throughout the whole life of _hash.
  */
 static inline int khash_str2int_set(void *_hash, const char *str, int value)
 {
diff --git a/htslib/htslib/knetfile.h b/htslib/htslib/knetfile.h
index c980258..b200a51 100644
--- a/htslib/htslib/knetfile.h
+++ b/htslib/htslib/knetfile.h
@@ -1,3 +1,29 @@
+/* The MIT License
+
+   Copyright (c) 2008 by Genome Research Ltd (GRL).
+                 2010 by Attractive Chaos <attractor at live.co.uk>
+
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   "Software"), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+
+   The above copyright notice and this permission notice shall be
+   included in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+   SOFTWARE.
+*/
+
 #ifndef KNETFILE_H
 #define KNETFILE_H
 
diff --git a/htslib/htslib/kstdint.h b/htslib/htslib/kstdint.h
deleted file mode 100644
index e948850..0000000
--- a/htslib/htslib/kstdint.h
+++ /dev/null
@@ -1,64 +0,0 @@
-#ifndef KSTDINT_H
-#define KSTDINT_H
-
-#include <limits.h>
-
-/* Basic assumptions: 1) "char" is 8-bit; 2) there is a 8-bit, 16-bit, 32-bit
- * and 64-bit integer type, respectively; 3) "short" is no less than "char",
- * "int" is no less than "short", "long" is no less than "int" and "long long"
- * is no less than "long"; 4) "int" is at least 16-bit, "long" at least 32-bit
- * and "long long" at least 64-bit. The last two assumptions are enforced by
- * the C99 spec.
- *
- * Following assumptions 1) and 2), we know that "signed char"=="int8_t" and
- * "short"=="int16_t" for sure. Further from the assumptions, a 32-bit integer
- * type must be either "int" or "long". We can test (UINT16_MAX==UINT_MAX) to
- * see which is the case. Similarly, a 64-bit integer must be either "long" or
- * "long long". We can test (UINT16_MAX==UINT_MAX) to get the definite answer.
- */
-
-/* 8-bit integers */
-typedef signed char          int8_t;
-typedef unsigned char       uint8_t;
-#define INT8_MIN     (-SCHAR_MAX-1)
-#define INT8_MAX          SCHAR_MAX
-#define UINT8_MAX         UCHAR_MAX
-
-/* 16-bit integers */
-typedef signed short        int16_t;
-typedef unsigned short     uint16_t;
-#define INT16_MIN     (-SHRT_MAX-1)
-#define INT16_MAX          SHRT_MAX
-#define UINT16_MAX        USHRT_MAX
-
-/* 32-bit integers */
-#if UINT16_MAX != UINT_MAX
-typedef signed int          int32_t;
-typedef unsigned int       uint32_t;
-#define INT32_MIN      (-INT_MAX-1)
-#define INT32_MAX           INT_MAX
-#define UINT32_MAX         UINT_MAX
-#else /* then int is 16-bit and long is 32-bit, which may happen to compilers for embedded CPUs */
-typedef signed long         int32_t;
-typedef unsigned long      uint32_t;
-#define INT32_MIN     (-LONG_MAX-1)
-#define INT32_MAX          LONG_MAX
-#define UINT32_MAX        ULONG_MAX
-#endif /* ~UINT16_MAX!=UINT_MAX */
-
-/* 64-bit integers */
-#if UINT32_MAX != ULONG_MAX
-typedef signed long         int64_t;
-typedef unsigned long      uint64_t;
-#define INT64_MIN     (-LONG_MAX-1)
-#define INT64_MAX          LONG_MAX
-#define UINT64_MAX        ULONG_MAX
-#else
-typedef signed long long    int64_t;
-typedef unsigned long long uint64_t;
-#define INT64_MIN    (-LLONG_MAX-1)
-#define INT64_MAX         LLONG_MAX
-#define UINT64_MAX       ULLONG_MAX
-#endif /* ~UINT32_MAX!=ULONG_MAX */
-
-#endif /* ~defined(KSTDINT_H) */
diff --git a/htslib/htslib/kstring.h b/htslib/htslib/kstring.h
index 2567efc..7e9b98d 100644
--- a/htslib/htslib/kstring.h
+++ b/htslib/htslib/kstring.h
@@ -1,6 +1,6 @@
 /* The MIT License
 
-   Copyright (c) by Attractive Chaos <attractor at live.co.uk> 
+   Copyright (C) 2011 by Attractive Chaos <attractor at live.co.uk>
 
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
diff --git a/htslib/htslib/sam.h b/htslib/htslib/sam.h
index 9169994..94c18f7 100644
--- a/htslib/htslib/sam.h
+++ b/htslib/htslib/sam.h
@@ -1,5 +1,30 @@
-#ifndef BAM_H
-#define BAM_H
+/*  sam.h -- SAM and BAM file I/O and manipulation.
+
+    Copyright (C) 2008, 2009, 2013-2014 Genome Research Ltd.
+    Copyright (C) 2010, 2012, 2013 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#ifndef HTSLIB_SAM_H
+#define HTSLIB_SAM_H
 
 #include <stdint.h>
 #include "hts.h"
@@ -19,13 +44,13 @@
  */
 
 typedef struct {
-	int32_t n_targets, ignore_sam_err;
-	uint32_t l_text;
-	uint32_t *target_len;
-	int8_t *cigar_tab;
-	char **target_name;
-	char *text;
-	void *sdict;
+    int32_t n_targets, ignore_sam_err;
+    uint32_t l_text;
+    uint32_t *target_len;
+    int8_t *cigar_tab;
+    char **target_name;
+    char *text;
+    void *sdict;
 } bam_hdr_t;
 
 /****************************
@@ -117,14 +142,14 @@ typedef struct {
  @field  mpos    0-based leftmost coordinate of next read in template
  */
 typedef struct {
-	int32_t tid;
-	int32_t pos;
-	uint32_t bin:16, qual:8, l_qname:8;
-	uint32_t flag:16, n_cigar:16;
-	int32_t l_qseq;
-	int32_t mtid;
-	int32_t mpos;
-	int32_t isize;
+    int32_t tid;
+    int32_t pos;
+    uint32_t bin:16, qual:8, l_qname:8;
+    uint32_t flag:16, n_cigar:16;
+    int32_t l_qseq;
+    int32_t mtid;
+    int32_t mpos;
+    int32_t isize;
 } bam1_core_t;
 
 /*! @typedef
@@ -133,9 +158,9 @@ typedef struct {
  @field  l_data     current length of bam1_t::data
  @field  m_data     maximum length of bam1_t::data
  @field  data       all variable-length data, concatenated; structure: qname-cigar-seq-qual-aux
- 
+
  @discussion Notes:
- 
+
  1. qname is zero tailing and core.l_qname includes the tailing '\0'.
  2. l_qseq is calculated from the total length of an alignment block
  on reading or from CIGAR.
@@ -143,11 +168,11 @@ typedef struct {
  4. seq is nybble-encoded according to bam_nt16_table.
  */
 typedef struct {
-	bam1_core_t core;
-	int l_data, m_data;
-	uint8_t *data;
+    bam1_core_t core;
+    int l_data, m_data;
+    uint8_t *data;
 #ifndef BAM_NO_ID
-	uint64_t id;
+    uint64_t id;
 #endif
 } bam1_t;
 
@@ -224,96 +249,98 @@ typedef struct {
 extern "C" {
 #endif
 
-	/***************
-	 *** BAM I/O ***
-	 ***************/
+    /***************
+     *** BAM I/O ***
+     ***************/
 
-	bam_hdr_t *bam_hdr_init(void);
-	bam_hdr_t *bam_hdr_read(BGZF *fp);
-	int bam_hdr_write(BGZF *fp, const bam_hdr_t *h);
-	void bam_hdr_destroy(bam_hdr_t *h);
-	int bam_name2id(bam_hdr_t *h, const char *ref);
-	bam_hdr_t* bam_hdr_dup(const bam_hdr_t *h0);
+    bam_hdr_t *bam_hdr_init(void);
+    bam_hdr_t *bam_hdr_read(BGZF *fp);
+    int bam_hdr_write(BGZF *fp, const bam_hdr_t *h);
+    void bam_hdr_destroy(bam_hdr_t *h);
+    int bam_name2id(bam_hdr_t *h, const char *ref);
+    bam_hdr_t* bam_hdr_dup(const bam_hdr_t *h0);
 
-	bam1_t *bam_init1(void);
-	void bam_destroy1(bam1_t *b);
-	int bam_read1(BGZF *fp, bam1_t *b);
-	int bam_write1(BGZF *fp, const bam1_t *b);
-	bam1_t *bam_copy1(bam1_t *bdst, const bam1_t *bsrc);
-	bam1_t *bam_dup1(const bam1_t *bsrc);
+    bam1_t *bam_init1(void);
+    void bam_destroy1(bam1_t *b);
+    int bam_read1(BGZF *fp, bam1_t *b);
+    int bam_write1(BGZF *fp, const bam1_t *b);
+    bam1_t *bam_copy1(bam1_t *bdst, const bam1_t *bsrc);
+    bam1_t *bam_dup1(const bam1_t *bsrc);
 
-	int bam_cigar2qlen(int n_cigar, const uint32_t *cigar);
-	int bam_cigar2rlen(int n_cigar, const uint32_t *cigar);
+    int bam_cigar2qlen(int n_cigar, const uint32_t *cigar);
+    int bam_cigar2rlen(int n_cigar, const uint32_t *cigar);
 
-	/*!
-	  @abstract Calculate the rightmost base position of an alignment on the
-	  reference genome.
+    /*!
+      @abstract Calculate the rightmost base position of an alignment on the
+      reference genome.
 
-	  @param  b  pointer to an alignment
-	  @return    the coordinate of the first base after the alignment, 0-based
+      @param  b  pointer to an alignment
+      @return    the coordinate of the first base after the alignment, 0-based
 
-	  @discussion For a mapped read, this is just b->core.pos + bam_cigar2rlen.
-	  For an unmapped read (either according to its flags or if it has no cigar
-	  string), we return b->core.pos + 1 by convention.
-	*/
-	int32_t bam_endpos(const bam1_t *b);
+      @discussion For a mapped read, this is just b->core.pos + bam_cigar2rlen.
+      For an unmapped read (either according to its flags or if it has no cigar
+      string), we return b->core.pos + 1 by convention.
+    */
+    int32_t bam_endpos(const bam1_t *b);
 
     int   bam_str2flag(const char *str);    /** returns negative value on error */
     char *bam_flag2str(int flag);   /** The string must be freed by the user */
 
-	/*************************
-	 *** BAM/CRAM indexing ***
-	 *************************/
+    /*************************
+     *** BAM/CRAM indexing ***
+     *************************/
+
+    // These BAM iterator functions work only on BAM files.  To work with either
+    // BAM or CRAM files use the sam_index_load() & sam_itr_*() functions.
+    #define bam_itr_destroy(iter) hts_itr_destroy(iter)
+    #define bam_itr_queryi(idx, tid, beg, end) sam_itr_queryi(idx, tid, beg, end)
+    #define bam_itr_querys(idx, hdr, region) sam_itr_querys(idx, hdr, region)
+    #define bam_itr_next(htsfp, itr, r) hts_itr_next((htsfp)->fp.bgzf, (itr), (r), 0)
 
-	// These BAM iterator functions work only on BAM files.  To work with either
-	// BAM or CRAM files use the sam_index_load() & sam_itr_*() functions.
-	#define bam_itr_destroy(iter) hts_itr_destroy(iter)
-	#define bam_itr_queryi(idx, tid, beg, end) sam_itr_queryi(idx, tid, beg, end)
-	#define bam_itr_querys(idx, hdr, region) sam_itr_querys(idx, hdr, region)
-	#define bam_itr_next(htsfp, itr, r) hts_itr_next((htsfp)->fp.bgzf, (itr), (r), 0)
+    // Load .csi or .bai BAM index file.
+    #define bam_index_load(fn) hts_idx_load((fn), HTS_FMT_BAI)
 
-	// Load .csi or .bai BAM index file.
-	#define bam_index_load(fn) hts_idx_load((fn), HTS_FMT_BAI)
+    int bam_index_build(const char *fn, int min_shift);
 
-	int bam_index_build(const char *fn, int min_shift);
+    // Load BAM (.csi or .bai) or CRAM (.crai) index file.
+    hts_idx_t *sam_index_load(htsFile *fp, const char *fn);
 
-	// Load BAM (.csi or .bai) or CRAM (.crai) index file.
-	hts_idx_t *sam_index_load(htsFile *fp, const char *fn);
+    #define sam_itr_destroy(iter) hts_itr_destroy(iter)
+    hts_itr_t *sam_itr_queryi(const hts_idx_t *idx, int tid, int beg, int end);
+    hts_itr_t *sam_itr_querys(const hts_idx_t *idx, bam_hdr_t *hdr, const char *region);
+    #define sam_itr_next(htsfp, itr, r) hts_itr_next((htsfp)->fp.bgzf, (itr), (r), (htsfp))
 
-	#define sam_itr_destroy(iter) hts_itr_destroy(iter)
-	hts_itr_t *sam_itr_queryi(const hts_idx_t *idx, int tid, int beg, int end);
-	hts_itr_t *sam_itr_querys(const hts_idx_t *idx, bam_hdr_t *hdr, const char *region);
-	#define sam_itr_next(htsfp, itr, r) hts_itr_next((htsfp)->fp.bgzf, (itr), (r), (htsfp))
+    /***************
+     *** SAM I/O ***
+     ***************/
 
-	/***************
-	 *** SAM I/O ***
-	 ***************/
+    #define sam_open(fn, mode) (hts_open((fn), (mode)))
+    #define sam_close(fp) hts_close(fp)
 
-	#define sam_open(fn, mode) (hts_open((fn), (mode)))
-	#define sam_close(fp) hts_close(fp)
+    int sam_open_mode(char *mode, const char *fn, const char *format);
 
-	typedef htsFile samFile;
-	bam_hdr_t *sam_hdr_parse(int l_text, const char *text);
-	bam_hdr_t *sam_hdr_read(samFile *fp);
-	int sam_hdr_write(samFile *fp, const bam_hdr_t *h);
+    typedef htsFile samFile;
+    bam_hdr_t *sam_hdr_parse(int l_text, const char *text);
+    bam_hdr_t *sam_hdr_read(samFile *fp);
+    int sam_hdr_write(samFile *fp, const bam_hdr_t *h);
 
-	int sam_parse1(kstring_t *s, bam_hdr_t *h, bam1_t *b);
-	int sam_format1(const bam_hdr_t *h, const bam1_t *b, kstring_t *str);
-	int sam_read1(samFile *fp, bam_hdr_t *h, bam1_t *b);
-	int sam_write1(samFile *fp, const bam_hdr_t *h, const bam1_t *b);
+    int sam_parse1(kstring_t *s, bam_hdr_t *h, bam1_t *b);
+    int sam_format1(const bam_hdr_t *h, const bam1_t *b, kstring_t *str);
+    int sam_read1(samFile *fp, bam_hdr_t *h, bam1_t *b);
+    int sam_write1(samFile *fp, const bam_hdr_t *h, const bam1_t *b);
 
-	/*************************************
-	 *** Manipulating auxiliary fields ***
-	 *************************************/
+    /*************************************
+     *** Manipulating auxiliary fields ***
+     *************************************/
 
-	uint8_t *bam_aux_get(const bam1_t *b, const char tag[2]);
-	int32_t bam_aux2i(const uint8_t *s);
-	double bam_aux2f(const uint8_t *s);
-	char bam_aux2A(const uint8_t *s);
-	char *bam_aux2Z(const uint8_t *s);
+    uint8_t *bam_aux_get(const bam1_t *b, const char tag[2]);
+    int32_t bam_aux2i(const uint8_t *s);
+    double bam_aux2f(const uint8_t *s);
+    char bam_aux2A(const uint8_t *s);
+    char *bam_aux2Z(const uint8_t *s);
 
-	void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data);
-	int bam_aux_del(bam1_t *b, uint8_t *s);
+    void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data);
+    int bam_aux_del(bam1_t *b, uint8_t *s);
 
 #ifdef __cplusplus
 }
@@ -344,10 +371,10 @@ extern "C" {
  overhead.
  */
 typedef struct {
-	bam1_t *b;
-	int32_t qpos;
-	int indel, level;
-	uint32_t is_del:1, is_head:1, is_tail:1, is_refskip:1, aux:28;
+    bam1_t *b;
+    int32_t qpos;
+    int indel, level;
+    uint32_t is_del:1, is_head:1, is_tail:1, is_refskip:1, aux:28;
 } bam_pileup1_t;
 
 typedef int (*bam_plp_auto_f)(void *data, bam1_t *b);
@@ -363,20 +390,20 @@ extern "C" {
 #endif
 
     /**
-     *  bam_plp_init() - sets an iterator over multiple 
+     *  bam_plp_init() - sets an iterator over multiple
      *  @func:      see mplp_func in bam_plcmd.c in samtools for an example. Expected return
      *              status: 0 on success, -1 on end, < -1 on non-recoverable errors
      *  @data:      user data to pass to @func
      */
-	bam_plp_t bam_plp_init(bam_plp_auto_f func, void *data);
-	void bam_plp_destroy(bam_plp_t iter);
-	int bam_plp_push(bam_plp_t iter, const bam1_t *b);
-	const bam_pileup1_t *bam_plp_next(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp);
-	const bam_pileup1_t *bam_plp_auto(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp);
-	void bam_plp_set_maxcnt(bam_plp_t iter, int maxcnt);
-	void bam_plp_reset(bam_plp_t iter);
-
-	bam_mplp_t bam_mplp_init(int n, bam_plp_auto_f func, void **data);
+    bam_plp_t bam_plp_init(bam_plp_auto_f func, void *data);
+    void bam_plp_destroy(bam_plp_t iter);
+    int bam_plp_push(bam_plp_t iter, const bam1_t *b);
+    const bam_pileup1_t *bam_plp_next(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp);
+    const bam_pileup1_t *bam_plp_auto(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp);
+    void bam_plp_set_maxcnt(bam_plp_t iter, int maxcnt);
+    void bam_plp_reset(bam_plp_t iter);
+
+    bam_mplp_t bam_mplp_init(int n, bam_plp_auto_f func, void **data);
     /**
      *  bam_mplp_init_overlaps() - if called, mpileup will detect overlapping
      *  read pairs and for each base pair set the base quality of the
@@ -386,9 +413,9 @@ extern "C" {
      *  it is multiplied by 0.8.
      */
     void bam_mplp_init_overlaps(bam_mplp_t iter);
-	void bam_mplp_destroy(bam_mplp_t iter);
-	void bam_mplp_set_maxcnt(bam_mplp_t iter, int maxcnt);
-	int bam_mplp_auto(bam_mplp_t iter, int *_tid, int *_pos, int *n_plp, const bam_pileup1_t **plp);
+    void bam_mplp_destroy(bam_mplp_t iter);
+    void bam_mplp_set_maxcnt(bam_mplp_t iter, int maxcnt);
+    int bam_mplp_auto(bam_mplp_t iter, int *_tid, int *_pos, int *n_plp, const bam_pileup1_t **plp);
 
 #ifdef __cplusplus
 }
diff --git a/htslib/htslib/synced_bcf_reader.h b/htslib/htslib/synced_bcf_reader.h
index 2f79a21..76d79d0 100644
--- a/htslib/htslib/synced_bcf_reader.h
+++ b/htslib/htslib/synced_bcf_reader.h
@@ -1,9 +1,33 @@
+/*  synced_bcf_reader.h -- stream through multiple VCF files.
+
+    Copyright (C) 2012-2014 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 /*
-	The synced_bcf_reader allows to keep multiple VCFs open and stream them
-	using the next_line iterator in a seamless matter without worrying about
-	chromosomes and synchronizing the sites. This is used by vcfcheck to
-	compare multiple VCFs simultaneously and is used also for merging,
-	creating intersections, etc.
+    The synced_bcf_reader allows to keep multiple VCFs open and stream them
+    using the next_line iterator in a seamless matter without worrying about
+    chromosomes and synchronizing the sites. This is used by vcfcheck to
+    compare multiple VCFs simultaneously and is used also for merging,
+    creating intersections, etc.
 
     The synced_bcf_reader also provides API for reading indexed BCF/VCF,
     hiding differences in BCF/VCF opening, indexing and reading.
@@ -25,15 +49,15 @@
         bcf_sr_destroy(sr);
 */
 
-#ifndef SYNCED_BCF_READER_H
-#define SYNCED_BCF_READER_H
+#ifndef HTSLIB_SYNCED_BCF_READER_H
+#define HTSLIB_SYNCED_BCF_READER_H
 
 #include "hts.h"
 #include "vcf.h"
 #include "tbx.h"
 
 // How should be treated sites with the same position but different alleles
-#define COLLAPSE_NONE   0   // require the exact same set of alleles in all files 
+#define COLLAPSE_NONE   0   // require the exact same set of alleles in all files
 #define COLLAPSE_SNPS   1   // allow different alleles, as long as they all are SNPs
 #define COLLAPSE_INDELS 2   // the same as above, but with indels
 #define COLLAPSE_ANY    4   // any combination of alleles can be returned by bcf_sr_next_line()
@@ -73,24 +97,24 @@ bcf_sr_regions_t;
 
 typedef struct
 {
-	htsFile *file;
+    htsFile *file;
     tbx_t *tbx_idx;
     hts_idx_t *bcf_idx;
-	bcf_hdr_t *header;
-	hts_itr_t *itr;
-	const char *fname;
-	bcf1_t **buffer;                // cached VCF records. First is the current record synced across the reader
-	int nbuffer, mbuffer;           // number of cached records (including the current record); number of allocated records
-	int nfilter_ids, *filter_ids;   // -1 for ".", otherwise filter id as returned by bcf_id2int
+    bcf_hdr_t *header;
+    hts_itr_t *itr;
+    const char *fname;
+    bcf1_t **buffer;                // cached VCF records. First is the current record synced across the reader
+    int nbuffer, mbuffer;           // number of cached records (including the current record); number of allocated records
+    int nfilter_ids, *filter_ids;   // -1 for ".", otherwise filter id as returned by bcf_id2int
     int type;
-	int *samples, n_smpl;	// list of columns in the order consistent with bcf_srs_t.samples
+    int *samples, n_smpl;   // list of columns in the order consistent with bcf_srs_t.samples
 }
 bcf_sr_t;
 
 typedef struct
 {
-	// Parameters controlling the logic
-	int collapse;       // How should the duplicate sites be treated. One of the COLLAPSE_* types above.
+    // Parameters controlling the logic
+    int collapse;       // How should the duplicate sites be treated. One of the COLLAPSE_* types above.
     char *apply_filters;    // If set, sites where none of the FILTER strings is listed
                             // will be skipped. Active only at the time of
                             // initialization, that is during the add_reader()
@@ -100,19 +124,24 @@ typedef struct
     int max_unpack;     // When reading VCFs and knowing some fields will not be needed, boost performance of vcf_parse1
     int *has_line;      // Corresponds to return value of bcf_sr_next_line but is not limited by sizeof(int). Use bcf_sr_has_line macro to query.
 
-	// Auxiliary data
-	bcf_sr_t *readers;
-	int nreaders;
-	int streaming;      // reading mode: index-jumping or streaming
+    // Auxiliary data
+    bcf_sr_t *readers;
+    int nreaders;
+    int streaming;      // reading mode: index-jumping or streaming
     int explicit_regs;  // was the list of regions se by bcf_sr_set_regions or guessed from tabix index?
-	char **samples;	// List of samples 
+    char **samples; // List of samples
     bcf_sr_regions_t *regions, *targets;    // see bcf_sr_set_[targets|regions] for description
-    int targets_als;    // subset to targets not only by position but also by alleles? (todo)
+    int targets_als;    // subset to targets not only by position but also by alleles?
+    int targets_exclude;
     kstring_t tmps;
-	int n_smpl;
+    int n_smpl;
 }
 bcf_srs_t;
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /** Init bcf_srs_t struct */
 bcf_srs_t *bcf_sr_init(void);
 
@@ -133,7 +162,7 @@ int bcf_sr_add_reader(bcf_srs_t *readers, const char *fname);
 void bcf_sr_remove_reader(bcf_srs_t *files, int i);
 
 
-/** 
+/**
  * bcf_sr_next_line() - the iterator
  * @readers:    holder of the open readers
  *
@@ -157,7 +186,7 @@ int bcf_sr_seek(bcf_srs_t *readers, const char *seq, int pos);
  * bcf_sr_set_samples() - sets active samples
  * @readers: holder of the open readers
  * @samples: this can be one of: file name with one sample per line;
- *           or column-separated list of samples; or '-' for a list of 
+ *           or column-separated list of samples; or '-' for a list of
  *           samples shared by all files. If first character is the
  *           exclamation mark, all but the listed samples are included.
  * @is_file: 0: list of samples; 1: file with sample names
@@ -189,6 +218,8 @@ int bcf_sr_set_samples(bcf_srs_t *readers, const char *samples, int is_file);
  *  duplicate VCF lines. It is up to the caller to examine targets->als if
  *  perfect match is sought after. Note that the duplicate positions in targets
  *  file are currently not supported.
+ *  Targets (but not regions) can be prefixed with "^" to request logical complement,
+ *  for example "^X,Y,MT" indicates that sequences X, Y and MT should be skipped.
  */
 int bcf_sr_set_targets(bcf_srs_t *readers, const char *targets, int is_file, int alleles);
 int bcf_sr_set_regions(bcf_srs_t *readers, const char *regions, int is_file);
@@ -196,7 +227,7 @@ int bcf_sr_set_regions(bcf_srs_t *readers, const char *regions, int is_file);
 
 
 /*
- *  bcf_sr_regions_init() 
+ *  bcf_sr_regions_init()
  *  @regions:   regions can be either a comma-separated list of regions
  *              (chr|chr:pos|chr:from-to|chr:from-) or VCF, BED, or
  *              tab-delimited file (the default). Uncompressed files
@@ -205,10 +236,10 @@ int bcf_sr_set_regions(bcf_srs_t *readers, const char *regions, int is_file);
  *  @is_file:   0: regions is a comma-separated list of regions
  *                  (chr|chr:pos|chr:from-to|chr:from-)
  *              1: VCF, BED or tab-delimited file
- *  @chr, from, to:       
+ *  @chr, from, to:
  *              Column indexes of chromosome, start position and end position
  *              in the tab-delimited file. The positions are 1-based and
- *              inclusive. 
+ *              inclusive.
  *              These parameters are ignored when reading from VCF, BED or
  *              tabix-indexed files. When end position column is not present,
  *              supply 'from' in place of 'to'. When 'to' is negative, first
@@ -252,4 +283,8 @@ int bcf_sr_regions_overlap(bcf_sr_regions_t *reg, const char *seq, int start, in
  */
 void bcf_sr_regions_flush(bcf_sr_regions_t *regs);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
diff --git a/htslib/htslib/tbx.h b/htslib/htslib/tbx.h
index 71b4ac2..3d84a86 100644
--- a/htslib/htslib/tbx.h
+++ b/htslib/htslib/tbx.h
@@ -1,5 +1,30 @@
-#ifndef TBX_H
-#define TBX_H
+/*  tbx.h -- tabix API functions.
+
+    Copyright (C) 2009, 2012-2014 Genome Research Ltd.
+    Copyright (C) 2010, 2012 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#ifndef HTSLIB_TBX_H
+#define HTSLIB_TBX_H
 
 #include "hts.h"
 
@@ -11,15 +36,15 @@
 #define TBX_UCSC    0x10000
 
 typedef struct {
-	int32_t preset;
-	int32_t sc, bc, ec; // seq col., beg col. and end col.
-	int32_t meta_char, line_skip;
+    int32_t preset;
+    int32_t sc, bc, ec; // seq col., beg col. and end col.
+    int32_t meta_char, line_skip;
 } tbx_conf_t;
 
 typedef struct {
-	tbx_conf_t conf;
-	hts_idx_t *idx;
-	void *dict;
+    tbx_conf_t conf;
+    hts_idx_t *idx;
+    void *dict;
 } tbx_t;
 
 extern tbx_conf_t tbx_conf_gff, tbx_conf_bed, tbx_conf_psltbl, tbx_conf_sam, tbx_conf_vcf;
@@ -28,22 +53,22 @@ extern tbx_conf_t tbx_conf_gff, tbx_conf_bed, tbx_conf_psltbl, tbx_conf_sam, tbx
 extern "C" {
 #endif
 
-	#define tbx_itr_destroy(iter) hts_itr_destroy(iter)
-	#define tbx_itr_queryi(tbx, tid, beg, end) hts_itr_query((tbx)->idx, (tid), (beg), (end), tbx_readrec)
-	#define tbx_itr_querys(tbx, s) hts_itr_querys((tbx)->idx, (s), (hts_name2id_f)(tbx_name2id), (tbx), hts_itr_query, tbx_readrec)
-	#define tbx_itr_next(htsfp, tbx, itr, r) hts_itr_next(hts_get_bgzfp(htsfp), (itr), (r), (tbx))
-	#define tbx_bgzf_itr_next(bgzfp, tbx, itr, r) hts_itr_next((bgzfp), (itr), (r), (tbx))
+    #define tbx_itr_destroy(iter) hts_itr_destroy(iter)
+    #define tbx_itr_queryi(tbx, tid, beg, end) hts_itr_query((tbx)->idx, (tid), (beg), (end), tbx_readrec)
+    #define tbx_itr_querys(tbx, s) hts_itr_querys((tbx)->idx, (s), (hts_name2id_f)(tbx_name2id), (tbx), hts_itr_query, tbx_readrec)
+    #define tbx_itr_next(htsfp, tbx, itr, r) hts_itr_next(hts_get_bgzfp(htsfp), (itr), (r), (tbx))
+    #define tbx_bgzf_itr_next(bgzfp, tbx, itr, r) hts_itr_next((bgzfp), (itr), (r), (tbx))
 
-	int tbx_name2id(tbx_t *tbx, const char *ss);
+    int tbx_name2id(tbx_t *tbx, const char *ss);
 
-	/* Internal helper function used by tbx_itr_next() */
-	BGZF *hts_get_bgzfp(htsFile *fp);
-	int tbx_readrec(BGZF *fp, void *tbxv, void *sv, int *tid, int *beg, int *end);
+    /* Internal helper function used by tbx_itr_next() */
+    BGZF *hts_get_bgzfp(htsFile *fp);
+    int tbx_readrec(BGZF *fp, void *tbxv, void *sv, int *tid, int *beg, int *end);
 
-	int tbx_index_build(const char *fn, int min_shift, const tbx_conf_t *conf);
-	tbx_t *tbx_index_load(const char *fn);
-	const char **tbx_seqnames(tbx_t *tbx, int *n);	// free the array but not the values
-	void tbx_destroy(tbx_t *tbx);
+    int tbx_index_build(const char *fn, int min_shift, const tbx_conf_t *conf);
+    tbx_t *tbx_index_load(const char *fn);
+    const char **tbx_seqnames(tbx_t *tbx, int *n);  // free the array but not the values
+    void tbx_destroy(tbx_t *tbx);
 
 #ifdef __cplusplus
 }
diff --git a/htslib/htslib/vcf.h b/htslib/htslib/vcf.h
index a4666de..38d418c 100644
--- a/htslib/htslib/vcf.h
+++ b/htslib/htslib/vcf.h
@@ -1,11 +1,36 @@
+/*  vcf.h -- VCF/BCF API functions.
+
+    Copyright (C) 2012, 2013 Broad Institute.
+    Copyright (C) 2012-2014 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 /*
-    todo: 
+    todo:
         - make the function names consistent
         - provide calls to abstract away structs as much as possible
  */
 
-#ifndef BCF_H
-#define BCF_H
+#ifndef HTSLIB_VCF_H
+#define HTSLIB_VCF_H
 
 #include <stdint.h>
 #include <limits.h>
@@ -48,9 +73,9 @@
    size of the hash table or, equivalently, the length of the id[] arrays.
 */
 
-#define BCF_DT_ID		0 // dictionary type
-#define BCF_DT_CTG		1
-#define BCF_DT_SAMPLE	2
+#define BCF_DT_ID       0 // dictionary type
+#define BCF_DT_CTG      1
+#define BCF_DT_SAMPLE   2
 
 // Complete textual representation of a header line
 typedef struct {
@@ -62,27 +87,27 @@ typedef struct {
 } bcf_hrec_t;
 
 typedef struct {
-	uint32_t info[3];  // stores Number:20, var:4, Type:4, ColType:4 for BCF_HL_FLT,INFO,FMT
+    uint32_t info[3];  // stores Number:20, var:4, Type:4, ColType:4 for BCF_HL_FLT,INFO,FMT
     bcf_hrec_t *hrec[3];
-	int id;
+    int id;
 } bcf_idinfo_t;
 
 typedef struct {
-	const char *key;
-	const bcf_idinfo_t *val;
+    const char *key;
+    const bcf_idinfo_t *val;
 } bcf_idpair_t;
 
 typedef struct {
-	int32_t n[3];
-	bcf_idpair_t *id[3];
-	void *dict[3]; // ID dictionary, contig dict and sample dict
-	char **samples;
+    int32_t n[3];
+    bcf_idpair_t *id[3];
+    void *dict[3]; // ID dictionary, contig dict and sample dict
+    char **samples;
     bcf_hrec_t **hrec;
     int nhrec;
     int ntransl, *transl[2];    // for bcf_translate()
     int nsamples_ori;           // for bcf_hdr_set_samples()
     uint8_t *keep_samples;
-	kstring_t mem;
+    kstring_t mem;
 } bcf_hdr_t;
 
 extern uint8_t bcf_type_shift[];
@@ -91,12 +116,12 @@ extern uint8_t bcf_type_shift[];
  * VCF record *
  **************/
 
-#define BCF_BT_NULL		0
-#define BCF_BT_INT8		1
-#define BCF_BT_INT16	2
-#define BCF_BT_INT32	3
-#define BCF_BT_FLOAT	5
-#define BCF_BT_CHAR		7
+#define BCF_BT_NULL     0
+#define BCF_BT_INT8     1
+#define BCF_BT_INT16    2
+#define BCF_BT_INT32    3
+#define BCF_BT_FLOAT    5
+#define BCF_BT_CHAR     7
 
 #define VCF_REF   0
 #define VCF_SNP   1
@@ -105,28 +130,28 @@ extern uint8_t bcf_type_shift[];
 #define VCF_OTHER 8
 
 typedef struct {
-	int type, n;	// variant type and the number of bases affected, negative for deletions
+    int type, n;    // variant type and the number of bases affected, negative for deletions
 } variant_t;
 
 typedef struct {
-	int id;             // id: numeric tag id, the corresponding string is bcf_hdr_t::id[BCF_DT_ID][$id].key
+    int id;             // id: numeric tag id, the corresponding string is bcf_hdr_t::id[BCF_DT_ID][$id].key
     int n, size, type;  // n: number of values per-sample; size: number of bytes per-sample; type: one of BCF_BT_* types
-	uint8_t *p;         // same as vptr and vptr_* in bcf_info_t below
+    uint8_t *p;         // same as vptr and vptr_* in bcf_info_t below
     uint32_t p_len;
     uint32_t p_off:31, p_free:1;
 } bcf_fmt_t;
 
 typedef struct {
-	int key;        // key: numeric tag id, the corresponding string is bcf_hdr_t::id[BCF_DT_ID][$key].key
+    int key;        // key: numeric tag id, the corresponding string is bcf_hdr_t::id[BCF_DT_ID][$key].key
     int type, len;  // type: one of BCF_BT_* types; len: vector length, 1 for scalars
-	union {
-		int32_t i; // integer value
-		float f;   // float value
-	} v1; // only set if $len==1; for easier access
-	uint8_t *vptr;          // pointer to data array in bcf1_t->shared.s, excluding the size+type and tag id bytes
+    union {
+        int32_t i; // integer value
+        float f;   // float value
+    } v1; // only set if $len==1; for easier access
+    uint8_t *vptr;          // pointer to data array in bcf1_t->shared.s, excluding the size+type and tag id bytes
     uint32_t vptr_len;      // length of the vptr block or, when set, of the vptr_mod block, excluding offset
     uint32_t vptr_off:31,   // vptr offset, i.e., the size of the INFO key plus size+type bytes
-            vptr_free:1;    // indicates that vptr-vptr_off must be freed; set only when modified and the new 
+            vptr_free:1;    // indicates that vptr-vptr_off must be freed; set only when modified and the new
                             //    data block is bigger than the original
 } bcf_info_t;
 
@@ -137,15 +162,15 @@ typedef struct {
 #define BCF1_DIRTY_INF 8
 
 typedef struct {
-	int m_fmt, m_info, m_id, m_als, m_allele, m_flt; // allocated size (high-water mark); do not change
-	int n_flt;  // Number of FILTER fields
-	int *flt;   // FILTER keys in the dictionary
+    int m_fmt, m_info, m_id, m_als, m_allele, m_flt; // allocated size (high-water mark); do not change
+    int n_flt;  // Number of FILTER fields
+    int *flt;   // FILTER keys in the dictionary
     char *id, *als;     // ID and REF+ALT block (\0-seperated)
-	char **allele;      // allele[0] is the REF (allele[] pointers to the als block); all null terminated
-	bcf_info_t *info;   // INFO
-	bcf_fmt_t *fmt;     // FORMAT and individual sample
-	variant_t *var;	    // $var and $var_type set only when set_variant_types called
-	int n_var, var_type;
+    char **allele;      // allele[0] is the REF (allele[] pointers to the als block); all null terminated
+    bcf_info_t *info;   // INFO
+    bcf_fmt_t *fmt;     // FORMAT and individual sample
+    variant_t *var;     // $var and $var_type set only when set_variant_types called
+    int n_var, var_type;
     int shared_dirty;   // if set, shared.s must be recreated on BCF output
     int indiv_dirty;    // if set, indiv.s must be recreated on BCF output
 } bcf_dec_t;
@@ -153,6 +178,7 @@ typedef struct {
 
 #define BCF_ERR_CTG_UNDEF 1
 #define BCF_ERR_TAG_UNDEF 2
+#define BCF_ERR_NCOLS     4
 
 /*
     The bcf1_t structure corresponds to one VCF/BCF line. Reading from VCF file
@@ -163,21 +189,20 @@ typedef struct {
     appropriately.
     Similarly, it is fast to output a BCF line because the columns (kept in
     shared.s, indiv.s, etc.) are written directly by bcf_write, whereas a VCF
-    line must be formatted in vcf_format. 
+    line must be formatted in vcf_format.
  */
 typedef struct {
-	int32_t rid;  // CHROM
-	int32_t pos;  // POS
-	int32_t rlen; // length of REF
-	float qual;   // QUAL
-	uint32_t n_info:16, n_allele:16;
-	uint32_t n_fmt:8, n_sample:24;
-	kstring_t shared, indiv;
-	bcf_dec_t d; // lazy evaluation: $d is not generated by bcf_read(), but by explicitly calling bcf_unpack()
+    int32_t rid;  // CHROM
+    int32_t pos;  // POS
+    int32_t rlen; // length of REF
+    float qual;   // QUAL
+    uint32_t n_info:16, n_allele:16;
+    uint32_t n_fmt:8, n_sample:24;
+    kstring_t shared, indiv;
+    bcf_dec_t d; // lazy evaluation: $d is not generated by bcf_read(), but by explicitly calling bcf_unpack()
     int max_unpack;         // Set to BCF_UN_STR, BCF_UN_FLT, or BCF_UN_INFO to boost performance of vcf_parse when some of the fields won't be needed
-	int unpacked;           // remember what has been unpacked to allow calling bcf_unpack() repeatedly without redoing the work
-	uint8_t *unpack_ptr;    // position of the last unpack call
-    int unpack_size[3];     // the original block size of ID, REF+ALT and FILTER 
+    int unpacked;           // remember what has been unpacked to allow calling bcf_unpack() repeatedly without redoing the work
+    int unpack_size[3];     // the original block size of ID, REF+ALT and FILTER
     int errcode;    // one of BCF_ERR_* codes
 } bcf1_t;
 
@@ -189,8 +214,8 @@ typedef struct {
 extern "C" {
 #endif
 
-	/***********************************************************************
-	 *  BCF and VCF I/O
+    /***********************************************************************
+     *  BCF and VCF I/O
      *
      *  A note about naming conventions: htslib internally represents VCF
      *  records as bcf1_t data structures, therefore most functions are
@@ -199,7 +224,7 @@ extern "C" {
      *  these cases, functions prefixed with bcf_ are more general and work
      *  with both BCF and VCF.
      *
-	 ***********************************************************************/
+     ***********************************************************************/
 
     /** These macros are defined only for consistency with other parts of htslib */
     #define bcf_init1()         bcf_init()
@@ -219,40 +244,40 @@ extern "C" {
      *  When opened for writing, the mandatory fileFormat and
      *  FILTER=PASS lines are added automatically.
      */
-	bcf_hdr_t *bcf_hdr_init(const char *mode);
+    bcf_hdr_t *bcf_hdr_init(const char *mode);
+
+    /** Destroy a BCF header struct */
+    void bcf_hdr_destroy(bcf_hdr_t *h);
 
-   	/** Destroy a BCF header struct */
-	void bcf_hdr_destroy(bcf_hdr_t *h);
+    /** Initialize a bcf1_t object; equivalent to calloc(1, sizeof(bcf1_t)) */
+    bcf1_t *bcf_init(void);
 
-	/** Initialize a bcf1_t object; equivalent to calloc(1, sizeof(bcf1_t)) */
-	bcf1_t *bcf_init(void);
-	
-	/** Deallocate a bcf1_t object */
-	void bcf_destroy(bcf1_t *v);
+    /** Deallocate a bcf1_t object */
+    void bcf_destroy(bcf1_t *v);
 
-    /** 
+    /**
      *  Same as bcf_destroy() but frees only the memory allocated by bcf1_t,
-     *  not the bcf1_t object itself. 
+     *  not the bcf1_t object itself.
      */
-	void bcf_empty(bcf1_t *v);
+    void bcf_empty(bcf1_t *v);
 
-	/** 
+    /**
      *  Make the bcf1_t object ready for next read. Intended mostly for
      *  internal use, the user should rarely need to call this function
      *  directly.
      */
-	void bcf_clear(bcf1_t *v);
+    void bcf_clear(bcf1_t *v);
 
 
     /** bcf_open and vcf_open mode: please see hts_open() in hts.h */
-	typedef htsFile vcfFile;
-	#define bcf_open(fn, mode) hts_open((fn), (mode))
-	#define vcf_open(fn, mode) hts_open((fn), (mode))
-	#define bcf_close(fp) hts_close(fp)
-	#define vcf_close(fp) hts_close(fp)
+    typedef htsFile vcfFile;
+    #define bcf_open(fn, mode) hts_open((fn), (mode))
+    #define vcf_open(fn, mode) hts_open((fn), (mode))
+    #define bcf_close(fp) hts_close(fp)
+    #define vcf_close(fp) hts_close(fp)
 
     /** Reads VCF or BCF header */
-	bcf_hdr_t *bcf_hdr_read(htsFile *fp);
+    bcf_hdr_t *bcf_hdr_read(htsFile *fp);
 
     /**
      *  bcf_hdr_set_samples() - for more efficient VCF parsing when only one/few samples are needed
@@ -281,13 +306,13 @@ extern "C" {
 
 
     /** Writes VCF or BCF header */
-	int bcf_hdr_write(htsFile *fp, const bcf_hdr_t *h);
+    int bcf_hdr_write(htsFile *fp, const bcf_hdr_t *h);
 
     /** Parse VCF line contained in kstring and populate the bcf1_t struct */
-	int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v);
+    int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v);
 
     /** The opposite of vcf_parse. It should rarely be called directly, see vcf_write */
-	int vcf_format(const bcf_hdr_t *h, const bcf1_t *v, kstring_t *s);
+    int vcf_format(const bcf_hdr_t *h, const bcf1_t *v, kstring_t *s);
 
     /**
      *  bcf_read() - read next VCF or BCF record
@@ -297,26 +322,26 @@ extern "C" {
      *  set to one of BCF_ERR* code and must be checked before calling
      *  vcf_write().
      */
-	int bcf_read(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v);
+    int bcf_read(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v);
 
-	/**
-	 *  bcf_unpack() - unpack/decode a BCF record (fills the bcf1_t::d field)
-     *  
+    /**
+     *  bcf_unpack() - unpack/decode a BCF record (fills the bcf1_t::d field)
+     *
      *  Note that bcf_unpack() must be called even when reading VCF. It is safe
      *  to call the function repeatedly, it will not unpack the same field
      *  twice.
-	 */
-	#define BCF_UN_STR  1       // up to ALT inclusive
-	#define BCF_UN_FLT  2       // up to FILTER
-	#define BCF_UN_INFO 4       // up to INFO
-	#define BCF_UN_SHR  (BCF_UN_STR|BCF_UN_FLT|BCF_UN_INFO) // all shared information
-	#define BCF_UN_FMT  8                           // unpack format and each sample
-	#define BCF_UN_IND  BCF_UN_FMT                  // a synonymo of BCF_UN_FMT
-	#define BCF_UN_ALL  (BCF_UN_SHR|BCF_UN_FMT)     // everything
-	int bcf_unpack(bcf1_t *b, int which);
+     */
+    #define BCF_UN_STR  1       // up to ALT inclusive
+    #define BCF_UN_FLT  2       // up to FILTER
+    #define BCF_UN_INFO 4       // up to INFO
+    #define BCF_UN_SHR  (BCF_UN_STR|BCF_UN_FLT|BCF_UN_INFO) // all shared information
+    #define BCF_UN_FMT  8                           // unpack format and each sample
+    #define BCF_UN_IND  BCF_UN_FMT                  // a synonymo of BCF_UN_FMT
+    #define BCF_UN_ALL  (BCF_UN_SHR|BCF_UN_FMT)     // everything
+    int bcf_unpack(bcf1_t *b, int which);
 
     /*
-     *  bcf_dup() - create a copy of BCF record. 
+     *  bcf_dup() - create a copy of BCF record.
      *
      *  Note that bcf_unpack() must be called on the returned copy as if it was
      *  obtained from bcf_read(). Also note that bcf_dup() calls bcf_sync1(src)
@@ -327,36 +352,41 @@ extern "C" {
     /**
      *  bcf_write() - write one VCF or BCF record. The type is determined at the open() call.
      */
-	int bcf_write(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v);
+    int bcf_write(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v);
 
     /**
      *  The following functions work only with VCFs and should rarely be called
      *  directly. Usually one wants to use their bcf_* alternatives, which work
      *  transparently with both VCFs and BCFs.
      */
-	bcf_hdr_t *vcf_hdr_read(htsFile *fp);
-	int vcf_hdr_write(htsFile *fp, const bcf_hdr_t *h);
-	int vcf_read(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v);
-	int vcf_write(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v);
+    bcf_hdr_t *vcf_hdr_read(htsFile *fp);
+    int vcf_hdr_write(htsFile *fp, const bcf_hdr_t *h);
+    int vcf_read(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v);
+    int vcf_write(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v);
 
-	/** Helper function for the bcf_itr_next() macro; internal use, ignore it */
-	int bcf_readrec(BGZF *fp, void *null, void *v, int *tid, int *beg, int *end);
+    /** Helper function for the bcf_itr_next() macro; internal use, ignore it */
+    int bcf_readrec(BGZF *fp, void *null, void *v, int *tid, int *beg, int *end);
 
 
 
-	/**************************************************************************
-	 *  Header querying and manipulation routines
-	 **************************************************************************/
+    /**************************************************************************
+     *  Header querying and manipulation routines
+     **************************************************************************/
 
     /** Create a new header using the supplied template */
     bcf_hdr_t *bcf_hdr_dup(const bcf_hdr_t *hdr);
-    /** Copy header lines from src to dst if not already present in dst. See also bcf_translate(). */
-    void bcf_hdr_combine(bcf_hdr_t *dst, const bcf_hdr_t *src);
+    /**
+     *  Copy header lines from src to dst if not already present in dst. See also bcf_translate().
+     *  Returns 0 on success or sets a bit on error:
+     *      1 .. conflicting definitions of tag length
+     *      // todo
+     */
+    int bcf_hdr_combine(bcf_hdr_t *dst, const bcf_hdr_t *src);
 
-    /** 
-     *  bcf_hdr_add_sample() - add a new sample. 
-     *  @param sample:  Sample name to be added. After all samples have been added, NULL 
-     *                  must be passed to update internal header structures. 
+    /**
+     *  bcf_hdr_add_sample() - add a new sample.
+     *  @param sample:  Sample name to be added. After all samples have been added, NULL
+     *                  must be passed to update internal header structures.
      */
     int bcf_hdr_add_sample(bcf_hdr_t *hdr, const char *sample);
 
@@ -376,7 +406,7 @@ extern "C" {
     const char *bcf_hdr_get_version(const bcf_hdr_t *hdr);
     void bcf_hdr_set_version(bcf_hdr_t *hdr, const char *version);
 
-    /** 
+    /**
      *  bcf_hdr_remove() - remove VCF header tag
      *  @param type:      one of BCF_HL_*
      *  @param key:       tag name
@@ -384,7 +414,7 @@ extern "C" {
     void bcf_hdr_remove(bcf_hdr_t *h, int type, const char *key);
 
     /**
-     *  bcf_hdr_subset() - creates a new copy of the header removing unwanted samples 
+     *  bcf_hdr_subset() - creates a new copy of the header removing unwanted samples
      *  @param n:        number of samples to keep
      *  @param samples:  names of the samples to keep
      *  @param imap:     mapping from index in @samples to the sample index in the original file
@@ -394,34 +424,45 @@ extern "C" {
      *  This function can be used to reorder samples.
      *  See also bcf_subset() which subsets individual records.
      */
-	bcf_hdr_t *bcf_hdr_subset(const bcf_hdr_t *h0, int n, char *const* samples, int *imap);
+    bcf_hdr_t *bcf_hdr_subset(const bcf_hdr_t *h0, int n, char *const* samples, int *imap);
 
     /** Creates a list of sequence names. It is up to the caller to free the list (but not the sequence names) */
-	const char **bcf_hdr_seqnames(const bcf_hdr_t *h, int *nseqs);
+    const char **bcf_hdr_seqnames(const bcf_hdr_t *h, int *nseqs);
 
     /** Get number of samples */
     #define bcf_hdr_nsamples(hdr) (hdr)->n[BCF_DT_SAMPLE]
 
 
     /** The following functions are for internal use and should rarely be called directly */
+    int bcf_hdr_parse(bcf_hdr_t *hdr, char *htxt);
+    int bcf_hdr_sync(bcf_hdr_t *h);
     bcf_hrec_t *bcf_hdr_parse_line(const bcf_hdr_t *h, const char *line, int *len);
     void bcf_hrec_format(const bcf_hrec_t *hrec, kstring_t *str);
     int bcf_hdr_add_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec);
-    bcf_hrec_t *bcf_hdr_get_hrec(const bcf_hdr_t *hdr, int type, const char *id);   // type is one of BCF_HL_FLT,..,BCF_HL_CTG
+    /**
+     *  bcf_hdr_get_hrec() - get header line info
+     *  @param type:  one of the BCF_HL_* types: FLT,INFO,FMT,CTG,STR,GEN
+     *  @param key:   the header key for generic lines (e.g. "fileformat"), any field
+     *                  for structured lines, typically "ID".
+     *  @param value: the value which pairs with key. Can be be NULL for BCF_HL_GEN
+     *  @param str_class: the class of BCF_HL_STR line (e.g. "ALT" or "SAMPLE"), otherwise NULL
+     */
+    bcf_hrec_t *bcf_hdr_get_hrec(const bcf_hdr_t *hdr, int type, const char *key, const char *value, const char *str_class);
     bcf_hrec_t *bcf_hrec_dup(bcf_hrec_t *hrec);
     void bcf_hrec_add_key(bcf_hrec_t *hrec, const char *str, int len);
     void bcf_hrec_set_val(bcf_hrec_t *hrec, int i, const char *str, int len, int is_quoted);
     int bcf_hrec_find_key(bcf_hrec_t *hrec, const char *key);
+    void hrec_add_idx(bcf_hrec_t *hrec, int idx);
     void bcf_hrec_destroy(bcf_hrec_t *hrec);
 
 
 
-	/**************************************************************************
-	 *  Individual record querying and manipulation routines
-	 **************************************************************************/
+    /**************************************************************************
+     *  Individual record querying and manipulation routines
+     **************************************************************************/
 
     /** See the description of bcf_hdr_subset() */
-	int bcf_subset(const bcf_hdr_t *h, bcf1_t *v, int n, int *imap);
+    int bcf_subset(const bcf_hdr_t *h, bcf1_t *v, int n, int *imap);
 
     /**
      *  bcf_translate() - translate tags ids to be consistent with different header. This function
@@ -437,7 +478,7 @@ extern "C" {
      */
     int bcf_get_variant_types(bcf1_t *rec);
     int bcf_get_variant_type(bcf1_t *rec, int ith_allele);
-	int bcf_is_snp(bcf1_t *v);
+    int bcf_is_snp(bcf1_t *v);
 
     /**
      *  bcf_update_filter() - sets the FILTER column
@@ -448,17 +489,17 @@ extern "C" {
     /**
      *  bcf_add_filter() - adds to the FILTER column
      *  @flt_id:   filter ID to add, numeric ID returned by bcf_id2int(hdr, BCF_DT_ID, "PASS")
-     *  
+     *
      *  If flt_id is PASS, all existing filters are removed first. If other than PASS, existing PASS is removed.
      */
     int bcf_add_filter(const bcf_hdr_t *hdr, bcf1_t *line, int flt_id);
     /**
      *  bcf_remove_filter() - removes from the FILTER column
      *  @flt_id:   filter ID to remove, numeric ID returned by bcf_id2int(hdr, BCF_DT_ID, "PASS")
-     *  @pass:     when set to 1 and no filters are present, set to PASS 
+     *  @pass:     when set to 1 and no filters are present, set to PASS
      */
     int bcf_remove_filter(const bcf_hdr_t *hdr, bcf1_t *line, int flt_id, int pass);
-    /** 
+    /**
      *  Returns 1 if present, 0 if absent, or -1 if filter does not exist. "PASS" and "." can be used interchangeably.
      */
     int bcf_has_filter(const bcf_hdr_t *hdr, bcf1_t *line, char *filter);
@@ -466,13 +507,18 @@ extern "C" {
      *  bcf_update_alleles() and bcf_update_alleles_str() - update REF and ALLT column
      *  @alleles:           Array of alleles
      *  @nals:              Number of alleles
-     *  @alleles_string:    Comma-separated alleles, starting with the REF allele  
+     *  @alleles_string:    Comma-separated alleles, starting with the REF allele
+     *
+     *  Not that in order for indexing to work correctly in presence of INFO/END tag,
+     *  the length of reference allele (line->rlen) must be set explicitly by the caller,
+     *  or otherwise, if rlen is zero, strlen(line->d.allele[0]) is used to set the length
+     *  on bcf_write().
      */
     int bcf_update_alleles(const bcf_hdr_t *hdr, bcf1_t *line, const char **alleles, int nals);
     int bcf_update_alleles_str(const bcf_hdr_t *hdr, bcf1_t *line, const char *alleles_string);
     int bcf_update_id(const bcf_hdr_t *hdr, bcf1_t *line, const char *id);
 
-    /* 
+    /*
      *  bcf_update_info_*() - functions for updating INFO fields
      *  @hdr:       the BCF header
      *  @line:      VCF line to be edited
@@ -491,10 +537,10 @@ extern "C" {
     #define bcf_update_info_string(hdr,line,key,string)    bcf_update_info((hdr),(line),(key),(string),1,BCF_HT_STR)
     int bcf_update_info(const bcf_hdr_t *hdr, bcf1_t *line, const char *key, const void *values, int n, int type);
 
-    /* 
+    /*
      *  bcf_update_format_*() - functions for updating FORMAT fields
      *  @values:    pointer to the array of values, the same number of elements
-     *              is expected for each sample. Missing values must be padded 
+     *              is expected for each sample. Missing values must be padded
      *              with bcf_*_missing or bcf_*_vector_end values.
      *  @n:         number of values in the array. If n==0, existing tag is removed.
      *
@@ -532,19 +578,30 @@ extern "C" {
         *b = dk - 1; *a = igt - k + *b;
     }
 
-    /**     
+    /**
      * bcf_get_fmt() - returns pointer to FORMAT's field data
-     * @header: for access to BCF_DT_ID dictionary 
+     * @header: for access to BCF_DT_ID dictionary
      * @line:   VCF line obtained from vcf_parse1
      * @fmt:    one of GT,PL,...
-     *  
+     *
      * Returns bcf_fmt_t* if the call succeeded, or returns NULL when the field
      * is not available.
-     */ 
+     */
     bcf_fmt_t *bcf_get_fmt(const bcf_hdr_t *hdr, bcf1_t *line, const char *key);
     bcf_info_t *bcf_get_info(const bcf_hdr_t *hdr, bcf1_t *line, const char *key);
 
     /**
+     * bcf_get_*_id() - returns pointer to FORMAT/INFO field data given the header index instead of the string ID
+     * @line: VCF line obtained from vcf_parse1
+     * @id:  The header index for the tag, obtained from bcf_hdr_id2int()
+     * 
+     * Returns bcf_fmt_t* / bcf_info_t*. These functions do not check if the index is valid 
+     * as their goal is to avoid the header lookup.
+     */
+    bcf_fmt_t *bcf_get_fmt_id(bcf1_t *line, const int id);
+    bcf_info_t *bcf_get_info_id(bcf1_t *line, const int id);
+
+    /**
      *  bcf_get_info_*() - get INFO values, integers or floats
      *  @hdr:       BCF header
      *  @line:      BCF record
@@ -553,7 +610,7 @@ extern "C" {
      *  @ndst:      pointer to the size of allocated memory
      *
      *  Returns negative value on error or the number of written values on
-     *  success. bcf_get_info_string() returns on success the number of 
+     *  success. bcf_get_info_string() returns on success the number of
      *  characters written excluding the null-terminating byte. bcf_get_info_flag()
      *  returns 1 when flag is set or 0 if not.
      *
@@ -572,10 +629,10 @@ extern "C" {
      *  bcf_get_format_*() - same as bcf_get_info*() above
      *
      *  The function bcf_get_format_string() is a higher-level (slower) variant of bcf_get_format_char().
-     *  see the description of bcf_update_format_string() and bcf_update_format_char() above. 
+     *  see the description of bcf_update_format_string() and bcf_update_format_char() above.
      *  Unlike other bcf_get_format__*() functions, bcf_get_format_string() allocates two arrays:
      *  a single block of \0-terminated strings collapsed into a single array and an array of pointers
-     *  to these strings. Both arrays must be cleaned by the user. 
+     *  to these strings. Both arrays must be cleaned by the user.
      *
      *  Returns negative value on error or the number of written values on success.
      *
@@ -585,7 +642,7 @@ extern "C" {
      *          for (i=0; i<bcf_hdr_nsamples(hdr); i++) printf("%s\n", dst[i]);
      *      free(dst[0]); free(dst);
      *
-     *  Example: 
+     *  Example:
      *      int ngt, *gt_arr = NULL, ngt_arr = 0;
      *      ngt = bcf_get_genotypes(hdr, line, &gt_arr, &ngt_arr);
      */
@@ -597,11 +654,11 @@ extern "C" {
     int bcf_get_format_values(const bcf_hdr_t *hdr, bcf1_t *line, const char *tag, void **dst, int *ndst, int type);
 
 
-	
-	/**************************************************************************
-	 *  Helper functions
-	 **************************************************************************/
- 
+
+    /**************************************************************************
+     *  Helper functions
+     **************************************************************************/
+
     /**
      *  bcf_hdr_id2int() - Translates string into numeric ID
      *  bcf_hdr_int2id() - Translates numeric ID into string
@@ -611,24 +668,24 @@ extern "C" {
      *  Returns -1 if string is not in dictionary, otherwise numeric ID which identifies
      *  fields in BCF records.
      */
-	int bcf_hdr_id2int(const bcf_hdr_t *hdr, int type, const char *id);
+    int bcf_hdr_id2int(const bcf_hdr_t *hdr, int type, const char *id);
     #define bcf_hdr_int2id(hdr,type,int_id) ((hdr)->id[type][int_id].key)
 
     /**
      *  bcf_hdr_name2id() - Translates sequence names (chromosomes) into numeric ID
      *  bcf_hdr_id2name() - Translates numeric ID to sequence name
      */
-	static inline int bcf_hdr_name2id(const bcf_hdr_t *hdr, const char *id) { return bcf_hdr_id2int(hdr, BCF_DT_CTG, id); }
+    static inline int bcf_hdr_name2id(const bcf_hdr_t *hdr, const char *id) { return bcf_hdr_id2int(hdr, BCF_DT_CTG, id); }
     static inline const char *bcf_hdr_id2name(const bcf_hdr_t *hdr, int rid) { return hdr->id[BCF_DT_CTG][rid].key; }
     static inline const char *bcf_seqname(const bcf_hdr_t *hdr, bcf1_t *rec) { return hdr->id[BCF_DT_CTG][rec->rid].key; }
 
     /**
-     *  bcf_hdr_id2*() - Macros for accessing bcf_idinfo_t 
+     *  bcf_hdr_id2*() - Macros for accessing bcf_idinfo_t
      *  @type:      one of BCF_HL_FLT, BCF_HL_INFO, BCF_HL_FMT
      *  @int_id:    return value of bcf_id2int, must be >=0
      *
      *  The returned values are:
-     *     bcf_hdr_id2length   ..  whether the number of values is fixed or variable, one of BCF_VL_* 
+     *     bcf_hdr_id2length   ..  whether the number of values is fixed or variable, one of BCF_VL_*
      *     bcf_hdr_id2number   ..  the number of values, 0xfffff for variable length fields
      *     bcf_hdr_id2type     ..  the field type, one of BCF_HT_*
      *     bcf_hdr_id2coltype  ..  the column type, one of BCF_HL_*
@@ -641,32 +698,32 @@ extern "C" {
     #define bcf_hdr_id2type(hdr,type,int_id)    ((hdr)->id[BCF_DT_ID][int_id].val->info[type]>>4 & 0xf)
     #define bcf_hdr_id2coltype(hdr,type,int_id) ((hdr)->id[BCF_DT_ID][int_id].val->info[type] & 0xf)
     #define bcf_hdr_idinfo_exists(hdr,type,int_id)  ((int_id<0 || bcf_hdr_id2coltype(hdr,type,int_id)==0xf) ? 0 : 1)
-    #define bcf_hdr_id2hrec(hdr,type,int_id)    ((hdr)->id[(type)==BCF_DT_CTG?BCF_DT_CTG:BCF_DT_ID][int_id].val->hrec[(type)==BCF_DT_CTG?0:type])
-    
-	void bcf_fmt_array(kstring_t *s, int n, int type, void *data);
-	uint8_t *bcf_fmt_sized_array(kstring_t *s, uint8_t *ptr);
-
-	void bcf_enc_vchar(kstring_t *s, int l, const char *a);
-	void bcf_enc_vint(kstring_t *s, int n, int32_t *a, int wsize);
-	void bcf_enc_vfloat(kstring_t *s, int n, float *a);
-
- 
-	/**************************************************************************
-	 *  BCF index
+    #define bcf_hdr_id2hrec(hdr,dict_type,col_type,int_id)    ((hdr)->id[(dict_type)==BCF_DT_CTG?BCF_DT_CTG:BCF_DT_ID][int_id].val->hrec[(dict_type)==BCF_DT_CTG?0:(col_type)])
+
+    void bcf_fmt_array(kstring_t *s, int n, int type, void *data);
+    uint8_t *bcf_fmt_sized_array(kstring_t *s, uint8_t *ptr);
+
+    void bcf_enc_vchar(kstring_t *s, int l, const char *a);
+    void bcf_enc_vint(kstring_t *s, int n, int32_t *a, int wsize);
+    void bcf_enc_vfloat(kstring_t *s, int n, float *a);
+
+
+    /**************************************************************************
+     *  BCF index
      *
      *  Note that these functions work with BCFs only. See synced_bcf_reader.h
      *  which provides (amongst other things) an API to work transparently with
      *  both indexed BCFs and VCFs.
-	 **************************************************************************/
- 
-	#define bcf_itr_destroy(iter) hts_itr_destroy(iter)
-	#define bcf_itr_queryi(idx, tid, beg, end) hts_itr_query((idx), (tid), (beg), (end), bcf_readrec)
-	#define bcf_itr_querys(idx, hdr, s) hts_itr_querys((idx), (s), (hts_name2id_f)(bcf_hdr_name2id), (hdr), hts_itr_query, bcf_readrec)
-	#define bcf_itr_next(htsfp, itr, r) hts_itr_next((htsfp)->fp.bgzf, (itr), (r), 0)
-	#define bcf_index_load(fn) hts_idx_load(fn, HTS_FMT_CSI)
-	#define bcf_index_seqnames(idx, hdr, nptr) hts_idx_seqnames((idx),(nptr),(hts_id2name_f)(bcf_hdr_id2name),(hdr))
+     **************************************************************************/
+
+    #define bcf_itr_destroy(iter) hts_itr_destroy(iter)
+    #define bcf_itr_queryi(idx, tid, beg, end) hts_itr_query((idx), (tid), (beg), (end), bcf_readrec)
+    #define bcf_itr_querys(idx, hdr, s) hts_itr_querys((idx), (s), (hts_name2id_f)(bcf_hdr_name2id), (hdr), hts_itr_query, bcf_readrec)
+    #define bcf_itr_next(htsfp, itr, r) hts_itr_next((htsfp)->fp.bgzf, (itr), (r), 0)
+    #define bcf_index_load(fn) hts_idx_load(fn, HTS_FMT_CSI)
+    #define bcf_index_seqnames(idx, hdr, nptr) hts_idx_seqnames((idx),(nptr),(hts_id2name_f)(bcf_hdr_id2name),(hdr))
 
-	int bcf_index_build(const char *fn, int min_shift);
+    int bcf_index_build(const char *fn, int min_shift);
 
 #ifdef __cplusplus
 }
@@ -682,7 +739,7 @@ extern "C" {
     0x8000, 0x80000000 are interpreted as missing values and 0x81, 0x8001,
     0x80000001 as end-of-vector indicators.  Similarly for floats, the value of
     0x7F800001 is interpreted as a missing value and 0x7F800002 as an
-    end-of-vector indicator. 
+    end-of-vector indicator.
     Note that the end-of-vector byte is not part of the vector.
 
     This trial BCF version (v2.2) is compatible with the VCF specification and
@@ -744,80 +801,80 @@ static inline void bcf_format_gt(bcf_fmt_t *fmt, int isample, kstring_t *str)
 
 static inline void bcf_enc_size(kstring_t *s, int size, int type)
 {
-	if (size >= 15) {
-		kputc(15<<4|type, s);
-		if (size >= 128) {
-			if (size >= 32768) {
-				int32_t x = size;
-				kputc(1<<4|BCF_BT_INT32, s);
-				kputsn((char*)&x, 4, s);
-			} else {
-				int16_t x = size;
-				kputc(1<<4|BCF_BT_INT16, s);
-				kputsn((char*)&x, 2, s);
-			}
-		} else {
-			kputc(1<<4|BCF_BT_INT8, s);
-			kputc(size, s);
-		}
-	} else kputc(size<<4|type, s);
+    if (size >= 15) {
+        kputc(15<<4|type, s);
+        if (size >= 128) {
+            if (size >= 32768) {
+                int32_t x = size;
+                kputc(1<<4|BCF_BT_INT32, s);
+                kputsn((char*)&x, 4, s);
+            } else {
+                int16_t x = size;
+                kputc(1<<4|BCF_BT_INT16, s);
+                kputsn((char*)&x, 2, s);
+            }
+        } else {
+            kputc(1<<4|BCF_BT_INT8, s);
+            kputc(size, s);
+        }
+    } else kputc(size<<4|type, s);
 }
 
 static inline int bcf_enc_inttype(long x)
 {
-	if (x <= INT8_MAX && x > bcf_int8_missing) return BCF_BT_INT8;
-	if (x <= INT16_MAX && x > bcf_int16_missing) return BCF_BT_INT16;
-	return BCF_BT_INT32;
+    if (x <= INT8_MAX && x > bcf_int8_missing) return BCF_BT_INT8;
+    if (x <= INT16_MAX && x > bcf_int16_missing) return BCF_BT_INT16;
+    return BCF_BT_INT32;
 }
 
 static inline void bcf_enc_int1(kstring_t *s, int32_t x)
 {
-	if (x == bcf_int32_vector_end) {
-		bcf_enc_size(s, 1, BCF_BT_INT8);
-		kputc(bcf_int8_vector_end, s);
-	} else if (x == bcf_int32_missing) {
-		bcf_enc_size(s, 1, BCF_BT_INT8);
-		kputc(bcf_int8_missing, s);
-	} else if (x <= INT8_MAX && x > bcf_int8_missing) {
-		bcf_enc_size(s, 1, BCF_BT_INT8);
-		kputc(x, s);
-	} else if (x <= INT16_MAX && x > bcf_int16_missing) {
-		int16_t z = x;
-		bcf_enc_size(s, 1, BCF_BT_INT16);
-		kputsn((char*)&z, 2, s);
-	} else {
-		int32_t z = x;
-		bcf_enc_size(s, 1, BCF_BT_INT32);
-		kputsn((char*)&z, 4, s);
-	}
+    if (x == bcf_int32_vector_end) {
+        bcf_enc_size(s, 1, BCF_BT_INT8);
+        kputc(bcf_int8_vector_end, s);
+    } else if (x == bcf_int32_missing) {
+        bcf_enc_size(s, 1, BCF_BT_INT8);
+        kputc(bcf_int8_missing, s);
+    } else if (x <= INT8_MAX && x > bcf_int8_missing) {
+        bcf_enc_size(s, 1, BCF_BT_INT8);
+        kputc(x, s);
+    } else if (x <= INT16_MAX && x > bcf_int16_missing) {
+        int16_t z = x;
+        bcf_enc_size(s, 1, BCF_BT_INT16);
+        kputsn((char*)&z, 2, s);
+    } else {
+        int32_t z = x;
+        bcf_enc_size(s, 1, BCF_BT_INT32);
+        kputsn((char*)&z, 4, s);
+    }
 }
 
 static inline int32_t bcf_dec_int1(const uint8_t *p, int type, uint8_t **q)
 {
-	if (type == BCF_BT_INT8) {
-		*q = (uint8_t*)p + 1;
-		return *(int8_t*)p;
-	} else if (type == BCF_BT_INT16) {
-		*q = (uint8_t*)p + 2;
-		return *(int16_t*)p;
-	} else {
-		*q = (uint8_t*)p + 4;
-		return *(int32_t*)p;
-	}
+    if (type == BCF_BT_INT8) {
+        *q = (uint8_t*)p + 1;
+        return *(int8_t*)p;
+    } else if (type == BCF_BT_INT16) {
+        *q = (uint8_t*)p + 2;
+        return *(int16_t*)p;
+    } else {
+        *q = (uint8_t*)p + 4;
+        return *(int32_t*)p;
+    }
 }
 
 static inline int32_t bcf_dec_typed_int1(const uint8_t *p, uint8_t **q)
 {
-	return bcf_dec_int1(p + 1, *p&0xf, q);
+    return bcf_dec_int1(p + 1, *p&0xf, q);
 }
 
 static inline int32_t bcf_dec_size(const uint8_t *p, uint8_t **q, int *type)
 {
-	*type = *p & 0xf;
-	if (*p>>4 != 15) {
-		*q = (uint8_t*)p + 1;
-		return *p>>4;
-	} else return bcf_dec_typed_int1(p + 1, q);
+    *type = *p & 0xf;
+    if (*p>>4 != 15) {
+        *q = (uint8_t*)p + 1;
+        return *p>>4;
+    } else return bcf_dec_typed_int1(p + 1, q);
 }
 
 #endif
diff --git a/htslib/htslib/vcf_sweep.h b/htslib/htslib/vcf_sweep.h
index 9d21d8a..9f295ee 100644
--- a/htslib/htslib/vcf_sweep.h
+++ b/htslib/htslib/vcf_sweep.h
@@ -1,5 +1,29 @@
-#ifndef __VCF_SWEEP_H__
-#define __VCF_SWEEP_H__
+/*  vcf_sweep.h -- forward/reverse sweep API.
+
+    Copyright (C) 2013 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#ifndef HTSLIB_VCF_SWEEP_H
+#define HTSLIB_VCF_SWEEP_H
 
 #include "hts.h"
 #include "vcf.h"
diff --git a/htslib/htslib/vcfutils.h b/htslib/htslib/vcfutils.h
index 18b9503..dc10395 100644
--- a/htslib/htslib/vcfutils.h
+++ b/htslib/htslib/vcfutils.h
@@ -1,9 +1,29 @@
-/*
-    Time will show if this module will be merged into others 
-    or perhaps removed completely.
-*/
-#ifndef VCF_UTILS_H
-#define VCF_UTILS_H
+/*  vcfutils.h -- allele-related utility functions.
+
+    Copyright (C) 2012, 2013 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#ifndef HTSLIB_VCFUTILS_H
+#define HTSLIB_VCFUTILS_H
 
 #include "vcf.h"
 
@@ -13,9 +33,9 @@
  *  @header:  for access to BCF_DT_ID dictionary
  *  @line:    VCF line obtain from vcf_parse1
  *
- *  Returns the number of removed alleles.
- *
- *  todo: BCF output
+ *  Returns the number of removed alleles on success or negative
+ *  on error:
+ *      -1 .. some allele index is out of bounds
  */
 int bcf_trim_alleles(const bcf_hdr_t *header, bcf1_t *line);
 
@@ -25,8 +45,6 @@ int bcf_trim_alleles(const bcf_hdr_t *header, bcf1_t *line);
  *  @header:  for access to BCF_DT_ID dictionary
  *  @line:    VCF line obtained from vcf_parse1
  *  @mask:    alleles to remove
- *
- *  todo: BCF output
  */
 void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int mask);
 
@@ -38,12 +56,12 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int mask);
  *  @ac:      array of length line->n_allele
  *  @which:   determine if INFO/AN,AC and indv fields be used
  *
- *  Returns 1 if the call succeeded, or 0 if the value could not 
+ *  Returns 1 if the call succeeded, or 0 if the value could not
  *  be determined.
  *
- *  The value of @which determines if existing INFO/AC,AN can be 
- *  used (BCF_UN_INFO) and and if indv fields can be splitted 
- *  (BCF_UN_FMT). 
+ *  The value of @which determines if existing INFO/AC,AN can be
+ *  used (BCF_UN_INFO) and and if indv fields can be splitted
+ *  (BCF_UN_FMT).
  */
 int bcf_calc_ac(const bcf_hdr_t *header, bcf1_t *line, int *ac, int which);
 
@@ -56,11 +74,11 @@ int bcf_calc_ac(const bcf_hdr_t *header, bcf1_t *line, int *ac, int which);
  * @jal:      index of the 2nd non-reference allele (starting from 1)
  *
  * Returns the type of the genotype (one of GT_HOM_RR, GT_HET_RA,
- * GT_HOM_AA, GT_HET_AA, GT_HAPL_R, GT_HAPL_A or GT_UNKN). If $ial 
- * is not NULL and the genotype has one or more non-reference 
- * alleles, $ial will be set. In case of GT_HET_AA, $ial is the 
- * position of the allele which appeared first in ALT. If $jal is 
- * not null and the genotype is GT_HET_AA, $jal will be set and is 
+ * GT_HOM_AA, GT_HET_AA, GT_HAPL_R, GT_HAPL_A or GT_UNKN). If $ial
+ * is not NULL and the genotype has one or more non-reference
+ * alleles, $ial will be set. In case of GT_HET_AA, $ial is the
+ * position of the allele which appeared first in ALT. If $jal is
+ * not null and the genotype is GT_HET_AA, $jal will be set and is
  * the position of the second allele in ALT.
  */
 #define GT_HOM_RR 0 // note: the actual value of GT_* matters, used in dosage r2 calculation
@@ -86,7 +104,7 @@ static inline int bcf_acgt2int(char c)
 /**
   * bcf_ij2G() - common task: allele indexes to Number=G index (diploid)
   * @i,j:  allele indexes, 0-based, i<=j
-  * 
+  *
   * Returns index to the Number=G diploid array
   */
 #define bcf_ij2G(i, j) ((j)*((j)+1)/2+(i))
diff --git a/htslib/kfunc.c b/htslib/kfunc.c
index 10c0973..faa2ac5 100644
--- a/htslib/kfunc.c
+++ b/htslib/kfunc.c
@@ -1,3 +1,29 @@
+/* The MIT License
+
+   Copyright (C) 2010, 2013 Genome Research Ltd.
+   Copyright (C) 2011 Attractive Chaos <attractor at live.co.uk>
+
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   "Software"), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+
+   The above copyright notice and this permission notice shall be
+   included in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+   SOFTWARE.
+*/
+
 #include <math.h>
 #include <stdlib.h>
 #include "htslib/kfunc.h"
diff --git a/htslib/knetfile.c b/htslib/knetfile.c
index 64b3fa6..400da4f 100644
--- a/htslib/knetfile.c
+++ b/htslib/knetfile.c
@@ -87,7 +87,7 @@ static int socket_connect(const char *host, const char *port)
 {
 #define __err_connect(func) do { perror(func); freeaddrinfo(res); return -1; } while (0)
 
-	int on = 1, fd;
+	int ai_err, on = 1, fd;
 	struct linger lng = { 0, 0 };
 	struct addrinfo hints, *res = 0;
 	memset(&hints, 0, sizeof(struct addrinfo));
@@ -95,7 +95,7 @@ static int socket_connect(const char *host, const char *port)
 	hints.ai_socktype = SOCK_STREAM;
 	/* In Unix/Mac, getaddrinfo() is the most convenient way to get
 	 * server information. */
-	if (getaddrinfo(host, port, &hints, &res) != 0) __err_connect("getaddrinfo");
+	if ((ai_err = getaddrinfo(host, port, &hints, &res)) != 0) { fprintf(stderr, "can't resolve %s:%s: %s\n", host, port, gai_strerror(ai_err)); return -1; }
 	if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) __err_connect("socket");
 	/* The following two setsockopt() are used by ftplib
 	 * (http://nbpfaus.net/~pfau/ftplib/). I am not sure if they
@@ -434,9 +434,20 @@ int khttp_connect_file(knetFile *fp)
 			rest -= my_netread(fp->fd, buf, l);
 		}
 	} else if (ret != 206 && ret != 200) {
+		// failed to open file
 		free(buf);
-		fprintf(stderr, "[khttp_connect_file] fail to open file (HTTP code: %d).\n", ret);
 		netclose(fp->fd);
+		switch (ret) {
+		case 401: errno = EPERM; break;
+		case 403: errno = EACCES; break;
+		case 404: errno = ENOENT; break;
+		case 407: errno = EPERM; break;
+		case 408: errno = ETIMEDOUT; break;
+		case 410: errno = ENOENT; break;
+		case 503: errno = EAGAIN; break;
+		case 504: errno = ETIMEDOUT; break;
+		default:  errno = (ret >= 400 && ret < 500)? EINVAL : EIO; break;
+		}
 		fp->fd = -1;
 		return -1;
 	}
diff --git a/htslib/kstring.c b/htslib/kstring.c
index b4202f5..0128266 100644
--- a/htslib/kstring.c
+++ b/htslib/kstring.c
@@ -1,3 +1,28 @@
+/* The MIT License
+
+   Copyright (C) 2011 by Attractive Chaos <attractor at live.co.uk>
+
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   "Software"), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+
+   The above copyright notice and this permission notice shall be
+   included in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+   SOFTWARE.
+*/
+
 #include <stdarg.h>
 #include <stdio.h>
 #include <ctype.h>
diff --git a/htslib/sam.c b/htslib/sam.c
index ae4b39b..d85b85b 100644
--- a/htslib/sam.c
+++ b/htslib/sam.c
@@ -1,3 +1,28 @@
+/*  sam.c -- SAM and BAM file I/O and manipulation.
+
+    Copyright (C) 2008-2010, 2012-2014 Genome Research Ltd.
+    Copyright (C) 2010, 2012, 2013 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -20,160 +45,160 @@ typedef khash_t(s2i) sdict_t;
 
 bam_hdr_t *bam_hdr_init()
 {
-	return (bam_hdr_t*)calloc(1, sizeof(bam_hdr_t));
+    return (bam_hdr_t*)calloc(1, sizeof(bam_hdr_t));
 }
 
 void bam_hdr_destroy(bam_hdr_t *h)
 {
-	int32_t i;
-	if (h == NULL) return;
-	if (h->target_name) {
-		for (i = 0; i < h->n_targets; ++i)
-			free(h->target_name[i]);
-		free(h->target_name);
-		free(h->target_len);
-	}
-	free(h->text); free(h->cigar_tab);
-	if (h->sdict) kh_destroy(s2i, (sdict_t*)h->sdict);
-	free(h);
+    int32_t i;
+    if (h == NULL) return;
+    if (h->target_name) {
+        for (i = 0; i < h->n_targets; ++i)
+            free(h->target_name[i]);
+        free(h->target_name);
+        free(h->target_len);
+    }
+    free(h->text); free(h->cigar_tab);
+    if (h->sdict) kh_destroy(s2i, (sdict_t*)h->sdict);
+    free(h);
 }
 
 bam_hdr_t *bam_hdr_dup(const bam_hdr_t *h0)
 {
-	if (h0 == NULL) return NULL;
-	bam_hdr_t *h;
-	if ((h = bam_hdr_init()) == NULL) return NULL;
-	// copy the simple data
-	h->n_targets = h0->n_targets;
-	h->ignore_sam_err = h0->ignore_sam_err;
-	h->l_text = h0->l_text;
-	// Then the pointery stuff
-	h->cigar_tab = NULL;
-	h->sdict = NULL;
-	h->text = (char*)calloc(h->l_text + 1, 1);
-	memcpy(h->text, h0->text, h->l_text);
-	h->target_len = (uint32_t*)calloc(h->n_targets, 4);
-	h->target_name = (char**)calloc(h->n_targets, sizeof(char*));
-	int i;
-	for (i = 0; i < h->n_targets; ++i) {
-		h->target_len[i] = h0->target_len[i];
-		h->target_name[i] = strdup(h0->target_name[i]);
-	}
-	return h;
+    if (h0 == NULL) return NULL;
+    bam_hdr_t *h;
+    if ((h = bam_hdr_init()) == NULL) return NULL;
+    // copy the simple data
+    h->n_targets = h0->n_targets;
+    h->ignore_sam_err = h0->ignore_sam_err;
+    h->l_text = h0->l_text;
+    // Then the pointery stuff
+    h->cigar_tab = NULL;
+    h->sdict = NULL;
+    h->text = (char*)calloc(h->l_text + 1, 1);
+    memcpy(h->text, h0->text, h->l_text);
+    h->target_len = (uint32_t*)calloc(h->n_targets, 4);
+    h->target_name = (char**)calloc(h->n_targets, sizeof(char*));
+    int i;
+    for (i = 0; i < h->n_targets; ++i) {
+        h->target_len[i] = h0->target_len[i];
+        h->target_name[i] = strdup(h0->target_name[i]);
+    }
+    return h;
 }
 
 
 static bam_hdr_t *hdr_from_dict(sdict_t *d)
 {
-	bam_hdr_t *h;
-	khint_t k;
-	h = bam_hdr_init();
-	h->sdict = d;
-	h->n_targets = kh_size(d);
-	h->target_len = (uint32_t*)malloc(4 * h->n_targets);
-	h->target_name = (char**)malloc(sizeof(char*) * h->n_targets);
-	for (k = kh_begin(d); k != kh_end(d); ++k) {
-		if (!kh_exist(d, k)) continue;
-		h->target_name[kh_val(d, k)>>32] = (char*)kh_key(d, k);
-		h->target_len[kh_val(d, k)>>32]  = kh_val(d, k)<<32>>32;
-		kh_val(d, k) >>= 32;
-	}
-	return h;
+    bam_hdr_t *h;
+    khint_t k;
+    h = bam_hdr_init();
+    h->sdict = d;
+    h->n_targets = kh_size(d);
+    h->target_len = (uint32_t*)malloc(4 * h->n_targets);
+    h->target_name = (char**)malloc(sizeof(char*) * h->n_targets);
+    for (k = kh_begin(d); k != kh_end(d); ++k) {
+        if (!kh_exist(d, k)) continue;
+        h->target_name[kh_val(d, k)>>32] = (char*)kh_key(d, k);
+        h->target_len[kh_val(d, k)>>32]  = kh_val(d, k)<<32>>32;
+        kh_val(d, k) >>= 32;
+    }
+    return h;
 }
 
 bam_hdr_t *bam_hdr_read(BGZF *fp)
 {
-	bam_hdr_t *h;
-	char buf[4];
-	int magic_len, has_EOF;
-	int32_t i = 1, name_len;
-	// check EOF
-	has_EOF = bgzf_check_EOF(fp);
-	if (has_EOF < 0) {
-		perror("[W::sam_hdr_read] bgzf_check_EOF");
-	} else if (has_EOF == 0 && hts_verbose >= 2)
-		fprintf(stderr, "[W::%s] EOF marker is absent. The input is probably truncated.\n", __func__);
-	// read "BAM1"
-	magic_len = bgzf_read(fp, buf, 4);
-	if (magic_len != 4 || strncmp(buf, "BAM\1", 4)) {
-		if (hts_verbose >= 1) fprintf(stderr, "[E::%s] invalid BAM binary header\n", __func__);
-		return 0;
-	}
-	h = bam_hdr_init();
-	// read plain text and the number of reference sequences
-	bgzf_read(fp, &h->l_text, 4);
-	if (fp->is_be) ed_swap_4p(&h->l_text);
-	h->text = (char*)malloc(h->l_text + 1);
-	h->text[h->l_text] = 0; // make sure it is NULL terminated
-	bgzf_read(fp, h->text, h->l_text);
-	bgzf_read(fp, &h->n_targets, 4);
-	if (fp->is_be) ed_swap_4p(&h->n_targets);
-	// read reference sequence names and lengths
-	h->target_name = (char**)calloc(h->n_targets, sizeof(char*));
-	h->target_len = (uint32_t*)calloc(h->n_targets, 4);
-	for (i = 0; i != h->n_targets; ++i) {
-		bgzf_read(fp, &name_len, 4);
-		if (fp->is_be) ed_swap_4p(&name_len);
-		h->target_name[i] = (char*)calloc(name_len, 1);
-		bgzf_read(fp, h->target_name[i], name_len);
-		bgzf_read(fp, &h->target_len[i], 4);
-		if (fp->is_be) ed_swap_4p(&h->target_len[i]);
-	}
-	return h;
+    bam_hdr_t *h;
+    char buf[4];
+    int magic_len, has_EOF;
+    int32_t i = 1, name_len;
+    // check EOF
+    has_EOF = bgzf_check_EOF(fp);
+    if (has_EOF < 0) {
+        perror("[W::sam_hdr_read] bgzf_check_EOF");
+    } else if (has_EOF == 0 && hts_verbose >= 2)
+        fprintf(stderr, "[W::%s] EOF marker is absent. The input is probably truncated.\n", __func__);
+    // read "BAM1"
+    magic_len = bgzf_read(fp, buf, 4);
+    if (magic_len != 4 || strncmp(buf, "BAM\1", 4)) {
+        if (hts_verbose >= 1) fprintf(stderr, "[E::%s] invalid BAM binary header\n", __func__);
+        return 0;
+    }
+    h = bam_hdr_init();
+    // read plain text and the number of reference sequences
+    bgzf_read(fp, &h->l_text, 4);
+    if (fp->is_be) ed_swap_4p(&h->l_text);
+    h->text = (char*)malloc(h->l_text + 1);
+    h->text[h->l_text] = 0; // make sure it is NULL terminated
+    bgzf_read(fp, h->text, h->l_text);
+    bgzf_read(fp, &h->n_targets, 4);
+    if (fp->is_be) ed_swap_4p(&h->n_targets);
+    // read reference sequence names and lengths
+    h->target_name = (char**)calloc(h->n_targets, sizeof(char*));
+    h->target_len = (uint32_t*)calloc(h->n_targets, 4);
+    for (i = 0; i != h->n_targets; ++i) {
+        bgzf_read(fp, &name_len, 4);
+        if (fp->is_be) ed_swap_4p(&name_len);
+        h->target_name[i] = (char*)calloc(name_len, 1);
+        bgzf_read(fp, h->target_name[i], name_len);
+        bgzf_read(fp, &h->target_len[i], 4);
+        if (fp->is_be) ed_swap_4p(&h->target_len[i]);
+    }
+    return h;
 }
 
 int bam_hdr_write(BGZF *fp, const bam_hdr_t *h)
 {
-	char buf[4];
-	int32_t i, name_len, x;
-	// write "BAM1"
-	strncpy(buf, "BAM\1", 4);
-	bgzf_write(fp, buf, 4);
-	// write plain text and the number of reference sequences
-	if (fp->is_be) {
-		x = ed_swap_4(h->l_text);
-		bgzf_write(fp, &x, 4);
-		if (h->l_text) bgzf_write(fp, h->text, h->l_text);
-		x = ed_swap_4(h->n_targets);
-		bgzf_write(fp, &x, 4);
-	} else {
-		bgzf_write(fp, &h->l_text, 4);
-		if (h->l_text) bgzf_write(fp, h->text, h->l_text);
-		bgzf_write(fp, &h->n_targets, 4);
-	}
-	// write sequence names and lengths
-	for (i = 0; i != h->n_targets; ++i) {
-		char *p = h->target_name[i];
-		name_len = strlen(p) + 1;
-		if (fp->is_be) {
-			x = ed_swap_4(name_len);
-			bgzf_write(fp, &x, 4);
-		} else bgzf_write(fp, &name_len, 4);
-		bgzf_write(fp, p, name_len);
-		if (fp->is_be) {
-			x = ed_swap_4(h->target_len[i]);
-			bgzf_write(fp, &x, 4);
-		} else bgzf_write(fp, &h->target_len[i], 4);
-	}
-	bgzf_flush(fp);
-	return 0;
+    char buf[4];
+    int32_t i, name_len, x;
+    // write "BAM1"
+    strncpy(buf, "BAM\1", 4);
+    bgzf_write(fp, buf, 4);
+    // write plain text and the number of reference sequences
+    if (fp->is_be) {
+        x = ed_swap_4(h->l_text);
+        bgzf_write(fp, &x, 4);
+        if (h->l_text) bgzf_write(fp, h->text, h->l_text);
+        x = ed_swap_4(h->n_targets);
+        bgzf_write(fp, &x, 4);
+    } else {
+        bgzf_write(fp, &h->l_text, 4);
+        if (h->l_text) bgzf_write(fp, h->text, h->l_text);
+        bgzf_write(fp, &h->n_targets, 4);
+    }
+    // write sequence names and lengths
+    for (i = 0; i != h->n_targets; ++i) {
+        char *p = h->target_name[i];
+        name_len = strlen(p) + 1;
+        if (fp->is_be) {
+            x = ed_swap_4(name_len);
+            bgzf_write(fp, &x, 4);
+        } else bgzf_write(fp, &name_len, 4);
+        bgzf_write(fp, p, name_len);
+        if (fp->is_be) {
+            x = ed_swap_4(h->target_len[i]);
+            bgzf_write(fp, &x, 4);
+        } else bgzf_write(fp, &h->target_len[i], 4);
+    }
+    bgzf_flush(fp);
+    return 0;
 }
 
 int bam_name2id(bam_hdr_t *h, const char *ref)
 {
-	sdict_t *d = (sdict_t*)h->sdict;
-	khint_t k;
-	if (h->sdict == 0) {
-		int i, absent;
-		d = kh_init(s2i);
-		for (i = 0; i < h->n_targets; ++i) {
-			k = kh_put(s2i, d, h->target_name[i], &absent);
-			kh_val(d, k) = i;
-		}
-		h->sdict = d;
-	}
-	k = kh_get(s2i, d, ref);
-	return k == kh_end(d)? -1 : kh_val(d, k);
+    sdict_t *d = (sdict_t*)h->sdict;
+    khint_t k;
+    if (h->sdict == 0) {
+        int i, absent;
+        d = kh_init(s2i);
+        for (i = 0; i < h->n_targets; ++i) {
+            k = kh_put(s2i, d, h->target_name[i], &absent);
+            kh_val(d, k) = i;
+        }
+        h->sdict = d;
+    }
+    k = kh_get(s2i, d, ref);
+    return k == kh_end(d)? -1 : kh_val(d, k);
 }
 
 /*************************
@@ -182,182 +207,182 @@ int bam_name2id(bam_hdr_t *h, const char *ref)
 
 bam1_t *bam_init1()
 {
-	return (bam1_t*)calloc(1, sizeof(bam1_t));
+    return (bam1_t*)calloc(1, sizeof(bam1_t));
 }
 
 void bam_destroy1(bam1_t *b)
 {
-	if (b == 0) return;
-	free(b->data); free(b);
+    if (b == 0) return;
+    free(b->data); free(b);
 }
 
 bam1_t *bam_copy1(bam1_t *bdst, const bam1_t *bsrc)
 {
-	uint8_t *data = bdst->data;
-	int m_data = bdst->m_data;   // backup data and m_data
-	if (m_data < bsrc->l_data) { // double the capacity
-		m_data = bsrc->l_data; kroundup32(m_data);
-		data = (uint8_t*)realloc(data, m_data);
-	}
-	memcpy(data, bsrc->data, bsrc->l_data); // copy var-len data
-	*bdst = *bsrc; // copy the rest
-	// restore the backup
-	bdst->m_data = m_data;
-	bdst->data = data;
-	return bdst;
+    uint8_t *data = bdst->data;
+    int m_data = bdst->m_data;   // backup data and m_data
+    if (m_data < bsrc->l_data) { // double the capacity
+        m_data = bsrc->l_data; kroundup32(m_data);
+        data = (uint8_t*)realloc(data, m_data);
+    }
+    memcpy(data, bsrc->data, bsrc->l_data); // copy var-len data
+    *bdst = *bsrc; // copy the rest
+    // restore the backup
+    bdst->m_data = m_data;
+    bdst->data = data;
+    return bdst;
 }
 
 bam1_t *bam_dup1(const bam1_t *bsrc)
 {
-	if (bsrc == NULL) return NULL;
-	bam1_t *bdst = bam_init1();
-	if (bdst == NULL) return NULL;
-	return bam_copy1(bdst, bsrc);
+    if (bsrc == NULL) return NULL;
+    bam1_t *bdst = bam_init1();
+    if (bdst == NULL) return NULL;
+    return bam_copy1(bdst, bsrc);
 }
 
 int bam_cigar2qlen(int n_cigar, const uint32_t *cigar)
 {
-	int k, l;
-	for (k = l = 0; k < n_cigar; ++k)
-		if (bam_cigar_type(bam_cigar_op(cigar[k]))&1)
-			l += bam_cigar_oplen(cigar[k]);
-	return l;
+    int k, l;
+    for (k = l = 0; k < n_cigar; ++k)
+        if (bam_cigar_type(bam_cigar_op(cigar[k]))&1)
+            l += bam_cigar_oplen(cigar[k]);
+    return l;
 }
 
 int bam_cigar2rlen(int n_cigar, const uint32_t *cigar)
 {
-	int k, l;
-	for (k = l = 0; k < n_cigar; ++k)
-		if (bam_cigar_type(bam_cigar_op(cigar[k]))&2)
-			l += bam_cigar_oplen(cigar[k]);
-	return l;
+    int k, l;
+    for (k = l = 0; k < n_cigar; ++k)
+        if (bam_cigar_type(bam_cigar_op(cigar[k]))&2)
+            l += bam_cigar_oplen(cigar[k]);
+    return l;
 }
 
 int32_t bam_endpos(const bam1_t *b)
 {
-	if (!(b->core.flag & BAM_FUNMAP) && b->core.n_cigar > 0)
-		return b->core.pos + bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b));
-	else
-		return b->core.pos + 1;
+    if (!(b->core.flag & BAM_FUNMAP) && b->core.n_cigar > 0)
+        return b->core.pos + bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b));
+    else
+        return b->core.pos + 1;
 }
 
 static inline int aux_type2size(uint8_t type)
 {
-	switch (type) {
-	case 'A': case 'c': case 'C':
-		return 1;
-	case 's': case 'S':
-		return 2;
-	case 'i': case 'I': case 'f':
-		return 4;
-	case 'd':
-		return 8;
-	case 'Z': case 'H': case 'B':
-		return type;
-	default:
-		return 0;
-	}
+    switch (type) {
+    case 'A': case 'c': case 'C':
+        return 1;
+    case 's': case 'S':
+        return 2;
+    case 'i': case 'I': case 'f':
+        return 4;
+    case 'd':
+        return 8;
+    case 'Z': case 'H': case 'B':
+        return type;
+    default:
+        return 0;
+    }
 }
 
 static void swap_data(const bam1_core_t *c, int l_data, uint8_t *data, int is_host)
 {
-	uint8_t *s;
-	uint32_t *cigar = (uint32_t*)(data + c->l_qname);
-	uint32_t i, n;
-	s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
-	for (i = 0; i < c->n_cigar; ++i) ed_swap_4p(&cigar[i]);
-	while (s < data + l_data) {
-		int size;
-		s += 2; // skip key
-		size = aux_type2size(*s); ++s; // skip type
-		switch (size) {
-		case 1: ++s; break;
-		case 2: ed_swap_2p(s); s += 2; break;
-		case 4: ed_swap_4p(s); s += 4; break;
-		case 8: ed_swap_8p(s); s += 8; break;
-		case 'Z':
-		case 'H':
-			while (*s) ++s;
-			++s;
-			break;
-		case 'B':
-			size = aux_type2size(*s); ++s;
-			if (is_host) memcpy(&n, s, 4), ed_swap_4p(s);
-			else ed_swap_4p(s), memcpy(&n, s, 4);
-			s += 4;
-			switch (size) {
-			case 1: s += n; break;
-			case 2: for (i = 0; i < n; ++i, s += 2) ed_swap_2p(s); break;
-			case 4: for (i = 0; i < n; ++i, s += 4) ed_swap_4p(s); break;
-			case 8: for (i = 0; i < n; ++i, s += 8) ed_swap_8p(s); break;
-			}
-			break;
-		}
-	}
+    uint8_t *s;
+    uint32_t *cigar = (uint32_t*)(data + c->l_qname);
+    uint32_t i, n;
+    s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
+    for (i = 0; i < c->n_cigar; ++i) ed_swap_4p(&cigar[i]);
+    while (s < data + l_data) {
+        int size;
+        s += 2; // skip key
+        size = aux_type2size(*s); ++s; // skip type
+        switch (size) {
+        case 1: ++s; break;
+        case 2: ed_swap_2p(s); s += 2; break;
+        case 4: ed_swap_4p(s); s += 4; break;
+        case 8: ed_swap_8p(s); s += 8; break;
+        case 'Z':
+        case 'H':
+            while (*s) ++s;
+            ++s;
+            break;
+        case 'B':
+            size = aux_type2size(*s); ++s;
+            if (is_host) memcpy(&n, s, 4), ed_swap_4p(s);
+            else ed_swap_4p(s), memcpy(&n, s, 4);
+            s += 4;
+            switch (size) {
+            case 1: s += n; break;
+            case 2: for (i = 0; i < n; ++i, s += 2) ed_swap_2p(s); break;
+            case 4: for (i = 0; i < n; ++i, s += 4) ed_swap_4p(s); break;
+            case 8: for (i = 0; i < n; ++i, s += 8) ed_swap_8p(s); break;
+            }
+            break;
+        }
+    }
 }
 
 int bam_read1(BGZF *fp, bam1_t *b)
 {
-	bam1_core_t *c = &b->core;
-	int32_t block_len, ret, i;
-	uint32_t x[8];
-	if ((ret = bgzf_read(fp, &block_len, 4)) != 4) {
-		if (ret == 0) return -1; // normal end-of-file
-		else return -2; // truncated
-	}
-	if (bgzf_read(fp, x, 32) != 32) return -3;
-	if (fp->is_be) {
-		ed_swap_4p(&block_len);
-		for (i = 0; i < 8; ++i) ed_swap_4p(x + i);
-	}
-	c->tid = x[0]; c->pos = x[1];
-	c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff;
-	c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff;
-	c->l_qseq = x[4];
-	c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7];
-	b->l_data = block_len - 32;
-	if (b->l_data < 0 || c->l_qseq < 0) return -4;
-	if ((char *)bam_get_aux(b) - (char *)b->data > b->l_data)
-		return -4;
-	if (b->m_data < b->l_data) {
-		b->m_data = b->l_data;
-		kroundup32(b->m_data);
-		b->data = (uint8_t*)realloc(b->data, b->m_data);
-		if (!b->data)
-			return -4;
-	}
-	if (bgzf_read(fp, b->data, b->l_data) != b->l_data) return -4;
-	//b->l_aux = b->l_data - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
-	if (fp->is_be) swap_data(c, b->l_data, b->data, 0);
-	return 4 + block_len;
+    bam1_core_t *c = &b->core;
+    int32_t block_len, ret, i;
+    uint32_t x[8];
+    if ((ret = bgzf_read(fp, &block_len, 4)) != 4) {
+        if (ret == 0) return -1; // normal end-of-file
+        else return -2; // truncated
+    }
+    if (bgzf_read(fp, x, 32) != 32) return -3;
+    if (fp->is_be) {
+        ed_swap_4p(&block_len);
+        for (i = 0; i < 8; ++i) ed_swap_4p(x + i);
+    }
+    c->tid = x[0]; c->pos = x[1];
+    c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff;
+    c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff;
+    c->l_qseq = x[4];
+    c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7];
+    b->l_data = block_len - 32;
+    if (b->l_data < 0 || c->l_qseq < 0) return -4;
+    if ((char *)bam_get_aux(b) - (char *)b->data > b->l_data)
+        return -4;
+    if (b->m_data < b->l_data) {
+        b->m_data = b->l_data;
+        kroundup32(b->m_data);
+        b->data = (uint8_t*)realloc(b->data, b->m_data);
+        if (!b->data)
+            return -4;
+    }
+    if (bgzf_read(fp, b->data, b->l_data) != b->l_data) return -4;
+    //b->l_aux = b->l_data - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
+    if (fp->is_be) swap_data(c, b->l_data, b->data, 0);
+    return 4 + block_len;
 }
 
 int bam_write1(BGZF *fp, const bam1_t *b)
 {
-	const bam1_core_t *c = &b->core;
-	uint32_t x[8], block_len = b->l_data + 32, y;
-	int i, ok;
-	x[0] = c->tid;
-	x[1] = c->pos;
-	x[2] = (uint32_t)c->bin<<16 | c->qual<<8 | c->l_qname;
-	x[3] = (uint32_t)c->flag<<16 | c->n_cigar;
-	x[4] = c->l_qseq;
-	x[5] = c->mtid;
-	x[6] = c->mpos;
-	x[7] = c->isize;
-	ok = (bgzf_flush_try(fp, 4 + block_len) >= 0);
-	if (fp->is_be) {
-		for (i = 0; i < 8; ++i) ed_swap_4p(x + i);
-		y = block_len;
-		if (ok) ok = (bgzf_write(fp, ed_swap_4p(&y), 4) >= 0);
-		swap_data(c, b->l_data, b->data, 1);
-	} else {
-		if (ok) ok = (bgzf_write(fp, &block_len, 4) >= 0);
-	}
-	if (ok) ok = (bgzf_write(fp, x, 32) >= 0);
-	if (ok) ok = (bgzf_write(fp, b->data, b->l_data) >= 0);
-	if (fp->is_be) swap_data(c, b->l_data, b->data, 0);
-	return ok? 4 + block_len : -1;
+    const bam1_core_t *c = &b->core;
+    uint32_t x[8], block_len = b->l_data + 32, y;
+    int i, ok;
+    x[0] = c->tid;
+    x[1] = c->pos;
+    x[2] = (uint32_t)c->bin<<16 | c->qual<<8 | c->l_qname;
+    x[3] = (uint32_t)c->flag<<16 | c->n_cigar;
+    x[4] = c->l_qseq;
+    x[5] = c->mtid;
+    x[6] = c->mpos;
+    x[7] = c->isize;
+    ok = (bgzf_flush_try(fp, 4 + block_len) >= 0);
+    if (fp->is_be) {
+        for (i = 0; i < 8; ++i) ed_swap_4p(x + i);
+        y = block_len;
+        if (ok) ok = (bgzf_write(fp, ed_swap_4p(&y), 4) >= 0);
+        swap_data(c, b->l_data, b->data, 1);
+    } else {
+        if (ok) ok = (bgzf_write(fp, &block_len, 4) >= 0);
+    }
+    if (ok) ok = (bgzf_write(fp, x, 32) >= 0);
+    if (ok) ok = (bgzf_write(fp, b->data, b->l_data) >= 0);
+    if (fp->is_be) swap_data(c, b->l_data, b->data, 0);
+    return ok? 4 + block_len : -1;
 }
 
 /********************
@@ -366,96 +391,96 @@ int bam_write1(BGZF *fp, const bam1_t *b)
 
 static hts_idx_t *bam_index(BGZF *fp, int min_shift)
 {
-	int n_lvls, i, fmt;
-	bam1_t *b;
-	hts_idx_t *idx;
-	bam_hdr_t *h;
-	h = bam_hdr_read(fp);
-	if (min_shift > 0) {
-		int64_t max_len = 0, s;
-		for (i = 0; i < h->n_targets; ++i)
-			if (max_len < h->target_len[i]) max_len = h->target_len[i];
-		max_len += 256;
-		for (n_lvls = 0, s = 1<<min_shift; max_len > s; ++n_lvls, s <<= 3);
-		fmt = HTS_FMT_CSI;
-	} else min_shift = 14, n_lvls = 5, fmt = HTS_FMT_BAI;
-	idx = hts_idx_init(h->n_targets, fmt, bgzf_tell(fp), min_shift, n_lvls);
-	bam_hdr_destroy(h);
-	b = bam_init1();
-	while (bam_read1(fp, b) >= 0) {
-		int l, ret;
-		l = bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b));
-		if (l == 0) l = 1; // no zero-length records
-		ret = hts_idx_push(idx, b->core.tid, b->core.pos, b->core.pos + l, bgzf_tell(fp), !(b->core.flag&BAM_FUNMAP));
-		if (ret < 0)
+    int n_lvls, i, fmt;
+    bam1_t *b;
+    hts_idx_t *idx;
+    bam_hdr_t *h;
+    h = bam_hdr_read(fp);
+    if (min_shift > 0) {
+        int64_t max_len = 0, s;
+        for (i = 0; i < h->n_targets; ++i)
+            if (max_len < h->target_len[i]) max_len = h->target_len[i];
+        max_len += 256;
+        for (n_lvls = 0, s = 1<<min_shift; max_len > s; ++n_lvls, s <<= 3);
+        fmt = HTS_FMT_CSI;
+    } else min_shift = 14, n_lvls = 5, fmt = HTS_FMT_BAI;
+    idx = hts_idx_init(h->n_targets, fmt, bgzf_tell(fp), min_shift, n_lvls);
+    bam_hdr_destroy(h);
+    b = bam_init1();
+    while (bam_read1(fp, b) >= 0) {
+        int l, ret;
+        l = bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b));
+        if (l == 0) l = 1; // no zero-length records
+        ret = hts_idx_push(idx, b->core.tid, b->core.pos, b->core.pos + l, bgzf_tell(fp), !(b->core.flag&BAM_FUNMAP));
+        if (ret < 0)
         {
             // unsorted
             bam_destroy1(b);
             hts_idx_destroy(idx);
             return NULL;
         }
-	}
-	hts_idx_finish(idx, bgzf_tell(fp));
-	bam_destroy1(b);
-	return idx;
+    }
+    hts_idx_finish(idx, bgzf_tell(fp));
+    bam_destroy1(b);
+    return idx;
 }
 
 int bam_index_build(const char *fn, int min_shift)
 {
-	hts_idx_t *idx;
-	htsFile *fp;
-	int ret = 0;
-
-	if ((fp = hts_open(fn, "r")) == 0) return -1;
-	if (fp->is_cram) {
-	    	ret = cram_index_build(fp->fp.cram, fn);
-	} else {
-			idx = bam_index(fp->fp.bgzf, min_shift);
-			if ( !idx )
-			{
-				hts_close(fp);
-				return -1;
-			}
-		hts_idx_save(idx, fn, min_shift > 0
-			     ? HTS_FMT_CSI : HTS_FMT_BAI);
-		hts_idx_destroy(idx);
-	}
-	hts_close(fp);
-
-	return ret;
+    hts_idx_t *idx;
+    htsFile *fp;
+    int ret = 0;
+
+    if ((fp = hts_open(fn, "r")) == 0) return -1;
+    if (fp->is_cram) {
+            ret = cram_index_build(fp->fp.cram, fn);
+    } else {
+            idx = bam_index(fp->fp.bgzf, min_shift);
+            if ( !idx )
+            {
+                hts_close(fp);
+                return -1;
+            }
+        hts_idx_save(idx, fn, min_shift > 0
+                 ? HTS_FMT_CSI : HTS_FMT_BAI);
+        hts_idx_destroy(idx);
+    }
+    hts_close(fp);
+
+    return ret;
 }
 
 static int bam_readrec(BGZF *fp, void *ignored, void *bv, int *tid, int *beg, int *end)
 {
-	bam1_t *b = bv;
-	int ret;
-	if ((ret = bam_read1(fp, b)) >= 0) {
-		*tid = b->core.tid; *beg = b->core.pos;
-		*end = b->core.pos + (b->core.n_cigar? bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b)) : 1);
-	}
-	return ret;
+    bam1_t *b = bv;
+    int ret;
+    if ((ret = bam_read1(fp, b)) >= 0) {
+        *tid = b->core.tid; *beg = b->core.pos;
+        *end = b->core.pos + (b->core.n_cigar? bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b)) : 1);
+    }
+    return ret;
 }
 
 // This is used only with read_rest=1 iterators, so need not set tid/beg/end.
 static int cram_readrec(BGZF *ignored, void *fpv, void *bv, int *tid, int *beg, int *end)
 {
-	htsFile *fp = fpv;
-	bam1_t *b = bv;
-	return cram_get_bam_seq(fp->fp.cram, &b);
+    htsFile *fp = fpv;
+    bam1_t *b = bv;
+    return cram_get_bam_seq(fp->fp.cram, &b);
 }
 
 // This is used only with read_rest=1 iterators, so need not set tid/beg/end.
 static int sam_bam_cram_readrec(BGZF *bgzfp, void *fpv, void *bv, int *tid, int *beg, int *end)
 {
-	htsFile *fp = fpv;
-	bam1_t *b = bv;
-	if (fp->is_bin) return bam_read1(bgzfp, b);
-	else if (fp->is_cram) return cram_get_bam_seq(fp->fp.cram, &b);
-	else {
-		// TODO Need headers available to implement this for SAM files
-		fprintf(stderr, "[sam_bam_cram_readrec] Not implemented for SAM files -- Exiting\n");
-		abort();
-	}
+    htsFile *fp = fpv;
+    bam1_t *b = bv;
+    if (fp->is_bin) return bam_read1(bgzfp, b);
+    else if (fp->is_cram) return cram_get_bam_seq(fp->fp.cram, &b);
+    else {
+        // TODO Need headers available to implement this for SAM files
+        fprintf(stderr, "[sam_bam_cram_readrec] Not implemented for SAM files -- Exiting\n");
+        abort();
+    }
 }
 
 // The CRAM implementation stores the loaded index within the cram_fd rather
@@ -469,83 +494,83 @@ typedef struct hts_cram_idx_t {
 
 hts_idx_t *sam_index_load(samFile *fp, const char *fn)
 {
-	if (fp->is_bin) return bam_index_load(fn);
-	else if (fp->is_cram) {
-		if (cram_index_load(fp->fp.cram, fn) < 0) return NULL;
-		// Cons up a fake "index" just pointing at the associated cram_fd:
-		hts_cram_idx_t *idx = malloc(sizeof (hts_cram_idx_t));
-		if (idx == NULL) return NULL;
-		idx->fmt = HTS_FMT_CRAI;
-		idx->cram = fp->fp.cram;
-		return (hts_idx_t *) idx;
-	}
-	else return NULL; // TODO Would use tbx_index_load if it returned hts_idx_t
+    if (fp->is_bin) return bam_index_load(fn);
+    else if (fp->is_cram) {
+        if (cram_index_load(fp->fp.cram, fn) < 0) return NULL;
+        // Cons up a fake "index" just pointing at the associated cram_fd:
+        hts_cram_idx_t *idx = malloc(sizeof (hts_cram_idx_t));
+        if (idx == NULL) return NULL;
+        idx->fmt = HTS_FMT_CRAI;
+        idx->cram = fp->fp.cram;
+        return (hts_idx_t *) idx;
+    }
+    else return NULL; // TODO Would use tbx_index_load if it returned hts_idx_t
 }
 
 static hts_itr_t *cram_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec)
 {
-	const hts_cram_idx_t *cidx = (const hts_cram_idx_t *) idx;
-	hts_itr_t *iter = (hts_itr_t *) calloc(1, sizeof(hts_itr_t));
-	if (iter == NULL) return NULL;
-
-	// Cons up a dummy iterator for which hts_itr_next() will simply invoke
-	// the readrec function:
-	iter->read_rest = 1;
-	iter->off = NULL;
-	iter->bins.a = NULL;
-	iter->readrec = readrec;
-
-	if (tid >= 0) {
-		cram_range r = { tid, beg+1, end };
-		if (cram_set_option(cidx->cram, CRAM_OPT_RANGE, &r) != 0) { free(iter); return NULL; }
-		iter->curr_off = 0;
-		// The following fields are not required by hts_itr_next(), but are
-		// filled in in case user code wants to look at them.
-		iter->tid = tid;
-		iter->beg = beg;
-		iter->end = end;
-	}
-	else switch (tid) {
-	case HTS_IDX_REST:
-		iter->curr_off = 0;
-		break;
-	case HTS_IDX_NONE:
-		iter->curr_off = 0;
-		iter->finished = 1;
-		break;
-	default:
-		fprintf(stderr, "[cram_itr_query] tid=%d not implemented for CRAM files -- Exiting\n", tid);
-		abort();
-		break;
-	}
-
-	return iter;
+    const hts_cram_idx_t *cidx = (const hts_cram_idx_t *) idx;
+    hts_itr_t *iter = (hts_itr_t *) calloc(1, sizeof(hts_itr_t));
+    if (iter == NULL) return NULL;
+
+    // Cons up a dummy iterator for which hts_itr_next() will simply invoke
+    // the readrec function:
+    iter->read_rest = 1;
+    iter->off = NULL;
+    iter->bins.a = NULL;
+    iter->readrec = readrec;
+
+    if (tid >= 0) {
+        cram_range r = { tid, beg+1, end };
+        if (cram_set_option(cidx->cram, CRAM_OPT_RANGE, &r) != 0) { free(iter); return NULL; }
+        iter->curr_off = 0;
+        // The following fields are not required by hts_itr_next(), but are
+        // filled in in case user code wants to look at them.
+        iter->tid = tid;
+        iter->beg = beg;
+        iter->end = end;
+    }
+    else switch (tid) {
+    case HTS_IDX_REST:
+        iter->curr_off = 0;
+        break;
+    case HTS_IDX_NONE:
+        iter->curr_off = 0;
+        iter->finished = 1;
+        break;
+    default:
+        fprintf(stderr, "[cram_itr_query] tid=%d not implemented for CRAM files -- Exiting\n", tid);
+        abort();
+        break;
+    }
+
+    return iter;
 }
 
 hts_itr_t *sam_itr_queryi(const hts_idx_t *idx, int tid, int beg, int end)
 {
-	const hts_cram_idx_t *cidx = (const hts_cram_idx_t *) idx;
-	if (idx == NULL)
-		return hts_itr_query(NULL, tid, beg, end, sam_bam_cram_readrec);
-	else if (cidx->fmt == HTS_FMT_CRAI)
-		return cram_itr_query(idx, tid, beg, end, cram_readrec);
-	else
-		return hts_itr_query(idx, tid, beg, end, bam_readrec);
+    const hts_cram_idx_t *cidx = (const hts_cram_idx_t *) idx;
+    if (idx == NULL)
+        return hts_itr_query(NULL, tid, beg, end, sam_bam_cram_readrec);
+    else if (cidx->fmt == HTS_FMT_CRAI)
+        return cram_itr_query(idx, tid, beg, end, cram_readrec);
+    else
+        return hts_itr_query(idx, tid, beg, end, bam_readrec);
 }
 
 static int cram_name2id(void *fdv, const char *ref)
 {
-	cram_fd *fd = (cram_fd *) fdv;
-	return sam_hdr_name2ref(fd->header, ref);
+    cram_fd *fd = (cram_fd *) fdv;
+    return sam_hdr_name2ref(fd->header, ref);
 }
 
 hts_itr_t *sam_itr_querys(const hts_idx_t *idx, bam_hdr_t *hdr, const char *region)
 {
-	const hts_cram_idx_t *cidx = (const hts_cram_idx_t *) idx;
-	if (cidx->fmt == HTS_FMT_CRAI)
-		return hts_itr_querys(idx, region, cram_name2id, cidx->cram, cram_itr_query, cram_readrec);
-	else
-		return hts_itr_querys(idx, region, (hts_name2id_f)(bam_name2id), hdr, hts_itr_query, bam_readrec);
+    const hts_cram_idx_t *cidx = (const hts_cram_idx_t *) idx;
+    if (cidx->fmt == HTS_FMT_CRAI)
+        return hts_itr_querys(idx, region, cram_name2id, cidx->cram, cram_itr_query, cram_readrec);
+    else
+        return hts_itr_querys(idx, region, (hts_name2id_f)(bam_name2id), hdr, hts_itr_query, bam_readrec);
 }
 
 /**********************
@@ -557,103 +582,103 @@ hts_itr_t *sam_itr_querys(const hts_idx_t *idx, bam_hdr_t *hdr, const char *regi
 
 bam_hdr_t *sam_hdr_parse(int l_text, const char *text)
 {
-	const char *q, *r, *p;
-	khash_t(s2i) *d;
-	d = kh_init(s2i);
-	for (p = text; *p; ++p) {
-		if (strncmp(p, "@SQ", 3) == 0) {
-			char *sn = 0;
-			int ln = -1;
-			for (q = p + 4;; ++q) {
-				if (strncmp(q, "SN:", 3) == 0) {
-					q += 3;
-					for (r = q; *r != '\t' && *r != '\n'; ++r);
-					sn = (char*)calloc(r - q + 1, 1);
-					strncpy(sn, q, r - q);
-					q = r;
-				} else if (strncmp(q, "LN:", 3) == 0)
-					ln = strtol(q + 3, (char**)&q, 10);
-				while (*q != '\t' && *q != '\n') ++q;
-				if (*q == '\n') break;
-			}
-			p = q;
-			if (sn && ln >= 0) {
-				khint_t k;
-				int absent;
-				k = kh_put(s2i, d, sn, &absent);
-				if (!absent) {
-					if (hts_verbose >= 2)
-						fprintf(stderr, "[W::%s] duplicated sequence '%s'\n", __func__, sn);
-					free(sn);
-				} else kh_val(d, k) = (int64_t)(kh_size(d) - 1)<<32 | ln;
-			}
-		}
-		while (*p != '\n') ++p;
-	}
-	return hdr_from_dict(d);
+    const char *q, *r, *p;
+    khash_t(s2i) *d;
+    d = kh_init(s2i);
+    for (p = text; *p; ++p) {
+        if (strncmp(p, "@SQ", 3) == 0) {
+            char *sn = 0;
+            int ln = -1;
+            for (q = p + 4;; ++q) {
+                if (strncmp(q, "SN:", 3) == 0) {
+                    q += 3;
+                    for (r = q; *r != '\t' && *r != '\n'; ++r);
+                    sn = (char*)calloc(r - q + 1, 1);
+                    strncpy(sn, q, r - q);
+                    q = r;
+                } else if (strncmp(q, "LN:", 3) == 0)
+                    ln = strtol(q + 3, (char**)&q, 10);
+                while (*q != '\t' && *q != '\n') ++q;
+                if (*q == '\n') break;
+            }
+            p = q;
+            if (sn && ln >= 0) {
+                khint_t k;
+                int absent;
+                k = kh_put(s2i, d, sn, &absent);
+                if (!absent) {
+                    if (hts_verbose >= 2)
+                        fprintf(stderr, "[W::%s] duplicated sequence '%s'\n", __func__, sn);
+                    free(sn);
+                } else kh_val(d, k) = (int64_t)(kh_size(d) - 1)<<32 | ln;
+            }
+        }
+        while (*p != '\n') ++p;
+    }
+    return hdr_from_dict(d);
 }
 
 bam_hdr_t *sam_hdr_read(htsFile *fp)
 {
-	if (fp->is_bin) {
-		return bam_hdr_read(fp->fp.bgzf);
-	} else if (fp->is_cram) {
-		return cram_header_to_bam(fp->fp.cram->header);
-	} else {
-		kstring_t str;
-		bam_hdr_t *h;
-		int has_SQ = 0;
-		str.l = str.m = 0; str.s = 0;
-		while (hts_getline(fp, KS_SEP_LINE, &fp->line) >= 0) {
-			if (fp->line.s[0] != '@') break;
-			if (fp->line.l > 3 && strncmp(fp->line.s,"@SQ",3) == 0) has_SQ = 1;
-			kputsn(fp->line.s, fp->line.l, &str);
-			kputc('\n', &str);
-		}
-		if (! has_SQ && fp->fn_aux) {
-			char line[2048];
-			FILE *f = fopen(fp->fn_aux, "r");
-			if (f == NULL) return NULL;
-			while (fgets(line, sizeof line, f)) {
-				const char *name = strtok(line, "\t");
-				const char *length = strtok(NULL, "\t");
-				ksprintf(&str, "@SQ\tSN:%s\tLN:%s\n", name, length);
-			}
-			fclose(f);
-		}
-		if (str.l == 0) kputsn("", 0, &str);
-		h = sam_hdr_parse(str.l, str.s);
-		h->l_text = str.l; h->text = str.s;
-		return h;
-	}
+    if (fp->is_bin) {
+        return bam_hdr_read(fp->fp.bgzf);
+    } else if (fp->is_cram) {
+        return cram_header_to_bam(fp->fp.cram->header);
+    } else {
+        kstring_t str;
+        bam_hdr_t *h;
+        int has_SQ = 0;
+        str.l = str.m = 0; str.s = 0;
+        while (hts_getline(fp, KS_SEP_LINE, &fp->line) >= 0) {
+            if (fp->line.s[0] != '@') break;
+            if (fp->line.l > 3 && strncmp(fp->line.s,"@SQ",3) == 0) has_SQ = 1;
+            kputsn(fp->line.s, fp->line.l, &str);
+            kputc('\n', &str);
+        }
+        if (! has_SQ && fp->fn_aux) {
+            char line[2048];
+            FILE *f = fopen(fp->fn_aux, "r");
+            if (f == NULL) return NULL;
+            while (fgets(line, sizeof line, f)) {
+                const char *name = strtok(line, "\t");
+                const char *length = strtok(NULL, "\t");
+                ksprintf(&str, "@SQ\tSN:%s\tLN:%s\n", name, length);
+            }
+            fclose(f);
+        }
+        if (str.l == 0) kputsn("", 0, &str);
+        h = sam_hdr_parse(str.l, str.s);
+        h->l_text = str.l; h->text = str.s;
+        return h;
+    }
 }
 
 int sam_hdr_write(htsFile *fp, const bam_hdr_t *h)
 {
-	if (fp->is_bin) {
-		bam_hdr_write(fp->fp.bgzf, h);
-	} else if (fp->is_cram) {
-		cram_fd *fd = fp->fp.cram;
-		if (cram_set_header(fd, bam_header_to_cram((bam_hdr_t *)h)) < 0) return -1;
-		if (fp->fn_aux)
-		    cram_load_reference(fd, fp->fn_aux);
-		if (cram_write_SAM_hdr(fd, fd->header) < 0) return -1;
-	} else {
-		char *p;
-		hputs(h->text, fp->fp.hfile);
-		p = strstr(h->text, "@SQ\t"); // FIXME: we need a loop to make sure "@SQ\t" does not match something unwanted!!!
-		if (p == 0) {
-			int i;
-			for (i = 0; i < h->n_targets; ++i) {
-				fp->line.l = 0;
-				kputsn("@SQ\tSN:", 7, &fp->line); kputs(h->target_name[i], &fp->line);
-				kputsn("\tLN:", 4, &fp->line); kputw(h->target_len[i], &fp->line); kputc('\n', &fp->line);
-				if ( hwrite(fp->fp.hfile, fp->line.s, fp->line.l) != fp->line.l ) return -1;
-			}
-		}
-		if ( hflush(fp->fp.hfile) != 0 ) return -1;
-	}
-	return 0;
+    if (fp->is_bin) {
+        bam_hdr_write(fp->fp.bgzf, h);
+    } else if (fp->is_cram) {
+        cram_fd *fd = fp->fp.cram;
+        if (cram_set_header(fd, bam_header_to_cram((bam_hdr_t *)h)) < 0) return -1;
+        if (fp->fn_aux)
+            cram_load_reference(fd, fp->fn_aux);
+        if (cram_write_SAM_hdr(fd, fd->header) < 0) return -1;
+    } else {
+        char *p;
+        hputs(h->text, fp->fp.hfile);
+        p = strstr(h->text, "@SQ\t"); // FIXME: we need a loop to make sure "@SQ\t" does not match something unwanted!!!
+        if (p == 0) {
+            int i;
+            for (i = 0; i < h->n_targets; ++i) {
+                fp->line.l = 0;
+                kputsn("@SQ\tSN:", 7, &fp->line); kputs(h->target_name[i], &fp->line);
+                kputsn("\tLN:", 4, &fp->line); kputw(h->target_len[i], &fp->line); kputc('\n', &fp->line);
+                if ( hwrite(fp->fp.hfile, fp->line.s, fp->line.l) != fp->line.l ) return -1;
+            }
+        }
+        if ( hflush(fp->fp.hfile) != 0 ) return -1;
+    }
+    return 0;
 }
 
 /**********************
@@ -668,173 +693,173 @@ int sam_parse1(kstring_t *s, bam_hdr_t *h, bam1_t *b)
 #define _parse_err(cond, msg) do { if ((cond) && hts_verbose >= 1) { fprintf(stderr, "[E::%s] " msg "\n", __func__); goto err_ret; } } while (0)
 #define _parse_warn(cond, msg) if ((cond) && hts_verbose >= 2) fprintf(stderr, "[W::%s] " msg "\n", __func__)
 
-	uint8_t *t;
-	char *p = s->s, *q;
-	int i;
-	kstring_t str;
-	bam1_core_t *c = &b->core;
-
-	str.l = b->l_data = 0;
-	str.s = (char*)b->data; str.m = b->m_data;
-	memset(c, 0, 32);
-	if (h->cigar_tab == 0) {
-		h->cigar_tab = (int8_t*) malloc(128);
-		for (i = 0; i < 128; ++i)
-			h->cigar_tab[i] = -1;
-		for (i = 0; BAM_CIGAR_STR[i]; ++i)
-			h->cigar_tab[(int)BAM_CIGAR_STR[i]] = i;
-	}
-	// qname
-	q = _read_token(p);
-	kputsn_(q, p - q, &str);
-	c->l_qname = p - q;
-	// flag
-	c->flag = strtol(p, &p, 0);
-	if (*p++ != '\t') goto err_ret; // malformated flag
-	// chr
-	q = _read_token(p);
-	if (strcmp(q, "*")) {
-		_parse_err(h->n_targets == 0, "missing SAM header");
-		c->tid = bam_name2id(h, q);
-		_parse_warn(c->tid < 0, "urecognized reference name; treated as unmapped");
-	} else c->tid = -1;
-	// pos
-	c->pos = strtol(p, &p, 10) - 1;
-	if (*p++ != '\t') goto err_ret;
-	if (c->pos < 0 && c->tid >= 0) {
-		_parse_warn(1, "mapped query cannot have zero coordinate; treated as unmapped");
-		c->tid = -1;
-	}
-	if (c->tid < 0) c->flag |= BAM_FUNMAP;
-	// mapq
-	c->qual = strtol(p, &p, 10);
-	if (*p++ != '\t') goto err_ret;
-	// cigar
-	if (*p != '*') {
-		uint32_t *cigar;
-		size_t n_cigar = 0;
-		for (q = p; *p && *p != '\t'; ++p)
-			if (!isdigit(*p)) ++n_cigar;
-		if (*p++ != '\t') goto err_ret;
-		_parse_err(n_cigar >= 65536, "too many CIGAR operations");
-		c->n_cigar = n_cigar;
-		_get_mem(uint32_t, &cigar, &str, c->n_cigar<<2);
-		for (i = 0; i < c->n_cigar; ++i, ++q) {
-			int op;
-			cigar[i] = strtol(q, &q, 10)<<BAM_CIGAR_SHIFT;
-			op = (uint8_t)*q >= 128? -1 : h->cigar_tab[(int)*q];
-			_parse_err(op < 0, "unrecognized CIGAR operator");
-			cigar[i] |= op;
-		}
-		i = bam_cigar2rlen(c->n_cigar, cigar);
-	} else {
-		_parse_warn(!(c->flag&BAM_FUNMAP), "mapped query must have a CIGAR; treated as unmapped");
-		c->flag |= BAM_FUNMAP;
-		q = _read_token(p);
-		i = 1;
-	}
-	c->bin = hts_reg2bin(c->pos, c->pos + i, 14, 5);
-	// mate chr
-	q = _read_token(p);
-	if (strcmp(q, "=") == 0) c->mtid = c->tid;
-	else if (strcmp(q, "*") == 0) c->mtid = -1;
-	else c->mtid = bam_name2id(h, q);
-	// mpos
-	c->mpos = strtol(p, &p, 10) - 1;
-	if (*p++ != '\t') goto err_ret;
-	if (c->mpos < 0 && c->mtid >= 0) {
-		_parse_warn(1, "mapped mate cannot have zero coordinate; treated as unmapped");
-		c->mtid = -1;
-	}
-	// tlen
-	c->isize = strtol(p, &p, 10);
-	if (*p++ != '\t') goto err_ret;
-	// seq
-	q = _read_token(p);
-	if (strcmp(q, "*")) {
-		c->l_qseq = p - q - 1;
-		i = bam_cigar2qlen(c->n_cigar, (uint32_t*)(str.s + c->l_qname));
-		_parse_err(c->n_cigar && i != c->l_qseq, "CIGAR and query sequence are of different length");
-		i = (c->l_qseq + 1) >> 1;
-		_get_mem(uint8_t, &t, &str, i);
-		memset(t, 0, i);
-		for (i = 0; i < c->l_qseq; ++i)
-			t[i>>1] |= seq_nt16_table[(int)q[i]] << ((~i&1)<<2);
-	} else c->l_qseq = 0;
-	// qual
-	q = _read_token_aux(p);
-	_get_mem(uint8_t, &t, &str, c->l_qseq);
-	if (strcmp(q, "*")) {
-		_parse_err(p - q - 1 != c->l_qseq, "SEQ and QUAL are of different length");
-		for (i = 0; i < c->l_qseq; ++i) t[i] = q[i] - 33;
-	} else memset(t, 0xff, c->l_qseq);
-	// aux
-	// Note that (like the bam1_core_t fields) this aux data in b->data is
-	// stored in host endianness; so there is no byte swapping needed here.
-	while (p < s->s + s->l) {
-		uint8_t type;
-		q = _read_token_aux(p); // FIXME: can be accelerated for long 'B' arrays
-		_parse_err(p - q - 1 < 6, "incomplete aux field");
-		kputsn_(q, 2, &str);
-		q += 3; type = *q++; ++q; // q points to value
-		if (type == 'A' || type == 'a' || type == 'c' || type == 'C') {
-			kputc_('A', &str);
-			kputc_(*q, &str);
-		} else if (type == 'i' || type == 'I') {
-			long x;
-			x = strtol(q, &q, 10);
-			if (x < 0) {
-				if (x >= INT8_MIN) {
-					kputc_('c', &str); kputc_(x, &str);
-				} else if (x >= INT16_MIN) {
-					int16_t y = x;
-					kputc_('s', &str); kputsn_((char*)&y, 2, &str);
-				} else {
-					int32_t y = x;
-					kputc_('i', &str); kputsn_(&y, 4, &str);
-				}
-			} else {
-				if (x <= UINT8_MAX) {
-					kputc_('C', &str); kputc_(x, &str);
-				} else if (x <= UINT16_MAX) {
-					uint16_t y = x;
-					kputc_('S', &str); kputsn_(&y, 2, &str);
-				} else {
-					uint32_t y = x;
-					kputc_('I', &str); kputsn_(&y, 4, &str);
-				}
-			}
-		} else if (type == 'f') {
-			float x;
-			x = strtod(q, &q);
-			kputc_('f', &str); kputsn_(&x, 4, &str);
-		} else if (type == 'd') {
-			double x;
-			x = strtod(q, &q);
-			kputc_('d', &str); kputsn_(&x, 8, &str);
-		} else if (type == 'Z' || type == 'H') {
-			kputc_(type, &str);kputsn_(q, p - q, &str); // note that this include the trailing NULL
-		} else if (type == 'B') {
-			int32_t n;
-			char *r;
-			_parse_err(p - q - 1 < 3, "incomplete B-typed aux field");
-			type = *q++; // q points to the first ',' following the typing byte
-			for (r = q, n = 0; *r; ++r)
-				if (*r == ',') ++n;
-			kputc_('B', &str); kputc_(type, &str); kputsn_(&n, 4, &str);
-			// FIXME: to evaluate which is faster: a) aligned array and then memmove(); b) unaligned array; c) kputsn_()
-			if (type == 'c')      while (q + 1 < p) { int8_t   x = strtol(q + 1, &q, 0); kputc_(x, &str); }
-			else if (type == 'C') while (q + 1 < p) { uint8_t  x = strtoul(q + 1, &q, 0); kputc_(x, &str); }
-			else if (type == 's') while (q + 1 < p) { int16_t  x = strtol(q + 1, &q, 0); kputsn_(&x, 2, &str); }
-			else if (type == 'S') while (q + 1 < p) { uint16_t x = strtoul(q + 1, &q, 0); kputsn_(&x, 2, &str); }
-			else if (type == 'i') while (q + 1 < p) { int32_t  x = strtol(q + 1, &q, 0); kputsn_(&x, 4, &str); }
-			else if (type == 'I') while (q + 1 < p) { uint32_t x = strtoul(q + 1, &q, 0); kputsn_(&x, 4, &str); }
-			else if (type == 'f') while (q + 1 < p) { float    x = strtod(q + 1, &q);    kputsn_(&x, 4, &str); }
-			else _parse_err(1, "unrecognized type");
-		} else _parse_err(1, "unrecognized type");
-	}
-	b->data = (uint8_t*)str.s; b->l_data = str.l; b->m_data = str.m;
-	return 0;
+    uint8_t *t;
+    char *p = s->s, *q;
+    int i;
+    kstring_t str;
+    bam1_core_t *c = &b->core;
+
+    str.l = b->l_data = 0;
+    str.s = (char*)b->data; str.m = b->m_data;
+    memset(c, 0, 32);
+    if (h->cigar_tab == 0) {
+        h->cigar_tab = (int8_t*) malloc(128);
+        for (i = 0; i < 128; ++i)
+            h->cigar_tab[i] = -1;
+        for (i = 0; BAM_CIGAR_STR[i]; ++i)
+            h->cigar_tab[(int)BAM_CIGAR_STR[i]] = i;
+    }
+    // qname
+    q = _read_token(p);
+    kputsn_(q, p - q, &str);
+    c->l_qname = p - q;
+    // flag
+    c->flag = strtol(p, &p, 0);
+    if (*p++ != '\t') goto err_ret; // malformated flag
+    // chr
+    q = _read_token(p);
+    if (strcmp(q, "*")) {
+        _parse_err(h->n_targets == 0, "missing SAM header");
+        c->tid = bam_name2id(h, q);
+        _parse_warn(c->tid < 0, "urecognized reference name; treated as unmapped");
+    } else c->tid = -1;
+    // pos
+    c->pos = strtol(p, &p, 10) - 1;
+    if (*p++ != '\t') goto err_ret;
+    if (c->pos < 0 && c->tid >= 0) {
+        _parse_warn(1, "mapped query cannot have zero coordinate; treated as unmapped");
+        c->tid = -1;
+    }
+    if (c->tid < 0) c->flag |= BAM_FUNMAP;
+    // mapq
+    c->qual = strtol(p, &p, 10);
+    if (*p++ != '\t') goto err_ret;
+    // cigar
+    if (*p != '*') {
+        uint32_t *cigar;
+        size_t n_cigar = 0;
+        for (q = p; *p && *p != '\t'; ++p)
+            if (!isdigit(*p)) ++n_cigar;
+        if (*p++ != '\t') goto err_ret;
+        _parse_err(n_cigar >= 65536, "too many CIGAR operations");
+        c->n_cigar = n_cigar;
+        _get_mem(uint32_t, &cigar, &str, c->n_cigar<<2);
+        for (i = 0; i < c->n_cigar; ++i, ++q) {
+            int op;
+            cigar[i] = strtol(q, &q, 10)<<BAM_CIGAR_SHIFT;
+            op = (uint8_t)*q >= 128? -1 : h->cigar_tab[(int)*q];
+            _parse_err(op < 0, "unrecognized CIGAR operator");
+            cigar[i] |= op;
+        }
+        i = bam_cigar2rlen(c->n_cigar, cigar);
+    } else {
+        _parse_warn(!(c->flag&BAM_FUNMAP), "mapped query must have a CIGAR; treated as unmapped");
+        c->flag |= BAM_FUNMAP;
+        q = _read_token(p);
+        i = 1;
+    }
+    c->bin = hts_reg2bin(c->pos, c->pos + i, 14, 5);
+    // mate chr
+    q = _read_token(p);
+    if (strcmp(q, "=") == 0) c->mtid = c->tid;
+    else if (strcmp(q, "*") == 0) c->mtid = -1;
+    else c->mtid = bam_name2id(h, q);
+    // mpos
+    c->mpos = strtol(p, &p, 10) - 1;
+    if (*p++ != '\t') goto err_ret;
+    if (c->mpos < 0 && c->mtid >= 0) {
+        _parse_warn(1, "mapped mate cannot have zero coordinate; treated as unmapped");
+        c->mtid = -1;
+    }
+    // tlen
+    c->isize = strtol(p, &p, 10);
+    if (*p++ != '\t') goto err_ret;
+    // seq
+    q = _read_token(p);
+    if (strcmp(q, "*")) {
+        c->l_qseq = p - q - 1;
+        i = bam_cigar2qlen(c->n_cigar, (uint32_t*)(str.s + c->l_qname));
+        _parse_err(c->n_cigar && i != c->l_qseq, "CIGAR and query sequence are of different length");
+        i = (c->l_qseq + 1) >> 1;
+        _get_mem(uint8_t, &t, &str, i);
+        memset(t, 0, i);
+        for (i = 0; i < c->l_qseq; ++i)
+            t[i>>1] |= seq_nt16_table[(int)q[i]] << ((~i&1)<<2);
+    } else c->l_qseq = 0;
+    // qual
+    q = _read_token_aux(p);
+    _get_mem(uint8_t, &t, &str, c->l_qseq);
+    if (strcmp(q, "*")) {
+        _parse_err(p - q - 1 != c->l_qseq, "SEQ and QUAL are of different length");
+        for (i = 0; i < c->l_qseq; ++i) t[i] = q[i] - 33;
+    } else memset(t, 0xff, c->l_qseq);
+    // aux
+    // Note that (like the bam1_core_t fields) this aux data in b->data is
+    // stored in host endianness; so there is no byte swapping needed here.
+    while (p < s->s + s->l) {
+        uint8_t type;
+        q = _read_token_aux(p); // FIXME: can be accelerated for long 'B' arrays
+        _parse_err(p - q - 1 < 6, "incomplete aux field");
+        kputsn_(q, 2, &str);
+        q += 3; type = *q++; ++q; // q points to value
+        if (type == 'A' || type == 'a' || type == 'c' || type == 'C') {
+            kputc_('A', &str);
+            kputc_(*q, &str);
+        } else if (type == 'i' || type == 'I') {
+            long x;
+            x = strtol(q, &q, 10);
+            if (x < 0) {
+                if (x >= INT8_MIN) {
+                    kputc_('c', &str); kputc_(x, &str);
+                } else if (x >= INT16_MIN) {
+                    int16_t y = x;
+                    kputc_('s', &str); kputsn_((char*)&y, 2, &str);
+                } else {
+                    int32_t y = x;
+                    kputc_('i', &str); kputsn_(&y, 4, &str);
+                }
+            } else {
+                if (x <= UINT8_MAX) {
+                    kputc_('C', &str); kputc_(x, &str);
+                } else if (x <= UINT16_MAX) {
+                    uint16_t y = x;
+                    kputc_('S', &str); kputsn_(&y, 2, &str);
+                } else {
+                    uint32_t y = x;
+                    kputc_('I', &str); kputsn_(&y, 4, &str);
+                }
+            }
+        } else if (type == 'f') {
+            float x;
+            x = strtod(q, &q);
+            kputc_('f', &str); kputsn_(&x, 4, &str);
+        } else if (type == 'd') {
+            double x;
+            x = strtod(q, &q);
+            kputc_('d', &str); kputsn_(&x, 8, &str);
+        } else if (type == 'Z' || type == 'H') {
+            kputc_(type, &str);kputsn_(q, p - q, &str); // note that this include the trailing NULL
+        } else if (type == 'B') {
+            int32_t n;
+            char *r;
+            _parse_err(p - q - 1 < 3, "incomplete B-typed aux field");
+            type = *q++; // q points to the first ',' following the typing byte
+            for (r = q, n = 0; *r; ++r)
+                if (*r == ',') ++n;
+            kputc_('B', &str); kputc_(type, &str); kputsn_(&n, 4, &str);
+            // FIXME: to evaluate which is faster: a) aligned array and then memmove(); b) unaligned array; c) kputsn_()
+            if (type == 'c')      while (q + 1 < p) { int8_t   x = strtol(q + 1, &q, 0); kputc_(x, &str); }
+            else if (type == 'C') while (q + 1 < p) { uint8_t  x = strtoul(q + 1, &q, 0); kputc_(x, &str); }
+            else if (type == 's') while (q + 1 < p) { int16_t  x = strtol(q + 1, &q, 0); kputsn_(&x, 2, &str); }
+            else if (type == 'S') while (q + 1 < p) { uint16_t x = strtoul(q + 1, &q, 0); kputsn_(&x, 2, &str); }
+            else if (type == 'i') while (q + 1 < p) { int32_t  x = strtol(q + 1, &q, 0); kputsn_(&x, 4, &str); }
+            else if (type == 'I') while (q + 1 < p) { uint32_t x = strtoul(q + 1, &q, 0); kputsn_(&x, 4, &str); }
+            else if (type == 'f') while (q + 1 < p) { float    x = strtod(q + 1, &q);    kputsn_(&x, 4, &str); }
+            else _parse_err(1, "unrecognized type");
+        } else _parse_err(1, "unrecognized type");
+    }
+    b->data = (uint8_t*)str.s; b->l_data = str.l; b->m_data = str.m;
+    return 0;
 
 #undef _parse_warn
 #undef _parse_err
@@ -842,173 +867,173 @@ int sam_parse1(kstring_t *s, bam_hdr_t *h, bam1_t *b)
 #undef _read_token_aux
 #undef _read_token
 err_ret:
-	b->data = (uint8_t*)str.s; b->l_data = str.l; b->m_data = str.m;
-	return -2;
+    b->data = (uint8_t*)str.s; b->l_data = str.l; b->m_data = str.m;
+    return -2;
 }
 
 int sam_read1(htsFile *fp, bam_hdr_t *h, bam1_t *b)
 {
-	if (fp->is_bin) {
-		int r = bam_read1(fp->fp.bgzf, b);
-		if (r >= 0) {
-			if (b->core.tid  >= h->n_targets || b->core.tid  < -1 ||
-			    b->core.mtid >= h->n_targets || b->core.mtid < -1)
-				return -3;
-		}
-		return r;
-	} else if (fp->is_cram) {
-		return cram_get_bam_seq(fp->fp.cram, &b);
-	} else {
-		int ret;
+    if (fp->is_bin) {
+        int r = bam_read1(fp->fp.bgzf, b);
+        if (r >= 0) {
+            if (b->core.tid  >= h->n_targets || b->core.tid  < -1 ||
+                b->core.mtid >= h->n_targets || b->core.mtid < -1)
+                return -3;
+        }
+        return r;
+    } else if (fp->is_cram) {
+        return cram_get_bam_seq(fp->fp.cram, &b);
+    } else {
+        int ret;
 err_recover:
-		if (fp->line.l == 0) {
-			ret = hts_getline(fp, KS_SEP_LINE, &fp->line);
-			if (ret < 0) return -1;
-		}
-		ret = sam_parse1(&fp->line, h, b);
-		fp->line.l = 0;
-		if (ret < 0) {
-			if (hts_verbose >= 1)
-				fprintf(stderr, "[W::%s] parse error at line %lld\n", __func__, (long long)fp->lineno);
-			if (h->ignore_sam_err) goto err_recover;
-		}
-		return ret;
-	}
+        if (fp->line.l == 0) {
+            ret = hts_getline(fp, KS_SEP_LINE, &fp->line);
+            if (ret < 0) return -1;
+        }
+        ret = sam_parse1(&fp->line, h, b);
+        fp->line.l = 0;
+        if (ret < 0) {
+            if (hts_verbose >= 1)
+                fprintf(stderr, "[W::%s] parse error at line %lld\n", __func__, (long long)fp->lineno);
+            if (h->ignore_sam_err) goto err_recover;
+        }
+        return ret;
+    }
 }
 
 int sam_format1(const bam_hdr_t *h, const bam1_t *b, kstring_t *str)
 {
-	int i;
-	uint8_t *s;
-	const bam1_core_t *c = &b->core;
-
-	str->l = 0;
-	kputsn(bam_get_qname(b), c->l_qname-1, str); kputc('\t', str); // query name
-	kputw(c->flag, str); kputc('\t', str); // flag
-	if (c->tid >= 0) { // chr
-		kputs(h->target_name[c->tid] , str);
-		kputc('\t', str);
-	} else kputsn("*\t", 2, str);
-	kputw(c->pos + 1, str); kputc('\t', str); // pos
-	kputw(c->qual, str); kputc('\t', str); // qual
-	if (c->n_cigar) { // cigar
-		uint32_t *cigar = bam_get_cigar(b);
-		for (i = 0; i < c->n_cigar; ++i) {
-			kputw(bam_cigar_oplen(cigar[i]), str);
-			kputc(bam_cigar_opchr(cigar[i]), str);
-		}
-	} else kputc('*', str);
-	kputc('\t', str);
-	if (c->mtid < 0) kputsn("*\t", 2, str); // mate chr
-	else if (c->mtid == c->tid) kputsn("=\t", 2, str);
-	else {
-		kputs(h->target_name[c->mtid], str);
-		kputc('\t', str);
-	}
-	kputw(c->mpos + 1, str); kputc('\t', str); // mate pos
-	kputw(c->isize, str); kputc('\t', str); // template len
-	if (c->l_qseq) { // seq and qual
-		uint8_t *s = bam_get_seq(b);
-		for (i = 0; i < c->l_qseq; ++i) kputc("=ACMGRSVTWYHKDBN"[bam_seqi(s, i)], str);
-		kputc('\t', str);
-		s = bam_get_qual(b);
-		if (s[0] == 0xff) kputc('*', str);
-		else for (i = 0; i < c->l_qseq; ++i) kputc(s[i] + 33, str);
-	} else kputsn("*\t*", 3, str);
-	s = bam_get_aux(b); // aux
-	while (s+4 <= b->data + b->l_data) {
-		uint8_t type, key[2];
-		key[0] = s[0]; key[1] = s[1];
-		s += 2; type = *s++;
-		kputc('\t', str); kputsn((char*)key, 2, str); kputc(':', str);
-		if (type == 'A') {
-			kputsn("A:", 2, str);
-			kputc(*s, str);
-			++s;
-		} else if (type == 'C') {
-			kputsn("i:", 2, str);
-			kputw(*s, str);
-			++s;
-		} else if (type == 'c') {
-			kputsn("i:", 2, str);
-			kputw(*(int8_t*)s, str);
-			++s;
-		} else if (type == 'S') {
-			if (s+2 <= b->data + b->l_data) {
-				kputsn("i:", 2, str);
-				kputw(*(uint16_t*)s, str);
-				s += 2;
-			} else return -1;
-		} else if (type == 's') {
-			if (s+2 <= b->data + b->l_data) {
-				kputsn("i:", 2, str);
-				kputw(*(int16_t*)s, str);
-				s += 2;
-			} else return -1;
-		} else if (type == 'I') {
-			if (s+4 <= b->data + b->l_data) {
-				kputsn("i:", 2, str);
-				kputuw(*(uint32_t*)s, str);
-				s += 4;
-			} else return -1;
-		} else if (type == 'i') {
-			if (s+4 <= b->data + b->l_data) {
-				kputsn("i:", 2, str);
-				kputw(*(int32_t*)s, str);
-				s += 4;
-			} else return -1;
-		} else if (type == 'f') {
-			if (s+4 <= b->data + b->l_data) {
-				ksprintf(str, "f:%g", *(float*)s);
-				s += 4;
-			} else return -1;
-			
-		} else if (type == 'd') {
-			if (s+8 <= b->data + b->l_data) {
-				ksprintf(str, "d:%g", *(double*)s);
-				s += 8;
-			} else return -1;
-		} else if (type == 'Z' || type == 'H') {
-			kputc(type, str); kputc(':', str);
-			while (s < b->data + b->l_data && *s) kputc(*s++, str);
-			if (s >= b->data + b->l_data)
-				return -1;
-			++s;
-		} else if (type == 'B') {
-			uint8_t sub_type = *(s++);
-			int32_t n;
-			memcpy(&n, s, 4);
-			s += 4; // no point to the start of the array
-			if (s + n >= b->data + b->l_data)
-				return -1;
-			kputsn("B:", 2, str); kputc(sub_type, str); // write the typing
-			for (i = 0; i < n; ++i) { // FIXME: for better performance, put the loop after "if"
-				kputc(',', str);
-				if ('c' == sub_type)	  { kputw(*(int8_t*)s, str); ++s; }
-				else if ('C' == sub_type) { kputw(*(uint8_t*)s, str); ++s; }
-				else if ('s' == sub_type) { kputw(*(int16_t*)s, str); s += 2; }
-				else if ('S' == sub_type) { kputw(*(uint16_t*)s, str); s += 2; }
-				else if ('i' == sub_type) { kputw(*(int32_t*)s, str); s += 4; }
-				else if ('I' == sub_type) { kputuw(*(uint32_t*)s, str); s += 4; }
-				else if ('f' == sub_type) { ksprintf(str, "%g", *(float*)s); s += 4; }
-			}
-		}
-	}
-	return str->l;
+    int i;
+    uint8_t *s;
+    const bam1_core_t *c = &b->core;
+
+    str->l = 0;
+    kputsn(bam_get_qname(b), c->l_qname-1, str); kputc('\t', str); // query name
+    kputw(c->flag, str); kputc('\t', str); // flag
+    if (c->tid >= 0) { // chr
+        kputs(h->target_name[c->tid] , str);
+        kputc('\t', str);
+    } else kputsn("*\t", 2, str);
+    kputw(c->pos + 1, str); kputc('\t', str); // pos
+    kputw(c->qual, str); kputc('\t', str); // qual
+    if (c->n_cigar) { // cigar
+        uint32_t *cigar = bam_get_cigar(b);
+        for (i = 0; i < c->n_cigar; ++i) {
+            kputw(bam_cigar_oplen(cigar[i]), str);
+            kputc(bam_cigar_opchr(cigar[i]), str);
+        }
+    } else kputc('*', str);
+    kputc('\t', str);
+    if (c->mtid < 0) kputsn("*\t", 2, str); // mate chr
+    else if (c->mtid == c->tid) kputsn("=\t", 2, str);
+    else {
+        kputs(h->target_name[c->mtid], str);
+        kputc('\t', str);
+    }
+    kputw(c->mpos + 1, str); kputc('\t', str); // mate pos
+    kputw(c->isize, str); kputc('\t', str); // template len
+    if (c->l_qseq) { // seq and qual
+        uint8_t *s = bam_get_seq(b);
+        for (i = 0; i < c->l_qseq; ++i) kputc("=ACMGRSVTWYHKDBN"[bam_seqi(s, i)], str);
+        kputc('\t', str);
+        s = bam_get_qual(b);
+        if (s[0] == 0xff) kputc('*', str);
+        else for (i = 0; i < c->l_qseq; ++i) kputc(s[i] + 33, str);
+    } else kputsn("*\t*", 3, str);
+    s = bam_get_aux(b); // aux
+    while (s+4 <= b->data + b->l_data) {
+        uint8_t type, key[2];
+        key[0] = s[0]; key[1] = s[1];
+        s += 2; type = *s++;
+        kputc('\t', str); kputsn((char*)key, 2, str); kputc(':', str);
+        if (type == 'A') {
+            kputsn("A:", 2, str);
+            kputc(*s, str);
+            ++s;
+        } else if (type == 'C') {
+            kputsn("i:", 2, str);
+            kputw(*s, str);
+            ++s;
+        } else if (type == 'c') {
+            kputsn("i:", 2, str);
+            kputw(*(int8_t*)s, str);
+            ++s;
+        } else if (type == 'S') {
+            if (s+2 <= b->data + b->l_data) {
+                kputsn("i:", 2, str);
+                kputw(*(uint16_t*)s, str);
+                s += 2;
+            } else return -1;
+        } else if (type == 's') {
+            if (s+2 <= b->data + b->l_data) {
+                kputsn("i:", 2, str);
+                kputw(*(int16_t*)s, str);
+                s += 2;
+            } else return -1;
+        } else if (type == 'I') {
+            if (s+4 <= b->data + b->l_data) {
+                kputsn("i:", 2, str);
+                kputuw(*(uint32_t*)s, str);
+                s += 4;
+            } else return -1;
+        } else if (type == 'i') {
+            if (s+4 <= b->data + b->l_data) {
+                kputsn("i:", 2, str);
+                kputw(*(int32_t*)s, str);
+                s += 4;
+            } else return -1;
+        } else if (type == 'f') {
+            if (s+4 <= b->data + b->l_data) {
+                ksprintf(str, "f:%g", *(float*)s);
+                s += 4;
+            } else return -1;
+
+        } else if (type == 'd') {
+            if (s+8 <= b->data + b->l_data) {
+                ksprintf(str, "d:%g", *(double*)s);
+                s += 8;
+            } else return -1;
+        } else if (type == 'Z' || type == 'H') {
+            kputc(type, str); kputc(':', str);
+            while (s < b->data + b->l_data && *s) kputc(*s++, str);
+            if (s >= b->data + b->l_data)
+                return -1;
+            ++s;
+        } else if (type == 'B') {
+            uint8_t sub_type = *(s++);
+            int32_t n;
+            memcpy(&n, s, 4);
+            s += 4; // no point to the start of the array
+            if (s + n >= b->data + b->l_data)
+                return -1;
+            kputsn("B:", 2, str); kputc(sub_type, str); // write the typing
+            for (i = 0; i < n; ++i) { // FIXME: for better performance, put the loop after "if"
+                kputc(',', str);
+                if ('c' == sub_type)      { kputw(*(int8_t*)s, str); ++s; }
+                else if ('C' == sub_type) { kputw(*(uint8_t*)s, str); ++s; }
+                else if ('s' == sub_type) { kputw(*(int16_t*)s, str); s += 2; }
+                else if ('S' == sub_type) { kputw(*(uint16_t*)s, str); s += 2; }
+                else if ('i' == sub_type) { kputw(*(int32_t*)s, str); s += 4; }
+                else if ('I' == sub_type) { kputuw(*(uint32_t*)s, str); s += 4; }
+                else if ('f' == sub_type) { ksprintf(str, "%g", *(float*)s); s += 4; }
+            }
+        }
+    }
+    return str->l;
 }
 
 int sam_write1(htsFile *fp, const bam_hdr_t *h, const bam1_t *b)
 {
-	if (fp->is_bin) {
-		return bam_write1(fp->fp.bgzf, b);
-	} else if (fp->is_cram) {
-		return cram_put_bam_seq(fp->fp.cram, (bam1_t *)b);
-	} else {
-		if (sam_format1(h, b, &fp->line) < 0) return -1;
-		kputc('\n', &fp->line);
-		if ( hwrite(fp->fp.hfile, fp->line.s, fp->line.l) != fp->line.l ) return -1;
-		return fp->line.l;
-	}
+    if (fp->is_bin) {
+        return bam_write1(fp->fp.bgzf, b);
+    } else if (fp->is_cram) {
+        return cram_put_bam_seq(fp->fp.cram, (bam1_t *)b);
+    } else {
+        if (sam_format1(h, b, &fp->line) < 0) return -1;
+        kputc('\n', &fp->line);
+        if ( hwrite(fp->fp.hfile, fp->line.s, fp->line.l) != fp->line.l ) return -1;
+        return fp->line.l;
+    }
 }
 
 /************************
@@ -1017,100 +1042,117 @@ int sam_write1(htsFile *fp, const bam_hdr_t *h, const bam1_t *b)
 
 void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data)
 {
-	int ori_len = b->l_data;
-	b->l_data += 3 + len;
-	if (b->m_data < b->l_data) {
-		b->m_data = b->l_data;
-		kroundup32(b->m_data);
-		b->data = (uint8_t*)realloc(b->data, b->m_data);
-	}
-	b->data[ori_len] = tag[0]; b->data[ori_len + 1] = tag[1];
-	b->data[ori_len + 2] = type;
-	memcpy(b->data + ori_len + 3, data, len);
+    int ori_len = b->l_data;
+    b->l_data += 3 + len;
+    if (b->m_data < b->l_data) {
+        b->m_data = b->l_data;
+        kroundup32(b->m_data);
+        b->data = (uint8_t*)realloc(b->data, b->m_data);
+    }
+    b->data[ori_len] = tag[0]; b->data[ori_len + 1] = tag[1];
+    b->data[ori_len + 2] = type;
+    memcpy(b->data + ori_len + 3, data, len);
 }
 
 static inline uint8_t *skip_aux(uint8_t *s)
 {
-	int size = aux_type2size(*s); ++s; // skip type
-	uint32_t n;
-	switch (size) {
-	case 'Z':
-	case 'H':
-		while (*s) ++s;
-		return s + 1;
-	case 'B':
-		size = aux_type2size(*s); ++s;
-		memcpy(&n, s, 4); s += 4;
-		return s + size * n;
-	case 0:
-		abort();
-		break;
-	default:
-		return s + size;
-	}
+    int size = aux_type2size(*s); ++s; // skip type
+    uint32_t n;
+    switch (size) {
+    case 'Z':
+    case 'H':
+        while (*s) ++s;
+        return s + 1;
+    case 'B':
+        size = aux_type2size(*s); ++s;
+        memcpy(&n, s, 4); s += 4;
+        return s + size * n;
+    case 0:
+        abort();
+        break;
+    default:
+        return s + size;
+    }
 }
 
 uint8_t *bam_aux_get(const bam1_t *b, const char tag[2])
 {
-	uint8_t *s;
-	int y = tag[0]<<8 | tag[1];
-	s = bam_get_aux(b);
-	while (s < b->data + b->l_data) {
-		int x = (int)s[0]<<8 | s[1];
-		s += 2;
-		if (x == y) return s;
-		s = skip_aux(s);
-	}
-	return 0;
+    uint8_t *s;
+    int y = tag[0]<<8 | tag[1];
+    s = bam_get_aux(b);
+    while (s < b->data + b->l_data) {
+        int x = (int)s[0]<<8 | s[1];
+        s += 2;
+        if (x == y) return s;
+        s = skip_aux(s);
+    }
+    return 0;
 }
 // s MUST BE returned by bam_aux_get()
 int bam_aux_del(bam1_t *b, uint8_t *s)
 {
-	uint8_t *p, *aux;
-	int l_aux = bam_get_l_aux(b);
-	aux = bam_get_aux(b);
-	p = s - 2;
-	s = skip_aux(s);
-	memmove(p, s, l_aux - (s - aux));
-	b->l_data -= s - p;
-	return 0;
+    uint8_t *p, *aux;
+    int l_aux = bam_get_l_aux(b);
+    aux = bam_get_aux(b);
+    p = s - 2;
+    s = skip_aux(s);
+    memmove(p, s, l_aux - (s - aux));
+    b->l_data -= s - p;
+    return 0;
 }
 
 int32_t bam_aux2i(const uint8_t *s)
 {
-	int type;
-	type = *s++;
-	if (type == 'c') return (int32_t)*(int8_t*)s;
-	else if (type == 'C') return (int32_t)*(uint8_t*)s;
-	else if (type == 's') return (int32_t)*(int16_t*)s;
-	else if (type == 'S') return (int32_t)*(uint16_t*)s;
-	else if (type == 'i' || type == 'I') return *(int32_t*)s;
-	else return 0;
+    int type;
+    type = *s++;
+    if (type == 'c') return (int32_t)*(int8_t*)s;
+    else if (type == 'C') return (int32_t)*(uint8_t*)s;
+    else if (type == 's') return (int32_t)*(int16_t*)s;
+    else if (type == 'S') return (int32_t)*(uint16_t*)s;
+    else if (type == 'i' || type == 'I') return *(int32_t*)s;
+    else return 0;
 }
 
 double bam_aux2f(const uint8_t *s)
 {
-	int type;
-	type = *s++;
-	if (type == 'd') return *(double*)s;
-	else if (type == 'f') return *(float*)s;
-	else return 0.0;
+    int type;
+    type = *s++;
+    if (type == 'd') return *(double*)s;
+    else if (type == 'f') return *(float*)s;
+    else return 0.0;
 }
 
 char bam_aux2A(const uint8_t *s)
 {
-	int type;
-	type = *s++;
-	if (type == 'A') return *(char*)s;
-	else return 0;
+    int type;
+    type = *s++;
+    if (type == 'A') return *(char*)s;
+    else return 0;
 }
 
 char *bam_aux2Z(const uint8_t *s)
 {
-	int type;
-	type = *s++;
-	if (type == 'Z' || type == 'H') return (char*)s;
-	else return 0;
+    int type;
+    type = *s++;
+    if (type == 'Z' || type == 'H') return (char*)s;
+    else return 0;
+}
+
+int sam_open_mode(char *mode, const char *fn, const char *format)
+{
+    // TODO Parse "bam5" etc for compression level
+    if (format == NULL) {
+        // Try to pick a format based on the filename extension
+        const char *ext = fn? strrchr(fn, '.') : NULL;
+        if (ext == NULL || strchr(ext, '/')) return -1;
+        return sam_open_mode(mode, fn, ext+1);
+    }
+    else if (strcmp(format, "bam") == 0) strcpy(mode, "b");
+    else if (strcmp(format, "cram") == 0) strcpy(mode, "c");
+    else if (strcmp(format, "sam") == 0) strcpy(mode, "");
+    else return -1;
+
+    return 0;
 }
 
 #define STRNCMP(a,b,n) (strncasecmp((a),(b),(n)) || strlen(a)!=(n))
@@ -1176,53 +1218,53 @@ char *bam_flag2str(int flag)
  *******************/
 
 typedef struct {
-	int k, x, y, end;
+    int k, x, y, end;
 } cstate_t;
 
 static cstate_t g_cstate_null = { -1, 0, 0, 0 };
 
 typedef struct __linkbuf_t {
-	bam1_t b;
-	int32_t beg, end;
-	cstate_t s;
-	struct __linkbuf_t *next;
+    bam1_t b;
+    int32_t beg, end;
+    cstate_t s;
+    struct __linkbuf_t *next;
 } lbnode_t;
 
 typedef struct {
-	int cnt, n, max;
-	lbnode_t **buf;
+    int cnt, n, max;
+    lbnode_t **buf;
 } mempool_t;
 
 static mempool_t *mp_init(void)
 {
-	mempool_t *mp;
-	mp = (mempool_t*)calloc(1, sizeof(mempool_t));
-	return mp;
+    mempool_t *mp;
+    mp = (mempool_t*)calloc(1, sizeof(mempool_t));
+    return mp;
 }
 static void mp_destroy(mempool_t *mp)
 {
-	int k;
-	for (k = 0; k < mp->n; ++k) {
-		free(mp->buf[k]->b.data);
-		free(mp->buf[k]);
-	}
-	free(mp->buf);
-	free(mp);
+    int k;
+    for (k = 0; k < mp->n; ++k) {
+        free(mp->buf[k]->b.data);
+        free(mp->buf[k]);
+    }
+    free(mp->buf);
+    free(mp);
 }
 static inline lbnode_t *mp_alloc(mempool_t *mp)
 {
-	++mp->cnt;
-	if (mp->n == 0) return (lbnode_t*)calloc(1, sizeof(lbnode_t));
-	else return mp->buf[--mp->n];
+    ++mp->cnt;
+    if (mp->n == 0) return (lbnode_t*)calloc(1, sizeof(lbnode_t));
+    else return mp->buf[--mp->n];
 }
 static inline void mp_free(mempool_t *mp, lbnode_t *p)
 {
-	--mp->cnt; p->next = 0; // clear lbnode_t::next here
-	if (mp->n == mp->max) {
-		mp->max = mp->max? mp->max<<1 : 256;
-		mp->buf = (lbnode_t**)realloc(mp->buf, sizeof(lbnode_t*) * mp->max);
-	}
-	mp->buf[mp->n++] = p;
+    --mp->cnt; p->next = 0; // clear lbnode_t::next here
+    if (mp->n == mp->max) {
+        mp->max = mp->max? mp->max<<1 : 256;
+        mp->buf = (lbnode_t**)realloc(mp->buf, sizeof(lbnode_t*) * mp->max);
+    }
+    mp->buf[mp->n++] = p;
 }
 
 /**********************
@@ -1238,76 +1280,76 @@ static inline int resolve_cigar2(bam_pileup1_t *p, int32_t pos, cstate_t *s)
 #define _cop(c) ((c)&BAM_CIGAR_MASK)
 #define _cln(c) ((c)>>BAM_CIGAR_SHIFT)
 
-	bam1_t *b = p->b;
-	bam1_core_t *c = &b->core;
-	uint32_t *cigar = bam_get_cigar(b);
-	int k;
-	// determine the current CIGAR operation
-//	fprintf(stderr, "%s\tpos=%d\tend=%d\t(%d,%d,%d)\n", bam_get_qname(b), pos, s->end, s->k, s->x, s->y);
-	if (s->k == -1) { // never processed
-		if (c->n_cigar == 1) { // just one operation, save a loop
-		  if (_cop(cigar[0]) == BAM_CMATCH || _cop(cigar[0]) == BAM_CEQUAL || _cop(cigar[0]) == BAM_CDIFF) s->k = 0, s->x = c->pos, s->y = 0;
-		} else { // find the first match or deletion
-			for (k = 0, s->x = c->pos, s->y = 0; k < c->n_cigar; ++k) {
-				int op = _cop(cigar[k]);
-				int l = _cln(cigar[k]);
-				if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CEQUAL || op == BAM_CDIFF) break;
-				else if (op == BAM_CREF_SKIP) s->x += l;
-				else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) s->y += l;
-			}
-			assert(k < c->n_cigar);
-			s->k = k;
-		}
-	} else { // the read has been processed before
-		int op, l = _cln(cigar[s->k]);
-		if (pos - s->x >= l) { // jump to the next operation
-			assert(s->k < c->n_cigar); // otherwise a bug: this function should not be called in this case
-			op = _cop(cigar[s->k+1]);
-			if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP || op == BAM_CEQUAL || op == BAM_CDIFF) { // jump to the next without a loop
-			  if (_cop(cigar[s->k]) == BAM_CMATCH|| _cop(cigar[s->k]) == BAM_CEQUAL || _cop(cigar[s->k]) == BAM_CDIFF) s->y += l;
-				s->x += l;
-				++s->k;
-			} else { // find the next M/D/N/=/X
-			  if (_cop(cigar[s->k]) == BAM_CMATCH|| _cop(cigar[s->k]) == BAM_CEQUAL || _cop(cigar[s->k]) == BAM_CDIFF) s->y += l;
-				s->x += l;
-				for (k = s->k + 1; k < c->n_cigar; ++k) {
-					op = _cop(cigar[k]), l = _cln(cigar[k]);
-					if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP || op == BAM_CEQUAL || op == BAM_CDIFF) break;
-					else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) s->y += l;
-				}
-				s->k = k;
-			}
-			assert(s->k < c->n_cigar); // otherwise a bug
-		} // else, do nothing
-	}
-	{ // collect pileup information
-		int op, l;
-		op = _cop(cigar[s->k]); l = _cln(cigar[s->k]);
-		p->is_del = p->indel = p->is_refskip = 0;
-		if (s->x + l - 1 == pos && s->k + 1 < c->n_cigar) { // peek the next operation
-			int op2 = _cop(cigar[s->k+1]);
-			int l2 = _cln(cigar[s->k+1]);
-			if (op2 == BAM_CDEL) p->indel = -(int)l2;
-			else if (op2 == BAM_CINS) p->indel = l2;
-			else if (op2 == BAM_CPAD && s->k + 2 < c->n_cigar) { // no working for adjacent padding
-				int l3 = 0;
-				for (k = s->k + 2; k < c->n_cigar; ++k) {
-					op2 = _cop(cigar[k]); l2 = _cln(cigar[k]);
-					if (op2 == BAM_CINS) l3 += l2;
-					else if (op2 == BAM_CDEL || op2 == BAM_CMATCH || op2 == BAM_CREF_SKIP || op2 == BAM_CEQUAL || op2 == BAM_CDIFF) break;
-				}
-				if (l3 > 0) p->indel = l3;
-			}
-		}
-		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-			p->qpos = s->y + (pos - s->x);
-		} else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
-			p->is_del = 1; p->qpos = s->y; // FIXME: distinguish D and N!!!!!
-			p->is_refskip = (op == BAM_CREF_SKIP);
-		} // cannot be other operations; otherwise a bug
-		p->is_head = (pos == c->pos); p->is_tail = (pos == s->end);
-	}
-	return 1;
+    bam1_t *b = p->b;
+    bam1_core_t *c = &b->core;
+    uint32_t *cigar = bam_get_cigar(b);
+    int k;
+    // determine the current CIGAR operation
+//  fprintf(stderr, "%s\tpos=%d\tend=%d\t(%d,%d,%d)\n", bam_get_qname(b), pos, s->end, s->k, s->x, s->y);
+    if (s->k == -1) { // never processed
+        if (c->n_cigar == 1) { // just one operation, save a loop
+          if (_cop(cigar[0]) == BAM_CMATCH || _cop(cigar[0]) == BAM_CEQUAL || _cop(cigar[0]) == BAM_CDIFF) s->k = 0, s->x = c->pos, s->y = 0;
+        } else { // find the first match or deletion
+            for (k = 0, s->x = c->pos, s->y = 0; k < c->n_cigar; ++k) {
+                int op = _cop(cigar[k]);
+                int l = _cln(cigar[k]);
+                if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CEQUAL || op == BAM_CDIFF) break;
+                else if (op == BAM_CREF_SKIP) s->x += l;
+                else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) s->y += l;
+            }
+            assert(k < c->n_cigar);
+            s->k = k;
+        }
+    } else { // the read has been processed before
+        int op, l = _cln(cigar[s->k]);
+        if (pos - s->x >= l) { // jump to the next operation
+            assert(s->k < c->n_cigar); // otherwise a bug: this function should not be called in this case
+            op = _cop(cigar[s->k+1]);
+            if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP || op == BAM_CEQUAL || op == BAM_CDIFF) { // jump to the next without a loop
+              if (_cop(cigar[s->k]) == BAM_CMATCH|| _cop(cigar[s->k]) == BAM_CEQUAL || _cop(cigar[s->k]) == BAM_CDIFF) s->y += l;
+                s->x += l;
+                ++s->k;
+            } else { // find the next M/D/N/=/X
+              if (_cop(cigar[s->k]) == BAM_CMATCH|| _cop(cigar[s->k]) == BAM_CEQUAL || _cop(cigar[s->k]) == BAM_CDIFF) s->y += l;
+                s->x += l;
+                for (k = s->k + 1; k < c->n_cigar; ++k) {
+                    op = _cop(cigar[k]), l = _cln(cigar[k]);
+                    if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP || op == BAM_CEQUAL || op == BAM_CDIFF) break;
+                    else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) s->y += l;
+                }
+                s->k = k;
+            }
+            assert(s->k < c->n_cigar); // otherwise a bug
+        } // else, do nothing
+    }
+    { // collect pileup information
+        int op, l;
+        op = _cop(cigar[s->k]); l = _cln(cigar[s->k]);
+        p->is_del = p->indel = p->is_refskip = 0;
+        if (s->x + l - 1 == pos && s->k + 1 < c->n_cigar) { // peek the next operation
+            int op2 = _cop(cigar[s->k+1]);
+            int l2 = _cln(cigar[s->k+1]);
+            if (op2 == BAM_CDEL) p->indel = -(int)l2;
+            else if (op2 == BAM_CINS) p->indel = l2;
+            else if (op2 == BAM_CPAD && s->k + 2 < c->n_cigar) { // no working for adjacent padding
+                int l3 = 0;
+                for (k = s->k + 2; k < c->n_cigar; ++k) {
+                    op2 = _cop(cigar[k]); l2 = _cln(cigar[k]);
+                    if (op2 == BAM_CINS) l3 += l2;
+                    else if (op2 == BAM_CDEL || op2 == BAM_CMATCH || op2 == BAM_CREF_SKIP || op2 == BAM_CEQUAL || op2 == BAM_CDIFF) break;
+                }
+                if (l3 > 0) p->indel = l3;
+            }
+        }
+        if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+            p->qpos = s->y + (pos - s->x);
+        } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
+            p->is_del = 1; p->qpos = s->y; // FIXME: distinguish D and N!!!!!
+            p->is_refskip = (op == BAM_CREF_SKIP);
+        } // cannot be other operations; otherwise a bug
+        p->is_head = (pos == c->pos); p->is_tail = (pos == s->end);
+    }
+    return 1;
 }
 
 /***********************
@@ -1319,34 +1361,34 @@ KHASH_MAP_INIT_STR(olap_hash, lbnode_t *)
 typedef khash_t(olap_hash) olap_hash_t;
 
 struct __bam_plp_t {
-	mempool_t *mp;
-	lbnode_t *head, *tail, *dummy;
-	int32_t tid, pos, max_tid, max_pos;
-	int is_eof, max_plp, error, maxcnt;
-	uint64_t id;
-	bam_pileup1_t *plp;
-	// for the "auto" interface only
-	bam1_t *b;
-	bam_plp_auto_f func;
-	void *data;
+    mempool_t *mp;
+    lbnode_t *head, *tail, *dummy;
+    int32_t tid, pos, max_tid, max_pos;
+    int is_eof, max_plp, error, maxcnt;
+    uint64_t id;
+    bam_pileup1_t *plp;
+    // for the "auto" interface only
+    bam1_t *b;
+    bam_plp_auto_f func;
+    void *data;
     olap_hash_t *overlaps;
 };
 
 bam_plp_t bam_plp_init(bam_plp_auto_f func, void *data)
 {
-	bam_plp_t iter;
-	iter = (bam_plp_t)calloc(1, sizeof(struct __bam_plp_t));
-	iter->mp = mp_init();
-	iter->head = iter->tail = mp_alloc(iter->mp);
-	iter->dummy = mp_alloc(iter->mp);
-	iter->max_tid = iter->max_pos = -1;
-	iter->maxcnt = 8000;
-	if (func) {
-		iter->func = func;
-		iter->data = data;
-		iter->b = bam_init1();
-	}
-	return iter;
+    bam_plp_t iter;
+    iter = (bam_plp_t)calloc(1, sizeof(struct __bam_plp_t));
+    iter->mp = mp_init();
+    iter->head = iter->tail = mp_alloc(iter->mp);
+    iter->dummy = mp_alloc(iter->mp);
+    iter->max_tid = iter->max_pos = -1;
+    iter->maxcnt = 8000;
+    if (func) {
+        iter->func = func;
+        iter->data = data;
+        iter->b = bam_init1();
+    }
+    return iter;
 }
 
 void bam_plp_init_overlaps(bam_plp_t iter)
@@ -1357,14 +1399,14 @@ void bam_plp_init_overlaps(bam_plp_t iter)
 void bam_plp_destroy(bam_plp_t iter)
 {
     if ( iter->overlaps ) kh_destroy(olap_hash, iter->overlaps);
-	mp_free(iter->mp, iter->dummy);
-	mp_free(iter->mp, iter->head);
-	if (iter->mp->cnt != 0)
-		fprintf(stderr, "[bam_plp_destroy] memory leak: %d. Continue anyway.\n", iter->mp->cnt);
-	mp_destroy(iter->mp);
-	if (iter->b) bam_destroy1(iter->b);
-	free(iter->plp);
-	free(iter);
+    mp_free(iter->mp, iter->dummy);
+    mp_free(iter->mp, iter->head);
+    if (iter->mp->cnt != 0)
+        fprintf(stderr, "[bam_plp_destroy] memory leak: %d. Continue anyway.\n", iter->mp->cnt);
+    mp_destroy(iter->mp);
+    if (iter->b) bam_destroy1(iter->b);
+    free(iter->plp);
+    free(iter);
 }
 
 
@@ -1396,16 +1438,16 @@ static inline int cigar_iref2iseq_set(uint32_t **cigar, uint32_t *cigar_max, int
         int ncig = (**cigar) >> BAM_CIGAR_SHIFT;
 
         if ( cig==BAM_CSOFT_CLIP ) { (*cigar)++; *iseq += ncig; *icig = 0; continue; }
-        if ( cig==BAM_CHARD_CLIP ) { (*cigar)++; *icig = 0; continue; }
-        if ( cig==BAM_CMATCH || cig==BAM_CEQUAL || cig==BAM_CDIFF ) 
-        { 
-            pos -= ncig; 
+        if ( cig==BAM_CHARD_CLIP || cig==BAM_CPAD ) { (*cigar)++; *icig = 0; continue; }
+        if ( cig==BAM_CMATCH || cig==BAM_CEQUAL || cig==BAM_CDIFF )
+        {
+            pos -= ncig;
             if ( pos < 0 ) { *icig = ncig + pos; *iseq += *icig; *iref += *icig; return BAM_CMATCH; }
             (*cigar)++; *iseq += ncig; *icig = 0; *iref += ncig;
             continue;
         }
         if ( cig==BAM_CINS ) { (*cigar)++; *iseq += ncig; *icig = 0; continue; }
-        if ( cig==BAM_CDEL ) 
+        if ( cig==BAM_CDEL || cig==BAM_CREF_SKIP )
         {
             pos -= ncig;
             if ( pos<0 ) pos = 0;
@@ -1425,21 +1467,21 @@ static inline int cigar_iref2iseq_next(uint32_t **cigar, uint32_t *cigar_max, in
         int cig  = (**cigar) & BAM_CIGAR_MASK;
         int ncig = (**cigar) >> BAM_CIGAR_SHIFT;
 
-        if ( cig==BAM_CMATCH || cig==BAM_CEQUAL || cig==BAM_CDIFF ) 
+        if ( cig==BAM_CMATCH || cig==BAM_CEQUAL || cig==BAM_CDIFF )
         {
             if ( *icig >= ncig - 1 ) { *icig = 0;  (*cigar)++; continue; }
-            (*iseq)++; (*icig)++; (*iref)++; 
-            return BAM_CMATCH; 
+            (*iseq)++; (*icig)++; (*iref)++;
+            return BAM_CMATCH;
         }
-        if ( cig==BAM_CDEL ) { (*cigar)++; (*iref) += ncig; *icig = 0; continue; }
+        if ( cig==BAM_CDEL || cig==BAM_CREF_SKIP ) { (*cigar)++; (*iref) += ncig; *icig = 0; continue; }
         if ( cig==BAM_CINS ) { (*cigar)++; *iseq += ncig; *icig = 0; continue; }
         if ( cig==BAM_CSOFT_CLIP ) { (*cigar)++; *iseq += ncig; *icig = 0; continue; }
-        if ( cig==BAM_CHARD_CLIP ) { (*cigar)++; *icig = 0; continue; }
+        if ( cig==BAM_CHARD_CLIP || cig==BAM_CPAD ) { (*cigar)++; *icig = 0; continue; }
         fprintf(stderr,"todo: cigar %d\n", cig);
         assert(0);
     }
     *iseq = -1;
-    *iref = -1; 
+    *iref = -1;
     return -1;
 }
 
@@ -1461,19 +1503,19 @@ static void tweak_overlap_quality(bam1_t *a, bam1_t *b)
     if ( b_ret<0 ) return;  // no overlap
 
     #if DBG
-        fprintf(stderr,"tweak %s  n_cigar=%d %d  .. %d-%d vs %d-%d\n", bam_get_qname(a), a->core.n_cigar, b->core.n_cigar, 
+        fprintf(stderr,"tweak %s  n_cigar=%d %d  .. %d-%d vs %d-%d\n", bam_get_qname(a), a->core.n_cigar, b->core.n_cigar,
             a->core.pos+1,a->core.pos+bam_cigar2rlen(a->core.n_cigar,bam_get_cigar(a)), b->core.pos+1, b->core.pos+bam_cigar2rlen(b->core.n_cigar,bam_get_cigar(b)));
     #endif
 
     while ( 1 )
     {
         // Increment reference position
-        while ( a_iref>=0 && a_iref < iref - a->core.pos ) 
+        while ( a_iref>=0 && a_iref < iref - a->core.pos )
             a_ret = cigar_iref2iseq_next(&a_cigar, a_cigar_max, &a_icig, &a_iseq, &a_iref);
         if ( a_ret<0 ) break;   // done
         if ( iref < a_iref + a->core.pos ) iref = a_iref + a->core.pos;
 
-        while ( b_iref>=0 && b_iref < iref - b->core.pos ) 
+        while ( b_iref>=0 && b_iref < iref - b->core.pos )
             b_ret = cigar_iref2iseq_next(&b_cigar, b_cigar_max, &b_icig, &b_iseq, &b_iref);
         if ( b_ret<0 ) break;   // done
         if ( iref < b_iref + b->core.pos ) iref = b_iref + b->core.pos;
@@ -1481,7 +1523,7 @@ static void tweak_overlap_quality(bam1_t *a, bam1_t *b)
         iref++;
         if ( a_iref+a->core.pos != b_iref+b->core.pos ) continue;   // only CMATCH positions, don't know what to do with indels
 
-        if ( bam_seqi(a_seq,a_iseq) == bam_seqi(b_seq,b_iseq) ) 
+        if ( bam_seqi(a_seq,a_iseq) == bam_seqi(b_seq,b_iseq) )
         {
             #if DBG
                 fprintf(stderr,"%c",seq_nt16_str[bam_seqi(a_seq,a_iseq)]);
@@ -1573,131 +1615,131 @@ static void overlap_remove(bam_plp_t iter, const bam1_t *b)
 // buffer yet (the current position is still the maximum position across all buffered reads).
 const bam_pileup1_t *bam_plp_next(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp)
 {
-	if (iter->error) { *_n_plp = -1; return 0; }
-	*_n_plp = 0;
-	if (iter->is_eof && iter->head->next == 0) return 0;
-	while (iter->is_eof || iter->max_tid > iter->tid || (iter->max_tid == iter->tid && iter->max_pos > iter->pos)) {
-		int n_plp = 0;
-		lbnode_t *p, *q;
-		// write iter->plp at iter->pos
-		iter->dummy->next = iter->head;
-		for (p = iter->head, q = iter->dummy; p->next; q = p, p = p->next) {
-			if (p->b.core.tid < iter->tid || (p->b.core.tid == iter->tid && p->end <= iter->pos)) { // then remove
+    if (iter->error) { *_n_plp = -1; return 0; }
+    *_n_plp = 0;
+    if (iter->is_eof && iter->head->next == 0) return 0;
+    while (iter->is_eof || iter->max_tid > iter->tid || (iter->max_tid == iter->tid && iter->max_pos > iter->pos)) {
+        int n_plp = 0;
+        lbnode_t *p, *q;
+        // write iter->plp at iter->pos
+        iter->dummy->next = iter->head;
+        for (p = iter->head, q = iter->dummy; p->next; q = p, p = p->next) {
+            if (p->b.core.tid < iter->tid || (p->b.core.tid == iter->tid && p->end <= iter->pos)) { // then remove
                 overlap_remove(iter, &p->b);
-				q->next = p->next; mp_free(iter->mp, p); p = q;
-			} else if (p->b.core.tid == iter->tid && p->beg <= iter->pos) { // here: p->end > pos; then add to pileup
-				if (n_plp == iter->max_plp) { // then double the capacity
-					iter->max_plp = iter->max_plp? iter->max_plp<<1 : 256;
-					iter->plp = (bam_pileup1_t*)realloc(iter->plp, sizeof(bam_pileup1_t) * iter->max_plp);
-				}
-				iter->plp[n_plp].b = &p->b;
-				if (resolve_cigar2(iter->plp + n_plp, iter->pos, &p->s)) ++n_plp; // actually always true...
-			}
-		}
-		iter->head = iter->dummy->next; // dummy->next may be changed
-		*_n_plp = n_plp; *_tid = iter->tid; *_pos = iter->pos;
-		// update iter->tid and iter->pos
-		if (iter->head->next) {
-			if (iter->tid > iter->head->b.core.tid) {
-				fprintf(stderr, "[%s] unsorted input. Pileup aborts.\n", __func__);
-				iter->error = 1;
-				*_n_plp = -1;
-				return 0;
-			}
-		}
-		if (iter->tid < iter->head->b.core.tid) { // come to a new reference sequence
-			iter->tid = iter->head->b.core.tid; iter->pos = iter->head->beg; // jump to the next reference
-		} else if (iter->pos < iter->head->beg) { // here: tid == head->b.core.tid
-			iter->pos = iter->head->beg; // jump to the next position
-		} else ++iter->pos; // scan contiguously
-		// return
-		if (n_plp) return iter->plp;
-		if (iter->is_eof && iter->head->next == 0) break;
-	}
-	return 0;
+                q->next = p->next; mp_free(iter->mp, p); p = q;
+            } else if (p->b.core.tid == iter->tid && p->beg <= iter->pos) { // here: p->end > pos; then add to pileup
+                if (n_plp == iter->max_plp) { // then double the capacity
+                    iter->max_plp = iter->max_plp? iter->max_plp<<1 : 256;
+                    iter->plp = (bam_pileup1_t*)realloc(iter->plp, sizeof(bam_pileup1_t) * iter->max_plp);
+                }
+                iter->plp[n_plp].b = &p->b;
+                if (resolve_cigar2(iter->plp + n_plp, iter->pos, &p->s)) ++n_plp; // actually always true...
+            }
+        }
+        iter->head = iter->dummy->next; // dummy->next may be changed
+        *_n_plp = n_plp; *_tid = iter->tid; *_pos = iter->pos;
+        // update iter->tid and iter->pos
+        if (iter->head->next) {
+            if (iter->tid > iter->head->b.core.tid) {
+                fprintf(stderr, "[%s] unsorted input. Pileup aborts.\n", __func__);
+                iter->error = 1;
+                *_n_plp = -1;
+                return 0;
+            }
+        }
+        if (iter->tid < iter->head->b.core.tid) { // come to a new reference sequence
+            iter->tid = iter->head->b.core.tid; iter->pos = iter->head->beg; // jump to the next reference
+        } else if (iter->pos < iter->head->beg) { // here: tid == head->b.core.tid
+            iter->pos = iter->head->beg; // jump to the next position
+        } else ++iter->pos; // scan contiguously
+        // return
+        if (n_plp) return iter->plp;
+        if (iter->is_eof && iter->head->next == 0) break;
+    }
+    return 0;
 }
 
 int bam_plp_push(bam_plp_t iter, const bam1_t *b)
 {
-	if (iter->error) return -1;
-	if (b) {
-		if (b->core.tid < 0) { overlap_remove(iter, b); return 0; }
+    if (iter->error) return -1;
+    if (b) {
+        if (b->core.tid < 0) { overlap_remove(iter, b); return 0; }
         // Skip only unmapped reads here, any additional filtering must be done in iter->func
         if (b->core.flag & BAM_FUNMAP) { overlap_remove(iter, b); return 0; }
-		if (iter->tid == b->core.tid && iter->pos == b->core.pos && iter->mp->cnt > iter->maxcnt) 
-        { 
-            overlap_remove(iter, b); 
-            return 0; 
+        if (iter->tid == b->core.tid && iter->pos == b->core.pos && iter->mp->cnt > iter->maxcnt)
+        {
+            overlap_remove(iter, b);
+            return 0;
         }
-		bam_copy1(&iter->tail->b, b);
+        bam_copy1(&iter->tail->b, b);
         overlap_push(iter, iter->tail);
 #ifndef BAM_NO_ID
-		iter->tail->b.id = iter->id++;
+        iter->tail->b.id = iter->id++;
 #endif
-		iter->tail->beg = b->core.pos;
-		iter->tail->end = b->core.pos + bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b));
-		iter->tail->s = g_cstate_null; iter->tail->s.end = iter->tail->end - 1; // initialize cstate_t
-		if (b->core.tid < iter->max_tid) {
-			fprintf(stderr, "[bam_pileup_core] the input is not sorted (chromosomes out of order)\n");
-			iter->error = 1;
-			return -1;
-		}
-		if ((b->core.tid == iter->max_tid) && (iter->tail->beg < iter->max_pos)) {
-			fprintf(stderr, "[bam_pileup_core] the input is not sorted (reads out of order)\n");
-			iter->error = 1;
-			return -1;
-		}
-		iter->max_tid = b->core.tid; iter->max_pos = iter->tail->beg;
-		if (iter->tail->end > iter->pos || iter->tail->b.core.tid > iter->tid) {
-			iter->tail->next = mp_alloc(iter->mp);
-			iter->tail = iter->tail->next;
-		}
-	} else iter->is_eof = 1;
-	return 0;
+        iter->tail->beg = b->core.pos;
+        iter->tail->end = b->core.pos + bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b));
+        iter->tail->s = g_cstate_null; iter->tail->s.end = iter->tail->end - 1; // initialize cstate_t
+        if (b->core.tid < iter->max_tid) {
+            fprintf(stderr, "[bam_pileup_core] the input is not sorted (chromosomes out of order)\n");
+            iter->error = 1;
+            return -1;
+        }
+        if ((b->core.tid == iter->max_tid) && (iter->tail->beg < iter->max_pos)) {
+            fprintf(stderr, "[bam_pileup_core] the input is not sorted (reads out of order)\n");
+            iter->error = 1;
+            return -1;
+        }
+        iter->max_tid = b->core.tid; iter->max_pos = iter->tail->beg;
+        if (iter->tail->end > iter->pos || iter->tail->b.core.tid > iter->tid) {
+            iter->tail->next = mp_alloc(iter->mp);
+            iter->tail = iter->tail->next;
+        }
+    } else iter->is_eof = 1;
+    return 0;
 }
 
 const bam_pileup1_t *bam_plp_auto(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp)
 {
-	const bam_pileup1_t *plp;
-	if (iter->func == 0 || iter->error) { *_n_plp = -1; return 0; }
-	if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp;
-	else { // no pileup line can be obtained; read alignments
-		*_n_plp = 0;
-		if (iter->is_eof) return 0;
+    const bam_pileup1_t *plp;
+    if (iter->func == 0 || iter->error) { *_n_plp = -1; return 0; }
+    if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp;
+    else { // no pileup line can be obtained; read alignments
+        *_n_plp = 0;
+        if (iter->is_eof) return 0;
         int ret;
-		while ( (ret=iter->func(iter->data, iter->b)) >= 0) {
-			if (bam_plp_push(iter, iter->b) < 0) {
-				*_n_plp = -1;
-				return 0;
-			}
-			if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp;
-			// otherwise no pileup line can be returned; read the next alignment.
-		}
+        while ( (ret=iter->func(iter->data, iter->b)) >= 0) {
+            if (bam_plp_push(iter, iter->b) < 0) {
+                *_n_plp = -1;
+                return 0;
+            }
+            if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp;
+            // otherwise no pileup line can be returned; read the next alignment.
+        }
         if ( ret < -1 ) { iter->error = ret; *_n_plp = -1; return 0; }
-		bam_plp_push(iter, 0);
-		if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp;
-		return 0;
-	}
+        bam_plp_push(iter, 0);
+        if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp;
+        return 0;
+    }
 }
 
 void bam_plp_reset(bam_plp_t iter)
 {
-	lbnode_t *p, *q;
-	iter->max_tid = iter->max_pos = -1;
-	iter->tid = iter->pos = 0;
-	iter->is_eof = 0;
-	for (p = iter->head; p->next;) {
+    lbnode_t *p, *q;
+    iter->max_tid = iter->max_pos = -1;
+    iter->tid = iter->pos = 0;
+    iter->is_eof = 0;
+    for (p = iter->head; p->next;) {
         overlap_remove(iter, NULL);
-		q = p->next;
-		mp_free(iter->mp, p);
-		p = q;
-	}
-	iter->head = iter->tail;
+        q = p->next;
+        mp_free(iter->mp, p);
+        p = q;
+    }
+    iter->head = iter->tail;
 }
 
 void bam_plp_set_maxcnt(bam_plp_t iter, int maxcnt)
 {
-	iter->maxcnt = maxcnt;
+    iter->maxcnt = maxcnt;
 }
 
 /************************
@@ -1705,29 +1747,29 @@ void bam_plp_set_maxcnt(bam_plp_t iter, int maxcnt)
  ************************/
 
 struct __bam_mplp_t {
-	int n;
-	uint64_t min, *pos;
-	bam_plp_t *iter;
-	int *n_plp;
-	const bam_pileup1_t **plp;
+    int n;
+    uint64_t min, *pos;
+    bam_plp_t *iter;
+    int *n_plp;
+    const bam_pileup1_t **plp;
 };
 
 bam_mplp_t bam_mplp_init(int n, bam_plp_auto_f func, void **data)
 {
-	int i;
-	bam_mplp_t iter;
-	iter = (bam_mplp_t)calloc(1, sizeof(struct __bam_mplp_t));
-	iter->pos = (uint64_t*)calloc(n, 8);
-	iter->n_plp = (int*)calloc(n, sizeof(int));
-	iter->plp = (const bam_pileup1_t**)calloc(n, sizeof(bam_pileup1_t*));
-	iter->iter = (bam_plp_t*)calloc(n, sizeof(bam_plp_t));
-	iter->n = n;
-	iter->min = (uint64_t)-1;
-	for (i = 0; i < n; ++i) {
-		iter->iter[i] = bam_plp_init(func, data[i]);
-		iter->pos[i] = iter->min;
-	}
-	return iter;
+    int i;
+    bam_mplp_t iter;
+    iter = (bam_mplp_t)calloc(1, sizeof(struct __bam_mplp_t));
+    iter->pos = (uint64_t*)calloc(n, 8);
+    iter->n_plp = (int*)calloc(n, sizeof(int));
+    iter->plp = (const bam_pileup1_t**)calloc(n, sizeof(bam_pileup1_t*));
+    iter->iter = (bam_plp_t*)calloc(n, sizeof(bam_plp_t));
+    iter->n = n;
+    iter->min = (uint64_t)-1;
+    for (i = 0; i < n; ++i) {
+        iter->iter[i] = bam_plp_init(func, data[i]);
+        iter->pos[i] = iter->min;
+    }
+    return iter;
 }
 
 void bam_mplp_init_overlaps(bam_mplp_t iter)
@@ -1739,42 +1781,42 @@ void bam_mplp_init_overlaps(bam_mplp_t iter)
 
 void bam_mplp_set_maxcnt(bam_mplp_t iter, int maxcnt)
 {
-	int i;
-	for (i = 0; i < iter->n; ++i)
-		iter->iter[i]->maxcnt = maxcnt;
+    int i;
+    for (i = 0; i < iter->n; ++i)
+        iter->iter[i]->maxcnt = maxcnt;
 }
 
 void bam_mplp_destroy(bam_mplp_t iter)
 {
-	int i;
-	for (i = 0; i < iter->n; ++i) bam_plp_destroy(iter->iter[i]);
-	free(iter->iter); free(iter->pos); free(iter->n_plp); free(iter->plp);
-	free(iter);
+    int i;
+    for (i = 0; i < iter->n; ++i) bam_plp_destroy(iter->iter[i]);
+    free(iter->iter); free(iter->pos); free(iter->n_plp); free(iter->plp);
+    free(iter);
 }
 
 int bam_mplp_auto(bam_mplp_t iter, int *_tid, int *_pos, int *n_plp, const bam_pileup1_t **plp)
 {
-	int i, ret = 0;
-	uint64_t new_min = (uint64_t)-1;
-	for (i = 0; i < iter->n; ++i) {
-		if (iter->pos[i] == iter->min) {
-			int tid, pos;
-			iter->plp[i] = bam_plp_auto(iter->iter[i], &tid, &pos, &iter->n_plp[i]);
+    int i, ret = 0;
+    uint64_t new_min = (uint64_t)-1;
+    for (i = 0; i < iter->n; ++i) {
+        if (iter->pos[i] == iter->min) {
+            int tid, pos;
+            iter->plp[i] = bam_plp_auto(iter->iter[i], &tid, &pos, &iter->n_plp[i]);
             if ( iter->iter[i]->error ) return -1;
-			iter->pos[i] = iter->plp[i] ? (uint64_t)tid<<32 | pos : 0;
-		}
-		if (iter->plp[i] && iter->pos[i] < new_min) new_min = iter->pos[i];
-	}
-	iter->min = new_min;
-	if (new_min == (uint64_t)-1) return 0;
-	*_tid = new_min>>32; *_pos = (uint32_t)new_min;
-	for (i = 0; i < iter->n; ++i) {
-		if (iter->pos[i] == iter->min) { // FIXME: valgrind reports "uninitialised value(s) at this line"
-			n_plp[i] = iter->n_plp[i], plp[i] = iter->plp[i];
-			++ret;
-		} else n_plp[i] = 0, plp[i] = 0;
-	}
-	return ret;
+            iter->pos[i] = iter->plp[i] ? (uint64_t)tid<<32 | pos : 0;
+        }
+        if (iter->plp[i] && iter->pos[i] < new_min) new_min = iter->pos[i];
+    }
+    iter->min = new_min;
+    if (new_min == (uint64_t)-1) return 0;
+    *_tid = new_min>>32; *_pos = (uint32_t)new_min;
+    for (i = 0; i < iter->n; ++i) {
+        if (iter->pos[i] == iter->min) { // FIXME: valgrind reports "uninitialised value(s) at this line"
+            n_plp[i] = iter->n_plp[i], plp[i] = iter->plp[i];
+            ++ret;
+        } else n_plp[i] = 0, plp[i] = 0;
+    }
+    return ret;
 }
 
 #endif // ~!defined(BAM_NO_PILEUP)
diff --git a/htslib/synced_bcf_reader.c b/htslib/synced_bcf_reader.c
index 5fc9594..19fa703 100644
--- a/htslib/synced_bcf_reader.c
+++ b/htslib/synced_bcf_reader.c
@@ -1,3 +1,27 @@
+/*  synced_bcf_reader.c -- stream through multiple VCF files.
+
+    Copyright (C) 2012-2014 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
@@ -17,7 +41,7 @@ typedef struct
 }
 region1_t;
 
-typedef struct _region_t 
+typedef struct _region_t
 {
     region1_t *regs;
     int nregs, mregs, creg;
@@ -60,7 +84,7 @@ static int *init_filters(bcf_hdr_t *hdr, const char *filters, int *nfilters)
 int bcf_sr_set_regions(bcf_srs_t *readers, const char *regions, int is_file)
 {
     assert( !readers->regions );
-    if ( readers->nreaders ) 
+    if ( readers->nreaders )
     {
         fprintf(stderr,"[%s:%d %s] Error: bcf_sr_set_regions() must be called before bcf_sr_add_reader()\n", __FILE__,__LINE__,__FUNCTION__);
         return -1;
@@ -74,6 +98,11 @@ int bcf_sr_set_regions(bcf_srs_t *readers, const char *regions, int is_file)
 int bcf_sr_set_targets(bcf_srs_t *readers, const char *targets, int is_file, int alleles)
 {
     assert( !readers->targets );
+    if ( targets[0]=='^' )
+    {
+        readers->targets_exclude = 1;
+        targets++;
+    }
     readers->targets = bcf_sr_regions_init(targets,is_file,0,1,-2);
     if ( !readers->targets ) return -1;
     readers->targets_als = alleles;
@@ -96,7 +125,7 @@ int bcf_sr_add_reader(bcf_srs_t *files, const char *fname)
 
     if ( files->require_index )
     {
-        if ( reader->type==FT_VCF_GZ ) 
+        if ( reader->type==FT_VCF_GZ )
         {
             reader->tbx_idx = tbx_index_load(fname);
             if ( !reader->tbx_idx )
@@ -107,12 +136,12 @@ int bcf_sr_add_reader(bcf_srs_t *files, const char *fname)
 
             reader->header = bcf_hdr_read(reader->file);
         }
-        else if ( reader->type==FT_BCF_GZ ) 
+        else if ( reader->type==FT_BCF_GZ )
         {
             reader->header = bcf_hdr_read(reader->file);
 
             reader->bcf_idx = bcf_index_load(fname);
-            if ( !reader->bcf_idx ) 
+            if ( !reader->bcf_idx )
             {
                 fprintf(stderr,"[add_reader] Could not load the index of %s\n", fname);
                 return 0;   // not indexed..?
@@ -124,7 +153,7 @@ int bcf_sr_add_reader(bcf_srs_t *files, const char *fname)
             return 0;
         }
     }
-    else 
+    else
     {
         if ( reader->type & FT_BCF )
         {
@@ -214,7 +243,7 @@ void bcf_sr_remove_reader(bcf_srs_t *files, int i)
 {
     assert( !files->samples );  // not ready for this yet
     bcf_sr_destroy1(&files->readers[i]);
-    if ( i+1 < files->nreaders ) 
+    if ( i+1 < files->nreaders )
     {
         memmove(&files->readers[i], &files->readers[i+1], (files->nreaders-i-1)*sizeof(bcf_sr_t));
         memmove(&files->has_line[i], &files->has_line[i+1], (files->nreaders-i-1)*sizeof(int));
@@ -317,10 +346,10 @@ static int _reader_seek(bcf_sr_t *reader, const char *seq, int start, int end)
         fprintf(stderr,"The coordinate is out of csi index limit: %d\n", end+1);
         exit(1);
     }
-    if ( reader->itr ) 
+    if ( reader->itr )
     {
-        hts_itr_destroy(reader->itr); 
-        reader->itr = NULL; 
+        hts_itr_destroy(reader->itr);
+        reader->itr = NULL;
     }
     reader->nbuffer = 0;
     if ( reader->tbx_idx )
@@ -380,7 +409,7 @@ static void _reader_fill_buffer(bcf_srs_t *files, bcf_sr_t *reader)
     int i, ret = 0;
     while (1)
     {
-        if ( reader->nbuffer+1 >= reader->mbuffer ) 
+        if ( reader->nbuffer+1 >= reader->mbuffer )
         {
             // Increase buffer size
             reader->mbuffer += 8;
@@ -433,11 +462,11 @@ static void _reader_fill_buffer(bcf_srs_t *files, bcf_sr_t *reader)
 
         if ( reader->buffer[reader->nbuffer]->pos != reader->buffer[1]->pos ) break;    // the buffer is full
     }
-    if ( ret<0 ) 
-    { 
+    if ( ret<0 )
+    {
         // done for this region
         tbx_itr_destroy(reader->itr);
-        reader->itr = NULL; 
+        reader->itr = NULL;
     }
     if ( files->collapse && reader->nbuffer>=2 && reader->buffer[1]->pos==reader->buffer[2]->pos )
         collapse_buffer(files, reader);
@@ -458,7 +487,7 @@ static void _reader_shift_buffer(bcf_sr_t *reader)
         bcf1_t *tmp = reader->buffer[1]; reader->buffer[1] = reader->buffer[i]; reader->buffer[i] = tmp;
         reader->nbuffer = 1;
     }
-    else 
+    else
         reader->nbuffer = 0;    // no other line
 }
 
@@ -511,7 +540,9 @@ static int _reader_match_alleles(bcf_srs_t *files, bcf_sr_t *reader, bcf1_t *tmp
                 if ( nmatch==tmpl->n_allele ) { irec=i; break; }    // found: exact match
                 continue;
             }
-            
+
+            if ( line->n_allele==1 && tmpl->n_allele==1 ) { irec=i; break; }    // both sites are non-variant
+
             // COLLAPSE_SOME: at least some ALTs must match
             for (ial=1; ial<tmpl->n_allele; ial++)
             {
@@ -553,13 +584,13 @@ int _reader_next_line(bcf_srs_t *files)
 
             // Update the minimum coordinate
             if ( !files->readers[i].nbuffer ) continue;
-            if ( min_pos > files->readers[i].buffer[1]->pos ) 
+            if ( min_pos > files->readers[i].buffer[1]->pos )
             {
-                min_pos = files->readers[i].buffer[1]->pos; 
+                min_pos = files->readers[i].buffer[1]->pos;
                 chr = bcf_seqname(files->readers[i].header, files->readers[i].buffer[1]);
             }
         }
-        if ( min_pos==INT_MAX ) 
+        if ( min_pos==INT_MAX )
         {
             if ( !files->regions ) break;
             continue;
@@ -568,18 +599,19 @@ int _reader_next_line(bcf_srs_t *files)
         // Skip this position if not present in targets
         if ( files->targets )
         {
-            if ( bcf_sr_regions_overlap(files->targets, chr, min_pos, min_pos)<0 ) 
+            int ret = bcf_sr_regions_overlap(files->targets, chr, min_pos, min_pos);
+            if ( (!files->targets_exclude && ret<0) || (files->targets_exclude && !ret) )
             {
                 // Remove all lines with this position from the buffer
                 for (i=0; i<files->nreaders; i++)
-                    if ( files->readers[i].nbuffer && files->readers[i].buffer[1]->pos==min_pos ) 
+                    if ( files->readers[i].nbuffer && files->readers[i].buffer[1]->pos==min_pos )
                         _reader_shift_buffer(&files->readers[i]);
                 min_pos = INT_MAX;
                 continue;
             }
         }
-        
-        break;  // done: min_pos is set 
+
+        break;  // done: min_pos is set
     }
 
     // There can be records with duplicate positions. Set the active line intelligently so that
@@ -589,7 +621,7 @@ int _reader_next_line(bcf_srs_t *files)
     for (i=0; i<files->nreaders; i++)
     {
         files->has_line[i] = 0;
-        
+
         // Skip readers with no records at this position
         if ( !files->readers[i].nbuffer || files->readers[i].buffer[1]->pos!=min_pos ) continue;
 
@@ -606,19 +638,19 @@ int _reader_next_line(bcf_srs_t *files)
 
 int bcf_sr_next_line(bcf_srs_t *files)
 {
-    if ( !files->targets_als ) 
+    if ( !files->targets_als )
         return _reader_next_line(files);
 
     while (1)
     {
         int i, ret = _reader_next_line(files);
         if ( !ret ) return ret;
-        
+
         for (i=0; i<files->nreaders; i++)
             if ( files->has_line[i] ) break;
 
         if ( _regions_match_alleles(files->targets, files->targets_als-1, files->readers[i].buffer[0]) ) return ret;
-        
+
         // Check if there are more duplicate lines in the buffers. If not, return this line as if it
         // matched the targets, even if there is a type mismatch
         for (i=0; i<files->nreaders; i++)
@@ -643,7 +675,7 @@ static void bcf_sr_seek_start(bcf_srs_t *readers)
 
 int bcf_sr_seek(bcf_srs_t *readers, const char *seq, int pos)
 {
-    if ( !seq && !pos ) 
+    if ( !seq && !pos )
     {
         // seek to start
         bcf_sr_seek_start(readers);
@@ -652,7 +684,7 @@ int bcf_sr_seek(bcf_srs_t *readers, const char *seq, int pos)
 
     bcf_sr_regions_overlap(readers->regions, seq, pos, pos);
     int i, nret = 0;
-    for (i=0; i<readers->nreaders; i++) 
+    for (i=0; i<readers->nreaders; i++)
     {
         nret += _reader_seek(&readers->readers[i],seq,pos,MAX_CSI_COOR-1);
     }
@@ -663,12 +695,12 @@ int bcf_sr_set_samples(bcf_srs_t *files, const char *fname, int is_file)
 {
     int i, j, nsmpl, free_smpl = 0;
     char **smpl = NULL;
-    
+
     void *exclude = (fname[0]=='^') ? khash_str2int_init() : NULL;
     if ( exclude || strcmp("-",fname) ) // "-" stands for all samples
     {
         smpl = hts_readlist(fname, is_file, &nsmpl);
-        if ( !smpl ) 
+        if ( !smpl )
         {
             fprintf(stderr,"Could not read the file: \"%s\"\n", fname);
             return 0;
@@ -709,15 +741,15 @@ int bcf_sr_set_samples(bcf_srs_t *files, const char *fname, int is_file)
     }
 
     if ( exclude ) khash_str2int_destroy(exclude);
-    if ( free_smpl ) 
+    if ( free_smpl )
     {
         for (i=0; i<nsmpl; i++) free(smpl[i]);
         free(smpl);
     }
 
-    if ( !files->n_smpl ) 
+    if ( !files->n_smpl )
     {
-        if ( files->nreaders>1 ) 
+        if ( files->nreaders>1 )
             fprintf(stderr,"No samples in common.\n");
         return 0;
     }
@@ -852,13 +884,13 @@ static int _regions_parse_line(char *line, int ichr,int ifrom,int ito, char **ch
     if ( line[0]=='#' ) return 0;
 
     int k,l;    // index of the start and end column of the tab-delimited file
-    if ( ifrom <= ito ) 
+    if ( ifrom <= ito )
         k = ifrom, l = ito;
-    else 
+    else
         l = ifrom, k = ito;
 
     int i;
-    char *se = line, *ss = NULL; // start and end 
+    char *se = line, *ss = NULL; // start and end
     char *tmp;
     for (i=0; i<=k && *se; i++)
     {
@@ -873,7 +905,7 @@ static int _regions_parse_line(char *line, int ichr,int ifrom,int ito, char **ch
     }
     else
     {
-        if ( k==ifrom ) 
+        if ( k==ifrom )
             *from = strtol(ss, &tmp, 10);
         else
             *to = strtol(ss, &tmp, 10);
@@ -885,7 +917,7 @@ static int _regions_parse_line(char *line, int ichr,int ifrom,int ito, char **ch
             while (*se && *se!='\t') se++;
         }
         if ( i<l ) return -1;
-        if ( k==ifrom ) 
+        if ( k==ifrom )
             *to = strtol(ss, &tmp, 10);
         else
             *from = strtol(ss, &tmp, 10);
@@ -922,7 +954,7 @@ bcf_sr_regions_t *bcf_sr_regions_init(const char *regions, int is_file, int ichr
     }
 
     reg->tbx = tbx_index_load(regions);
-    if ( !reg->tbx ) 
+    if ( !reg->tbx )
     {
         int len = strlen(regions);
         int is_bed  = strcasecmp(".bed",regions+len-4) ? 0 : 1;
@@ -936,14 +968,14 @@ bcf_sr_regions_t *bcf_sr_regions_init(const char *regions, int is_file, int ichr
             char *chr, *chr_end;
             int from, to, ret;
             ret = _regions_parse_line(reg->line.s, ichr,ifrom,abs(ito), &chr,&chr_end,&from,&to);
-            if ( ret < 0 ) 
+            if ( ret < 0 )
             {
                 if ( ito<0 )
                     ret = _regions_parse_line(reg->line.s, ichr,ifrom,ifrom, &chr,&chr_end,&from,&to);
                 if ( ret<0 )
                 {
                     fprintf(stderr,"[%s:%d] Could not parse the file %s, using the columns %d,%d[,%d]\n", __FILE__,__LINE__,regions,ichr+1,ifrom+1,ito+1);
-                    hts_close(reg->file); reg->file = NULL; free(reg); 
+                    hts_close(reg->file); reg->file = NULL; free(reg);
                     return NULL;
                 }
             }
@@ -981,11 +1013,11 @@ void bcf_sr_regions_destroy(bcf_sr_regions_t *reg)
     if ( reg->als ) free(reg->als);
     if ( reg->als_str.s ) free(reg->als_str.s);
     free(reg->line.s);
-    if ( reg->regs ) 
+    if ( reg->regs )
     {
          // free only in-memory names, tbx names are const
-        for (i=0; i<reg->nseqs; i++) 
-        {   
+        for (i=0; i<reg->nseqs; i++)
+        {
             free(reg->seq_names[i]);
             free(reg->regs[i].regs);
         }
@@ -1078,7 +1110,7 @@ int bcf_sr_regions_next(bcf_sr_regions_t *reg)
             if ( ret<0 ) { reg->iseq = -1; return -1; }
         }
         ret = _regions_parse_line(reg->line.s, ichr,ifrom,ito, &chr,&chr_end,&from,&to);
-        if ( ret<0 ) 
+        if ( ret<0 )
         {
             fprintf(stderr,"[%s:%d] Could not parse the file %s, using the columns %d,%d,%d\n", __FILE__,__LINE__,reg->fname,ichr+1,ifrom+1,ito+1);
             return -1;
@@ -1112,10 +1144,10 @@ static int _regions_match_alleles(bcf_sr_regions_t *reg, int als_idx, bcf1_t *re
         }
         char *se = ss;
         reg->nals = 1;
-        while ( *se && *se!='\t' ) 
-        { 
+        while ( *se && *se!='\t' )
+        {
             if ( *se==',' ) reg->nals++;
-            se++; 
+            se++;
         }
         ks_resize(&reg->als_str, se-ss+1+reg->nals);
         reg->als_str.l = 0;
diff --git a/htslib/tabix.c b/htslib/tabix.c
index 2a30fed..b0af21d 100644
--- a/htslib/tabix.c
+++ b/htslib/tabix.c
@@ -1,3 +1,28 @@
+/*  tabix.c -- Generic indexer for TAB-delimited genome position files.
+
+    Copyright (C) 2009-2011 Broad Institute.
+    Copyright (C) 2010-2012, 2014 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -90,7 +115,7 @@ static int query_regions(char **argv, int argc, int mode)
     {
         htsFile *fp = hts_open(fname,"r");
         if ( !fp ) error("Could not read %s\n", fname);
-        htsFile *out = hts_open("-","w"); 
+        htsFile *out = hts_open("-","w");
         if ( !out ) error("Could not open stdout\n", fname);
         hts_idx_t *idx = bcf_index_load(fname);
         if ( !idx ) error("Could not load .csi index of %s\n", fname);
@@ -145,7 +170,7 @@ static int query_chroms(char *fname)
         hts_idx_t *idx = bcf_index_load(fname);
         if ( !idx ) error("Could not load .csi index of %s\n", fname);
         seq = bcf_index_seqnames(idx, hdr, &nseq);
-        for (i=0; i<nseq; i++) 
+        for (i=0; i<nseq; i++)
             printf("%s\n", seq[i]);
         free(seq);
         bcf_hdr_destroy(hdr);
@@ -231,10 +256,10 @@ int reheader_file(const char *fname, const char *header, int ftype, tbx_conf_t *
 
 static int usage(void)
 {
-	fprintf(stderr, "\n");
-	fprintf(stderr, "Version: %s\n", hts_version());
-	fprintf(stderr, "Usage:   tabix [OPTIONS] [FILE] [REGION [...]]\n");
-	fprintf(stderr, "Options:\n");
+    fprintf(stderr, "\n");
+    fprintf(stderr, "Version: %s\n", hts_version());
+    fprintf(stderr, "Usage:   tabix [OPTIONS] [FILE] [REGION [...]]\n");
+    fprintf(stderr, "Options:\n");
     fprintf(stderr, "   -0, --zero-based        coordinates are zero-based\n");
     fprintf(stderr, "   -b, --begin INT         column number for region start [4]\n");
     fprintf(stderr, "   -c, --comment CHAR      skip comment lines starting with CHAR [null]\n");
@@ -249,16 +274,16 @@ static int usage(void)
     fprintf(stderr, "   -s, --sequence INT      column number for sequence names (suppressed by -p) [1]\n");
     fprintf(stderr, "   -S, --skip-lines INT    skip first INT lines [0]\n");
     fprintf(stderr, "\n");
-	return 1;
+    return 1;
 }
 
 int main(int argc, char *argv[])
 {
-	int c, min_shift = -1, is_force = 0, list_chroms = 0, mode = 0;
-	tbx_conf_t conf = tbx_conf_gff, *conf_ptr = NULL;
+    int c, min_shift = -1, is_force = 0, list_chroms = 0, mode = 0;
+    tbx_conf_t conf = tbx_conf_gff, *conf_ptr = NULL;
     char *reheader = NULL;
 
-    static struct option loptions[] = 
+    static struct option loptions[] =
     {
         {"help",0,0,'h'},
         {"zero-based",0,0,'0'},
@@ -276,9 +301,9 @@ int main(int argc, char *argv[])
         {0,0,0,0}
     };
 
-	while ((c = getopt_long(argc, argv, "hH?0b:c:e:fm:p:s:S:lr:", loptions,NULL)) >= 0)
+    while ((c = getopt_long(argc, argv, "hH?0b:c:e:fm:p:s:S:lr:", loptions,NULL)) >= 0)
     {
-        switch (c) 
+        switch (c)
         {
             case 'r': reheader = optarg; break;
             case 'h': mode = PRINT_HEADER; break;
@@ -303,7 +328,7 @@ int main(int argc, char *argv[])
         }
     }
 
-	if ( optind==argc ) return usage();
+    if ( optind==argc ) return usage();
 
     if ( list_chroms )
         return query_chroms(argv[optind]);
@@ -315,16 +340,16 @@ int main(int argc, char *argv[])
     int ftype = file_type(fname);
     if ( !conf_ptr )    // no preset given
     {
-        if ( ftype==IS_GFF ) conf_ptr = &tbx_conf_gff; 
+        if ( ftype==IS_GFF ) conf_ptr = &tbx_conf_gff;
         else if ( ftype==IS_BED ) conf_ptr = &tbx_conf_bed;
         else if ( ftype==IS_SAM ) conf_ptr = &tbx_conf_sam;
         else if ( ftype==IS_VCF ) conf_ptr = &tbx_conf_vcf;
         else if ( ftype==IS_BCF )
-        { 
+        {
             if ( min_shift <= 0 ) min_shift = 14;
         }
         else if ( ftype==IS_BAM )
-        { 
+        {
             if ( min_shift <= 0 ) min_shift = 14;
         }
     }
@@ -352,7 +377,7 @@ int main(int argc, char *argv[])
 
     if ( min_shift > 0 ) // CSI index
     {
-        if ( ftype==IS_BCF ) 
+        if ( ftype==IS_BCF )
         {
             if ( bcf_index_build(fname, min_shift)!=0 ) error("bcf_index_build failed: %s\n", fname);
             return 0;
@@ -370,5 +395,5 @@ int main(int argc, char *argv[])
         if ( tbx_index_build(fname, min_shift, &conf) ) error("tbx_index_build failed: %s\n", fname);
         return 0;
     }
-	return 0;
+    return 0;
 }
diff --git a/htslib/tbx.c b/htslib/tbx.c
index 035cb68..4a5bdd2 100644
--- a/htslib/tbx.c
+++ b/htslib/tbx.c
@@ -1,3 +1,28 @@
+/*  tbx.c -- tabix API functions.
+
+    Copyright (C) 2009, 2010, 2012-2014 Genome Research Ltd.
+    Copyright (C) 2010-2012 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
@@ -16,103 +41,103 @@ tbx_conf_t tbx_conf_sam = { TBX_SAM, 3, 4, 0, '@', 0 };
 tbx_conf_t tbx_conf_vcf = { TBX_VCF, 1, 2, 0, '#', 0 };
 
 typedef struct {
-	int64_t beg, end;
-	char *ss, *se;
-	int tid;
+    int64_t beg, end;
+    char *ss, *se;
+    int tid;
 } tbx_intv_t;
 
 static inline int get_tid(tbx_t *tbx, const char *ss, int is_add)
 {
-	khint_t k;
-	khash_t(s2i) *d;
-	if (tbx->dict == 0) tbx->dict = kh_init(s2i);
-	d = (khash_t(s2i)*)tbx->dict;
-	if (is_add) {
-		int absent;
-		k = kh_put(s2i, d, ss, &absent);
-		if (absent) {
-			kh_key(d, k) = strdup(ss);
-			kh_val(d, k) = kh_size(d) - 1;
-		}
-	} else k = kh_get(s2i, d, ss);
-	return k == kh_end(d)? -1 : kh_val(d, k);
+    khint_t k;
+    khash_t(s2i) *d;
+    if (tbx->dict == 0) tbx->dict = kh_init(s2i);
+    d = (khash_t(s2i)*)tbx->dict;
+    if (is_add) {
+        int absent;
+        k = kh_put(s2i, d, ss, &absent);
+        if (absent) {
+            kh_key(d, k) = strdup(ss);
+            kh_val(d, k) = kh_size(d) - 1;
+        }
+    } else k = kh_get(s2i, d, ss);
+    return k == kh_end(d)? -1 : kh_val(d, k);
 }
 
 int tbx_name2id(tbx_t *tbx, const char *ss)
 {
-	return get_tid(tbx, ss, 0);
+    return get_tid(tbx, ss, 0);
 }
 
 int tbx_parse1(const tbx_conf_t *conf, int len, char *line, tbx_intv_t *intv)
 {
-	int i, b = 0, id = 1, ncols = 0;
-	char *s;
-	intv->ss = intv->se = 0; intv->beg = intv->end = -1;
-	for (i = 0; i <= len; ++i) {
-		if (line[i] == '\t' || line[i] == 0) {
+    int i, b = 0, id = 1, ncols = 0;
+    char *s;
+    intv->ss = intv->se = 0; intv->beg = intv->end = -1;
+    for (i = 0; i <= len; ++i) {
+        if (line[i] == '\t' || line[i] == 0) {
             ++ncols;
-			if (id == conf->sc) {
-				intv->ss = line + b; intv->se = line + i;
-			} else if (id == conf->bc) {
-				// here ->beg is 0-based.
-				intv->beg = intv->end = strtol(line + b, &s, 0);
+            if (id == conf->sc) {
+                intv->ss = line + b; intv->se = line + i;
+            } else if (id == conf->bc) {
+                // here ->beg is 0-based.
+                intv->beg = intv->end = strtol(line + b, &s, 0);
                 if ( s==line+b ) return -1; // expected int
-				if (!(conf->preset&TBX_UCSC)) --intv->beg;
-				else ++intv->end;
-				if (intv->beg < 0) intv->beg = 0;
-				if (intv->end < 1) intv->end = 1;
-			} else {
-				if ((conf->preset&0xffff) == TBX_GENERIC) {
-					if (id == conf->ec) 
+                if (!(conf->preset&TBX_UCSC)) --intv->beg;
+                else ++intv->end;
+                if (intv->beg < 0) intv->beg = 0;
+                if (intv->end < 1) intv->end = 1;
+            } else {
+                if ((conf->preset&0xffff) == TBX_GENERIC) {
+                    if (id == conf->ec)
                     {
                         intv->end = strtol(line + b, &s, 0);
                         if ( s==line+b ) return -1; // expected int
                     }
-				} else if ((conf->preset&0xffff) == TBX_SAM) {
-					if (id == 6) { // CIGAR
-						int l = 0, op;
-						char *t;
-						for (s = line + b; s < line + i;) {
-							long x = strtol(s, &t, 10);
-							op = toupper(*t);
-							if (op == 'M' || op == 'D' || op == 'N') l += x;
-							s = t + 1;
-						}
-						if (l == 0) l = 1;
-						intv->end = intv->beg + l;
-					}
-				} else if ((conf->preset&0xffff) == TBX_VCF) {
-					if (id == 4) {
-						if (b < i) intv->end = intv->beg + (i - b);
-					} else if (id == 8) { // look for "END="
-						int c = line[i];
-						line[i] = 0;
-						s = strstr(line + b, "END=");
-						if (s == line + b) s += 4;
-						else if (s) {
-							s = strstr(line + b, ";END=");
-							if (s) s += 5;
-						}
-						if (s) intv->end = strtol(s, &s, 0);
-						line[i] = c;
-					}
-				}
-			}
-			b = i + 1;
-			++id;
-		}
-	}
-	if (intv->ss == 0 || intv->se == 0 || intv->beg < 0 || intv->end < 0) return -1;
-	return 0;
+                } else if ((conf->preset&0xffff) == TBX_SAM) {
+                    if (id == 6) { // CIGAR
+                        int l = 0, op;
+                        char *t;
+                        for (s = line + b; s < line + i;) {
+                            long x = strtol(s, &t, 10);
+                            op = toupper(*t);
+                            if (op == 'M' || op == 'D' || op == 'N') l += x;
+                            s = t + 1;
+                        }
+                        if (l == 0) l = 1;
+                        intv->end = intv->beg + l;
+                    }
+                } else if ((conf->preset&0xffff) == TBX_VCF) {
+                    if (id == 4) {
+                        if (b < i) intv->end = intv->beg + (i - b);
+                    } else if (id == 8) { // look for "END="
+                        int c = line[i];
+                        line[i] = 0;
+                        s = strstr(line + b, "END=");
+                        if (s == line + b) s += 4;
+                        else if (s) {
+                            s = strstr(line + b, ";END=");
+                            if (s) s += 5;
+                        }
+                        if (s) intv->end = strtol(s, &s, 0);
+                        line[i] = c;
+                    }
+                }
+            }
+            b = i + 1;
+            ++id;
+        }
+    }
+    if (intv->ss == 0 || intv->se == 0 || intv->beg < 0 || intv->end < 0) return -1;
+    return 0;
 }
 
 static inline int get_intv(tbx_t *tbx, kstring_t *str, tbx_intv_t *intv, int is_add)
 {
-	if (tbx_parse1(&tbx->conf, str->l, str->s, intv) == 0) {
-		int c = *intv->se;
-		*intv->se = '\0'; intv->tid = get_tid(tbx, intv->ss, is_add); *intv->se = c;
-		return (intv->tid >= 0 && intv->beg >= 0 && intv->end >= 0)? 0 : -1;
-	} else {
+    if (tbx_parse1(&tbx->conf, str->l, str->s, intv) == 0) {
+        int c = *intv->se;
+        *intv->se = '\0'; intv->tid = get_tid(tbx, intv->ss, is_add); *intv->se = c;
+        return (intv->tid >= 0 && intv->beg >= 0 && intv->end >= 0)? 0 : -1;
+    } else {
         char *type = NULL;
         switch (tbx->conf.preset&0xffff)
         {
@@ -121,170 +146,170 @@ static inline int get_intv(tbx_t *tbx, kstring_t *str, tbx_intv_t *intv, int is_
             case TBX_UCSC: type = "TBX_UCSC"; break;
             default: type = "TBX_GENERIC"; break;
         }
-		fprintf(stderr, "[E::%s] failed to parse %s, was wrong -p [type] used?\nThe offending line was: \"%s\"\n", __func__, type, str->s);
-		return -1;
-	}
+        fprintf(stderr, "[E::%s] failed to parse %s, was wrong -p [type] used?\nThe offending line was: \"%s\"\n", __func__, type, str->s);
+        return -1;
+    }
 }
 
 int tbx_readrec(BGZF *fp, void *tbxv, void *sv, int *tid, int *beg, int *end)
 {
-	tbx_t *tbx = (tbx_t *) tbxv;
-	kstring_t *s = (kstring_t *) sv;
-	int ret;
-	if ((ret = bgzf_getline(fp, '\n', s)) >= 0) {
-		tbx_intv_t intv;
-		get_intv(tbx, s, &intv, 0);
-		*tid = intv.tid; *beg = intv.beg; *end = intv.end;
-	}
-	return ret;
+    tbx_t *tbx = (tbx_t *) tbxv;
+    kstring_t *s = (kstring_t *) sv;
+    int ret;
+    if ((ret = bgzf_getline(fp, '\n', s)) >= 0) {
+        tbx_intv_t intv;
+        get_intv(tbx, s, &intv, 0);
+        *tid = intv.tid; *beg = intv.beg; *end = intv.end;
+    }
+    return ret;
 }
 
 void tbx_set_meta(tbx_t *tbx)
 {
-	int i, l = 0, l_nm;
-	uint32_t x[7];
-	char **name;
-	uint8_t *meta;
-	khint_t k;
-	khash_t(s2i) *d = (khash_t(s2i)*)tbx->dict;
+    int i, l = 0, l_nm;
+    uint32_t x[7];
+    char **name;
+    uint8_t *meta;
+    khint_t k;
+    khash_t(s2i) *d = (khash_t(s2i)*)tbx->dict;
 
-	memcpy(x, &tbx->conf, 24);
-	name = (char**)malloc(sizeof(char*) * kh_size(d));
-	for (k = kh_begin(d), l = 0; k != kh_end(d); ++k) {
-		if (!kh_exist(d, k)) continue;
-		name[kh_val(d, k)] = (char*)kh_key(d, k);
-		l += strlen(kh_key(d, k)) + 1; // +1 to include '\0'
-	}
-	l_nm = x[6] = l;
-	meta = (uint8_t*)malloc(l_nm + 28);
-	if (ed_is_big())
-		for (i = 0; i < 7; ++i)
-			x[i] = ed_swap_4(x[i]);
-	memcpy(meta, x, 28);
-	for (l = 28, i = 0; i < (int)kh_size(d); ++i) {
-		int x = strlen(name[i]) + 1;
-		memcpy(meta + l, name[i], x);
-		l += x;
-	}
-	free(name);
-	hts_idx_set_meta(tbx->idx, l, meta, 0);
+    memcpy(x, &tbx->conf, 24);
+    name = (char**)malloc(sizeof(char*) * kh_size(d));
+    for (k = kh_begin(d), l = 0; k != kh_end(d); ++k) {
+        if (!kh_exist(d, k)) continue;
+        name[kh_val(d, k)] = (char*)kh_key(d, k);
+        l += strlen(kh_key(d, k)) + 1; // +1 to include '\0'
+    }
+    l_nm = x[6] = l;
+    meta = (uint8_t*)malloc(l_nm + 28);
+    if (ed_is_big())
+        for (i = 0; i < 7; ++i)
+            x[i] = ed_swap_4(x[i]);
+    memcpy(meta, x, 28);
+    for (l = 28, i = 0; i < (int)kh_size(d); ++i) {
+        int x = strlen(name[i]) + 1;
+        memcpy(meta + l, name[i], x);
+        l += x;
+    }
+    free(name);
+    hts_idx_set_meta(tbx->idx, l, meta, 0);
 }
 
 tbx_t *tbx_index(BGZF *fp, int min_shift, const tbx_conf_t *conf)
 {
-	tbx_t *tbx;
-	kstring_t str;
-	int ret, first = 0, n_lvls, fmt;
-	int64_t lineno = 0;
-	uint64_t last_off = 0;
-	tbx_intv_t intv;
+    tbx_t *tbx;
+    kstring_t str;
+    int ret, first = 0, n_lvls, fmt;
+    int64_t lineno = 0;
+    uint64_t last_off = 0;
+    tbx_intv_t intv;
 
-	str.s = 0; str.l = str.m = 0;
-	tbx = (tbx_t*)calloc(1, sizeof(tbx_t));
-	tbx->conf = *conf;
-	if (min_shift > 0) n_lvls = (TBX_MAX_SHIFT - min_shift + 2) / 3, fmt = HTS_FMT_CSI;
-	else min_shift = 14, n_lvls = 5, fmt = HTS_FMT_TBI;
-	while ((ret = bgzf_getline(fp, '\n', &str)) >= 0) {
-		++lineno;
-		if (lineno <= tbx->conf.line_skip || str.s[0] == tbx->conf.meta_char) {
-			last_off = bgzf_tell(fp);
-			continue;
-		}
-		if (first == 0) {
-			tbx->idx = hts_idx_init(0, fmt, last_off, min_shift, n_lvls);
-			first = 1;
-		}
-		get_intv(tbx, &str, &intv, 1);
-		ret = hts_idx_push(tbx->idx, intv.tid, intv.beg, intv.end, bgzf_tell(fp), 1);
-		if (ret < 0) 
+    str.s = 0; str.l = str.m = 0;
+    tbx = (tbx_t*)calloc(1, sizeof(tbx_t));
+    tbx->conf = *conf;
+    if (min_shift > 0) n_lvls = (TBX_MAX_SHIFT - min_shift + 2) / 3, fmt = HTS_FMT_CSI;
+    else min_shift = 14, n_lvls = 5, fmt = HTS_FMT_TBI;
+    while ((ret = bgzf_getline(fp, '\n', &str)) >= 0) {
+        ++lineno;
+        if (lineno <= tbx->conf.line_skip || str.s[0] == tbx->conf.meta_char) {
+            last_off = bgzf_tell(fp);
+            continue;
+        }
+        if (first == 0) {
+            tbx->idx = hts_idx_init(0, fmt, last_off, min_shift, n_lvls);
+            first = 1;
+        }
+        get_intv(tbx, &str, &intv, 1);
+        ret = hts_idx_push(tbx->idx, intv.tid, intv.beg, intv.end, bgzf_tell(fp), 1);
+        if (ret < 0)
         {
             free(str.s);
             tbx_destroy(tbx);
             return NULL;
         }
-	}
-	if ( !tbx->idx ) tbx->idx = hts_idx_init(0, fmt, last_off, min_shift, n_lvls);   // empty file
-	if ( !tbx->dict ) tbx->dict = kh_init(s2i);
-	hts_idx_finish(tbx->idx, bgzf_tell(fp));
-	tbx_set_meta(tbx);
-	free(str.s);
-	return tbx;
+    }
+    if ( !tbx->idx ) tbx->idx = hts_idx_init(0, fmt, last_off, min_shift, n_lvls);   // empty file
+    if ( !tbx->dict ) tbx->dict = kh_init(s2i);
+    hts_idx_finish(tbx->idx, bgzf_tell(fp));
+    tbx_set_meta(tbx);
+    free(str.s);
+    return tbx;
 }
 
 void tbx_destroy(tbx_t *tbx)
 {
-	khash_t(s2i) *d = (khash_t(s2i)*)tbx->dict;
-	if (d != NULL)
-	{
-		khint_t k;
-		for (k = kh_begin(d); k != kh_end(d); ++k)
-			if (kh_exist(d, k)) free((char*)kh_key(d, k));
-	}
-	hts_idx_destroy(tbx->idx);
-	kh_destroy(s2i, d);
-	free(tbx);
+    khash_t(s2i) *d = (khash_t(s2i)*)tbx->dict;
+    if (d != NULL)
+    {
+        khint_t k;
+        for (k = kh_begin(d); k != kh_end(d); ++k)
+            if (kh_exist(d, k)) free((char*)kh_key(d, k));
+    }
+    hts_idx_destroy(tbx->idx);
+    kh_destroy(s2i, d);
+    free(tbx);
 }
 
 int tbx_index_build(const char *fn, int min_shift, const tbx_conf_t *conf)
 {
-	tbx_t *tbx;
-	BGZF *fp;
+    tbx_t *tbx;
+    BGZF *fp;
     if ( bgzf_is_bgzf(fn)!=1 ) { fprintf(stderr,"Not a BGZF file: %s\n", fn); return -1; }
-	if ((fp = bgzf_open(fn, "r")) == 0) return -1;
+    if ((fp = bgzf_open(fn, "r")) == 0) return -1;
     if ( !fp->is_compressed ) { bgzf_close(fp); return -1; }
-	tbx = tbx_index(fp, min_shift, conf);
-	bgzf_close(fp);
+    tbx = tbx_index(fp, min_shift, conf);
+    bgzf_close(fp);
     if ( !tbx ) return -1;
-	hts_idx_save(tbx->idx, fn, min_shift > 0? HTS_FMT_CSI : HTS_FMT_TBI);
-	tbx_destroy(tbx);
-	return 0;
+    hts_idx_save(tbx->idx, fn, min_shift > 0? HTS_FMT_CSI : HTS_FMT_TBI);
+    tbx_destroy(tbx);
+    return 0;
 }
 
 tbx_t *tbx_index_load(const char *fn)
 {
-	tbx_t *tbx;
-	uint8_t *meta;
-	char *nm, *p;
-	uint32_t x[7];
-	int l_meta, l_nm;
-	tbx = (tbx_t*)calloc(1, sizeof(tbx_t));
-	tbx->idx = hts_idx_load(fn, HTS_FMT_TBI);
-	if ( !tbx->idx ) 
+    tbx_t *tbx;
+    uint8_t *meta;
+    char *nm, *p;
+    uint32_t x[7];
+    int l_meta, l_nm;
+    tbx = (tbx_t*)calloc(1, sizeof(tbx_t));
+    tbx->idx = hts_idx_load(fn, HTS_FMT_TBI);
+    if ( !tbx->idx )
     {
         free(tbx);
         return NULL;
     }
-	meta = hts_idx_get_meta(tbx->idx, &l_meta);
-	memcpy(x, meta, 28);
-	memcpy(&tbx->conf, x, 24);
-	p = nm = (char*)meta + 28;
-	l_nm = x[6];
-	for (; p - nm < l_nm; p += strlen(p) + 1) get_tid(tbx, p, 1);
-	return tbx;
+    meta = hts_idx_get_meta(tbx->idx, &l_meta);
+    memcpy(x, meta, 28);
+    memcpy(&tbx->conf, x, 24);
+    p = nm = (char*)meta + 28;
+    l_nm = x[6];
+    for (; p - nm < l_nm; p += strlen(p) + 1) get_tid(tbx, p, 1);
+    return tbx;
 }
 
 const char **tbx_seqnames(tbx_t *tbx, int *n)
 {
-	khash_t(s2i) *d = (khash_t(s2i)*)tbx->dict;
-	if (d == NULL)
-	{
-		*n = 0;
-		return NULL;
-	}
-	int tid, m = kh_size(d);
-	const char **names = (const char**) calloc(m,sizeof(const char*));
-	khint_t k;
-	for (k=kh_begin(d); k<kh_end(d); k++)
-	{
-		if ( !kh_exist(d,k) ) continue;
+    khash_t(s2i) *d = (khash_t(s2i)*)tbx->dict;
+    if (d == NULL)
+    {
+        *n = 0;
+        return NULL;
+    }
+    int tid, m = kh_size(d);
+    const char **names = (const char**) calloc(m,sizeof(const char*));
+    khint_t k;
+    for (k=kh_begin(d); k<kh_end(d); k++)
+    {
+        if ( !kh_exist(d,k) ) continue;
         tid = kh_val(d,k);
         assert( tid<m );
-		names[tid] = kh_key(d,k);
-	}
+        names[tid] = kh_key(d,k);
+    }
     // sanity check: there should be no gaps
     for (tid=0; tid<m; tid++)
         assert(names[tid]);
-	*n = m;
-	return names;
+    *n = m;
+    return names;
 }
 
diff --git a/htslib/vcf.c b/htslib/vcf.c
index 67216b0..fb44980 100644
--- a/htslib/vcf.c
+++ b/htslib/vcf.c
@@ -1,3 +1,29 @@
+/*  vcf.c -- VCF/BCF API functions.
+
+    Copyright (C) 2012, 2013 Broad Institute.
+    Copyright (C) 2012-2014 Genome Research Ltd.
+    Portions copyright (C) 2014 Intel Corporation.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <zlib.h>
 #include <stdio.h>
 #include <ctype.h>
@@ -13,14 +39,14 @@
 
 #include "htslib/khash.h"
 KHASH_MAP_INIT_STR(vdict, bcf_idinfo_t)
-typedef khash_t(vdict) vdict_t;
+    typedef khash_t(vdict) vdict_t;
 
 #include "htslib/kseq.h"
 KSTREAM_DECLARE(gzFile, gzread)
 
-uint32_t bcf_float_missing    = 0x7F800001;
-uint32_t bcf_float_vector_end = 0x7F800002;
-uint8_t bcf_type_shift[] = { 0, 0, 1, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+    uint32_t bcf_float_missing    = 0x7F800001;
+    uint32_t bcf_float_vector_end = 0x7F800002;
+    uint8_t bcf_type_shift[] = { 0, 0, 1, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 static bcf_idinfo_t bcf_idinfo_def = { .info = { 15, 15, 15 }, .hrec = { NULL, NULL, NULL}, .id = -1 };
 
 /*************************
@@ -86,11 +112,11 @@ void bcf_hdr_parse_sample_line(bcf_hdr_t *h, const char *str)
 
 int bcf_hdr_sync(bcf_hdr_t *h)
 {
-	int i;
-	for (i = 0; i < 3; i++) 
+    int i;
+    for (i = 0; i < 3; i++)
     {
-		vdict_t *d = (vdict_t*)h->dict[i];
-		khint_t k;
+        vdict_t *d = (vdict_t*)h->dict[i];
+        khint_t k;
 
         // find out the largest id, there may be holes because of IDX
         int max_id = -1;
@@ -115,8 +141,8 @@ int bcf_hdr_sync(bcf_hdr_t *h)
             h->id[i][kh_val(d,k).id].key = kh_key(d,k);
             h->id[i][kh_val(d,k).id].val = &kh_val(d,k);
         }
-	}
-	return 0;
+    }
+    return 0;
 }
 
 void bcf_hrec_destroy(bcf_hrec_t *hrec)
@@ -273,9 +299,20 @@ bcf_hrec_t *bcf_hdr_parse_line(const bcf_hdr_t *h, const char *line, int *len)
     while ( *q && *q!='\n' && nopen )
     {
         p = ++q;
-        while ( *q && *q!='=' ) q++;
+        while ( *q && isalnum(*q) ) q++;
         n = q-p;
-        if ( *q!='=' || !n ) { *len = q-line+1; bcf_hrec_destroy(hrec); return NULL; } // wrong format
+        if ( *q!='=' || !n )
+        {
+            // wrong format
+            while ( *q && *q!='\n' ) q++;
+            kstring_t tmp = {0,0,0};
+            kputsn(line,q-line,&tmp);
+            fprintf(stderr,"Could not parse the header line: \"%s\"\n", tmp.s);
+            free(tmp.s);
+            *len = q-line+1;
+            bcf_hrec_destroy(hrec);
+            return NULL;
+        }
         bcf_hrec_add_key(hrec, p, q-p);
         p = ++q;
         int quoted = *p=='"' ? 1 : 0;
@@ -284,12 +321,12 @@ bcf_hrec_t *bcf_hdr_parse_line(const bcf_hdr_t *h, const char *line, int *len)
         {
             if ( !*q ) break;
             if ( quoted ) { if ( *q=='"' && !is_escaped(p,q) ) break; }
-            else 
-            { 
+            else
+            {
                 if ( *q=='<' ) nopen++;
                 if ( *q=='>' ) nopen--;
                 if ( !nopen ) break;
-                if ( *q==',' && nopen==1 ) break; 
+                if ( *q==',' && nopen==1 ) break;
             }
             q++;
         }
@@ -307,17 +344,17 @@ int bcf_hdr_register_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec)
     // contig
     int i,j,k, ret;
     char *str;
-    if ( !strcmp(hrec->key, "contig") ) 
+    if ( !strcmp(hrec->key, "contig") )
     {
         hrec->type = BCF_HL_CTG;
 
         // Get the contig ID ($str) and length ($j)
-        i = bcf_hrec_find_key(hrec,"length"); 
+        i = bcf_hrec_find_key(hrec,"length");
         if ( i<0 ) return 0;
         if ( sscanf(hrec->vals[i],"%d",&j)!=1 ) return 0;
 
-        i = bcf_hrec_find_key(hrec,"ID"); 
-        if ( i<0 ) return 0; 
+        i = bcf_hrec_find_key(hrec,"ID");
+        if ( i<0 ) return 0;
         str = strdup(hrec->vals[i]);
 
         // Register in the dictionary
@@ -355,10 +392,10 @@ int bcf_hdr_register_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec)
     else if ( !strcmp(hrec->key, "FORMAT") ) hrec->type = BCF_HL_FMT;
     else if ( hrec->nkeys>0 ) { hrec->type = BCF_HL_STR; return 1; }
     else return 0;
-    
+
     // INFO/FILTER/FORMAT
     char *id = NULL;
-    int type = -1, num = -1, var = -1, idx = -1; 
+    int type = -1, num = -1, var = -1, idx = -1;
     for (i=0; i<hrec->nkeys; i++)
     {
         if ( !strcmp(hrec->keys[i], "ID") ) id = hrec->vals[i];
@@ -366,7 +403,7 @@ int bcf_hdr_register_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec)
         {
             char *tmp = hrec->vals[i];
             idx = strtol(hrec->vals[i], &tmp, 10);
-            if ( *tmp ) 
+            if ( *tmp )
             {
                 fprintf(stderr,"[%s:%d %s] Error parsing the IDX tag, skipping.\n", __FILE__,__LINE__,__FUNCTION__);
                 return 0;
@@ -390,13 +427,12 @@ int bcf_hdr_register_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec)
             else if ( !strcmp(hrec->vals[i],"R") ) var = BCF_VL_R;
             else if ( !strcmp(hrec->vals[i],"G") ) var = BCF_VL_G;
             else if ( !strcmp(hrec->vals[i],".") ) var = BCF_VL_VAR;
-            else 
-            { 
+            else
+            {
                 sscanf(hrec->vals[i],"%d",&num);
                 var = BCF_VL_FIXED;
             }
             if (var != BCF_VL_FIXED) num = 0xfffff;
-
         }
     }
     uint32_t info = (uint32_t)num<<12 | var<<8 | type<<4 | hrec->type;
@@ -406,8 +442,8 @@ int bcf_hdr_register_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec)
 
     vdict_t *d = (vdict_t*)hdr->dict[BCF_DT_ID];
     k = kh_put(vdict, d, str, &ret);
-    if ( !ret ) 
-    { 
+    if ( !ret )
+    {
         // already present
         free(str);
         if ( kh_val(d, k).hrec[info&0xf] ) return 0;
@@ -427,11 +463,13 @@ int bcf_hdr_register_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec)
 
 int bcf_hdr_add_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec)
 {
+    if ( !hrec ) return 0;
+
     hrec->type = BCF_HL_GEN;
     if ( !bcf_hdr_register_hrec(hdr,hrec) )
     {
         // If one of the hashed field, then it is already present
-        if ( hrec->type != BCF_HL_GEN ) 
+        if ( hrec->type != BCF_HL_GEN )
         {
             bcf_hrec_destroy(hrec);
             return 0;
@@ -445,7 +483,7 @@ int bcf_hdr_add_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec)
             if ( !strcmp(hdr->hrec[i]->key,hrec->key) && !strcmp(hrec->key,"fileformat") ) break;
             if ( !strcmp(hdr->hrec[i]->key,hrec->key) && !strcmp(hdr->hrec[i]->value,hrec->value) ) break;
         }
-        if ( i<hdr->nhrec ) 
+        if ( i<hdr->nhrec )
         {
             bcf_hrec_destroy(hrec);
             return 0;
@@ -460,20 +498,38 @@ int bcf_hdr_add_hrec(bcf_hdr_t *hdr, bcf_hrec_t *hrec)
     return hrec->type==BCF_HL_GEN ? 0 : 1;
 }
 
-bcf_hrec_t *bcf_hdr_get_hrec(const bcf_hdr_t *hdr, int type, const char *id)
+/*
+ *  Note that while querying of FLT,INFO,FMT,CTG lines is fast (the keys are hashed),
+ *  the STR,GEN lines are searched for linearly in a linked list of all header lines.
+ *  This may become a problem for VCFs with huge headers, we might need to build a
+ *  dictionary for these lines as well.
+ */
+bcf_hrec_t *bcf_hdr_get_hrec(const bcf_hdr_t *hdr, int type, const char *key, const char *value, const char *str_class)
 {
     int i;
     if ( type==BCF_HL_GEN )
     {
         for (i=0; i<hdr->nhrec; i++)
         {
-             if ( hdr->hrec[i]->type!=BCF_HL_GEN ) continue;
-             if ( !strcmp(hdr->hrec[i]->key,id) ) return hdr->hrec[i];
+            if ( hdr->hrec[i]->type!=type ) continue;
+            if ( strcmp(hdr->hrec[i]->key,key) ) continue;
+            if ( !value || !strcmp(hdr->hrec[i]->value,value) ) return hdr->hrec[i];
+        }
+        return NULL;
+    }
+    else if ( type==BCF_HL_STR )
+    {
+        for (i=0; i<hdr->nhrec; i++)
+        {
+            if ( hdr->hrec[i]->type!=type ) continue;
+            if ( strcmp(hdr->hrec[i]->key,str_class) ) continue;
+            int j = bcf_hrec_find_key(hdr->hrec[i],key);
+            if ( j>=0 && !strcmp(hdr->hrec[i]->vals[j],value) ) return hdr->hrec[i];
         }
         return NULL;
     }
     vdict_t *d = type==BCF_HL_CTG ? (vdict_t*)hdr->dict[BCF_DT_CTG] : (vdict_t*)hdr->dict[BCF_DT_ID];
-    khint_t k = kh_get(vdict, d, id);
+    khint_t k = kh_get(vdict, d, value);
     if ( k == kh_end(d) ) return NULL;
     return kh_val(d, k).hrec[type==BCF_HL_CTG?0:type];
 }
@@ -509,7 +565,7 @@ int bcf_hdr_parse(bcf_hdr_t *hdr, char *htxt)
 
     // Check sanity: "fileformat" string must come as first
     bcf_hrec_t *hrec = bcf_hdr_parse_line(hdr,p,&len);
-    if ( !hrec->key || strcasecmp(hrec->key,"fileformat") )
+    if ( !hrec || !hrec->key || strcasecmp(hrec->key,"fileformat") )
         fprintf(stderr, "[W::%s] The first line should be ##fileformat; is the VCF/BCF header broken?\n", __func__);
     needs_sync += bcf_hdr_add_hrec(hdr, hrec);
 
@@ -523,10 +579,9 @@ int bcf_hdr_parse(bcf_hdr_t *hdr, char *htxt)
         needs_sync += bcf_hdr_add_hrec(hdr, hrec);
         p += len;
     }
-    bcf_hdr_parse_sample_line(hdr,p);
-    if ( needs_sync ) bcf_hdr_sync(hdr);
+    bcf_hdr_parse_sample_line(hdr,p);   // calls hdr_sync
     bcf_hdr_check_sanity(hdr);
-	return 0;
+    return 0;
 }
 
 int bcf_hdr_append(bcf_hdr_t *hdr, const char *line)
@@ -547,7 +602,7 @@ void bcf_hdr_remove(bcf_hdr_t *hdr, int type, const char *key)
     {
         if ( type==BCF_HL_FLT || type==BCF_HL_INFO || type==BCF_HL_FMT || type== BCF_HL_CTG )
         {
-            hrec = bcf_hdr_get_hrec(hdr, type, key);
+            hrec = bcf_hdr_get_hrec(hdr, type, "ID", key, NULL);
             if ( !hrec ) return;
 
             for (i=0; i<hdr->nhrec; i++)
@@ -563,7 +618,16 @@ void bcf_hdr_remove(bcf_hdr_t *hdr, int type, const char *key)
             for (i=0; i<hdr->nhrec; i++)
             {
                 if ( hdr->hrec[i]->type!=type ) continue;
-                if ( !strcmp(hdr->hrec[i]->key,key) ) break;
+                if ( type==BCF_HL_GEN )
+                {
+                    if ( !strcmp(hdr->hrec[i]->key,key) ) break;
+                }
+                else
+                {
+                    // not all structured lines have ID, we could be more sophisticated as in bcf_hdr_get_hrec()
+                    int j = bcf_hrec_find_key(hdr->hrec[i], "ID");
+                    if ( j>=0 && !strcmp(hdr->hrec[i]->vals[j],key) ) break;
+                }
             }
             if ( i==hdr->nhrec ) return;
             hrec = hdr->hrec[i];
@@ -603,8 +667,8 @@ int bcf_hdr_printf(bcf_hdr_t *hdr, const char *fmt, ...)
 
 const char *bcf_hdr_get_version(const bcf_hdr_t *hdr)
 {
-    bcf_hrec_t *hrec = bcf_hdr_get_hrec(hdr, BCF_HL_GEN, "fileformat");
-    if ( !hrec ) 
+    bcf_hrec_t *hrec = bcf_hdr_get_hrec(hdr, BCF_HL_GEN, "fileformat", NULL, NULL);
+    if ( !hrec )
     {
         fprintf(stderr,"No version string found, assuming VCFv4.2\n");
         return "VCFv4.2";
@@ -614,7 +678,7 @@ const char *bcf_hdr_get_version(const bcf_hdr_t *hdr)
 
 void bcf_hdr_set_version(bcf_hdr_t *hdr, const char *version)
 {
-    bcf_hrec_t *hrec = bcf_hdr_get_hrec(hdr, BCF_HL_GEN, "fileformat");
+    bcf_hrec_t *hrec = bcf_hdr_get_hrec(hdr, BCF_HL_GEN, "fileformat", NULL, NULL);
     if ( !hrec )
     {
         int len;
@@ -633,73 +697,73 @@ void bcf_hdr_set_version(bcf_hdr_t *hdr, const char *version)
 
 bcf_hdr_t *bcf_hdr_init(const char *mode)
 {
-	int i;
-	bcf_hdr_t *h;
-	h = (bcf_hdr_t*)calloc(1, sizeof(bcf_hdr_t));
-	for (i = 0; i < 3; ++i)
-		h->dict[i] = kh_init(vdict);
+    int i;
+    bcf_hdr_t *h;
+    h = (bcf_hdr_t*)calloc(1, sizeof(bcf_hdr_t));
+    for (i = 0; i < 3; ++i)
+        h->dict[i] = kh_init(vdict);
     if ( strchr(mode,'w') )
     {
         bcf_hdr_append(h, "##fileformat=VCFv4.2");
         // The filter PASS must appear first in the dictionary
         bcf_hdr_append(h, "##FILTER=<ID=PASS,Description=\"All filters passed\">");
     }
-	return h;
+    return h;
 }
 
 void bcf_hdr_destroy(bcf_hdr_t *h)
 {
-	int i;
-	khint_t k;
-	for (i = 0; i < 3; ++i) {
-		vdict_t *d = (vdict_t*)h->dict[i];
-		if (d == 0) continue;
-		for (k = kh_begin(d); k != kh_end(d); ++k)
-			if (kh_exist(d, k)) free((char*)kh_key(d, k));
-		kh_destroy(vdict, d);
-		free(h->id[i]);
-	}
+    int i;
+    khint_t k;
+    for (i = 0; i < 3; ++i) {
+        vdict_t *d = (vdict_t*)h->dict[i];
+        if (d == 0) continue;
+        for (k = kh_begin(d); k != kh_end(d); ++k)
+            if (kh_exist(d, k)) free((char*)kh_key(d, k));
+        kh_destroy(vdict, d);
+        free(h->id[i]);
+    }
     for (i=0; i<h->nhrec; i++)
         bcf_hrec_destroy(h->hrec[i]);
     if (h->nhrec) free(h->hrec);
     if (h->samples) free(h->samples);
     free(h->keep_samples);
     free(h->transl[0]); free(h->transl[1]);
-	free(h->mem.s);
-	free(h);
+    free(h->mem.s);
+    free(h);
 }
 
 bcf_hdr_t *bcf_hdr_read(htsFile *hfp)
 {
-	if (!hfp->is_bin) 
+    if (!hfp->is_bin)
         return vcf_hdr_read(hfp);
 
     BGZF *fp = hfp->fp.bgzf;
-	uint8_t magic[5];
-	bcf_hdr_t *h;
-	h = bcf_hdr_init("r");
-	if ( bgzf_read(fp, magic, 5)<0 ) 
+    uint8_t magic[5];
+    bcf_hdr_t *h;
+    h = bcf_hdr_init("r");
+    if ( bgzf_read(fp, magic, 5)<0 )
     {
         fprintf(stderr,"[%s:%d %s] Failed to read the header (reading BCF in text mode?)\n", __FILE__,__LINE__,__FUNCTION__);
         return NULL;
     }
-	if (strncmp((char*)magic, "BCF\2\2", 5) != 0) 
+    if (strncmp((char*)magic, "BCF\2\2", 5) != 0)
     {
-        if (!strncmp((char*)magic, "BCF", 3)) 
+        if (!strncmp((char*)magic, "BCF", 3))
             fprintf(stderr,"[%s:%d %s] invalid BCF2 magic string: only BCFv2.2 is supported.\n", __FILE__,__LINE__,__FUNCTION__);
-		else if (hts_verbose >= 2)
-			fprintf(stderr, "[E::%s] invalid BCF2 magic string\n", __func__);
-		bcf_hdr_destroy(h);
-		return 0;
-	}
+        else if (hts_verbose >= 2)
+            fprintf(stderr, "[E::%s] invalid BCF2 magic string\n", __func__);
+        bcf_hdr_destroy(h);
+        return 0;
+    }
     int hlen;
     char *htxt;
-	bgzf_read(fp, &hlen, 4);
-	htxt = (char*)malloc(hlen);
-	bgzf_read(fp, htxt, hlen);
-	bcf_hdr_parse(h, htxt);
+    bgzf_read(fp, &hlen, 4);
+    htxt = (char*)malloc(hlen);
+    bgzf_read(fp, htxt, hlen);
+    bcf_hdr_parse(h, htxt);
     free(htxt);
-	return h;
+    return h;
 }
 
 int bcf_hdr_write(htsFile *hfp, const bcf_hdr_t *h)
@@ -711,9 +775,9 @@ int bcf_hdr_write(htsFile *hfp, const bcf_hdr_t *h)
     hlen++; // include the \0 byte
 
     BGZF *fp = hfp->fp.bgzf;
-	if ( bgzf_write(fp, "BCF\2\2", 5) !=5 ) return -1;
-	if ( bgzf_write(fp, &hlen, 4) !=4 ) return -1;
-	if ( bgzf_write(fp, htxt, hlen) != hlen ) return -1;
+    if ( bgzf_write(fp, "BCF\2\2", 5) !=5 ) return -1;
+    if ( bgzf_write(fp, &hlen, 4) !=4 ) return -1;
+    if ( bgzf_write(fp, htxt, hlen) != hlen ) return -1;
 
     free(htxt);
     return 0;
@@ -725,35 +789,34 @@ int bcf_hdr_write(htsFile *hfp, const bcf_hdr_t *h)
 
 bcf1_t *bcf_init1()
 {
-	bcf1_t *v;
-	v = (bcf1_t*)calloc(1, sizeof(bcf1_t));
-	return v;
+    bcf1_t *v;
+    v = (bcf1_t*)calloc(1, sizeof(bcf1_t));
+    return v;
 }
 
 void bcf_clear(bcf1_t *v)
 {
     int i;
-    for (i=0; i<v->d.m_info; i++) 
+    for (i=0; i<v->d.m_info; i++)
     {
-        if ( v->d.info[i].vptr_free ) 
+        if ( v->d.info[i].vptr_free )
         {
             free(v->d.info[i].vptr - v->d.info[i].vptr_off);
             v->d.info[i].vptr_free = 0;
         }
     }
-    for (i=0; i<v->d.m_fmt; i++) 
+    for (i=0; i<v->d.m_fmt; i++)
     {
-        if ( v->d.fmt[i].p_free ) 
+        if ( v->d.fmt[i].p_free )
         {
             free(v->d.fmt[i].p - v->d.fmt[i].p_off);
             v->d.fmt[i].p_free = 0;
         }
     }
-	v->rid = v->pos = v->rlen = v->unpacked = 0;
-    v->unpack_ptr = NULL;
-	bcf_float_set_missing(v->qual);
-	v->n_info = v->n_allele = v->n_fmt = v->n_sample = 0;
-	v->shared.l = v->indiv.l = 0;
+    v->rid = v->pos = v->rlen = v->unpacked = 0;
+    bcf_float_set_missing(v->qual);
+    v->n_info = v->n_allele = v->n_fmt = v->n_sample = 0;
+    v->shared.l = v->indiv.l = 0;
     v->d.var_type = -1;
     v->d.shared_dirty = 0;
     v->d.indiv_dirty  = 0;
@@ -768,40 +831,40 @@ void bcf_empty1(bcf1_t *v)
     bcf_clear1(v);
     free(v->d.id);
     free(v->d.als);
-	free(v->d.allele); free(v->d.flt); free(v->d.info); free(v->d.fmt);
-	if (v->d.var ) free(v->d.var);
-	free(v->shared.s); free(v->indiv.s);
+    free(v->d.allele); free(v->d.flt); free(v->d.info); free(v->d.fmt);
+    if (v->d.var ) free(v->d.var);
+    free(v->shared.s); free(v->indiv.s);
 }
 
 void bcf_destroy1(bcf1_t *v)
 {
     bcf_empty1(v);
-	free(v);
+    free(v);
 }
 
 static inline int bcf_read1_core(BGZF *fp, bcf1_t *v)
 {
-	uint32_t x[8];
-	int ret;
-	if ((ret = bgzf_read(fp, x, 32)) != 32) {
-		if (ret == 0) return -1;
-		return -2;
-	}
+    uint32_t x[8];
+    int ret;
+    if ((ret = bgzf_read(fp, x, 32)) != 32) {
+        if (ret == 0) return -1;
+        return -2;
+    }
     bcf_clear1(v);
-	x[0] -= 24; // to exclude six 32-bit integers
-	ks_resize(&v->shared, x[0]);
-	ks_resize(&v->indiv, x[1]);
-	memcpy(v, x + 2, 16);
-	v->n_allele = x[6]>>16; v->n_info = x[6]&0xffff;
-	v->n_fmt = x[7]>>24; v->n_sample = x[7]&0xffffff;
-	v->shared.l = x[0], v->indiv.l = x[1];
+    x[0] -= 24; // to exclude six 32-bit integers
+    ks_resize(&v->shared, x[0]);
+    ks_resize(&v->indiv, x[1]);
+    memcpy(v, x + 2, 16);
+    v->n_allele = x[6]>>16; v->n_info = x[6]&0xffff;
+    v->n_fmt = x[7]>>24; v->n_sample = x[7]&0xffffff;
+    v->shared.l = x[0], v->indiv.l = x[1];
 
     // silent fix of broken BCFs produced by earlier versions of bcf_subset, prior to and including bd6ed8b4
     if ( (!v->indiv.l || !v->n_sample) && v->n_fmt ) v->n_fmt = 0;
 
-	bgzf_read(fp, v->shared.s, v->shared.l);
-	bgzf_read(fp, v->indiv.s, v->indiv.l);
-	return 0;
+    bgzf_read(fp, v->shared.s, v->shared.l);
+    bgzf_read(fp, v->indiv.s, v->indiv.l);
+    return 0;
 }
 
 #define bit_array_size(n) ((n)/8+1)
@@ -851,8 +914,8 @@ int bcf_subset_format(const bcf_hdr_t *hdr, bcf1_t *rec)
     return 0;
 }
 
-int bcf_read(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v) 
-{ 
+int bcf_read(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v)
+{
     if (!fp->is_bin) return vcf_read(fp,h,v);
     int ret = bcf_read1_core(fp->fp.bgzf, v);
     if ( ret!=0 || !h->keep_samples ) return ret;
@@ -861,11 +924,11 @@ int bcf_read(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v)
 
 int bcf_readrec(BGZF *fp, void *null, void *vv, int *tid, int *beg, int *end)
 {
-	bcf1_t *v = (bcf1_t *) vv;
-	int ret;
-	if ((ret = bcf_read1_core(fp, v)) >= 0)
-		*tid = v->rid, *beg = v->pos, *end = v->pos + v->rlen;
-	return ret;
+    bcf1_t *v = (bcf1_t *) vv;
+    int ret;
+    if ((ret = bcf_read1_core(fp, v)) >= 0)
+        *tid = v->rid, *beg = v->pos, *end = v->pos + v->rlen;
+    return ret;
 }
 
 static inline void bcf1_sync_id(bcf1_t *line, kstring_t *str)
@@ -880,7 +943,7 @@ static inline void bcf1_sync_alleles(bcf1_t *line, kstring_t *str)
     int i;
     for (i=0; i<line->n_allele; i++)
         bcf_enc_vchar(str, strlen(line->d.allele[i]), line->d.allele[i]);
-    line->rlen = line->n_allele ? strlen(line->d.allele[0]) : 0;     // beware: this neglects SV's END tag
+    if ( !line->rlen && line->n_allele ) line->rlen = strlen(line->d.allele[0]);
 }
 static inline void bcf1_sync_filter(bcf1_t *line, kstring_t *str)
 {
@@ -888,6 +951,7 @@ static inline void bcf1_sync_filter(bcf1_t *line, kstring_t *str)
     if ( line->d.n_flt ) bcf_enc_vint(str, line->d.n_flt, line->d.flt, -1);
     else bcf_enc_vint(str, 0, 0, -1);
 }
+
 static inline void bcf1_sync_info(bcf1_t *line, kstring_t *str)
 {
     // pairs of typed vectors
@@ -896,7 +960,7 @@ static inline void bcf1_sync_info(bcf1_t *line, kstring_t *str)
     {
         bcf_info_t *info = &line->d.info[i];
         if ( !info->vptr )
-        { 
+        {
             // marked for removal
             if ( irm < 0 ) irm = i;
             continue;
@@ -913,60 +977,100 @@ static inline void bcf1_sync_info(bcf1_t *line, kstring_t *str)
 
 static int bcf1_sync(bcf1_t *line)
 {
+    char *shared_ori = line->shared.s;
+    size_t prev_len;
+
     kstring_t tmp = {0,0,0};
     if ( !line->shared.l )
     {
-        // New line, get ready for BCF output
+        // New line created via API, BCF data blocks do not exist. Get it ready for BCF output
         tmp = line->shared;
         bcf1_sync_id(line, &tmp);
+        line->unpack_size[0] = tmp.l; prev_len = tmp.l;
+
         bcf1_sync_alleles(line, &tmp);
+        line->unpack_size[1] = tmp.l - prev_len; prev_len = tmp.l;
+
         bcf1_sync_filter(line, &tmp);
+        line->unpack_size[2] = tmp.l - prev_len;
+
         bcf1_sync_info(line, &tmp);
         line->shared = tmp;
     }
     else if ( line->d.shared_dirty )
     {
         // The line was edited, update the BCF data block, ptr_ori points
-        // to the original unchanged BCF data. 
+        // to the original unchanged BCF data.
         uint8_t *ptr_ori = (uint8_t *) line->shared.s;
 
+        assert( line->unpacked & BCF_UN_STR );
+
         // ID: single typed string
-        if ( line->d.shared_dirty & BCF1_DIRTY_ID ) 
+        if ( line->d.shared_dirty & BCF1_DIRTY_ID )
             bcf1_sync_id(line, &tmp);
-        else 
+        else
             kputsn_(ptr_ori, line->unpack_size[0], &tmp);
         ptr_ori += line->unpack_size[0];
+        line->unpack_size[0] = tmp.l; prev_len = tmp.l;
 
         // REF+ALT: list of typed strings
-        if ( line->d.shared_dirty & BCF1_DIRTY_ALS  )
+        if ( line->d.shared_dirty & BCF1_DIRTY_ALS )
             bcf1_sync_alleles(line, &tmp);
         else
         {
             kputsn_(ptr_ori, line->unpack_size[1], &tmp);
-            line->rlen = line->n_allele ? strlen(line->d.allele[0]) : 0;     // beware: this neglects SV's END tag
+            if ( !line->rlen && line->n_allele ) line->rlen = strlen(line->d.allele[0]);
         }
         ptr_ori += line->unpack_size[1];
+        line->unpack_size[1] = tmp.l - prev_len; prev_len = tmp.l;
 
-        // FILTER: typed vector of integers
-        if ( line->d.shared_dirty & BCF1_DIRTY_FLT )
-            bcf1_sync_filter(line, &tmp);
-        else if ( line->d.n_flt ) 
-            kputsn_(ptr_ori, line->unpack_size[2], &tmp);
-        else
-            bcf_enc_vint(&tmp, 0, 0, -1);
-        ptr_ori += line->unpack_size[2];
-
-        // INFO: pairs of typed vectors
-        if ( line->d.shared_dirty & BCF1_DIRTY_INF ) 
-            bcf1_sync_info(line, &tmp);
-        else
+        if ( line->unpacked & BCF_UN_FLT )
         {
-            int size = line->shared.l - (size_t)ptr_ori + (size_t)line->shared.s;
-            kputsn_(ptr_ori, size, &tmp);
+            // FILTER: typed vector of integers
+            if ( line->d.shared_dirty & BCF1_DIRTY_FLT )
+                bcf1_sync_filter(line, &tmp);
+            else if ( line->d.n_flt )
+                kputsn_(ptr_ori, line->unpack_size[2], &tmp);
+            else
+                bcf_enc_vint(&tmp, 0, 0, -1);
+            ptr_ori += line->unpack_size[2];
+            line->unpack_size[2] = tmp.l - prev_len;
+
+            if ( line->unpacked & BCF_UN_INFO )
+            {
+                // INFO: pairs of typed vectors
+                if ( line->d.shared_dirty & BCF1_DIRTY_INF )
+                {
+                    bcf1_sync_info(line, &tmp);
+                    ptr_ori = (uint8_t*)line->shared.s + line->shared.l;
+                }
+            }
         }
+
+        int size = line->shared.l - (size_t)ptr_ori + (size_t)line->shared.s;
+        if ( size ) kputsn_(ptr_ori, size, &tmp);
+
         free(line->shared.s);
         line->shared = tmp;
     }
+    if ( line->shared.s != shared_ori && line->unpacked & BCF_UN_INFO )
+    {
+        // Reallocated line->shared.s block invalidated line->d.info[].vptr pointers
+        size_t off_new = line->unpack_size[0] + line->unpack_size[1] + line->unpack_size[2];
+        int i;
+        for (i=0; i<line->n_info; i++)
+        {
+            uint8_t *vptr_free = line->d.info[i].vptr_free ? line->d.info[i].vptr - line->d.info[i].vptr_off : NULL;
+            line->d.info[i].vptr = (uint8_t*) line->shared.s + off_new + line->d.info[i].vptr_off;
+            off_new += line->d.info[i].vptr_len + line->d.info[i].vptr_off;
+            if ( vptr_free )
+            {
+                free(vptr_free);
+                line->d.info[i].vptr_free = 0;
+            }
+        }
+    }
+
     if ( line->n_sample && line->n_fmt && (!line->indiv.l || line->d.indiv_dirty) )
     {
         // The genotype fields changed or are not present
@@ -975,11 +1079,11 @@ static int bcf1_sync(bcf1_t *line)
         for (i=0; i<line->n_fmt; i++)
         {
             bcf_fmt_t *fmt = &line->d.fmt[i];
-            if ( !fmt->p ) 
-            { 
+            if ( !fmt->p )
+            {
                 // marked for removal
                 if ( irm < 0 ) irm = i;
-                continue; 
+                continue;
             }
             kputsn_(fmt->p - fmt->p_off, fmt->p_len + fmt->p_off, &tmp);
             if ( irm >=0 )
@@ -992,6 +1096,20 @@ static int bcf1_sync(bcf1_t *line)
         if ( irm>=0 ) line->n_fmt = irm;
         free(line->indiv.s);
         line->indiv = tmp;
+
+        // Reallocated line->indiv.s block invalidated line->d.fmt[].p pointers
+        size_t off_new = 0;
+        for (i=0; i<line->n_fmt; i++)
+        {
+            uint8_t *p_free = line->d.fmt[i].p_free ? line->d.fmt[i].p - line->d.fmt[i].p_off : NULL;
+            line->d.fmt[i].p = (uint8_t*) line->indiv.s + off_new + line->d.fmt[i].p_off;
+            off_new += line->d.fmt[i].p_len + line->d.fmt[i].p_off;
+            if ( p_free )
+            {
+                free(p_free);
+                line->d.fmt[i].p_free = 0;
+            }
+        }
     }
     if ( !line->n_sample ) line->n_fmt = 0;
     line->d.shared_dirty = line->d.indiv_dirty = 0;
@@ -1003,7 +1121,7 @@ bcf1_t *bcf_dup(bcf1_t *src)
     bcf1_sync(src);
 
     bcf1_t *out = bcf_init1();
-    
+
     out->rid  = src->rid;
     out->pos  = src->pos;
     out->rlen = src->rlen;
@@ -1024,11 +1142,16 @@ bcf1_t *bcf_dup(bcf1_t *src)
 
 int bcf_write(htsFile *hfp, const bcf_hdr_t *h, bcf1_t *v)
 {
-    assert( bcf_hdr_nsamples(h)==v->n_sample );
+    if ( bcf_hdr_nsamples(h)!=v->n_sample )
+    {
+        fprintf(stderr,"[%s:%d %s] Broken VCF record, the number of columns at %s:%d does not match the number of samples (%d vs %d).\n",
+                __FILE__,__LINE__,__FUNCTION__,bcf_seqname(h,v),v->pos+1, v->n_sample,bcf_hdr_nsamples(h));
+        return -1;
+    }
 
     if ( !hfp->is_bin ) return vcf_write(hfp,h,v);
 
-    if ( v->errcode ) 
+    if ( v->errcode )
     {
         // vcf_parse1() encountered a new contig or tag, undeclared in the
         // header.  At this point, the header must have been printed,
@@ -1040,16 +1163,16 @@ int bcf_write(htsFile *hfp, const bcf_hdr_t *h, bcf1_t *v)
     bcf1_sync(v);   // check if the BCF record was modified
 
     BGZF *fp = hfp->fp.bgzf;
-	uint32_t x[8];
-	x[0] = v->shared.l + 24; // to include six 32-bit integers
-	x[1] = v->indiv.l;
-	memcpy(x + 2, v, 16);
-	x[6] = (uint32_t)v->n_allele<<16 | v->n_info;
-	x[7] = (uint32_t)v->n_fmt<<24 | v->n_sample;
-	if ( bgzf_write(fp, x, 32) != 32 ) return -1;
+    uint32_t x[8];
+    x[0] = v->shared.l + 24; // to include six 32-bit integers
+    x[1] = v->indiv.l;
+    memcpy(x + 2, v, 16);
+    x[6] = (uint32_t)v->n_allele<<16 | v->n_info;
+    x[7] = (uint32_t)v->n_fmt<<24 | v->n_sample;
+    if ( bgzf_write(fp, x, 32) != 32 ) return -1;
     if ( bgzf_write(fp, v->shared.s, v->shared.l) != v->shared.l ) return -1;
-	if ( bgzf_write(fp, v->indiv.s, v->indiv.l) != v->indiv.l ) return -1;
-	return 0;
+    if ( bgzf_write(fp, v->indiv.s, v->indiv.l) != v->indiv.l ) return -1;
+    return 0;
 }
 
 /**********************
@@ -1096,7 +1219,7 @@ bcf_hdr_t *vcf_hdr_read(htsFile *fp)
         kputc('\n', &txt);
         if (s->s[1] != '#') break;
     }
-    if ( !txt.s ) 
+    if ( !txt.s )
     {
         fprintf(stderr,"[%s:%d %s] Could not read the header\n", __FILE__,__LINE__,__FUNCTION__);
         return NULL;
@@ -1111,7 +1234,7 @@ bcf_hdr_t *vcf_hdr_read(htsFile *fp)
         const char **names = tbx_seqnames(idx, &n);
         for (i=0; i<n; i++)
         {
-            bcf_hrec_t *hrec = bcf_hdr_get_hrec(h, BCF_DT_CTG, (char*) names[i]);
+            bcf_hrec_t *hrec = bcf_hdr_get_hrec(h, BCF_HL_CTG, "ID", (char*) names[i], NULL);
             if ( hrec ) continue;
             hrec = (bcf_hrec_t*) calloc(1,sizeof(bcf_hrec_t));
             hrec->key = strdup("contig");
@@ -1140,7 +1263,7 @@ int bcf_hdr_set(bcf_hdr_t *hdr, const char *fname)
     {
         int k;
         bcf_hrec_t *hrec = bcf_hdr_parse_line(hdr,lines[i],&k);
-        bcf_hdr_add_hrec(hdr, hrec);
+        if ( hrec ) bcf_hdr_add_hrec(hdr, hrec);
         free(lines[i]);
     }
     bcf_hdr_parse_sample_line(hdr,lines[n-1]);
@@ -1234,66 +1357,66 @@ int vcf_hdr_write(htsFile *fp, const bcf_hdr_t *h)
 
 void bcf_enc_vint(kstring_t *s, int n, int32_t *a, int wsize)
 {
-	int32_t max = INT32_MIN + 1, min = INT32_MAX;
-	int i;
-	if (n == 0) bcf_enc_size(s, 0, BCF_BT_NULL);
-	else if (n == 1) bcf_enc_int1(s, a[0]);
-	else {
-		if (wsize <= 0) wsize = n;
-		for (i = 0; i < n; ++i) {
-			if (a[i] == bcf_int32_missing || a[i] == bcf_int32_vector_end ) continue;
-			if (max < a[i]) max = a[i];
-			if (min > a[i]) min = a[i];
-		}
-		if (max <= INT8_MAX && min > bcf_int8_vector_end) {
-			bcf_enc_size(s, wsize, BCF_BT_INT8);
-			for (i = 0; i < n; ++i)
+    int32_t max = INT32_MIN + 1, min = INT32_MAX;
+    int i;
+    if (n == 0) bcf_enc_size(s, 0, BCF_BT_NULL);
+    else if (n == 1) bcf_enc_int1(s, a[0]);
+    else {
+        if (wsize <= 0) wsize = n;
+        for (i = 0; i < n; ++i) {
+            if (a[i] == bcf_int32_missing || a[i] == bcf_int32_vector_end ) continue;
+            if (max < a[i]) max = a[i];
+            if (min > a[i]) min = a[i];
+        }
+        if (max <= INT8_MAX && min > bcf_int8_vector_end) {
+            bcf_enc_size(s, wsize, BCF_BT_INT8);
+            for (i = 0; i < n; ++i)
                 if ( a[i]==bcf_int32_vector_end ) kputc(bcf_int8_vector_end, s);
                 else if ( a[i]==bcf_int32_missing ) kputc(bcf_int8_missing, s);
                 else kputc(a[i], s);
-		} else if (max <= INT16_MAX && min > bcf_int16_vector_end) {
-			bcf_enc_size(s, wsize, BCF_BT_INT16);
-			for (i = 0; i < n; ++i) 
+        } else if (max <= INT16_MAX && min > bcf_int16_vector_end) {
+            bcf_enc_size(s, wsize, BCF_BT_INT16);
+            for (i = 0; i < n; ++i)
             {
                 int16_t x;
                 if ( a[i]==bcf_int32_vector_end ) x = bcf_int16_vector_end;
                 else if ( a[i]==bcf_int32_missing ) x = bcf_int16_missing;
                 else x = a[i];
-				kputsn((char*)&x, 2, s);
-			}
-		} else {
-			bcf_enc_size(s, wsize, BCF_BT_INT32);
-			for (i = 0; i < n; ++i) {
-				int32_t x = a[i];
-				kputsn((char*)&x, 4, s);
-			}
-		}
-	}
+                kputsn((char*)&x, 2, s);
+            }
+        } else {
+            bcf_enc_size(s, wsize, BCF_BT_INT32);
+            for (i = 0; i < n; ++i) {
+                int32_t x = a[i];
+                kputsn((char*)&x, 4, s);
+            }
+        }
+    }
 }
 
 void bcf_enc_vfloat(kstring_t *s, int n, float *a)
 {
-	bcf_enc_size(s, n, BCF_BT_FLOAT);
-	kputsn((char*)a, n << 2, s);
+    bcf_enc_size(s, n, BCF_BT_FLOAT);
+    kputsn((char*)a, n << 2, s);
 }
 
 void bcf_enc_vchar(kstring_t *s, int l, const char *a)
 {
-	bcf_enc_size(s, l, BCF_BT_CHAR);
-	kputsn(a, l, s);
+    bcf_enc_size(s, l, BCF_BT_CHAR);
+    kputsn(a, l, s);
 }
 
 void bcf_fmt_array(kstring_t *s, int n, int type, void *data)
 {
-	int j = 0;
-	if (n == 0) {
-		kputc('.', s);
-		return;
-	}
-    if (type == BCF_BT_CHAR) 
+    int j = 0;
+    if (n == 0) {
+        kputc('.', s);
+        return;
+    }
+    if (type == BCF_BT_CHAR)
     {
         char *p = (char*)data;
-        for (j = 0; j < n && *p; ++j, ++p) 
+        for (j = 0; j < n && *p; ++j, ++p)
         {
             if ( *p==bcf_str_missing ) kputc('.', s);
             else kputc(*p, s);
@@ -1324,10 +1447,10 @@ void bcf_fmt_array(kstring_t *s, int n, int type, void *data)
 
 uint8_t *bcf_fmt_sized_array(kstring_t *s, uint8_t *ptr)
 {
-	int x, type;
-	x = bcf_dec_size(ptr, &ptr, &type);
-	bcf_fmt_array(s, x, type, ptr);
-	return ptr + (x << bcf_type_shift[type]);
+    int x, type;
+    x = bcf_dec_size(ptr, &ptr, &type);
+    bcf_fmt_array(s, x, type, ptr);
+    return ptr + (x << bcf_type_shift[type]);
 }
 
 /********************
@@ -1335,22 +1458,22 @@ uint8_t *bcf_fmt_sized_array(kstring_t *s, uint8_t *ptr)
  ********************/
 
 typedef struct {
-	int key, max_m, size, offset;
-	uint32_t is_gt:1, max_g:15, max_l:16;
-	uint32_t y;
-	uint8_t *buf;
+    int key, max_m, size, offset;
+    uint32_t is_gt:1, max_g:15, max_l:16;
+    uint32_t y;
+    uint8_t *buf;
 } fmt_aux_t;
 
 static inline void align_mem(kstring_t *s)
 {
-	if (s->l&7) {
-		uint64_t zero = 0;
-		int l = ((s->l + 7)>>3<<3) - s->l;
-		kputsn((char*)&zero, l, s);
-	}
+    if (s->l&7) {
+        uint64_t zero = 0;
+        int l = ((s->l + 7)>>3<<3) - s->l;
+        kputsn((char*)&zero, l, s);
+    }
 }
 
-// p,q is the start and the end of the FORMAT field 
+// p,q is the start and the end of the FORMAT field
 int _vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p, char *q)
 {
     if ( !bcf_hdr_nsamples(h) ) return 0;
@@ -1367,7 +1490,7 @@ int _vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p, char
     for (r = p, v->n_fmt = 1; *r; ++r)
         if (*r == ':') ++v->n_fmt;
     char *end = s->s + s->l;
-    if ( q>=end ) 
+    if ( q>=end )
     {
         fprintf(stderr,"[%s:%d %s] Error: FORMAT column with no sample columns starting at %s:%d\n", __FILE__,__LINE__,__FUNCTION__,s->s,v->pos+1);
         return -1;
@@ -1404,7 +1527,7 @@ int _vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p, char
         if ( h->keep_samples )
         {
             n_sample_ori++;
-            if ( !bit_array_test(h->keep_samples,n_sample_ori) ) 
+            if ( !bit_array_test(h->keep_samples,n_sample_ori) )
             {
                 while ( *r!='\t' && r<end ) r++;
                 if ( *r=='\t' ) { *r = 0; r++; }
@@ -1444,7 +1567,7 @@ int _vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p, char
             f->size = f->is_gt? f->max_g << 2 : f->max_l;
         } else if ((f->y>>4&0xf) == BCF_HT_REAL || (f->y>>4&0xf) == BCF_HT_INT) {
             f->size = f->max_m << 2;
-        } else 
+        } else
         {
             fprintf(stderr, "[E::%s] the format type %d currently not supported\n", __func__, f->y>>4&0xf);
             abort(); // I do not know how to do with Flag in the genotype fields
@@ -1473,7 +1596,7 @@ int _vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p, char
             }
         }
         if ( m == bcf_hdr_nsamples(h) ) break;
-        
+
         j = 0; // j-th format field, m-th sample
         while ( *t )
         {
@@ -1484,9 +1607,9 @@ int _vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p, char
                     for (l = 0;; ++t) {
                         if (*t == '.') ++t, x[l++] = is_phased;
                         else x[l++] = (strtol(t, &t, 10) + 1) << 1 | is_phased;
-                        #if THOROUGH_SANITY_CHECKS
-                            assert( 0 );    // success of strtol,strtod not checked
-                        #endif
+#if THOROUGH_SANITY_CHECKS
+                        assert( 0 );    // success of strtol,strtod not checked
+#endif
                         is_phased = (*t == '|');
                         if (*t == ':' || *t == 0) break;
                     }
@@ -1513,7 +1636,7 @@ int _vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p, char
                     else x[l++] = strtod(t, &t);
                     if (*t == ':' || *t == 0) break;
                 }
-                if ( !l ) bcf_float_set_missing(x[l++]);    // An empty field, insert missing value 
+                if ( !l ) bcf_float_set_missing(x[l++]);    // An empty field, insert missing value
                 for (; l < z->size>>2; ++l) bcf_float_set_vector_end(x[l]);
             } else abort();
             if (*t == 0) {
@@ -1540,8 +1663,8 @@ int _vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p, char
                     }
                 }
                 break;
-            } 
-            else 
+            }
+            else
             {
                 if (*t == ':') ++j;
                 t++;
@@ -1550,45 +1673,54 @@ int _vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p, char
         m++; t++;
     }
 
-	// write individual genotype information
-	kstring_t *str = &v->indiv;
+    // write individual genotype information
+    kstring_t *str = &v->indiv;
     int i;
-	if (v->n_sample > 0) {
-		for (i = 0; i < v->n_fmt; ++i) {
-			fmt_aux_t *z = &fmt[i];
-			bcf_enc_int1(str, z->key);
-			if ((z->y>>4&0xf) == BCF_HT_STR && !z->is_gt) {
-				bcf_enc_size(str, z->size, BCF_BT_CHAR);
-				kputsn((char*)z->buf, z->size * v->n_sample, str);
-			} else if ((z->y>>4&0xf) == BCF_HT_INT || z->is_gt) {
-				bcf_enc_vint(str, (z->size>>2) * v->n_sample, (int32_t*)z->buf, z->size>>2);
-			} else {
-				bcf_enc_size(str, z->size>>2, BCF_BT_FLOAT);
-				kputsn((char*)z->buf, z->size * v->n_sample, str);
-			}
-		}
-	}
+    if (v->n_sample > 0) {
+        for (i = 0; i < v->n_fmt; ++i) {
+            fmt_aux_t *z = &fmt[i];
+            bcf_enc_int1(str, z->key);
+            if ((z->y>>4&0xf) == BCF_HT_STR && !z->is_gt) {
+                bcf_enc_size(str, z->size, BCF_BT_CHAR);
+                kputsn((char*)z->buf, z->size * v->n_sample, str);
+            } else if ((z->y>>4&0xf) == BCF_HT_INT || z->is_gt) {
+                bcf_enc_vint(str, (z->size>>2) * v->n_sample, (int32_t*)z->buf, z->size>>2);
+            } else {
+                bcf_enc_size(str, z->size>>2, BCF_BT_FLOAT);
+                kputsn((char*)z->buf, z->size * v->n_sample, str);
+            }
+        }
+    }
+
+    if ( v->n_sample!=bcf_hdr_nsamples(h) )
+    {
+        fprintf(stderr,"[%s:%d %s] Number of columns at %s:%d does not match the number of samples (%d vs %d).\n",
+                __FILE__,__LINE__,__FUNCTION__,bcf_seqname(h,v),v->pos+1, v->n_sample,bcf_hdr_nsamples(h));
+        v->errcode |= BCF_ERR_NCOLS;
+        return -1;
+    }
+
     return 0;
 }
 
 int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v)
 {
-	int i = 0;
-	char *p, *q, *r, *t;
-	kstring_t *str;
-	khint_t k;
-	ks_tokaux_t aux;
+    int i = 0;
+    char *p, *q, *r, *t;
+    kstring_t *str;
+    khint_t k;
+    ks_tokaux_t aux;
 
     bcf_clear1(v);
-	str = &v->shared;
-	memset(&aux, 0, sizeof(ks_tokaux_t));
-	for (p = kstrtok(s->s, "\t", &aux), i = 0; p; p = kstrtok(0, 0, &aux), ++i) {
-		q = (char*)aux.p;
-		*q = 0;
-		if (i == 0) { // CHROM
-			vdict_t *d = (vdict_t*)h->dict[BCF_DT_CTG];
-			k = kh_get(vdict, d, p);
-			if (k == kh_end(d)) 
+    str = &v->shared;
+    memset(&aux, 0, sizeof(ks_tokaux_t));
+    for (p = kstrtok(s->s, "\t", &aux), i = 0; p; p = kstrtok(0, 0, &aux), ++i) {
+        q = (char*)aux.p;
+        *q = 0;
+        if (i == 0) { // CHROM
+            vdict_t *d = (vdict_t*)h->dict[BCF_DT_CTG];
+            k = kh_get(vdict, d, p);
+            if (k == kh_end(d))
             {
                 // Simple error recovery for chromosomes not defined in the header. It will not help when VCF header has
                 // been already printed, but will enable tools like vcfcheck to proceed.
@@ -1601,47 +1733,47 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v)
                 if ( bcf_hdr_add_hrec((bcf_hdr_t*)h, hrec) ) bcf_hdr_sync((bcf_hdr_t*)h);
                 k = kh_get(vdict, d, p);
                 v->errcode = BCF_ERR_CTG_UNDEF;
-			}
+            }
             v->rid = kh_val(d, k).id;
-		} else if (i == 1) { // POS
-			v->pos = atoi(p) - 1;
-		} else if (i == 2) { // ID
-			if (strcmp(p, ".")) bcf_enc_vchar(str, q - p, p);
-			else bcf_enc_size(str, 0, BCF_BT_CHAR);
-		} else if (i == 3) { // REF
-			bcf_enc_vchar(str, q - p, p);
-			v->n_allele = 1, v->rlen = q - p;
-		} else if (i == 4) { // ALT
-			if (strcmp(p, ".")) {
-				for (r = t = p;; ++r) {
-					if (*r == ',' || *r == 0) {
-						bcf_enc_vchar(str, r - t, t);
-						t = r + 1;
-						++v->n_allele;
-					}
-					if (r == q) break;
-				}
-			}
-		} else if (i == 5) { // QUAL
-			if (strcmp(p, ".")) v->qual = atof(p);
-			else memcpy(&v->qual, &bcf_float_missing, 4);
+        } else if (i == 1) { // POS
+            v->pos = atoi(p) - 1;
+        } else if (i == 2) { // ID
+            if (strcmp(p, ".")) bcf_enc_vchar(str, q - p, p);
+            else bcf_enc_size(str, 0, BCF_BT_CHAR);
+        } else if (i == 3) { // REF
+            bcf_enc_vchar(str, q - p, p);
+            v->n_allele = 1, v->rlen = q - p;
+        } else if (i == 4) { // ALT
+            if (strcmp(p, ".")) {
+                for (r = t = p;; ++r) {
+                    if (*r == ',' || *r == 0) {
+                        bcf_enc_vchar(str, r - t, t);
+                        t = r + 1;
+                        ++v->n_allele;
+                    }
+                    if (r == q) break;
+                }
+            }
+        } else if (i == 5) { // QUAL
+            if (strcmp(p, ".")) v->qual = atof(p);
+            else memcpy(&v->qual, &bcf_float_missing, 4);
             if ( v->max_unpack && !(v->max_unpack>>1) ) return 0; // BCF_UN_STR
-		} else if (i == 6) { // FILTER
-			if (strcmp(p, ".")) {
-				int32_t *a;
-				int n_flt = 1, i;
-				ks_tokaux_t aux1;
-				vdict_t *d = (vdict_t*)h->dict[BCF_DT_ID];
-				// count the number of filters
-				if (*(q-1) == ';') *(q-1) = 0;
-				for (r = p; *r; ++r)
-					if (*r == ';') ++n_flt;
-				a = (int32_t*)alloca(n_flt * 4);
-				// add filters
-				for (t = kstrtok(p, ";", &aux1), i = 0; t; t = kstrtok(0, 0, &aux1)) {
-					*(char*)aux1.p = 0;
-					k = kh_get(vdict, d, t);
-					if (k == kh_end(d)) 
+        } else if (i == 6) { // FILTER
+            if (strcmp(p, ".")) {
+                int32_t *a;
+                int n_flt = 1, i;
+                ks_tokaux_t aux1;
+                vdict_t *d = (vdict_t*)h->dict[BCF_DT_ID];
+                // count the number of filters
+                if (*(q-1) == ';') *(q-1) = 0;
+                for (r = p; *r; ++r)
+                    if (*r == ';') ++n_flt;
+                a = (int32_t*)alloca(n_flt * 4);
+                // add filters
+                for (t = kstrtok(p, ";", &aux1), i = 0; t; t = kstrtok(0, 0, &aux1)) {
+                    *(char*)aux1.p = 0;
+                    k = kh_get(vdict, d, t);
+                    if (k == kh_end(d))
                     {
                         // Simple error recovery for FILTERs not defined in the header. It will not help when VCF header has
                         // been already printed, but will enable tools like vcfcheck to proceed.
@@ -1655,31 +1787,31 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v)
                         k = kh_get(vdict, d, t);
                         v->errcode = BCF_ERR_TAG_UNDEF;
                     }
-					a[i++] = kh_val(d, k).id;
-				}
-				n_flt = i;
-				bcf_enc_vint(str, n_flt, a, -1);
-			} else bcf_enc_vint(str, 0, 0, -1);
+                    a[i++] = kh_val(d, k).id;
+                }
+                n_flt = i;
+                bcf_enc_vint(str, n_flt, a, -1);
+            } else bcf_enc_vint(str, 0, 0, -1);
             if ( v->max_unpack && !(v->max_unpack>>2) ) return 0;    // BCF_UN_FLT
-		} else if (i == 7) { // INFO
-			char *key;
-			vdict_t *d = (vdict_t*)h->dict[BCF_DT_ID];
-			v->n_info = 0;
-			if (strcmp(p, ".")) {
-				if (*(q-1) == ';') *(q-1) = 0;
-				for (r = key = p;; ++r) {
-					int c;
-					char *val, *end;
-					if (*r != ';' && *r != '=' && *r != 0) continue;
-					val = end = 0;
-					c = *r; *r = 0;
-					if (c == '=') {
-						val = r + 1;
-						for (end = val; *end != ';' && *end != 0; ++end);
-						c = *end; *end = 0;
-					} else end = r;
-					k = kh_get(vdict, d, key);
-					if (k == kh_end(d) || kh_val(d, k).info[BCF_HL_INFO] == 15) 
+        } else if (i == 7) { // INFO
+            char *key;
+            vdict_t *d = (vdict_t*)h->dict[BCF_DT_ID];
+            v->n_info = 0;
+            if (strcmp(p, ".")) {
+                if (*(q-1) == ';') *(q-1) = 0;
+                for (r = key = p;; ++r) {
+                    int c;
+                    char *val, *end;
+                    if (*r != ';' && *r != '=' && *r != 0) continue;
+                    val = end = 0;
+                    c = *r; *r = 0;
+                    if (c == '=') {
+                        val = r + 1;
+                        for (end = val; *end != ';' && *end != 0; ++end);
+                        c = *end; *end = 0;
+                    } else end = r;
+                    k = kh_get(vdict, d, key);
+                    if (k == kh_end(d) || kh_val(d, k).info[BCF_HL_INFO] == 15)
                     {
                         fprintf(stderr, "[W::%s] INFO '%s' is not defined in the header, assuming Type=String\n", __func__, key);
                         kstring_t tmp = {0,0,0};
@@ -1739,11 +1871,11 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v)
                     key = r + 1;
                 }
             }
-            if ( v->max_unpack && !(v->max_unpack>>3) ) return 0; 
-		} else if (i == 8) // FORMAT
+            if ( v->max_unpack && !(v->max_unpack>>3) ) return 0;
+        } else if (i == 8) // FORMAT
             return _vcf_parse_format(s, h, v, p, q);
-	}
-	return 0;
+    }
+    return 0;
 }
 
 int vcf_read(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v)
@@ -1791,19 +1923,19 @@ static inline uint8_t *bcf_unpack_info_core1(uint8_t *ptr, bcf_info_t *info)
 int bcf_unpack(bcf1_t *b, int which)
 {
     if ( !b->shared.l ) return 0; // Building a new BCF record from scratch
-	uint8_t *ptr = (uint8_t*)b->shared.s, *ptr_ori;
-	int *offset, i;
-	bcf_dec_t *d = &b->d;
-	if (which & BCF_UN_FLT) which |= BCF_UN_STR;
-	if (which & BCF_UN_INFO) which |= BCF_UN_SHR;
-	if ((which&BCF_UN_STR) && !(b->unpacked&BCF_UN_STR)) 
-    { 
-        kstring_t tmp; 
+    uint8_t *ptr = (uint8_t*)b->shared.s, *ptr_ori;
+    int *offset, i;
+    bcf_dec_t *d = &b->d;
+    if (which & BCF_UN_FLT) which |= BCF_UN_STR;
+    if (which & BCF_UN_INFO) which |= BCF_UN_SHR;
+    if ((which&BCF_UN_STR) && !(b->unpacked&BCF_UN_STR))
+    {
+        kstring_t tmp;
 
         // ID
         tmp.l = 0; tmp.s = d->id; tmp.m = d->m_id;
         ptr_ori = ptr;
-        ptr = bcf_fmt_sized_array(&tmp, ptr); 
+        ptr = bcf_fmt_sized_array(&tmp, ptr);
         b->unpack_size[0] = ptr - ptr_ori;
         kputc('\0', &tmp);
         d->id = tmp.s; d->m_id = tmp.m;
@@ -1823,92 +1955,98 @@ int bcf_unpack(bcf1_t *b, int which)
         hts_expand(char*, b->n_allele, d->m_allele, d->allele); // NM: hts_expand() is a macro
         for (i = 0; i < b->n_allele; ++i)
             d->allele[i] = d->als + offset[i];
-        b->unpack_ptr = ptr;
         b->unpacked |= BCF_UN_STR;
-	}
-	if ((which&BCF_UN_FLT) && !(b->unpacked&BCF_UN_FLT)) { // FILTER
-		ptr = b->unpack_ptr;
+    }
+    if ((which&BCF_UN_FLT) && !(b->unpacked&BCF_UN_FLT)) { // FILTER
+        ptr = (uint8_t*)b->shared.s + b->unpack_size[0] + b->unpack_size[1];
         ptr_ori = ptr;
-		if (*ptr>>4) {
-			int type;
-			d->n_flt = bcf_dec_size(ptr, &ptr, &type);
-			hts_expand(int, d->n_flt, d->m_flt, d->flt);
-			for (i = 0; i < d->n_flt; ++i)
-				d->flt[i] = bcf_dec_int1(ptr, type, &ptr);
-		} else ++ptr, d->n_flt = 0;
+        if (*ptr>>4) {
+            int type;
+            d->n_flt = bcf_dec_size(ptr, &ptr, &type);
+            hts_expand(int, d->n_flt, d->m_flt, d->flt);
+            for (i = 0; i < d->n_flt; ++i)
+                d->flt[i] = bcf_dec_int1(ptr, type, &ptr);
+        } else ++ptr, d->n_flt = 0;
         b->unpack_size[2] = ptr - ptr_ori;
-		b->unpack_ptr = ptr;
-		b->unpacked |= BCF_UN_FLT;
-	}
-	if ((which&BCF_UN_INFO) && !(b->unpacked&BCF_UN_INFO)) { // INFO
-		ptr = b->unpack_ptr;
-		hts_expand(bcf_info_t, b->n_info, d->m_info, d->info);
-	    for (i = 0; i < d->m_info; ++i) d->info[i].vptr_free = 0;
-	    for (i = 0; i < b->n_info; ++i)
+        b->unpacked |= BCF_UN_FLT;
+    }
+    if ((which&BCF_UN_INFO) && !(b->unpacked&BCF_UN_INFO)) { // INFO
+        ptr = (uint8_t*)b->shared.s + b->unpack_size[0] + b->unpack_size[1] + b->unpack_size[2];
+        hts_expand(bcf_info_t, b->n_info, d->m_info, d->info);
+        for (i = 0; i < d->m_info; ++i) d->info[i].vptr_free = 0;
+        for (i = 0; i < b->n_info; ++i)
             ptr = bcf_unpack_info_core1(ptr, &d->info[i]);
-		b->unpacked |= BCF_UN_INFO;
-	}
-	if ((which&BCF_UN_FMT) && b->n_sample && !(b->unpacked&BCF_UN_FMT)) { // FORMAT
+        b->unpacked |= BCF_UN_INFO;
+    }
+    if ((which&BCF_UN_FMT) && b->n_sample && !(b->unpacked&BCF_UN_FMT)) { // FORMAT
         ptr = (uint8_t*)b->indiv.s;
-		hts_expand(bcf_fmt_t, b->n_fmt, d->m_fmt, d->fmt);
+        hts_expand(bcf_fmt_t, b->n_fmt, d->m_fmt, d->fmt);
         for (i = 0; i < d->m_fmt; ++i) d->fmt[i].p_free = 0;
         for (i = 0; i < b->n_fmt; ++i)
             ptr = bcf_unpack_fmt_core1(ptr, b->n_sample, &d->fmt[i]);
-		b->unpacked |= BCF_UN_FMT;
-	}
-	return 0;
+        b->unpacked |= BCF_UN_FMT;
+    }
+    return 0;
 }
 
 int vcf_format(const bcf_hdr_t *h, const bcf1_t *v, kstring_t *s)
 {
-	int i;
-	bcf_unpack((bcf1_t*)v, BCF_UN_ALL);
-	kputs(h->id[BCF_DT_CTG][v->rid].key, s); // CHROM
-	kputc('\t', s); kputw(v->pos + 1, s); // POS
-	kputc('\t', s); kputs(v->d.id ? v->d.id : ".", s); // ID
-	kputc('\t', s); // REF
-	if (v->n_allele > 0) kputs(v->d.allele[0], s);
-	else kputc('.', s);
-	kputc('\t', s); // ALT
-	if (v->n_allele > 1) {
-		for (i = 1; i < v->n_allele; ++i) {
-			if (i > 1) kputc(',', s);
-			kputs(v->d.allele[i], s);
-		}
-	} else kputc('.', s);
-	kputc('\t', s); // QUAL
-	if (memcmp(&v->qual, &bcf_float_missing, 4) == 0) kputc('.', s); // QUAL
-	else ksprintf(s, "%g", v->qual);
-	kputc('\t', s); // FILTER
-	if (v->d.n_flt) {
-		for (i = 0; i < v->d.n_flt; ++i) {
-			if (i) kputc(';', s);
-			kputs(h->id[BCF_DT_ID][v->d.flt[i]].key, s);
-		}
-	} else kputc('.', s);
-	kputc('\t', s); // INFO
-	if (v->n_info) {
+    int i;
+    bcf_unpack((bcf1_t*)v, BCF_UN_ALL);
+    kputs(h->id[BCF_DT_CTG][v->rid].key, s); // CHROM
+    kputc('\t', s); kputw(v->pos + 1, s); // POS
+    kputc('\t', s); kputs(v->d.id ? v->d.id : ".", s); // ID
+    kputc('\t', s); // REF
+    if (v->n_allele > 0) kputs(v->d.allele[0], s);
+    else kputc('.', s);
+    kputc('\t', s); // ALT
+    if (v->n_allele > 1) {
+        for (i = 1; i < v->n_allele; ++i) {
+            if (i > 1) kputc(',', s);
+            kputs(v->d.allele[i], s);
+        }
+    } else kputc('.', s);
+    kputc('\t', s); // QUAL
+    if ( bcf_float_is_missing(v->qual) ) kputc('.', s); // QUAL
+    else ksprintf(s, "%g", v->qual);
+    kputc('\t', s); // FILTER
+    if (v->d.n_flt) {
+        for (i = 0; i < v->d.n_flt; ++i) {
+            if (i) kputc(';', s);
+            kputs(h->id[BCF_DT_ID][v->d.flt[i]].key, s);
+        }
+    } else kputc('.', s);
+    kputc('\t', s); // INFO
+    if (v->n_info) {
         int first = 1;
-		for (i = 0; i < v->n_info; ++i) {
-			bcf_info_t *z = &v->d.info[i];
+        for (i = 0; i < v->n_info; ++i) {
+            bcf_info_t *z = &v->d.info[i];
             if ( !z->vptr ) continue;
-			if ( !first ) kputc(';', s); first = 0;
-			kputs(h->id[BCF_DT_ID][z->key].key, s);
-			if (z->len <= 0) continue;
-			kputc('=', s);
-			if (z->len == 1) {
-				if (z->type == BCF_BT_FLOAT) ksprintf(s, "%g", z->v1.f);
-				else if (z->type != BCF_BT_CHAR) kputw(z->v1.i, s);
-				else kputc(z->v1.i, s);
-			} else bcf_fmt_array(s, z->len, z->type, z->vptr);
-		}
+            if ( !first ) kputc(';', s); first = 0;
+            kputs(h->id[BCF_DT_ID][z->key].key, s);
+            if (z->len <= 0) continue;
+            kputc('=', s);
+            if (z->len == 1) 
+            {
+                switch (z->type) 
+                {
+                    case BCF_BT_INT8:  if ( z->v1.i==bcf_int8_missing ) kputc('.', s); else kputw(z->v1.i, s); break;
+                    case BCF_BT_INT16: if ( z->v1.i==bcf_int16_missing ) kputc('.', s); else kputw(z->v1.i, s); break;
+                    case BCF_BT_INT32: if ( z->v1.i==bcf_int32_missing ) kputc('.', s); else kputw(z->v1.i, s); break;
+                    case BCF_BT_FLOAT: if ( bcf_float_is_missing(z->v1.f) ) kputc('.', s); else ksprintf(s, "%g", z->v1.f); break;
+                    case BCF_BT_CHAR:  kputc(z->v1.i, s); break;
+                    default: fprintf(stderr,"todo: type %d\n", z->type); exit(1); break;
+                }
+            }
+            else bcf_fmt_array(s, z->len, z->type, z->vptr);
+        }
         if ( first ) kputc('.', s);
-	} else kputc('.', s);
-	// FORMAT and individual information
-	if (v->n_sample)
+    } else kputc('.', s);
+    // FORMAT and individual information
+    if (v->n_sample)
     {
         int i,j;
-        if ( v->n_fmt) 
+        if ( v->n_fmt)
         {
             int gt_i = -1;
             bcf_fmt_t *fmt = v->d.fmt;
@@ -1916,7 +2054,7 @@ int vcf_format(const bcf_hdr_t *h, const bcf1_t *v, kstring_t *s)
             for (i = 0; i < (int)v->n_fmt; ++i) {
                 if ( !fmt[i].p ) continue;
                 kputc(!first ? ':' : '\t', s); first = 0;
-                if ( fmt[i].id<0 ) //!bcf_hdr_idinfo_exists(h,BCF_HL_FMT,fmt[i].id) ) 
+                if ( fmt[i].id<0 ) //!bcf_hdr_idinfo_exists(h,BCF_HL_FMT,fmt[i].id) )
                 {
                     fprintf(stderr, "[E::%s] invalid BCF, the FORMAT tag id=%d not present in the header.\n", __func__, fmt[i].id);
                     abort();
@@ -1943,9 +2081,9 @@ int vcf_format(const bcf_hdr_t *h, const bcf1_t *v, kstring_t *s)
         else
             for (j=0; j<=v->n_sample; j++)
                 kputs("\t.", s);
-	}
+    }
     kputc('\n', s);
-	return 0;
+    return 0;
 }
 
 int vcf_write_line(htsFile *fp, kstring_t *line)
@@ -1977,10 +2115,10 @@ int vcf_write(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v)
 
 int bcf_hdr_id2int(const bcf_hdr_t *h, int which, const char *id)
 {
-	khint_t k;
-	vdict_t *d = (vdict_t*)h->dict[which];
-	k = kh_get(vdict, d, id);
-	return k == kh_end(d)? -1 : kh_val(d, k).id;
+    khint_t k;
+    vdict_t *d = (vdict_t*)h->dict[which];
+    k = kh_get(vdict, d, id);
+    return k == kh_end(d)? -1 : kh_val(d, k).id;
 }
 
 
@@ -1990,62 +2128,62 @@ int bcf_hdr_id2int(const bcf_hdr_t *h, int which, const char *id)
 
 hts_idx_t *bcf_index(htsFile *fp, int min_shift)
 {
-	int n_lvls, i;
-	bcf1_t *b;
-	hts_idx_t *idx;
-	bcf_hdr_t *h;
-	int64_t max_len = 0, s;
-	h = bcf_hdr_read(fp);
+    int n_lvls, i;
+    bcf1_t *b;
+    hts_idx_t *idx;
+    bcf_hdr_t *h;
+    int64_t max_len = 0, s;
+    h = bcf_hdr_read(fp);
     if ( !h ) return NULL;
     int nids = 0;
-	for (i = 0; i < h->n[BCF_DT_CTG]; ++i)
+    for (i = 0; i < h->n[BCF_DT_CTG]; ++i)
     {
         if ( !h->id[BCF_DT_CTG][i].val ) continue;
-		if ( max_len < h->id[BCF_DT_CTG][i].val->info[0] ) max_len = h->id[BCF_DT_CTG][i].val->info[0];
+        if ( max_len < h->id[BCF_DT_CTG][i].val->info[0] ) max_len = h->id[BCF_DT_CTG][i].val->info[0];
         nids++;
     }
     if ( !max_len ) max_len = ((int64_t)1<<31) - 1;  // In case contig line is broken.
-	max_len += 256;
-	for (n_lvls = 0, s = 1<<min_shift; max_len > s; ++n_lvls, s <<= 3);
-	idx = hts_idx_init(nids, HTS_FMT_CSI, bgzf_tell(fp->fp.bgzf), min_shift, n_lvls);
-	b = bcf_init1();
-	while (bcf_read1(fp,h, b) >= 0) {
-		int ret;
-		ret = hts_idx_push(idx, b->rid, b->pos, b->pos + b->rlen, bgzf_tell(fp->fp.bgzf), 1);
-		if (ret < 0)
+    max_len += 256;
+    for (n_lvls = 0, s = 1<<min_shift; max_len > s; ++n_lvls, s <<= 3);
+    idx = hts_idx_init(nids, HTS_FMT_CSI, bgzf_tell(fp->fp.bgzf), min_shift, n_lvls);
+    b = bcf_init1();
+    while (bcf_read1(fp,h, b) >= 0) {
+        int ret;
+        ret = hts_idx_push(idx, b->rid, b->pos, b->pos + b->rlen, bgzf_tell(fp->fp.bgzf), 1);
+        if (ret < 0)
         {
             bcf_destroy1(b);
             hts_idx_destroy(idx);
             return NULL;
         }
-	}
-	hts_idx_finish(idx, bgzf_tell(fp->fp.bgzf));
-	bcf_destroy1(b);
-	bcf_hdr_destroy(h);
-	return idx;
+    }
+    hts_idx_finish(idx, bgzf_tell(fp->fp.bgzf));
+    bcf_destroy1(b);
+    bcf_hdr_destroy(h);
+    return idx;
 }
 
 int bcf_index_build(const char *fn, int min_shift)
 {
     htsFile *fp;
-	hts_idx_t *idx;
-	if ((fp = hts_open(fn, "rb")) == 0) return -1;
+    hts_idx_t *idx;
+    if ((fp = hts_open(fn, "rb")) == 0) return -1;
     if ( !fp->fp.bgzf->is_compressed ) { hts_close(fp); return -1; }
-	idx = bcf_index(fp, min_shift);
-	hts_close(fp);
+    idx = bcf_index(fp, min_shift);
+    hts_close(fp);
     if ( !idx ) return -1;
-	hts_idx_save(idx, fn, HTS_FMT_CSI);
-	hts_idx_destroy(idx);
-	return 0;
+    hts_idx_save(idx, fn, HTS_FMT_CSI);
+    hts_idx_destroy(idx);
+    return 0;
 }
 
 /*****************
  *** Utilities ***
  *****************/
 
-void bcf_hdr_combine(bcf_hdr_t *dst, const bcf_hdr_t *src)
+int bcf_hdr_combine(bcf_hdr_t *dst, const bcf_hdr_t *src)
 {
-    int i, ndst_ori = dst->nhrec, need_sync = 0;
+    int i, ndst_ori = dst->nhrec, need_sync = 0, ret = 0;
     for (i=0; i<src->nhrec; i++)
     {
         if ( src->hrec[i]->type==BCF_HL_GEN && src->hrec[i]->value )
@@ -2054,19 +2192,52 @@ void bcf_hdr_combine(bcf_hdr_t *dst, const bcf_hdr_t *src)
             for (j=0; j<ndst_ori; j++)
             {
                 if ( dst->hrec[j]->type!=BCF_HL_GEN ) continue;
-                if ( !strcmp(src->hrec[i]->key,dst->hrec[j]->key) && !strcmp(src->hrec[i]->value,dst->hrec[j]->value) ) break;
+
+                // Checking only the key part of generic lines, otherwise
+                // the VCFs are too verbose. Should we perhaps add a flag
+                // to bcf_hdr_combine() and make this optional?
+                if ( !strcmp(src->hrec[i]->key,dst->hrec[j]->key) ) break;
             }
             if ( j>=ndst_ori )
                 need_sync += bcf_hdr_add_hrec(dst, bcf_hrec_dup(src->hrec[i]));
         }
+        else if ( src->hrec[i]->type==BCF_HL_STR )
+        {
+            // NB: we are ignoring fields without ID
+            int j = bcf_hrec_find_key(src->hrec[i],"ID");
+            if ( j>=0 )
+            {
+                bcf_hrec_t *rec = bcf_hdr_get_hrec(dst, src->hrec[i]->type, "ID", src->hrec[i]->vals[j], src->hrec[i]->key);
+                if ( !rec )
+                    need_sync += bcf_hdr_add_hrec(dst, bcf_hrec_dup(src->hrec[i]));
+            }
+        }
         else
         {
-            bcf_hrec_t *rec = bcf_hdr_get_hrec(dst, src->hrec[i]->type, src->hrec[i]->vals[0]);
+            int j = bcf_hrec_find_key(src->hrec[i],"ID");
+            assert( j>=0 ); // this should always be true for valid VCFs
+
+            bcf_hrec_t *rec = bcf_hdr_get_hrec(dst, src->hrec[i]->type, "ID", src->hrec[i]->vals[j], NULL);
             if ( !rec )
                 need_sync += bcf_hdr_add_hrec(dst, bcf_hrec_dup(src->hrec[i]));
+            else if ( src->hrec[i]->type==BCF_HL_INFO || src->hrec[i]->type==BCF_HL_FMT )
+            {
+                // Check that both records are of the same type. The bcf_hdr_id2length
+                // macro cannot be used here because dst header is not synced yet.
+                vdict_t *d_src = (vdict_t*)src->dict[BCF_DT_ID];
+                vdict_t *d_dst = (vdict_t*)dst->dict[BCF_DT_ID];
+                khint_t k_src  = kh_get(vdict, d_src, src->hrec[i]->vals[0]);
+                khint_t k_dst  = kh_get(vdict, d_dst, src->hrec[i]->vals[0]);
+                if ( (kh_val(d_src,k_src).info[rec->type]>>8 & 0xf) != (kh_val(d_dst,k_dst).info[rec->type]>>8 & 0xf) )
+                {
+                    fprintf(stderr,"Warning: trying to combine \"%s\" tag definitions of different lengths\n", src->hrec[i]->vals[0]);
+                    ret |= 1;
+                }
+            }
         }
     }
     if ( need_sync ) bcf_hdr_sync(dst);
+    return ret;
 }
 int bcf_translate(const bcf_hdr_t *dst_hdr, bcf_hdr_t *src_hdr, bcf1_t *line)
 {
@@ -2104,15 +2275,16 @@ int bcf_translate(const bcf_hdr_t *dst_hdr, bcf_hdr_t *src_hdr, bcf1_t *line)
     }
     bcf_unpack(line,BCF_UN_ALL);
 
-    // CHROM 
+    // CHROM
     if ( src_hdr->transl[BCF_DT_CTG][line->rid] >=0 ) line->rid = src_hdr->transl[BCF_DT_CTG][line->rid];
 
     // FILTER
     for (i=0; i<line->d.n_flt; i++)
     {
         int src_id = line->d.flt[i];
-        if ( src_hdr->transl[BCF_DT_ID][src_id] >=0 ) 
+        if ( src_hdr->transl[BCF_DT_ID][src_id] >=0 )
             line->d.flt[i] = src_hdr->transl[BCF_DT_ID][src_id];
+        line->d.shared_dirty |= BCF1_DIRTY_FLT;
     }
 
     // INFO
@@ -2137,6 +2309,7 @@ int bcf_translate(const bcf_hdr_t *dst_hdr, bcf_hdr_t *src_hdr, bcf1_t *line)
             assert( !info->vptr_free );
             kstring_t str = {0,0,0};
             bcf_enc_int1(&str, dst_id);
+            bcf_enc_size(&str, info->len,info->type);
             info->vptr_off = str.l;
             kputsn((char*)info->vptr, info->vptr_len, &str);
             info->vptr = (uint8_t*)str.s + info->vptr_off;
@@ -2168,6 +2341,7 @@ int bcf_translate(const bcf_hdr_t *dst_hdr, bcf_hdr_t *src_hdr, bcf1_t *line)
             assert( !fmt->p_free );
             kstring_t str = {0,0,0};
             bcf_enc_int1(&str, dst_id);
+            bcf_enc_size(&str, fmt->n, fmt->type);
             fmt->p_off = str.l;
             kputsn((char*)fmt->p, fmt->p_len, &str);
             fmt->p = (uint8_t*)str.s + fmt->p_off;
@@ -2192,42 +2366,42 @@ bcf_hdr_t *bcf_hdr_subset(const bcf_hdr_t *h0, int n, char *const* samples, int
 {
     int hlen;
     char *htxt = bcf_hdr_fmt_text(h0, 1, &hlen);
-	kstring_t str;
-	bcf_hdr_t *h;
-	str.l = str.m = 0; str.s = 0;
-	h = bcf_hdr_init("w");
+    kstring_t str;
+    bcf_hdr_t *h;
+    str.l = str.m = 0; str.s = 0;
+    h = bcf_hdr_init("w");
     bcf_hdr_set_version(h,bcf_hdr_get_version(h0));
     int j;
     for (j=0; j<n; j++) imap[j] = -1;
-	if ( bcf_hdr_nsamples(h0) > 0) {
-		char *p;
-		int i = 0, end = n? 8 : 7;
-		while ((p = strstr(htxt, "#CHROM\t")) != 0)
-			if (p > htxt && *(p-1) == '\n') break;
-		while ((p = strchr(p, '\t')) != 0 && i < end) ++i, ++p;
-		if (i != end) {
-			free(h); free(str.s);
-			return 0; // malformated header
-		}
-		kputsn(htxt, p - htxt, &str);
-		for (i = 0; i < n; ++i) {
-			imap[i] = bcf_hdr_id2int(h0, BCF_DT_SAMPLE, samples[i]);
-			if (imap[i] < 0) continue;
-			kputc('\t', &str);
-			kputs(samples[i], &str);
-		}
-	} else kputsn(htxt, hlen, &str);
+    if ( bcf_hdr_nsamples(h0) > 0) {
+        char *p;
+        int i = 0, end = n? 8 : 7;
+        while ((p = strstr(htxt, "#CHROM\t")) != 0)
+            if (p > htxt && *(p-1) == '\n') break;
+        while ((p = strchr(p, '\t')) != 0 && i < end) ++i, ++p;
+        if (i != end) {
+            free(h); free(str.s);
+            return 0; // malformated header
+        }
+        kputsn(htxt, p - htxt, &str);
+        for (i = 0; i < n; ++i) {
+            imap[i] = bcf_hdr_id2int(h0, BCF_DT_SAMPLE, samples[i]);
+            if (imap[i] < 0) continue;
+            kputc('\t', &str);
+            kputs(samples[i], &str);
+        }
+    } else kputsn(htxt, hlen, &str);
     while (str.l && (!str.s[str.l-1] || str.s[str.l-1]=='\n') ) str.l--; // kill trailing zeros and newlines
     kputc('\n',&str);
-	bcf_hdr_parse(h, str.s);
+    bcf_hdr_parse(h, str.s);
     free(str.s);
     free(htxt);
-	return h;
+    return h;
 }
 
 int bcf_hdr_set_samples(bcf_hdr_t *hdr, const char *samples, int is_file)
 {
-    if ( samples && !strcmp("-",samples) ) return 0;            // keep all samples 
+    if ( samples && !strcmp("-",samples) ) return 0;            // keep all samples
 
     hdr->nsamples_ori = bcf_hdr_nsamples(hdr);
     if ( !samples ) { bcf_hdr_nsamples(hdr) = 0; return 0; }    // exclude all samples
@@ -2243,29 +2417,29 @@ int bcf_hdr_set_samples(bcf_hdr_t *hdr, const char *samples, int is_file)
     for (i=0; i<n; i++)
     {
         idx = bcf_hdr_id2int(hdr,BCF_DT_SAMPLE,smpls[i]);
-        if ( idx<0 ) 
-        { 
+        if ( idx<0 )
+        {
             if ( !ret ) ret = i+1;
-            continue; 
+            continue;
         }
         assert( idx<bcf_hdr_nsamples(hdr) );
-        if (  samples[0]=='^' ) 
+        if (  samples[0]=='^' )
             bit_array_clear(hdr->keep_samples, idx);
-        else 
+        else
             bit_array_set(hdr->keep_samples, idx);
     }
     for (i=0; i<n; i++) free(smpls[i]);
     free(smpls);
 
     bcf_hdr_nsamples(hdr) = 0;
-    for (i=0; i<hdr->nsamples_ori; i++) 
+    for (i=0; i<hdr->nsamples_ori; i++)
         if ( bit_array_test(hdr->keep_samples,i) ) bcf_hdr_nsamples(hdr)++;
     if ( !bcf_hdr_nsamples(hdr) ) { free(hdr->keep_samples); hdr->keep_samples=NULL; }
     else
     {
         char **samples = (char**) malloc(sizeof(char*)*bcf_hdr_nsamples(hdr));
         idx = 0;
-        for (i=0; i<hdr->nsamples_ori; i++) 
+        for (i=0; i<hdr->nsamples_ori; i++)
             if ( bit_array_test(hdr->keep_samples,i) ) samples[idx++] = strdup(hdr->samples[i]);
         free(hdr->samples);
         hdr->samples = samples;
@@ -2279,7 +2453,7 @@ int bcf_hdr_set_samples(bcf_hdr_t *hdr, const char *samples, int is_file)
 
         // add the subset back
         hdr->dict[BCF_DT_SAMPLE] = d = kh_init(vdict);
-        for (i=0; i<bcf_hdr_nsamples(hdr); i++) 
+        for (i=0; i<bcf_hdr_nsamples(hdr); i++)
         {
             int ignore, k = kh_put(vdict, d, hdr->samples[i], &ignore);
             kh_val(d, k) = bcf_idinfo_def;
@@ -2293,110 +2467,124 @@ int bcf_hdr_set_samples(bcf_hdr_t *hdr, const char *samples, int is_file)
 
 int bcf_subset(const bcf_hdr_t *h, bcf1_t *v, int n, int *imap)
 {
-	kstring_t ind;
-	ind.s = 0; ind.l = ind.m = 0;
-	if (n) {
-		bcf_fmt_t *fmt;
-		int i, j;
-		fmt = (bcf_fmt_t*)alloca(v->n_fmt * sizeof(bcf_fmt_t));
+    kstring_t ind;
+    ind.s = 0; ind.l = ind.m = 0;
+    if (n) {
+        bcf_fmt_t *fmt;
+        int i, j;
+        fmt = (bcf_fmt_t*)alloca(v->n_fmt * sizeof(bcf_fmt_t));
         uint8_t *ptr = (uint8_t*)v->indiv.s;
         for (i = 0; i < v->n_fmt; ++i)
             ptr = bcf_unpack_fmt_core1(ptr, v->n_sample, &fmt[i]);
-		for (i = 0; i < (int)v->n_fmt; ++i) {
-			bcf_fmt_t *f = &fmt[i];
-			bcf_enc_int1(&ind, f->id);
-			bcf_enc_size(&ind, f->n, f->type);
-			for (j = 0; j < n; ++j)
-				if (imap[j] >= 0) kputsn((char*)(f->p + imap[j] * f->size), f->size, &ind);
-		}
-		for (i = j = 0; j < n; ++j) if (imap[j] >= 0) ++i;
-		v->n_sample = i;
-	} else v->n_sample = 0;
+        for (i = 0; i < (int)v->n_fmt; ++i) {
+            bcf_fmt_t *f = &fmt[i];
+            bcf_enc_int1(&ind, f->id);
+            bcf_enc_size(&ind, f->n, f->type);
+            for (j = 0; j < n; ++j)
+                if (imap[j] >= 0) kputsn((char*)(f->p + imap[j] * f->size), f->size, &ind);
+        }
+        for (i = j = 0; j < n; ++j) if (imap[j] >= 0) ++i;
+        v->n_sample = i;
+    } else v->n_sample = 0;
     if ( !v->n_sample ) v->n_fmt = 0;
-	free(v->indiv.s);
-	v->indiv = ind;
+    free(v->indiv.s);
+    v->indiv = ind;
     v->unpacked &= ~BCF_UN_FMT;    // only BCF is ready for output, VCF will need to unpack again
-	return 0;
+    return 0;
 }
 
 int bcf_is_snp(bcf1_t *v)
 {
-	int i;
-	bcf_unpack(v, BCF_UN_STR);
-	for (i = 0; i < v->n_allele; ++i)
-		if (strlen(v->d.allele[i]) != 1) break;
-	return i == v->n_allele;
+    int i;
+    bcf_unpack(v, BCF_UN_STR);
+    for (i = 0; i < v->n_allele; ++i)
+    {
+        if ( v->d.allele[i][1]==0 ) continue;
+
+        // mpileup's <X> allele, see also below. This is not completely satisfactory,
+        // a general library is here narrowly tailored to fit samtools.
+        if ( v->d.allele[i][0]=='<' && v->d.allele[i][1]=='X' && v->d.allele[i][2]=='>' ) continue;
+
+        break;
+    }
+    return i == v->n_allele;
 }
 
 static void bcf_set_variant_type(const char *ref, const char *alt, variant_t *var)
 {
-	// The most frequent case
-	if ( !ref[1] && !alt[1] )
-	{
-		if ( *alt == '.' || *ref==*alt ) { var->n = 0; var->type = VCF_REF; return; }
+    // The most frequent case
+    if ( !ref[1] && !alt[1] )
+    {
+        if ( *alt == '.' || *ref==*alt ) { var->n = 0; var->type = VCF_REF; return; }
         if ( *alt == 'X' ) { var->n = 0; var->type = VCF_REF; return; }  // mpileup's X allele shouldn't be treated as variant
-		var->n = 1; var->type = VCF_SNP; return;
-	}
-
-	const char *r = ref, *a = alt;
-	while (*r && *a && *r==*a ) { r++; a++; }
-
-	if ( *a && !*r )
-	{
-		while ( *a ) a++;
-		var->n = (a-alt)-(r-ref); var->type = VCF_INDEL; return;
-	}
-	else if ( *r && !*a )
-	{
-		while ( *r ) r++;
-		var->n = (a-alt)-(r-ref); var->type = VCF_INDEL; return;
-	}
-	else if ( !*r && !*a )
-	{
-		var->n = 0; var->type = VCF_REF; return;
-	}
+        var->n = 1; var->type = VCF_SNP; return;
+    }
+    if ( alt[0]=='<' )
+    {
+        if ( alt[1]=='X' && alt[2]=='>' ) { var->n = 0; var->type = VCF_REF; return; }  // mpileup's X allele shouldn't be treated as variant
+        var->type = VCF_OTHER;
+        return;
+    }
+
+    const char *r = ref, *a = alt;
+    while (*r && *a && *r==*a ) { r++; a++; }
+
+    if ( *a && !*r )
+    {
+        while ( *a ) a++;
+        var->n = (a-alt)-(r-ref); var->type = VCF_INDEL; return;
+    }
+    else if ( *r && !*a )
+    {
+        while ( *r ) r++;
+        var->n = (a-alt)-(r-ref); var->type = VCF_INDEL; return;
+    }
+    else if ( !*r && !*a )
+    {
+        var->n = 0; var->type = VCF_REF; return;
+    }
 
     const char *re = r, *ae = a;
     while ( re[1] ) re++;
     while ( ae[1] ) ae++;
     while ( *re==*ae && re>r && ae>a ) { re--; ae--; }
-    if ( ae==a ) 
-    { 
+    if ( ae==a )
+    {
         if ( re==r ) { var->n = 1; var->type = VCF_SNP; return; }
         var->n = -(re-r);
         if ( *re==*ae ) { var->type = VCF_INDEL; return; }
         var->type = VCF_OTHER; return;
     }
-    else if ( re==r ) 
-    { 
+    else if ( re==r )
+    {
         var->n = ae-a;
         if ( *re==*ae ) { var->type = VCF_INDEL; return; }
         var->type = VCF_OTHER; return;
     }
 
-	var->type = ( re-r == ae-a ) ? VCF_MNP : VCF_OTHER;
+    var->type = ( re-r == ae-a ) ? VCF_MNP : VCF_OTHER;
     var->n = ( re-r > ae-a ) ? -(re-r+1) : ae-a+1;
 
-	// should do also complex events, SVs, etc...
+    // should do also complex events, SVs, etc...
 }
 
 static void bcf_set_variant_types(bcf1_t *b)
 {
     if ( !(b->unpacked & BCF_UN_STR) ) bcf_unpack(b, BCF_UN_STR);
-	bcf_dec_t *d = &b->d;
-	if ( d->n_var < b->n_allele ) 
-	{
-		d->var = (variant_t *) realloc(d->var, sizeof(variant_t)*b->n_allele);
-		d->n_var = b->n_allele;
-	}
-	int i;
-	b->d.var_type = 0;
-	for (i=1; i<b->n_allele; i++)
-	{
-		bcf_set_variant_type(d->allele[0],d->allele[i], &d->var[i]);
-		b->d.var_type |= d->var[i].type;
-		//fprintf(stderr,"[set_variant_type] %d   %s %s -> %d %d .. %d\n", b->pos+1,d->allele[0],d->allele[i],d->var[i].type,d->var[i].n, b->d.var_type);
-	}
+    bcf_dec_t *d = &b->d;
+    if ( d->n_var < b->n_allele )
+    {
+        d->var = (variant_t *) realloc(d->var, sizeof(variant_t)*b->n_allele);
+        d->n_var = b->n_allele;
+    }
+    int i;
+    b->d.var_type = 0;
+    for (i=1; i<b->n_allele; i++)
+    {
+        bcf_set_variant_type(d->allele[0],d->allele[i], &d->var[i]);
+        b->d.var_type |= d->var[i].type;
+        //fprintf(stderr,"[set_variant_type] %d   %s %s -> %d %d .. %d\n", b->pos+1,d->allele[0],d->allele[i],d->var[i].type,d->var[i].n, b->d.var_type);
+    }
 }
 
 int bcf_get_variant_types(bcf1_t *rec)
@@ -2436,7 +2624,7 @@ int bcf_update_info(const bcf_hdr_t *hdr, bcf1_t *line, const char *key, const v
         }
         return 0;
     }
-    
+
     // Encode the values and determine the size required to accommodate the values
     kstring_t str = {0,0,0};
     bcf_enc_int1(&str, inf_id);
@@ -2473,7 +2661,7 @@ int bcf_update_info(const bcf_hdr_t *hdr, bcf1_t *line, const char *key, const v
         }
         else
         {
-            assert( !inf->vptr_free );  // fix the caller or improve here: this has been modified before 
+            assert( !inf->vptr_free );  // fix the caller or improve here: this has been modified before
             bcf_unpack_info_core1((uint8_t*)str.s, inf);
             inf->vptr_free = 1;
             line->d.shared_dirty |= BCF1_DIRTY_INF;
@@ -2495,7 +2683,7 @@ int bcf_update_info(const bcf_hdr_t *hdr, bcf1_t *line, const char *key, const v
 
 int bcf_update_format_string(const bcf_hdr_t *hdr, bcf1_t *line, const char *key, const char **values, int n)
 {
-    if ( !n ) 
+    if ( !n )
         return bcf_update_format(hdr,line,key,NULL,0,BCF_HT_STR);
 
     int i, max_len = 0;
@@ -2611,7 +2799,7 @@ int bcf_update_format(const bcf_hdr_t *hdr, bcf1_t *line, const char *key, const
         }
         else
         {
-            assert( !fmt->p_free );  // fix the caller or improve here: this has been modified before 
+            assert( !fmt->p_free );  // fix the caller or improve here: this has been modified before
             bcf_unpack_fmt_core1((uint8_t*)str.s, line->n_sample, fmt);
             fmt->p_free = 1;
             line->d.indiv_dirty = 1;
@@ -2709,7 +2897,7 @@ int bcf_update_alleles(const bcf_hdr_t *hdr, bcf1_t *line, const char **alleles,
     int i;
     for (i=0; i<nals; i++)
         if ( alleles[i]>=line->d.als && alleles[i]<line->d.als+line->d.m_als ) break;
-    if ( i==nals ) 
+    if ( i==nals )
     {
         // all alleles point elsewhere, reuse the existing block
         tmp.l = 0; tmp.s = line->d.als; tmp.m = line->d.m_als;
@@ -2759,28 +2947,41 @@ int bcf_update_id(const bcf_hdr_t *hdr, bcf1_t *line, const char *id)
 
 bcf_fmt_t *bcf_get_fmt(const bcf_hdr_t *hdr, bcf1_t *line, const char *key)
 {
-    int i, id = bcf_hdr_id2int(hdr, BCF_DT_ID, key);
+    int id = bcf_hdr_id2int(hdr, BCF_DT_ID, key);
     if ( !bcf_hdr_idinfo_exists(hdr,BCF_HL_FMT,id) ) return NULL;   // no such FMT field in the header
+    return bcf_get_fmt_id(line, id);
+}
+
+bcf_info_t *bcf_get_info(const bcf_hdr_t *hdr, bcf1_t *line, const char *key)
+{
+    int id = bcf_hdr_id2int(hdr, BCF_DT_ID, key);
+    if ( !bcf_hdr_idinfo_exists(hdr,BCF_HL_INFO,id) ) return NULL;   // no such INFO field in the header
+    return bcf_get_info_id(line, id);
+}
+
+bcf_fmt_t *bcf_get_fmt_id(bcf1_t *line, const int id) 
+{
+    int i;
     if ( !(line->unpacked & BCF_UN_FMT) ) bcf_unpack(line, BCF_UN_FMT);
-    for (i=0; i<line->n_fmt; i++)  
+    for (i=0; i<line->n_fmt; i++)
     {
         if ( line->d.fmt[i].id==id ) return &line->d.fmt[i];
     }
     return NULL;
 }
 
-bcf_info_t *bcf_get_info(const bcf_hdr_t *hdr, bcf1_t *line, const char *key)
+bcf_info_t *bcf_get_info_id(bcf1_t *line, const int id) 
 {
-    int i, id = bcf_hdr_id2int(hdr, BCF_DT_ID, key);
-    if ( !bcf_hdr_idinfo_exists(hdr,BCF_HL_INFO,id) ) return NULL;   // no such INFO field in the header
+    int i;
     if ( !(line->unpacked & BCF_UN_INFO) ) bcf_unpack(line, BCF_UN_INFO);
-    for (i=0; i<line->n_info; i++)  
+    for (i=0; i<line->n_info; i++)
     {
         if ( line->d.info[i].key==id ) return &line->d.info[i];
     }
     return NULL;
 }
 
+
 int bcf_get_info_values(const bcf_hdr_t *hdr, bcf1_t *line, const char *tag, void **dst, int *ndst, int type)
 {
     int i,j, tag_id = bcf_hdr_id2int(hdr, BCF_DT_ID, tag);
@@ -2797,7 +2998,7 @@ int bcf_get_info_values(const bcf_hdr_t *hdr, bcf1_t *line, const char *tag, voi
     bcf_info_t *info = &line->d.info[i];
     if ( type==BCF_HT_STR )
     {
-        if ( *ndst < info->len+1 ) 
+        if ( *ndst < info->len+1 )
         {
             *ndst = info->len + 1;
             *dst  = realloc(*dst, *ndst);
@@ -2818,7 +3019,20 @@ int bcf_get_info_values(const bcf_hdr_t *hdr, bcf1_t *line, const char *tag, voi
     if ( info->len == 1 )
     {
         if ( info->type==BCF_BT_FLOAT ) *((float*)*dst) = info->v1.f;
-        else *((int32_t*)*dst) = info->v1.i;
+        else 
+        {
+            #define BRANCH(type_t, missing) { \
+                if ( info->v1.i==missing ) *((int32_t*)*dst) = bcf_int32_missing; \
+                else *((int32_t*)*dst) = info->v1.i; \
+            }
+            switch (info->type)
+            {
+                case BCF_BT_INT8:  BRANCH(int8_t,  bcf_int8_missing ); break;
+                case BCF_BT_INT16: BRANCH(int16_t, bcf_int16_missing); break;
+                case BCF_BT_INT32: BRANCH(int32_t, bcf_int32_missing); break;
+            }
+            #undef BRANCH
+        }
         return 1;
     }
 
@@ -2868,7 +3082,7 @@ int bcf_get_format_string(const bcf_hdr_t *hdr, bcf1_t *line, const char *tag, c
     int n = (fmt->n+1)*nsmpl;
     if ( *ndst < n )
     {
-        (*dst)[0] = realloc((*dst)[0], n); 
+        (*dst)[0] = realloc((*dst)[0], n);
         if ( !(*dst)[0] ) return -4;    // could not alloc
         *ndst = n;
     }
@@ -2904,7 +3118,7 @@ int bcf_get_format_values(const bcf_hdr_t *hdr, bcf1_t *line, const char *tag, v
     if ( type==BCF_HT_STR )
     {
         int n = fmt->n*bcf_hdr_nsamples(hdr);
-        if ( *ndst < n ) 
+        if ( *ndst < n )
         {
             *dst  = realloc(*dst, n);
             if ( !*dst ) return -4;     // could not alloc
@@ -2924,30 +3138,30 @@ int bcf_get_format_values(const bcf_hdr_t *hdr, bcf1_t *line, const char *tag, v
         if ( !dst ) return -4;     // could not alloc
     }
 
-    #define BRANCH(type_t, is_missing, is_vector_end, set_missing, set_vector_end, out_type_t) { \
-        out_type_t *tmp = (out_type_t *) *dst; \
-        type_t *p = (type_t*) fmt->p; \
-        for (i=0; i<nsmpl; i++) \
+#define BRANCH(type_t, is_missing, is_vector_end, set_missing, set_vector_end, out_type_t) { \
+    out_type_t *tmp = (out_type_t *) *dst; \
+    type_t *p = (type_t*) fmt->p; \
+    for (i=0; i<nsmpl; i++) \
+    { \
+        for (j=0; j<fmt->n; j++) \
         { \
-            for (j=0; j<fmt->n; j++) \
-            { \
-                if ( is_missing ) set_missing; \
-                else if ( is_vector_end ) { set_vector_end; break; } \
-                else *tmp = p[j]; \
-                tmp++; \
-            } \
-            for (; j<fmt->n; j++) { set_vector_end; tmp++; } \
-            p = (type_t *)((char *)p + fmt->size); \
+            if ( is_missing ) set_missing; \
+            else if ( is_vector_end ) { set_vector_end; break; } \
+            else *tmp = p[j]; \
+            tmp++; \
         } \
-    }
-    switch (fmt->type) {
-        case BCF_BT_INT8:  BRANCH(int8_t,  p[j]==bcf_int8_missing,  p[j]==bcf_int8_vector_end,  *tmp=bcf_int32_missing, *tmp=bcf_int32_vector_end, int32_t); break;
-        case BCF_BT_INT16: BRANCH(int16_t, p[j]==bcf_int16_missing, p[j]==bcf_int16_vector_end, *tmp=bcf_int32_missing, *tmp=bcf_int32_vector_end, int32_t); break;
-        case BCF_BT_INT32: BRANCH(int32_t, p[j]==bcf_int32_missing, p[j]==bcf_int32_vector_end, *tmp=bcf_int32_missing, *tmp=bcf_int32_vector_end, int32_t); break;
-        case BCF_BT_FLOAT: BRANCH(float,   bcf_float_is_missing(p[j]), bcf_float_is_vector_end(p[j]), bcf_float_set_missing(*tmp), bcf_float_set_vector_end(*tmp), float); break;
-        default: fprintf(stderr,"TODO: %s:%d .. fmt->type=%d\n", __FILE__,__LINE__, fmt->type); exit(1);
-    }
-    #undef BRANCH
-    return nsmpl*fmt->n;
+        for (; j<fmt->n; j++) { set_vector_end; tmp++; } \
+        p = (type_t *)((char *)p + fmt->size); \
+    } \
+}
+switch (fmt->type) {
+    case BCF_BT_INT8:  BRANCH(int8_t,  p[j]==bcf_int8_missing,  p[j]==bcf_int8_vector_end,  *tmp=bcf_int32_missing, *tmp=bcf_int32_vector_end, int32_t); break;
+    case BCF_BT_INT16: BRANCH(int16_t, p[j]==bcf_int16_missing, p[j]==bcf_int16_vector_end, *tmp=bcf_int32_missing, *tmp=bcf_int32_vector_end, int32_t); break;
+    case BCF_BT_INT32: BRANCH(int32_t, p[j]==bcf_int32_missing, p[j]==bcf_int32_vector_end, *tmp=bcf_int32_missing, *tmp=bcf_int32_vector_end, int32_t); break;
+    case BCF_BT_FLOAT: BRANCH(float,   bcf_float_is_missing(p[j]), bcf_float_is_vector_end(p[j]), bcf_float_set_missing(*tmp), bcf_float_set_vector_end(*tmp), float); break;
+    default: fprintf(stderr,"TODO: %s:%d .. fmt->type=%d\n", __FILE__,__LINE__, fmt->type); exit(1);
+}
+#undef BRANCH
+return nsmpl*fmt->n;
 }
 
diff --git a/htslib/vcf_sweep.c b/htslib/vcf_sweep.c
index 2598460..27e26a8 100644
--- a/htslib/vcf_sweep.c
+++ b/htslib/vcf_sweep.c
@@ -1,3 +1,27 @@
+/*  vcf_sweep.c -- forward/reverse sweep API.
+
+    Copyright (C) 2013 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include "htslib/vcf_sweep.h"
 #include "htslib/bgzf.h"
 
diff --git a/htslib/vcfutils.c b/htslib/vcfutils.c
index a4a9c2f..3f64836 100644
--- a/htslib/vcfutils.c
+++ b/htslib/vcfutils.c
@@ -1,30 +1,54 @@
+/*  vcfutils.c -- allele-related utility functions.
+
+    Copyright (C) 2012-2014 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include "htslib/vcfutils.h"
 
 int bcf_calc_ac(const bcf_hdr_t *header, bcf1_t *line, int *ac, int which)
 {
-	int i;
-	for (i=0; i<line->n_allele; i++) ac[i]=0;
-
-	// Use INFO/AC,AN field only when asked
-	if ( which&BCF_UN_INFO )
-	{
-		bcf_unpack(line, BCF_UN_INFO);
-		int an_id = bcf_hdr_id2int(header, BCF_DT_ID, "AN");
-		int ac_id = bcf_hdr_id2int(header, BCF_DT_ID, "AC");
+    int i;
+    for (i=0; i<line->n_allele; i++) ac[i]=0;
+
+    // Use INFO/AC,AN field only when asked
+    if ( which&BCF_UN_INFO )
+    {
+        bcf_unpack(line, BCF_UN_INFO);
+        int an_id = bcf_hdr_id2int(header, BCF_DT_ID, "AN");
+        int ac_id = bcf_hdr_id2int(header, BCF_DT_ID, "AC");
         int i, an=-1, ac_len=0, ac_type=0;
         uint8_t *ac_ptr=NULL;
-		if ( an_id>=0 && ac_id>=0 )
-		{
-			for (i=0; i<line->n_info; i++)
-			{
-				bcf_info_t *z = &line->d.info[i];
-				if ( z->key == an_id ) an = z->v1.i;
-				else if ( z->key == ac_id ) { ac_ptr = z->vptr; ac_len = z->len; ac_type = z->type; }
-			}
+        if ( an_id>=0 && ac_id>=0 )
+        {
+            for (i=0; i<line->n_info; i++)
+            {
+                bcf_info_t *z = &line->d.info[i];
+                if ( z->key == an_id ) an = z->v1.i;
+                else if ( z->key == ac_id ) { ac_ptr = z->vptr; ac_len = z->len; ac_type = z->type; }
+            }
         }
         if ( an>=0 && ac_ptr )
         {
-			int nac = 0;
+            int nac = 0;
             #define BRANCH_INT(type_t) {        \
                 type_t *p = (type_t *) ac_ptr;  \
                 for (i=0; i<ac_len; i++)        \
@@ -41,33 +65,33 @@ int bcf_calc_ac(const bcf_hdr_t *header, bcf1_t *line, int *ac, int which)
             }
             #undef BRANCH_INT
             assert( an>=nac );  // sanity check for missing values
-			ac[0] = an - nac;
-			return 1;
+            ac[0] = an - nac;
+            return 1;
         }
-	}
-
-	// Split genotype fields only when asked
-	if ( which&BCF_UN_FMT )
-	{
-		int i, gt_id = bcf_hdr_id2int(header,BCF_DT_ID,"GT");
-		if ( gt_id<0 ) return 0;
-		bcf_unpack(line, BCF_UN_FMT);
-		bcf_fmt_t *fmt_gt = NULL;
-		for (i=0; i<(int)line->n_fmt; i++) 
-			if ( line->d.fmt[i].id==gt_id ) { fmt_gt = &line->d.fmt[i]; break; }
-		if ( !fmt_gt ) return 0;
+    }
+
+    // Split genotype fields only when asked
+    if ( which&BCF_UN_FMT )
+    {
+        int i, gt_id = bcf_hdr_id2int(header,BCF_DT_ID,"GT");
+        if ( gt_id<0 ) return 0;
+        bcf_unpack(line, BCF_UN_FMT);
+        bcf_fmt_t *fmt_gt = NULL;
+        for (i=0; i<(int)line->n_fmt; i++)
+            if ( line->d.fmt[i].id==gt_id ) { fmt_gt = &line->d.fmt[i]; break; }
+        if ( !fmt_gt ) return 0;
         #define BRANCH_INT(type_t,missing,vector_end) { \
-		    for (i=0; i<line->n_sample; i++) \
-		    { \
+            for (i=0; i<line->n_sample; i++) \
+            { \
                 type_t *p = (type_t*) (fmt_gt->p + i*fmt_gt->size); \
-		    	int ial; \
-		    	for (ial=0; ial<fmt_gt->n; ial++) \
-		    	{ \
+                int ial; \
+                for (ial=0; ial<fmt_gt->n; ial++) \
+                { \
                     if ( p[ial]==vector_end ) break; /* smaller ploidy */ \
                     if ( !(p[ial]>>1) || p[ial]==missing ) continue; /* missing allele */ \
-		    		ac[(p[ial]>>1)-1]++; \
-		    	} \
-		    } \
+                    ac[(p[ial]>>1)-1]++; \
+                } \
+            } \
         }
         switch (fmt_gt->type) {
             case BCF_BT_INT8:  BRANCH_INT(int8_t,  bcf_int8_missing, bcf_int8_vector_end); break;
@@ -76,9 +100,9 @@ int bcf_calc_ac(const bcf_hdr_t *header, bcf1_t *line, int *ac, int which)
             default: fprintf(stderr, "[E::%s] todo: %d at %s:%d\n", __func__, fmt_gt->type, header->id[BCF_DT_CTG][line->rid].key, line->pos+1); exit(1); break;
         }
         #undef BRANCH_INT
-		return 1;
-	}
-	return 0;
+        return 1;
+    }
+    return 0;
 }
 
 int bcf_gt_type(bcf_fmt_t *fmt_ptr, int isample, int *_ial, int *_jal)
@@ -125,7 +149,7 @@ int bcf_gt_type(bcf_fmt_t *fmt_ptr, int isample, int *_ial, int *_jal)
     if ( !nals ) return GT_UNKN;
     if ( nals==1 )
         return has_ref ? GT_HAPL_R : GT_HAPL_A;
-    if ( !has_ref ) 
+    if ( !has_ref )
         return has_alt==1 ? GT_HOM_AA : GT_HET_AA;
     if ( !has_alt )
         return GT_HOM_RR;
@@ -146,11 +170,11 @@ int bcf_trim_alleles(const bcf_hdr_t *header, bcf1_t *line)
         { \
             type_t *p = (type_t*) (gt->p + i*gt->size); \
             int ial; \
-            for (ial=0; ial<gt->size; ial++) \
+            for (ial=0; ial<gt->n; ial++) \
             { \
                 if ( p[ial]==vector_end ) break; /* smaller ploidy */ \
                 if ( !(p[ial]>>1) || p[ial]==missing ) continue; /* missing allele */ \
-                assert((p[ial]>>1)-1<line->n_allele); \
+                if ( (p[ial]>>1)-1 >= line->n_allele ) return -1; \
                 ac[(p[ial]>>1)-1]++; \
             } \
         } \
@@ -164,7 +188,7 @@ int bcf_trim_alleles(const bcf_hdr_t *header, bcf1_t *line)
     #undef BRANCH
 
     int rm_als = 0, nrm = 0;
-    for (i=1; i<line->n_allele; i++) 
+    for (i=1; i<line->n_allele; i++)
     {
         if ( !ac[i] ) { rm_als |= 1<<i; nrm++; }
     }
@@ -183,7 +207,7 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
     kputs(line->d.allele[0], &str);
 
     int nrm = 0, i,j;  // i: ori alleles, j: new alleles
-    for (i=1, j=1; i<line->n_allele; i++) 
+    for (i=1, j=1; i<line->n_allele; i++)
     {
         if ( rm_mask & 1<<i )
         {
@@ -217,7 +241,7 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
     {
         bcf_info_t *info = &line->d.info[i];
         int vlen = bcf_hdr_id2length(header,BCF_HL_INFO,info->key);
-        
+
         if ( vlen!=BCF_VL_A && vlen!=BCF_VL_G && vlen!=BCF_VL_R ) continue; // no need to change
 
         int type = bcf_hdr_id2type(header,BCF_HL_INFO,info->key);
@@ -228,14 +252,14 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
         mdat = mdat_bytes / size;
         nret = bcf_get_info_values(header, line, bcf_hdr_int2id(header,BCF_DT_ID,info->key), (void**)&dat, &mdat, type);
         mdat_bytes = mdat * size;
-        if ( nret<0 ) 
-        { 
-            fprintf(stderr,"[%s:%d %s] Could not access INFO/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__, 
-                bcf_hdr_int2id(header,BCF_DT_ID,info->key), bcf_seqname(header,line), line->pos+1, nret); 
+        if ( nret<0 )
+        {
+            fprintf(stderr,"[%s:%d %s] Could not access INFO/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__,
+                bcf_hdr_int2id(header,BCF_DT_ID,info->key), bcf_seqname(header,line), line->pos+1, nret);
             exit(1);
         }
-        if ( type==BCF_HT_STR ) 
-        { 
+        if ( type==BCF_HT_STR )
+        {
             str.l = 0;
             char *ss = (char*) dat, *se = (char*) dat;
             if ( vlen==BCF_VL_A || vlen==BCF_VL_R )
@@ -252,11 +276,11 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
                 {
                     if ( !*se ) break;
                     while ( *se && *se!=',' ) se++;
-                    if ( rm_mask & 1<<(j+inc) ) 
-                    { 
+                    if ( rm_mask & 1<<(j+inc) )
+                    {
                         if ( *se ) se++;
-                        ss = se; 
-                        continue; 
+                        ss = se;
+                        continue;
                     }
                     if ( str.l ) kputc(',',&str);
                     kputsn(ss,se-ss,&str);
@@ -275,11 +299,11 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
                         if ( !*se ) break;
                         while ( *se && *se!=',' ) se++;
                         n++;
-                        if ( rm_mask & 1<<j || rm_mask & 1<<k ) 
-                        { 
+                        if ( rm_mask & 1<<j || rm_mask & 1<<k )
+                        {
                             if ( *se ) se++;
-                            ss = se; 
-                            continue; 
+                            ss = se;
+                            continue;
                         }
                         if ( str.l ) kputc(',',&str);
                         kputsn(ss,se-ss,&str);
@@ -298,9 +322,9 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
                         bcf_hdr_int2id(header,BCF_DT_ID,info->key), bcf_seqname(header,line), line->pos+1, nret);
                 exit(1);
             }
-            continue; 
+            continue;
         }
-        
+
         if ( vlen==BCF_VL_A || vlen==BCF_VL_R )
         {
             int inc = 0, ntop;
@@ -331,7 +355,7 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
                     k++; \
                 } \
             }
-            switch (type) 
+            switch (type)
             {
                 case BCF_HT_INT:  BRANCH(int32_t,ptr[j]==bcf_int32_vector_end); break;
                 case BCF_HT_REAL: BRANCH(float,bcf_float_is_vector_end(ptr[j])); break;
@@ -360,7 +384,7 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
                     } \
                 } \
             }
-            switch (type) 
+            switch (type)
             {
                 case BCF_HT_INT:  BRANCH(int32_t,ptr[l_ori]==bcf_int32_vector_end); break;
                 case BCF_HT_REAL: BRANCH(float,bcf_float_is_vector_end(ptr[l_ori])); break;
@@ -404,7 +428,7 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
         }
     }
 
-    // Remove from Number=G, Number=R and Number=A FORMAT fields. 
+    // Remove from Number=G, Number=R and Number=A FORMAT fields.
     // Assuming haploid or diploid GTs
     for (i=0; i<line->n_fmt; i++)
     {
@@ -422,14 +446,14 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
         mdat = mdat_bytes / size;
         nret = bcf_get_format_values(header, line, bcf_hdr_int2id(header,BCF_DT_ID,fmt->id), (void**)&dat, &mdat, type);
         mdat_bytes = mdat * size;
-        if ( nret<0 ) 
-        { 
-            fprintf(stderr,"[%s:%d %s] Could not access FORMAT/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__, 
-                    bcf_hdr_int2id(header,BCF_DT_ID,fmt->id), bcf_seqname(header,line), line->pos+1, nret); 
+        if ( nret<0 )
+        {
+            fprintf(stderr,"[%s:%d %s] Could not access FORMAT/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__,
+                    bcf_hdr_int2id(header,BCF_DT_ID,fmt->id), bcf_seqname(header,line), line->pos+1, nret);
             exit(1);
         }
 
-        if ( type==BCF_HT_STR ) 
+        if ( type==BCF_HT_STR )
         {
             int size = nret/line->n_sample;     // number of bytes per sample
             str.l = 0;
@@ -571,7 +595,7 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
                     } \
                 } \
             }
-            switch (type) 
+            switch (type)
             {
                 case BCF_HT_INT:  BRANCH(int32_t,ptr_src[k_src]==bcf_int32_vector_end); break;
                 case BCF_HT_REAL: BRANCH(float,bcf_float_is_vector_end(ptr_src[k_src])); break;
@@ -620,7 +644,7 @@ void bcf_remove_alleles(const bcf_hdr_t *header, bcf1_t *line, int rm_mask)
                     } \
                 } \
             }
-            switch (type) 
+            switch (type)
             {
                 case BCF_HT_INT:  BRANCH(int32_t,ptr_src[k_src]==bcf_int32_vector_end); break;
                 case BCF_HT_REAL: BRANCH(float,bcf_float_is_vector_end(ptr_src[k_src])); break;
diff --git a/htslib/version.h b/htslib/version.h
index f72d827..6a43f98 100644
--- a/htslib/version.h
+++ b/htslib/version.h
@@ -1 +1 @@
-#define HTS_VERSION "0.0.1"
\ No newline at end of file
+#define HTS_VERSION "1.1"
diff --git a/pysam.egg-info/PKG-INFO b/pysam.egg-info/PKG-INFO
index 41920ad..354479b 100644
--- a/pysam.egg-info/PKG-INFO
+++ b/pysam.egg-info/PKG-INFO
@@ -1,8 +1,8 @@
 Metadata-Version: 1.1
 Name: pysam
-Version: 0.8.0
+Version: 0.8.1
 Summary: pysam
-Home-page: http://code.google.com/p/pysam/
+Home-page: https://github.com/pysam-developers/pysam
 Author: Andreas Heger
 Author-email: andreas.heger at gmail.com
 License: MIT
diff --git a/pysam.egg-info/SOURCES.txt b/pysam.egg-info/SOURCES.txt
index 214947c..ee169f1 100644
--- a/pysam.egg-info/SOURCES.txt
+++ b/pysam.egg-info/SOURCES.txt
@@ -15,9 +15,9 @@ doc/faq.rst
 doc/glossary.rst
 doc/index.rst
 doc/make.bat
+doc/out
 doc/release.rst
 doc/usage.rst
-htslib/__init__.py
 htslib/bgzf.c
 htslib/bgzip.c
 htslib/config.h
@@ -74,7 +74,6 @@ htslib/cram/vlen.c
 htslib/cram/vlen.h
 htslib/cram/zfio.c
 htslib/cram/zfio.h
-htslib/htslib/__init__.py
 htslib/htslib/bgzf.h
 htslib/htslib/faidx.h
 htslib/htslib/hfile.h
@@ -87,7 +86,6 @@ htslib/htslib/klist.h
 htslib/htslib/knetfile.h
 htslib/htslib/kseq.h
 htslib/htslib/ksort.h
-htslib/htslib/kstdint.h
 htslib/htslib/kstring.h
 htslib/htslib/sam.h
 htslib/htslib/synced_bcf_reader.h
@@ -100,6 +98,9 @@ pysam/TabProxies.c
 pysam/TabProxies.pxd
 pysam/TabProxies.pyx
 pysam/__init__.py
+pysam/calignmentfile.c
+pysam/calignmentfile.pxd
+pysam/calignmentfile.pyx
 pysam/cfaidx.c
 pysam/cfaidx.pxd
 pysam/cfaidx.pyx
@@ -133,10 +134,8 @@ pysam.egg-info/PKG-INFO
 pysam.egg-info/SOURCES.txt
 pysam.egg-info/dependency_links.txt
 pysam.egg-info/not-zip-safe
-pysam.egg-info/requires.txt
 pysam.egg-info/top_level.txt
 pysam/include/__init__.py
-samtools/__init__.py
 samtools/bam.c.pysam.c
 samtools/bam.h
 samtools/bam2bcf.c.pysam.c
@@ -147,17 +146,21 @@ samtools/bam_aux.c.pysam.c
 samtools/bam_cat.c.pysam.c
 samtools/bam_color.c.pysam.c
 samtools/bam_endian.h
+samtools/bam_flags.c.pysam.c
 samtools/bam_import.c.pysam.c
 samtools/bam_index.c.pysam.c
 samtools/bam_lpileup.c.pysam.c
+samtools/bam_lpileup.h
 samtools/bam_mate.c.pysam.c
 samtools/bam_md.c.pysam.c
-samtools/bam_pileup.c.pysam.c
+samtools/bam_plbuf.c.pysam.c
+samtools/bam_plbuf.h
 samtools/bam_plcmd.c.pysam.c
 samtools/bam_reheader.c.pysam.c
 samtools/bam_rmdup.c.pysam.c
 samtools/bam_rmdupse.c.pysam.c
 samtools/bam_sort.c.pysam.c
+samtools/bam_split.c.pysam.c
 samtools/bam_stat.c.pysam.c
 samtools/bam_tview.c.pysam.c
 samtools/bam_tview.h
@@ -166,31 +169,18 @@ samtools/bam_tview_html.c.pysam.c
 samtools/bamshuf.c.pysam.c
 samtools/bedcov.c.pysam.c
 samtools/bedidx.c.pysam.c
-samtools/bgzf.c.pysam.c
-samtools/bgzf.h
 samtools/cut_target.c.pysam.c
 samtools/errmod.c.pysam.c
 samtools/errmod.h
 samtools/faidx.c.pysam.c
-samtools/faidx.h
 samtools/kaln.c.pysam.c
 samtools/kaln.h
-samtools/khash.h
-samtools/klist.h
-samtools/knetfile.c.pysam.c
-samtools/knetfile.h
 samtools/kprobaln.c
 samtools/kprobaln.c.pysam.c
 samtools/kprobaln.h
-samtools/kseq.h
-samtools/ksort.h
-samtools/kstring.c.pysam.c
-samtools/kstring.h
 samtools/padding.c.pysam.c
 samtools/phase.c.pysam.c
 samtools/pysam.h
-samtools/razf.c.pysam.c
-samtools/razf.h
 samtools/sam.c.pysam.c
 samtools/sam.h
 samtools/sam_header.c.pysam.c
@@ -198,30 +188,17 @@ samtools/sam_header.h
 samtools/sam_view.c.pysam.c
 samtools/sample.c.pysam.c
 samtools/sample.h
-samtools/bcftools/__init__.py
-samtools/bcftools/bcf.c.pysam.c
-samtools/bcftools/bcf.h
-samtools/bcftools/bcf2qcall.c.pysam.c
-samtools/bcftools/bcfutils.c.pysam.c
-samtools/bcftools/call1.c.pysam.c
-samtools/bcftools/em.c.pysam.c
-samtools/bcftools/fet.c.pysam.c
-samtools/bcftools/index.c.pysam.c
-samtools/bcftools/kfunc.c.pysam.c
-samtools/bcftools/kmin.c.pysam.c
-samtools/bcftools/kmin.h
-samtools/bcftools/mut.c.pysam.c
-samtools/bcftools/prob1.c.pysam.c
-samtools/bcftools/prob1.h
-samtools/bcftools/vcf.c.pysam.c
+samtools/samtools.h
+samtools/stats.c.pysam.c
+samtools/stats_isize.c.pysam.c
+samtools/stats_isize.h
+samtools/version.h
 samtools/misc/ace2sam.c.pysam.c
 samtools/misc/md5.c.pysam.c
 samtools/misc/md5.h
-samtools/win32/__init__.py
+samtools/test/test.c.pysam.c
+samtools/test/test.h
 samtools/win32/xcurses.h
 samtools/win32/zconf.h
 samtools/win32/zlib.h
-tests/00README.txt
-tests/example.gtf.gz.tbi
-tests/pysam_test.py
-tests/tabix_test.py
\ No newline at end of file
+tests/00README.txt
\ No newline at end of file
diff --git a/pysam.egg-info/requires.txt b/pysam.egg-info/requires.txt
deleted file mode 100644
index b8144e4..0000000
--- a/pysam.egg-info/requires.txt
+++ /dev/null
@@ -1 +0,0 @@
-cython>=0.20.1
\ No newline at end of file
diff --git a/pysam.py b/pysam.py
index a3d9f6f..0823abd 100644
--- a/pysam.py
+++ b/pysam.py
@@ -1 +1 @@
-raise ImportError( '''calling "import pysam" from the source directory is not supported - please import pysam from somewhere else.''')
+raise ImportError('''calling "import pysam" from the source directory is not supported - please import pysam from somewhere else.''')
diff --git a/pysam/TabProxies.c b/pysam/TabProxies.c
index ccbae13..06ef6b0 100644
--- a/pysam/TabProxies.c
+++ b/pysam/TabProxies.c
@@ -1,4 +1,4 @@
-/* Generated by Cython 0.20.1 on Tue Jul 15 21:20:07 2014 */
+/* Generated by Cython 0.20.1 on Fri Nov 21 19:49:04 2014 */
 
 #define PY_SSIZE_T_CLEAN
 #ifndef CYTHON_USE_PYLONG_INTERNALS
@@ -542,6 +542,45 @@ struct __pyx_obj_5pysam_10TabProxies_GTFProxy;
 struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy;
 struct __pyx_obj_5pysam_10TabProxies_BedProxy;
 struct __pyx_obj_5pysam_10TabProxies_VCFProxy;
+struct __pyx_opt_args_5pysam_10TabProxies__force_bytes;
+struct __pyx_opt_args_5pysam_10TabProxies__charptr_to_str;
+struct __pyx_opt_args_5pysam_10TabProxies__force_str;
+
+/* "pysam/TabProxies.pyx":26
+ *     _FILENAME_ENCODING = 'ascii'
+ * 
+ * cdef bytes _force_bytes(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *     u"""convert string or unicode object to bytes, assuming ascii encoding.
+ *     """
+ */
+struct __pyx_opt_args_5pysam_10TabProxies__force_bytes {
+  int __pyx_n;
+  PyObject *encoding;
+};
+
+/* "pysam/TabProxies.pyx":43
+ *     return _force_bytes(s)
+ * 
+ * cdef _charptr_to_str(char* s, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *     if PY_MAJOR_VERSION < 3:
+ *         return s
+ */
+struct __pyx_opt_args_5pysam_10TabProxies__charptr_to_str {
+  int __pyx_n;
+  PyObject *encoding;
+};
+
+/* "pysam/TabProxies.pyx":49
+ *         return s.decode(encoding)
+ * 
+ * cdef inline _force_str(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *     """Return s converted to str type of current Python "
+ *     "(bytes in Py2, unicode in Py3)"""
+ */
+struct __pyx_opt_args_5pysam_10TabProxies__force_str {
+  int __pyx_n;
+  PyObject *encoding;
+};
 
 /* "pysam/TabProxies.pxd":41
  *   ctypedef int uint64_t
@@ -560,13 +599,14 @@ struct __pyx_obj_5pysam_10TabProxies_TupleProxy {
   int nbytes;
   int offset;
   int is_modified;
+  PyObject *encoding;
 };
 
 
-/* "pysam/TabProxies.pxd":60
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "pysam/TabProxies.pxd":63
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class GTFProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class GTFProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -577,10 +617,10 @@ struct __pyx_obj_5pysam_10TabProxies_GTFProxy {
 };
 
 
-/* "pysam/TabProxies.pxd":69
+/* "pysam/TabProxies.pxd":73
  *     cdef char * getAttributes( self )
  * 
- * cdef class NamedTupleProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class NamedTupleProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  *     pass
  * 
  */
@@ -589,10 +629,10 @@ struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy {
 };
 
 
-/* "pysam/TabProxies.pxd":72
+/* "pysam/TabProxies.pxd":76
  *     pass
  * 
- * cdef class BedProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class BedProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -605,10 +645,10 @@ struct __pyx_obj_5pysam_10TabProxies_BedProxy {
 };
 
 
-/* "pysam/TabProxies.pxd":83
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "pysam/TabProxies.pxd":88
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class VCFProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class VCFProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -620,8 +660,8 @@ struct __pyx_obj_5pysam_10TabProxies_VCFProxy {
 
 
 
-/* "pysam/TabProxies.pyx":86
- *      return not (buffer <= p < buffer + nbytes )
+/* "pysam/TabProxies.pyx":81
+ *      return not (buffer <= p < buffer + nbytes)
  * 
  * cdef class TupleProxy:             # <<<<<<<<<<<<<<
  *     '''Proxy class for access to parsed row as a tuple.
@@ -629,7 +669,8 @@ struct __pyx_obj_5pysam_10TabProxies_VCFProxy {
  */
 
 struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy {
-  int (*getMaxFields)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, size_t);
+  int (*getMaxFields)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *);
+  int (*getMinFields)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *);
   PyObject *(*take)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t);
   PyObject *(*present)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t);
   PyObject *(*copy)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t);
@@ -638,10 +679,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *__pyx_vtabptr_5pysam_10TabProxies_TupleProxy;
 
 
-/* "pysam/TabProxies.pyx":330
- *         return str(v)
+/* "pysam/TabProxies.pyx":368
  * 
- * cdef class GTFProxy( TupleProxy ):             # <<<<<<<<<<<<<<
+ * 
+ * cdef class GTFProxy(TupleProxy):             # <<<<<<<<<<<<<<
  *     '''Proxy class for access to GTF fields.
  * 
  */
@@ -653,10 +694,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy *__pyx_vtabptr_5pysam_10TabProxies_GTFProxy;
 
 
-/* "pysam/TabProxies.pyx":580
- *         self.fromDict( r )
+/* "pysam/TabProxies.pyx":630
+ * 
  * 
- * cdef class NamedTupleProxy( TupleProxy ):             # <<<<<<<<<<<<<<
+ * cdef class NamedTupleProxy(TupleProxy):             # <<<<<<<<<<<<<<
  * 
  *     map_key2field = {}
  */
@@ -667,10 +708,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy *__pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy;
 
 
-/* "pysam/TabProxies.pyx":600
+/* "pysam/TabProxies.pyx":653
  * 
  * 
- * cdef class BedProxy( NamedTupleProxy ):             # <<<<<<<<<<<<<<
+ * cdef class BedProxy(NamedTupleProxy):             # <<<<<<<<<<<<<<
  *     '''Proxy class for access to Bed fields.
  * 
  */
@@ -681,10 +722,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy *__pyx_vtabptr_5pysam_10TabProxies_BedProxy;
 
 
-/* "pysam/TabProxies.pyx":666
+/* "pysam/TabProxies.pyx":724
  *         TupleProxy._setindex(self, idx, str(value) )
  * 
- * cdef class VCFProxy( NamedTupleProxy ):             # <<<<<<<<<<<<<<
+ * cdef class VCFProxy(NamedTupleProxy):             # <<<<<<<<<<<<<<
  *     '''Proxy class for access to VCF fields.
  * 
  */
@@ -794,11 +835,15 @@ static void __Pyx_WriteUnraisable(const char *name, int clineno,
                                   int lineno, const char *filename,
                                   int full_traceback); /*proto*/
 
+static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/
+
+static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \
+    PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \
+    const char* function_name); /*proto*/
+
 static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
     Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/
 
-static CYTHON_INLINE int __Pyx_CheckKeywordStrings(PyObject *kwdict, const char* function_name, int kw_allowed); /*proto*/
-
 static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);
 
 static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);
@@ -823,12 +868,6 @@ static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {
 #define __Pyx_PyList_Append(L,x) PyList_Append(L,x)
 #endif
 
-static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/
-
-static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \
-    PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \
-    const char* function_name); /*proto*/
-
 static PyObject* __Pyx_PyObject_CallMethodTuple(PyObject* obj, PyObject* method_name, PyObject* args) {
     PyObject *method, *result = NULL;
     if (unlikely(!args)) return NULL;
@@ -875,6 +914,8 @@ static CYTHON_INLINE int __Pyx_PySequence_Contains(PyObject* item, PyObject* seq
     return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
 }
 
+static CYTHON_INLINE int __Pyx_CheckKeywordStrings(PyObject *kwdict, const char* function_name, int kw_allowed); /*proto*/
+
 static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
 
 static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
@@ -1107,8 +1148,8 @@ static PyTypeObject *__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy = 0;
 static PyTypeObject *__pyx_ptype_5pysam_10TabProxies_BedProxy = 0;
 static PyTypeObject *__pyx_ptype_5pysam_10TabProxies_VCFProxy = 0;
 static PyObject *__pyx_v_5pysam_10TabProxies__FILENAME_ENCODING = 0;
-static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *); /*proto*/
-static PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *); /*proto*/
+static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *, struct __pyx_opt_args_5pysam_10TabProxies__force_bytes *__pyx_optional_args); /*proto*/
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *, struct __pyx_opt_args_5pysam_10TabProxies__force_str *__pyx_optional_args); /*proto*/
 static char *__pyx_f_5pysam_10TabProxies_StrOrEmpty(char *); /*proto*/
 static int __pyx_f_5pysam_10TabProxies_isNew(char *, char *, size_t); /*proto*/
 #define __Pyx_MODULE_NAME "pysam.TabProxies"
@@ -1123,7 +1164,7 @@ static PyObject *__pyx_builtin_StopIteration;
 static PyObject *__pyx_builtin_xrange;
 static PyObject *__pyx_builtin_AttributeError;
 static PyObject *__pyx_builtin_KeyError;
-static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self, PyObject *__pyx_v_encoding); /* proto */
 static void __pyx_pf_5pysam_10TabProxies_10TupleProxy_2__dealloc__(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_4_getindex(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self, int __pyx_v_index); /* proto */
 static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self, PyObject *__pyx_v_key); /* proto */
@@ -1176,17 +1217,17 @@ static PyObject *__pyx_tp_new_5pysam_10TabProxies_GTFProxy(PyTypeObject *t, PyOb
 static PyObject *__pyx_tp_new_5pysam_10TabProxies_NamedTupleProxy(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_10TabProxies_BedProxy(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_10TabProxies_VCFProxy(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static char __pyx_k_[] = "";
 static char __pyx_k_s[] = "\"%s\"";
 static char __pyx_k_v[] = "v";
-static char __pyx_k__3[] = "";
+static char __pyx_k__8[] = "\t";
 static char __pyx_k_id[] = "id";
-static char __pyx_k__10[] = "\t";
-static char __pyx_k__13[] = ".";
-static char __pyx_k__23[] = ";";
-static char __pyx_k__26[] = " ";
-static char __pyx_k__27[] = "\"";
-static char __pyx_k__29[] = "; ";
-static char __pyx_k__31[] = "-";
+static char __pyx_k__10[] = ".";
+static char __pyx_k__20[] = ";";
+static char __pyx_k__23[] = " ";
+static char __pyx_k__24[] = "\"";
+static char __pyx_k__26[] = "; ";
+static char __pyx_k__28[] = "-";
 static char __pyx_k_alt[] = "alt";
 static char __pyx_k_end[] = "end";
 static char __pyx_k_pos[] = "pos";
@@ -1232,6 +1273,7 @@ static char __pyx_k_indices[] = "indices";
 static char __pyx_k_itemRGB[] = "itemRGB";
 static char __pyx_k_setitem[] = "__setitem__";
 static char __pyx_k_KeyError[] = "KeyError";
+static char __pyx_k_encoding[] = "encoding";
 static char __pyx_k_fromDict[] = "fromDict";
 static char __pyx_k_getindex[] = "_getindex";
 static char __pyx_k_maxsplit[] = "maxsplit";
@@ -1269,14 +1311,16 @@ static char __pyx_k_list_index_out_of_range_i_i[] = "list index out of range %i
 static char __pyx_k_feature_attributes_as_a_string[] = "feature attributes (as a string).";
 static char __pyx_k_home_andreas_devel_pysam_pysam[] = "/home/andreas/devel/pysam/pysam/TabProxies.pyx";
 static char __pyx_k_Argument_must_be_string_bytes_or[] = "Argument must be string, bytes or unicode.";
-static char __pyx_k_Argument_must_be_string_or_unico[] = "Argument must be string or unicode.";
 static char __pyx_k_bed_format_requires_at_least_thr[] = "bed format requires at least three columns";
 static char __pyx_k_feature_end_in_0_based_open_clos[] = "feature end (in 0-based open/closed coordinates).";
 static char __pyx_k_feature_start_in_0_based_open_cl[] = "feature start (in 0-based open/closed coordinates).";
 static char __pyx_k_length_of_buffer_i_number_of_byt[] = "length of buffer (%i) != number of bytes (%i)";
-static char __pyx_k_row_too_large_more_than_i_fields[] = "row too large - more than %i fields";
+static char __pyx_k_out_of_memory_in_TupleProxy_copy[] = "out of memory in TupleProxy.copy()";
+static char __pyx_k_out_of_memory_in_TupleProxy_upda[] = "out of memory in TupleProxy.update()";
+static char __pyx_k_parsing_error_fewer_that_i_field[] = "parsing error: fewer that %i fields in line: %s";
+static char __pyx_k_parsing_error_more_than_i_fields[] = "parsing error: more than %i fields in line: %s";
+static PyObject *__pyx_kp_s_;
 static PyObject *__pyx_kp_u_Argument_must_be_string_bytes_or;
-static PyObject *__pyx_kp_u_Argument_must_be_string_or_unico;
 static PyObject *__pyx_n_s_AttributeError;
 static PyObject *__pyx_kp_s_GTFProxy_has_no_attribute_s;
 static PyObject *__pyx_n_s_IndexError;
@@ -1286,13 +1330,12 @@ static PyObject *__pyx_n_s_StringTypes;
 static PyObject *__pyx_n_s_TypeError;
 static PyObject *__pyx_n_s_ValueError;
 static PyObject *__pyx_kp_s__10;
-static PyObject *__pyx_kp_s__13;
+static PyObject *__pyx_kp_s__20;
 static PyObject *__pyx_kp_s__23;
+static PyObject *__pyx_kp_s__24;
 static PyObject *__pyx_kp_s__26;
-static PyObject *__pyx_kp_s__27;
-static PyObject *__pyx_kp_s__29;
-static PyObject *__pyx_kp_s__3;
-static PyObject *__pyx_kp_s__31;
+static PyObject *__pyx_kp_s__28;
+static PyObject *__pyx_kp_s__8;
 static PyObject *__pyx_n_s_alt;
 static PyObject *__pyx_n_s_append;
 static PyObject *__pyx_n_s_asDict;
@@ -1305,6 +1348,7 @@ static PyObject *__pyx_n_s_blockStarts;
 static PyObject *__pyx_n_s_contig;
 static PyObject *__pyx_n_s_decode;
 static PyObject *__pyx_n_s_encode;
+static PyObject *__pyx_n_s_encoding;
 static PyObject *__pyx_n_s_end;
 static PyObject *__pyx_n_s_feature;
 static PyObject *__pyx_kp_s_field_s_not_set;
@@ -1335,6 +1379,10 @@ static PyObject *__pyx_n_s_map_key2field;
 static PyObject *__pyx_n_s_maxsplit;
 static PyObject *__pyx_n_s_name;
 static PyObject *__pyx_kp_s_out_of_memory;
+static PyObject *__pyx_kp_s_out_of_memory_in_TupleProxy_copy;
+static PyObject *__pyx_kp_s_out_of_memory_in_TupleProxy_upda;
+static PyObject *__pyx_kp_s_parsing_error_fewer_that_i_field;
+static PyObject *__pyx_kp_s_parsing_error_more_than_i_fields;
 static PyObject *__pyx_n_s_pos;
 static PyObject *__pyx_n_s_pysam_TabProxies;
 static PyObject *__pyx_n_s_pyx_vtable;
@@ -1342,7 +1390,6 @@ static PyObject *__pyx_n_s_qual;
 static PyObject *__pyx_n_s_quote;
 static PyObject *__pyx_n_s_range;
 static PyObject *__pyx_n_s_ref;
-static PyObject *__pyx_kp_s_row_too_large_more_than_i_fields;
 static PyObject *__pyx_kp_s_s;
 static PyObject *__pyx_kp_s_s_s;
 static PyObject *__pyx_kp_s_s_s_2;
@@ -1378,39 +1425,36 @@ static PyObject *__pyx_int_9;
 static PyObject *__pyx_int_10;
 static PyObject *__pyx_int_11;
 static PyObject *__pyx_int_neg_1;
-static PyObject *__pyx_tuple_;
 static PyObject *__pyx_tuple__2;
+static PyObject *__pyx_tuple__3;
 static PyObject *__pyx_tuple__4;
 static PyObject *__pyx_tuple__5;
 static PyObject *__pyx_tuple__6;
 static PyObject *__pyx_tuple__7;
-static PyObject *__pyx_tuple__8;
 static PyObject *__pyx_tuple__9;
+static PyObject *__pyx_slice__22;
 static PyObject *__pyx_slice__25;
-static PyObject *__pyx_slice__28;
 static PyObject *__pyx_tuple__11;
 static PyObject *__pyx_tuple__12;
+static PyObject *__pyx_tuple__13;
 static PyObject *__pyx_tuple__14;
 static PyObject *__pyx_tuple__15;
 static PyObject *__pyx_tuple__16;
 static PyObject *__pyx_tuple__17;
 static PyObject *__pyx_tuple__18;
 static PyObject *__pyx_tuple__19;
-static PyObject *__pyx_tuple__20;
 static PyObject *__pyx_tuple__21;
-static PyObject *__pyx_tuple__22;
-static PyObject *__pyx_tuple__24;
+static PyObject *__pyx_tuple__27;
+static PyObject *__pyx_tuple__29;
 static PyObject *__pyx_tuple__30;
+static PyObject *__pyx_tuple__31;
 static PyObject *__pyx_tuple__32;
-static PyObject *__pyx_tuple__33;
 static PyObject *__pyx_tuple__34;
-static PyObject *__pyx_tuple__35;
-static PyObject *__pyx_tuple__37;
-static PyObject *__pyx_codeobj__36;
-static PyObject *__pyx_codeobj__38;
+static PyObject *__pyx_codeobj__33;
+static PyObject *__pyx_codeobj__35;
 
-/* "pysam/TabProxies.pyx":9
- * from cpython cimport PyErr_SetString, PyBytes_Check, PyUnicode_Check, PyBytes_FromStringAndSize
+/* "pysam/TabProxies.pyx":12
+ * 
  * 
  * cdef from_string_and_size(char* s, size_t length):             # <<<<<<<<<<<<<<
  *     if PY_MAJOR_VERSION < 3:
@@ -1427,7 +1471,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_from_string_and_size(char *__pyx_v_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("from_string_and_size", 0);
 
-  /* "pysam/TabProxies.pyx":10
+  /* "pysam/TabProxies.pyx":13
  * 
  * cdef from_string_and_size(char* s, size_t length):
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
@@ -1437,7 +1481,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_from_string_and_size(char *__pyx_v_
   __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":11
+    /* "pysam/TabProxies.pyx":14
  * cdef from_string_and_size(char* s, size_t length):
  *     if PY_MAJOR_VERSION < 3:
  *         return s[:length]             # <<<<<<<<<<<<<<
@@ -1445,7 +1489,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_from_string_and_size(char *__pyx_v_
  *         return s[:length].decode("ascii")
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromStringAndSize(__pyx_v_s + 0, __pyx_v_length - 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromStringAndSize(__pyx_v_s + 0, __pyx_v_length - 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -1453,7 +1497,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_from_string_and_size(char *__pyx_v_
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":13
+    /* "pysam/TabProxies.pyx":16
  *         return s[:length]
  *     else:
  *         return s[:length].decode("ascii")             # <<<<<<<<<<<<<<
@@ -1461,15 +1505,15 @@ static PyObject *__pyx_f_5pysam_10TabProxies_from_string_and_size(char *__pyx_v_
  * # filename encoding (copied from lxml.etree.pyx)
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, __pyx_v_length, NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, __pyx_v_length, NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":9
- * from cpython cimport PyErr_SetString, PyBytes_Check, PyUnicode_Check, PyBytes_FromStringAndSize
+  /* "pysam/TabProxies.pyx":12
+ * 
  * 
  * cdef from_string_and_size(char* s, size_t length):             # <<<<<<<<<<<<<<
  *     if PY_MAJOR_VERSION < 3:
@@ -1487,164 +1531,34 @@ static PyObject *__pyx_f_5pysam_10TabProxies_from_string_and_size(char *__pyx_v_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":23
- *     _FILENAME_ENCODING = 'ascii'
- * 
- * cdef bytes _my_encodeFilename(object filename):             # <<<<<<<<<<<<<<
- *     u"""Make sure a filename is 8-bit encoded (or None).
- *     """
- */
-
-static PyObject *__pyx_f_5pysam_10TabProxies__my_encodeFilename(PyObject *__pyx_v_filename) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_RefNannySetupContext("_my_encodeFilename", 0);
-
-  /* "pysam/TabProxies.pyx":26
- *     u"""Make sure a filename is 8-bit encoded (or None).
- *     """
- *     if filename is None:             # <<<<<<<<<<<<<<
- *         return None
- *     elif PyBytes_Check(filename):
- */
-  __pyx_t_1 = (__pyx_v_filename == Py_None);
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/TabProxies.pyx":27
- *     """
- *     if filename is None:
- *         return None             # <<<<<<<<<<<<<<
- *     elif PyBytes_Check(filename):
- *         return filename
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = ((PyObject*)Py_None);
-    goto __pyx_L0;
-  }
-
-  /* "pysam/TabProxies.pyx":28
- *     if filename is None:
- *         return None
- *     elif PyBytes_Check(filename):             # <<<<<<<<<<<<<<
- *         return filename
- *     elif PyUnicode_Check(filename):
- */
-  __pyx_t_2 = (PyBytes_Check(__pyx_v_filename) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/TabProxies.pyx":29
- *         return None
- *     elif PyBytes_Check(filename):
- *         return filename             # <<<<<<<<<<<<<<
- *     elif PyUnicode_Check(filename):
- *         return filename.encode(_FILENAME_ENCODING)
- */
-    __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_filename))||((__pyx_v_filename) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_filename)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_INCREF(__pyx_v_filename);
-    __pyx_r = ((PyObject*)__pyx_v_filename);
-    goto __pyx_L0;
-  }
-
-  /* "pysam/TabProxies.pyx":30
- *     elif PyBytes_Check(filename):
- *         return filename
- *     elif PyUnicode_Check(filename):             # <<<<<<<<<<<<<<
- *         return filename.encode(_FILENAME_ENCODING)
- *     else:
- */
-  __pyx_t_2 = (PyUnicode_Check(__pyx_v_filename) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/TabProxies.pyx":31
- *         return filename
- *     elif PyUnicode_Check(filename):
- *         return filename.encode(_FILENAME_ENCODING)             # <<<<<<<<<<<<<<
- *     else:
- *         raise TypeError, u"Argument must be string or unicode."
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_INCREF(__pyx_v_5pysam_10TabProxies__FILENAME_ENCODING);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_5pysam_10TabProxies__FILENAME_ENCODING);
-    __Pyx_GIVEREF(__pyx_v_5pysam_10TabProxies__FILENAME_ENCODING);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    if (!(likely(PyBytes_CheckExact(__pyx_t_5))||((__pyx_t_5) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_5)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_r = ((PyObject*)__pyx_t_5);
-    __pyx_t_5 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/TabProxies.pyx":33
- *         return filename.encode(_FILENAME_ENCODING)
- *     else:
- *         raise TypeError, u"Argument must be string or unicode."             # <<<<<<<<<<<<<<
- * 
- * cdef bytes _force_bytes(object s):
- */
-    __Pyx_Raise(__pyx_builtin_TypeError, __pyx_kp_u_Argument_must_be_string_or_unico, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/TabProxies.pyx":23
+/* "pysam/TabProxies.pyx":26
  *     _FILENAME_ENCODING = 'ascii'
  * 
- * cdef bytes _my_encodeFilename(object filename):             # <<<<<<<<<<<<<<
- *     u"""Make sure a filename is 8-bit encoded (or None).
- *     """
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.TabProxies._my_encodeFilename", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/TabProxies.pyx":35
- *         raise TypeError, u"Argument must be string or unicode."
- * 
- * cdef bytes _force_bytes(object s):             # <<<<<<<<<<<<<<
+ * cdef bytes _force_bytes(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
  *     u"""convert string or unicode object to bytes, assuming ascii encoding.
  *     """
  */
 
-static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *__pyx_v_s) {
+static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *__pyx_v_s, struct __pyx_opt_args_5pysam_10TabProxies__force_bytes *__pyx_optional_args) {
+  PyObject *__pyx_v_encoding = ((PyObject *)__pyx_n_s_ascii);
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   int __pyx_t_2;
   PyObject *__pyx_t_3 = NULL;
   PyObject *__pyx_t_4 = NULL;
+  PyObject *__pyx_t_5 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_force_bytes", 0);
+  if (__pyx_optional_args) {
+    if (__pyx_optional_args->__pyx_n > 0) {
+      __pyx_v_encoding = __pyx_optional_args->encoding;
+    }
+  }
 
-  /* "pysam/TabProxies.pyx":38
+  /* "pysam/TabProxies.pyx":29
  *     u"""convert string or unicode object to bytes, assuming ascii encoding.
  *     """
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
@@ -1654,7 +1568,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *__pyx_v_s) {
   __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":39
+    /* "pysam/TabProxies.pyx":30
  *     """
  *     if PY_MAJOR_VERSION < 3:
  *         return s             # <<<<<<<<<<<<<<
@@ -1662,13 +1576,13 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *__pyx_v_s) {
  *         return None
  */
     __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 39; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_INCREF(__pyx_v_s);
     __pyx_r = ((PyObject*)__pyx_v_s);
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":40
+  /* "pysam/TabProxies.pyx":31
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  *     elif s is None:             # <<<<<<<<<<<<<<
@@ -1679,7 +1593,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *__pyx_v_s) {
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/TabProxies.pyx":41
+    /* "pysam/TabProxies.pyx":32
  *         return s
  *     elif s is None:
  *         return None             # <<<<<<<<<<<<<<
@@ -1692,7 +1606,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":42
+  /* "pysam/TabProxies.pyx":33
  *     elif s is None:
  *         return None
  *     elif PyBytes_Check(s):             # <<<<<<<<<<<<<<
@@ -1702,65 +1616,71 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *__pyx_v_s) {
   __pyx_t_2 = (PyBytes_Check(__pyx_v_s) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/TabProxies.pyx":43
+    /* "pysam/TabProxies.pyx":34
  *         return None
  *     elif PyBytes_Check(s):
  *         return s             # <<<<<<<<<<<<<<
  *     elif PyUnicode_Check(s):
- *         return s.encode('ascii')
+ *         return s.encode(encoding)
  */
     __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_INCREF(__pyx_v_s);
     __pyx_r = ((PyObject*)__pyx_v_s);
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":44
+  /* "pysam/TabProxies.pyx":35
  *     elif PyBytes_Check(s):
  *         return s
  *     elif PyUnicode_Check(s):             # <<<<<<<<<<<<<<
- *         return s.encode('ascii')
+ *         return s.encode(encoding)
  *     else:
  */
   __pyx_t_2 = (PyUnicode_Check(__pyx_v_s) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/TabProxies.pyx":45
+    /* "pysam/TabProxies.pyx":36
  *         return s
  *     elif PyUnicode_Check(s):
- *         return s.encode('ascii')             # <<<<<<<<<<<<<<
+ *         return s.encode(encoding)             # <<<<<<<<<<<<<<
  *     else:
  *         raise TypeError, u"Argument must be string, bytes or unicode."
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple_, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_INCREF(__pyx_v_encoding);
+    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_encoding);
+    __Pyx_GIVEREF(__pyx_v_encoding);
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    if (!(likely(PyBytes_CheckExact(__pyx_t_4))||((__pyx_t_4) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_4)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_r = ((PyObject*)__pyx_t_4);
-    __pyx_t_4 = 0;
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    if (!(likely(PyBytes_CheckExact(__pyx_t_5))||((__pyx_t_5) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_5)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_r = ((PyObject*)__pyx_t_5);
+    __pyx_t_5 = 0;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":47
- *         return s.encode('ascii')
+    /* "pysam/TabProxies.pyx":38
+ *         return s.encode(encoding)
  *     else:
  *         raise TypeError, u"Argument must be string, bytes or unicode."             # <<<<<<<<<<<<<<
  * 
  * cdef inline bytes _force_cmdline_bytes(object s):
  */
     __Pyx_Raise(__pyx_builtin_TypeError, __pyx_kp_u_Argument_must_be_string_bytes_or, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":35
- *         raise TypeError, u"Argument must be string or unicode."
+  /* "pysam/TabProxies.pyx":26
+ *     _FILENAME_ENCODING = 'ascii'
  * 
- * cdef bytes _force_bytes(object s):             # <<<<<<<<<<<<<<
+ * cdef bytes _force_bytes(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
  *     u"""convert string or unicode object to bytes, assuming ascii encoding.
  *     """
  */
@@ -1769,6 +1689,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *__pyx_v_s) {
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_5);
   __Pyx_AddTraceback("pysam.TabProxies._force_bytes", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -1777,7 +1698,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_bytes(PyObject *__pyx_v_s) {
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":49
+/* "pysam/TabProxies.pyx":40
  *         raise TypeError, u"Argument must be string, bytes or unicode."
  * 
  * cdef inline bytes _force_cmdline_bytes(object s):             # <<<<<<<<<<<<<<
@@ -1794,21 +1715,21 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_10TabProxies__force_cmdline_bytes(
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_force_cmdline_bytes", 0);
 
-  /* "pysam/TabProxies.pyx":50
+  /* "pysam/TabProxies.pyx":41
  * 
  * cdef inline bytes _force_cmdline_bytes(object s):
  *     return _force_bytes(s)             # <<<<<<<<<<<<<<
  * 
- * cdef _charptr_to_str(char* s):
+ * cdef _charptr_to_str(char* s, encoding="ascii"):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_10TabProxies__force_bytes(__pyx_v_s); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_f_5pysam_10TabProxies__force_bytes(__pyx_v_s, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 41; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":49
+  /* "pysam/TabProxies.pyx":40
  *         raise TypeError, u"Argument must be string, bytes or unicode."
  * 
  * cdef inline bytes _force_cmdline_bytes(object s):             # <<<<<<<<<<<<<<
@@ -1827,27 +1748,35 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_10TabProxies__force_cmdline_bytes(
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":52
+/* "pysam/TabProxies.pyx":43
  *     return _force_bytes(s)
  * 
- * cdef _charptr_to_str(char* s):             # <<<<<<<<<<<<<<
+ * cdef _charptr_to_str(char* s, encoding="ascii"):             # <<<<<<<<<<<<<<
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  */
 
-static PyObject *__pyx_f_5pysam_10TabProxies__charptr_to_str(char *__pyx_v_s) {
+static PyObject *__pyx_f_5pysam_10TabProxies__charptr_to_str(char *__pyx_v_s, struct __pyx_opt_args_5pysam_10TabProxies__charptr_to_str *__pyx_optional_args) {
+  PyObject *__pyx_v_encoding = ((PyObject *)__pyx_n_s_ascii);
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_charptr_to_str", 0);
+  if (__pyx_optional_args) {
+    if (__pyx_optional_args->__pyx_n > 0) {
+      __pyx_v_encoding = __pyx_optional_args->encoding;
+    }
+  }
 
-  /* "pysam/TabProxies.pyx":53
+  /* "pysam/TabProxies.pyx":44
  * 
- * cdef _charptr_to_str(char* s):
+ * cdef _charptr_to_str(char* s, encoding="ascii"):
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
  *         return s
  *     else:
@@ -1855,15 +1784,15 @@ static PyObject *__pyx_f_5pysam_10TabProxies__charptr_to_str(char *__pyx_v_s) {
   __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":54
- * cdef _charptr_to_str(char* s):
+    /* "pysam/TabProxies.pyx":45
+ * cdef _charptr_to_str(char* s, encoding="ascii"):
  *     if PY_MAJOR_VERSION < 3:
  *         return s             # <<<<<<<<<<<<<<
  *     else:
- *         return s.decode("ascii")
+ *         return s.decode(encoding)
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -1871,25 +1800,37 @@ static PyObject *__pyx_f_5pysam_10TabProxies__charptr_to_str(char *__pyx_v_s) {
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":56
+    /* "pysam/TabProxies.pyx":47
  *         return s
  *     else:
- *         return s.decode("ascii")             # <<<<<<<<<<<<<<
+ *         return s.decode(encoding)             # <<<<<<<<<<<<<<
  * 
- * cdef _force_str(object s):
+ * cdef inline _force_str(object s, encoding="ascii"):
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, strlen(__pyx_v_s), NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_decode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_INCREF(__pyx_v_encoding);
+    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_encoding);
+    __Pyx_GIVEREF(__pyx_v_encoding);
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __pyx_r = __pyx_t_4;
+    __pyx_t_4 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":52
+  /* "pysam/TabProxies.pyx":43
  *     return _force_bytes(s)
  * 
- * cdef _charptr_to_str(char* s):             # <<<<<<<<<<<<<<
+ * cdef _charptr_to_str(char* s, encoding="ascii"):             # <<<<<<<<<<<<<<
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  */
@@ -1897,6 +1838,8 @@ static PyObject *__pyx_f_5pysam_10TabProxies__charptr_to_str(char *__pyx_v_s) {
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
   __Pyx_AddTraceback("pysam.TabProxies._charptr_to_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -1905,29 +1848,36 @@ static PyObject *__pyx_f_5pysam_10TabProxies__charptr_to_str(char *__pyx_v_s) {
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":58
- *         return s.decode("ascii")
+/* "pysam/TabProxies.pyx":49
+ *         return s.decode(encoding)
  * 
- * cdef _force_str(object s):             # <<<<<<<<<<<<<<
- *     """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
- *     if s is None:
+ * cdef inline _force_str(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *     """Return s converted to str type of current Python "
+ *     "(bytes in Py2, unicode in Py3)"""
  */
 
-static PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *__pyx_v_s) {
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *__pyx_v_s, struct __pyx_opt_args_5pysam_10TabProxies__force_str *__pyx_optional_args) {
+  PyObject *__pyx_v_encoding = ((PyObject *)__pyx_n_s_ascii);
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   int __pyx_t_2;
   PyObject *__pyx_t_3 = NULL;
   PyObject *__pyx_t_4 = NULL;
+  PyObject *__pyx_t_5 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_force_str", 0);
+  if (__pyx_optional_args) {
+    if (__pyx_optional_args->__pyx_n > 0) {
+      __pyx_v_encoding = __pyx_optional_args->encoding;
+    }
+  }
 
-  /* "pysam/TabProxies.pyx":60
- * cdef _force_str(object s):
- *     """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
+  /* "pysam/TabProxies.pyx":52
+ *     """Return s converted to str type of current Python "
+ *     "(bytes in Py2, unicode in Py3)"""
  *     if s is None:             # <<<<<<<<<<<<<<
  *         return None
  *     if PY_MAJOR_VERSION < 3:
@@ -1936,8 +1886,8 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *__pyx_v_s) {
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/TabProxies.pyx":61
- *     """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
+    /* "pysam/TabProxies.pyx":53
+ *     "(bytes in Py2, unicode in Py3)"""
  *     if s is None:
  *         return None             # <<<<<<<<<<<<<<
  *     if PY_MAJOR_VERSION < 3:
@@ -1949,7 +1899,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":62
+  /* "pysam/TabProxies.pyx":54
  *     if s is None:
  *         return None
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
@@ -1959,12 +1909,12 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *__pyx_v_s) {
   __pyx_t_2 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/TabProxies.pyx":63
+    /* "pysam/TabProxies.pyx":55
  *         return None
  *     if PY_MAJOR_VERSION < 3:
  *         return s             # <<<<<<<<<<<<<<
  *     elif PyBytes_Check(s):
- *         return s.decode('ascii')
+ *         return s.decode(encoding)
  */
     __Pyx_XDECREF(__pyx_r);
     __Pyx_INCREF(__pyx_v_s);
@@ -1972,41 +1922,47 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":64
+  /* "pysam/TabProxies.pyx":56
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  *     elif PyBytes_Check(s):             # <<<<<<<<<<<<<<
- *         return s.decode('ascii')
+ *         return s.decode(encoding)
  *     else:
  */
   __pyx_t_2 = (PyBytes_Check(__pyx_v_s) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/TabProxies.pyx":65
+    /* "pysam/TabProxies.pyx":57
  *         return s
  *     elif PyBytes_Check(s):
- *         return s.decode('ascii')             # <<<<<<<<<<<<<<
+ *         return s.decode(encoding)             # <<<<<<<<<<<<<<
  *     else:
  *         # assume unicode
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_decode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_decode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_INCREF(__pyx_v_encoding);
+    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_encoding);
+    __Pyx_GIVEREF(__pyx_v_encoding);
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_r = __pyx_t_4;
-    __pyx_t_4 = 0;
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    __pyx_r = __pyx_t_5;
+    __pyx_t_5 = 0;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":68
+    /* "pysam/TabProxies.pyx":60
  *     else:
  *         # assume unicode
  *         return s             # <<<<<<<<<<<<<<
  * 
- * cdef char * nextItem( char * buffer ):
+ * cdef char * nextItem(char * buffer):
  */
     __Pyx_XDECREF(__pyx_r);
     __Pyx_INCREF(__pyx_v_s);
@@ -2014,18 +1970,19 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":58
- *         return s.decode("ascii")
+  /* "pysam/TabProxies.pyx":49
+ *         return s.decode(encoding)
  * 
- * cdef _force_str(object s):             # <<<<<<<<<<<<<<
- *     """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
- *     if s is None:
+ * cdef inline _force_str(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *     """Return s converted to str type of current Python "
+ *     "(bytes in Py2, unicode in Py3)"""
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_5);
   __Pyx_AddTraceback("pysam.TabProxies._force_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -2034,12 +1991,12 @@ static PyObject *__pyx_f_5pysam_10TabProxies__force_str(PyObject *__pyx_v_s) {
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":70
+/* "pysam/TabProxies.pyx":62
  *         return s
  * 
- * cdef char * nextItem( char * buffer ):             # <<<<<<<<<<<<<<
+ * cdef char * nextItem(char * buffer):             # <<<<<<<<<<<<<<
  *     cdef char * pos
- *     pos = strchr( buffer, '\t' )
+ *     pos = strchr(buffer, '\t')
  */
 
 static char *__pyx_f_5pysam_10TabProxies_nextItem(char *__pyx_v_buffer) {
@@ -2054,53 +2011,61 @@ static char *__pyx_f_5pysam_10TabProxies_nextItem(char *__pyx_v_buffer) {
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("nextItem", 0);
 
-  /* "pysam/TabProxies.pyx":72
- * cdef char * nextItem( char * buffer ):
+  /* "pysam/TabProxies.pyx":64
+ * cdef char * nextItem(char * buffer):
  *     cdef char * pos
- *     pos = strchr( buffer, '\t' )             # <<<<<<<<<<<<<<
- *     if pos == NULL: raise ValueError( "malformatted entry at %s" % buffer )
- *     pos[0] = '\0'
+ *     pos = strchr(buffer, '\t')             # <<<<<<<<<<<<<<
+ *     if pos == NULL:
+ *         raise ValueError("malformatted entry at %s" % buffer)
  */
   __pyx_v_pos = strchr(__pyx_v_buffer, '\t');
 
-  /* "pysam/TabProxies.pyx":73
+  /* "pysam/TabProxies.pyx":65
  *     cdef char * pos
- *     pos = strchr( buffer, '\t' )
- *     if pos == NULL: raise ValueError( "malformatted entry at %s" % buffer )             # <<<<<<<<<<<<<<
+ *     pos = strchr(buffer, '\t')
+ *     if pos == NULL:             # <<<<<<<<<<<<<<
+ *         raise ValueError("malformatted entry at %s" % buffer)
  *     pos[0] = '\0'
- *     pos += 1
  */
   __pyx_t_1 = ((__pyx_v_pos == NULL) != 0);
   if (__pyx_t_1) {
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_buffer); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/TabProxies.pyx":66
+ *     pos = strchr(buffer, '\t')
+ *     if pos == NULL:
+ *         raise ValueError("malformatted entry at %s" % buffer)             # <<<<<<<<<<<<<<
+ *     pos[0] = '\0'
+ *     pos += 1
+ */
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_buffer); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_malformatted_entry_at_s, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_malformatted_entry_at_s, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_3);
     __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":74
- *     pos = strchr( buffer, '\t' )
- *     if pos == NULL: raise ValueError( "malformatted entry at %s" % buffer )
+  /* "pysam/TabProxies.pyx":67
+ *     if pos == NULL:
+ *         raise ValueError("malformatted entry at %s" % buffer)
  *     pos[0] = '\0'             # <<<<<<<<<<<<<<
  *     pos += 1
  *     return pos
  */
   (__pyx_v_pos[0]) = '\x00';
 
-  /* "pysam/TabProxies.pyx":75
- *     if pos == NULL: raise ValueError( "malformatted entry at %s" % buffer )
+  /* "pysam/TabProxies.pyx":68
+ *         raise ValueError("malformatted entry at %s" % buffer)
  *     pos[0] = '\0'
  *     pos += 1             # <<<<<<<<<<<<<<
  *     return pos
@@ -2108,22 +2073,22 @@ static char *__pyx_f_5pysam_10TabProxies_nextItem(char *__pyx_v_buffer) {
  */
   __pyx_v_pos = (__pyx_v_pos + 1);
 
-  /* "pysam/TabProxies.pyx":76
+  /* "pysam/TabProxies.pyx":69
  *     pos[0] = '\0'
  *     pos += 1
  *     return pos             # <<<<<<<<<<<<<<
  * 
- * cdef char *StrOrEmpty( char * buffer ):
+ * cdef char *StrOrEmpty(char * buffer):
  */
   __pyx_r = __pyx_v_pos;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":70
+  /* "pysam/TabProxies.pyx":62
  *         return s
  * 
- * cdef char * nextItem( char * buffer ):             # <<<<<<<<<<<<<<
+ * cdef char * nextItem(char * buffer):             # <<<<<<<<<<<<<<
  *     cdef char * pos
- *     pos = strchr( buffer, '\t' )
+ *     pos = strchr(buffer, '\t')
  */
 
   /* function exit code */
@@ -2137,12 +2102,12 @@ static char *__pyx_f_5pysam_10TabProxies_nextItem(char *__pyx_v_buffer) {
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":78
+/* "pysam/TabProxies.pyx":71
  *     return pos
  * 
- * cdef char *StrOrEmpty( char * buffer ):             # <<<<<<<<<<<<<<
- *      if buffer == NULL: return ""
- *      else: return buffer
+ * cdef char *StrOrEmpty(char * buffer):             # <<<<<<<<<<<<<<
+ *      if buffer == NULL:
+ *          return ""
  */
 
 static char *__pyx_f_5pysam_10TabProxies_StrOrEmpty(char *__pyx_v_buffer) {
@@ -2151,37 +2116,45 @@ static char *__pyx_f_5pysam_10TabProxies_StrOrEmpty(char *__pyx_v_buffer) {
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("StrOrEmpty", 0);
 
-  /* "pysam/TabProxies.pyx":79
+  /* "pysam/TabProxies.pyx":72
  * 
- * cdef char *StrOrEmpty( char * buffer ):
- *      if buffer == NULL: return ""             # <<<<<<<<<<<<<<
+ * cdef char *StrOrEmpty(char * buffer):
+ *      if buffer == NULL:             # <<<<<<<<<<<<<<
+ *          return ""
  *      else: return buffer
- * 
  */
   __pyx_t_1 = ((__pyx_v_buffer == NULL) != 0);
   if (__pyx_t_1) {
-    __pyx_r = __pyx_k__3;
+
+    /* "pysam/TabProxies.pyx":73
+ * cdef char *StrOrEmpty(char * buffer):
+ *      if buffer == NULL:
+ *          return ""             # <<<<<<<<<<<<<<
+ *      else: return buffer
+ * 
+ */
+    __pyx_r = __pyx_k_;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":80
- * cdef char *StrOrEmpty( char * buffer ):
- *      if buffer == NULL: return ""
+    /* "pysam/TabProxies.pyx":74
+ *      if buffer == NULL:
+ *          return ""
  *      else: return buffer             # <<<<<<<<<<<<<<
  * 
- * cdef int isNew( char * p, char * buffer, size_t nbytes ):
+ * cdef int isNew(char * p, char * buffer, size_t nbytes):
  */
     __pyx_r = __pyx_v_buffer;
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":78
+  /* "pysam/TabProxies.pyx":71
  *     return pos
  * 
- * cdef char *StrOrEmpty( char * buffer ):             # <<<<<<<<<<<<<<
- *      if buffer == NULL: return ""
- *      else: return buffer
+ * cdef char *StrOrEmpty(char * buffer):             # <<<<<<<<<<<<<<
+ *      if buffer == NULL:
+ *          return ""
  */
 
   /* function exit code */
@@ -2190,12 +2163,12 @@ static char *__pyx_f_5pysam_10TabProxies_StrOrEmpty(char *__pyx_v_buffer) {
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":82
+/* "pysam/TabProxies.pyx":76
  *      else: return buffer
  * 
- * cdef int isNew( char * p, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
- *      if p == NULL: return 0
- *      return not (buffer <= p < buffer + nbytes )
+ * cdef int isNew(char * p, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
+ *      if p == NULL:
+ *          return 0
  */
 
 static int __pyx_f_5pysam_10TabProxies_isNew(char *__pyx_v_p, char *__pyx_v_buffer, size_t __pyx_v_nbytes) {
@@ -2204,23 +2177,31 @@ static int __pyx_f_5pysam_10TabProxies_isNew(char *__pyx_v_p, char *__pyx_v_buff
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("isNew", 0);
 
-  /* "pysam/TabProxies.pyx":83
- * 
- * cdef int isNew( char * p, char * buffer, size_t nbytes ):
- *      if p == NULL: return 0             # <<<<<<<<<<<<<<
- *      return not (buffer <= p < buffer + nbytes )
+  /* "pysam/TabProxies.pyx":77
  * 
+ * cdef int isNew(char * p, char * buffer, size_t nbytes):
+ *      if p == NULL:             # <<<<<<<<<<<<<<
+ *          return 0
+ *      return not (buffer <= p < buffer + nbytes)
  */
   __pyx_t_1 = ((__pyx_v_p == NULL) != 0);
   if (__pyx_t_1) {
+
+    /* "pysam/TabProxies.pyx":78
+ * cdef int isNew(char * p, char * buffer, size_t nbytes):
+ *      if p == NULL:
+ *          return 0             # <<<<<<<<<<<<<<
+ *      return not (buffer <= p < buffer + nbytes)
+ * 
+ */
     __pyx_r = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":84
- * cdef int isNew( char * p, char * buffer, size_t nbytes ):
- *      if p == NULL: return 0
- *      return not (buffer <= p < buffer + nbytes )             # <<<<<<<<<<<<<<
+  /* "pysam/TabProxies.pyx":79
+ *      if p == NULL:
+ *          return 0
+ *      return not (buffer <= p < buffer + nbytes)             # <<<<<<<<<<<<<<
  * 
  * cdef class TupleProxy:
  */
@@ -2231,12 +2212,12 @@ static int __pyx_f_5pysam_10TabProxies_isNew(char *__pyx_v_p, char *__pyx_v_buff
   __pyx_r = (!(__pyx_t_1 != 0));
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":82
+  /* "pysam/TabProxies.pyx":76
  *      else: return buffer
  * 
- * cdef int isNew( char * p, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
- *      if p == NULL: return 0
- *      return not (buffer <= p < buffer + nbytes )
+ * cdef int isNew(char * p, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
+ *      if p == NULL:
+ *          return 0
  */
 
   /* function exit code */
@@ -2245,10 +2226,10 @@ static int __pyx_f_5pysam_10TabProxies_isNew(char *__pyx_v_p, char *__pyx_v_buff
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":97
+/* "pysam/TabProxies.pyx":92
  *     '''
  * 
- *     def __cinit__(self ):             # <<<<<<<<<<<<<<
+ *     def __cinit__(self, encoding="ascii"):             # <<<<<<<<<<<<<<
  *         self.data = NULL
  *         self.fields = NULL
  */
@@ -2256,35 +2237,76 @@ static int __pyx_f_5pysam_10TabProxies_isNew(char *__pyx_v_p, char *__pyx_v_buff
 /* Python wrapper */
 static int __pyx_pw_5pysam_10TabProxies_10TupleProxy_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
 static int __pyx_pw_5pysam_10TabProxies_10TupleProxy_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  PyObject *__pyx_v_encoding = 0;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__cinit__ (wrapper)", 0);
-  if (unlikely(PyTuple_GET_SIZE(__pyx_args) > 0)) {
-    __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 0, 0, PyTuple_GET_SIZE(__pyx_args)); return -1;}
-  if (unlikely(__pyx_kwds) && unlikely(PyDict_Size(__pyx_kwds) > 0) && unlikely(!__Pyx_CheckKeywordStrings(__pyx_kwds, "__cinit__", 0))) return -1;
-  __pyx_r = __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_self));
+  {
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_encoding,0};
+    PyObject* values[1] = {0};
+    values[0] = ((PyObject *)__pyx_n_s_ascii);
+    if (unlikely(__pyx_kwds)) {
+      Py_ssize_t kw_args;
+      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
+      switch (pos_args) {
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+      kw_args = PyDict_Size(__pyx_kwds);
+      switch (pos_args) {
+        case  0:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_encoding);
+          if (value) { values[0] = value; kw_args--; }
+        }
+      }
+      if (unlikely(kw_args > 0)) {
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+    } else {
+      switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+    }
+    __pyx_v_encoding = values[0];
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 0, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("pysam.TabProxies.TupleProxy.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_RefNannyFinishContext();
+  return -1;
+  __pyx_L4_argument_unpacking_done:;
+  __pyx_r = __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_self), __pyx_v_encoding);
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self) {
+static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self, PyObject *__pyx_v_encoding) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__cinit__", 0);
 
-  /* "pysam/TabProxies.pyx":98
+  /* "pysam/TabProxies.pyx":93
  * 
- *     def __cinit__(self ):
+ *     def __cinit__(self, encoding="ascii"):
  *         self.data = NULL             # <<<<<<<<<<<<<<
  *         self.fields = NULL
  *         self.index = 0
  */
   __pyx_v_self->data = NULL;
 
-  /* "pysam/TabProxies.pyx":99
- *     def __cinit__(self ):
+  /* "pysam/TabProxies.pyx":94
+ *     def __cinit__(self, encoding="ascii"):
  *         self.data = NULL
  *         self.fields = NULL             # <<<<<<<<<<<<<<
  *         self.index = 0
@@ -2292,7 +2314,7 @@ static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_
  */
   __pyx_v_self->fields = NULL;
 
-  /* "pysam/TabProxies.pyx":100
+  /* "pysam/TabProxies.pyx":95
  *         self.data = NULL
  *         self.fields = NULL
  *         self.index = 0             # <<<<<<<<<<<<<<
@@ -2301,7 +2323,7 @@ static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_
  */
   __pyx_v_self->index = 0;
 
-  /* "pysam/TabProxies.pyx":101
+  /* "pysam/TabProxies.pyx":96
  *         self.fields = NULL
  *         self.index = 0
  *         self.nbytes = 0             # <<<<<<<<<<<<<<
@@ -2310,7 +2332,7 @@ static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_
  */
   __pyx_v_self->nbytes = 0;
 
-  /* "pysam/TabProxies.pyx":102
+  /* "pysam/TabProxies.pyx":97
  *         self.index = 0
  *         self.nbytes = 0
  *         self.is_modified = 0             # <<<<<<<<<<<<<<
@@ -2319,7 +2341,7 @@ static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_
  */
   __pyx_v_self->is_modified = 0;
 
-  /* "pysam/TabProxies.pyx":103
+  /* "pysam/TabProxies.pyx":98
  *         self.nbytes = 0
  *         self.is_modified = 0
  *         self.nfields = 0             # <<<<<<<<<<<<<<
@@ -2328,19 +2350,32 @@ static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_
  */
   __pyx_v_self->nfields = 0;
 
-  /* "pysam/TabProxies.pyx":105
+  /* "pysam/TabProxies.pyx":100
  *         self.nfields = 0
  *         # start counting at field offset
  *         self.offset = 0             # <<<<<<<<<<<<<<
+ *         self.encoding = encoding
  * 
- *     def __dealloc__(self):
  */
   __pyx_v_self->offset = 0;
 
-  /* "pysam/TabProxies.pyx":97
+  /* "pysam/TabProxies.pyx":101
+ *         # start counting at field offset
+ *         self.offset = 0
+ *         self.encoding = encoding             # <<<<<<<<<<<<<<
+ * 
+ *     def __dealloc__(self):
+ */
+  __Pyx_INCREF(__pyx_v_encoding);
+  __Pyx_GIVEREF(__pyx_v_encoding);
+  __Pyx_GOTREF(__pyx_v_self->encoding);
+  __Pyx_DECREF(__pyx_v_self->encoding);
+  __pyx_v_self->encoding = __pyx_v_encoding;
+
+  /* "pysam/TabProxies.pyx":92
  *     '''
  * 
- *     def __cinit__(self ):             # <<<<<<<<<<<<<<
+ *     def __cinit__(self, encoding="ascii"):             # <<<<<<<<<<<<<<
  *         self.data = NULL
  *         self.fields = NULL
  */
@@ -2351,8 +2386,8 @@ static int __pyx_pf_5pysam_10TabProxies_10TupleProxy___cinit__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":107
- *         self.offset = 0
+/* "pysam/TabProxies.pyx":103
+ *         self.encoding = encoding
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
  *         cdef int x
@@ -2377,51 +2412,51 @@ static void __pyx_pf_5pysam_10TabProxies_10TupleProxy_2__dealloc__(struct __pyx_
   int __pyx_t_2;
   __Pyx_RefNannySetupContext("__dealloc__", 0);
 
-  /* "pysam/TabProxies.pyx":109
+  /* "pysam/TabProxies.pyx":105
  *     def __dealloc__(self):
  *         cdef int x
  *         if self.is_modified:             # <<<<<<<<<<<<<<
  *             for x from 0 <= x < self.nfields:
- *                 if isNew( self.fields[x], self.data, self.nbytes ):
+ *                 if isNew(self.fields[x], self.data, self.nbytes):
  */
   __pyx_t_1 = (__pyx_v_self->is_modified != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":110
+    /* "pysam/TabProxies.pyx":106
  *         cdef int x
  *         if self.is_modified:
  *             for x from 0 <= x < self.nfields:             # <<<<<<<<<<<<<<
- *                 if isNew( self.fields[x], self.data, self.nbytes ):
- *                     free( self.fields[x] )
+ *                 if isNew(self.fields[x], self.data, self.nbytes):
+ *                     free(self.fields[x])
  */
     __pyx_t_2 = __pyx_v_self->nfields;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
 
-      /* "pysam/TabProxies.pyx":111
+      /* "pysam/TabProxies.pyx":107
  *         if self.is_modified:
  *             for x from 0 <= x < self.nfields:
- *                 if isNew( self.fields[x], self.data, self.nbytes ):             # <<<<<<<<<<<<<<
- *                     free( self.fields[x] )
+ *                 if isNew(self.fields[x], self.data, self.nbytes):             # <<<<<<<<<<<<<<
+ *                     free(self.fields[x])
  *                     self.fields[x] = NULL
  */
       __pyx_t_1 = (__pyx_f_5pysam_10TabProxies_isNew((__pyx_v_self->fields[__pyx_v_x]), __pyx_v_self->data, __pyx_v_self->nbytes) != 0);
       if (__pyx_t_1) {
 
-        /* "pysam/TabProxies.pyx":112
+        /* "pysam/TabProxies.pyx":108
  *             for x from 0 <= x < self.nfields:
- *                 if isNew( self.fields[x], self.data, self.nbytes ):
- *                     free( self.fields[x] )             # <<<<<<<<<<<<<<
+ *                 if isNew(self.fields[x], self.data, self.nbytes):
+ *                     free(self.fields[x])             # <<<<<<<<<<<<<<
  *                     self.fields[x] = NULL
  * 
  */
         free((__pyx_v_self->fields[__pyx_v_x]));
 
-        /* "pysam/TabProxies.pyx":113
- *                 if isNew( self.fields[x], self.data, self.nbytes ):
- *                     free( self.fields[x] )
+        /* "pysam/TabProxies.pyx":109
+ *                 if isNew(self.fields[x], self.data, self.nbytes):
+ *                     free(self.fields[x])
  *                     self.fields[x] = NULL             # <<<<<<<<<<<<<<
  * 
- *         if self.data != NULL: free(self.data)
+ *         if self.data != NULL:
  */
         (__pyx_v_self->fields[__pyx_v_x]) = NULL;
         goto __pyx_L6;
@@ -2432,36 +2467,52 @@ static void __pyx_pf_5pysam_10TabProxies_10TupleProxy_2__dealloc__(struct __pyx_
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":115
+  /* "pysam/TabProxies.pyx":111
  *                     self.fields[x] = NULL
  * 
- *         if self.data != NULL: free(self.data)             # <<<<<<<<<<<<<<
- *         if self.fields != NULL: free( self.fields )
- * 
+ *         if self.data != NULL:             # <<<<<<<<<<<<<<
+ *             free(self.data)
+ *         if self.fields != NULL:
  */
   __pyx_t_1 = ((__pyx_v_self->data != NULL) != 0);
   if (__pyx_t_1) {
+
+    /* "pysam/TabProxies.pyx":112
+ * 
+ *         if self.data != NULL:
+ *             free(self.data)             # <<<<<<<<<<<<<<
+ *         if self.fields != NULL:
+ *             free(self.fields)
+ */
     free(__pyx_v_self->data);
     goto __pyx_L7;
   }
   __pyx_L7:;
 
-  /* "pysam/TabProxies.pyx":116
- * 
- *         if self.data != NULL: free(self.data)
- *         if self.fields != NULL: free( self.fields )             # <<<<<<<<<<<<<<
+  /* "pysam/TabProxies.pyx":113
+ *         if self.data != NULL:
+ *             free(self.data)
+ *         if self.fields != NULL:             # <<<<<<<<<<<<<<
+ *             free(self.fields)
  * 
- *     cdef take( self, char * buffer, size_t nbytes ):
  */
   __pyx_t_1 = ((__pyx_v_self->fields != NULL) != 0);
   if (__pyx_t_1) {
+
+    /* "pysam/TabProxies.pyx":114
+ *             free(self.data)
+ *         if self.fields != NULL:
+ *             free(self.fields)             # <<<<<<<<<<<<<<
+ * 
+ *     cdef take(self, char * buffer, size_t nbytes):
+ */
     free(__pyx_v_self->fields);
     goto __pyx_L8;
   }
   __pyx_L8:;
 
-  /* "pysam/TabProxies.pyx":107
- *         self.offset = 0
+  /* "pysam/TabProxies.pyx":103
+ *         self.encoding = encoding
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
  *         cdef int x
@@ -2472,10 +2523,10 @@ static void __pyx_pf_5pysam_10TabProxies_10TupleProxy_2__dealloc__(struct __pyx_
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/TabProxies.pyx":118
- *         if self.fields != NULL: free( self.fields )
+/* "pysam/TabProxies.pyx":116
+ *             free(self.fields)
  * 
- *     cdef take( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef take(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''start presenting buffer.
  * 
  */
@@ -2489,39 +2540,39 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_take(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("take", 0);
 
-  /* "pysam/TabProxies.pyx":123
+  /* "pysam/TabProxies.pyx":121
  *         Take ownership of the pointer.
  *         '''
  *         self.data = buffer             # <<<<<<<<<<<<<<
  *         self.nbytes = nbytes
- *         self.update( buffer, nbytes )
+ *         self.update(buffer, nbytes)
  */
   __pyx_v_self->data = __pyx_v_buffer;
 
-  /* "pysam/TabProxies.pyx":124
+  /* "pysam/TabProxies.pyx":122
  *         '''
  *         self.data = buffer
  *         self.nbytes = nbytes             # <<<<<<<<<<<<<<
- *         self.update( buffer, nbytes )
+ *         self.update(buffer, nbytes)
  * 
  */
   __pyx_v_self->nbytes = __pyx_v_nbytes;
 
-  /* "pysam/TabProxies.pyx":125
+  /* "pysam/TabProxies.pyx":123
  *         self.data = buffer
  *         self.nbytes = nbytes
- *         self.update( buffer, nbytes )             # <<<<<<<<<<<<<<
+ *         self.update(buffer, nbytes)             # <<<<<<<<<<<<<<
  * 
- *     cdef present( self, char * buffer, size_t nbytes ):
+ *     cdef present(self, char * buffer, size_t nbytes):
  */
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->update(__pyx_v_self, __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->update(__pyx_v_self, __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":118
- *         if self.fields != NULL: free( self.fields )
+  /* "pysam/TabProxies.pyx":116
+ *             free(self.fields)
  * 
- *     cdef take( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef take(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''start presenting buffer.
  * 
  */
@@ -2539,10 +2590,10 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_take(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":127
- *         self.update( buffer, nbytes )
+/* "pysam/TabProxies.pyx":125
+ *         self.update(buffer, nbytes)
  * 
- *     cdef present( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef present(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''start presenting buffer.
  * 
  */
@@ -2556,21 +2607,21 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_present(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("present", 0);
 
-  /* "pysam/TabProxies.pyx":132
+  /* "pysam/TabProxies.pyx":130
  *         Do not take ownership of the pointer.
  *         '''
- *         self.update( buffer, nbytes )             # <<<<<<<<<<<<<<
+ *         self.update(buffer, nbytes)             # <<<<<<<<<<<<<<
  * 
- *     cdef copy( self, char * buffer, size_t nbytes ):
+ *     cdef copy(self, char * buffer, size_t nbytes):
  */
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->update(__pyx_v_self, __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->update(__pyx_v_self, __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":127
- *         self.update( buffer, nbytes )
+  /* "pysam/TabProxies.pyx":125
+ *         self.update(buffer, nbytes)
  * 
- *     cdef present( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef present(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''start presenting buffer.
  * 
  */
@@ -2588,10 +2639,10 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_present(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":134
- *         self.update( buffer, nbytes )
+/* "pysam/TabProxies.pyx":132
+ *         self.update(buffer, nbytes)
  * 
- *     cdef copy( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef copy(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''start presenting buffer of size *nbytes*.
  * 
  */
@@ -2607,81 +2658,81 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_copy(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("copy", 0);
 
-  /* "pysam/TabProxies.pyx":143
+  /* "pysam/TabProxies.pyx":141
  *         cdef int s
  *         # +1 for '\0'
  *         s = sizeof(char) *  (nbytes + 1)             # <<<<<<<<<<<<<<
- *         self.data = <char*>malloc( s )
+ *         self.data = <char*>malloc(s)
  *         if self.data == NULL:
  */
   __pyx_v_s = ((sizeof(char)) * (__pyx_v_nbytes + 1));
 
-  /* "pysam/TabProxies.pyx":144
+  /* "pysam/TabProxies.pyx":142
  *         # +1 for '\0'
  *         s = sizeof(char) *  (nbytes + 1)
- *         self.data = <char*>malloc( s )             # <<<<<<<<<<<<<<
+ *         self.data = <char*>malloc(s)             # <<<<<<<<<<<<<<
  *         if self.data == NULL:
- *             raise ValueError("out of memory" )
+ *             raise ValueError("out of memory in TupleProxy.copy()")
  */
   __pyx_v_self->data = ((char *)malloc(__pyx_v_s));
 
-  /* "pysam/TabProxies.pyx":145
+  /* "pysam/TabProxies.pyx":143
  *         s = sizeof(char) *  (nbytes + 1)
- *         self.data = <char*>malloc( s )
+ *         self.data = <char*>malloc(s)
  *         if self.data == NULL:             # <<<<<<<<<<<<<<
- *             raise ValueError("out of memory" )
+ *             raise ValueError("out of memory in TupleProxy.copy()")
  *         self.nbytes = nbytes
  */
   __pyx_t_1 = ((__pyx_v_self->data == NULL) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":146
- *         self.data = <char*>malloc( s )
+    /* "pysam/TabProxies.pyx":144
+ *         self.data = <char*>malloc(s)
  *         if self.data == NULL:
- *             raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("out of memory in TupleProxy.copy()")             # <<<<<<<<<<<<<<
  *         self.nbytes = nbytes
- *         memcpy( <char*>self.data, buffer, s )
+ *         memcpy(<char*>self.data, buffer, s)
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__4, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 146; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__2, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 146; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":147
+  /* "pysam/TabProxies.pyx":145
  *         if self.data == NULL:
- *             raise ValueError("out of memory" )
+ *             raise ValueError("out of memory in TupleProxy.copy()")
  *         self.nbytes = nbytes             # <<<<<<<<<<<<<<
- *         memcpy( <char*>self.data, buffer, s )
- *         self.update( self.data, nbytes )
+ *         memcpy(<char*>self.data, buffer, s)
+ *         self.update(self.data, nbytes)
  */
   __pyx_v_self->nbytes = __pyx_v_nbytes;
 
-  /* "pysam/TabProxies.pyx":148
- *             raise ValueError("out of memory" )
+  /* "pysam/TabProxies.pyx":146
+ *             raise ValueError("out of memory in TupleProxy.copy()")
  *         self.nbytes = nbytes
- *         memcpy( <char*>self.data, buffer, s )             # <<<<<<<<<<<<<<
- *         self.update( self.data, nbytes )
+ *         memcpy(<char*>self.data, buffer, s)             # <<<<<<<<<<<<<<
+ *         self.update(self.data, nbytes)
  * 
  */
   memcpy(((char *)__pyx_v_self->data), __pyx_v_buffer, __pyx_v_s);
 
-  /* "pysam/TabProxies.pyx":149
+  /* "pysam/TabProxies.pyx":147
  *         self.nbytes = nbytes
- *         memcpy( <char*>self.data, buffer, s )
- *         self.update( self.data, nbytes )             # <<<<<<<<<<<<<<
+ *         memcpy(<char*>self.data, buffer, s)
+ *         self.update(self.data, nbytes)             # <<<<<<<<<<<<<<
  * 
- *     cdef int getMaxFields( self, size_t nbytes ):
+ *     cdef int getMinFields(self):
  */
-  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->update(__pyx_v_self, __pyx_v_self->data, __pyx_v_nbytes); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->update(__pyx_v_self, __pyx_v_self->data, __pyx_v_nbytes); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 147; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":134
- *         self.update( buffer, nbytes )
+  /* "pysam/TabProxies.pyx":132
+ *         self.update(buffer, nbytes)
  * 
- *     cdef copy( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef copy(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''start presenting buffer of size *nbytes*.
  * 
  */
@@ -2699,35 +2750,35 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_copy(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":151
- *         self.update( self.data, nbytes )
+/* "pysam/TabProxies.pyx":149
+ *         self.update(self.data, nbytes)
  * 
- *     cdef int getMaxFields( self, size_t nbytes ):             # <<<<<<<<<<<<<<
- *         '''initialize fields.'''
- *         return nbytes / 2
+ *     cdef int getMinFields(self):             # <<<<<<<<<<<<<<
+ *         '''return minimum number of fields.'''
+ *         # 1 is not a valid tabix entry, but TupleProxy
  */
 
-static int __pyx_f_5pysam_10TabProxies_10TupleProxy_getMaxFields(CYTHON_UNUSED struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self, size_t __pyx_v_nbytes) {
+static int __pyx_f_5pysam_10TabProxies_10TupleProxy_getMinFields(CYTHON_UNUSED struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("getMaxFields", 0);
+  __Pyx_RefNannySetupContext("getMinFields", 0);
 
   /* "pysam/TabProxies.pyx":153
- *     cdef int getMaxFields( self, size_t nbytes ):
- *         '''initialize fields.'''
- *         return nbytes / 2             # <<<<<<<<<<<<<<
+ *         # 1 is not a valid tabix entry, but TupleProxy
+ *         # could be more generic.
+ *         return 1             # <<<<<<<<<<<<<<
  * 
- *     cdef update( self, char * buffer, size_t nbytes ):
+ *     cdef int getMaxFields(self):
  */
-  __pyx_r = (__pyx_v_nbytes / 2);
+  __pyx_r = 1;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":151
- *         self.update( self.data, nbytes )
+  /* "pysam/TabProxies.pyx":149
+ *         self.update(self.data, nbytes)
  * 
- *     cdef int getMaxFields( self, size_t nbytes ):             # <<<<<<<<<<<<<<
- *         '''initialize fields.'''
- *         return nbytes / 2
+ *     cdef int getMinFields(self):             # <<<<<<<<<<<<<<
+ *         '''return minimum number of fields.'''
+ *         # 1 is not a valid tabix entry, but TupleProxy
  */
 
   /* function exit code */
@@ -2737,9 +2788,46 @@ static int __pyx_f_5pysam_10TabProxies_10TupleProxy_getMaxFields(CYTHON_UNUSED s
 }
 
 /* "pysam/TabProxies.pyx":155
- *         return nbytes / 2
+ *         return 1
+ * 
+ *     cdef int getMaxFields(self):             # <<<<<<<<<<<<<<
+ *         '''return maximum number of fields. Return
+ *         0 for unknown length.'''
+ */
+
+static int __pyx_f_5pysam_10TabProxies_10TupleProxy_getMaxFields(CYTHON_UNUSED struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_self) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("getMaxFields", 0);
+
+  /* "pysam/TabProxies.pyx":158
+ *         '''return maximum number of fields. Return
+ *         0 for unknown length.'''
+ *         return 0             # <<<<<<<<<<<<<<
+ * 
+ *     cdef update(self, char * buffer, size_t nbytes):
+ */
+  __pyx_r = 0;
+  goto __pyx_L0;
+
+  /* "pysam/TabProxies.pyx":155
+ *         return 1
+ * 
+ *     cdef int getMaxFields(self):             # <<<<<<<<<<<<<<
+ *         '''return maximum number of fields. Return
+ *         0 for unknown length.'''
+ */
+
+  /* function exit code */
+  __pyx_L0:;
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/TabProxies.pyx":160
+ *         return 0
  * 
- *     cdef update( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef update(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''update internal data.
  * 
  */
@@ -2760,13 +2848,14 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
   int __pyx_t_6;
   int __pyx_t_7;
   int __pyx_t_8;
+  size_t __pyx_t_9;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("update", 0);
 
-  /* "pysam/TabProxies.pyx":175
- *         cdef int max_fields, x
+  /* "pysam/TabProxies.pyx":181
+ *         cdef int max_fields, min_fields, x
  * 
  *         assert strlen(buffer) == nbytes, \             # <<<<<<<<<<<<<<
  *             "length of buffer (%i) != number of bytes (%i)" % (
@@ -2776,18 +2865,18 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
   if (unlikely(!Py_OptimizeFlag)) {
     if (unlikely(!((strlen(__pyx_v_buffer) == __pyx_v_nbytes) != 0))) {
 
-      /* "pysam/TabProxies.pyx":177
+      /* "pysam/TabProxies.pyx":183
  *         assert strlen(buffer) == nbytes, \
  *             "length of buffer (%i) != number of bytes (%i)" % (
  *                 strlen(buffer), nbytes)             # <<<<<<<<<<<<<<
  * 
  *         if buffer[nbytes] != 0:
  */
-      __pyx_t_1 = __Pyx_PyInt_FromSize_t(strlen(__pyx_v_buffer)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyInt_FromSize_t(strlen(__pyx_v_buffer)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyInt_FromSize_t(__pyx_v_nbytes); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyInt_FromSize_t(__pyx_v_nbytes); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
       __Pyx_GIVEREF(__pyx_t_1);
@@ -2796,24 +2885,24 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
       __pyx_t_1 = 0;
       __pyx_t_2 = 0;
 
-      /* "pysam/TabProxies.pyx":176
+      /* "pysam/TabProxies.pyx":182
  * 
  *         assert strlen(buffer) == nbytes, \
  *             "length of buffer (%i) != number of bytes (%i)" % (             # <<<<<<<<<<<<<<
  *                 strlen(buffer), nbytes)
  * 
  */
-      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_length_of_buffer_i_number_of_byt, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_length_of_buffer_i_number_of_byt, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       PyErr_SetObject(PyExc_AssertionError, __pyx_t_2);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
   #endif
 
-  /* "pysam/TabProxies.pyx":179
+  /* "pysam/TabProxies.pyx":185
  *                 strlen(buffer), nbytes)
  * 
  *         if buffer[nbytes] != 0:             # <<<<<<<<<<<<<<
@@ -2823,32 +2912,32 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
   __pyx_t_4 = (((__pyx_v_buffer[__pyx_v_nbytes]) != 0) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/TabProxies.pyx":180
+    /* "pysam/TabProxies.pyx":186
  * 
  *         if buffer[nbytes] != 0:
  *             raise ValueError("incomplete line at %s" % buffer)             # <<<<<<<<<<<<<<
  * 
  *         #################################
  */
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_buffer); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_buffer); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_incomplete_line_at_s, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_incomplete_line_at_s, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_3);
     __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":184
+  /* "pysam/TabProxies.pyx":190
  *         #################################
  *         # remove line breaks and feeds and update number of bytes
  *         x = nbytes - 1             # <<<<<<<<<<<<<<
@@ -2857,7 +2946,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
  */
   __pyx_v_x = (__pyx_v_nbytes - 1);
 
-  /* "pysam/TabProxies.pyx":185
+  /* "pysam/TabProxies.pyx":191
  *         # remove line breaks and feeds and update number of bytes
  *         x = nbytes - 1
  *         while x > 0 and (buffer[x] == '\n' or buffer[x] == '\r'):             # <<<<<<<<<<<<<<
@@ -2880,7 +2969,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
     }
     if (!__pyx_t_5) break;
 
-    /* "pysam/TabProxies.pyx":186
+    /* "pysam/TabProxies.pyx":192
  *         x = nbytes - 1
  *         while x > 0 and (buffer[x] == '\n' or buffer[x] == '\r'):
  *             buffer[x] = '\0'             # <<<<<<<<<<<<<<
@@ -2889,7 +2978,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
  */
     (__pyx_v_buffer[__pyx_v_x]) = '\x00';
 
-    /* "pysam/TabProxies.pyx":187
+    /* "pysam/TabProxies.pyx":193
  *         while x > 0 and (buffer[x] == '\n' or buffer[x] == '\r'):
  *             buffer[x] = '\0'
  *             x -= 1             # <<<<<<<<<<<<<<
@@ -2899,7 +2988,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
     __pyx_v_x = (__pyx_v_x - 1);
   }
 
-  /* "pysam/TabProxies.pyx":188
+  /* "pysam/TabProxies.pyx":194
  *             buffer[x] = '\0'
  *             x -= 1
  *         self.nbytes = x + 1             # <<<<<<<<<<<<<<
@@ -2908,44 +2997,52 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
  */
   __pyx_v_self->nbytes = (__pyx_v_x + 1);
 
-  /* "pysam/TabProxies.pyx":192
+  /* "pysam/TabProxies.pyx":198
  *         #################################
  *         # clear data
- *         if self.fields != NULL: free(self.fields)             # <<<<<<<<<<<<<<
+ *         if self.fields != NULL:             # <<<<<<<<<<<<<<
+ *             free(self.fields)
  * 
- *         for field from 0 <= field < self.nfields:
  */
   __pyx_t_5 = ((__pyx_v_self->fields != NULL) != 0);
   if (__pyx_t_5) {
+
+    /* "pysam/TabProxies.pyx":199
+ *         # clear data
+ *         if self.fields != NULL:
+ *             free(self.fields)             # <<<<<<<<<<<<<<
+ * 
+ *         for field from 0 <= field < self.nfields:
+ */
     free(__pyx_v_self->fields);
     goto __pyx_L6;
   }
   __pyx_L6:;
 
-  /* "pysam/TabProxies.pyx":194
- *         if self.fields != NULL: free(self.fields)
+  /* "pysam/TabProxies.pyx":201
+ *             free(self.fields)
  * 
  *         for field from 0 <= field < self.nfields:             # <<<<<<<<<<<<<<
- *             if isNew( self.fields[field], self.data, self.nbytes ):
- *                 free( self.fields[field] )
+ *             if isNew(self.fields[field], self.data, self.nbytes):
+ *                 free(self.fields[field])
  */
   __pyx_t_8 = __pyx_v_self->nfields;
   for (__pyx_v_field = 0; __pyx_v_field < __pyx_t_8; __pyx_v_field++) {
 
-    /* "pysam/TabProxies.pyx":195
+    /* "pysam/TabProxies.pyx":202
  * 
  *         for field from 0 <= field < self.nfields:
- *             if isNew( self.fields[field], self.data, self.nbytes ):             # <<<<<<<<<<<<<<
- *                 free( self.fields[field] )
+ *             if isNew(self.fields[field], self.data, self.nbytes):             # <<<<<<<<<<<<<<
+ *                 free(self.fields[field])
  * 
  */
     __pyx_t_5 = (__pyx_f_5pysam_10TabProxies_isNew((__pyx_v_self->fields[__pyx_v_field]), __pyx_v_self->data, __pyx_v_self->nbytes) != 0);
     if (__pyx_t_5) {
 
-      /* "pysam/TabProxies.pyx":196
+      /* "pysam/TabProxies.pyx":203
  *         for field from 0 <= field < self.nfields:
- *             if isNew( self.fields[field], self.data, self.nbytes ):
- *                 free( self.fields[field] )             # <<<<<<<<<<<<<<
+ *             if isNew(self.fields[field], self.data, self.nbytes):
+ *                 free(self.fields[field])             # <<<<<<<<<<<<<<
  * 
  *         self.is_modified = self.nfields = 0
  */
@@ -2955,8 +3052,8 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
     __pyx_L9:;
   }
 
-  /* "pysam/TabProxies.pyx":198
- *                 free( self.fields[field] )
+  /* "pysam/TabProxies.pyx":205
+ *                 free(self.fields[field])
  * 
  *         self.is_modified = self.nfields = 0             # <<<<<<<<<<<<<<
  * 
@@ -2965,49 +3062,104 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
   __pyx_v_self->is_modified = 0;
   __pyx_v_self->nfields = 0;
 
-  /* "pysam/TabProxies.pyx":202
+  /* "pysam/TabProxies.pyx":209
  *         #################################
  *         # allocate new
- *         max_fields = self.getMaxFields( nbytes )             # <<<<<<<<<<<<<<
- *         self.fields = <char **>calloc( max_fields, sizeof(char *) )
- *         if self.fields == NULL:
+ *         max_fields = self.getMaxFields()             # <<<<<<<<<<<<<<
+ *         # pre-count fields - better would be
+ *         # to guess or dynamically grow
  */
-  __pyx_v_max_fields = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->getMaxFields(__pyx_v_self, __pyx_v_nbytes);
+  __pyx_v_max_fields = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->getMaxFields(__pyx_v_self);
 
-  /* "pysam/TabProxies.pyx":203
- *         # allocate new
- *         max_fields = self.getMaxFields( nbytes )
- *         self.fields = <char **>calloc( max_fields, sizeof(char *) )             # <<<<<<<<<<<<<<
+  /* "pysam/TabProxies.pyx":212
+ *         # pre-count fields - better would be
+ *         # to guess or dynamically grow
+ *         if max_fields == 0:             # <<<<<<<<<<<<<<
+ *             for x from 0 <= x < nbytes:
+ *                 if buffer[x] == '\t':
+ */
+  __pyx_t_5 = ((__pyx_v_max_fields == 0) != 0);
+  if (__pyx_t_5) {
+
+    /* "pysam/TabProxies.pyx":213
+ *         # to guess or dynamically grow
+ *         if max_fields == 0:
+ *             for x from 0 <= x < nbytes:             # <<<<<<<<<<<<<<
+ *                 if buffer[x] == '\t':
+ *                     max_fields += 1
+ */
+    __pyx_t_9 = __pyx_v_nbytes;
+    for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_9; __pyx_v_x++) {
+
+      /* "pysam/TabProxies.pyx":214
+ *         if max_fields == 0:
+ *             for x from 0 <= x < nbytes:
+ *                 if buffer[x] == '\t':             # <<<<<<<<<<<<<<
+ *                     max_fields += 1
+ *             max_fields += 1
+ */
+      __pyx_t_5 = (((__pyx_v_buffer[__pyx_v_x]) == '\t') != 0);
+      if (__pyx_t_5) {
+
+        /* "pysam/TabProxies.pyx":215
+ *             for x from 0 <= x < nbytes:
+ *                 if buffer[x] == '\t':
+ *                     max_fields += 1             # <<<<<<<<<<<<<<
+ *             max_fields += 1
+ * 
+ */
+        __pyx_v_max_fields = (__pyx_v_max_fields + 1);
+        goto __pyx_L13;
+      }
+      __pyx_L13:;
+    }
+
+    /* "pysam/TabProxies.pyx":216
+ *                 if buffer[x] == '\t':
+ *                     max_fields += 1
+ *             max_fields += 1             # <<<<<<<<<<<<<<
+ * 
+ *         self.fields = <char **>calloc(max_fields, sizeof(char *))
+ */
+    __pyx_v_max_fields = (__pyx_v_max_fields + 1);
+    goto __pyx_L10;
+  }
+  __pyx_L10:;
+
+  /* "pysam/TabProxies.pyx":218
+ *             max_fields += 1
+ * 
+ *         self.fields = <char **>calloc(max_fields, sizeof(char *))             # <<<<<<<<<<<<<<
  *         if self.fields == NULL:
- *             raise ValueError("out of memory" )
+ *             raise ValueError("out of memory in TupleProxy.update()")
  */
   __pyx_v_self->fields = ((char **)calloc(__pyx_v_max_fields, (sizeof(char *))));
 
-  /* "pysam/TabProxies.pyx":204
- *         max_fields = self.getMaxFields( nbytes )
- *         self.fields = <char **>calloc( max_fields, sizeof(char *) )
+  /* "pysam/TabProxies.pyx":219
+ * 
+ *         self.fields = <char **>calloc(max_fields, sizeof(char *))
  *         if self.fields == NULL:             # <<<<<<<<<<<<<<
- *             raise ValueError("out of memory" )
+ *             raise ValueError("out of memory in TupleProxy.update()")
  * 
  */
   __pyx_t_5 = ((__pyx_v_self->fields == NULL) != 0);
   if (__pyx_t_5) {
 
-    /* "pysam/TabProxies.pyx":205
- *         self.fields = <char **>calloc( max_fields, sizeof(char *) )
+    /* "pysam/TabProxies.pyx":220
+ *         self.fields = <char **>calloc(max_fields, sizeof(char *))
  *         if self.fields == NULL:
- *             raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("out of memory in TupleProxy.update()")             # <<<<<<<<<<<<<<
  * 
  *         #################################
  */
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__3, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":209
+  /* "pysam/TabProxies.pyx":224
  *         #################################
  *         # start filling
  *         field = 0             # <<<<<<<<<<<<<<
@@ -3016,7 +3168,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
  */
   __pyx_v_field = 0;
 
-  /* "pysam/TabProxies.pyx":210
+  /* "pysam/TabProxies.pyx":225
  *         # start filling
  *         field = 0
  *         self.fields[field] = pos = buffer             # <<<<<<<<<<<<<<
@@ -3026,65 +3178,134 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
   (__pyx_v_self->fields[__pyx_v_field]) = __pyx_v_buffer;
   __pyx_v_pos = __pyx_v_buffer;
 
-  /* "pysam/TabProxies.pyx":211
+  /* "pysam/TabProxies.pyx":226
  *         field = 0
  *         self.fields[field] = pos = buffer
  *         field += 1             # <<<<<<<<<<<<<<
  *         old_pos = pos
- * 
+ *         while 1:
  */
   __pyx_v_field = (__pyx_v_field + 1);
 
-  /* "pysam/TabProxies.pyx":212
+  /* "pysam/TabProxies.pyx":227
  *         self.fields[field] = pos = buffer
  *         field += 1
  *         old_pos = pos             # <<<<<<<<<<<<<<
- * 
  *         while 1:
+ * 
  */
   __pyx_v_old_pos = __pyx_v_pos;
 
-  /* "pysam/TabProxies.pyx":214
+  /* "pysam/TabProxies.pyx":228
+ *         field += 1
  *         old_pos = pos
- * 
  *         while 1:             # <<<<<<<<<<<<<<
  * 
- *             pos = <char*>memchr( pos, '\t', nbytes )
+ *             pos = <char*>memchr(pos, '\t', nbytes)
  */
   while (1) {
 
-    /* "pysam/TabProxies.pyx":216
+    /* "pysam/TabProxies.pyx":230
  *         while 1:
  * 
- *             pos = <char*>memchr( pos, '\t', nbytes )             # <<<<<<<<<<<<<<
- *             if pos == NULL: break
- *             pos[0] = '\0'
+ *             pos = <char*>memchr(pos, '\t', nbytes)             # <<<<<<<<<<<<<<
+ *             if pos == NULL:
+ *                 break
  */
     __pyx_v_pos = ((char *)memchr(__pyx_v_pos, '\t', __pyx_v_nbytes));
 
-    /* "pysam/TabProxies.pyx":217
+    /* "pysam/TabProxies.pyx":231
  * 
- *             pos = <char*>memchr( pos, '\t', nbytes )
- *             if pos == NULL: break             # <<<<<<<<<<<<<<
- *             pos[0] = '\0'
- *             pos += 1
+ *             pos = <char*>memchr(pos, '\t', nbytes)
+ *             if pos == NULL:             # <<<<<<<<<<<<<<
+ *                 break
+ *             if field >= max_fields:
  */
     __pyx_t_5 = ((__pyx_v_pos == NULL) != 0);
     if (__pyx_t_5) {
-      goto __pyx_L12_break;
+
+      /* "pysam/TabProxies.pyx":232
+ *             pos = <char*>memchr(pos, '\t', nbytes)
+ *             if pos == NULL:
+ *                 break             # <<<<<<<<<<<<<<
+ *             if field >= max_fields:
+ *                 raise ValueError(
+ */
+      goto __pyx_L16_break;
+    }
+
+    /* "pysam/TabProxies.pyx":233
+ *             if pos == NULL:
+ *                 break
+ *             if field >= max_fields:             # <<<<<<<<<<<<<<
+ *                 raise ValueError(
+ *                     "parsing error: more than %i fields in line: %s" %
+ */
+    __pyx_t_5 = ((__pyx_v_field >= __pyx_v_max_fields) != 0);
+    if (__pyx_t_5) {
+
+      /* "pysam/TabProxies.pyx":236
+ *                 raise ValueError(
+ *                     "parsing error: more than %i fields in line: %s" %
+ *                     (max_fields, buffer))             # <<<<<<<<<<<<<<
+ * 
+ *             pos[0] = '\0'
+ */
+      __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_max_fields); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_buffer); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_2);
+      __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_1);
+      PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_3);
+      __Pyx_GIVEREF(__pyx_t_3);
+      PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_2);
+      __Pyx_GIVEREF(__pyx_t_2);
+      __pyx_t_3 = 0;
+      __pyx_t_2 = 0;
+
+      /* "pysam/TabProxies.pyx":235
+ *             if field >= max_fields:
+ *                 raise ValueError(
+ *                     "parsing error: more than %i fields in line: %s" %             # <<<<<<<<<<<<<<
+ *                     (max_fields, buffer))
+ * 
+ */
+      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_parsing_error_more_than_i_fields, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_2);
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+
+      /* "pysam/TabProxies.pyx":234
+ *                 break
+ *             if field >= max_fields:
+ *                 raise ValueError(             # <<<<<<<<<<<<<<
+ *                     "parsing error: more than %i fields in line: %s" %
+ *                     (max_fields, buffer))
+ */
+      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_1);
+      PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
+      __Pyx_GIVEREF(__pyx_t_2);
+      __pyx_t_2 = 0;
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_2);
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      __Pyx_Raise(__pyx_t_2, 0, 0, 0);
+      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/TabProxies.pyx":218
- *             pos = <char*>memchr( pos, '\t', nbytes )
- *             if pos == NULL: break
+    /* "pysam/TabProxies.pyx":238
+ *                     (max_fields, buffer))
+ * 
  *             pos[0] = '\0'             # <<<<<<<<<<<<<<
  *             pos += 1
  *             self.fields[field] = pos
  */
     (__pyx_v_pos[0]) = '\x00';
 
-    /* "pysam/TabProxies.pyx":219
- *             if pos == NULL: break
+    /* "pysam/TabProxies.pyx":239
+ * 
  *             pos[0] = '\0'
  *             pos += 1             # <<<<<<<<<<<<<<
  *             self.fields[field] = pos
@@ -3092,104 +3313,138 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
  */
     __pyx_v_pos = (__pyx_v_pos + 1);
 
-    /* "pysam/TabProxies.pyx":220
+    /* "pysam/TabProxies.pyx":240
  *             pos[0] = '\0'
  *             pos += 1
  *             self.fields[field] = pos             # <<<<<<<<<<<<<<
  *             field += 1
- *             if field > max_fields:
+ *             nbytes -= pos - old_pos
  */
     (__pyx_v_self->fields[__pyx_v_field]) = __pyx_v_pos;
 
-    /* "pysam/TabProxies.pyx":221
+    /* "pysam/TabProxies.pyx":241
  *             pos += 1
  *             self.fields[field] = pos
  *             field += 1             # <<<<<<<<<<<<<<
- *             if field > max_fields:
- *                 raise ValueError("row too large - more than %i fields" % max_fields )
+ *             nbytes -= pos - old_pos
+ *             if nbytes < 0:
  */
     __pyx_v_field = (__pyx_v_field + 1);
 
-    /* "pysam/TabProxies.pyx":222
+    /* "pysam/TabProxies.pyx":242
  *             self.fields[field] = pos
  *             field += 1
- *             if field > max_fields:             # <<<<<<<<<<<<<<
- *                 raise ValueError("row too large - more than %i fields" % max_fields )
- *             nbytes -= pos - old_pos
+ *             nbytes -= pos - old_pos             # <<<<<<<<<<<<<<
+ *             if nbytes < 0:
+ *                 break
  */
-    __pyx_t_5 = ((__pyx_v_field > __pyx_v_max_fields) != 0);
-    if (__pyx_t_5) {
+    __pyx_v_nbytes = (__pyx_v_nbytes - (__pyx_v_pos - __pyx_v_old_pos));
 
-      /* "pysam/TabProxies.pyx":223
+    /* "pysam/TabProxies.pyx":243
  *             field += 1
- *             if field > max_fields:
- *                 raise ValueError("row too large - more than %i fields" % max_fields )             # <<<<<<<<<<<<<<
  *             nbytes -= pos - old_pos
- *             if nbytes < 0: break
- */
-      __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_max_fields); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_row_too_large_more_than_i_fields, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
-      __Pyx_GIVEREF(__pyx_t_2);
-      __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/TabProxies.pyx":224
- *             if field > max_fields:
- *                 raise ValueError("row too large - more than %i fields" % max_fields )
- *             nbytes -= pos - old_pos             # <<<<<<<<<<<<<<
- *             if nbytes < 0: break
+ *             if nbytes < 0:             # <<<<<<<<<<<<<<
+ *                 break
  *             old_pos = pos
  */
-    __pyx_v_nbytes = (__pyx_v_nbytes - (__pyx_v_pos - __pyx_v_old_pos));
+    __pyx_t_5 = ((__pyx_v_nbytes < 0) != 0);
+    if (__pyx_t_5) {
 
-    /* "pysam/TabProxies.pyx":225
- *                 raise ValueError("row too large - more than %i fields" % max_fields )
+      /* "pysam/TabProxies.pyx":244
  *             nbytes -= pos - old_pos
- *             if nbytes < 0: break             # <<<<<<<<<<<<<<
+ *             if nbytes < 0:
+ *                 break             # <<<<<<<<<<<<<<
  *             old_pos = pos
- * 
+ *         self.nfields = field
  */
-    __pyx_t_5 = ((__pyx_v_nbytes < 0) != 0);
-    if (__pyx_t_5) {
-      goto __pyx_L12_break;
+      goto __pyx_L16_break;
     }
 
-    /* "pysam/TabProxies.pyx":226
- *             nbytes -= pos - old_pos
- *             if nbytes < 0: break
+    /* "pysam/TabProxies.pyx":245
+ *             if nbytes < 0:
+ *                 break
  *             old_pos = pos             # <<<<<<<<<<<<<<
- * 
  *         self.nfields = field
+ *         if self.nfields < self.getMinFields():
  */
     __pyx_v_old_pos = __pyx_v_pos;
   }
-  __pyx_L12_break:;
+  __pyx_L16_break:;
 
-  /* "pysam/TabProxies.pyx":228
+  /* "pysam/TabProxies.pyx":246
+ *                 break
  *             old_pos = pos
- * 
  *         self.nfields = field             # <<<<<<<<<<<<<<
- * 
- *     def _getindex( self, int index ):
+ *         if self.nfields < self.getMinFields():
+ *             raise ValueError(
  */
   __pyx_v_self->nfields = __pyx_v_field;
 
-  /* "pysam/TabProxies.pyx":155
- *         return nbytes / 2
+  /* "pysam/TabProxies.pyx":247
+ *             old_pos = pos
+ *         self.nfields = field
+ *         if self.nfields < self.getMinFields():             # <<<<<<<<<<<<<<
+ *             raise ValueError(
+ *                 "parsing error: fewer that %i fields in line: %s" %
+ */
+  __pyx_t_5 = ((__pyx_v_self->nfields < ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->getMinFields(__pyx_v_self)) != 0);
+  if (__pyx_t_5) {
+
+    /* "pysam/TabProxies.pyx":250
+ *             raise ValueError(
+ *                 "parsing error: fewer that %i fields in line: %s" %
+ *                 (self.getMinFields(), buffer))             # <<<<<<<<<<<<<<
+ * 
+ *     def _getindex(self, int index):
+ */
+    __pyx_t_2 = __Pyx_PyInt_From_int(((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_self->__pyx_vtab)->getMinFields(__pyx_v_self)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 250; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_buffer); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 250; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 250; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
+    __Pyx_GIVEREF(__pyx_t_2);
+    PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_1);
+    __Pyx_GIVEREF(__pyx_t_1);
+    __pyx_t_2 = 0;
+    __pyx_t_1 = 0;
+
+    /* "pysam/TabProxies.pyx":249
+ *         if self.nfields < self.getMinFields():
+ *             raise ValueError(
+ *                 "parsing error: fewer that %i fields in line: %s" %             # <<<<<<<<<<<<<<
+ *                 (self.getMinFields(), buffer))
+ * 
+ */
+    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_parsing_error_fewer_that_i_field, __pyx_t_3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+
+    /* "pysam/TabProxies.pyx":248
+ *         self.nfields = field
+ *         if self.nfields < self.getMinFields():
+ *             raise ValueError(             # <<<<<<<<<<<<<<
+ *                 "parsing error: fewer that %i fields in line: %s" %
+ *                 (self.getMinFields(), buffer))
+ */
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
+    __Pyx_GIVEREF(__pyx_t_1);
+    __pyx_t_1 = 0;
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
+
+  /* "pysam/TabProxies.pyx":160
+ *         return 0
  * 
- *     cdef update( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef update(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''update internal data.
  * 
  */
@@ -3209,10 +3464,10 @@ static PyObject *__pyx_f_5pysam_10TabProxies_10TupleProxy_update(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":230
- *         self.nfields = field
+/* "pysam/TabProxies.pyx":252
+ *                 (self.getMinFields(), buffer))
  * 
- *     def _getindex( self, int index ):             # <<<<<<<<<<<<<<
+ *     def _getindex(self, int index):             # <<<<<<<<<<<<<<
  *         '''return item at idx index'''
  *         cdef int i = index
  */
@@ -3229,7 +3484,7 @@ static PyObject *__pyx_pw_5pysam_10TabProxies_10TupleProxy_5_getindex(PyObject *
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("_getindex (wrapper)", 0);
   assert(__pyx_arg_index); {
-    __pyx_v_index = __Pyx_PyInt_As_int(__pyx_arg_index); if (unlikely((__pyx_v_index == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 230; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_index = __Pyx_PyInt_As_int(__pyx_arg_index); if (unlikely((__pyx_v_index == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L3_error:;
@@ -3252,81 +3507,98 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_4_getindex(struct __p
   PyObject *__pyx_t_2 = NULL;
   PyObject *__pyx_t_3 = NULL;
   PyObject *__pyx_t_4 = NULL;
+  struct __pyx_opt_args_5pysam_10TabProxies__force_str __pyx_t_5;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_getindex", 0);
 
-  /* "pysam/TabProxies.pyx":232
- *     def _getindex( self, int index ):
+  /* "pysam/TabProxies.pyx":254
+ *     def _getindex(self, int index):
  *         '''return item at idx index'''
  *         cdef int i = index             # <<<<<<<<<<<<<<
- *         if i < 0: i += self.nfields
- *         if i < 0: raise IndexError( "list index out of range" )
+ *         if i < 0:
+ *             i += self.nfields
  */
   __pyx_v_i = __pyx_v_index;
 
-  /* "pysam/TabProxies.pyx":233
+  /* "pysam/TabProxies.pyx":255
  *         '''return item at idx index'''
  *         cdef int i = index
- *         if i < 0: i += self.nfields             # <<<<<<<<<<<<<<
- *         if i < 0: raise IndexError( "list index out of range" )
- *         i += self.offset
+ *         if i < 0:             # <<<<<<<<<<<<<<
+ *             i += self.nfields
+ *         if i < 0:
  */
   __pyx_t_1 = ((__pyx_v_i < 0) != 0);
   if (__pyx_t_1) {
+
+    /* "pysam/TabProxies.pyx":256
+ *         cdef int i = index
+ *         if i < 0:
+ *             i += self.nfields             # <<<<<<<<<<<<<<
+ *         if i < 0:
+ *             raise IndexError("list index out of range")
+ */
     __pyx_v_i = (__pyx_v_i + __pyx_v_self->nfields);
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":234
- *         cdef int i = index
- *         if i < 0: i += self.nfields
- *         if i < 0: raise IndexError( "list index out of range" )             # <<<<<<<<<<<<<<
- *         i += self.offset
- *         if i >= self.nfields:
+  /* "pysam/TabProxies.pyx":257
+ *         if i < 0:
+ *             i += self.nfields
+ *         if i < 0:             # <<<<<<<<<<<<<<
+ *             raise IndexError("list index out of range")
+ *         # apply offset - separating a fixed number
  */
   __pyx_t_1 = ((__pyx_v_i < 0) != 0);
   if (__pyx_t_1) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/TabProxies.pyx":258
+ *             i += self.nfields
+ *         if i < 0:
+ *             raise IndexError("list index out of range")             # <<<<<<<<<<<<<<
+ *         # apply offset - separating a fixed number
+ *         # of fields from a variable number such as in VCF
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_tuple__4, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":235
- *         if i < 0: i += self.nfields
- *         if i < 0: raise IndexError( "list index out of range" )
+  /* "pysam/TabProxies.pyx":261
+ *         # apply offset - separating a fixed number
+ *         # of fields from a variable number such as in VCF
  *         i += self.offset             # <<<<<<<<<<<<<<
  *         if i >= self.nfields:
- *             raise IndexError( "list index out of range %i >= %i" % (i, self.nfields ))
+ *             raise IndexError(
  */
   __pyx_v_i = (__pyx_v_i + __pyx_v_self->offset);
 
-  /* "pysam/TabProxies.pyx":236
- *         if i < 0: raise IndexError( "list index out of range" )
+  /* "pysam/TabProxies.pyx":262
+ *         # of fields from a variable number such as in VCF
  *         i += self.offset
  *         if i >= self.nfields:             # <<<<<<<<<<<<<<
- *             raise IndexError( "list index out of range %i >= %i" % (i, self.nfields ))
- *         return self.fields[i]
+ *             raise IndexError(
+ *                 "list index out of range %i >= %i" %
  */
   __pyx_t_1 = ((__pyx_v_i >= __pyx_v_self->nfields) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":237
- *         i += self.offset
- *         if i >= self.nfields:
- *             raise IndexError( "list index out of range %i >= %i" % (i, self.nfields ))             # <<<<<<<<<<<<<<
- *         return self.fields[i]
+    /* "pysam/TabProxies.pyx":265
+ *             raise IndexError(
+ *                 "list index out of range %i >= %i" %
+ *                 (i, self.nfields))             # <<<<<<<<<<<<<<
+ *         return _force_str(self.fields[i], self.encoding)
  * 
  */
-    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_i); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_i); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 265; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_self->nfields); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_self->nfields); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 265; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 265; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
@@ -3334,40 +3606,64 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_4_getindex(struct __p
     __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_2 = 0;
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_list_index_out_of_range_i_i, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/TabProxies.pyx":264
+ *         if i >= self.nfields:
+ *             raise IndexError(
+ *                 "list index out of range %i >= %i" %             # <<<<<<<<<<<<<<
+ *                 (i, self.nfields))
+ *         return _force_str(self.fields[i], self.encoding)
+ */
+    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_list_index_out_of_range_i_i, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 264; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/TabProxies.pyx":263
+ *         i += self.offset
+ *         if i >= self.nfields:
+ *             raise IndexError(             # <<<<<<<<<<<<<<
+ *                 "list index out of range %i >= %i" %
+ *                 (i, self.nfields))
+ */
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 263; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_3);
     __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 263; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 263; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":238
- *         if i >= self.nfields:
- *             raise IndexError( "list index out of range %i >= %i" % (i, self.nfields ))
- *         return self.fields[i]             # <<<<<<<<<<<<<<
+  /* "pysam/TabProxies.pyx":266
+ *                 "list index out of range %i >= %i" %
+ *                 (i, self.nfields))
+ *         return _force_str(self.fields[i], self.encoding)             # <<<<<<<<<<<<<<
  * 
- *     def __getitem__( self, key ):
+ *     def __getitem__(self, key):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __Pyx_PyBytes_FromString((__pyx_v_self->fields[__pyx_v_i])); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyBytes_FromString((__pyx_v_self->fields[__pyx_v_i])); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
+  __pyx_t_4 = __pyx_v_self->encoding;
+  __Pyx_INCREF(__pyx_t_4);
+  __pyx_t_5.__pyx_n = 1;
+  __pyx_t_5.encoding = __pyx_t_4;
+  __pyx_t_2 = __pyx_f_5pysam_10TabProxies__force_str(__pyx_t_3, &__pyx_t_5); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":230
- *         self.nfields = field
+  /* "pysam/TabProxies.pyx":252
+ *                 (self.getMinFields(), buffer))
  * 
- *     def _getindex( self, int index ):             # <<<<<<<<<<<<<<
+ *     def _getindex(self, int index):             # <<<<<<<<<<<<<<
  *         '''return item at idx index'''
  *         cdef int i = index
  */
@@ -3385,12 +3681,12 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_4_getindex(struct __p
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":240
- *         return self.fields[i]
+/* "pysam/TabProxies.pyx":268
+ *         return _force_str(self.fields[i], self.encoding)
  * 
- *     def __getitem__( self, key ):             # <<<<<<<<<<<<<<
- *         if type(key) == int: return self._getindex( key )
- *         # slice object
+ *     def __getitem__(self, key):             # <<<<<<<<<<<<<<
+ *         if type(key) == int:
+ *             return self._getindex(key)
  */
 
 /* Python wrapper */
@@ -3429,26 +3725,34 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__getitem__", 0);
 
-  /* "pysam/TabProxies.pyx":241
+  /* "pysam/TabProxies.pyx":269
  * 
- *     def __getitem__( self, key ):
- *         if type(key) == int: return self._getindex( key )             # <<<<<<<<<<<<<<
+ *     def __getitem__(self, key):
+ *         if type(key) == int:             # <<<<<<<<<<<<<<
+ *             return self._getindex(key)
  *         # slice object
- *         start, end, step = key.indices( self.nfields )
  */
-  __pyx_t_1 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_v_key)), ((PyObject *)((PyObject*)(&PyInt_Type))), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_v_key)), ((PyObject *)((PyObject*)(&PyInt_Type))), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 269; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 269; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_2) {
+
+    /* "pysam/TabProxies.pyx":270
+ *     def __getitem__(self, key):
+ *         if type(key) == int:
+ *             return self._getindex(key)             # <<<<<<<<<<<<<<
+ *         # slice object
+ *         start, end, step = key.indices(self.nfields)
+ */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_key);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_key);
     __Pyx_GIVEREF(__pyx_v_key);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -3457,23 +3761,23 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":243
- *         if type(key) == int: return self._getindex( key )
+  /* "pysam/TabProxies.pyx":272
+ *             return self._getindex(key)
  *         # slice object
- *         start, end, step = key.indices( self.nfields )             # <<<<<<<<<<<<<<
+ *         start, end, step = key.indices(self.nfields)             # <<<<<<<<<<<<<<
  *         result = []
- *         for index in range( start, end, step ):
+ *         for index in range(start, end, step):
  */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_indices); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_indices); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_self->nfields); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_self->nfields); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_3);
   __Pyx_GIVEREF(__pyx_t_3);
   __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -3487,7 +3791,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
     if (unlikely(size != 3)) {
       if (size > 3) __Pyx_RaiseTooManyValuesError(3);
       else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     #if CYTHON_COMPILING_IN_CPYTHON
     if (likely(PyTuple_CheckExact(sequence))) {
@@ -3503,17 +3807,17 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
     __Pyx_INCREF(__pyx_t_4);
     __Pyx_INCREF(__pyx_t_5);
     #else
-    __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_4 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = PySequence_ITEM(sequence, 2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PySequence_ITEM(sequence, 2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     #endif
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   } else {
     Py_ssize_t index = -1;
-    __pyx_t_6 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __pyx_t_7 = Py_TYPE(__pyx_t_6)->tp_iternext;
@@ -3523,7 +3827,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
     __Pyx_GOTREF(__pyx_t_4);
     index = 2; __pyx_t_5 = __pyx_t_7(__pyx_t_6); if (unlikely(!__pyx_t_5)) goto __pyx_L4_unpacking_failed;
     __Pyx_GOTREF(__pyx_t_5);
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_7(__pyx_t_6), 3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_IternextUnpackEndCheck(__pyx_t_7(__pyx_t_6), 3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_7 = NULL;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     goto __pyx_L5_unpacking_done;
@@ -3531,7 +3835,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __pyx_t_7 = NULL;
     if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_L5_unpacking_done:;
   }
   __pyx_v_start = __pyx_t_1;
@@ -3541,26 +3845,26 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
   __pyx_v_step = __pyx_t_5;
   __pyx_t_5 = 0;
 
-  /* "pysam/TabProxies.pyx":244
+  /* "pysam/TabProxies.pyx":273
  *         # slice object
- *         start, end, step = key.indices( self.nfields )
+ *         start, end, step = key.indices(self.nfields)
  *         result = []             # <<<<<<<<<<<<<<
- *         for index in range( start, end, step ):
- *             result.append( self._getindex( index ) )
+ *         for index in range(start, end, step):
+ *             result.append(self._getindex(index))
  */
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 273; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __pyx_v_result = ((PyObject*)__pyx_t_3);
   __pyx_t_3 = 0;
 
-  /* "pysam/TabProxies.pyx":245
- *         start, end, step = key.indices( self.nfields )
+  /* "pysam/TabProxies.pyx":274
+ *         start, end, step = key.indices(self.nfields)
  *         result = []
- *         for index in range( start, end, step ):             # <<<<<<<<<<<<<<
- *             result.append( self._getindex( index ) )
+ *         for index in range(start, end, step):             # <<<<<<<<<<<<<<
+ *             result.append(self._getindex(index))
  *         return result
  */
-  __pyx_t_3 = PyTuple_New(3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_v_start);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_start);
@@ -3571,14 +3875,14 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
   __Pyx_INCREF(__pyx_v_step);
   PyTuple_SET_ITEM(__pyx_t_3, 2, __pyx_v_step);
   __Pyx_GIVEREF(__pyx_v_step);
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_range, __pyx_t_3, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_range, __pyx_t_3, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if (PyList_CheckExact(__pyx_t_5) || PyTuple_CheckExact(__pyx_t_5)) {
     __pyx_t_3 = __pyx_t_5; __Pyx_INCREF(__pyx_t_3); __pyx_t_8 = 0;
     __pyx_t_9 = NULL;
   } else {
-    __pyx_t_8 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_t_9 = Py_TYPE(__pyx_t_3)->tp_iternext;
   }
@@ -3587,16 +3891,16 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
     if (!__pyx_t_9 && PyList_CheckExact(__pyx_t_3)) {
       if (__pyx_t_8 >= PyList_GET_SIZE(__pyx_t_3)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_5 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_8); __Pyx_INCREF(__pyx_t_5); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_8); __Pyx_INCREF(__pyx_t_5); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_5 = PySequence_ITEM(__pyx_t_3, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PySequence_ITEM(__pyx_t_3, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_9 && PyTuple_CheckExact(__pyx_t_3)) {
       if (__pyx_t_8 >= PyTuple_GET_SIZE(__pyx_t_3)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_5 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_8); __Pyx_INCREF(__pyx_t_5); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_8); __Pyx_INCREF(__pyx_t_5); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_5 = PySequence_ITEM(__pyx_t_3, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PySequence_ITEM(__pyx_t_3, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_5 = __pyx_t_9(__pyx_t_3);
@@ -3604,7 +3908,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -3613,47 +3917,47 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
     __Pyx_XDECREF_SET(__pyx_v_index, __pyx_t_5);
     __pyx_t_5 = 0;
 
-    /* "pysam/TabProxies.pyx":246
+    /* "pysam/TabProxies.pyx":275
  *         result = []
- *         for index in range( start, end, step ):
- *             result.append( self._getindex( index ) )             # <<<<<<<<<<<<<<
+ *         for index in range(start, end, step):
+ *             result.append(self._getindex(index))             # <<<<<<<<<<<<<<
  *         return result
  * 
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_INCREF(__pyx_v_index);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_index);
     __Pyx_GIVEREF(__pyx_v_index);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_10 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_1); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_1); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   }
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/TabProxies.pyx":247
- *         for index in range( start, end, step ):
- *             result.append( self._getindex( index ) )
+  /* "pysam/TabProxies.pyx":276
+ *         for index in range(start, end, step):
+ *             result.append(self._getindex(index))
  *         return result             # <<<<<<<<<<<<<<
  * 
- *     def _setindex( self, index, value ):
+ *     def _setindex(self, index, value):
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(__pyx_v_result);
   __pyx_r = __pyx_v_result;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":240
- *         return self.fields[i]
+  /* "pysam/TabProxies.pyx":268
+ *         return _force_str(self.fields[i], self.encoding)
  * 
- *     def __getitem__( self, key ):             # <<<<<<<<<<<<<<
- *         if type(key) == int: return self._getindex( key )
- *         # slice object
+ *     def __getitem__(self, key):             # <<<<<<<<<<<<<<
+ *         if type(key) == int:
+ *             return self._getindex(key)
  */
 
   /* function exit code */
@@ -3676,10 +3980,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_6__getitem__(struct _
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":249
+/* "pysam/TabProxies.pyx":278
  *         return result
  * 
- *     def _setindex( self, index, value ):             # <<<<<<<<<<<<<<
+ *     def _setindex(self, index, value):             # <<<<<<<<<<<<<<
  *         '''set item at idx index.'''
  *         cdef int idx = index
  */
@@ -3716,11 +4020,11 @@ static PyObject *__pyx_pw_5pysam_10TabProxies_10TupleProxy_9_setindex(PyObject *
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("_setindex", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("_setindex", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 278; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_setindex") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_setindex") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 278; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -3733,7 +4037,7 @@ static PyObject *__pyx_pw_5pysam_10TabProxies_10TupleProxy_9_setindex(PyObject *
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_setindex", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("_setindex", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 278; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.TabProxies.TupleProxy._setindex", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -3754,77 +4058,86 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_8_setindex(struct __p
   int __pyx_t_1;
   int __pyx_t_2;
   PyObject *__pyx_t_3 = NULL;
-  char *__pyx_t_4;
+  int __pyx_t_4;
+  char *__pyx_t_5;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_setindex", 0);
   __Pyx_INCREF(__pyx_v_value);
 
-  /* "pysam/TabProxies.pyx":251
- *     def _setindex( self, index, value ):
+  /* "pysam/TabProxies.pyx":280
+ *     def _setindex(self, index, value):
  *         '''set item at idx index.'''
  *         cdef int idx = index             # <<<<<<<<<<<<<<
- *         if idx < 0: raise IndexError( "list index out of range" )
- *         if idx >= self.nfields:
+ *         if idx < 0:
+ *             raise IndexError("list index out of range")
  */
-  __pyx_t_1 = __Pyx_PyInt_As_int(__pyx_v_index); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_As_int(__pyx_v_index); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 280; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_idx = __pyx_t_1;
 
-  /* "pysam/TabProxies.pyx":252
+  /* "pysam/TabProxies.pyx":281
  *         '''set item at idx index.'''
  *         cdef int idx = index
- *         if idx < 0: raise IndexError( "list index out of range" )             # <<<<<<<<<<<<<<
+ *         if idx < 0:             # <<<<<<<<<<<<<<
+ *             raise IndexError("list index out of range")
  *         if idx >= self.nfields:
- *             raise IndexError( "list index out of range" )
  */
   __pyx_t_2 = ((__pyx_v_idx < 0) != 0);
   if (__pyx_t_2) {
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/TabProxies.pyx":282
+ *         cdef int idx = index
+ *         if idx < 0:
+ *             raise IndexError("list index out of range")             # <<<<<<<<<<<<<<
+ *         if idx >= self.nfields:
+ *             raise IndexError("list index out of range")
+ */
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":253
- *         cdef int idx = index
- *         if idx < 0: raise IndexError( "list index out of range" )
+  /* "pysam/TabProxies.pyx":283
+ *         if idx < 0:
+ *             raise IndexError("list index out of range")
  *         if idx >= self.nfields:             # <<<<<<<<<<<<<<
- *             raise IndexError( "list index out of range" )
+ *             raise IndexError("list index out of range")
  * 
  */
   __pyx_t_2 = ((__pyx_v_idx >= __pyx_v_self->nfields) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/TabProxies.pyx":254
- *         if idx < 0: raise IndexError( "list index out of range" )
+    /* "pysam/TabProxies.pyx":284
+ *             raise IndexError("list index out of range")
  *         if idx >= self.nfields:
- *             raise IndexError( "list index out of range" )             # <<<<<<<<<<<<<<
+ *             raise IndexError("list index out of range")             # <<<<<<<<<<<<<<
  * 
- *         if isNew( self.fields[idx], self.data, self.nbytes ):
+ *         if isNew(self.fields[idx], self.data, self.nbytes):
  */
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_tuple__8, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 284; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 284; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":256
- *             raise IndexError( "list index out of range" )
+  /* "pysam/TabProxies.pyx":286
+ *             raise IndexError("list index out of range")
  * 
- *         if isNew( self.fields[idx], self.data, self.nbytes ):             # <<<<<<<<<<<<<<
- *             free( self.fields[idx] )
+ *         if isNew(self.fields[idx], self.data, self.nbytes):             # <<<<<<<<<<<<<<
+ *             free(self.fields[idx] )
  * 
  */
   __pyx_t_2 = (__pyx_f_5pysam_10TabProxies_isNew((__pyx_v_self->fields[__pyx_v_idx]), __pyx_v_self->data, __pyx_v_self->nbytes) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/TabProxies.pyx":257
+    /* "pysam/TabProxies.pyx":287
  * 
- *         if isNew( self.fields[idx], self.data, self.nbytes ):
- *             free( self.fields[idx] )             # <<<<<<<<<<<<<<
+ *         if isNew(self.fields[idx], self.data, self.nbytes):
+ *             free(self.fields[idx] )             # <<<<<<<<<<<<<<
  * 
  *         self.is_modified = 1
  */
@@ -3833,38 +4146,37 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_8_setindex(struct __p
   }
   __pyx_L5:;
 
-  /* "pysam/TabProxies.pyx":259
- *             free( self.fields[idx] )
+  /* "pysam/TabProxies.pyx":289
+ *             free(self.fields[idx] )
  * 
  *         self.is_modified = 1             # <<<<<<<<<<<<<<
  * 
- *         if value == None:
+ *         if value is None:
  */
   __pyx_v_self->is_modified = 1;
 
-  /* "pysam/TabProxies.pyx":261
+  /* "pysam/TabProxies.pyx":291
  *         self.is_modified = 1
  * 
- *         if value == None:             # <<<<<<<<<<<<<<
+ *         if value is None:             # <<<<<<<<<<<<<<
  *             self.fields[idx] = NULL
  *             return
  */
-  __pyx_t_3 = PyObject_RichCompare(__pyx_v_value, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 261; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 261; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  if (__pyx_t_2) {
+  __pyx_t_2 = (__pyx_v_value == Py_None);
+  __pyx_t_4 = (__pyx_t_2 != 0);
+  if (__pyx_t_4) {
 
-    /* "pysam/TabProxies.pyx":262
+    /* "pysam/TabProxies.pyx":292
  * 
- *         if value == None:
+ *         if value is None:
  *             self.fields[idx] = NULL             # <<<<<<<<<<<<<<
  *             return
  * 
  */
     (__pyx_v_self->fields[__pyx_v_idx]) = NULL;
 
-    /* "pysam/TabProxies.pyx":263
- *         if value == None:
+    /* "pysam/TabProxies.pyx":293
+ *         if value is None:
  *             self.fields[idx] = NULL
  *             return             # <<<<<<<<<<<<<<
  * 
@@ -3875,74 +4187,74 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_8_setindex(struct __p
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":266
+  /* "pysam/TabProxies.pyx":296
  * 
  *         # conversion with error checking
  *         value = _force_bytes(value)             # <<<<<<<<<<<<<<
  *         cdef char * tmp = <char*>value
- *         self.fields[idx] = <char*>malloc( (strlen( tmp ) + 1) * sizeof(char) )
+ *         self.fields[idx] = <char*>malloc((strlen( tmp ) + 1) * sizeof(char))
  */
-  __pyx_t_3 = __pyx_f_5pysam_10TabProxies__force_bytes(__pyx_v_value); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __pyx_f_5pysam_10TabProxies__force_bytes(__pyx_v_value, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_3);
   __pyx_t_3 = 0;
 
-  /* "pysam/TabProxies.pyx":267
+  /* "pysam/TabProxies.pyx":297
  *         # conversion with error checking
  *         value = _force_bytes(value)
  *         cdef char * tmp = <char*>value             # <<<<<<<<<<<<<<
- *         self.fields[idx] = <char*>malloc( (strlen( tmp ) + 1) * sizeof(char) )
+ *         self.fields[idx] = <char*>malloc((strlen( tmp ) + 1) * sizeof(char))
  *         if self.fields[idx] == NULL:
  */
-  __pyx_t_4 = __Pyx_PyObject_AsString(__pyx_v_value); if (unlikely((!__pyx_t_4) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_tmp = ((char *)__pyx_t_4);
+  __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_value); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_tmp = ((char *)__pyx_t_5);
 
-  /* "pysam/TabProxies.pyx":268
+  /* "pysam/TabProxies.pyx":298
  *         value = _force_bytes(value)
  *         cdef char * tmp = <char*>value
- *         self.fields[idx] = <char*>malloc( (strlen( tmp ) + 1) * sizeof(char) )             # <<<<<<<<<<<<<<
+ *         self.fields[idx] = <char*>malloc((strlen( tmp ) + 1) * sizeof(char))             # <<<<<<<<<<<<<<
  *         if self.fields[idx] == NULL:
  *             raise ValueError("out of memory" )
  */
   (__pyx_v_self->fields[__pyx_v_idx]) = ((char *)malloc(((strlen(__pyx_v_tmp) + 1) * (sizeof(char)))));
 
-  /* "pysam/TabProxies.pyx":269
+  /* "pysam/TabProxies.pyx":299
  *         cdef char * tmp = <char*>value
- *         self.fields[idx] = <char*>malloc( (strlen( tmp ) + 1) * sizeof(char) )
+ *         self.fields[idx] = <char*>malloc((strlen( tmp ) + 1) * sizeof(char))
  *         if self.fields[idx] == NULL:             # <<<<<<<<<<<<<<
  *             raise ValueError("out of memory" )
- *         strcpy( self.fields[idx], tmp )
+ *         strcpy(self.fields[idx], tmp)
  */
-  __pyx_t_2 = (((__pyx_v_self->fields[__pyx_v_idx]) == NULL) != 0);
-  if (__pyx_t_2) {
+  __pyx_t_4 = (((__pyx_v_self->fields[__pyx_v_idx]) == NULL) != 0);
+  if (__pyx_t_4) {
 
-    /* "pysam/TabProxies.pyx":270
- *         self.fields[idx] = <char*>malloc( (strlen( tmp ) + 1) * sizeof(char) )
+    /* "pysam/TabProxies.pyx":300
+ *         self.fields[idx] = <char*>malloc((strlen( tmp ) + 1) * sizeof(char))
  *         if self.fields[idx] == NULL:
  *             raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
- *         strcpy( self.fields[idx], tmp )
+ *         strcpy(self.fields[idx], tmp)
  * 
  */
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":271
+  /* "pysam/TabProxies.pyx":301
  *         if self.fields[idx] == NULL:
  *             raise ValueError("out of memory" )
- *         strcpy( self.fields[idx], tmp )             # <<<<<<<<<<<<<<
+ *         strcpy(self.fields[idx], tmp)             # <<<<<<<<<<<<<<
  * 
- *     def __setitem__(self, index, value ):
+ *     def __setitem__(self, index, value):
  */
   strcpy((__pyx_v_self->fields[__pyx_v_idx]), __pyx_v_tmp);
 
-  /* "pysam/TabProxies.pyx":249
+  /* "pysam/TabProxies.pyx":278
  *         return result
  * 
- *     def _setindex( self, index, value ):             # <<<<<<<<<<<<<<
+ *     def _setindex(self, index, value):             # <<<<<<<<<<<<<<
  *         '''set item at idx index.'''
  *         cdef int idx = index
  */
@@ -3961,10 +4273,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_8_setindex(struct __p
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":273
- *         strcpy( self.fields[idx], tmp )
+/* "pysam/TabProxies.pyx":303
+ *         strcpy(self.fields[idx], tmp)
  * 
- *     def __setitem__(self, index, value ):             # <<<<<<<<<<<<<<
+ *     def __setitem__(self, index, value):             # <<<<<<<<<<<<<<
  *         '''set item at *index* to *value*'''
  *         cdef int i = index
  */
@@ -4000,51 +4312,59 @@ static int __pyx_pf_5pysam_10TabProxies_10TupleProxy_10__setitem__(struct __pyx_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__setitem__", 0);
 
-  /* "pysam/TabProxies.pyx":275
- *     def __setitem__(self, index, value ):
+  /* "pysam/TabProxies.pyx":305
+ *     def __setitem__(self, index, value):
  *         '''set item at *index* to *value*'''
  *         cdef int i = index             # <<<<<<<<<<<<<<
- *         if i < 0: i += self.nfields
- *         i += self.offset
+ *         if i < 0:
+ *             i += self.nfields
  */
-  __pyx_t_1 = __Pyx_PyInt_As_int(__pyx_v_index); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_As_int(__pyx_v_index); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 305; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_i = __pyx_t_1;
 
-  /* "pysam/TabProxies.pyx":276
+  /* "pysam/TabProxies.pyx":306
  *         '''set item at *index* to *value*'''
  *         cdef int i = index
- *         if i < 0: i += self.nfields             # <<<<<<<<<<<<<<
+ *         if i < 0:             # <<<<<<<<<<<<<<
+ *             i += self.nfields
  *         i += self.offset
- * 
  */
   __pyx_t_2 = ((__pyx_v_i < 0) != 0);
   if (__pyx_t_2) {
+
+    /* "pysam/TabProxies.pyx":307
+ *         cdef int i = index
+ *         if i < 0:
+ *             i += self.nfields             # <<<<<<<<<<<<<<
+ *         i += self.offset
+ * 
+ */
     __pyx_v_i = (__pyx_v_i + __pyx_v_self->nfields);
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":277
- *         cdef int i = index
- *         if i < 0: i += self.nfields
+  /* "pysam/TabProxies.pyx":308
+ *         if i < 0:
+ *             i += self.nfields
  *         i += self.offset             # <<<<<<<<<<<<<<
  * 
- *         self._setindex( i, value )
+ *         self._setindex(i, value)
  */
   __pyx_v_i = (__pyx_v_i + __pyx_v_self->offset);
 
-  /* "pysam/TabProxies.pyx":279
+  /* "pysam/TabProxies.pyx":310
  *         i += self.offset
  * 
- *         self._setindex( i, value )             # <<<<<<<<<<<<<<
+ *         self._setindex(i, value)             # <<<<<<<<<<<<<<
  * 
  *     def __len__(self):
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 310; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_i); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_i); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 310; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 310; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4);
   __Pyx_GIVEREF(__pyx_t_4);
@@ -4052,16 +4372,16 @@ static int __pyx_pf_5pysam_10TabProxies_10TupleProxy_10__setitem__(struct __pyx_
   PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
   __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 310; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/TabProxies.pyx":273
- *         strcpy( self.fields[idx], tmp )
+  /* "pysam/TabProxies.pyx":303
+ *         strcpy(self.fields[idx], tmp)
  * 
- *     def __setitem__(self, index, value ):             # <<<<<<<<<<<<<<
+ *     def __setitem__(self, index, value):             # <<<<<<<<<<<<<<
  *         '''set item at *index* to *value*'''
  *         cdef int i = index
  */
@@ -4080,8 +4400,8 @@ static int __pyx_pf_5pysam_10TabProxies_10TupleProxy_10__setitem__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":281
- *         self._setindex( i, value )
+/* "pysam/TabProxies.pyx":312
+ *         self._setindex(i, value)
  * 
  *     def __len__(self):             # <<<<<<<<<<<<<<
  *         return self.nfields
@@ -4106,7 +4426,7 @@ static Py_ssize_t __pyx_pf_5pysam_10TabProxies_10TupleProxy_12__len__(struct __p
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__len__", 0);
 
-  /* "pysam/TabProxies.pyx":282
+  /* "pysam/TabProxies.pyx":313
  * 
  *     def __len__(self):
  *         return self.nfields             # <<<<<<<<<<<<<<
@@ -4116,8 +4436,8 @@ static Py_ssize_t __pyx_pf_5pysam_10TabProxies_10TupleProxy_12__len__(struct __p
   __pyx_r = __pyx_v_self->nfields;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":281
- *         self._setindex( i, value )
+  /* "pysam/TabProxies.pyx":312
+ *         self._setindex(i, value)
  * 
  *     def __len__(self):             # <<<<<<<<<<<<<<
  *         return self.nfields
@@ -4130,7 +4450,7 @@ static Py_ssize_t __pyx_pf_5pysam_10TabProxies_10TupleProxy_12__len__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":284
+/* "pysam/TabProxies.pyx":315
  *         return self.nfields
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -4156,7 +4476,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_14__iter__(struct __p
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
 
-  /* "pysam/TabProxies.pyx":285
+  /* "pysam/TabProxies.pyx":316
  * 
  *     def __iter__(self):
  *         self.index = 0             # <<<<<<<<<<<<<<
@@ -4165,7 +4485,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_14__iter__(struct __p
  */
   __pyx_v_self->index = 0;
 
-  /* "pysam/TabProxies.pyx":286
+  /* "pysam/TabProxies.pyx":317
  *     def __iter__(self):
  *         self.index = 0
  *         return self             # <<<<<<<<<<<<<<
@@ -4177,7 +4497,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_14__iter__(struct __p
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":284
+  /* "pysam/TabProxies.pyx":315
  *         return self.nfields
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -4192,7 +4512,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_14__iter__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":288
+/* "pysam/TabProxies.pyx":319
  *         return self
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -4223,12 +4543,15 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_16__next__(struct __p
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
+  struct __pyx_opt_args_5pysam_10TabProxies__force_str __pyx_t_5;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__next__", 0);
 
-  /* "pysam/TabProxies.pyx":291
+  /* "pysam/TabProxies.pyx":322
  *         """python version of next().
  *         """
  *         if self.index >= self.nfields:             # <<<<<<<<<<<<<<
@@ -4238,7 +4561,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_16__next__(struct __p
   __pyx_t_1 = ((__pyx_v_self->index >= __pyx_v_self->nfields) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":292
+    /* "pysam/TabProxies.pyx":323
  *         """
  *         if self.index >= self.nfields:
  *             raise StopIteration             # <<<<<<<<<<<<<<
@@ -4246,36 +4569,44 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_16__next__(struct __p
  *         self.index += 1
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":293
+  /* "pysam/TabProxies.pyx":324
  *         if self.index >= self.nfields:
  *             raise StopIteration
  *         cdef char * retval = self.fields[self.index]             # <<<<<<<<<<<<<<
  *         self.index += 1
- *         if retval == NULL: return None
+ *         if retval == NULL:
  */
   __pyx_v_retval = (__pyx_v_self->fields[__pyx_v_self->index]);
 
-  /* "pysam/TabProxies.pyx":294
+  /* "pysam/TabProxies.pyx":325
  *             raise StopIteration
  *         cdef char * retval = self.fields[self.index]
  *         self.index += 1             # <<<<<<<<<<<<<<
- *         if retval == NULL: return None
- *         else: return retval
+ *         if retval == NULL:
+ *             return None
  */
   __pyx_v_self->index = (__pyx_v_self->index + 1);
 
-  /* "pysam/TabProxies.pyx":295
+  /* "pysam/TabProxies.pyx":326
  *         cdef char * retval = self.fields[self.index]
  *         self.index += 1
- *         if retval == NULL: return None             # <<<<<<<<<<<<<<
- *         else: return retval
- * 
+ *         if retval == NULL:             # <<<<<<<<<<<<<<
+ *             return None
+ *         else:
  */
   __pyx_t_1 = ((__pyx_v_retval == NULL) != 0);
   if (__pyx_t_1) {
+
+    /* "pysam/TabProxies.pyx":327
+ *         self.index += 1
+ *         if retval == NULL:
+ *             return None             # <<<<<<<<<<<<<<
+ *         else:
+ *             return _force_str(retval, self.encoding)
+ */
     __Pyx_XDECREF(__pyx_r);
     __Pyx_INCREF(Py_None);
     __pyx_r = Py_None;
@@ -4283,22 +4614,30 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_16__next__(struct __p
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":296
- *         self.index += 1
- *         if retval == NULL: return None
- *         else: return retval             # <<<<<<<<<<<<<<
+    /* "pysam/TabProxies.pyx":329
+ *             return None
+ *         else:
+ *             return _force_str(retval, self.encoding)             # <<<<<<<<<<<<<<
  * 
  *     def __str__(self):
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_retval); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_retval); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
+    __pyx_t_3 = __pyx_v_self->encoding;
+    __Pyx_INCREF(__pyx_t_3);
+    __pyx_t_5.__pyx_n = 1;
+    __pyx_t_5.encoding = __pyx_t_3;
+    __pyx_t_4 = __pyx_f_5pysam_10TabProxies__force_str(__pyx_t_2, &__pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __pyx_r = __pyx_t_4;
+    __pyx_t_4 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":288
+  /* "pysam/TabProxies.pyx":319
  *         return self
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -4309,6 +4648,8 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_16__next__(struct __p
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
   __Pyx_AddTraceback("pysam.TabProxies.TupleProxy.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
@@ -4317,8 +4658,8 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_16__next__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":298
- *         else: return retval
+/* "pysam/TabProxies.pyx":331
+ *             return _force_str(retval, self.encoding)
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
  *         '''return original data'''
@@ -4346,21 +4687,22 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_18__str__(struct __py
   PyObject *__pyx_v_result = NULL;
   long __pyx_v_x;
   char *__pyx_v_cpy;
+  PyObject *__pyx_v_r = NULL;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   PyObject *__pyx_t_2 = NULL;
   int __pyx_t_3;
   long __pyx_t_4;
-  char *__pyx_t_5;
-  int __pyx_t_6;
-  PyObject *__pyx_t_7 = NULL;
+  PyObject *__pyx_t_5 = NULL;
+  PyObject *__pyx_t_6 = NULL;
+  int __pyx_t_7;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__str__", 0);
 
-  /* "pysam/TabProxies.pyx":301
+  /* "pysam/TabProxies.pyx":334
  *         '''return original data'''
  *         # copy and replace \0 bytes with \t characters
  *         if self.is_modified:             # <<<<<<<<<<<<<<
@@ -4370,167 +4712,202 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_18__str__(struct __py
   __pyx_t_1 = (__pyx_v_self->is_modified != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":303
+    /* "pysam/TabProxies.pyx":336
  *         if self.is_modified:
  *             # todo: treat NULL values
  *             result = []             # <<<<<<<<<<<<<<
- *             for x in xrange( 0, self.nfields ):
- *                 result.append( StrOrEmpty( self.fields[x]).decode('ascii') )
+ *             for x in xrange(0, self.nfields):
+ *                 result.append(StrOrEmpty(self.fields[x]).decode(self.encoding))
  */
-    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_v_result = __pyx_t_2;
     __pyx_t_2 = 0;
 
-    /* "pysam/TabProxies.pyx":304
+    /* "pysam/TabProxies.pyx":337
  *             # todo: treat NULL values
  *             result = []
- *             for x in xrange( 0, self.nfields ):             # <<<<<<<<<<<<<<
- *                 result.append( StrOrEmpty( self.fields[x]).decode('ascii') )
- *             return "\t".join( result )
+ *             for x in xrange(0, self.nfields):             # <<<<<<<<<<<<<<
+ *                 result.append(StrOrEmpty(self.fields[x]).decode(self.encoding))
+ *             return "\t".join(result)
  */
     __pyx_t_3 = __pyx_v_self->nfields;
     for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {
       __pyx_v_x = __pyx_t_4;
 
-      /* "pysam/TabProxies.pyx":305
+      /* "pysam/TabProxies.pyx":338
  *             result = []
- *             for x in xrange( 0, self.nfields ):
- *                 result.append( StrOrEmpty( self.fields[x]).decode('ascii') )             # <<<<<<<<<<<<<<
- *             return "\t".join( result )
+ *             for x in xrange(0, self.nfields):
+ *                 result.append(StrOrEmpty(self.fields[x]).decode(self.encoding))             # <<<<<<<<<<<<<<
+ *             return "\t".join(result)
  *         else:
  */
-      __pyx_t_5 = __pyx_f_5pysam_10TabProxies_StrOrEmpty((__pyx_v_self->fields[__pyx_v_x]));
-      __pyx_t_2 = __Pyx_decode_c_string(__pyx_t_5, 0, strlen(__pyx_t_5), NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 305; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_f_5pysam_10TabProxies_StrOrEmpty((__pyx_v_self->fields[__pyx_v_x]))); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_2);
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_decode); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
+      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_6 = __Pyx_PyObject_Append(__pyx_v_result, __pyx_t_2); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 305; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_INCREF(__pyx_v_self->encoding);
+      PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_self->encoding);
+      __Pyx_GIVEREF(__pyx_v_self->encoding);
+      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_2, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_6);
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+      __pyx_t_7 = __Pyx_PyObject_Append(__pyx_v_result, __pyx_t_6); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     }
 
-    /* "pysam/TabProxies.pyx":306
- *             for x in xrange( 0, self.nfields ):
- *                 result.append( StrOrEmpty( self.fields[x]).decode('ascii') )
- *             return "\t".join( result )             # <<<<<<<<<<<<<<
+    /* "pysam/TabProxies.pyx":339
+ *             for x in xrange(0, self.nfields):
+ *                 result.append(StrOrEmpty(self.fields[x]).decode(self.encoding))
+ *             return "\t".join(result)             # <<<<<<<<<<<<<<
  *         else:
- *             cpy = <char*>calloc( sizeof(char), self.nbytes+1 )
+ *             cpy = <char*>calloc(sizeof(char), self.nbytes+1)
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__10, __pyx_v_result); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 306; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
+    __pyx_t_6 = __Pyx_PyString_Join(__pyx_kp_s__8, __pyx_v_result); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_6);
+    __pyx_r = __pyx_t_6;
+    __pyx_t_6 = 0;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":308
- *             return "\t".join( result )
+    /* "pysam/TabProxies.pyx":341
+ *             return "\t".join(result)
  *         else:
- *             cpy = <char*>calloc( sizeof(char), self.nbytes+1 )             # <<<<<<<<<<<<<<
+ *             cpy = <char*>calloc(sizeof(char), self.nbytes+1)             # <<<<<<<<<<<<<<
  *             if cpy == NULL:
- *                 raise ValueError("out of memory" )
+ *                 raise ValueError("out of memory")
  */
     __pyx_v_cpy = ((char *)calloc((sizeof(char)), (__pyx_v_self->nbytes + 1)));
 
-    /* "pysam/TabProxies.pyx":309
+    /* "pysam/TabProxies.pyx":342
  *         else:
- *             cpy = <char*>calloc( sizeof(char), self.nbytes+1 )
+ *             cpy = <char*>calloc(sizeof(char), self.nbytes+1)
  *             if cpy == NULL:             # <<<<<<<<<<<<<<
- *                 raise ValueError("out of memory" )
- *             memcpy( cpy, self.data, self.nbytes+1)
+ *                 raise ValueError("out of memory")
+ *             memcpy(cpy, self.data, self.nbytes+1)
  */
     __pyx_t_1 = ((__pyx_v_cpy == NULL) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/TabProxies.pyx":310
- *             cpy = <char*>calloc( sizeof(char), self.nbytes+1 )
+      /* "pysam/TabProxies.pyx":343
+ *             cpy = <char*>calloc(sizeof(char), self.nbytes+1)
  *             if cpy == NULL:
- *                 raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
- *             memcpy( cpy, self.data, self.nbytes+1)
+ *                 raise ValueError("out of memory")             # <<<<<<<<<<<<<<
+ *             memcpy(cpy, self.data, self.nbytes+1)
  *             for x from 0 <= x < self.nbytes:
  */
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__11, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 310; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 310; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_6);
+      __Pyx_Raise(__pyx_t_6, 0, 0, 0);
+      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/TabProxies.pyx":311
+    /* "pysam/TabProxies.pyx":344
  *             if cpy == NULL:
- *                 raise ValueError("out of memory" )
- *             memcpy( cpy, self.data, self.nbytes+1)             # <<<<<<<<<<<<<<
+ *                 raise ValueError("out of memory")
+ *             memcpy(cpy, self.data, self.nbytes+1)             # <<<<<<<<<<<<<<
  *             for x from 0 <= x < self.nbytes:
- *                 if cpy[x] == '\0': cpy[x] = '\t'
+ *                 if cpy[x] == '\0':
  */
     memcpy(__pyx_v_cpy, __pyx_v_self->data, (__pyx_v_self->nbytes + 1));
 
-    /* "pysam/TabProxies.pyx":312
- *                 raise ValueError("out of memory" )
- *             memcpy( cpy, self.data, self.nbytes+1)
+    /* "pysam/TabProxies.pyx":345
+ *                 raise ValueError("out of memory")
+ *             memcpy(cpy, self.data, self.nbytes+1)
  *             for x from 0 <= x < self.nbytes:             # <<<<<<<<<<<<<<
- *                 if cpy[x] == '\0': cpy[x] = '\t'
- *             result = cpy[:self.nbytes]
+ *                 if cpy[x] == '\0':
+ *                     cpy[x] = '\t'
  */
     __pyx_t_3 = __pyx_v_self->nbytes;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_3; __pyx_v_x++) {
 
-      /* "pysam/TabProxies.pyx":313
- *             memcpy( cpy, self.data, self.nbytes+1)
+      /* "pysam/TabProxies.pyx":346
+ *             memcpy(cpy, self.data, self.nbytes+1)
  *             for x from 0 <= x < self.nbytes:
- *                 if cpy[x] == '\0': cpy[x] = '\t'             # <<<<<<<<<<<<<<
+ *                 if cpy[x] == '\0':             # <<<<<<<<<<<<<<
+ *                     cpy[x] = '\t'
  *             result = cpy[:self.nbytes]
- *             free(cpy)
  */
       __pyx_t_1 = (((__pyx_v_cpy[__pyx_v_x]) == '\x00') != 0);
       if (__pyx_t_1) {
+
+        /* "pysam/TabProxies.pyx":347
+ *             for x from 0 <= x < self.nbytes:
+ *                 if cpy[x] == '\0':
+ *                     cpy[x] = '\t'             # <<<<<<<<<<<<<<
+ *             result = cpy[:self.nbytes]
+ *             free(cpy)
+ */
         (__pyx_v_cpy[__pyx_v_x]) = '\t';
         goto __pyx_L9;
       }
       __pyx_L9:;
     }
 
-    /* "pysam/TabProxies.pyx":314
- *             for x from 0 <= x < self.nbytes:
- *                 if cpy[x] == '\0': cpy[x] = '\t'
+    /* "pysam/TabProxies.pyx":348
+ *                 if cpy[x] == '\0':
+ *                     cpy[x] = '\t'
  *             result = cpy[:self.nbytes]             # <<<<<<<<<<<<<<
  *             free(cpy)
- *             return result.decode('ascii')
+ *             r = result.decode(self.encoding)
  */
-    __pyx_t_2 = __Pyx_PyBytes_FromStringAndSize(__pyx_v_cpy + 0, __pyx_v_self->nbytes - 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_v_result = __pyx_t_2;
-    __pyx_t_2 = 0;
+    __pyx_t_6 = __Pyx_PyBytes_FromStringAndSize(__pyx_v_cpy + 0, __pyx_v_self->nbytes - 0); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_6);
+    __pyx_v_result = __pyx_t_6;
+    __pyx_t_6 = 0;
 
-    /* "pysam/TabProxies.pyx":315
- *                 if cpy[x] == '\0': cpy[x] = '\t'
+    /* "pysam/TabProxies.pyx":349
+ *                     cpy[x] = '\t'
  *             result = cpy[:self.nbytes]
  *             free(cpy)             # <<<<<<<<<<<<<<
- *             return result.decode('ascii')
- * 
+ *             r = result.decode(self.encoding)
+ *             return r
  */
     free(__pyx_v_cpy);
 
-    /* "pysam/TabProxies.pyx":316
+    /* "pysam/TabProxies.pyx":350
  *             result = cpy[:self.nbytes]
  *             free(cpy)
- *             return result.decode('ascii')             # <<<<<<<<<<<<<<
+ *             r = result.decode(self.encoding)             # <<<<<<<<<<<<<<
+ *             return r
  * 
- * def toDot( v ):
  */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_result, __pyx_n_s_decode); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_result, __pyx_n_s_decode); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_6);
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__12, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
+    __Pyx_INCREF(__pyx_v_self->encoding);
+    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_self->encoding);
+    __Pyx_GIVEREF(__pyx_v_self->encoding);
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
+    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_r = __pyx_t_7;
-    __pyx_t_7 = 0;
+    __pyx_v_r = __pyx_t_5;
+    __pyx_t_5 = 0;
+
+    /* "pysam/TabProxies.pyx":351
+ *             free(cpy)
+ *             r = result.decode(self.encoding)
+ *             return r             # <<<<<<<<<<<<<<
+ * 
+ * def toDot(v):
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(__pyx_v_r);
+    __pyx_r = __pyx_v_r;
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":298
- *         else: return retval
+  /* "pysam/TabProxies.pyx":331
+ *             return _force_str(retval, self.encoding)
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
  *         '''return original data'''
@@ -4540,22 +4917,24 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_10TupleProxy_18__str__(struct __py
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_7);
+  __Pyx_XDECREF(__pyx_t_5);
+  __Pyx_XDECREF(__pyx_t_6);
   __Pyx_AddTraceback("pysam.TabProxies.TupleProxy.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_result);
+  __Pyx_XDECREF(__pyx_v_r);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":318
- *             return result.decode('ascii')
+/* "pysam/TabProxies.pyx":353
+ *             return r
  * 
- * def toDot( v ):             # <<<<<<<<<<<<<<
+ * def toDot(v):             # <<<<<<<<<<<<<<
  *     '''convert value to '.' if None'''
- *     if v == None: return "."
+ *     if v is None:
  */
 
 /* Python wrapper */
@@ -4576,65 +4955,73 @@ static PyObject *__pyx_pw_5pysam_10TabProxies_1toDot(PyObject *__pyx_self, PyObj
 static PyObject *__pyx_pf_5pysam_10TabProxies_toDot(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_v) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
+  int __pyx_t_1;
   int __pyx_t_2;
   PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("toDot", 0);
 
-  /* "pysam/TabProxies.pyx":320
- * def toDot( v ):
+  /* "pysam/TabProxies.pyx":355
+ * def toDot(v):
  *     '''convert value to '.' if None'''
- *     if v == None: return "."             # <<<<<<<<<<<<<<
- *     else: return str(v)
- * 
+ *     if v is None:             # <<<<<<<<<<<<<<
+ *         return "."
+ *     else:
  */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_v, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_1 = (__pyx_v_v == Py_None);
+  __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
+
+    /* "pysam/TabProxies.pyx":356
+ *     '''convert value to '.' if None'''
+ *     if v is None:
+ *         return "."             # <<<<<<<<<<<<<<
+ *     else:
+ *         return str(v)
+ */
     __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_kp_s__13);
-    __pyx_r = __pyx_kp_s__13;
+    __Pyx_INCREF(__pyx_kp_s__10);
+    __pyx_r = __pyx_kp_s__10;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":321
- *     '''convert value to '.' if None'''
- *     if v == None: return "."
- *     else: return str(v)             # <<<<<<<<<<<<<<
+    /* "pysam/TabProxies.pyx":358
+ *         return "."
+ *     else:
+ *         return str(v)             # <<<<<<<<<<<<<<
  * 
- * def quote( v ):
+ * def quote(v):
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_v);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_v);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_v);
     __Pyx_GIVEREF(__pyx_v_v);
-    __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
+    __pyx_t_4 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __pyx_r = __pyx_t_4;
+    __pyx_t_4 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":318
- *             return result.decode('ascii')
+  /* "pysam/TabProxies.pyx":353
+ *             return r
  * 
- * def toDot( v ):             # <<<<<<<<<<<<<<
+ * def toDot(v):             # <<<<<<<<<<<<<<
  *     '''convert value to '.' if None'''
- *     if v == None: return "."
+ *     if v is None:
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
   __Pyx_AddTraceback("pysam.TabProxies.toDot", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
@@ -4643,10 +5030,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_toDot(CYTHON_UNUSED PyObject *__py
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":323
- *     else: return str(v)
+/* "pysam/TabProxies.pyx":360
+ *         return str(v)
  * 
- * def quote( v ):             # <<<<<<<<<<<<<<
+ * def quote(v):             # <<<<<<<<<<<<<<
  *     '''return a quoted attribute.'''
  *     if type(v) in types.StringTypes:
  */
@@ -4678,24 +5065,24 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_2quote(CYTHON_UNUSED PyObject *__p
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("quote", 0);
 
-  /* "pysam/TabProxies.pyx":325
- * def quote( v ):
+  /* "pysam/TabProxies.pyx":362
+ * def quote(v):
  *     '''return a quoted attribute.'''
  *     if type(v) in types.StringTypes:             # <<<<<<<<<<<<<<
  *         return '"%s"' % v
  *     else:
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_types); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_types); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_StringTypes); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_StringTypes); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = (__Pyx_PySequence_Contains(((PyObject *)Py_TYPE(__pyx_v_v)), __pyx_t_2, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PySequence_Contains(((PyObject *)Py_TYPE(__pyx_v_v)), __pyx_t_2, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = (__pyx_t_3 != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/TabProxies.pyx":326
+    /* "pysam/TabProxies.pyx":363
  *     '''return a quoted attribute.'''
  *     if type(v) in types.StringTypes:
  *         return '"%s"' % v             # <<<<<<<<<<<<<<
@@ -4703,7 +5090,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_2quote(CYTHON_UNUSED PyObject *__p
  *         return str(v)
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s, __pyx_v_v); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s, __pyx_v_v); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -4711,20 +5098,20 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_2quote(CYTHON_UNUSED PyObject *__p
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":328
+    /* "pysam/TabProxies.pyx":365
  *         return '"%s"' % v
  *     else:
  *         return str(v)             # <<<<<<<<<<<<<<
  * 
- * cdef class GTFProxy( TupleProxy ):
+ * 
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_v_v);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_v);
     __Pyx_GIVEREF(__pyx_v_v);
-    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_r = __pyx_t_1;
@@ -4732,10 +5119,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_2quote(CYTHON_UNUSED PyObject *__p
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":323
- *     else: return str(v)
+  /* "pysam/TabProxies.pyx":360
+ *         return str(v)
  * 
- * def quote( v ):             # <<<<<<<<<<<<<<
+ * def quote(v):             # <<<<<<<<<<<<<<
  *     '''return a quoted attribute.'''
  *     if type(v) in types.StringTypes:
  */
@@ -4752,10 +5139,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_2quote(CYTHON_UNUSED PyObject *__p
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":343
+/* "pysam/TabProxies.pyx":381
  *     '''
  * 
- *     def __cinit__(self ):             # <<<<<<<<<<<<<<
+ *     def __cinit__(self):             # <<<<<<<<<<<<<<
  *         # automatically calls TupleProxy.__cinit__
  *         self.hasOwnAttributes = False
  */
@@ -4781,8 +5168,8 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy___cinit__(struct __pyx_obj_5py
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__cinit__", 0);
 
-  /* "pysam/TabProxies.pyx":345
- *     def __cinit__(self ):
+  /* "pysam/TabProxies.pyx":383
+ *     def __cinit__(self):
  *         # automatically calls TupleProxy.__cinit__
  *         self.hasOwnAttributes = False             # <<<<<<<<<<<<<<
  *         self._attributes = NULL
@@ -4790,7 +5177,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy___cinit__(struct __pyx_obj_5py
  */
   __pyx_v_self->hasOwnAttributes = 0;
 
-  /* "pysam/TabProxies.pyx":346
+  /* "pysam/TabProxies.pyx":384
  *         # automatically calls TupleProxy.__cinit__
  *         self.hasOwnAttributes = False
  *         self._attributes = NULL             # <<<<<<<<<<<<<<
@@ -4799,10 +5186,10 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy___cinit__(struct __pyx_obj_5py
  */
   __pyx_v_self->_attributes = NULL;
 
-  /* "pysam/TabProxies.pyx":343
+  /* "pysam/TabProxies.pyx":381
  *     '''
  * 
- *     def __cinit__(self ):             # <<<<<<<<<<<<<<
+ *     def __cinit__(self):             # <<<<<<<<<<<<<<
  *         # automatically calls TupleProxy.__cinit__
  *         self.hasOwnAttributes = False
  */
@@ -4813,7 +5200,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy___cinit__(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":348
+/* "pysam/TabProxies.pyx":386
  *         self._attributes = NULL
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -4837,7 +5224,7 @@ static void __pyx_pf_5pysam_10TabProxies_8GTFProxy_2__dealloc__(struct __pyx_obj
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("__dealloc__", 0);
 
-  /* "pysam/TabProxies.pyx":350
+  /* "pysam/TabProxies.pyx":388
  *     def __dealloc__(self):
  *         # automatically calls TupleProxy.__dealloc__
  *         if self.hasOwnAttributes:             # <<<<<<<<<<<<<<
@@ -4847,19 +5234,19 @@ static void __pyx_pf_5pysam_10TabProxies_8GTFProxy_2__dealloc__(struct __pyx_obj
   __pyx_t_1 = (__pyx_v_self->hasOwnAttributes != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":351
+    /* "pysam/TabProxies.pyx":389
  *         # automatically calls TupleProxy.__dealloc__
  *         if self.hasOwnAttributes:
  *             free(self._attributes)             # <<<<<<<<<<<<<<
  * 
- *     cdef int getMaxFields( self, size_t nbytes ):
+ *     cdef int getMinFields(self):
  */
     free(__pyx_v_self->_attributes);
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":348
+  /* "pysam/TabProxies.pyx":386
  *         self._attributes = NULL
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -4871,21 +5258,58 @@ static void __pyx_pf_5pysam_10TabProxies_8GTFProxy_2__dealloc__(struct __pyx_obj
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/TabProxies.pyx":353
+/* "pysam/TabProxies.pyx":391
+ *             free(self._attributes)
+ * 
+ *     cdef int getMinFields(self):             # <<<<<<<<<<<<<<
+ *         '''return minimum number of fields.'''
+ *         return 3
+ */
+
+static int __pyx_f_5pysam_10TabProxies_8GTFProxy_getMinFields(CYTHON_UNUSED struct __pyx_obj_5pysam_10TabProxies_GTFProxy *__pyx_v_self) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("getMinFields", 0);
+
+  /* "pysam/TabProxies.pyx":393
+ *     cdef int getMinFields(self):
+ *         '''return minimum number of fields.'''
+ *         return 3             # <<<<<<<<<<<<<<
+ * 
+ *     cdef int getMaxFields(self):
+ */
+  __pyx_r = 3;
+  goto __pyx_L0;
+
+  /* "pysam/TabProxies.pyx":391
  *             free(self._attributes)
  * 
- *     cdef int getMaxFields( self, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef int getMinFields(self):             # <<<<<<<<<<<<<<
+ *         '''return minimum number of fields.'''
+ *         return 3
+ */
+
+  /* function exit code */
+  __pyx_L0:;
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/TabProxies.pyx":395
+ *         return 3
+ * 
+ *     cdef int getMaxFields(self):             # <<<<<<<<<<<<<<
  *         '''return max number of fields.'''
  *         return 9
  */
 
-static int __pyx_f_5pysam_10TabProxies_8GTFProxy_getMaxFields(CYTHON_UNUSED struct __pyx_obj_5pysam_10TabProxies_GTFProxy *__pyx_v_self, CYTHON_UNUSED size_t __pyx_v_nbytes) {
+static int __pyx_f_5pysam_10TabProxies_8GTFProxy_getMaxFields(CYTHON_UNUSED struct __pyx_obj_5pysam_10TabProxies_GTFProxy *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("getMaxFields", 0);
 
-  /* "pysam/TabProxies.pyx":355
- *     cdef int getMaxFields( self, size_t nbytes ):
+  /* "pysam/TabProxies.pyx":397
+ *     cdef int getMaxFields(self):
  *         '''return max number of fields.'''
  *         return 9             # <<<<<<<<<<<<<<
  * 
@@ -4894,10 +5318,10 @@ static int __pyx_f_5pysam_10TabProxies_8GTFProxy_getMaxFields(CYTHON_UNUSED stru
   __pyx_r = 9;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":353
- *             free(self._attributes)
+  /* "pysam/TabProxies.pyx":395
+ *         return 3
  * 
- *     cdef int getMaxFields( self, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef int getMaxFields(self):             # <<<<<<<<<<<<<<
  *         '''return max number of fields.'''
  *         return 9
  */
@@ -4908,12 +5332,12 @@ static int __pyx_f_5pysam_10TabProxies_8GTFProxy_getMaxFields(CYTHON_UNUSED stru
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":359
+/* "pysam/TabProxies.pyx":401
  *     property contig:
  *        '''contig of feature.'''
- *        def __get__( self ): return self._getindex( 0 )             # <<<<<<<<<<<<<<
- *        def __set__( self, value ): self._setindex( 0, value )
- * 
+ *        def __get__(self):             # <<<<<<<<<<<<<<
+ *            return self._getindex(0)
+ *        def __set__(self, value):
  */
 
 /* Python wrapper */
@@ -4938,16 +5362,32 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6contig___get__(struct _
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
+
+  /* "pysam/TabProxies.pyx":402
+ *        '''contig of feature.'''
+ *        def __get__(self):
+ *            return self._getindex(0)             # <<<<<<<<<<<<<<
+ *        def __set__(self, value):
+ *            self._setindex(0, value)
+ */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 359; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 402; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__14, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 359; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__11, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 402; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
+  /* "pysam/TabProxies.pyx":401
+ *     property contig:
+ *        '''contig of feature.'''
+ *        def __get__(self):             # <<<<<<<<<<<<<<
+ *            return self._getindex(0)
+ *        def __set__(self, value):
+ */
+
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
@@ -4960,12 +5400,12 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6contig___get__(struct _
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":360
- *        '''contig of feature.'''
- *        def __get__( self ): return self._getindex( 0 )
- *        def __set__( self, value ): self._setindex( 0, value )             # <<<<<<<<<<<<<<
+/* "pysam/TabProxies.pyx":403
+ *        def __get__(self):
+ *            return self._getindex(0)
+ *        def __set__(self, value):             # <<<<<<<<<<<<<<
+ *            self._setindex(0, value)
  * 
- *     property source:
  */
 
 /* Python wrapper */
@@ -4991,9 +5431,17 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_6contig_2__set__(struct __pyx_
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__set__", 0);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/TabProxies.pyx":404
+ *            return self._getindex(0)
+ *        def __set__(self, value):
+ *            self._setindex(0, value)             # <<<<<<<<<<<<<<
+ * 
+ *     property source:
+ */
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_0);
@@ -5001,12 +5449,20 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_6contig_2__set__(struct __pyx_
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
+  /* "pysam/TabProxies.pyx":403
+ *        def __get__(self):
+ *            return self._getindex(0)
+ *        def __set__(self, value):             # <<<<<<<<<<<<<<
+ *            self._setindex(0, value)
+ * 
+ */
+
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
@@ -5021,12 +5477,12 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_6contig_2__set__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":364
+/* "pysam/TabProxies.pyx":408
  *     property source:
  *        '''feature source.'''
- *        def __get__( self ): return self._getindex( 1 )             # <<<<<<<<<<<<<<
- *        def __set__( self, value ): self._setindex( 1, value )
- * 
+ *        def __get__(self):             # <<<<<<<<<<<<<<
+ *            return self._getindex(1)
+ *        def __set__(self, value):
  */
 
 /* Python wrapper */
@@ -5051,16 +5507,32 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6source___get__(struct _
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
+
+  /* "pysam/TabProxies.pyx":409
+ *        '''feature source.'''
+ *        def __get__(self):
+ *            return self._getindex(1)             # <<<<<<<<<<<<<<
+ *        def __set__(self, value):
+ *            self._setindex(1, value)
+ */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__15, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__12, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
+  /* "pysam/TabProxies.pyx":408
+ *     property source:
+ *        '''feature source.'''
+ *        def __get__(self):             # <<<<<<<<<<<<<<
+ *            return self._getindex(1)
+ *        def __set__(self, value):
+ */
+
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
@@ -5073,12 +5545,12 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6source___get__(struct _
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":365
- *        '''feature source.'''
- *        def __get__( self ): return self._getindex( 1 )
- *        def __set__( self, value ): self._setindex( 1, value )             # <<<<<<<<<<<<<<
+/* "pysam/TabProxies.pyx":410
+ *        def __get__(self):
+ *            return self._getindex(1)
+ *        def __set__(self, value):             # <<<<<<<<<<<<<<
+ *            self._setindex(1, value)
  * 
- *     property feature:
  */
 
 /* Python wrapper */
@@ -5104,9 +5576,17 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_6source_2__set__(struct __pyx_
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__set__", 0);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/TabProxies.pyx":411
+ *            return self._getindex(1)
+ *        def __set__(self, value):
+ *            self._setindex(1, value)             # <<<<<<<<<<<<<<
+ * 
+ *     property feature:
+ */
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_1);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_1);
@@ -5114,12 +5594,20 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_6source_2__set__(struct __pyx_
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
+  /* "pysam/TabProxies.pyx":410
+ *        def __get__(self):
+ *            return self._getindex(1)
+ *        def __set__(self, value):             # <<<<<<<<<<<<<<
+ *            self._setindex(1, value)
+ * 
+ */
+
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
@@ -5134,7 +5622,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_6source_2__set__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":369
+/* "pysam/TabProxies.pyx":415
  *     property feature:
  *        '''feature name.'''
  *        def __get__( self ): return self._getindex( 2 )             # <<<<<<<<<<<<<<
@@ -5165,9 +5653,9 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_7feature___get__(struct
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__13, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_r = __pyx_t_2;
@@ -5186,7 +5674,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_7feature___get__(struct
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":370
+/* "pysam/TabProxies.pyx":416
  *        '''feature name.'''
  *        def __get__( self ): return self._getindex( 2 )
  *        def __set__( self, value ): self._setindex( 2, value )             # <<<<<<<<<<<<<<
@@ -5217,9 +5705,9 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_7feature_2__set__(struct __pyx
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__set__", 0);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_2);
@@ -5227,7 +5715,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_7feature_2__set__(struct __pyx
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -5247,7 +5735,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_7feature_2__set__(struct __pyx
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":374
+/* "pysam/TabProxies.pyx":420
  *     property start:
  *        '''feature start (in 0-based open/closed coordinates).'''
  *        def __get__( self ): return int( self._getindex( 3 )) - 1             # <<<<<<<<<<<<<<
@@ -5278,15 +5766,15 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_5start___get__(struct __
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__14, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyNumber_Subtract(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyNumber_Subtract(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_r = __pyx_t_2;
@@ -5305,7 +5793,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_5start___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":375
+/* "pysam/TabProxies.pyx":421
  *        '''feature start (in 0-based open/closed coordinates).'''
  *        def __get__( self ): return int( self._getindex( 3 )) - 1
  *        def __set__( self, value ): self._setindex( 3, str(value+1) )             # <<<<<<<<<<<<<<
@@ -5336,19 +5824,19 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_5start_2__set__(struct __pyx_o
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__set__", 0);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyNumber_Add(__pyx_v_value, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyNumber_Add(__pyx_v_value, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
   __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_int_3);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_int_3);
@@ -5356,7 +5844,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_5start_2__set__(struct __pyx_o
   PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
   __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -5376,7 +5864,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_5start_2__set__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":379
+/* "pysam/TabProxies.pyx":425
  *     property end:
  *        '''feature end (in 0-based open/closed coordinates).'''
  *        def __get__( self ): return int( self._getindex( 4 ) )             # <<<<<<<<<<<<<<
@@ -5407,12 +5895,12 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_3end___get__(struct __py
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__18, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__15, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_r = __pyx_t_1;
@@ -5431,7 +5919,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_3end___get__(struct __py
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":380
+/* "pysam/TabProxies.pyx":426
  *        '''feature end (in 0-based open/closed coordinates).'''
  *        def __get__( self ): return int( self._getindex( 4 ) )
  *        def __set__( self, value ): self._setindex( 4, str(value) )             # <<<<<<<<<<<<<<
@@ -5462,17 +5950,17 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_3end_2__set__(struct __pyx_obj
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__set__", 0);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_4);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_4);
@@ -5480,7 +5968,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_3end_2__set__(struct __pyx_obj
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_3);
   __Pyx_GIVEREF(__pyx_t_3);
   __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -5500,7 +5988,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_3end_2__set__(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":384
+/* "pysam/TabProxies.pyx":430
  *     property score:
  *        '''feature score.'''
  *        def __get__( self ):             # <<<<<<<<<<<<<<
@@ -5536,33 +6024,33 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_5score___get__(struct __
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/TabProxies.pyx":385
+  /* "pysam/TabProxies.pyx":431
  *        '''feature score.'''
  *        def __get__( self ):
  *            v = self._getindex(5)             # <<<<<<<<<<<<<<
  *            if v == "" or v[0] == '.':
  *                return None
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__19, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_v = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":386
+  /* "pysam/TabProxies.pyx":432
  *        def __get__( self ):
  *            v = self._getindex(5)
  *            if v == "" or v[0] == '.':             # <<<<<<<<<<<<<<
  *                return None
  *            else:
  */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_v, __pyx_kp_s__3, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_v, __pyx_kp_s_, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (!__pyx_t_3) {
-    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_v, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_v, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s__13, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s__10, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_t_5 = __pyx_t_4;
   } else {
@@ -5570,7 +6058,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_5score___get__(struct __
   }
   if (__pyx_t_5) {
 
-    /* "pysam/TabProxies.pyx":387
+    /* "pysam/TabProxies.pyx":433
  *            v = self._getindex(5)
  *            if v == "" or v[0] == '.':
  *                return None             # <<<<<<<<<<<<<<
@@ -5584,7 +6072,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_5score___get__(struct __
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":389
+    /* "pysam/TabProxies.pyx":435
  *                return None
  *            else:
  *                return float(v)             # <<<<<<<<<<<<<<
@@ -5592,15 +6080,15 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_5score___get__(struct __
  *        def __set__( self, value ): self._setindex( 5, value )
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_6 = __Pyx_PyObject_AsDouble(__pyx_v_v); if (unlikely(__pyx_t_6 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_2 = PyFloat_FromDouble(__pyx_t_6); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_AsDouble(__pyx_v_v); if (unlikely(__pyx_t_6 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyFloat_FromDouble(__pyx_t_6); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":384
+  /* "pysam/TabProxies.pyx":430
  *     property score:
  *        '''feature score.'''
  *        def __get__( self ):             # <<<<<<<<<<<<<<
@@ -5621,7 +6109,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_5score___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":391
+/* "pysam/TabProxies.pyx":437
  *                return float(v)
  * 
  *        def __set__( self, value ): self._setindex( 5, value )             # <<<<<<<<<<<<<<
@@ -5652,9 +6140,9 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_5score_2__set__(struct __pyx_o
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__set__", 0);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_5);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_5);
@@ -5662,7 +6150,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_5score_2__set__(struct __pyx_o
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -5682,7 +6170,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_5score_2__set__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":395
+/* "pysam/TabProxies.pyx":441
  *     property strand:
  *        '''feature strand.'''
  *        def __get__( self ): return self._getindex( 6 )             # <<<<<<<<<<<<<<
@@ -5713,9 +6201,9 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6strand___get__(struct _
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__20, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_r = __pyx_t_2;
@@ -5734,7 +6222,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6strand___get__(struct _
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":396
+/* "pysam/TabProxies.pyx":442
  *        '''feature strand.'''
  *        def __get__( self ): return self._getindex( 6 )
  *        def __set__( self, value ): self._setindex( 6, value )             # <<<<<<<<<<<<<<
@@ -5765,9 +6253,9 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_6strand_2__set__(struct __pyx_
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__set__", 0);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 396; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 396; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_6);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_6);
@@ -5775,7 +6263,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_6strand_2__set__(struct __pyx_
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 396; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -5795,7 +6283,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_6strand_2__set__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":400
+/* "pysam/TabProxies.pyx":446
  *     property frame:
  *        '''feature frame.'''
  *        def __get__( self ): return self._getindex( 7 )             # <<<<<<<<<<<<<<
@@ -5826,9 +6314,9 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_5frame___get__(struct __
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__21, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__18, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_r = __pyx_t_2;
@@ -5847,7 +6335,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_5frame___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":401
+/* "pysam/TabProxies.pyx":447
  *        '''feature frame.'''
  *        def __get__( self ): return self._getindex( 7 )
  *        def __set__( self, value ): self._setindex( 7, value )             # <<<<<<<<<<<<<<
@@ -5878,9 +6366,9 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_5frame_2__set__(struct __pyx_o
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__set__", 0);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_7);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_7);
@@ -5888,7 +6376,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_5frame_2__set__(struct __pyx_o
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -5908,7 +6396,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_5frame_2__set__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":405
+/* "pysam/TabProxies.pyx":451
  *     property attributes:
  *        '''feature attributes (as a string).'''
  *        def __get__( self ):             # <<<<<<<<<<<<<<
@@ -5940,7 +6428,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes___get__(str
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/TabProxies.pyx":406
+  /* "pysam/TabProxies.pyx":452
  *        '''feature attributes (as a string).'''
  *        def __get__( self ):
  *            if self.hasOwnAttributes:             # <<<<<<<<<<<<<<
@@ -5950,15 +6438,15 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes___get__(str
   __pyx_t_1 = (__pyx_v_self->hasOwnAttributes != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":407
+    /* "pysam/TabProxies.pyx":453
  *        def __get__( self ):
  *            if self.hasOwnAttributes:
  *                return self._attributes             # <<<<<<<<<<<<<<
  *            else:
- *                return self._getindex( 8 )
+ *                return self._getindex(8)
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->_attributes); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->_attributes); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 453; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -5966,17 +6454,17 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes___get__(str
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":409
+    /* "pysam/TabProxies.pyx":455
  *                return self._attributes
  *            else:
- *                return self._getindex( 8 )             # <<<<<<<<<<<<<<
+ *                return self._getindex(8)             # <<<<<<<<<<<<<<
  *        def __set__( self, value ):
  *            if self.hasOwnAttributes:
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getindex); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__22, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__19, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_r = __pyx_t_3;
@@ -5984,7 +6472,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes___get__(str
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":405
+  /* "pysam/TabProxies.pyx":451
  *     property attributes:
  *        '''feature attributes (as a string).'''
  *        def __get__( self ):             # <<<<<<<<<<<<<<
@@ -6004,9 +6492,9 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes___get__(str
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":410
+/* "pysam/TabProxies.pyx":456
  *            else:
- *                return self._getindex( 8 )
+ *                return self._getindex(8)
  *        def __set__( self, value ):             # <<<<<<<<<<<<<<
  *            if self.hasOwnAttributes:
  *                free(self._attributes)
@@ -6037,8 +6525,8 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes_2__set__(struct _
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__set__", 0);
 
-  /* "pysam/TabProxies.pyx":411
- *                return self._getindex( 8 )
+  /* "pysam/TabProxies.pyx":457
+ *                return self._getindex(8)
  *        def __set__( self, value ):
  *            if self.hasOwnAttributes:             # <<<<<<<<<<<<<<
  *                free(self._attributes)
@@ -6047,7 +6535,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes_2__set__(struct _
   __pyx_t_1 = (__pyx_v_self->hasOwnAttributes != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":412
+    /* "pysam/TabProxies.pyx":458
  *        def __set__( self, value ):
  *            if self.hasOwnAttributes:
  *                free(self._attributes)             # <<<<<<<<<<<<<<
@@ -6056,7 +6544,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes_2__set__(struct _
  */
     free(__pyx_v_self->_attributes);
 
-    /* "pysam/TabProxies.pyx":413
+    /* "pysam/TabProxies.pyx":459
  *            if self.hasOwnAttributes:
  *                free(self._attributes)
  *                self._attributes = NULL             # <<<<<<<<<<<<<<
@@ -6065,7 +6553,7 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes_2__set__(struct _
  */
     __pyx_v_self->_attributes = NULL;
 
-    /* "pysam/TabProxies.pyx":414
+    /* "pysam/TabProxies.pyx":460
  *                free(self._attributes)
  *                self._attributes = NULL
  *                self.hasOwnAttributes = False             # <<<<<<<<<<<<<<
@@ -6077,16 +6565,16 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes_2__set__(struct _
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":415
+  /* "pysam/TabProxies.pyx":461
  *                self._attributes = NULL
  *                self.hasOwnAttributes = False
  *            self._setindex(8, value )             # <<<<<<<<<<<<<<
  * 
- *     cdef char * getAttributes( self ):
+ *     cdef char * getAttributes(self):
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setindex); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_int_8);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_int_8);
@@ -6094,15 +6582,15 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes_2__set__(struct _
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/TabProxies.pyx":410
+  /* "pysam/TabProxies.pyx":456
  *            else:
- *                return self._getindex( 8 )
+ *                return self._getindex(8)
  *        def __set__( self, value ):             # <<<<<<<<<<<<<<
  *            if self.hasOwnAttributes:
  *                free(self._attributes)
@@ -6122,10 +6610,10 @@ static int __pyx_pf_5pysam_10TabProxies_8GTFProxy_10attributes_2__set__(struct _
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":417
+/* "pysam/TabProxies.pyx":463
  *            self._setindex(8, value )
  * 
- *     cdef char * getAttributes( self ):             # <<<<<<<<<<<<<<
+ *     cdef char * getAttributes(self):             # <<<<<<<<<<<<<<
  *        '''return pointer to attributes.'''
  *        if self.hasOwnAttributes:
  */
@@ -6136,8 +6624,8 @@ static char *__pyx_f_5pysam_10TabProxies_8GTFProxy_getAttributes(struct __pyx_ob
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("getAttributes", 0);
 
-  /* "pysam/TabProxies.pyx":419
- *     cdef char * getAttributes( self ):
+  /* "pysam/TabProxies.pyx":465
+ *     cdef char * getAttributes(self):
  *        '''return pointer to attributes.'''
  *        if self.hasOwnAttributes:             # <<<<<<<<<<<<<<
  *            return self._attributes
@@ -6146,22 +6634,22 @@ static char *__pyx_f_5pysam_10TabProxies_8GTFProxy_getAttributes(struct __pyx_ob
   __pyx_t_1 = (__pyx_v_self->hasOwnAttributes != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":420
+    /* "pysam/TabProxies.pyx":466
  *        '''return pointer to attributes.'''
  *        if self.hasOwnAttributes:
  *            return self._attributes             # <<<<<<<<<<<<<<
  *        else:
- *            return self.fields[ 8 ]
+ *            return self.fields[8]
  */
     __pyx_r = __pyx_v_self->_attributes;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":422
+    /* "pysam/TabProxies.pyx":468
  *            return self._attributes
  *        else:
- *            return self.fields[ 8 ]             # <<<<<<<<<<<<<<
+ *            return self.fields[8]             # <<<<<<<<<<<<<<
  * 
  *     def asDict( self ):
  */
@@ -6169,10 +6657,10 @@ static char *__pyx_f_5pysam_10TabProxies_8GTFProxy_getAttributes(struct __pyx_ob
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":417
+  /* "pysam/TabProxies.pyx":463
  *            self._setindex(8, value )
  * 
- *     cdef char * getAttributes( self ):             # <<<<<<<<<<<<<<
+ *     cdef char * getAttributes(self):             # <<<<<<<<<<<<<<
  *        '''return pointer to attributes.'''
  *        if self.hasOwnAttributes:
  */
@@ -6183,8 +6671,8 @@ static char *__pyx_f_5pysam_10TabProxies_8GTFProxy_getAttributes(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":424
- *            return self.fields[ 8 ]
+/* "pysam/TabProxies.pyx":470
+ *            return self.fields[8]
  * 
  *     def asDict( self ):             # <<<<<<<<<<<<<<
  *         """parse attributes - return as dict
@@ -6238,40 +6726,40 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("asDict", 0);
 
-  /* "pysam/TabProxies.pyx":429
+  /* "pysam/TabProxies.pyx":475
  * 
  *         # remove comments
  *         attributes = self.attributes             # <<<<<<<<<<<<<<
  * 
  *         # separate into fields
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_attributes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_attributes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_attributes = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":432
+  /* "pysam/TabProxies.pyx":478
  * 
  *         # separate into fields
  *         fields = [x.strip() for x in attributes.split(";")[:-1]]             # <<<<<<<<<<<<<<
  * 
  *         result = {}
  */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_attributes, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_attributes, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__24, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__21, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_t_3, 0, -1, NULL, NULL, &__pyx_slice__25, 0, 1, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_t_3, 0, -1, NULL, NULL, &__pyx_slice__22, 0, 1, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) {
     __pyx_t_3 = __pyx_t_2; __Pyx_INCREF(__pyx_t_3); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_t_5 = Py_TYPE(__pyx_t_3)->tp_iternext;
   }
@@ -6280,16 +6768,16 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
     if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_3)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_3)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_3)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_3)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_2 = __pyx_t_5(__pyx_t_3);
@@ -6297,7 +6785,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -6305,31 +6793,31 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
     }
     __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_2);
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_x, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_x, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
   }
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_v_fields = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":434
+  /* "pysam/TabProxies.pyx":480
  *         fields = [x.strip() for x in attributes.split(";")[:-1]]
  * 
  *         result = {}             # <<<<<<<<<<<<<<
  * 
  *         for f in fields:
  */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_result = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":436
+  /* "pysam/TabProxies.pyx":482
  *         result = {}
  * 
  *         for f in fields:             # <<<<<<<<<<<<<<
@@ -6340,39 +6828,39 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
   for (;;) {
     if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_1)) break;
     #if CYTHON_COMPILING_IN_CPYTHON
-    __pyx_t_3 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     #else
-    __pyx_t_3 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     #endif
     __Pyx_XDECREF_SET(__pyx_v_f, __pyx_t_3);
     __pyx_t_3 = 0;
 
-    /* "pysam/TabProxies.pyx":439
+    /* "pysam/TabProxies.pyx":485
  *             # split at most once in order to avoid separating
  *             # multi-word values
  *             d = [x.strip() for x in string.split(f, " ", maxsplit=1)]             # <<<<<<<<<<<<<<
  * 
  *             n,v = d[0], d[1]
  */
-    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_string); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_string); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_INCREF(__pyx_v_f);
     PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_f);
     __Pyx_GIVEREF(__pyx_v_f);
-    __Pyx_INCREF(__pyx_kp_s__26);
-    PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_kp_s__26);
-    __Pyx_GIVEREF(__pyx_kp_s__26);
-    __pyx_t_7 = PyDict_New(); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_INCREF(__pyx_kp_s__23);
+    PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_kp_s__23);
+    __Pyx_GIVEREF(__pyx_kp_s__23);
+    __pyx_t_7 = PyDict_New(); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
-    if (PyDict_SetItem(__pyx_t_7, __pyx_n_s_maxsplit, __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_6, __pyx_t_7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_7, __pyx_n_s_maxsplit, __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_6, __pyx_t_7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
@@ -6381,7 +6869,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
       __pyx_t_7 = __pyx_t_8; __Pyx_INCREF(__pyx_t_7); __pyx_t_9 = 0;
       __pyx_t_5 = NULL;
     } else {
-      __pyx_t_9 = -1; __pyx_t_7 = PyObject_GetIter(__pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = -1; __pyx_t_7 = PyObject_GetIter(__pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __pyx_t_5 = Py_TYPE(__pyx_t_7)->tp_iternext;
     }
@@ -6390,16 +6878,16 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
       if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_7)) {
         if (__pyx_t_9 >= PyList_GET_SIZE(__pyx_t_7)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_8 = PyList_GET_ITEM(__pyx_t_7, __pyx_t_9); __Pyx_INCREF(__pyx_t_8); __pyx_t_9++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PyList_GET_ITEM(__pyx_t_7, __pyx_t_9); __Pyx_INCREF(__pyx_t_8); __pyx_t_9++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_9); __pyx_t_9++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_9); __pyx_t_9++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_7)) {
         if (__pyx_t_9 >= PyTuple_GET_SIZE(__pyx_t_7)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_7, __pyx_t_9); __Pyx_INCREF(__pyx_t_8); __pyx_t_9++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_7, __pyx_t_9); __Pyx_INCREF(__pyx_t_8); __pyx_t_9++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_9); __pyx_t_9++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_9); __pyx_t_9++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_8 = __pyx_t_5(__pyx_t_7);
@@ -6407,7 +6895,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -6415,53 +6903,53 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
       }
       __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_8);
       __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_x, __pyx_n_s_strip); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_x, __pyx_n_s_strip); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      if (unlikely(__Pyx_ListComp_Append(__pyx_t_3, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(__Pyx_ListComp_Append(__pyx_t_3, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     }
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __Pyx_XDECREF_SET(__pyx_v_d, ((PyObject*)__pyx_t_3));
     __pyx_t_3 = 0;
 
-    /* "pysam/TabProxies.pyx":441
+    /* "pysam/TabProxies.pyx":487
  *             d = [x.strip() for x in string.split(f, " ", maxsplit=1)]
  * 
  *             n,v = d[0], d[1]             # <<<<<<<<<<<<<<
  *             if len(d) > 2:
  *                 v = d[1:]
  */
-    __pyx_t_3 = __Pyx_GetItemInt_List(__pyx_v_d, 0, long, 1, __Pyx_PyInt_From_long, 1, 0, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_3 = __Pyx_GetItemInt_List(__pyx_v_d, 0, long, 1, __Pyx_PyInt_From_long, 1, 0, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_7 = __Pyx_GetItemInt_List(__pyx_v_d, 1, long, 1, __Pyx_PyInt_From_long, 1, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_7 = __Pyx_GetItemInt_List(__pyx_v_d, 1, long, 1, __Pyx_PyInt_From_long, 1, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_XDECREF_SET(__pyx_v_n, __pyx_t_3);
     __pyx_t_3 = 0;
     __Pyx_XDECREF_SET(__pyx_v_v, __pyx_t_7);
     __pyx_t_7 = 0;
 
-    /* "pysam/TabProxies.pyx":442
+    /* "pysam/TabProxies.pyx":488
  * 
  *             n,v = d[0], d[1]
  *             if len(d) > 2:             # <<<<<<<<<<<<<<
  *                 v = d[1:]
  * 
  */
-    __pyx_t_9 = PyList_GET_SIZE(__pyx_v_d); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyList_GET_SIZE(__pyx_v_d); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_10 = ((__pyx_t_9 > 2) != 0);
     if (__pyx_t_10) {
 
-      /* "pysam/TabProxies.pyx":443
+      /* "pysam/TabProxies.pyx":489
  *             n,v = d[0], d[1]
  *             if len(d) > 2:
  *                 v = d[1:]             # <<<<<<<<<<<<<<
  * 
  *             if v[0] == '"' and v[-1] == '"':
  */
-      __pyx_t_7 = __Pyx_PyList_GetSlice(__pyx_v_d, 1, PY_SSIZE_T_MAX); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyList_GetSlice(__pyx_v_d, 1, PY_SSIZE_T_MAX); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF_SET(__pyx_v_v, __pyx_t_7);
       __pyx_t_7 = 0;
@@ -6469,21 +6957,21 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
     }
     __pyx_L9:;
 
-    /* "pysam/TabProxies.pyx":445
+    /* "pysam/TabProxies.pyx":491
  *                 v = d[1:]
  * 
  *             if v[0] == '"' and v[-1] == '"':             # <<<<<<<<<<<<<<
  *                 v = v[1:-1]
  *             else:
  */
-    __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_v, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_v, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_10 = (__Pyx_PyString_Equals(__pyx_t_7, __pyx_kp_s__27, Py_EQ)); if (unlikely(__pyx_t_10 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = (__Pyx_PyString_Equals(__pyx_t_7, __pyx_kp_s__24, Py_EQ)); if (unlikely(__pyx_t_10 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     if (__pyx_t_10) {
-      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_v, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_v, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_7, __pyx_kp_s__27, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_7, __pyx_kp_s__24, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __pyx_t_12 = __pyx_t_11;
     } else {
@@ -6491,14 +6979,14 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
     }
     if (__pyx_t_12) {
 
-      /* "pysam/TabProxies.pyx":446
+      /* "pysam/TabProxies.pyx":492
  * 
  *             if v[0] == '"' and v[-1] == '"':
  *                 v = v[1:-1]             # <<<<<<<<<<<<<<
  *             else:
  *                 ## try to convert to a value
  */
-      __pyx_t_7 = __Pyx_PyObject_GetSlice(__pyx_v_v, 1, -1, NULL, NULL, &__pyx_slice__28, 1, 1, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_GetSlice(__pyx_v_v, 1, -1, NULL, NULL, &__pyx_slice__25, 1, 1, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF_SET(__pyx_v_v, __pyx_t_7);
       __pyx_t_7 = 0;
@@ -6506,7 +6994,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
     }
     /*else*/ {
 
-      /* "pysam/TabProxies.pyx":449
+      /* "pysam/TabProxies.pyx":495
  *             else:
  *                 ## try to convert to a value
  *                 try:             # <<<<<<<<<<<<<<
@@ -6520,27 +7008,27 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
         __Pyx_XGOTREF(__pyx_t_15);
         /*try:*/ {
 
-          /* "pysam/TabProxies.pyx":450
+          /* "pysam/TabProxies.pyx":496
  *                 ## try to convert to a value
  *                 try:
  *                     v = float(v)             # <<<<<<<<<<<<<<
  *                     v = int(v)
  *                 except ValueError:
  */
-          __pyx_t_16 = __Pyx_PyObject_AsDouble(__pyx_v_v); if (unlikely(__pyx_t_16 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L11_error;}
-          __pyx_t_7 = PyFloat_FromDouble(__pyx_t_16); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L11_error;}
+          __pyx_t_16 = __Pyx_PyObject_AsDouble(__pyx_v_v); if (unlikely(__pyx_t_16 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L11_error;}
+          __pyx_t_7 = PyFloat_FromDouble(__pyx_t_16); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L11_error;}
           __Pyx_GOTREF(__pyx_t_7);
           __Pyx_DECREF_SET(__pyx_v_v, __pyx_t_7);
           __pyx_t_7 = 0;
 
-          /* "pysam/TabProxies.pyx":451
+          /* "pysam/TabProxies.pyx":497
  *                 try:
  *                     v = float(v)
  *                     v = int(v)             # <<<<<<<<<<<<<<
  *                 except ValueError:
  *                     pass
  */
-          __pyx_t_7 = PyNumber_Int(__pyx_v_v); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L11_error;}
+          __pyx_t_7 = PyNumber_Int(__pyx_v_v); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L11_error;}
           __Pyx_GOTREF(__pyx_t_7);
           __Pyx_DECREF_SET(__pyx_v_v, __pyx_t_7);
           __pyx_t_7 = 0;
@@ -6556,7 +7044,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
         __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
         __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0;
 
-        /* "pysam/TabProxies.pyx":452
+        /* "pysam/TabProxies.pyx":498
  *                     v = float(v)
  *                     v = int(v)
  *                 except ValueError:             # <<<<<<<<<<<<<<
@@ -6569,7 +7057,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
           goto __pyx_L12_exception_handled;
         }
 
-        /* "pysam/TabProxies.pyx":454
+        /* "pysam/TabProxies.pyx":500
  *                 except ValueError:
  *                     pass
  *                 except TypeError:             # <<<<<<<<<<<<<<
@@ -6598,31 +7086,31 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
     }
     __pyx_L10:;
 
-    /* "pysam/TabProxies.pyx":457
+    /* "pysam/TabProxies.pyx":503
  *                     pass
  * 
  *             result[n] = v             # <<<<<<<<<<<<<<
  * 
  *         return result
  */
-    if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_n, __pyx_v_v) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_n, __pyx_v_v) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":459
+  /* "pysam/TabProxies.pyx":505
  *             result[n] = v
  * 
  *         return result             # <<<<<<<<<<<<<<
  * 
- *     def fromDict( self, d ):
+ *     def fromDict(self, d):
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(__pyx_v_result);
   __pyx_r = __pyx_v_result;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":424
- *            return self.fields[ 8 ]
+  /* "pysam/TabProxies.pyx":470
+ *            return self.fields[8]
  * 
  *     def asDict( self ):             # <<<<<<<<<<<<<<
  *         """parse attributes - return as dict
@@ -6653,10 +7141,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_4asDict(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":461
+/* "pysam/TabProxies.pyx":507
  *         return result
  * 
- *     def fromDict( self, d ):             # <<<<<<<<<<<<<<
+ *     def fromDict(self, d):             # <<<<<<<<<<<<<<
  *         '''set attributes from a dictionary.'''
  *         cdef char * p
  */
@@ -6701,7 +7189,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("fromDict", 0);
 
-  /* "pysam/TabProxies.pyx":467
+  /* "pysam/TabProxies.pyx":513
  * 
  *         # clean up if this field is set twice
  *         if self.hasOwnAttributes:             # <<<<<<<<<<<<<<
@@ -6711,7 +7199,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
   __pyx_t_1 = (__pyx_v_self->hasOwnAttributes != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":468
+    /* "pysam/TabProxies.pyx":514
  *         # clean up if this field is set twice
  *         if self.hasOwnAttributes:
  *             free(self._attributes)             # <<<<<<<<<<<<<<
@@ -6723,35 +7211,35 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":470
+  /* "pysam/TabProxies.pyx":516
  *             free(self._attributes)
  * 
  *         aa = []             # <<<<<<<<<<<<<<
  *         for k,v in d.items():
  *             if type(v) in types.StringTypes:
  */
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 470; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_aa = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":471
+  /* "pysam/TabProxies.pyx":517
  * 
  *         aa = []
  *         for k,v in d.items():             # <<<<<<<<<<<<<<
  *             if type(v) in types.StringTypes:
  *                 aa.append( '%s "%s"' % (k,v) )
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_d, __pyx_n_s_items); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_d, __pyx_n_s_items); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (PyList_CheckExact(__pyx_t_3) || PyTuple_CheckExact(__pyx_t_3)) {
     __pyx_t_2 = __pyx_t_3; __Pyx_INCREF(__pyx_t_2); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_t_5 = Py_TYPE(__pyx_t_2)->tp_iternext;
   }
@@ -6760,16 +7248,16 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
     if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_2)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_3 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_2)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_3 = __pyx_t_5(__pyx_t_2);
@@ -6777,7 +7265,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -6793,7 +7281,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
       if (unlikely(size != 2)) {
         if (size > 2) __Pyx_RaiseTooManyValuesError(2);
         else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       #if CYTHON_COMPILING_IN_CPYTHON
       if (likely(PyTuple_CheckExact(sequence))) {
@@ -6806,15 +7294,15 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
       __Pyx_INCREF(__pyx_t_6);
       __Pyx_INCREF(__pyx_t_7);
       #else
-      __pyx_t_6 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_7 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       #endif
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     } else {
       Py_ssize_t index = -1;
-      __pyx_t_8 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       __pyx_t_9 = Py_TYPE(__pyx_t_8)->tp_iternext;
@@ -6822,7 +7310,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
       __Pyx_GOTREF(__pyx_t_6);
       index = 1; __pyx_t_7 = __pyx_t_9(__pyx_t_8); if (unlikely(!__pyx_t_7)) goto __pyx_L6_unpacking_failed;
       __Pyx_GOTREF(__pyx_t_7);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_9 = NULL;
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       goto __pyx_L7_unpacking_done;
@@ -6830,7 +7318,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       __pyx_t_9 = NULL;
       if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_L7_unpacking_done:;
     }
     __Pyx_XDECREF_SET(__pyx_v_k, __pyx_t_6);
@@ -6838,31 +7326,31 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
     __Pyx_XDECREF_SET(__pyx_v_v, __pyx_t_7);
     __pyx_t_7 = 0;
 
-    /* "pysam/TabProxies.pyx":472
+    /* "pysam/TabProxies.pyx":518
  *         aa = []
  *         for k,v in d.items():
  *             if type(v) in types.StringTypes:             # <<<<<<<<<<<<<<
  *                 aa.append( '%s "%s"' % (k,v) )
  *             else:
  */
-    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_types); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_types); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_StringTypes); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_StringTypes); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_1 = (__Pyx_PySequence_Contains(((PyObject *)Py_TYPE(__pyx_v_v)), __pyx_t_7, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = (__Pyx_PySequence_Contains(((PyObject *)Py_TYPE(__pyx_v_v)), __pyx_t_7, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __pyx_t_10 = (__pyx_t_1 != 0);
     if (__pyx_t_10) {
 
-      /* "pysam/TabProxies.pyx":473
+      /* "pysam/TabProxies.pyx":519
  *         for k,v in d.items():
  *             if type(v) in types.StringTypes:
  *                 aa.append( '%s "%s"' % (k,v) )             # <<<<<<<<<<<<<<
  *             else:
  *                 aa.append( '%s %s' % (k,str(v)) )
  */
-      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 473; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_INCREF(__pyx_v_k);
       PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_k);
@@ -6870,31 +7358,31 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
       __Pyx_INCREF(__pyx_v_v);
       PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_v_v);
       __Pyx_GIVEREF(__pyx_v_v);
-      __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_7); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 473; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_7); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_aa, __pyx_t_3); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 473; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_aa, __pyx_t_3); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       goto __pyx_L8;
     }
     /*else*/ {
 
-      /* "pysam/TabProxies.pyx":475
+      /* "pysam/TabProxies.pyx":521
  *                 aa.append( '%s "%s"' % (k,v) )
  *             else:
  *                 aa.append( '%s %s' % (k,str(v)) )             # <<<<<<<<<<<<<<
  * 
  *         a = "; ".join( aa ) + ";"
  */
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_INCREF(__pyx_v_v);
       PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_v);
       __Pyx_GIVEREF(__pyx_v_v);
-      __pyx_t_7 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_INCREF(__pyx_v_k);
       PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_k);
@@ -6902,52 +7390,52 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
       PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_7);
       __Pyx_GIVEREF(__pyx_t_7);
       __pyx_t_7 = 0;
-      __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_s_s_2, __pyx_t_3); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_s_s_2, __pyx_t_3); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_aa, __pyx_t_7); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_aa, __pyx_t_7); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     }
     __pyx_L8:;
   }
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":477
+  /* "pysam/TabProxies.pyx":523
  *                 aa.append( '%s %s' % (k,str(v)) )
  * 
  *         a = "; ".join( aa ) + ";"             # <<<<<<<<<<<<<<
  *         p = a
  *         l = len(a)
  */
-  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__29, __pyx_v_aa); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__26, __pyx_v_aa); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_7 = PyNumber_Add(__pyx_t_2, __pyx_kp_s__23); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = PyNumber_Add(__pyx_t_2, __pyx_kp_s__20); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_v_a = ((PyObject*)__pyx_t_7);
   __pyx_t_7 = 0;
 
-  /* "pysam/TabProxies.pyx":478
+  /* "pysam/TabProxies.pyx":524
  * 
  *         a = "; ".join( aa ) + ";"
  *         p = a             # <<<<<<<<<<<<<<
  *         l = len(a)
  *         self._attributes = <char *>calloc( l + 1, sizeof(char) )
  */
-  __pyx_t_12 = __Pyx_PyObject_AsString(__pyx_v_a); if (unlikely((!__pyx_t_12) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = __Pyx_PyObject_AsString(__pyx_v_a); if (unlikely((!__pyx_t_12) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_p = __pyx_t_12;
 
-  /* "pysam/TabProxies.pyx":479
+  /* "pysam/TabProxies.pyx":525
  *         a = "; ".join( aa ) + ";"
  *         p = a
  *         l = len(a)             # <<<<<<<<<<<<<<
  *         self._attributes = <char *>calloc( l + 1, sizeof(char) )
  *         if self._attributes == NULL:
  */
-  __pyx_t_4 = PyObject_Length(__pyx_v_a); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyObject_Length(__pyx_v_a); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_l = __pyx_t_4;
 
-  /* "pysam/TabProxies.pyx":480
+  /* "pysam/TabProxies.pyx":526
  *         p = a
  *         l = len(a)
  *         self._attributes = <char *>calloc( l + 1, sizeof(char) )             # <<<<<<<<<<<<<<
@@ -6956,7 +7444,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
  */
   __pyx_v_self->_attributes = ((char *)calloc((__pyx_v_l + 1), (sizeof(char))));
 
-  /* "pysam/TabProxies.pyx":481
+  /* "pysam/TabProxies.pyx":527
  *         l = len(a)
  *         self._attributes = <char *>calloc( l + 1, sizeof(char) )
  *         if self._attributes == NULL:             # <<<<<<<<<<<<<<
@@ -6966,21 +7454,21 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
   __pyx_t_10 = ((__pyx_v_self->_attributes == NULL) != 0);
   if (__pyx_t_10) {
 
-    /* "pysam/TabProxies.pyx":482
+    /* "pysam/TabProxies.pyx":528
  *         self._attributes = <char *>calloc( l + 1, sizeof(char) )
  *         if self._attributes == NULL:
  *             raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
  *         memcpy( self._attributes, p, l )
  * 
  */
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__30, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__27, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_Raise(__pyx_t_7, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":483
+  /* "pysam/TabProxies.pyx":529
  *         if self._attributes == NULL:
  *             raise ValueError("out of memory" )
  *         memcpy( self._attributes, p, l )             # <<<<<<<<<<<<<<
@@ -6989,7 +7477,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
  */
   memcpy(__pyx_v_self->_attributes, __pyx_v_p, __pyx_v_l);
 
-  /* "pysam/TabProxies.pyx":485
+  /* "pysam/TabProxies.pyx":531
  *         memcpy( self._attributes, p, l )
  * 
  *         self.hasOwnAttributes = True             # <<<<<<<<<<<<<<
@@ -6998,7 +7486,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
  */
   __pyx_v_self->hasOwnAttributes = 1;
 
-  /* "pysam/TabProxies.pyx":486
+  /* "pysam/TabProxies.pyx":532
  * 
  *         self.hasOwnAttributes = True
  *         self.is_modified = True             # <<<<<<<<<<<<<<
@@ -7007,10 +7495,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
  */
   __pyx_v_self->__pyx_base.is_modified = 1;
 
-  /* "pysam/TabProxies.pyx":461
+  /* "pysam/TabProxies.pyx":507
  *         return result
  * 
- *     def fromDict( self, d ):             # <<<<<<<<<<<<<<
+ *     def fromDict(self, d):             # <<<<<<<<<<<<<<
  *         '''set attributes from a dictionary.'''
  *         cdef char * p
  */
@@ -7036,7 +7524,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_6fromDict(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":488
+/* "pysam/TabProxies.pyx":534
  *         self.is_modified = True
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
@@ -7076,7 +7564,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_8__str__(struct __pyx_ob
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__str__", 0);
 
-  /* "pysam/TabProxies.pyx":492
+  /* "pysam/TabProxies.pyx":538
  *         cdef int x
  * 
  *         if self.is_modified:             # <<<<<<<<<<<<<<
@@ -7086,7 +7574,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_8__str__(struct __pyx_ob
   __pyx_t_1 = (__pyx_v_self->__pyx_base.is_modified != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":493
+    /* "pysam/TabProxies.pyx":539
  * 
  *         if self.is_modified:
  *             return "\t".join(             # <<<<<<<<<<<<<<
@@ -7095,134 +7583,134 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_8__str__(struct __pyx_ob
  */
     __Pyx_XDECREF(__pyx_r);
 
-    /* "pysam/TabProxies.pyx":494
+    /* "pysam/TabProxies.pyx":540
  *         if self.is_modified:
  *             return "\t".join(
  *                 (self.contig,             # <<<<<<<<<<<<<<
  *                  self.source,
  *                  self.feature,
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_contig); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 494; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_contig); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
 
-    /* "pysam/TabProxies.pyx":495
+    /* "pysam/TabProxies.pyx":541
  *             return "\t".join(
  *                 (self.contig,
  *                  self.source,             # <<<<<<<<<<<<<<
  *                  self.feature,
  *                  str(self.start+1),
  */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_source); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_source); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
 
-    /* "pysam/TabProxies.pyx":496
+    /* "pysam/TabProxies.pyx":542
  *                 (self.contig,
  *                  self.source,
  *                  self.feature,             # <<<<<<<<<<<<<<
  *                  str(self.start+1),
  *                  str(self.end),
  */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_feature); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_feature); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
 
-    /* "pysam/TabProxies.pyx":497
+    /* "pysam/TabProxies.pyx":543
  *                  self.source,
  *                  self.feature,
  *                  str(self.start+1),             # <<<<<<<<<<<<<<
  *                  str(self.end),
  *                  toDot(self.score),
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_start); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_start); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_6 = PyNumber_Add(__pyx_t_5, __pyx_int_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyNumber_Add(__pyx_t_5, __pyx_int_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_6);
     __Pyx_GIVEREF(__pyx_t_6);
     __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_5, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_5, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-    /* "pysam/TabProxies.pyx":498
+    /* "pysam/TabProxies.pyx":544
  *                  self.feature,
  *                  str(self.start+1),
  *                  str(self.end),             # <<<<<<<<<<<<<<
  *                  toDot(self.score),
  *                  self.strand,
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_end); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_end); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_5);
     __Pyx_GIVEREF(__pyx_t_5);
     __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_7, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_7, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
 
-    /* "pysam/TabProxies.pyx":499
+    /* "pysam/TabProxies.pyx":545
  *                  str(self.start+1),
  *                  str(self.end),
  *                  toDot(self.score),             # <<<<<<<<<<<<<<
  *                  self.strand,
  *                  self.frame,
  */
-    __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_toDot); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_toDot); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_score); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_score); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
     __Pyx_GIVEREF(__pyx_t_8);
     __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-    /* "pysam/TabProxies.pyx":500
+    /* "pysam/TabProxies.pyx":546
  *                  str(self.end),
  *                  toDot(self.score),
  *                  self.strand,             # <<<<<<<<<<<<<<
  *                  self.frame,
  *                  self.attributes ) )
  */
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_strand); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_strand); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
 
-    /* "pysam/TabProxies.pyx":501
+    /* "pysam/TabProxies.pyx":547
  *                  toDot(self.score),
  *                  self.strand,
  *                  self.frame,             # <<<<<<<<<<<<<<
  *                  self.attributes ) )
  *         else:
  */
-    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_frame); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_frame); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
 
-    /* "pysam/TabProxies.pyx":502
+    /* "pysam/TabProxies.pyx":548
  *                  self.strand,
  *                  self.frame,
  *                  self.attributes ) )             # <<<<<<<<<<<<<<
  *         else:
  *             return TupleProxy.__str__(self)
  */
-    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_attributes); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_attributes); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 548; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
 
-    /* "pysam/TabProxies.pyx":494
+    /* "pysam/TabProxies.pyx":540
  *         if self.is_modified:
  *             return "\t".join(
  *                 (self.contig,             # <<<<<<<<<<<<<<
  *                  self.source,
  *                  self.feature,
  */
-    __pyx_t_11 = PyTuple_New(9); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 494; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = PyTuple_New(9); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
@@ -7252,14 +7740,14 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_8__str__(struct __pyx_ob
     __pyx_t_7 = 0;
     __pyx_t_10 = 0;
 
-    /* "pysam/TabProxies.pyx":493
+    /* "pysam/TabProxies.pyx":539
  * 
  *         if self.is_modified:
  *             return "\t".join(             # <<<<<<<<<<<<<<
  *                 (self.contig,
  *                  self.source,
  */
-    __pyx_t_10 = __Pyx_PyString_Join(__pyx_kp_s__10, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 493; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyString_Join(__pyx_kp_s__8, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 539; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
     __pyx_r = __pyx_t_10;
@@ -7268,22 +7756,22 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_8__str__(struct __pyx_ob
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":504
+    /* "pysam/TabProxies.pyx":550
  *                  self.attributes ) )
  *         else:
  *             return TupleProxy.__str__(self)             # <<<<<<<<<<<<<<
  * 
- *     def invert( self, int lcontig ):
+ *     def invert(self, int lcontig):
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_str); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_str); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __Pyx_INCREF(((PyObject *)__pyx_v_self));
     PyTuple_SET_ITEM(__pyx_t_11, 0, ((PyObject *)__pyx_v_self));
     __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_11, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_11, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
@@ -7292,7 +7780,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_8__str__(struct __pyx_ob
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":488
+  /* "pysam/TabProxies.pyx":534
  *         self.is_modified = True
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
@@ -7320,10 +7808,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_8__str__(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":506
+/* "pysam/TabProxies.pyx":552
  *             return TupleProxy.__str__(self)
  * 
- *     def invert( self, int lcontig ):             # <<<<<<<<<<<<<<
+ *     def invert(self, int lcontig):             # <<<<<<<<<<<<<<
  *         '''invert coordinates to negative strand coordinates
  * 
  */
@@ -7340,7 +7828,7 @@ static PyObject *__pyx_pw_5pysam_10TabProxies_8GTFProxy_11invert(PyObject *__pyx
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("invert (wrapper)", 0);
   assert(__pyx_arg_lcontig); {
-    __pyx_v_lcontig = __Pyx_PyInt_As_int(__pyx_arg_lcontig); if (unlikely((__pyx_v_lcontig == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_lcontig = __Pyx_PyInt_As_int(__pyx_arg_lcontig); if (unlikely((__pyx_v_lcontig == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L3_error:;
@@ -7370,35 +7858,35 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_10invert(struct __pyx_ob
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("invert", 0);
 
-  /* "pysam/TabProxies.pyx":512
+  /* "pysam/TabProxies.pyx":558
  *         negative strand.'''
  * 
  *         if self.strand[0] == '-':             # <<<<<<<<<<<<<<
  *             start = min(self.start, self.end)
  *             end = max(self.start, self.end)
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_strand); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_strand); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_GetItemInt(__pyx_t_1, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = __Pyx_GetItemInt(__pyx_t_1, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s__31, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s__28, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_3) {
 
-    /* "pysam/TabProxies.pyx":513
+    /* "pysam/TabProxies.pyx":559
  * 
  *         if self.strand[0] == '-':
  *             start = min(self.start, self.end)             # <<<<<<<<<<<<<<
  *             end = max(self.start, self.end)
  *             self.start, self.end = lcontig - end, lcontig - start
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_end); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_end); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_LT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_LT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     if (__pyx_t_3) {
       __Pyx_INCREF(__pyx_t_2);
@@ -7415,19 +7903,19 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_10invert(struct __pyx_ob
     __pyx_v_start = __pyx_t_2;
     __pyx_t_2 = 0;
 
-    /* "pysam/TabProxies.pyx":514
+    /* "pysam/TabProxies.pyx":560
  *         if self.strand[0] == '-':
  *             start = min(self.start, self.end)
  *             end = max(self.start, self.end)             # <<<<<<<<<<<<<<
  *             self.start, self.end = lcontig - end, lcontig - start
  * 
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_end); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_end); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_start); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_start); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_t_4, Py_GT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_t_4, Py_GT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     if (__pyx_t_3) {
       __Pyx_INCREF(__pyx_t_2);
@@ -7444,35 +7932,35 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_10invert(struct __pyx_ob
     __pyx_v_end = __pyx_t_2;
     __pyx_t_2 = 0;
 
-    /* "pysam/TabProxies.pyx":515
+    /* "pysam/TabProxies.pyx":561
  *             start = min(self.start, self.end)
  *             end = max(self.start, self.end)
  *             self.start, self.end = lcontig - end, lcontig - start             # <<<<<<<<<<<<<<
  * 
  *     def keys( self ):
  */
-    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_lcontig); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_lcontig); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = PyNumber_Subtract(__pyx_t_2, __pyx_v_end); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_Subtract(__pyx_t_2, __pyx_v_end); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_lcontig); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_lcontig); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_4 = PyNumber_Subtract(__pyx_t_2, __pyx_v_start); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyNumber_Subtract(__pyx_t_2, __pyx_v_start); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_start, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_start, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_end, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_end, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":506
+  /* "pysam/TabProxies.pyx":552
  *             return TupleProxy.__str__(self)
  * 
- *     def invert( self, int lcontig ):             # <<<<<<<<<<<<<<
+ *     def invert(self, int lcontig):             # <<<<<<<<<<<<<<
  *         '''invert coordinates to negative strand coordinates
  * 
  */
@@ -7495,7 +7983,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_10invert(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":517
+/* "pysam/TabProxies.pyx":563
  *             self.start, self.end = lcontig - end, lcontig - start
  * 
  *     def keys( self ):             # <<<<<<<<<<<<<<
@@ -7534,38 +8022,46 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_12keys(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("keys", 0);
 
-  /* "pysam/TabProxies.pyx":519
+  /* "pysam/TabProxies.pyx":565
  *     def keys( self ):
  *         '''return a list of attributes defined in this entry.'''
  *         r = self.attributes             # <<<<<<<<<<<<<<
- *         return [ x.strip().split(" ")[0] for x in r.split(";") if x.strip() != '' ]
- * 
+ *         return [x.strip().split(" ")[0]
+ *                 for x in r.split(";") if x.strip() != '']
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_attributes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_attributes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_r = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":520
+  /* "pysam/TabProxies.pyx":566
  *         '''return a list of attributes defined in this entry.'''
  *         r = self.attributes
- *         return [ x.strip().split(" ")[0] for x in r.split(";") if x.strip() != '' ]             # <<<<<<<<<<<<<<
+ *         return [x.strip().split(" ")[0]             # <<<<<<<<<<<<<<
+ *                 for x in r.split(";") if x.strip() != '']
  * 
- *     def __getitem__(self, key):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_r, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/TabProxies.pyx":567
+ *         r = self.attributes
+ *         return [x.strip().split(" ")[0]
+ *                 for x in r.split(";") if x.strip() != '']             # <<<<<<<<<<<<<<
+ * 
+ *     def __getitem__(self, key):
+ */
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_r, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__32, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__29, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (PyList_CheckExact(__pyx_t_3) || PyTuple_CheckExact(__pyx_t_3)) {
     __pyx_t_2 = __pyx_t_3; __Pyx_INCREF(__pyx_t_2); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_t_5 = Py_TYPE(__pyx_t_2)->tp_iternext;
   }
@@ -7574,16 +8070,16 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_12keys(struct __pyx_obj_
     if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_2)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_3 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_2)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_3 = __pyx_t_5(__pyx_t_2);
@@ -7591,7 +8087,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_12keys(struct __pyx_obj_
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -7599,29 +8095,37 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_12keys(struct __pyx_obj_
     }
     __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_x, __pyx_n_s_strip); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_x, __pyx_n_s_strip); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_7 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_kp_s__3, Py_NE)); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_kp_s_, Py_NE)); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     if (__pyx_t_7) {
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_x, __pyx_n_s_strip); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+      /* "pysam/TabProxies.pyx":566
+ *         '''return a list of attributes defined in this entry.'''
+ *         r = self.attributes
+ *         return [x.strip().split(" ")[0]             # <<<<<<<<<<<<<<
+ *                 for x in r.split(";") if x.strip() != '']
+ * 
+ */
+      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_x, __pyx_n_s_strip); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_split); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_split); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__33, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__30, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_GetItemInt(__pyx_t_3, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_6 = __Pyx_GetItemInt(__pyx_t_3, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       goto __pyx_L5;
     }
@@ -7632,7 +8136,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_12keys(struct __pyx_obj_
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":517
+  /* "pysam/TabProxies.pyx":563
  *             self.start, self.end = lcontig - end, lcontig - start
  * 
  *     def keys( self ):             # <<<<<<<<<<<<<<
@@ -7656,11 +8160,11 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_12keys(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":522
- *         return [ x.strip().split(" ")[0] for x in r.split(";") if x.strip() != '' ]
+/* "pysam/TabProxies.pyx":569
+ *                 for x in r.split(";") if x.strip() != '']
  * 
  *     def __getitem__(self, key):             # <<<<<<<<<<<<<<
- *         return self.__getattr__( key )
+ *         return self.__getattr__(key)
  * 
  */
 
@@ -7688,22 +8192,22 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_14__getitem__(struct __p
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__getitem__", 0);
 
-  /* "pysam/TabProxies.pyx":523
+  /* "pysam/TabProxies.pyx":570
  * 
  *     def __getitem__(self, key):
- *         return self.__getattr__( key )             # <<<<<<<<<<<<<<
+ *         return self.__getattr__(key)             # <<<<<<<<<<<<<<
  * 
- *     def __getattr__(self, item ):
+ *     def __getattr__(self, item):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getattr); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getattr); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_key);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_key);
   __Pyx_GIVEREF(__pyx_v_key);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -7711,11 +8215,11 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_14__getitem__(struct __p
   __pyx_t_3 = 0;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":522
- *         return [ x.strip().split(" ")[0] for x in r.split(";") if x.strip() != '' ]
+  /* "pysam/TabProxies.pyx":569
+ *                 for x in r.split(";") if x.strip() != '']
  * 
  *     def __getitem__(self, key):             # <<<<<<<<<<<<<<
- *         return self.__getattr__( key )
+ *         return self.__getattr__(key)
  * 
  */
 
@@ -7732,10 +8236,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_14__getitem__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":525
- *         return self.__getattr__( key )
+/* "pysam/TabProxies.pyx":572
+ *         return self.__getattr__(key)
  * 
- *     def __getattr__(self, item ):             # <<<<<<<<<<<<<<
+ *     def __getattr__(self, item):             # <<<<<<<<<<<<<<
  *         """Generic lookup of attribute from GFF/GTF attributes
  *         Only called if there *isn't* an attribute with this name
  */
@@ -7773,12 +8277,14 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
   int __pyx_t_4;
   int __pyx_t_5;
   int __pyx_t_6;
+  PyObject *__pyx_t_7 = NULL;
+  struct __pyx_opt_args_5pysam_10TabProxies__force_str __pyx_t_8;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__getattr__", 0);
 
-  /* "pysam/TabProxies.pyx":548
+  /* "pysam/TabProxies.pyx":595
  *         # disappeard after accessing the C data structures
  *         # directly and so did the bug.
  *         cdef char * attributes = self.getAttributes()             # <<<<<<<<<<<<<<
@@ -7787,32 +8293,32 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
  */
   __pyx_v_attributes = ((struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy *)__pyx_v_self->__pyx_base.__pyx_vtab)->getAttributes(__pyx_v_self);
 
-  /* "pysam/TabProxies.pyx":552
+  /* "pysam/TabProxies.pyx":599
  *         # add space in order to make sure
  *         # to not pick up a field that is a prefix of another field
  *         r = _force_bytes(item + " ")             # <<<<<<<<<<<<<<
  *         query = r
  *         start = strstr(attributes, query)
  */
-  __pyx_t_1 = PyNumber_Add(__pyx_v_item, __pyx_kp_s__26); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyNumber_Add(__pyx_v_item, __pyx_kp_s__23); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __pyx_f_5pysam_10TabProxies__force_bytes(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __pyx_f_5pysam_10TabProxies__force_bytes(__pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_r = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":553
+  /* "pysam/TabProxies.pyx":600
  *         # to not pick up a field that is a prefix of another field
  *         r = _force_bytes(item + " ")
  *         query = r             # <<<<<<<<<<<<<<
  *         start = strstr(attributes, query)
  * 
  */
-  __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_v_r); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_v_r); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_query = __pyx_t_3;
 
-  /* "pysam/TabProxies.pyx":554
+  /* "pysam/TabProxies.pyx":601
  *         r = _force_bytes(item + " ")
  *         query = r
  *         start = strstr(attributes, query)             # <<<<<<<<<<<<<<
@@ -7821,40 +8327,40 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
  */
   __pyx_v_start = strstr(__pyx_v_attributes, __pyx_v_query);
 
-  /* "pysam/TabProxies.pyx":556
+  /* "pysam/TabProxies.pyx":603
  *         start = strstr(attributes, query)
  * 
  *         if start == NULL:             # <<<<<<<<<<<<<<
- *             raise AttributeError("'GTFProxy' has no attribute '%s'" % item )
+ *             raise AttributeError("'GTFProxy' has no attribute '%s'" % item)
  * 
  */
   __pyx_t_4 = ((__pyx_v_start == NULL) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/TabProxies.pyx":557
+    /* "pysam/TabProxies.pyx":604
  * 
  *         if start == NULL:
- *             raise AttributeError("'GTFProxy' has no attribute '%s'" % item )             # <<<<<<<<<<<<<<
+ *             raise AttributeError("'GTFProxy' has no attribute '%s'" % item)             # <<<<<<<<<<<<<<
  * 
  *         start += strlen(query)
  */
-    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_GTFProxy_has_no_attribute_s, __pyx_v_item); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_GTFProxy_has_no_attribute_s, __pyx_v_item); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_AttributeError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_AttributeError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":559
- *             raise AttributeError("'GTFProxy' has no attribute '%s'" % item )
+  /* "pysam/TabProxies.pyx":606
+ *             raise AttributeError("'GTFProxy' has no attribute '%s'" % item)
  * 
  *         start += strlen(query)             # <<<<<<<<<<<<<<
  *         # skip gaps before
@@ -7862,7 +8368,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
  */
   __pyx_v_start = (__pyx_v_start + strlen(__pyx_v_query));
 
-  /* "pysam/TabProxies.pyx":561
+  /* "pysam/TabProxies.pyx":608
  *         start += strlen(query)
  *         # skip gaps before
  *         while start[0] == ' ':             # <<<<<<<<<<<<<<
@@ -7873,7 +8379,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
     __pyx_t_4 = (((__pyx_v_start[0]) == ' ') != 0);
     if (!__pyx_t_4) break;
 
-    /* "pysam/TabProxies.pyx":562
+    /* "pysam/TabProxies.pyx":609
  *         # skip gaps before
  *         while start[0] == ' ':
  *             start += 1             # <<<<<<<<<<<<<<
@@ -7883,7 +8389,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
     __pyx_v_start = (__pyx_v_start + 1);
   }
 
-  /* "pysam/TabProxies.pyx":564
+  /* "pysam/TabProxies.pyx":611
  *             start += 1
  * 
  *         if start[0] == '"':             # <<<<<<<<<<<<<<
@@ -7893,30 +8399,30 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
   __pyx_t_4 = (((__pyx_v_start[0]) == '"') != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/TabProxies.pyx":565
+    /* "pysam/TabProxies.pyx":612
  * 
  *         if start[0] == '"':
  *             start += 1             # <<<<<<<<<<<<<<
  *             end = start
- *             while end[0] != '\0' and end[0] != '"': end += 1
+ *             while end[0] != '\0' and end[0] != '"':
  */
     __pyx_v_start = (__pyx_v_start + 1);
 
-    /* "pysam/TabProxies.pyx":566
+    /* "pysam/TabProxies.pyx":613
  *         if start[0] == '"':
  *             start += 1
  *             end = start             # <<<<<<<<<<<<<<
- *             while end[0] != '\0' and end[0] != '"': end += 1
- *             l = end - start
+ *             while end[0] != '\0' and end[0] != '"':
+ *                 end += 1
  */
     __pyx_v_end = __pyx_v_start;
 
-    /* "pysam/TabProxies.pyx":567
+    /* "pysam/TabProxies.pyx":614
  *             start += 1
  *             end = start
- *             while end[0] != '\0' and end[0] != '"': end += 1             # <<<<<<<<<<<<<<
+ *             while end[0] != '\0' and end[0] != '"':             # <<<<<<<<<<<<<<
+ *                 end += 1
  *             l = end - start
- *             result = _force_str(PyBytes_FromStringAndSize( start, l ))
  */
     while (1) {
       __pyx_t_4 = (((__pyx_v_end[0]) != '\x00') != 0);
@@ -7927,39 +8433,68 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
         __pyx_t_6 = __pyx_t_4;
       }
       if (!__pyx_t_6) break;
+
+      /* "pysam/TabProxies.pyx":615
+ *             end = start
+ *             while end[0] != '\0' and end[0] != '"':
+ *                 end += 1             # <<<<<<<<<<<<<<
+ *             l = end - start
+ *             result = _force_str(PyBytes_FromStringAndSize(start, l),
+ */
       __pyx_v_end = (__pyx_v_end + 1);
     }
 
-    /* "pysam/TabProxies.pyx":568
- *             end = start
- *             while end[0] != '\0' and end[0] != '"': end += 1
+    /* "pysam/TabProxies.pyx":616
+ *             while end[0] != '\0' and end[0] != '"':
+ *                 end += 1
  *             l = end - start             # <<<<<<<<<<<<<<
- *             result = _force_str(PyBytes_FromStringAndSize( start, l ))
- *             return result
+ *             result = _force_str(PyBytes_FromStringAndSize(start, l),
+ *                                 self.encoding)
  */
     __pyx_v_l = (__pyx_v_end - __pyx_v_start);
 
-    /* "pysam/TabProxies.pyx":569
- *             while end[0] != '\0' and end[0] != '"': end += 1
+    /* "pysam/TabProxies.pyx":617
+ *                 end += 1
  *             l = end - start
- *             result = _force_str(PyBytes_FromStringAndSize( start, l ))             # <<<<<<<<<<<<<<
+ *             result = _force_str(PyBytes_FromStringAndSize(start, l),             # <<<<<<<<<<<<<<
+ *                                 self.encoding)
  *             return result
- *         else:
  */
-    __pyx_t_2 = PyBytes_FromStringAndSize(__pyx_v_start, __pyx_v_l); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 569; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyBytes_FromStringAndSize(__pyx_v_start, __pyx_v_l); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __pyx_f_5pysam_10TabProxies__force_str(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 569; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_v_result = __pyx_t_1;
-    __pyx_t_1 = 0;
 
-    /* "pysam/TabProxies.pyx":570
+    /* "pysam/TabProxies.pyx":618
+ *             l = end - start
+ *             result = _force_str(PyBytes_FromStringAndSize(start, l),
+ *                                 self.encoding)             # <<<<<<<<<<<<<<
+ *             return result
+ *         else:
+ */
+    __pyx_t_1 = __pyx_v_self->__pyx_base.encoding;
+    __Pyx_INCREF(__pyx_t_1);
+
+    /* "pysam/TabProxies.pyx":617
+ *                 end += 1
  *             l = end - start
- *             result = _force_str(PyBytes_FromStringAndSize( start, l ))
+ *             result = _force_str(PyBytes_FromStringAndSize(start, l),             # <<<<<<<<<<<<<<
+ *                                 self.encoding)
+ *             return result
+ */
+    __pyx_t_8.__pyx_n = 1;
+    __pyx_t_8.encoding = __pyx_t_1;
+    __pyx_t_7 = __pyx_f_5pysam_10TabProxies__force_str(__pyx_t_2, &__pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    __pyx_v_result = __pyx_t_7;
+    __pyx_t_7 = 0;
+
+    /* "pysam/TabProxies.pyx":619
+ *             result = _force_str(PyBytes_FromStringAndSize(start, l),
+ *                                 self.encoding)
  *             return result             # <<<<<<<<<<<<<<
  *         else:
- *             return _force_str(start)
+ *             return _force_str(start, self.encoding)
  */
     __Pyx_XDECREF(__pyx_r);
     __Pyx_INCREF(__pyx_v_result);
@@ -7968,28 +8503,33 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
   }
   /*else*/ {
 
-    /* "pysam/TabProxies.pyx":572
+    /* "pysam/TabProxies.pyx":621
  *             return result
  *         else:
- *             return _force_str(start)             # <<<<<<<<<<<<<<
+ *             return _force_str(start, self.encoding)             # <<<<<<<<<<<<<<
  * 
- *     def setAttribute( self, name, value ):
+ *     def setAttribute(self, name, value):
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 572; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __pyx_f_5pysam_10TabProxies__force_str(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 572; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyBytes_FromString(__pyx_v_start); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __pyx_t_1 = __pyx_v_self->__pyx_base.encoding;
+    __Pyx_INCREF(__pyx_t_1);
+    __pyx_t_8.__pyx_n = 1;
+    __pyx_t_8.encoding = __pyx_t_1;
+    __pyx_t_2 = __pyx_f_5pysam_10TabProxies__force_str(__pyx_t_7, &__pyx_t_8); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/TabProxies.pyx":525
- *         return self.__getattr__( key )
+  /* "pysam/TabProxies.pyx":572
+ *         return self.__getattr__(key)
  * 
- *     def __getattr__(self, item ):             # <<<<<<<<<<<<<<
+ *     def __getattr__(self, item):             # <<<<<<<<<<<<<<
  *         """Generic lookup of attribute from GFF/GTF attributes
  *         Only called if there *isn't* an attribute with this name
  */
@@ -7998,6 +8538,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_7);
   __Pyx_AddTraceback("pysam.TabProxies.GTFProxy.__getattr__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
@@ -8008,10 +8549,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_16__getattr__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":574
- *             return _force_str(start)
+/* "pysam/TabProxies.pyx":623
+ *             return _force_str(start, self.encoding)
  * 
- *     def setAttribute( self, name, value ):             # <<<<<<<<<<<<<<
+ *     def setAttribute(self, name, value):             # <<<<<<<<<<<<<<
  *         '''convenience method to set an attribute.'''
  *         r = self.asDict()
  */
@@ -8048,11 +8589,11 @@ static PyObject *__pyx_pw_5pysam_10TabProxies_8GTFProxy_19setAttribute(PyObject
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("setAttribute", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("setAttribute", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setAttribute") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setAttribute") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -8065,7 +8606,7 @@ static PyObject *__pyx_pw_5pysam_10TabProxies_8GTFProxy_19setAttribute(PyObject
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setAttribute", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("setAttribute", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.TabProxies.GTFProxy.setAttribute", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -8090,54 +8631,54 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_18setAttribute(struct __
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("setAttribute", 0);
 
-  /* "pysam/TabProxies.pyx":576
- *     def setAttribute( self, name, value ):
+  /* "pysam/TabProxies.pyx":625
+ *     def setAttribute(self, name, value):
  *         '''convenience method to set an attribute.'''
  *         r = self.asDict()             # <<<<<<<<<<<<<<
  *         r[name] = value
- *         self.fromDict( r )
+ *         self.fromDict(r)
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_asDict); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_asDict); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_r = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":577
+  /* "pysam/TabProxies.pyx":626
  *         '''convenience method to set an attribute.'''
  *         r = self.asDict()
  *         r[name] = value             # <<<<<<<<<<<<<<
- *         self.fromDict( r )
+ *         self.fromDict(r)
  * 
  */
-  if (unlikely(PyObject_SetItem(__pyx_v_r, __pyx_v_name, __pyx_v_value) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(PyObject_SetItem(__pyx_v_r, __pyx_v_name, __pyx_v_value) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/TabProxies.pyx":578
+  /* "pysam/TabProxies.pyx":627
  *         r = self.asDict()
  *         r[name] = value
- *         self.fromDict( r )             # <<<<<<<<<<<<<<
+ *         self.fromDict(r)             # <<<<<<<<<<<<<<
+ * 
  * 
- * cdef class NamedTupleProxy( TupleProxy ):
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fromDict); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fromDict); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_v_r);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_r);
   __Pyx_GIVEREF(__pyx_v_r);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/TabProxies.pyx":574
- *             return _force_str(start)
+  /* "pysam/TabProxies.pyx":623
+ *             return _force_str(start, self.encoding)
  * 
- *     def setAttribute( self, name, value ):             # <<<<<<<<<<<<<<
+ *     def setAttribute(self, name, value):             # <<<<<<<<<<<<<<
  *         '''convenience method to set an attribute.'''
  *         r = self.asDict()
  */
@@ -8158,10 +8699,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8GTFProxy_18setAttribute(struct __
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":584
+/* "pysam/TabProxies.pyx":634
  *     map_key2field = {}
  * 
- *     def __setattr__(self, key, value ):             # <<<<<<<<<<<<<<
+ *     def __setattr__(self, key, value):             # <<<<<<<<<<<<<<
  *         '''set attribute.'''
  *         cdef int idx
  */
@@ -8200,16 +8741,16 @@ static int __pyx_pf_5pysam_10TabProxies_15NamedTupleProxy___setattr__(struct __p
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__setattr__", 0);
 
-  /* "pysam/TabProxies.pyx":587
+  /* "pysam/TabProxies.pyx":637
  *         '''set attribute.'''
  *         cdef int idx
  *         idx, f = self.map_key2field[key]             # <<<<<<<<<<<<<<
  *         if self.nfields < idx:
- *             raise KeyError( "field %s not set" % key )
+ *             raise KeyError("field %s not set" % key)
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_map_key2field); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_map_key2field); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyObject_GetItem(__pyx_t_1, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = PyObject_GetItem(__pyx_t_1, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if ((likely(PyTuple_CheckExact(__pyx_t_2))) || (PyList_CheckExact(__pyx_t_2))) {
@@ -8222,7 +8763,7 @@ static int __pyx_pf_5pysam_10TabProxies_15NamedTupleProxy___setattr__(struct __p
     if (unlikely(size != 2)) {
       if (size > 2) __Pyx_RaiseTooManyValuesError(2);
       else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     #if CYTHON_COMPILING_IN_CPYTHON
     if (likely(PyTuple_CheckExact(sequence))) {
@@ -8235,15 +8776,15 @@ static int __pyx_pf_5pysam_10TabProxies_15NamedTupleProxy___setattr__(struct __p
     __Pyx_INCREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_t_3);
     #else
-    __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     #endif
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   } else {
     Py_ssize_t index = -1;
-    __pyx_t_4 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_t_5 = Py_TYPE(__pyx_t_4)->tp_iternext;
@@ -8251,7 +8792,7 @@ static int __pyx_pf_5pysam_10TabProxies_15NamedTupleProxy___setattr__(struct __p
     __Pyx_GOTREF(__pyx_t_1);
     index = 1; __pyx_t_3 = __pyx_t_5(__pyx_t_4); if (unlikely(!__pyx_t_3)) goto __pyx_L3_unpacking_failed;
     __Pyx_GOTREF(__pyx_t_3);
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_5(__pyx_t_4), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_IternextUnpackEndCheck(__pyx_t_5(__pyx_t_4), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_5 = NULL;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     goto __pyx_L4_unpacking_done;
@@ -8259,67 +8800,67 @@ static int __pyx_pf_5pysam_10TabProxies_15NamedTupleProxy___setattr__(struct __p
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
     if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_L4_unpacking_done:;
   }
-  __pyx_t_6 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_6 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_6 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_idx = __pyx_t_6;
   __pyx_v_f = __pyx_t_3;
   __pyx_t_3 = 0;
 
-  /* "pysam/TabProxies.pyx":588
+  /* "pysam/TabProxies.pyx":638
  *         cdef int idx
  *         idx, f = self.map_key2field[key]
  *         if self.nfields < idx:             # <<<<<<<<<<<<<<
- *             raise KeyError( "field %s not set" % key )
- *         TupleProxy.__setitem__(self, idx, str(value) )
+ *             raise KeyError("field %s not set" % key)
+ *         TupleProxy.__setitem__(self, idx, str(value))
  */
   __pyx_t_7 = ((__pyx_v_self->__pyx_base.nfields < __pyx_v_idx) != 0);
   if (__pyx_t_7) {
 
-    /* "pysam/TabProxies.pyx":589
+    /* "pysam/TabProxies.pyx":639
  *         idx, f = self.map_key2field[key]
  *         if self.nfields < idx:
- *             raise KeyError( "field %s not set" % key )             # <<<<<<<<<<<<<<
- *         TupleProxy.__setitem__(self, idx, str(value) )
+ *             raise KeyError("field %s not set" % key)             # <<<<<<<<<<<<<<
+ *         TupleProxy.__setitem__(self, idx, str(value))
  * 
  */
-    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_field_s_not_set, __pyx_v_key); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_field_s_not_set, __pyx_v_key); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":590
+  /* "pysam/TabProxies.pyx":640
  *         if self.nfields < idx:
- *             raise KeyError( "field %s not set" % key )
- *         TupleProxy.__setitem__(self, idx, str(value) )             # <<<<<<<<<<<<<<
+ *             raise KeyError("field %s not set" % key)
+ *         TupleProxy.__setitem__(self, idx, str(value))             # <<<<<<<<<<<<<<
  * 
- *     def __getattr__(self, key ):
+ *     def __getattr__(self, key):
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_setitem); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_setitem); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_idx); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_idx); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_4 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyTuple_New(3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_self));
@@ -8330,16 +8871,16 @@ static int __pyx_pf_5pysam_10TabProxies_15NamedTupleProxy___setattr__(struct __p
   __Pyx_GIVEREF(__pyx_t_4);
   __pyx_t_3 = 0;
   __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/TabProxies.pyx":584
+  /* "pysam/TabProxies.pyx":634
  *     map_key2field = {}
  * 
- *     def __setattr__(self, key, value ):             # <<<<<<<<<<<<<<
+ *     def __setattr__(self, key, value):             # <<<<<<<<<<<<<<
  *         '''set attribute.'''
  *         cdef int idx
  */
@@ -8360,10 +8901,10 @@ static int __pyx_pf_5pysam_10TabProxies_15NamedTupleProxy___setattr__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":592
- *         TupleProxy.__setitem__(self, idx, str(value) )
+/* "pysam/TabProxies.pyx":642
+ *         TupleProxy.__setitem__(self, idx, str(value))
  * 
- *     def __getattr__(self, key ):             # <<<<<<<<<<<<<<
+ *     def __getattr__(self, key):             # <<<<<<<<<<<<<<
  *         cdef int idx
  *         idx, f = self.map_key2field[key]
  */
@@ -8393,21 +8934,22 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_15NamedTupleProxy_2__getattr__(str
   PyObject *(*__pyx_t_5)(PyObject *);
   int __pyx_t_6;
   int __pyx_t_7;
+  struct __pyx_opt_args_5pysam_10TabProxies__force_str __pyx_t_8;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__getattr__", 0);
 
-  /* "pysam/TabProxies.pyx":594
- *     def __getattr__(self, key ):
+  /* "pysam/TabProxies.pyx":644
+ *     def __getattr__(self, key):
  *         cdef int idx
  *         idx, f = self.map_key2field[key]             # <<<<<<<<<<<<<<
  *         if self.nfields < idx:
- *             raise KeyError( "field %s not set" % key )
+ *             raise KeyError("field %s not set" % key)
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_map_key2field); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_map_key2field); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyObject_GetItem(__pyx_t_1, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = PyObject_GetItem(__pyx_t_1, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if ((likely(PyTuple_CheckExact(__pyx_t_2))) || (PyList_CheckExact(__pyx_t_2))) {
@@ -8420,7 +8962,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_15NamedTupleProxy_2__getattr__(str
     if (unlikely(size != 2)) {
       if (size > 2) __Pyx_RaiseTooManyValuesError(2);
       else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     #if CYTHON_COMPILING_IN_CPYTHON
     if (likely(PyTuple_CheckExact(sequence))) {
@@ -8433,15 +8975,15 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_15NamedTupleProxy_2__getattr__(str
     __Pyx_INCREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_t_3);
     #else
-    __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     #endif
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   } else {
     Py_ssize_t index = -1;
-    __pyx_t_4 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_t_5 = Py_TYPE(__pyx_t_4)->tp_iternext;
@@ -8449,7 +8991,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_15NamedTupleProxy_2__getattr__(str
     __Pyx_GOTREF(__pyx_t_1);
     index = 1; __pyx_t_3 = __pyx_t_5(__pyx_t_4); if (unlikely(!__pyx_t_3)) goto __pyx_L3_unpacking_failed;
     __Pyx_GOTREF(__pyx_t_3);
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_5(__pyx_t_4), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_IternextUnpackEndCheck(__pyx_t_5(__pyx_t_4), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_5 = NULL;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     goto __pyx_L4_unpacking_done;
@@ -8457,73 +8999,124 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_15NamedTupleProxy_2__getattr__(str
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
     if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_L4_unpacking_done:;
   }
-  __pyx_t_6 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_6 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_6 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_idx = __pyx_t_6;
   __pyx_v_f = __pyx_t_3;
   __pyx_t_3 = 0;
 
-  /* "pysam/TabProxies.pyx":595
+  /* "pysam/TabProxies.pyx":645
  *         cdef int idx
  *         idx, f = self.map_key2field[key]
  *         if self.nfields < idx:             # <<<<<<<<<<<<<<
- *             raise KeyError( "field %s not set" % key )
- *         return f( self.fields[idx] )
+ *             raise KeyError("field %s not set" % key)
+ *         if f == str:
  */
   __pyx_t_7 = ((__pyx_v_self->__pyx_base.nfields < __pyx_v_idx) != 0);
   if (__pyx_t_7) {
 
-    /* "pysam/TabProxies.pyx":596
+    /* "pysam/TabProxies.pyx":646
  *         idx, f = self.map_key2field[key]
  *         if self.nfields < idx:
- *             raise KeyError( "field %s not set" % key )             # <<<<<<<<<<<<<<
- *         return f( self.fields[idx] )
- * 
+ *             raise KeyError("field %s not set" % key)             # <<<<<<<<<<<<<<
+ *         if f == str:
+ *             return _force_str(self.fields[idx],
  */
-    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_field_s_not_set, __pyx_v_key); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_field_s_not_set, __pyx_v_key); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":597
+  /* "pysam/TabProxies.pyx":647
  *         if self.nfields < idx:
- *             raise KeyError( "field %s not set" % key )
- *         return f( self.fields[idx] )             # <<<<<<<<<<<<<<
+ *             raise KeyError("field %s not set" % key)
+ *         if f == str:             # <<<<<<<<<<<<<<
+ *             return _force_str(self.fields[idx],
+ *                               self.encoding)
+ */
+  __pyx_t_2 = PyObject_RichCompare(__pyx_v_f, ((PyObject *)((PyObject*)(&PyString_Type))), Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  if (__pyx_t_7) {
+
+    /* "pysam/TabProxies.pyx":648
+ *             raise KeyError("field %s not set" % key)
+ *         if f == str:
+ *             return _force_str(self.fields[idx],             # <<<<<<<<<<<<<<
+ *                               self.encoding)
+ *         return f(self.fields[idx])
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_2 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[__pyx_v_idx])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+
+    /* "pysam/TabProxies.pyx":649
+ *         if f == str:
+ *             return _force_str(self.fields[idx],
+ *                               self.encoding)             # <<<<<<<<<<<<<<
+ *         return f(self.fields[idx])
+ * 
+ */
+    __pyx_t_3 = __pyx_v_self->__pyx_base.encoding;
+    __Pyx_INCREF(__pyx_t_3);
+
+    /* "pysam/TabProxies.pyx":648
+ *             raise KeyError("field %s not set" % key)
+ *         if f == str:
+ *             return _force_str(self.fields[idx],             # <<<<<<<<<<<<<<
+ *                               self.encoding)
+ *         return f(self.fields[idx])
+ */
+    __pyx_t_8.__pyx_n = 1;
+    __pyx_t_8.encoding = __pyx_t_3;
+    __pyx_t_1 = __pyx_f_5pysam_10TabProxies__force_str(__pyx_t_2, &__pyx_t_8); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __pyx_r = __pyx_t_1;
+    __pyx_t_1 = 0;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/TabProxies.pyx":650
+ *             return _force_str(self.fields[idx],
+ *                               self.encoding)
+ *         return f(self.fields[idx])             # <<<<<<<<<<<<<<
  * 
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[__pyx_v_idx])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[__pyx_v_idx])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_v_f, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
+  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
+  __Pyx_GIVEREF(__pyx_t_1);
+  __pyx_t_1 = 0;
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_v_f, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":592
- *         TupleProxy.__setitem__(self, idx, str(value) )
+  /* "pysam/TabProxies.pyx":642
+ *         TupleProxy.__setitem__(self, idx, str(value))
  * 
- *     def __getattr__(self, key ):             # <<<<<<<<<<<<<<
+ *     def __getattr__(self, key):             # <<<<<<<<<<<<<<
  *         cdef int idx
  *         idx, f = self.map_key2field[key]
  */
@@ -8543,33 +9136,70 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_15NamedTupleProxy_2__getattr__(str
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":619
- *         'blockStarts': (11, bytes), }
+/* "pysam/TabProxies.pyx":672
+ *         'blockStarts': (11, str), }
+ * 
+ *     cdef int getMinFields(self):             # <<<<<<<<<<<<<<
+ *         '''return minimum number of fields.'''
+ *         return 3
+ */
+
+static int __pyx_f_5pysam_10TabProxies_8BedProxy_getMinFields(CYTHON_UNUSED struct __pyx_obj_5pysam_10TabProxies_BedProxy *__pyx_v_self) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("getMinFields", 0);
+
+  /* "pysam/TabProxies.pyx":674
+ *     cdef int getMinFields(self):
+ *         '''return minimum number of fields.'''
+ *         return 3             # <<<<<<<<<<<<<<
+ * 
+ *     cdef int getMaxFields(self):
+ */
+  __pyx_r = 3;
+  goto __pyx_L0;
+
+  /* "pysam/TabProxies.pyx":672
+ *         'blockStarts': (11, str), }
+ * 
+ *     cdef int getMinFields(self):             # <<<<<<<<<<<<<<
+ *         '''return minimum number of fields.'''
+ *         return 3
+ */
+
+  /* function exit code */
+  __pyx_L0:;
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/TabProxies.pyx":676
+ *         return 3
  * 
- *     cdef int getMaxFields( self, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef int getMaxFields(self):             # <<<<<<<<<<<<<<
  *         '''return max number of fields.'''
  *         return 12
  */
 
-static int __pyx_f_5pysam_10TabProxies_8BedProxy_getMaxFields(CYTHON_UNUSED struct __pyx_obj_5pysam_10TabProxies_BedProxy *__pyx_v_self, CYTHON_UNUSED size_t __pyx_v_nbytes) {
+static int __pyx_f_5pysam_10TabProxies_8BedProxy_getMaxFields(CYTHON_UNUSED struct __pyx_obj_5pysam_10TabProxies_BedProxy *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("getMaxFields", 0);
 
-  /* "pysam/TabProxies.pyx":621
- *     cdef int getMaxFields( self, size_t nbytes ):
+  /* "pysam/TabProxies.pyx":678
+ *     cdef int getMaxFields(self):
  *         '''return max number of fields.'''
  *         return 12             # <<<<<<<<<<<<<<
  * 
- *     cdef update( self, char * buffer, size_t nbytes ):
+ *     cdef update(self, char * buffer, size_t nbytes):
  */
   __pyx_r = 12;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":619
- *         'blockStarts': (11, bytes), }
+  /* "pysam/TabProxies.pyx":676
+ *         return 3
  * 
- *     cdef int getMaxFields( self, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef int getMaxFields(self):             # <<<<<<<<<<<<<<
  *         '''return max number of fields.'''
  *         return 12
  */
@@ -8580,10 +9210,10 @@ static int __pyx_f_5pysam_10TabProxies_8BedProxy_getMaxFields(CYTHON_UNUSED stru
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":623
+/* "pysam/TabProxies.pyx":680
  *         return 12
  * 
- *     cdef update( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef update(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''update internal data.
  * 
  */
@@ -8599,42 +9229,42 @@ static PyObject *__pyx_f_5pysam_10TabProxies_8BedProxy_update(struct __pyx_obj_5
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("update", 0);
 
-  /* "pysam/TabProxies.pyx":628
+  /* "pysam/TabProxies.pyx":685
  *         nbytes does not include the terminal '\0'.
  *         '''
- *         TupleProxy.update( self, buffer, nbytes )             # <<<<<<<<<<<<<<
+ *         TupleProxy.update(self, buffer, nbytes)             # <<<<<<<<<<<<<<
  * 
  *         if self.nfields < 3:
  */
-  __pyx_t_1 = __pyx_vtabptr_5pysam_10TabProxies_TupleProxy->update(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_self), __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_vtabptr_5pysam_10TabProxies_TupleProxy->update(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_self), __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":630
- *         TupleProxy.update( self, buffer, nbytes )
+  /* "pysam/TabProxies.pyx":687
+ *         TupleProxy.update(self, buffer, nbytes)
  * 
  *         if self.nfields < 3:             # <<<<<<<<<<<<<<
- *             raise ValueError( "bed format requires at least three columns" )
- * 
+ *             raise ValueError(
+ *                 "bed format requires at least three columns")
  */
   __pyx_t_2 = ((__pyx_v_self->__pyx_base.__pyx_base.nfields < 3) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/TabProxies.pyx":631
+    /* "pysam/TabProxies.pyx":688
  * 
  *         if self.nfields < 3:
- *             raise ValueError( "bed format requires at least three columns" )             # <<<<<<<<<<<<<<
+ *             raise ValueError(             # <<<<<<<<<<<<<<
+ *                 "bed format requires at least three columns")
  * 
- *         # determines bed format
  */
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__34, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__31, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_Raise(__pyx_t_1, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/TabProxies.pyx":634
+  /* "pysam/TabProxies.pyx":692
  * 
  *         # determines bed format
  *         self.bedfields = self.nfields             # <<<<<<<<<<<<<<
@@ -8644,7 +9274,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_8BedProxy_update(struct __pyx_obj_5
   __pyx_t_3 = __pyx_v_self->__pyx_base.__pyx_base.nfields;
   __pyx_v_self->bedfields = __pyx_t_3;
 
-  /* "pysam/TabProxies.pyx":637
+  /* "pysam/TabProxies.pyx":695
  * 
  *         # do automatic conversion
  *         self.contig = self.fields[0]             # <<<<<<<<<<<<<<
@@ -8653,7 +9283,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_8BedProxy_update(struct __pyx_obj_5
  */
   __pyx_v_self->contig = (__pyx_v_self->__pyx_base.__pyx_base.fields[0]);
 
-  /* "pysam/TabProxies.pyx":638
+  /* "pysam/TabProxies.pyx":696
  *         # do automatic conversion
  *         self.contig = self.fields[0]
  *         self.start = atoi( self.fields[1] )             # <<<<<<<<<<<<<<
@@ -8662,7 +9292,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_8BedProxy_update(struct __pyx_obj_5
  */
   __pyx_v_self->start = atoi((__pyx_v_self->__pyx_base.__pyx_base.fields[1]));
 
-  /* "pysam/TabProxies.pyx":639
+  /* "pysam/TabProxies.pyx":697
  *         self.contig = self.fields[0]
  *         self.start = atoi( self.fields[1] )
  *         self.end = atoi( self.fields[2] )             # <<<<<<<<<<<<<<
@@ -8671,10 +9301,10 @@ static PyObject *__pyx_f_5pysam_10TabProxies_8BedProxy_update(struct __pyx_obj_5
  */
   __pyx_v_self->end = atoi((__pyx_v_self->__pyx_base.__pyx_base.fields[2]));
 
-  /* "pysam/TabProxies.pyx":623
+  /* "pysam/TabProxies.pyx":680
  *         return 12
  * 
- *     cdef update( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef update(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''update internal data.
  * 
  */
@@ -8692,7 +9322,7 @@ static PyObject *__pyx_f_5pysam_10TabProxies_8BedProxy_update(struct __pyx_obj_5
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":648
+/* "pysam/TabProxies.pyx":706
  *     #    def __get__( self ): return self.end
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
@@ -8727,7 +9357,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8BedProxy___str__(struct __pyx_obj
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__str__", 0);
 
-  /* "pysam/TabProxies.pyx":650
+  /* "pysam/TabProxies.pyx":708
  *     def __str__(self):
  * 
  *         cdef int save_fields = self.nfields             # <<<<<<<<<<<<<<
@@ -8737,7 +9367,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8BedProxy___str__(struct __pyx_obj
   __pyx_t_1 = __pyx_v_self->__pyx_base.__pyx_base.nfields;
   __pyx_v_save_fields = __pyx_t_1;
 
-  /* "pysam/TabProxies.pyx":652
+  /* "pysam/TabProxies.pyx":710
  *         cdef int save_fields = self.nfields
  *         # ensure fields to use correct format
  *         self.nfields = self.bedfields             # <<<<<<<<<<<<<<
@@ -8747,28 +9377,28 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8BedProxy___str__(struct __pyx_obj
   __pyx_t_1 = __pyx_v_self->bedfields;
   __pyx_v_self->__pyx_base.__pyx_base.nfields = __pyx_t_1;
 
-  /* "pysam/TabProxies.pyx":653
+  /* "pysam/TabProxies.pyx":711
  *         # ensure fields to use correct format
  *         self.nfields = self.bedfields
  *         retval = TupleProxy.__str__( self )             # <<<<<<<<<<<<<<
  *         self.nfields = save_fields
  *         return retval
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_str); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_str); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_v_self));
   __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_v_retval = __pyx_t_4;
   __pyx_t_4 = 0;
 
-  /* "pysam/TabProxies.pyx":654
+  /* "pysam/TabProxies.pyx":712
  *         self.nfields = self.bedfields
  *         retval = TupleProxy.__str__( self )
  *         self.nfields = save_fields             # <<<<<<<<<<<<<<
@@ -8777,7 +9407,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8BedProxy___str__(struct __pyx_obj
  */
   __pyx_v_self->__pyx_base.__pyx_base.nfields = __pyx_v_save_fields;
 
-  /* "pysam/TabProxies.pyx":655
+  /* "pysam/TabProxies.pyx":713
  *         retval = TupleProxy.__str__( self )
  *         self.nfields = save_fields
  *         return retval             # <<<<<<<<<<<<<<
@@ -8789,7 +9419,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8BedProxy___str__(struct __pyx_obj
   __pyx_r = __pyx_v_retval;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":648
+  /* "pysam/TabProxies.pyx":706
  *     #    def __get__( self ): return self.end
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
@@ -8811,7 +9441,7 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8BedProxy___str__(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":657
+/* "pysam/TabProxies.pyx":715
  *         return retval
  * 
  *     def __setattr__(self, key, value ):             # <<<<<<<<<<<<<<
@@ -8854,45 +9484,45 @@ static int __pyx_pf_5pysam_10TabProxies_8BedProxy_2__setattr__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__setattr__", 0);
 
-  /* "pysam/TabProxies.pyx":659
+  /* "pysam/TabProxies.pyx":717
  *     def __setattr__(self, key, value ):
  *         '''set attribute.'''
  *         if key == "start": self.start = value             # <<<<<<<<<<<<<<
  *         elif key == "end": self.end = value
  * 
  */
-  __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_start, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_start, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_1) {
-    __pyx_t_2 = __Pyx_PyInt_As_uint32_t(__pyx_v_value); if (unlikely((__pyx_t_2 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_As_uint32_t(__pyx_v_value); if (unlikely((__pyx_t_2 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_self->start = __pyx_t_2;
     goto __pyx_L3;
   }
 
-  /* "pysam/TabProxies.pyx":660
+  /* "pysam/TabProxies.pyx":718
  *         '''set attribute.'''
  *         if key == "start": self.start = value
  *         elif key == "end": self.end = value             # <<<<<<<<<<<<<<
  * 
  *         cdef int idx
  */
-  __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_end, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 660; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_end, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_1) {
-    __pyx_t_2 = __Pyx_PyInt_As_uint32_t(__pyx_v_value); if (unlikely((__pyx_t_2 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 660; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_As_uint32_t(__pyx_v_value); if (unlikely((__pyx_t_2 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_self->end = __pyx_t_2;
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":663
+  /* "pysam/TabProxies.pyx":721
  * 
  *         cdef int idx
  *         idx, f = self.map_key2field[key]             # <<<<<<<<<<<<<<
  *         TupleProxy._setindex(self, idx, str(value) )
  * 
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_map_key2field); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_map_key2field); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyObject_GetItem(__pyx_t_3, __pyx_v_key); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_4 = PyObject_GetItem(__pyx_t_3, __pyx_v_key); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if ((likely(PyTuple_CheckExact(__pyx_t_4))) || (PyList_CheckExact(__pyx_t_4))) {
@@ -8905,7 +9535,7 @@ static int __pyx_pf_5pysam_10TabProxies_8BedProxy_2__setattr__(struct __pyx_obj_
     if (unlikely(size != 2)) {
       if (size > 2) __Pyx_RaiseTooManyValuesError(2);
       else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     #if CYTHON_COMPILING_IN_CPYTHON
     if (likely(PyTuple_CheckExact(sequence))) {
@@ -8918,15 +9548,15 @@ static int __pyx_pf_5pysam_10TabProxies_8BedProxy_2__setattr__(struct __pyx_obj_
     __Pyx_INCREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_t_5);
     #else
-    __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_5 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     #endif
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   } else {
     Py_ssize_t index = -1;
-    __pyx_t_6 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __pyx_t_7 = Py_TYPE(__pyx_t_6)->tp_iternext;
@@ -8934,7 +9564,7 @@ static int __pyx_pf_5pysam_10TabProxies_8BedProxy_2__setattr__(struct __pyx_obj_
     __Pyx_GOTREF(__pyx_t_3);
     index = 1; __pyx_t_5 = __pyx_t_7(__pyx_t_6); if (unlikely(!__pyx_t_5)) goto __pyx_L4_unpacking_failed;
     __Pyx_GOTREF(__pyx_t_5);
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_7(__pyx_t_6), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_IternextUnpackEndCheck(__pyx_t_7(__pyx_t_6), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_7 = NULL;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     goto __pyx_L5_unpacking_done;
@@ -8942,35 +9572,35 @@ static int __pyx_pf_5pysam_10TabProxies_8BedProxy_2__setattr__(struct __pyx_obj_
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __pyx_t_7 = NULL;
     if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_L5_unpacking_done:;
   }
-  __pyx_t_8 = __Pyx_PyInt_As_int(__pyx_t_3); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyInt_As_int(__pyx_t_3); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_v_idx = __pyx_t_8;
   __pyx_v_f = __pyx_t_5;
   __pyx_t_5 = 0;
 
-  /* "pysam/TabProxies.pyx":664
+  /* "pysam/TabProxies.pyx":722
  *         cdef int idx
  *         idx, f = self.map_key2field[key]
  *         TupleProxy._setindex(self, idx, str(value) )             # <<<<<<<<<<<<<<
  * 
- * cdef class VCFProxy( NamedTupleProxy ):
+ * cdef class VCFProxy(NamedTupleProxy):
  */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_setindex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_setindex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = __Pyx_PyInt_From_int(__pyx_v_idx); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyInt_From_int(__pyx_v_idx); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = PyTuple_New(3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_v_self));
@@ -8981,13 +9611,13 @@ static int __pyx_pf_5pysam_10TabProxies_8BedProxy_2__setattr__(struct __pyx_obj_
   __Pyx_GIVEREF(__pyx_t_6);
   __pyx_t_5 = 0;
   __pyx_t_6 = 0;
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-  /* "pysam/TabProxies.pyx":657
+  /* "pysam/TabProxies.pyx":715
  *         return retval
  * 
  *     def __setattr__(self, key, value ):             # <<<<<<<<<<<<<<
@@ -9011,10 +9641,10 @@ static int __pyx_pf_5pysam_10TabProxies_8BedProxy_2__setattr__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":682
- *         'format' : (8, bytes) }
+/* "pysam/TabProxies.pyx":741
+ *         'format' : (8, str) }
  * 
- *     def __cinit__(self ):             # <<<<<<<<<<<<<<
+ *     def __cinit__(self):             # <<<<<<<<<<<<<<
  *         # automatically calls TupleProxy.__cinit__
  *         # start indexed access at genotypes
  */
@@ -9040,19 +9670,19 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy___cinit__(struct __pyx_obj_5py
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__cinit__", 0);
 
-  /* "pysam/TabProxies.pyx":685
+  /* "pysam/TabProxies.pyx":744
  *         # automatically calls TupleProxy.__cinit__
  *         # start indexed access at genotypes
  *         self.offset = 9             # <<<<<<<<<<<<<<
  * 
- *     cdef update( self, char * buffer, size_t nbytes ):
+ *     cdef update(self, char * buffer, size_t nbytes):
  */
   __pyx_v_self->__pyx_base.__pyx_base.offset = 9;
 
-  /* "pysam/TabProxies.pyx":682
- *         'format' : (8, bytes) }
+  /* "pysam/TabProxies.pyx":741
+ *         'format' : (8, str) }
  * 
- *     def __cinit__(self ):             # <<<<<<<<<<<<<<
+ *     def __cinit__(self):             # <<<<<<<<<<<<<<
  *         # automatically calls TupleProxy.__cinit__
  *         # start indexed access at genotypes
  */
@@ -9063,10 +9693,10 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy___cinit__(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":687
+/* "pysam/TabProxies.pyx":746
  *         self.offset = 9
  * 
- *     cdef update( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef update(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''update internal data.
  * 
  */
@@ -9080,39 +9710,39 @@ static PyObject *__pyx_f_5pysam_10TabProxies_8VCFProxy_update(struct __pyx_obj_5
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("update", 0);
 
-  /* "pysam/TabProxies.pyx":692
+  /* "pysam/TabProxies.pyx":751
  *         nbytes does not include the terminal '\0'.
  *         '''
- *         TupleProxy.update( self, buffer, nbytes )             # <<<<<<<<<<<<<<
+ *         TupleProxy.update(self, buffer, nbytes)             # <<<<<<<<<<<<<<
  * 
  *         self.contig = self.fields[0]
  */
-  __pyx_t_1 = __pyx_vtabptr_5pysam_10TabProxies_TupleProxy->update(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_self), __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_vtabptr_5pysam_10TabProxies_TupleProxy->update(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_self), __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":694
- *         TupleProxy.update( self, buffer, nbytes )
+  /* "pysam/TabProxies.pyx":753
+ *         TupleProxy.update(self, buffer, nbytes)
  * 
  *         self.contig = self.fields[0]             # <<<<<<<<<<<<<<
  *         # vcf counts from 1 - correct here
- *         self.pos = atoi( self.fields[1] ) - 1
+ *         self.pos = atoi(self.fields[1]) - 1
  */
   __pyx_v_self->contig = (__pyx_v_self->__pyx_base.__pyx_base.fields[0]);
 
-  /* "pysam/TabProxies.pyx":696
+  /* "pysam/TabProxies.pyx":755
  *         self.contig = self.fields[0]
  *         # vcf counts from 1 - correct here
- *         self.pos = atoi( self.fields[1] ) - 1             # <<<<<<<<<<<<<<
+ *         self.pos = atoi(self.fields[1]) - 1             # <<<<<<<<<<<<<<
  * 
  *     def __len__(self):
  */
   __pyx_v_self->pos = (atoi((__pyx_v_self->__pyx_base.__pyx_base.fields[1])) - 1);
 
-  /* "pysam/TabProxies.pyx":687
+  /* "pysam/TabProxies.pyx":746
  *         self.offset = 9
  * 
- *     cdef update( self, char * buffer, size_t nbytes ):             # <<<<<<<<<<<<<<
+ *     cdef update(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
  *         '''update internal data.
  * 
  */
@@ -9130,8 +9760,8 @@ static PyObject *__pyx_f_5pysam_10TabProxies_8VCFProxy_update(struct __pyx_obj_5
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":698
- *         self.pos = atoi( self.fields[1] ) - 1
+/* "pysam/TabProxies.pyx":757
+ *         self.pos = atoi(self.fields[1]) - 1
  * 
  *     def __len__(self):             # <<<<<<<<<<<<<<
  *         '''return number of genotype fields.'''
@@ -9163,7 +9793,7 @@ static Py_ssize_t __pyx_pf_5pysam_10TabProxies_8VCFProxy_2__len__(struct __pyx_o
   long __pyx_t_3;
   __Pyx_RefNannySetupContext("__len__", 0);
 
-  /* "pysam/TabProxies.pyx":700
+  /* "pysam/TabProxies.pyx":759
  *     def __len__(self):
  *         '''return number of genotype fields.'''
  *         return max(0, self.nfields - 9)             # <<<<<<<<<<<<<<
@@ -9180,8 +9810,8 @@ static Py_ssize_t __pyx_pf_5pysam_10TabProxies_8VCFProxy_2__len__(struct __pyx_o
   __pyx_r = __pyx_t_3;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":698
- *         self.pos = atoi( self.fields[1] ) - 1
+  /* "pysam/TabProxies.pyx":757
+ *         self.pos = atoi(self.fields[1]) - 1
  * 
  *     def __len__(self):             # <<<<<<<<<<<<<<
  *         '''return number of genotype fields.'''
@@ -9194,10 +9824,10 @@ static Py_ssize_t __pyx_pf_5pysam_10TabProxies_8VCFProxy_2__len__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":704
+/* "pysam/TabProxies.pyx":763
  *     property pos:
  *        '''feature end (in 0-based open/closed coordinates).'''
- *        def __get__( self ):             # <<<<<<<<<<<<<<
+ *        def __get__(self):             # <<<<<<<<<<<<<<
  *            return self.pos
  * 
  */
@@ -9224,24 +9854,24 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8VCFProxy_3pos___get__(struct __py
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/TabProxies.pyx":705
+  /* "pysam/TabProxies.pyx":764
  *        '''feature end (in 0-based open/closed coordinates).'''
- *        def __get__( self ):
+ *        def __get__(self):
  *            return self.pos             # <<<<<<<<<<<<<<
  * 
- *     def __setattr__(self, key, value ):
+ *     def __setattr__(self, key, value):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 764; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/TabProxies.pyx":704
+  /* "pysam/TabProxies.pyx":763
  *     property pos:
  *        '''feature end (in 0-based open/closed coordinates).'''
- *        def __get__( self ):             # <<<<<<<<<<<<<<
+ *        def __get__(self):             # <<<<<<<<<<<<<<
  *            return self.pos
  * 
  */
@@ -9257,10 +9887,10 @@ static PyObject *__pyx_pf_5pysam_10TabProxies_8VCFProxy_3pos___get__(struct __py
   return __pyx_r;
 }
 
-/* "pysam/TabProxies.pyx":707
+/* "pysam/TabProxies.pyx":766
  *            return self.pos
  * 
- *     def __setattr__(self, key, value ):             # <<<<<<<<<<<<<<
+ *     def __setattr__(self, key, value):             # <<<<<<<<<<<<<<
  *         '''set attribute.'''
  *         if key == "pos":
  */
@@ -9301,34 +9931,34 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy_4__setattr__(struct __pyx_obj_
   __Pyx_RefNannySetupContext("__setattr__", 0);
   __Pyx_INCREF(__pyx_v_value);
 
-  /* "pysam/TabProxies.pyx":709
- *     def __setattr__(self, key, value ):
+  /* "pysam/TabProxies.pyx":768
+ *     def __setattr__(self, key, value):
  *         '''set attribute.'''
  *         if key == "pos":             # <<<<<<<<<<<<<<
  *             self.pos = value
  *             value += 1
  */
-  __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_pos, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_pos, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_1) {
 
-    /* "pysam/TabProxies.pyx":710
+    /* "pysam/TabProxies.pyx":769
  *         '''set attribute.'''
  *         if key == "pos":
  *             self.pos = value             # <<<<<<<<<<<<<<
  *             value += 1
  * 
  */
-    __pyx_t_2 = __Pyx_PyInt_As_uint32_t(__pyx_v_value); if (unlikely((__pyx_t_2 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_As_uint32_t(__pyx_v_value); if (unlikely((__pyx_t_2 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_self->pos = __pyx_t_2;
 
-    /* "pysam/TabProxies.pyx":711
+    /* "pysam/TabProxies.pyx":770
  *         if key == "pos":
  *             self.pos = value
  *             value += 1             # <<<<<<<<<<<<<<
  * 
  *         cdef int idx
  */
-    __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_v_value, __pyx_int_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_v_value, __pyx_int_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_3);
     __pyx_t_3 = 0;
@@ -9336,16 +9966,16 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy_4__setattr__(struct __pyx_obj_
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":714
+  /* "pysam/TabProxies.pyx":773
  * 
  *         cdef int idx
  *         idx, f = self.map_key2field[key]             # <<<<<<<<<<<<<<
- *         TupleProxy._setindex(self, idx, str(value) )
+ *         TupleProxy._setindex(self, idx, str(value))
  * 
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_map_key2field); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_map_key2field); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyObject_GetItem(__pyx_t_3, __pyx_v_key); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_4 = PyObject_GetItem(__pyx_t_3, __pyx_v_key); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if ((likely(PyTuple_CheckExact(__pyx_t_4))) || (PyList_CheckExact(__pyx_t_4))) {
@@ -9358,7 +9988,7 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy_4__setattr__(struct __pyx_obj_
     if (unlikely(size != 2)) {
       if (size > 2) __Pyx_RaiseTooManyValuesError(2);
       else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     #if CYTHON_COMPILING_IN_CPYTHON
     if (likely(PyTuple_CheckExact(sequence))) {
@@ -9371,15 +10001,15 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy_4__setattr__(struct __pyx_obj_
     __Pyx_INCREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_t_5);
     #else
-    __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_5 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     #endif
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   } else {
     Py_ssize_t index = -1;
-    __pyx_t_6 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __pyx_t_7 = Py_TYPE(__pyx_t_6)->tp_iternext;
@@ -9387,7 +10017,7 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy_4__setattr__(struct __pyx_obj_
     __Pyx_GOTREF(__pyx_t_3);
     index = 1; __pyx_t_5 = __pyx_t_7(__pyx_t_6); if (unlikely(!__pyx_t_5)) goto __pyx_L4_unpacking_failed;
     __Pyx_GOTREF(__pyx_t_5);
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_7(__pyx_t_6), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_IternextUnpackEndCheck(__pyx_t_7(__pyx_t_6), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_7 = NULL;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     goto __pyx_L5_unpacking_done;
@@ -9395,34 +10025,34 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy_4__setattr__(struct __pyx_obj_
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __pyx_t_7 = NULL;
     if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_L5_unpacking_done:;
   }
-  __pyx_t_8 = __Pyx_PyInt_As_int(__pyx_t_3); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyInt_As_int(__pyx_t_3); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_v_idx = __pyx_t_8;
   __pyx_v_f = __pyx_t_5;
   __pyx_t_5 = 0;
 
-  /* "pysam/TabProxies.pyx":715
+  /* "pysam/TabProxies.pyx":774
  *         cdef int idx
  *         idx, f = self.map_key2field[key]
- *         TupleProxy._setindex(self, idx, str(value) )             # <<<<<<<<<<<<<<
+ *         TupleProxy._setindex(self, idx, str(value))             # <<<<<<<<<<<<<<
  * 
  */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_setindex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_n_s_setindex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = __Pyx_PyInt_From_int(__pyx_v_idx); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyInt_From_int(__pyx_v_idx); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_v_value);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = PyTuple_New(3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_v_self));
@@ -9433,16 +10063,16 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy_4__setattr__(struct __pyx_obj_
   __Pyx_GIVEREF(__pyx_t_6);
   __pyx_t_5 = 0;
   __pyx_t_6 = 0;
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-  /* "pysam/TabProxies.pyx":707
+  /* "pysam/TabProxies.pyx":766
  *            return self.pos
  * 
- *     def __setattr__(self, key, value ):             # <<<<<<<<<<<<<<
+ *     def __setattr__(self, key, value):             # <<<<<<<<<<<<<<
  *         '''set attribute.'''
  *         if key == "pos":
  */
@@ -9465,7 +10095,7 @@ static int __pyx_pf_5pysam_10TabProxies_8VCFProxy_4__setattr__(struct __pyx_obj_
 }
 static struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy __pyx_vtable_5pysam_10TabProxies_TupleProxy;
 
-static PyObject *__pyx_tp_new_5pysam_10TabProxies_TupleProxy(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
+static PyObject *__pyx_tp_new_5pysam_10TabProxies_TupleProxy(PyTypeObject *t, PyObject *a, PyObject *k) {
   struct __pyx_obj_5pysam_10TabProxies_TupleProxy *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
@@ -9476,18 +10106,21 @@ static PyObject *__pyx_tp_new_5pysam_10TabProxies_TupleProxy(PyTypeObject *t, CY
   if (unlikely(!o)) return 0;
   p = ((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)o);
   p->__pyx_vtab = __pyx_vtabptr_5pysam_10TabProxies_TupleProxy;
-  if (unlikely(__pyx_pw_5pysam_10TabProxies_10TupleProxy_1__cinit__(o, __pyx_empty_tuple, NULL) < 0)) {
+  p->encoding = Py_None; Py_INCREF(Py_None);
+  if (unlikely(__pyx_pw_5pysam_10TabProxies_10TupleProxy_1__cinit__(o, a, k) < 0)) {
     Py_DECREF(o); o = 0;
   }
   return o;
 }
 
 static void __pyx_tp_dealloc_5pysam_10TabProxies_TupleProxy(PyObject *o) {
+  struct __pyx_obj_5pysam_10TabProxies_TupleProxy *p = (struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)o;
   #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
+  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
   }
   #endif
+  PyObject_GC_UnTrack(o);
   {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
@@ -9496,8 +10129,27 @@ static void __pyx_tp_dealloc_5pysam_10TabProxies_TupleProxy(PyObject *o) {
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
+  Py_CLEAR(p->encoding);
   (*Py_TYPE(o)->tp_free)(o);
 }
+
+static int __pyx_tp_traverse_5pysam_10TabProxies_TupleProxy(PyObject *o, visitproc v, void *a) {
+  int e;
+  struct __pyx_obj_5pysam_10TabProxies_TupleProxy *p = (struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)o;
+  if (p->encoding) {
+    e = (*v)(p->encoding, a); if (e) return e;
+  }
+  return 0;
+}
+
+static int __pyx_tp_clear_5pysam_10TabProxies_TupleProxy(PyObject *o) {
+  PyObject* tmp;
+  struct __pyx_obj_5pysam_10TabProxies_TupleProxy *p = (struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)o;
+  tmp = ((PyObject*)p->encoding);
+  p->encoding = Py_None; Py_INCREF(Py_None);
+  Py_XDECREF(tmp);
+  return 0;
+}
 static PyObject *__pyx_sq_item_5pysam_10TabProxies_TupleProxy(PyObject *o, Py_ssize_t i) {
   PyObject *r;
   PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0;
@@ -9567,10 +10219,10 @@ static PyTypeObject __pyx_type_5pysam_10TabProxies_TupleProxy = {
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
   __Pyx_DOCSTR("Proxy class for access to parsed row as a tuple.\n\n    This class represents a table row for fast read-access.\n\n    Access to individual fields is via the [] operator.\n    \n    Only read-only access is implemented.\n\n    "), /*tp_doc*/
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  __pyx_tp_traverse_5pysam_10TabProxies_TupleProxy, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_10TabProxies_TupleProxy, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   __pyx_pw_5pysam_10TabProxies_10TupleProxy_15__iter__, /*tp_iter*/
@@ -9617,10 +10269,11 @@ static PyObject *__pyx_tp_new_5pysam_10TabProxies_GTFProxy(PyTypeObject *t, PyOb
 
 static void __pyx_tp_dealloc_5pysam_10TabProxies_GTFProxy(PyObject *o) {
   #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
+  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
   }
   #endif
+  PyObject_GC_UnTrack(o);
   {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
@@ -9629,6 +10282,7 @@ static void __pyx_tp_dealloc_5pysam_10TabProxies_GTFProxy(PyObject *o) {
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
+  PyObject_GC_Track(o);
   __pyx_tp_dealloc_5pysam_10TabProxies_TupleProxy(o);
 }
 static PyObject *__pyx_sq_item_5pysam_10TabProxies_GTFProxy(PyObject *o, Py_ssize_t i) {
@@ -9848,10 +10502,10 @@ static PyTypeObject __pyx_type_5pysam_10TabProxies_GTFProxy = {
   __pyx_tp_getattro_5pysam_10TabProxies_GTFProxy, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
   __Pyx_DOCSTR("Proxy class for access to GTF fields.\n\n    This class represents a GTF entry for fast read-access.\n    Write-access has been added as well, though some care must\n    be taken. If any of the string fields (contig, source, ...)\n    are set, the new value is tied to the lifetime of the\n    argument that was supplied.\n\n    The only exception is the attributes field when set from\n    a dictionary - this field will manage its own memory.\n    "), /*tp_doc*/
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  __pyx_tp_traverse_5pysam_10TabProxies_TupleProxy, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_10TabProxies_TupleProxy, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   #if CYTHON_COMPILING_IN_PYPY
@@ -9954,10 +10608,10 @@ static PyTypeObject __pyx_type_5pysam_10TabProxies_NamedTupleProxy = {
   __pyx_tp_getattro_5pysam_10TabProxies_NamedTupleProxy, /*tp_getattro*/
   __pyx_tp_setattro_5pysam_10TabProxies_NamedTupleProxy, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
   0, /*tp_doc*/
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  __pyx_tp_traverse_5pysam_10TabProxies_TupleProxy, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_10TabProxies_TupleProxy, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   #if CYTHON_COMPILING_IN_PYPY
@@ -10046,10 +10700,10 @@ static PyTypeObject __pyx_type_5pysam_10TabProxies_BedProxy = {
   0, /*tp_getattro*/
   __pyx_tp_setattro_5pysam_10TabProxies_BedProxy, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
   __Pyx_DOCSTR("Proxy class for access to Bed fields.\n\n    This class represents a BED entry for fast read-access.\n    "), /*tp_doc*/
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  __pyx_tp_traverse_5pysam_10TabProxies_TupleProxy, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_10TabProxies_TupleProxy, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   #if CYTHON_COMPILING_IN_PYPY
@@ -10177,10 +10831,10 @@ static PyTypeObject __pyx_type_5pysam_10TabProxies_VCFProxy = {
   0, /*tp_getattro*/
   __pyx_tp_setattro_5pysam_10TabProxies_VCFProxy, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("Proxy class for access to VCF fields.\n\n    The genotypes are accessed via index.\n    "), /*tp_doc*/
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  __Pyx_DOCSTR("Proxy class for access to VCF fields.\n\n    The genotypes are accessed via a numeric index.\n    Sample headers are not available.\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_10TabProxies_TupleProxy, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_10TabProxies_TupleProxy, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   #if CYTHON_COMPILING_IN_PYPY
@@ -10243,8 +10897,8 @@ static struct PyModuleDef __pyx_moduledef = {
 #endif
 
 static __Pyx_StringTabEntry __pyx_string_tab[] = {
+  {&__pyx_kp_s_, __pyx_k_, sizeof(__pyx_k_), 0, 0, 1, 0},
   {&__pyx_kp_u_Argument_must_be_string_bytes_or, __pyx_k_Argument_must_be_string_bytes_or, sizeof(__pyx_k_Argument_must_be_string_bytes_or), 0, 1, 0, 0},
-  {&__pyx_kp_u_Argument_must_be_string_or_unico, __pyx_k_Argument_must_be_string_or_unico, sizeof(__pyx_k_Argument_must_be_string_or_unico), 0, 1, 0, 0},
   {&__pyx_n_s_AttributeError, __pyx_k_AttributeError, sizeof(__pyx_k_AttributeError), 0, 0, 1, 1},
   {&__pyx_kp_s_GTFProxy_has_no_attribute_s, __pyx_k_GTFProxy_has_no_attribute_s, sizeof(__pyx_k_GTFProxy_has_no_attribute_s), 0, 0, 1, 0},
   {&__pyx_n_s_IndexError, __pyx_k_IndexError, sizeof(__pyx_k_IndexError), 0, 0, 1, 1},
@@ -10254,13 +10908,12 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_TypeError, __pyx_k_TypeError, sizeof(__pyx_k_TypeError), 0, 0, 1, 1},
   {&__pyx_n_s_ValueError, __pyx_k_ValueError, sizeof(__pyx_k_ValueError), 0, 0, 1, 1},
   {&__pyx_kp_s__10, __pyx_k__10, sizeof(__pyx_k__10), 0, 0, 1, 0},
-  {&__pyx_kp_s__13, __pyx_k__13, sizeof(__pyx_k__13), 0, 0, 1, 0},
+  {&__pyx_kp_s__20, __pyx_k__20, sizeof(__pyx_k__20), 0, 0, 1, 0},
   {&__pyx_kp_s__23, __pyx_k__23, sizeof(__pyx_k__23), 0, 0, 1, 0},
+  {&__pyx_kp_s__24, __pyx_k__24, sizeof(__pyx_k__24), 0, 0, 1, 0},
   {&__pyx_kp_s__26, __pyx_k__26, sizeof(__pyx_k__26), 0, 0, 1, 0},
-  {&__pyx_kp_s__27, __pyx_k__27, sizeof(__pyx_k__27), 0, 0, 1, 0},
-  {&__pyx_kp_s__29, __pyx_k__29, sizeof(__pyx_k__29), 0, 0, 1, 0},
-  {&__pyx_kp_s__3, __pyx_k__3, sizeof(__pyx_k__3), 0, 0, 1, 0},
-  {&__pyx_kp_s__31, __pyx_k__31, sizeof(__pyx_k__31), 0, 0, 1, 0},
+  {&__pyx_kp_s__28, __pyx_k__28, sizeof(__pyx_k__28), 0, 0, 1, 0},
+  {&__pyx_kp_s__8, __pyx_k__8, sizeof(__pyx_k__8), 0, 0, 1, 0},
   {&__pyx_n_s_alt, __pyx_k_alt, sizeof(__pyx_k_alt), 0, 0, 1, 1},
   {&__pyx_n_s_append, __pyx_k_append, sizeof(__pyx_k_append), 0, 0, 1, 1},
   {&__pyx_n_s_asDict, __pyx_k_asDict, sizeof(__pyx_k_asDict), 0, 0, 1, 1},
@@ -10273,6 +10926,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_contig, __pyx_k_contig, sizeof(__pyx_k_contig), 0, 0, 1, 1},
   {&__pyx_n_s_decode, __pyx_k_decode, sizeof(__pyx_k_decode), 0, 0, 1, 1},
   {&__pyx_n_s_encode, __pyx_k_encode, sizeof(__pyx_k_encode), 0, 0, 1, 1},
+  {&__pyx_n_s_encoding, __pyx_k_encoding, sizeof(__pyx_k_encoding), 0, 0, 1, 1},
   {&__pyx_n_s_end, __pyx_k_end, sizeof(__pyx_k_end), 0, 0, 1, 1},
   {&__pyx_n_s_feature, __pyx_k_feature, sizeof(__pyx_k_feature), 0, 0, 1, 1},
   {&__pyx_kp_s_field_s_not_set, __pyx_k_field_s_not_set, sizeof(__pyx_k_field_s_not_set), 0, 0, 1, 0},
@@ -10303,6 +10957,10 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_maxsplit, __pyx_k_maxsplit, sizeof(__pyx_k_maxsplit), 0, 0, 1, 1},
   {&__pyx_n_s_name, __pyx_k_name, sizeof(__pyx_k_name), 0, 0, 1, 1},
   {&__pyx_kp_s_out_of_memory, __pyx_k_out_of_memory, sizeof(__pyx_k_out_of_memory), 0, 0, 1, 0},
+  {&__pyx_kp_s_out_of_memory_in_TupleProxy_copy, __pyx_k_out_of_memory_in_TupleProxy_copy, sizeof(__pyx_k_out_of_memory_in_TupleProxy_copy), 0, 0, 1, 0},
+  {&__pyx_kp_s_out_of_memory_in_TupleProxy_upda, __pyx_k_out_of_memory_in_TupleProxy_upda, sizeof(__pyx_k_out_of_memory_in_TupleProxy_upda), 0, 0, 1, 0},
+  {&__pyx_kp_s_parsing_error_fewer_that_i_field, __pyx_k_parsing_error_fewer_that_i_field, sizeof(__pyx_k_parsing_error_fewer_that_i_field), 0, 0, 1, 0},
+  {&__pyx_kp_s_parsing_error_more_than_i_fields, __pyx_k_parsing_error_more_than_i_fields, sizeof(__pyx_k_parsing_error_more_than_i_fields), 0, 0, 1, 0},
   {&__pyx_n_s_pos, __pyx_k_pos, sizeof(__pyx_k_pos), 0, 0, 1, 1},
   {&__pyx_n_s_pysam_TabProxies, __pyx_k_pysam_TabProxies, sizeof(__pyx_k_pysam_TabProxies), 0, 0, 1, 1},
   {&__pyx_n_s_pyx_vtable, __pyx_k_pyx_vtable, sizeof(__pyx_k_pyx_vtable), 0, 0, 1, 1},
@@ -10310,7 +10968,6 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_quote, __pyx_k_quote, sizeof(__pyx_k_quote), 0, 0, 1, 1},
   {&__pyx_n_s_range, __pyx_k_range, sizeof(__pyx_k_range), 0, 0, 1, 1},
   {&__pyx_n_s_ref, __pyx_k_ref, sizeof(__pyx_k_ref), 0, 0, 1, 1},
-  {&__pyx_kp_s_row_too_large_more_than_i_fields, __pyx_k_row_too_large_more_than_i_fields, sizeof(__pyx_k_row_too_large_more_than_i_fields), 0, 0, 1, 0},
   {&__pyx_kp_s_s, __pyx_k_s, sizeof(__pyx_k_s), 0, 0, 1, 0},
   {&__pyx_kp_s_s_s, __pyx_k_s_s, sizeof(__pyx_k_s_s), 0, 0, 1, 0},
   {&__pyx_kp_s_s_s_2, __pyx_k_s_s_2, sizeof(__pyx_k_s_s_2), 0, 0, 1, 0},
@@ -10336,18 +10993,18 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {0, 0, 0, 0, 0, 0, 0}
 };
 static int __Pyx_InitCachedBuiltins(void) {
-  __pyx_builtin_TypeError = __Pyx_GetBuiltinName(__pyx_n_s_TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_IndexError = __Pyx_GetBuiltinName(__pyx_n_s_IndexError); if (!__pyx_builtin_IndexError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_TypeError = __Pyx_GetBuiltinName(__pyx_n_s_TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_IndexError = __Pyx_GetBuiltinName(__pyx_n_s_IndexError); if (!__pyx_builtin_IndexError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   #if PY_MAJOR_VERSION >= 3
-  __pyx_builtin_xrange = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_xrange) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_xrange = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_xrange) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 337; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   #else
-  __pyx_builtin_xrange = __Pyx_GetBuiltinName(__pyx_n_s_xrange); if (!__pyx_builtin_xrange) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_xrange = __Pyx_GetBuiltinName(__pyx_n_s_xrange); if (!__pyx_builtin_xrange) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 337; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   #endif
-  __pyx_builtin_AttributeError = __Pyx_GetBuiltinName(__pyx_n_s_AttributeError); if (!__pyx_builtin_AttributeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_KeyError = __Pyx_GetBuiltinName(__pyx_n_s_KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_AttributeError = __Pyx_GetBuiltinName(__pyx_n_s_AttributeError); if (!__pyx_builtin_AttributeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_KeyError = __Pyx_GetBuiltinName(__pyx_n_s_KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   return 0;
   __pyx_L1_error:;
   return -1;
@@ -10357,299 +11014,274 @@ static int __Pyx_InitCachedConstants(void) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0);
 
-  /* "pysam/TabProxies.pyx":45
- *         return s
- *     elif PyUnicode_Check(s):
- *         return s.encode('ascii')             # <<<<<<<<<<<<<<
- *     else:
- *         raise TypeError, u"Argument must be string, bytes or unicode."
- */
-  __pyx_tuple_ = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple_);
-  __Pyx_GIVEREF(__pyx_tuple_);
-
-  /* "pysam/TabProxies.pyx":65
- *         return s
- *     elif PyBytes_Check(s):
- *         return s.decode('ascii')             # <<<<<<<<<<<<<<
- *     else:
- *         # assume unicode
- */
-  __pyx_tuple__2 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__2);
-  __Pyx_GIVEREF(__pyx_tuple__2);
-
-  /* "pysam/TabProxies.pyx":146
- *         self.data = <char*>malloc( s )
+  /* "pysam/TabProxies.pyx":144
+ *         self.data = <char*>malloc(s)
  *         if self.data == NULL:
- *             raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("out of memory in TupleProxy.copy()")             # <<<<<<<<<<<<<<
  *         self.nbytes = nbytes
- *         memcpy( <char*>self.data, buffer, s )
+ *         memcpy(<char*>self.data, buffer, s)
  */
-  __pyx_tuple__4 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory); if (unlikely(!__pyx_tuple__4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 146; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__4);
-  __Pyx_GIVEREF(__pyx_tuple__4);
+  __pyx_tuple__2 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory_in_TupleProxy_copy); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__2);
+  __Pyx_GIVEREF(__pyx_tuple__2);
 
-  /* "pysam/TabProxies.pyx":205
- *         self.fields = <char **>calloc( max_fields, sizeof(char *) )
+  /* "pysam/TabProxies.pyx":220
+ *         self.fields = <char **>calloc(max_fields, sizeof(char *))
  *         if self.fields == NULL:
- *             raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("out of memory in TupleProxy.update()")             # <<<<<<<<<<<<<<
  * 
  *         #################################
  */
-  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__5);
-  __Pyx_GIVEREF(__pyx_tuple__5);
+  __pyx_tuple__3 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory_in_TupleProxy_upda); if (unlikely(!__pyx_tuple__3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__3);
+  __Pyx_GIVEREF(__pyx_tuple__3);
 
-  /* "pysam/TabProxies.pyx":234
- *         cdef int i = index
- *         if i < 0: i += self.nfields
- *         if i < 0: raise IndexError( "list index out of range" )             # <<<<<<<<<<<<<<
- *         i += self.offset
- *         if i >= self.nfields:
+  /* "pysam/TabProxies.pyx":258
+ *             i += self.nfields
+ *         if i < 0:
+ *             raise IndexError("list index out of range")             # <<<<<<<<<<<<<<
+ *         # apply offset - separating a fixed number
+ *         # of fields from a variable number such as in VCF
  */
-  __pyx_tuple__6 = PyTuple_Pack(1, __pyx_kp_s_list_index_out_of_range); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__6);
-  __Pyx_GIVEREF(__pyx_tuple__6);
+  __pyx_tuple__4 = PyTuple_Pack(1, __pyx_kp_s_list_index_out_of_range); if (unlikely(!__pyx_tuple__4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__4);
+  __Pyx_GIVEREF(__pyx_tuple__4);
 
-  /* "pysam/TabProxies.pyx":252
- *         '''set item at idx index.'''
+  /* "pysam/TabProxies.pyx":282
  *         cdef int idx = index
- *         if idx < 0: raise IndexError( "list index out of range" )             # <<<<<<<<<<<<<<
+ *         if idx < 0:
+ *             raise IndexError("list index out of range")             # <<<<<<<<<<<<<<
  *         if idx >= self.nfields:
- *             raise IndexError( "list index out of range" )
+ *             raise IndexError("list index out of range")
  */
-  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_s_list_index_out_of_range); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__7);
-  __Pyx_GIVEREF(__pyx_tuple__7);
+  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_s_list_index_out_of_range); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__5);
+  __Pyx_GIVEREF(__pyx_tuple__5);
 
-  /* "pysam/TabProxies.pyx":254
- *         if idx < 0: raise IndexError( "list index out of range" )
+  /* "pysam/TabProxies.pyx":284
+ *             raise IndexError("list index out of range")
  *         if idx >= self.nfields:
- *             raise IndexError( "list index out of range" )             # <<<<<<<<<<<<<<
+ *             raise IndexError("list index out of range")             # <<<<<<<<<<<<<<
  * 
- *         if isNew( self.fields[idx], self.data, self.nbytes ):
+ *         if isNew(self.fields[idx], self.data, self.nbytes):
  */
-  __pyx_tuple__8 = PyTuple_Pack(1, __pyx_kp_s_list_index_out_of_range); if (unlikely(!__pyx_tuple__8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__8);
-  __Pyx_GIVEREF(__pyx_tuple__8);
+  __pyx_tuple__6 = PyTuple_Pack(1, __pyx_kp_s_list_index_out_of_range); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 284; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__6);
+  __Pyx_GIVEREF(__pyx_tuple__6);
 
-  /* "pysam/TabProxies.pyx":270
- *         self.fields[idx] = <char*>malloc( (strlen( tmp ) + 1) * sizeof(char) )
+  /* "pysam/TabProxies.pyx":300
+ *         self.fields[idx] = <char*>malloc((strlen( tmp ) + 1) * sizeof(char))
  *         if self.fields[idx] == NULL:
  *             raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
- *         strcpy( self.fields[idx], tmp )
+ *         strcpy(self.fields[idx], tmp)
  * 
  */
-  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__9);
-  __Pyx_GIVEREF(__pyx_tuple__9);
+  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__7);
+  __Pyx_GIVEREF(__pyx_tuple__7);
 
-  /* "pysam/TabProxies.pyx":310
- *             cpy = <char*>calloc( sizeof(char), self.nbytes+1 )
+  /* "pysam/TabProxies.pyx":343
+ *             cpy = <char*>calloc(sizeof(char), self.nbytes+1)
  *             if cpy == NULL:
- *                 raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
- *             memcpy( cpy, self.data, self.nbytes+1)
+ *                 raise ValueError("out of memory")             # <<<<<<<<<<<<<<
+ *             memcpy(cpy, self.data, self.nbytes+1)
  *             for x from 0 <= x < self.nbytes:
  */
-  __pyx_tuple__11 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 310; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__11);
-  __Pyx_GIVEREF(__pyx_tuple__11);
-
-  /* "pysam/TabProxies.pyx":316
- *             result = cpy[:self.nbytes]
- *             free(cpy)
- *             return result.decode('ascii')             # <<<<<<<<<<<<<<
- * 
- * def toDot( v ):
- */
-  __pyx_tuple__12 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__12);
-  __Pyx_GIVEREF(__pyx_tuple__12);
+  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__9);
+  __Pyx_GIVEREF(__pyx_tuple__9);
 
-  /* "pysam/TabProxies.pyx":359
- *     property contig:
+  /* "pysam/TabProxies.pyx":402
  *        '''contig of feature.'''
- *        def __get__( self ): return self._getindex( 0 )             # <<<<<<<<<<<<<<
- *        def __set__( self, value ): self._setindex( 0, value )
- * 
+ *        def __get__(self):
+ *            return self._getindex(0)             # <<<<<<<<<<<<<<
+ *        def __set__(self, value):
+ *            self._setindex(0, value)
  */
-  __pyx_tuple__14 = PyTuple_Pack(1, __pyx_int_0); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 359; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__14);
-  __Pyx_GIVEREF(__pyx_tuple__14);
+  __pyx_tuple__11 = PyTuple_Pack(1, __pyx_int_0); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 402; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__11);
+  __Pyx_GIVEREF(__pyx_tuple__11);
 
-  /* "pysam/TabProxies.pyx":364
- *     property source:
+  /* "pysam/TabProxies.pyx":409
  *        '''feature source.'''
- *        def __get__( self ): return self._getindex( 1 )             # <<<<<<<<<<<<<<
- *        def __set__( self, value ): self._setindex( 1, value )
- * 
+ *        def __get__(self):
+ *            return self._getindex(1)             # <<<<<<<<<<<<<<
+ *        def __set__(self, value):
+ *            self._setindex(1, value)
  */
-  __pyx_tuple__15 = PyTuple_Pack(1, __pyx_int_1); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__15);
-  __Pyx_GIVEREF(__pyx_tuple__15);
+  __pyx_tuple__12 = PyTuple_Pack(1, __pyx_int_1); if (unlikely(!__pyx_tuple__12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__12);
+  __Pyx_GIVEREF(__pyx_tuple__12);
 
-  /* "pysam/TabProxies.pyx":369
+  /* "pysam/TabProxies.pyx":415
  *     property feature:
  *        '''feature name.'''
  *        def __get__( self ): return self._getindex( 2 )             # <<<<<<<<<<<<<<
  *        def __set__( self, value ): self._setindex( 2, value )
  * 
  */
-  __pyx_tuple__16 = PyTuple_Pack(1, __pyx_int_2); if (unlikely(!__pyx_tuple__16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__16);
-  __Pyx_GIVEREF(__pyx_tuple__16);
+  __pyx_tuple__13 = PyTuple_Pack(1, __pyx_int_2); if (unlikely(!__pyx_tuple__13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__13);
+  __Pyx_GIVEREF(__pyx_tuple__13);
 
-  /* "pysam/TabProxies.pyx":374
+  /* "pysam/TabProxies.pyx":420
  *     property start:
  *        '''feature start (in 0-based open/closed coordinates).'''
  *        def __get__( self ): return int( self._getindex( 3 )) - 1             # <<<<<<<<<<<<<<
  *        def __set__( self, value ): self._setindex( 3, str(value+1) )
  * 
  */
-  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_int_3); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__17);
-  __Pyx_GIVEREF(__pyx_tuple__17);
+  __pyx_tuple__14 = PyTuple_Pack(1, __pyx_int_3); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__14);
+  __Pyx_GIVEREF(__pyx_tuple__14);
 
-  /* "pysam/TabProxies.pyx":379
+  /* "pysam/TabProxies.pyx":425
  *     property end:
  *        '''feature end (in 0-based open/closed coordinates).'''
  *        def __get__( self ): return int( self._getindex( 4 ) )             # <<<<<<<<<<<<<<
  *        def __set__( self, value ): self._setindex( 4, str(value) )
  * 
  */
-  __pyx_tuple__18 = PyTuple_Pack(1, __pyx_int_4); if (unlikely(!__pyx_tuple__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__18);
-  __Pyx_GIVEREF(__pyx_tuple__18);
+  __pyx_tuple__15 = PyTuple_Pack(1, __pyx_int_4); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__15);
+  __Pyx_GIVEREF(__pyx_tuple__15);
 
-  /* "pysam/TabProxies.pyx":385
+  /* "pysam/TabProxies.pyx":431
  *        '''feature score.'''
  *        def __get__( self ):
  *            v = self._getindex(5)             # <<<<<<<<<<<<<<
  *            if v == "" or v[0] == '.':
  *                return None
  */
-  __pyx_tuple__19 = PyTuple_Pack(1, __pyx_int_5); if (unlikely(!__pyx_tuple__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__19);
-  __Pyx_GIVEREF(__pyx_tuple__19);
+  __pyx_tuple__16 = PyTuple_Pack(1, __pyx_int_5); if (unlikely(!__pyx_tuple__16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__16);
+  __Pyx_GIVEREF(__pyx_tuple__16);
 
-  /* "pysam/TabProxies.pyx":395
+  /* "pysam/TabProxies.pyx":441
  *     property strand:
  *        '''feature strand.'''
  *        def __get__( self ): return self._getindex( 6 )             # <<<<<<<<<<<<<<
  *        def __set__( self, value ): self._setindex( 6, value )
  * 
  */
-  __pyx_tuple__20 = PyTuple_Pack(1, __pyx_int_6); if (unlikely(!__pyx_tuple__20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__20);
-  __Pyx_GIVEREF(__pyx_tuple__20);
+  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_int_6); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__17);
+  __Pyx_GIVEREF(__pyx_tuple__17);
 
-  /* "pysam/TabProxies.pyx":400
+  /* "pysam/TabProxies.pyx":446
  *     property frame:
  *        '''feature frame.'''
  *        def __get__( self ): return self._getindex( 7 )             # <<<<<<<<<<<<<<
  *        def __set__( self, value ): self._setindex( 7, value )
  * 
  */
-  __pyx_tuple__21 = PyTuple_Pack(1, __pyx_int_7); if (unlikely(!__pyx_tuple__21)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__21);
-  __Pyx_GIVEREF(__pyx_tuple__21);
+  __pyx_tuple__18 = PyTuple_Pack(1, __pyx_int_7); if (unlikely(!__pyx_tuple__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__18);
+  __Pyx_GIVEREF(__pyx_tuple__18);
 
-  /* "pysam/TabProxies.pyx":409
+  /* "pysam/TabProxies.pyx":455
  *                return self._attributes
  *            else:
- *                return self._getindex( 8 )             # <<<<<<<<<<<<<<
+ *                return self._getindex(8)             # <<<<<<<<<<<<<<
  *        def __set__( self, value ):
  *            if self.hasOwnAttributes:
  */
-  __pyx_tuple__22 = PyTuple_Pack(1, __pyx_int_8); if (unlikely(!__pyx_tuple__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__22);
-  __Pyx_GIVEREF(__pyx_tuple__22);
+  __pyx_tuple__19 = PyTuple_Pack(1, __pyx_int_8); if (unlikely(!__pyx_tuple__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__19);
+  __Pyx_GIVEREF(__pyx_tuple__19);
 
-  /* "pysam/TabProxies.pyx":432
+  /* "pysam/TabProxies.pyx":478
  * 
  *         # separate into fields
  *         fields = [x.strip() for x in attributes.split(";")[:-1]]             # <<<<<<<<<<<<<<
  * 
  *         result = {}
  */
-  __pyx_tuple__24 = PyTuple_Pack(1, __pyx_kp_s__23); if (unlikely(!__pyx_tuple__24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__24);
-  __Pyx_GIVEREF(__pyx_tuple__24);
-  __pyx_slice__25 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__25);
-  __Pyx_GIVEREF(__pyx_slice__25);
+  __pyx_tuple__21 = PyTuple_Pack(1, __pyx_kp_s__20); if (unlikely(!__pyx_tuple__21)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__21);
+  __Pyx_GIVEREF(__pyx_tuple__21);
+  __pyx_slice__22 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_slice__22);
+  __Pyx_GIVEREF(__pyx_slice__22);
 
-  /* "pysam/TabProxies.pyx":446
+  /* "pysam/TabProxies.pyx":492
  * 
  *             if v[0] == '"' and v[-1] == '"':
  *                 v = v[1:-1]             # <<<<<<<<<<<<<<
  *             else:
  *                 ## try to convert to a value
  */
-  __pyx_slice__28 = PySlice_New(__pyx_int_1, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__28)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__28);
-  __Pyx_GIVEREF(__pyx_slice__28);
+  __pyx_slice__25 = PySlice_New(__pyx_int_1, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_slice__25);
+  __Pyx_GIVEREF(__pyx_slice__25);
 
-  /* "pysam/TabProxies.pyx":482
+  /* "pysam/TabProxies.pyx":528
  *         self._attributes = <char *>calloc( l + 1, sizeof(char) )
  *         if self._attributes == NULL:
  *             raise ValueError("out of memory" )             # <<<<<<<<<<<<<<
  *         memcpy( self._attributes, p, l )
  * 
  */
-  __pyx_tuple__30 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory); if (unlikely(!__pyx_tuple__30)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__30);
-  __Pyx_GIVEREF(__pyx_tuple__30);
+  __pyx_tuple__27 = PyTuple_Pack(1, __pyx_kp_s_out_of_memory); if (unlikely(!__pyx_tuple__27)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__27);
+  __Pyx_GIVEREF(__pyx_tuple__27);
 
-  /* "pysam/TabProxies.pyx":520
- *         '''return a list of attributes defined in this entry.'''
+  /* "pysam/TabProxies.pyx":567
  *         r = self.attributes
- *         return [ x.strip().split(" ")[0] for x in r.split(";") if x.strip() != '' ]             # <<<<<<<<<<<<<<
+ *         return [x.strip().split(" ")[0]
+ *                 for x in r.split(";") if x.strip() != '']             # <<<<<<<<<<<<<<
  * 
  *     def __getitem__(self, key):
  */
-  __pyx_tuple__32 = PyTuple_Pack(1, __pyx_kp_s__23); if (unlikely(!__pyx_tuple__32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__32);
-  __Pyx_GIVEREF(__pyx_tuple__32);
-  __pyx_tuple__33 = PyTuple_Pack(1, __pyx_kp_s__26); if (unlikely(!__pyx_tuple__33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__33);
-  __Pyx_GIVEREF(__pyx_tuple__33);
+  __pyx_tuple__29 = PyTuple_Pack(1, __pyx_kp_s__20); if (unlikely(!__pyx_tuple__29)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__29);
+  __Pyx_GIVEREF(__pyx_tuple__29);
+
+  /* "pysam/TabProxies.pyx":566
+ *         '''return a list of attributes defined in this entry.'''
+ *         r = self.attributes
+ *         return [x.strip().split(" ")[0]             # <<<<<<<<<<<<<<
+ *                 for x in r.split(";") if x.strip() != '']
+ * 
+ */
+  __pyx_tuple__30 = PyTuple_Pack(1, __pyx_kp_s__23); if (unlikely(!__pyx_tuple__30)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__30);
+  __Pyx_GIVEREF(__pyx_tuple__30);
 
-  /* "pysam/TabProxies.pyx":631
+  /* "pysam/TabProxies.pyx":688
  * 
  *         if self.nfields < 3:
- *             raise ValueError( "bed format requires at least three columns" )             # <<<<<<<<<<<<<<
+ *             raise ValueError(             # <<<<<<<<<<<<<<
+ *                 "bed format requires at least three columns")
  * 
- *         # determines bed format
  */
-  __pyx_tuple__34 = PyTuple_Pack(1, __pyx_kp_s_bed_format_requires_at_least_thr); if (unlikely(!__pyx_tuple__34)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__34);
-  __Pyx_GIVEREF(__pyx_tuple__34);
+  __pyx_tuple__31 = PyTuple_Pack(1, __pyx_kp_s_bed_format_requires_at_least_thr); if (unlikely(!__pyx_tuple__31)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__31);
+  __Pyx_GIVEREF(__pyx_tuple__31);
 
-  /* "pysam/TabProxies.pyx":318
- *             return result.decode('ascii')
+  /* "pysam/TabProxies.pyx":353
+ *             return r
  * 
- * def toDot( v ):             # <<<<<<<<<<<<<<
+ * def toDot(v):             # <<<<<<<<<<<<<<
  *     '''convert value to '.' if None'''
- *     if v == None: return "."
+ *     if v is None:
  */
-  __pyx_tuple__35 = PyTuple_Pack(1, __pyx_n_s_v); if (unlikely(!__pyx_tuple__35)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__35);
-  __Pyx_GIVEREF(__pyx_tuple__35);
-  __pyx_codeobj__36 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__35, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_toDot, 318, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__36)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__32 = PyTuple_Pack(1, __pyx_n_s_v); if (unlikely(!__pyx_tuple__32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__32);
+  __Pyx_GIVEREF(__pyx_tuple__32);
+  __pyx_codeobj__33 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__32, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_toDot, 353, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/TabProxies.pyx":323
- *     else: return str(v)
+  /* "pysam/TabProxies.pyx":360
+ *         return str(v)
  * 
- * def quote( v ):             # <<<<<<<<<<<<<<
+ * def quote(v):             # <<<<<<<<<<<<<<
  *     '''return a quoted attribute.'''
  *     if type(v) in types.StringTypes:
  */
-  __pyx_tuple__37 = PyTuple_Pack(1, __pyx_n_s_v); if (unlikely(!__pyx_tuple__37)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__37);
-  __Pyx_GIVEREF(__pyx_tuple__37);
-  __pyx_codeobj__38 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__37, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_quote, 323, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__38)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__34 = PyTuple_Pack(1, __pyx_n_s_v); if (unlikely(!__pyx_tuple__34)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__34);
+  __Pyx_GIVEREF(__pyx_tuple__34);
+  __pyx_codeobj__35 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__34, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_quote, 360, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__35)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_RefNannyFinishContext();
   return 0;
   __pyx_L1_error:;
@@ -10762,16 +11394,17 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   /*--- Function export code ---*/
   /*--- Type init code ---*/
   __pyx_vtabptr_5pysam_10TabProxies_TupleProxy = &__pyx_vtable_5pysam_10TabProxies_TupleProxy;
-  __pyx_vtable_5pysam_10TabProxies_TupleProxy.getMaxFields = (int (*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, size_t))__pyx_f_5pysam_10TabProxies_10TupleProxy_getMaxFields;
+  __pyx_vtable_5pysam_10TabProxies_TupleProxy.getMaxFields = (int (*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *))__pyx_f_5pysam_10TabProxies_10TupleProxy_getMaxFields;
+  __pyx_vtable_5pysam_10TabProxies_TupleProxy.getMinFields = (int (*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *))__pyx_f_5pysam_10TabProxies_10TupleProxy_getMinFields;
   __pyx_vtable_5pysam_10TabProxies_TupleProxy.take = (PyObject *(*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t))__pyx_f_5pysam_10TabProxies_10TupleProxy_take;
   __pyx_vtable_5pysam_10TabProxies_TupleProxy.present = (PyObject *(*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t))__pyx_f_5pysam_10TabProxies_10TupleProxy_present;
   __pyx_vtable_5pysam_10TabProxies_TupleProxy.copy = (PyObject *(*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t))__pyx_f_5pysam_10TabProxies_10TupleProxy_copy;
   __pyx_vtable_5pysam_10TabProxies_TupleProxy.update = (PyObject *(*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t))__pyx_f_5pysam_10TabProxies_10TupleProxy_update;
-  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_TupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_TupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_10TabProxies_TupleProxy.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_TupleProxy, "__setitem__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_TupleProxy, "__setitem__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_10TabProxies_10TupleProxy_10__setitem__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_10TabProxies_10TupleProxy_10__setitem__.doc = __pyx_doc_5pysam_10TabProxies_10TupleProxy_10__setitem__;
@@ -10781,7 +11414,7 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   #endif
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_TupleProxy, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_TupleProxy, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_10TabProxies_10TupleProxy_16__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_10TabProxies_10TupleProxy_16__next__.doc = __pyx_doc_5pysam_10TabProxies_10TupleProxy_16__next__;
@@ -10791,7 +11424,7 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   #endif
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_TupleProxy, "__str__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_TupleProxy, "__str__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_10TabProxies_10TupleProxy_18__str__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_10TabProxies_10TupleProxy_18__str__.doc = __pyx_doc_5pysam_10TabProxies_10TupleProxy_18__str__;
@@ -10799,19 +11432,20 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_TupleProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_TupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "TupleProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_TupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_TupleProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_TupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "TupleProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_TupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_10TabProxies_TupleProxy = &__pyx_type_5pysam_10TabProxies_TupleProxy;
   __pyx_vtabptr_5pysam_10TabProxies_GTFProxy = &__pyx_vtable_5pysam_10TabProxies_GTFProxy;
   __pyx_vtable_5pysam_10TabProxies_GTFProxy.__pyx_base = *__pyx_vtabptr_5pysam_10TabProxies_TupleProxy;
-  __pyx_vtable_5pysam_10TabProxies_GTFProxy.__pyx_base.getMaxFields = (int (*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, size_t))__pyx_f_5pysam_10TabProxies_8GTFProxy_getMaxFields;
+  __pyx_vtable_5pysam_10TabProxies_GTFProxy.__pyx_base.getMaxFields = (int (*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *))__pyx_f_5pysam_10TabProxies_8GTFProxy_getMaxFields;
+  __pyx_vtable_5pysam_10TabProxies_GTFProxy.__pyx_base.getMinFields = (int (*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *))__pyx_f_5pysam_10TabProxies_8GTFProxy_getMinFields;
   __pyx_vtable_5pysam_10TabProxies_GTFProxy.getAttributes = (char *(*)(struct __pyx_obj_5pysam_10TabProxies_GTFProxy *))__pyx_f_5pysam_10TabProxies_8GTFProxy_getAttributes;
   __pyx_type_5pysam_10TabProxies_GTFProxy.tp_base = __pyx_ptype_5pysam_10TabProxies_TupleProxy;
-  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_GTFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_GTFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_10TabProxies_GTFProxy.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_GTFProxy, "__getattr__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_GTFProxy, "__getattr__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_10TabProxies_8GTFProxy_16__getattr__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_10TabProxies_8GTFProxy_16__getattr__.doc = __pyx_doc_5pysam_10TabProxies_8GTFProxy_16__getattr__;
@@ -10819,17 +11453,17 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_GTFProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_GTFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "GTFProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_GTFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_GTFProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_GTFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "GTFProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_GTFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_10TabProxies_GTFProxy = &__pyx_type_5pysam_10TabProxies_GTFProxy;
   __pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy = &__pyx_vtable_5pysam_10TabProxies_NamedTupleProxy;
   __pyx_vtable_5pysam_10TabProxies_NamedTupleProxy.__pyx_base = *__pyx_vtabptr_5pysam_10TabProxies_TupleProxy;
   __pyx_type_5pysam_10TabProxies_NamedTupleProxy.tp_base = __pyx_ptype_5pysam_10TabProxies_TupleProxy;
-  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_NamedTupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_NamedTupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_10TabProxies_NamedTupleProxy.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_NamedTupleProxy, "__setattr__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_NamedTupleProxy, "__setattr__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_10TabProxies_15NamedTupleProxy___setattr__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_10TabProxies_15NamedTupleProxy___setattr__.doc = __pyx_doc_5pysam_10TabProxies_15NamedTupleProxy___setattr__;
@@ -10837,19 +11471,20 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_NamedTupleProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "NamedTupleProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_NamedTupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_NamedTupleProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "NamedTupleProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_NamedTupleProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_10TabProxies_NamedTupleProxy = &__pyx_type_5pysam_10TabProxies_NamedTupleProxy;
   __pyx_vtabptr_5pysam_10TabProxies_BedProxy = &__pyx_vtable_5pysam_10TabProxies_BedProxy;
   __pyx_vtable_5pysam_10TabProxies_BedProxy.__pyx_base = *__pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy;
-  __pyx_vtable_5pysam_10TabProxies_BedProxy.__pyx_base.__pyx_base.getMaxFields = (int (*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, size_t))__pyx_f_5pysam_10TabProxies_8BedProxy_getMaxFields;
+  __pyx_vtable_5pysam_10TabProxies_BedProxy.__pyx_base.__pyx_base.getMaxFields = (int (*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *))__pyx_f_5pysam_10TabProxies_8BedProxy_getMaxFields;
+  __pyx_vtable_5pysam_10TabProxies_BedProxy.__pyx_base.__pyx_base.getMinFields = (int (*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *))__pyx_f_5pysam_10TabProxies_8BedProxy_getMinFields;
   __pyx_vtable_5pysam_10TabProxies_BedProxy.__pyx_base.__pyx_base.update = (PyObject *(*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t))__pyx_f_5pysam_10TabProxies_8BedProxy_update;
   __pyx_type_5pysam_10TabProxies_BedProxy.tp_base = __pyx_ptype_5pysam_10TabProxies_NamedTupleProxy;
-  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_BedProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_BedProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_10TabProxies_BedProxy.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_BedProxy, "__setattr__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_BedProxy, "__setattr__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_10TabProxies_8BedProxy_2__setattr__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_10TabProxies_8BedProxy_2__setattr__.doc = __pyx_doc_5pysam_10TabProxies_8BedProxy_2__setattr__;
@@ -10857,18 +11492,18 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_BedProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_BedProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "BedProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_BedProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_BedProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_BedProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "BedProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_BedProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_10TabProxies_BedProxy = &__pyx_type_5pysam_10TabProxies_BedProxy;
   __pyx_vtabptr_5pysam_10TabProxies_VCFProxy = &__pyx_vtable_5pysam_10TabProxies_VCFProxy;
   __pyx_vtable_5pysam_10TabProxies_VCFProxy.__pyx_base = *__pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy;
   __pyx_vtable_5pysam_10TabProxies_VCFProxy.__pyx_base.__pyx_base.update = (PyObject *(*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t))__pyx_f_5pysam_10TabProxies_8VCFProxy_update;
   __pyx_type_5pysam_10TabProxies_VCFProxy.tp_base = __pyx_ptype_5pysam_10TabProxies_NamedTupleProxy;
-  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_VCFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_10TabProxies_VCFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_10TabProxies_VCFProxy.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_VCFProxy, "__len__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_VCFProxy, "__len__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_10TabProxies_8VCFProxy_2__len__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_10TabProxies_8VCFProxy_2__len__.doc = __pyx_doc_5pysam_10TabProxies_8VCFProxy_2__len__;
@@ -10878,7 +11513,7 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   #endif
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_VCFProxy, "__setattr__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_10TabProxies_VCFProxy, "__setattr__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_10TabProxies_8VCFProxy_4__setattr__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_10TabProxies_8VCFProxy_4__setattr__.doc = __pyx_doc_5pysam_10TabProxies_8VCFProxy_4__setattr__;
@@ -10886,8 +11521,8 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_VCFProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_VCFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "VCFProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_VCFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_10TabProxies_VCFProxy.tp_dict, __pyx_vtabptr_5pysam_10TabProxies_VCFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "VCFProxy", (PyObject *)&__pyx_type_5pysam_10TabProxies_VCFProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_10TabProxies_VCFProxy = &__pyx_type_5pysam_10TabProxies_VCFProxy;
   /*--- Type import code ---*/
   __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "type", 
@@ -10936,28 +11571,28 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_string, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":17
+  /* "pysam/TabProxies.pyx":20
  * # filename encoding (copied from lxml.etree.pyx)
  * cdef str _FILENAME_ENCODING
  * _FILENAME_ENCODING = sys.getfilesystemencoding()             # <<<<<<<<<<<<<<
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getfilesystemencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getfilesystemencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_XGOTREF(__pyx_v_5pysam_10TabProxies__FILENAME_ENCODING);
   __Pyx_DECREF_SET(__pyx_v_5pysam_10TabProxies__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
   __Pyx_GIVEREF(__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":18
+  /* "pysam/TabProxies.pyx":21
  * cdef str _FILENAME_ENCODING
  * _FILENAME_ENCODING = sys.getfilesystemencoding()
  * if _FILENAME_ENCODING is None:             # <<<<<<<<<<<<<<
@@ -10968,22 +11603,22 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   __pyx_t_4 = (__pyx_t_3 != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/TabProxies.pyx":19
+    /* "pysam/TabProxies.pyx":22
  * _FILENAME_ENCODING = sys.getfilesystemencoding()
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = sys.getdefaultencoding()             # <<<<<<<<<<<<<<
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = 'ascii'
  */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getdefaultencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getdefaultencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_XGOTREF(__pyx_v_5pysam_10TabProxies__FILENAME_ENCODING);
     __Pyx_DECREF_SET(__pyx_v_5pysam_10TabProxies__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
     __Pyx_GIVEREF(__pyx_t_1);
@@ -10992,7 +11627,7 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   }
   __pyx_L2:;
 
-  /* "pysam/TabProxies.pyx":20
+  /* "pysam/TabProxies.pyx":23
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  * if _FILENAME_ENCODING is None:             # <<<<<<<<<<<<<<
@@ -11003,12 +11638,12 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   __pyx_t_3 = (__pyx_t_4 != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/TabProxies.pyx":21
+    /* "pysam/TabProxies.pyx":24
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = 'ascii'             # <<<<<<<<<<<<<<
  * 
- * cdef bytes _my_encodeFilename(object filename):
+ * cdef bytes _force_bytes(object s, encoding="ascii"):
  */
     __Pyx_INCREF(__pyx_n_s_ascii);
     __Pyx_XGOTREF(__pyx_v_5pysam_10TabProxies__FILENAME_ENCODING);
@@ -11018,79 +11653,79 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   }
   __pyx_L3:;
 
-  /* "pysam/TabProxies.pyx":318
- *             return result.decode('ascii')
+  /* "pysam/TabProxies.pyx":353
+ *             return r
  * 
- * def toDot( v ):             # <<<<<<<<<<<<<<
+ * def toDot(v):             # <<<<<<<<<<<<<<
  *     '''convert value to '.' if None'''
- *     if v == None: return "."
+ *     if v is None:
  */
-  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_10TabProxies_1toDot, NULL, __pyx_n_s_pysam_TabProxies); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_10TabProxies_1toDot, NULL, __pyx_n_s_pysam_TabProxies); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_toDot, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_toDot, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":323
- *     else: return str(v)
+  /* "pysam/TabProxies.pyx":360
+ *         return str(v)
  * 
- * def quote( v ):             # <<<<<<<<<<<<<<
+ * def quote(v):             # <<<<<<<<<<<<<<
  *     '''return a quoted attribute.'''
  *     if type(v) in types.StringTypes:
  */
-  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_10TabProxies_3quote, NULL, __pyx_n_s_pysam_TabProxies); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_10TabProxies_3quote, NULL, __pyx_n_s_pysam_TabProxies); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_quote, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_quote, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/TabProxies.pyx":582
- * cdef class NamedTupleProxy( TupleProxy ):
+  /* "pysam/TabProxies.pyx":632
+ * cdef class NamedTupleProxy(TupleProxy):
  * 
  *     map_key2field = {}             # <<<<<<<<<<<<<<
  * 
- *     def __setattr__(self, key, value ):
+ *     def __setattr__(self, key, value):
  */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem((PyObject *)__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy->tp_dict, __pyx_n_s_map_key2field, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem((PyObject *)__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy->tp_dict, __pyx_n_s_map_key2field, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   PyType_Modified(__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy);
 
-  /* "pysam/TabProxies.pyx":605
+  /* "pysam/TabProxies.pyx":658
  *     This class represents a BED entry for fast read-access.
  *     '''
  *     map_key2field = {             # <<<<<<<<<<<<<<
- *         'contig' : (0, bytes),
+ *         'contig' : (0, str),
  *         'start' : (1, int),
  */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
 
-  /* "pysam/TabProxies.pyx":606
+  /* "pysam/TabProxies.pyx":659
  *     '''
  *     map_key2field = {
- *         'contig' : (0, bytes),             # <<<<<<<<<<<<<<
+ *         'contig' : (0, str),             # <<<<<<<<<<<<<<
  *         'start' : (1, int),
  *         'end' : (2, int),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_0);
   __Pyx_GIVEREF(__pyx_int_0);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_contig, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_contig, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":607
+  /* "pysam/TabProxies.pyx":660
  *     map_key2field = {
- *         'contig' : (0, bytes),
+ *         'contig' : (0, str),
  *         'start' : (1, int),             # <<<<<<<<<<<<<<
  *         'end' : (2, int),
- *         'name' : (3, bytes),
+ *         'name' : (3, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 660; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_1);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_1);
@@ -11098,17 +11733,17 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyInt_Type))));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyInt_Type))));
   __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyInt_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_start, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_start, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":608
- *         'contig' : (0, bytes),
+  /* "pysam/TabProxies.pyx":661
+ *         'contig' : (0, str),
  *         'start' : (1, int),
  *         'end' : (2, int),             # <<<<<<<<<<<<<<
- *         'name' : (3, bytes),
+ *         'name' : (3, str),
  *         'score' : (4, float),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_2);
@@ -11116,35 +11751,35 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyInt_Type))));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyInt_Type))));
   __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyInt_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_end, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_end, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":609
+  /* "pysam/TabProxies.pyx":662
  *         'start' : (1, int),
  *         'end' : (2, int),
- *         'name' : (3, bytes),             # <<<<<<<<<<<<<<
+ *         'name' : (3, str),             # <<<<<<<<<<<<<<
  *         'score' : (4, float),
- *         'strand' : (5, bytes),
+ *         'strand' : (5, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 662; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_3);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_3);
   __Pyx_GIVEREF(__pyx_int_3);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_name, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_name, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":610
+  /* "pysam/TabProxies.pyx":663
  *         'end' : (2, int),
- *         'name' : (3, bytes),
+ *         'name' : (3, str),
  *         'score' : (4, float),             # <<<<<<<<<<<<<<
- *         'strand' : (5, bytes),
- *         'thickStart' : (6, int ),
+ *         'strand' : (5, str),
+ *         'thickStart' : (6, int),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 610; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_4);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_4);
@@ -11152,35 +11787,35 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyFloat_Type))));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyFloat_Type))));
   __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyFloat_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_score, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_score, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":611
- *         'name' : (3, bytes),
+  /* "pysam/TabProxies.pyx":664
+ *         'name' : (3, str),
  *         'score' : (4, float),
- *         'strand' : (5, bytes),             # <<<<<<<<<<<<<<
- *         'thickStart' : (6, int ),
+ *         'strand' : (5, str),             # <<<<<<<<<<<<<<
+ *         'thickStart' : (6, int),
  *         'thickEnd' : (7, int),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_5);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_5);
   __Pyx_GIVEREF(__pyx_int_5);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_strand, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_strand, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":612
+  /* "pysam/TabProxies.pyx":665
  *         'score' : (4, float),
- *         'strand' : (5, bytes),
- *         'thickStart' : (6, int ),             # <<<<<<<<<<<<<<
+ *         'strand' : (5, str),
+ *         'thickStart' : (6, int),             # <<<<<<<<<<<<<<
  *         'thickEnd' : (7, int),
- *         'itemRGB' : (8, bytes),
+ *         'itemRGB' : (8, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 612; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 665; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_6);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_6);
@@ -11188,17 +11823,17 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyInt_Type))));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyInt_Type))));
   __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyInt_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_thickStart, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_thickStart, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":613
- *         'strand' : (5, bytes),
- *         'thickStart' : (6, int ),
+  /* "pysam/TabProxies.pyx":666
+ *         'strand' : (5, str),
+ *         'thickStart' : (6, int),
  *         'thickEnd' : (7, int),             # <<<<<<<<<<<<<<
- *         'itemRGB' : (8, bytes),
+ *         'itemRGB' : (8, str),
  *         'blockCount': (9, int),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_7);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_7);
@@ -11206,35 +11841,35 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyInt_Type))));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyInt_Type))));
   __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyInt_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_thickEnd, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_thickEnd, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":614
- *         'thickStart' : (6, int ),
+  /* "pysam/TabProxies.pyx":667
+ *         'thickStart' : (6, int),
  *         'thickEnd' : (7, int),
- *         'itemRGB' : (8, bytes),             # <<<<<<<<<<<<<<
+ *         'itemRGB' : (8, str),             # <<<<<<<<<<<<<<
  *         'blockCount': (9, int),
- *         'blockSizes': (10, bytes),
+ *         'blockSizes': (10, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_8);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_8);
   __Pyx_GIVEREF(__pyx_int_8);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_itemRGB, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_itemRGB, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":615
+  /* "pysam/TabProxies.pyx":668
  *         'thickEnd' : (7, int),
- *         'itemRGB' : (8, bytes),
+ *         'itemRGB' : (8, str),
  *         'blockCount': (9, int),             # <<<<<<<<<<<<<<
- *         'blockSizes': (10, bytes),
- *         'blockStarts': (11, bytes), }
+ *         'blockSizes': (10, str),
+ *         'blockStarts': (11, str), }
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 668; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_9);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_9);
@@ -11242,84 +11877,84 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyInt_Type))));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyInt_Type))));
   __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyInt_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_blockCount, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_blockCount, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":616
- *         'itemRGB' : (8, bytes),
+  /* "pysam/TabProxies.pyx":669
+ *         'itemRGB' : (8, str),
  *         'blockCount': (9, int),
- *         'blockSizes': (10, bytes),             # <<<<<<<<<<<<<<
- *         'blockStarts': (11, bytes), }
+ *         'blockSizes': (10, str),             # <<<<<<<<<<<<<<
+ *         'blockStarts': (11, str), }
  * 
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 616; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_10);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_10);
   __Pyx_GIVEREF(__pyx_int_10);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_blockSizes, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_blockSizes, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":617
+  /* "pysam/TabProxies.pyx":670
  *         'blockCount': (9, int),
- *         'blockSizes': (10, bytes),
- *         'blockStarts': (11, bytes), }             # <<<<<<<<<<<<<<
+ *         'blockSizes': (10, str),
+ *         'blockStarts': (11, str), }             # <<<<<<<<<<<<<<
  * 
- *     cdef int getMaxFields( self, size_t nbytes ):
+ *     cdef int getMinFields(self):
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 670; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_11);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_11);
   __Pyx_GIVEREF(__pyx_int_11);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_blockStarts, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_blockStarts, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (PyDict_SetItem((PyObject *)__pyx_ptype_5pysam_10TabProxies_BedProxy->tp_dict, __pyx_n_s_map_key2field, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem((PyObject *)__pyx_ptype_5pysam_10TabProxies_BedProxy->tp_dict, __pyx_n_s_map_key2field, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   PyType_Modified(__pyx_ptype_5pysam_10TabProxies_BedProxy);
 
-  /* "pysam/TabProxies.pyx":671
- *     The genotypes are accessed via index.
+  /* "pysam/TabProxies.pyx":730
+ *     Sample headers are not available.
  *     '''
  *     map_key2field = {             # <<<<<<<<<<<<<<
- *         'contig' : (0, bytes),
+ *         'contig' : (0, str),
  *         'pos' : (1, int),
  */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
 
-  /* "pysam/TabProxies.pyx":672
+  /* "pysam/TabProxies.pyx":731
  *     '''
  *     map_key2field = {
- *         'contig' : (0, bytes),             # <<<<<<<<<<<<<<
+ *         'contig' : (0, str),             # <<<<<<<<<<<<<<
  *         'pos' : (1, int),
- *         'id' : (2, bytes),
+ *         'id' : (2, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 672; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 731; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_0);
   __Pyx_GIVEREF(__pyx_int_0);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_contig, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_contig, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":673
+  /* "pysam/TabProxies.pyx":732
  *     map_key2field = {
- *         'contig' : (0, bytes),
+ *         'contig' : (0, str),
  *         'pos' : (1, int),             # <<<<<<<<<<<<<<
- *         'id' : (2, bytes),
- *         'ref' : (3, bytes),
+ *         'id' : (2, str),
+ *         'ref' : (3, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_1);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_1);
@@ -11327,135 +11962,135 @@ PyMODINIT_FUNC PyInit_TabProxies(void)
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyInt_Type))));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyInt_Type))));
   __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyInt_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_pos, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_pos, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":674
- *         'contig' : (0, bytes),
+  /* "pysam/TabProxies.pyx":733
+ *         'contig' : (0, str),
  *         'pos' : (1, int),
- *         'id' : (2, bytes),             # <<<<<<<<<<<<<<
- *         'ref' : (3, bytes),
- *         'alt' : (4, bytes),
+ *         'id' : (2, str),             # <<<<<<<<<<<<<<
+ *         'ref' : (3, str),
+ *         'alt' : (4, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 674; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_2);
   __Pyx_GIVEREF(__pyx_int_2);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_id, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_id, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":675
+  /* "pysam/TabProxies.pyx":734
  *         'pos' : (1, int),
- *         'id' : (2, bytes),
- *         'ref' : (3, bytes),             # <<<<<<<<<<<<<<
- *         'alt' : (4, bytes),
- *         'qual' : (5, bytes),
+ *         'id' : (2, str),
+ *         'ref' : (3, str),             # <<<<<<<<<<<<<<
+ *         'alt' : (4, str),
+ *         'qual' : (5, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_3);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_3);
   __Pyx_GIVEREF(__pyx_int_3);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_ref, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_ref, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":676
- *         'id' : (2, bytes),
- *         'ref' : (3, bytes),
- *         'alt' : (4, bytes),             # <<<<<<<<<<<<<<
- *         'qual' : (5, bytes),
- *         'filter' : (6, bytes),
+  /* "pysam/TabProxies.pyx":735
+ *         'id' : (2, str),
+ *         'ref' : (3, str),
+ *         'alt' : (4, str),             # <<<<<<<<<<<<<<
+ *         'qual' : (5, str),
+ *         'filter' : (6, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_4);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_4);
   __Pyx_GIVEREF(__pyx_int_4);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_alt, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_alt, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":677
- *         'ref' : (3, bytes),
- *         'alt' : (4, bytes),
- *         'qual' : (5, bytes),             # <<<<<<<<<<<<<<
- *         'filter' : (6, bytes),
- *         'info' : (7, bytes),
+  /* "pysam/TabProxies.pyx":736
+ *         'ref' : (3, str),
+ *         'alt' : (4, str),
+ *         'qual' : (5, str),             # <<<<<<<<<<<<<<
+ *         'filter' : (6, str),
+ *         'info' : (7, str),
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_5);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_5);
   __Pyx_GIVEREF(__pyx_int_5);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_qual, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_qual, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":678
- *         'alt' : (4, bytes),
- *         'qual' : (5, bytes),
- *         'filter' : (6, bytes),             # <<<<<<<<<<<<<<
- *         'info' : (7, bytes),
- *         'format' : (8, bytes) }
+  /* "pysam/TabProxies.pyx":737
+ *         'alt' : (4, str),
+ *         'qual' : (5, str),
+ *         'filter' : (6, str),             # <<<<<<<<<<<<<<
+ *         'info' : (7, str),
+ *         'format' : (8, str) }
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_6);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_6);
   __Pyx_GIVEREF(__pyx_int_6);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_filter, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_filter, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":679
- *         'qual' : (5, bytes),
- *         'filter' : (6, bytes),
- *         'info' : (7, bytes),             # <<<<<<<<<<<<<<
- *         'format' : (8, bytes) }
+  /* "pysam/TabProxies.pyx":738
+ *         'qual' : (5, str),
+ *         'filter' : (6, str),
+ *         'info' : (7, str),             # <<<<<<<<<<<<<<
+ *         'format' : (8, str) }
  * 
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 679; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_7);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_7);
   __Pyx_GIVEREF(__pyx_int_7);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_info, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_info, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/TabProxies.pyx":680
- *         'filter' : (6, bytes),
- *         'info' : (7, bytes),
- *         'format' : (8, bytes) }             # <<<<<<<<<<<<<<
+  /* "pysam/TabProxies.pyx":739
+ *         'filter' : (6, str),
+ *         'info' : (7, str),
+ *         'format' : (8, str) }             # <<<<<<<<<<<<<<
  * 
- *     def __cinit__(self ):
+ *     def __cinit__(self):
  */
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 680; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 739; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_8);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_8);
   __Pyx_GIVEREF(__pyx_int_8);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyBytes_Type))));
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_format, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_format, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (PyDict_SetItem((PyObject *)__pyx_ptype_5pysam_10TabProxies_VCFProxy->tp_dict, __pyx_n_s_map_key2field, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem((PyObject *)__pyx_ptype_5pysam_10TabProxies_VCFProxy->tp_dict, __pyx_n_s_map_key2field, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   PyType_Modified(__pyx_ptype_5pysam_10TabProxies_VCFProxy);
 
@@ -11789,126 +12424,6 @@ static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno,
     }
 }
 
-static void __Pyx_RaiseArgtupleInvalid(
-    const char* func_name,
-    int exact,
-    Py_ssize_t num_min,
-    Py_ssize_t num_max,
-    Py_ssize_t num_found)
-{
-    Py_ssize_t num_expected;
-    const char *more_or_less;
-    if (num_found < num_min) {
-        num_expected = num_min;
-        more_or_less = "at least";
-    } else {
-        num_expected = num_max;
-        more_or_less = "at most";
-    }
-    if (exact) {
-        more_or_less = "exactly";
-    }
-    PyErr_Format(PyExc_TypeError,
-                 "%.200s() takes %.8s %" CYTHON_FORMAT_SSIZE_T "d positional argument%.1s (%" CYTHON_FORMAT_SSIZE_T "d given)",
-                 func_name, more_or_less, num_expected,
-                 (num_expected == 1) ? "" : "s", num_found);
-}
-
-static CYTHON_INLINE int __Pyx_CheckKeywordStrings(
-    PyObject *kwdict,
-    const char* function_name,
-    int kw_allowed)
-{
-    PyObject* key = 0;
-    Py_ssize_t pos = 0;
-#if CPYTHON_COMPILING_IN_PYPY
-    if (!kw_allowed && PyDict_Next(kwdict, &pos, &key, 0))
-        goto invalid_keyword;
-    return 1;
-#else
-    while (PyDict_Next(kwdict, &pos, &key, 0)) {
-        #if PY_MAJOR_VERSION < 3
-        if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key)))
-        #endif
-            if (unlikely(!PyUnicode_Check(key)))
-                goto invalid_keyword_type;
-    }
-    if ((!kw_allowed) && unlikely(key))
-        goto invalid_keyword;
-    return 1;
-invalid_keyword_type:
-    PyErr_Format(PyExc_TypeError,
-        "%.200s() keywords must be strings", function_name);
-    return 0;
-#endif
-invalid_keyword:
-    PyErr_Format(PyExc_TypeError,
-    #if PY_MAJOR_VERSION < 3
-        "%.200s() got an unexpected keyword argument '%.200s'",
-        function_name, PyString_AsString(key));
-    #else
-        "%s() got an unexpected keyword argument '%U'",
-        function_name, key);
-    #endif
-    return 0;
-}
-
-static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
-    PyErr_Format(PyExc_ValueError,
-                 "too many values to unpack (expected %" CYTHON_FORMAT_SSIZE_T "d)", expected);
-}
-
-static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
-    PyErr_Format(PyExc_ValueError,
-                 "need more than %" CYTHON_FORMAT_SSIZE_T "d value%.1s to unpack",
-                 index, (index == 1) ? "" : "s");
-}
-
-static CYTHON_INLINE int __Pyx_IterFinish(void) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyThreadState *tstate = PyThreadState_GET();
-    PyObject* exc_type = tstate->curexc_type;
-    if (unlikely(exc_type)) {
-        if (likely(exc_type == PyExc_StopIteration) || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration)) {
-            PyObject *exc_value, *exc_tb;
-            exc_value = tstate->curexc_value;
-            exc_tb = tstate->curexc_traceback;
-            tstate->curexc_type = 0;
-            tstate->curexc_value = 0;
-            tstate->curexc_traceback = 0;
-            Py_DECREF(exc_type);
-            Py_XDECREF(exc_value);
-            Py_XDECREF(exc_tb);
-            return 0;
-        } else {
-            return -1;
-        }
-    }
-    return 0;
-#else
-    if (unlikely(PyErr_Occurred())) {
-        if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) {
-            PyErr_Clear();
-            return 0;
-        } else {
-            return -1;
-        }
-    }
-    return 0;
-#endif
-}
-
-static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected) {
-    if (unlikely(retval)) {
-        Py_DECREF(retval);
-        __Pyx_RaiseTooManyValuesError(expected);
-        return -1;
-    } else {
-        return __Pyx_IterFinish();
-    }
-    return 0;
-}
-
 static void __Pyx_RaiseDoubleKeywordsError(
     const char* func_name,
     PyObject* kw_name)
@@ -12023,6 +12538,87 @@ bad:
     return -1;
 }
 
+static void __Pyx_RaiseArgtupleInvalid(
+    const char* func_name,
+    int exact,
+    Py_ssize_t num_min,
+    Py_ssize_t num_max,
+    Py_ssize_t num_found)
+{
+    Py_ssize_t num_expected;
+    const char *more_or_less;
+    if (num_found < num_min) {
+        num_expected = num_min;
+        more_or_less = "at least";
+    } else {
+        num_expected = num_max;
+        more_or_less = "at most";
+    }
+    if (exact) {
+        more_or_less = "exactly";
+    }
+    PyErr_Format(PyExc_TypeError,
+                 "%.200s() takes %.8s %" CYTHON_FORMAT_SSIZE_T "d positional argument%.1s (%" CYTHON_FORMAT_SSIZE_T "d given)",
+                 func_name, more_or_less, num_expected,
+                 (num_expected == 1) ? "" : "s", num_found);
+}
+
+static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
+    PyErr_Format(PyExc_ValueError,
+                 "too many values to unpack (expected %" CYTHON_FORMAT_SSIZE_T "d)", expected);
+}
+
+static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
+    PyErr_Format(PyExc_ValueError,
+                 "need more than %" CYTHON_FORMAT_SSIZE_T "d value%.1s to unpack",
+                 index, (index == 1) ? "" : "s");
+}
+
+static CYTHON_INLINE int __Pyx_IterFinish(void) {
+#if CYTHON_COMPILING_IN_CPYTHON
+    PyThreadState *tstate = PyThreadState_GET();
+    PyObject* exc_type = tstate->curexc_type;
+    if (unlikely(exc_type)) {
+        if (likely(exc_type == PyExc_StopIteration) || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration)) {
+            PyObject *exc_value, *exc_tb;
+            exc_value = tstate->curexc_value;
+            exc_tb = tstate->curexc_traceback;
+            tstate->curexc_type = 0;
+            tstate->curexc_value = 0;
+            tstate->curexc_traceback = 0;
+            Py_DECREF(exc_type);
+            Py_XDECREF(exc_value);
+            Py_XDECREF(exc_tb);
+            return 0;
+        } else {
+            return -1;
+        }
+    }
+    return 0;
+#else
+    if (unlikely(PyErr_Occurred())) {
+        if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) {
+            PyErr_Clear();
+            return 0;
+        } else {
+            return -1;
+        }
+    }
+    return 0;
+#endif
+}
+
+static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected) {
+    if (unlikely(retval)) {
+        Py_DECREF(retval);
+        __Pyx_RaiseTooManyValuesError(expected);
+        return -1;
+    } else {
+        return __Pyx_IterFinish();
+    }
+    return 0;
+}
+
 static CYTHON_INLINE int __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
     if (likely(PyList_CheckExact(L))) {
         if (unlikely(__Pyx_PyList_Append(L, x) < 0)) return -1;
@@ -12058,6 +12654,45 @@ static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {
     return result;
 }
 
+static CYTHON_INLINE int __Pyx_CheckKeywordStrings(
+    PyObject *kwdict,
+    const char* function_name,
+    int kw_allowed)
+{
+    PyObject* key = 0;
+    Py_ssize_t pos = 0;
+#if CPYTHON_COMPILING_IN_PYPY
+    if (!kw_allowed && PyDict_Next(kwdict, &pos, &key, 0))
+        goto invalid_keyword;
+    return 1;
+#else
+    while (PyDict_Next(kwdict, &pos, &key, 0)) {
+        #if PY_MAJOR_VERSION < 3
+        if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key)))
+        #endif
+            if (unlikely(!PyUnicode_Check(key)))
+                goto invalid_keyword_type;
+    }
+    if ((!kw_allowed) && unlikely(key))
+        goto invalid_keyword;
+    return 1;
+invalid_keyword_type:
+    PyErr_Format(PyExc_TypeError,
+        "%.200s() keywords must be strings", function_name);
+    return 0;
+#endif
+invalid_keyword:
+    PyErr_Format(PyExc_TypeError,
+    #if PY_MAJOR_VERSION < 3
+        "%.200s() got an unexpected keyword argument '%.200s'",
+        function_name, PyString_AsString(key));
+    #else
+        "%s() got an unexpected keyword argument '%U'",
+        function_name, key);
+    #endif
+    return 0;
+}
+
 static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals) {
 #if CYTHON_COMPILING_IN_PYPY
     return PyObject_RichCompareBool(s1, s2, equals);
diff --git a/pysam/TabProxies.pxd b/pysam/TabProxies.pxd
index 61a2a5d..22211eb 100644
--- a/pysam/TabProxies.pxd
+++ b/pysam/TabProxies.pxd
@@ -47,29 +47,33 @@ cdef class TupleProxy:
         int index
         int nbytes
         int offset
-        cdef bint is_modified
+        bint is_modified
 
-    cdef int getMaxFields( self, size_t nbytes )
-#    cdef char * _getindex( self, int idx )
+    cdef encoding
 
-    cdef take( self, char * buffer, size_t nbytes )
-    cdef present( self, char * buffer, size_t nbytes )
-    cdef copy( self, char * buffer, size_t nbytes )
-    cdef update( self, char * buffer, size_t nbytes )
+    cdef int getMaxFields(self)
+    cdef int getMinFields(self)
+#    cdef char * _getindex(self, int idx)
+
+    cdef take(self, char * buffer, size_t nbytes)
+    cdef present(self, char * buffer, size_t nbytes)
+    cdef copy(self, char * buffer, size_t nbytes)
+    cdef update(self, char * buffer, size_t nbytes)
 
-cdef class GTFProxy( TupleProxy) :
+cdef class GTFProxy(TupleProxy) :
 
     cdef:
         char * _attributes
         cdef bint hasOwnAttributes
 
-    cdef int getMaxFields( self, size_t nbytes )
+    cdef int getMaxFields(self)
+    cdef int getMinFields(self)
     cdef char * getAttributes( self )
 
-cdef class NamedTupleProxy( TupleProxy) :
+cdef class NamedTupleProxy(TupleProxy) :
     pass
 
-cdef class BedProxy( NamedTupleProxy) :
+cdef class BedProxy(NamedTupleProxy) :
 
     cdef:
         char * contig
@@ -77,10 +81,11 @@ cdef class BedProxy( NamedTupleProxy) :
         uint32_t end
         int bedfields
 
-    cdef int getMaxFields( self, size_t nbytes )
-    cdef update( self, char * buffer, size_t nbytes )
+    cdef int getMaxFields(self)
+    cdef int getMinFields(self)
+    cdef update(self, char * buffer, size_t nbytes)
 
-cdef class VCFProxy( NamedTupleProxy) :
+cdef class VCFProxy(NamedTupleProxy) :
 
     cdef:
         char * contig
diff --git a/pysam/TabProxies.pyx b/pysam/TabProxies.pyx
index 45e40f5..bc2742d 100644
--- a/pysam/TabProxies.pyx
+++ b/pysam/TabProxies.pyx
@@ -3,8 +3,11 @@ import sys
 import string
 
 from cpython.version cimport PY_MAJOR_VERSION
+from cpython cimport PyErr_SetString, PyBytes_Check
+from cpython cimport PyUnicode_Check, PyBytes_FromStringAndSize
+
+from libc.stdio cimport printf
 
-from cpython cimport PyErr_SetString, PyBytes_Check, PyUnicode_Check, PyBytes_FromStringAndSize
 
 cdef from_string_and_size(char* s, size_t length):
     if PY_MAJOR_VERSION < 3:
@@ -20,19 +23,7 @@ if _FILENAME_ENCODING is None:
 if _FILENAME_ENCODING is None:
     _FILENAME_ENCODING = 'ascii'
 
-cdef bytes _my_encodeFilename(object filename):
-    u"""Make sure a filename is 8-bit encoded (or None).
-    """
-    if filename is None:
-        return None
-    elif PyBytes_Check(filename):
-        return filename
-    elif PyUnicode_Check(filename):
-        return filename.encode(_FILENAME_ENCODING)
-    else:
-        raise TypeError, u"Argument must be string or unicode."
-
-cdef bytes _force_bytes(object s):
+cdef bytes _force_bytes(object s, encoding="ascii"):
     u"""convert string or unicode object to bytes, assuming ascii encoding.
     """
     if PY_MAJOR_VERSION < 3:
@@ -42,46 +33,50 @@ cdef bytes _force_bytes(object s):
     elif PyBytes_Check(s):
         return s
     elif PyUnicode_Check(s):
-        return s.encode('ascii')
+        return s.encode(encoding)
     else:
         raise TypeError, u"Argument must be string, bytes or unicode."
 
 cdef inline bytes _force_cmdline_bytes(object s):
     return _force_bytes(s)
 
-cdef _charptr_to_str(char* s):
+cdef _charptr_to_str(char* s, encoding="ascii"):
     if PY_MAJOR_VERSION < 3:
         return s
     else:
-        return s.decode("ascii")
+        return s.decode(encoding)
 
-cdef _force_str(object s):
-    """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
+cdef inline _force_str(object s, encoding="ascii"):
+    """Return s converted to str type of current Python "
+    "(bytes in Py2, unicode in Py3)"""
     if s is None:
         return None
     if PY_MAJOR_VERSION < 3:
         return s
     elif PyBytes_Check(s):
-        return s.decode('ascii')
+        return s.decode(encoding)
     else:
         # assume unicode
         return s
 
-cdef char * nextItem( char * buffer ):
+cdef char * nextItem(char * buffer):
     cdef char * pos
-    pos = strchr( buffer, '\t' )
-    if pos == NULL: raise ValueError( "malformatted entry at %s" % buffer )
+    pos = strchr(buffer, '\t')
+    if pos == NULL:
+        raise ValueError("malformatted entry at %s" % buffer)
     pos[0] = '\0'
     pos += 1
     return pos
 
-cdef char *StrOrEmpty( char * buffer ):
-     if buffer == NULL: return ""
+cdef char *StrOrEmpty(char * buffer):
+     if buffer == NULL:
+         return ""
      else: return buffer
 
-cdef int isNew( char * p, char * buffer, size_t nbytes ):
-     if p == NULL: return 0
-     return not (buffer <= p < buffer + nbytes )
+cdef int isNew(char * p, char * buffer, size_t nbytes):
+     if p == NULL:
+         return 0
+     return not (buffer <= p < buffer + nbytes)
 
 cdef class TupleProxy:
     '''Proxy class for access to parsed row as a tuple.
@@ -94,7 +89,7 @@ cdef class TupleProxy:
 
     '''
 
-    def __cinit__(self ): 
+    def __cinit__(self, encoding="ascii"): 
         self.data = NULL
         self.fields = NULL
         self.index = 0
@@ -103,35 +98,38 @@ cdef class TupleProxy:
         self.nfields = 0
         # start counting at field offset
         self.offset = 0
+        self.encoding = encoding
 
     def __dealloc__(self):
         cdef int x
         if self.is_modified:
             for x from 0 <= x < self.nfields:
-                if isNew( self.fields[x], self.data, self.nbytes ):
-                    free( self.fields[x] )
+                if isNew(self.fields[x], self.data, self.nbytes):
+                    free(self.fields[x])
                     self.fields[x] = NULL
 
-        if self.data != NULL: free(self.data)
-        if self.fields != NULL: free( self.fields )
+        if self.data != NULL:
+            free(self.data)
+        if self.fields != NULL:
+            free(self.fields)
 
-    cdef take( self, char * buffer, size_t nbytes ):
+    cdef take(self, char * buffer, size_t nbytes):
         '''start presenting buffer.
 
         Take ownership of the pointer.
         '''
         self.data = buffer
         self.nbytes = nbytes
-        self.update( buffer, nbytes )
+        self.update(buffer, nbytes)
 
-    cdef present( self, char * buffer, size_t nbytes ):
+    cdef present(self, char * buffer, size_t nbytes):
         '''start presenting buffer.
 
         Do not take ownership of the pointer.
         '''
-        self.update( buffer, nbytes )
+        self.update(buffer, nbytes)
 
-    cdef copy( self, char * buffer, size_t nbytes ):
+    cdef copy(self, char * buffer, size_t nbytes):
         '''start presenting buffer of size *nbytes*.
 
         Buffer is a '\0'-terminated string without the '\n'.
@@ -141,18 +139,25 @@ cdef class TupleProxy:
         cdef int s
         # +1 for '\0'
         s = sizeof(char) *  (nbytes + 1)
-        self.data = <char*>malloc( s ) 
+        self.data = <char*>malloc(s)
         if self.data == NULL:
-            raise ValueError("out of memory" )
+            raise ValueError("out of memory in TupleProxy.copy()")
         self.nbytes = nbytes
-        memcpy( <char*>self.data, buffer, s )
-        self.update( self.data, nbytes )
+        memcpy(<char*>self.data, buffer, s)
+        self.update(self.data, nbytes)
 
-    cdef int getMaxFields( self, size_t nbytes ):
-        '''initialize fields.'''
-        return nbytes / 2
+    cdef int getMinFields(self):
+        '''return minimum number of fields.'''
+        # 1 is not a valid tabix entry, but TupleProxy
+        # could be more generic.
+        return 1
 
-    cdef update( self, char * buffer, size_t nbytes ):
+    cdef int getMaxFields(self):
+        '''return maximum number of fields. Return 
+        0 for unknown length.'''
+        return 0
+
+    cdef update(self, char * buffer, size_t nbytes):
         '''update internal data.
 
         *buffer* is a \0 terminated string.
@@ -164,13 +169,14 @@ cdef class TupleProxy:
         to collect any number of fields until nbytes
         is exhausted.
 
-        If max_fields is set, the number of fields is initialized to 
+        If max_fields is set, the number of fields is initialized to
         max_fields.
+
         '''
         cdef char * pos
         cdef char * old_pos
         cdef int field
-        cdef int max_fields, x
+        cdef int max_fields, min_fields, x
 
         assert strlen(buffer) == nbytes, \
             "length of buffer (%i) != number of bytes (%i)" % (
@@ -189,20 +195,29 @@ cdef class TupleProxy:
 
         #################################
         # clear data
-        if self.fields != NULL: free(self.fields)
-        
+        if self.fields != NULL:
+            free(self.fields)
+ 
         for field from 0 <= field < self.nfields:
-            if isNew( self.fields[field], self.data, self.nbytes ):
-                free( self.fields[field] )
+            if isNew(self.fields[field], self.data, self.nbytes):
+                free(self.fields[field])
                 
         self.is_modified = self.nfields = 0
 
         #################################
         # allocate new
-        max_fields = self.getMaxFields( nbytes )
-        self.fields = <char **>calloc( max_fields, sizeof(char *) ) 
+        max_fields = self.getMaxFields()
+        # pre-count fields - better would be
+        # to guess or dynamically grow
+        if max_fields == 0:
+            for x from 0 <= x < nbytes:
+                if buffer[x] == '\t':
+                    max_fields += 1
+            max_fields += 1
+
+        self.fields = <char **>calloc(max_fields, sizeof(char *)) 
         if self.fields == NULL:
-            raise ValueError("out of memory" )
+            raise ValueError("out of memory in TupleProxy.update()")
 
         #################################
         # start filling
@@ -210,73 +225,89 @@ cdef class TupleProxy:
         self.fields[field] = pos = buffer
         field += 1
         old_pos = pos
-        
         while 1:
+            
+            pos = <char*>memchr(pos, '\t', nbytes)
+            if pos == NULL:
+                break
+            if field >= max_fields:
+                raise ValueError(
+                    "parsing error: more than %i fields in line: %s" %
+                    (max_fields, buffer))
 
-            pos = <char*>memchr( pos, '\t', nbytes )
-            if pos == NULL: break
             pos[0] = '\0'
             pos += 1
             self.fields[field] = pos
             field += 1
-            if field > max_fields:
-                raise ValueError("row too large - more than %i fields" % max_fields )
             nbytes -= pos - old_pos
-            if nbytes < 0: break
+            if nbytes < 0:
+                break
             old_pos = pos
-
         self.nfields = field
+        if self.nfields < self.getMinFields():
+            raise ValueError(
+                "parsing error: fewer that %i fields in line: %s" %
+                (self.getMinFields(), buffer))
 
-    def _getindex( self, int index ):
+    def _getindex(self, int index):
         '''return item at idx index'''
         cdef int i = index
-        if i < 0: i += self.nfields
-        if i < 0: raise IndexError( "list index out of range" )
+        if i < 0:
+            i += self.nfields
+        if i < 0:
+            raise IndexError("list index out of range")
+        # apply offset - separating a fixed number 
+        # of fields from a variable number such as in VCF
         i += self.offset
         if i >= self.nfields:
-            raise IndexError( "list index out of range %i >= %i" % (i, self.nfields ))
-        return self.fields[i] 
+            raise IndexError(
+                "list index out of range %i >= %i" %
+                (i, self.nfields))
+        return _force_str(self.fields[i], self.encoding)
 
-    def __getitem__( self, key ):
-        if type(key) == int: return self._getindex( key )
+    def __getitem__(self, key):
+        if type(key) == int:
+            return self._getindex(key)
         # slice object
-        start, end, step = key.indices( self.nfields )
+        start, end, step = key.indices(self.nfields)
         result = []
-        for index in range( start, end, step ):
-            result.append( self._getindex( index ) )
+        for index in range(start, end, step):
+            result.append(self._getindex(index))
         return result
 
-    def _setindex( self, index, value ):
+    def _setindex(self, index, value):
         '''set item at idx index.'''
         cdef int idx = index
-        if idx < 0: raise IndexError( "list index out of range" )        
+        if idx < 0:
+            raise IndexError("list index out of range")
         if idx >= self.nfields:
-            raise IndexError( "list index out of range" )
+            raise IndexError("list index out of range")
 
-        if isNew( self.fields[idx], self.data, self.nbytes ):
-            free( self.fields[idx] )
+        if isNew(self.fields[idx], self.data, self.nbytes):
+            free(self.fields[idx] )
 
         self.is_modified = 1
 
-        if value == None:
+        if value is None:
             self.fields[idx] = NULL
             return
 
         # conversion with error checking
         value = _force_bytes(value)
         cdef char * tmp = <char*>value
-        self.fields[idx] = <char*>malloc( (strlen( tmp ) + 1) * sizeof(char) )
+        self.fields[idx] = <char*>malloc((strlen( tmp ) + 1) * sizeof(char))
         if self.fields[idx] == NULL:
             raise ValueError("out of memory" )
-        strcpy( self.fields[idx], tmp )
+        strcpy(self.fields[idx], tmp)
 
-    def __setitem__(self, index, value ):
+    def __setitem__(self, index, value):
         '''set item at *index* to *value*'''
         cdef int i = index
-        if i < 0: i += self.nfields
+        if i < 0:
+            i += self.nfields
         i += self.offset
         
-        self._setindex( i, value )
+        self._setindex(i, value)
 
     def __len__(self):
         return self.nfields
@@ -292,8 +323,10 @@ cdef class TupleProxy:
             raise StopIteration
         cdef char * retval = self.fields[self.index]
         self.index += 1
-        if retval == NULL: return None
-        else: return retval
+        if retval == NULL:
+            return None
+        else:
+            return _force_str(retval, self.encoding)
 
     def __str__(self):
         '''return original data'''
@@ -301,33 +334,38 @@ cdef class TupleProxy:
         if self.is_modified:
             # todo: treat NULL values
             result = []
-            for x in xrange( 0, self.nfields ):
-                result.append( StrOrEmpty( self.fields[x]).decode('ascii') )
-            return "\t".join( result )
+            for x in xrange(0, self.nfields):
+                result.append(StrOrEmpty(self.fields[x]).decode(self.encoding))
+            return "\t".join(result)
         else:
-            cpy = <char*>calloc( sizeof(char), self.nbytes+1 )
+            cpy = <char*>calloc(sizeof(char), self.nbytes+1)
             if cpy == NULL:
-                raise ValueError("out of memory" )
-            memcpy( cpy, self.data, self.nbytes+1)
+                raise ValueError("out of memory")
+            memcpy(cpy, self.data, self.nbytes+1)
             for x from 0 <= x < self.nbytes:
-                if cpy[x] == '\0': cpy[x] = '\t'
+                if cpy[x] == '\0':
+                    cpy[x] = '\t'
             result = cpy[:self.nbytes]
             free(cpy)
-            return result.decode('ascii')
+            r = result.decode(self.encoding)
+            return r
 
-def toDot( v ):
+def toDot(v):
     '''convert value to '.' if None'''
-    if v == None: return "." 
-    else: return str(v)
+    if v is None:
+        return "." 
+    else:
+        return str(v)
 
-def quote( v ):
+def quote(v):
     '''return a quoted attribute.'''
     if type(v) in types.StringTypes:
         return '"%s"' % v
     else: 
         return str(v)
 
-cdef class GTFProxy( TupleProxy ):
+
+cdef class GTFProxy(TupleProxy):
     '''Proxy class for access to GTF fields.
 
     This class represents a GTF entry for fast read-access.
@@ -340,7 +378,7 @@ cdef class GTFProxy( TupleProxy ):
     a dictionary - this field will manage its own memory.
     '''
 
-    def __cinit__(self ): 
+    def __cinit__(self): 
         # automatically calls TupleProxy.__cinit__
         self.hasOwnAttributes = False
         self._attributes = NULL
@@ -350,19 +388,27 @@ cdef class GTFProxy( TupleProxy ):
         if self.hasOwnAttributes:
             free(self._attributes)
 
-    cdef int getMaxFields( self, size_t nbytes ):
+    cdef int getMinFields(self):
+        '''return minimum number of fields.'''
+        return 3
+
+    cdef int getMaxFields(self):
         '''return max number of fields.'''
         return 9
 
     property contig:
        '''contig of feature.'''
-       def __get__( self ): return self._getindex( 0 )
-       def __set__( self, value ): self._setindex( 0, value )
+       def __get__(self):
+           return self._getindex(0)
+       def __set__(self, value):
+           self._setindex(0, value)
 
     property source:
        '''feature source.'''
-       def __get__( self ): return self._getindex( 1 )
-       def __set__( self, value ): self._setindex( 1, value )
+       def __get__(self):
+           return self._getindex(1)
+       def __set__(self, value):
+           self._setindex(1, value)
 
     property feature:
        '''feature name.'''
@@ -406,7 +452,7 @@ cdef class GTFProxy( TupleProxy ):
            if self.hasOwnAttributes:
                return self._attributes
            else:
-               return self._getindex( 8 )
+               return self._getindex(8)
        def __set__( self, value ): 
            if self.hasOwnAttributes:
                free(self._attributes)
@@ -414,12 +460,12 @@ cdef class GTFProxy( TupleProxy ):
                self.hasOwnAttributes = False
            self._setindex(8, value )
 
-    cdef char * getAttributes( self ):
+    cdef char * getAttributes(self):
        '''return pointer to attributes.'''
        if self.hasOwnAttributes:
            return self._attributes
        else:
-           return self.fields[ 8 ]
+           return self.fields[8]
 
     def asDict( self ):
         """parse attributes - return as dict
@@ -458,7 +504,7 @@ cdef class GTFProxy( TupleProxy ):
         
         return result
     
-    def fromDict( self, d ):
+    def fromDict(self, d):
         '''set attributes from a dictionary.'''
         cdef char * p
         cdef int l
@@ -503,7 +549,7 @@ cdef class GTFProxy( TupleProxy ):
         else: 
             return TupleProxy.__str__(self)
 
-    def invert( self, int lcontig ):
+    def invert(self, int lcontig):
         '''invert coordinates to negative strand coordinates
         
         This method will only act if the feature is on the
@@ -517,12 +563,13 @@ cdef class GTFProxy( TupleProxy ):
     def keys( self ):
         '''return a list of attributes defined in this entry.'''
         r = self.attributes
-        return [ x.strip().split(" ")[0] for x in r.split(";") if x.strip() != '' ]
+        return [x.strip().split(" ")[0]
+                for x in r.split(";") if x.strip() != '']
 
     def __getitem__(self, key):
-        return self.__getattr__( key )
+        return self.__getattr__(key)
 
-    def __getattr__(self, item ):
+    def __getattr__(self, item):
         """Generic lookup of attribute from GFF/GTF attributes 
         Only called if there *isn't* an attribute with this name
         """
@@ -554,7 +601,7 @@ cdef class GTFProxy( TupleProxy ):
         start = strstr(attributes, query)
 
         if start == NULL:
-            raise AttributeError("'GTFProxy' has no attribute '%s'" % item )
+            raise AttributeError("'GTFProxy' has no attribute '%s'" % item)
 
         start += strlen(query)
         # skip gaps before
@@ -564,71 +611,82 @@ cdef class GTFProxy( TupleProxy ):
         if start[0] == '"':
             start += 1
             end = start
-            while end[0] != '\0' and end[0] != '"': end += 1
+            while end[0] != '\0' and end[0] != '"':
+                end += 1
             l = end - start
-            result = _force_str(PyBytes_FromStringAndSize( start, l ))
+            result = _force_str(PyBytes_FromStringAndSize(start, l),
+                                self.encoding)
             return result
         else:
-            return _force_str(start)
+            return _force_str(start, self.encoding)
 
-    def setAttribute( self, name, value ):
+    def setAttribute(self, name, value):
         '''convenience method to set an attribute.'''
         r = self.asDict()
         r[name] = value
-        self.fromDict( r )
+        self.fromDict(r)
 
-cdef class NamedTupleProxy( TupleProxy ):
+
+cdef class NamedTupleProxy(TupleProxy):
 
     map_key2field = {}
 
-    def __setattr__(self, key, value ):
+    def __setattr__(self, key, value):
         '''set attribute.'''
         cdef int idx
         idx, f = self.map_key2field[key]
         if self.nfields < idx:
-            raise KeyError( "field %s not set" % key )
-        TupleProxy.__setitem__(self, idx, str(value) )
+            raise KeyError("field %s not set" % key)
+        TupleProxy.__setitem__(self, idx, str(value))
 
-    def __getattr__(self, key ):
+    def __getattr__(self, key):
         cdef int idx
         idx, f = self.map_key2field[key]
         if self.nfields < idx:
-            raise KeyError( "field %s not set" % key )
-        return f( self.fields[idx] )
+            raise KeyError("field %s not set" % key)
+        if f == str:
+            return _force_str(self.fields[idx],
+                              self.encoding)
+        return f(self.fields[idx])
 
 
-cdef class BedProxy( NamedTupleProxy ):
+cdef class BedProxy(NamedTupleProxy):
     '''Proxy class for access to Bed fields.
 
     This class represents a BED entry for fast read-access.
     '''
     map_key2field = {
-        'contig' : (0, bytes),
+        'contig' : (0, str),
         'start' : (1, int),
         'end' : (2, int),
-        'name' : (3, bytes),
+        'name' : (3, str),
         'score' : (4, float),
-        'strand' : (5, bytes),
-        'thickStart' : (6, int ),
+        'strand' : (5, str),
+        'thickStart' : (6, int),
         'thickEnd' : (7, int),
-        'itemRGB' : (8, bytes),
+        'itemRGB' : (8, str),
         'blockCount': (9, int),
-        'blockSizes': (10, bytes),
-        'blockStarts': (11, bytes), } 
+        'blockSizes': (10, str),
+        'blockStarts': (11, str), } 
+
+    cdef int getMinFields(self):
+        '''return minimum number of fields.'''
+        return 3
 
-    cdef int getMaxFields( self, size_t nbytes ):
+    cdef int getMaxFields(self):
         '''return max number of fields.'''
         return 12
 
-    cdef update( self, char * buffer, size_t nbytes ):
+    cdef update(self, char * buffer, size_t nbytes):
         '''update internal data.
 
         nbytes does not include the terminal '\0'.
         '''
-        TupleProxy.update( self, buffer, nbytes )
+        TupleProxy.update(self, buffer, nbytes)
 
         if self.nfields < 3:
-            raise ValueError( "bed format requires at least three columns" )
+            raise ValueError(
+                "bed format requires at least three columns")
 
         # determines bed format
         self.bedfields = self.nfields
@@ -663,37 +721,38 @@ cdef class BedProxy( NamedTupleProxy ):
         idx, f = self.map_key2field[key]
         TupleProxy._setindex(self, idx, str(value) )
 
-cdef class VCFProxy( NamedTupleProxy ):
+cdef class VCFProxy(NamedTupleProxy):
     '''Proxy class for access to VCF fields.
 
-    The genotypes are accessed via index.
+    The genotypes are accessed via a numeric index.
+    Sample headers are not available.
     '''
     map_key2field = { 
-        'contig' : (0, bytes),
+        'contig' : (0, str),
         'pos' : (1, int),
-        'id' : (2, bytes),
-        'ref' : (3, bytes),
-        'alt' : (4, bytes),
-        'qual' : (5, bytes),
-        'filter' : (6, bytes),
-        'info' : (7, bytes),
-        'format' : (8, bytes) }
-
-    def __cinit__(self ): 
+        'id' : (2, str),
+        'ref' : (3, str),
+        'alt' : (4, str),
+        'qual' : (5, str),
+        'filter' : (6, str),
+        'info' : (7, str),
+        'format' : (8, str) }
+
+    def __cinit__(self): 
         # automatically calls TupleProxy.__cinit__
         # start indexed access at genotypes
         self.offset = 9
 
-    cdef update( self, char * buffer, size_t nbytes ):
+    cdef update(self, char * buffer, size_t nbytes):
         '''update internal data.
         
         nbytes does not include the terminal '\0'.
         '''
-        TupleProxy.update( self, buffer, nbytes )
+        TupleProxy.update(self, buffer, nbytes)
 
         self.contig = self.fields[0]
         # vcf counts from 1 - correct here
-        self.pos = atoi( self.fields[1] ) - 1
+        self.pos = atoi(self.fields[1]) - 1
                              
     def __len__(self):
         '''return number of genotype fields.'''
@@ -701,10 +760,10 @@ cdef class VCFProxy( NamedTupleProxy ):
 
     property pos:
        '''feature end (in 0-based open/closed coordinates).'''
-       def __get__( self ): 
+       def __get__(self): 
            return self.pos
 
-    def __setattr__(self, key, value ):
+    def __setattr__(self, key, value):
         '''set attribute.'''
         if key == "pos": 
             self.pos = value
@@ -712,5 +771,5 @@ cdef class VCFProxy( NamedTupleProxy ):
 
         cdef int idx
         idx, f = self.map_key2field[key]
-        TupleProxy._setindex(self, idx, str(value) )
+        TupleProxy._setindex(self, idx, str(value))
 
diff --git a/pysam/__init__.py b/pysam/__init__.py
index 6e8a70e..e38427a 100644
--- a/pysam/__init__.py
+++ b/pysam/__init__.py
@@ -4,13 +4,14 @@ import pysam.ctabix as ctabix
 from pysam.ctabix import *
 import pysam.csamfile as csamfile
 from pysam.csamfile import *
+import pysam.calignmentfile as calignmentfile
+from pysam.calignmentfile import *
 import pysam.cfaidx as cfaidx
 from pysam.cfaidx import *
 import pysam.cvcf as cvcf
 from pysam.cvcf import *
 import pysam.csamtools as csamtools
 
-
 import pysam.Pileup as Pileup
 import os
 
@@ -30,7 +31,7 @@ class SamtoolsDispatcher(object):
     '''samtools dispatcher.
 
     Emulates the samtools command line as module calls.
-    
+
     Captures stdout and stderr.
 
     Raises a :class:`pysam.SamtoolsError` exception in case
@@ -76,7 +77,7 @@ class SamtoolsDispatcher(object):
                           x.startswith("[bam_index_load]") or
                           x.startswith("[bam_sort_core]") or
                           x.startswith("[samopen] SAM header is present"))
-              ]
+        ]
         if stderr:
             raise SamtoolsError("\n".join(stderr))
 
@@ -91,7 +92,7 @@ class SamtoolsDispatcher(object):
 
         return stdout
 
-    def getMessages(self):
+    def get_messages(self):
         return self.stderr
 
     def usage(self):
@@ -139,12 +140,13 @@ for key, options in SAMTOOLS_DISPATCH.items():
     cmd, parser = options
     globals()[key] = SamtoolsDispatcher(cmd, parser)
 
-# hack to export all the symbols from csamtools
+# hack to export all the symbols from separate modules
 __all__ = \
     libchtslib.__all__ + \
     ctabix.__all__ + \
     cvcf.__all__ +\
     cfaidx.__all__ +\
+    calignmentfile.__all__ +\
     csamfile.__all__ +\
     ["SamtoolsError", "SamtoolsDispatcher"] +\
     list(SAMTOOLS_DISPATCH) +\
@@ -165,3 +167,17 @@ def get_defines():
     '''return a list of defined compilation parameters.'''
     return [('_FILE_OFFSET_BITS', '64'),
             ('_USE_KNETFILE', '')]
+
+
+def get_libraries():
+    '''return a list of libraries to link against.'''
+    # Note that this list does not include csamtools.so as there are
+    # numerous name conflicts with libchtslib.so.
+    dirname = os.path.abspath(os.path.join(os.path.dirname(__file__)))
+    return [os.path.join(dirname, x) for x in (
+        'libchtslib.so',
+        'TabProxies.so',
+        'cfaidx.so',
+        'csamfile.so',
+        'cvcf.so',
+        'ctabix.so')]
diff --git a/pysam/csamfile.c b/pysam/calignmentfile.c
similarity index 60%
copy from pysam/csamfile.c
copy to pysam/calignmentfile.c
index 5b338b9..8924bf6 100644
--- a/pysam/csamfile.c
+++ b/pysam/calignmentfile.c
@@ -1,4 +1,4 @@
-/* Generated by Cython 0.20.1 on Wed Jul 16 21:19:35 2014 */
+/* Generated by Cython 0.20.1 on Fri Nov 21 21:18:01 2014 */
 
 #define PY_SSIZE_T_CLEAN
 #ifndef CYTHON_USE_PYLONG_INTERNALS
@@ -338,8 +338,8 @@ static CYTHON_INLINE float __PYX_NAN() {
 #define _USE_MATH_DEFINES
 #endif
 #include <math.h>
-#define __PYX_HAVE__pysam__csamfile
-#define __PYX_HAVE_API__pysam__csamfile
+#define __PYX_HAVE__pysam__calignmentfile
+#define __PYX_HAVE_API__pysam__calignmentfile
 #include "stdint.h"
 #include "string.h"
 #include "stdlib.h"
@@ -541,7 +541,8 @@ static const char *__pyx_filename;
 
 
 static const char *__pyx_f[] = {
-  "csamfile.pyx",
+  "calignmentfile.pyx",
+  "array.pxd",
   "cfaidx.pxd",
   "type.pxd",
   "bool.pxd",
@@ -549,40 +550,46 @@ static const char *__pyx_f[] = {
 };
 
 /*--- Type declarations ---*/
-struct __pyx_obj_5pysam_6cfaidx_Fastafile;
+#ifndef _ARRAYARRAY_H
+struct arrayobject;
+typedef struct arrayobject arrayobject;
+#endif
+struct __pyx_obj_5pysam_6cfaidx_FastaFile;
 struct __pyx_obj_5pysam_6cfaidx_FastqProxy;
+struct __pyx_obj_5pysam_6cfaidx_FastqFile;
+struct __pyx_obj_5pysam_6cfaidx_Fastafile;
 struct __pyx_obj_5pysam_6cfaidx_Fastqfile;
-struct __pyx_obj_5pysam_8csamfile_AlignedRead;
-struct __pyx_obj_5pysam_8csamfile_Samfile;
-struct __pyx_obj_5pysam_8csamfile_PileupProxy;
-struct __pyx_obj_5pysam_8csamfile_PileupRead;
-struct __pyx_obj_5pysam_8csamfile_IteratorRow;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowHead;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowAll;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection;
-struct __pyx_obj_5pysam_8csamfile_IteratorColumn;
-struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion;
-struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs;
-struct __pyx_obj_5pysam_8csamfile_IndexedReads;
-struct __pyx_obj_5pysam_8csamfile_SNPCall;
-struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr;
-struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr;
-struct __pyx_t_5pysam_8csamfile___iterdata;
-typedef struct __pyx_t_5pysam_8csamfile___iterdata __pyx_t_5pysam_8csamfile___iterdata;
-struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag;
-struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData;
-struct __pyx_opt_args_5pysam_8csamfile_get_type_code;
-
-/* "pysam/csamfile.pxd":54
+struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment;
+struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile;
+struct __pyx_obj_5pysam_14calignmentfile_PileupColumn;
+struct __pyx_obj_5pysam_14calignmentfile_PileupRead;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRow;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs;
+struct __pyx_obj_5pysam_14calignmentfile_IndexedReads;
+struct __pyx_obj_5pysam_14calignmentfile_SNPCall;
+struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr;
+struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr;
+struct __pyx_t_5pysam_14calignmentfile___iterdata;
+typedef struct __pyx_t_5pysam_14calignmentfile___iterdata __pyx_t_5pysam_14calignmentfile___iterdata;
+struct __pyx_opt_args_5pysam_14calignmentfile_14AlignedSegment_setTag;
+struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData;
+struct __pyx_opt_args_5pysam_14calignmentfile__getTypeCode;
+
+/* "pysam/calignmentfile.pxd":53
  * # Utility types
  * 
  * ctypedef struct __iterdata:             # <<<<<<<<<<<<<<
  *     htsFile * htsfile
  *     bam_hdr_t * header
  */
-struct __pyx_t_5pysam_8csamfile___iterdata {
+struct __pyx_t_5pysam_14calignmentfile___iterdata {
   htsFile *htsfile;
   bam_hdr_t *header;
   hts_itr_t *iter;
@@ -592,39 +599,39 @@ struct __pyx_t_5pysam_8csamfile___iterdata {
   int seq_len;
 };
 
-/* "pysam/csamfile.pxd":73
- *     # add an alignment tag with value to the AlignedRead
+/* "pysam/calignmentfile.pxd":72
+ *     # add an alignment tag with value to the AlignedSegment
  *     # an existing tag of the same name will be replaced.
  *     cpdef setTag( self, tag, value, value_type = ?, replace = ? )             # <<<<<<<<<<<<<<
  * 
- * cdef class Samfile:
+ * cdef class AlignmentFile:
  */
-struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag {
+struct __pyx_opt_args_5pysam_14calignmentfile_14AlignedSegment_setTag {
   int __pyx_n;
   PyObject *value_type;
   PyObject *replace;
 };
 
-/* "pysam/csamfile.pxd":179
+/* "pysam/calignmentfile.pxd":179
  *     cdef char * getSequence( self )
  *     cdef setMask(self, mask)
  *     cdef setupIteratorData(self,             # <<<<<<<<<<<<<<
  *                            int tid,
  *                            int start,
  */
-struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData {
+struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData {
   int __pyx_n;
-  int reopen;
+  int multiple_iterators;
 };
 
-/* "pysam/csamfile.pyx":1948
- *     return qual
+/* "pysam/calignmentfile.pyx":2069
  * 
- * cdef inline uint8_t get_type_code(value, value_type = None):             # <<<<<<<<<<<<<<
+ * 
+ * cdef inline uint8_t _getTypeCode(value, value_type = None):             # <<<<<<<<<<<<<<
  *     '''guess type code for a *value*. If *value_type* is None,
  *     the type code will be inferred based on the Python type of
  */
-struct __pyx_opt_args_5pysam_8csamfile_get_type_code {
+struct __pyx_opt_args_5pysam_14calignmentfile__getTypeCode {
   int __pyx_n;
   PyObject *value_type;
 };
@@ -632,13 +639,13 @@ struct __pyx_opt_args_5pysam_8csamfile_get_type_code {
 /* "cfaidx.pxd":14
  *         char *s
  * 
- * cdef class Fastafile:             # <<<<<<<<<<<<<<
+ * cdef class FastaFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename, _references, _lengths, reference2length
  *     cdef faidx_t* fastafile
  */
-struct __pyx_obj_5pysam_6cfaidx_Fastafile {
+struct __pyx_obj_5pysam_6cfaidx_FastaFile {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile *__pyx_vtab;
   PyObject *_filename;
   PyObject *_references;
   PyObject *_lengths;
@@ -663,43 +670,66 @@ struct __pyx_obj_5pysam_6cfaidx_FastqProxy {
 /* "cfaidx.pxd":25
  * 
  * 
- * cdef class Fastqfile:             # <<<<<<<<<<<<<<
+ * cdef class FastqFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename
  *     cdef gzFile fastqfile
  */
-struct __pyx_obj_5pysam_6cfaidx_Fastqfile {
+struct __pyx_obj_5pysam_6cfaidx_FastqFile {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile *__pyx_vtab;
   PyObject *_filename;
   gzFile fastqfile;
   kseq_t *entry;
 };
 
 
-/* "pysam/csamfile.pxd":66
+/* "cfaidx.pxd":35
+ * 
+ * # Compatibility Layer for pysam < 0.8
+ * cdef class Fastafile(FastaFile):             # <<<<<<<<<<<<<<
+ *     pass
+ * 
+ */
+struct __pyx_obj_5pysam_6cfaidx_Fastafile {
+  struct __pyx_obj_5pysam_6cfaidx_FastaFile __pyx_base;
+};
+
+
+/* "cfaidx.pxd":38
+ *     pass
+ * 
+ * cdef class Fastqfile(FastqFile):             # <<<<<<<<<<<<<<
+ *     pass
+ */
+struct __pyx_obj_5pysam_6cfaidx_Fastqfile {
+  struct __pyx_obj_5pysam_6cfaidx_FastqFile __pyx_base;
+};
+
+
+/* "pysam/calignmentfile.pxd":65
  * #
  * # Note: need to declare all C fields and methods here
- * cdef class AlignedRead:             # <<<<<<<<<<<<<<
+ * cdef class AlignedSegment:             # <<<<<<<<<<<<<<
  * 
- *     # object that this AlignedRead represents
+ *     # object that this AlignedSegment represents
  */
-struct __pyx_obj_5pysam_8csamfile_AlignedRead {
+struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment *__pyx_vtab;
   bam1_t *_delegate;
 };
 
 
-/* "pysam/csamfile.pxd":75
+/* "pysam/calignmentfile.pxd":74
  *     cpdef setTag( self, tag, value, value_type = ?, replace = ? )
  * 
- * cdef class Samfile:             # <<<<<<<<<<<<<<
+ * cdef class AlignmentFile:             # <<<<<<<<<<<<<<
  * 
  *     cdef object _filename
  */
-struct __pyx_obj_5pysam_8csamfile_Samfile {
+struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_8csamfile_Samfile *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile *__pyx_vtab;
   PyObject *_filename;
   htsFile *htsfile;
   BGZF *fp;
@@ -714,14 +744,14 @@ struct __pyx_obj_5pysam_8csamfile_Samfile {
 };
 
 
-/* "pysam/csamfile.pxd":112
+/* "pysam/calignmentfile.pxd":111
  *     cdef char * _getrname(self, int tid)
  * 
- * cdef class PileupProxy:             # <<<<<<<<<<<<<<
+ * cdef class PileupColumn:             # <<<<<<<<<<<<<<
  *     cdef bam_pileup1_t ** plp
  *     cdef int tid
  */
-struct __pyx_obj_5pysam_8csamfile_PileupProxy {
+struct __pyx_obj_5pysam_14calignmentfile_PileupColumn {
   PyObject_HEAD
   bam_pileup1_t **plp;
   int tid;
@@ -730,16 +760,16 @@ struct __pyx_obj_5pysam_8csamfile_PileupProxy {
 };
 
 
-/* "pysam/csamfile.pxd":118
+/* "pysam/calignmentfile.pxd":117
  *     cdef int n_pu
  * 
  * cdef class PileupRead:             # <<<<<<<<<<<<<<
- *     cdef AlignedRead _alignment
+ *     cdef AlignedSegment _alignment
  *     cdef int32_t  _qpos
  */
-struct __pyx_obj_5pysam_8csamfile_PileupRead {
+struct __pyx_obj_5pysam_14calignmentfile_PileupRead {
   PyObject_HEAD
-  struct __pyx_obj_5pysam_8csamfile_AlignedRead *_alignment;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *_alignment;
   int32_t _qpos;
   int _indel;
   int _level;
@@ -749,172 +779,174 @@ struct __pyx_obj_5pysam_8csamfile_PileupRead {
 };
 
 
-/* "pysam/csamfile.pxd":127
+/* "pysam/calignmentfile.pxd":126
  *     cdef uint32_t _is_tail
  * 
  * cdef class IteratorRow:             # <<<<<<<<<<<<<<
  *     cdef int retval
  *     cdef bam1_t * b
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRow {
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRow {
   PyObject_HEAD
   int retval;
   bam1_t *b;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *samfile;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *samfile;
   htsFile *htsfile;
+  bam_hdr_t *header;
   int owns_samfile;
 };
 
 
-/* "pysam/csamfile.pxd":134
+/* "pysam/calignmentfile.pxd":134
  *     cdef int owns_samfile
  * 
  * cdef class IteratorRowRegion(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef hts_itr_t * iter
  *     cdef bam1_t * getCurrent( self )
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion *__pyx_vtab;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion *__pyx_vtab;
   hts_itr_t *iter;
 };
 
 
-/* "pysam/csamfile.pxd":139
+/* "pysam/calignmentfile.pxd":139
  *     cdef int cnext(self)
  * 
  * cdef class IteratorRowHead(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef int max_rows
  *     cdef int current_row
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowHead {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowHead *__pyx_vtab;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowHead *__pyx_vtab;
   int max_rows;
   int current_row;
 };
 
 
-/* "pysam/csamfile.pxd":145
+/* "pysam/calignmentfile.pxd":145
  *     cdef int cnext(self)
  * 
  * cdef class IteratorRowAll(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef bam1_t * getCurrent( self )
  *     cdef int cnext(self)
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowAll {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowAll *__pyx_vtab;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowAll *__pyx_vtab;
 };
 
 
-/* "pysam/csamfile.pxd":149
+/* "pysam/calignmentfile.pxd":149
  *     cdef int cnext(self)
  * 
  * cdef class IteratorRowAllRefs(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef int         tid
  *     cdef IteratorRowRegion rowiter
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
   int tid;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *rowiter;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *rowiter;
 };
 
 
-/* "pysam/csamfile.pxd":153
+/* "pysam/calignmentfile.pxd":153
  *     cdef IteratorRowRegion rowiter
  * 
  * cdef class IteratorRowSelection(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef int current_pos
  *     cdef positions
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection *__pyx_vtab;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowSelection *__pyx_vtab;
   int current_pos;
   PyObject *positions;
   BGZF *fp;
 };
 
 
-/* "pysam/csamfile.pxd":160
+/* "pysam/calignmentfile.pxd":160
  *     cdef BGZF * fp
  * 
  * cdef class IteratorColumn:             # <<<<<<<<<<<<<<
  * 
  *     # result of the last plbuf_push
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorColumn {
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn *__pyx_vtab;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *iter;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn *__pyx_vtab;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *iter;
   int tid;
   int pos;
   int n_plp;
   int mask;
   bam_pileup1_t *plp;
   bam_plp_t pileup_iter;
-  __pyx_t_5pysam_8csamfile___iterdata iterdata;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *samfile;
+  __pyx_t_5pysam_14calignmentfile___iterdata iterdata;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *samfile;
   struct __pyx_obj_5pysam_6cfaidx_Fastafile *fastafile;
   PyObject *stepper;
   int max_depth;
 };
 
 
-/* "pysam/csamfile.pxd":187
- *     cdef reset(self, tid, start, end)
+/* "pysam/calignmentfile.pxd":188
+ *     cdef _free_pileup_iter(self)
  * 
  * cdef class IteratorColumnRegion(IteratorColumn):             # <<<<<<<<<<<<<<
  *     cdef int start
  *     cdef int end
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn __pyx_base;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn __pyx_base;
   int start;
   int end;
   int truncate;
 };
 
 
-/* "pysam/csamfile.pxd":192
+/* "pysam/calignmentfile.pxd":193
  *     cdef int truncate
  * 
  * cdef class IteratorColumnAllRefs(IteratorColumn):             # <<<<<<<<<<<<<<
  *     pass
  * 
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn __pyx_base;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn __pyx_base;
 };
 
 
-/* "pysam/csamfile.pxd":195
+/* "pysam/calignmentfile.pxd":196
  *     pass
  * 
  * cdef class IndexedReads:             # <<<<<<<<<<<<<<
- *     cdef Samfile samfile
+ *     cdef AlignmentFile samfile
  *     cdef htsFile * htsfile
  */
-struct __pyx_obj_5pysam_8csamfile_IndexedReads {
+struct __pyx_obj_5pysam_14calignmentfile_IndexedReads {
   PyObject_HEAD
-  struct __pyx_obj_5pysam_8csamfile_Samfile *samfile;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *samfile;
   htsFile *htsfile;
   PyObject *index;
   int owns_samfile;
   BGZF *fp;
+  bam_hdr_t *header;
 };
 
 
-/* "pysam/csamfile.pyx":3324
+/* "pysam/calignmentfile.pyx":3530
  * 
  * 
  * cdef class SNPCall:             # <<<<<<<<<<<<<<
  *     '''the results of a SNP call.'''
  *     cdef int _tid
  */
-struct __pyx_obj_5pysam_8csamfile_SNPCall {
+struct __pyx_obj_5pysam_14calignmentfile_SNPCall {
   PyObject_HEAD
   int _tid;
   int _pos;
@@ -927,14 +959,14 @@ struct __pyx_obj_5pysam_8csamfile_SNPCall {
 };
 
 
-/* "pysam/csamfile.pyx":105
+/* "pysam/calignmentfile.pyx":110
  * cdef char* CODE2CIGAR= "MIDNSHP=X"
  * if IS_PYTHON3:
  *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
  * else:
  *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
  */
-struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr {
+struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr {
   PyObject_HEAD
   PyObject *__pyx_v_x;
   PyObject *__pyx_v_y;
@@ -944,14 +976,14 @@ struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr {
 };
 
 
-/* "pysam/csamfile.pyx":107
+/* "pysam/calignmentfile.pyx":112
  *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
  * else:
  *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
  * CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )
  * 
  */
-struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr {
+struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr {
   PyObject_HEAD
   PyObject *__pyx_v_x;
   PyObject *__pyx_v_y;
@@ -965,125 +997,152 @@ struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr {
 /* "cfaidx.pxd":14
  *         char *s
  * 
- * cdef class Fastafile:             # <<<<<<<<<<<<<<
+ * cdef class FastaFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename, _references, _lengths, reference2length
  *     cdef faidx_t* fastafile
  */
 
-struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile {
-  char *(*_fetch)(struct __pyx_obj_5pysam_6cfaidx_Fastafile *, char *, int, int, int *);
+struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile {
+  char *(*_fetch)(struct __pyx_obj_5pysam_6cfaidx_FastaFile *, char *, int, int, int *);
 };
-static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile *__pyx_vtabptr_5pysam_6cfaidx_Fastafile;
+static struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile *__pyx_vtabptr_5pysam_6cfaidx_FastaFile;
 
 
 /* "cfaidx.pxd":25
  * 
  * 
- * cdef class Fastqfile:             # <<<<<<<<<<<<<<
+ * cdef class FastqFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename
  *     cdef gzFile fastqfile
  */
 
+struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile {
+  kseq_t *(*getCurrent)(struct __pyx_obj_5pysam_6cfaidx_FastqFile *);
+  int (*cnext)(struct __pyx_obj_5pysam_6cfaidx_FastqFile *);
+};
+static struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile *__pyx_vtabptr_5pysam_6cfaidx_FastqFile;
+
+
+/* "cfaidx.pxd":35
+ * 
+ * # Compatibility Layer for pysam < 0.8
+ * cdef class Fastafile(FastaFile):             # <<<<<<<<<<<<<<
+ *     pass
+ * 
+ */
+
+struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile {
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile __pyx_base;
+};
+static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile *__pyx_vtabptr_5pysam_6cfaidx_Fastafile;
+
+
+/* "cfaidx.pxd":38
+ *     pass
+ * 
+ * cdef class Fastqfile(FastqFile):             # <<<<<<<<<<<<<<
+ *     pass
+ */
+
 struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile {
-  kseq_t *(*getCurrent)(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *);
-  int (*cnext)(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *);
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile __pyx_base;
 };
 static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile *__pyx_vtabptr_5pysam_6cfaidx_Fastqfile;
 
 
-/* "pysam/csamfile.pyx":2045
+/* "pysam/calignmentfile.pyx":2176
  * ###########################################################
  * ###########################################################
- * cdef class AlignedRead:             # <<<<<<<<<<<<<<
- *     '''
- *     Class representing an aligned read. See the SAM format specification for
+ * cdef class AlignedSegment:             # <<<<<<<<<<<<<<
+ *     '''Class representing an aligned segment.
+ * 
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead {
-  PyObject *(*setTag)(struct __pyx_obj_5pysam_8csamfile_AlignedRead *, PyObject *, PyObject *, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag *__pyx_optional_args);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment {
+  PyObject *(*setTag)(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *, PyObject *, PyObject *, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_14calignmentfile_14AlignedSegment_setTag *__pyx_optional_args);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead *__pyx_vtabptr_5pysam_8csamfile_AlignedRead;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment *__pyx_vtabptr_5pysam_14calignmentfile_AlignedSegment;
 
 
-/* "pysam/csamfile.pyx":247
+/* "pysam/calignmentfile.pyx":235
  * 
  * 
- * cdef class Samfile:             # <<<<<<<<<<<<<<
+ * cdef class AlignmentFile:             # <<<<<<<<<<<<<<
  *     '''*(filename, mode=None, template = None,
- *          referencenames = None, referencelengths = None,
+ *          referencenames=None, referencelengths = None,
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_Samfile {
-  bam_hdr_t *(*_buildHeader)(struct __pyx_obj_5pysam_8csamfile_Samfile *, PyObject *);
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_Samfile *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_Samfile *);
-  int (*write)(struct __pyx_obj_5pysam_8csamfile_Samfile *, struct __pyx_obj_5pysam_8csamfile_AlignedRead *, int __pyx_skip_dispatch);
-  char *(*_getrname)(struct __pyx_obj_5pysam_8csamfile_Samfile *, int);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile {
+  bam_hdr_t *(*_buildHeader)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *, PyObject *);
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *);
+  int (*write)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *, int __pyx_skip_dispatch);
+  char *(*_getrname)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *, int);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_Samfile *__pyx_vtabptr_5pysam_8csamfile_Samfile;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile *__pyx_vtabptr_5pysam_14calignmentfile_AlignmentFile;
 
 
-/* "pysam/csamfile.pyx":1266
- *             hts_close(self.htsfile)
+/* "pysam/calignmentfile.pyx":1345
  * 
- * cdef class IteratorRowRegion(IteratorRow):             # <<<<<<<<<<<<<<
- *     """*(Samfile samfile, int tid, int beg, int end, int reopen = True )*
  * 
+ * cdef class IteratorRowRegion(IteratorRow):             # <<<<<<<<<<<<<<
+ *     """*(AlignmentFile samfile, int tid, int beg, int end,
+ *     int multiple_iterators=False)*
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion {
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion {
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion *__pyx_vtabptr_5pysam_8csamfile_IteratorRowRegion;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion *__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowRegion;
 
 
-/* "pysam/csamfile.pyx":1330
+/* "pysam/calignmentfile.pyx":1396
  *         hts_itr_destroy(self.iter)
  * 
  * cdef class IteratorRowHead(IteratorRow):             # <<<<<<<<<<<<<<
- *     """*(Samfile samfile, n, int reopen = True)*
+ *     """*(AlignmentFile samfile, n, int multiple_iterators=False)*
  * 
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowHead {
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowHead {
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowHead *__pyx_vtabptr_5pysam_8csamfile_IteratorRowHead;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowHead *__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowHead;
 
 
-/* "pysam/csamfile.pyx":1383
+/* "pysam/calignmentfile.pyx":1445
  * 
  * 
  * cdef class IteratorRowAll(IteratorRow):             # <<<<<<<<<<<<<<
- *     """*(Samfile samfile, int reopen = True)*
+ *     """*(AlignmentFile samfile, int multiple_iterators=False)*
  * 
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowAll {
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowAll {
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowAll *__pyx_vtabptr_5pysam_8csamfile_IteratorRowAll;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowAll *__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowAll;
 
 
-/* "pysam/csamfile.pyx":1484
+/* "pysam/calignmentfile.pyx":1552
  * 
  * 
  * cdef class IteratorRowSelection(IteratorRow):             # <<<<<<<<<<<<<<
- *     """*(Samfile samfile)*
+ *     """*(AlignmentFile samfile)*
  * 
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection {
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowSelection {
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection *__pyx_vtabptr_5pysam_8csamfile_IteratorRowSelection;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowSelection *__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowSelection;
 
 
-/* "pysam/csamfile.pyx":1608
+/* "pysam/calignmentfile.pyx":1694
  *     return ret
  * 
  * cdef class IteratorColumn:             # <<<<<<<<<<<<<<
@@ -1091,17 +1150,18 @@ static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection *__pyx_vtab
  * 
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn {
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *);
-  char *(*getSequence)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *);
-  PyObject *(*setMask)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, PyObject *);
-  PyObject *(*setupIteratorData)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, int, int, int, struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData *__pyx_optional_args);
-  PyObject *(*reset)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, PyObject *, PyObject *, PyObject *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn {
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *);
+  char *(*getSequence)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *);
+  PyObject *(*setMask)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *, PyObject *);
+  PyObject *(*setupIteratorData)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *, int, int, int, struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData *__pyx_optional_args);
+  PyObject *(*reset)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *, PyObject *, PyObject *, PyObject *);
+  PyObject *(*_free_pileup_iter)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn *__pyx_vtabptr_5pysam_8csamfile_IteratorColumn;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn *__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumn;
 
 
-/* "pysam/csamfile.pyx":1782
+/* "pysam/calignmentfile.pyx":1885
  * 
  * 
  * cdef class IteratorColumnRegion(IteratorColumn):             # <<<<<<<<<<<<<<
@@ -1109,13 +1169,13 @@ static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn *__pyx_vtabptr_5p
  *     '''
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion {
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn __pyx_base;
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnRegion {
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn __pyx_base;
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion *__pyx_vtabptr_5pysam_8csamfile_IteratorColumnRegion;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnRegion *__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnRegion;
 
 
-/* "pysam/csamfile.pyx":1820
+/* "pysam/calignmentfile.pyx":1923
  * 
  * 
  * cdef class IteratorColumnAllRefs(IteratorColumn):             # <<<<<<<<<<<<<<
@@ -1123,10 +1183,10 @@ static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion *__pyx_vtab
  *     """
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs {
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn __pyx_base;
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnAllRefs {
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn __pyx_base;
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_vtabptr_5pysam_8csamfile_IteratorColumnAllRefs;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnAllRefs *__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnAllRefs;
 #ifndef CYTHON_REFNANNY
   #define CYTHON_REFNANNY 0
 #endif
@@ -1309,23 +1369,6 @@ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject
 static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
 
 #if CYTHON_COMPILING_IN_CPYTHON
-#define __Pyx_PyObject_DelAttrStr(o,n) __Pyx_PyObject_SetAttrStr(o,n,NULL)
-static CYTHON_INLINE int __Pyx_PyObject_SetAttrStr(PyObject* obj, PyObject* attr_name, PyObject* value) {
-    PyTypeObject* tp = Py_TYPE(obj);
-    if (likely(tp->tp_setattro))
-        return tp->tp_setattro(obj, attr_name, value);
-#if PY_MAJOR_VERSION < 3
-    if (likely(tp->tp_setattr))
-        return tp->tp_setattr(obj, PyString_AS_STRING(attr_name), value);
-#endif
-    return PyObject_SetAttr(obj, attr_name, value);
-}
-#else
-#define __Pyx_PyObject_DelAttrStr(o,n)   PyObject_DelAttr(o,n)
-#define __Pyx_PyObject_SetAttrStr(o,n,v) PyObject_SetAttr(o,n,v)
-#endif
-
-#if CYTHON_COMPILING_IN_CPYTHON
 static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {
     PyListObject* L = (PyListObject*) list;
     Py_ssize_t len = Py_SIZE(list);
@@ -1341,23 +1384,6 @@ static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {
 #define __Pyx_PyList_Append(L,x) PyList_Append(L,x)
 #endif
 
-#if PY_MAJOR_VERSION < 3
-#define __Pyx_PyString_Join __Pyx_PyBytes_Join
-#define __Pyx_PyBaseString_Join(s, v) (PyUnicode_CheckExact(s) ? PyUnicode_Join(s, v) : __Pyx_PyBytes_Join(s, v))
-#else
-#define __Pyx_PyString_Join PyUnicode_Join
-#define __Pyx_PyBaseString_Join PyUnicode_Join
-#endif
-#if CYTHON_COMPILING_IN_CPYTHON
-    #if PY_MAJOR_VERSION < 3
-    #define __Pyx_PyBytes_Join _PyString_Join
-    #else
-    #define __Pyx_PyBytes_Join _PyBytes_Join
-    #endif
-#else
-static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* values); /*proto*/
-#endif
-
 static CYTHON_INLINE int __Pyx_CheckKeywordStrings(PyObject *kwdict, const char* function_name, int kw_allowed); /*proto*/
 
 static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/
@@ -1446,6 +1472,23 @@ bad:
 
 static CYTHON_INLINE int __Pyx_PyObject_Append(PyObject* L, PyObject* x); /*proto*/
 
+#if PY_MAJOR_VERSION < 3
+#define __Pyx_PyString_Join __Pyx_PyBytes_Join
+#define __Pyx_PyBaseString_Join(s, v) (PyUnicode_CheckExact(s) ? PyUnicode_Join(s, v) : __Pyx_PyBytes_Join(s, v))
+#else
+#define __Pyx_PyString_Join PyUnicode_Join
+#define __Pyx_PyBaseString_Join PyUnicode_Join
+#endif
+#if CYTHON_COMPILING_IN_CPYTHON
+    #if PY_MAJOR_VERSION < 3
+    #define __Pyx_PyBytes_Join _PyString_Join
+    #else
+    #define __Pyx_PyBytes_Join _PyBytes_Join
+    #endif
+#else
+static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* values); /*proto*/
+#endif
+
 static CYTHON_INLINE int __Pyx_PySequence_Contains(PyObject* item, PyObject* seq, int eq) {
     int result = PySequence_Contains(seq, item);
     return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
@@ -1501,71 +1544,136 @@ static CYTHON_INLINE long __Pyx_div_long(long, long); /* proto */
 
 static CYTHON_INLINE long __Pyx_mod_long(long, long); /* proto */
 
+#if CYTHON_COMPILING_IN_CPYTHON
+#define __Pyx_PyObject_DelAttrStr(o,n) __Pyx_PyObject_SetAttrStr(o,n,NULL)
+static CYTHON_INLINE int __Pyx_PyObject_SetAttrStr(PyObject* obj, PyObject* attr_name, PyObject* value) {
+    PyTypeObject* tp = Py_TYPE(obj);
+    if (likely(tp->tp_setattro))
+        return tp->tp_setattro(obj, attr_name, value);
+#if PY_MAJOR_VERSION < 3
+    if (likely(tp->tp_setattr))
+        return tp->tp_setattr(obj, PyString_AS_STRING(attr_name), value);
+#endif
+    return PyObject_SetAttr(obj, attr_name, value);
+}
+#else
+#define __Pyx_PyObject_DelAttrStr(o,n)   PyObject_DelAttr(o,n)
+#define __Pyx_PyObject_SetAttrStr(o,n,v) PyObject_SetAttr(o,n,v)
+#endif
+
 static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname);
 
 static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
 
 static void* __Pyx_GetVtable(PyObject *dict); /*proto*/
 
-static PyObject *__Pyx_CalculateMetaclass(PyTypeObject *metaclass, PyObject *bases);
-
-static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type);
-
-#define __Pyx_CyFunction_USED 1
-#include <structmember.h>
-#define __Pyx_CYFUNCTION_STATICMETHOD  0x01
-#define __Pyx_CYFUNCTION_CLASSMETHOD   0x02
-#define __Pyx_CYFUNCTION_CCLASS        0x04
-#define __Pyx_CyFunction_GetClosure(f) \
-    (((__pyx_CyFunctionObject *) (f))->func_closure)
-#define __Pyx_CyFunction_GetClassObj(f) \
-    (((__pyx_CyFunctionObject *) (f))->func_classobj)
-#define __Pyx_CyFunction_Defaults(type, f) \
-    ((type *)(((__pyx_CyFunctionObject *) (f))->defaults))
-#define __Pyx_CyFunction_SetDefaultsGetter(f, g) \
-    ((__pyx_CyFunctionObject *) (f))->defaults_getter = (g)
-typedef struct {
-    PyCFunctionObject func;
-    PyObject *func_dict;
-    PyObject *func_weakreflist;
-    PyObject *func_name;
-    PyObject *func_qualname;
-    PyObject *func_doc;
-    PyObject *func_globals;
-    PyObject *func_code;
-    PyObject *func_closure;
-    PyObject *func_classobj; /* No-args super() class cell */
-    void *defaults;
-    int defaults_pyobjects;
-    int flags;
-    PyObject *defaults_tuple;   /* Const defaults tuple */
-    PyObject *defaults_kwdict;  /* Const kwonly defaults dict */
-    PyObject *(*defaults_getter)(PyObject *);
-    PyObject *func_annotations; /* function annotations dict */
-} __pyx_CyFunctionObject;
-static PyTypeObject *__pyx_CyFunctionType = 0;
-#define __Pyx_CyFunction_NewEx(ml, flags, qualname, self, module, globals, code) \
-    __Pyx_CyFunction_New(__pyx_CyFunctionType, ml, flags, qualname, self, module, globals, code)
-static PyObject *__Pyx_CyFunction_New(PyTypeObject *, PyMethodDef *ml,
-                                      int flags, PyObject* qualname,
-                                      PyObject *self,
-                                      PyObject *module, PyObject *globals,
-                                      PyObject* code);
-static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *m,
-                                                         size_t size,
-                                                         int pyobjects);
-static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *m,
-                                                            PyObject *tuple);
-static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *m,
-                                                             PyObject *dict);
-static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *m,
-                                                              PyObject *dict);
-static int __Pyx_CyFunction_init(void);
-
-static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *qualname,
-                                           PyObject *mkw, PyObject *modname, PyObject *doc); /*proto*/
-static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict,
-                                      PyObject *mkw, int calculate_metaclass, int allow_py2_metaclass); /*proto*/
+#ifndef _ARRAYARRAY_H
+#define _ARRAYARRAY_H
+typedef struct arraydescr {
+    int typecode;
+    int itemsize;
+    PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
+    int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
+#if PY_VERSION_HEX >= 0x03000000
+    char *formats;
+#endif
+} arraydescr;
+struct arrayobject {
+    PyObject_HEAD
+    Py_ssize_t ob_size;
+    union {
+        char *ob_item;
+        float *as_floats;
+        double *as_doubles;
+        int *as_ints;
+        unsigned int *as_uints;
+        unsigned char *as_uchars;
+        signed char *as_schars;
+        char *as_chars;
+        unsigned long *as_ulongs;
+        long *as_longs;
+        short *as_shorts;
+        unsigned short *as_ushorts;
+        Py_UNICODE *as_pyunicodes;
+        void *as_voidptr;
+    } data;
+    Py_ssize_t allocated;
+    struct arraydescr *ob_descr;
+    PyObject *weakreflist; /* List of weak references */
+#if PY_VERSION_HEX >= 0x03000000
+        int ob_exports;  /* Number of exported buffers */
+#endif
+};
+#ifndef NO_NEWARRAY_INLINE
+static CYTHON_INLINE PyObject * newarrayobject(PyTypeObject *type, Py_ssize_t size,
+    struct arraydescr *descr) {
+    arrayobject *op;
+    size_t nbytes;
+    if (size < 0) {
+        PyErr_BadInternalCall();
+        return NULL;
+    }
+    nbytes = size * descr->itemsize;
+    if (nbytes / descr->itemsize != (size_t)size) {
+        return PyErr_NoMemory();
+    }
+    op = (arrayobject *) type->tp_alloc(type, 0);
+    if (op == NULL) {
+        return NULL;
+    }
+    op->ob_descr = descr;
+    op->allocated = size;
+    op->weakreflist = NULL;
+    op->ob_size = size;
+    if (size <= 0) {
+        op->data.ob_item = NULL;
+    }
+    else {
+        op->data.ob_item = PyMem_NEW(char, nbytes);
+        if (op->data.ob_item == NULL) {
+            Py_DECREF(op);
+            return PyErr_NoMemory();
+        }
+    }
+    return (PyObject *) op;
+}
+#else
+PyObject* newarrayobject(PyTypeObject *type, Py_ssize_t size,
+    struct arraydescr *descr);
+#endif /* ifndef NO_NEWARRAY_INLINE */
+static CYTHON_INLINE int resize(arrayobject *self, Py_ssize_t n) {
+    void *items = (void*) self->data.ob_item;
+    PyMem_Resize(items, char, (size_t)(n * self->ob_descr->itemsize));
+    if (items == NULL) {
+        PyErr_NoMemory();
+        return -1;
+    }
+    self->data.ob_item = (char*) items;
+    self->ob_size = n;
+    self->allocated = n;
+    return 0;
+}
+static CYTHON_INLINE int resize_smart(arrayobject *self, Py_ssize_t n) {
+    void *items = (void*) self->data.ob_item;
+    Py_ssize_t newsize;
+    if (n < self->allocated) {
+        if (n*4 > self->allocated) {
+            self->ob_size = n;
+            return 0;
+        }
+    }
+    newsize = n  * 3 / 2 + 1;
+    PyMem_Resize(items, char, (size_t)(newsize * self->ob_descr->itemsize));
+    if (items == NULL) {
+        PyErr_NoMemory();
+        return -1;
+    }
+    self->data.ob_item = (char*) items;
+    self->ob_size = n;
+    self->allocated = newsize;
+    return 0;
+}
+#endif
 
 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); /*proto*/
 
@@ -1575,8 +1683,6 @@ static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *);
 
 static CYTHON_INLINE uint32_t __Pyx_PyInt_As_uint32_t(PyObject *);
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value);
-
 static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int8_t(int8_t value);
 
 static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint8_t(uint8_t value);
@@ -1593,6 +1699,8 @@ static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value);
 
 static CYTHON_INLINE size_t __Pyx_PyInt_As_size_t(PyObject *);
 
+static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value);
+
 static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_As_PY_LONG_LONG(PyObject *);
 
 static CYTHON_INLINE PyObject* __Pyx_PyInt_From_PY_LONG_LONG(PY_LONG_LONG value);
@@ -1603,16 +1711,18 @@ static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint64_t(uint64_t value);
 
 static CYTHON_INLINE int64_t __Pyx_PyInt_As_int64_t(PyObject *);
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_char(char value);
-
-static CYTHON_INLINE uint8_t __Pyx_PyInt_As_uint8_t(PyObject *);
+static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *);
 
 static CYTHON_INLINE int32_t __Pyx_PyInt_As_int32_t(PyObject *);
 
-static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *);
+static CYTHON_INLINE uint8_t __Pyx_PyInt_As_uint8_t(PyObject *);
+
+static CYTHON_INLINE PyObject* __Pyx_PyInt_From_char(char value);
 
 static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *);
 
+static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type);
+
 static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
 
 #define __Pyx_Generator_USED
@@ -1687,8 +1797,10 @@ static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
 /* Module declarations from 'pysam.chtslib' */
 
 /* Module declarations from 'pysam.cfaidx' */
-static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastafile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastaFile = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastqProxy = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastqFile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastafile = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastqfile = 0;
 
 /* Module declarations from 'cpython.version' */
@@ -1770,53 +1882,58 @@ static PyTypeObject *__pyx_ptype_7cpython_7complex_complex = 0;
 
 /* Module declarations from 'cpython' */
 
-/* Module declarations from 'pysam.csamfile' */
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_AlignedRead = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_Samfile = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_PileupProxy = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_PileupRead = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRow = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowRegion = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowHead = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowAll = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowAllRefs = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowSelection = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorColumn = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorColumnRegion = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorColumnAllRefs = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IndexedReads = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_SNPCall = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile___pyx_scope_struct__genexpr = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile___pyx_scope_struct_1_genexpr = 0;
-static PyObject *__pyx_v_5pysam_8csamfile__FILENAME_ENCODING = 0;
-static char *__pyx_v_5pysam_8csamfile_CODE2CIGAR;
-static int __pyx_v_5pysam_8csamfile_max_pos;
-static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *, size_t); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_makeAlignedRead(bam1_t *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_makePileupProxy(bam_pileup1_t **, int, int, int); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *); /*proto*/
-static int __pyx_f_5pysam_8csamfile___advance_all(void *, bam1_t *); /*proto*/
-static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *, bam1_t *); /*proto*/
-static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *); /*proto*/
-static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *); /*proto*/
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *, uint32_t, uint32_t); /*proto*/
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_qual_range(bam1_t *, uint32_t, uint32_t); /*proto*/
-static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *, struct __pyx_opt_args_5pysam_8csamfile_get_type_code *__pyx_optional_args); /*proto*/
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObject *, PyObject *, PyObject *, PyObject *); /*proto*/
-#define __Pyx_MODULE_NAME "pysam.csamfile"
-int __pyx_module_is_main_pysam__csamfile = 0;
-
-/* Implementation of 'pysam.csamfile' */
-static PyObject *__pyx_builtin_object;
+/* Module declarations from 'array' */
+
+/* Module declarations from 'cpython.array' */
+static PyTypeObject *__pyx_ptype_7cpython_5array_array = 0;
+static CYTHON_INLINE int __pyx_f_7cpython_5array_extend_buffer(arrayobject *, char *, Py_ssize_t); /*proto*/
+
+/* Module declarations from 'pysam.calignmentfile' */
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_AlignedSegment = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_AlignmentFile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_PileupColumn = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_PileupRead = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRow = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowHead = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowAll = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowAllRefs = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowSelection = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorColumn = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorColumnRegion = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorColumnAllRefs = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IndexedReads = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_SNPCall = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile___pyx_scope_struct__genexpr = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr = 0;
+static PyObject *__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING = 0;
+static char *__pyx_v_5pysam_14calignmentfile_CODE2CIGAR;
+static int __pyx_v_5pysam_14calignmentfile_max_pos;
+static PyObject *__pyx_f_5pysam_14calignmentfile_from_string_and_size(char *, size_t); /*proto*/
+static PyObject *__pyx_f_5pysam_14calignmentfile__encodeFilename(PyObject *); /*proto*/
+static PyObject *__pyx_f_5pysam_14calignmentfile__forceBytes(PyObject *); /*proto*/
+static PyObject *__pyx_f_5pysam_14calignmentfile__charptr_to_str(char *); /*proto*/
+static PyObject *__pyx_f_5pysam_14calignmentfile__forceStr(PyObject *); /*proto*/
+static PyObject *__pyx_f_5pysam_14calignmentfile_makeAlignedSegment(bam1_t *); /*proto*/
+static PyObject *__pyx_f_5pysam_14calignmentfile_makePileupColumn(bam_pileup1_t **, int, int, int); /*proto*/
+static PyObject *__pyx_f_5pysam_14calignmentfile_makePileupRead(bam_pileup1_t *); /*proto*/
+static PyObject *__pyx_f_5pysam_14calignmentfile_convertBinaryTagToList(uint8_t *); /*proto*/
+static int __pyx_f_5pysam_14calignmentfile___advance_nofilter(void *, bam1_t *); /*proto*/
+static int __pyx_f_5pysam_14calignmentfile___advance_all(void *, bam1_t *); /*proto*/
+static int __pyx_f_5pysam_14calignmentfile___advance_snpcalls(void *, bam1_t *); /*proto*/
+static CYTHON_INLINE int32_t __pyx_f_5pysam_14calignmentfile__getQueryStart(bam1_t *); /*proto*/
+static CYTHON_INLINE int32_t __pyx_f_5pysam_14calignmentfile__getQueryEnd(bam1_t *); /*proto*/
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_14calignmentfile__getSequenceRange(bam1_t *, uint32_t, uint32_t); /*proto*/
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_14calignmentfile__getQualitiesRange(bam1_t *, uint32_t, uint32_t); /*proto*/
+static CYTHON_INLINE uint8_t __pyx_f_5pysam_14calignmentfile__getTypeCode(PyObject *, struct __pyx_opt_args_5pysam_14calignmentfile__getTypeCode *__pyx_optional_args); /*proto*/
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_14calignmentfile_convert_python_tag(PyObject *, PyObject *, PyObject *, PyObject *); /*proto*/
+#define __Pyx_MODULE_NAME "pysam.calignmentfile"
+int __pyx_module_is_main_pysam__calignmentfile = 0;
+
+/* Implementation of 'pysam.calignmentfile' */
 static PyObject *__pyx_builtin_TypeError;
 static PyObject *__pyx_builtin_enumerate;
 static PyObject *__pyx_builtin_ord;
-static PyObject *__pyx_builtin_map;
 static PyObject *__pyx_builtin_ValueError;
 static PyObject *__pyx_builtin_IOError;
 static PyObject *__pyx_builtin_OverflowError;
@@ -1827,200 +1944,252 @@ static PyObject *__pyx_builtin_zip;
 static PyObject *__pyx_builtin_sorted;
 static PyObject *__pyx_builtin_KeyError;
 static PyObject *__pyx_builtin_StopIteration;
+static PyObject *__pyx_builtin_chr;
 static PyObject *__pyx_builtin_min;
 static PyObject *__pyx_builtin_max;
-static PyObject *__pyx_builtin_chr;
-static PyObject *__pyx_pf_5pysam_8csamfile_genexpr(CYTHON_UNUSED PyObject *__pyx_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_3genexpr(CYTHON_UNUSED PyObject *__pyx_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_12PileupColumn___str__(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_template, PyObject *__pyx_v_referencenames, PyObject *__pyx_v_referencelengths, PyObject *__pyx_v_text, PyObject *__pyx_v_header, PyObject *__pyx_v_port, PyObject *__pyx_v_add_sq_text, PyObject *__pyx_v_check_header, PyObject *__pyx_v_check_sq); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8gettid(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10getrname(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_tid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_14reset(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_16seek(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, uint64_t __pyx_v_offset, int __pyx_v_where); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_18tell(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid, PyObject *__pyx_v_callback, PyObject *__pyx_v_until_eof, PyObject *__pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_22head(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_n); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_until_eof); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_34write(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_36__enter__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_exc_type, CYTHON_UNUSED PyObject *__pyx_v_exc_value, CYTHON_UNUSED PyObject *__pyx_v_traceback); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8filename___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4text___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_fields, PyObject *__pyx_v_record); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_46__next__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRow *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen); /* proto */
-static void __pyx_pf_5pysam_8csamfile_11IteratorRow_2__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorRow *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_beg, int __pyx_v_end, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_6__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_n, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_4__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_positions, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_fastafile); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_6hasReference(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *__pyx_v_self, CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, int __pyx_v_truncate, CYTHON_UNUSED PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, CYTHON_UNUSED PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_8csamfile_11AlignedRead_2__dealloc__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_other); /* proto */
-static Py_hash_t __pyx_pf_5pysam_8csamfile_11AlignedRead_8__hash__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_10_convert_python_tag(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_pytag, PyObject *__pyx_v_value, PyObject *__pyx_v_fmts, PyObject *__pyx_v_args); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5qname___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5qname_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qname); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_values); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_cigar); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3seq___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_seq); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qual___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4qual_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qual); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5query___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5qqual___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6qstart___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qend___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tags); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12setTag(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag, PyObject *__pyx_v_value, PyObject *__pyx_v_value_type, PyObject *__pyx_v_replace); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4flag___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4flag_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_flag); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rname___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rname_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3tid___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3tid_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3pos___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3pos_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_pos); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3bin___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_bin); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4rlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4aend___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4alen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qual); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mtid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mtid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mpos); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mpos); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5isize___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5isize_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_isize); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_isize); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9positions___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15inferred_length___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_13aligned_pairs___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6blocks___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_14overlap(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, uint32_t __pyx_v_start, uint32_t __pyx_v_end); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_16opt(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_18fancy_str(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11PileupProxy___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3tid___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_1n___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11PileupProxy_1n_2__set__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self, PyObject *__pyx_v_n); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3pos___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_10PileupRead___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_9alignment___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_4qpos___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5indel___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_tail___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5level___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_17consensus_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_11snp_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_12IndexedReads___init__(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_12IndexedReads_2build(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_12IndexedReads_4find(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self, PyObject *__pyx_v_qname); /* proto */
-static void __pyx_pf_5pysam_8csamfile_12IndexedReads_6__dealloc__(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self); /* proto */
-static PyObject *__pyx_tp_new_5pysam_8csamfile_AlignedRead(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_Samfile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupProxy(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupRead(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRow(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowRegion(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowHead(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowAll(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowAllRefs(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowSelection(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumn(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumnRegion(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumnAllRefs(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IndexedReads(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_SNPCall(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct__genexpr(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_builtin_map;
+static PyObject *__pyx_builtin_MemoryError;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_4genexpr(CYTHON_UNUSED PyObject *__pyx_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7genexpr(CYTHON_UNUSED PyObject *__pyx_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_13AlignmentFile___cinit__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_2_isOpen(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_4_hasIndex(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_6_open(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_template, PyObject *__pyx_v_referencenames, PyObject *__pyx_v_referencelengths, PyObject *__pyx_v_text, PyObject *__pyx_v_header, PyObject *__pyx_v_port, PyObject *__pyx_v_add_sq_text, PyObject *__pyx_v_check_header, PyObject *__pyx_ [...]
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_8gettid(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_10getrname(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_tid); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_12_parseRegion(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_14reset(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_16seek(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, uint64_t __pyx_v_offset, int __pyx_v_where); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_18tell(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_20fetch(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid, PyObject *__pyx_v_callback, PyObject *__pyx_v_until_eof, PyObject *__pyx_v_multiple_iterators); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_22head(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_n, PyObject *__pyx_v_multiple_iterators); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_24mate(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_read); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_26count(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_until_eof); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_28pileup(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_kwargs); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_30close(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static void __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_32__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_34write(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_read); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_36__enter__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_38__exit__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_exc_type, CYTHON_UNUSED PyObject *__pyx_v_exc_value, CYTHON_UNUSED PyObject *__pyx_v_traceback); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_8filename___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_11nreferences___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_10references___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_7lengths___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_6mapped___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_40_checkIndex(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_8unmapped___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_12nocoordinate___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_4text___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_6header___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_42_buildLine(CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_fields, PyObject *__pyx_v_record); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_44__iter__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_46__next__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_11IteratorRow___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_multiple_iterators); /* proto */
+static void __pyx_pf_5pysam_14calignmentfile_11IteratorRow_2__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_beg, int __pyx_v_end, int __pyx_v_multiple_iterators); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion_4__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self); /* proto */
+static void __pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion_6__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_15IteratorRowHead___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_n, int __pyx_v_multiple_iterators); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_15IteratorRowHead_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_15IteratorRowHead_4__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14IteratorRowAll___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_multiple_iterators); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorRowAll_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorRowAll_4__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, PyObject *__pyx_v_multiple_iterators); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs_2nextiter(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs_4__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_20IteratorRowSelection___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, PyObject *__pyx_v_positions, int __pyx_v_multiple_iterators); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_20IteratorRowSelection_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_20IteratorRowSelection_4__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14IteratorColumn___cinit__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, PyObject *__pyx_v_kwargs); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorColumn_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorColumn_7seq_len___get__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorColumn_4addReference(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_fastafile); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorColumn_6hasReference(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self); /* proto */
+static void __pyx_pf_5pysam_14calignmentfile_14IteratorColumn_8__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_20IteratorColumnRegion___cinit__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion *__pyx_v_self, CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, int __pyx_v_truncate, CYTHON_UNUSED PyObject *__pyx_v_kwargs); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_21IteratorColumnAllRefs___cinit__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, CYTHON_UNUSED PyObject *__pyx_v_kwargs); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_toQualityString(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_qualities); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_2fromQualityString(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_quality_string); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment___init__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static void __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_2__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4__str__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6compare(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_other); /* proto */
+static Py_hash_t __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8__hash__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10query_name___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10query_name_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_qname); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4flag___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4flag_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_flag); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12reference_id___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12reference_id_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_tid); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15reference_start___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15reference_start_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_pos); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_qual); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigarstring___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigarstring_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_cigar); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_mtid); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_mpos); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12query_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15template_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15template_length_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_isize); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14query_sequence___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14query_sequence_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_seq); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15query_qualities___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15query_qualities_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_qual); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tags___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tags_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_tags); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3bin___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3bin_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_bin); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_paired___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_paired_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10is_reverse___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10is_reverse_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read1___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read1_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read2___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read2_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_secondary___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_secondary_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_13reference_end___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16reference_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_22query_alignment_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_24query_alignment_sequence___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_25query_alignment_qualities___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_21query_alignment_start___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_19query_alignment_end___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_21query_aligment_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10get_reference_positions(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_full_length); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12infer_query_length(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_always); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14get_aligned_pairs(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16get_blocks(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_18get_overlap(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, uint32_t __pyx_v_start, uint32_t __pyx_v_end); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigartuples___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigartuples_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_values); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_20setTag(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_tag, PyObject *__pyx_v_value, PyObject *__pyx_v_value_type, PyObject *__pyx_v_replace); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_22opt(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_tag); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qname___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qname_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3tid___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3tid_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3pos___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3pos_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mapq___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mapq_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rnext___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rnext_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5pnext___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5pnext_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5cigar___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5cigar_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tlen___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tlen_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3seq___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3seq_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qual___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qual_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4alen___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4alen_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4aend___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4aend_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4rlen___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4rlen_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5query___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5query_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qqual___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qqual_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6qstart___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6qstart_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qend___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qend_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qlen___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qlen_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mrnm___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mrnm_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mpos___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mpos_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rname___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rname_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5isize___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5isize_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6blocks___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_13aligned_pairs___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15inferred_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9positions___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_24overlap(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_2__str__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_12reference_id___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_9nsegments___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn_9nsegments_2__set__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self, PyObject *__pyx_v_n); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_13reference_pos___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_7pileups___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_3pos___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn_3pos_2__set__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_3tid___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn_3tid_2__set__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_1n___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn_1n_2__set__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self, PyObject *__pyx_v_v); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_10PileupRead___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_2__str__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_9alignment___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_14query_position___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_5indel___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_5level___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_6is_del___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_7is_head___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_7is_tail___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_10is_refskip___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_3tid___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_3pos___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_14reference_base___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_8genotype___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_17consensus_quality___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_11snp_quality___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_15mapping_quality___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_8coverage___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall___str__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_14calignmentfile_12IndexedReads___init__(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_multiple_iterators); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12IndexedReads_2build(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12IndexedReads_4find(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *__pyx_v_self, PyObject *__pyx_v_query_name); /* proto */
+static void __pyx_pf_5pysam_14calignmentfile_12IndexedReads_6__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *__pyx_v_self); /* proto */
+static int __pyx_pf_7cpython_5array_5array___getbuffer__(arrayobject *__pyx_v_self, Py_buffer *__pyx_v_info, CYTHON_UNUSED int __pyx_v_flags); /* proto */
+static void __pyx_pf_7cpython_5array_5array_2__releasebuffer__(CYTHON_UNUSED arrayobject *__pyx_v_self, Py_buffer *__pyx_v_info); /* proto */
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_AlignedSegment(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_AlignmentFile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_PileupColumn(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_PileupRead(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRow(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowRegion(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowHead(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowAll(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowAllRefs(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowSelection(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorColumn(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorColumnRegion(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorColumnAllRefs(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IndexedReads(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_SNPCall(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile___pyx_scope_struct__genexpr(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static char __pyx_k_A[] = "A";
 static char __pyx_k_B[] = "B";
 static char __pyx_k_C[] = "C";
@@ -2040,6 +2209,7 @@ static char __pyx_k_n[] = "n";
 static char __pyx_k_r[] = "r";
 static char __pyx_k_s[] = "@%s";
 static char __pyx_k_w[] = "w";
+static char __pyx_k_x[] = "x";
 static char __pyx_k_AS[] = "AS";
 static char __pyx_k_CL[] = "CL";
 static char __pyx_k_CN[] = "CN";
@@ -2068,29 +2238,25 @@ static char __pyx_k_SP[] = "SP";
 static char __pyx_k_SQ[] = "SQ";
 static char __pyx_k_UR[] = "UR";
 static char __pyx_k_VN[] = "VN";
-static char __pyx_k__3[] = "\t";
-static char __pyx_k__4[] = "\n";
-static char __pyx_k__6[] = "-";
-static char __pyx_k__9[] = "";
+static char __pyx_k__4[] = "-";
+static char __pyx_k__7[] = "";
 static char __pyx_k_os[] = "os";
 static char __pyx_k_rU[] = "rU";
 static char __pyx_k_rb[] = "rb";
 static char __pyx_k_re[] = "re";
 static char __pyx_k_wb[] = "wb";
 static char __pyx_k_wh[] = "wh";
-static char __pyx_k__14[] = "[:-]";
-static char __pyx_k__41[] = "@";
+static char __pyx_k__12[] = "[:-]";
+static char __pyx_k__38[] = "\n";
+static char __pyx_k__40[] = "@";
+static char __pyx_k__43[] = "\t";
 static char __pyx_k__47[] = ":";
-static char __pyx_k__76[] = "<";
-static char __pyx_k__79[] = "(";
-static char __pyx_k__80[] = ")";
+static char __pyx_k__74[] = "<";
 static char __pyx_k_all[] = "all";
 static char __pyx_k_bai[] = ".bai";
 static char __pyx_k_beg[] = "beg";
-static char __pyx_k_bin[] = "bin";
 static char __pyx_k_c_2[] = "%c";
 static char __pyx_k_chr[] = "chr";
-static char __pyx_k_doc[] = "__doc__";
 static char __pyx_k_end[] = "end";
 static char __pyx_k_ftp[] = "ftp:";
 static char __pyx_k_get[] = "get";
@@ -2103,11 +2269,10 @@ static char __pyx_k_pos[] = "pos";
 static char __pyx_k_raw[] = "raw";
 static char __pyx_k_s_2[] = "s";
 static char __pyx_k_s_s[] = "%s:%s";
-static char __pyx_k_seq[] = "seq";
-static char __pyx_k_str[] = "__str__";
 static char __pyx_k_sys[] = "sys";
 static char __pyx_k_tag[] = "tag";
 static char __pyx_k_tid[] = "tid";
+static char __pyx_k_wb0[] = "wb0";
 static char __pyx_k_wbu[] = "wbu";
 static char __pyx_k_zip[] = "zip";
 static char __pyx_k_2scB[] = "2scB";
@@ -2121,43 +2286,28 @@ static char __pyx_k_2sci[] = "2sci";
 static char __pyx_k_Zidf[] = "Zidf";
 static char __pyx_k_args[] = "args";
 static char __pyx_k_flag[] = "flag";
-static char __pyx_k_fmts[] = "fmts";
 static char __pyx_k_http[] = "http:";
 static char __pyx_k_init[] = "__init__";
 static char __pyx_k_join[] = "join";
 static char __pyx_k_main[] = "__main__";
-static char __pyx_k_mapq[] = "mapq";
 static char __pyx_k_mode[] = "mode";
-static char __pyx_k_mpos[] = "mpos";
-static char __pyx_k_mrnm[] = "mrnm";
-static char __pyx_k_mtid[] = "mtid";
 static char __pyx_k_open[] = "_open";
 static char __pyx_k_path[] = "path";
 static char __pyx_k_port[] = "port";
-static char __pyx_k_qpos[] = "qpos";
-static char __pyx_k_qseq[] = "qseq";
-static char __pyx_k_qual[] = "qual";
-static char __pyx_k_rlen[] = "rlen";
 static char __pyx_k_seek[] = "seek";
-static char __pyx_k_self[] = "self";
 static char __pyx_k_send[] = "send";
 static char __pyx_k_tags[] = "tags";
 static char __pyx_k_test[] = "__test__";
 static char __pyx_k_text[] = "text";
 static char __pyx_k_warn[] = "warn";
 static char __pyx_k_all_2[] = "__all__";
+static char __pyx_k_array[] = "array";
 static char __pyx_k_ascii[] = "ascii";
-static char __pyx_k_bqual[] = "bqual";
-static char __pyx_k_cigar[] = "cigar";
 static char __pyx_k_close[] = "close";
 static char __pyx_k_fetch[] = "fetch";
 static char __pyx_k_indel[] = "indel";
-static char __pyx_k_isize[] = "isize";
 static char __pyx_k_items[] = "items";
 static char __pyx_k_level[] = "level";
-static char __pyx_k_pytag[] = "pytag";
-static char __pyx_k_qname[] = "qname";
-static char __pyx_k_rname[] = "rname";
 static char __pyx_k_split[] = "split";
 static char __pyx_k_start[] = "start";
 static char __pyx_k_strip[] = "strip";
@@ -2167,6 +2317,7 @@ static char __pyx_k_value[] = "value";
 static char __pyx_k_where[] = "where";
 static char __pyx_k_write[] = "write";
 static char __pyx_k_2sc_is[] = "2sc%is";
+static char __pyx_k_always[] = "always";
 static char __pyx_k_append[] = "append";
 static char __pyx_k_ctypes[] = "ctypes";
 static char __pyx_k_decode[] = "decode";
@@ -2179,32 +2330,22 @@ static char __pyx_k_header[] = "header";
 static char __pyx_k_import[] = "__import__";
 static char __pyx_k_isOpen[] = "_isOpen";
 static char __pyx_k_is_del[] = "is_del";
-static char __pyx_k_l_data[] = "l_data";
-static char __pyx_k_l_qseq[] = "l_qseq";
-static char __pyx_k_m_data[] = "m_data";
-static char __pyx_k_module[] = "__module__";
-static char __pyx_k_object[] = "object";
 static char __pyx_k_offset[] = "offset";
 static char __pyx_k_record[] = "record";
 static char __pyx_k_region[] = "region";
-static char __pyx_k_reopen[] = "reopen";
 static char __pyx_k_setTag[] = "setTag";
 static char __pyx_k_sorted[] = "sorted";
 static char __pyx_k_struct[] = "struct";
 static char __pyx_k_IOError[] = "IOError";
 static char __pyx_k_OSError[] = "OSError";
-static char __pyx_k_Samfile[] = "Samfile";
 static char __pyx_k_compile[] = "compile";
 static char __pyx_k_findall[] = "findall";
 static char __pyx_k_is_head[] = "is_head";
 static char __pyx_k_is_tail[] = "is_tail";
 static char __pyx_k_islower[] = "islower";
 static char __pyx_k_isupper[] = "isupper";
-static char __pyx_k_l_qname[] = "l_qname";
 static char __pyx_k_lengths[] = "lengths";
-static char __pyx_k_n_cigar[] = "n_cigar";
 static char __pyx_k_pileups[] = "pileups";
-static char __pyx_k_prepare[] = "__prepare__";
 static char __pyx_k_replace[] = "replace";
 static char __pyx_k_samfile[] = "samfile";
 static char __pyx_k_stepper[] = "stepper";
@@ -2218,15 +2359,14 @@ static char __pyx_k_filename[] = "filename";
 static char __pyx_k_genotype[] = "genotype";
 static char __pyx_k_hasIndex[] = "_hasIndex";
 static char __pyx_k_nextiter[] = "nextiter";
+static char __pyx_k_nofilter[] = "nofilter";
 static char __pyx_k_platform[] = "platform";
-static char __pyx_k_qualname[] = "__qualname__";
 static char __pyx_k_samtools[] = "samtools";
 static char __pyx_k_tempfile[] = "tempfile";
 static char __pyx_k_template[] = "template";
 static char __pyx_k_truncate[] = "truncate";
 static char __pyx_k_warnings[] = "warnings";
 static char __pyx_k_2sccI_i_s[] = "2sccI%i%s";
-static char __pyx_k_30s_10s_s[] = "%-30s %-10s= %s";
 static char __pyx_k_MIDNSHP_X[] = "MIDNSHP=X";
 static char __pyx_k_TypeError[] = "TypeError";
 static char __pyx_k_alignment[] = "alignment";
@@ -2236,99 +2376,113 @@ static char __pyx_k_exc_value[] = "exc_value";
 static char __pyx_k_fastafile[] = "fastafile";
 static char __pyx_k_itertools[] = "itertools";
 static char __pyx_k_max_depth[] = "max_depth";
-static char __pyx_k_metaclass[] = "__metaclass__";
+static char __pyx_k_nsegments[] = "nsegments";
 static char __pyx_k_pack_into[] = "pack_into";
 static char __pyx_k_positions[] = "positions";
+static char __pyx_k_qualities[] = "qualities";
 static char __pyx_k_reference[] = "reference";
 static char __pyx_k_traceback[] = "traceback";
 static char __pyx_k_until_eof[] = "until_eof";
 static char __pyx_k_CIGAR2CODE[] = "CIGAR2CODE";
 static char __pyx_k_IS_PYTHON3[] = "IS_PYTHON3";
 static char __pyx_k_PileupRead[] = "PileupRead";
-static char __pyx_k_Query_name[] = "Query name";
 static char __pyx_k_ValueError[] = "ValueError";
 static char __pyx_k_checkIndex[] = "_checkIndex";
+static char __pyx_k_get_blocks[] = "get_blocks";
+static char __pyx_k_is_refskip[] = "is_refskip";
+static char __pyx_k_nsegmentes[] = "nsegmentes";
 static char __pyx_k_pyx_vtable[] = "__pyx_vtable__";
+static char __pyx_k_query_name[] = "query_name";
 static char __pyx_k_references[] = "references";
 static char __pyx_k_startswith[] = "startswith";
 static char __pyx_k_value_type[] = "value_type";
-static char __pyx_k_AlignedRead[] = "AlignedRead";
-static char __pyx_k_Binary_flag[] = "Binary flag";
 static char __pyx_k_CIGAR_REGEX[] = "CIGAR_REGEX";
-static char __pyx_k_Insert_size[] = "Insert size";
 static char __pyx_k_IteratorRow[] = "IteratorRow";
-static char __pyx_k_PileupProxy[] = "PileupProxy";
+static char __pyx_k_MemoryError[] = "MemoryError";
 static char __pyx_k_add_sq_text[] = "add_sq_text";
+static char __pyx_k_cigarstring[] = "cigarstring";
+static char __pyx_k_cigartuples[] = "cigartuples";
 static char __pyx_k_collections[] = "collections";
 static char __pyx_k_d_MIDNSHP_X[] = "(\\d+)([MIDNSHP=X])";
 static char __pyx_k_defaultdict[] = "defaultdict";
+static char __pyx_k_full_length[] = "full_length";
+static char __pyx_k_get_overlap[] = "get_overlap";
 static char __pyx_k_nreferences[] = "nreferences";
 static char __pyx_k_parseRegion[] = "_parseRegion";
 static char __pyx_k_snp_quality[] = "snp_quality";
-static char __pyx_k_Contig_index[] = "Contig index";
 static char __pyx_k_IndexedReads[] = "IndexedReads";
 static char __pyx_k_PileupColumn[] = "PileupColumn";
 static char __pyx_k_SQ_SN_s_LN_s[] = "@SQ\tSN:%s\tLN:%s\n";
 static char __pyx_k_check_header[] = "check_header";
-static char __pyx_k_getattribute[] = "__getattribute__";
-static char __pyx_k_version_info[] = "version_info";
-static char __pyx_k_Cigar_entries[] = "Cigar entries";
+static char __pyx_k_is_refskip_2[] = "_is_refskip";
+static char __pyx_k_query_length[] = "query_length";
+static char __pyx_k_reference_id[] = "reference_id";
+static char __pyx_k_AlignmentFile[] = "AlignmentFile";
 static char __pyx_k_Invalid_tag_s[] = "Invalid tag: %s";
 static char __pyx_k_OverflowError[] = "OverflowError";
 static char __pyx_k_StopIteration[] = "StopIteration";
 static char __pyx_k_VALID_HEADERS[] = "VALID_HEADERS";
+static char __pyx_k_reference_end[] = "reference_end";
+static char __pyx_k_reference_pos[] = "reference_pos";
+static char __pyx_k_AlignedSegment[] = "AlignedSegment";
 static char __pyx_k_AttributeError[] = "AttributeError";
 static char __pyx_k_IteratorColumn[] = "IteratorColumn";
-static char __pyx_k_Quality_scores[] = "Quality scores";
-static char __pyx_k_Query_sequence[] = "Query sequence";
 static char __pyx_k_mate_not_found[] = "mate not found";
 static char __pyx_k_properties_bin[] = "properties bin";
-static char __pyx_k_pysam_csamfile[] = "pysam.csamfile";
+static char __pyx_k_quality_string[] = "quality_string";
+static char __pyx_k_query_position[] = "query_position";
+static char __pyx_k_query_sequence[] = "query_sequence";
 static char __pyx_k_reference_base[] = "reference_base";
 static char __pyx_k_referencenames[] = "referencenames";
+static char __pyx_k_truncated_file[] = "truncated file";
 static char __pyx_k_unknown_type_s[] = "unknown type '%s'";
-static char __pyx_k_Mapping_quality[] = "Mapping quality";
 static char __pyx_k_mapping_quality[] = "mapping_quality";
 static char __pyx_k_properties_flag[] = "properties flag";
+static char __pyx_k_query_qualities[] = "query_qualities";
+static char __pyx_k_reference_start[] = "reference_start";
+static char __pyx_k_template_length[] = "template_length";
+static char __pyx_k_toQualityString[] = "toQualityString";
 static char __pyx_k_file_s_not_found[] = "file `%s` not found";
 static char __pyx_k_read_s_not_found[] = "read %s not found";
+static char __pyx_k_reference_length[] = "reference_length";
 static char __pyx_k_referencelengths[] = "referencelengths";
 static char __pyx_k_consensus_quality[] = "consensus_quality";
+static char __pyx_k_fromQualityString[] = "fromQualityString";
+static char __pyx_k_get_aligned_pairs[] = "get_aligned_pairs";
 static char __pyx_k_mapping_quality_2[] = "mapping quality";
+static char __pyx_k_next_reference_id[] = "next_reference_id";
 static char __pyx_k_tag_s_not_present[] = "tag '%s' not present";
-static char __pyx_k_PileupColumn___str[] = "PileupColumn.__str__";
 static char __pyx_k_VALID_HEADER_ORDER[] = "VALID_HEADER_ORDER";
 static char __pyx_k_VALID_HEADER_TYPES[] = "VALID_HEADER_TYPES";
 static char __pyx_k_end_out_of_range_i[] = "end out of range (%i)";
 static char __pyx_k_getdefaultencoding[] = "getdefaultencoding";
+static char __pyx_k_infer_query_length[] = "infer_query_length";
 static char __pyx_k_mate_s_is_unmapped[] = "mate %s: is unmapped";
+static char __pyx_k_multiple_iterators[] = "multiple_iterators";
 static char __pyx_k_read_s_is_unpaired[] = "read %s: is unpaired";
 static char __pyx_k_true_if_QC_failure[] = "true if QC failure";
-static char __pyx_k_Maximum_data_length[] = "Maximum data length";
 static char __pyx_k_NotImplementedError[] = "NotImplementedError";
 static char __pyx_k_VALID_HEADER_FIELDS[] = "VALID_HEADER_FIELDS";
 static char __pyx_k_invalid_reference_s[] = "invalid reference `%s`";
+static char __pyx_k_query_alignment_end[] = "query_alignment_end";
 static char __pyx_k_the_genotype_called[] = "the genotype called.";
-static char __pyx_k_the_template_length[] = "the template length";
-static char __pyx_k_Bam_index_bin_number[] = "Bam index bin number";
-static char __pyx_k_Length_of_query_name[] = "Length of query name";
 static char __pyx_k_create_string_buffer[] = "create_string_buffer";
+static char __pyx_k_next_reference_start[] = "next_reference_start";
+static char __pyx_k_pysam_calignmentfile[] = "pysam.calignmentfile";
 static char __pyx_k_start_out_of_range_i[] = "start out of range (%i)";
-static char __pyx_k_Position_of_mate_pair[] = "Position of mate pair";
 static char __pyx_k_could_not_open_file_s[] = "could not open file `%s`";
 static char __pyx_k_getfilesystemencoding[] = "getfilesystemencoding";
 static char __pyx_k_header_line_without_s[] = "header line without '@': '%s'";
+static char __pyx_k_query_alignment_start[] = "query_alignment_start";
 static char __pyx_k_true_if_this_is_read1[] = "true if this is read1";
 static char __pyx_k_true_if_this_is_read2[] = "true if this is read2";
-static char __pyx_k_Count_of_cigar_entries[] = "Count of cigar entries";
 static char __pyx_k_error_during_iteration[] = "error during iteration";
-static char __pyx_k_Length_of_auxilary_data[] = "Length of auxilary data";
+static char __pyx_k_query_alignment_length[] = "query_alignment_length";
 static char __pyx_k_current_sequence_length[] = "current sequence length.";
-static char __pyx_k_Length_of_query_sequence[] = "Length of query sequence";
-static char __pyx_k_the_position_of_the_mate[] = "the position of the mate";
-static char __pyx_k_Mapped_position_on_contig[] = "Mapped position on contig";
+static char __pyx_k_get_reference_positions[] = "get_reference_positions";
+static char __pyx_k_query_alignment_sequence[] = "query_alignment_sequence";
+static char __pyx_k_query_alignment_qualities[] = "query_alignment_qualities";
 static char __pyx_k_unknown_auxilliary_type_s[] = "unknown auxilliary type '%s'";
-static char __pyx_k_Contig_index_for_mate_pair[] = "Contig index for mate pair";
 static char __pyx_k_nucleotide_position_of_SNP[] = "nucleotide position of SNP.";
 static char __pyx_k_tid_i_out_of_range_0_tid_i[] = "tid %i out of range 0<=tid<%i";
 static char __pyx_k_unable_to_open_index_for_s[] = "unable to open index for `%s` ";
@@ -2342,55 +2496,51 @@ static char __pyx_k_seek_no_available_in_streams[] = "seek no available in strea
 static char __pyx_k_true_if_the_mate_is_unmapped[] = "true if the mate is unmapped";
 static char __pyx_k_no_index_available_for_pileup[] = "no index available for pileup";
 static char __pyx_k_true_if_not_primary_alignment[] = "true if not primary alignment";
-static char __pyx_k_home_andreas_devel_pysam_pysam[] = "/home/andreas/devel/pysam/pysam/csamfile.pyx";
-static char __pyx_k_term_target_ID_DEPRECATED_from[] = "\n        :term:`target` ID\n\n        DEPRECATED from pysam-0.4 - use tid in the future.\n        The rname field caused a lot of confusion as it returns\n        the :term:`target` ID instead of the reference sequence\n        name.\n\n        .. note::\n\n            This field contains the index of the reference sequence\n            in the sequence dictionary. To obtain the name\n            of the reference sequence, use :meth [...]
-static char __pyx_k_term_target_ID_note_This_field[] = "\n        :term:`target` ID\n\n        .. note::\n\n            This field contains the index of the reference sequence\n            in the sequence dictionary. To obtain the name\n            of the reference sequence, use :meth:`pysam.Samfile.getrname()`\n\n        ";
-static char __pyx_k_A_pileup_column_A_pileup_column[] = "A pileup column. A pileup column contains\n    all the reads that map to a certain target base.\n\n    tid\n        chromosome ID as is defined in the header\n    pos\n        the target base coordinate (0-based)\n    n\n        number of reads mapping to this column\n    pileups\n        list of reads (:class:`pysam.PileupRead`) aligned to this column\n    ";
-static char __pyx_k_a_list_of_start_and_end_positio[] = " a list of start and end positions of\n        aligned gapless blocks.\n\n        The start and end positions are in genomic \n        coordinates. \n      \n        Blocks are not normalized, i.e. two blocks \n        might be directly adjacent. This happens if\n        the two blocks are separated by an insertion \n        in the read.\n        ";
+static char __pyx_k_home_andreas_devel_pysam_pysam[] = "/home/andreas/devel/pysam/pysam/calignmentfile.pyx";
+static char __pyx_k_at_least_one_signed_integer_out[] = "at least one signed integer out of range of BAM/SAM specification";
 static char __pyx_k_fetch_called_on_bamfile_without[] = "fetch called on bamfile without index";
 static char __pyx_k_file_does_not_have_valid_header[] = "file does not have valid header (mode='%s') - is it BAM format?";
 static char __pyx_k_header_line_with_invalid_type_s[] = "header line with invalid type '%s': '%s'";
 static char __pyx_k_incomplete_sequence_information[] = "incomplete sequence information in '%s'";
-static char __pyx_k_inferred_read_length_from_CIGAR[] = "inferred read length from CIGAR string.\n\n        Returns 0 if CIGAR string is not present.\n        ";
 static char __pyx_k_invalid_coordinates_start_i_end[] = "invalid coordinates: start (%i) > end (%i)";
 static char __pyx_k_malformatted_header_no_in_field[] = "malformatted header: no ':' in field";
 static char __pyx_k_number_of_reads_mapping_to_this[] = "number of reads mapping to this column.";
 static char __pyx_k_quality_and_sequence_mismatch_i[] = "quality and sequence mismatch: %i != %i";
+static char __pyx_k_term_reference_ID_note_This_fie[] = ":term:`reference` ID\n\n        .. note::\n\n            This field contains the index of the reference sequence in\n            the sequence dictionary. To obtain the name of the\n            reference sequence, use\n            :meth:`pysam.AlignmentFile.getrname()`\n\n        ";
 static char __pyx_k_the_chromosome_ID_as_is_defined[] = "the chromosome ID as is defined in the header";
-static char __pyx_k_the_root_mean_square_rms_of_the[] = "the root mean square (rms) of the mapping quality of all reads involved in the call.";
+static char __pyx_k_the_query_template_name_None_if[] = "the query template name (None if not present)";
+static char __pyx_k_the_root_mean_square_rms_of_the[] = "the root mean square (rms) of the mapping quality of all reads\n       involved in the call.";
 static char __pyx_k_true_if_read_itself_is_unmapped[] = "true if read itself is unmapped";
 static char __pyx_k_1_iff_the_base_on_the_padded_rea[] = "1 iff the base on the padded read is a deletion";
+static char __pyx_k_AlignmentFile_mapped_only_availa[] = "AlignmentFile.mapped only available in bam files";
 static char __pyx_k_Argument_must_be_string_bytes_or[] = "Argument must be string, bytes or unicode.";
 static char __pyx_k_Argument_must_be_string_or_unico[] = "Argument must be string or unicode.";
 static char __pyx_k_Invalid_clipping_in_CIGAR_string[] = "Invalid clipping in CIGAR string";
-static char __pyx_k_PileupProxy_accessed_after_itera[] = "PileupProxy accessed after iterator finished";
-static char __pyx_k_Samfile_mapped_only_available_in[] = "Samfile.mapped only available in bam files";
-static char __pyx_k_This_class_cannot_be_instantiate[] = "This class cannot be instantiated from Python";
+static char __pyx_k_PileupColumn_accessed_after_iter[] = "PileupColumn accessed after iterator finished";
 static char __pyx_k_Unsupported_value_type_in_set_op[] = "Unsupported value_type in set_option";
-static char __pyx_k_a_class_pysam_AlignedRead_object[] = "a :class:`pysam.AlignedRead` object of the aligned read";
-static char __pyx_k_a_list_of_aligned_read_and_refer[] = "a list of aligned read and reference positions.\n\n        Unaligned position are marked by None.\n        ";
-static char __pyx_k_a_list_of_reference_positions_th[] = "a list of reference positions that this read aligns to.";
+static char __pyx_k_a_class_pysam_AlignedSegment_obj[] = "a :class:`pysam.AlignedSegment` object of the aligned read";
 static char __pyx_k_aligned_length_of_the_read_on_th[] = "aligned length of the read on the reference genome.\n\n        This is equal to `aend - pos`. Returns None if not available.";
-static char __pyx_k_aligned_portion_of_the_read_This[] = "aligned portion of the read.\n\n        This is a substring of :attr:`seq` that excludes flanking bases that were\n        :term:`soft clipped` (None if not present). It is equal to ``seq[qstart:qend]``.\n\n        In Python 3, this property is of type bytes. Assigning a\n        unicode string to it consisting of ASCII characters only will\n        work, but is inefficient.\n\n        SAM/BAM files may include extra flanking base [...]
-static char __pyx_k_aligned_query_sequence_quality_v[] = "aligned query sequence quality values (None if not\n        present). These are the quality values that correspond to :attr:`query`, that is,\n        they exclude qualities of :term:`soft clipped` bases. This is equal to\n        ``qual[qstart:qend]``.\n\n        This property is read-only.\n\n        In Python 3, this property is of type bytes.";
+static char __pyx_k_aligned_portion_of_the_read_This[] = "aligned portion of the read.\n\n        This is a substring of :attr:`seq` that excludes flanking\n        bases that were :term:`soft clipped` (None if not present). It\n        is equal to ``seq[qstart:qend]``.\n\n        SAM/BAM files may include extra flanking bases that are not\n        part of the alignment.  These bases may be the result of the\n        Smith-Waterman or other algorithms, which may not require\n        alig [...]
+static char __pyx_k_aligned_query_sequence_quality_v[] = "aligned query sequence quality values (None if not present). These\n        are the quality values that correspond to :attr:`query`, that\n        is, they exclude qualities of :term:`soft clipped` bases. This\n        is equal to ``qual[qstart:qend]``.\n\n        Quality scores are returned as a python array of unsigned\n        chars. Note that this is not the ASCII-encoded value typically\n        seen in FASTQ or SAM formatted [...]
 static char __pyx_k_aligned_reference_position_of_th[] = "aligned reference position of the read on the reference genome.  \n        \n        aend points to one past the last aligned residue.\n        Returns None if not available.";
+static char __pyx_k_at_least_one_integer_out_of_rang[] = "at least one integer out of range of BAM/SAM specification";
 static char __pyx_k_can_not_iterate_over_samfile_wit[] = "can not iterate over samfile without header";
 static char __pyx_k_can_only_IndexReads_on_bam_files[] = "can only IndexReads on bam files";
 static char __pyx_k_can_t_guess_type_or_invalid_type[] = "can't guess type or invalid type code specified";
 static char __pyx_k_could_not_open_file_mode_s_is_it[] = "could not open file (mode='%s') - is it SAM/BAM format?";
 static char __pyx_k_coverage_or_read_depth_the_numbe[] = "coverage or read depth - the number of reads involved in the call.";
 static char __pyx_k_either_supply_options_template_h[] = "either supply options `template`, `header` or  both `referencenames` and `referencelengths` for writing";
-static char __pyx_k_end_index_of_the_aligned_query_p[] = "end index of the aligned query portion of the sequence (0-based, exclusive)";
+static char __pyx_k_end_index_of_the_aligned_query_p[] = "end index of the aligned query portion of the sequence (0-based,\n        exclusive)";
 static char __pyx_k_fetch_called_for_htsfile_without[] = "fetch called for htsfile without header";
 static char __pyx_k_fetching_by_region_is_not_availa[] = "fetching by region is not available for sam files";
 static char __pyx_k_file_header_is_empty_mode_s_is_i[] = "file header is empty (mode='%s') - is it SAM/BAM format?";
-static char __pyx_k_full_contents_of_the_term_sam_fi[] = "full contents of the :term:`sam file` header as a string.";
-static char __pyx_k_header_information_within_the_te[] = "header information within the :term:`sam file`. The records and fields are returned as\n        a two-level dictionary.\n        ";
-static char __pyx_k_indel_length_0_for_no_indel_posi[] = "indel length; 0 for no indel, positive for ins and negative for del";
+static char __pyx_k_full_contents_of_the_term_sam_fi[] = "full contents of the :term:`sam file` header as a string\n    \n        See :attr:`pysam.AlignmentFile.header` to get a parsed\n        representation of the header.\n\n        ";
+static char __pyx_k_header_information_within_the_te[] = "header information within the :term:`sam file`. The records and\n        fields are returned as a two-level dictionary. \n\n        The first level contains the record (``HD``, ``SQ``, etc) and\n        the second level contains the fields (``VN``, ``LN``, etc).\n        \n        The parser is validating and will raise an AssertionError if\n        if encounters any record or field tags that are not part of\n        the SAM speci [...]
+static char __pyx_k_indel_length_0_for_no_indel_posi[] = "indel length; 0 for no indel, positive for ins and negative            for del";
 static char __pyx_k_integer_i_out_of_range_of_BAM_SA[] = "integer %i out of range of BAM/SAM specification";
 static char __pyx_k_invalid_type_for_record_s_s_expe[] = "invalid type for record %s: %s, expected %s";
 static char __pyx_k_length_of_the_aligned_query_sequ[] = "length of the aligned query sequence.\n\n        This is equal to :attr:`qend` - :attr:`qstart`";
-static char __pyx_k_length_of_the_read_This_includes[] = "length of the read. This includes soft-clipped bases\n        and is equal to ``len(seq)``.\n\n        This property is read-only.\n\n        Returns 0 if not available.";
+static char __pyx_k_length_of_the_query_template_Thi[] = "length of the query template. This includes soft-clipped bases\n        and is equal to ``len(seq)``.\n\n        This property is read-only.\n\n        Returns 0 if not available.";
 static char __pyx_k_list_of_reads_class_pysam_Pileup[] = "list of reads (:class:`pysam.PileupRead`) aligned to this column";
 static char __pyx_k_mapping_information_not_recorded[] = "mapping information not recorded in index or index not available";
 static char __pyx_k_multiple_s_lines_are_not_permitt[] = "multiple '%s' lines are not permitted";
@@ -2399,21 +2549,26 @@ static char __pyx_k_number_of_term_filename_associat[] = "number of :term:`filen
 static char __pyx_k_number_of_term_reference_sequenc[] = "number of :term:`reference` sequences in the file.";
 static char __pyx_k_pileup_of_samfiles_not_implement[] = "pileup of samfiles not implemented yet";
 static char __pyx_k_position_of_the_read_base_at_the[] = "position of the read base at the pileup site, 0-based";
-static char __pyx_k_read_sequence_base_qualities_inc[] = "read sequence base qualities, including :term:`soft\n        clipped` bases (None if not present).\n\n        In Python 3, this property is of type bytes and assigning a\n        unicode string to it consisting of ASCII characters only will\n        work, but is inefficient.\n\n        Note that to set quality scores the sequence has to be set\n        previously as this will determine the permitted length of\n        the quality  [...]
-static char __pyx_k_read_sequence_bases_including_te[] = "read sequence bases, including :term:`soft clipped` bases \n        (None if not present).\n\n        In Python 3, this property is of type bytes and assigning a\n        unicode string to it consisting of ASCII characters only will\n        work, but is inefficient.\n\n        Note that assigning to seq will invalidate any quality scores.\n        Thus, to in-place edit the sequence and quality scores, copies of\n        the qual [...]
+static char __pyx_k_read_sequence_base_qualities_inc[] = "read sequence base qualities, including :term:`soft\n        clipped` bases (None if not present).\n\n        Quality scores are returned as a python array of unsigned\n        chars. Note that this is not the ASCII-encoded value typically\n        seen in FASTQ or SAM formatted files. Thus, no offset of 33\n        needs to be subtracted.\n\n        Note that to set quality scores the sequence has to be set\n        beforehand as [...]
+static char __pyx_k_read_sequence_bases_including_te[] = "read sequence bases, including :term:`soft clipped` bases \n        (None if not present).\n\n        Note that assigning to seq will invalidate any quality scores.\n        Thus, to in-place edit the sequence and quality scores, copies of\n        the quality scores need to be taken. Consider trimming for example::\n\n           q = read.qual\n           read.seq = read.seq[5:10]\n           read.qual = q[5:10]\n\n        The seq [...]
 static char __pyx_k_reference_base_at_pos_N_if_no_re[] = "reference base at pos. ``N`` if no reference sequence supplied.";
+static char __pyx_k_reference_id_i_out_of_range_0_ti[] = "reference_id %i out of range 0<=tid<%i";
 static char __pyx_k_reference_sequence_for_s_tid_i_n[] = "reference sequence for '%s' (tid=%i) not found";
 static char __pyx_k_seek_only_available_in_bam_files[] = "seek only available in bam files";
-static char __pyx_k_start_index_of_the_aligned_query[] = "start index of the aligned query portion of the sequence (0-based, inclusive).\n\n        This the index of the first base in :attr:`seq` that is not soft-clipped.\n        ";
+static char __pyx_k_start_index_of_the_aligned_query[] = "start index of the aligned query portion of the sequence (0-based,\n        inclusive).\n\n        This the index of the first base in :attr:`seq` that is not\n        soft-clipped.\n\n        ";
 static char __pyx_k_the_genotype_quality_Phred_scale[] = "the genotype quality (Phred-scaled).";
-static char __pyx_k_the_insert_size_deprecated_use_t[] = "the insert size\n        deprecated: use tlen instead";
-static char __pyx_k_the_position_of_the_mate_depreca[] = "the position of the mate\n        deprecated, use PNEXT instead.";
-static char __pyx_k_the_query_name_None_if_not_prese[] = "the query name (None if not present)";
-static char __pyx_k_the_snp_quality_Phred_scaled_pro[] = "the snp quality (Phred scaled) - probability of consensus being identical to reference sequence.";
+static char __pyx_k_the_length_of_the_query_read_Thi[] = "the length of the query/read.\n\n        This value corresponds to the length of the sequence supplied\n        in the BAM/SAM file. The length of a query is 0 if there is no\n        sequence in the BAM/SAM file. In those cases, the read length\n        can be inferred from the CIGAR alignment, see\n        :meth:`pysam.AlignmentFile.infer_query_length.`.\n\n        This property can be set by providing a sequence.\n        ";
+static char __pyx_k_the_level_of_the_read_in_the_vie[] = "the level of the read in the \"viewer\" mode";
+static char __pyx_k_the_observed_query_template_leng[] = "the observed query template length";
+static char __pyx_k_the_position_in_the_reference_se[] = "the position in the reference sequence (0-based).";
+static char __pyx_k_the_position_of_the_mate_next_re[] = "the position of the mate/next read.";
+static char __pyx_k_the_reference_sequence_number_as[] = "the reference sequence number as defined in the header";
+static char __pyx_k_the_snp_quality_Phred_scaled_pro[] = "the snp quality (Phred scaled) - probability of consensus being\n       identical to reference sequence.";
 static char __pyx_k_the_tags_in_the_AUX_field_This_p[] = "the tags in the AUX field.\n\n        This property permits convenience access to\n        the tags. Changes it the returned list will\n        not update the tags automatically. Instead,\n        the following is required for adding a\n        new tag::\n\n            read.tags = read.tags + [(\"RG\",0)]\n\n        This method will happily write the same tag\n        multiple times.\n        ";
-static char __pyx_k_the_term_cigar_alignment_The_ali[] = "the :term:`cigar` alignment. The alignment\n        is returned as a list of tuples of (operation, length). \n\n        If the alignment is not present, an empty list is\n        returned.\n\n        The operations are:\n\n        +-----+--------------+-----+\n        |M    |BAM_CMATCH    |0    |\n        +-----+--------------+-----+\n        |I    |BAM_CINS      |1    |\n        +-----+--------------+-----+\n        |D    |BAM_CD [...]
-static char __pyx_k_the_term_cigar_alignment_as_a_st[] = "the :term:`cigar` alignment as a string.\n        \n        The cigar string is a string of alternating integers\n        and characters denoting the length and the type of\n        an operation.\n\n        .. note::\n            The order length,operation is specified in the\n            SAM format. It is different from the order of\n            the :attr:`cigar` property.\n\n        Returns the empty string if not present.\n\n   [...]
-static char __pyx_k_the_term_reference_id_of_the_mat[] = "the :term:`reference` id of the mate\n        deprecated, use RNEXT instead.\n        ";
+static char __pyx_k_the_term_cigar_alignment_The_ali[] = "the :term:`cigar` alignment. The alignment\n        is returned as a list of tuples of (operation, length). \n\n        If the alignment is not present, None is returned.\n\n        The operations are:\n\n        +-----+--------------+-----+\n        |M    |BAM_CMATCH    |0    |\n        +-----+--------------+-----+\n        |I    |BAM_CINS      |1    |\n        +-----+--------------+-----+\n        |D    |BAM_CDEL      |2    |\n  [...]
+static char __pyx_k_the_term_cigar_alignment_as_a_st[] = "the :term:`cigar` alignment as a string.\n        \n        The cigar string is a string of alternating integers\n        and characters denoting the length and the type of\n        an operation.\n\n        .. note::\n            The order length,operation is specified in the\n            SAM format. It is different from the order of\n            the :attr:`cigar` property.\n\n        Returns None if not present.\n\n        To uns [...]
+static char __pyx_k_the_term_reference_id_of_the_mat[] = "the :term:`reference` id of the mate/next read.";
+static char __pyx_k_this_class_cannot_be_instantiate[] = "this class cannot be instantiated from Python";
 static char __pyx_k_total_number_of_mapped_alignment[] = "total number of mapped alignments in file.\n        ";
 static char __pyx_k_total_number_of_reads_without_co[] = "total number of reads without coordinates\n        ";
 static char __pyx_k_total_number_of_unmapped_reads_i[] = "total number of unmapped reads in file.\n        ";
@@ -2422,13 +2577,12 @@ static char __pyx_k_true_if_read_is_mapped_in_a_prop[] = "true if read is mapped
 static char __pyx_k_true_if_read_is_mapped_to_revers[] = "true if read is mapped to reverse strand";
 static char __pyx_k_true_if_read_is_paired_in_sequen[] = "true if read is paired in sequencing";
 static char __pyx_k_true_is_read_is_mapped_to_revers[] = "true is read is mapped to reverse strand";
-static char __pyx_k_tuple_of_the_lengths_of_the_term[] = "tuple of the lengths of the :term:`reference` sequences. The lengths are in the same order as\n        :attr:`pysam.Samfile.references`\n        ";
+static char __pyx_k_tuple_of_the_lengths_of_the_term[] = "tuple of the lengths of the :term:`reference` sequences. The\n        lengths are in the same order as\n        :attr:`pysam.AlignmentFile.references`\n\n        ";
 static char __pyx_k_tuple_with_the_names_of_term_ref[] = "tuple with the names of :term:`reference` sequences.";
 static char __pyx_k_unequal_names_and_lengths_of_ref[] = "unequal names and lengths of reference sequences";
 static char __pyx_k_unknown_field_code_s_in_record_s[] = "unknown field code '%s' in record '%s'";
 static char __pyx_k_unknown_stepper_option_s_in_Iter[] = "unknown stepper option `%s` in IteratorColumn";
 static char __pyx_k_file_does_not_have_valid_header_2[] = "file does not have valid header (mode='%s') - is it SAM format?";
-static char __pyx_k_the_term_reference_id_of_the_mat_2[] = "the :term:`reference` id of the mate ";
 static PyObject *__pyx_kp_s_2scB;
 static PyObject *__pyx_kp_s_2scH;
 static PyObject *__pyx_kp_s_2scI;
@@ -2439,27 +2593,21 @@ static PyObject *__pyx_kp_s_2sccI_i_s;
 static PyObject *__pyx_kp_s_2scf;
 static PyObject *__pyx_kp_s_2sch;
 static PyObject *__pyx_kp_s_2sci;
-static PyObject *__pyx_kp_s_30s_10s_s;
 static PyObject *__pyx_n_s_A;
 static PyObject *__pyx_n_s_AS;
-static PyObject *__pyx_kp_s_A_pileup_column_A_pileup_column;
-static PyObject *__pyx_n_s_AlignedRead;
+static PyObject *__pyx_n_s_AlignedSegment;
+static PyObject *__pyx_n_s_AlignmentFile;
+static PyObject *__pyx_kp_s_AlignmentFile_mapped_only_availa;
 static PyObject *__pyx_kp_u_Argument_must_be_string_bytes_or;
 static PyObject *__pyx_kp_u_Argument_must_be_string_or_unico;
 static PyObject *__pyx_n_s_AttributeError;
 static PyObject *__pyx_n_s_B;
-static PyObject *__pyx_kp_s_Bam_index_bin_number;
-static PyObject *__pyx_kp_s_Binary_flag;
 static PyObject *__pyx_n_s_C;
 static PyObject *__pyx_n_s_CIGAR2CODE;
 static PyObject *__pyx_n_s_CIGAR_REGEX;
 static PyObject *__pyx_n_s_CL;
 static PyObject *__pyx_n_s_CN;
 static PyObject *__pyx_n_s_CO;
-static PyObject *__pyx_kp_s_Cigar_entries;
-static PyObject *__pyx_kp_s_Contig_index;
-static PyObject *__pyx_kp_s_Contig_index_for_mate_pair;
-static PyObject *__pyx_kp_s_Count_of_cigar_entries;
 static PyObject *__pyx_n_s_D;
 static PyObject *__pyx_n_s_DS;
 static PyObject *__pyx_n_s_DT;
@@ -2474,7 +2622,6 @@ static PyObject *__pyx_n_s_IOError;
 static PyObject *__pyx_n_s_IS_PYTHON3;
 static PyObject *__pyx_kp_s_I_O_operation_on_closed_file;
 static PyObject *__pyx_n_s_IndexedReads;
-static PyObject *__pyx_kp_s_Insert_size;
 static PyObject *__pyx_kp_s_Invalid_tag_s;
 static PyObject *__pyx_n_s_IteratorColumn;
 static PyObject *__pyx_n_s_IteratorRow;
@@ -2482,13 +2629,8 @@ static PyObject *__pyx_n_s_KS;
 static PyObject *__pyx_n_s_KeyError;
 static PyObject *__pyx_n_s_LB;
 static PyObject *__pyx_n_s_LN;
-static PyObject *__pyx_kp_s_Length_of_auxilary_data;
-static PyObject *__pyx_kp_s_Length_of_query_name;
-static PyObject *__pyx_kp_s_Length_of_query_sequence;
 static PyObject *__pyx_n_s_M5;
-static PyObject *__pyx_kp_s_Mapped_position_on_contig;
-static PyObject *__pyx_kp_s_Mapping_quality;
-static PyObject *__pyx_kp_s_Maximum_data_length;
+static PyObject *__pyx_n_s_MemoryError;
 static PyObject *__pyx_n_s_NotImplementedError;
 static PyObject *__pyx_n_s_OSError;
 static PyObject *__pyx_n_s_OverflowError;
@@ -2499,14 +2641,8 @@ static PyObject *__pyx_n_s_PN;
 static PyObject *__pyx_n_s_PP;
 static PyObject *__pyx_n_s_PU;
 static PyObject *__pyx_n_s_PileupColumn;
-static PyObject *__pyx_n_s_PileupColumn___str;
-static PyObject *__pyx_n_s_PileupProxy;
-static PyObject *__pyx_kp_s_PileupProxy_accessed_after_itera;
+static PyObject *__pyx_kp_s_PileupColumn_accessed_after_iter;
 static PyObject *__pyx_n_s_PileupRead;
-static PyObject *__pyx_kp_s_Position_of_mate_pair;
-static PyObject *__pyx_kp_s_Quality_scores;
-static PyObject *__pyx_kp_s_Query_name;
-static PyObject *__pyx_kp_s_Query_sequence;
 static PyObject *__pyx_n_s_RG;
 static PyObject *__pyx_n_s_S;
 static PyObject *__pyx_n_s_SM;
@@ -2515,10 +2651,7 @@ static PyObject *__pyx_n_s_SO;
 static PyObject *__pyx_n_s_SP;
 static PyObject *__pyx_n_s_SQ;
 static PyObject *__pyx_kp_s_SQ_SN_s_LN_s;
-static PyObject *__pyx_n_s_Samfile;
-static PyObject *__pyx_kp_s_Samfile_mapped_only_available_in;
 static PyObject *__pyx_n_s_StopIteration;
-static PyObject *__pyx_kp_s_This_class_cannot_be_instantiate;
 static PyObject *__pyx_n_s_TypeError;
 static PyObject *__pyx_n_s_UR;
 static PyObject *__pyx_kp_s_Unsupported_value_type_in_set_op;
@@ -2530,28 +2663,28 @@ static PyObject *__pyx_n_s_VN;
 static PyObject *__pyx_n_s_ValueError;
 static PyObject *__pyx_n_s_Z;
 static PyObject *__pyx_n_s_Zidf;
-static PyObject *__pyx_kp_s__14;
-static PyObject *__pyx_kp_s__3;
-static PyObject *__pyx_kp_s__4;
-static PyObject *__pyx_kp_s__41;
+static PyObject *__pyx_kp_s__12;
+static PyObject *__pyx_kp_s__38;
+static PyObject *__pyx_kp_b__4;
+static PyObject *__pyx_kp_s__40;
+static PyObject *__pyx_kp_s__43;
 static PyObject *__pyx_kp_s__47;
-static PyObject *__pyx_kp_b__6;
-static PyObject *__pyx_kp_s__76;
-static PyObject *__pyx_kp_s__79;
-static PyObject *__pyx_kp_s__80;
-static PyObject *__pyx_kp_s__9;
+static PyObject *__pyx_kp_s__7;
+static PyObject *__pyx_kp_s__74;
 static PyObject *__pyx_n_s_add_sq_text;
 static PyObject *__pyx_n_s_alignment;
 static PyObject *__pyx_n_s_all;
 static PyObject *__pyx_n_s_all_2;
+static PyObject *__pyx_n_s_always;
 static PyObject *__pyx_n_s_append;
 static PyObject *__pyx_n_s_args;
+static PyObject *__pyx_n_s_array;
 static PyObject *__pyx_n_s_ascii;
+static PyObject *__pyx_kp_s_at_least_one_integer_out_of_rang;
+static PyObject *__pyx_kp_s_at_least_one_signed_integer_out;
 static PyObject *__pyx_n_s_b;
 static PyObject *__pyx_kp_b_bai;
 static PyObject *__pyx_n_s_beg;
-static PyObject *__pyx_n_s_bin;
-static PyObject *__pyx_n_s_bqual;
 static PyObject *__pyx_n_s_buildLine;
 static PyObject *__pyx_n_s_c;
 static PyObject *__pyx_kp_s_c_2;
@@ -2565,7 +2698,8 @@ static PyObject *__pyx_n_s_checkIndex;
 static PyObject *__pyx_n_s_check_header;
 static PyObject *__pyx_n_s_check_sq;
 static PyObject *__pyx_n_s_chr;
-static PyObject *__pyx_n_s_cigar;
+static PyObject *__pyx_n_s_cigarstring;
+static PyObject *__pyx_n_s_cigartuples;
 static PyObject *__pyx_n_s_close;
 static PyObject *__pyx_n_s_collections;
 static PyObject *__pyx_n_s_compile;
@@ -2579,7 +2713,6 @@ static PyObject *__pyx_n_s_d;
 static PyObject *__pyx_kp_s_d_MIDNSHP_X;
 static PyObject *__pyx_n_s_decode;
 static PyObject *__pyx_n_s_defaultdict;
-static PyObject *__pyx_n_s_doc;
 static PyObject *__pyx_kp_s_either_supply_options_template_h;
 static PyObject *__pyx_n_s_encode;
 static PyObject *__pyx_n_s_end;
@@ -2605,11 +2738,15 @@ static PyObject *__pyx_kp_s_file_s_not_found;
 static PyObject *__pyx_n_s_filename;
 static PyObject *__pyx_n_s_findall;
 static PyObject *__pyx_n_s_flag;
-static PyObject *__pyx_n_s_fmts;
+static PyObject *__pyx_n_s_fromQualityString;
 static PyObject *__pyx_kp_b_ftp;
+static PyObject *__pyx_n_s_full_length;
 static PyObject *__pyx_n_s_genotype;
 static PyObject *__pyx_n_s_get;
-static PyObject *__pyx_n_s_getattribute;
+static PyObject *__pyx_n_s_get_aligned_pairs;
+static PyObject *__pyx_n_s_get_blocks;
+static PyObject *__pyx_n_s_get_overlap;
+static PyObject *__pyx_n_s_get_reference_positions;
 static PyObject *__pyx_n_s_getdefaultencoding;
 static PyObject *__pyx_n_s_getfilesystemencoding;
 static PyObject *__pyx_n_s_gettid;
@@ -2625,6 +2762,7 @@ static PyObject *__pyx_kp_s_i_c;
 static PyObject *__pyx_n_s_import;
 static PyObject *__pyx_kp_s_incomplete_sequence_information;
 static PyObject *__pyx_n_s_indel;
+static PyObject *__pyx_n_s_infer_query_length;
 static PyObject *__pyx_n_s_init;
 static PyObject *__pyx_kp_s_integer_i_out_of_range_of_BAM_SA;
 static PyObject *__pyx_kp_s_invalid_coordinates_start_i_end;
@@ -2634,45 +2772,40 @@ static PyObject *__pyx_kp_s_invalid_type_for_record_s_s_expe;
 static PyObject *__pyx_n_s_isOpen;
 static PyObject *__pyx_n_s_is_del;
 static PyObject *__pyx_n_s_is_head;
+static PyObject *__pyx_n_s_is_refskip;
+static PyObject *__pyx_n_s_is_refskip_2;
 static PyObject *__pyx_n_s_is_tail;
-static PyObject *__pyx_n_s_isize;
 static PyObject *__pyx_n_s_islower;
 static PyObject *__pyx_n_s_isupper;
 static PyObject *__pyx_n_s_items;
 static PyObject *__pyx_n_s_itertools;
 static PyObject *__pyx_n_s_join;
-static PyObject *__pyx_n_s_l_data;
-static PyObject *__pyx_n_s_l_qname;
-static PyObject *__pyx_n_s_l_qseq;
 static PyObject *__pyx_n_s_lengths;
 static PyObject *__pyx_n_s_level;
-static PyObject *__pyx_n_s_m_data;
 static PyObject *__pyx_n_s_main;
 static PyObject *__pyx_kp_s_malformatted_header_no_in_field;
 static PyObject *__pyx_n_s_map;
 static PyObject *__pyx_kp_s_mapping_information_not_recorded;
 static PyObject *__pyx_n_s_mapping_quality;
-static PyObject *__pyx_n_s_mapq;
 static PyObject *__pyx_kp_s_mate_not_found;
 static PyObject *__pyx_kp_s_mate_s_is_unmapped;
 static PyObject *__pyx_n_s_max;
 static PyObject *__pyx_n_s_max_depth;
-static PyObject *__pyx_n_s_metaclass;
 static PyObject *__pyx_n_s_min;
 static PyObject *__pyx_n_s_mode;
-static PyObject *__pyx_n_s_module;
-static PyObject *__pyx_n_s_mpos;
-static PyObject *__pyx_n_s_mrnm;
-static PyObject *__pyx_n_s_mtid;
+static PyObject *__pyx_n_s_multiple_iterators;
 static PyObject *__pyx_kp_s_multiple_s_lines_are_not_permitt;
 static PyObject *__pyx_n_s_n;
-static PyObject *__pyx_n_s_n_cigar;
+static PyObject *__pyx_n_s_next_reference_id;
+static PyObject *__pyx_n_s_next_reference_start;
 static PyObject *__pyx_n_s_nextiter;
 static PyObject *__pyx_kp_s_no_index_available_for_fetch;
 static PyObject *__pyx_kp_s_no_index_available_for_iteration;
 static PyObject *__pyx_kp_s_no_index_available_for_pileup;
+static PyObject *__pyx_n_s_nofilter;
 static PyObject *__pyx_n_s_nreferences;
-static PyObject *__pyx_n_s_object;
+static PyObject *__pyx_n_s_nsegmentes;
+static PyObject *__pyx_n_s_nsegments;
 static PyObject *__pyx_n_s_offset;
 static PyObject *__pyx_n_s_open;
 static PyObject *__pyx_n_s_ord;
@@ -2686,16 +2819,21 @@ static PyObject *__pyx_n_s_platform;
 static PyObject *__pyx_n_s_port;
 static PyObject *__pyx_n_s_pos;
 static PyObject *__pyx_n_s_positions;
-static PyObject *__pyx_n_s_prepare;
-static PyObject *__pyx_n_s_pysam_csamfile;
-static PyObject *__pyx_n_s_pytag;
+static PyObject *__pyx_n_s_pysam_calignmentfile;
 static PyObject *__pyx_n_s_pyx_vtable;
-static PyObject *__pyx_n_s_qname;
-static PyObject *__pyx_n_s_qpos;
-static PyObject *__pyx_n_s_qseq;
-static PyObject *__pyx_n_s_qual;
+static PyObject *__pyx_n_s_qualities;
 static PyObject *__pyx_kp_s_quality_and_sequence_mismatch_i;
-static PyObject *__pyx_n_s_qualname;
+static PyObject *__pyx_n_s_quality_string;
+static PyObject *__pyx_n_s_query_alignment_end;
+static PyObject *__pyx_n_s_query_alignment_length;
+static PyObject *__pyx_n_s_query_alignment_qualities;
+static PyObject *__pyx_n_s_query_alignment_sequence;
+static PyObject *__pyx_n_s_query_alignment_start;
+static PyObject *__pyx_n_s_query_length;
+static PyObject *__pyx_n_s_query_name;
+static PyObject *__pyx_n_s_query_position;
+static PyObject *__pyx_n_s_query_qualities;
+static PyObject *__pyx_n_s_query_sequence;
 static PyObject *__pyx_n_s_r;
 static PyObject *__pyx_n_s_rU;
 static PyObject *__pyx_n_s_raw;
@@ -2706,15 +2844,18 @@ static PyObject *__pyx_kp_s_read_s_not_found;
 static PyObject *__pyx_n_s_record;
 static PyObject *__pyx_n_s_reference;
 static PyObject *__pyx_n_s_reference_base;
+static PyObject *__pyx_n_s_reference_end;
+static PyObject *__pyx_n_s_reference_id;
+static PyObject *__pyx_kp_s_reference_id_i_out_of_range_0_ti;
+static PyObject *__pyx_n_s_reference_length;
+static PyObject *__pyx_n_s_reference_pos;
 static PyObject *__pyx_kp_s_reference_sequence_for_s_tid_i_n;
+static PyObject *__pyx_n_s_reference_start;
 static PyObject *__pyx_n_s_referencelengths;
 static PyObject *__pyx_n_s_referencenames;
 static PyObject *__pyx_n_s_references;
 static PyObject *__pyx_n_s_region;
-static PyObject *__pyx_n_s_reopen;
 static PyObject *__pyx_n_s_replace;
-static PyObject *__pyx_n_s_rlen;
-static PyObject *__pyx_n_s_rname;
 static PyObject *__pyx_kp_s_s;
 static PyObject *__pyx_n_s_s_2;
 static PyObject *__pyx_kp_s_s_s;
@@ -2723,9 +2864,7 @@ static PyObject *__pyx_n_s_samtools;
 static PyObject *__pyx_n_s_seek;
 static PyObject *__pyx_kp_s_seek_no_available_in_streams;
 static PyObject *__pyx_kp_s_seek_only_available_in_bam_files;
-static PyObject *__pyx_n_s_self;
 static PyObject *__pyx_n_s_send;
-static PyObject *__pyx_n_s_seq;
 static PyObject *__pyx_n_s_setTag;
 static PyObject *__pyx_n_s_snp_quality;
 static PyObject *__pyx_n_s_sorted;
@@ -2734,7 +2873,6 @@ static PyObject *__pyx_n_s_start;
 static PyObject *__pyx_kp_s_start_out_of_range_i;
 static PyObject *__pyx_n_s_startswith;
 static PyObject *__pyx_n_s_stepper;
-static PyObject *__pyx_n_s_str;
 static PyObject *__pyx_n_s_strip;
 static PyObject *__pyx_n_s_struct;
 static PyObject *__pyx_n_s_sys;
@@ -2743,13 +2881,17 @@ static PyObject *__pyx_kp_s_tag_s_not_present;
 static PyObject *__pyx_n_s_tags;
 static PyObject *__pyx_n_s_tempfile;
 static PyObject *__pyx_n_s_template;
+static PyObject *__pyx_n_s_template_length;
 static PyObject *__pyx_n_s_test;
 static PyObject *__pyx_n_s_text;
+static PyObject *__pyx_kp_s_this_class_cannot_be_instantiate;
 static PyObject *__pyx_n_s_throw;
 static PyObject *__pyx_n_s_tid;
 static PyObject *__pyx_kp_s_tid_i_out_of_range_0_tid_i;
+static PyObject *__pyx_n_s_toQualityString;
 static PyObject *__pyx_n_s_traceback;
 static PyObject *__pyx_n_s_truncate;
+static PyObject *__pyx_kp_s_truncated_file;
 static PyObject *__pyx_n_s_types;
 static PyObject *__pyx_kp_s_unable_to_open_index_for_s;
 static PyObject *__pyx_kp_s_unequal_names_and_lengths_of_ref;
@@ -2760,46 +2902,55 @@ static PyObject *__pyx_kp_s_unknown_type_s;
 static PyObject *__pyx_n_s_until_eof;
 static PyObject *__pyx_n_s_value;
 static PyObject *__pyx_n_s_value_type;
-static PyObject *__pyx_n_s_version_info;
 static PyObject *__pyx_n_s_w;
 static PyObject *__pyx_n_s_warn;
 static PyObject *__pyx_n_s_warnings;
 static PyObject *__pyx_n_s_wb;
+static PyObject *__pyx_n_s_wb0;
 static PyObject *__pyx_n_s_wbu;
 static PyObject *__pyx_n_s_wh;
 static PyObject *__pyx_n_s_where;
 static PyObject *__pyx_n_s_write;
+static PyObject *__pyx_n_s_x;
 static PyObject *__pyx_n_s_zip;
 static PyObject *__pyx_int_0;
 static PyObject *__pyx_int_1;
 static PyObject *__pyx_int_2;
-static PyObject *__pyx_int_3;
+static PyObject *__pyx_int_33;
+static PyObject *__pyx_int_128;
 static PyObject *__pyx_int_255;
+static PyObject *__pyx_int_256;
 static PyObject *__pyx_int_8000;
+static PyObject *__pyx_int_32768;
 static PyObject *__pyx_int_65535;
+static PyObject *__pyx_int_65536;
 static PyObject *__pyx_int_536870912;
+static PyObject *__pyx_int_2147483648;
 static PyObject *__pyx_int_4294967295;
+static PyObject *__pyx_int_4294967296;
 static PyObject *__pyx_int_neg_4;
 static PyObject *__pyx_int_neg_127;
+static PyObject *__pyx_int_neg_128;
 static PyObject *__pyx_int_neg_32767;
+static PyObject *__pyx_int_neg_32768;
 static PyObject *__pyx_int_neg_2147483648;
-static int __pyx_k__57;
+static int __pyx_k__62;
 static PyObject *__pyx_tuple_;
+static PyObject *__pyx_slice__8;
 static PyObject *__pyx_tuple__2;
+static PyObject *__pyx_tuple__3;
 static PyObject *__pyx_tuple__5;
-static PyObject *__pyx_tuple__7;
-static PyObject *__pyx_tuple__8;
-static PyObject *__pyx_slice__10;
-static PyObject *__pyx_slice__43;
+static PyObject *__pyx_tuple__6;
+static PyObject *__pyx_tuple__9;
+static PyObject *__pyx_slice__42;
 static PyObject *__pyx_slice__45;
 static PyObject *__pyx_slice__46;
-static PyObject *__pyx_slice__61;
-static PyObject *__pyx_slice__65;
-static PyObject *__pyx_slice__70;
-static PyObject *__pyx_slice__74;
+static PyObject *__pyx_slice__68;
+static PyObject *__pyx_slice__72;
+static PyObject *__pyx_tuple__10;
 static PyObject *__pyx_tuple__11;
-static PyObject *__pyx_tuple__12;
 static PyObject *__pyx_tuple__13;
+static PyObject *__pyx_tuple__14;
 static PyObject *__pyx_tuple__15;
 static PyObject *__pyx_tuple__16;
 static PyObject *__pyx_tuple__17;
@@ -2823,10 +2974,8 @@ static PyObject *__pyx_tuple__34;
 static PyObject *__pyx_tuple__35;
 static PyObject *__pyx_tuple__36;
 static PyObject *__pyx_tuple__37;
-static PyObject *__pyx_tuple__38;
 static PyObject *__pyx_tuple__39;
-static PyObject *__pyx_tuple__40;
-static PyObject *__pyx_tuple__42;
+static PyObject *__pyx_tuple__41;
 static PyObject *__pyx_tuple__44;
 static PyObject *__pyx_tuple__48;
 static PyObject *__pyx_tuple__49;
@@ -2837,36 +2986,38 @@ static PyObject *__pyx_tuple__53;
 static PyObject *__pyx_tuple__54;
 static PyObject *__pyx_tuple__55;
 static PyObject *__pyx_tuple__56;
+static PyObject *__pyx_tuple__57;
 static PyObject *__pyx_tuple__58;
 static PyObject *__pyx_tuple__59;
 static PyObject *__pyx_tuple__60;
-static PyObject *__pyx_tuple__62;
+static PyObject *__pyx_tuple__61;
 static PyObject *__pyx_tuple__63;
 static PyObject *__pyx_tuple__64;
+static PyObject *__pyx_tuple__65;
 static PyObject *__pyx_tuple__66;
 static PyObject *__pyx_tuple__67;
-static PyObject *__pyx_tuple__68;
 static PyObject *__pyx_tuple__69;
+static PyObject *__pyx_tuple__70;
 static PyObject *__pyx_tuple__71;
-static PyObject *__pyx_tuple__72;
 static PyObject *__pyx_tuple__73;
 static PyObject *__pyx_tuple__75;
+static PyObject *__pyx_tuple__76;
 static PyObject *__pyx_tuple__77;
 static PyObject *__pyx_tuple__78;
+static PyObject *__pyx_tuple__79;
+static PyObject *__pyx_tuple__80;
 static PyObject *__pyx_tuple__81;
 static PyObject *__pyx_tuple__82;
 static PyObject *__pyx_tuple__83;
 static PyObject *__pyx_tuple__84;
 static PyObject *__pyx_tuple__85;
-static PyObject *__pyx_tuple__87;
+static PyObject *__pyx_tuple__86;
 static PyObject *__pyx_tuple__88;
-static PyObject *__pyx_tuple__89;
-static PyObject *__pyx_tuple__90;
-static PyObject *__pyx_tuple__91;
-static PyObject *__pyx_codeobj__86;
-static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
+static PyObject *__pyx_codeobj__87;
+static PyObject *__pyx_codeobj__89;
+static PyObject *__pyx_gb_5pysam_14calignmentfile_6generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
 
-/* "pysam/csamfile.pyx":105
+/* "pysam/calignmentfile.pyx":110
  * cdef char* CODE2CIGAR= "MIDNSHP=X"
  * if IS_PYTHON3:
  *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
@@ -2874,8 +3025,8 @@ static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__p
  *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
  */
 
-static PyObject *__pyx_pf_5pysam_8csamfile_genexpr(CYTHON_UNUSED PyObject *__pyx_self) {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *__pyx_cur_scope;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_4genexpr(CYTHON_UNUSED PyObject *__pyx_self) {
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *__pyx_cur_scope;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_lineno = 0;
@@ -2883,15 +3034,15 @@ static PyObject *__pyx_pf_5pysam_8csamfile_genexpr(CYTHON_UNUSED PyObject *__pyx
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("genexpr", 0);
-  __pyx_cur_scope = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct__genexpr(__pyx_ptype_5pysam_8csamfile___pyx_scope_struct__genexpr, __pyx_empty_tuple, NULL);
+  __pyx_cur_scope = (struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *)__pyx_tp_new_5pysam_14calignmentfile___pyx_scope_struct__genexpr(__pyx_ptype_5pysam_14calignmentfile___pyx_scope_struct__genexpr, __pyx_empty_tuple, NULL);
   if (unlikely(!__pyx_cur_scope)) {
     __Pyx_RefNannyFinishContext();
     return NULL;
   }
   __Pyx_GOTREF(__pyx_cur_scope);
-  __Pyx_TraceCall("genexpr", __pyx_f[0], 105);
+  __Pyx_TraceCall("genexpr", __pyx_f[0], 110);
   {
-    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_8csamfile_2generator, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_14calignmentfile_6generator, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_cur_scope);
     __Pyx_RefNannyFinishContext();
     return (PyObject *) gen;
@@ -2901,7 +3052,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_genexpr(CYTHON_UNUSED PyObject *__pyx
   __pyx_r = Py_None; __Pyx_INCREF(Py_None);
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_DECREF(((PyObject *)__pyx_cur_scope));
@@ -2911,9 +3062,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_genexpr(CYTHON_UNUSED PyObject *__pyx
   return __pyx_r;
 }
 
-static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
+static PyObject *__pyx_gb_5pysam_14calignmentfile_6generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
 {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *__pyx_cur_scope = ((struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)__pyx_generator->closure);
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *__pyx_cur_scope = ((struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *)__pyx_generator->closure);
   PyObject *__pyx_r = NULL;
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
@@ -2932,12 +3083,12 @@ static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__p
     return NULL;
   }
   __pyx_L3_first_run:;
-  if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_INCREF(__pyx_int_0);
   __pyx_t_1 = __pyx_int_0;
-  __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_5pysam_8csamfile_CODE2CIGAR); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_5pysam_14calignmentfile_CODE2CIGAR); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __pyx_t_4 = Py_TYPE(__pyx_t_3)->tp_iternext;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -2948,7 +3099,7 @@ static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__p
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -2962,12 +3113,12 @@ static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__p
     __Pyx_XGOTREF(__pyx_cur_scope->__pyx_v_x);
     __Pyx_XDECREF_SET(__pyx_cur_scope->__pyx_v_x, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1);
     __pyx_t_1 = __pyx_t_2;
     __pyx_t_2 = 0;
-    __pyx_t_2 = PyList_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_cur_scope->__pyx_v_y);
     PyList_SET_ITEM(__pyx_t_2, 0, __pyx_cur_scope->__pyx_v_y);
@@ -2995,7 +3146,7 @@ static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__p
     __pyx_cur_scope->__pyx_t_1 = 0;
     __Pyx_XGOTREF(__pyx_t_3);
     __pyx_t_4 = __pyx_cur_scope->__pyx_t_2;
-    if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -3015,9 +3166,9 @@ static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__p
   __Pyx_RefNannyFinishContext();
   return NULL;
 }
-static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
+static PyObject *__pyx_gb_5pysam_14calignmentfile_9generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
 
-/* "pysam/csamfile.pyx":107
+/* "pysam/calignmentfile.pyx":112
  *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
  * else:
  *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
@@ -3025,8 +3176,8 @@ static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__
  * 
  */
 
-static PyObject *__pyx_pf_5pysam_8csamfile_3genexpr(CYTHON_UNUSED PyObject *__pyx_self) {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *__pyx_cur_scope;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7genexpr(CYTHON_UNUSED PyObject *__pyx_self) {
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *__pyx_cur_scope;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_lineno = 0;
@@ -3034,15 +3185,15 @@ static PyObject *__pyx_pf_5pysam_8csamfile_3genexpr(CYTHON_UNUSED PyObject *__py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("genexpr", 0);
-  __pyx_cur_scope = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct_1_genexpr(__pyx_ptype_5pysam_8csamfile___pyx_scope_struct_1_genexpr, __pyx_empty_tuple, NULL);
+  __pyx_cur_scope = (struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *)__pyx_tp_new_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr(__pyx_ptype_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr, __pyx_empty_tuple, NULL);
   if (unlikely(!__pyx_cur_scope)) {
     __Pyx_RefNannyFinishContext();
     return NULL;
   }
   __Pyx_GOTREF(__pyx_cur_scope);
-  __Pyx_TraceCall("genexpr", __pyx_f[0], 107);
+  __Pyx_TraceCall("genexpr", __pyx_f[0], 112);
   {
-    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_8csamfile_5generator1, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_14calignmentfile_9generator1, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_cur_scope);
     __Pyx_RefNannyFinishContext();
     return (PyObject *) gen;
@@ -3052,7 +3203,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_3genexpr(CYTHON_UNUSED PyObject *__py
   __pyx_r = Py_None; __Pyx_INCREF(Py_None);
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_DECREF(((PyObject *)__pyx_cur_scope));
@@ -3062,9 +3213,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_3genexpr(CYTHON_UNUSED PyObject *__py
   return __pyx_r;
 }
 
-static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
+static PyObject *__pyx_gb_5pysam_14calignmentfile_9generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
 {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *__pyx_cur_scope = ((struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)__pyx_generator->closure);
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *__pyx_cur_scope = ((struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *)__pyx_generator->closure);
   PyObject *__pyx_r = NULL;
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
@@ -3084,12 +3235,12 @@ static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__
     return NULL;
   }
   __pyx_L3_first_run:;
-  if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_INCREF(__pyx_int_0);
   __pyx_t_1 = __pyx_int_0;
-  __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_5pysam_8csamfile_CODE2CIGAR); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_5pysam_14calignmentfile_CODE2CIGAR); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __pyx_t_4 = Py_TYPE(__pyx_t_3)->tp_iternext;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -3100,7 +3251,7 @@ static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -3114,20 +3265,20 @@ static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__
     __Pyx_XGOTREF(__pyx_cur_scope->__pyx_v_x);
     __Pyx_XDECREF_SET(__pyx_cur_scope->__pyx_v_x, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1);
     __pyx_t_1 = __pyx_t_2;
     __pyx_t_2 = 0;
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_cur_scope->__pyx_v_y);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_cur_scope->__pyx_v_y);
     __Pyx_GIVEREF(__pyx_cur_scope->__pyx_v_y);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = PyList_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyList_SET_ITEM(__pyx_t_2, 0, __pyx_t_5);
     __Pyx_GIVEREF(__pyx_t_5);
@@ -3155,7 +3306,7 @@ static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__
     __pyx_cur_scope->__pyx_t_1 = 0;
     __Pyx_XGOTREF(__pyx_t_3);
     __pyx_t_4 = __pyx_cur_scope->__pyx_t_2;
-    if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -3177,7 +3328,7 @@ static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__
   return NULL;
 }
 
-/* "pysam/csamfile.pyx":28
+/* "pysam/calignmentfile.pyx":32
  * ########################################################################
  * IS_PYTHON3 = PY_MAJOR_VERSION >= 3
  * cdef from_string_and_size(char* s, size_t length):             # <<<<<<<<<<<<<<
@@ -3185,7 +3336,7 @@ static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__
  *         return s[:length]
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s, size_t __pyx_v_length) {
+static PyObject *__pyx_f_5pysam_14calignmentfile_from_string_and_size(char *__pyx_v_s, size_t __pyx_v_length) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -3195,9 +3346,9 @@ static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s,
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("from_string_and_size", 0);
-  __Pyx_TraceCall("from_string_and_size", __pyx_f[0], 28);
+  __Pyx_TraceCall("from_string_and_size", __pyx_f[0], 32);
 
-  /* "pysam/csamfile.pyx":29
+  /* "pysam/calignmentfile.pyx":33
  * IS_PYTHON3 = PY_MAJOR_VERSION >= 3
  * cdef from_string_and_size(char* s, size_t length):
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
@@ -3207,7 +3358,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s,
   __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":30
+    /* "pysam/calignmentfile.pyx":34
  * cdef from_string_and_size(char* s, size_t length):
  *     if PY_MAJOR_VERSION < 3:
  *         return s[:length]             # <<<<<<<<<<<<<<
@@ -3215,7 +3366,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s,
  *         return s[:length].decode("ascii")
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromStringAndSize(__pyx_v_s + 0, __pyx_v_length - 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromStringAndSize(__pyx_v_s + 0, __pyx_v_length - 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -3223,7 +3374,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s,
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":32
+    /* "pysam/calignmentfile.pyx":36
  *         return s[:length]
  *     else:
  *         return s[:length].decode("ascii")             # <<<<<<<<<<<<<<
@@ -3231,14 +3382,14 @@ static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s,
  * # filename encoding (copied from lxml.etree.pyx)
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, __pyx_v_length, NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, __pyx_v_length, NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":28
+  /* "pysam/calignmentfile.pyx":32
  * ########################################################################
  * IS_PYTHON3 = PY_MAJOR_VERSION >= 3
  * cdef from_string_and_size(char* s, size_t length):             # <<<<<<<<<<<<<<
@@ -3249,7 +3400,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s,
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.from_string_and_size", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.from_string_and_size", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -3258,7 +3409,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s,
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":45
+/* "pysam/calignmentfile.pyx":49
  * #_C_FILENAME_ENCODING = <char*>_FILENAME_ENCODING
  * 
  * cdef bytes _encodeFilename(object filename):             # <<<<<<<<<<<<<<
@@ -3266,7 +3417,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s,
  *     if filename is None:
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_filename) {
+static PyObject *__pyx_f_5pysam_14calignmentfile__encodeFilename(PyObject *__pyx_v_filename) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -3279,9 +3430,9 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_encodeFilename", 0);
-  __Pyx_TraceCall("_encodeFilename", __pyx_f[0], 45);
+  __Pyx_TraceCall("_encodeFilename", __pyx_f[0], 49);
 
-  /* "pysam/csamfile.pyx":47
+  /* "pysam/calignmentfile.pyx":51
  * cdef bytes _encodeFilename(object filename):
  *     """Make sure a filename is 8-bit encoded (or None)."""
  *     if filename is None:             # <<<<<<<<<<<<<<
@@ -3292,7 +3443,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":48
+    /* "pysam/calignmentfile.pyx":52
  *     """Make sure a filename is 8-bit encoded (or None)."""
  *     if filename is None:
  *         return None             # <<<<<<<<<<<<<<
@@ -3305,7 +3456,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":49
+  /* "pysam/calignmentfile.pyx":53
  *     if filename is None:
  *         return None
  *     elif PyBytes_Check(filename):             # <<<<<<<<<<<<<<
@@ -3315,7 +3466,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
   __pyx_t_2 = (PyBytes_Check(__pyx_v_filename) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":50
+    /* "pysam/calignmentfile.pyx":54
  *         return None
  *     elif PyBytes_Check(filename):
  *         return filename             # <<<<<<<<<<<<<<
@@ -3323,13 +3474,13 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
  *         return filename.encode(_FILENAME_ENCODING)
  */
     __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_filename))||((__pyx_v_filename) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_filename)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_v_filename))||((__pyx_v_filename) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_filename)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_INCREF(__pyx_v_filename);
     __pyx_r = ((PyObject*)__pyx_v_filename);
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":51
+  /* "pysam/calignmentfile.pyx":55
  *     elif PyBytes_Check(filename):
  *         return filename
  *     elif PyUnicode_Check(filename):             # <<<<<<<<<<<<<<
@@ -3339,7 +3490,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
   __pyx_t_2 = (PyUnicode_Check(__pyx_v_filename) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":52
+    /* "pysam/calignmentfile.pyx":56
  *         return filename
  *     elif PyUnicode_Check(filename):
  *         return filename.encode(_FILENAME_ENCODING)             # <<<<<<<<<<<<<<
@@ -3347,25 +3498,25 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
  *         raise TypeError, u"Argument must be string or unicode."
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_INCREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    __Pyx_GIVEREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_INCREF(__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING);
+    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING);
+    __Pyx_GIVEREF(__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING);
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    if (!(likely(PyBytes_CheckExact(__pyx_t_5))||((__pyx_t_5) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_5)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_t_5))||((__pyx_t_5) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_5)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_r = ((PyObject*)__pyx_t_5);
     __pyx_t_5 = 0;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":54
+    /* "pysam/calignmentfile.pyx":58
  *         return filename.encode(_FILENAME_ENCODING)
  *     else:
  *         raise TypeError, u"Argument must be string or unicode."             # <<<<<<<<<<<<<<
@@ -3373,10 +3524,10 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
  * cdef bytes _forceBytes(object s):
  */
     __Pyx_Raise(__pyx_builtin_TypeError, __pyx_kp_u_Argument_must_be_string_or_unico, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":45
+  /* "pysam/calignmentfile.pyx":49
  * #_C_FILENAME_ENCODING = <char*>_FILENAME_ENCODING
  * 
  * cdef bytes _encodeFilename(object filename):             # <<<<<<<<<<<<<<
@@ -3389,7 +3540,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
   __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile._encodeFilename", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile._encodeFilename", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -3398,15 +3549,15 @@ static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_file
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":56
+/* "pysam/calignmentfile.pyx":60
  *         raise TypeError, u"Argument must be string or unicode."
  * 
  * cdef bytes _forceBytes(object s):             # <<<<<<<<<<<<<<
- *     u"""convert string or unicode object to bytes, assuming ascii encoding.
- *     """
+ *     u"""convert string or unicode object to bytes, assuming
+ *     ascii encoding.
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
+static PyObject *__pyx_f_5pysam_14calignmentfile__forceBytes(PyObject *__pyx_v_s) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -3418,10 +3569,10 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_forceBytes", 0);
-  __Pyx_TraceCall("_forceBytes", __pyx_f[0], 56);
+  __Pyx_TraceCall("_forceBytes", __pyx_f[0], 60);
 
-  /* "pysam/csamfile.pyx":59
- *     u"""convert string or unicode object to bytes, assuming ascii encoding.
+  /* "pysam/calignmentfile.pyx":64
+ *     ascii encoding.
  *     """
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
  *         return s
@@ -3430,7 +3581,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
   __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":60
+    /* "pysam/calignmentfile.pyx":65
  *     """
  *     if PY_MAJOR_VERSION < 3:
  *         return s             # <<<<<<<<<<<<<<
@@ -3438,13 +3589,13 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
  *         return None
  */
     __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_INCREF(__pyx_v_s);
     __pyx_r = ((PyObject*)__pyx_v_s);
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":61
+  /* "pysam/calignmentfile.pyx":66
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  *     elif s is None:             # <<<<<<<<<<<<<<
@@ -3455,7 +3606,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":62
+    /* "pysam/calignmentfile.pyx":67
  *         return s
  *     elif s is None:
  *         return None             # <<<<<<<<<<<<<<
@@ -3468,7 +3619,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":63
+  /* "pysam/calignmentfile.pyx":68
  *     elif s is None:
  *         return None
  *     elif PyBytes_Check(s):             # <<<<<<<<<<<<<<
@@ -3478,7 +3629,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
   __pyx_t_2 = (PyBytes_Check(__pyx_v_s) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":64
+    /* "pysam/calignmentfile.pyx":69
  *         return None
  *     elif PyBytes_Check(s):
  *         return s             # <<<<<<<<<<<<<<
@@ -3486,13 +3637,13 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
  *         return s.encode('ascii')
  */
     __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_INCREF(__pyx_v_s);
     __pyx_r = ((PyObject*)__pyx_v_s);
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":65
+  /* "pysam/calignmentfile.pyx":70
  *     elif PyBytes_Check(s):
  *         return s
  *     elif PyUnicode_Check(s):             # <<<<<<<<<<<<<<
@@ -3502,7 +3653,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
   __pyx_t_2 = (PyUnicode_Check(__pyx_v_s) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":66
+    /* "pysam/calignmentfile.pyx":71
  *         return s
  *     elif PyUnicode_Check(s):
  *         return s.encode('ascii')             # <<<<<<<<<<<<<<
@@ -3510,19 +3661,19 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
  *         raise TypeError, u"Argument must be string, bytes or unicode."
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple_, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple_, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    if (!(likely(PyBytes_CheckExact(__pyx_t_4))||((__pyx_t_4) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_4)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_t_4))||((__pyx_t_4) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_4)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_r = ((PyObject*)__pyx_t_4);
     __pyx_t_4 = 0;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":68
+    /* "pysam/calignmentfile.pyx":73
  *         return s.encode('ascii')
  *     else:
  *         raise TypeError, u"Argument must be string, bytes or unicode."             # <<<<<<<<<<<<<<
@@ -3530,22 +3681,22 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
  * cdef inline bytes _forceCmdlineBytes(object s):
  */
     __Pyx_Raise(__pyx_builtin_TypeError, __pyx_kp_u_Argument_must_be_string_bytes_or, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 68; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":56
+  /* "pysam/calignmentfile.pyx":60
  *         raise TypeError, u"Argument must be string or unicode."
  * 
  * cdef bytes _forceBytes(object s):             # <<<<<<<<<<<<<<
- *     u"""convert string or unicode object to bytes, assuming ascii encoding.
- *     """
+ *     u"""convert string or unicode object to bytes, assuming
+ *     ascii encoding.
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile._forceBytes", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile._forceBytes", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -3554,7 +3705,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":70
+/* "pysam/calignmentfile.pyx":75
  *         raise TypeError, u"Argument must be string, bytes or unicode."
  * 
  * cdef inline bytes _forceCmdlineBytes(object s):             # <<<<<<<<<<<<<<
@@ -3562,7 +3713,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
  * 
  */
 
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile__forceCmdlineBytes(PyObject *__pyx_v_s) {
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_14calignmentfile__forceCmdlineBytes(PyObject *__pyx_v_s) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -3571,9 +3722,9 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile__forceCmdlineBytes(PyObj
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_forceCmdlineBytes", 0);
-  __Pyx_TraceCall("_forceCmdlineBytes", __pyx_f[0], 70);
+  __Pyx_TraceCall("_forceCmdlineBytes", __pyx_f[0], 75);
 
-  /* "pysam/csamfile.pyx":71
+  /* "pysam/calignmentfile.pyx":76
  * 
  * cdef inline bytes _forceCmdlineBytes(object s):
  *     return _forceBytes(s)             # <<<<<<<<<<<<<<
@@ -3581,13 +3732,13 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile__forceCmdlineBytes(PyObj
  * cdef _charptr_to_str(char* s):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_s); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_s); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 76; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":70
+  /* "pysam/calignmentfile.pyx":75
  *         raise TypeError, u"Argument must be string, bytes or unicode."
  * 
  * cdef inline bytes _forceCmdlineBytes(object s):             # <<<<<<<<<<<<<<
@@ -3598,7 +3749,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile__forceCmdlineBytes(PyObj
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile._forceCmdlineBytes", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile._forceCmdlineBytes", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -3607,7 +3758,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile__forceCmdlineBytes(PyObj
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":73
+/* "pysam/calignmentfile.pyx":78
  *     return _forceBytes(s)
  * 
  * cdef _charptr_to_str(char* s):             # <<<<<<<<<<<<<<
@@ -3615,7 +3766,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile__forceCmdlineBytes(PyObj
  *         return s
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
+static PyObject *__pyx_f_5pysam_14calignmentfile__charptr_to_str(char *__pyx_v_s) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -3625,9 +3776,9 @@ static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_charptr_to_str", 0);
-  __Pyx_TraceCall("_charptr_to_str", __pyx_f[0], 73);
+  __Pyx_TraceCall("_charptr_to_str", __pyx_f[0], 78);
 
-  /* "pysam/csamfile.pyx":74
+  /* "pysam/calignmentfile.pyx":79
  * 
  * cdef _charptr_to_str(char* s):
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
@@ -3637,7 +3788,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
   __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":75
+    /* "pysam/calignmentfile.pyx":80
  * cdef _charptr_to_str(char* s):
  *     if PY_MAJOR_VERSION < 3:
  *         return s             # <<<<<<<<<<<<<<
@@ -3645,7 +3796,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
  *         return s.decode("ascii")
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 75; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -3653,7 +3804,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":77
+    /* "pysam/calignmentfile.pyx":82
  *         return s
  *     else:
  *         return s.decode("ascii")             # <<<<<<<<<<<<<<
@@ -3661,14 +3812,14 @@ static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
  * cdef _forceStr(object s):
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, strlen(__pyx_v_s), NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, strlen(__pyx_v_s), NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":73
+  /* "pysam/calignmentfile.pyx":78
  *     return _forceBytes(s)
  * 
  * cdef _charptr_to_str(char* s):             # <<<<<<<<<<<<<<
@@ -3679,7 +3830,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile._charptr_to_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile._charptr_to_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -3688,7 +3839,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":79
+/* "pysam/calignmentfile.pyx":84
  *         return s.decode("ascii")
  * 
  * cdef _forceStr(object s):             # <<<<<<<<<<<<<<
@@ -3696,7 +3847,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
  *     (bytes in Py2, unicode in Py3)"""
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
+static PyObject *__pyx_f_5pysam_14calignmentfile__forceStr(PyObject *__pyx_v_s) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -3708,9 +3859,9 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_forceStr", 0);
-  __Pyx_TraceCall("_forceStr", __pyx_f[0], 79);
+  __Pyx_TraceCall("_forceStr", __pyx_f[0], 84);
 
-  /* "pysam/csamfile.pyx":82
+  /* "pysam/calignmentfile.pyx":87
  *     """Return s converted to str type of current Python
  *     (bytes in Py2, unicode in Py3)"""
  *     if s is None:             # <<<<<<<<<<<<<<
@@ -3721,7 +3872,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":83
+    /* "pysam/calignmentfile.pyx":88
  *     (bytes in Py2, unicode in Py3)"""
  *     if s is None:
  *         return None             # <<<<<<<<<<<<<<
@@ -3734,7 +3885,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":84
+  /* "pysam/calignmentfile.pyx":89
  *     if s is None:
  *         return None
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
@@ -3744,7 +3895,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
   __pyx_t_2 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":85
+    /* "pysam/calignmentfile.pyx":90
  *         return None
  *     if PY_MAJOR_VERSION < 3:
  *         return s             # <<<<<<<<<<<<<<
@@ -3757,7 +3908,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":86
+  /* "pysam/calignmentfile.pyx":91
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  *     elif PyBytes_Check(s):             # <<<<<<<<<<<<<<
@@ -3767,7 +3918,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
   __pyx_t_2 = (PyBytes_Check(__pyx_v_s) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":87
+    /* "pysam/calignmentfile.pyx":92
  *         return s
  *     elif PyBytes_Check(s):
  *         return s.decode('ascii')             # <<<<<<<<<<<<<<
@@ -3775,9 +3926,9 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
  *         # assume unicode
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_decode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_decode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __pyx_r = __pyx_t_4;
@@ -3786,7 +3937,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":90
+    /* "pysam/calignmentfile.pyx":95
  *     else:
  *         # assume unicode
  *         return s             # <<<<<<<<<<<<<<
@@ -3799,7 +3950,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":79
+  /* "pysam/calignmentfile.pyx":84
  *         return s.decode("ascii")
  * 
  * cdef _forceStr(object s):             # <<<<<<<<<<<<<<
@@ -3811,7 +3962,7 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile._forceStr", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile._forceStr", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -3820,16 +3971,16 @@ static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":120
+/* "pysam/calignmentfile.pyx":125
  * #####################################################################
- * cdef class AlignedRead
- * cdef makeAlignedRead(bam1_t * src):             # <<<<<<<<<<<<<<
- *     '''enter src into AlignedRead.'''
+ * cdef class AlignedSegment
+ * cdef object makeAlignedSegment(bam1_t * src):             # <<<<<<<<<<<<<<
+ *     '''enter src into AlignedSegment.'''
  *     # note that the following does not call __init__
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile_makeAlignedRead(bam1_t *__pyx_v_src) {
-  struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_dest = 0;
+static PyObject *__pyx_f_5pysam_14calignmentfile_makeAlignedSegment(bam1_t *__pyx_v_src) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_dest = 0;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -3837,55 +3988,55 @@ static PyObject *__pyx_f_5pysam_8csamfile_makeAlignedRead(bam1_t *__pyx_v_src) {
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("makeAlignedRead", 0);
-  __Pyx_TraceCall("makeAlignedRead", __pyx_f[0], 120);
+  __Pyx_RefNannySetupContext("makeAlignedSegment", 0);
+  __Pyx_TraceCall("makeAlignedSegment", __pyx_f[0], 125);
 
-  /* "pysam/csamfile.pyx":123
- *     '''enter src into AlignedRead.'''
+  /* "pysam/calignmentfile.pyx":128
+ *     '''enter src into AlignedSegment.'''
  *     # note that the following does not call __init__
- *     cdef AlignedRead dest = AlignedRead.__new__(AlignedRead)             # <<<<<<<<<<<<<<
+ *     cdef AlignedSegment dest = AlignedSegment.__new__(AlignedSegment)             # <<<<<<<<<<<<<<
  *     dest._delegate = bam_dup1(src)
  *     return dest
  */
-  __pyx_t_1 = __pyx_tp_new_5pysam_8csamfile_AlignedRead(((PyTypeObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_AlignedRead)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_tp_new_5pysam_14calignmentfile_AlignedSegment(((PyTypeObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_AlignedSegment)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_8csamfile_AlignedRead)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_dest = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_t_1);
+  if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_14calignmentfile_AlignedSegment)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_dest = ((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":124
+  /* "pysam/calignmentfile.pyx":129
  *     # note that the following does not call __init__
- *     cdef AlignedRead dest = AlignedRead.__new__(AlignedRead)
+ *     cdef AlignedSegment dest = AlignedSegment.__new__(AlignedSegment)
  *     dest._delegate = bam_dup1(src)             # <<<<<<<<<<<<<<
  *     return dest
  * 
  */
   __pyx_v_dest->_delegate = bam_dup1(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":125
- *     cdef AlignedRead dest = AlignedRead.__new__(AlignedRead)
+  /* "pysam/calignmentfile.pyx":130
+ *     cdef AlignedSegment dest = AlignedSegment.__new__(AlignedSegment)
  *     dest._delegate = bam_dup1(src)
  *     return dest             # <<<<<<<<<<<<<<
  * 
- * cdef class PileupProxy
+ * 
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(((PyObject *)__pyx_v_dest));
   __pyx_r = ((PyObject *)__pyx_v_dest);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":120
+  /* "pysam/calignmentfile.pyx":125
  * #####################################################################
- * cdef class AlignedRead
- * cdef makeAlignedRead(bam1_t * src):             # <<<<<<<<<<<<<<
- *     '''enter src into AlignedRead.'''
+ * cdef class AlignedSegment
+ * cdef object makeAlignedSegment(bam1_t * src):             # <<<<<<<<<<<<<<
+ *     '''enter src into AlignedSegment.'''
  *     # note that the following does not call __init__
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.makeAlignedRead", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.makeAlignedSegment", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XDECREF((PyObject *)__pyx_v_dest);
@@ -3895,16 +4046,16 @@ static PyObject *__pyx_f_5pysam_8csamfile_makeAlignedRead(bam1_t *__pyx_v_src) {
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":128
+/* "pysam/calignmentfile.pyx":134
  * 
- * cdef class PileupProxy
- * cdef makePileupProxy(bam_pileup1_t ** plp, int tid, int pos, int n):             # <<<<<<<<<<<<<<
+ * cdef class PileupColumn
+ * cdef makePileupColumn(bam_pileup1_t ** plp, int tid, int pos, int n_pu):             # <<<<<<<<<<<<<<
  *     # note that the following does not call __init__
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
+ *      cdef PileupColumn dest = PileupColumn.__new__(PileupColumn)
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile_makePileupProxy(bam_pileup1_t **__pyx_v_plp, int __pyx_v_tid, int __pyx_v_pos, int __pyx_v_n) {
-  struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_dest = 0;
+static PyObject *__pyx_f_5pysam_14calignmentfile_makePileupColumn(bam_pileup1_t **__pyx_v_plp, int __pyx_v_tid, int __pyx_v_pos, int __pyx_v_n_pu) {
+  struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_dest = 0;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -3912,64 +4063,61 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupProxy(bam_pileup1_t **__pyx_
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("makePileupProxy", 0);
-  __Pyx_TraceCall("makePileupProxy", __pyx_f[0], 128);
+  __Pyx_RefNannySetupContext("makePileupColumn", 0);
+  __Pyx_TraceCall("makePileupColumn", __pyx_f[0], 134);
 
-  /* "pysam/csamfile.pyx":130
- * cdef makePileupProxy(bam_pileup1_t ** plp, int tid, int pos, int n):
+  /* "pysam/calignmentfile.pyx":136
+ * cdef makePileupColumn(bam_pileup1_t ** plp, int tid, int pos, int n_pu):
  *     # note that the following does not call __init__
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)             # <<<<<<<<<<<<<<
+ *      cdef PileupColumn dest = PileupColumn.__new__(PileupColumn)             # <<<<<<<<<<<<<<
  *      dest.plp = plp
  *      dest.tid = tid
  */
-  __pyx_t_1 = __pyx_tp_new_5pysam_8csamfile_PileupProxy(((PyTypeObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_PileupProxy)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_tp_new_5pysam_14calignmentfile_PileupColumn(((PyTypeObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_PileupColumn)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_8csamfile_PileupProxy)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_dest = ((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_t_1);
+  if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_14calignmentfile_PileupColumn)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_dest = ((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":131
+  /* "pysam/calignmentfile.pyx":137
  *     # note that the following does not call __init__
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
+ *      cdef PileupColumn dest = PileupColumn.__new__(PileupColumn)
  *      dest.plp = plp             # <<<<<<<<<<<<<<
  *      dest.tid = tid
  *      dest.pos = pos
  */
   __pyx_v_dest->plp = __pyx_v_plp;
 
-  /* "pysam/csamfile.pyx":132
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
+  /* "pysam/calignmentfile.pyx":138
+ *      cdef PileupColumn dest = PileupColumn.__new__(PileupColumn)
  *      dest.plp = plp
  *      dest.tid = tid             # <<<<<<<<<<<<<<
  *      dest.pos = pos
- *      dest.n = n
+ *      dest.n_pu = n_pu
  */
   __pyx_v_dest->tid = __pyx_v_tid;
 
-  /* "pysam/csamfile.pyx":133
+  /* "pysam/calignmentfile.pyx":139
  *      dest.plp = plp
  *      dest.tid = tid
  *      dest.pos = pos             # <<<<<<<<<<<<<<
- *      dest.n = n
+ *      dest.n_pu = n_pu
  *      return dest
  */
   __pyx_v_dest->pos = __pyx_v_pos;
 
-  /* "pysam/csamfile.pyx":134
+  /* "pysam/calignmentfile.pyx":140
  *      dest.tid = tid
  *      dest.pos = pos
- *      dest.n = n             # <<<<<<<<<<<<<<
+ *      dest.n_pu = n_pu             # <<<<<<<<<<<<<<
  *      return dest
  * 
  */
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_n); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 134; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_dest), __pyx_n_s_n, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 134; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_v_dest->n_pu = __pyx_v_n_pu;
 
-  /* "pysam/csamfile.pyx":135
+  /* "pysam/calignmentfile.pyx":141
  *      dest.pos = pos
- *      dest.n = n
+ *      dest.n_pu = n_pu
  *      return dest             # <<<<<<<<<<<<<<
  * 
  * cdef class PileupRead
@@ -3979,18 +4127,18 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupProxy(bam_pileup1_t **__pyx_
   __pyx_r = ((PyObject *)__pyx_v_dest);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":128
+  /* "pysam/calignmentfile.pyx":134
  * 
- * cdef class PileupProxy
- * cdef makePileupProxy(bam_pileup1_t ** plp, int tid, int pos, int n):             # <<<<<<<<<<<<<<
+ * cdef class PileupColumn
+ * cdef makePileupColumn(bam_pileup1_t ** plp, int tid, int pos, int n_pu):             # <<<<<<<<<<<<<<
  *     # note that the following does not call __init__
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
+ *      cdef PileupColumn dest = PileupColumn.__new__(PileupColumn)
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.makePileupProxy", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.makePileupColumn", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XDECREF((PyObject *)__pyx_v_dest);
@@ -4000,16 +4148,16 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupProxy(bam_pileup1_t **__pyx_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":138
+/* "pysam/calignmentfile.pyx":144
  * 
  * cdef class PileupRead
- * cdef makePileupRead( bam_pileup1_t * src ):             # <<<<<<<<<<<<<<
+ * cdef makePileupRead(bam_pileup1_t * src):             # <<<<<<<<<<<<<<
  *     '''fill a  PileupRead object from a bam_pileup1_t * object.'''
  *     cdef PileupRead dest = PileupRead.__new__(PileupRead)
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_src) {
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_dest = 0;
+static PyObject *__pyx_f_5pysam_14calignmentfile_makePileupRead(bam_pileup1_t *__pyx_v_src) {
+  struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_dest = 0;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4021,40 +4169,40 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("makePileupRead", 0);
-  __Pyx_TraceCall("makePileupRead", __pyx_f[0], 138);
+  __Pyx_TraceCall("makePileupRead", __pyx_f[0], 144);
 
-  /* "pysam/csamfile.pyx":140
- * cdef makePileupRead( bam_pileup1_t * src ):
+  /* "pysam/calignmentfile.pyx":146
+ * cdef makePileupRead(bam_pileup1_t * src):
  *     '''fill a  PileupRead object from a bam_pileup1_t * object.'''
  *     cdef PileupRead dest = PileupRead.__new__(PileupRead)             # <<<<<<<<<<<<<<
- *     dest._alignment = makeAlignedRead( src.b )
+ *     dest._alignment = makeAlignedSegment(src.b)
  *     dest._qpos = src.qpos
  */
-  __pyx_t_1 = __pyx_tp_new_5pysam_8csamfile_PileupRead(((PyTypeObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_PileupRead)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_tp_new_5pysam_14calignmentfile_PileupRead(((PyTypeObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_PileupRead)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 146; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_8csamfile_PileupRead)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_dest = ((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_t_1);
+  if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_14calignmentfile_PileupRead)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 146; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_dest = ((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":141
+  /* "pysam/calignmentfile.pyx":147
  *     '''fill a  PileupRead object from a bam_pileup1_t * object.'''
  *     cdef PileupRead dest = PileupRead.__new__(PileupRead)
- *     dest._alignment = makeAlignedRead( src.b )             # <<<<<<<<<<<<<<
+ *     dest._alignment = makeAlignedSegment(src.b)             # <<<<<<<<<<<<<<
  *     dest._qpos = src.qpos
  *     dest._indel = src.indel
  */
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_src->b); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_f_5pysam_14calignmentfile_makeAlignedSegment(__pyx_v_src->b); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 147; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_8csamfile_AlignedRead))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_14calignmentfile_AlignedSegment))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 147; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GIVEREF(__pyx_t_1);
   __Pyx_GOTREF(__pyx_v_dest->_alignment);
   __Pyx_DECREF(((PyObject *)__pyx_v_dest->_alignment));
-  __pyx_v_dest->_alignment = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_t_1);
+  __pyx_v_dest->_alignment = ((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":142
+  /* "pysam/calignmentfile.pyx":148
  *     cdef PileupRead dest = PileupRead.__new__(PileupRead)
- *     dest._alignment = makeAlignedRead( src.b )
+ *     dest._alignment = makeAlignedSegment(src.b)
  *     dest._qpos = src.qpos             # <<<<<<<<<<<<<<
  *     dest._indel = src.indel
  *     dest._level = src.level
@@ -4062,8 +4210,8 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   __pyx_t_2 = __pyx_v_src->qpos;
   __pyx_v_dest->_qpos = __pyx_t_2;
 
-  /* "pysam/csamfile.pyx":143
- *     dest._alignment = makeAlignedRead( src.b )
+  /* "pysam/calignmentfile.pyx":149
+ *     dest._alignment = makeAlignedSegment(src.b)
  *     dest._qpos = src.qpos
  *     dest._indel = src.indel             # <<<<<<<<<<<<<<
  *     dest._level = src.level
@@ -4072,7 +4220,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   __pyx_t_3 = __pyx_v_src->indel;
   __pyx_v_dest->_indel = __pyx_t_3;
 
-  /* "pysam/csamfile.pyx":144
+  /* "pysam/calignmentfile.pyx":150
  *     dest._qpos = src.qpos
  *     dest._indel = src.indel
  *     dest._level = src.level             # <<<<<<<<<<<<<<
@@ -4082,7 +4230,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   __pyx_t_3 = __pyx_v_src->level;
   __pyx_v_dest->_level = __pyx_t_3;
 
-  /* "pysam/csamfile.pyx":145
+  /* "pysam/calignmentfile.pyx":151
  *     dest._indel = src.indel
  *     dest._level = src.level
  *     dest._is_del = src.is_del             # <<<<<<<<<<<<<<
@@ -4092,7 +4240,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   __pyx_t_4 = __pyx_v_src->is_del;
   __pyx_v_dest->_is_del = __pyx_t_4;
 
-  /* "pysam/csamfile.pyx":146
+  /* "pysam/calignmentfile.pyx":152
  *     dest._level = src.level
  *     dest._is_del = src.is_del
  *     dest._is_head = src.is_head             # <<<<<<<<<<<<<<
@@ -4102,7 +4250,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   __pyx_t_4 = __pyx_v_src->is_head;
   __pyx_v_dest->_is_head = __pyx_t_4;
 
-  /* "pysam/csamfile.pyx":147
+  /* "pysam/calignmentfile.pyx":153
  *     dest._is_del = src.is_del
  *     dest._is_head = src.is_head
  *     dest._is_tail = src.is_tail             # <<<<<<<<<<<<<<
@@ -4112,7 +4260,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   __pyx_t_4 = __pyx_v_src->is_tail;
   __pyx_v_dest->_is_tail = __pyx_t_4;
 
-  /* "pysam/csamfile.pyx":148
+  /* "pysam/calignmentfile.pyx":154
  *     dest._is_head = src.is_head
  *     dest._is_tail = src.is_tail
  *     return dest             # <<<<<<<<<<<<<<
@@ -4124,10 +4272,10 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   __pyx_r = ((PyObject *)__pyx_v_dest);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":138
+  /* "pysam/calignmentfile.pyx":144
  * 
  * cdef class PileupRead
- * cdef makePileupRead( bam_pileup1_t * src ):             # <<<<<<<<<<<<<<
+ * cdef makePileupRead(bam_pileup1_t * src):             # <<<<<<<<<<<<<<
  *     '''fill a  PileupRead object from a bam_pileup1_t * object.'''
  *     cdef PileupRead dest = PileupRead.__new__(PileupRead)
  */
@@ -4135,7 +4283,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.makePileupRead", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.makePileupRead", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XDECREF((PyObject *)__pyx_v_dest);
@@ -4145,7 +4293,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":150
+/* "pysam/calignmentfile.pyx":156
  *     return dest
  * 
  * cdef convertBinaryTagToList( uint8_t * s ):             # <<<<<<<<<<<<<<
@@ -4153,7 +4301,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_
  *     cdef char auxtype
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_v_s) {
+static PyObject *__pyx_f_5pysam_14calignmentfile_convertBinaryTagToList(uint8_t *__pyx_v_s) {
   char __pyx_v_auxtype;
   uint8_t __pyx_v_byte_size;
   int32_t __pyx_v_nvalues;
@@ -4171,9 +4319,9 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("convertBinaryTagToList", 0);
-  __Pyx_TraceCall("convertBinaryTagToList", __pyx_f[0], 150);
+  __Pyx_TraceCall("convertBinaryTagToList", __pyx_f[0], 156);
 
-  /* "pysam/csamfile.pyx":157
+  /* "pysam/calignmentfile.pyx":163
  * 
  *     # get byte size
  *     auxtype = s[0]             # <<<<<<<<<<<<<<
@@ -4182,7 +4330,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
   __pyx_v_auxtype = (__pyx_v_s[0]);
 
-  /* "pysam/csamfile.pyx":158
+  /* "pysam/calignmentfile.pyx":164
  *     # get byte size
  *     auxtype = s[0]
  *     byte_size = aux_type2size( auxtype )             # <<<<<<<<<<<<<<
@@ -4191,7 +4339,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
   __pyx_v_byte_size = aux_type2size(__pyx_v_auxtype);
 
-  /* "pysam/csamfile.pyx":159
+  /* "pysam/calignmentfile.pyx":165
  *     auxtype = s[0]
  *     byte_size = aux_type2size( auxtype )
  *     s += 1             # <<<<<<<<<<<<<<
@@ -4200,7 +4348,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
   __pyx_v_s = (__pyx_v_s + 1);
 
-  /* "pysam/csamfile.pyx":161
+  /* "pysam/calignmentfile.pyx":167
  *     s += 1
  *     # get number of values in array
  *     nvalues = (<int32_t*>s)[0]             # <<<<<<<<<<<<<<
@@ -4209,7 +4357,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
   __pyx_v_nvalues = (((int32_t *)__pyx_v_s)[0]);
 
-  /* "pysam/csamfile.pyx":162
+  /* "pysam/calignmentfile.pyx":168
  *     # get number of values in array
  *     nvalues = (<int32_t*>s)[0]
  *     s += 4             # <<<<<<<<<<<<<<
@@ -4218,19 +4366,19 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
   __pyx_v_s = (__pyx_v_s + 4);
 
-  /* "pysam/csamfile.pyx":164
+  /* "pysam/calignmentfile.pyx":170
  *     s += 4
  *     # get values
  *     values = []             # <<<<<<<<<<<<<<
  *     if auxtype == 'c':
  *         for x from 0 <= x < nvalues:
  */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 170; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_values = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":189
+  /* "pysam/calignmentfile.pyx":195
  *             values.append((<uint32_t*>s)[0])
  *             s += 4
  *     elif auxtype == 'f':             # <<<<<<<<<<<<<<
@@ -4239,7 +4387,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
   switch (__pyx_v_auxtype) {
 
-    /* "pysam/csamfile.pyx":165
+    /* "pysam/calignmentfile.pyx":171
  *     # get values
  *     values = []
  *     if auxtype == 'c':             # <<<<<<<<<<<<<<
@@ -4248,7 +4396,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
     case 'c':
 
-    /* "pysam/csamfile.pyx":166
+    /* "pysam/calignmentfile.pyx":172
  *     values = []
  *     if auxtype == 'c':
  *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
@@ -4258,19 +4406,19 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     __pyx_t_2 = __pyx_v_nvalues;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
 
-      /* "pysam/csamfile.pyx":167
+      /* "pysam/calignmentfile.pyx":173
  *     if auxtype == 'c':
  *         for x from 0 <= x < nvalues:
  *             values.append((<int8_t*>s)[0])             # <<<<<<<<<<<<<<
  *             s += 1
  *     elif auxtype == 'C':
  */
-      __pyx_t_1 = __Pyx_PyInt_From_int8_t((((int8_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyInt_From_int8_t((((int8_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":168
+      /* "pysam/calignmentfile.pyx":174
  *         for x from 0 <= x < nvalues:
  *             values.append((<int8_t*>s)[0])
  *             s += 1             # <<<<<<<<<<<<<<
@@ -4281,7 +4429,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     }
     break;
 
-    /* "pysam/csamfile.pyx":169
+    /* "pysam/calignmentfile.pyx":175
  *             values.append((<int8_t*>s)[0])
  *             s += 1
  *     elif auxtype == 'C':             # <<<<<<<<<<<<<<
@@ -4290,7 +4438,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
     case 'C':
 
-    /* "pysam/csamfile.pyx":170
+    /* "pysam/calignmentfile.pyx":176
  *             s += 1
  *     elif auxtype == 'C':
  *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
@@ -4300,19 +4448,19 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     __pyx_t_2 = __pyx_v_nvalues;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
 
-      /* "pysam/csamfile.pyx":171
+      /* "pysam/calignmentfile.pyx":177
  *     elif auxtype == 'C':
  *         for x from 0 <= x < nvalues:
  *             values.append((<uint8_t*>s)[0])             # <<<<<<<<<<<<<<
  *             s += 1
  *     elif auxtype == 's':
  */
-      __pyx_t_1 = __Pyx_PyInt_From_uint8_t((((uint8_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyInt_From_uint8_t((((uint8_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":172
+      /* "pysam/calignmentfile.pyx":178
  *         for x from 0 <= x < nvalues:
  *             values.append((<uint8_t*>s)[0])
  *             s += 1             # <<<<<<<<<<<<<<
@@ -4323,7 +4471,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     }
     break;
 
-    /* "pysam/csamfile.pyx":173
+    /* "pysam/calignmentfile.pyx":179
  *             values.append((<uint8_t*>s)[0])
  *             s += 1
  *     elif auxtype == 's':             # <<<<<<<<<<<<<<
@@ -4332,7 +4480,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
     case 's':
 
-    /* "pysam/csamfile.pyx":174
+    /* "pysam/calignmentfile.pyx":180
  *             s += 1
  *     elif auxtype == 's':
  *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
@@ -4342,19 +4490,19 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     __pyx_t_2 = __pyx_v_nvalues;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
 
-      /* "pysam/csamfile.pyx":175
+      /* "pysam/calignmentfile.pyx":181
  *     elif auxtype == 's':
  *         for x from 0 <= x < nvalues:
  *             values.append((<int16_t*>s)[0])             # <<<<<<<<<<<<<<
  *             s += 2
  *     elif auxtype == 'S':
  */
-      __pyx_t_1 = __Pyx_PyInt_From_int16_t((((int16_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyInt_From_int16_t((((int16_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":176
+      /* "pysam/calignmentfile.pyx":182
  *         for x from 0 <= x < nvalues:
  *             values.append((<int16_t*>s)[0])
  *             s += 2             # <<<<<<<<<<<<<<
@@ -4365,7 +4513,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     }
     break;
 
-    /* "pysam/csamfile.pyx":177
+    /* "pysam/calignmentfile.pyx":183
  *             values.append((<int16_t*>s)[0])
  *             s += 2
  *     elif auxtype == 'S':             # <<<<<<<<<<<<<<
@@ -4374,7 +4522,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
     case 'S':
 
-    /* "pysam/csamfile.pyx":178
+    /* "pysam/calignmentfile.pyx":184
  *             s += 2
  *     elif auxtype == 'S':
  *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
@@ -4384,19 +4532,19 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     __pyx_t_2 = __pyx_v_nvalues;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
 
-      /* "pysam/csamfile.pyx":179
+      /* "pysam/calignmentfile.pyx":185
  *     elif auxtype == 'S':
  *         for x from 0 <= x < nvalues:
  *             values.append((<uint16_t*>s)[0])             # <<<<<<<<<<<<<<
  *             s += 2
  *     elif auxtype == 'i':
  */
-      __pyx_t_1 = __Pyx_PyInt_From_uint16_t((((uint16_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyInt_From_uint16_t((((uint16_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":180
+      /* "pysam/calignmentfile.pyx":186
  *         for x from 0 <= x < nvalues:
  *             values.append((<uint16_t*>s)[0])
  *             s += 2             # <<<<<<<<<<<<<<
@@ -4407,7 +4555,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     }
     break;
 
-    /* "pysam/csamfile.pyx":181
+    /* "pysam/calignmentfile.pyx":187
  *             values.append((<uint16_t*>s)[0])
  *             s += 2
  *     elif auxtype == 'i':             # <<<<<<<<<<<<<<
@@ -4416,7 +4564,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
     case 'i':
 
-    /* "pysam/csamfile.pyx":182
+    /* "pysam/calignmentfile.pyx":188
  *             s += 2
  *     elif auxtype == 'i':
  *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
@@ -4426,19 +4574,19 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     __pyx_t_2 = __pyx_v_nvalues;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
 
-      /* "pysam/csamfile.pyx":183
+      /* "pysam/calignmentfile.pyx":189
  *     elif auxtype == 'i':
  *         for x from 0 <= x < nvalues:
  *             values.append((<int32_t*>s)[0])             # <<<<<<<<<<<<<<
  *             s += 4
  *     elif auxtype == 'I':
  */
-      __pyx_t_1 = __Pyx_PyInt_From_int32_t((((int32_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyInt_From_int32_t((((int32_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":184
+      /* "pysam/calignmentfile.pyx":190
  *         for x from 0 <= x < nvalues:
  *             values.append((<int32_t*>s)[0])
  *             s += 4             # <<<<<<<<<<<<<<
@@ -4449,7 +4597,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     }
     break;
 
-    /* "pysam/csamfile.pyx":185
+    /* "pysam/calignmentfile.pyx":191
  *             values.append((<int32_t*>s)[0])
  *             s += 4
  *     elif auxtype == 'I':             # <<<<<<<<<<<<<<
@@ -4458,7 +4606,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
     case 'I':
 
-    /* "pysam/csamfile.pyx":186
+    /* "pysam/calignmentfile.pyx":192
  *             s += 4
  *     elif auxtype == 'I':
  *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
@@ -4468,19 +4616,19 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     __pyx_t_2 = __pyx_v_nvalues;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
 
-      /* "pysam/csamfile.pyx":187
+      /* "pysam/calignmentfile.pyx":193
  *     elif auxtype == 'I':
  *         for x from 0 <= x < nvalues:
  *             values.append((<uint32_t*>s)[0])             # <<<<<<<<<<<<<<
  *             s += 4
  *     elif auxtype == 'f':
  */
-      __pyx_t_1 = __Pyx_PyInt_From_uint32_t((((uint32_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyInt_From_uint32_t((((uint32_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 193; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 193; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":188
+      /* "pysam/calignmentfile.pyx":194
  *         for x from 0 <= x < nvalues:
  *             values.append((<uint32_t*>s)[0])
  *             s += 4             # <<<<<<<<<<<<<<
@@ -4491,7 +4639,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     }
     break;
 
-    /* "pysam/csamfile.pyx":189
+    /* "pysam/calignmentfile.pyx":195
  *             values.append((<uint32_t*>s)[0])
  *             s += 4
  *     elif auxtype == 'f':             # <<<<<<<<<<<<<<
@@ -4500,7 +4648,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  */
     case 'f':
 
-    /* "pysam/csamfile.pyx":190
+    /* "pysam/calignmentfile.pyx":196
  *             s += 4
  *     elif auxtype == 'f':
  *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
@@ -4510,19 +4658,19 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     __pyx_t_2 = __pyx_v_nvalues;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
 
-      /* "pysam/csamfile.pyx":191
+      /* "pysam/calignmentfile.pyx":197
  *     elif auxtype == 'f':
  *         for x from 0 <= x < nvalues:
  *             values.append((<float*>s)[0])             # <<<<<<<<<<<<<<
  *             s += 4
  * 
  */
-      __pyx_t_1 = PyFloat_FromDouble((((float *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyFloat_FromDouble((((float *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":192
+      /* "pysam/calignmentfile.pyx":198
  *         for x from 0 <= x < nvalues:
  *             values.append((<float*>s)[0])
  *             s += 4             # <<<<<<<<<<<<<<
@@ -4535,7 +4683,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
     default: break;
   }
 
-  /* "pysam/csamfile.pyx":194
+  /* "pysam/calignmentfile.pyx":200
  *             s += 4
  * 
  *     return byte_size, nvalues, values             # <<<<<<<<<<<<<<
@@ -4543,11 +4691,11 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint8_t(__pyx_v_byte_size); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_uint8_t(__pyx_v_byte_size); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_nvalues); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_nvalues); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = PyTuple_New(3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyTuple_New(3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
@@ -4562,7 +4710,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
   __pyx_t_5 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":150
+  /* "pysam/calignmentfile.pyx":156
  *     return dest
  * 
  * cdef convertBinaryTagToList( uint8_t * s ):             # <<<<<<<<<<<<<<
@@ -4575,7 +4723,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_4);
   __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.convertBinaryTagToList", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.convertBinaryTagToList", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_values);
@@ -4585,141 +4733,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":210
- *         list of reads (:class:`pysam.PileupRead`) aligned to this column
- *     '''
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_12PileupColumn_1__str__(PyObject *__pyx_self, PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_12PileupColumn___str__[] = "PileupColumn.__str__(self)";
-static PyMethodDef __pyx_mdef_5pysam_8csamfile_12PileupColumn_1__str__ = {__Pyx_NAMESTR("__str__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_12PileupColumn_1__str__, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_12PileupColumn___str__)};
-static PyObject *__pyx_pw_5pysam_8csamfile_12PileupColumn_1__str__(PyObject *__pyx_self, PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_12PileupColumn___str__(__pyx_self, ((PyObject *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_12PileupColumn___str__(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 210);
-
-  /* "pysam/csamfile.pyx":211
- *     '''
- *     def __str__(self):
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\             # <<<<<<<<<<<<<<
- *             "\n" + "\n".join( map(str, self.pileups) )
- * 
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_n); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyTuple_New(3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_4, 2, __pyx_t_3);
-  __Pyx_GIVEREF(__pyx_t_3);
-  __pyx_t_1 = 0;
-  __pyx_t_2 = 0;
-  __pyx_t_3 = 0;
-  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = PyNumber_Add(__pyx_t_3, __pyx_kp_s__4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":212
- *     def __str__(self):
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_pileups); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_3);
-  __Pyx_GIVEREF(__pyx_t_3);
-  __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__4, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = PyNumber_Add(__pyx_t_4, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":210
- *         list of reads (:class:`pysam.PileupRead`) aligned to this column
- *     '''
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.PileupColumn.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":309
+/* "pysam/calignmentfile.pyx":297
  *     '''
  * 
  *     def __cinit__(self, *args, **kwargs ):             # <<<<<<<<<<<<<<
@@ -4728,8 +4742,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_12PileupColumn___str__(CYTHON_UNUSED
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_7Samfile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_7Samfile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static int __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_args = 0;
   PyObject *__pyx_v_kwargs = 0;
   int __pyx_r;
@@ -4741,7 +4755,7 @@ static int __pyx_pw_5pysam_8csamfile_7Samfile_1__cinit__(PyObject *__pyx_v_self,
   __Pyx_GOTREF(__pyx_v_kwargs);
   __Pyx_INCREF(__pyx_args);
   __pyx_v_args = __pyx_args;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_args, __pyx_v_kwargs);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile___cinit__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_args, __pyx_v_kwargs);
 
   /* function exit code */
   __Pyx_XDECREF(__pyx_v_args);
@@ -4750,7 +4764,7 @@ static int __pyx_pw_5pysam_8csamfile_7Samfile_1__cinit__(PyObject *__pyx_v_self,
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs) {
+static int __pyx_pf_5pysam_14calignmentfile_13AlignmentFile___cinit__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4762,9 +4776,9 @@ static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__cinit__", 0);
-  __Pyx_TraceCall("__cinit__", __pyx_f[0], 309);
+  __Pyx_TraceCall("__cinit__", __pyx_f[0], 297);
 
-  /* "pysam/csamfile.pyx":310
+  /* "pysam/calignmentfile.pyx":298
  * 
  *     def __cinit__(self, *args, **kwargs ):
  *         self.htsfile = NULL             # <<<<<<<<<<<<<<
@@ -4773,7 +4787,7 @@ static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_
  */
   __pyx_v_self->htsfile = NULL;
 
-  /* "pysam/csamfile.pyx":311
+  /* "pysam/calignmentfile.pyx":299
  *     def __cinit__(self, *args, **kwargs ):
  *         self.htsfile = NULL
  *         self._filename = None             # <<<<<<<<<<<<<<
@@ -4786,7 +4800,7 @@ static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_
   __Pyx_DECREF(__pyx_v_self->_filename);
   __pyx_v_self->_filename = Py_None;
 
-  /* "pysam/csamfile.pyx":312
+  /* "pysam/calignmentfile.pyx":300
  *         self.htsfile = NULL
  *         self._filename = None
  *         self.isbam = False             # <<<<<<<<<<<<<<
@@ -4795,7 +4809,7 @@ static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_
  */
   __pyx_v_self->isbam = 0;
 
-  /* "pysam/csamfile.pyx":313
+  /* "pysam/calignmentfile.pyx":301
  *         self._filename = None
  *         self.isbam = False
  *         self.isstream = False             # <<<<<<<<<<<<<<
@@ -4804,27 +4818,27 @@ static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_
  */
   __pyx_v_self->isstream = 0;
 
-  /* "pysam/csamfile.pyx":314
+  /* "pysam/calignmentfile.pyx":302
  *         self.isbam = False
  *         self.isstream = False
  *         self._open(*args, **kwargs)             # <<<<<<<<<<<<<<
  * 
  *         # allocate memory for iterator
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 302; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 302; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_t_3 = __pyx_v_kwargs;
   __Pyx_INCREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 302; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/csamfile.pyx":317
+  /* "pysam/calignmentfile.pyx":305
  * 
  *         # allocate memory for iterator
  *         self.b = <bam1_t*>calloc(1, sizeof(bam1_t))             # <<<<<<<<<<<<<<
@@ -4833,7 +4847,7 @@ static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_
  */
   __pyx_v_self->b = ((bam1_t *)calloc(1, (sizeof(bam1_t))));
 
-  /* "pysam/csamfile.pyx":309
+  /* "pysam/calignmentfile.pyx":297
  *     '''
  * 
  *     def __cinit__(self, *args, **kwargs ):             # <<<<<<<<<<<<<<
@@ -4849,7 +4863,7 @@ static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -4857,7 +4871,7 @@ static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":319
+/* "pysam/calignmentfile.pyx":307
  *         self.b = <bam1_t*>calloc(1, sizeof(bam1_t))
  * 
  *     def _isOpen( self ):             # <<<<<<<<<<<<<<
@@ -4866,20 +4880,20 @@ static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_2_isOpen[] = "Samfile._isOpen(self)\nreturn true if htsfile has been opened.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_2_isOpen[] = "AlignmentFile._isOpen(self)\nreturn true if htsfile has been opened.";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("_isOpen (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_2_isOpen(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_2_isOpen(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4888,9 +4902,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(struct __pyx_obj_5p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_isOpen", 0);
-  __Pyx_TraceCall("_isOpen", __pyx_f[0], 319);
+  __Pyx_TraceCall("_isOpen", __pyx_f[0], 307);
 
-  /* "pysam/csamfile.pyx":321
+  /* "pysam/calignmentfile.pyx":309
  *     def _isOpen( self ):
  *         '''return true if htsfile has been opened.'''
  *         return self.htsfile != NULL             # <<<<<<<<<<<<<<
@@ -4898,13 +4912,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(struct __pyx_obj_5p
  *     def _hasIndex( self ):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->htsfile != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->htsfile != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 309; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":319
+  /* "pysam/calignmentfile.pyx":307
  *         self.b = <bam1_t*>calloc(1, sizeof(bam1_t))
  * 
  *     def _isOpen( self ):             # <<<<<<<<<<<<<<
@@ -4915,7 +4929,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(struct __pyx_obj_5p
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._isOpen", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._isOpen", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -4924,7 +4938,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(struct __pyx_obj_5p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":323
+/* "pysam/calignmentfile.pyx":311
  *         return self.htsfile != NULL
  * 
  *     def _hasIndex( self ):             # <<<<<<<<<<<<<<
@@ -4933,20 +4947,20 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(struct __pyx_obj_5p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_5_hasIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_4_hasIndex[] = "Samfile._hasIndex(self)\nreturn true if htsfile has an existing (and opened) index.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_5_hasIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_5_hasIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_4_hasIndex[] = "AlignmentFile._hasIndex(self)\nreturn true if htsfile has an existing (and opened) index.";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_5_hasIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("_hasIndex (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_4_hasIndex(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_4_hasIndex(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4955,9 +4969,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_hasIndex", 0);
-  __Pyx_TraceCall("_hasIndex", __pyx_f[0], 323);
+  __Pyx_TraceCall("_hasIndex", __pyx_f[0], 311);
 
-  /* "pysam/csamfile.pyx":325
+  /* "pysam/calignmentfile.pyx":313
  *     def _hasIndex( self ):
  *         '''return true if htsfile has an existing (and opened) index.'''
  *         return self.index != NULL             # <<<<<<<<<<<<<<
@@ -4965,13 +4979,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(struct __pyx_obj_
  *     def _open(self,
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->index != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->index != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 313; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":323
+  /* "pysam/calignmentfile.pyx":311
  *         return self.htsfile != NULL
  * 
  *     def _hasIndex( self ):             # <<<<<<<<<<<<<<
@@ -4982,7 +4996,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(struct __pyx_obj_
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._hasIndex", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._hasIndex", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -4991,7 +5005,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":327
+/* "pysam/calignmentfile.pyx":315
  *         return self.index != NULL
  * 
  *     def _open(self,             # <<<<<<<<<<<<<<
@@ -5000,12 +5014,12 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(struct __pyx_obj_
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_6_open[] = "Samfile._open(self, filename, mode=None, Samfile template=None, referencenames=None, referencelengths=None, text=None, header=None, port=None, add_sq_text=True, check_header=True, check_sq=True)\nopen a sam/bam file.\n\n        If _open is called on an existing bamfile, the current file will be\n        closed and a new file will be opened.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_6_open[] = "AlignmentFile._open(self, filename, mode=None, AlignmentFile template=None, referencenames=None, referencelengths=None, text=None, header=None, port=None, add_sq_text=True, check_header=True, check_sq=True)\nopen a sam/bam file.\n\n        If _open is called on an existing bamfile, the current file will be\n        closed and a new file will be opened.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_filename = 0;
   PyObject *__pyx_v_mode = 0;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_template = 0;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_template = 0;
   PyObject *__pyx_v_referencenames = 0;
   PyObject *__pyx_v_referencelengths = 0;
   PyObject *__pyx_v_text = 0;
@@ -5024,35 +5038,35 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
     static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_filename,&__pyx_n_s_mode,&__pyx_n_s_template,&__pyx_n_s_referencenames,&__pyx_n_s_referencelengths,&__pyx_n_s_text,&__pyx_n_s_header,&__pyx_n_s_port,&__pyx_n_s_add_sq_text,&__pyx_n_s_check_header,&__pyx_n_s_check_sq,0};
     PyObject* values[11] = {0,0,0,0,0,0,0,0,0,0,0};
 
-    /* "pysam/csamfile.pyx":329
+    /* "pysam/calignmentfile.pyx":317
  *     def _open(self,
  *               filename,
  *               mode=None,             # <<<<<<<<<<<<<<
- *               Samfile template=None,
+ *               AlignmentFile template=None,
  *               referencenames=None,
  */
     values[1] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":330
+    /* "pysam/calignmentfile.pyx":318
  *               filename,
  *               mode=None,
- *               Samfile template=None,             # <<<<<<<<<<<<<<
+ *               AlignmentFile template=None,             # <<<<<<<<<<<<<<
  *               referencenames=None,
  *               referencelengths=None,
  */
-    values[2] = (PyObject *)((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None);
+    values[2] = (PyObject *)((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)Py_None);
 
-    /* "pysam/csamfile.pyx":331
+    /* "pysam/calignmentfile.pyx":319
  *               mode=None,
- *               Samfile template=None,
+ *               AlignmentFile template=None,
  *               referencenames=None,             # <<<<<<<<<<<<<<
  *               referencelengths=None,
  *               text=None,
  */
     values[3] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":332
- *               Samfile template=None,
+    /* "pysam/calignmentfile.pyx":320
+ *               AlignmentFile template=None,
  *               referencenames=None,
  *               referencelengths=None,             # <<<<<<<<<<<<<<
  *               text=None,
@@ -5060,7 +5074,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
  */
     values[4] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":333
+    /* "pysam/calignmentfile.pyx":321
  *               referencenames=None,
  *               referencelengths=None,
  *               text=None,             # <<<<<<<<<<<<<<
@@ -5069,7 +5083,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
  */
     values[5] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":334
+    /* "pysam/calignmentfile.pyx":322
  *               referencelengths=None,
  *               text=None,
  *               header=None,             # <<<<<<<<<<<<<<
@@ -5078,7 +5092,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
  */
     values[6] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":335
+    /* "pysam/calignmentfile.pyx":323
  *               text=None,
  *               header=None,
  *               port=None,             # <<<<<<<<<<<<<<
@@ -5087,7 +5101,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
  */
     values[7] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":336
+    /* "pysam/calignmentfile.pyx":324
  *               header=None,
  *               port=None,
  *               add_sq_text=True,             # <<<<<<<<<<<<<<
@@ -5096,7 +5110,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
  */
     values[8] = ((PyObject *)Py_True);
 
-    /* "pysam/csamfile.pyx":337
+    /* "pysam/calignmentfile.pyx":325
  *               port=None,
  *               add_sq_text=True,
  *               check_header=True,             # <<<<<<<<<<<<<<
@@ -5105,7 +5119,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
  */
     values[9] = ((PyObject *)Py_True);
 
-    /* "pysam/csamfile.pyx":338
+    /* "pysam/calignmentfile.pyx":326
  *               add_sq_text=True,
  *               check_header=True,
  *               check_sq=True):             # <<<<<<<<<<<<<<
@@ -5188,7 +5202,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_open") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_open") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 315; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -5209,7 +5223,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
     }
     __pyx_v_filename = values[0];
     __pyx_v_mode = values[1];
-    __pyx_v_template = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[2]);
+    __pyx_v_template = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[2]);
     __pyx_v_referencenames = values[3];
     __pyx_v_referencelengths = values[4];
     __pyx_v_text = values[5];
@@ -5221,16 +5235,16 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_open", 0, 1, 11, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("_open", 0, 1, 11, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 315; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_template), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "template", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_6_open(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_filename, __pyx_v_mode, __pyx_v_template, __pyx_v_referencenames, __pyx_v_referencelengths, __pyx_v_text, __pyx_v_header, __pyx_v_port, __pyx_v_add_sq_text, __pyx_v_check_header, __pyx_v_check_sq);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_template), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "template", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_6_open(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_filename, __pyx_v_mode, __pyx_v_template, __pyx_v_referencenames, __pyx_v_referencelengths, __pyx_v_text, __pyx_v_header, __pyx_v_port, __pyx_v_add_sq_text, __pyx_v_check_header, __pyx_v_check_sq);
 
-  /* "pysam/csamfile.pyx":327
+  /* "pysam/calignmentfile.pyx":315
  *         return self.index != NULL
  * 
  *     def _open(self,             # <<<<<<<<<<<<<<
@@ -5247,7 +5261,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_sel
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_template, PyObject *__pyx_v_referencenames, PyObject *__pyx_v_referencelengths, PyObject *__pyx_v_text, PyObject *__pyx_v_header, PyObject *__pyx_v_port, PyObject *__pyx_v_add_sq_text, PyObject *__pyx_v_check_header, PyObject *__pyx_v_check_sq) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_6_open(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_template, PyObject *__pyx_v_referencenames, PyObject *__pyx_v_referencelengths, PyObject *__pyx_v_text, PyObject *__pyx_v_header, PyObject *__pyx_v_port, PyObject *__pyx_v_add_sq_text, PyObject *__pyx_v_check_header, PyObject *__pyx_ [...]
   CYTHON_UNUSED PyObject *__pyx_v_msg = NULL;
   PyObject *__pyx_v_bmode = 0;
   char *__pyx_v_ctext;
@@ -5288,12 +5302,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_open", 0);
-  __Pyx_TraceCall("_open", __pyx_f[0], 327);
+  __Pyx_TraceCall("_open", __pyx_f[0], 315);
   __Pyx_INCREF(__pyx_v_filename);
+  __Pyx_INCREF(__pyx_v_mode);
   __Pyx_INCREF(__pyx_v_referencenames);
   __Pyx_INCREF(__pyx_v_text);
 
-  /* "pysam/csamfile.pyx":346
+  /* "pysam/calignmentfile.pyx":334
  * 
  *         # read mode autodetection
  *         if mode is None:             # <<<<<<<<<<<<<<
@@ -5304,7 +5319,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":347
+    /* "pysam/calignmentfile.pyx":335
  *         # read mode autodetection
  *         if mode is None:
  *             try:             # <<<<<<<<<<<<<<
@@ -5318,16 +5333,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
       __Pyx_XGOTREF(__pyx_t_5);
       /*try:*/ {
 
-        /* "pysam/csamfile.pyx":348
+        /* "pysam/calignmentfile.pyx":336
  *         if mode is None:
  *             try:
  *                 self._open(filename, 'rb',             # <<<<<<<<<<<<<<
  *                            template=template,
  *                            referencenames=referencenames,
  */
-        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_INCREF(__pyx_v_filename);
         PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_filename);
@@ -5335,96 +5350,96 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
         __Pyx_INCREF(__pyx_n_s_rb);
         PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_n_s_rb);
         __Pyx_GIVEREF(__pyx_n_s_rb);
-        __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_8);
 
-        /* "pysam/csamfile.pyx":349
+        /* "pysam/calignmentfile.pyx":337
  *             try:
  *                 self._open(filename, 'rb',
  *                            template=template,             # <<<<<<<<<<<<<<
  *                            referencenames=referencenames,
  *                            referencelengths=referencelengths,
  */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_template, ((PyObject *)__pyx_v_template)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_template, ((PyObject *)__pyx_v_template)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
 
-        /* "pysam/csamfile.pyx":350
+        /* "pysam/calignmentfile.pyx":338
  *                 self._open(filename, 'rb',
  *                            template=template,
  *                            referencenames=referencenames,             # <<<<<<<<<<<<<<
  *                            referencelengths=referencelengths,
  *                            text=text,
  */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_referencenames, __pyx_v_referencenames) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_referencenames, __pyx_v_referencenames) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
 
-        /* "pysam/csamfile.pyx":351
+        /* "pysam/calignmentfile.pyx":339
  *                            template=template,
  *                            referencenames=referencenames,
  *                            referencelengths=referencelengths,             # <<<<<<<<<<<<<<
  *                            text=text,
  *                            header=header,
  */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_referencelengths, __pyx_v_referencelengths) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_referencelengths, __pyx_v_referencelengths) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
 
-        /* "pysam/csamfile.pyx":352
+        /* "pysam/calignmentfile.pyx":340
  *                            referencenames=referencenames,
  *                            referencelengths=referencelengths,
  *                            text=text,             # <<<<<<<<<<<<<<
  *                            header=header,
  *                            port=port,
  */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_text, __pyx_v_text) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_text, __pyx_v_text) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
 
-        /* "pysam/csamfile.pyx":353
+        /* "pysam/calignmentfile.pyx":341
  *                            referencelengths=referencelengths,
  *                            text=text,
  *                            header=header,             # <<<<<<<<<<<<<<
  *                            port=port,
  *                            check_header=check_header,
  */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_header, __pyx_v_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_header, __pyx_v_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
 
-        /* "pysam/csamfile.pyx":354
+        /* "pysam/calignmentfile.pyx":342
  *                            text=text,
  *                            header=header,
  *                            port=port,             # <<<<<<<<<<<<<<
  *                            check_header=check_header,
  *                            check_sq=check_sq)
  */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_port, __pyx_v_port) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_port, __pyx_v_port) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
 
-        /* "pysam/csamfile.pyx":355
+        /* "pysam/calignmentfile.pyx":343
  *                            header=header,
  *                            port=port,
  *                            check_header=check_header,             # <<<<<<<<<<<<<<
  *                            check_sq=check_sq)
  *                 return
  */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_check_header, __pyx_v_check_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_check_header, __pyx_v_check_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
 
-        /* "pysam/csamfile.pyx":356
+        /* "pysam/calignmentfile.pyx":344
  *                            port=port,
  *                            check_header=check_header,
  *                            check_sq=check_sq)             # <<<<<<<<<<<<<<
  *                 return
  *             except ValueError, msg:
  */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_check_sq, __pyx_v_check_sq) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_check_sq, __pyx_v_check_sq) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
 
-        /* "pysam/csamfile.pyx":348
+        /* "pysam/calignmentfile.pyx":336
  *         if mode is None:
  *             try:
  *                 self._open(filename, 'rb',             # <<<<<<<<<<<<<<
  *                            template=template,
  *                            referencenames=referencenames,
  */
-        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_7, __pyx_t_8); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_7, __pyx_t_8); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-        /* "pysam/csamfile.pyx":357
+        /* "pysam/calignmentfile.pyx":345
  *                            check_header=check_header,
  *                            check_sq=check_sq)
  *                 return             # <<<<<<<<<<<<<<
@@ -5445,7 +5460,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
       __Pyx_XDECREF(__pyx_t_8); __pyx_t_8 = 0;
       __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-      /* "pysam/csamfile.pyx":358
+      /* "pysam/calignmentfile.pyx":346
  *                            check_sq=check_sq)
  *                 return
  *             except ValueError, msg:             # <<<<<<<<<<<<<<
@@ -5454,8 +5469,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
       __pyx_t_10 = PyErr_ExceptionMatches(__pyx_builtin_ValueError);
       if (__pyx_t_10) {
-        __Pyx_AddTraceback("pysam.csamfile.Samfile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_8, &__pyx_t_7) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
+        if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_8, &__pyx_t_7) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 346; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_GOTREF(__pyx_t_7);
@@ -5487,16 +5502,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
       __pyx_L11_try_end:;
     }
 
-    /* "pysam/csamfile.pyx":361
+    /* "pysam/calignmentfile.pyx":349
  *                 pass
  * 
  *             self._open(filename, 'r',             # <<<<<<<<<<<<<<
  *                        template=template,
  *                        referencenames=referencenames,
  */
-    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_INCREF(__pyx_v_filename);
     PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_filename);
@@ -5504,111 +5519,111 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
     __Pyx_INCREF(__pyx_n_s_r);
     PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_n_s_r);
     __Pyx_GIVEREF(__pyx_n_s_r);
-    __pyx_t_9 = PyDict_New(); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyDict_New(); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
 
-    /* "pysam/csamfile.pyx":362
+    /* "pysam/calignmentfile.pyx":350
  * 
  *             self._open(filename, 'r',
  *                        template=template,             # <<<<<<<<<<<<<<
  *                        referencenames=referencenames,
  *                        referencelengths=referencelengths,
  */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_template, ((PyObject *)__pyx_v_template)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_template, ((PyObject *)__pyx_v_template)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":363
+    /* "pysam/calignmentfile.pyx":351
  *             self._open(filename, 'r',
  *                        template=template,
  *                        referencenames=referencenames,             # <<<<<<<<<<<<<<
  *                        referencelengths=referencelengths,
  *                        text=text,
  */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_referencenames, __pyx_v_referencenames) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_referencenames, __pyx_v_referencenames) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":364
+    /* "pysam/calignmentfile.pyx":352
  *                        template=template,
  *                        referencenames=referencenames,
  *                        referencelengths=referencelengths,             # <<<<<<<<<<<<<<
  *                        text=text,
  *                        header=header,
  */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_referencelengths, __pyx_v_referencelengths) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_referencelengths, __pyx_v_referencelengths) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":365
+    /* "pysam/calignmentfile.pyx":353
  *                        referencenames=referencenames,
  *                        referencelengths=referencelengths,
  *                        text=text,             # <<<<<<<<<<<<<<
  *                        header=header,
  *                        port=port,
  */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_text, __pyx_v_text) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_text, __pyx_v_text) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":366
+    /* "pysam/calignmentfile.pyx":354
  *                        referencelengths=referencelengths,
  *                        text=text,
  *                        header=header,             # <<<<<<<<<<<<<<
  *                        port=port,
  *                        check_header=check_header,
  */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_header, __pyx_v_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_header, __pyx_v_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":367
+    /* "pysam/calignmentfile.pyx":355
  *                        text=text,
  *                        header=header,
  *                        port=port,             # <<<<<<<<<<<<<<
  *                        check_header=check_header,
  *                        check_sq=check_sq)
  */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_port, __pyx_v_port) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_port, __pyx_v_port) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":368
+    /* "pysam/calignmentfile.pyx":356
  *                        header=header,
  *                        port=port,
  *                        check_header=check_header,             # <<<<<<<<<<<<<<
  *                        check_sq=check_sq)
  *             return
  */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_check_header, __pyx_v_check_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_check_header, __pyx_v_check_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":369
+    /* "pysam/calignmentfile.pyx":357
  *                        port=port,
  *                        check_header=check_header,
  *                        check_sq=check_sq)             # <<<<<<<<<<<<<<
  *             return
  * 
  */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_check_sq, __pyx_v_check_sq) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_check_sq, __pyx_v_check_sq) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":361
+    /* "pysam/calignmentfile.pyx":349
  *                 pass
  * 
  *             self._open(filename, 'r',             # <<<<<<<<<<<<<<
  *                        template=template,
  *                        referencenames=referencenames,
  */
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_8, __pyx_t_9); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_8, __pyx_t_9); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-    /* "pysam/csamfile.pyx":370
+    /* "pysam/calignmentfile.pyx":358
  *                        check_header=check_header,
  *                        check_sq=check_sq)
  *             return             # <<<<<<<<<<<<<<
  * 
- *         assert mode in ( "r","w","rb","wb", "wh", "wbu", "rU" ), \
+ *         assert mode in ("r","w","rb","wb", "wh", "wbu", "rU", "wb0"), \
  */
     __Pyx_XDECREF(__pyx_r);
     __pyx_r = Py_None; __Pyx_INCREF(Py_None);
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":372
+  /* "pysam/calignmentfile.pyx":360
  *             return
  * 
- *         assert mode in ( "r","w","rb","wb", "wh", "wbu", "rU" ), \             # <<<<<<<<<<<<<<
+ *         assert mode in ("r","w","rb","wb", "wh", "wbu", "rU", "wb0"), \             # <<<<<<<<<<<<<<
  *             "invalid file opening mode `%s`" % mode
  * 
  */
@@ -5616,82 +5631,88 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
   if (unlikely(!Py_OptimizeFlag)) {
     __Pyx_INCREF(__pyx_v_mode);
     __pyx_t_6 = __pyx_v_mode;
-    __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (!__pyx_t_2) {
-      __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_w, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_w, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_11 = __pyx_t_1;
     } else {
       __pyx_t_11 = __pyx_t_2;
     }
     if (!__pyx_t_11) {
-      __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_rb, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_rb, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_1 = __pyx_t_2;
     } else {
       __pyx_t_1 = __pyx_t_11;
     }
     if (!__pyx_t_1) {
-      __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wb, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wb, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_2 = __pyx_t_11;
     } else {
       __pyx_t_2 = __pyx_t_1;
     }
     if (!__pyx_t_2) {
-      __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wh, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wh, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_11 = __pyx_t_1;
     } else {
       __pyx_t_11 = __pyx_t_2;
     }
     if (!__pyx_t_11) {
-      __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wbu, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wbu, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_1 = __pyx_t_2;
     } else {
       __pyx_t_1 = __pyx_t_11;
     }
     if (!__pyx_t_1) {
-      __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_rU, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_rU, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_2 = __pyx_t_11;
     } else {
       __pyx_t_2 = __pyx_t_1;
     }
+    if (!__pyx_t_2) {
+      __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wb0, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __pyx_t_1;
+    } else {
+      __pyx_t_11 = __pyx_t_2;
+    }
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    if (unlikely(!(__pyx_t_2 != 0))) {
+    if (unlikely(!(__pyx_t_11 != 0))) {
 
-      /* "pysam/csamfile.pyx":373
+      /* "pysam/calignmentfile.pyx":361
  * 
- *         assert mode in ( "r","w","rb","wb", "wh", "wbu", "rU" ), \
+ *         assert mode in ("r","w","rb","wb", "wh", "wbu", "rU", "wb0"), \
  *             "invalid file opening mode `%s`" % mode             # <<<<<<<<<<<<<<
  * 
  *         # close a previously opened file
  */
-      __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_invalid_file_opening_mode_s, __pyx_v_mode); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_invalid_file_opening_mode_s, __pyx_v_mode); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       PyErr_SetObject(PyExc_AssertionError, __pyx_t_6);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
   #endif
 
-  /* "pysam/csamfile.pyx":376
+  /* "pysam/calignmentfile.pyx":364
  * 
  *         # close a previously opened file
  *         if self.htsfile != NULL:             # <<<<<<<<<<<<<<
  *             self.close()
  * 
  */
-  __pyx_t_2 = ((__pyx_v_self->htsfile != NULL) != 0);
-  if (__pyx_t_2) {
+  __pyx_t_11 = ((__pyx_v_self->htsfile != NULL) != 0);
+  if (__pyx_t_11) {
 
-    /* "pysam/csamfile.pyx":377
+    /* "pysam/calignmentfile.pyx":365
  *         # close a previously opened file
  *         if self.htsfile != NULL:
  *             self.close()             # <<<<<<<<<<<<<<
  * 
- *         cdef bytes bmode = mode.encode('ascii')
+ *         # for htslib, wbu seems to not work
  */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -5699,30 +5720,53 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
   }
   __pyx_L14:;
 
-  /* "pysam/csamfile.pyx":379
- *             self.close()
+  /* "pysam/calignmentfile.pyx":368
+ * 
+ *         # for htslib, wbu seems to not work
+ *         if mode == "wbu":             # <<<<<<<<<<<<<<
+ *             mode = "wb0"
+ * 
+ */
+  __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_v_mode, __pyx_n_s_wbu, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__pyx_t_11) {
+
+    /* "pysam/calignmentfile.pyx":369
+ *         # for htslib, wbu seems to not work
+ *         if mode == "wbu":
+ *             mode = "wb0"             # <<<<<<<<<<<<<<
+ * 
+ *         cdef bytes bmode = mode.encode('ascii')
+ */
+    __Pyx_INCREF(__pyx_n_s_wb0);
+    __Pyx_DECREF_SET(__pyx_v_mode, __pyx_n_s_wb0);
+    goto __pyx_L15;
+  }
+  __pyx_L15:;
+
+  /* "pysam/calignmentfile.pyx":371
+ *             mode = "wb0"
  * 
  *         cdef bytes bmode = mode.encode('ascii')             # <<<<<<<<<<<<<<
  *         self._filename = filename = _encodeFilename(filename)
  *         self.isstream = filename == b"-"
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_mode, __pyx_n_s_encode); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_mode, __pyx_n_s_encode); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  if (!(likely(PyBytes_CheckExact(__pyx_t_6))||((__pyx_t_6) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_6)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!(likely(PyBytes_CheckExact(__pyx_t_6))||((__pyx_t_6) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_6)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_bmode = ((PyObject*)__pyx_t_6);
   __pyx_t_6 = 0;
 
-  /* "pysam/csamfile.pyx":380
+  /* "pysam/calignmentfile.pyx":372
  * 
  *         cdef bytes bmode = mode.encode('ascii')
  *         self._filename = filename = _encodeFilename(filename)             # <<<<<<<<<<<<<<
  *         self.isstream = filename == b"-"
  * 
  */
-  __pyx_t_6 = __pyx_f_5pysam_8csamfile__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __pyx_f_5pysam_14calignmentfile__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_INCREF(__pyx_t_6);
   __Pyx_GIVEREF(__pyx_t_6);
@@ -5733,34 +5777,34 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
   __Pyx_DECREF_SET(__pyx_v_filename, __pyx_t_6);
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-  /* "pysam/csamfile.pyx":381
+  /* "pysam/calignmentfile.pyx":373
  *         cdef bytes bmode = mode.encode('ascii')
  *         self._filename = filename = _encodeFilename(filename)
  *         self.isstream = filename == b"-"             # <<<<<<<<<<<<<<
  * 
  *         self.isbam = len(mode) > 1 and mode[1] == 'b'
  */
-  __pyx_t_6 = PyObject_RichCompare(__pyx_v_filename, __pyx_kp_b__6, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_6); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = PyObject_RichCompare(__pyx_v_filename, __pyx_kp_b__4, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_6); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
   __pyx_v_self->isstream = __pyx_t_10;
 
-  /* "pysam/csamfile.pyx":383
+  /* "pysam/calignmentfile.pyx":375
  *         self.isstream = filename == b"-"
  * 
  *         self.isbam = len(mode) > 1 and mode[1] == 'b'             # <<<<<<<<<<<<<<
  * 
  *         self.isremote = filename.startswith(b"http:") or \
  */
-  __pyx_t_12 = PyObject_Length(__pyx_v_mode); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_6 = __Pyx_PyBool_FromLong((__pyx_t_12 > 1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = PyObject_Length(__pyx_v_mode); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyBool_FromLong((__pyx_t_12 > 1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_2) {
+  __pyx_t_11 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__pyx_t_11) {
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_mode, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_mode, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_8 = PyObject_RichCompare(__pyx_t_9, __pyx_n_s_b, Py_EQ); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = PyObject_RichCompare(__pyx_t_9, __pyx_n_s_b, Py_EQ); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __pyx_t_9 = __pyx_t_8;
     __pyx_t_8 = 0;
@@ -5768,36 +5812,36 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
     __pyx_t_9 = __pyx_t_6;
     __pyx_t_6 = 0;
   }
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   __pyx_v_self->isbam = __pyx_t_10;
 
-  /* "pysam/csamfile.pyx":385
+  /* "pysam/calignmentfile.pyx":377
  *         self.isbam = len(mode) > 1 and mode[1] == 'b'
  * 
  *         self.isremote = filename.startswith(b"http:") or \             # <<<<<<<<<<<<<<
  *                         filename.startswith(b"ftp:")
  * 
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_2) {
+  __pyx_t_11 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!__pyx_t_11) {
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-    /* "pysam/csamfile.pyx":386
+    /* "pysam/calignmentfile.pyx":378
  * 
  *         self.isremote = filename.startswith(b"http:") or \
  *                         filename.startswith(b"ftp:")             # <<<<<<<<<<<<<<
  * 
  *         cdef char * ctext
  */
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__8, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __pyx_t_9 = __pyx_t_8;
@@ -5807,18 +5851,18 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
     __pyx_t_6 = 0;
   }
 
-  /* "pysam/csamfile.pyx":385
+  /* "pysam/calignmentfile.pyx":377
  *         self.isbam = len(mode) > 1 and mode[1] == 'b'
  * 
  *         self.isremote = filename.startswith(b"http:") or \             # <<<<<<<<<<<<<<
  *                         filename.startswith(b"ftp:")
  * 
  */
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   __pyx_v_self->isremote = __pyx_t_10;
 
-  /* "pysam/csamfile.pyx":389
+  /* "pysam/calignmentfile.pyx":381
  * 
  *         cdef char * ctext
  *         ctext = NULL             # <<<<<<<<<<<<<<
@@ -5827,30 +5871,30 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
   __pyx_v_ctext = NULL;
 
-  /* "pysam/csamfile.pyx":391
+  /* "pysam/calignmentfile.pyx":383
  *         ctext = NULL
  * 
  *         if mode[0] == 'w':             # <<<<<<<<<<<<<<
  *             # open file for writing
  * 
  */
-  __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_w, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_w, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  if (__pyx_t_2) {
+  if (__pyx_t_11) {
 
-    /* "pysam/csamfile.pyx":395
+    /* "pysam/calignmentfile.pyx":387
  * 
  *             # header structure (used for writing)
  *             if template:             # <<<<<<<<<<<<<<
  *                 self.header = bam_hdr_dup(template.header)
  *             elif header:
  */
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(((PyObject *)__pyx_v_template)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (__pyx_t_2) {
+    __pyx_t_11 = __Pyx_PyObject_IsTrue(((PyObject *)__pyx_v_template)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__pyx_t_11) {
 
-      /* "pysam/csamfile.pyx":396
+      /* "pysam/calignmentfile.pyx":388
  *             # header structure (used for writing)
  *             if template:
  *                 self.header = bam_hdr_dup(template.header)             # <<<<<<<<<<<<<<
@@ -5858,32 +5902,32 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  *                 self.header = self._buildHeader(header)
  */
       __pyx_v_self->header = bam_hdr_dup(__pyx_v_template->header);
-      goto __pyx_L16;
+      goto __pyx_L17;
     }
 
-    /* "pysam/csamfile.pyx":397
+    /* "pysam/calignmentfile.pyx":389
  *             if template:
  *                 self.header = bam_hdr_dup(template.header)
  *             elif header:             # <<<<<<<<<<<<<<
  *                 self.header = self._buildHeader(header)
  *             else:
  */
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_header); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (__pyx_t_2) {
+    __pyx_t_11 = __Pyx_PyObject_IsTrue(__pyx_v_header); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__pyx_t_11) {
 
-      /* "pysam/csamfile.pyx":398
+      /* "pysam/calignmentfile.pyx":390
  *                 self.header = bam_hdr_dup(template.header)
  *             elif header:
  *                 self.header = self._buildHeader(header)             # <<<<<<<<<<<<<<
  *             else:
  *                 # build header from a target names and lengths
  */
-      __pyx_v_self->header = ((struct __pyx_vtabstruct_5pysam_8csamfile_Samfile *)__pyx_v_self->__pyx_vtab)->_buildHeader(__pyx_v_self, __pyx_v_header);
-      goto __pyx_L16;
+      __pyx_v_self->header = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self->__pyx_vtab)->_buildHeader(__pyx_v_self, __pyx_v_header);
+      goto __pyx_L17;
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":401
+      /* "pysam/calignmentfile.pyx":393
  *             else:
  *                 # build header from a target names and lengths
  *                 assert referencenames and referencelengths, \             # <<<<<<<<<<<<<<
@@ -5892,21 +5936,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
       #ifndef CYTHON_WITHOUT_ASSERTIONS
       if (unlikely(!Py_OptimizeFlag)) {
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_referencenames); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        if (__pyx_t_2) {
-          __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_referencelengths); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_t_11 = __pyx_t_1;
+        __pyx_t_11 = __Pyx_PyObject_IsTrue(__pyx_v_referencenames); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (__pyx_t_11) {
+          __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_referencelengths); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_1 = __pyx_t_2;
         } else {
-          __pyx_t_11 = __pyx_t_2;
+          __pyx_t_1 = __pyx_t_11;
         }
-        if (unlikely(!__pyx_t_11)) {
+        if (unlikely(!__pyx_t_1)) {
           PyErr_SetObject(PyExc_AssertionError, __pyx_kp_s_either_supply_options_template_h);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
       }
       #endif
 
-      /* "pysam/csamfile.pyx":405
+      /* "pysam/calignmentfile.pyx":397
  *                      "or  both `referencenames` and `referencelengths` "
  *                      "for writing")
  *                 assert len(referencenames) == len(referencelengths), \             # <<<<<<<<<<<<<<
@@ -5915,29 +5959,29 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
       #ifndef CYTHON_WITHOUT_ASSERTIONS
       if (unlikely(!Py_OptimizeFlag)) {
-        __pyx_t_12 = PyObject_Length(__pyx_v_referencenames); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_13 = PyObject_Length(__pyx_v_referencelengths); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_12 = PyObject_Length(__pyx_v_referencenames); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_13 = PyObject_Length(__pyx_v_referencelengths); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         if (unlikely(!((__pyx_t_12 == __pyx_t_13) != 0))) {
           PyErr_SetObject(PyExc_AssertionError, __pyx_kp_s_unequal_names_and_lengths_of_ref);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
       }
       #endif
 
-      /* "pysam/csamfile.pyx":409
+      /* "pysam/calignmentfile.pyx":401
  * 
  *                 # allocate and fill header
  *                 referencenames = [_forceBytes(ref) for ref in referencenames]             # <<<<<<<<<<<<<<
  *                 self.header = bam_hdr_init()
  *                 self.header.n_targets = len(referencenames)
  */
-      __pyx_t_9 = PyList_New(0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyList_New(0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       if (PyList_CheckExact(__pyx_v_referencenames) || PyTuple_CheckExact(__pyx_v_referencenames)) {
         __pyx_t_6 = __pyx_v_referencenames; __Pyx_INCREF(__pyx_t_6); __pyx_t_13 = 0;
         __pyx_t_14 = NULL;
       } else {
-        __pyx_t_13 = -1; __pyx_t_6 = PyObject_GetIter(__pyx_v_referencenames); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_13 = -1; __pyx_t_6 = PyObject_GetIter(__pyx_v_referencenames); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         __pyx_t_14 = Py_TYPE(__pyx_t_6)->tp_iternext;
       }
@@ -5945,16 +5989,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
         if (!__pyx_t_14 && PyList_CheckExact(__pyx_t_6)) {
           if (__pyx_t_13 >= PyList_GET_SIZE(__pyx_t_6)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_8 = PyList_GET_ITEM(__pyx_t_6, __pyx_t_13); __Pyx_INCREF(__pyx_t_8); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = PyList_GET_ITEM(__pyx_t_6, __pyx_t_13); __Pyx_INCREF(__pyx_t_8); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_8 = PySequence_ITEM(__pyx_t_6, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = PySequence_ITEM(__pyx_t_6, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else if (!__pyx_t_14 && PyTuple_CheckExact(__pyx_t_6)) {
           if (__pyx_t_13 >= PyTuple_GET_SIZE(__pyx_t_6)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_6, __pyx_t_13); __Pyx_INCREF(__pyx_t_8); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_6, __pyx_t_13); __Pyx_INCREF(__pyx_t_8); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_8 = PySequence_ITEM(__pyx_t_6, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = PySequence_ITEM(__pyx_t_6, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else {
           __pyx_t_8 = __pyx_t_14(__pyx_t_6);
@@ -5962,7 +6006,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
             PyObject* exc_type = PyErr_Occurred();
             if (exc_type) {
               if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             }
             break;
           }
@@ -5970,16 +6014,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
         }
         __Pyx_XDECREF_SET(__pyx_v_ref, __pyx_t_8);
         __pyx_t_8 = 0;
-        __pyx_t_8 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_ref); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_ref); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
-        if (unlikely(__Pyx_ListComp_Append(__pyx_t_9, (PyObject*)__pyx_t_8))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(__Pyx_ListComp_Append(__pyx_t_9, (PyObject*)__pyx_t_8))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       }
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_DECREF_SET(__pyx_v_referencenames, __pyx_t_9);
       __pyx_t_9 = 0;
 
-      /* "pysam/csamfile.pyx":410
+      /* "pysam/calignmentfile.pyx":402
  *                 # allocate and fill header
  *                 referencenames = [_forceBytes(ref) for ref in referencenames]
  *                 self.header = bam_hdr_init()             # <<<<<<<<<<<<<<
@@ -5988,17 +6032,17 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
       __pyx_v_self->header = bam_hdr_init();
 
-      /* "pysam/csamfile.pyx":411
+      /* "pysam/calignmentfile.pyx":403
  *                 referencenames = [_forceBytes(ref) for ref in referencenames]
  *                 self.header = bam_hdr_init()
  *                 self.header.n_targets = len(referencenames)             # <<<<<<<<<<<<<<
  *                 n = 0
  *                 for x in referencenames:
  */
-      __pyx_t_13 = PyObject_Length(__pyx_v_referencenames); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_13 = PyObject_Length(__pyx_v_referencenames); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 403; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_v_self->header->n_targets = __pyx_t_13;
 
-      /* "pysam/csamfile.pyx":412
+      /* "pysam/calignmentfile.pyx":404
  *                 self.header = bam_hdr_init()
  *                 self.header.n_targets = len(referencenames)
  *                 n = 0             # <<<<<<<<<<<<<<
@@ -6008,18 +6052,18 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
       __Pyx_INCREF(__pyx_int_0);
       __pyx_v_n = __pyx_int_0;
 
-      /* "pysam/csamfile.pyx":413
+      /* "pysam/calignmentfile.pyx":405
  *                 self.header.n_targets = len(referencenames)
  *                 n = 0
  *                 for x in referencenames:             # <<<<<<<<<<<<<<
  *                     n += len(x) + 1
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
+ *                 self.header.target_name = <char**>calloc(
  */
       if (PyList_CheckExact(__pyx_v_referencenames) || PyTuple_CheckExact(__pyx_v_referencenames)) {
         __pyx_t_9 = __pyx_v_referencenames; __Pyx_INCREF(__pyx_t_9); __pyx_t_13 = 0;
         __pyx_t_14 = NULL;
       } else {
-        __pyx_t_13 = -1; __pyx_t_9 = PyObject_GetIter(__pyx_v_referencenames); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_13 = -1; __pyx_t_9 = PyObject_GetIter(__pyx_v_referencenames); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __pyx_t_14 = Py_TYPE(__pyx_t_9)->tp_iternext;
       }
@@ -6027,16 +6071,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
         if (!__pyx_t_14 && PyList_CheckExact(__pyx_t_9)) {
           if (__pyx_t_13 >= PyList_GET_SIZE(__pyx_t_9)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_6 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_13); __Pyx_INCREF(__pyx_t_6); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_6 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_13); __Pyx_INCREF(__pyx_t_6); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_6 = PySequence_ITEM(__pyx_t_9, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_6 = PySequence_ITEM(__pyx_t_9, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else if (!__pyx_t_14 && PyTuple_CheckExact(__pyx_t_9)) {
           if (__pyx_t_13 >= PyTuple_GET_SIZE(__pyx_t_9)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_9, __pyx_t_13); __Pyx_INCREF(__pyx_t_6); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_9, __pyx_t_13); __Pyx_INCREF(__pyx_t_6); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_6 = PySequence_ITEM(__pyx_t_9, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_6 = PySequence_ITEM(__pyx_t_9, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else {
           __pyx_t_6 = __pyx_t_14(__pyx_t_9);
@@ -6044,7 +6088,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
             PyObject* exc_type = PyErr_Occurred();
             if (exc_type) {
               if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             }
             break;
           }
@@ -6053,17 +6097,17 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
         __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_6);
         __pyx_t_6 = 0;
 
-        /* "pysam/csamfile.pyx":414
+        /* "pysam/calignmentfile.pyx":406
  *                 n = 0
  *                 for x in referencenames:
  *                     n += len(x) + 1             # <<<<<<<<<<<<<<
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
+ *                 self.header.target_name = <char**>calloc(
+ *                     n, sizeof(char*))
  */
-        __pyx_t_12 = PyObject_Length(__pyx_v_x); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_6 = PyInt_FromSsize_t((__pyx_t_12 + 1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_12 = PyObject_Length(__pyx_v_x); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyInt_FromSsize_t((__pyx_t_12 + 1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_8 = PyNumber_InPlaceAdd(__pyx_v_n, __pyx_t_6); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PyNumber_InPlaceAdd(__pyx_v_n, __pyx_t_6); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __Pyx_DECREF_SET(__pyx_v_n, __pyx_t_8);
@@ -6071,140 +6115,156 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
       }
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-      /* "pysam/csamfile.pyx":415
+      /* "pysam/calignmentfile.pyx":408
+ *                     n += len(x) + 1
+ *                 self.header.target_name = <char**>calloc(
+ *                     n, sizeof(char*))             # <<<<<<<<<<<<<<
+ *                 self.header.target_len = <uint32_t*>calloc(
+ *                     n, sizeof(uint32_t))
+ */
+      __pyx_t_15 = __Pyx_PyInt_As_size_t(__pyx_v_n); if (unlikely((__pyx_t_15 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+      /* "pysam/calignmentfile.pyx":407
  *                 for x in referencenames:
  *                     n += len(x) + 1
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))             # <<<<<<<<<<<<<<
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
- *                 for x from 0 <= x < self.header.n_targets:
+ *                 self.header.target_name = <char**>calloc(             # <<<<<<<<<<<<<<
+ *                     n, sizeof(char*))
+ *                 self.header.target_len = <uint32_t*>calloc(
  */
-      __pyx_t_15 = __Pyx_PyInt_As_size_t(__pyx_v_n); if (unlikely((__pyx_t_15 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_v_self->header->target_name = ((char **)calloc(__pyx_t_15, (sizeof(char *))));
 
-      /* "pysam/csamfile.pyx":416
- *                     n += len(x) + 1
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))             # <<<<<<<<<<<<<<
+      /* "pysam/calignmentfile.pyx":410
+ *                     n, sizeof(char*))
+ *                 self.header.target_len = <uint32_t*>calloc(
+ *                     n, sizeof(uint32_t))             # <<<<<<<<<<<<<<
  *                 for x from 0 <= x < self.header.n_targets:
  *                     self.header.target_len[x] = referencelengths[x]
  */
-      __pyx_t_15 = __Pyx_PyInt_As_size_t(__pyx_v_n); if (unlikely((__pyx_t_15 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_15 = __Pyx_PyInt_As_size_t(__pyx_v_n); if (unlikely((__pyx_t_15 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+      /* "pysam/calignmentfile.pyx":409
+ *                 self.header.target_name = <char**>calloc(
+ *                     n, sizeof(char*))
+ *                 self.header.target_len = <uint32_t*>calloc(             # <<<<<<<<<<<<<<
+ *                     n, sizeof(uint32_t))
+ *                 for x from 0 <= x < self.header.n_targets:
+ */
       __pyx_v_self->header->target_len = ((uint32_t *)calloc(__pyx_t_15, (sizeof(uint32_t))));
 
-      /* "pysam/csamfile.pyx":417
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
+      /* "pysam/calignmentfile.pyx":411
+ *                 self.header.target_len = <uint32_t*>calloc(
+ *                     n, sizeof(uint32_t))
  *                 for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
  *                     self.header.target_len[x] = referencelengths[x]
  *                     name = referencenames[x]
  */
       __pyx_t_16 = __pyx_v_self->header->n_targets;
       for (__pyx_t_17 = 0; __pyx_t_17 < __pyx_t_16; __pyx_t_17++) {
-        __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
         __pyx_t_9 = 0;
 
-        /* "pysam/csamfile.pyx":418
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
+        /* "pysam/calignmentfile.pyx":412
+ *                     n, sizeof(uint32_t))
  *                 for x from 0 <= x < self.header.n_targets:
  *                     self.header.target_len[x] = referencelengths[x]             # <<<<<<<<<<<<<<
  *                     name = referencenames[x]
  *                     self.header.target_name[x] = <char*>calloc(
  */
-        __pyx_t_9 = PyObject_GetItem(__pyx_v_referencelengths, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_9 = PyObject_GetItem(__pyx_v_referencelengths, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 412; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_18 = __Pyx_PyInt_As_uint32_t(__pyx_t_9); if (unlikely((__pyx_t_18 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_18 = __Pyx_PyInt_As_uint32_t(__pyx_t_9); if (unlikely((__pyx_t_18 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 412; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_13 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_13 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_13 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_13 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 412; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         (__pyx_v_self->header->target_len[__pyx_t_13]) = __pyx_t_18;
 
-        /* "pysam/csamfile.pyx":419
+        /* "pysam/calignmentfile.pyx":413
  *                 for x from 0 <= x < self.header.n_targets:
  *                     self.header.target_len[x] = referencelengths[x]
  *                     name = referencenames[x]             # <<<<<<<<<<<<<<
  *                     self.header.target_name[x] = <char*>calloc(
  *                         len(name) + 1, sizeof(char))
  */
-        __pyx_t_9 = PyObject_GetItem(__pyx_v_referencenames, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 419; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_9 = PyObject_GetItem(__pyx_v_referencenames, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_XDECREF_SET(__pyx_v_name, __pyx_t_9);
         __pyx_t_9 = 0;
 
-        /* "pysam/csamfile.pyx":421
+        /* "pysam/calignmentfile.pyx":415
  *                     name = referencenames[x]
  *                     self.header.target_name[x] = <char*>calloc(
  *                         len(name) + 1, sizeof(char))             # <<<<<<<<<<<<<<
  *                     strncpy(self.header.target_name[x], name, len(name))
  * 
  */
-        __pyx_t_13 = PyObject_Length(__pyx_v_name); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_13 = PyObject_Length(__pyx_v_name); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-        /* "pysam/csamfile.pyx":420
+        /* "pysam/calignmentfile.pyx":414
  *                     self.header.target_len[x] = referencelengths[x]
  *                     name = referencenames[x]
  *                     self.header.target_name[x] = <char*>calloc(             # <<<<<<<<<<<<<<
  *                         len(name) + 1, sizeof(char))
  *                     strncpy(self.header.target_name[x], name, len(name))
  */
-        __pyx_t_12 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_12 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_12 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_12 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         (__pyx_v_self->header->target_name[__pyx_t_12]) = ((char *)calloc((__pyx_t_13 + 1), (sizeof(char))));
 
-        /* "pysam/csamfile.pyx":422
+        /* "pysam/calignmentfile.pyx":416
  *                     self.header.target_name[x] = <char*>calloc(
  *                         len(name) + 1, sizeof(char))
  *                     strncpy(self.header.target_name[x], name, len(name))             # <<<<<<<<<<<<<<
  * 
  *                 # Optionally, if there is no text, add a SAM
  */
-        __pyx_t_13 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_13 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_19 = __Pyx_PyObject_AsString(__pyx_v_name); if (unlikely((!__pyx_t_19) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_12 = PyObject_Length(__pyx_v_name); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_13 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_13 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_19 = __Pyx_PyObject_AsString(__pyx_v_name); if (unlikely((!__pyx_t_19) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_12 = PyObject_Length(__pyx_v_name); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         strncpy((__pyx_v_self->header->target_name[__pyx_t_13]), __pyx_t_19, __pyx_t_12);
-        __pyx_t_17 = __Pyx_PyInt_As_long(__pyx_v_x); if (unlikely((__pyx_t_17 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_17 = __Pyx_PyInt_As_long(__pyx_v_x); if (unlikely((__pyx_t_17 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
 
-      /* "pysam/csamfile.pyx":417
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
+      /* "pysam/calignmentfile.pyx":411
+ *                 self.header.target_len = <uint32_t*>calloc(
+ *                     n, sizeof(uint32_t))
  *                 for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
  *                     self.header.target_len[x] = referencelengths[x]
  *                     name = referencenames[x]
  */
-      __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
       __pyx_t_9 = 0;
 
-      /* "pysam/csamfile.pyx":426
+      /* "pysam/calignmentfile.pyx":420
  *                 # Optionally, if there is no text, add a SAM
  *                 # compatible header to output file.
  *                 if text is None and add_sq_text:             # <<<<<<<<<<<<<<
  *                     text = []
  *                     for x from 0 <= x < self.header.n_targets:
  */
-      __pyx_t_11 = (__pyx_v_text == Py_None);
-      if (__pyx_t_11) {
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_add_sq_text); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_1 = __pyx_t_2;
+      __pyx_t_1 = (__pyx_v_text == Py_None);
+      if (__pyx_t_1) {
+        __pyx_t_11 = __Pyx_PyObject_IsTrue(__pyx_v_add_sq_text); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __pyx_t_11;
       } else {
-        __pyx_t_1 = __pyx_t_11;
+        __pyx_t_2 = __pyx_t_1;
       }
-      if (__pyx_t_1) {
+      if (__pyx_t_2) {
 
-        /* "pysam/csamfile.pyx":427
+        /* "pysam/calignmentfile.pyx":421
  *                 # compatible header to output file.
  *                 if text is None and add_sq_text:
  *                     text = []             # <<<<<<<<<<<<<<
  *                     for x from 0 <= x < self.header.n_targets:
  *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
  */
-        __pyx_t_9 = PyList_New(0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyList_New(0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF_SET(__pyx_v_text, __pyx_t_9);
         __pyx_t_9 = 0;
 
-        /* "pysam/csamfile.pyx":428
+        /* "pysam/calignmentfile.pyx":422
  *                 if text is None and add_sq_text:
  *                     text = []
  *                     for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
@@ -6213,42 +6273,42 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
         __pyx_t_16 = __pyx_v_self->header->n_targets;
         for (__pyx_t_17 = 0; __pyx_t_17 < __pyx_t_16; __pyx_t_17++) {
-          __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_9);
           __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
           __pyx_t_9 = 0;
 
-          /* "pysam/csamfile.pyx":430
+          /* "pysam/calignmentfile.pyx":424
  *                     for x from 0 <= x < self.header.n_targets:
  *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
  *                                     (_forceStr(referencenames[x]),             # <<<<<<<<<<<<<<
  *                                      referencelengths[x]))
  *                     text = ''.join(text)
  */
-          __pyx_t_9 = PyObject_GetItem(__pyx_v_referencenames, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_9 = PyObject_GetItem(__pyx_v_referencenames, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_9);
-          __pyx_t_8 = __pyx_f_5pysam_8csamfile__forceStr(__pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = __pyx_f_5pysam_14calignmentfile__forceStr(__pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_8);
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-          /* "pysam/csamfile.pyx":431
+          /* "pysam/calignmentfile.pyx":425
  *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
  *                                     (_forceStr(referencenames[x]),
  *                                      referencelengths[x]))             # <<<<<<<<<<<<<<
  *                     text = ''.join(text)
  * 
  */
-          __pyx_t_9 = PyObject_GetItem(__pyx_v_referencelengths, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_9 = PyObject_GetItem(__pyx_v_referencelengths, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_9);
 
-          /* "pysam/csamfile.pyx":430
+          /* "pysam/calignmentfile.pyx":424
  *                     for x from 0 <= x < self.header.n_targets:
  *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
  *                                     (_forceStr(referencenames[x]),             # <<<<<<<<<<<<<<
  *                                      referencelengths[x]))
  *                     text = ''.join(text)
  */
-          __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_6);
           PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
           __Pyx_GIVEREF(__pyx_t_8);
@@ -6257,83 +6317,82 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
           __pyx_t_8 = 0;
           __pyx_t_9 = 0;
 
-          /* "pysam/csamfile.pyx":429
+          /* "pysam/calignmentfile.pyx":423
  *                     text = []
  *                     for x from 0 <= x < self.header.n_targets:
  *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \             # <<<<<<<<<<<<<<
  *                                     (_forceStr(referencenames[x]),
  *                                      referencelengths[x]))
  */
-          __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_SQ_SN_s_LN_s, __pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_SQ_SN_s_LN_s, __pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_9);
           __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          __pyx_t_20 = __Pyx_PyObject_Append(__pyx_v_text, __pyx_t_9); if (unlikely(__pyx_t_20 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_20 = __Pyx_PyObject_Append(__pyx_v_text, __pyx_t_9); if (unlikely(__pyx_t_20 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-          __pyx_t_17 = __Pyx_PyInt_As_long(__pyx_v_x); if (unlikely((__pyx_t_17 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_17 = __Pyx_PyInt_As_long(__pyx_v_x); if (unlikely((__pyx_t_17 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
 
-        /* "pysam/csamfile.pyx":428
+        /* "pysam/calignmentfile.pyx":422
  *                 if text is None and add_sq_text:
  *                     text = []
  *                     for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
  *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
  *                                     (_forceStr(referencenames[x]),
  */
-        __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
         __pyx_t_9 = 0;
 
-        /* "pysam/csamfile.pyx":432
+        /* "pysam/calignmentfile.pyx":426
  *                                     (_forceStr(referencenames[x]),
  *                                      referencelengths[x]))
  *                     text = ''.join(text)             # <<<<<<<<<<<<<<
  * 
- *                 if text != None:
+ *                 if text is not None:
  */
-        __pyx_t_9 = __Pyx_PyString_Join(__pyx_kp_s__9, __pyx_v_text); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyString_Join(__pyx_kp_s__7, __pyx_v_text); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF_SET(__pyx_v_text, __pyx_t_9);
         __pyx_t_9 = 0;
-        goto __pyx_L23;
+        goto __pyx_L24;
       }
-      __pyx_L23:;
+      __pyx_L24:;
 
-      /* "pysam/csamfile.pyx":434
+      /* "pysam/calignmentfile.pyx":428
  *                     text = ''.join(text)
  * 
- *                 if text != None:             # <<<<<<<<<<<<<<
+ *                 if text is not None:             # <<<<<<<<<<<<<<
  *                     # copy without \0
  *                     text = _forceBytes(text)
  */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_text, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+      __pyx_t_2 = (__pyx_v_text != Py_None);
+      __pyx_t_1 = (__pyx_t_2 != 0);
       if (__pyx_t_1) {
 
-        /* "pysam/csamfile.pyx":436
- *                 if text != None:
+        /* "pysam/calignmentfile.pyx":430
+ *                 if text is not None:
  *                     # copy without \0
  *                     text = _forceBytes(text)             # <<<<<<<<<<<<<<
  *                     ctext = text
  *                     self.header.l_text = strlen(ctext)
  */
-        __pyx_t_9 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_text); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_text); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF_SET(__pyx_v_text, __pyx_t_9);
         __pyx_t_9 = 0;
 
-        /* "pysam/csamfile.pyx":437
+        /* "pysam/calignmentfile.pyx":431
  *                     # copy without \0
  *                     text = _forceBytes(text)
  *                     ctext = text             # <<<<<<<<<<<<<<
  *                     self.header.l_text = strlen(ctext)
  *                     self.header.text = <char*>calloc(
  */
-        __pyx_t_21 = __Pyx_PyObject_AsString(__pyx_v_text); if (unlikely((!__pyx_t_21) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_21 = __Pyx_PyObject_AsString(__pyx_v_text); if (unlikely((!__pyx_t_21) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_v_ctext = __pyx_t_21;
 
-        /* "pysam/csamfile.pyx":438
+        /* "pysam/calignmentfile.pyx":432
  *                     text = _forceBytes(text)
  *                     ctext = text
  *                     self.header.l_text = strlen(ctext)             # <<<<<<<<<<<<<<
@@ -6342,7 +6401,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
         __pyx_v_self->header->l_text = strlen(__pyx_v_ctext);
 
-        /* "pysam/csamfile.pyx":439
+        /* "pysam/calignmentfile.pyx":433
  *                     ctext = text
  *                     self.header.l_text = strlen(ctext)
  *                     self.header.text = <char*>calloc(             # <<<<<<<<<<<<<<
@@ -6351,7 +6410,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
         __pyx_v_self->header->text = ((char *)calloc(strlen(__pyx_v_ctext), (sizeof(char))));
 
-        /* "pysam/csamfile.pyx":441
+        /* "pysam/calignmentfile.pyx":435
  *                     self.header.text = <char*>calloc(
  *                         strlen(ctext), sizeof(char))
  *                     memcpy(self.header.text, ctext, strlen(ctext))             # <<<<<<<<<<<<<<
@@ -6359,24 +6418,24 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  *             # open file. Header gets written to file at the same time for bam files
  */
         memcpy(__pyx_v_self->header->text, __pyx_v_ctext, strlen(__pyx_v_ctext));
-        goto __pyx_L26;
+        goto __pyx_L27;
       }
-      __pyx_L26:;
+      __pyx_L27:;
     }
-    __pyx_L16:;
+    __pyx_L17:;
 
-    /* "pysam/csamfile.pyx":445
+    /* "pysam/calignmentfile.pyx":439
  *             # open file. Header gets written to file at the same time for bam files
  *             # and sam files (in the latter case, the mode needs to be wh)
  *             self.htsfile = hts_open(filename, bmode)             # <<<<<<<<<<<<<<
  * 
  *             # for compatibility - "w" writes sam file without header
  */
-    __pyx_t_22 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_22) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_23 = __Pyx_PyObject_AsString(__pyx_v_bmode); if (unlikely((!__pyx_t_23) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_22 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_22) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_23 = __Pyx_PyObject_AsString(__pyx_v_bmode); if (unlikely((!__pyx_t_23) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_self->htsfile = hts_open(__pyx_t_22, __pyx_t_23);
 
-    /* "pysam/csamfile.pyx":448
+    /* "pysam/calignmentfile.pyx":442
  * 
  *             # for compatibility - "w" writes sam file without header
  *             if self.isbam or "h" in mode:             # <<<<<<<<<<<<<<
@@ -6384,14 +6443,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  *                 sam_hdr_write(self.htsfile, self.header)
  */
     if (!(__pyx_v_self->isbam != 0)) {
-      __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_n_s_h, __pyx_v_mode, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_11 = __pyx_t_1;
+      __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_n_s_h, __pyx_v_mode, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __pyx_t_1;
     } else {
-      __pyx_t_11 = (__pyx_v_self->isbam != 0);
+      __pyx_t_2 = (__pyx_v_self->isbam != 0);
     }
-    if (__pyx_t_11) {
+    if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":450
+      /* "pysam/calignmentfile.pyx":444
  *             if self.isbam or "h" in mode:
  *                 # write header to htsfile
  *                 sam_hdr_write(self.htsfile, self.header)             # <<<<<<<<<<<<<<
@@ -6399,36 +6458,36 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  *         elif mode[0] == "r":
  */
       sam_hdr_write(__pyx_v_self->htsfile, __pyx_v_self->header);
-      goto __pyx_L27;
+      goto __pyx_L28;
     }
-    __pyx_L27:;
-    goto __pyx_L15;
+    __pyx_L28:;
+    goto __pyx_L16;
   }
 
-  /* "pysam/csamfile.pyx":452
+  /* "pysam/calignmentfile.pyx":446
  *                 sam_hdr_write(self.htsfile, self.header)
  * 
  *         elif mode[0] == "r":             # <<<<<<<<<<<<<<
  *             # open file for reading
  *             if (filename != b"-"
  */
-  __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 452; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 452; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  if (__pyx_t_11) {
+  if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":454
+    /* "pysam/calignmentfile.pyx":448
  *         elif mode[0] == "r":
  *             # open file for reading
  *             if (filename != b"-"             # <<<<<<<<<<<<<<
  *                 and not self.isremote
  *                 and not os.path.exists(filename)):
  */
-    __pyx_t_11 = (__Pyx_PyBytes_Equals(__pyx_v_filename, __pyx_kp_b__6, Py_NE)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (__pyx_t_11) {
+    __pyx_t_2 = (__Pyx_PyBytes_Equals(__pyx_v_filename, __pyx_kp_b__4, Py_NE)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":455
+      /* "pysam/calignmentfile.pyx":449
  *             # open file for reading
  *             if (filename != b"-"
  *                 and not self.isremote             # <<<<<<<<<<<<<<
@@ -6438,77 +6497,77 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
       __pyx_t_1 = (!(__pyx_v_self->isremote != 0));
       if (__pyx_t_1) {
 
-        /* "pysam/csamfile.pyx":456
+        /* "pysam/calignmentfile.pyx":450
  *             if (filename != b"-"
  *                 and not self.isremote
  *                 and not os.path.exists(filename)):             # <<<<<<<<<<<<<<
  *                 raise IOError("file `%s` not found" % filename)
  * 
  */
-        __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_path); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_path); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_exists); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_exists); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         __Pyx_INCREF(__pyx_v_filename);
         PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_filename);
         __Pyx_GIVEREF(__pyx_v_filename);
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_24 = (!__pyx_t_2);
-        __pyx_t_2 = __pyx_t_24;
+        __pyx_t_24 = (!__pyx_t_11);
+        __pyx_t_11 = __pyx_t_24;
       } else {
-        __pyx_t_2 = __pyx_t_1;
+        __pyx_t_11 = __pyx_t_1;
       }
-      __pyx_t_1 = __pyx_t_2;
-    } else {
       __pyx_t_1 = __pyx_t_11;
+    } else {
+      __pyx_t_1 = __pyx_t_2;
     }
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":457
+      /* "pysam/calignmentfile.pyx":451
  *                 and not self.isremote
  *                 and not os.path.exists(filename)):
  *                 raise IOError("file `%s` not found" % filename)             # <<<<<<<<<<<<<<
  * 
  *             # try to detect errors
  */
-      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_s_not_found, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_s_not_found, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
       __Pyx_GIVEREF(__pyx_t_8);
       __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_Raise(__pyx_t_8, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":460
+    /* "pysam/calignmentfile.pyx":454
  * 
  *             # try to detect errors
  *             self.htsfile = hts_open(filename, bmode)             # <<<<<<<<<<<<<<
  *             if self.htsfile == NULL:
  *                 raise ValueError(
  */
-    __pyx_t_22 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_22) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 460; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_23 = __Pyx_PyObject_AsString(__pyx_v_bmode); if (unlikely((!__pyx_t_23) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 460; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_22 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_22) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_23 = __Pyx_PyObject_AsString(__pyx_v_bmode); if (unlikely((!__pyx_t_23) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_self->htsfile = hts_open(__pyx_t_22, __pyx_t_23);
 
-    /* "pysam/csamfile.pyx":461
+    /* "pysam/calignmentfile.pyx":455
  *             # try to detect errors
  *             self.htsfile = hts_open(filename, bmode)
  *             if self.htsfile == NULL:             # <<<<<<<<<<<<<<
@@ -6518,37 +6577,37 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
     __pyx_t_1 = ((__pyx_v_self->htsfile == NULL) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":464
+      /* "pysam/calignmentfile.pyx":458
  *                 raise ValueError(
  *                     "could not open file (mode='%s') - "
  *                     "is it SAM/BAM format?" % mode)             # <<<<<<<<<<<<<<
  * 
  *             # get file pointer
  */
-      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_file_mode_s_is_it, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 464; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_file_mode_s_is_it, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 458; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
 
-      /* "pysam/csamfile.pyx":462
+      /* "pysam/calignmentfile.pyx":456
  *             self.htsfile = hts_open(filename, bmode)
  *             if self.htsfile == NULL:
  *                 raise ValueError(             # <<<<<<<<<<<<<<
  *                     "could not open file (mode='%s') - "
  *                     "is it SAM/BAM format?" % mode)
  */
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
       __Pyx_GIVEREF(__pyx_t_8);
       __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_Raise(__pyx_t_8, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":469
+    /* "pysam/calignmentfile.pyx":463
  *             # TODO: this is specific to BAM files
  *             #       refactor to make generalizable
  *             self.fp = self.htsfile.fp.bgzf             # <<<<<<<<<<<<<<
@@ -6558,7 +6617,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
     __pyx_t_25 = __pyx_v_self->htsfile->fp.bgzf;
     __pyx_v_self->fp = __pyx_t_25;
 
-    /* "pysam/csamfile.pyx":472
+    /* "pysam/calignmentfile.pyx":466
  * 
  *             # bam files require a valid header
  *             if self.isbam:             # <<<<<<<<<<<<<<
@@ -6568,7 +6627,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
     __pyx_t_1 = (__pyx_v_self->isbam != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":473
+      /* "pysam/calignmentfile.pyx":467
  *             # bam files require a valid header
  *             if self.isbam:
  *                 self.header = sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
@@ -6577,7 +6636,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
       __pyx_v_self->header = sam_hdr_read(__pyx_v_self->htsfile);
 
-      /* "pysam/csamfile.pyx":474
+      /* "pysam/calignmentfile.pyx":468
  *             if self.isbam:
  *                 self.header = sam_hdr_read(self.htsfile)
  *                 if self.header == NULL:             # <<<<<<<<<<<<<<
@@ -6587,50 +6646,50 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
       __pyx_t_1 = ((__pyx_v_self->header == NULL) != 0);
       if (__pyx_t_1) {
 
-        /* "pysam/csamfile.pyx":477
+        /* "pysam/calignmentfile.pyx":471
  *                     raise ValueError(
  *                         "file does not have valid header (mode='%s') "
  *                         "- is it BAM format?" % mode )             # <<<<<<<<<<<<<<
  *             else:
  *                 # in sam files it is optional (htsfile full of unmapped reads)
  */
-        __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_does_not_have_valid_header, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_does_not_have_valid_header, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
 
-        /* "pysam/csamfile.pyx":475
+        /* "pysam/calignmentfile.pyx":469
  *                 self.header = sam_hdr_read(self.htsfile)
  *                 if self.header == NULL:
  *                     raise ValueError(             # <<<<<<<<<<<<<<
  *                         "file does not have valid header (mode='%s') "
  *                         "- is it BAM format?" % mode )
  */
-        __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 469; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
         __Pyx_GIVEREF(__pyx_t_8);
         __pyx_t_8 = 0;
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 469; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __Pyx_Raise(__pyx_t_8, 0, 0, 0);
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 469; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
-      goto __pyx_L30;
+      goto __pyx_L31;
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":480
+      /* "pysam/calignmentfile.pyx":474
  *             else:
  *                 # in sam files it is optional (htsfile full of unmapped reads)
  *                 if check_header:             # <<<<<<<<<<<<<<
  *                     self.header = sam_hdr_read(self.htsfile)
  *                     if self.header == NULL:
  */
-      __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_check_header); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_check_header); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 474; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       if (__pyx_t_1) {
 
-        /* "pysam/csamfile.pyx":481
+        /* "pysam/calignmentfile.pyx":475
  *                 # in sam files it is optional (htsfile full of unmapped reads)
  *                 if check_header:
  *                     self.header = sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
@@ -6639,7 +6698,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  */
         __pyx_v_self->header = sam_hdr_read(__pyx_v_self->htsfile);
 
-        /* "pysam/csamfile.pyx":482
+        /* "pysam/calignmentfile.pyx":476
  *                 if check_header:
  *                     self.header = sam_hdr_read(self.htsfile)
  *                     if self.header == NULL:             # <<<<<<<<<<<<<<
@@ -6649,141 +6708,141 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
         __pyx_t_1 = ((__pyx_v_self->header == NULL) != 0);
         if (__pyx_t_1) {
 
-          /* "pysam/csamfile.pyx":485
+          /* "pysam/calignmentfile.pyx":479
  *                         raise ValueError(
  *                             "file does not have valid header (mode='%s') "
  *                             "- is it SAM format?" % mode )             # <<<<<<<<<<<<<<
  *                     # self.header.ignore_sam_err = True
  * 
  */
-          __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_does_not_have_valid_header_2, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_does_not_have_valid_header_2, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_8);
 
-          /* "pysam/csamfile.pyx":483
+          /* "pysam/calignmentfile.pyx":477
  *                     self.header = sam_hdr_read(self.htsfile)
  *                     if self.header == NULL:
  *                         raise ValueError(             # <<<<<<<<<<<<<<
  *                             "file does not have valid header (mode='%s') "
  *                             "- is it SAM format?" % mode )
  */
-          __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_6);
           PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
           __Pyx_GIVEREF(__pyx_t_8);
           __pyx_t_8 = 0;
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_8);
           __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
           __Pyx_Raise(__pyx_t_8, 0, 0, 0);
           __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
-        goto __pyx_L32;
+        goto __pyx_L33;
       }
-      __pyx_L32:;
+      __pyx_L33:;
     }
-    __pyx_L30:;
+    __pyx_L31:;
 
-    /* "pysam/csamfile.pyx":490
+    /* "pysam/calignmentfile.pyx":484
  *             # disabled for autodetection to work needs to be disabled
  *             # so that reading from sam-files without headers works
  *             if check_sq and self.header.n_targets == 0:             # <<<<<<<<<<<<<<
  *                 raise ValueError(
  *                     ("file header is empty (mode='%s') - "
  */
-    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_check_sq); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_check_sq); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_1) {
-      __pyx_t_11 = (__pyx_v_self->header->n_targets == 0);
-      __pyx_t_2 = __pyx_t_11;
+      __pyx_t_2 = (__pyx_v_self->header->n_targets == 0);
+      __pyx_t_11 = __pyx_t_2;
     } else {
-      __pyx_t_2 = __pyx_t_1;
+      __pyx_t_11 = __pyx_t_1;
     }
-    if (__pyx_t_2) {
+    if (__pyx_t_11) {
 
-      /* "pysam/csamfile.pyx":493
+      /* "pysam/calignmentfile.pyx":487
  *                 raise ValueError(
  *                     ("file header is empty (mode='%s') - "
  *                      "is it SAM/BAM format?") % mode)             # <<<<<<<<<<<<<<
  * 
  *         if self.htsfile == NULL:
  */
-      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_header_is_empty_mode_s_is_i, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 493; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_header_is_empty_mode_s_is_i, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
 
-      /* "pysam/csamfile.pyx":491
+      /* "pysam/calignmentfile.pyx":485
  *             # so that reading from sam-files without headers works
  *             if check_sq and self.header.n_targets == 0:
  *                 raise ValueError(             # <<<<<<<<<<<<<<
  *                     ("file header is empty (mode='%s') - "
  *                      "is it SAM/BAM format?") % mode)
  */
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
       __Pyx_GIVEREF(__pyx_t_8);
       __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_Raise(__pyx_t_8, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
-    goto __pyx_L15;
+    goto __pyx_L16;
   }
-  __pyx_L15:;
+  __pyx_L16:;
 
-  /* "pysam/csamfile.pyx":495
+  /* "pysam/calignmentfile.pyx":489
  *                      "is it SAM/BAM format?") % mode)
  * 
  *         if self.htsfile == NULL:             # <<<<<<<<<<<<<<
  *             raise IOError("could not open file `%s`" % filename )
  * 
  */
-  __pyx_t_2 = ((__pyx_v_self->htsfile == NULL) != 0);
-  if (__pyx_t_2) {
+  __pyx_t_11 = ((__pyx_v_self->htsfile == NULL) != 0);
+  if (__pyx_t_11) {
 
-    /* "pysam/csamfile.pyx":496
+    /* "pysam/calignmentfile.pyx":490
  * 
  *         if self.htsfile == NULL:
  *             raise IOError("could not open file `%s`" % filename )             # <<<<<<<<<<<<<<
  * 
  *         # check for index and open if present
  */
-    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_file_s, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_file_s, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
     __Pyx_GIVEREF(__pyx_t_8);
     __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_Raise(__pyx_t_8, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":499
+  /* "pysam/calignmentfile.pyx":493
  * 
  *         # check for index and open if present
  *         if mode[0] == "r" and self.isbam:             # <<<<<<<<<<<<<<
  * 
  *             if not self.isremote:
  */
-  __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 493; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_8);
-  __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_8, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_8, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 493; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-  if (__pyx_t_2) {
+  if (__pyx_t_11) {
     __pyx_t_1 = (__pyx_v_self->isbam != 0);
   } else {
-    __pyx_t_1 = __pyx_t_2;
+    __pyx_t_1 = __pyx_t_11;
   }
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":501
+    /* "pysam/calignmentfile.pyx":495
  *         if mode[0] == "r" and self.isbam:
  * 
  *             if not self.isremote:             # <<<<<<<<<<<<<<
@@ -6793,76 +6852,76 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
     __pyx_t_1 = ((!(__pyx_v_self->isremote != 0)) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":502
+      /* "pysam/calignmentfile.pyx":496
  * 
  *             if not self.isremote:
  *                 if not os.path.exists(filename + b".bai") \             # <<<<<<<<<<<<<<
  *                         and not os.path.exists( filename[:-4] + b".bai"):
  *                     self.index = NULL
  */
-      __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_8, __pyx_n_s_path); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_8, __pyx_n_s_path); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_exists); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_exists); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = PyNumber_Add(__pyx_v_filename, __pyx_kp_b_bai); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyNumber_Add(__pyx_v_filename, __pyx_kp_b_bai); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_6);
       __Pyx_GIVEREF(__pyx_t_6);
       __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_9, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_9, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_2 = ((!__pyx_t_1) != 0);
-      if (__pyx_t_2) {
+      __pyx_t_11 = ((!__pyx_t_1) != 0);
+      if (__pyx_t_11) {
 
-        /* "pysam/csamfile.pyx":503
+        /* "pysam/calignmentfile.pyx":497
  *             if not self.isremote:
  *                 if not os.path.exists(filename + b".bai") \
  *                         and not os.path.exists( filename[:-4] + b".bai"):             # <<<<<<<<<<<<<<
  *                     self.index = NULL
  *                 else:
  */
-        __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_path); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_path); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_exists); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_exists); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_GetSlice(__pyx_v_filename, 0, -4, NULL, NULL, &__pyx_slice__10, 0, 1, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetSlice(__pyx_v_filename, 0, -4, NULL, NULL, &__pyx_slice__8, 0, 1, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_8 = PyNumber_Add(__pyx_t_9, __pyx_kp_b_bai); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PyNumber_Add(__pyx_t_9, __pyx_kp_b_bai); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
         __Pyx_GIVEREF(__pyx_t_8);
         __pyx_t_8 = 0;
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_11 = ((!__pyx_t_1) != 0);
-        __pyx_t_1 = __pyx_t_11;
-      } else {
+        __pyx_t_2 = ((!__pyx_t_1) != 0);
         __pyx_t_1 = __pyx_t_2;
+      } else {
+        __pyx_t_1 = __pyx_t_11;
       }
       if (__pyx_t_1) {
 
-        /* "pysam/csamfile.pyx":504
+        /* "pysam/calignmentfile.pyx":498
  *                 if not os.path.exists(filename + b".bai") \
  *                         and not os.path.exists( filename[:-4] + b".bai"):
  *                     self.index = NULL             # <<<<<<<<<<<<<<
@@ -6870,21 +6929,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
  *                     # returns NULL if there is no index or index could not be opened
  */
         __pyx_v_self->index = NULL;
-        goto __pyx_L38;
+        goto __pyx_L39;
       }
       /*else*/ {
 
-        /* "pysam/csamfile.pyx":507
+        /* "pysam/calignmentfile.pyx":501
  *                 else:
  *                     # returns NULL if there is no index or index could not be opened
  *                     self.index = hts_idx_load(filename, HTS_FMT_BAI)             # <<<<<<<<<<<<<<
  *                     if self.index == NULL:
  *                         raise IOError("error while opening index `%s` " % filename )
  */
-        __pyx_t_26 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_26) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_26 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_26) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_v_self->index = hts_idx_load(__pyx_t_26, HTS_FMT_BAI);
 
-        /* "pysam/csamfile.pyx":508
+        /* "pysam/calignmentfile.pyx":502
  *                     # returns NULL if there is no index or index could not be opened
  *                     self.index = hts_idx_load(filename, HTS_FMT_BAI)
  *                     if self.index == NULL:             # <<<<<<<<<<<<<<
@@ -6894,44 +6953,44 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
         __pyx_t_1 = ((__pyx_v_self->index == NULL) != 0);
         if (__pyx_t_1) {
 
-          /* "pysam/csamfile.pyx":509
+          /* "pysam/calignmentfile.pyx":503
  *                     self.index = hts_idx_load(filename, HTS_FMT_BAI)
  *                     if self.index == NULL:
  *                         raise IOError("error while opening index `%s` " % filename )             # <<<<<<<<<<<<<<
  *             else:
  *                 self.index = hts_idx_load(filename, HTS_FMT_BAI)
  */
-          __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_error_while_opening_index_s, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_error_while_opening_index_s, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_8);
-          __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_9);
           PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
           __Pyx_GIVEREF(__pyx_t_8);
           __pyx_t_8 = 0;
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_8);
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
           __Pyx_Raise(__pyx_t_8, 0, 0, 0);
           __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
       }
-      __pyx_L38:;
-      goto __pyx_L37;
+      __pyx_L39:;
+      goto __pyx_L38;
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":511
+      /* "pysam/calignmentfile.pyx":505
  *                         raise IOError("error while opening index `%s` " % filename )
  *             else:
  *                 self.index = hts_idx_load(filename, HTS_FMT_BAI)             # <<<<<<<<<<<<<<
  *                 if self.index == NULL:
  *                     warnings.warn("unable to open index for `%s` " % filename)
  */
-      __pyx_t_26 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_26) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_26 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_26) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_v_self->index = hts_idx_load(__pyx_t_26, HTS_FMT_BAI);
 
-      /* "pysam/csamfile.pyx":512
+      /* "pysam/calignmentfile.pyx":506
  *             else:
  *                 self.index = hts_idx_load(filename, HTS_FMT_BAI)
  *                 if self.index == NULL:             # <<<<<<<<<<<<<<
@@ -6941,37 +7000,37 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
       __pyx_t_1 = ((__pyx_v_self->index == NULL) != 0);
       if (__pyx_t_1) {
 
-        /* "pysam/csamfile.pyx":513
+        /* "pysam/calignmentfile.pyx":507
  *                 self.index = hts_idx_load(filename, HTS_FMT_BAI)
  *                 if self.index == NULL:
  *                     warnings.warn("unable to open index for `%s` " % filename)             # <<<<<<<<<<<<<<
  * 
  *             if not self.isstream:
  */
-        __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_warnings); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_warnings); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_8, __pyx_n_s_warn); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_8, __pyx_n_s_warn); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_unable_to_open_index_for_s, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_unable_to_open_index_for_s, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
         __Pyx_GIVEREF(__pyx_t_8);
         __pyx_t_8 = 0;
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        goto __pyx_L40;
+        goto __pyx_L41;
       }
-      __pyx_L40:;
+      __pyx_L41:;
     }
-    __pyx_L37:;
+    __pyx_L38:;
 
-    /* "pysam/csamfile.pyx":515
+    /* "pysam/calignmentfile.pyx":509
  *                     warnings.warn("unable to open index for `%s` " % filename)
  * 
  *             if not self.isstream:             # <<<<<<<<<<<<<<
@@ -6981,22 +7040,22 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
     __pyx_t_1 = ((!(__pyx_v_self->isstream != 0)) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":516
+      /* "pysam/calignmentfile.pyx":510
  * 
  *             if not self.isstream:
  *                 self.start_offset = bgzf_tell(self.fp)             # <<<<<<<<<<<<<<
  * 
- *     def gettid( self, reference ):
+ *     def gettid(self, reference):
  */
       __pyx_v_self->start_offset = bgzf_tell(__pyx_v_self->fp);
-      goto __pyx_L41;
+      goto __pyx_L42;
     }
-    __pyx_L41:;
-    goto __pyx_L36;
+    __pyx_L42:;
+    goto __pyx_L37;
   }
-  __pyx_L36:;
+  __pyx_L37:;
 
-  /* "pysam/csamfile.pyx":327
+  /* "pysam/calignmentfile.pyx":315
  *         return self.index != NULL
  * 
  *     def _open(self,             # <<<<<<<<<<<<<<
@@ -7012,7 +7071,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
   __Pyx_XDECREF(__pyx_t_7);
   __Pyx_XDECREF(__pyx_t_8);
   __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_msg);
@@ -7022,6 +7081,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
   __Pyx_XDECREF(__pyx_v_name);
   __Pyx_XDECREF(__pyx_v_ref);
   __Pyx_XDECREF(__pyx_v_filename);
+  __Pyx_XDECREF(__pyx_v_mode);
   __Pyx_XDECREF(__pyx_v_referencenames);
   __Pyx_XDECREF(__pyx_v_text);
   __Pyx_XGIVEREF(__pyx_r);
@@ -7030,29 +7090,29 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":518
+/* "pysam/calignmentfile.pyx":512
  *                 self.start_offset = bgzf_tell(self.fp)
  * 
- *     def gettid( self, reference ):             # <<<<<<<<<<<<<<
+ *     def gettid(self, reference):             # <<<<<<<<<<<<<<
  *         '''
  *         convert :term:`reference` name into numerical :term:`tid`
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_9gettid(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_8gettid[] = "Samfile.gettid(self, reference)\n\n        convert :term:`reference` name into numerical :term:`tid`\n\n        returns -1 if reference is not known.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_9gettid(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_9gettid(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_8gettid[] = "AlignmentFile.gettid(self, reference)\n\n        convert :term:`reference` name into numerical :term:`tid`\n\n        returns -1 if reference is not known.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_9gettid(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("gettid (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_8gettid(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((PyObject *)__pyx_v_reference));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_8gettid(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), ((PyObject *)__pyx_v_reference));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8gettid(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_8gettid(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -7065,71 +7125,71 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8gettid(struct __pyx_obj_5py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("gettid", 0);
-  __Pyx_TraceCall("gettid", __pyx_f[0], 518);
+  __Pyx_TraceCall("gettid", __pyx_f[0], 512);
   __Pyx_INCREF(__pyx_v_reference);
 
-  /* "pysam/csamfile.pyx":524
+  /* "pysam/calignmentfile.pyx":518
  *         returns -1 if reference is not known.
  *         '''
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
+ *             raise ValueError("I/O operation on closed file")
  *         reference = _forceBytes(reference)
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":525
+    /* "pysam/calignmentfile.pyx":519
  *         '''
  *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  *         reference = _forceBytes(reference)
  *         return bam_name2id(self.header, reference)
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__11, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":526
+  /* "pysam/calignmentfile.pyx":520
  *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )
+ *             raise ValueError("I/O operation on closed file")
  *         reference = _forceBytes(reference)             # <<<<<<<<<<<<<<
  *         return bam_name2id(self.header, reference)
  * 
  */
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_reference); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 526; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_reference); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF_SET(__pyx_v_reference, __pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":527
- *             raise ValueError( "I/O operation on closed file" )
+  /* "pysam/calignmentfile.pyx":521
+ *             raise ValueError("I/O operation on closed file")
  *         reference = _forceBytes(reference)
  *         return bam_name2id(self.header, reference)             # <<<<<<<<<<<<<<
  * 
- *     def getrname( self, tid ):
+ *     def getrname(self, tid):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_reference); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyInt_From_int(bam_name2id(__pyx_v_self->header, __pyx_t_5)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_reference); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_int(bam_name2id(__pyx_v_self->header, __pyx_t_5)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":518
+  /* "pysam/calignmentfile.pyx":512
  *                 self.start_offset = bgzf_tell(self.fp)
  * 
- *     def gettid( self, reference ):             # <<<<<<<<<<<<<<
+ *     def gettid(self, reference):             # <<<<<<<<<<<<<<
  *         '''
  *         convert :term:`reference` name into numerical :term:`tid`
  */
@@ -7138,7 +7198,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8gettid(struct __pyx_obj_5py
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.gettid", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.gettid", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_reference);
@@ -7148,29 +7208,29 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8gettid(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":529
+/* "pysam/calignmentfile.pyx":523
  *         return bam_name2id(self.header, reference)
  * 
- *     def getrname( self, tid ):             # <<<<<<<<<<<<<<
+ *     def getrname(self, tid):             # <<<<<<<<<<<<<<
  *         '''
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_11getrname(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_10getrname[] = "Samfile.getrname(self, tid)\n\n        convert numerical :term:`tid` into :term:`reference` name.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_11getrname(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_11getrname(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_10getrname[] = "AlignmentFile.getrname(self, tid)\n\n        convert numerical :term:`tid` into :term:`reference` name.";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_11getrname(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("getrname (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_10getrname(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((PyObject *)__pyx_v_tid));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_10getrname(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), ((PyObject *)__pyx_v_tid));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10getrname(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_tid) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_10getrname(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_tid) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -7183,69 +7243,69 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10getrname(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("getrname", 0);
-  __Pyx_TraceCall("getrname", __pyx_f[0], 529);
+  __Pyx_TraceCall("getrname", __pyx_f[0], 523);
 
-  /* "pysam/csamfile.pyx":532
+  /* "pysam/calignmentfile.pyx":526
  *         '''
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
+ *             raise ValueError("I/O operation on closed file")
  *         if not 0 <= tid < self.header.n_targets:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 532; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 526; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 532; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 526; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 532; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 526; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":533
+    /* "pysam/calignmentfile.pyx":527
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
+ *             raise ValueError("reference_id %i out of range 0<=tid<%i" %
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__12, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__10, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":534
+  /* "pysam/calignmentfile.pyx":528
  *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )
+ *             raise ValueError("I/O operation on closed file")
  *         if not 0 <= tid < self.header.n_targets:             # <<<<<<<<<<<<<<
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- *                              (tid, self.header.n_targets ) )
+ *             raise ValueError("reference_id %i out of range 0<=tid<%i" %
+ *                              (tid, self.header.n_targets))
  */
-  __pyx_t_2 = PyObject_RichCompare(__pyx_int_0, __pyx_v_tid, Py_LE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyObject_RichCompare(__pyx_int_0, __pyx_v_tid, Py_LE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__Pyx_PyObject_IsTrue(__pyx_t_2)) {
     __Pyx_DECREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyObject_RichCompare(__pyx_v_tid, __pyx_t_1, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyObject_RichCompare(__pyx_v_tid, __pyx_t_1, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   }
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_3 = ((!__pyx_t_4) != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":536
+    /* "pysam/calignmentfile.pyx":530
  *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- *                              (tid, self.header.n_targets ) )             # <<<<<<<<<<<<<<
+ *             raise ValueError("reference_id %i out of range 0<=tid<%i" %
+ *                              (tid, self.header.n_targets))             # <<<<<<<<<<<<<<
  *         return _charptr_to_str(self.header.target_name[tid])
  * 
  */
-    __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_v_tid);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_tid);
@@ -7254,48 +7314,48 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10getrname(struct __pyx_obj_
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/csamfile.pyx":535
- *             raise ValueError( "I/O operation on closed file" )
+    /* "pysam/calignmentfile.pyx":529
+ *             raise ValueError("I/O operation on closed file")
  *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %             # <<<<<<<<<<<<<<
- *                              (tid, self.header.n_targets ) )
+ *             raise ValueError("reference_id %i out of range 0<=tid<%i" %             # <<<<<<<<<<<<<<
+ *                              (tid, self.header.n_targets))
  *         return _charptr_to_str(self.header.target_name[tid])
  */
-    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_tid_i_out_of_range_0_tid_i, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_reference_id_i_out_of_range_0_ti, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 529; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 529; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 529; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 529; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":537
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- *                              (tid, self.header.n_targets ) )
+  /* "pysam/calignmentfile.pyx":531
+ *             raise ValueError("reference_id %i out of range 0<=tid<%i" %
+ *                              (tid, self.header.n_targets))
  *         return _charptr_to_str(self.header.target_name[tid])             # <<<<<<<<<<<<<<
  * 
- *     cdef char * _getrname( self, int tid ): # TODO unused
+ *     cdef char * _getrname(self, int tid):   # TODO unused
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_5 = __Pyx_PyIndex_AsSsize_t(__pyx_v_tid); if (unlikely((__pyx_t_5 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile__charptr_to_str((__pyx_v_self->header->target_name[__pyx_t_5])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyIndex_AsSsize_t(__pyx_v_tid); if (unlikely((__pyx_t_5 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __pyx_f_5pysam_14calignmentfile__charptr_to_str((__pyx_v_self->header->target_name[__pyx_t_5])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":529
+  /* "pysam/calignmentfile.pyx":523
  *         return bam_name2id(self.header, reference)
  * 
- *     def getrname( self, tid ):             # <<<<<<<<<<<<<<
+ *     def getrname(self, tid):             # <<<<<<<<<<<<<<
  *         '''
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  */
@@ -7304,7 +7364,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10getrname(struct __pyx_obj_
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.getrname", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.getrname", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -7313,15 +7373,15 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10getrname(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":539
+/* "pysam/calignmentfile.pyx":533
  *         return _charptr_to_str(self.header.target_name[tid])
  * 
- *     cdef char * _getrname( self, int tid ): # TODO unused             # <<<<<<<<<<<<<<
+ *     cdef char * _getrname(self, int tid):   # TODO unused             # <<<<<<<<<<<<<<
  *         '''
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  */
 
-static char *__pyx_f_5pysam_8csamfile_7Samfile__getrname(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, int __pyx_v_tid) {
+static char *__pyx_f_5pysam_14calignmentfile_13AlignmentFile__getrname(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, int __pyx_v_tid) {
   char *__pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -7334,40 +7394,40 @@ static char *__pyx_f_5pysam_8csamfile_7Samfile__getrname(struct __pyx_obj_5pysam
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_getrname", 0);
-  __Pyx_TraceCall("_getrname", __pyx_f[0], 539);
+  __Pyx_TraceCall("_getrname", __pyx_f[0], 533);
 
-  /* "pysam/csamfile.pyx":542
+  /* "pysam/calignmentfile.pyx":536
  *         '''
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError("I/O operation on closed file")
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":543
+    /* "pysam/calignmentfile.pyx":537
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  *         if not self._isOpen():
  *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  * 
  *         if not 0 <= tid < self.header.n_targets:
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__13, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__11, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":545
+  /* "pysam/calignmentfile.pyx":539
  *             raise ValueError("I/O operation on closed file")
  * 
  *         if not 0 <= tid < self.header.n_targets:             # <<<<<<<<<<<<<<
@@ -7381,18 +7441,18 @@ static char *__pyx_f_5pysam_8csamfile_7Samfile__getrname(struct __pyx_obj_5pysam
   __pyx_t_3 = ((!(__pyx_t_4 != 0)) != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":547
+    /* "pysam/calignmentfile.pyx":541
  *         if not 0 <= tid < self.header.n_targets:
  *             raise ValueError("tid %i out of range 0<=tid<%i" %
  *                              (tid, self.header.n_targets ))             # <<<<<<<<<<<<<<
  *         return self.header.target_name[tid]
  * 
  */
-    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
@@ -7401,30 +7461,30 @@ static char *__pyx_f_5pysam_8csamfile_7Samfile__getrname(struct __pyx_obj_5pysam
     __pyx_t_2 = 0;
     __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":546
+    /* "pysam/calignmentfile.pyx":540
  * 
  *         if not 0 <= tid < self.header.n_targets:
  *             raise ValueError("tid %i out of range 0<=tid<%i" %             # <<<<<<<<<<<<<<
  *                              (tid, self.header.n_targets ))
  *         return self.header.target_name[tid]
  */
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_tid_i_out_of_range_0_tid_i, __pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_tid_i_out_of_range_0_tid_i, __pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_Raise(__pyx_t_1, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":548
+  /* "pysam/calignmentfile.pyx":542
  *             raise ValueError("tid %i out of range 0<=tid<%i" %
  *                              (tid, self.header.n_targets ))
  *         return self.header.target_name[tid]             # <<<<<<<<<<<<<<
@@ -7434,10 +7494,10 @@ static char *__pyx_f_5pysam_8csamfile_7Samfile__getrname(struct __pyx_obj_5pysam
   __pyx_r = (__pyx_v_self->header->target_name[__pyx_v_tid]);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":539
+  /* "pysam/calignmentfile.pyx":533
  *         return _charptr_to_str(self.header.target_name[tid])
  * 
- *     cdef char * _getrname( self, int tid ): # TODO unused             # <<<<<<<<<<<<<<
+ *     cdef char * _getrname(self, int tid):   # TODO unused             # <<<<<<<<<<<<<<
  *         '''
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  */
@@ -7447,7 +7507,7 @@ static char *__pyx_f_5pysam_8csamfile_7Samfile__getrname(struct __pyx_obj_5pysam
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_WriteUnraisable("pysam.csamfile.Samfile._getrname", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __Pyx_WriteUnraisable("pysam.calignmentfile.AlignmentFile._getrname", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -7455,7 +7515,7 @@ static char *__pyx_f_5pysam_8csamfile_7Samfile__getrname(struct __pyx_obj_5pysam
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":550
+/* "pysam/calignmentfile.pyx":544
  *         return self.header.target_name[tid]
  * 
  *     def _parseRegion(self,             # <<<<<<<<<<<<<<
@@ -7464,9 +7524,9 @@ static char *__pyx_f_5pysam_8csamfile_7Samfile__getrname(struct __pyx_obj_5pysam
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_12_parseRegion[] = "Samfile._parseRegion(self, reference=None, start=None, end=None, region=None, tid=None)\n\n        parse region information.\n\n        raise ValueError for for invalid regions.\n\n        returns a tuple of flag, tid, start and end. Flag indicates\n        whether some coordinates were supplied.\n\n        Note that regions are 1-based, while start,end are python coordinates.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_13_parseRegion(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_12_parseRegion[] = "AlignmentFile._parseRegion(self, reference=None, start=None, end=None, region=None, tid=None)\nparse region information.\n\n        Raises ValueError for invalid regions.\n\n        Returns a tuple of a flag, :term:`tid`, start and end. The\n        flag indicates whether some coordinates were supplied.\n\n        Note that region strings are 1-based, while *start* and *end* denote\n        an interval in p [...]
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_13_parseRegion(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_reference = 0;
   PyObject *__pyx_v_start = 0;
   PyObject *__pyx_v_end = 0;
@@ -7482,7 +7542,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__p
     static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,&__pyx_n_s_tid,0};
     PyObject* values[5] = {0,0,0,0,0};
 
-    /* "pysam/csamfile.pyx":551
+    /* "pysam/calignmentfile.pyx":545
  * 
  *     def _parseRegion(self,
  *                      reference=None,             # <<<<<<<<<<<<<<
@@ -7491,7 +7551,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__p
  */
     values[0] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":552
+    /* "pysam/calignmentfile.pyx":546
  *     def _parseRegion(self,
  *                      reference=None,
  *                      start=None,             # <<<<<<<<<<<<<<
@@ -7500,7 +7560,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__p
  */
     values[1] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":553
+    /* "pysam/calignmentfile.pyx":547
  *                      reference=None,
  *                      start=None,
  *                      end=None,             # <<<<<<<<<<<<<<
@@ -7509,21 +7569,21 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__p
  */
     values[2] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":554
+    /* "pysam/calignmentfile.pyx":548
  *                      start=None,
  *                      end=None,
  *                      region=None,             # <<<<<<<<<<<<<<
  *                      tid=None):
- *         '''
+ *         '''parse region information.
  */
     values[3] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":555
+    /* "pysam/calignmentfile.pyx":549
  *                      end=None,
  *                      region=None,
  *                      tid=None):             # <<<<<<<<<<<<<<
- *         '''
- *         parse region information.
+ *         '''parse region information.
+ * 
  */
     values[4] = ((PyObject *)Py_None);
     if (unlikely(__pyx_kwds)) {
@@ -7567,7 +7627,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__p
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_parseRegion") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_parseRegion") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 544; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -7588,15 +7648,15 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__p
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_parseRegion", 0, 0, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("_parseRegion", 0, 0, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 544; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_tid);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_12_parseRegion(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_tid);
 
-  /* "pysam/csamfile.pyx":550
+  /* "pysam/calignmentfile.pyx":544
  *         return self.header.target_name[tid]
  * 
  *     def _parseRegion(self,             # <<<<<<<<<<<<<<
@@ -7609,7 +7669,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__p
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_12_parseRegion(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid) {
   int __pyx_v_rtid;
   PY_LONG_LONG __pyx_v_rstart;
   PY_LONG_LONG __pyx_v_rend;
@@ -7634,11 +7694,11 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_parseRegion", 0);
-  __Pyx_TraceCall("_parseRegion", __pyx_f[0], 550);
+  __Pyx_TraceCall("_parseRegion", __pyx_f[0], 544);
   __Pyx_INCREF(__pyx_v_reference);
   __Pyx_INCREF(__pyx_v_region);
 
-  /* "pysam/csamfile.pyx":570
+  /* "pysam/calignmentfile.pyx":565
  *         cdef long long rend
  * 
  *         rtid = -1             # <<<<<<<<<<<<<<
@@ -7647,7 +7707,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
  */
   __pyx_v_rtid = -1;
 
-  /* "pysam/csamfile.pyx":571
+  /* "pysam/calignmentfile.pyx":566
  * 
  *         rtid = -1
  *         rstart = 0             # <<<<<<<<<<<<<<
@@ -7656,28 +7716,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
  */
   __pyx_v_rstart = 0;
 
-  /* "pysam/csamfile.pyx":572
+  /* "pysam/calignmentfile.pyx":567
  *         rtid = -1
  *         rstart = 0
  *         rend = max_pos             # <<<<<<<<<<<<<<
  *         if start != None:
  *             try:
  */
-  __pyx_v_rend = __pyx_v_5pysam_8csamfile_max_pos;
+  __pyx_v_rend = __pyx_v_5pysam_14calignmentfile_max_pos;
 
-  /* "pysam/csamfile.pyx":573
+  /* "pysam/calignmentfile.pyx":568
  *         rstart = 0
  *         rend = max_pos
  *         if start != None:             # <<<<<<<<<<<<<<
  *             try:
  *                 rstart = start
  */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_start, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_v_start, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":574
+    /* "pysam/calignmentfile.pyx":569
  *         rend = max_pos
  *         if start != None:
  *             try:             # <<<<<<<<<<<<<<
@@ -7691,14 +7751,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
       __Pyx_XGOTREF(__pyx_t_5);
       /*try:*/ {
 
-        /* "pysam/csamfile.pyx":575
+        /* "pysam/calignmentfile.pyx":570
  *         if start != None:
  *             try:
  *                 rstart = start             # <<<<<<<<<<<<<<
  *             except OverflowError:
- *                 raise ValueError( 'start out of range (%i)' % start )
+ *                 raise ValueError('start out of range (%i)' % start)
  */
-        __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_v_start); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_v_start); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __pyx_v_rstart = __pyx_t_6;
       }
       __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -7708,41 +7768,41 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
       __pyx_L4_error:;
       __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":576
+      /* "pysam/calignmentfile.pyx":571
  *             try:
  *                 rstart = start
  *             except OverflowError:             # <<<<<<<<<<<<<<
- *                 raise ValueError( 'start out of range (%i)' % start )
+ *                 raise ValueError('start out of range (%i)' % start)
  * 
  */
       __pyx_t_7 = PyErr_ExceptionMatches(__pyx_builtin_OverflowError);
       if (__pyx_t_7) {
-        __Pyx_AddTraceback("pysam.csamfile.Samfile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_1, &__pyx_t_8, &__pyx_t_9) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
+        if (__Pyx_GetException(&__pyx_t_1, &__pyx_t_8, &__pyx_t_9) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_GOTREF(__pyx_t_9);
 
-        /* "pysam/csamfile.pyx":577
+        /* "pysam/calignmentfile.pyx":572
  *                 rstart = start
  *             except OverflowError:
- *                 raise ValueError( 'start out of range (%i)' % start )             # <<<<<<<<<<<<<<
+ *                 raise ValueError('start out of range (%i)' % start)             # <<<<<<<<<<<<<<
  * 
  *         if end != None:
  */
-        __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_start_out_of_range_i, __pyx_v_start); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_start_out_of_range_i, __pyx_v_start); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 572; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 572; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_11);
         PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_10);
         __Pyx_GIVEREF(__pyx_t_10);
         __pyx_t_10 = 0;
-        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 572; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         __Pyx_Raise(__pyx_t_10, 0, 0, 0);
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 572; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -7766,19 +7826,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":579
- *                 raise ValueError( 'start out of range (%i)' % start )
+  /* "pysam/calignmentfile.pyx":574
+ *                 raise ValueError('start out of range (%i)' % start)
  * 
  *         if end != None:             # <<<<<<<<<<<<<<
  *             try:
  *                 rend = end
  */
-  __pyx_t_9 = PyObject_RichCompare(__pyx_v_end, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = PyObject_RichCompare(__pyx_v_end, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":580
+    /* "pysam/calignmentfile.pyx":575
  * 
  *         if end != None:
  *             try:             # <<<<<<<<<<<<<<
@@ -7792,14 +7852,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
       __Pyx_XGOTREF(__pyx_t_3);
       /*try:*/ {
 
-        /* "pysam/csamfile.pyx":581
+        /* "pysam/calignmentfile.pyx":576
  *         if end != None:
  *             try:
  *                 rend = end             # <<<<<<<<<<<<<<
  *             except OverflowError:
- *                 raise ValueError( 'end out of range (%i)' % end )
+ *                 raise ValueError('end out of range (%i)' % end)
  */
-        __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_v_end); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L15_error;}
+        __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_v_end); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L15_error;}
         __pyx_v_rend = __pyx_t_6;
       }
       __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0;
@@ -7813,41 +7873,41 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
       __Pyx_XDECREF(__pyx_t_8); __pyx_t_8 = 0;
       __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-      /* "pysam/csamfile.pyx":582
+      /* "pysam/calignmentfile.pyx":577
  *             try:
  *                 rend = end
  *             except OverflowError:             # <<<<<<<<<<<<<<
- *                 raise ValueError( 'end out of range (%i)' % end )
+ *                 raise ValueError('end out of range (%i)' % end)
  * 
  */
       __pyx_t_7 = PyErr_ExceptionMatches(__pyx_builtin_OverflowError);
       if (__pyx_t_7) {
-        __Pyx_AddTraceback("pysam.csamfile.Samfile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_8, &__pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
+        __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
+        if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_8, &__pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_GOTREF(__pyx_t_1);
 
-        /* "pysam/csamfile.pyx":583
+        /* "pysam/calignmentfile.pyx":578
  *                 rend = end
  *             except OverflowError:
- *                 raise ValueError( 'end out of range (%i)' % end )             # <<<<<<<<<<<<<<
+ *                 raise ValueError('end out of range (%i)' % end)             # <<<<<<<<<<<<<<
  * 
  *         if region:
  */
-        __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_end_out_of_range_i, __pyx_v_end); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
+        __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_end_out_of_range_i, __pyx_v_end); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
+        __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
         __Pyx_GOTREF(__pyx_t_11);
         PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_10);
         __Pyx_GIVEREF(__pyx_t_10);
         __pyx_t_10 = 0;
-        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
+        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         __Pyx_Raise(__pyx_t_10, 0, 0, 0);
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -7871,124 +7931,124 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   }
   __pyx_L14:;
 
-  /* "pysam/csamfile.pyx":585
- *                 raise ValueError( 'end out of range (%i)' % end )
+  /* "pysam/calignmentfile.pyx":580
+ *                 raise ValueError('end out of range (%i)' % end)
  * 
  *         if region:             # <<<<<<<<<<<<<<
  *             region = _forceStr(region)
  *             parts = re.split("[:-]", region)
  */
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_region); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_region); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":586
+    /* "pysam/calignmentfile.pyx":581
  * 
  *         if region:
  *             region = _forceStr(region)             # <<<<<<<<<<<<<<
  *             parts = re.split("[:-]", region)
  *             reference = parts[0]
  */
-    __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceStr(__pyx_v_region); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __pyx_f_5pysam_14calignmentfile__forceStr(__pyx_v_region); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF_SET(__pyx_v_region, __pyx_t_1);
     __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":587
+    /* "pysam/calignmentfile.pyx":582
  *         if region:
  *             region = _forceStr(region)
  *             parts = re.split("[:-]", region)             # <<<<<<<<<<<<<<
  *             reference = parts[0]
  *             if len(parts) >= 2:
  */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_INCREF(__pyx_kp_s__14);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_kp_s__14);
-    __Pyx_GIVEREF(__pyx_kp_s__14);
+    __Pyx_INCREF(__pyx_kp_s__12);
+    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_kp_s__12);
+    __Pyx_GIVEREF(__pyx_kp_s__12);
     __Pyx_INCREF(__pyx_v_region);
     PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_region);
     __Pyx_GIVEREF(__pyx_v_region);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_v_parts = __pyx_t_9;
     __pyx_t_9 = 0;
 
-    /* "pysam/csamfile.pyx":588
+    /* "pysam/calignmentfile.pyx":583
  *             region = _forceStr(region)
  *             parts = re.split("[:-]", region)
  *             reference = parts[0]             # <<<<<<<<<<<<<<
  *             if len(parts) >= 2:
  *                 rstart = int(parts[1]) - 1
  */
-    __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_parts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_parts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF_SET(__pyx_v_reference, __pyx_t_9);
     __pyx_t_9 = 0;
 
-    /* "pysam/csamfile.pyx":589
+    /* "pysam/calignmentfile.pyx":584
  *             parts = re.split("[:-]", region)
  *             reference = parts[0]
  *             if len(parts) >= 2:             # <<<<<<<<<<<<<<
  *                 rstart = int(parts[1]) - 1
  *             if len(parts) >= 3:
  */
-    __pyx_t_12 = PyObject_Length(__pyx_v_parts); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_12 = PyObject_Length(__pyx_v_parts); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 584; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_2 = ((__pyx_t_12 >= 2) != 0);
     if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":590
+      /* "pysam/calignmentfile.pyx":585
  *             reference = parts[0]
  *             if len(parts) >= 2:
  *                 rstart = int(parts[1]) - 1             # <<<<<<<<<<<<<<
  *             if len(parts) >= 3:
  *                 rend = int(parts[2])
  */
-      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_parts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_parts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_1 = PyNumber_Int(__pyx_t_9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyNumber_Int(__pyx_t_9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_9 = PyNumber_Subtract(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyNumber_Subtract(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_t_9); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_t_9); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       __pyx_v_rstart = __pyx_t_6;
       goto __pyx_L26;
     }
     __pyx_L26:;
 
-    /* "pysam/csamfile.pyx":591
+    /* "pysam/calignmentfile.pyx":586
  *             if len(parts) >= 2:
  *                 rstart = int(parts[1]) - 1
  *             if len(parts) >= 3:             # <<<<<<<<<<<<<<
  *                 rend = int(parts[2])
  * 
  */
-    __pyx_t_12 = PyObject_Length(__pyx_v_parts); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_12 = PyObject_Length(__pyx_v_parts); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_2 = ((__pyx_t_12 >= 3) != 0);
     if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":592
+      /* "pysam/calignmentfile.pyx":587
  *                 rstart = int(parts[1]) - 1
  *             if len(parts) >= 3:
  *                 rend = int(parts[2])             # <<<<<<<<<<<<<<
  * 
  *         if not reference:
  */
-      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_parts, 2, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_parts, 2, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_1 = PyNumber_Int(__pyx_t_9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyNumber_Int(__pyx_t_9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_t_1); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_t_1); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __pyx_v_rend = __pyx_t_6;
       goto __pyx_L27;
@@ -7998,18 +8058,18 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   }
   __pyx_L25:;
 
-  /* "pysam/csamfile.pyx":594
+  /* "pysam/calignmentfile.pyx":589
  *                 rend = int(parts[2])
  * 
  *         if not reference:             # <<<<<<<<<<<<<<
  *             return 0, 0, 0, 0
  * 
  */
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_reference); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_reference); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_13 = ((!__pyx_t_2) != 0);
   if (__pyx_t_13) {
 
-    /* "pysam/csamfile.pyx":595
+    /* "pysam/calignmentfile.pyx":590
  * 
  *         if not reference:
  *             return 0, 0, 0, 0             # <<<<<<<<<<<<<<
@@ -8017,12 +8077,12 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
  *         if tid is not None:
  */
     __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_tuple__15);
-    __pyx_r = __pyx_tuple__15;
+    __Pyx_INCREF(__pyx_tuple__13);
+    __pyx_r = __pyx_tuple__13;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":597
+  /* "pysam/calignmentfile.pyx":592
  *             return 0, 0, 0, 0
  * 
  *         if tid is not None:             # <<<<<<<<<<<<<<
@@ -8033,44 +8093,44 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   __pyx_t_2 = (__pyx_t_13 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":598
+    /* "pysam/calignmentfile.pyx":593
  * 
  *         if tid is not None:
  *             rtid = tid             # <<<<<<<<<<<<<<
  *         else:
  *             rtid = self.gettid(reference)
  */
-    __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_v_tid); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_v_tid); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_rtid = __pyx_t_7;
     goto __pyx_L29;
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":600
+    /* "pysam/calignmentfile.pyx":595
  *             rtid = tid
  *         else:
  *             rtid = self.gettid(reference)             # <<<<<<<<<<<<<<
  * 
  *         if rtid < 0:
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_gettid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_gettid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_INCREF(__pyx_v_reference);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_reference);
     __Pyx_GIVEREF(__pyx_v_reference);
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_t_8); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_t_8); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
     __pyx_v_rtid = __pyx_t_7;
   }
   __pyx_L29:;
 
-  /* "pysam/csamfile.pyx":602
+  /* "pysam/calignmentfile.pyx":597
  *             rtid = self.gettid(reference)
  * 
  *         if rtid < 0:             # <<<<<<<<<<<<<<
@@ -8080,37 +8140,37 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   __pyx_t_2 = ((__pyx_v_rtid < 0) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":604
+    /* "pysam/calignmentfile.pyx":599
  *         if rtid < 0:
  *             raise ValueError(
  *                 "invalid reference `%s`" % reference)             # <<<<<<<<<<<<<<
  *         if rstart > rend:
  *             raise ValueError(
  */
-    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_invalid_reference_s, __pyx_v_reference); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_invalid_reference_s, __pyx_v_reference); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
 
-    /* "pysam/csamfile.pyx":603
+    /* "pysam/calignmentfile.pyx":598
  * 
  *         if rtid < 0:
  *             raise ValueError(             # <<<<<<<<<<<<<<
  *                 "invalid reference `%s`" % reference)
  *         if rstart > rend:
  */
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
     __Pyx_GIVEREF(__pyx_t_8);
     __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __Pyx_Raise(__pyx_t_8, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":605
+  /* "pysam/calignmentfile.pyx":600
  *             raise ValueError(
  *                 "invalid reference `%s`" % reference)
  *         if rstart > rend:             # <<<<<<<<<<<<<<
@@ -8120,18 +8180,18 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   __pyx_t_2 = ((__pyx_v_rstart > __pyx_v_rend) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":607
+    /* "pysam/calignmentfile.pyx":602
  *         if rstart > rend:
  *             raise ValueError(
  *                 'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))             # <<<<<<<<<<<<<<
  *         if not 0 <= rstart < max_pos:
  *             raise ValueError('start out of range (%i)' % rstart)
  */
-    __pyx_t_8 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rstart); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rstart); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 602; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_9 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rend); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rend); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 602; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 602; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_8);
     __Pyx_GIVEREF(__pyx_t_8);
@@ -8139,31 +8199,31 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
     __Pyx_GIVEREF(__pyx_t_9);
     __pyx_t_8 = 0;
     __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_invalid_coordinates_start_i_end, __pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_invalid_coordinates_start_i_end, __pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 602; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":606
+    /* "pysam/calignmentfile.pyx":601
  *                 "invalid reference `%s`" % reference)
  *         if rstart > rend:
  *             raise ValueError(             # <<<<<<<<<<<<<<
  *                 'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))
  *         if not 0 <= rstart < max_pos:
  */
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 601; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_9);
     __Pyx_GIVEREF(__pyx_t_9);
     __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 601; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_Raise(__pyx_t_9, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 601; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":608
+  /* "pysam/calignmentfile.pyx":603
  *             raise ValueError(
  *                 'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))
  *         if not 0 <= rstart < max_pos:             # <<<<<<<<<<<<<<
@@ -8172,37 +8232,37 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
  */
   __pyx_t_2 = (0 <= __pyx_v_rstart);
   if (__pyx_t_2) {
-    __pyx_t_2 = (__pyx_v_rstart < __pyx_v_5pysam_8csamfile_max_pos);
+    __pyx_t_2 = (__pyx_v_rstart < __pyx_v_5pysam_14calignmentfile_max_pos);
   }
   __pyx_t_13 = ((!(__pyx_t_2 != 0)) != 0);
   if (__pyx_t_13) {
 
-    /* "pysam/csamfile.pyx":609
+    /* "pysam/calignmentfile.pyx":604
  *                 'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))
  *         if not 0 <= rstart < max_pos:
  *             raise ValueError('start out of range (%i)' % rstart)             # <<<<<<<<<<<<<<
  *         if not 0 <= rend <= max_pos:
  *             raise ValueError('end out of range (%i)' % rend)
  */
-    __pyx_t_9 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rstart); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rstart); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_start_out_of_range_i, __pyx_t_9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_start_out_of_range_i, __pyx_t_9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __Pyx_Raise(__pyx_t_1, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":610
+  /* "pysam/calignmentfile.pyx":605
  *         if not 0 <= rstart < max_pos:
  *             raise ValueError('start out of range (%i)' % rstart)
  *         if not 0 <= rend <= max_pos:             # <<<<<<<<<<<<<<
@@ -8211,37 +8271,37 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
  */
   __pyx_t_13 = (0 <= __pyx_v_rend);
   if (__pyx_t_13) {
-    __pyx_t_13 = (__pyx_v_rend <= __pyx_v_5pysam_8csamfile_max_pos);
+    __pyx_t_13 = (__pyx_v_rend <= __pyx_v_5pysam_14calignmentfile_max_pos);
   }
   __pyx_t_2 = ((!(__pyx_t_13 != 0)) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":611
+    /* "pysam/calignmentfile.pyx":606
  *             raise ValueError('start out of range (%i)' % rstart)
  *         if not 0 <= rend <= max_pos:
  *             raise ValueError('end out of range (%i)' % rend)             # <<<<<<<<<<<<<<
  * 
  *         return 1, rtid, rstart, rend
  */
-    __pyx_t_1 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rend); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rend); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_end_out_of_range_i, __pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_end_out_of_range_i, __pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_9);
     __Pyx_GIVEREF(__pyx_t_9);
     __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_Raise(__pyx_t_9, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":613
+  /* "pysam/calignmentfile.pyx":608
  *             raise ValueError('end out of range (%i)' % rend)
  * 
  *         return 1, rtid, rstart, rend             # <<<<<<<<<<<<<<
@@ -8249,13 +8309,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
  *     def reset(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_9 = __Pyx_PyInt_From_int(__pyx_v_rtid); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyInt_From_int(__pyx_v_rtid); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_1 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rstart); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rstart); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_8 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rend); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rend); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
-  __pyx_t_10 = PyTuple_New(4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = PyTuple_New(4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   __Pyx_INCREF(__pyx_int_1);
   PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_int_1);
@@ -8273,7 +8333,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   __pyx_t_10 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":550
+  /* "pysam/calignmentfile.pyx":544
  *         return self.header.target_name[tid]
  * 
  *     def _parseRegion(self,             # <<<<<<<<<<<<<<
@@ -8288,7 +8348,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   __Pyx_XDECREF(__pyx_t_9);
   __Pyx_XDECREF(__pyx_t_10);
   __Pyx_XDECREF(__pyx_t_11);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_parts);
@@ -8300,29 +8360,29 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":615
+/* "pysam/calignmentfile.pyx":610
  *         return 1, rtid, rstart, rend
  * 
  *     def reset(self):             # <<<<<<<<<<<<<<
- *         '''reset file position to beginning of read section.'''
- *         return self.seek(self.start_offset, 0)
+ *         '''reset file position to beginning of file just after
+ *         the header.'''
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_15reset(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_14reset[] = "Samfile.reset(self)\nreset file position to beginning of read section.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_15reset(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_15reset(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_14reset[] = "AlignmentFile.reset(self)\nreset file position to beginning of file just after\n        the header.";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_15reset(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("reset (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_14reset(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_14reset(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_14reset(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_14reset(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -8333,21 +8393,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_14reset(struct __pyx_obj_5py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("reset", 0);
-  __Pyx_TraceCall("reset", __pyx_f[0], 615);
+  __Pyx_TraceCall("reset", __pyx_f[0], 610);
 
-  /* "pysam/csamfile.pyx":617
- *     def reset(self):
- *         '''reset file position to beginning of read section.'''
+  /* "pysam/calignmentfile.pyx":613
+ *         '''reset file position to beginning of file just after
+ *         the header.'''
  *         return self.seek(self.start_offset, 0)             # <<<<<<<<<<<<<<
  * 
  *     def seek(self, uint64_t offset, int where = 0):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_seek); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_seek); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int64_t(__pyx_v_self->start_offset); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_int64_t(__pyx_v_self->start_offset); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
@@ -8355,7 +8415,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_14reset(struct __pyx_obj_5py
   PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_int_0);
   __Pyx_GIVEREF(__pyx_int_0);
   __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -8363,12 +8423,12 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_14reset(struct __pyx_obj_5py
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":615
+  /* "pysam/calignmentfile.pyx":610
  *         return 1, rtid, rstart, rend
  * 
  *     def reset(self):             # <<<<<<<<<<<<<<
- *         '''reset file position to beginning of read section.'''
- *         return self.seek(self.start_offset, 0)
+ *         '''reset file position to beginning of file just after
+ *         the header.'''
  */
 
   /* function exit code */
@@ -8376,7 +8436,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_14reset(struct __pyx_obj_5py
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.reset", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.reset", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -8385,18 +8445,18 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_14reset(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":619
+/* "pysam/calignmentfile.pyx":615
  *         return self.seek(self.start_offset, 0)
  * 
  *     def seek(self, uint64_t offset, int where = 0):             # <<<<<<<<<<<<<<
  *         '''
- *         move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`.
+ *         move file pointer to position *offset*, see :meth:`pysam.AlignmentFile.tell`.
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_17seek(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_16seek[] = "Samfile.seek(self, uint64_t offset, int where=0)\n\n        move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_17seek(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_17seek(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_16seek[] = "AlignmentFile.seek(self, uint64_t offset, int where=0)\n\n        move file pointer to position *offset*, see :meth:`pysam.AlignmentFile.tell`.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_17seek(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   uint64_t __pyx_v_offset;
   int __pyx_v_where;
   int __pyx_lineno = 0;
@@ -8429,7 +8489,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_17seek(PyObject *__pyx_v_sel
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "seek") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "seek") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -8439,29 +8499,29 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_17seek(PyObject *__pyx_v_sel
         default: goto __pyx_L5_argtuple_error;
       }
     }
-    __pyx_v_offset = __Pyx_PyInt_As_uint64_t(values[0]); if (unlikely((__pyx_v_offset == (uint64_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_offset = __Pyx_PyInt_As_uint64_t(values[0]); if (unlikely((__pyx_v_offset == (uint64_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     if (values[1]) {
-      __pyx_v_where = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_where == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_where = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_where == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
       __pyx_v_where = ((int)0);
     }
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("seek", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("seek", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.seek", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.seek", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_16seek(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_offset, __pyx_v_where);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_16seek(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_offset, __pyx_v_where);
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_16seek(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, uint64_t __pyx_v_offset, int __pyx_v_where) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_16seek(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, uint64_t __pyx_v_offset, int __pyx_v_where) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -8473,40 +8533,40 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_16seek(struct __pyx_obj_5pys
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("seek", 0);
-  __Pyx_TraceCall("seek", __pyx_f[0], 619);
+  __Pyx_TraceCall("seek", __pyx_f[0], 615);
 
-  /* "pysam/csamfile.pyx":624
+  /* "pysam/calignmentfile.pyx":620
  *         '''
  * 
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError( "I/O operation on closed file" )
  *         if not self.isbam:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":625
+    /* "pysam/calignmentfile.pyx":621
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *         if not self.isbam:
  *             raise NotImplementedError("seek only available in bam files")
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__14, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":626
+  /* "pysam/calignmentfile.pyx":622
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )
  *         if not self.isbam:             # <<<<<<<<<<<<<<
@@ -8516,21 +8576,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_16seek(struct __pyx_obj_5pys
   __pyx_t_4 = ((!(__pyx_v_self->isbam != 0)) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":627
+    /* "pysam/calignmentfile.pyx":623
  *             raise ValueError( "I/O operation on closed file" )
  *         if not self.isbam:
  *             raise NotImplementedError("seek only available in bam files")             # <<<<<<<<<<<<<<
  *         if self.isstream:
  *             raise OSError("seek no available in streams")
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__15, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":628
+  /* "pysam/calignmentfile.pyx":624
  *         if not self.isbam:
  *             raise NotImplementedError("seek only available in bam files")
  *         if self.isstream:             # <<<<<<<<<<<<<<
@@ -8540,21 +8600,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_16seek(struct __pyx_obj_5pys
   __pyx_t_4 = (__pyx_v_self->isstream != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":629
+    /* "pysam/calignmentfile.pyx":625
  *             raise NotImplementedError("seek only available in bam files")
  *         if self.isstream:
  *             raise OSError("seek no available in streams")             # <<<<<<<<<<<<<<
  * 
  *         return bgzf_seek(self.fp, offset, where)
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_OSError, __pyx_tuple__18, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_OSError, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":631
+  /* "pysam/calignmentfile.pyx":627
  *             raise OSError("seek no available in streams")
  * 
  *         return bgzf_seek(self.fp, offset, where)             # <<<<<<<<<<<<<<
@@ -8562,25 +8622,25 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_16seek(struct __pyx_obj_5pys
  *     def tell(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_int64_t(bgzf_seek(__pyx_v_self->fp, __pyx_v_offset, __pyx_v_where)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_int64_t(bgzf_seek(__pyx_v_self->fp, __pyx_v_offset, __pyx_v_where)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":619
+  /* "pysam/calignmentfile.pyx":615
  *         return self.seek(self.start_offset, 0)
  * 
  *     def seek(self, uint64_t offset, int where = 0):             # <<<<<<<<<<<<<<
  *         '''
- *         move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`.
+ *         move file pointer to position *offset*, see :meth:`pysam.AlignmentFile.tell`.
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.seek", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.seek", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -8589,29 +8649,29 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_16seek(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":633
+/* "pysam/calignmentfile.pyx":629
  *         return bgzf_seek(self.fp, offset, where)
  * 
  *     def tell(self):             # <<<<<<<<<<<<<<
  *         '''
- *         return current file position
+ *         return current file position.
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_19tell(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_18tell[] = "Samfile.tell(self)\n\n        return current file position\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_19tell(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_19tell(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_18tell[] = "AlignmentFile.tell(self)\n\n        return current file position.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_19tell(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("tell (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_18tell(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_18tell(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_18tell(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_18tell(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -8623,42 +8683,42 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_18tell(struct __pyx_obj_5pys
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("tell", 0);
-  __Pyx_TraceCall("tell", __pyx_f[0], 633);
+  __Pyx_TraceCall("tell", __pyx_f[0], 629);
 
-  /* "pysam/csamfile.pyx":637
- *         return current file position
+  /* "pysam/calignmentfile.pyx":633
+ *         return current file position.
  *         '''
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
+ *             raise ValueError("I/O operation on closed file")
  *         if not self.isbam:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":638
+    /* "pysam/calignmentfile.pyx":634
  *         '''
  *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  *         if not self.isbam:
  *             raise NotImplementedError("seek only available in bam files")
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__19, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":639
+  /* "pysam/calignmentfile.pyx":635
  *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )
+ *             raise ValueError("I/O operation on closed file")
  *         if not self.isbam:             # <<<<<<<<<<<<<<
  *             raise NotImplementedError("seek only available in bam files")
  * 
@@ -8666,21 +8726,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_18tell(struct __pyx_obj_5pys
   __pyx_t_4 = ((!(__pyx_v_self->isbam != 0)) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":640
- *             raise ValueError( "I/O operation on closed file" )
+    /* "pysam/calignmentfile.pyx":636
+ *             raise ValueError("I/O operation on closed file")
  *         if not self.isbam:
  *             raise NotImplementedError("seek only available in bam files")             # <<<<<<<<<<<<<<
  * 
  *         return bgzf_tell(self.fp)
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__20, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__18, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":642
+  /* "pysam/calignmentfile.pyx":638
  *             raise NotImplementedError("seek only available in bam files")
  * 
  *         return bgzf_tell(self.fp)             # <<<<<<<<<<<<<<
@@ -8688,25 +8748,25 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_18tell(struct __pyx_obj_5pys
  *     def fetch(self,
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_int64_t(bgzf_tell(__pyx_v_self->fp)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_int64_t(bgzf_tell(__pyx_v_self->fp)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":633
+  /* "pysam/calignmentfile.pyx":629
  *         return bgzf_seek(self.fp, offset, where)
  * 
  *     def tell(self):             # <<<<<<<<<<<<<<
  *         '''
- *         return current file position
+ *         return current file position.
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.tell", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.tell", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -8715,7 +8775,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_18tell(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":644
+/* "pysam/calignmentfile.pyx":640
  *         return bgzf_tell(self.fp)
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
@@ -8724,9 +8784,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_18tell(struct __pyx_obj_5pys
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_20fetch[] = "Samfile.fetch(self, reference=None, start=None, end=None, region=None, tid=None, callback=None, until_eof=False, reopen=True)\nfetch aligned reads in a :term:`region` using 0-based indexing. The\n        region is specified by :term:`reference`, *start* and\n        *end*. Alternatively, a samtools :term:`region` string can be\n        supplied.\n\n        Without *reference* or *region* all mapped reads will be\n        fetche [...]
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_21fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_20fetch[] = "AlignmentFile.fetch(self, reference=None, start=None, end=None, region=None, tid=None, callback=None, until_eof=False, multiple_iterators=False)\nfetch aligned reads in a :term:`region` using 0-based indexing. The\n        region is specified by :term:`reference`, *start* and\n        *end*. Alternatively, a samtools :term:`region` string can be\n        supplied.\n\n        Without *reference* or *region* all map [...]
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_21fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_reference = 0;
   PyObject *__pyx_v_start = 0;
   PyObject *__pyx_v_end = 0;
@@ -8734,7 +8794,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
   PyObject *__pyx_v_tid = 0;
   PyObject *__pyx_v_callback = 0;
   PyObject *__pyx_v_until_eof = 0;
-  PyObject *__pyx_v_reopen = 0;
+  PyObject *__pyx_v_multiple_iterators = 0;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -8742,10 +8802,10 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("fetch (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,&__pyx_n_s_tid,&__pyx_n_s_callback,&__pyx_n_s_until_eof,&__pyx_n_s_reopen,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,&__pyx_n_s_tid,&__pyx_n_s_callback,&__pyx_n_s_until_eof,&__pyx_n_s_multiple_iterators,0};
     PyObject* values[8] = {0,0,0,0,0,0,0,0};
 
-    /* "pysam/csamfile.pyx":645
+    /* "pysam/calignmentfile.pyx":641
  * 
  *     def fetch(self,
  *               reference=None,             # <<<<<<<<<<<<<<
@@ -8754,7 +8814,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
  */
     values[0] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":646
+    /* "pysam/calignmentfile.pyx":642
  *     def fetch(self,
  *               reference=None,
  *               start=None,             # <<<<<<<<<<<<<<
@@ -8763,7 +8823,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
  */
     values[1] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":647
+    /* "pysam/calignmentfile.pyx":643
  *               reference=None,
  *               start=None,
  *               end=None,             # <<<<<<<<<<<<<<
@@ -8772,7 +8832,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
  */
     values[2] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":648
+    /* "pysam/calignmentfile.pyx":644
  *               start=None,
  *               end=None,
  *               region=None,             # <<<<<<<<<<<<<<
@@ -8781,7 +8841,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
  */
     values[3] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":649
+    /* "pysam/calignmentfile.pyx":645
  *               end=None,
  *               region=None,
  *               tid=None,             # <<<<<<<<<<<<<<
@@ -8790,32 +8850,32 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
  */
     values[4] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":650
+    /* "pysam/calignmentfile.pyx":646
  *               region=None,
  *               tid=None,
  *               callback=None,             # <<<<<<<<<<<<<<
  *               until_eof=False,
- *               reopen=True):
+ *               multiple_iterators=False):
  */
     values[5] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":651
+    /* "pysam/calignmentfile.pyx":647
  *               tid=None,
  *               callback=None,
  *               until_eof=False,             # <<<<<<<<<<<<<<
- *               reopen=True):
+ *               multiple_iterators=False):
  *         '''fetch aligned reads in a :term:`region` using 0-based indexing. The
  */
     values[6] = ((PyObject *)Py_False);
 
-    /* "pysam/csamfile.pyx":652
+    /* "pysam/calignmentfile.pyx":648
  *               callback=None,
  *               until_eof=False,
- *               reopen=True):             # <<<<<<<<<<<<<<
+ *               multiple_iterators=False):             # <<<<<<<<<<<<<<
  *         '''fetch aligned reads in a :term:`region` using 0-based indexing. The
  *         region is specified by :term:`reference`, *start* and
  */
-    values[7] = ((PyObject *)Py_True);
+    values[7] = ((PyObject *)Py_False);
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
       const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
@@ -8870,12 +8930,12 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
         }
         case  7:
         if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
           if (value) { values[7] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "fetch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "fetch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -8898,19 +8958,19 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
     __pyx_v_tid = values[4];
     __pyx_v_callback = values[5];
     __pyx_v_until_eof = values[6];
-    __pyx_v_reopen = values[7];
+    __pyx_v_multiple_iterators = values[7];
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("fetch", 0, 0, 8, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("fetch", 0, 0, 8, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_20fetch(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_tid, __pyx_v_callback, __pyx_v_until_eof, __pyx_v_reopen);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_20fetch(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_tid, __pyx_v_callback, __pyx_v_until_eof, __pyx_v_multiple_iterators);
 
-  /* "pysam/csamfile.pyx":644
+  /* "pysam/calignmentfile.pyx":640
  *         return bgzf_tell(self.fp)
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
@@ -8923,7 +8983,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_se
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid, PyObject *__pyx_v_callback, PyObject *__pyx_v_until_eof, PyObject *__pyx_v_reopen) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_20fetch(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid, PyObject *__pyx_v_callback, PyObject *__pyx_v_until_eof, PyObject *__pyx_v_multiple_iterators) {
   int __pyx_v_rtid;
   int __pyx_v_rstart;
   int __pyx_v_rend;
@@ -8950,58 +9010,58 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("fetch", 0);
-  __Pyx_TraceCall("fetch", __pyx_f[0], 644);
-  __Pyx_INCREF(__pyx_v_reopen);
+  __Pyx_TraceCall("fetch", __pyx_f[0], 640);
+  __Pyx_INCREF(__pyx_v_multiple_iterators);
 
-  /* "pysam/csamfile.pyx":685
+  /* "pysam/calignmentfile.pyx":678
  *         cdef int rtid, rstart, rend, has_coord
  * 
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError( "I/O operation on closed file" )
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":686
+    /* "pysam/calignmentfile.pyx":679
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         has_coord, rtid, rstart, rend = self._parseRegion(reference,
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__21, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__19, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 679; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 679; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":688
+  /* "pysam/calignmentfile.pyx":681
  *             raise ValueError( "I/O operation on closed file" )
  * 
  *         has_coord, rtid, rstart, rend = self._parseRegion(reference,             # <<<<<<<<<<<<<<
  *                                                           start,
  *                                                           end,
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_parseRegion); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_parseRegion); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
 
-  /* "pysam/csamfile.pyx":692
+  /* "pysam/calignmentfile.pyx":685
  *                                                           end,
  *                                                           region,
  *                                                           tid)             # <<<<<<<<<<<<<<
  * 
  *         # Turn of re-opening if htsfile is a stream
  */
-  __pyx_t_1 = PyTuple_New(5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_v_reference);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_reference);
@@ -9019,14 +9079,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
   PyTuple_SET_ITEM(__pyx_t_1, 4, __pyx_v_tid);
   __Pyx_GIVEREF(__pyx_v_tid);
 
-  /* "pysam/csamfile.pyx":688
+  /* "pysam/calignmentfile.pyx":681
  *             raise ValueError( "I/O operation on closed file" )
  * 
  *         has_coord, rtid, rstart, rend = self._parseRegion(reference,             # <<<<<<<<<<<<<<
  *                                                           start,
  *                                                           end,
  */
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -9040,7 +9100,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
     if (unlikely(size != 4)) {
       if (size > 4) __Pyx_RaiseTooManyValuesError(4);
       else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     #if CYTHON_COMPILING_IN_CPYTHON
     if (likely(PyTuple_CheckExact(sequence))) {
@@ -9063,7 +9123,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
       Py_ssize_t i;
       PyObject** temps[4] = {&__pyx_t_1,&__pyx_t_2,&__pyx_t_6,&__pyx_t_7};
       for (i=0; i < 4; i++) {
-        PyObject* item = PySequence_ITEM(sequence, i); if (unlikely(!item)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        PyObject* item = PySequence_ITEM(sequence, i); if (unlikely(!item)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(item);
         *(temps[i]) = item;
       }
@@ -9073,7 +9133,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
   } else {
     Py_ssize_t index = -1;
     PyObject** temps[4] = {&__pyx_t_1,&__pyx_t_2,&__pyx_t_6,&__pyx_t_7};
-    __pyx_t_8 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __pyx_t_9 = Py_TYPE(__pyx_t_8)->tp_iternext;
@@ -9082,7 +9142,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
       __Pyx_GOTREF(item);
       *(temps[index]) = item;
     }
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_9 = NULL;
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
     goto __pyx_L5_unpacking_done;
@@ -9090,71 +9150,71 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
     __pyx_t_9 = NULL;
     if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_L5_unpacking_done:;
   }
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_11 = __Pyx_PyInt_As_int(__pyx_t_2); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyInt_As_int(__pyx_t_2); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_12 = __Pyx_PyInt_As_int(__pyx_t_6); if (unlikely((__pyx_t_12 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = __Pyx_PyInt_As_int(__pyx_t_6); if (unlikely((__pyx_t_12 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_7); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_7); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
   __pyx_v_has_coord = __pyx_t_10;
   __pyx_v_rtid = __pyx_t_11;
   __pyx_v_rstart = __pyx_t_12;
   __pyx_v_rend = __pyx_t_13;
 
-  /* "pysam/csamfile.pyx":695
+  /* "pysam/calignmentfile.pyx":688
  * 
  *         # Turn of re-opening if htsfile is a stream
  *         if self.isstream:             # <<<<<<<<<<<<<<
- *             reopen = False
+ *             multiple_iterators = False
  * 
  */
   __pyx_t_4 = (__pyx_v_self->isstream != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":696
+    /* "pysam/calignmentfile.pyx":689
  *         # Turn of re-opening if htsfile is a stream
  *         if self.isstream:
- *             reopen = False             # <<<<<<<<<<<<<<
+ *             multiple_iterators = False             # <<<<<<<<<<<<<<
  * 
  *         if self.isbam:
  */
     __Pyx_INCREF(Py_False);
-    __Pyx_DECREF_SET(__pyx_v_reopen, Py_False);
+    __Pyx_DECREF_SET(__pyx_v_multiple_iterators, Py_False);
     goto __pyx_L6;
   }
   __pyx_L6:;
 
-  /* "pysam/csamfile.pyx":698
- *             reopen = False
+  /* "pysam/calignmentfile.pyx":691
+ *             multiple_iterators = False
  * 
  *         if self.isbam:             # <<<<<<<<<<<<<<
  *             if not until_eof and not self._hasIndex() and not self.isremote:
- *                 raise ValueError( "fetch called on bamfile without index" )
+ *                 raise ValueError("fetch called on bamfile without index")
  */
   __pyx_t_4 = (__pyx_v_self->isbam != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":699
+    /* "pysam/calignmentfile.pyx":692
  * 
  *         if self.isbam:
  *             if not until_eof and not self._hasIndex() and not self.isremote:             # <<<<<<<<<<<<<<
- *                 raise ValueError( "fetch called on bamfile without index" )
+ *                 raise ValueError("fetch called on bamfile without index")
  * 
  */
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_until_eof); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_until_eof); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_3 = ((!__pyx_t_4) != 0);
     if (__pyx_t_3) {
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __pyx_t_14 = ((!__pyx_t_4) != 0);
       if (__pyx_t_14) {
@@ -9169,45 +9229,61 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
     }
     if (__pyx_t_14) {
 
-      /* "pysam/csamfile.pyx":700
+      /* "pysam/calignmentfile.pyx":693
  *         if self.isbam:
  *             if not until_eof and not self._hasIndex() and not self.isremote:
- *                 raise ValueError( "fetch called on bamfile without index" )             # <<<<<<<<<<<<<<
+ *                 raise ValueError("fetch called on bamfile without index")             # <<<<<<<<<<<<<<
  * 
  *             if has_coord:
  */
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__22, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__20, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_Raise(__pyx_t_7, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":702
- *                 raise ValueError( "fetch called on bamfile without index" )
+    /* "pysam/calignmentfile.pyx":695
+ *                 raise ValueError("fetch called on bamfile without index")
  * 
  *             if has_coord:             # <<<<<<<<<<<<<<
- *                 return IteratorRowRegion(self, rtid, rstart, rend,
- *                                          reopen=reopen)
+ *                 return IteratorRowRegion(
+ *                     self, rtid, rstart, rend,
  */
     __pyx_t_14 = (__pyx_v_has_coord != 0);
     if (__pyx_t_14) {
 
-      /* "pysam/csamfile.pyx":703
+      /* "pysam/calignmentfile.pyx":696
  * 
  *             if has_coord:
- *                 return IteratorRowRegion(self, rtid, rstart, rend,             # <<<<<<<<<<<<<<
- *                                          reopen=reopen)
- *             else:
+ *                 return IteratorRowRegion(             # <<<<<<<<<<<<<<
+ *                     self, rtid, rstart, rend,
+ *                     multiple_iterators=multiple_iterators)
  */
       __Pyx_XDECREF(__pyx_r);
-      __pyx_t_7 = __Pyx_PyInt_From_int(__pyx_v_rtid); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+      /* "pysam/calignmentfile.pyx":697
+ *             if has_coord:
+ *                 return IteratorRowRegion(
+ *                     self, rtid, rstart, rend,             # <<<<<<<<<<<<<<
+ *                     multiple_iterators=multiple_iterators)
+ *             else:
+ */
+      __pyx_t_7 = __Pyx_PyInt_From_int(__pyx_v_rtid); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_5 = __Pyx_PyInt_From_int(__pyx_v_rstart); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyInt_From_int(__pyx_v_rstart); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+      /* "pysam/calignmentfile.pyx":696
+ * 
+ *             if has_coord:
+ *                 return IteratorRowRegion(             # <<<<<<<<<<<<<<
+ *                     self, rtid, rstart, rend,
+ *                     multiple_iterators=multiple_iterators)
+ */
+      __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_INCREF(((PyObject *)__pyx_v_self));
       PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
@@ -9221,26 +9297,26 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
       __pyx_t_7 = 0;
       __pyx_t_5 = 0;
       __pyx_t_6 = 0;
-      __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
 
-      /* "pysam/csamfile.pyx":704
- *             if has_coord:
- *                 return IteratorRowRegion(self, rtid, rstart, rend,
- *                                          reopen=reopen)             # <<<<<<<<<<<<<<
+      /* "pysam/calignmentfile.pyx":698
+ *                 return IteratorRowRegion(
+ *                     self, rtid, rstart, rend,
+ *                     multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
  *             else:
  *                 if until_eof:
  */
-      if (PyDict_SetItem(__pyx_t_6, __pyx_n_s_reopen, __pyx_v_reopen) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (PyDict_SetItem(__pyx_t_6, __pyx_n_s_multiple_iterators, __pyx_v_multiple_iterators) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-      /* "pysam/csamfile.pyx":703
+      /* "pysam/calignmentfile.pyx":696
  * 
  *             if has_coord:
- *                 return IteratorRowRegion(self, rtid, rstart, rend,             # <<<<<<<<<<<<<<
- *                                          reopen=reopen)
- *             else:
+ *                 return IteratorRowRegion(             # <<<<<<<<<<<<<<
+ *                     self, rtid, rstart, rend,
+ *                     multiple_iterators=multiple_iterators)
  */
-      __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowRegion)), __pyx_t_2, __pyx_t_6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion)), __pyx_t_2, __pyx_t_6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
@@ -9250,33 +9326,49 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":706
- *                                          reopen=reopen)
+      /* "pysam/calignmentfile.pyx":700
+ *                     multiple_iterators=multiple_iterators)
  *             else:
  *                 if until_eof:             # <<<<<<<<<<<<<<
- *                     return IteratorRowAll(self, reopen=reopen)
- *                 else:
+ *                     return IteratorRowAll(self,
+ *                                           multiple_iterators=multiple_iterators)
  */
-      __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_until_eof); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_until_eof); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       if (__pyx_t_14) {
 
-        /* "pysam/csamfile.pyx":707
+        /* "pysam/calignmentfile.pyx":701
  *             else:
  *                 if until_eof:
- *                     return IteratorRowAll(self, reopen=reopen)             # <<<<<<<<<<<<<<
+ *                     return IteratorRowAll(self,             # <<<<<<<<<<<<<<
+ *                                           multiple_iterators=multiple_iterators)
  *                 else:
- *                     # AH: check - reason why no reopen for AllRefs?
  */
         __Pyx_XDECREF(__pyx_r);
-        __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_INCREF(((PyObject *)__pyx_v_self));
         PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_v_self));
         __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-        __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
-        if (PyDict_SetItem(__pyx_t_6, __pyx_n_s_reopen, __pyx_v_reopen) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowAll)), __pyx_t_5, __pyx_t_6); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+        /* "pysam/calignmentfile.pyx":702
+ *                 if until_eof:
+ *                     return IteratorRowAll(self,
+ *                                           multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
+ *                 else:
+ *                     # AH: check - reason why no multiple_iterators for AllRefs?
+ */
+        if (PyDict_SetItem(__pyx_t_6, __pyx_n_s_multiple_iterators, __pyx_v_multiple_iterators) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+        /* "pysam/calignmentfile.pyx":701
+ *             else:
+ *                 if until_eof:
+ *                     return IteratorRowAll(self,             # <<<<<<<<<<<<<<
+ *                                           multiple_iterators=multiple_iterators)
+ *                 else:
+ */
+        __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRowAll)), __pyx_t_5, __pyx_t_6); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
@@ -9286,79 +9378,99 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
       }
       /*else*/ {
 
-        /* "pysam/csamfile.pyx":710
+        /* "pysam/calignmentfile.pyx":705
  *                 else:
- *                     # AH: check - reason why no reopen for AllRefs?
- *                     return IteratorRowAllRefs(self) # , reopen=reopen )             # <<<<<<<<<<<<<<
+ *                     # AH: check - reason why no multiple_iterators for AllRefs?
+ *                     return IteratorRowAllRefs(self,             # <<<<<<<<<<<<<<
+ *                                               multiple_iterators=multiple_iterators)
  *         else:
- *             if has_coord:
  */
         __Pyx_XDECREF(__pyx_r);
-        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_INCREF(((PyObject *)__pyx_v_self));
         PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
         __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-        __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowAllRefs)), __pyx_t_2, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
+
+        /* "pysam/calignmentfile.pyx":706
+ *                     # AH: check - reason why no multiple_iterators for AllRefs?
+ *                     return IteratorRowAllRefs(self,
+ *                                               multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
+ *         else:
+ *             if has_coord:
+ */
+        if (PyDict_SetItem(__pyx_t_6, __pyx_n_s_multiple_iterators, __pyx_v_multiple_iterators) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+        /* "pysam/calignmentfile.pyx":705
+ *                 else:
+ *                     # AH: check - reason why no multiple_iterators for AllRefs?
+ *                     return IteratorRowAllRefs(self,             # <<<<<<<<<<<<<<
+ *                                               multiple_iterators=multiple_iterators)
+ *         else:
+ */
+        __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRowAllRefs)), __pyx_t_2, __pyx_t_6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_5);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_r = __pyx_t_6;
-        __pyx_t_6 = 0;
+        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+        __pyx_r = __pyx_t_5;
+        __pyx_t_5 = 0;
         goto __pyx_L0;
       }
     }
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":712
- *                     return IteratorRowAllRefs(self) # , reopen=reopen )
+    /* "pysam/calignmentfile.pyx":708
+ *                                               multiple_iterators=multiple_iterators)
  *         else:
  *             if has_coord:             # <<<<<<<<<<<<<<
- *                 raise ValueError ("fetching by region is not available for sam files")
- * 
+ *                 raise ValueError(
+ *                     "fetching by region is not available for sam files")
  */
     __pyx_t_14 = (__pyx_v_has_coord != 0);
     if (__pyx_t_14) {
 
-      /* "pysam/csamfile.pyx":713
+      /* "pysam/calignmentfile.pyx":709
  *         else:
  *             if has_coord:
- *                 raise ValueError ("fetching by region is not available for sam files")             # <<<<<<<<<<<<<<
+ *                 raise ValueError(             # <<<<<<<<<<<<<<
+ *                     "fetching by region is not available for sam files")
  * 
- *             if callback:
  */
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__23, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__21, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
+      __Pyx_Raise(__pyx_t_5, 0, 0, 0);
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":715
- *                 raise ValueError ("fetching by region is not available for sam files")
+    /* "pysam/calignmentfile.pyx":712
+ *                     "fetching by region is not available for sam files")
  * 
  *             if callback:             # <<<<<<<<<<<<<<
  *                 raise NotImplementedError("callback not implemented yet")
  * 
  */
-    __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_callback); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_callback); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_14) {
 
-      /* "pysam/csamfile.pyx":716
+      /* "pysam/calignmentfile.pyx":713
  * 
  *             if callback:
  *                 raise NotImplementedError("callback not implemented yet")             # <<<<<<<<<<<<<<
  * 
  *             if self.header == NULL:
  */
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__24, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__22, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
+      __Pyx_Raise(__pyx_t_5, 0, 0, 0);
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":718
+    /* "pysam/calignmentfile.pyx":715
  *                 raise NotImplementedError("callback not implemented yet")
  * 
  *             if self.header == NULL:             # <<<<<<<<<<<<<<
@@ -9368,21 +9480,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
     __pyx_t_14 = ((__pyx_v_self->header == NULL) != 0);
     if (__pyx_t_14) {
 
-      /* "pysam/csamfile.pyx":719
+      /* "pysam/calignmentfile.pyx":716
  * 
  *             if self.header == NULL:
  *                 raise ValueError("fetch called for htsfile without header")             # <<<<<<<<<<<<<<
  * 
  *             # check if targets are defined
  */
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__25, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__23, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
+      __Pyx_Raise(__pyx_t_5, 0, 0, 0);
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":723
+    /* "pysam/calignmentfile.pyx":720
  *             # check if targets are defined
  *             # give warning, sam_read1 segfaults
  *             if self.header.n_targets == 0:             # <<<<<<<<<<<<<<
@@ -9392,52 +9504,68 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
     __pyx_t_14 = ((__pyx_v_self->header->n_targets == 0) != 0);
     if (__pyx_t_14) {
 
-      /* "pysam/csamfile.pyx":724
+      /* "pysam/calignmentfile.pyx":721
  *             # give warning, sam_read1 segfaults
  *             if self.header.n_targets == 0:
  *                 warnings.warn("fetch called for htsfile without header")             # <<<<<<<<<<<<<<
  * 
- *             return IteratorRowAll(self, reopen=reopen)
+ *             return IteratorRowAll(self,
  */
-      __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_warnings); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_warn); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__26, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_warnings); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
+      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_warn); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+      __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__24, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       goto __pyx_L14;
     }
     __pyx_L14:;
 
-    /* "pysam/csamfile.pyx":726
+    /* "pysam/calignmentfile.pyx":723
  *                 warnings.warn("fetch called for htsfile without header")
  * 
- *             return IteratorRowAll(self, reopen=reopen)             # <<<<<<<<<<<<<<
+ *             return IteratorRowAll(self,             # <<<<<<<<<<<<<<
+ *                                   multiple_iterators=multiple_iterators)
  * 
- *     def head(self, n):
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
+    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 723; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
     __Pyx_INCREF(((PyObject *)__pyx_v_self));
-    PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_v_self));
+    PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_v_self));
     __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-    __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 723; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_6);
+
+    /* "pysam/calignmentfile.pyx":724
+ * 
+ *             return IteratorRowAll(self,
+ *                                   multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
+ * 
+ *     def head(self, n, multiple_iterators=True):
+ */
+    if (PyDict_SetItem(__pyx_t_6, __pyx_n_s_multiple_iterators, __pyx_v_multiple_iterators) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 723; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/calignmentfile.pyx":723
+ *                 warnings.warn("fetch called for htsfile without header")
+ * 
+ *             return IteratorRowAll(self,             # <<<<<<<<<<<<<<
+ *                                   multiple_iterators=multiple_iterators)
+ * 
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRowAll)), __pyx_t_5, __pyx_t_6); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 723; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_reopen, __pyx_v_reopen) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowAll)), __pyx_t_6, __pyx_t_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
+    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_r = __pyx_t_5;
-    __pyx_t_5 = 0;
+    __pyx_r = __pyx_t_2;
+    __pyx_t_2 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":644
+  /* "pysam/calignmentfile.pyx":640
  *         return bgzf_tell(self.fp)
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
@@ -9453,56 +9581,108 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5py
   __Pyx_XDECREF(__pyx_t_6);
   __Pyx_XDECREF(__pyx_t_7);
   __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_reopen);
+  __Pyx_XDECREF(__pyx_v_multiple_iterators);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":728
- *             return IteratorRowAll(self, reopen=reopen)
+/* "pysam/calignmentfile.pyx":726
+ *                                   multiple_iterators=multiple_iterators)
+ * 
+ *     def head(self, n, multiple_iterators=True):             # <<<<<<<<<<<<<<
+ *         '''return iterator over the first n alignments.
  * 
- *     def head(self, n):             # <<<<<<<<<<<<<<
- *         '''
- *         return iterator over the first n alignments.
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_23head(PyObject *__pyx_v_self, PyObject *__pyx_v_n); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_22head[] = "Samfile.head(self, n)\n\n        return iterator over the first n alignments. \n\n        This is useful for inspecting the bam-file.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_23head(PyObject *__pyx_v_self, PyObject *__pyx_v_n) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_23head(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_22head[] = "AlignmentFile.head(self, n, multiple_iterators=True)\nreturn iterator over the first n alignments. \n\n        This is useful for inspecting the bam-file.\n\n        *multiple_iterators* is set to True by default in order to\n        avoid changing the current file position.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_23head(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  PyObject *__pyx_v_n = 0;
+  PyObject *__pyx_v_multiple_iterators = 0;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("head (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_22head(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((PyObject *)__pyx_v_n));
+  {
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_n,&__pyx_n_s_multiple_iterators,0};
+    PyObject* values[2] = {0,0};
+    values[1] = ((PyObject *)Py_True);
+    if (unlikely(__pyx_kwds)) {
+      Py_ssize_t kw_args;
+      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
+      switch (pos_args) {
+        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+      kw_args = PyDict_Size(__pyx_kwds);
+      switch (pos_args) {
+        case  0:
+        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_n)) != 0)) kw_args--;
+        else goto __pyx_L5_argtuple_error;
+        case  1:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
+          if (value) { values[1] = value; kw_args--; }
+        }
+      }
+      if (unlikely(kw_args > 0)) {
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "head") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+    } else {
+      switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+    }
+    __pyx_v_n = values[0];
+    __pyx_v_multiple_iterators = values[1];
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("head", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.head", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_RefNannyFinishContext();
+  return NULL;
+  __pyx_L4_argument_unpacking_done:;
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_22head(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_n, __pyx_v_multiple_iterators);
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_22head(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_n) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_22head(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_n, PyObject *__pyx_v_multiple_iterators) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("head", 0);
-  __Pyx_TraceCall("head", __pyx_f[0], 728);
+  __Pyx_TraceCall("head", __pyx_f[0], 726);
 
-  /* "pysam/csamfile.pyx":734
- *         This is useful for inspecting the bam-file.
+  /* "pysam/calignmentfile.pyx":734
+ *         avoid changing the current file position.
  *         '''
- *         return IteratorRowHead(self, n)             # <<<<<<<<<<<<<<
+ *         return IteratorRowHead(self, n,             # <<<<<<<<<<<<<<
+ *                                multiple_iterators=multiple_iterators)
  * 
- *     def mate(self,
  */
   __Pyx_XDECREF(__pyx_r);
   __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
@@ -9513,26 +9693,47 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_22head(struct __pyx_obj_5pys
   __Pyx_INCREF(__pyx_v_n);
   PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_n);
   __Pyx_GIVEREF(__pyx_v_n);
-  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowHead)), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
+
+  /* "pysam/calignmentfile.pyx":735
+ *         '''
+ *         return IteratorRowHead(self, n,
+ *                                multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
+ * 
+ *     def mate(self,
+ */
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_multiple_iterators, __pyx_v_multiple_iterators) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":734
+ *         avoid changing the current file position.
+ *         '''
+ *         return IteratorRowHead(self, n,             # <<<<<<<<<<<<<<
+ *                                multiple_iterators=multiple_iterators)
+ * 
+ */
+  __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRowHead)), __pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_r = __pyx_t_3;
+  __pyx_t_3 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":728
- *             return IteratorRowAll(self, reopen=reopen)
+  /* "pysam/calignmentfile.pyx":726
+ *                                   multiple_iterators=multiple_iterators)
+ * 
+ *     def head(self, n, multiple_iterators=True):             # <<<<<<<<<<<<<<
+ *         '''return iterator over the first n alignments.
  * 
- *     def head(self, n):             # <<<<<<<<<<<<<<
- *         '''
- *         return iterator over the first n alignments.
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.head", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.head", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -9541,26 +9742,26 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_22head(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":736
- *         return IteratorRowHead(self, n)
+/* "pysam/calignmentfile.pyx":737
+ *                                multiple_iterators=multiple_iterators)
  * 
  *     def mate(self,             # <<<<<<<<<<<<<<
- *              AlignedRead read):
- *         '''return the mate of :class:`AlignedRead` *read*.
+ *              AlignedSegment read):
+ *         '''return the mate of :class:`AlignedSegment` *read*.
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_25mate(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_24mate[] = "Samfile.mate(self, AlignedRead read)\nreturn the mate of :class:`AlignedRead` *read*.\n\n        Throws a ValueError if read is unpaired or the mate\n        is unmapped.\n\n        .. note::\n\n            Calling this method will change the file position.\n            This might interfere with any iterators that have\n            not re-opened the file.\n\n        .. note::\n  \n           This method is too slow for high-thro [...]
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_25mate(PyObject *__pyx_v_self, PyObject *__pyx_v_read) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_25mate(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_24mate[] = "AlignmentFile.mate(self, AlignedSegment read)\nreturn the mate of :class:`AlignedSegment` *read*.\n\n        Throws a ValueError if read is unpaired or the mate\n        is unmapped.\n\n        .. note::\n\n            Calling this method will change the file position.\n            This might interfere with any iterators that have\n            not re-opened the file.\n\n        .. note::\n  \n           This method [...]
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_25mate(PyObject *__pyx_v_self, PyObject *__pyx_v_read) {
   CYTHON_UNUSED int __pyx_lineno = 0;
   CYTHON_UNUSED const char *__pyx_filename = NULL;
   CYTHON_UNUSED int __pyx_clineno = 0;
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("mate (wrapper)", 0);
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_read), __pyx_ptype_5pysam_8csamfile_AlignedRead, 1, "read", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_24mate(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_read));
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_read), __pyx_ptype_5pysam_14calignmentfile_AlignedSegment, 1, "read", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_24mate(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), ((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_read));
 
   /* function exit code */
   goto __pyx_L0;
@@ -9571,7 +9772,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_25mate(PyObject *__pyx_v_sel
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_24mate(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_read) {
   uint32_t __pyx_v_flag;
   int __pyx_v_x;
   PyObject *__pyx_v_mate = NULL;
@@ -9592,9 +9793,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("mate", 0);
-  __Pyx_TraceCall("mate", __pyx_f[0], 736);
+  __Pyx_TraceCall("mate", __pyx_f[0], 737);
 
-  /* "pysam/csamfile.pyx":756
+  /* "pysam/calignmentfile.pyx":757
  * 
  *         '''
  *         cdef uint32_t flag = read._delegate.core.flag             # <<<<<<<<<<<<<<
@@ -9604,77 +9805,93 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
   __pyx_t_1 = __pyx_v_read->_delegate->core.flag;
   __pyx_v_flag = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":758
+  /* "pysam/calignmentfile.pyx":759
  *         cdef uint32_t flag = read._delegate.core.flag
  * 
  *         if flag & BAM_FPAIRED == 0:             # <<<<<<<<<<<<<<
- *             raise ValueError("read %s: is unpaired" % (read.qname))
- *         if flag & BAM_FMUNMAP != 0:
+ *             raise ValueError("read %s: is unpaired" %
+ *                              (read.query_name))
  */
   __pyx_t_2 = (((__pyx_v_flag & BAM_FPAIRED) == 0) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":759
- * 
+    /* "pysam/calignmentfile.pyx":761
  *         if flag & BAM_FPAIRED == 0:
- *             raise ValueError("read %s: is unpaired" % (read.qname))             # <<<<<<<<<<<<<<
+ *             raise ValueError("read %s: is unpaired" %
+ *                              (read.query_name))             # <<<<<<<<<<<<<<
  *         if flag & BAM_FMUNMAP != 0:
- *             raise ValueError("mate %s: is unmapped" % (read.qname))
+ *             raise ValueError("mate %s: is unmapped" %
  */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_read), __pyx_n_s_qname); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_read), __pyx_n_s_query_name); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_read_s_is_unpaired, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/calignmentfile.pyx":760
+ * 
+ *         if flag & BAM_FPAIRED == 0:
+ *             raise ValueError("read %s: is unpaired" %             # <<<<<<<<<<<<<<
+ *                              (read.query_name))
+ *         if flag & BAM_FMUNMAP != 0:
+ */
+    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_read_s_is_unpaired, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_4);
     __Pyx_GIVEREF(__pyx_t_4);
     __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_Raise(__pyx_t_4, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":760
- *         if flag & BAM_FPAIRED == 0:
- *             raise ValueError("read %s: is unpaired" % (read.qname))
+  /* "pysam/calignmentfile.pyx":762
+ *             raise ValueError("read %s: is unpaired" %
+ *                              (read.query_name))
  *         if flag & BAM_FMUNMAP != 0:             # <<<<<<<<<<<<<<
- *             raise ValueError("mate %s: is unmapped" % (read.qname))
- * 
+ *             raise ValueError("mate %s: is unmapped" %
+ *                              (read.query_name))
  */
   __pyx_t_2 = (((__pyx_v_flag & BAM_FMUNMAP) != 0) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":761
- *             raise ValueError("read %s: is unpaired" % (read.qname))
+    /* "pysam/calignmentfile.pyx":764
  *         if flag & BAM_FMUNMAP != 0:
- *             raise ValueError("mate %s: is unmapped" % (read.qname))             # <<<<<<<<<<<<<<
+ *             raise ValueError("mate %s: is unmapped" %
+ *                              (read.query_name))             # <<<<<<<<<<<<<<
  * 
  *         # xor flags to get the other mate
  */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_read), __pyx_n_s_qname); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_read), __pyx_n_s_query_name); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 764; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_mate_s_is_unmapped, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/calignmentfile.pyx":763
+ *                              (read.query_name))
+ *         if flag & BAM_FMUNMAP != 0:
+ *             raise ValueError("mate %s: is unmapped" %             # <<<<<<<<<<<<<<
+ *                              (read.query_name))
+ * 
+ */
+    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_mate_s_is_unmapped, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_3);
     __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":764
+  /* "pysam/calignmentfile.pyx":767
  * 
  *         # xor flags to get the other mate
  *         cdef int x = BAM_FREAD1 + BAM_FREAD2             # <<<<<<<<<<<<<<
@@ -9683,53 +9900,53 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
  */
   __pyx_v_x = (BAM_FREAD1 + BAM_FREAD2);
 
-  /* "pysam/csamfile.pyx":765
+  /* "pysam/calignmentfile.pyx":768
  *         # xor flags to get the other mate
  *         cdef int x = BAM_FREAD1 + BAM_FREAD2
  *         flag = (flag ^ x) & x             # <<<<<<<<<<<<<<
  * 
- *         # the following code is not using the C API and
+ *         # Make sure to use a separate file to jump around
  */
   __pyx_v_flag = ((__pyx_v_flag ^ __pyx_v_x) & __pyx_v_x);
 
-  /* "pysam/csamfile.pyx":769
- *         # the following code is not using the C API and
- *         # could thus be made much quicker
+  /* "pysam/calignmentfile.pyx":776
+ *         # could thus be made much quicker, for example
+ *         # by using tell and seek.
  *         for mate in self.fetch(             # <<<<<<<<<<<<<<
  *                 read._delegate.core.mpos,
  *                 read._delegate.core.mpos + 1,
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fetch); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fetch); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
 
-  /* "pysam/csamfile.pyx":770
- *         # could thus be made much quicker
+  /* "pysam/calignmentfile.pyx":777
+ *         # by using tell and seek.
  *         for mate in self.fetch(
  *                 read._delegate.core.mpos,             # <<<<<<<<<<<<<<
  *                 read._delegate.core.mpos + 1,
- *                 tid=read._delegate.core.mtid):
+ *                 tid=read._delegate.core.mtid,
  */
-  __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_read->_delegate->core.mpos); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_read->_delegate->core.mpos); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
 
-  /* "pysam/csamfile.pyx":771
+  /* "pysam/calignmentfile.pyx":778
  *         for mate in self.fetch(
  *                 read._delegate.core.mpos,
  *                 read._delegate.core.mpos + 1,             # <<<<<<<<<<<<<<
- *                 tid=read._delegate.core.mtid):
- *             if mate.flag & flag != 0 and \
+ *                 tid=read._delegate.core.mtid,
+ *                 multiple_iterators=True):
  */
-  __pyx_t_5 = __Pyx_PyInt_From_long((__pyx_v_read->_delegate->core.mpos + 1)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyInt_From_long((__pyx_v_read->_delegate->core.mpos + 1)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
 
-  /* "pysam/csamfile.pyx":769
- *         # the following code is not using the C API and
- *         # could thus be made much quicker
+  /* "pysam/calignmentfile.pyx":776
+ *         # could thus be made much quicker, for example
+ *         # by using tell and seek.
  *         for mate in self.fetch(             # <<<<<<<<<<<<<<
  *                 read._delegate.core.mpos,
  *                 read._delegate.core.mpos + 1,
  */
-  __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_4);
   __Pyx_GIVEREF(__pyx_t_4);
@@ -9737,29 +9954,38 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
   __Pyx_GIVEREF(__pyx_t_5);
   __pyx_t_4 = 0;
   __pyx_t_5 = 0;
-  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
 
-  /* "pysam/csamfile.pyx":772
+  /* "pysam/calignmentfile.pyx":779
  *                 read._delegate.core.mpos,
  *                 read._delegate.core.mpos + 1,
- *                 tid=read._delegate.core.mtid):             # <<<<<<<<<<<<<<
+ *                 tid=read._delegate.core.mtid,             # <<<<<<<<<<<<<<
+ *                 multiple_iterators=True):
  *             if mate.flag & flag != 0 and \
- *                mate.qname == read.qname:
  */
-  __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_read->_delegate->core.mtid); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_read->_delegate->core.mtid); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 779; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_tid, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_tid, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/csamfile.pyx":769
- *         # the following code is not using the C API and
- *         # could thus be made much quicker
+  /* "pysam/calignmentfile.pyx":780
+ *                 read._delegate.core.mpos + 1,
+ *                 tid=read._delegate.core.mtid,
+ *                 multiple_iterators=True):             # <<<<<<<<<<<<<<
+ *             if mate.flag & flag != 0 and \
+ *                mate.query_name == read.query_name:
+ */
+  if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_multiple_iterators, Py_True) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":776
+ *         # could thus be made much quicker, for example
+ *         # by using tell and seek.
  *         for mate in self.fetch(             # <<<<<<<<<<<<<<
  *                 read._delegate.core.mpos,
  *                 read._delegate.core.mpos + 1,
  */
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_6, __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_6, __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
@@ -9768,7 +9994,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
     __pyx_t_5 = __pyx_t_4; __Pyx_INCREF(__pyx_t_5); __pyx_t_7 = 0;
     __pyx_t_8 = NULL;
   } else {
-    __pyx_t_7 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __pyx_t_8 = Py_TYPE(__pyx_t_5)->tp_iternext;
   }
@@ -9777,16 +10003,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
     if (!__pyx_t_8 && PyList_CheckExact(__pyx_t_5)) {
       if (__pyx_t_7 >= PyList_GET_SIZE(__pyx_t_5)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_4 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_7); __Pyx_INCREF(__pyx_t_4); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_7); __Pyx_INCREF(__pyx_t_4); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_4 = PySequence_ITEM(__pyx_t_5, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PySequence_ITEM(__pyx_t_5, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_8 && PyTuple_CheckExact(__pyx_t_5)) {
       if (__pyx_t_7 >= PyTuple_GET_SIZE(__pyx_t_5)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_7); __Pyx_INCREF(__pyx_t_4); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_7); __Pyx_INCREF(__pyx_t_4); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_4 = PySequence_ITEM(__pyx_t_5, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PySequence_ITEM(__pyx_t_5, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_4 = __pyx_t_8(__pyx_t_5);
@@ -9794,7 +10020,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -9803,42 +10029,42 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
     __Pyx_XDECREF_SET(__pyx_v_mate, __pyx_t_4);
     __pyx_t_4 = 0;
 
-    /* "pysam/csamfile.pyx":773
- *                 read._delegate.core.mpos + 1,
- *                 tid=read._delegate.core.mtid):
+    /* "pysam/calignmentfile.pyx":781
+ *                 tid=read._delegate.core.mtid,
+ *                 multiple_iterators=True):
  *             if mate.flag & flag != 0 and \             # <<<<<<<<<<<<<<
- *                mate.qname == read.qname:
+ *                mate.query_name == read.query_name:
  *                 break
  */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_mate, __pyx_n_s_flag); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_mate, __pyx_n_s_flag); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_flag); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_flag); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = PyNumber_And(__pyx_t_4, __pyx_t_6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyNumber_And(__pyx_t_4, __pyx_t_6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":774
- *                 tid=read._delegate.core.mtid):
+      /* "pysam/calignmentfile.pyx":782
+ *                 multiple_iterators=True):
  *             if mate.flag & flag != 0 and \
- *                mate.qname == read.qname:             # <<<<<<<<<<<<<<
+ *                mate.query_name == read.query_name:             # <<<<<<<<<<<<<<
  *                 break
  *         else:
  */
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_mate, __pyx_n_s_qname); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_mate, __pyx_n_s_query_name); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_read), __pyx_n_s_qname); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_read), __pyx_n_s_query_name); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_4 = PyObject_RichCompare(__pyx_t_6, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyObject_RichCompare(__pyx_t_6, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_9 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
       __pyx_t_10 = __pyx_t_9;
     } else {
@@ -9846,9 +10072,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
     }
     if (__pyx_t_10) {
 
-      /* "pysam/csamfile.pyx":775
+      /* "pysam/calignmentfile.pyx":783
  *             if mate.flag & flag != 0 and \
- *                mate.qname == read.qname:
+ *                mate.query_name == read.query_name:
  *                 break             # <<<<<<<<<<<<<<
  *         else:
  *             raise ValueError("mate not found")
@@ -9858,23 +10084,23 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":777
+    /* "pysam/calignmentfile.pyx":785
  *                 break
  *         else:
  *             raise ValueError("mate not found")             # <<<<<<<<<<<<<<
  * 
  *         return mate
  */
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__27, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__25, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_Raise(__pyx_t_4, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
   __pyx_L6_break:;
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/csamfile.pyx":779
+  /* "pysam/calignmentfile.pyx":787
  *             raise ValueError("mate not found")
  * 
  *         return mate             # <<<<<<<<<<<<<<
@@ -9886,12 +10112,12 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
   __pyx_r = __pyx_v_mate;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":736
- *         return IteratorRowHead(self, n)
+  /* "pysam/calignmentfile.pyx":737
+ *                                multiple_iterators=multiple_iterators)
  * 
  *     def mate(self,             # <<<<<<<<<<<<<<
- *              AlignedRead read):
- *         '''return the mate of :class:`AlignedRead` *read*.
+ *              AlignedSegment read):
+ *         '''return the mate of :class:`AlignedSegment` *read*.
  */
 
   /* function exit code */
@@ -9900,7 +10126,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
   __Pyx_XDECREF(__pyx_t_4);
   __Pyx_XDECREF(__pyx_t_5);
   __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.mate", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.mate", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_mate);
@@ -9910,7 +10136,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":781
+/* "pysam/calignmentfile.pyx":789
  *         return mate
  * 
  *     def count(self,             # <<<<<<<<<<<<<<
@@ -9919,9 +10145,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pys
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_26count[] = "Samfile.count(self, reference=None, start=None, end=None, region=None, until_eof=False)\n*(reference = None, start = None, end = None,\n        region = None, callback = None, until_eof = False)*\n\n        count reads :term:`region` using 0-based indexing. The region\n        is specified by :term:`reference`, *start* and\n        *end*. Alternatively, a samtools :term:`region` string can be\n        supplied.\n\n        Note  [...]
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_27count(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_26count[] = "AlignmentFile.count(self, reference=None, start=None, end=None, region=None, until_eof=False)\n*(reference = None, start = None, end = None,\n        region = None, callback = None, until_eof = False)*\n\n        count reads :term:`region` using 0-based indexing. The region\n        is specified by :term:`reference`, *start* and\n        *end*. Alternatively, a samtools :term:`region` string can be\n        suppli [...]
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_27count(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_reference = 0;
   PyObject *__pyx_v_start = 0;
   PyObject *__pyx_v_end = 0;
@@ -9937,7 +10163,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_se
     static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,&__pyx_n_s_until_eof,0};
     PyObject* values[5] = {0,0,0,0,0};
 
-    /* "pysam/csamfile.pyx":782
+    /* "pysam/calignmentfile.pyx":790
  * 
  *     def count(self,
  *               reference=None,             # <<<<<<<<<<<<<<
@@ -9946,7 +10172,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_se
  */
     values[0] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":783
+    /* "pysam/calignmentfile.pyx":791
  *     def count(self,
  *               reference=None,
  *               start=None,             # <<<<<<<<<<<<<<
@@ -9955,7 +10181,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_se
  */
     values[1] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":784
+    /* "pysam/calignmentfile.pyx":792
  *               reference=None,
  *               start=None,
  *               end=None,             # <<<<<<<<<<<<<<
@@ -9964,7 +10190,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_se
  */
     values[2] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":785
+    /* "pysam/calignmentfile.pyx":793
  *               start=None,
  *               end=None,
  *               region=None,             # <<<<<<<<<<<<<<
@@ -9973,7 +10199,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_se
  */
     values[3] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":786
+    /* "pysam/calignmentfile.pyx":794
  *               end=None,
  *               region=None,
  *               until_eof=False):             # <<<<<<<<<<<<<<
@@ -10022,7 +10248,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_se
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "count") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "count") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 789; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -10043,15 +10269,15 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_se
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("count", 0, 0, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("count", 0, 0, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 789; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.count", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.count", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_26count(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_until_eof);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_26count(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_until_eof);
 
-  /* "pysam/csamfile.pyx":781
+  /* "pysam/calignmentfile.pyx":789
  *         return mate
  * 
  *     def count(self,             # <<<<<<<<<<<<<<
@@ -10064,8 +10290,8 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_se
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_until_eof) {
-  CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read = 0;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_26count(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_until_eof) {
+  CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_read = 0;
   long __pyx_v_counter;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -10081,105 +10307,105 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("count", 0);
-  __Pyx_TraceCall("count", __pyx_f[0], 781);
+  __Pyx_TraceCall("count", __pyx_f[0], 789);
 
-  /* "pysam/csamfile.pyx":799
+  /* "pysam/calignmentfile.pyx":807
  *         '''
- *         cdef AlignedRead read
+ *         cdef AlignedSegment read
  *         cdef long counter = 0             # <<<<<<<<<<<<<<
  * 
  *         if not self._isOpen():
  */
   __pyx_v_counter = 0;
 
-  /* "pysam/csamfile.pyx":801
+  /* "pysam/calignmentfile.pyx":809
  *         cdef long counter = 0
  * 
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError( "I/O operation on closed file" )
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":802
+    /* "pysam/calignmentfile.pyx":810
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         for read in self.fetch(reference=reference,
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__28, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__26, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":804
+  /* "pysam/calignmentfile.pyx":812
  *             raise ValueError( "I/O operation on closed file" )
  * 
  *         for read in self.fetch(reference=reference,             # <<<<<<<<<<<<<<
  *                                start=start,
  *                                end=end,
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fetch); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fetch); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_reference, __pyx_v_reference) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_reference, __pyx_v_reference) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":805
+  /* "pysam/calignmentfile.pyx":813
  * 
  *         for read in self.fetch(reference=reference,
  *                                start=start,             # <<<<<<<<<<<<<<
  *                                end=end,
  *                                region=region,
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_start, __pyx_v_start) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_start, __pyx_v_start) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":806
+  /* "pysam/calignmentfile.pyx":814
  *         for read in self.fetch(reference=reference,
  *                                start=start,
  *                                end=end,             # <<<<<<<<<<<<<<
  *                                region=region,
  *                                until_eof=until_eof):
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_end, __pyx_v_end) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_end, __pyx_v_end) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":807
+  /* "pysam/calignmentfile.pyx":815
  *                                start=start,
  *                                end=end,
  *                                region=region,             # <<<<<<<<<<<<<<
  *                                until_eof=until_eof):
  *             counter += 1
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_region, __pyx_v_region) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_region, __pyx_v_region) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":808
+  /* "pysam/calignmentfile.pyx":816
  *                                end=end,
  *                                region=region,
  *                                until_eof=until_eof):             # <<<<<<<<<<<<<<
  *             counter += 1
  * 
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_until_eof, __pyx_v_until_eof) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_until_eof, __pyx_v_until_eof) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":804
+  /* "pysam/calignmentfile.pyx":812
  *             raise ValueError( "I/O operation on closed file" )
  * 
  *         for read in self.fetch(reference=reference,             # <<<<<<<<<<<<<<
  *                                start=start,
  *                                end=end,
  */
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, __pyx_t_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, __pyx_t_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -10187,7 +10413,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5py
     __pyx_t_1 = __pyx_t_5; __Pyx_INCREF(__pyx_t_1); __pyx_t_6 = 0;
     __pyx_t_7 = NULL;
   } else {
-    __pyx_t_6 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_t_7 = Py_TYPE(__pyx_t_1)->tp_iternext;
   }
@@ -10196,16 +10422,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5py
     if (!__pyx_t_7 && PyList_CheckExact(__pyx_t_1)) {
       if (__pyx_t_6 >= PyList_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_5 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_6); __Pyx_INCREF(__pyx_t_5); __pyx_t_6++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_6); __Pyx_INCREF(__pyx_t_5); __pyx_t_6++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_5 = PySequence_ITEM(__pyx_t_1, __pyx_t_6); __pyx_t_6++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PySequence_ITEM(__pyx_t_1, __pyx_t_6); __pyx_t_6++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_7 && PyTuple_CheckExact(__pyx_t_1)) {
       if (__pyx_t_6 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_5 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_6); __Pyx_INCREF(__pyx_t_5); __pyx_t_6++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_6); __Pyx_INCREF(__pyx_t_5); __pyx_t_6++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_5 = PySequence_ITEM(__pyx_t_1, __pyx_t_6); __pyx_t_6++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PySequence_ITEM(__pyx_t_1, __pyx_t_6); __pyx_t_6++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_5 = __pyx_t_7(__pyx_t_1);
@@ -10213,17 +10439,17 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5py
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
       __Pyx_GOTREF(__pyx_t_5);
     }
-    if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5pysam_8csamfile_AlignedRead))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_XDECREF_SET(__pyx_v_read, ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_t_5));
+    if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5pysam_14calignmentfile_AlignedSegment))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_XDECREF_SET(__pyx_v_read, ((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_t_5));
     __pyx_t_5 = 0;
 
-    /* "pysam/csamfile.pyx":809
+    /* "pysam/calignmentfile.pyx":817
  *                                region=region,
  *                                until_eof=until_eof):
  *             counter += 1             # <<<<<<<<<<<<<<
@@ -10234,7 +10460,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5py
   }
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":811
+  /* "pysam/calignmentfile.pyx":819
  *             counter += 1
  * 
  *         return counter             # <<<<<<<<<<<<<<
@@ -10242,13 +10468,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5py
  *     def pileup( self,
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_long(__pyx_v_counter); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_long(__pyx_v_counter); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":781
+  /* "pysam/calignmentfile.pyx":789
  *         return mate
  * 
  *     def count(self,             # <<<<<<<<<<<<<<
@@ -10261,7 +10487,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5py
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.count", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.count", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF((PyObject *)__pyx_v_read);
@@ -10271,7 +10497,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":813
+/* "pysam/calignmentfile.pyx":821
  *         return counter
  * 
  *     def pileup( self,             # <<<<<<<<<<<<<<
@@ -10280,9 +10506,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5py
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_28pileup[] = "Samfile.pileup(self, reference=None, start=None, end=None, region=None, **kwargs)\n\n        perform a :term:`pileup` within a :term:`region`. The region is specified by\n        :term:`reference`, *start* and *end* (using 0-based indexing).\n        Alternatively, a samtools *region* string can be supplied.\n\n        Without *reference* or *region* all reads will be used for the pileup. The reads will be returned\n        or [...]
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_29pileup(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_28pileup[] = "AlignmentFile.pileup(self, reference=None, start=None, end=None, region=None, **kwargs)\nperform a :term:`pileup` within a :term:`region`. The region is\n        specified by :term:`reference`, *start* and *end* (using\n        0-based indexing).  Alternatively, a samtools *region* string\n        can be supplied.\n\n        Without *reference* or *region* all reads will be used for the\n        pileup. The reads [...]
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_29pileup(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_reference = 0;
   PyObject *__pyx_v_start = 0;
   PyObject *__pyx_v_end = 0;
@@ -10300,7 +10526,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_s
     static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,0};
     PyObject* values[4] = {0,0,0,0};
 
-    /* "pysam/csamfile.pyx":814
+    /* "pysam/calignmentfile.pyx":822
  * 
  *     def pileup( self,
  *                 reference = None,             # <<<<<<<<<<<<<<
@@ -10309,7 +10535,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_s
  */
     values[0] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":815
+    /* "pysam/calignmentfile.pyx":823
  *     def pileup( self,
  *                 reference = None,
  *                 start = None,             # <<<<<<<<<<<<<<
@@ -10318,7 +10544,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_s
  */
     values[1] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":816
+    /* "pysam/calignmentfile.pyx":824
  *                 reference = None,
  *                 start = None,
  *                 end = None,             # <<<<<<<<<<<<<<
@@ -10327,12 +10553,12 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_s
  */
     values[2] = ((PyObject *)Py_None);
 
-    /* "pysam/csamfile.pyx":817
+    /* "pysam/calignmentfile.pyx":825
  *                 start = None,
  *                 end = None,
  *                 region = None,             # <<<<<<<<<<<<<<
  *                 **kwargs ):
- *         '''
+ *         '''perform a :term:`pileup` within a :term:`region`. The region is
  */
     values[3] = ((PyObject *)Py_None);
     if (unlikely(__pyx_kwds)) {
@@ -10370,7 +10596,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_s
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "pileup") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "pileup") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -10389,16 +10615,16 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_s
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("pileup", 0, 0, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("pileup", 0, 0, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.pileup", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.pileup", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_28pileup(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_kwargs);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_28pileup(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_kwargs);
 
-  /* "pysam/csamfile.pyx":813
+  /* "pysam/calignmentfile.pyx":821
  *         return counter
  * 
  *     def pileup( self,             # <<<<<<<<<<<<<<
@@ -10412,7 +10638,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_s
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_kwargs) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_28pileup(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_kwargs) {
   int __pyx_v_rtid;
   int __pyx_v_rstart;
   int __pyx_v_rend;
@@ -10437,57 +10663,57 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("pileup", 0);
-  __Pyx_TraceCall("pileup", __pyx_f[0], 813);
+  __Pyx_TraceCall("pileup", __pyx_f[0], 821);
 
-  /* "pysam/csamfile.pyx":867
+  /* "pysam/calignmentfile.pyx":891
  *         cdef int rtid, rstart, rend, has_coord
  * 
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError( "I/O operation on closed file" )
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":868
+    /* "pysam/calignmentfile.pyx":892
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         has_coord, rtid, rstart, rend = self._parseRegion(
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__29, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__27, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":870
+  /* "pysam/calignmentfile.pyx":894
  *             raise ValueError( "I/O operation on closed file" )
  * 
  *         has_coord, rtid, rstart, rend = self._parseRegion(             # <<<<<<<<<<<<<<
  *             reference, start, end, region )
  * 
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_parseRegion); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_parseRegion); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
 
-  /* "pysam/csamfile.pyx":871
+  /* "pysam/calignmentfile.pyx":895
  * 
  *         has_coord, rtid, rstart, rend = self._parseRegion(
  *             reference, start, end, region )             # <<<<<<<<<<<<<<
  * 
  *         if self.isbam:
  */
-  __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_v_reference);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_reference);
@@ -10502,14 +10728,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
   PyTuple_SET_ITEM(__pyx_t_1, 3, __pyx_v_region);
   __Pyx_GIVEREF(__pyx_v_region);
 
-  /* "pysam/csamfile.pyx":870
+  /* "pysam/calignmentfile.pyx":894
  *             raise ValueError( "I/O operation on closed file" )
  * 
  *         has_coord, rtid, rstart, rend = self._parseRegion(             # <<<<<<<<<<<<<<
  *             reference, start, end, region )
  * 
  */
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -10523,7 +10749,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
     if (unlikely(size != 4)) {
       if (size > 4) __Pyx_RaiseTooManyValuesError(4);
       else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     #if CYTHON_COMPILING_IN_CPYTHON
     if (likely(PyTuple_CheckExact(sequence))) {
@@ -10546,7 +10772,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
       Py_ssize_t i;
       PyObject** temps[4] = {&__pyx_t_1,&__pyx_t_2,&__pyx_t_6,&__pyx_t_7};
       for (i=0; i < 4; i++) {
-        PyObject* item = PySequence_ITEM(sequence, i); if (unlikely(!item)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        PyObject* item = PySequence_ITEM(sequence, i); if (unlikely(!item)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(item);
         *(temps[i]) = item;
       }
@@ -10556,7 +10782,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
   } else {
     Py_ssize_t index = -1;
     PyObject** temps[4] = {&__pyx_t_1,&__pyx_t_2,&__pyx_t_6,&__pyx_t_7};
-    __pyx_t_8 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __pyx_t_9 = Py_TYPE(__pyx_t_8)->tp_iternext;
@@ -10565,7 +10791,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
       __Pyx_GOTREF(item);
       *(temps[index]) = item;
     }
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_9 = NULL;
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
     goto __pyx_L5_unpacking_done;
@@ -10573,23 +10799,23 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
     __pyx_t_9 = NULL;
     if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_L5_unpacking_done:;
   }
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_11 = __Pyx_PyInt_As_int(__pyx_t_2); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyInt_As_int(__pyx_t_2); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_12 = __Pyx_PyInt_As_int(__pyx_t_6); if (unlikely((__pyx_t_12 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = __Pyx_PyInt_As_int(__pyx_t_6); if (unlikely((__pyx_t_12 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_7); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_7); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
   __pyx_v_has_coord = __pyx_t_10;
   __pyx_v_rtid = __pyx_t_11;
   __pyx_v_rstart = __pyx_t_12;
   __pyx_v_rend = __pyx_t_13;
 
-  /* "pysam/csamfile.pyx":873
+  /* "pysam/calignmentfile.pyx":897
  *             reference, start, end, region )
  * 
  *         if self.isbam:             # <<<<<<<<<<<<<<
@@ -10599,38 +10825,38 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
   __pyx_t_4 = (__pyx_v_self->isbam != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":874
+    /* "pysam/calignmentfile.pyx":898
  * 
  *         if self.isbam:
  *             if not self._hasIndex():             # <<<<<<<<<<<<<<
  *                 raise ValueError("no index available for pileup")
  * 
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __pyx_t_3 = ((!__pyx_t_4) != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/csamfile.pyx":875
+      /* "pysam/calignmentfile.pyx":899
  *         if self.isbam:
  *             if not self._hasIndex():
  *                 raise ValueError("no index available for pileup")             # <<<<<<<<<<<<<<
  * 
  *             if has_coord:
  */
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__30, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__28, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 899; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_Raise(__pyx_t_7, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 899; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":877
+    /* "pysam/calignmentfile.pyx":901
  *                 raise ValueError("no index available for pileup")
  * 
  *             if has_coord:             # <<<<<<<<<<<<<<
@@ -10640,7 +10866,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
     __pyx_t_3 = (__pyx_v_has_coord != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/csamfile.pyx":878
+      /* "pysam/calignmentfile.pyx":902
  * 
  *             if has_coord:
  *                 return IteratorColumnRegion(self,             # <<<<<<<<<<<<<<
@@ -10648,75 +10874,75 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
  *                                             start = rstart,
  */
       __Pyx_XDECREF(__pyx_r);
-      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_INCREF(((PyObject *)__pyx_v_self));
       PyTuple_SET_ITEM(__pyx_t_7, 0, ((PyObject *)__pyx_v_self));
       __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
 
-      /* "pysam/csamfile.pyx":882
+      /* "pysam/calignmentfile.pyx":906
  *                                             start = rstart,
  *                                             end = rend,
  *                                             **kwargs )             # <<<<<<<<<<<<<<
  *             else:
  *                 return IteratorColumnAllRefs(self, **kwargs )
  */
-      __pyx_t_5 = PyDict_Copy(__pyx_v_kwargs); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyDict_Copy(__pyx_v_kwargs); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
 
-      /* "pysam/csamfile.pyx":879
+      /* "pysam/calignmentfile.pyx":903
  *             if has_coord:
  *                 return IteratorColumnRegion(self,
  *                                             tid = rtid,             # <<<<<<<<<<<<<<
  *                                             start = rstart,
  *                                             end = rend,
  */
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rtid); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 879; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rtid); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       if (unlikely(PyDict_GetItem(__pyx_t_5, __pyx_n_s_tid))) {
-        __Pyx_RaiseDoubleKeywordsError("function", __pyx_n_s_tid); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_RaiseDoubleKeywordsError("function", __pyx_n_s_tid); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
-      if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_tid, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_tid, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-      /* "pysam/csamfile.pyx":880
+      /* "pysam/calignmentfile.pyx":904
  *                 return IteratorColumnRegion(self,
  *                                             tid = rtid,
  *                                             start = rstart,             # <<<<<<<<<<<<<<
  *                                             end = rend,
  *                                             **kwargs )
  */
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rstart); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 880; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rstart); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 904; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       if (unlikely(PyDict_GetItem(__pyx_t_5, __pyx_n_s_start))) {
-        __Pyx_RaiseDoubleKeywordsError("function", __pyx_n_s_start); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_RaiseDoubleKeywordsError("function", __pyx_n_s_start); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
-      if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_start, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_start, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-      /* "pysam/csamfile.pyx":881
+      /* "pysam/calignmentfile.pyx":905
  *                                             tid = rtid,
  *                                             start = rstart,
  *                                             end = rend,             # <<<<<<<<<<<<<<
  *                                             **kwargs )
  *             else:
  */
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 881; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 905; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       if (unlikely(PyDict_GetItem(__pyx_t_5, __pyx_n_s_end))) {
-        __Pyx_RaiseDoubleKeywordsError("function", __pyx_n_s_end); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_RaiseDoubleKeywordsError("function", __pyx_n_s_end); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
-      if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_end, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_end, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-      /* "pysam/csamfile.pyx":878
+      /* "pysam/calignmentfile.pyx":902
  * 
  *             if has_coord:
  *                 return IteratorColumnRegion(self,             # <<<<<<<<<<<<<<
  *                                             tid = rtid,
  *                                             start = rstart,
  */
-      __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorColumnRegion)), __pyx_t_7, __pyx_t_5); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorColumnRegion)), __pyx_t_7, __pyx_t_5); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
@@ -10726,7 +10952,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":884
+      /* "pysam/calignmentfile.pyx":908
  *                                             **kwargs )
  *             else:
  *                 return IteratorColumnAllRefs(self, **kwargs )             # <<<<<<<<<<<<<<
@@ -10734,14 +10960,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
  *         else:
  */
       __Pyx_XDECREF(__pyx_r);
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_INCREF(((PyObject *)__pyx_v_self));
       PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_v_self));
       __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
       __pyx_t_5 = __pyx_v_kwargs;
       __Pyx_INCREF(__pyx_t_5);
-      __pyx_t_7 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorColumnAllRefs)), __pyx_t_6, __pyx_t_5); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorColumnAllRefs)), __pyx_t_6, __pyx_t_5); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
@@ -10752,21 +10978,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":887
+    /* "pysam/calignmentfile.pyx":911
  * 
  *         else:
  *             raise NotImplementedError( "pileup of samfiles not implemented yet" )             # <<<<<<<<<<<<<<
  * 
  *     def close( self ):
  */
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__31, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__29, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_Raise(__pyx_t_7, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":813
+  /* "pysam/calignmentfile.pyx":821
  *         return counter
  * 
  *     def pileup( self,             # <<<<<<<<<<<<<<
@@ -10782,7 +11008,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
   __Pyx_XDECREF(__pyx_t_6);
   __Pyx_XDECREF(__pyx_t_7);
   __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.pileup", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.pileup", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -10791,39 +11017,39 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":889
+/* "pysam/calignmentfile.pyx":913
  *             raise NotImplementedError( "pileup of samfiles not implemented yet" )
  * 
  *     def close( self ):             # <<<<<<<<<<<<<<
  *         '''
- *         closes the :class:`pysam.Samfile`.'''
+ *         closes the :class:`pysam.AlignmentFile`.'''
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_31close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_30close[] = "Samfile.close(self)\n\n        closes the :class:`pysam.Samfile`.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_31close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_31close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_30close[] = "AlignmentFile.close(self)\n\n        closes the :class:`pysam.AlignmentFile`.";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_31close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("close (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_30close(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_30close(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_30close(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("close", 0);
-  __Pyx_TraceCall("close", __pyx_f[0], 889);
+  __Pyx_TraceCall("close", __pyx_f[0], 913);
 
-  /* "pysam/csamfile.pyx":892
+  /* "pysam/calignmentfile.pyx":916
  *         '''
- *         closes the :class:`pysam.Samfile`.'''
+ *         closes the :class:`pysam.AlignmentFile`.'''
  *         if self.htsfile != NULL:             # <<<<<<<<<<<<<<
  *             hts_close(self.htsfile)
  *             hts_idx_destroy(self.index);
@@ -10831,8 +11057,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5py
   __pyx_t_1 = ((__pyx_v_self->htsfile != NULL) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":893
- *         closes the :class:`pysam.Samfile`.'''
+    /* "pysam/calignmentfile.pyx":917
+ *         closes the :class:`pysam.AlignmentFile`.'''
  *         if self.htsfile != NULL:
  *             hts_close(self.htsfile)             # <<<<<<<<<<<<<<
  *             hts_idx_destroy(self.index);
@@ -10840,7 +11066,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5py
  */
     hts_close(__pyx_v_self->htsfile);
 
-    /* "pysam/csamfile.pyx":894
+    /* "pysam/calignmentfile.pyx":918
  *         if self.htsfile != NULL:
  *             hts_close(self.htsfile)
  *             hts_idx_destroy(self.index);             # <<<<<<<<<<<<<<
@@ -10849,7 +11075,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5py
  */
     hts_idx_destroy(__pyx_v_self->index);
 
-    /* "pysam/csamfile.pyx":895
+    /* "pysam/calignmentfile.pyx":919
  *             hts_close(self.htsfile)
  *             hts_idx_destroy(self.index);
  *             self.htsfile = NULL             # <<<<<<<<<<<<<<
@@ -10861,12 +11087,12 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5py
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":889
+  /* "pysam/calignmentfile.pyx":913
  *             raise NotImplementedError( "pileup of samfiles not implemented yet" )
  * 
  *     def close( self ):             # <<<<<<<<<<<<<<
  *         '''
- *         closes the :class:`pysam.Samfile`.'''
+ *         closes the :class:`pysam.AlignmentFile`.'''
  */
 
   /* function exit code */
@@ -10877,7 +11103,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":897
+/* "pysam/calignmentfile.pyx":921
  *             self.htsfile = NULL
  * 
  *     def __dealloc__( self ):             # <<<<<<<<<<<<<<
@@ -10886,17 +11112,17 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5py
  */
 
 /* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_7Samfile_33__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_7Samfile_33__dealloc__(PyObject *__pyx_v_self) {
+static void __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_33__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_33__dealloc__(PyObject *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_32__dealloc__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
 }
 
-static void __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static void __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_32__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
@@ -10906,23 +11132,23 @@ static void __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(struct __pyx_obj_5p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 897);
+  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 921);
 
-  /* "pysam/csamfile.pyx":901
+  /* "pysam/calignmentfile.pyx":925
  *         # note: no doc string
  *         # note: __del__ is not called.
  *         self.close()             # <<<<<<<<<<<<<<
  *         bam_destroy1(self.b)
  *         if self.header != NULL:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 901; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 901; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":902
+  /* "pysam/calignmentfile.pyx":926
  *         # note: __del__ is not called.
  *         self.close()
  *         bam_destroy1(self.b)             # <<<<<<<<<<<<<<
@@ -10931,7 +11157,7 @@ static void __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(struct __pyx_obj_5p
  */
   bam_destroy1(__pyx_v_self->b);
 
-  /* "pysam/csamfile.pyx":903
+  /* "pysam/calignmentfile.pyx":927
  *         self.close()
  *         bam_destroy1(self.b)
  *         if self.header != NULL:             # <<<<<<<<<<<<<<
@@ -10941,19 +11167,19 @@ static void __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(struct __pyx_obj_5p
   __pyx_t_3 = ((__pyx_v_self->header != NULL) != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":904
+    /* "pysam/calignmentfile.pyx":928
  *         bam_destroy1(self.b)
  *         if self.header != NULL:
  *             bam_hdr_destroy(self.header)             # <<<<<<<<<<<<<<
  * 
- *     cpdef int write( self, AlignedRead read ) except -1:
+ *     cpdef int write(self, AlignedSegment read) except -1:
  */
     bam_hdr_destroy(__pyx_v_self->header);
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":897
+  /* "pysam/calignmentfile.pyx":921
  *             self.htsfile = NULL
  * 
  *     def __dealloc__( self ):             # <<<<<<<<<<<<<<
@@ -10966,22 +11192,22 @@ static void __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(struct __pyx_obj_5p
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_WriteUnraisable("pysam.csamfile.Samfile.__dealloc__", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __Pyx_WriteUnraisable("pysam.calignmentfile.AlignmentFile.__dealloc__", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/csamfile.pyx":906
+/* "pysam/calignmentfile.pyx":930
  *             bam_hdr_destroy(self.header)
  * 
- *     cpdef int write( self, AlignedRead read ) except -1:             # <<<<<<<<<<<<<<
+ *     cpdef int write(self, AlignedSegment read) except -1:             # <<<<<<<<<<<<<<
  *         '''
- *         write a single :class:`pysam.AlignedRead` to disk.
+ *         write a single :class:`pysam.AlignedSegment` to disk.
  */
 
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_35write(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/
-static int __pyx_f_5pysam_8csamfile_7Samfile_write(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read, int __pyx_skip_dispatch) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_35write(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/
+static int __pyx_f_5pysam_14calignmentfile_13AlignmentFile_write(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_read, int __pyx_skip_dispatch) {
   int __pyx_v_x;
   int __pyx_r;
   __Pyx_RefNannyDeclarations
@@ -10996,23 +11222,23 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_write(struct __pyx_obj_5pysam_8csam
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("write", 0);
-  __Pyx_TraceCall("write", __pyx_f[0], 906);
+  __Pyx_TraceCall("write", __pyx_f[0], 930);
   /* Check if called by wrapper */
   if (unlikely(__pyx_skip_dispatch)) ;
   /* Check if overridden in Python */
   else if (unlikely(Py_TYPE(((PyObject *)__pyx_v_self))->tp_dictoffset != 0)) {
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_write); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_write); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    if (!PyCFunction_Check(__pyx_t_1) || (PyCFunction_GET_FUNCTION(__pyx_t_1) != (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_35write)) {
-      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!PyCFunction_Check(__pyx_t_1) || (PyCFunction_GET_FUNCTION(__pyx_t_1) != (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_35write)) {
+      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_INCREF(((PyObject *)__pyx_v_read));
       PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_read));
       __Pyx_GIVEREF(((PyObject *)__pyx_v_read));
-      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_4 = __Pyx_PyInt_As_int(__pyx_t_3); if (unlikely((__pyx_t_4 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyInt_As_int(__pyx_t_3); if (unlikely((__pyx_t_4 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       __pyx_r = __pyx_t_4;
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -11021,24 +11247,24 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_write(struct __pyx_obj_5pysam_8csam
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   }
 
-  /* "pysam/csamfile.pyx":912
+  /* "pysam/calignmentfile.pyx":936
  *         returns the number of bytes written.
  *         '''
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             return 0
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_t_6 = ((!__pyx_t_5) != 0);
   if (__pyx_t_6) {
 
-    /* "pysam/csamfile.pyx":913
+    /* "pysam/calignmentfile.pyx":937
  *         '''
  *         if not self._isOpen():
  *             return 0             # <<<<<<<<<<<<<<
@@ -11049,7 +11275,7 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_write(struct __pyx_obj_5pysam_8csam
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":915
+  /* "pysam/calignmentfile.pyx":939
  *             return 0
  * 
  *         x = sam_write1(self.htsfile,             # <<<<<<<<<<<<<<
@@ -11058,7 +11284,7 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_write(struct __pyx_obj_5pysam_8csam
  */
   __pyx_v_x = sam_write1(__pyx_v_self->htsfile, __pyx_v_self->header, __pyx_v_read->_delegate);
 
-  /* "pysam/csamfile.pyx":919
+  /* "pysam/calignmentfile.pyx":943
  *                        read._delegate)
  * 
  *         return x             # <<<<<<<<<<<<<<
@@ -11068,12 +11294,12 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_write(struct __pyx_obj_5pysam_8csam
   __pyx_r = __pyx_v_x;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":906
+  /* "pysam/calignmentfile.pyx":930
  *             bam_hdr_destroy(self.header)
  * 
- *     cpdef int write( self, AlignedRead read ) except -1:             # <<<<<<<<<<<<<<
+ *     cpdef int write(self, AlignedSegment read) except -1:             # <<<<<<<<<<<<<<
  *         '''
- *         write a single :class:`pysam.AlignedRead` to disk.
+ *         write a single :class:`pysam.AlignedSegment` to disk.
  */
 
   /* function exit code */
@@ -11081,7 +11307,7 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_write(struct __pyx_obj_5pysam_8csam
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_WriteUnraisable("pysam.csamfile.Samfile.write", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __Pyx_WriteUnraisable("pysam.calignmentfile.AlignmentFile.write", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -11090,17 +11316,17 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_write(struct __pyx_obj_5pysam_8csam
 }
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_35write(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_34write[] = "Samfile.write(self, AlignedRead read) -> int\n\n        write a single :class:`pysam.AlignedRead` to disk.\n\n        returns the number of bytes written.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_35write(PyObject *__pyx_v_self, PyObject *__pyx_v_read) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_35write(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_34write[] = "AlignmentFile.write(self, AlignedSegment read) -> int\n\n        write a single :class:`pysam.AlignedSegment` to disk.\n\n        returns the number of bytes written.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_35write(PyObject *__pyx_v_self, PyObject *__pyx_v_read) {
   CYTHON_UNUSED int __pyx_lineno = 0;
   CYTHON_UNUSED const char *__pyx_filename = NULL;
   CYTHON_UNUSED int __pyx_clineno = 0;
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("write (wrapper)", 0);
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_read), __pyx_ptype_5pysam_8csamfile_AlignedRead, 1, "read", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_34write(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_read));
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_read), __pyx_ptype_5pysam_14calignmentfile_AlignedSegment, 1, "read", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_34write(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), ((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_read));
 
   /* function exit code */
   goto __pyx_L0;
@@ -11111,7 +11337,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_35write(PyObject *__pyx_v_se
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_34write(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_34write(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_read) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -11120,9 +11346,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_34write(struct __pyx_obj_5py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("write", 0);
-  __Pyx_TraceCall("write", __pyx_f[0], 906);
+  __Pyx_TraceCall("write", __pyx_f[0], 930);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(((struct __pyx_vtabstruct_5pysam_8csamfile_Samfile *)__pyx_v_self->__pyx_vtab)->write(__pyx_v_self, __pyx_v_read, 1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(((struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self->__pyx_vtab)->write(__pyx_v_self, __pyx_v_read, 1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -11131,7 +11357,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_34write(struct __pyx_obj_5py
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.write", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.write", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -11140,7 +11366,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_34write(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":921
+/* "pysam/calignmentfile.pyx":945
  *         return x
  * 
  *     def __enter__(self):             # <<<<<<<<<<<<<<
@@ -11149,27 +11375,27 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_34write(struct __pyx_obj_5py
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_37__enter__(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_36__enter__[] = "Samfile.__enter__(self)";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_37__enter__(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_37__enter__(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_36__enter__[] = "AlignmentFile.__enter__(self)";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_37__enter__(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__enter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_36__enter__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_36__enter__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_36__enter__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_36__enter__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__enter__", 0);
-  __Pyx_TraceCall("__enter__", __pyx_f[0], 921);
+  __Pyx_TraceCall("__enter__", __pyx_f[0], 945);
 
-  /* "pysam/csamfile.pyx":922
+  /* "pysam/calignmentfile.pyx":946
  * 
  *     def __enter__(self):
  *         return self             # <<<<<<<<<<<<<<
@@ -11181,7 +11407,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_36__enter__(struct __pyx_obj
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":921
+  /* "pysam/calignmentfile.pyx":945
  *         return x
  * 
  *     def __enter__(self):             # <<<<<<<<<<<<<<
@@ -11197,7 +11423,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_36__enter__(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":924
+/* "pysam/calignmentfile.pyx":948
  *         return self
  * 
  *     def __exit__(self, exc_type, exc_value, traceback):             # <<<<<<<<<<<<<<
@@ -11206,9 +11432,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_36__enter__(struct __pyx_obj
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_39__exit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_38__exit__[] = "Samfile.__exit__(self, exc_type, exc_value, traceback)";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_39__exit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_39__exit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_38__exit__[] = "AlignmentFile.__exit__(self, exc_type, exc_value, traceback)";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_39__exit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   CYTHON_UNUSED PyObject *__pyx_v_exc_type = 0;
   CYTHON_UNUSED PyObject *__pyx_v_exc_value = 0;
   CYTHON_UNUSED PyObject *__pyx_v_traceback = 0;
@@ -11239,16 +11465,16 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_39__exit__(PyObject *__pyx_v
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_exc_value)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_traceback)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__exit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__exit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
       goto __pyx_L5_argtuple_error;
@@ -11263,20 +11489,20 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_39__exit__(PyObject *__pyx_v
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__exit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.__exit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_exc_type, __pyx_v_exc_value, __pyx_v_traceback);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_38__exit__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_exc_type, __pyx_v_exc_value, __pyx_v_traceback);
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_exc_type, CYTHON_UNUSED PyObject *__pyx_v_exc_value, CYTHON_UNUSED PyObject *__pyx_v_traceback) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_38__exit__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_exc_type, CYTHON_UNUSED PyObject *__pyx_v_exc_value, CYTHON_UNUSED PyObject *__pyx_v_traceback) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -11286,23 +11512,23 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__exit__", 0);
-  __Pyx_TraceCall("__exit__", __pyx_f[0], 924);
+  __Pyx_TraceCall("__exit__", __pyx_f[0], 948);
 
-  /* "pysam/csamfile.pyx":925
+  /* "pysam/calignmentfile.pyx":949
  * 
  *     def __exit__(self, exc_type, exc_value, traceback):
  *         self.close()             # <<<<<<<<<<<<<<
  *         return False
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":926
+  /* "pysam/calignmentfile.pyx":950
  *     def __exit__(self, exc_type, exc_value, traceback):
  *         self.close()
  *         return False             # <<<<<<<<<<<<<<
@@ -11314,7 +11540,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(struct __pyx_obj_
   __pyx_r = Py_False;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":924
+  /* "pysam/calignmentfile.pyx":948
  *         return self
  * 
  *     def __exit__(self, exc_type, exc_value, traceback):             # <<<<<<<<<<<<<<
@@ -11326,7 +11552,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(struct __pyx_obj_
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__exit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.__exit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -11335,7 +11561,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":935
+/* "pysam/calignmentfile.pyx":959
  *     property filename:
  *         '''number of :term:`filename` associated with this object.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -11344,26 +11570,26 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(struct __pyx_obj_
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_8filename_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_8filename_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_8filename_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_8filename_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_8filename___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_8filename___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8filename___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_8filename___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 935);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 959);
 
-  /* "pysam/csamfile.pyx":936
+  /* "pysam/calignmentfile.pyx":960
  *         '''number of :term:`filename` associated with this object.'''
  *         def __get__(self):
  *             return self._filename             # <<<<<<<<<<<<<<
@@ -11375,7 +11601,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8filename___get__(struct __p
   __pyx_r = __pyx_v_self->_filename;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":935
+  /* "pysam/calignmentfile.pyx":959
  *     property filename:
  *         '''number of :term:`filename` associated with this object.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -11391,7 +11617,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8filename___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":940
+/* "pysam/calignmentfile.pyx":964
  *     property nreferences:
  *         '''number of :term:`reference` sequences in the file.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -11400,19 +11626,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8filename___get__(struct __p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_11nreferences_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_11nreferences_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_11nreferences_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_11nreferences_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_11nreferences___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_11nreferences___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -11424,32 +11650,32 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 940);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 964);
 
-  /* "pysam/csamfile.pyx":941
+  /* "pysam/calignmentfile.pyx":965
  *         '''number of :term:`reference` sequences in the file.'''
  *         def __get__(self):
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *             return self.header.n_targets
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__32, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__30, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":942
+  /* "pysam/calignmentfile.pyx":966
  *         def __get__(self):
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
  *             return self.header.n_targets             # <<<<<<<<<<<<<<
@@ -11457,13 +11683,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(struct
  *     property references:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 966; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":940
+  /* "pysam/calignmentfile.pyx":964
  *     property nreferences:
  *         '''number of :term:`reference` sequences in the file.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -11475,7 +11701,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(struct
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.nreferences.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.nreferences.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -11484,7 +11710,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":946
+/* "pysam/calignmentfile.pyx":970
  *     property references:
  *         """tuple with the names of :term:`reference` sequences."""
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -11493,19 +11719,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(struct
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_10references_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_10references_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_10references_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_10references_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_10references___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_10references___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_v_t = NULL;
   long __pyx_v_x;
   PyObject *__pyx_r = NULL;
@@ -11521,44 +11747,44 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 946);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 970);
 
-  /* "pysam/csamfile.pyx":947
+  /* "pysam/calignmentfile.pyx":971
  *         """tuple with the names of :term:`reference` sequences."""
  *         def __get__(self):
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *             t = []
  *             for x from 0 <= x < self.header.n_targets:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__33, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__31, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":948
+  /* "pysam/calignmentfile.pyx":972
  *         def __get__(self):
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
  *             t = []             # <<<<<<<<<<<<<<
  *             for x from 0 <= x < self.header.n_targets:
  *                 t.append(_charptr_to_str(self.header.target_name[x]))
  */
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 972; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_t = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":949
+  /* "pysam/calignmentfile.pyx":973
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
  *             t = []
  *             for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
@@ -11568,20 +11794,20 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct
   __pyx_t_5 = __pyx_v_self->header->n_targets;
   for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_5; __pyx_v_x++) {
 
-    /* "pysam/csamfile.pyx":950
+    /* "pysam/calignmentfile.pyx":974
  *             t = []
  *             for x from 0 <= x < self.header.n_targets:
  *                 t.append(_charptr_to_str(self.header.target_name[x]))             # <<<<<<<<<<<<<<
  *             return tuple(t)
  * 
  */
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile__charptr_to_str((__pyx_v_self->header->target_name[__pyx_v_x])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __pyx_f_5pysam_14calignmentfile__charptr_to_str((__pyx_v_self->header->target_name[__pyx_v_x])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 974; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_t, __pyx_t_2); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_t, __pyx_t_2); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 974; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   }
 
-  /* "pysam/csamfile.pyx":951
+  /* "pysam/calignmentfile.pyx":975
  *             for x from 0 <= x < self.header.n_targets:
  *                 t.append(_charptr_to_str(self.header.target_name[x]))
  *             return tuple(t)             # <<<<<<<<<<<<<<
@@ -11589,13 +11815,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct
  *     property lengths:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = PyList_AsTuple(__pyx_v_t); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 951; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_AsTuple(__pyx_v_t); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":946
+  /* "pysam/calignmentfile.pyx":970
  *     property references:
  *         """tuple with the names of :term:`reference` sequences."""
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -11607,7 +11833,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.references.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.references.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_t);
@@ -11617,8 +11843,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":957
- *         :attr:`pysam.Samfile.references`
+/* "pysam/calignmentfile.pyx":983
+ * 
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
@@ -11626,19 +11852,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7lengths_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7lengths_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_7lengths_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_7lengths_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_7lengths___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_7lengths___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_v_t = NULL;
   long __pyx_v_x;
   PyObject *__pyx_r = NULL;
@@ -11654,44 +11880,44 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 957);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 983);
 
-  /* "pysam/csamfile.pyx":958
+  /* "pysam/calignmentfile.pyx":984
  *         """
  *         def __get__(self):
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *             t = []
  *             for x from 0 <= x < self.header.n_targets:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__34, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__32, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":959
+  /* "pysam/calignmentfile.pyx":985
  *         def __get__(self):
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
  *             t = []             # <<<<<<<<<<<<<<
  *             for x from 0 <= x < self.header.n_targets:
  *                 t.append( self.header.target_len[x] )
  */
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 959; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_t = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":960
+  /* "pysam/calignmentfile.pyx":986
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
  *             t = []
  *             for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
@@ -11701,20 +11927,20 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __py
   __pyx_t_5 = __pyx_v_self->header->n_targets;
   for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_5; __pyx_v_x++) {
 
-    /* "pysam/csamfile.pyx":961
+    /* "pysam/calignmentfile.pyx":987
  *             t = []
  *             for x from 0 <= x < self.header.n_targets:
  *                 t.append( self.header.target_len[x] )             # <<<<<<<<<<<<<<
  *             return tuple(t)
  * 
  */
-    __pyx_t_2 = __Pyx_PyInt_From_uint32_t((__pyx_v_self->header->target_len[__pyx_v_x])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_From_uint32_t((__pyx_v_self->header->target_len[__pyx_v_x])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_t, __pyx_t_2); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_t, __pyx_t_2); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   }
 
-  /* "pysam/csamfile.pyx":962
+  /* "pysam/calignmentfile.pyx":988
  *             for x from 0 <= x < self.header.n_targets:
  *                 t.append( self.header.target_len[x] )
  *             return tuple(t)             # <<<<<<<<<<<<<<
@@ -11722,14 +11948,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __py
  *     property mapped:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = PyList_AsTuple(__pyx_v_t); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 962; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_AsTuple(__pyx_v_t); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":957
- *         :attr:`pysam.Samfile.references`
+  /* "pysam/calignmentfile.pyx":983
+ * 
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
@@ -11740,7 +11966,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __py
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.lengths.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.lengths.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_t);
@@ -11750,7 +11976,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":967
+/* "pysam/calignmentfile.pyx":993
  *         """total number of mapped alignments in file.
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -11759,19 +11985,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __py
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_6mapped_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_6mapped_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_6mapped_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_6mapped_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_6mapped___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_6mapped___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   int __pyx_v_tid;
   uint64_t __pyx_v_total;
   uint64_t __pyx_v_mapped;
@@ -11786,23 +12012,23 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 967);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 993);
 
-  /* "pysam/csamfile.pyx":968
+  /* "pysam/calignmentfile.pyx":994
  *         """
  *         def __get__(self):
  *             self._checkIndex()             # <<<<<<<<<<<<<<
  *             cdef int tid
  *             cdef uint64_t total = 0
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_checkIndex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_checkIndex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":970
+  /* "pysam/calignmentfile.pyx":996
  *             self._checkIndex()
  *             cdef int tid
  *             cdef uint64_t total = 0             # <<<<<<<<<<<<<<
@@ -11811,7 +12037,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx
  */
   __pyx_v_total = 0;
 
-  /* "pysam/csamfile.pyx":972
+  /* "pysam/calignmentfile.pyx":998
  *             cdef uint64_t total = 0
  *             cdef uint64_t mapped, unmapped
  *             for tid from 0 <= tid < self.header.n_targets:             # <<<<<<<<<<<<<<
@@ -11821,7 +12047,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx
   __pyx_t_3 = __pyx_v_self->header->n_targets;
   for (__pyx_v_tid = 0; __pyx_v_tid < __pyx_t_3; __pyx_v_tid++) {
 
-    /* "pysam/csamfile.pyx":973
+    /* "pysam/calignmentfile.pyx":999
  *             cdef uint64_t mapped, unmapped
  *             for tid from 0 <= tid < self.header.n_targets:
  *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)             # <<<<<<<<<<<<<<
@@ -11830,7 +12056,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx
  */
     hts_idx_get_stat(__pyx_v_self->index, __pyx_v_tid, (&__pyx_v_mapped), (&__pyx_v_unmapped));
 
-    /* "pysam/csamfile.pyx":974
+    /* "pysam/calignmentfile.pyx":1000
  *             for tid from 0 <= tid < self.header.n_targets:
  *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
  *                 total += mapped             # <<<<<<<<<<<<<<
@@ -11840,7 +12066,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx
     __pyx_v_total = (__pyx_v_total + __pyx_v_mapped);
   }
 
-  /* "pysam/csamfile.pyx":975
+  /* "pysam/calignmentfile.pyx":1001
  *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
  *                 total += mapped
  *             return total             # <<<<<<<<<<<<<<
@@ -11848,13 +12074,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx
  *     def _checkIndex(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_uint64_t(__pyx_v_total); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_uint64_t(__pyx_v_total); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":967
+  /* "pysam/calignmentfile.pyx":993
  *         """total number of mapped alignments in file.
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -11866,7 +12092,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.mapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.mapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -11875,7 +12101,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":977
+/* "pysam/calignmentfile.pyx":1003
  *             return total
  * 
  *     def _checkIndex(self):             # <<<<<<<<<<<<<<
@@ -11884,20 +12110,20 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_41_checkIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_40_checkIndex[] = "Samfile._checkIndex(self)\ncheck if index is present. Otherwise raise\n        an error.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_41_checkIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_41_checkIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_40_checkIndex[] = "AlignmentFile._checkIndex(self)\ncheck if index is present. Otherwise raise\n        an error.";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_41_checkIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("_checkIndex (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_40_checkIndex(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_40_checkIndex(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -11909,66 +12135,66 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_checkIndex", 0);
-  __Pyx_TraceCall("_checkIndex", __pyx_f[0], 977);
+  __Pyx_TraceCall("_checkIndex", __pyx_f[0], 1003);
 
-  /* "pysam/csamfile.pyx":980
+  /* "pysam/calignmentfile.pyx":1006
  *         '''check if index is present. Otherwise raise
  *         an error.'''
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError("I/O operation on closed file")
  *         if not self.isbam:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":981
+    /* "pysam/calignmentfile.pyx":1007
  *         an error.'''
  *         if not self._isOpen():
  *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")
+ *             raise AttributeError("AlignmentFile.mapped only available in bam files")
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__35, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__33, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":982
+  /* "pysam/calignmentfile.pyx":1008
  *         if not self._isOpen():
  *             raise ValueError("I/O operation on closed file")
  *         if not self.isbam:             # <<<<<<<<<<<<<<
- *             raise AttributeError("Samfile.mapped only available in bam files")
+ *             raise AttributeError("AlignmentFile.mapped only available in bam files")
  *         if self.index == NULL:
  */
   __pyx_t_4 = ((!(__pyx_v_self->isbam != 0)) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":983
+    /* "pysam/calignmentfile.pyx":1009
  *             raise ValueError("I/O operation on closed file")
  *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")             # <<<<<<<<<<<<<<
+ *             raise AttributeError("AlignmentFile.mapped only available in bam files")             # <<<<<<<<<<<<<<
  *         if self.index == NULL:
  *             raise ValueError("mapping information not recorded in index "
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_AttributeError, __pyx_tuple__36, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_AttributeError, __pyx_tuple__34, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":984
+  /* "pysam/calignmentfile.pyx":1010
  *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")
+ *             raise AttributeError("AlignmentFile.mapped only available in bam files")
  *         if self.index == NULL:             # <<<<<<<<<<<<<<
  *             raise ValueError("mapping information not recorded in index "
  *                                  "or index not available")
@@ -11976,21 +12202,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(struct __pyx_o
   __pyx_t_4 = ((__pyx_v_self->index == NULL) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":985
- *             raise AttributeError("Samfile.mapped only available in bam files")
+    /* "pysam/calignmentfile.pyx":1011
+ *             raise AttributeError("AlignmentFile.mapped only available in bam files")
  *         if self.index == NULL:
  *             raise ValueError("mapping information not recorded in index "             # <<<<<<<<<<<<<<
  *                                  "or index not available")
  * 
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__37, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__35, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":977
+  /* "pysam/calignmentfile.pyx":1003
  *             return total
  * 
  *     def _checkIndex(self):             # <<<<<<<<<<<<<<
@@ -12004,7 +12230,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(struct __pyx_o
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._checkIndex", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._checkIndex", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -12013,7 +12239,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":992
+/* "pysam/calignmentfile.pyx":1018
  *         """total number of unmapped reads in file.
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -12022,19 +12248,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(struct __pyx_o
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_8unmapped_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_8unmapped_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_8unmapped_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_8unmapped_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_8unmapped___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_8unmapped___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   int __pyx_v_tid;
   uint64_t __pyx_v_total;
   uint64_t __pyx_v_mapped;
@@ -12049,23 +12275,23 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 992);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 1018);
 
-  /* "pysam/csamfile.pyx":993
+  /* "pysam/calignmentfile.pyx":1019
  *         """
  *         def __get__(self):
  *             self._checkIndex()             # <<<<<<<<<<<<<<
  *             cdef int tid
  *             cdef uint64_t total = 0
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_checkIndex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_checkIndex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":995
+  /* "pysam/calignmentfile.pyx":1021
  *             self._checkIndex()
  *             cdef int tid
  *             cdef uint64_t total = 0             # <<<<<<<<<<<<<<
@@ -12074,7 +12300,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __p
  */
   __pyx_v_total = 0;
 
-  /* "pysam/csamfile.pyx":997
+  /* "pysam/calignmentfile.pyx":1023
  *             cdef uint64_t total = 0
  *             cdef uint64_t mapped, unmapped
  *             for tid from 0 <= tid < self.header.n_targets:             # <<<<<<<<<<<<<<
@@ -12084,7 +12310,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __p
   __pyx_t_3 = __pyx_v_self->header->n_targets;
   for (__pyx_v_tid = 0; __pyx_v_tid < __pyx_t_3; __pyx_v_tid++) {
 
-    /* "pysam/csamfile.pyx":998
+    /* "pysam/calignmentfile.pyx":1024
  *             cdef uint64_t mapped, unmapped
  *             for tid from 0 <= tid < self.header.n_targets:
  *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)             # <<<<<<<<<<<<<<
@@ -12093,7 +12319,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __p
  */
     hts_idx_get_stat(__pyx_v_self->index, __pyx_v_tid, (&__pyx_v_mapped), (&__pyx_v_unmapped));
 
-    /* "pysam/csamfile.pyx":999
+    /* "pysam/calignmentfile.pyx":1025
  *             for tid from 0 <= tid < self.header.n_targets:
  *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
  *                 total += unmapped             # <<<<<<<<<<<<<<
@@ -12103,7 +12329,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __p
     __pyx_v_total = (__pyx_v_total + __pyx_v_unmapped);
   }
 
-  /* "pysam/csamfile.pyx":1000
+  /* "pysam/calignmentfile.pyx":1026
  *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
  *                 total += unmapped
  *             return total             # <<<<<<<<<<<<<<
@@ -12111,13 +12337,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __p
  *     property nocoordinate:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_uint64_t(__pyx_v_total); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_uint64_t(__pyx_v_total); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":992
+  /* "pysam/calignmentfile.pyx":1018
  *         """total number of unmapped reads in file.
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -12129,7 +12355,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __p
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.unmapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.unmapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -12138,7 +12364,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1005
+/* "pysam/calignmentfile.pyx":1031
  *         """total number of reads without coordinates
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -12147,19 +12373,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_12nocoordinate_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_12nocoordinate_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_12nocoordinate_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_12nocoordinate_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_12nocoordinate___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_12nocoordinate___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -12169,23 +12395,23 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(struc
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 1005);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 1031);
 
-  /* "pysam/csamfile.pyx":1006
+  /* "pysam/calignmentfile.pyx":1032
  *         """
  *         def __get__(self):
  *             self._checkIndex()             # <<<<<<<<<<<<<<
  *             return hts_idx_get_n_no_coor(self.index)
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_checkIndex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_checkIndex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":1007
+  /* "pysam/calignmentfile.pyx":1033
  *         def __get__(self):
  *             self._checkIndex()
  *             return hts_idx_get_n_no_coor(self.index)             # <<<<<<<<<<<<<<
@@ -12193,13 +12419,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(struc
  *     property text:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_uint64_t(hts_idx_get_n_no_coor(__pyx_v_self->index)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_uint64_t(hts_idx_get_n_no_coor(__pyx_v_self->index)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1033; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1005
+  /* "pysam/calignmentfile.pyx":1031
  *         """total number of reads without coordinates
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -12211,7 +12437,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(struc
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.nocoordinate.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.nocoordinate.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -12220,28 +12446,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(struc
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1011
- *     property text:
- *         '''full contents of the :term:`sam file` header as a string.'''
+/* "pysam/calignmentfile.pyx":1042
+ * 
+ *         '''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return from_string_and_size(self.header.text, self.header.l_text)
+ *             if not self._isOpen():
+ *                 raise ValueError( "I/O operation on closed file" )
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_4text_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_4text_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_4text_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_4text_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_4text___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_4text___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4text___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_4text___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -12253,58 +12479,66 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4text___get__(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 1011);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 1042);
 
-  /* "pysam/csamfile.pyx":1012
- *         '''full contents of the :term:`sam file` header as a string.'''
+  /* "pysam/calignmentfile.pyx":1043
+ *         '''
  *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             if not self._isOpen():             # <<<<<<<<<<<<<<
+ *                 raise ValueError( "I/O operation on closed file" )
  *             return from_string_and_size(self.header.text, self.header.l_text)
- * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__38, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/calignmentfile.pyx":1044
+ *         def __get__(self):
+ *             if not self._isOpen():
+ *                 raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             return from_string_and_size(self.header.text, self.header.l_text)
+ * 
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__36, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1013
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
+  /* "pysam/calignmentfile.pyx":1045
+ *             if not self._isOpen():
+ *                 raise ValueError( "I/O operation on closed file" )
  *             return from_string_and_size(self.header.text, self.header.l_text)             # <<<<<<<<<<<<<<
  * 
  *     property header:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile_from_string_and_size(__pyx_v_self->header->text, __pyx_v_self->header->l_text); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __pyx_f_5pysam_14calignmentfile_from_string_and_size(__pyx_v_self->header->text, __pyx_v_self->header->l_text); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1011
- *     property text:
- *         '''full contents of the :term:`sam file` header as a string.'''
+  /* "pysam/calignmentfile.pyx":1042
+ * 
+ *         '''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return from_string_and_size(self.header.text, self.header.l_text)
+ *             if not self._isOpen():
+ *                 raise ValueError( "I/O operation on closed file" )
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.text.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.text.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -12313,34 +12547,35 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4text___get__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1019
- *         a two-level dictionary.
+/* "pysam/calignmentfile.pyx":1068
+ * 
  *         '''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- * 
+ *             if not self._isOpen():
+ *                 raise ValueError( "I/O operation on closed file" )
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_6header_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_6header_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_6header_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_6header_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_6header___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_6header___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_v_result = NULL;
   PyObject *__pyx_v_t = NULL;
   PyObject *__pyx_v_line = NULL;
   PyObject *__pyx_v_fields = NULL;
   PyObject *__pyx_v_record = NULL;
   PyObject *__pyx_v_x = NULL;
+  PyObject *__pyx_v_idx = NULL;
   PyObject *__pyx_v_field = NULL;
   PyObject *__pyx_v_key = NULL;
   PyObject *__pyx_v_value = NULL;
@@ -12362,88 +12597,97 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
   PyObject *(*__pyx_t_11)(PyObject *);
   PyObject *__pyx_t_12 = NULL;
   PyObject *__pyx_t_13 = NULL;
-  PyObject *(*__pyx_t_14)(PyObject *);
+  PyObject *__pyx_t_14 = NULL;
+  PyObject *(*__pyx_t_15)(PyObject *);
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 1019);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 1068);
 
-  /* "pysam/csamfile.pyx":1020
+  /* "pysam/calignmentfile.pyx":1069
  *         '''
  *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             if not self._isOpen():             # <<<<<<<<<<<<<<
+ *                 raise ValueError( "I/O operation on closed file" )
  * 
- *             result = {}
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__39, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/calignmentfile.pyx":1070
+ *         def __get__(self):
+ *             if not self._isOpen():
+ *                 raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ * 
+ *             result = {}
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__37, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1022
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
+  /* "pysam/calignmentfile.pyx":1072
+ *                 raise ValueError( "I/O operation on closed file" )
  * 
  *             result = {}             # <<<<<<<<<<<<<<
  * 
  *             if self.header.text != NULL:
  */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1072; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_result = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":1024
+  /* "pysam/calignmentfile.pyx":1074
  *             result = {}
  * 
  *             if self.header.text != NULL:             # <<<<<<<<<<<<<<
- *                 # convert to python string (note: call self.text to create 0-terminated string)
- *                 t = self.text
+ *                 # convert to python string (note: call self.text to
+ *                 # create 0-terminated string)
  */
   __pyx_t_4 = ((__pyx_v_self->header->text != NULL) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":1026
- *             if self.header.text != NULL:
- *                 # convert to python string (note: call self.text to create 0-terminated string)
+    /* "pysam/calignmentfile.pyx":1077
+ *                 # convert to python string (note: call self.text to
+ *                 # create 0-terminated string)
  *                 t = self.text             # <<<<<<<<<<<<<<
  *                 for line in t.split("\n"):
  *                     if not line.strip(): continue
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_text); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_text); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1077; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_v_t = __pyx_t_2;
     __pyx_t_2 = 0;
 
-    /* "pysam/csamfile.pyx":1027
- *                 # convert to python string (note: call self.text to create 0-terminated string)
+    /* "pysam/calignmentfile.pyx":1078
+ *                 # create 0-terminated string)
  *                 t = self.text
  *                 for line in t.split("\n"):             # <<<<<<<<<<<<<<
  *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
+ *                     assert line.startswith("@"), \
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_t, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_t, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1078; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__40, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__39, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1078; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (PyList_CheckExact(__pyx_t_1) || PyTuple_CheckExact(__pyx_t_1)) {
       __pyx_t_2 = __pyx_t_1; __Pyx_INCREF(__pyx_t_2); __pyx_t_5 = 0;
       __pyx_t_6 = NULL;
     } else {
-      __pyx_t_5 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1078; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __pyx_t_6 = Py_TYPE(__pyx_t_2)->tp_iternext;
     }
@@ -12452,16 +12696,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
       if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_2)) {
         if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_2)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1078; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1078; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_2)) {
         if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1078; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1078; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_1 = __pyx_t_6(__pyx_t_2);
@@ -12469,7 +12713,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1078; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -12478,96 +12722,112 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
       __Pyx_XDECREF_SET(__pyx_v_line, __pyx_t_1);
       __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":1028
+      /* "pysam/calignmentfile.pyx":1079
  *                 t = self.text
  *                 for line in t.split("\n"):
  *                     if not line.strip(): continue             # <<<<<<<<<<<<<<
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
- *                     fields = line[1:].split("\t")
+ *                     assert line.startswith("@"), \
+ *                         "header line without '@': '%s'" % line
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __pyx_t_3 = ((!__pyx_t_4) != 0);
       if (__pyx_t_3) {
         goto __pyx_L5_continue;
       }
 
-      /* "pysam/csamfile.pyx":1029
+      /* "pysam/calignmentfile.pyx":1080
  *                 for line in t.split("\n"):
  *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line             # <<<<<<<<<<<<<<
+ *                     assert line.startswith("@"), \             # <<<<<<<<<<<<<<
+ *                         "header line without '@': '%s'" % line
  *                     fields = line[1:].split("\t")
- *                     record = fields[0]
  */
       #ifndef CYTHON_WITHOUT_ASSERTIONS
       if (unlikely(!Py_OptimizeFlag)) {
-        __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_tuple__42, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_tuple__41, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         if (unlikely(!__pyx_t_3)) {
-          __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_header_line_without_s, __pyx_v_line); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+          /* "pysam/calignmentfile.pyx":1081
+ *                     if not line.strip(): continue
+ *                     assert line.startswith("@"), \
+ *                         "header line without '@': '%s'" % line             # <<<<<<<<<<<<<<
+ *                     fields = line[1:].split("\t")
+ *                     record = fields[0]
+ */
+          __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_header_line_without_s, __pyx_v_line); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1081; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_1);
           PyErr_SetObject(PyExc_AssertionError, __pyx_t_1);
           __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
       }
       #endif
 
-      /* "pysam/csamfile.pyx":1030
- *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
+      /* "pysam/calignmentfile.pyx":1082
+ *                     assert line.startswith("@"), \
+ *                         "header line without '@': '%s'" % line
  *                     fields = line[1:].split("\t")             # <<<<<<<<<<<<<<
  *                     record = fields[0]
- *                     assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)
+ *                     assert record in VALID_HEADER_TYPES, \
  */
-      __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_line, 1, 0, NULL, NULL, &__pyx_slice__43, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_line, 1, 0, NULL, NULL, &__pyx_slice__42, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_tuple__44, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_tuple__44, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __Pyx_XDECREF_SET(__pyx_v_fields, __pyx_t_1);
       __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":1031
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
+      /* "pysam/calignmentfile.pyx":1083
+ *                         "header line without '@': '%s'" % line
  *                     fields = line[1:].split("\t")
  *                     record = fields[0]             # <<<<<<<<<<<<<<
- *                     assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)
- * 
+ *                     assert record in VALID_HEADER_TYPES, \
+ *                         "header line with invalid type '%s': '%s'" % (record, line)
  */
-      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_fields, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_fields, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1083; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_XDECREF_SET(__pyx_v_record, __pyx_t_1);
       __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":1032
+      /* "pysam/calignmentfile.pyx":1084
  *                     fields = line[1:].split("\t")
  *                     record = fields[0]
- *                     assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)             # <<<<<<<<<<<<<<
+ *                     assert record in VALID_HEADER_TYPES, \             # <<<<<<<<<<<<<<
+ *                         "header line with invalid type '%s': '%s'" % (record, line)
  * 
- *                     # treat comments
  */
       #ifndef CYTHON_WITHOUT_ASSERTIONS
       if (unlikely(!Py_OptimizeFlag)) {
-        __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1084; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_record, __pyx_t_1, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_record, __pyx_t_1, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1084; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         if (unlikely(!(__pyx_t_3 != 0))) {
-          __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+          /* "pysam/calignmentfile.pyx":1085
+ *                     record = fields[0]
+ *                     assert record in VALID_HEADER_TYPES, \
+ *                         "header line with invalid type '%s': '%s'" % (record, line)             # <<<<<<<<<<<<<<
+ * 
+ *                     # treat comments
+ */
+          __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_1);
           __Pyx_INCREF(__pyx_v_record);
           PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_record);
@@ -12575,65 +12835,73 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
           __Pyx_INCREF(__pyx_v_line);
           PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_line);
           __Pyx_GIVEREF(__pyx_v_line);
-          __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_header_line_with_invalid_type_s, __pyx_t_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_header_line_with_invalid_type_s, __pyx_t_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_7);
           __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
           PyErr_SetObject(PyExc_AssertionError, __pyx_t_7);
           __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1084; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
       }
       #endif
 
-      /* "pysam/csamfile.pyx":1035
+      /* "pysam/calignmentfile.pyx":1088
  * 
  *                     # treat comments
  *                     if record == "CO":             # <<<<<<<<<<<<<<
- *                         if record not in result: result[record] = []
- *                         result[record].append( "\t".join( fields[1:] ) )
+ *                         if record not in result:
+ *                             result[record] = []
  */
-      __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_record, __pyx_n_s_CO, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_record, __pyx_n_s_CO, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1088; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       if (__pyx_t_3) {
 
-        /* "pysam/csamfile.pyx":1036
+        /* "pysam/calignmentfile.pyx":1089
  *                     # treat comments
  *                     if record == "CO":
- *                         if record not in result: result[record] = []             # <<<<<<<<<<<<<<
- *                         result[record].append( "\t".join( fields[1:] ) )
- *                         continue
+ *                         if record not in result:             # <<<<<<<<<<<<<<
+ *                             result[record] = []
+ *                         result[record].append("\t".join( fields[1:]))
  */
-        __pyx_t_3 = (__Pyx_PyDict_Contains(__pyx_v_record, __pyx_v_result, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = (__Pyx_PyDict_Contains(__pyx_v_record, __pyx_v_result, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1089; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_4 = (__pyx_t_3 != 0);
         if (__pyx_t_4) {
-          __pyx_t_7 = PyList_New(0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+          /* "pysam/calignmentfile.pyx":1090
+ *                     if record == "CO":
+ *                         if record not in result:
+ *                             result[record] = []             # <<<<<<<<<<<<<<
+ *                         result[record].append("\t".join( fields[1:]))
+ *                         continue
+ */
+          __pyx_t_7 = PyList_New(0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1090; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_7);
-          if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_record, __pyx_t_7) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_record, __pyx_t_7) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1090; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
           goto __pyx_L9;
         }
         __pyx_L9:;
 
-        /* "pysam/csamfile.pyx":1037
- *                     if record == "CO":
- *                         if record not in result: result[record] = []
- *                         result[record].append( "\t".join( fields[1:] ) )             # <<<<<<<<<<<<<<
+        /* "pysam/calignmentfile.pyx":1091
+ *                         if record not in result:
+ *                             result[record] = []
+ *                         result[record].append("\t".join( fields[1:]))             # <<<<<<<<<<<<<<
  *                         continue
  *                     # the following is clumsy as generators do not work?
  */
-        __pyx_t_7 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_7 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_fields, 1, 0, NULL, NULL, &__pyx_slice__45, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_fields, 1, 0, NULL, NULL, &__pyx_slice__45, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_8 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyString_Join(__pyx_kp_s__43, __pyx_t_1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_9 = __Pyx_PyObject_Append(__pyx_t_7, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_Append(__pyx_t_7, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
 
-        /* "pysam/csamfile.pyx":1038
- *                         if record not in result: result[record] = []
- *                         result[record].append( "\t".join( fields[1:] ) )
+        /* "pysam/calignmentfile.pyx":1092
+ *                             result[record] = []
+ *                         result[record].append("\t".join( fields[1:]))
  *                         continue             # <<<<<<<<<<<<<<
  *                     # the following is clumsy as generators do not work?
  *                     x = {}
@@ -12641,105 +12909,114 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
         goto __pyx_L5_continue;
       }
 
-      /* "pysam/csamfile.pyx":1040
+      /* "pysam/calignmentfile.pyx":1094
  *                         continue
  *                     # the following is clumsy as generators do not work?
  *                     x = {}             # <<<<<<<<<<<<<<
- *                     for field in fields[1:]:
- *                         if ":" not in field:
+ * 
+ *                     for idx, field in enumerate(fields[1:]):
  */
-      __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1040; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1094; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_XDECREF_SET(__pyx_v_x, ((PyObject*)__pyx_t_8));
       __pyx_t_8 = 0;
 
-      /* "pysam/csamfile.pyx":1041
- *                     # the following is clumsy as generators do not work?
+      /* "pysam/calignmentfile.pyx":1096
  *                     x = {}
- *                     for field in fields[1:]:             # <<<<<<<<<<<<<<
+ * 
+ *                     for idx, field in enumerate(fields[1:]):             # <<<<<<<<<<<<<<
  *                         if ":" not in field:
  *                             raise ValueError("malformatted header: no ':' in field" )
  */
-      __pyx_t_8 = __Pyx_PyObject_GetSlice(__pyx_v_fields, 1, 0, NULL, NULL, &__pyx_slice__46, 1, 0, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      if (PyList_CheckExact(__pyx_t_8) || PyTuple_CheckExact(__pyx_t_8)) {
-        __pyx_t_7 = __pyx_t_8; __Pyx_INCREF(__pyx_t_7); __pyx_t_10 = 0;
+      __Pyx_INCREF(__pyx_int_0);
+      __pyx_t_8 = __pyx_int_0;
+      __pyx_t_7 = __Pyx_PyObject_GetSlice(__pyx_v_fields, 1, 0, NULL, NULL, &__pyx_slice__46, 1, 0, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_7);
+      if (PyList_CheckExact(__pyx_t_7) || PyTuple_CheckExact(__pyx_t_7)) {
+        __pyx_t_1 = __pyx_t_7; __Pyx_INCREF(__pyx_t_1); __pyx_t_10 = 0;
         __pyx_t_11 = NULL;
       } else {
-        __pyx_t_10 = -1; __pyx_t_7 = PyObject_GetIter(__pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_11 = Py_TYPE(__pyx_t_7)->tp_iternext;
+        __pyx_t_10 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_1);
+        __pyx_t_11 = Py_TYPE(__pyx_t_1)->tp_iternext;
       }
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       for (;;) {
-        if (!__pyx_t_11 && PyList_CheckExact(__pyx_t_7)) {
-          if (__pyx_t_10 >= PyList_GET_SIZE(__pyx_t_7)) break;
+        if (!__pyx_t_11 && PyList_CheckExact(__pyx_t_1)) {
+          if (__pyx_t_10 >= PyList_GET_SIZE(__pyx_t_1)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_8 = PyList_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_7 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_10); __Pyx_INCREF(__pyx_t_7); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_7 = PySequence_ITEM(__pyx_t_1, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
-        } else if (!__pyx_t_11 && PyTuple_CheckExact(__pyx_t_7)) {
-          if (__pyx_t_10 >= PyTuple_GET_SIZE(__pyx_t_7)) break;
+        } else if (!__pyx_t_11 && PyTuple_CheckExact(__pyx_t_1)) {
+          if (__pyx_t_10 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_10); __Pyx_INCREF(__pyx_t_7); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_7 = PySequence_ITEM(__pyx_t_1, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else {
-          __pyx_t_8 = __pyx_t_11(__pyx_t_7);
-          if (unlikely(!__pyx_t_8)) {
+          __pyx_t_7 = __pyx_t_11(__pyx_t_1);
+          if (unlikely(!__pyx_t_7)) {
             PyObject* exc_type = PyErr_Occurred();
             if (exc_type) {
               if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             }
             break;
           }
-          __Pyx_GOTREF(__pyx_t_8);
+          __Pyx_GOTREF(__pyx_t_7);
         }
-        __Pyx_XDECREF_SET(__pyx_v_field, __pyx_t_8);
-        __pyx_t_8 = 0;
+        __Pyx_XDECREF_SET(__pyx_v_field, __pyx_t_7);
+        __pyx_t_7 = 0;
+        __Pyx_INCREF(__pyx_t_8);
+        __Pyx_XDECREF_SET(__pyx_v_idx, __pyx_t_8);
+        __pyx_t_7 = PyNumber_Add(__pyx_t_8, __pyx_int_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_7);
+        __Pyx_DECREF(__pyx_t_8);
+        __pyx_t_8 = __pyx_t_7;
+        __pyx_t_7 = 0;
 
-        /* "pysam/csamfile.pyx":1042
- *                     x = {}
- *                     for field in fields[1:]:
+        /* "pysam/calignmentfile.pyx":1097
+ * 
+ *                     for idx, field in enumerate(fields[1:]):
  *                         if ":" not in field:             # <<<<<<<<<<<<<<
  *                             raise ValueError("malformatted header: no ':' in field" )
- *                         key, value = field.split(":",1)
+ *                         key, value = field.split(":", 1)
  */
-        __pyx_t_4 = (__Pyx_PySequence_Contains(__pyx_kp_s__47, __pyx_v_field, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_4 = (__Pyx_PySequence_Contains(__pyx_kp_s__47, __pyx_v_field, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_3 = (__pyx_t_4 != 0);
         if (__pyx_t_3) {
 
-          /* "pysam/csamfile.pyx":1043
- *                     for field in fields[1:]:
+          /* "pysam/calignmentfile.pyx":1098
+ *                     for idx, field in enumerate(fields[1:]):
  *                         if ":" not in field:
  *                             raise ValueError("malformatted header: no ':' in field" )             # <<<<<<<<<<<<<<
- *                         key, value = field.split(":",1)
- *                         # uppercase keys must be valid
+ *                         key, value = field.split(":", 1)
+ *                         if key in ("CL",):
  */
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__48, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__48, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1098; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_7);
+          __Pyx_Raise(__pyx_t_7, 0, 0, 0);
+          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1098; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
 
-        /* "pysam/csamfile.pyx":1044
+        /* "pysam/calignmentfile.pyx":1099
  *                         if ":" not in field:
  *                             raise ValueError("malformatted header: no ':' in field" )
- *                         key, value = field.split(":",1)             # <<<<<<<<<<<<<<
- *                         # uppercase keys must be valid
- *                         # lowercase are permitted for user fields
+ *                         key, value = field.split(":", 1)             # <<<<<<<<<<<<<<
+ *                         if key in ("CL",):
+ *                             # special treatment for command line
  */
-        __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_field, __pyx_n_s_split); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_tuple__49, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        if ((likely(PyTuple_CheckExact(__pyx_t_1))) || (PyList_CheckExact(__pyx_t_1))) {
-          PyObject* sequence = __pyx_t_1;
+        __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_field, __pyx_n_s_split); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_7);
+        __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_tuple__49, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_12);
+        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+        if ((likely(PyTuple_CheckExact(__pyx_t_12))) || (PyList_CheckExact(__pyx_t_12))) {
+          PyObject* sequence = __pyx_t_12;
           #if CYTHON_COMPILING_IN_CPYTHON
           Py_ssize_t size = Py_SIZE(sequence);
           #else
@@ -12748,359 +13025,512 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
           if (unlikely(size != 2)) {
             if (size > 2) __Pyx_RaiseTooManyValuesError(2);
             else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-            {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           #if CYTHON_COMPILING_IN_CPYTHON
           if (likely(PyTuple_CheckExact(sequence))) {
-            __pyx_t_8 = PyTuple_GET_ITEM(sequence, 0); 
-            __pyx_t_12 = PyTuple_GET_ITEM(sequence, 1); 
+            __pyx_t_7 = PyTuple_GET_ITEM(sequence, 0); 
+            __pyx_t_13 = PyTuple_GET_ITEM(sequence, 1); 
           } else {
-            __pyx_t_8 = PyList_GET_ITEM(sequence, 0); 
-            __pyx_t_12 = PyList_GET_ITEM(sequence, 1); 
+            __pyx_t_7 = PyList_GET_ITEM(sequence, 0); 
+            __pyx_t_13 = PyList_GET_ITEM(sequence, 1); 
           }
-          __Pyx_INCREF(__pyx_t_8);
-          __Pyx_INCREF(__pyx_t_12);
+          __Pyx_INCREF(__pyx_t_7);
+          __Pyx_INCREF(__pyx_t_13);
           #else
-          __pyx_t_8 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __pyx_t_12 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_12);
+          __pyx_t_7 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_7);
+          __pyx_t_13 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_13);
           #endif
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
         } else {
           Py_ssize_t index = -1;
-          __pyx_t_13 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_14 = PyObject_GetIter(__pyx_t_12); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_14);
+          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
+          __pyx_t_15 = Py_TYPE(__pyx_t_14)->tp_iternext;
+          index = 0; __pyx_t_7 = __pyx_t_15(__pyx_t_14); if (unlikely(!__pyx_t_7)) goto __pyx_L13_unpacking_failed;
+          __Pyx_GOTREF(__pyx_t_7);
+          index = 1; __pyx_t_13 = __pyx_t_15(__pyx_t_14); if (unlikely(!__pyx_t_13)) goto __pyx_L13_unpacking_failed;
           __Pyx_GOTREF(__pyx_t_13);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __pyx_t_14 = Py_TYPE(__pyx_t_13)->tp_iternext;
-          index = 0; __pyx_t_8 = __pyx_t_14(__pyx_t_13); if (unlikely(!__pyx_t_8)) goto __pyx_L13_unpacking_failed;
-          __Pyx_GOTREF(__pyx_t_8);
-          index = 1; __pyx_t_12 = __pyx_t_14(__pyx_t_13); if (unlikely(!__pyx_t_12)) goto __pyx_L13_unpacking_failed;
-          __Pyx_GOTREF(__pyx_t_12);
-          if (__Pyx_IternextUnpackEndCheck(__pyx_t_14(__pyx_t_13), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_t_14 = NULL;
-          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+          if (__Pyx_IternextUnpackEndCheck(__pyx_t_15(__pyx_t_14), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_15 = NULL;
+          __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
           goto __pyx_L14_unpacking_done;
           __pyx_L13_unpacking_failed:;
-          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
-          __pyx_t_14 = NULL;
+          __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
+          __pyx_t_15 = NULL;
           if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __pyx_L14_unpacking_done:;
         }
-        __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_8);
-        __pyx_t_8 = 0;
-        __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_12);
-        __pyx_t_12 = 0;
+        __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_7);
+        __pyx_t_7 = 0;
+        __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_13);
+        __pyx_t_13 = 0;
 
-        /* "pysam/csamfile.pyx":1047
- *                         # uppercase keys must be valid
- *                         # lowercase are permitted for user fields
- *                         if key in VALID_HEADER_FIELDS[record]:             # <<<<<<<<<<<<<<
- *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
- *                         elif not key.isupper():
+        /* "pysam/calignmentfile.pyx":1100
+ *                             raise ValueError("malformatted header: no ':' in field" )
+ *                         key, value = field.split(":", 1)
+ *                         if key in ("CL",):             # <<<<<<<<<<<<<<
+ *                             # special treatment for command line
+ *                             # statements (CL). These might contain
  */
-        __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_FIELDS); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1047; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_12 = PyObject_GetItem(__pyx_t_1, __pyx_v_record); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1047; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_12);
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_key, __pyx_t_12, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1047; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_INCREF(__pyx_v_key);
+        __pyx_t_12 = __pyx_v_key;
+        __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_t_12, __pyx_n_s_CL, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1100; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
         __pyx_t_4 = (__pyx_t_3 != 0);
         if (__pyx_t_4) {
 
-          /* "pysam/csamfile.pyx":1048
- *                         # lowercase are permitted for user fields
- *                         if key in VALID_HEADER_FIELDS[record]:
+          /* "pysam/calignmentfile.pyx":1107
+ *                             # header. Thus, in contravention to the
+ *                             # SAM API, consume the rest of the line.
+ *                             key, value = "\t".join(fields[idx+1:]).split(":", 1)             # <<<<<<<<<<<<<<
+ *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
+ *                             break
+ */
+          __pyx_t_12 = PyNumber_Add(__pyx_v_idx, __pyx_int_1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_12);
+          __pyx_t_13 = __Pyx_PyObject_GetSlice(__pyx_v_fields, 0, 0, &__pyx_t_12, NULL, NULL, 0, 0, 1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_13);
+          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
+          __pyx_t_12 = __Pyx_PyString_Join(__pyx_kp_s__43, __pyx_t_13); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_12);
+          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+          __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_t_12, __pyx_n_s_split); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_13);
+          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
+          __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_13, __pyx_tuple__50, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_12);
+          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+          if ((likely(PyTuple_CheckExact(__pyx_t_12))) || (PyList_CheckExact(__pyx_t_12))) {
+            PyObject* sequence = __pyx_t_12;
+            #if CYTHON_COMPILING_IN_CPYTHON
+            Py_ssize_t size = Py_SIZE(sequence);
+            #else
+            Py_ssize_t size = PySequence_Size(sequence);
+            #endif
+            if (unlikely(size != 2)) {
+              if (size > 2) __Pyx_RaiseTooManyValuesError(2);
+              else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
+              {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            }
+            #if CYTHON_COMPILING_IN_CPYTHON
+            if (likely(PyTuple_CheckExact(sequence))) {
+              __pyx_t_13 = PyTuple_GET_ITEM(sequence, 0); 
+              __pyx_t_7 = PyTuple_GET_ITEM(sequence, 1); 
+            } else {
+              __pyx_t_13 = PyList_GET_ITEM(sequence, 0); 
+              __pyx_t_7 = PyList_GET_ITEM(sequence, 1); 
+            }
+            __Pyx_INCREF(__pyx_t_13);
+            __Pyx_INCREF(__pyx_t_7);
+            #else
+            __pyx_t_13 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            __Pyx_GOTREF(__pyx_t_13);
+            __pyx_t_7 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            __Pyx_GOTREF(__pyx_t_7);
+            #endif
+            __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
+          } else {
+            Py_ssize_t index = -1;
+            __pyx_t_14 = PyObject_GetIter(__pyx_t_12); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            __Pyx_GOTREF(__pyx_t_14);
+            __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
+            __pyx_t_15 = Py_TYPE(__pyx_t_14)->tp_iternext;
+            index = 0; __pyx_t_13 = __pyx_t_15(__pyx_t_14); if (unlikely(!__pyx_t_13)) goto __pyx_L16_unpacking_failed;
+            __Pyx_GOTREF(__pyx_t_13);
+            index = 1; __pyx_t_7 = __pyx_t_15(__pyx_t_14); if (unlikely(!__pyx_t_7)) goto __pyx_L16_unpacking_failed;
+            __Pyx_GOTREF(__pyx_t_7);
+            if (__Pyx_IternextUnpackEndCheck(__pyx_t_15(__pyx_t_14), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            __pyx_t_15 = NULL;
+            __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
+            goto __pyx_L17_unpacking_done;
+            __pyx_L16_unpacking_failed:;
+            __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
+            __pyx_t_15 = NULL;
+            if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
+            {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            __pyx_L17_unpacking_done:;
+          }
+          __Pyx_DECREF_SET(__pyx_v_key, __pyx_t_13);
+          __pyx_t_13 = 0;
+          __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_7);
+          __pyx_t_7 = 0;
+
+          /* "pysam/calignmentfile.pyx":1108
+ *                             # SAM API, consume the rest of the line.
+ *                             key, value = "\t".join(fields[idx+1:]).split(":", 1)
  *                             x[key] = VALID_HEADER_FIELDS[record][key](value)             # <<<<<<<<<<<<<<
- *                         elif not key.isupper():
- *                             x[key] = value
+ *                             break
+ * 
  */
-          __pyx_t_12 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_FIELDS); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_12 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_FIELDS); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_12);
-          __pyx_t_1 = PyObject_GetItem(__pyx_t_12, __pyx_v_record); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-          __Pyx_GOTREF(__pyx_t_1);
+          __pyx_t_7 = PyObject_GetItem(__pyx_t_12, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1108; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __Pyx_GOTREF(__pyx_t_7);
           __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          __pyx_t_12 = PyObject_GetItem(__pyx_t_1, __pyx_v_key); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_12 = PyObject_GetItem(__pyx_t_7, __pyx_v_key); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1108; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_12);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
+          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+          __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_7);
           __Pyx_INCREF(__pyx_v_value);
-          PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_value);
+          PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_value);
           __Pyx_GIVEREF(__pyx_v_value);
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
+          __pyx_t_13 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_7, NULL); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_13);
           __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          if (unlikely(PyDict_SetItem(__pyx_v_x, __pyx_v_key, __pyx_t_8) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          goto __pyx_L15;
+          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+          if (unlikely(PyDict_SetItem(__pyx_v_x, __pyx_v_key, __pyx_t_13) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+
+          /* "pysam/calignmentfile.pyx":1109
+ *                             key, value = "\t".join(fields[idx+1:]).split(":", 1)
+ *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
+ *                             break             # <<<<<<<<<<<<<<
+ * 
+ *                         # uppercase keys must be valid
+ */
+          goto __pyx_L11_break;
         }
 
-        /* "pysam/csamfile.pyx":1049
+        /* "pysam/calignmentfile.pyx":1112
+ * 
+ *                         # uppercase keys must be valid
+ *                         if key in VALID_HEADER_FIELDS[record]:             # <<<<<<<<<<<<<<
+ *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
+ *                         # lowercase are permitted for user fields
+ */
+        __pyx_t_13 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_FIELDS); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_13);
+        __pyx_t_7 = PyObject_GetItem(__pyx_t_13, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1112; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __Pyx_GOTREF(__pyx_t_7);
+        __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+        __pyx_t_4 = (__Pyx_PySequence_Contains(__pyx_v_key, __pyx_t_7, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+        __pyx_t_3 = (__pyx_t_4 != 0);
+        if (__pyx_t_3) {
+
+          /* "pysam/calignmentfile.pyx":1113
+ *                         # uppercase keys must be valid
  *                         if key in VALID_HEADER_FIELDS[record]:
+ *                             x[key] = VALID_HEADER_FIELDS[record][key](value)             # <<<<<<<<<<<<<<
+ *                         # lowercase are permitted for user fields
+ *                         elif not key.isupper():
+ */
+          __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_FIELDS); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_7);
+          __pyx_t_13 = PyObject_GetItem(__pyx_t_7, __pyx_v_record); if (unlikely(__pyx_t_13 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1113; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __Pyx_GOTREF(__pyx_t_13);
+          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+          __pyx_t_7 = PyObject_GetItem(__pyx_t_13, __pyx_v_key); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1113; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __Pyx_GOTREF(__pyx_t_7);
+          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+          __pyx_t_13 = PyTuple_New(1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_13);
+          __Pyx_INCREF(__pyx_v_value);
+          PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_v_value);
+          __Pyx_GIVEREF(__pyx_v_value);
+          __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_13, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_12);
+          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+          if (unlikely(PyDict_SetItem(__pyx_v_x, __pyx_v_key, __pyx_t_12) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
+          goto __pyx_L18;
+        }
+
+        /* "pysam/calignmentfile.pyx":1115
  *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
+ *                         # lowercase are permitted for user fields
  *                         elif not key.isupper():             # <<<<<<<<<<<<<<
  *                             x[key] = value
  *                         else:
  */
-        __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_isupper); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1049; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1049; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1049; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_3 = ((!__pyx_t_4) != 0);
-        if (__pyx_t_3) {
+        __pyx_t_12 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_isupper); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1115; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_12);
+        __pyx_t_13 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1115; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_13);
+        __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
+        __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_13); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1115; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+        __pyx_t_4 = ((!__pyx_t_3) != 0);
+        if (__pyx_t_4) {
 
-          /* "pysam/csamfile.pyx":1050
- *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
+          /* "pysam/calignmentfile.pyx":1116
+ *                         # lowercase are permitted for user fields
  *                         elif not key.isupper():
  *                             x[key] = value             # <<<<<<<<<<<<<<
  *                         else:
- *                             raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) )
+ *                             raise ValueError(
  */
-          if (unlikely(PyDict_SetItem(__pyx_v_x, __pyx_v_key, __pyx_v_value) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1050; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          goto __pyx_L15;
+          if (unlikely(PyDict_SetItem(__pyx_v_x, __pyx_v_key, __pyx_v_value) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1116; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          goto __pyx_L18;
         }
         /*else*/ {
 
-          /* "pysam/csamfile.pyx":1052
- *                             x[key] = value
- *                         else:
- *                             raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) )             # <<<<<<<<<<<<<<
+          /* "pysam/calignmentfile.pyx":1120
+ *                             raise ValueError(
+ *                                 "unknown field code '%s' in record '%s'" %
+ *                                 (key, record))             # <<<<<<<<<<<<<<
  * 
  *                     if VALID_HEADER_TYPES[record] == dict:
  */
-          __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
+          __pyx_t_13 = PyTuple_New(2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1120; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_13);
           __Pyx_INCREF(__pyx_v_key);
-          PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_key);
+          PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_v_key);
           __Pyx_GIVEREF(__pyx_v_key);
           __Pyx_INCREF(__pyx_v_record);
-          PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_record);
+          PyTuple_SET_ITEM(__pyx_t_13, 1, __pyx_v_record);
           __Pyx_GIVEREF(__pyx_v_record);
-          __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_unknown_field_code_s_in_record_s, __pyx_t_1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
-          PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_8);
-          __Pyx_GIVEREF(__pyx_t_8);
-          __pyx_t_8 = 0;
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+          /* "pysam/calignmentfile.pyx":1119
+ *                         else:
+ *                             raise ValueError(
+ *                                 "unknown field code '%s' in record '%s'" %             # <<<<<<<<<<<<<<
+ *                                 (key, record))
+ * 
+ */
+          __pyx_t_12 = __Pyx_PyString_Format(__pyx_kp_s_unknown_field_code_s_in_record_s, __pyx_t_13); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1119; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_12);
+          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+
+          /* "pysam/calignmentfile.pyx":1118
+ *                             x[key] = value
+ *                         else:
+ *                             raise ValueError(             # <<<<<<<<<<<<<<
+ *                                 "unknown field code '%s' in record '%s'" %
+ *                                 (key, record))
+ */
+          __pyx_t_13 = PyTuple_New(1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_13);
+          PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_t_12);
+          __Pyx_GIVEREF(__pyx_t_12);
+          __pyx_t_12 = 0;
+          __pyx_t_12 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_13, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_12);
+          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+          __Pyx_Raise(__pyx_t_12, 0, 0, 0);
+          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
-        __pyx_L15:;
+        __pyx_L18:;
       }
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+      __pyx_L11_break:;
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
 
-      /* "pysam/csamfile.pyx":1054
- *                             raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) )
+      /* "pysam/calignmentfile.pyx":1122
+ *                                 (key, record))
  * 
  *                     if VALID_HEADER_TYPES[record] == dict:             # <<<<<<<<<<<<<<
  *                         if record in result:
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )
+ *                             raise ValueError(
  */
-      __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_8 = PyObject_GetItem(__pyx_t_7, __pyx_v_record); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_8, ((PyObject *)((PyObject*)(&PyDict_Type))), Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyObject_GetItem(__pyx_t_8, __pyx_v_record); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      if (__pyx_t_3) {
+      __pyx_t_8 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)((PyObject*)(&PyDict_Type))), Py_EQ); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      if (__pyx_t_4) {
 
-        /* "pysam/csamfile.pyx":1055
+        /* "pysam/calignmentfile.pyx":1123
  * 
  *                     if VALID_HEADER_TYPES[record] == dict:
  *                         if record in result:             # <<<<<<<<<<<<<<
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )
+ *                             raise ValueError(
+ *                                 "multiple '%s' lines are not permitted" % record)
+ */
+        __pyx_t_4 = (__Pyx_PyDict_Contains(__pyx_v_record, __pyx_v_result, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = (__pyx_t_4 != 0);
+        if (__pyx_t_3) {
+
+          /* "pysam/calignmentfile.pyx":1125
+ *                         if record in result:
+ *                             raise ValueError(
+ *                                 "multiple '%s' lines are not permitted" % record)             # <<<<<<<<<<<<<<
+ * 
  *                         result[record] = x
  */
-        __pyx_t_3 = (__Pyx_PyDict_Contains(__pyx_v_record, __pyx_v_result, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1055; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_4 = (__pyx_t_3 != 0);
-        if (__pyx_t_4) {
+          __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_multiple_s_lines_are_not_permitt, __pyx_v_record); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_8);
 
-          /* "pysam/csamfile.pyx":1056
+          /* "pysam/calignmentfile.pyx":1124
  *                     if VALID_HEADER_TYPES[record] == dict:
  *                         if record in result:
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )             # <<<<<<<<<<<<<<
- *                         result[record] = x
- *                     elif VALID_HEADER_TYPES[record] == list:
+ *                             raise ValueError(             # <<<<<<<<<<<<<<
+ *                                 "multiple '%s' lines are not permitted" % record)
+ * 
  */
-          __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_multiple_s_lines_are_not_permitt, __pyx_v_record); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_1);
+          PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_8);
+          __Pyx_GIVEREF(__pyx_t_8);
+          __pyx_t_8 = 0;
+          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_8);
-          PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_7);
-          __Pyx_GIVEREF(__pyx_t_7);
-          __pyx_t_7 = 0;
-          __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_8, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_7);
+          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+          __Pyx_Raise(__pyx_t_8, 0, 0, 0);
           __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          __Pyx_Raise(__pyx_t_7, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
 
-        /* "pysam/csamfile.pyx":1057
- *                         if record in result:
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )
+        /* "pysam/calignmentfile.pyx":1127
+ *                                 "multiple '%s' lines are not permitted" % record)
+ * 
  *                         result[record] = x             # <<<<<<<<<<<<<<
  *                     elif VALID_HEADER_TYPES[record] == list:
  *                         if record not in result: result[record] = []
  */
-        if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_record, __pyx_v_x) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        goto __pyx_L16;
+        if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_record, __pyx_v_x) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1127; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        goto __pyx_L19;
       }
 
-      /* "pysam/csamfile.pyx":1058
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )
+      /* "pysam/calignmentfile.pyx":1128
+ * 
  *                         result[record] = x
  *                     elif VALID_HEADER_TYPES[record] == list:             # <<<<<<<<<<<<<<
  *                         if record not in result: result[record] = []
- *                         result[record].append( x )
+ *                         result[record].append(x)
  */
-      __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1058; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_8 = PyObject_GetItem(__pyx_t_7, __pyx_v_record); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1058; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_8, ((PyObject *)((PyObject*)(&PyList_Type))), Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1058; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyObject_GetItem(__pyx_t_8, __pyx_v_record); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1058; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      if (__pyx_t_4) {
+      __pyx_t_8 = PyObject_RichCompare(__pyx_t_1, ((PyObject *)((PyObject*)(&PyList_Type))), Py_EQ); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      if (__pyx_t_3) {
 
-        /* "pysam/csamfile.pyx":1059
+        /* "pysam/calignmentfile.pyx":1129
  *                         result[record] = x
  *                     elif VALID_HEADER_TYPES[record] == list:
  *                         if record not in result: result[record] = []             # <<<<<<<<<<<<<<
- *                         result[record].append( x )
+ *                         result[record].append(x)
  * 
  */
-        __pyx_t_4 = (__Pyx_PyDict_Contains(__pyx_v_record, __pyx_v_result, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_3 = (__pyx_t_4 != 0);
-        if (__pyx_t_3) {
-          __pyx_t_7 = PyList_New(0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_record, __pyx_t_7) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          goto __pyx_L18;
+        __pyx_t_3 = (__Pyx_PyDict_Contains(__pyx_v_record, __pyx_v_result, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_4 = (__pyx_t_3 != 0);
+        if (__pyx_t_4) {
+          __pyx_t_8 = PyList_New(0); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_8);
+          if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_record, __pyx_t_8) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+          goto __pyx_L21;
         }
-        __pyx_L18:;
+        __pyx_L21:;
 
-        /* "pysam/csamfile.pyx":1060
+        /* "pysam/calignmentfile.pyx":1130
  *                     elif VALID_HEADER_TYPES[record] == list:
  *                         if record not in result: result[record] = []
- *                         result[record].append( x )             # <<<<<<<<<<<<<<
+ *                         result[record].append(x)             # <<<<<<<<<<<<<<
  * 
  *                 # if there are no SQ lines in the header, add the reference names
  */
-        __pyx_t_7 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1060; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_9 = __Pyx_PyObject_Append(__pyx_t_7, __pyx_v_x); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1060; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        goto __pyx_L16;
+        __pyx_t_8 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_v_record); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1130; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __Pyx_GOTREF(__pyx_t_8);
+        __pyx_t_9 = __Pyx_PyObject_Append(__pyx_t_8, __pyx_v_x); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+        goto __pyx_L19;
       }
-      __pyx_L16:;
+      __pyx_L19:;
       __pyx_L5_continue:;
     }
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-    /* "pysam/csamfile.pyx":1068
- *                 # SQ lines, the SQ information is not part of the textual header and thus
- *                 # are missing from the output. See issue 84.
+    /* "pysam/calignmentfile.pyx":1140
+ *                 # the SQ information is not part of the textual header
+ *                 # and thus are missing from the output. See issue 84.
  *                 if "SQ" not in result:             # <<<<<<<<<<<<<<
  *                     sq = []
- *                     for ref, length in zip( self.references, self.lengths ):
+ *                     for ref, length in zip(self.references, self.lengths):
  */
-    __pyx_t_3 = (__Pyx_PyDict_Contains(__pyx_n_s_SQ, __pyx_v_result, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1068; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = (__pyx_t_3 != 0);
-    if (__pyx_t_4) {
+    __pyx_t_4 = (__Pyx_PyDict_Contains(__pyx_n_s_SQ, __pyx_v_result, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = (__pyx_t_4 != 0);
+    if (__pyx_t_3) {
 
-      /* "pysam/csamfile.pyx":1069
- *                 # are missing from the output. See issue 84.
+      /* "pysam/calignmentfile.pyx":1141
+ *                 # and thus are missing from the output. See issue 84.
  *                 if "SQ" not in result:
  *                     sq = []             # <<<<<<<<<<<<<<
- *                     for ref, length in zip( self.references, self.lengths ):
- *                         sq.append( {'LN': length, 'SN': ref } )
+ *                     for ref, length in zip(self.references, self.lengths):
+ *                         sq.append({'LN': length, 'SN': ref })
  */
-      __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __pyx_v_sq = ((PyObject*)__pyx_t_2);
       __pyx_t_2 = 0;
 
-      /* "pysam/csamfile.pyx":1070
+      /* "pysam/calignmentfile.pyx":1142
  *                 if "SQ" not in result:
  *                     sq = []
- *                     for ref, length in zip( self.references, self.lengths ):             # <<<<<<<<<<<<<<
- *                         sq.append( {'LN': length, 'SN': ref } )
+ *                     for ref, length in zip(self.references, self.lengths):             # <<<<<<<<<<<<<<
+ *                         sq.append({'LN': length, 'SN': ref })
  *                     result["SQ"] = sq
  */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_references); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_references); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_lengths); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_lengths); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_2);
+      __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_1);
+      PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
-      PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_7);
-      __Pyx_GIVEREF(__pyx_t_7);
+      PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_8);
+      __Pyx_GIVEREF(__pyx_t_8);
       __pyx_t_2 = 0;
-      __pyx_t_7 = 0;
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_zip, __pyx_t_8, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      if (PyList_CheckExact(__pyx_t_7) || PyTuple_CheckExact(__pyx_t_7)) {
-        __pyx_t_8 = __pyx_t_7; __Pyx_INCREF(__pyx_t_8); __pyx_t_5 = 0;
+      __pyx_t_8 = 0;
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_zip, __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      if (PyList_CheckExact(__pyx_t_8) || PyTuple_CheckExact(__pyx_t_8)) {
+        __pyx_t_1 = __pyx_t_8; __Pyx_INCREF(__pyx_t_1); __pyx_t_5 = 0;
         __pyx_t_6 = NULL;
       } else {
-        __pyx_t_5 = -1; __pyx_t_8 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_6 = Py_TYPE(__pyx_t_8)->tp_iternext;
+        __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_8); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_1);
+        __pyx_t_6 = Py_TYPE(__pyx_t_1)->tp_iternext;
       }
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       for (;;) {
-        if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_8)) {
-          if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_8)) break;
+        if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_1)) {
+          if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_1)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_7 = PyList_GET_ITEM(__pyx_t_8, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_8); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_7 = PySequence_ITEM(__pyx_t_8, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
-        } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_8)) {
-          if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_8)) break;
+        } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_1)) {
+          if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_8, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_8); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_7 = PySequence_ITEM(__pyx_t_8, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else {
-          __pyx_t_7 = __pyx_t_6(__pyx_t_8);
-          if (unlikely(!__pyx_t_7)) {
+          __pyx_t_8 = __pyx_t_6(__pyx_t_1);
+          if (unlikely(!__pyx_t_8)) {
             PyObject* exc_type = PyErr_Occurred();
             if (exc_type) {
               if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             }
             break;
           }
-          __Pyx_GOTREF(__pyx_t_7);
+          __Pyx_GOTREF(__pyx_t_8);
         }
-        if ((likely(PyTuple_CheckExact(__pyx_t_7))) || (PyList_CheckExact(__pyx_t_7))) {
-          PyObject* sequence = __pyx_t_7;
+        if ((likely(PyTuple_CheckExact(__pyx_t_8))) || (PyList_CheckExact(__pyx_t_8))) {
+          PyObject* sequence = __pyx_t_8;
           #if CYTHON_COMPILING_IN_CPYTHON
           Py_ssize_t size = Py_SIZE(sequence);
           #else
@@ -13109,100 +13539,100 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
           if (unlikely(size != 2)) {
             if (size > 2) __Pyx_RaiseTooManyValuesError(2);
             else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-            {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           #if CYTHON_COMPILING_IN_CPYTHON
           if (likely(PyTuple_CheckExact(sequence))) {
             __pyx_t_2 = PyTuple_GET_ITEM(sequence, 0); 
-            __pyx_t_1 = PyTuple_GET_ITEM(sequence, 1); 
+            __pyx_t_12 = PyTuple_GET_ITEM(sequence, 1); 
           } else {
             __pyx_t_2 = PyList_GET_ITEM(sequence, 0); 
-            __pyx_t_1 = PyList_GET_ITEM(sequence, 1); 
+            __pyx_t_12 = PyList_GET_ITEM(sequence, 1); 
           }
           __Pyx_INCREF(__pyx_t_2);
-          __Pyx_INCREF(__pyx_t_1);
+          __Pyx_INCREF(__pyx_t_12);
           #else
-          __pyx_t_2 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_2);
-          __pyx_t_1 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
+          __pyx_t_12 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_12);
           #endif
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
         } else {
           Py_ssize_t index = -1;
-          __pyx_t_12 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_12);
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          __pyx_t_14 = Py_TYPE(__pyx_t_12)->tp_iternext;
-          index = 0; __pyx_t_2 = __pyx_t_14(__pyx_t_12); if (unlikely(!__pyx_t_2)) goto __pyx_L22_unpacking_failed;
+          __pyx_t_13 = PyObject_GetIter(__pyx_t_8); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_13);
+          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+          __pyx_t_15 = Py_TYPE(__pyx_t_13)->tp_iternext;
+          index = 0; __pyx_t_2 = __pyx_t_15(__pyx_t_13); if (unlikely(!__pyx_t_2)) goto __pyx_L25_unpacking_failed;
           __Pyx_GOTREF(__pyx_t_2);
-          index = 1; __pyx_t_1 = __pyx_t_14(__pyx_t_12); if (unlikely(!__pyx_t_1)) goto __pyx_L22_unpacking_failed;
-          __Pyx_GOTREF(__pyx_t_1);
-          if (__Pyx_IternextUnpackEndCheck(__pyx_t_14(__pyx_t_12), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_t_14 = NULL;
-          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          goto __pyx_L23_unpacking_done;
-          __pyx_L22_unpacking_failed:;
-          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          __pyx_t_14 = NULL;
+          index = 1; __pyx_t_12 = __pyx_t_15(__pyx_t_13); if (unlikely(!__pyx_t_12)) goto __pyx_L25_unpacking_failed;
+          __Pyx_GOTREF(__pyx_t_12);
+          if (__Pyx_IternextUnpackEndCheck(__pyx_t_15(__pyx_t_13), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_15 = NULL;
+          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+          goto __pyx_L26_unpacking_done;
+          __pyx_L25_unpacking_failed:;
+          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+          __pyx_t_15 = NULL;
           if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_L23_unpacking_done:;
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_L26_unpacking_done:;
         }
         __Pyx_XDECREF_SET(__pyx_v_ref, __pyx_t_2);
         __pyx_t_2 = 0;
-        __Pyx_XDECREF_SET(__pyx_v_length, __pyx_t_1);
-        __pyx_t_1 = 0;
+        __Pyx_XDECREF_SET(__pyx_v_length, __pyx_t_12);
+        __pyx_t_12 = 0;
 
-        /* "pysam/csamfile.pyx":1071
+        /* "pysam/calignmentfile.pyx":1143
  *                     sq = []
- *                     for ref, length in zip( self.references, self.lengths ):
- *                         sq.append( {'LN': length, 'SN': ref } )             # <<<<<<<<<<<<<<
+ *                     for ref, length in zip(self.references, self.lengths):
+ *                         sq.append({'LN': length, 'SN': ref })             # <<<<<<<<<<<<<<
  *                     result["SQ"] = sq
  * 
  */
-        __pyx_t_7 = PyDict_New(); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        if (PyDict_SetItem(__pyx_t_7, __pyx_n_s_LN, __pyx_v_length) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        if (PyDict_SetItem(__pyx_t_7, __pyx_n_s_SN, __pyx_v_ref) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_sq, __pyx_t_7); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+        __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1143; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_8);
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_LN, __pyx_v_length) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1143; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_SN, __pyx_v_ref) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1143; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_sq, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1143; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       }
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":1072
- *                     for ref, length in zip( self.references, self.lengths ):
- *                         sq.append( {'LN': length, 'SN': ref } )
+      /* "pysam/calignmentfile.pyx":1144
+ *                     for ref, length in zip(self.references, self.lengths):
+ *                         sq.append({'LN': length, 'SN': ref })
  *                     result["SQ"] = sq             # <<<<<<<<<<<<<<
  * 
  *             return result
  */
-      if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_n_s_SQ, __pyx_v_sq) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1072; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      goto __pyx_L19;
+      if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_n_s_SQ, __pyx_v_sq) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      goto __pyx_L22;
     }
-    __pyx_L19:;
+    __pyx_L22:;
     goto __pyx_L4;
   }
   __pyx_L4:;
 
-  /* "pysam/csamfile.pyx":1074
+  /* "pysam/calignmentfile.pyx":1146
  *                     result["SQ"] = sq
  * 
  *             return result             # <<<<<<<<<<<<<<
  * 
- *     def _buildLine( self, fields, record ):
+ *     def _buildLine(self, fields, record):
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(__pyx_v_result);
   __pyx_r = __pyx_v_result;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1019
- *         a two-level dictionary.
+  /* "pysam/calignmentfile.pyx":1068
+ * 
  *         '''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- * 
+ *             if not self._isOpen():
+ *                 raise ValueError( "I/O operation on closed file" )
  */
 
   /* function exit code */
@@ -13213,7 +13643,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
   __Pyx_XDECREF(__pyx_t_8);
   __Pyx_XDECREF(__pyx_t_12);
   __Pyx_XDECREF(__pyx_t_13);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.header.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_14);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.header.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_result);
@@ -13222,6 +13653,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
   __Pyx_XDECREF(__pyx_v_fields);
   __Pyx_XDECREF(__pyx_v_record);
   __Pyx_XDECREF(__pyx_v_x);
+  __Pyx_XDECREF(__pyx_v_idx);
   __Pyx_XDECREF(__pyx_v_field);
   __Pyx_XDECREF(__pyx_v_key);
   __Pyx_XDECREF(__pyx_v_value);
@@ -13234,18 +13666,18 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1076
+/* "pysam/calignmentfile.pyx":1148
  *             return result
  * 
- *     def _buildLine( self, fields, record ):             # <<<<<<<<<<<<<<
+ *     def _buildLine(self, fields, record):             # <<<<<<<<<<<<<<
  *         '''build a header line from *fields* dictionary for *record*'''
  * 
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_43_buildLine(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_42_buildLine[] = "Samfile._buildLine(self, fields, record)\nbuild a header line from *fields* dictionary for *record*";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_43_buildLine(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_43_buildLine(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_42_buildLine[] = "AlignmentFile._buildLine(self, fields, record)\nbuild a header line from *fields* dictionary for *record*";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_43_buildLine(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_fields = 0;
   PyObject *__pyx_v_record = 0;
   int __pyx_lineno = 0;
@@ -13274,11 +13706,11 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_43_buildLine(PyObject *__pyx
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_record)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("_buildLine", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1076; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("_buildLine", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1148; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_buildLine") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1076; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_buildLine") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1148; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -13291,20 +13723,20 @@ static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_43_buildLine(PyObject *__pyx
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_buildLine", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1076; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("_buildLine", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1148; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._buildLine", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._buildLine", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_fields, __pyx_v_record);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_42_buildLine(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self), __pyx_v_fields, __pyx_v_record);
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_fields, PyObject *__pyx_v_record) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_42_buildLine(CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_fields, PyObject *__pyx_v_record) {
   PyObject *__pyx_v_line = NULL;
   PyObject *__pyx_v_key = NULL;
   PyObject *__pyx_r = NULL;
@@ -13322,18 +13754,18 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_buildLine", 0);
-  __Pyx_TraceCall("_buildLine", __pyx_f[0], 1076);
+  __Pyx_TraceCall("_buildLine", __pyx_f[0], 1148);
 
-  /* "pysam/csamfile.pyx":1080
+  /* "pysam/calignmentfile.pyx":1152
  * 
  *         # TODO: add checking for field and sort order
- *         line = ["@%s" % record ]             # <<<<<<<<<<<<<<
+ *         line = ["@%s" % record]             # <<<<<<<<<<<<<<
  *         # comment
  *         if record == "CO":
  */
-  __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_s, __pyx_v_record); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_s, __pyx_v_record); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1152; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1152; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   PyList_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
@@ -13341,63 +13773,63 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
   __pyx_v_line = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":1082
- *         line = ["@%s" % record ]
+  /* "pysam/calignmentfile.pyx":1154
+ *         line = ["@%s" % record]
  *         # comment
  *         if record == "CO":             # <<<<<<<<<<<<<<
- *             line.append( fields )
+ *             line.append(fields)
  *         # user tags
  */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_record, __pyx_n_s_CO, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_record, __pyx_n_s_CO, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":1083
+    /* "pysam/calignmentfile.pyx":1155
  *         # comment
  *         if record == "CO":
- *             line.append( fields )             # <<<<<<<<<<<<<<
+ *             line.append(fields)             # <<<<<<<<<<<<<<
  *         # user tags
  *         elif record.islower():
  */
-    __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_v_fields); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1083; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_v_fields); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     goto __pyx_L3;
   }
 
-  /* "pysam/csamfile.pyx":1085
- *             line.append( fields )
+  /* "pysam/calignmentfile.pyx":1157
+ *             line.append(fields)
  *         # user tags
  *         elif record.islower():             # <<<<<<<<<<<<<<
  *             for key in sorted(fields):
- *                 line.append( "%s:%s" % (key, str(fields[key])))
+ *                 line.append("%s:%s" % (key, str(fields[key])))
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_record, __pyx_n_s_islower); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_record, __pyx_n_s_islower); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1157; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1157; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1157; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":1086
+    /* "pysam/calignmentfile.pyx":1158
  *         # user tags
  *         elif record.islower():
  *             for key in sorted(fields):             # <<<<<<<<<<<<<<
- *                 line.append( "%s:%s" % (key, str(fields[key])))
+ *                 line.append("%s:%s" % (key, str(fields[key])))
  *         # defined tags
  */
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_v_fields);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_fields);
     __Pyx_GIVEREF(__pyx_v_fields);
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_sorted, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_sorted, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) {
       __pyx_t_1 = __pyx_t_2; __Pyx_INCREF(__pyx_t_1); __pyx_t_5 = 0;
       __pyx_t_6 = NULL;
     } else {
-      __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __pyx_t_6 = Py_TYPE(__pyx_t_1)->tp_iternext;
     }
@@ -13406,16 +13838,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
       if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_1)) {
         if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_1)) {
         if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_2 = __pyx_t_6(__pyx_t_1);
@@ -13423,7 +13855,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -13432,24 +13864,24 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
       __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_2);
       __pyx_t_2 = 0;
 
-      /* "pysam/csamfile.pyx":1087
+      /* "pysam/calignmentfile.pyx":1159
  *         elif record.islower():
  *             for key in sorted(fields):
- *                 line.append( "%s:%s" % (key, str(fields[key])))             # <<<<<<<<<<<<<<
+ *                 line.append("%s:%s" % (key, str(fields[key])))             # <<<<<<<<<<<<<<
  *         # defined tags
  *         else:
  */
-      __pyx_t_2 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_2 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1159; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_INCREF(__pyx_v_key);
       PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_key);
@@ -13457,10 +13889,10 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
       PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     }
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -13468,23 +13900,23 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":1091
+    /* "pysam/calignmentfile.pyx":1163
  *         else:
  *             # write fields of the specification
  *             for key in VALID_HEADER_ORDER[record]:             # <<<<<<<<<<<<<<
  *                 if key in fields:
- *                     line.append( "%s:%s" % (key, str(fields[key])))
+ *                     line.append("%s:%s" % (key, str(fields[key])))
  */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_ORDER); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_ORDER); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyObject_GetItem(__pyx_t_1, __pyx_v_record); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = PyObject_GetItem(__pyx_t_1, __pyx_v_record); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) {
       __pyx_t_1 = __pyx_t_2; __Pyx_INCREF(__pyx_t_1); __pyx_t_5 = 0;
       __pyx_t_6 = NULL;
     } else {
-      __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __pyx_t_6 = Py_TYPE(__pyx_t_1)->tp_iternext;
     }
@@ -13493,16 +13925,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
       if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_1)) {
         if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_1)) {
         if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_2 = __pyx_t_6(__pyx_t_1);
@@ -13510,7 +13942,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -13519,35 +13951,35 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
       __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_2);
       __pyx_t_2 = 0;
 
-      /* "pysam/csamfile.pyx":1092
+      /* "pysam/calignmentfile.pyx":1164
  *             # write fields of the specification
  *             for key in VALID_HEADER_ORDER[record]:
  *                 if key in fields:             # <<<<<<<<<<<<<<
- *                     line.append( "%s:%s" % (key, str(fields[key])))
+ *                     line.append("%s:%s" % (key, str(fields[key])))
  *             # write user fields
  */
-      __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_key, __pyx_v_fields, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1092; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_key, __pyx_v_fields, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_8 = (__pyx_t_3 != 0);
       if (__pyx_t_8) {
 
-        /* "pysam/csamfile.pyx":1093
+        /* "pysam/calignmentfile.pyx":1165
  *             for key in VALID_HEADER_ORDER[record]:
  *                 if key in fields:
- *                     line.append( "%s:%s" % (key, str(fields[key])))             # <<<<<<<<<<<<<<
+ *                     line.append("%s:%s" % (key, str(fields[key])))             # <<<<<<<<<<<<<<
  *             # write user fields
  *             for key in fields:
  */
-        __pyx_t_2 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_2 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_2);
         __Pyx_GIVEREF(__pyx_t_2);
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_INCREF(__pyx_v_key);
         PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_key);
@@ -13555,10 +13987,10 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
         PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_2);
         __Pyx_GIVEREF(__pyx_t_2);
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         goto __pyx_L8;
       }
@@ -13566,18 +13998,18 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
     }
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":1095
- *                     line.append( "%s:%s" % (key, str(fields[key])))
+    /* "pysam/calignmentfile.pyx":1167
+ *                     line.append("%s:%s" % (key, str(fields[key])))
  *             # write user fields
  *             for key in fields:             # <<<<<<<<<<<<<<
  *                 if not key.isupper():
- *                     line.append( "%s:%s" % (key, str(fields[key])))
+ *                     line.append("%s:%s" % (key, str(fields[key])))
  */
     if (PyList_CheckExact(__pyx_v_fields) || PyTuple_CheckExact(__pyx_v_fields)) {
       __pyx_t_1 = __pyx_v_fields; __Pyx_INCREF(__pyx_t_1); __pyx_t_5 = 0;
       __pyx_t_6 = NULL;
     } else {
-      __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_fields); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_fields); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __pyx_t_6 = Py_TYPE(__pyx_t_1)->tp_iternext;
     }
@@ -13585,16 +14017,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
       if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_1)) {
         if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_1)) {
         if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_2 = __pyx_t_6(__pyx_t_1);
@@ -13602,7 +14034,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -13611,41 +14043,41 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
       __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_2);
       __pyx_t_2 = 0;
 
-      /* "pysam/csamfile.pyx":1096
+      /* "pysam/calignmentfile.pyx":1168
  *             # write user fields
  *             for key in fields:
  *                 if not key.isupper():             # <<<<<<<<<<<<<<
- *                     line.append( "%s:%s" % (key, str(fields[key])))
+ *                     line.append("%s:%s" % (key, str(fields[key])))
  * 
  */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_isupper); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_isupper); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __pyx_t_3 = ((!__pyx_t_8) != 0);
       if (__pyx_t_3) {
 
-        /* "pysam/csamfile.pyx":1097
+        /* "pysam/calignmentfile.pyx":1169
  *             for key in fields:
  *                 if not key.isupper():
- *                     line.append( "%s:%s" % (key, str(fields[key])))             # <<<<<<<<<<<<<<
+ *                     line.append("%s:%s" % (key, str(fields[key])))             # <<<<<<<<<<<<<<
  * 
- *         return "\t".join( line )
+ *         return "\t".join(line)
  */
-        __pyx_t_7 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_7 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1169; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_7);
         __Pyx_GIVEREF(__pyx_t_7);
         __pyx_t_7 = 0;
-        __pyx_t_7 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_INCREF(__pyx_v_key);
         PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_key);
@@ -13653,10 +14085,10 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
         PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_7);
         __Pyx_GIVEREF(__pyx_t_7);
         __pyx_t_7 = 0;
-        __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_t_7); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_t_7); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         goto __pyx_L11;
       }
@@ -13666,24 +14098,24 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1099
- *                     line.append( "%s:%s" % (key, str(fields[key])))
+  /* "pysam/calignmentfile.pyx":1171
+ *                     line.append("%s:%s" % (key, str(fields[key])))
  * 
- *         return "\t".join( line )             # <<<<<<<<<<<<<<
+ *         return "\t".join(line)             # <<<<<<<<<<<<<<
  * 
- *     cdef bam_hdr_t * _buildHeader( self, new_header ):
+ *     cdef bam_hdr_t * _buildHeader(self, new_header):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_v_line); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s__43, __pyx_v_line); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1076
+  /* "pysam/calignmentfile.pyx":1148
  *             return result
  * 
- *     def _buildLine( self, fields, record ):             # <<<<<<<<<<<<<<
+ *     def _buildLine(self, fields, record):             # <<<<<<<<<<<<<<
  *         '''build a header line from *fields* dictionary for *record*'''
  * 
  */
@@ -13693,7 +14125,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._buildLine", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._buildLine", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_line);
@@ -13704,15 +14136,15 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED s
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1101
- *         return "\t".join( line )
+/* "pysam/calignmentfile.pyx":1173
+ *         return "\t".join(line)
  * 
- *     cdef bam_hdr_t * _buildHeader( self, new_header ):             # <<<<<<<<<<<<<<
+ *     cdef bam_hdr_t * _buildHeader(self, new_header):             # <<<<<<<<<<<<<<
  *         '''return a new header built from a dictionary in *new_header*.
  * 
  */
 
-static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_new_header) {
+static bam_hdr_t *__pyx_f_5pysam_14calignmentfile_13AlignmentFile__buildHeader(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self, PyObject *__pyx_v_new_header) {
   PyObject *__pyx_v_lines = NULL;
   bam_hdr_t *__pyx_v_dest;
   PyObject *__pyx_v_record = NULL;
@@ -13754,21 +14186,21 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_buildHeader", 0);
-  __Pyx_TraceCall("_buildHeader", __pyx_f[0], 1101);
+  __Pyx_TraceCall("_buildHeader", __pyx_f[0], 1173);
 
-  /* "pysam/csamfile.pyx":1107
+  /* "pysam/calignmentfile.pyx":1179
  *         '''
  * 
  *         lines = []             # <<<<<<<<<<<<<<
  * 
  *         # check if hash exists
  */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_lines = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1114
+  /* "pysam/calignmentfile.pyx":1186
  *         cdef bam_hdr_t * dest
  * 
  *         dest = bam_hdr_init()             # <<<<<<<<<<<<<<
@@ -13777,20 +14209,20 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
  */
   __pyx_v_dest = bam_hdr_init();
 
-  /* "pysam/csamfile.pyx":1117
+  /* "pysam/calignmentfile.pyx":1189
  * 
  *         # first: defined tags
  *         for record in VALID_HEADERS:             # <<<<<<<<<<<<<<
  *             if record in new_header:
  *                 ttype = VALID_HEADER_TYPES[record]
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADERS); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADERS); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   if (PyList_CheckExact(__pyx_t_1) || PyTuple_CheckExact(__pyx_t_1)) {
     __pyx_t_2 = __pyx_t_1; __Pyx_INCREF(__pyx_t_2); __pyx_t_3 = 0;
     __pyx_t_4 = NULL;
   } else {
-    __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_t_4 = Py_TYPE(__pyx_t_2)->tp_iternext;
   }
@@ -13799,16 +14231,16 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
     if (!__pyx_t_4 && PyList_CheckExact(__pyx_t_2)) {
       if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_4 && PyTuple_CheckExact(__pyx_t_2)) {
       if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_1 = __pyx_t_4(__pyx_t_2);
@@ -13816,7 +14248,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -13825,69 +14257,69 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
     __Pyx_XDECREF_SET(__pyx_v_record, __pyx_t_1);
     __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":1118
+    /* "pysam/calignmentfile.pyx":1190
  *         # first: defined tags
  *         for record in VALID_HEADERS:
  *             if record in new_header:             # <<<<<<<<<<<<<<
  *                 ttype = VALID_HEADER_TYPES[record]
  *                 data = new_header[record]
  */
-    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_v_record, __pyx_v_new_header, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_v_record, __pyx_v_new_header, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_6 = (__pyx_t_5 != 0);
     if (__pyx_t_6) {
 
-      /* "pysam/csamfile.pyx":1119
+      /* "pysam/calignmentfile.pyx":1191
  *         for record in VALID_HEADERS:
  *             if record in new_header:
  *                 ttype = VALID_HEADER_TYPES[record]             # <<<<<<<<<<<<<<
  *                 data = new_header[record]
  *                 if type(data) != type(ttype()):
  */
-      __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1119; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_7 = PyObject_GetItem(__pyx_t_1, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1119; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_7 = PyObject_GetItem(__pyx_t_1, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1191; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_XDECREF_SET(__pyx_v_ttype, __pyx_t_7);
       __pyx_t_7 = 0;
 
-      /* "pysam/csamfile.pyx":1120
+      /* "pysam/calignmentfile.pyx":1192
  *             if record in new_header:
  *                 ttype = VALID_HEADER_TYPES[record]
  *                 data = new_header[record]             # <<<<<<<<<<<<<<
  *                 if type(data) != type(ttype()):
  *                     raise ValueError(
  */
-      __pyx_t_7 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1120; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_7 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1192; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_XDECREF_SET(__pyx_v_data, __pyx_t_7);
       __pyx_t_7 = 0;
 
-      /* "pysam/csamfile.pyx":1121
+      /* "pysam/calignmentfile.pyx":1193
  *                 ttype = VALID_HEADER_TYPES[record]
  *                 data = new_header[record]
  *                 if type(data) != type(ttype()):             # <<<<<<<<<<<<<<
  *                     raise ValueError(
  *                         "invalid type for record %s: %s, expected %s" %
  */
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_v_ttype, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1121; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_v_ttype, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1193; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_1 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_v_data)), ((PyObject *)Py_TYPE(__pyx_t_7)), Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1121; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_v_data)), ((PyObject *)Py_TYPE(__pyx_t_7)), Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1193; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1121; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1193; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       if (__pyx_t_6) {
 
-        /* "pysam/csamfile.pyx":1124
+        /* "pysam/calignmentfile.pyx":1196
  *                     raise ValueError(
  *                         "invalid type for record %s: %s, expected %s" %
  *                         (record, type(data), type(ttype())))             # <<<<<<<<<<<<<<
  *                 if type(data) is dict:
  *                     lines.append(self._buildLine(data, record))
  */
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_v_ttype, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_v_ttype, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_7 = PyTuple_New(3); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyTuple_New(3); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_INCREF(__pyx_v_record);
         PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_record);
@@ -13900,38 +14332,38 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         __Pyx_GIVEREF(((PyObject *)Py_TYPE(__pyx_t_1)));
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-        /* "pysam/csamfile.pyx":1123
+        /* "pysam/calignmentfile.pyx":1195
  *                 if type(data) != type(ttype()):
  *                     raise ValueError(
  *                         "invalid type for record %s: %s, expected %s" %             # <<<<<<<<<<<<<<
  *                         (record, type(data), type(ttype())))
  *                 if type(data) is dict:
  */
-        __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_invalid_type_for_record_s_s_expe, __pyx_t_7); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_invalid_type_for_record_s_s_expe, __pyx_t_7); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1195; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
 
-        /* "pysam/csamfile.pyx":1122
+        /* "pysam/calignmentfile.pyx":1194
  *                 data = new_header[record]
  *                 if type(data) != type(ttype()):
  *                     raise ValueError(             # <<<<<<<<<<<<<<
  *                         "invalid type for record %s: %s, expected %s" %
  *                         (record, type(data), type(ttype())))
  */
-        __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_1);
         __Pyx_GIVEREF(__pyx_t_1);
         __pyx_t_1 = 0;
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_7, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_7, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         __Pyx_Raise(__pyx_t_1, 0, 0, 0);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
 
-      /* "pysam/csamfile.pyx":1125
+      /* "pysam/calignmentfile.pyx":1197
  *                         "invalid type for record %s: %s, expected %s" %
  *                         (record, type(data), type(ttype())))
  *                 if type(data) is dict:             # <<<<<<<<<<<<<<
@@ -13942,16 +14374,16 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
       __pyx_t_5 = (__pyx_t_6 != 0);
       if (__pyx_t_5) {
 
-        /* "pysam/csamfile.pyx":1126
+        /* "pysam/calignmentfile.pyx":1198
  *                         (record, type(data), type(ttype())))
  *                 if type(data) is dict:
  *                     lines.append(self._buildLine(data, record))             # <<<<<<<<<<<<<<
  *                 else:
  *                     for fields in new_header[record]:
  */
-        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_INCREF(__pyx_v_data);
         PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_data);
@@ -13959,30 +14391,30 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         __Pyx_INCREF(__pyx_v_record);
         PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_v_record);
         __Pyx_GIVEREF(__pyx_v_record);
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
         goto __pyx_L7;
       }
       /*else*/ {
 
-        /* "pysam/csamfile.pyx":1128
+        /* "pysam/calignmentfile.pyx":1200
  *                     lines.append(self._buildLine(data, record))
  *                 else:
  *                     for fields in new_header[record]:             # <<<<<<<<<<<<<<
  *                         lines.append(self._buildLine(fields, record))
  * 
  */
-        __pyx_t_8 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_8 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1200; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_8);
         if (PyList_CheckExact(__pyx_t_8) || PyTuple_CheckExact(__pyx_t_8)) {
           __pyx_t_7 = __pyx_t_8; __Pyx_INCREF(__pyx_t_7); __pyx_t_10 = 0;
           __pyx_t_11 = NULL;
         } else {
-          __pyx_t_10 = -1; __pyx_t_7 = PyObject_GetIter(__pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_10 = -1; __pyx_t_7 = PyObject_GetIter(__pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_7);
           __pyx_t_11 = Py_TYPE(__pyx_t_7)->tp_iternext;
         }
@@ -13991,16 +14423,16 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
           if (!__pyx_t_11 && PyList_CheckExact(__pyx_t_7)) {
             if (__pyx_t_10 >= PyList_GET_SIZE(__pyx_t_7)) break;
             #if CYTHON_COMPILING_IN_CPYTHON
-            __pyx_t_8 = PyList_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            __pyx_t_8 = PyList_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             #else
-            __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             #endif
           } else if (!__pyx_t_11 && PyTuple_CheckExact(__pyx_t_7)) {
             if (__pyx_t_10 >= PyTuple_GET_SIZE(__pyx_t_7)) break;
             #if CYTHON_COMPILING_IN_CPYTHON
-            __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             #else
-            __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             #endif
           } else {
             __pyx_t_8 = __pyx_t_11(__pyx_t_7);
@@ -14008,7 +14440,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
               PyObject* exc_type = PyErr_Occurred();
               if (exc_type) {
                 if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-                else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+                else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
               }
               break;
             }
@@ -14017,16 +14449,16 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
           __Pyx_XDECREF_SET(__pyx_v_fields, __pyx_t_8);
           __pyx_t_8 = 0;
 
-          /* "pysam/csamfile.pyx":1129
+          /* "pysam/calignmentfile.pyx":1201
  *                 else:
  *                     for fields in new_header[record]:
  *                         lines.append(self._buildLine(fields, record))             # <<<<<<<<<<<<<<
  * 
  *         # then: user tags (lower case), sorted alphabetically
  */
-          __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_8);
-          __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_1);
           __Pyx_INCREF(__pyx_v_fields);
           PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_fields);
@@ -14034,11 +14466,11 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
           __Pyx_INCREF(__pyx_v_record);
           PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_record);
           __Pyx_GIVEREF(__pyx_v_record);
-          __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_1, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_1, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_12);
           __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
           __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_12); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_12); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
         }
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
@@ -14050,31 +14482,31 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
   }
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":1132
+  /* "pysam/calignmentfile.pyx":1204
  * 
  *         # then: user tags (lower case), sorted alphabetically
  *         for record, data in sorted(new_header.items()):             # <<<<<<<<<<<<<<
  *             if record in VALID_HEADERS: continue
  *             if type( data ) is dict:
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_new_header, __pyx_n_s_items); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_new_header, __pyx_n_s_items); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_7);
   __Pyx_GIVEREF(__pyx_t_7);
   __pyx_t_7 = 0;
-  __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_sorted, __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_sorted, __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (PyList_CheckExact(__pyx_t_7) || PyTuple_CheckExact(__pyx_t_7)) {
     __pyx_t_2 = __pyx_t_7; __Pyx_INCREF(__pyx_t_2); __pyx_t_3 = 0;
     __pyx_t_4 = NULL;
   } else {
-    __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_t_4 = Py_TYPE(__pyx_t_2)->tp_iternext;
   }
@@ -14083,16 +14515,16 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
     if (!__pyx_t_4 && PyList_CheckExact(__pyx_t_2)) {
       if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_7); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_7); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_4 && PyTuple_CheckExact(__pyx_t_2)) {
       if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_7); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_7); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_7 = __pyx_t_4(__pyx_t_2);
@@ -14100,7 +14532,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -14116,7 +14548,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
       if (unlikely(size != 2)) {
         if (size > 2) __Pyx_RaiseTooManyValuesError(2);
         else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       #if CYTHON_COMPILING_IN_CPYTHON
       if (likely(PyTuple_CheckExact(sequence))) {
@@ -14129,15 +14561,15 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
       __Pyx_INCREF(__pyx_t_12);
       __Pyx_INCREF(__pyx_t_1);
       #else
-      __pyx_t_12 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_12 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_12);
-      __pyx_t_1 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       #endif
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     } else {
       Py_ssize_t index = -1;
-      __pyx_t_8 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __pyx_t_13 = Py_TYPE(__pyx_t_8)->tp_iternext;
@@ -14145,7 +14577,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
       __Pyx_GOTREF(__pyx_t_12);
       index = 1; __pyx_t_1 = __pyx_t_13(__pyx_t_8); if (unlikely(!__pyx_t_1)) goto __pyx_L12_unpacking_failed;
       __Pyx_GOTREF(__pyx_t_1);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_13(__pyx_t_8), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (__Pyx_IternextUnpackEndCheck(__pyx_t_13(__pyx_t_8), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_13 = NULL;
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       goto __pyx_L13_unpacking_done;
@@ -14153,7 +14585,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       __pyx_t_13 = NULL;
       if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_L13_unpacking_done:;
     }
     __Pyx_XDECREF_SET(__pyx_v_record, __pyx_t_12);
@@ -14161,23 +14593,23 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
     __Pyx_XDECREF_SET(__pyx_v_data, __pyx_t_1);
     __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":1133
+    /* "pysam/calignmentfile.pyx":1205
  *         # then: user tags (lower case), sorted alphabetically
  *         for record, data in sorted(new_header.items()):
  *             if record in VALID_HEADERS: continue             # <<<<<<<<<<<<<<
  *             if type( data ) is dict:
  *                 lines.append( self._buildLine( data, record ) )
  */
-    __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADERS); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1133; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADERS); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1205; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_v_record, __pyx_t_7, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1133; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_v_record, __pyx_t_7, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1205; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __pyx_t_6 = (__pyx_t_5 != 0);
     if (__pyx_t_6) {
       goto __pyx_L10_continue;
     }
 
-    /* "pysam/csamfile.pyx":1134
+    /* "pysam/calignmentfile.pyx":1206
  *         for record, data in sorted(new_header.items()):
  *             if record in VALID_HEADERS: continue
  *             if type( data ) is dict:             # <<<<<<<<<<<<<<
@@ -14188,16 +14620,16 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
     __pyx_t_5 = (__pyx_t_6 != 0);
     if (__pyx_t_5) {
 
-      /* "pysam/csamfile.pyx":1135
+      /* "pysam/calignmentfile.pyx":1207
  *             if record in VALID_HEADERS: continue
  *             if type( data ) is dict:
  *                 lines.append( self._buildLine( data, record ) )             # <<<<<<<<<<<<<<
  *             else:
  *                 for fields in new_header[record]:
  */
-      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1207; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1207; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_INCREF(__pyx_v_data);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_data);
@@ -14205,30 +14637,30 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
       __Pyx_INCREF(__pyx_v_record);
       PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_record);
       __Pyx_GIVEREF(__pyx_v_record);
-      __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_1, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_1, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1207; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_12);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_12); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_12); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1207; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
       goto __pyx_L15;
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":1137
+      /* "pysam/calignmentfile.pyx":1209
  *                 lines.append( self._buildLine( data, record ) )
  *             else:
  *                 for fields in new_header[record]:             # <<<<<<<<<<<<<<
  *                     lines.append( self._buildLine( fields, record ) )
  * 
  */
-      __pyx_t_12 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_12 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1209; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_12);
       if (PyList_CheckExact(__pyx_t_12) || PyTuple_CheckExact(__pyx_t_12)) {
         __pyx_t_1 = __pyx_t_12; __Pyx_INCREF(__pyx_t_1); __pyx_t_10 = 0;
         __pyx_t_11 = NULL;
       } else {
-        __pyx_t_10 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_12); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_12); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __pyx_t_11 = Py_TYPE(__pyx_t_1)->tp_iternext;
       }
@@ -14237,16 +14669,16 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         if (!__pyx_t_11 && PyList_CheckExact(__pyx_t_1)) {
           if (__pyx_t_10 >= PyList_GET_SIZE(__pyx_t_1)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_12 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_10); __Pyx_INCREF(__pyx_t_12); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_12 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_10); __Pyx_INCREF(__pyx_t_12); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_12 = PySequence_ITEM(__pyx_t_1, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_12 = PySequence_ITEM(__pyx_t_1, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else if (!__pyx_t_11 && PyTuple_CheckExact(__pyx_t_1)) {
           if (__pyx_t_10 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_12 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_10); __Pyx_INCREF(__pyx_t_12); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_12 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_10); __Pyx_INCREF(__pyx_t_12); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_12 = PySequence_ITEM(__pyx_t_1, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_12 = PySequence_ITEM(__pyx_t_1, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else {
           __pyx_t_12 = __pyx_t_11(__pyx_t_1);
@@ -14254,7 +14686,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
             PyObject* exc_type = PyErr_Occurred();
             if (exc_type) {
               if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             }
             break;
           }
@@ -14263,16 +14695,16 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         __Pyx_XDECREF_SET(__pyx_v_fields, __pyx_t_12);
         __pyx_t_12 = 0;
 
-        /* "pysam/csamfile.pyx":1138
+        /* "pysam/calignmentfile.pyx":1210
  *             else:
  *                 for fields in new_header[record]:
  *                     lines.append( self._buildLine( fields, record ) )             # <<<<<<<<<<<<<<
  * 
  *         text = "\n".join(lines) + "\n"
  */
-        __pyx_t_12 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_12 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_12);
-        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_INCREF(__pyx_v_fields);
         PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_fields);
@@ -14280,11 +14712,11 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         __Pyx_INCREF(__pyx_v_record);
         PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_v_record);
         __Pyx_GIVEREF(__pyx_v_record);
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       }
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -14294,22 +14726,22 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
   }
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":1140
+  /* "pysam/calignmentfile.pyx":1212
  *                     lines.append( self._buildLine( fields, record ) )
  * 
  *         text = "\n".join(lines) + "\n"             # <<<<<<<<<<<<<<
  *         if dest.text != NULL: free( dest.text )
  *         dest.text = <char*>calloc( len(text), sizeof(char))
  */
-  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__4, __pyx_v_lines); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__38, __pyx_v_lines); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyNumber_Add(__pyx_t_2, __pyx_kp_s__4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyNumber_Add(__pyx_t_2, __pyx_kp_s__38); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_v_text = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1141
+  /* "pysam/calignmentfile.pyx":1213
  * 
  *         text = "\n".join(lines) + "\n"
  *         if dest.text != NULL: free( dest.text )             # <<<<<<<<<<<<<<
@@ -14323,89 +14755,89 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
   }
   __pyx_L18:;
 
-  /* "pysam/csamfile.pyx":1142
+  /* "pysam/calignmentfile.pyx":1214
  *         text = "\n".join(lines) + "\n"
  *         if dest.text != NULL: free( dest.text )
  *         dest.text = <char*>calloc( len(text), sizeof(char))             # <<<<<<<<<<<<<<
  *         dest.l_text = len(text)
  *         cdef bytes btext = text.encode('ascii')
  */
-  __pyx_t_3 = PyObject_Length(__pyx_v_text); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_Length(__pyx_v_text); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_dest->text = ((char *)calloc(__pyx_t_3, (sizeof(char))));
 
-  /* "pysam/csamfile.pyx":1143
+  /* "pysam/calignmentfile.pyx":1215
  *         if dest.text != NULL: free( dest.text )
  *         dest.text = <char*>calloc( len(text), sizeof(char))
  *         dest.l_text = len(text)             # <<<<<<<<<<<<<<
  *         cdef bytes btext = text.encode('ascii')
  *         strncpy( dest.text, btext, dest.l_text )
  */
-  __pyx_t_3 = PyObject_Length(__pyx_v_text); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1143; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_Length(__pyx_v_text); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_dest->l_text = __pyx_t_3;
 
-  /* "pysam/csamfile.pyx":1144
+  /* "pysam/calignmentfile.pyx":1216
  *         dest.text = <char*>calloc( len(text), sizeof(char))
  *         dest.l_text = len(text)
  *         cdef bytes btext = text.encode('ascii')             # <<<<<<<<<<<<<<
  *         strncpy( dest.text, btext, dest.l_text )
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_text, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_text, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__50, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__51, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (!(likely(PyBytes_CheckExact(__pyx_t_2))||((__pyx_t_2) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_2)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!(likely(PyBytes_CheckExact(__pyx_t_2))||((__pyx_t_2) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_2)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_btext = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":1145
+  /* "pysam/calignmentfile.pyx":1217
  *         dest.l_text = len(text)
  *         cdef bytes btext = text.encode('ascii')
  *         strncpy( dest.text, btext, dest.l_text )             # <<<<<<<<<<<<<<
  * 
  *         cdef bytes bseqname
  */
-  __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_btext); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1145; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_btext); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   strncpy(__pyx_v_dest->text, __pyx_t_14, __pyx_v_dest->l_text);
 
-  /* "pysam/csamfile.pyx":1149
+  /* "pysam/calignmentfile.pyx":1221
  *         cdef bytes bseqname
  *         # collect targets
  *         if "SQ" in new_header:             # <<<<<<<<<<<<<<
  *             seqs = []
  *             for fields in new_header["SQ"]:
  */
-  __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_n_s_SQ, __pyx_v_new_header, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_n_s_SQ, __pyx_v_new_header, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_6 = (__pyx_t_5 != 0);
   if (__pyx_t_6) {
 
-    /* "pysam/csamfile.pyx":1150
+    /* "pysam/calignmentfile.pyx":1222
  *         # collect targets
  *         if "SQ" in new_header:
  *             seqs = []             # <<<<<<<<<<<<<<
  *             for fields in new_header["SQ"]:
  *                 try:
  */
-    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_v_seqs = ((PyObject*)__pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/csamfile.pyx":1151
+    /* "pysam/calignmentfile.pyx":1223
  *         if "SQ" in new_header:
  *             seqs = []
  *             for fields in new_header["SQ"]:             # <<<<<<<<<<<<<<
  *                 try:
  *                     seqs.append( (fields["SN"], fields["LN"] ) )
  */
-    __pyx_t_2 = PyObject_GetItem(__pyx_v_new_header, __pyx_n_s_SQ); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = PyObject_GetItem(__pyx_v_new_header, __pyx_n_s_SQ); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
     if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) {
       __pyx_t_1 = __pyx_t_2; __Pyx_INCREF(__pyx_t_1); __pyx_t_3 = 0;
       __pyx_t_4 = NULL;
     } else {
-      __pyx_t_3 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __pyx_t_4 = Py_TYPE(__pyx_t_1)->tp_iternext;
     }
@@ -14414,16 +14846,16 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
       if (!__pyx_t_4 && PyList_CheckExact(__pyx_t_1)) {
         if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_4 && PyTuple_CheckExact(__pyx_t_1)) {
         if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_2 = __pyx_t_4(__pyx_t_1);
@@ -14431,7 +14863,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -14440,7 +14872,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
       __Pyx_XDECREF_SET(__pyx_v_fields, __pyx_t_2);
       __pyx_t_2 = 0;
 
-      /* "pysam/csamfile.pyx":1152
+      /* "pysam/calignmentfile.pyx":1224
  *             seqs = []
  *             for fields in new_header["SQ"]:
  *                 try:             # <<<<<<<<<<<<<<
@@ -14454,18 +14886,18 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         __Pyx_XGOTREF(__pyx_t_17);
         /*try:*/ {
 
-          /* "pysam/csamfile.pyx":1153
+          /* "pysam/calignmentfile.pyx":1225
  *             for fields in new_header["SQ"]:
  *                 try:
  *                     seqs.append( (fields["SN"], fields["LN"] ) )             # <<<<<<<<<<<<<<
  *                 except KeyError:
  *                     raise KeyError( "incomplete sequence information in '%s'" % str(fields))
  */
-          __pyx_t_2 = PyObject_GetItem(__pyx_v_fields, __pyx_n_s_SN); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1153; __pyx_clineno = __LINE__; goto __pyx_L22_error;};
+          __pyx_t_2 = PyObject_GetItem(__pyx_v_fields, __pyx_n_s_SN); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1225; __pyx_clineno = __LINE__; goto __pyx_L22_error;};
           __Pyx_GOTREF(__pyx_t_2);
-          __pyx_t_8 = PyObject_GetItem(__pyx_v_fields, __pyx_n_s_LN); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1153; __pyx_clineno = __LINE__; goto __pyx_L22_error;};
+          __pyx_t_8 = PyObject_GetItem(__pyx_v_fields, __pyx_n_s_LN); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1225; __pyx_clineno = __LINE__; goto __pyx_L22_error;};
           __Pyx_GOTREF(__pyx_t_8);
-          __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1153; __pyx_clineno = __LINE__; goto __pyx_L22_error;}
+          __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1225; __pyx_clineno = __LINE__; goto __pyx_L22_error;}
           __Pyx_GOTREF(__pyx_t_7);
           PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_2);
           __Pyx_GIVEREF(__pyx_t_2);
@@ -14473,7 +14905,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
           __Pyx_GIVEREF(__pyx_t_8);
           __pyx_t_2 = 0;
           __pyx_t_8 = 0;
-          __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_seqs, __pyx_t_7); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1153; __pyx_clineno = __LINE__; goto __pyx_L22_error;}
+          __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_seqs, __pyx_t_7); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1225; __pyx_clineno = __LINE__; goto __pyx_L22_error;}
           __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         }
         __Pyx_XDECREF(__pyx_t_15); __pyx_t_15 = 0;
@@ -14486,7 +14918,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         __Pyx_XDECREF(__pyx_t_8); __pyx_t_8 = 0;
         __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0;
 
-        /* "pysam/csamfile.pyx":1154
+        /* "pysam/calignmentfile.pyx":1226
  *                 try:
  *                     seqs.append( (fields["SN"], fields["LN"] ) )
  *                 except KeyError:             # <<<<<<<<<<<<<<
@@ -14495,41 +14927,41 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
  */
         __pyx_t_18 = PyErr_ExceptionMatches(__pyx_builtin_KeyError);
         if (__pyx_t_18) {
-          __Pyx_AddTraceback("pysam.csamfile.Samfile._buildHeader", __pyx_clineno, __pyx_lineno, __pyx_filename);
-          if (__Pyx_GetException(&__pyx_t_7, &__pyx_t_8, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1154; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
+          __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile._buildHeader", __pyx_clineno, __pyx_lineno, __pyx_filename);
+          if (__Pyx_GetException(&__pyx_t_7, &__pyx_t_8, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1226; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
           __Pyx_GOTREF(__pyx_t_7);
           __Pyx_GOTREF(__pyx_t_8);
           __Pyx_GOTREF(__pyx_t_2);
 
-          /* "pysam/csamfile.pyx":1155
+          /* "pysam/calignmentfile.pyx":1227
  *                     seqs.append( (fields["SN"], fields["LN"] ) )
  *                 except KeyError:
  *                     raise KeyError( "incomplete sequence information in '%s'" % str(fields))             # <<<<<<<<<<<<<<
  * 
  *             dest.n_targets = len(seqs)
  */
-          __pyx_t_12 = PyTuple_New(1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
+          __pyx_t_12 = PyTuple_New(1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1227; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
           __Pyx_GOTREF(__pyx_t_12);
           __Pyx_INCREF(__pyx_v_fields);
           PyTuple_SET_ITEM(__pyx_t_12, 0, __pyx_v_fields);
           __Pyx_GIVEREF(__pyx_v_fields);
-          __pyx_t_19 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_12, NULL); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
+          __pyx_t_19 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_12, NULL); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1227; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
           __Pyx_GOTREF(__pyx_t_19);
           __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          __pyx_t_12 = __Pyx_PyString_Format(__pyx_kp_s_incomplete_sequence_information, __pyx_t_19); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
+          __pyx_t_12 = __Pyx_PyString_Format(__pyx_kp_s_incomplete_sequence_information, __pyx_t_19); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1227; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
           __Pyx_GOTREF(__pyx_t_12);
           __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
-          __pyx_t_19 = PyTuple_New(1); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
+          __pyx_t_19 = PyTuple_New(1); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1227; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
           __Pyx_GOTREF(__pyx_t_19);
           PyTuple_SET_ITEM(__pyx_t_19, 0, __pyx_t_12);
           __Pyx_GIVEREF(__pyx_t_12);
           __pyx_t_12 = 0;
-          __pyx_t_12 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_19, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
+          __pyx_t_12 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_19, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1227; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
           __Pyx_GOTREF(__pyx_t_12);
           __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
           __Pyx_Raise(__pyx_t_12, 0, 0, 0);
           __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1227; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
           __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
           __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
           __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -14552,52 +14984,52 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
     }
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":1157
+    /* "pysam/calignmentfile.pyx":1229
  *                     raise KeyError( "incomplete sequence information in '%s'" % str(fields))
  * 
  *             dest.n_targets = len(seqs)             # <<<<<<<<<<<<<<
- *             dest.target_name = <char**>calloc( dest.n_targets, sizeof(char*) )
- *             dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )
+ *             dest.target_name = <char**>calloc(dest.n_targets, sizeof(char*))
+ *             dest.target_len = <uint32_t*>calloc(dest.n_targets, sizeof(uint32_t))
  */
-    __pyx_t_3 = PyList_GET_SIZE(__pyx_v_seqs); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1157; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyList_GET_SIZE(__pyx_v_seqs); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_dest->n_targets = __pyx_t_3;
 
-    /* "pysam/csamfile.pyx":1158
+    /* "pysam/calignmentfile.pyx":1230
  * 
  *             dest.n_targets = len(seqs)
- *             dest.target_name = <char**>calloc( dest.n_targets, sizeof(char*) )             # <<<<<<<<<<<<<<
- *             dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )
+ *             dest.target_name = <char**>calloc(dest.n_targets, sizeof(char*))             # <<<<<<<<<<<<<<
+ *             dest.target_len = <uint32_t*>calloc(dest.n_targets, sizeof(uint32_t))
  * 
  */
     __pyx_v_dest->target_name = ((char **)calloc(__pyx_v_dest->n_targets, (sizeof(char *))));
 
-    /* "pysam/csamfile.pyx":1159
+    /* "pysam/calignmentfile.pyx":1231
  *             dest.n_targets = len(seqs)
- *             dest.target_name = <char**>calloc( dest.n_targets, sizeof(char*) )
- *             dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )             # <<<<<<<<<<<<<<
+ *             dest.target_name = <char**>calloc(dest.n_targets, sizeof(char*))
+ *             dest.target_len = <uint32_t*>calloc(dest.n_targets, sizeof(uint32_t))             # <<<<<<<<<<<<<<
  * 
  *             for x from 0 <= x < dest.n_targets:
  */
     __pyx_v_dest->target_len = ((uint32_t *)calloc(__pyx_v_dest->n_targets, (sizeof(uint32_t))));
 
-    /* "pysam/csamfile.pyx":1161
- *             dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )
+    /* "pysam/calignmentfile.pyx":1233
+ *             dest.target_len = <uint32_t*>calloc(dest.n_targets, sizeof(uint32_t))
  * 
  *             for x from 0 <= x < dest.n_targets:             # <<<<<<<<<<<<<<
  *                 seqname, seqlen = seqs[x]
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
+ *                 dest.target_name[x] = <char*>calloc(
  */
     __pyx_t_20 = __pyx_v_dest->n_targets;
     for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_20; __pyx_v_x++) {
 
-      /* "pysam/csamfile.pyx":1162
+      /* "pysam/calignmentfile.pyx":1234
  * 
  *             for x from 0 <= x < dest.n_targets:
  *                 seqname, seqlen = seqs[x]             # <<<<<<<<<<<<<<
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
- *                 bseqname = seqname.encode('ascii')
+ *                 dest.target_name[x] = <char*>calloc(
+ *                     len(seqname) + 1, sizeof(char))
  */
-      __pyx_t_1 = __Pyx_GetItemInt_List(__pyx_v_seqs, __pyx_v_x, long, 1, __Pyx_PyInt_From_long, 1, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_1 = __Pyx_GetItemInt_List(__pyx_v_seqs, __pyx_v_x, long, 1, __Pyx_PyInt_From_long, 1, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1234; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_1);
       if ((likely(PyTuple_CheckExact(__pyx_t_1))) || (PyList_CheckExact(__pyx_t_1))) {
         PyObject* sequence = __pyx_t_1;
@@ -14609,7 +15041,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         if (unlikely(size != 2)) {
           if (size > 2) __Pyx_RaiseTooManyValuesError(2);
           else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         #if CYTHON_COMPILING_IN_CPYTHON
         if (likely(PyTuple_CheckExact(sequence))) {
@@ -14622,15 +15054,15 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         __Pyx_INCREF(__pyx_t_2);
         __Pyx_INCREF(__pyx_t_8);
         #else
-        __pyx_t_2 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_8 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         #endif
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       } else {
         Py_ssize_t index = -1;
-        __pyx_t_7 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __pyx_t_13 = Py_TYPE(__pyx_t_7)->tp_iternext;
@@ -14638,7 +15070,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         __Pyx_GOTREF(__pyx_t_2);
         index = 1; __pyx_t_8 = __pyx_t_13(__pyx_t_7); if (unlikely(!__pyx_t_8)) goto __pyx_L34_unpacking_failed;
         __Pyx_GOTREF(__pyx_t_8);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_13(__pyx_t_7), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (__Pyx_IternextUnpackEndCheck(__pyx_t_13(__pyx_t_7), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_13 = NULL;
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         goto __pyx_L35_unpacking_done;
@@ -14646,7 +15078,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         __pyx_t_13 = NULL;
         if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_L35_unpacking_done:;
       }
       __Pyx_XDECREF_SET(__pyx_v_seqname, __pyx_t_2);
@@ -14654,58 +15086,82 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
       __Pyx_XDECREF_SET(__pyx_v_seqlen, __pyx_t_8);
       __pyx_t_8 = 0;
 
-      /* "pysam/csamfile.pyx":1163
+      /* "pysam/calignmentfile.pyx":1236
+ *                 seqname, seqlen = seqs[x]
+ *                 dest.target_name[x] = <char*>calloc(
+ *                     len(seqname) + 1, sizeof(char))             # <<<<<<<<<<<<<<
+ *                 bseqname = seqname.encode('ascii')
+ *                 strncpy(dest.target_name[x], bseqname,
+ */
+      __pyx_t_3 = PyObject_Length(__pyx_v_seqname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+      /* "pysam/calignmentfile.pyx":1235
  *             for x from 0 <= x < dest.n_targets:
  *                 seqname, seqlen = seqs[x]
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )             # <<<<<<<<<<<<<<
+ *                 dest.target_name[x] = <char*>calloc(             # <<<<<<<<<<<<<<
+ *                     len(seqname) + 1, sizeof(char))
  *                 bseqname = seqname.encode('ascii')
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
  */
-      __pyx_t_3 = PyObject_Length(__pyx_v_seqname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       (__pyx_v_dest->target_name[__pyx_v_x]) = ((char *)calloc((__pyx_t_3 + 1), (sizeof(char))));
 
-      /* "pysam/csamfile.pyx":1164
- *                 seqname, seqlen = seqs[x]
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
+      /* "pysam/calignmentfile.pyx":1237
+ *                 dest.target_name[x] = <char*>calloc(
+ *                     len(seqname) + 1, sizeof(char))
  *                 bseqname = seqname.encode('ascii')             # <<<<<<<<<<<<<<
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
- *                 dest.target_len[x] = seqlen
+ *                 strncpy(dest.target_name[x], bseqname,
+ *                         len(seqname) + 1)
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_seqname, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_seqname, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__51, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__52, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      if (!(likely(PyBytes_CheckExact(__pyx_t_8))||((__pyx_t_8) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_8)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (!(likely(PyBytes_CheckExact(__pyx_t_8))||((__pyx_t_8) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_8)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_XDECREF_SET(__pyx_v_bseqname, ((PyObject*)__pyx_t_8));
       __pyx_t_8 = 0;
 
-      /* "pysam/csamfile.pyx":1165
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
+      /* "pysam/calignmentfile.pyx":1238
+ *                     len(seqname) + 1, sizeof(char))
  *                 bseqname = seqname.encode('ascii')
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )             # <<<<<<<<<<<<<<
+ *                 strncpy(dest.target_name[x], bseqname,             # <<<<<<<<<<<<<<
+ *                         len(seqname) + 1)
+ *                 dest.target_len[x] = seqlen
+ */
+      __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_bseqname); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+      /* "pysam/calignmentfile.pyx":1239
+ *                 bseqname = seqname.encode('ascii')
+ *                 strncpy(dest.target_name[x], bseqname,
+ *                         len(seqname) + 1)             # <<<<<<<<<<<<<<
  *                 dest.target_len[x] = seqlen
  * 
  */
-      __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_bseqname); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_3 = PyObject_Length(__pyx_v_seqname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      strncpy((__pyx_v_dest->target_name[__pyx_v_x]), __pyx_t_14, (__pyx_t_3 + 1));
+      __pyx_t_3 = PyObject_Length(__pyx_v_seqname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-      /* "pysam/csamfile.pyx":1166
+      /* "pysam/calignmentfile.pyx":1238
+ *                     len(seqname) + 1, sizeof(char))
  *                 bseqname = seqname.encode('ascii')
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
+ *                 strncpy(dest.target_name[x], bseqname,             # <<<<<<<<<<<<<<
+ *                         len(seqname) + 1)
+ *                 dest.target_len[x] = seqlen
+ */
+      strncpy((__pyx_v_dest->target_name[__pyx_v_x]), __pyx_t_14, (__pyx_t_3 + 1));
+
+      /* "pysam/calignmentfile.pyx":1240
+ *                 strncpy(dest.target_name[x], bseqname,
+ *                         len(seqname) + 1)
  *                 dest.target_len[x] = seqlen             # <<<<<<<<<<<<<<
  * 
  *         return dest
  */
-      __pyx_t_21 = __Pyx_PyInt_As_uint32_t(__pyx_v_seqlen); if (unlikely((__pyx_t_21 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_21 = __Pyx_PyInt_As_uint32_t(__pyx_v_seqlen); if (unlikely((__pyx_t_21 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1240; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       (__pyx_v_dest->target_len[__pyx_v_x]) = __pyx_t_21;
     }
     goto __pyx_L19;
   }
   __pyx_L19:;
 
-  /* "pysam/csamfile.pyx":1168
+  /* "pysam/calignmentfile.pyx":1242
  *                 dest.target_len[x] = seqlen
  * 
  *         return dest             # <<<<<<<<<<<<<<
@@ -14715,10 +15171,10 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
   __pyx_r = __pyx_v_dest;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1101
- *         return "\t".join( line )
+  /* "pysam/calignmentfile.pyx":1173
+ *         return "\t".join(line)
  * 
- *     cdef bam_hdr_t * _buildHeader( self, new_header ):             # <<<<<<<<<<<<<<
+ *     cdef bam_hdr_t * _buildHeader(self, new_header):             # <<<<<<<<<<<<<<
  *         '''return a new header built from a dictionary in *new_header*.
  * 
  */
@@ -14731,7 +15187,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
   __Pyx_XDECREF(__pyx_t_8);
   __Pyx_XDECREF(__pyx_t_12);
   __Pyx_XDECREF(__pyx_t_19);
-  __Pyx_WriteUnraisable("pysam.csamfile.Samfile._buildHeader", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __Pyx_WriteUnraisable("pysam.calignmentfile.AlignmentFile._buildHeader", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_lines);
@@ -14750,7 +15206,7 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1178
+/* "pysam/calignmentfile.pyx":1252
  *     ## Possible solutions: deprecate or open new file handle
  *     ###############################################################
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -14759,19 +15215,19 @@ static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_ob
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_45__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_45__iter__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_45__iter__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_45__iter__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_44__iter__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_44__iter__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -14784,40 +15240,40 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1178);
+  __Pyx_TraceCall("__iter__", __pyx_f[0], 1252);
 
-  /* "pysam/csamfile.pyx":1179
+  /* "pysam/calignmentfile.pyx":1253
  *     ###############################################################
  *     def __iter__(self):
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError( "I/O operation on closed file" )
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":1180
+    /* "pysam/calignmentfile.pyx":1254
  *     def __iter__(self):
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         if not self.isbam and self.header.n_targets == 0:
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__52, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__53, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1182
+  /* "pysam/calignmentfile.pyx":1256
  *             raise ValueError( "I/O operation on closed file" )
  * 
  *         if not self.isbam and self.header.n_targets == 0:             # <<<<<<<<<<<<<<
@@ -14833,21 +15289,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_
   }
   if (__pyx_t_5) {
 
-    /* "pysam/csamfile.pyx":1183
+    /* "pysam/calignmentfile.pyx":1257
  * 
  *         if not self.isbam and self.header.n_targets == 0:
  *             raise NotImplementedError(             # <<<<<<<<<<<<<<
  *                 "can not iterate over samfile without header")
  *         return self
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__53, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__54, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1185
+  /* "pysam/calignmentfile.pyx":1259
  *             raise NotImplementedError(
  *                 "can not iterate over samfile without header")
  *         return self             # <<<<<<<<<<<<<<
@@ -14859,7 +15315,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1178
+  /* "pysam/calignmentfile.pyx":1252
  *     ## Possible solutions: deprecate or open new file handle
  *     ###############################################################
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -14871,7 +15327,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -14880,7 +15336,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1187
+/* "pysam/calignmentfile.pyx":1261
  *         return self
  * 
  *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
@@ -14888,14 +15344,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_
  * 
  */
 
-static bam1_t *__pyx_f_5pysam_8csamfile_7Samfile_getCurrent(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static bam1_t *__pyx_f_5pysam_14calignmentfile_13AlignmentFile_getCurrent(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   bam1_t *__pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1187);
+  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1261);
 
-  /* "pysam/csamfile.pyx":1188
+  /* "pysam/calignmentfile.pyx":1262
  * 
  *     cdef bam1_t * getCurrent( self ):
  *         return self.b             # <<<<<<<<<<<<<<
@@ -14905,7 +15361,7 @@ static bam1_t *__pyx_f_5pysam_8csamfile_7Samfile_getCurrent(struct __pyx_obj_5py
   __pyx_r = __pyx_v_self->b;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1187
+  /* "pysam/calignmentfile.pyx":1261
  *         return self
  * 
  *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
@@ -14920,24 +15376,24 @@ static bam1_t *__pyx_f_5pysam_8csamfile_7Samfile_getCurrent(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1190
+/* "pysam/calignmentfile.pyx":1264
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
  *         '''
- *         cversion of iterator. Used by :class:`pysam.Samfile.IteratorColumn`.
+ *         cversion of iterator. Used by :class:`pysam.AlignmentFile.IteratorColumn`.
  */
 
-static int __pyx_f_5pysam_8csamfile_7Samfile_cnext(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static int __pyx_f_5pysam_14calignmentfile_13AlignmentFile_cnext(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1190);
+  __Pyx_TraceCall("cnext", __pyx_f[0], 1264);
 
-  /* "pysam/csamfile.pyx":1195
+  /* "pysam/calignmentfile.pyx":1268
+ *         cversion of iterator. Used by :class:`pysam.AlignmentFile.IteratorColumn`.
  *         '''
- *         cdef int ret
  *         return sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
  *                          self.header,
  *                          self.b)
@@ -14945,12 +15401,12 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_cnext(struct __pyx_obj_5pysam_8csam
   __pyx_r = sam_read1(__pyx_v_self->htsfile, __pyx_v_self->header, __pyx_v_self->b);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1190
+  /* "pysam/calignmentfile.pyx":1264
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
  *         '''
- *         cversion of iterator. Used by :class:`pysam.Samfile.IteratorColumn`.
+ *         cversion of iterator. Used by :class:`pysam.AlignmentFile.IteratorColumn`.
  */
 
   /* function exit code */
@@ -14960,7 +15416,7 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_cnext(struct __pyx_obj_5pysam_8csam
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1199
+/* "pysam/calignmentfile.pyx":1272
  *                          self.b)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -14969,23 +15425,23 @@ static int __pyx_f_5pysam_8csamfile_7Samfile_cnext(struct __pyx_obj_5pysam_8csam
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_47__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_46__next__[] = "\n        python version of next().\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_47__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_46__next__[] = "\n        python version of next().\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_7Samfile_46__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_14calignmentfile_13AlignmentFile_46__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_47__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_47__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_46__next__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_13AlignmentFile_46__next__(((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_46__next__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_13AlignmentFile_46__next__(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_self) {
   int __pyx_v_ret;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -14996,55 +15452,79 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_46__next__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1199);
+  __Pyx_TraceCall("__next__", __pyx_f[0], 1272);
 
-  /* "pysam/csamfile.pyx":1204
+  /* "pysam/calignmentfile.pyx":1276
+ *         python version of next().
  *         """
- *         cdef int ret
- *         ret = sam_read1(self.htsfile, self.header, self.b)             # <<<<<<<<<<<<<<
+ *         cdef int ret = self.cnext()             # <<<<<<<<<<<<<<
  *         if (ret >= 0):
- *             return makeAlignedRead(self.b)
+ *             return makeAlignedSegment(self.b)
  */
-  __pyx_v_ret = sam_read1(__pyx_v_self->htsfile, __pyx_v_self->header, __pyx_v_self->b);
+  __pyx_v_ret = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile *)__pyx_v_self->__pyx_vtab)->cnext(__pyx_v_self);
 
-  /* "pysam/csamfile.pyx":1205
- *         cdef int ret
- *         ret = sam_read1(self.htsfile, self.header, self.b)
+  /* "pysam/calignmentfile.pyx":1277
+ *         """
+ *         cdef int ret = self.cnext()
  *         if (ret >= 0):             # <<<<<<<<<<<<<<
- *             return makeAlignedRead(self.b)
- *         else:
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):
  */
   __pyx_t_1 = ((__pyx_v_ret >= 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1206
- *         ret = sam_read1(self.htsfile, self.header, self.b)
+    /* "pysam/calignmentfile.pyx":1278
+ *         cdef int ret = self.cnext()
  *         if (ret >= 0):
- *             return makeAlignedRead(self.b)             # <<<<<<<<<<<<<<
- *         else:
- *             raise StopIteration
+ *             return makeAlignedSegment(self.b)             # <<<<<<<<<<<<<<
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __pyx_f_5pysam_14calignmentfile_makeAlignedSegment(__pyx_v_self->b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1278; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
+
+  /* "pysam/calignmentfile.pyx":1279
+ *         if (ret >= 0):
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):             # <<<<<<<<<<<<<<
+ *             raise IOError('truncated file')
+ *         else:
+ */
+  __pyx_t_1 = ((__pyx_v_ret == -2) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/calignmentfile.pyx":1280
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise StopIteration
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__55, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1280; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1280; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":1208
- *             return makeAlignedRead(self.b)
+    /* "pysam/calignmentfile.pyx":1282
+ *             raise IOError('truncated file')
  *         else:
  *             raise StopIteration             # <<<<<<<<<<<<<<
  * 
  * ##-------------------------------------------------------------------
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1199
+  /* "pysam/calignmentfile.pyx":1272
  *                          self.b)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -15055,7 +15535,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_46__next__(struct __pyx_obj_
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignmentFile.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -15064,19 +15544,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_46__next__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1236
+/* "pysam/calignmentfile.pyx":1310
  *     '''
  * 
- *     def __init__(self, Samfile samfile, int reopen=True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, int multiple_iterators=False):             # <<<<<<<<<<<<<<
  * 
  *         if not samfile._isOpen():
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  int __pyx_v_reopen;
+static int __pyx_pw_5pysam_14calignmentfile_11IteratorRow_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_11IteratorRow_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
+  int __pyx_v_multiple_iterators;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -15084,7 +15564,7 @@ static int __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__(PyObject *__pyx_v_s
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_reopen,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_multiple_iterators,0};
     PyObject* values[2] = {0,0};
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
@@ -15102,12 +15582,12 @@ static int __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__(PyObject *__pyx_v_s
         else goto __pyx_L5_argtuple_error;
         case  1:
         if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
           if (value) { values[1] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1236; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1310; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -15117,23 +15597,23 @@ static int __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__(PyObject *__pyx_v_s
         default: goto __pyx_L5_argtuple_error;
       }
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
     if (values[1]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1236; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_multiple_iterators = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_multiple_iterators == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1310; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
-      __pyx_v_reopen = ((int)1);
+      __pyx_v_multiple_iterators = ((int)0);
     }
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1236; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1310; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRow.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRow.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRow *)__pyx_v_self), __pyx_v_samfile, __pyx_v_reopen);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1310; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_11IteratorRow___init__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *)__pyx_v_self), __pyx_v_samfile, __pyx_v_multiple_iterators);
 
   /* function exit code */
   goto __pyx_L0;
@@ -15144,7 +15624,7 @@ static int __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__(PyObject *__pyx_v_s
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRow *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen) {
+static int __pyx_pf_5pysam_14calignmentfile_11IteratorRow___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_multiple_iterators) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -15153,45 +15633,46 @@ static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5py
   int __pyx_t_4;
   char const *__pyx_t_5;
   htsFile *__pyx_t_6;
+  bam_hdr_t *__pyx_t_7;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1236);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 1310);
 
-  /* "pysam/csamfile.pyx":1238
- *     def __init__(self, Samfile samfile, int reopen=True):
+  /* "pysam/calignmentfile.pyx":1312
+ *     def __init__(self, AlignmentFile samfile, int multiple_iterators=False):
  * 
  *         if not samfile._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
+ *             raise ValueError("I/O operation on closed file")
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":1239
+    /* "pysam/calignmentfile.pyx":1313
  * 
  *         if not samfile._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  * 
  *         # makes sure that samfile stays alive as long as the
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__54, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__56, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1313; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1313; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1243
+  /* "pysam/calignmentfile.pyx":1317
  *         # makes sure that samfile stays alive as long as the
  *         # iterator is alive
  *         self.samfile = samfile             # <<<<<<<<<<<<<<
@@ -15204,54 +15685,70 @@ static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5py
   __Pyx_DECREF(((PyObject *)__pyx_v_self->samfile));
   __pyx_v_self->samfile = __pyx_v_samfile;
 
-  /* "pysam/csamfile.pyx":1247
+  /* "pysam/calignmentfile.pyx":1321
  *         # reopen the file - note that this makes the iterator
  *         # slow and causes pileup to slow down significantly.
- *         if reopen:             # <<<<<<<<<<<<<<
+ *         if multiple_iterators:             # <<<<<<<<<<<<<<
  *             self.htsfile = hts_open(samfile._filename, 'r')
  *             assert self.htsfile != NULL
  */
-  __pyx_t_4 = (__pyx_v_reopen != 0);
+  __pyx_t_4 = (__pyx_v_multiple_iterators != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":1248
+    /* "pysam/calignmentfile.pyx":1322
  *         # slow and causes pileup to slow down significantly.
- *         if reopen:
+ *         if multiple_iterators:
  *             self.htsfile = hts_open(samfile._filename, 'r')             # <<<<<<<<<<<<<<
  *             assert self.htsfile != NULL
  *             # read header - required for accurate positioning
  */
-    __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_samfile->_filename); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_samfile->_filename); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_self->htsfile = hts_open(__pyx_t_5, __pyx_k_r);
 
-    /* "pysam/csamfile.pyx":1249
- *         if reopen:
+    /* "pysam/calignmentfile.pyx":1323
+ *         if multiple_iterators:
  *             self.htsfile = hts_open(samfile._filename, 'r')
  *             assert self.htsfile != NULL             # <<<<<<<<<<<<<<
  *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)
+ *             # could a tell/seek work?
  */
     #ifndef CYTHON_WITHOUT_ASSERTIONS
     if (unlikely(!Py_OptimizeFlag)) {
       if (unlikely(!((__pyx_v_self->htsfile != NULL) != 0))) {
         PyErr_SetNone(PyExc_AssertionError);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1249; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
     }
     #endif
 
-    /* "pysam/csamfile.pyx":1251
- *             assert self.htsfile != NULL
+    /* "pysam/calignmentfile.pyx":1326
  *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
+ *             # could a tell/seek work?
+ *             self.header = sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
+ *             assert self.header != NULL
+ *             self.owns_samfile = True
+ */
+    __pyx_v_self->header = sam_hdr_read(__pyx_v_self->htsfile);
+
+    /* "pysam/calignmentfile.pyx":1327
+ *             # could a tell/seek work?
+ *             self.header = sam_hdr_read(self.htsfile)
+ *             assert self.header != NULL             # <<<<<<<<<<<<<<
  *             self.owns_samfile = True
  *         else:
  */
-    sam_hdr_read(__pyx_v_self->htsfile);
+    #ifndef CYTHON_WITHOUT_ASSERTIONS
+    if (unlikely(!Py_OptimizeFlag)) {
+      if (unlikely(!((__pyx_v_self->header != NULL) != 0))) {
+        PyErr_SetNone(PyExc_AssertionError);
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      }
+    }
+    #endif
 
-    /* "pysam/csamfile.pyx":1252
- *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)
+    /* "pysam/calignmentfile.pyx":1328
+ *             self.header = sam_hdr_read(self.htsfile)
+ *             assert self.header != NULL
  *             self.owns_samfile = True             # <<<<<<<<<<<<<<
  *         else:
  *             self.htsfile = self.samfile.htsfile
@@ -15261,29 +15758,39 @@ static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5py
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":1254
+    /* "pysam/calignmentfile.pyx":1330
  *             self.owns_samfile = True
  *         else:
  *             self.htsfile = self.samfile.htsfile             # <<<<<<<<<<<<<<
  *             self.owns_samfile = False
- * 
+ *             self.header = self.samfile.header
  */
     __pyx_t_6 = __pyx_v_self->samfile->htsfile;
     __pyx_v_self->htsfile = __pyx_t_6;
 
-    /* "pysam/csamfile.pyx":1255
+    /* "pysam/calignmentfile.pyx":1331
  *         else:
  *             self.htsfile = self.samfile.htsfile
  *             self.owns_samfile = False             # <<<<<<<<<<<<<<
+ *             self.header = self.samfile.header
  * 
- *         self.retval = 0
  */
     __pyx_v_self->owns_samfile = 0;
+
+    /* "pysam/calignmentfile.pyx":1332
+ *             self.htsfile = self.samfile.htsfile
+ *             self.owns_samfile = False
+ *             self.header = self.samfile.header             # <<<<<<<<<<<<<<
+ * 
+ *         self.retval = 0
+ */
+    __pyx_t_7 = __pyx_v_self->samfile->header;
+    __pyx_v_self->header = __pyx_t_7;
   }
   __pyx_L4:;
 
-  /* "pysam/csamfile.pyx":1257
- *             self.owns_samfile = False
+  /* "pysam/calignmentfile.pyx":1334
+ *             self.header = self.samfile.header
  * 
  *         self.retval = 0             # <<<<<<<<<<<<<<
  * 
@@ -15291,7 +15798,7 @@ static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5py
  */
   __pyx_v_self->retval = 0;
 
-  /* "pysam/csamfile.pyx":1259
+  /* "pysam/calignmentfile.pyx":1336
  *         self.retval = 0
  * 
  *         self.b = bam_init1()             # <<<<<<<<<<<<<<
@@ -15300,10 +15807,10 @@ static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5py
  */
   __pyx_v_self->b = bam_init1();
 
-  /* "pysam/csamfile.pyx":1236
+  /* "pysam/calignmentfile.pyx":1310
  *     '''
  * 
- *     def __init__(self, Samfile samfile, int reopen=True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, int multiple_iterators=False):             # <<<<<<<<<<<<<<
  * 
  *         if not samfile._isOpen():
  */
@@ -15314,7 +15821,7 @@ static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5py
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRow.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRow.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -15322,7 +15829,7 @@ static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1261
+/* "pysam/calignmentfile.pyx":1338
  *         self.b = bam_init1()
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -15331,24 +15838,24 @@ static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5py
  */
 
 /* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_11IteratorRow_3__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_11IteratorRow_3__dealloc__(PyObject *__pyx_v_self) {
+static void __pyx_pw_5pysam_14calignmentfile_11IteratorRow_3__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_14calignmentfile_11IteratorRow_3__dealloc__(PyObject *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_11IteratorRow_2__dealloc__(((struct __pyx_obj_5pysam_8csamfile_IteratorRow *)__pyx_v_self));
+  __pyx_pf_5pysam_14calignmentfile_11IteratorRow_2__dealloc__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
 }
 
-static void __pyx_pf_5pysam_8csamfile_11IteratorRow_2__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorRow *__pyx_v_self) {
+static void __pyx_pf_5pysam_14calignmentfile_11IteratorRow_2__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1261);
+  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1338);
 
-  /* "pysam/csamfile.pyx":1262
+  /* "pysam/calignmentfile.pyx":1339
  * 
  *     def __dealloc__(self):
  *         bam_destroy1(self.b)             # <<<<<<<<<<<<<<
@@ -15357,29 +15864,38 @@ static void __pyx_pf_5pysam_8csamfile_11IteratorRow_2__dealloc__(struct __pyx_ob
  */
   bam_destroy1(__pyx_v_self->b);
 
-  /* "pysam/csamfile.pyx":1263
+  /* "pysam/calignmentfile.pyx":1340
  *     def __dealloc__(self):
  *         bam_destroy1(self.b)
  *         if self.owns_samfile:             # <<<<<<<<<<<<<<
  *             hts_close(self.htsfile)
- * 
+ *             bam_hdr_destroy(self.header)
  */
   __pyx_t_1 = (__pyx_v_self->owns_samfile != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1264
+    /* "pysam/calignmentfile.pyx":1341
  *         bam_destroy1(self.b)
  *         if self.owns_samfile:
  *             hts_close(self.htsfile)             # <<<<<<<<<<<<<<
+ *             bam_hdr_destroy(self.header)
  * 
- * cdef class IteratorRowRegion(IteratorRow):
  */
     hts_close(__pyx_v_self->htsfile);
+
+    /* "pysam/calignmentfile.pyx":1342
+ *         if self.owns_samfile:
+ *             hts_close(self.htsfile)
+ *             bam_hdr_destroy(self.header)             # <<<<<<<<<<<<<<
+ * 
+ * 
+ */
+    bam_hdr_destroy(__pyx_v_self->header);
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1261
+  /* "pysam/calignmentfile.pyx":1338
  *         self.b = bam_init1()
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -15392,22 +15908,22 @@ static void __pyx_pf_5pysam_8csamfile_11IteratorRow_2__dealloc__(struct __pyx_ob
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/csamfile.pyx":1291
+/* "pysam/calignmentfile.pyx":1357
  *     """
  * 
- *     def __init__(self, Samfile samfile,             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile,             # <<<<<<<<<<<<<<
  *                  int tid, int beg, int end,
- *                  int reopen=True):
+ *                  int multiple_iterators=False):
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
+static int __pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
   int __pyx_v_tid;
   int __pyx_v_beg;
   int __pyx_v_end;
-  int __pyx_v_reopen;
+  int __pyx_v_multiple_iterators;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -15415,7 +15931,7 @@ static int __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__(PyObject *__p
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_tid,&__pyx_n_s_beg,&__pyx_n_s_end,&__pyx_n_s_reopen,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_tid,&__pyx_n_s_beg,&__pyx_n_s_end,&__pyx_n_s_multiple_iterators,0};
     PyObject* values[5] = {0,0,0,0,0};
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
@@ -15437,26 +15953,26 @@ static int __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__(PyObject *__p
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_tid)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1357; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_beg)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1357; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  3:
         if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1357; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  4:
         if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
           if (value) { values[4] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1357; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -15469,41 +15985,41 @@ static int __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__(PyObject *__p
         default: goto __pyx_L5_argtuple_error;
       }
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    __pyx_v_tid = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    __pyx_v_beg = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_beg == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    __pyx_v_end = __Pyx_PyInt_As_int(values[3]); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
+    __pyx_v_tid = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1358; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_beg = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_beg == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1358; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_end = __Pyx_PyInt_As_int(values[3]); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1358; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     if (values[4]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[4]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1293; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_multiple_iterators = __Pyx_PyInt_As_int(values[4]); if (unlikely((__pyx_v_multiple_iterators == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1359; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
 
-      /* "pysam/csamfile.pyx":1293
- *     def __init__(self, Samfile samfile,
+      /* "pysam/calignmentfile.pyx":1359
+ *     def __init__(self, AlignmentFile samfile,
  *                  int tid, int beg, int end,
- *                  int reopen=True):             # <<<<<<<<<<<<<<
+ *                  int multiple_iterators=False):             # <<<<<<<<<<<<<<
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  */
-      __pyx_v_reopen = ((int)1);
+      __pyx_v_multiple_iterators = ((int)0);
     }
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1357; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowRegion.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowRegion.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self), __pyx_v_samfile, __pyx_v_tid, __pyx_v_beg, __pyx_v_end, __pyx_v_reopen);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion___init__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)__pyx_v_self), __pyx_v_samfile, __pyx_v_tid, __pyx_v_beg, __pyx_v_end, __pyx_v_multiple_iterators);
 
-  /* "pysam/csamfile.pyx":1291
+  /* "pysam/calignmentfile.pyx":1357
  *     """
  * 
- *     def __init__(self, Samfile samfile,             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile,             # <<<<<<<<<<<<<<
  *                  int tid, int beg, int end,
- *                  int reopen=True):
+ *                  int multiple_iterators=False):
  */
 
   /* function exit code */
@@ -15515,7 +16031,7 @@ static int __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__(PyObject *__p
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_beg, int __pyx_v_end, int __pyx_v_reopen) {
+static int __pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_beg, int __pyx_v_end, int __pyx_v_multiple_iterators) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -15529,18 +16045,18 @@ static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1291);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 1357);
 
-  /* "pysam/csamfile.pyx":1295
- *                  int reopen=True):
+  /* "pysam/calignmentfile.pyx":1361
+ *                  int multiple_iterators=False):
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
  * 
  *         if not samfile._hasIndex():
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
@@ -15548,51 +16064,51 @@ static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_o
   __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
   __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_multiple_iterators); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_multiple_iterators, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/csamfile.pyx":1297
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+  /* "pysam/calignmentfile.pyx":1363
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  * 
  *         if not samfile._hasIndex():             # <<<<<<<<<<<<<<
  *             raise ValueError( "no index available for iteration" )
  * 
  */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_t_6 = ((!__pyx_t_5) != 0);
   if (__pyx_t_6) {
 
-    /* "pysam/csamfile.pyx":1298
+    /* "pysam/calignmentfile.pyx":1364
  * 
  *         if not samfile._hasIndex():
  *             raise ValueError( "no index available for iteration" )             # <<<<<<<<<<<<<<
  * 
  *         self.iter = sam_itr_queryi(
  */
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__55, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1298; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__57, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1298; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1300
+  /* "pysam/calignmentfile.pyx":1366
  *             raise ValueError( "no index available for iteration" )
  * 
  *         self.iter = sam_itr_queryi(             # <<<<<<<<<<<<<<
@@ -15601,12 +16117,12 @@ static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_o
  */
   __pyx_v_self->iter = sam_itr_queryi(__pyx_v_self->__pyx_base.samfile->index, __pyx_v_tid, __pyx_v_beg, __pyx_v_end);
 
-  /* "pysam/csamfile.pyx":1291
+  /* "pysam/calignmentfile.pyx":1357
  *     """
  * 
- *     def __init__(self, Samfile samfile,             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile,             # <<<<<<<<<<<<<<
  *                  int tid, int beg, int end,
- *                  int reopen=True):
+ *                  int multiple_iterators=False):
  */
 
   /* function exit code */
@@ -15617,7 +16133,7 @@ static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_o
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowRegion.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowRegion.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -15625,7 +16141,7 @@ static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1306
+/* "pysam/calignmentfile.pyx":1372
  *             end)
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -15634,38 +16150,38 @@ static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_o
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_3__iter__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_3__iter__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_3__iter__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion_2__iter__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1306);
+  __Pyx_TraceCall("__iter__", __pyx_f[0], 1372);
 
-  /* "pysam/csamfile.pyx":1307
+  /* "pysam/calignmentfile.pyx":1373
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
  * 
- *     cdef bam1_t * getCurrent( self ):
+ *     cdef bam1_t * getCurrent(self):
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1306
+  /* "pysam/calignmentfile.pyx":1372
  *             end)
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -15681,24 +16197,24 @@ static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_2__iter__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1309
+/* "pysam/calignmentfile.pyx":1375
  *         return self
  * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
+ *     cdef bam1_t * getCurrent(self):             # <<<<<<<<<<<<<<
  *         return self.b
  * 
  */
 
-static bam1_t *__pyx_f_5pysam_8csamfile_17IteratorRowRegion_getCurrent(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
+static bam1_t *__pyx_f_5pysam_14calignmentfile_17IteratorRowRegion_getCurrent(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self) {
   bam1_t *__pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1309);
+  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1375);
 
-  /* "pysam/csamfile.pyx":1310
+  /* "pysam/calignmentfile.pyx":1376
  * 
- *     cdef bam1_t * getCurrent( self ):
+ *     cdef bam1_t * getCurrent(self):
  *         return self.b             # <<<<<<<<<<<<<<
  * 
  *     cdef int cnext(self):
@@ -15706,10 +16222,10 @@ static bam1_t *__pyx_f_5pysam_8csamfile_17IteratorRowRegion_getCurrent(struct __
   __pyx_r = __pyx_v_self->__pyx_base.b;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1309
+  /* "pysam/calignmentfile.pyx":1375
  *         return self
  * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
+ *     cdef bam1_t * getCurrent(self):             # <<<<<<<<<<<<<<
  *         return self.b
  * 
  */
@@ -15721,7 +16237,7 @@ static bam1_t *__pyx_f_5pysam_8csamfile_17IteratorRowRegion_getCurrent(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1312
+/* "pysam/calignmentfile.pyx":1378
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -15729,14 +16245,14 @@ static bam1_t *__pyx_f_5pysam_8csamfile_17IteratorRowRegion_getCurrent(struct __
  *         self.retval = hts_itr_next(self.htsfile.fp.bgzf,
  */
 
-static int __pyx_f_5pysam_8csamfile_17IteratorRowRegion_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
+static int __pyx_f_5pysam_14calignmentfile_17IteratorRowRegion_cnext(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1312);
+  __Pyx_TraceCall("cnext", __pyx_f[0], 1378);
 
-  /* "pysam/csamfile.pyx":1314
+  /* "pysam/calignmentfile.pyx":1380
  *     cdef int cnext(self):
  *         '''cversion of iterator. Used by IteratorColumn'''
  *         self.retval = hts_itr_next(self.htsfile.fp.bgzf,             # <<<<<<<<<<<<<<
@@ -15745,7 +16261,7 @@ static int __pyx_f_5pysam_8csamfile_17IteratorRowRegion_cnext(struct __pyx_obj_5
  */
   __pyx_v_self->__pyx_base.retval = hts_itr_next(__pyx_v_self->__pyx_base.htsfile->fp.bgzf, __pyx_v_self->iter, __pyx_v_self->__pyx_base.b, NULL);
 
-  /* "pysam/csamfile.pyx":1312
+  /* "pysam/calignmentfile.pyx":1378
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -15760,7 +16276,7 @@ static int __pyx_f_5pysam_8csamfile_17IteratorRowRegion_cnext(struct __pyx_obj_5
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1319
+/* "pysam/calignmentfile.pyx":1385
  *                                    NULL)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -15769,23 +16285,23 @@ static int __pyx_f_5pysam_8csamfile_17IteratorRowRegion_cnext(struct __pyx_obj_5
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_5__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_17IteratorRowRegion_4__next__[] = "python version of next().\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_5__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_17IteratorRowRegion_4__next__[] = "python version of next().\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_17IteratorRowRegion_4__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_14calignmentfile_17IteratorRowRegion_4__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_5__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_5__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion_4__next__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion_4__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -15795,53 +16311,53 @@ static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1319);
+  __Pyx_TraceCall("__next__", __pyx_f[0], 1385);
 
-  /* "pysam/csamfile.pyx":1322
+  /* "pysam/calignmentfile.pyx":1388
  *         """python version of next().
  *         """
  *         self.cnext()             # <<<<<<<<<<<<<<
  *         if self.retval < 0:
  *             raise StopIteration
  */
-  ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self->__pyx_vtab)->cnext(__pyx_v_self);
+  ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion *)__pyx_v_self->__pyx_vtab)->cnext(__pyx_v_self);
 
-  /* "pysam/csamfile.pyx":1323
+  /* "pysam/calignmentfile.pyx":1389
  *         """
  *         self.cnext()
  *         if self.retval < 0:             # <<<<<<<<<<<<<<
  *             raise StopIteration
- *         return makeAlignedRead(self.b)
+ *         return makeAlignedSegment(self.b)
  */
   __pyx_t_1 = ((__pyx_v_self->__pyx_base.retval < 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1324
+    /* "pysam/calignmentfile.pyx":1390
  *         self.cnext()
  *         if self.retval < 0:
  *             raise StopIteration             # <<<<<<<<<<<<<<
- *         return makeAlignedRead(self.b)
+ *         return makeAlignedSegment(self.b)
  * 
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1325
+  /* "pysam/calignmentfile.pyx":1391
  *         if self.retval < 0:
  *             raise StopIteration
- *         return makeAlignedRead(self.b)             # <<<<<<<<<<<<<<
+ *         return makeAlignedSegment(self.b)             # <<<<<<<<<<<<<<
  * 
  *     def __dealloc__(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __pyx_f_5pysam_14calignmentfile_makeAlignedSegment(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1319
+  /* "pysam/calignmentfile.pyx":1385
  *                                    NULL)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -15852,7 +16368,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(struct
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowRegion.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowRegion.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -15861,8 +16377,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1327
- *         return makeAlignedRead(self.b)
+/* "pysam/calignmentfile.pyx":1393
+ *         return makeAlignedSegment(self.b)
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
  *         hts_itr_destroy(self.iter)
@@ -15870,23 +16386,23 @@ static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(struct
  */
 
 /* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_7__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_7__dealloc__(PyObject *__pyx_v_self) {
+static void __pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_7__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_7__dealloc__(PyObject *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_6__dealloc__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self));
+  __pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion_6__dealloc__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
 }
 
-static void __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_6__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
+static void __pyx_pf_5pysam_14calignmentfile_17IteratorRowRegion_6__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1327);
+  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1393);
 
-  /* "pysam/csamfile.pyx":1328
+  /* "pysam/calignmentfile.pyx":1394
  * 
  *     def __dealloc__(self):
  *         hts_itr_destroy(self.iter)             # <<<<<<<<<<<<<<
@@ -15895,8 +16411,8 @@ static void __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_6__dealloc__(struct __
  */
   hts_itr_destroy(__pyx_v_self->iter);
 
-  /* "pysam/csamfile.pyx":1327
- *         return makeAlignedRead(self.b)
+  /* "pysam/calignmentfile.pyx":1393
+ *         return makeAlignedSegment(self.b)
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
  *         hts_itr_destroy(self.iter)
@@ -15908,20 +16424,20 @@ static void __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_6__dealloc__(struct __
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/csamfile.pyx":1346
+/* "pysam/calignmentfile.pyx":1408
  *     """
  * 
- *     def __init__(self, Samfile samfile, int n, int reopen=True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, int n, int multiple_iterators=False):             # <<<<<<<<<<<<<<
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
+static int __pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
   int __pyx_v_n;
-  int __pyx_v_reopen;
+  int __pyx_v_multiple_iterators;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -15929,7 +16445,7 @@ static int __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__(PyObject *__pyx
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_n,&__pyx_n_s_reopen,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_n,&__pyx_n_s_multiple_iterators,0};
     PyObject* values[3] = {0,0,0};
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
@@ -15949,16 +16465,16 @@ static int __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__(PyObject *__pyx
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_n)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1408; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
           if (value) { values[2] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1408; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -15969,24 +16485,24 @@ static int __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__(PyObject *__pyx
         default: goto __pyx_L5_argtuple_error;
       }
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    __pyx_v_n = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_n == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
+    __pyx_v_n = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_n == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1408; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     if (values[2]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_multiple_iterators = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_multiple_iterators == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1408; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
-      __pyx_v_reopen = ((int)1);
+      __pyx_v_multiple_iterators = ((int)0);
     }
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1408; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowHead.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowHead.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *)__pyx_v_self), __pyx_v_samfile, __pyx_v_n, __pyx_v_reopen);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1408; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_15IteratorRowHead___init__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *)__pyx_v_self), __pyx_v_samfile, __pyx_v_n, __pyx_v_multiple_iterators);
 
   /* function exit code */
   goto __pyx_L0;
@@ -15997,7 +16513,7 @@ static int __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__(PyObject *__pyx
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_n, int __pyx_v_reopen) {
+static int __pyx_pf_5pysam_14calignmentfile_15IteratorRowHead___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_n, int __pyx_v_multiple_iterators) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -16009,18 +16525,18 @@ static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1346);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 1408);
 
-  /* "pysam/csamfile.pyx":1348
- *     def __init__(self, Samfile samfile, int n, int reopen=True):
+  /* "pysam/calignmentfile.pyx":1410
+ *     def __init__(self, AlignmentFile samfile, int n, int multiple_iterators=False):
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
  * 
  *         self.max_rows = n
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
@@ -16028,21 +16544,21 @@ static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj
   __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
   __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_multiple_iterators); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_multiple_iterators, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/csamfile.pyx":1350
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+  /* "pysam/calignmentfile.pyx":1412
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  * 
  *         self.max_rows = n             # <<<<<<<<<<<<<<
  *         self.current_row = 0
@@ -16050,7 +16566,7 @@ static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj
  */
   __pyx_v_self->max_rows = __pyx_v_n;
 
-  /* "pysam/csamfile.pyx":1351
+  /* "pysam/calignmentfile.pyx":1413
  * 
  *         self.max_rows = n
  *         self.current_row = 0             # <<<<<<<<<<<<<<
@@ -16059,12 +16575,12 @@ static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj
  */
   __pyx_v_self->current_row = 0;
 
-  /* "pysam/csamfile.pyx":1346
+  /* "pysam/calignmentfile.pyx":1408
  *     """
  * 
- *     def __init__(self, Samfile samfile, int n, int reopen=True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, int n, int multiple_iterators=False):             # <<<<<<<<<<<<<<
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  */
 
   /* function exit code */
@@ -16075,7 +16591,7 @@ static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowHead.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowHead.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -16083,7 +16599,7 @@ static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1353
+/* "pysam/calignmentfile.pyx":1415
  *         self.current_row = 0
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -16092,26 +16608,26 @@ static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_15IteratorRowHead_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_15IteratorRowHead_3__iter__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_3__iter__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_3__iter__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_15IteratorRowHead_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_15IteratorRowHead_2__iter__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_15IteratorRowHead_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1353);
+  __Pyx_TraceCall("__iter__", __pyx_f[0], 1415);
 
-  /* "pysam/csamfile.pyx":1354
+  /* "pysam/calignmentfile.pyx":1416
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
@@ -16123,7 +16639,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_2__iter__(struct __
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1353
+  /* "pysam/calignmentfile.pyx":1415
  *         self.current_row = 0
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -16139,7 +16655,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_2__iter__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1356
+/* "pysam/calignmentfile.pyx":1418
  *         return self
  * 
  *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
@@ -16147,14 +16663,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_2__iter__(struct __
  * 
  */
 
-static bam1_t *__pyx_f_5pysam_8csamfile_15IteratorRowHead_getCurrent(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self) {
+static bam1_t *__pyx_f_5pysam_14calignmentfile_15IteratorRowHead_getCurrent(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *__pyx_v_self) {
   bam1_t *__pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1356);
+  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1418);
 
-  /* "pysam/csamfile.pyx":1357
+  /* "pysam/calignmentfile.pyx":1419
  * 
  *     cdef bam1_t * getCurrent( self ):
  *         return self.b             # <<<<<<<<<<<<<<
@@ -16164,7 +16680,7 @@ static bam1_t *__pyx_f_5pysam_8csamfile_15IteratorRowHead_getCurrent(struct __py
   __pyx_r = __pyx_v_self->__pyx_base.b;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1356
+  /* "pysam/calignmentfile.pyx":1418
  *         return self
  * 
  *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
@@ -16179,7 +16695,7 @@ static bam1_t *__pyx_f_5pysam_8csamfile_15IteratorRowHead_getCurrent(struct __py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1359
+/* "pysam/calignmentfile.pyx":1421
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -16187,14 +16703,14 @@ static bam1_t *__pyx_f_5pysam_8csamfile_15IteratorRowHead_getCurrent(struct __py
  *         return sam_read1(self.htsfile,
  */
 
-static int __pyx_f_5pysam_8csamfile_15IteratorRowHead_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self) {
+static int __pyx_f_5pysam_14calignmentfile_15IteratorRowHead_cnext(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1359);
+  __Pyx_TraceCall("cnext", __pyx_f[0], 1421);
 
-  /* "pysam/csamfile.pyx":1361
+  /* "pysam/calignmentfile.pyx":1423
  *     cdef int cnext(self):
  *         '''cversion of iterator. Used by IteratorColumn'''
  *         return sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
@@ -16204,7 +16720,7 @@ static int __pyx_f_5pysam_8csamfile_15IteratorRowHead_cnext(struct __pyx_obj_5py
   __pyx_r = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1359
+  /* "pysam/calignmentfile.pyx":1421
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -16219,7 +16735,7 @@ static int __pyx_f_5pysam_8csamfile_15IteratorRowHead_cnext(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1365
+/* "pysam/calignmentfile.pyx":1427
  *                          self.b)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -16228,23 +16744,23 @@ static int __pyx_f_5pysam_8csamfile_15IteratorRowHead_cnext(struct __pyx_obj_5py
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_15IteratorRowHead_5__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_15IteratorRowHead_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_5__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_15IteratorRowHead_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_15IteratorRowHead_4__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_14calignmentfile_15IteratorRowHead_4__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_8csamfile_15IteratorRowHead_5__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_5__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_15IteratorRowHead_4__next__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_15IteratorRowHead_4__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *__pyx_v_self) {
   int __pyx_v_ret;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -16255,9 +16771,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(struct __
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1365);
+  __Pyx_TraceCall("__next__", __pyx_f[0], 1427);
 
-  /* "pysam/csamfile.pyx":1370
+  /* "pysam/calignmentfile.pyx":1432
  *         pyrex uses this non-standard name instead of next()
  *         """
  *         if self.current_row >= self.max_rows:             # <<<<<<<<<<<<<<
@@ -16267,73 +16783,97 @@ static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(struct __
   __pyx_t_1 = ((__pyx_v_self->current_row >= __pyx_v_self->max_rows) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1371
+    /* "pysam/calignmentfile.pyx":1433
  *         """
  *         if self.current_row >= self.max_rows:
  *             raise StopIteration             # <<<<<<<<<<<<<<
  * 
- *         cdef int ret
+ *         cdef int ret = self.cnext()
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1374
+  /* "pysam/calignmentfile.pyx":1435
+ *             raise StopIteration
  * 
- *         cdef int ret
- *         ret = sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                         self.samfile.header, self.b)
+ *         cdef int ret = self.cnext()             # <<<<<<<<<<<<<<
  *         if (ret >= 0):
+ *             self.current_row += 1
  */
-  __pyx_v_ret = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
+  __pyx_v_ret = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowHead *)__pyx_v_self->__pyx_vtab)->cnext(__pyx_v_self);
 
-  /* "pysam/csamfile.pyx":1376
- *         ret = sam_read1(self.htsfile,
- *                         self.samfile.header, self.b)
+  /* "pysam/calignmentfile.pyx":1436
+ * 
+ *         cdef int ret = self.cnext()
  *         if (ret >= 0):             # <<<<<<<<<<<<<<
  *             self.current_row += 1
- *             return makeAlignedRead( self.b )
+ *             return makeAlignedSegment( self.b )
  */
   __pyx_t_1 = ((__pyx_v_ret >= 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1377
- *                         self.samfile.header, self.b)
+    /* "pysam/calignmentfile.pyx":1437
+ *         cdef int ret = self.cnext()
  *         if (ret >= 0):
  *             self.current_row += 1             # <<<<<<<<<<<<<<
- *             return makeAlignedRead( self.b )
- *         else:
+ *             return makeAlignedSegment( self.b )
+ *         elif (ret == -2):
  */
     __pyx_v_self->current_row = (__pyx_v_self->current_row + 1);
 
-    /* "pysam/csamfile.pyx":1378
+    /* "pysam/calignmentfile.pyx":1438
  *         if (ret >= 0):
  *             self.current_row += 1
- *             return makeAlignedRead( self.b )             # <<<<<<<<<<<<<<
- *         else:
- *             raise StopIteration
+ *             return makeAlignedSegment( self.b )             # <<<<<<<<<<<<<<
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __pyx_f_5pysam_14calignmentfile_makeAlignedSegment(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1438; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
+
+  /* "pysam/calignmentfile.pyx":1439
+ *             self.current_row += 1
+ *             return makeAlignedSegment( self.b )
+ *         elif (ret == -2):             # <<<<<<<<<<<<<<
+ *             raise IOError('truncated file')
+ *         else:
+ */
+  __pyx_t_1 = ((__pyx_v_ret == -2) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/calignmentfile.pyx":1440
+ *             return makeAlignedSegment( self.b )
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise StopIteration
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__58, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":1380
- *             return makeAlignedRead( self.b )
+    /* "pysam/calignmentfile.pyx":1442
+ *             raise IOError('truncated file')
  *         else:
  *             raise StopIteration             # <<<<<<<<<<<<<<
  * 
  * 
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1365
+  /* "pysam/calignmentfile.pyx":1427
  *                          self.b)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -16344,7 +16884,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(struct __
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowHead.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowHead.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -16353,19 +16893,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1399
+/* "pysam/calignmentfile.pyx":1457
  *     """
  * 
- *     def __init__(self, Samfile samfile, int reopen = True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, int multiple_iterators=False):             # <<<<<<<<<<<<<<
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  int __pyx_v_reopen;
+static int __pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
+  int __pyx_v_multiple_iterators;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -16373,7 +16913,7 @@ static int __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__(PyObject *__pyx_
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_reopen,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_multiple_iterators,0};
     PyObject* values[2] = {0,0};
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
@@ -16391,12 +16931,12 @@ static int __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__(PyObject *__pyx_
         else goto __pyx_L5_argtuple_error;
         case  1:
         if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
           if (value) { values[1] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1399; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1457; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -16406,23 +16946,23 @@ static int __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__(PyObject *__pyx_
         default: goto __pyx_L5_argtuple_error;
       }
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
     if (values[1]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1399; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_multiple_iterators = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_multiple_iterators == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1457; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
-      __pyx_v_reopen = ((int)1);
+      __pyx_v_multiple_iterators = ((int)0);
     }
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1399; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1457; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAll.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowAll.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1399; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *)__pyx_v_self), __pyx_v_samfile, __pyx_v_reopen);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14IteratorRowAll___init__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *)__pyx_v_self), __pyx_v_samfile, __pyx_v_multiple_iterators);
 
   /* function exit code */
   goto __pyx_L0;
@@ -16433,7 +16973,7 @@ static int __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__(PyObject *__pyx_
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen) {
+static int __pyx_pf_5pysam_14calignmentfile_14IteratorRowAll___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_multiple_iterators) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -16445,18 +16985,18 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1399);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 1457);
 
-  /* "pysam/csamfile.pyx":1401
- *     def __init__(self, Samfile samfile, int reopen = True):
+  /* "pysam/calignmentfile.pyx":1459
+ *     def __init__(self, AlignmentFile samfile, int multiple_iterators=False):
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
  * 
  *     def __iter__(self):
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
@@ -16464,25 +17004,25 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(struct __pyx_obj_
   __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
   __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_multiple_iterators); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_multiple_iterators, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/csamfile.pyx":1399
+  /* "pysam/calignmentfile.pyx":1457
  *     """
  * 
- *     def __init__(self, Samfile samfile, int reopen = True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, int multiple_iterators=False):             # <<<<<<<<<<<<<<
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  */
 
   /* function exit code */
@@ -16493,7 +17033,7 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(struct __pyx_obj_
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAll.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowAll.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -16501,8 +17041,8 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1403
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+/* "pysam/calignmentfile.pyx":1461
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
@@ -16510,26 +17050,26 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(struct __pyx_obj_
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorRowAll_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorRowAll_3__iter__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_3__iter__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_3__iter__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorRowAll_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14IteratorRowAll_2__iter__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorRowAll_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1403);
+  __Pyx_TraceCall("__iter__", __pyx_f[0], 1461);
 
-  /* "pysam/csamfile.pyx":1404
+  /* "pysam/calignmentfile.pyx":1462
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
@@ -16541,8 +17081,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_2__iter__(struct __p
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1403
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+  /* "pysam/calignmentfile.pyx":1461
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
@@ -16557,7 +17097,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_2__iter__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1406
+/* "pysam/calignmentfile.pyx":1464
  *         return self
  * 
  *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
@@ -16565,14 +17105,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_2__iter__(struct __p
  * 
  */
 
-static bam1_t *__pyx_f_5pysam_8csamfile_14IteratorRowAll_getCurrent(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self) {
+static bam1_t *__pyx_f_5pysam_14calignmentfile_14IteratorRowAll_getCurrent(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *__pyx_v_self) {
   bam1_t *__pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1406);
+  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1464);
 
-  /* "pysam/csamfile.pyx":1407
+  /* "pysam/calignmentfile.pyx":1465
  * 
  *     cdef bam1_t * getCurrent( self ):
  *         return self.b             # <<<<<<<<<<<<<<
@@ -16582,7 +17122,7 @@ static bam1_t *__pyx_f_5pysam_8csamfile_14IteratorRowAll_getCurrent(struct __pyx
   __pyx_r = __pyx_v_self->__pyx_base.b;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1406
+  /* "pysam/calignmentfile.pyx":1464
  *         return self
  * 
  *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
@@ -16597,7 +17137,7 @@ static bam1_t *__pyx_f_5pysam_8csamfile_14IteratorRowAll_getCurrent(struct __pyx
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1409
+/* "pysam/calignmentfile.pyx":1467
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -16605,14 +17145,14 @@ static bam1_t *__pyx_f_5pysam_8csamfile_14IteratorRowAll_getCurrent(struct __pyx
  *         return sam_read1(self.htsfile,
  */
 
-static int __pyx_f_5pysam_8csamfile_14IteratorRowAll_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self) {
+static int __pyx_f_5pysam_14calignmentfile_14IteratorRowAll_cnext(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1409);
+  __Pyx_TraceCall("cnext", __pyx_f[0], 1467);
 
-  /* "pysam/csamfile.pyx":1411
+  /* "pysam/calignmentfile.pyx":1469
  *     cdef int cnext(self):
  *         '''cversion of iterator. Used by IteratorColumn'''
  *         return sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
@@ -16622,7 +17162,7 @@ static int __pyx_f_5pysam_8csamfile_14IteratorRowAll_cnext(struct __pyx_obj_5pys
   __pyx_r = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1409
+  /* "pysam/calignmentfile.pyx":1467
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -16637,7 +17177,7 @@ static int __pyx_f_5pysam_8csamfile_14IteratorRowAll_cnext(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1415
+/* "pysam/calignmentfile.pyx":1473
  *                          self.b)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -16646,23 +17186,23 @@ static int __pyx_f_5pysam_8csamfile_14IteratorRowAll_cnext(struct __pyx_obj_5pys
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorRowAll_5__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_14IteratorRowAll_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_5__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14IteratorRowAll_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_14IteratorRowAll_4__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_14calignmentfile_14IteratorRowAll_4__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorRowAll_5__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_5__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorRowAll_4__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14IteratorRowAll_4__next__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorRowAll_4__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *__pyx_v_self) {
   int __pyx_v_ret;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -16673,55 +17213,79 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_4__next__(struct __p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1415);
+  __Pyx_TraceCall("__next__", __pyx_f[0], 1473);
 
-  /* "pysam/csamfile.pyx":1421
+  /* "pysam/calignmentfile.pyx":1478
+ *         pyrex uses this non-standard name instead of next()
  *         """
- *         cdef int ret
- *         ret = sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                         self.samfile.header,
- *                         self.b)
+ *         cdef int ret = self.cnext()             # <<<<<<<<<<<<<<
+ *         if (ret >= 0):
+ *             return makeAlignedSegment(self.b)
  */
-  __pyx_v_ret = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
+  __pyx_v_ret = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowAll *)__pyx_v_self->__pyx_vtab)->cnext(__pyx_v_self);
 
-  /* "pysam/csamfile.pyx":1424
- *                         self.samfile.header,
- *                         self.b)
+  /* "pysam/calignmentfile.pyx":1479
+ *         """
+ *         cdef int ret = self.cnext()
  *         if (ret >= 0):             # <<<<<<<<<<<<<<
- *             return makeAlignedRead(self.b)
- *         else:
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):
  */
   __pyx_t_1 = ((__pyx_v_ret >= 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1425
- *                         self.b)
+    /* "pysam/calignmentfile.pyx":1480
+ *         cdef int ret = self.cnext()
  *         if (ret >= 0):
- *             return makeAlignedRead(self.b)             # <<<<<<<<<<<<<<
- *         else:
- *             raise StopIteration
+ *             return makeAlignedSegment(self.b)             # <<<<<<<<<<<<<<
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __pyx_f_5pysam_14calignmentfile_makeAlignedSegment(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
+
+  /* "pysam/calignmentfile.pyx":1481
+ *         if (ret >= 0):
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):             # <<<<<<<<<<<<<<
+ *             raise IOError('truncated file')
+ *         else:
+ */
+  __pyx_t_1 = ((__pyx_v_ret == -2) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/calignmentfile.pyx":1482
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise StopIteration
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__59, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":1427
- *             return makeAlignedRead(self.b)
+    /* "pysam/calignmentfile.pyx":1484
+ *             raise IOError('truncated file')
  *         else:
  *             raise StopIteration             # <<<<<<<<<<<<<<
  * 
  * 
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1415
+  /* "pysam/calignmentfile.pyx":1473
  *                          self.b)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -16732,7 +17296,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_4__next__(struct __p
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAll.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowAll.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -16741,19 +17305,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_4__next__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1438
+/* "pysam/calignmentfile.pyx":1495
  *     """
  * 
- *     def __init__(self, Samfile samfile, reopen=True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, multiple_iterators=False):             # <<<<<<<<<<<<<<
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+ *         IteratorRow.__init__(self, samfile,
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  PyObject *__pyx_v_reopen = 0;
+static int __pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
+  PyObject *__pyx_v_multiple_iterators = 0;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -16761,9 +17325,9 @@ static int __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__(PyObject *__
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_reopen,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_multiple_iterators,0};
     PyObject* values[2] = {0,0};
-    values[1] = ((PyObject *)Py_True);
+    values[1] = ((PyObject *)Py_False);
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
       const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
@@ -16780,12 +17344,12 @@ static int __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__(PyObject *__
         else goto __pyx_L5_argtuple_error;
         case  1:
         if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
           if (value) { values[1] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1438; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1495; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -16795,19 +17359,19 @@ static int __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__(PyObject *__
         default: goto __pyx_L5_argtuple_error;
       }
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    __pyx_v_reopen = values[1];
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
+    __pyx_v_multiple_iterators = values[1];
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1438; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1495; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAllRefs.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowAllRefs.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1438; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)__pyx_v_self), __pyx_v_samfile, __pyx_v_reopen);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs___init__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *)__pyx_v_self), __pyx_v_samfile, __pyx_v_multiple_iterators);
 
   /* function exit code */
   goto __pyx_L0;
@@ -16818,7 +17382,7 @@ static int __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__(PyObject *__
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_reopen) {
+static int __pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, PyObject *__pyx_v_multiple_iterators) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -16832,18 +17396,18 @@ static int __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(struct __pyx_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1438);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 1495);
 
-  /* "pysam/csamfile.pyx":1440
- *     def __init__(self, Samfile samfile, reopen=True):
+  /* "pysam/calignmentfile.pyx":1497
+ *     def __init__(self, AlignmentFile samfile, multiple_iterators=False):
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
+ *         IteratorRow.__init__(self, samfile,             # <<<<<<<<<<<<<<
+ *                              multiple_iterators=multiple_iterators)
  * 
- *         if not samfile._hasIndex():
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
@@ -16851,48 +17415,64 @@ static int __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(struct __pyx_
   __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
   __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_v_reopen) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":1498
+ * 
+ *         IteratorRow.__init__(self, samfile,
+ *                              multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
+ * 
+ *         if not samfile._hasIndex():
+ */
+  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_multiple_iterators, __pyx_v_multiple_iterators) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":1497
+ *     def __init__(self, AlignmentFile samfile, multiple_iterators=False):
+ * 
+ *         IteratorRow.__init__(self, samfile,             # <<<<<<<<<<<<<<
+ *                              multiple_iterators=multiple_iterators)
+ * 
+ */
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/csamfile.pyx":1442
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+  /* "pysam/calignmentfile.pyx":1500
+ *                              multiple_iterators=multiple_iterators)
  * 
  *         if not samfile._hasIndex():             # <<<<<<<<<<<<<<
  *             raise ValueError("no index available for fetch")
  * 
  */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_t_6 = ((!__pyx_t_5) != 0);
   if (__pyx_t_6) {
 
-    /* "pysam/csamfile.pyx":1443
+    /* "pysam/calignmentfile.pyx":1501
  * 
  *         if not samfile._hasIndex():
  *             raise ValueError("no index available for fetch")             # <<<<<<<<<<<<<<
  * 
  *         self.tid = -1
  */
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__56, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__60, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1445
+  /* "pysam/calignmentfile.pyx":1503
  *             raise ValueError("no index available for fetch")
  * 
  *         self.tid = -1             # <<<<<<<<<<<<<<
@@ -16901,12 +17481,12 @@ static int __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(struct __pyx_
  */
   __pyx_v_self->tid = -1;
 
-  /* "pysam/csamfile.pyx":1438
+  /* "pysam/calignmentfile.pyx":1495
  *     """
  * 
- *     def __init__(self, Samfile samfile, reopen=True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, multiple_iterators=False):             # <<<<<<<<<<<<<<
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+ *         IteratorRow.__init__(self, samfile,
  */
 
   /* function exit code */
@@ -16917,7 +17497,7 @@ static int __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(struct __pyx_
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAllRefs.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowAllRefs.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -16925,58 +17505,60 @@ static int __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1447
+/* "pysam/calignmentfile.pyx":1505
  *         self.tid = -1
  * 
  *     def nextiter(self):             # <<<<<<<<<<<<<<
- *         self.rowiter = IteratorRowRegion(self.samfile,
- *                                          self.tid,
+ *         # get a new iterator for a chromosome. The file
+ *         # will not be re-opened.
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_3nextiter(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter[] = "IteratorRowAllRefs.nextiter(self)";
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_3nextiter(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_3nextiter(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_18IteratorRowAllRefs_2nextiter[] = "IteratorRowAllRefs.nextiter(self)";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_3nextiter(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("nextiter (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs_2nextiter(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs_2nextiter(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
+  htsFile *__pyx_t_3;
+  bam_hdr_t *__pyx_t_4;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("nextiter", 0);
-  __Pyx_TraceCall("nextiter", __pyx_f[0], 1447);
+  __Pyx_TraceCall("nextiter", __pyx_f[0], 1505);
 
-  /* "pysam/csamfile.pyx":1449
- *     def nextiter(self):
+  /* "pysam/calignmentfile.pyx":1509
+ *         # will not be re-opened.
  *         self.rowiter = IteratorRowRegion(self.samfile,
  *                                          self.tid,             # <<<<<<<<<<<<<<
  *                                          0,
  *                                          1<<29)
  */
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
 
-  /* "pysam/csamfile.pyx":1448
- * 
- *     def nextiter(self):
+  /* "pysam/calignmentfile.pyx":1508
+ *         # get a new iterator for a chromosome. The file
+ *         # will not be re-opened.
  *         self.rowiter = IteratorRowRegion(self.samfile,             # <<<<<<<<<<<<<<
  *                                          self.tid,
  *                                          0,
  */
-  __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(((PyObject *)__pyx_v_self->__pyx_base.samfile));
   PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self->__pyx_base.samfile));
@@ -16990,21 +17572,50 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(struct
   PyTuple_SET_ITEM(__pyx_t_2, 3, __pyx_int_536870912);
   __Pyx_GIVEREF(__pyx_int_536870912);
   __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowRegion)), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion)), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_GIVEREF(__pyx_t_1);
   __Pyx_GOTREF(__pyx_v_self->rowiter);
   __Pyx_DECREF(((PyObject *)__pyx_v_self->rowiter));
-  __pyx_v_self->rowiter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_t_1);
+  __pyx_v_self->rowiter = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1447
+  /* "pysam/calignmentfile.pyx":1514
+ *         # set htsfile and header of the rowiter
+ *         # to the values in this iterator to reflect multiple_iterators
+ *         self.rowiter.htsfile = self.htsfile             # <<<<<<<<<<<<<<
+ *         self.rowiter.header = self.header
+ * 
+ */
+  __pyx_t_3 = __pyx_v_self->__pyx_base.htsfile;
+  __pyx_v_self->rowiter->__pyx_base.htsfile = __pyx_t_3;
+
+  /* "pysam/calignmentfile.pyx":1515
+ *         # to the values in this iterator to reflect multiple_iterators
+ *         self.rowiter.htsfile = self.htsfile
+ *         self.rowiter.header = self.header             # <<<<<<<<<<<<<<
+ * 
+ *         # make sure the iterator understand that IteratorRowAllRefs
+ */
+  __pyx_t_4 = __pyx_v_self->__pyx_base.header;
+  __pyx_v_self->rowiter->__pyx_base.header = __pyx_t_4;
+
+  /* "pysam/calignmentfile.pyx":1519
+ *         # make sure the iterator understand that IteratorRowAllRefs
+ *         # has ownership
+ *         self.rowiter.owns_samfile = False             # <<<<<<<<<<<<<<
+ * 
+ *     def __iter__(self):
+ */
+  __pyx_v_self->rowiter->__pyx_base.owns_samfile = 0;
+
+  /* "pysam/calignmentfile.pyx":1505
  *         self.tid = -1
  * 
  *     def nextiter(self):             # <<<<<<<<<<<<<<
- *         self.rowiter = IteratorRowRegion(self.samfile,
- *                                          self.tid,
+ *         # get a new iterator for a chromosome. The file
+ *         # will not be re-opened.
  */
 
   /* function exit code */
@@ -17013,7 +17624,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(struct
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAllRefs.nextiter", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowAllRefs.nextiter", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -17022,8 +17633,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1453
- *                                          1<<29)
+/* "pysam/calignmentfile.pyx":1521
+ *         self.rowiter.owns_samfile = False
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
@@ -17031,26 +17642,26 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(struct
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_5__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_5__iter__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_5__iter__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_5__iter__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_4__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs_4__iter__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_4__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs_4__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1453);
+  __Pyx_TraceCall("__iter__", __pyx_f[0], 1521);
 
-  /* "pysam/csamfile.pyx":1454
+  /* "pysam/calignmentfile.pyx":1522
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
@@ -17062,8 +17673,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_4__iter__(struct
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1453
- *                                          1<<29)
+  /* "pysam/calignmentfile.pyx":1521
+ *         self.rowiter.owns_samfile = False
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
@@ -17078,7 +17689,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_4__iter__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1456
+/* "pysam/calignmentfile.pyx":1524
  *         return self
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -17087,23 +17698,23 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_4__iter__(struct
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_7__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_6__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_7__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_18IteratorRowAllRefs_6__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_7__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_7__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -17116,9 +17727,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1456);
+  __Pyx_TraceCall("__next__", __pyx_f[0], 1524);
 
-  /* "pysam/csamfile.pyx":1462
+  /* "pysam/calignmentfile.pyx":1530
  *         """
  *         # Create an initial iterator
  *         if self.tid == -1:             # <<<<<<<<<<<<<<
@@ -17128,21 +17739,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
   __pyx_t_1 = ((__pyx_v_self->tid == -1) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1463
+    /* "pysam/calignmentfile.pyx":1531
  *         # Create an initial iterator
  *         if self.tid == -1:
  *             if not self.samfile.nreferences:             # <<<<<<<<<<<<<<
  *                 raise StopIteration
  *             self.tid = 0
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self->__pyx_base.samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self->__pyx_base.samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_t_3 = ((!__pyx_t_1) != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/csamfile.pyx":1464
+      /* "pysam/calignmentfile.pyx":1532
  *         if self.tid == -1:
  *             if not self.samfile.nreferences:
  *                 raise StopIteration             # <<<<<<<<<<<<<<
@@ -17150,10 +17761,10 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
  *             self.nextiter()
  */
       __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1464; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1532; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":1465
+    /* "pysam/calignmentfile.pyx":1533
  *             if not self.samfile.nreferences:
  *                 raise StopIteration
  *             self.tid = 0             # <<<<<<<<<<<<<<
@@ -17162,16 +17773,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
  */
     __pyx_v_self->tid = 0;
 
-    /* "pysam/csamfile.pyx":1466
+    /* "pysam/calignmentfile.pyx":1534
  *                 raise StopIteration
  *             self.tid = 0
  *             self.nextiter()             # <<<<<<<<<<<<<<
  * 
  *         while 1:
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_nextiter); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_nextiter); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
@@ -17179,7 +17790,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1468
+  /* "pysam/calignmentfile.pyx":1536
  *             self.nextiter()
  * 
  *         while 1:             # <<<<<<<<<<<<<<
@@ -17188,42 +17799,42 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
  */
   while (1) {
 
-    /* "pysam/csamfile.pyx":1469
+    /* "pysam/calignmentfile.pyx":1537
  * 
  *         while 1:
  *             self.rowiter.cnext()             # <<<<<<<<<<<<<<
  * 
  *             # If current iterator is not exhausted, return aligned read
  */
-    ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self->rowiter->__pyx_vtab)->cnext(__pyx_v_self->rowiter);
+    ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion *)__pyx_v_self->rowiter->__pyx_vtab)->cnext(__pyx_v_self->rowiter);
 
-    /* "pysam/csamfile.pyx":1472
+    /* "pysam/calignmentfile.pyx":1540
  * 
  *             # If current iterator is not exhausted, return aligned read
- *             if self.rowiter.retval>0:             # <<<<<<<<<<<<<<
- *                 return makeAlignedRead(self.rowiter.b)
+ *             if self.rowiter.retval > 0:             # <<<<<<<<<<<<<<
+ *                 return makeAlignedSegment(self.rowiter.b)
  * 
  */
     __pyx_t_3 = ((__pyx_v_self->rowiter->__pyx_base.retval > 0) != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/csamfile.pyx":1473
+      /* "pysam/calignmentfile.pyx":1541
  *             # If current iterator is not exhausted, return aligned read
- *             if self.rowiter.retval>0:
- *                 return makeAlignedRead(self.rowiter.b)             # <<<<<<<<<<<<<<
+ *             if self.rowiter.retval > 0:
+ *                 return makeAlignedSegment(self.rowiter.b)             # <<<<<<<<<<<<<<
  * 
  *             self.tid += 1
  */
       __Pyx_XDECREF(__pyx_r);
-      __pyx_t_4 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->rowiter->__pyx_base.b); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1473; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __pyx_f_5pysam_14calignmentfile_makeAlignedSegment(__pyx_v_self->rowiter->__pyx_base.b); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
       __pyx_r = __pyx_t_4;
       __pyx_t_4 = 0;
       goto __pyx_L0;
     }
 
-    /* "pysam/csamfile.pyx":1475
- *                 return makeAlignedRead(self.rowiter.b)
+    /* "pysam/calignmentfile.pyx":1543
+ *                 return makeAlignedSegment(self.rowiter.b)
  * 
  *             self.tid += 1             # <<<<<<<<<<<<<<
  * 
@@ -17231,34 +17842,34 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
  */
     __pyx_v_self->tid = (__pyx_v_self->tid + 1);
 
-    /* "pysam/csamfile.pyx":1478
+    /* "pysam/calignmentfile.pyx":1546
  * 
  *             # Otherwise, proceed to next reference or stop
  *             if self.tid < self.samfile.nreferences:             # <<<<<<<<<<<<<<
  *                 self.nextiter()
  *             else:
  */
-    __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self->__pyx_base.samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self->__pyx_base.samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = PyObject_RichCompare(__pyx_t_4, __pyx_t_2, Py_LT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyObject_RichCompare(__pyx_t_4, __pyx_t_2, Py_LT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     if (__pyx_t_3) {
 
-      /* "pysam/csamfile.pyx":1479
+      /* "pysam/calignmentfile.pyx":1547
  *             # Otherwise, proceed to next reference or stop
  *             if self.tid < self.samfile.nreferences:
  *                 self.nextiter()             # <<<<<<<<<<<<<<
  *             else:
  *                 raise StopIteration
  */
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_nextiter); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_nextiter); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -17266,7 +17877,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":1481
+      /* "pysam/calignmentfile.pyx":1549
  *                 self.nextiter()
  *             else:
  *                 raise StopIteration             # <<<<<<<<<<<<<<
@@ -17274,12 +17885,12 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
  * 
  */
       __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1549; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     __pyx_L8:;
   }
 
-  /* "pysam/csamfile.pyx":1456
+  /* "pysam/calignmentfile.pyx":1524
  *         return self
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -17294,7 +17905,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_4);
   __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAllRefs.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowAllRefs.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -17303,20 +17914,20 @@ static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1494
+/* "pysam/calignmentfile.pyx":1562
  *     """
  * 
- *     def __init__(self, Samfile samfile, positions, int reopen=True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, positions, int multiple_iterators=True):             # <<<<<<<<<<<<<<
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
+static int __pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
   PyObject *__pyx_v_positions = 0;
-  int __pyx_v_reopen;
+  int __pyx_v_multiple_iterators;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -17324,7 +17935,7 @@ static int __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__(PyObject *
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_positions,&__pyx_n_s_reopen,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_positions,&__pyx_n_s_multiple_iterators,0};
     PyObject* values[3] = {0,0,0};
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
@@ -17344,16 +17955,16 @@ static int __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__(PyObject *
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_positions)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1562; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
           if (value) { values[2] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1562; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -17364,24 +17975,24 @@ static int __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__(PyObject *
         default: goto __pyx_L5_argtuple_error;
       }
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
     __pyx_v_positions = values[1];
     if (values[2]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_multiple_iterators = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_multiple_iterators == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1562; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
-      __pyx_v_reopen = ((int)1);
+      __pyx_v_multiple_iterators = ((int)1);
     }
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1562; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowSelection.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowSelection.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)__pyx_v_self), __pyx_v_samfile, __pyx_v_positions, __pyx_v_reopen);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_20IteratorRowSelection___init__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *)__pyx_v_self), __pyx_v_samfile, __pyx_v_positions, __pyx_v_multiple_iterators);
 
   /* function exit code */
   goto __pyx_L0;
@@ -17392,7 +18003,7 @@ static int __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__(PyObject *
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_positions, int __pyx_v_reopen) {
+static int __pyx_pf_5pysam_14calignmentfile_20IteratorRowSelection___init__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, PyObject *__pyx_v_positions, int __pyx_v_multiple_iterators) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -17405,18 +18016,18 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1494);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 1562);
 
-  /* "pysam/csamfile.pyx":1496
- *     def __init__(self, Samfile samfile, positions, int reopen=True):
+  /* "pysam/calignmentfile.pyx":1564
+ *     def __init__(self, AlignmentFile samfile, positions, int multiple_iterators=True):
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)             # <<<<<<<<<<<<<<
  * 
  *         self.positions = positions
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
@@ -17424,21 +18035,21 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __py
   __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
   PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
   __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_multiple_iterators); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_multiple_iterators, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/csamfile.pyx":1498
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+  /* "pysam/calignmentfile.pyx":1566
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  * 
  *         self.positions = positions             # <<<<<<<<<<<<<<
  *         self.current_pos = 0
@@ -17450,7 +18061,7 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __py
   __Pyx_DECREF(__pyx_v_self->positions);
   __pyx_v_self->positions = __pyx_v_positions;
 
-  /* "pysam/csamfile.pyx":1499
+  /* "pysam/calignmentfile.pyx":1567
  * 
  *         self.positions = positions
  *         self.current_pos = 0             # <<<<<<<<<<<<<<
@@ -17459,7 +18070,7 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __py
  */
   __pyx_v_self->current_pos = 0;
 
-  /* "pysam/csamfile.pyx":1501
+  /* "pysam/calignmentfile.pyx":1569
  *         self.current_pos = 0
  * 
  *         self.fp = self.htsfile.fp.bgzf             # <<<<<<<<<<<<<<
@@ -17469,12 +18080,12 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __py
   __pyx_t_5 = __pyx_v_self->__pyx_base.htsfile->fp.bgzf;
   __pyx_v_self->fp = __pyx_t_5;
 
-  /* "pysam/csamfile.pyx":1494
+  /* "pysam/calignmentfile.pyx":1562
  *     """
  * 
- *     def __init__(self, Samfile samfile, positions, int reopen=True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, positions, int multiple_iterators=True):             # <<<<<<<<<<<<<<
  * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
+ *         IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
  */
 
   /* function exit code */
@@ -17485,7 +18096,7 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __py
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowSelection.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowSelection.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -17493,7 +18104,7 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1503
+/* "pysam/calignmentfile.pyx":1571
  *         self.fp = self.htsfile.fp.bgzf
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -17502,26 +18113,26 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __py
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_3__iter__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_3__iter__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_3__iter__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorRowSelection_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_20IteratorRowSelection_2__iter__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_20IteratorRowSelection_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1503);
+  __Pyx_TraceCall("__iter__", __pyx_f[0], 1571);
 
-  /* "pysam/csamfile.pyx":1504
+  /* "pysam/calignmentfile.pyx":1572
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
@@ -17533,7 +18144,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_2__iter__(stru
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1503
+  /* "pysam/calignmentfile.pyx":1571
  *         self.fp = self.htsfile.fp.bgzf
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -17549,7 +18160,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_2__iter__(stru
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1506
+/* "pysam/calignmentfile.pyx":1574
  *         return self
  * 
  *     cdef bam1_t * getCurrent(self):             # <<<<<<<<<<<<<<
@@ -17557,14 +18168,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_2__iter__(stru
  * 
  */
 
-static bam1_t *__pyx_f_5pysam_8csamfile_20IteratorRowSelection_getCurrent(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self) {
+static bam1_t *__pyx_f_5pysam_14calignmentfile_20IteratorRowSelection_getCurrent(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *__pyx_v_self) {
   bam1_t *__pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1506);
+  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1574);
 
-  /* "pysam/csamfile.pyx":1507
+  /* "pysam/calignmentfile.pyx":1575
  * 
  *     cdef bam1_t * getCurrent(self):
  *         return self.b             # <<<<<<<<<<<<<<
@@ -17574,7 +18185,7 @@ static bam1_t *__pyx_f_5pysam_8csamfile_20IteratorRowSelection_getCurrent(struct
   __pyx_r = __pyx_v_self->__pyx_base.b;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1506
+  /* "pysam/calignmentfile.pyx":1574
  *         return self
  * 
  *     cdef bam1_t * getCurrent(self):             # <<<<<<<<<<<<<<
@@ -17589,7 +18200,7 @@ static bam1_t *__pyx_f_5pysam_8csamfile_20IteratorRowSelection_getCurrent(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1509
+/* "pysam/calignmentfile.pyx":1577
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -17597,7 +18208,7 @@ static bam1_t *__pyx_f_5pysam_8csamfile_20IteratorRowSelection_getCurrent(struct
  * 
  */
 
-static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self) {
+static int __pyx_f_5pysam_14calignmentfile_20IteratorRowSelection_cnext(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -17609,9 +18220,9 @@ static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_ob
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1509);
+  __Pyx_TraceCall("cnext", __pyx_f[0], 1577);
 
-  /* "pysam/csamfile.pyx":1513
+  /* "pysam/calignmentfile.pyx":1581
  * 
  *         # end iteration if out of positions
  *         if self.current_pos >= len(self.positions): return -1             # <<<<<<<<<<<<<<
@@ -17620,7 +18231,7 @@ static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_ob
  */
   __pyx_t_1 = __pyx_v_self->positions;
   __Pyx_INCREF(__pyx_t_1);
-  __pyx_t_2 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_t_3 = ((__pyx_v_self->current_pos >= __pyx_t_2) != 0);
   if (__pyx_t_3) {
@@ -17628,19 +18239,19 @@ static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_ob
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":1516
+  /* "pysam/calignmentfile.pyx":1584
  * 
  *         bgzf_seek(self.fp,
  *                   self.positions[self.current_pos],             # <<<<<<<<<<<<<<
  *                   0)
  *         self.current_pos += 1
  */
-  __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_self->positions, __pyx_v_self->current_pos, int, 1, __Pyx_PyInt_From_int, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1516; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_self->positions, __pyx_v_self->current_pos, int, 1, __Pyx_PyInt_From_int, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1584; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_4 = __Pyx_PyInt_As_int64_t(__pyx_t_1); if (unlikely((__pyx_t_4 == (int64_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_As_int64_t(__pyx_t_1); if (unlikely((__pyx_t_4 == (int64_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1584; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1515
+  /* "pysam/calignmentfile.pyx":1583
  *         if self.current_pos >= len(self.positions): return -1
  * 
  *         bgzf_seek(self.fp,             # <<<<<<<<<<<<<<
@@ -17649,7 +18260,7 @@ static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_ob
  */
   bgzf_seek(__pyx_v_self->fp, __pyx_t_4, 0);
 
-  /* "pysam/csamfile.pyx":1518
+  /* "pysam/calignmentfile.pyx":1586
  *                   self.positions[self.current_pos],
  *                   0)
  *         self.current_pos += 1             # <<<<<<<<<<<<<<
@@ -17658,7 +18269,7 @@ static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_ob
  */
   __pyx_v_self->current_pos = (__pyx_v_self->current_pos + 1);
 
-  /* "pysam/csamfile.pyx":1519
+  /* "pysam/calignmentfile.pyx":1587
  *                   0)
  *         self.current_pos += 1
  *         return sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
@@ -17668,7 +18279,7 @@ static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_ob
   __pyx_r = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1509
+  /* "pysam/calignmentfile.pyx":1577
  *         return self.b
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -17679,7 +18290,7 @@ static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_ob
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_WriteUnraisable("pysam.csamfile.IteratorRowSelection.cnext", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __Pyx_WriteUnraisable("pysam.calignmentfile.IteratorRowSelection.cnext", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -17687,7 +18298,7 @@ static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1523
+/* "pysam/calignmentfile.pyx":1591
  *                          self.b)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -17696,23 +18307,23 @@ static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_ob
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_5__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_20IteratorRowSelection_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_5__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_20IteratorRowSelection_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_20IteratorRowSelection_4__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_14calignmentfile_20IteratorRowSelection_4__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_5__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_5__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorRowSelection_4__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_20IteratorRowSelection_4__next__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_20IteratorRowSelection_4__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *__pyx_v_self) {
   int __pyx_v_ret;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -17723,55 +18334,79 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_4__next__(stru
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1523);
+  __Pyx_TraceCall("__next__", __pyx_f[0], 1591);
 
-  /* "pysam/csamfile.pyx":1529
+  /* "pysam/calignmentfile.pyx":1596
+ *         pyrex uses this non-standard name instead of next()
  *         """
- * 
  *         cdef int ret = self.cnext()             # <<<<<<<<<<<<<<
  *         if (ret >= 0):
- *             return makeAlignedRead(self.b)
+ *             return makeAlignedSegment(self.b)
  */
-  __pyx_v_ret = ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection *)__pyx_v_self->__pyx_vtab)->cnext(__pyx_v_self);
+  __pyx_v_ret = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowSelection *)__pyx_v_self->__pyx_vtab)->cnext(__pyx_v_self);
 
-  /* "pysam/csamfile.pyx":1530
- * 
+  /* "pysam/calignmentfile.pyx":1597
+ *         """
  *         cdef int ret = self.cnext()
  *         if (ret >= 0):             # <<<<<<<<<<<<<<
- *             return makeAlignedRead(self.b)
- *         else:
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):
  */
   __pyx_t_1 = ((__pyx_v_ret >= 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1531
+    /* "pysam/calignmentfile.pyx":1598
  *         cdef int ret = self.cnext()
  *         if (ret >= 0):
- *             return makeAlignedRead(self.b)             # <<<<<<<<<<<<<<
- *         else:
- *             raise StopIteration
+ *             return makeAlignedSegment(self.b)             # <<<<<<<<<<<<<<
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __pyx_f_5pysam_14calignmentfile_makeAlignedSegment(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
+
+  /* "pysam/calignmentfile.pyx":1599
+ *         if (ret >= 0):
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):             # <<<<<<<<<<<<<<
+ *             raise IOError('truncated file')
+ *         else:
+ */
+  __pyx_t_1 = ((__pyx_v_ret == -2) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/calignmentfile.pyx":1600
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise StopIteration
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__61, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":1533
- *             return makeAlignedRead(self.b)
+    /* "pysam/calignmentfile.pyx":1602
+ *             raise IOError('truncated file')
  *         else:
  *             raise StopIteration             # <<<<<<<<<<<<<<
  * 
  * 
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1602; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1523
+  /* "pysam/calignmentfile.pyx":1591
  *                          self.b)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -17782,7 +18417,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_4__next__(stru
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowSelection.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorRowSelection.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -17791,32 +18426,32 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_4__next__(stru
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1536
+/* "pysam/calignmentfile.pyx":1605
  * 
  * 
- * cdef int __advance_all(void *data, bam1_t *b):             # <<<<<<<<<<<<<<
+ * cdef int __advance_nofilter(void *data, bam1_t *b):             # <<<<<<<<<<<<<<
  *     '''advance without any read filtering.
  *     '''
  */
 
-static int __pyx_f_5pysam_8csamfile___advance_all(void *__pyx_v_data, bam1_t *__pyx_v_b) {
-  __pyx_t_5pysam_8csamfile___iterdata *__pyx_v_d;
+static int __pyx_f_5pysam_14calignmentfile___advance_nofilter(void *__pyx_v_data, bam1_t *__pyx_v_b) {
+  __pyx_t_5pysam_14calignmentfile___iterdata *__pyx_v_d;
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__advance_all", 0);
-  __Pyx_TraceCall("__advance_all", __pyx_f[0], 1536);
+  __Pyx_RefNannySetupContext("__advance_nofilter", 0);
+  __Pyx_TraceCall("__advance_nofilter", __pyx_f[0], 1605);
 
-  /* "pysam/csamfile.pyx":1540
+  /* "pysam/calignmentfile.pyx":1609
  *     '''
  *     cdef __iterdata * d
  *     d = <__iterdata*>data             # <<<<<<<<<<<<<<
  *     return sam_itr_next(d.htsfile, d.iter, b)
  * 
  */
-  __pyx_v_d = ((__pyx_t_5pysam_8csamfile___iterdata *)__pyx_v_data);
+  __pyx_v_d = ((__pyx_t_5pysam_14calignmentfile___iterdata *)__pyx_v_data);
 
-  /* "pysam/csamfile.pyx":1541
+  /* "pysam/calignmentfile.pyx":1610
  *     cdef __iterdata * d
  *     d = <__iterdata*>data
  *     return sam_itr_next(d.htsfile, d.iter, b)             # <<<<<<<<<<<<<<
@@ -17826,10 +18461,10 @@ static int __pyx_f_5pysam_8csamfile___advance_all(void *__pyx_v_data, bam1_t *__
   __pyx_r = sam_itr_next(__pyx_v_d->htsfile, __pyx_v_d->iter, __pyx_v_b);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1536
+  /* "pysam/calignmentfile.pyx":1605
  * 
  * 
- * cdef int __advance_all(void *data, bam1_t *b):             # <<<<<<<<<<<<<<
+ * cdef int __advance_nofilter(void *data, bam1_t *b):             # <<<<<<<<<<<<<<
  *     '''advance without any read filtering.
  *     '''
  */
@@ -17841,7 +18476,127 @@ static int __pyx_f_5pysam_8csamfile___advance_all(void *__pyx_v_data, bam1_t *__
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1544
+/* "pysam/calignmentfile.pyx":1613
+ * 
+ * 
+ * cdef int __advance_all(void *data, bam1_t *b):             # <<<<<<<<<<<<<<
+ *     '''only use reads for pileup passing basic
+ *     filters:
+ */
+
+static int __pyx_f_5pysam_14calignmentfile___advance_all(void *__pyx_v_data, bam1_t *__pyx_v_b) {
+  __pyx_t_5pysam_14calignmentfile___iterdata *__pyx_v_d;
+  PyObject *__pyx_v_mask = 0;
+  int __pyx_v_ret;
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_t_2;
+  PyObject *__pyx_t_3 = NULL;
+  int __pyx_t_4;
+  int __pyx_t_5;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__advance_all", 0);
+  __Pyx_TraceCall("__advance_all", __pyx_f[0], 1613);
+
+  /* "pysam/calignmentfile.pyx":1621
+ * 
+ *     cdef __iterdata * d
+ *     cdef mask = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP             # <<<<<<<<<<<<<<
+ *     d = <__iterdata*>data
+ *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
+ */
+  __pyx_t_1 = __Pyx_PyInt_From_int((((BAM_FUNMAP | BAM_FSECONDARY) | BAM_FQCFAIL) | BAM_FDUP)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_v_mask = __pyx_t_1;
+  __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":1622
+ *     cdef __iterdata * d
+ *     cdef mask = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP
+ *     d = <__iterdata*>data             # <<<<<<<<<<<<<<
+ *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
+ *     while ret >= 0 and b.core.flag & mask:
+ */
+  __pyx_v_d = ((__pyx_t_5pysam_14calignmentfile___iterdata *)__pyx_v_data);
+
+  /* "pysam/calignmentfile.pyx":1623
+ *     cdef mask = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP
+ *     d = <__iterdata*>data
+ *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)             # <<<<<<<<<<<<<<
+ *     while ret >= 0 and b.core.flag & mask:
+ *         ret = sam_itr_next(d.htsfile, d.iter, b)
+ */
+  __pyx_v_ret = sam_itr_next(__pyx_v_d->htsfile, __pyx_v_d->iter, __pyx_v_b);
+
+  /* "pysam/calignmentfile.pyx":1624
+ *     d = <__iterdata*>data
+ *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
+ *     while ret >= 0 and b.core.flag & mask:             # <<<<<<<<<<<<<<
+ *         ret = sam_itr_next(d.htsfile, d.iter, b)
+ * 
+ */
+  while (1) {
+    __pyx_t_2 = (__pyx_v_ret >= 0);
+    if (__pyx_t_2) {
+      __pyx_t_1 = __Pyx_PyInt_From_uint16_t(__pyx_v_b->core.flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_1);
+      __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_v_mask); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __pyx_t_5 = __pyx_t_4;
+    } else {
+      __pyx_t_5 = __pyx_t_2;
+    }
+    if (!__pyx_t_5) break;
+
+    /* "pysam/calignmentfile.pyx":1625
+ *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
+ *     while ret >= 0 and b.core.flag & mask:
+ *         ret = sam_itr_next(d.htsfile, d.iter, b)             # <<<<<<<<<<<<<<
+ * 
+ *     return ret
+ */
+    __pyx_v_ret = sam_itr_next(__pyx_v_d->htsfile, __pyx_v_d->iter, __pyx_v_b);
+  }
+
+  /* "pysam/calignmentfile.pyx":1627
+ *         ret = sam_itr_next(d.htsfile, d.iter, b)
+ * 
+ *     return ret             # <<<<<<<<<<<<<<
+ * 
+ * 
+ */
+  __pyx_r = __pyx_v_ret;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":1613
+ * 
+ * 
+ * cdef int __advance_all(void *data, bam1_t *b):             # <<<<<<<<<<<<<<
+ *     '''only use reads for pileup passing basic
+ *     filters:
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_WriteUnraisable("pysam.calignmentfile.__advance_all", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __pyx_r = 0;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_mask);
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":1630
  * 
  * 
  * cdef int __advance_snpcalls(void * data, bam1_t * b):             # <<<<<<<<<<<<<<
@@ -17849,8 +18604,8 @@ static int __pyx_f_5pysam_8csamfile___advance_all(void *__pyx_v_data, bam1_t *__
  *     the samtools pileup.
  */
 
-static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_t *__pyx_v_b) {
-  __pyx_t_5pysam_8csamfile___iterdata *__pyx_v_d;
+static int __pyx_f_5pysam_14calignmentfile___advance_snpcalls(void *__pyx_v_data, bam1_t *__pyx_v_b) {
+  __pyx_t_5pysam_14calignmentfile___iterdata *__pyx_v_d;
   int __pyx_v_ret;
   int __pyx_v_skip;
   int __pyx_v_q;
@@ -17871,18 +18626,18 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__advance_snpcalls", 0);
-  __Pyx_TraceCall("__advance_snpcalls", __pyx_f[0], 1544);
+  __Pyx_TraceCall("__advance_snpcalls", __pyx_f[0], 1630);
 
-  /* "pysam/csamfile.pyx":1556
+  /* "pysam/calignmentfile.pyx":1642
  *     # 2. bam_cap_mapQ
  *     cdef __iterdata * d
  *     d = <__iterdata*>data             # <<<<<<<<<<<<<<
  * 
  *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
  */
-  __pyx_v_d = ((__pyx_t_5pysam_8csamfile___iterdata *)__pyx_v_data);
+  __pyx_v_d = ((__pyx_t_5pysam_14calignmentfile___iterdata *)__pyx_v_data);
 
-  /* "pysam/csamfile.pyx":1558
+  /* "pysam/calignmentfile.pyx":1644
  *     d = <__iterdata*>data
  * 
  *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)             # <<<<<<<<<<<<<<
@@ -17891,7 +18646,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
  */
   __pyx_v_ret = sam_itr_next(__pyx_v_d->htsfile, __pyx_v_d->iter, __pyx_v_b);
 
-  /* "pysam/csamfile.pyx":1559
+  /* "pysam/calignmentfile.pyx":1645
  * 
  *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
  *     cdef int skip = 0             # <<<<<<<<<<<<<<
@@ -17900,7 +18655,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
  */
   __pyx_v_skip = 0;
 
-  /* "pysam/csamfile.pyx":1561
+  /* "pysam/calignmentfile.pyx":1647
  *     cdef int skip = 0
  *     cdef int q
  *     cdef int is_cns = 1             # <<<<<<<<<<<<<<
@@ -17909,7 +18664,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
  */
   __pyx_v_is_cns = 1;
 
-  /* "pysam/csamfile.pyx":1562
+  /* "pysam/calignmentfile.pyx":1648
  *     cdef int q
  *     cdef int is_cns = 1
  *     cdef int is_nobaq = 0             # <<<<<<<<<<<<<<
@@ -17918,7 +18673,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
  */
   __pyx_v_is_nobaq = 0;
 
-  /* "pysam/csamfile.pyx":1563
+  /* "pysam/calignmentfile.pyx":1649
  *     cdef int is_cns = 1
  *     cdef int is_nobaq = 0
  *     cdef int capQ_thres = 0             # <<<<<<<<<<<<<<
@@ -17927,7 +18682,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
  */
   __pyx_v_capQ_thres = 0;
 
-  /* "pysam/csamfile.pyx":1566
+  /* "pysam/calignmentfile.pyx":1652
  * 
  *     # reload sequence
  *     if d.fastafile != NULL and b.core.tid != d.tid:             # <<<<<<<<<<<<<<
@@ -17943,7 +18698,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
   }
   if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":1567
+    /* "pysam/calignmentfile.pyx":1653
  *     # reload sequence
  *     if d.fastafile != NULL and b.core.tid != d.tid:
  *         if d.seq != NULL:             # <<<<<<<<<<<<<<
@@ -17953,7 +18708,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     __pyx_t_3 = ((__pyx_v_d->seq != NULL) != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/csamfile.pyx":1568
+      /* "pysam/calignmentfile.pyx":1654
  *     if d.fastafile != NULL and b.core.tid != d.tid:
  *         if d.seq != NULL:
  *             free(d.seq)             # <<<<<<<<<<<<<<
@@ -17965,7 +18720,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     }
     __pyx_L4:;
 
-    /* "pysam/csamfile.pyx":1569
+    /* "pysam/calignmentfile.pyx":1655
  *         if d.seq != NULL:
  *             free(d.seq)
  *         d.tid = b.core.tid             # <<<<<<<<<<<<<<
@@ -17975,16 +18730,16 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     __pyx_t_4 = __pyx_v_b->core.tid;
     __pyx_v_d->tid = __pyx_t_4;
 
-    /* "pysam/csamfile.pyx":1570
+    /* "pysam/calignmentfile.pyx":1656
  *             free(d.seq)
  *         d.tid = b.core.tid
  *         d.seq = faidx_fetch_seq(             # <<<<<<<<<<<<<<
  *             d.fastafile,
  *             d.header.target_name[d.tid],
  */
-    __pyx_v_d->seq = faidx_fetch_seq(__pyx_v_d->fastafile, (__pyx_v_d->header->target_name[__pyx_v_d->tid]), 0, __pyx_v_5pysam_8csamfile_max_pos, (&__pyx_v_d->seq_len));
+    __pyx_v_d->seq = faidx_fetch_seq(__pyx_v_d->fastafile, (__pyx_v_d->header->target_name[__pyx_v_d->tid]), 0, __pyx_v_5pysam_14calignmentfile_max_pos, (&__pyx_v_d->seq_len));
 
-    /* "pysam/csamfile.pyx":1576
+    /* "pysam/calignmentfile.pyx":1662
  *             &d.seq_len)
  * 
  *         if d.seq == NULL:             # <<<<<<<<<<<<<<
@@ -17994,34 +18749,34 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     __pyx_t_3 = ((__pyx_v_d->seq == NULL) != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/csamfile.pyx":1579
+      /* "pysam/calignmentfile.pyx":1665
  *             raise ValueError(
  *                 "reference sequence for '%s' (tid=%i) not found" % \
  *                 (d.header.target_name[d.tid],             # <<<<<<<<<<<<<<
  *                  d.tid))
  * 
  */
-      __pyx_t_5 = __Pyx_PyBytes_FromString((__pyx_v_d->header->target_name[__pyx_v_d->tid])); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyBytes_FromString((__pyx_v_d->header->target_name[__pyx_v_d->tid])); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1665; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
 
-      /* "pysam/csamfile.pyx":1580
+      /* "pysam/calignmentfile.pyx":1666
  *                 "reference sequence for '%s' (tid=%i) not found" % \
  *                 (d.header.target_name[d.tid],
  *                  d.tid))             # <<<<<<<<<<<<<<
  * 
- * 
+ *     while ret >= 0:
  */
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_d->tid); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_d->tid); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
 
-      /* "pysam/csamfile.pyx":1579
+      /* "pysam/calignmentfile.pyx":1665
  *             raise ValueError(
  *                 "reference sequence for '%s' (tid=%i) not found" % \
  *                 (d.header.target_name[d.tid],             # <<<<<<<<<<<<<<
  *                  d.tid))
  * 
  */
-      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1665; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_5);
       __Pyx_GIVEREF(__pyx_t_5);
@@ -18030,42 +18785,42 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
       __pyx_t_5 = 0;
       __pyx_t_6 = 0;
 
-      /* "pysam/csamfile.pyx":1578
+      /* "pysam/calignmentfile.pyx":1664
  *         if d.seq == NULL:
  *             raise ValueError(
  *                 "reference sequence for '%s' (tid=%i) not found" % \             # <<<<<<<<<<<<<<
  *                 (d.header.target_name[d.tid],
  *                  d.tid))
  */
-      __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_reference_sequence_for_s_tid_i_n, __pyx_t_7); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_reference_sequence_for_s_tid_i_n, __pyx_t_7); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
 
-      /* "pysam/csamfile.pyx":1577
+      /* "pysam/calignmentfile.pyx":1663
  * 
  *         if d.seq == NULL:
  *             raise ValueError(             # <<<<<<<<<<<<<<
  *                 "reference sequence for '%s' (tid=%i) not found" % \
  *                 (d.header.target_name[d.tid],
  */
-      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1577; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_6);
       __Pyx_GIVEREF(__pyx_t_6);
       __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_7, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1577; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_7, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __Pyx_Raise(__pyx_t_6, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1577; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1583
- * 
+  /* "pysam/calignmentfile.pyx":1668
+ *                  d.tid))
  * 
  *     while ret >= 0:             # <<<<<<<<<<<<<<
  *         skip = 0
@@ -18075,7 +18830,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     __pyx_t_3 = ((__pyx_v_ret >= 0) != 0);
     if (!__pyx_t_3) break;
 
-    /* "pysam/csamfile.pyx":1584
+    /* "pysam/calignmentfile.pyx":1669
  * 
  *     while ret >= 0:
  *         skip = 0             # <<<<<<<<<<<<<<
@@ -18084,7 +18839,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
  */
     __pyx_v_skip = 0;
 
-    /* "pysam/csamfile.pyx":1587
+    /* "pysam/calignmentfile.pyx":1672
  * 
  *         # realign read - changes base qualities
  *         if d.seq != NULL and is_cns and not is_nobaq:             # <<<<<<<<<<<<<<
@@ -18105,7 +18860,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     }
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1588
+      /* "pysam/calignmentfile.pyx":1673
  *         # realign read - changes base qualities
  *         if d.seq != NULL and is_cns and not is_nobaq:
  *             bam_prob_realn(b, d.seq)             # <<<<<<<<<<<<<<
@@ -18117,7 +18872,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     }
     __pyx_L8:;
 
-    /* "pysam/csamfile.pyx":1590
+    /* "pysam/calignmentfile.pyx":1675
  *             bam_prob_realn(b, d.seq)
  * 
  *         if d.seq != NULL and capQ_thres > 10:             # <<<<<<<<<<<<<<
@@ -18133,7 +18888,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     }
     if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":1591
+      /* "pysam/calignmentfile.pyx":1676
  * 
  *         if d.seq != NULL and capQ_thres > 10:
  *             q = bam_cap_mapQ(b, d.seq, capQ_thres)             # <<<<<<<<<<<<<<
@@ -18142,7 +18897,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
  */
       __pyx_v_q = bam_cap_mapQ(__pyx_v_b, __pyx_v_d->seq, __pyx_v_capQ_thres);
 
-      /* "pysam/csamfile.pyx":1592
+      /* "pysam/calignmentfile.pyx":1677
  *         if d.seq != NULL and capQ_thres > 10:
  *             q = bam_cap_mapQ(b, d.seq, capQ_thres)
  *             if q < 0:             # <<<<<<<<<<<<<<
@@ -18152,7 +18907,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
       __pyx_t_2 = ((__pyx_v_q < 0) != 0);
       if (__pyx_t_2) {
 
-        /* "pysam/csamfile.pyx":1593
+        /* "pysam/calignmentfile.pyx":1678
  *             q = bam_cap_mapQ(b, d.seq, capQ_thres)
  *             if q < 0:
  *                 skip = 1             # <<<<<<<<<<<<<<
@@ -18163,7 +18918,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
         goto __pyx_L10;
       }
 
-      /* "pysam/csamfile.pyx":1594
+      /* "pysam/calignmentfile.pyx":1679
  *             if q < 0:
  *                 skip = 1
  *             elif b.core.qual > q:             # <<<<<<<<<<<<<<
@@ -18173,7 +18928,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
       __pyx_t_2 = ((__pyx_v_b->core.qual > __pyx_v_q) != 0);
       if (__pyx_t_2) {
 
-        /* "pysam/csamfile.pyx":1595
+        /* "pysam/calignmentfile.pyx":1680
  *                 skip = 1
  *             elif b.core.qual > q:
  *                 b.core.qual = q             # <<<<<<<<<<<<<<
@@ -18188,7 +18943,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     }
     __pyx_L9:;
 
-    /* "pysam/csamfile.pyx":1596
+    /* "pysam/calignmentfile.pyx":1681
  *             elif b.core.qual > q:
  *                 b.core.qual = q
  *         if b.core.flag & BAM_FUNMAP:             # <<<<<<<<<<<<<<
@@ -18198,7 +18953,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     __pyx_t_2 = ((__pyx_v_b->core.flag & BAM_FUNMAP) != 0);
     if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":1597
+      /* "pysam/calignmentfile.pyx":1682
  *                 b.core.qual = q
  *         if b.core.flag & BAM_FUNMAP:
  *             skip = 1             # <<<<<<<<<<<<<<
@@ -18209,7 +18964,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
       goto __pyx_L11;
     }
 
-    /* "pysam/csamfile.pyx":1598
+    /* "pysam/calignmentfile.pyx":1683
  *         if b.core.flag & BAM_FUNMAP:
  *             skip = 1
  *         elif b.core.flag & 1 and not b.core.flag & 2:             # <<<<<<<<<<<<<<
@@ -18224,31 +18979,39 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
     }
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1599
+      /* "pysam/calignmentfile.pyx":1684
  *             skip = 1
  *         elif b.core.flag & 1 and not b.core.flag & 2:
  *             skip = 1             # <<<<<<<<<<<<<<
  * 
- *         if not skip: break
+ *         if not skip:
  */
       __pyx_v_skip = 1;
       goto __pyx_L11;
     }
     __pyx_L11:;
 
-    /* "pysam/csamfile.pyx":1601
+    /* "pysam/calignmentfile.pyx":1686
  *             skip = 1
  * 
- *         if not skip: break             # <<<<<<<<<<<<<<
+ *         if not skip:             # <<<<<<<<<<<<<<
+ *             break
  *         # additional filters
- * 
  */
     __pyx_t_1 = ((!(__pyx_v_skip != 0)) != 0);
     if (__pyx_t_1) {
+
+      /* "pysam/calignmentfile.pyx":1687
+ * 
+ *         if not skip:
+ *             break             # <<<<<<<<<<<<<<
+ *         # additional filters
+ * 
+ */
       goto __pyx_L7_break;
     }
 
-    /* "pysam/csamfile.pyx":1604
+    /* "pysam/calignmentfile.pyx":1690
  *         # additional filters
  * 
  *         ret = sam_itr_next(d.htsfile, d.iter, b)             # <<<<<<<<<<<<<<
@@ -18259,7 +19022,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
   }
   __pyx_L7_break:;
 
-  /* "pysam/csamfile.pyx":1606
+  /* "pysam/calignmentfile.pyx":1692
  *         ret = sam_itr_next(d.htsfile, d.iter, b)
  * 
  *     return ret             # <<<<<<<<<<<<<<
@@ -18269,7 +19032,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
   __pyx_r = __pyx_v_ret;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1544
+  /* "pysam/calignmentfile.pyx":1630
  * 
  * 
  * cdef int __advance_snpcalls(void * data, bam1_t * b):             # <<<<<<<<<<<<<<
@@ -18282,7 +19045,7 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
   __Pyx_XDECREF(__pyx_t_5);
   __Pyx_XDECREF(__pyx_t_6);
   __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_WriteUnraisable("pysam.csamfile.__advance_snpcalls", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __Pyx_WriteUnraisable("pysam.calignmentfile.__advance_snpcalls", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -18290,18 +19053,18 @@ static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1658
+/* "pysam/calignmentfile.pyx":1745
  *     '''
  * 
- *     def __cinit__( self, Samfile samfile, **kwargs ):             # <<<<<<<<<<<<<<
+ *     def __cinit__( self, AlignmentFile samfile, **kwargs ):             # <<<<<<<<<<<<<<
  *         self.samfile = samfile
  *         # TODO
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_14IteratorColumn_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_14IteratorColumn_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
+static int __pyx_pw_5pysam_14calignmentfile_14IteratorColumn_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14IteratorColumn_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
   PyObject *__pyx_v_kwargs = 0;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
@@ -18329,26 +19092,26 @@ static int __pyx_pw_5pysam_8csamfile_14IteratorColumn_1__cinit__(PyObject *__pyx
         else goto __pyx_L5_argtuple_error;
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1658; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1745; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 1) {
       goto __pyx_L5_argtuple_error;
     } else {
       values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1658; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1745; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumn.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), __pyx_v_samfile, __pyx_v_kwargs);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14IteratorColumn___cinit__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self), __pyx_v_samfile, __pyx_v_kwargs);
 
   /* function exit code */
   goto __pyx_L0;
@@ -18360,7 +19123,7 @@ static int __pyx_pw_5pysam_8csamfile_14IteratorColumn_1__cinit__(PyObject *__pyx
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_kwargs) {
+static int __pyx_pf_5pysam_14calignmentfile_14IteratorColumn___cinit__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, PyObject *__pyx_v_kwargs) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -18370,11 +19133,11 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__cinit__", 0);
-  __Pyx_TraceCall("__cinit__", __pyx_f[0], 1658);
+  __Pyx_TraceCall("__cinit__", __pyx_f[0], 1745);
 
-  /* "pysam/csamfile.pyx":1659
+  /* "pysam/calignmentfile.pyx":1746
  * 
- *     def __cinit__( self, Samfile samfile, **kwargs ):
+ *     def __cinit__( self, AlignmentFile samfile, **kwargs ):
  *         self.samfile = samfile             # <<<<<<<<<<<<<<
  *         # TODO
  *         # self.mask = kwargs.get("mask", BAM_DEF_MASK )
@@ -18385,30 +19148,30 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
   __Pyx_DECREF(((PyObject *)__pyx_v_self->samfile));
   __pyx_v_self->samfile = __pyx_v_samfile;
 
-  /* "pysam/csamfile.pyx":1662
+  /* "pysam/calignmentfile.pyx":1749
  *         # TODO
  *         # self.mask = kwargs.get("mask", BAM_DEF_MASK )
  *         self.fastafile = kwargs.get("fastafile", None)             # <<<<<<<<<<<<<<
  *         self.stepper = kwargs.get("stepper", None)
  *         self.max_depth = kwargs.get("max_depth", 8000)
  */
-  __pyx_t_1 = __Pyx_PyDict_GetItemDefault(__pyx_v_kwargs, __pyx_n_s_fastafile, Py_None); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1662; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyDict_GetItemDefault(__pyx_v_kwargs, __pyx_n_s_fastafile, Py_None); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1749; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_6cfaidx_Fastafile))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1662; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_6cfaidx_Fastafile))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1749; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GIVEREF(__pyx_t_1);
   __Pyx_GOTREF(__pyx_v_self->fastafile);
   __Pyx_DECREF(((PyObject *)__pyx_v_self->fastafile));
   __pyx_v_self->fastafile = ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1663
+  /* "pysam/calignmentfile.pyx":1750
  *         # self.mask = kwargs.get("mask", BAM_DEF_MASK )
  *         self.fastafile = kwargs.get("fastafile", None)
  *         self.stepper = kwargs.get("stepper", None)             # <<<<<<<<<<<<<<
  *         self.max_depth = kwargs.get("max_depth", 8000)
  *         self.iterdata.seq = NULL
  */
-  __pyx_t_1 = __Pyx_PyDict_GetItemDefault(__pyx_v_kwargs, __pyx_n_s_stepper, Py_None); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyDict_GetItemDefault(__pyx_v_kwargs, __pyx_n_s_stepper, Py_None); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
   __Pyx_GOTREF(__pyx_v_self->stepper);
@@ -18416,20 +19179,20 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
   __pyx_v_self->stepper = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1664
+  /* "pysam/calignmentfile.pyx":1751
  *         self.fastafile = kwargs.get("fastafile", None)
  *         self.stepper = kwargs.get("stepper", None)
  *         self.max_depth = kwargs.get("max_depth", 8000)             # <<<<<<<<<<<<<<
  *         self.iterdata.seq = NULL
  *         self.tid = 0
  */
-  __pyx_t_1 = __Pyx_PyDict_GetItemDefault(__pyx_v_kwargs, __pyx_n_s_max_depth, __pyx_int_8000); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyDict_GetItemDefault(__pyx_v_kwargs, __pyx_n_s_max_depth, __pyx_int_8000); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_2 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_2 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_self->max_depth = __pyx_t_2;
 
-  /* "pysam/csamfile.pyx":1665
+  /* "pysam/calignmentfile.pyx":1752
  *         self.stepper = kwargs.get("stepper", None)
  *         self.max_depth = kwargs.get("max_depth", 8000)
  *         self.iterdata.seq = NULL             # <<<<<<<<<<<<<<
@@ -18438,7 +19201,7 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
  */
   __pyx_v_self->iterdata.seq = NULL;
 
-  /* "pysam/csamfile.pyx":1666
+  /* "pysam/calignmentfile.pyx":1753
  *         self.max_depth = kwargs.get("max_depth", 8000)
  *         self.iterdata.seq = NULL
  *         self.tid = 0             # <<<<<<<<<<<<<<
@@ -18447,7 +19210,7 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
  */
   __pyx_v_self->tid = 0;
 
-  /* "pysam/csamfile.pyx":1667
+  /* "pysam/calignmentfile.pyx":1754
  *         self.iterdata.seq = NULL
  *         self.tid = 0
  *         self.pos = 0             # <<<<<<<<<<<<<<
@@ -18456,7 +19219,7 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
  */
   __pyx_v_self->pos = 0;
 
-  /* "pysam/csamfile.pyx":1668
+  /* "pysam/calignmentfile.pyx":1755
  *         self.tid = 0
  *         self.pos = 0
  *         self.n_plp = 0             # <<<<<<<<<<<<<<
@@ -18465,7 +19228,7 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
  */
   __pyx_v_self->n_plp = 0;
 
-  /* "pysam/csamfile.pyx":1669
+  /* "pysam/calignmentfile.pyx":1756
  *         self.pos = 0
  *         self.n_plp = 0
  *         self.plp = NULL             # <<<<<<<<<<<<<<
@@ -18474,7 +19237,7 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
  */
   __pyx_v_self->plp = NULL;
 
-  /* "pysam/csamfile.pyx":1670
+  /* "pysam/calignmentfile.pyx":1757
  *         self.n_plp = 0
  *         self.plp = NULL
  *         self.pileup_iter = <bam_plp_t>NULL             # <<<<<<<<<<<<<<
@@ -18483,10 +19246,10 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
  */
   __pyx_v_self->pileup_iter = ((bam_plp_t)NULL);
 
-  /* "pysam/csamfile.pyx":1658
+  /* "pysam/calignmentfile.pyx":1745
  *     '''
  * 
- *     def __cinit__( self, Samfile samfile, **kwargs ):             # <<<<<<<<<<<<<<
+ *     def __cinit__( self, AlignmentFile samfile, **kwargs ):             # <<<<<<<<<<<<<<
  *         self.samfile = samfile
  *         # TODO
  */
@@ -18496,7 +19259,7 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
   goto __pyx_L0;
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumn.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -18504,7 +19267,7 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1672
+/* "pysam/calignmentfile.pyx":1759
  *         self.pileup_iter = <bam_plp_t>NULL
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -18513,26 +19276,26 @@ static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_3__iter__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_3__iter__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14IteratorColumn_2__iter__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorColumn_2__iter__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1672);
+  __Pyx_TraceCall("__iter__", __pyx_f[0], 1759);
 
-  /* "pysam/csamfile.pyx":1673
+  /* "pysam/calignmentfile.pyx":1760
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
@@ -18544,7 +19307,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_2__iter__(struct __p
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1672
+  /* "pysam/calignmentfile.pyx":1759
  *         self.pileup_iter = <bam_plp_t>NULL
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -18560,7 +19323,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_2__iter__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1675
+/* "pysam/calignmentfile.pyx":1762
  *         return self
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -18568,14 +19331,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_2__iter__(struct __p
  *         '''
  */
 
-static int __pyx_f_5pysam_8csamfile_14IteratorColumn_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
+static int __pyx_f_5pysam_14calignmentfile_14IteratorColumn_cnext(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1675);
+  __Pyx_TraceCall("cnext", __pyx_f[0], 1762);
 
-  /* "pysam/csamfile.pyx":1678
+  /* "pysam/calignmentfile.pyx":1765
  *         '''perform next iteration.
  *         '''
  *         self.plp = bam_plp_auto( self.pileup_iter,             # <<<<<<<<<<<<<<
@@ -18584,7 +19347,7 @@ static int __pyx_f_5pysam_8csamfile_14IteratorColumn_cnext(struct __pyx_obj_5pys
  */
   __pyx_v_self->plp = bam_plp_auto(__pyx_v_self->pileup_iter, (&__pyx_v_self->tid), (&__pyx_v_self->pos), (&__pyx_v_self->n_plp));
 
-  /* "pysam/csamfile.pyx":1675
+  /* "pysam/calignmentfile.pyx":1762
  *         return self
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -18599,7 +19362,7 @@ static int __pyx_f_5pysam_8csamfile_14IteratorColumn_cnext(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1683
+/* "pysam/calignmentfile.pyx":1770
  *                                  &self.n_plp )
  * 
  *     cdef char * getSequence( self ):             # <<<<<<<<<<<<<<
@@ -18607,14 +19370,14 @@ static int __pyx_f_5pysam_8csamfile_14IteratorColumn_cnext(struct __pyx_obj_5pys
  *         '''
  */
 
-static char *__pyx_f_5pysam_8csamfile_14IteratorColumn_getSequence(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
+static char *__pyx_f_5pysam_14calignmentfile_14IteratorColumn_getSequence(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self) {
   char *__pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("getSequence", 0);
-  __Pyx_TraceCall("getSequence", __pyx_f[0], 1683);
+  __Pyx_TraceCall("getSequence", __pyx_f[0], 1770);
 
-  /* "pysam/csamfile.pyx":1686
+  /* "pysam/calignmentfile.pyx":1773
  *         '''return current reference sequence underlying the iterator.
  *         '''
  *         return self.iterdata.seq             # <<<<<<<<<<<<<<
@@ -18624,7 +19387,7 @@ static char *__pyx_f_5pysam_8csamfile_14IteratorColumn_getSequence(struct __pyx_
   __pyx_r = __pyx_v_self->iterdata.seq;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1683
+  /* "pysam/calignmentfile.pyx":1770
  *                                  &self.n_plp )
  * 
  *     cdef char * getSequence( self ):             # <<<<<<<<<<<<<<
@@ -18639,7 +19402,7 @@ static char *__pyx_f_5pysam_8csamfile_14IteratorColumn_getSequence(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1690
+/* "pysam/calignmentfile.pyx":1777
  *     property seq_len:
  *         '''current sequence length.'''
  *         def __get__(self): return self.iterdata.seq_len             # <<<<<<<<<<<<<<
@@ -18648,19 +19411,19 @@ static char *__pyx_f_5pysam_8csamfile_14IteratorColumn_getSequence(struct __pyx_
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_7seq_len_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_7seq_len_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_7seq_len_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_7seq_len_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14IteratorColumn_7seq_len___get__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorColumn_7seq_len___get__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -18669,9 +19432,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(str
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 1690);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 1777);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->iterdata.seq_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->iterdata.seq_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -18680,7 +19443,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(str
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.seq_len.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumn.seq_len.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -18689,7 +19452,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(str
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1692
+/* "pysam/calignmentfile.pyx":1779
  *         def __get__(self): return self.iterdata.seq_len
  * 
  *     def addReference(self, Fastafile fastafile):             # <<<<<<<<<<<<<<
@@ -18698,17 +19461,17 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(str
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_5addReference(PyObject *__pyx_v_self, PyObject *__pyx_v_fastafile); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_14IteratorColumn_4addReference[] = "IteratorColumn.addReference(self, Fastafile fastafile)\n\n       add reference sequences in *fastafile* to iterator.";
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_5addReference(PyObject *__pyx_v_self, PyObject *__pyx_v_fastafile) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_5addReference(PyObject *__pyx_v_self, PyObject *__pyx_v_fastafile); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14IteratorColumn_4addReference[] = "IteratorColumn.addReference(self, Fastafile fastafile)\n\n       add reference sequences in *fastafile* to iterator.";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_5addReference(PyObject *__pyx_v_self, PyObject *__pyx_v_fastafile) {
   CYTHON_UNUSED int __pyx_lineno = 0;
   CYTHON_UNUSED const char *__pyx_filename = NULL;
   CYTHON_UNUSED int __pyx_clineno = 0;
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("addReference (wrapper)", 0);
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_fastafile), __pyx_ptype_5pysam_6cfaidx_Fastafile, 1, "fastafile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_fastafile));
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_fastafile), __pyx_ptype_5pysam_6cfaidx_Fastafile, 1, "fastafile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1779; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14IteratorColumn_4addReference(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self), ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_fastafile));
 
   /* function exit code */
   goto __pyx_L0;
@@ -18719,16 +19482,16 @@ static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_5addReference(PyObje
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_fastafile) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorColumn_4addReference(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_fastafile) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   faidx_t *__pyx_t_2;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("addReference", 0);
-  __Pyx_TraceCall("addReference", __pyx_f[0], 1692);
+  __Pyx_TraceCall("addReference", __pyx_f[0], 1779);
 
-  /* "pysam/csamfile.pyx":1695
+  /* "pysam/calignmentfile.pyx":1782
  *        '''
  *        add reference sequences in *fastafile* to iterator.'''
  *        self.fastafile = fastafile             # <<<<<<<<<<<<<<
@@ -18741,7 +19504,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(struct
   __Pyx_DECREF(((PyObject *)__pyx_v_self->fastafile));
   __pyx_v_self->fastafile = __pyx_v_fastafile;
 
-  /* "pysam/csamfile.pyx":1696
+  /* "pysam/calignmentfile.pyx":1783
  *        add reference sequences in *fastafile* to iterator.'''
  *        self.fastafile = fastafile
  *        if self.iterdata.seq != NULL: free(self.iterdata.seq)             # <<<<<<<<<<<<<<
@@ -18755,7 +19518,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(struct
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1697
+  /* "pysam/calignmentfile.pyx":1784
  *        self.fastafile = fastafile
  *        if self.iterdata.seq != NULL: free(self.iterdata.seq)
  *        self.iterdata.tid = -1             # <<<<<<<<<<<<<<
@@ -18764,17 +19527,17 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(struct
  */
   __pyx_v_self->iterdata.tid = -1;
 
-  /* "pysam/csamfile.pyx":1698
+  /* "pysam/calignmentfile.pyx":1785
  *        if self.iterdata.seq != NULL: free(self.iterdata.seq)
  *        self.iterdata.tid = -1
  *        self.iterdata.fastafile = self.fastafile.fastafile             # <<<<<<<<<<<<<<
  * 
  *     def hasReference(self):
  */
-  __pyx_t_2 = __pyx_v_self->fastafile->fastafile;
+  __pyx_t_2 = __pyx_v_self->fastafile->__pyx_base.fastafile;
   __pyx_v_self->iterdata.fastafile = __pyx_t_2;
 
-  /* "pysam/csamfile.pyx":1692
+  /* "pysam/calignmentfile.pyx":1779
  *         def __get__(self): return self.iterdata.seq_len
  * 
  *     def addReference(self, Fastafile fastafile):             # <<<<<<<<<<<<<<
@@ -18790,7 +19553,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1700
+/* "pysam/calignmentfile.pyx":1787
  *        self.iterdata.fastafile = self.fastafile.fastafile
  * 
  *     def hasReference(self):             # <<<<<<<<<<<<<<
@@ -18799,27 +19562,27 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(struct
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_7hasReference(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_14IteratorColumn_6hasReference[] = "IteratorColumn.hasReference(self)\n\n        return true if iterator is associated with a reference";
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_7hasReference(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_7hasReference(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14IteratorColumn_6hasReference[] = "IteratorColumn.hasReference(self)\n\n        return true if iterator is associated with a reference";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_7hasReference(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("hasReference (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn_6hasReference(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14IteratorColumn_6hasReference(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_6hasReference(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14IteratorColumn_6hasReference(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("hasReference", 0);
-  __Pyx_TraceCall("hasReference", __pyx_f[0], 1700);
+  __Pyx_TraceCall("hasReference", __pyx_f[0], 1787);
 
-  /* "pysam/csamfile.pyx":1703
+  /* "pysam/calignmentfile.pyx":1790
  *         '''
  *         return true if iterator is associated with a reference'''
  *         return self.fastafile             # <<<<<<<<<<<<<<
@@ -18831,7 +19594,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_6hasReference(struct
   __pyx_r = ((PyObject *)__pyx_v_self->fastafile);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1700
+  /* "pysam/calignmentfile.pyx":1787
  *        self.iterdata.fastafile = self.fastafile.fastafile
  * 
  *     def hasReference(self):             # <<<<<<<<<<<<<<
@@ -18847,7 +19610,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_6hasReference(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1705
+/* "pysam/calignmentfile.pyx":1792
  *         return self.fastafile
  * 
  *     cdef setMask(self, mask):             # <<<<<<<<<<<<<<
@@ -18855,7 +19618,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_6hasReference(struct
  * 
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setMask(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_mask) {
+static PyObject *__pyx_f_5pysam_14calignmentfile_14IteratorColumn_setMask(CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_mask) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -18864,22 +19627,22 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setMask(CYTHON_UNUSED
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("setMask", 0);
-  __Pyx_TraceCall("setMask", __pyx_f[0], 1705);
+  __Pyx_TraceCall("setMask", __pyx_f[0], 1792);
 
-  /* "pysam/csamfile.pyx":1710
+  /* "pysam/calignmentfile.pyx":1797
  *         reads with bits set in *mask* will be skipped.
  *         '''
  *         raise NotImplementedError()             # <<<<<<<<<<<<<<
  *         # self.mask = mask
  *         # bam_plp_set_mask( self.pileup_iter, self.mask )
  */
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_Raise(__pyx_t_1, 0, 0, 0);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":1705
+  /* "pysam/calignmentfile.pyx":1792
  *         return self.fastafile
  * 
  *     cdef setMask(self, mask):             # <<<<<<<<<<<<<<
@@ -18890,7 +19653,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setMask(CYTHON_UNUSED
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.setMask", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumn.setMask", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
@@ -18898,7 +19661,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setMask(CYTHON_UNUSED
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1714
+/* "pysam/calignmentfile.pyx":1801
  *         # bam_plp_set_mask( self.pileup_iter, self.mask )
  * 
  *     cdef setupIteratorData( self,             # <<<<<<<<<<<<<<
@@ -18906,8 +19669,8 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setMask(CYTHON_UNUSED
  *                             int start,
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData *__pyx_optional_args) {
-  int __pyx_v_reopen = ((int)0);
+static PyObject *__pyx_f_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData *__pyx_optional_args) {
+  int __pyx_v_multiple_iterators = ((int)0);
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -18919,37 +19682,37 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
   hts_itr_t *__pyx_t_7;
   bam_hdr_t *__pyx_t_8;
   int __pyx_t_9;
-  faidx_t *__pyx_t_10;
-  int __pyx_t_11;
+  int __pyx_t_10;
+  faidx_t *__pyx_t_11;
   int __pyx_t_12;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("setupIteratorData", 0);
-  __Pyx_TraceCall("setupIteratorData", __pyx_f[0], 1714);
+  __Pyx_TraceCall("setupIteratorData", __pyx_f[0], 1801);
   if (__pyx_optional_args) {
     if (__pyx_optional_args->__pyx_n > 0) {
-      __pyx_v_reopen = __pyx_optional_args->reopen;
+      __pyx_v_multiple_iterators = __pyx_optional_args->multiple_iterators;
     }
   }
 
-  /* "pysam/csamfile.pyx":1721
+  /* "pysam/calignmentfile.pyx":1808
  *         '''setup the iterator structure'''
  * 
- *         self.iter = IteratorRowRegion(self.samfile, tid, start, end, reopen)             # <<<<<<<<<<<<<<
+ *         self.iter = IteratorRowRegion(self.samfile, tid, start, end, multiple_iterators)             # <<<<<<<<<<<<<<
  *         self.iterdata.htsfile = self.samfile.htsfile
  *         self.iterdata.iter = self.iter.iter
  */
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1808; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_start); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_start); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1808; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_end); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_end); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1808; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_multiple_iterators); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1808; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = PyTuple_New(5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyTuple_New(5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1808; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_INCREF(((PyObject *)__pyx_v_self->samfile));
   PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_v_self->samfile));
@@ -18966,18 +19729,18 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
   __pyx_t_2 = 0;
   __pyx_t_3 = 0;
   __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowRegion)), __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion)), __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1808; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   __Pyx_GIVEREF(__pyx_t_4);
   __Pyx_GOTREF(__pyx_v_self->iter);
   __Pyx_DECREF(((PyObject *)__pyx_v_self->iter));
-  __pyx_v_self->iter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_t_4);
+  __pyx_v_self->iter = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)__pyx_t_4);
   __pyx_t_4 = 0;
 
-  /* "pysam/csamfile.pyx":1722
+  /* "pysam/calignmentfile.pyx":1809
  * 
- *         self.iter = IteratorRowRegion(self.samfile, tid, start, end, reopen)
+ *         self.iter = IteratorRowRegion(self.samfile, tid, start, end, multiple_iterators)
  *         self.iterdata.htsfile = self.samfile.htsfile             # <<<<<<<<<<<<<<
  *         self.iterdata.iter = self.iter.iter
  *         self.iterdata.seq = NULL
@@ -18985,8 +19748,8 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
   __pyx_t_6 = __pyx_v_self->samfile->htsfile;
   __pyx_v_self->iterdata.htsfile = __pyx_t_6;
 
-  /* "pysam/csamfile.pyx":1723
- *         self.iter = IteratorRowRegion(self.samfile, tid, start, end, reopen)
+  /* "pysam/calignmentfile.pyx":1810
+ *         self.iter = IteratorRowRegion(self.samfile, tid, start, end, multiple_iterators)
  *         self.iterdata.htsfile = self.samfile.htsfile
  *         self.iterdata.iter = self.iter.iter             # <<<<<<<<<<<<<<
  *         self.iterdata.seq = NULL
@@ -18995,7 +19758,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
   __pyx_t_7 = __pyx_v_self->iter->iter;
   __pyx_v_self->iterdata.iter = __pyx_t_7;
 
-  /* "pysam/csamfile.pyx":1724
+  /* "pysam/calignmentfile.pyx":1811
  *         self.iterdata.htsfile = self.samfile.htsfile
  *         self.iterdata.iter = self.iter.iter
  *         self.iterdata.seq = NULL             # <<<<<<<<<<<<<<
@@ -19004,7 +19767,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
  */
   __pyx_v_self->iterdata.seq = NULL;
 
-  /* "pysam/csamfile.pyx":1725
+  /* "pysam/calignmentfile.pyx":1812
  *         self.iterdata.iter = self.iter.iter
  *         self.iterdata.seq = NULL
  *         self.iterdata.tid = -1             # <<<<<<<<<<<<<<
@@ -19013,135 +19776,164 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
  */
   __pyx_v_self->iterdata.tid = -1;
 
-  /* "pysam/csamfile.pyx":1726
+  /* "pysam/calignmentfile.pyx":1813
  *         self.iterdata.seq = NULL
  *         self.iterdata.tid = -1
  *         self.iterdata.header = self.samfile.header             # <<<<<<<<<<<<<<
  * 
- *         if self.fastafile != None:
+ *         if self.fastafile is not None:
  */
   __pyx_t_8 = __pyx_v_self->samfile->header;
   __pyx_v_self->iterdata.header = __pyx_t_8;
 
-  /* "pysam/csamfile.pyx":1728
+  /* "pysam/calignmentfile.pyx":1815
  *         self.iterdata.header = self.samfile.header
  * 
- *         if self.fastafile != None:             # <<<<<<<<<<<<<<
+ *         if self.fastafile is not None:             # <<<<<<<<<<<<<<
  *             self.iterdata.fastafile = self.fastafile.fastafile
  *         else:
  */
-  __pyx_t_4 = PyObject_RichCompare(((PyObject *)__pyx_v_self->fastafile), Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_9 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (__pyx_t_9) {
+  __pyx_t_9 = (((PyObject *)__pyx_v_self->fastafile) != Py_None);
+  __pyx_t_10 = (__pyx_t_9 != 0);
+  if (__pyx_t_10) {
 
-    /* "pysam/csamfile.pyx":1729
+    /* "pysam/calignmentfile.pyx":1816
  * 
- *         if self.fastafile != None:
+ *         if self.fastafile is not None:
  *             self.iterdata.fastafile = self.fastafile.fastafile             # <<<<<<<<<<<<<<
  *         else:
  *             self.iterdata.fastafile = NULL
  */
-    __pyx_t_10 = __pyx_v_self->fastafile->fastafile;
-    __pyx_v_self->iterdata.fastafile = __pyx_t_10;
+    __pyx_t_11 = __pyx_v_self->fastafile->__pyx_base.fastafile;
+    __pyx_v_self->iterdata.fastafile = __pyx_t_11;
     goto __pyx_L3;
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":1731
+    /* "pysam/calignmentfile.pyx":1818
  *             self.iterdata.fastafile = self.fastafile.fastafile
  *         else:
  *             self.iterdata.fastafile = NULL             # <<<<<<<<<<<<<<
  * 
- *         if self.stepper == None or self.stepper == "all":
+ *         # Free any previously allocated memory before reassigning
  */
     __pyx_v_self->iterdata.fastafile = NULL;
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1733
- *             self.iterdata.fastafile = NULL
+  /* "pysam/calignmentfile.pyx":1822
+ *         # Free any previously allocated memory before reassigning
+ *         # pileup_iter
+ *         self._free_pileup_iter()             # <<<<<<<<<<<<<<
+ * 
+ *         if self.stepper is None or self.stepper == "all":
+ */
+  __pyx_t_4 = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self->__pyx_vtab)->_free_pileup_iter(__pyx_v_self); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+
+  /* "pysam/calignmentfile.pyx":1824
+ *         self._free_pileup_iter()
  * 
- *         if self.stepper == None or self.stepper == "all":             # <<<<<<<<<<<<<<
+ *         if self.stepper is None or self.stepper == "all":             # <<<<<<<<<<<<<<
  *             self.pileup_iter = bam_plp_init(
  *                 <bam_plp_auto_f>&__advance_all,
  */
-  __pyx_t_4 = PyObject_RichCompare(__pyx_v_self->stepper, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_9 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (!__pyx_t_9) {
-    __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_v_self->stepper, __pyx_n_s_all, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_12 = __pyx_t_11;
-  } else {
+  __pyx_t_10 = (__pyx_v_self->stepper == Py_None);
+  if (!__pyx_t_10) {
+    __pyx_t_9 = (__Pyx_PyString_Equals(__pyx_v_self->stepper, __pyx_n_s_all, Py_EQ)); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_12 = __pyx_t_9;
+  } else {
+    __pyx_t_12 = __pyx_t_10;
   }
   if (__pyx_t_12) {
 
-    /* "pysam/csamfile.pyx":1734
+    /* "pysam/calignmentfile.pyx":1825
  * 
- *         if self.stepper == None or self.stepper == "all":
+ *         if self.stepper is None or self.stepper == "all":
  *             self.pileup_iter = bam_plp_init(             # <<<<<<<<<<<<<<
  *                 <bam_plp_auto_f>&__advance_all,
  *                 &self.iterdata)
  */
-    __pyx_v_self->pileup_iter = bam_plp_init(((bam_plp_auto_f)(&__pyx_f_5pysam_8csamfile___advance_all)), (&__pyx_v_self->iterdata));
+    __pyx_v_self->pileup_iter = bam_plp_init(((bam_plp_auto_f)(&__pyx_f_5pysam_14calignmentfile___advance_all)), (&__pyx_v_self->iterdata));
     goto __pyx_L4;
   }
 
-  /* "pysam/csamfile.pyx":1737
+  /* "pysam/calignmentfile.pyx":1828
  *                 <bam_plp_auto_f>&__advance_all,
  *                 &self.iterdata)
+ *         elif self.stepper == "nofilter":             # <<<<<<<<<<<<<<
+ *             self.pileup_iter = bam_plp_init(
+ *                 <bam_plp_auto_f>&__advance_nofilter,
+ */
+  __pyx_t_12 = (__Pyx_PyString_Equals(__pyx_v_self->stepper, __pyx_n_s_nofilter, Py_EQ)); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__pyx_t_12) {
+
+    /* "pysam/calignmentfile.pyx":1829
+ *                 &self.iterdata)
+ *         elif self.stepper == "nofilter":
+ *             self.pileup_iter = bam_plp_init(             # <<<<<<<<<<<<<<
+ *                 <bam_plp_auto_f>&__advance_nofilter,
+ *                 &self.iterdata)
+ */
+    __pyx_v_self->pileup_iter = bam_plp_init(((bam_plp_auto_f)(&__pyx_f_5pysam_14calignmentfile___advance_nofilter)), (&__pyx_v_self->iterdata));
+    goto __pyx_L4;
+  }
+
+  /* "pysam/calignmentfile.pyx":1832
+ *                 <bam_plp_auto_f>&__advance_nofilter,
+ *                 &self.iterdata)
  *         elif self.stepper == "samtools":             # <<<<<<<<<<<<<<
  *             self.pileup_iter = bam_plp_init(
  *                 <bam_plp_auto_f>&__advance_snpcalls,
  */
-  __pyx_t_12 = (__Pyx_PyString_Equals(__pyx_v_self->stepper, __pyx_n_s_samtools, Py_EQ)); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = (__Pyx_PyString_Equals(__pyx_v_self->stepper, __pyx_n_s_samtools, Py_EQ)); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_12) {
 
-    /* "pysam/csamfile.pyx":1738
+    /* "pysam/calignmentfile.pyx":1833
  *                 &self.iterdata)
  *         elif self.stepper == "samtools":
  *             self.pileup_iter = bam_plp_init(             # <<<<<<<<<<<<<<
  *                 <bam_plp_auto_f>&__advance_snpcalls,
  *                 &self.iterdata)
  */
-    __pyx_v_self->pileup_iter = bam_plp_init(((bam_plp_auto_f)(&__pyx_f_5pysam_8csamfile___advance_snpcalls)), (&__pyx_v_self->iterdata));
+    __pyx_v_self->pileup_iter = bam_plp_init(((bam_plp_auto_f)(&__pyx_f_5pysam_14calignmentfile___advance_snpcalls)), (&__pyx_v_self->iterdata));
     goto __pyx_L4;
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":1743
+    /* "pysam/calignmentfile.pyx":1838
  *         else:
  *             raise ValueError(
  *                 "unknown stepper option `%s` in IteratorColumn" % self.stepper)             # <<<<<<<<<<<<<<
  * 
  *         if self.max_depth:
  */
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_unknown_stepper_option_s_in_Iter, __pyx_v_self->stepper); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_unknown_stepper_option_s_in_Iter, __pyx_v_self->stepper); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
 
-    /* "pysam/csamfile.pyx":1742
+    /* "pysam/calignmentfile.pyx":1837
  *                 &self.iterdata)
  *         else:
  *             raise ValueError(             # <<<<<<<<<<<<<<
  *                 "unknown stepper option `%s` in IteratorColumn" % self.stepper)
  * 
  */
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4);
     __Pyx_GIVEREF(__pyx_t_4);
     __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_Raise(__pyx_t_4, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
   __pyx_L4:;
 
-  /* "pysam/csamfile.pyx":1745
+  /* "pysam/calignmentfile.pyx":1840
  *                 "unknown stepper option `%s` in IteratorColumn" % self.stepper)
  * 
  *         if self.max_depth:             # <<<<<<<<<<<<<<
@@ -19151,7 +19943,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
   __pyx_t_12 = (__pyx_v_self->max_depth != 0);
   if (__pyx_t_12) {
 
-    /* "pysam/csamfile.pyx":1746
+    /* "pysam/calignmentfile.pyx":1841
  * 
  *         if self.max_depth:
  *             bam_plp_set_maxcnt(self.pileup_iter, self.max_depth)             # <<<<<<<<<<<<<<
@@ -19163,7 +19955,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
   }
   __pyx_L5:;
 
-  /* "pysam/csamfile.pyx":1714
+  /* "pysam/calignmentfile.pyx":1801
  *         # bam_plp_set_mask( self.pileup_iter, self.mask )
  * 
  *     cdef setupIteratorData( self,             # <<<<<<<<<<<<<<
@@ -19180,7 +19972,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
   __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.setupIteratorData", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumn.setupIteratorData", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -19189,7 +19981,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1750
+/* "pysam/calignmentfile.pyx":1845
  *         # bam_plp_set_mask( self.pileup_iter, self.mask )
  * 
  *     cdef reset( self, tid, start, end ):             # <<<<<<<<<<<<<<
@@ -19197,7 +19989,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(str
  * 
  */
 
-static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, PyObject *__pyx_v_tid, PyObject *__pyx_v_start, PyObject *__pyx_v_end) {
+static PyObject *__pyx_f_5pysam_14calignmentfile_14IteratorColumn_reset(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self, PyObject *__pyx_v_tid, PyObject *__pyx_v_start, PyObject *__pyx_v_end) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -19210,16 +20002,16 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_ob
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("reset", 0);
-  __Pyx_TraceCall("reset", __pyx_f[0], 1750);
+  __Pyx_TraceCall("reset", __pyx_f[0], 1845);
 
-  /* "pysam/csamfile.pyx":1756
+  /* "pysam/calignmentfile.pyx":1851
  *         having to incur the full set-up costs.
  *         '''
- *         self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen = 0 )             # <<<<<<<<<<<<<<
+ *         self.iter = IteratorRowRegion( self.samfile, tid, start, end, multiple_iterators = 0 )             # <<<<<<<<<<<<<<
  *         self.iterdata.iter = self.iter.iter
  * 
  */
-  __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1756; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(((PyObject *)__pyx_v_self->samfile));
   PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_self->samfile));
@@ -19233,22 +20025,22 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_ob
   __Pyx_INCREF(__pyx_v_end);
   PyTuple_SET_ITEM(__pyx_t_1, 3, __pyx_v_end);
   __Pyx_GIVEREF(__pyx_v_end);
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1756; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_reopen, __pyx_int_0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1756; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowRegion)), __pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1756; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_multiple_iterators, __pyx_int_0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion)), __pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_GIVEREF(__pyx_t_3);
   __Pyx_GOTREF(__pyx_v_self->iter);
   __Pyx_DECREF(((PyObject *)__pyx_v_self->iter));
-  __pyx_v_self->iter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_t_3);
+  __pyx_v_self->iter = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)__pyx_t_3);
   __pyx_t_3 = 0;
 
-  /* "pysam/csamfile.pyx":1757
+  /* "pysam/calignmentfile.pyx":1852
  *         '''
- *         self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen = 0 )
+ *         self.iter = IteratorRowRegion( self.samfile, tid, start, end, multiple_iterators = 0 )
  *         self.iterdata.iter = self.iter.iter             # <<<<<<<<<<<<<<
  * 
  *         # invalidate sequence if different tid
@@ -19256,22 +20048,22 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_ob
   __pyx_t_4 = __pyx_v_self->iter->iter;
   __pyx_v_self->iterdata.iter = __pyx_t_4;
 
-  /* "pysam/csamfile.pyx":1760
+  /* "pysam/calignmentfile.pyx":1855
  * 
  *         # invalidate sequence if different tid
  *         if self.tid != tid:             # <<<<<<<<<<<<<<
  *             if self.iterdata.seq != NULL: free( self.iterdata.seq )
  *             self.iterdata.seq = NULL
  */
-  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_v_tid, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_v_tid, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_5) {
 
-    /* "pysam/csamfile.pyx":1761
+    /* "pysam/calignmentfile.pyx":1856
  *         # invalidate sequence if different tid
  *         if self.tid != tid:
  *             if self.iterdata.seq != NULL: free( self.iterdata.seq )             # <<<<<<<<<<<<<<
@@ -19285,7 +20077,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_ob
     }
     __pyx_L4:;
 
-    /* "pysam/csamfile.pyx":1762
+    /* "pysam/calignmentfile.pyx":1857
  *         if self.tid != tid:
  *             if self.iterdata.seq != NULL: free( self.iterdata.seq )
  *             self.iterdata.seq = NULL             # <<<<<<<<<<<<<<
@@ -19294,7 +20086,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_ob
  */
     __pyx_v_self->iterdata.seq = NULL;
 
-    /* "pysam/csamfile.pyx":1763
+    /* "pysam/calignmentfile.pyx":1858
  *             if self.iterdata.seq != NULL: free( self.iterdata.seq )
  *             self.iterdata.seq = NULL
  *             self.iterdata.tid = -1             # <<<<<<<<<<<<<<
@@ -19306,16 +20098,16 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_ob
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1766
+  /* "pysam/calignmentfile.pyx":1861
  * 
  *         # self.pileup_iter = bam_plp_init( &__advancepileup, &self.iterdata )
  *         bam_plp_reset(self.pileup_iter)             # <<<<<<<<<<<<<<
  * 
- *     def __dealloc__(self):
+ *     cdef _free_pileup_iter(self):
  */
   bam_plp_reset(__pyx_v_self->pileup_iter);
 
-  /* "pysam/csamfile.pyx":1750
+  /* "pysam/calignmentfile.pyx":1845
  *         # bam_plp_set_mask( self.pileup_iter, self.mask )
  * 
  *     cdef reset( self, tid, start, end ):             # <<<<<<<<<<<<<<
@@ -19330,7 +20122,7 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_ob
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.reset", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumn.reset", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -19339,35 +20131,25 @@ static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1768
+/* "pysam/calignmentfile.pyx":1863
  *         bam_plp_reset(self.pileup_iter)
  * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         # reset in order to avoid memory leak messages for iterators
- *         # that have not been fully consumed
+ *     cdef _free_pileup_iter(self):             # <<<<<<<<<<<<<<
+ *         '''free the memory alloc'd by bam_plp_init.
+ * 
  */
 
-/* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_14IteratorColumn_9__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_14IteratorColumn_9__dealloc__(PyObject *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-}
-
-static void __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
+static PyObject *__pyx_f_5pysam_14calignmentfile_14IteratorColumn__free_pileup_iter(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1768);
+  __Pyx_RefNannySetupContext("_free_pileup_iter", 0);
+  __Pyx_TraceCall("_free_pileup_iter", __pyx_f[0], 1863);
 
-  /* "pysam/csamfile.pyx":1771
- *         # reset in order to avoid memory leak messages for iterators
- *         # that have not been fully consumed
+  /* "pysam/calignmentfile.pyx":1869
+ *         another pileup_iter, or else memory will be lost.
+ *         '''
  *         if self.pileup_iter != <bam_plp_t>NULL:             # <<<<<<<<<<<<<<
  *             bam_plp_reset(self.pileup_iter)
  *             bam_plp_destroy(self.pileup_iter)
@@ -19375,8 +20157,8 @@ static void __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(struct __pyx
   __pyx_t_1 = ((__pyx_v_self->pileup_iter != ((bam_plp_t)NULL)) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1772
- *         # that have not been fully consumed
+    /* "pysam/calignmentfile.pyx":1870
+ *         '''
  *         if self.pileup_iter != <bam_plp_t>NULL:
  *             bam_plp_reset(self.pileup_iter)             # <<<<<<<<<<<<<<
  *             bam_plp_destroy(self.pileup_iter)
@@ -19384,47 +20166,104 @@ static void __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(struct __pyx
  */
     bam_plp_reset(__pyx_v_self->pileup_iter);
 
-    /* "pysam/csamfile.pyx":1773
+    /* "pysam/calignmentfile.pyx":1871
  *         if self.pileup_iter != <bam_plp_t>NULL:
  *             bam_plp_reset(self.pileup_iter)
  *             bam_plp_destroy(self.pileup_iter)             # <<<<<<<<<<<<<<
  *             self.pileup_iter = <bam_plp_t>NULL
- *             self.plp = <bam_pileup1_t*>NULL
+ * 
  */
     bam_plp_destroy(__pyx_v_self->pileup_iter);
 
-    /* "pysam/csamfile.pyx":1774
+    /* "pysam/calignmentfile.pyx":1872
  *             bam_plp_reset(self.pileup_iter)
  *             bam_plp_destroy(self.pileup_iter)
  *             self.pileup_iter = <bam_plp_t>NULL             # <<<<<<<<<<<<<<
- *             self.plp = <bam_pileup1_t*>NULL
  * 
+ *     def __dealloc__(self):
  */
     __pyx_v_self->pileup_iter = ((bam_plp_t)NULL);
+    goto __pyx_L3;
+  }
+  __pyx_L3:;
 
-    /* "pysam/csamfile.pyx":1775
- *             bam_plp_destroy(self.pileup_iter)
+  /* "pysam/calignmentfile.pyx":1863
+ *         bam_plp_reset(self.pileup_iter)
+ * 
+ *     cdef _free_pileup_iter(self):             # <<<<<<<<<<<<<<
+ *         '''free the memory alloc'd by bam_plp_init.
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":1874
  *             self.pileup_iter = <bam_plp_t>NULL
- *             self.plp = <bam_pileup1_t*>NULL             # <<<<<<<<<<<<<<
+ * 
+ *     def __dealloc__(self):             # <<<<<<<<<<<<<<
+ *         # reset in order to avoid memory leak messages for iterators
+ *         # that have not been fully consumed
+ */
+
+/* Python wrapper */
+static void __pyx_pw_5pysam_14calignmentfile_14IteratorColumn_9__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_14calignmentfile_14IteratorColumn_9__dealloc__(PyObject *__pyx_v_self) {
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
+  __pyx_pf_5pysam_14calignmentfile_14IteratorColumn_8__dealloc__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+}
+
+static void __pyx_pf_5pysam_14calignmentfile_14IteratorColumn_8__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *__pyx_v_self) {
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_t_2;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__dealloc__", 0);
+  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1874);
+
+  /* "pysam/calignmentfile.pyx":1877
+ *         # reset in order to avoid memory leak messages for iterators
+ *         # that have not been fully consumed
+ *         self._free_pileup_iter()             # <<<<<<<<<<<<<<
+ *         self.plp = <bam_pileup1_t*>NULL
+ * 
+ */
+  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self->__pyx_vtab)->_free_pileup_iter(__pyx_v_self); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1877; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":1878
+ *         # that have not been fully consumed
+ *         self._free_pileup_iter()
+ *         self.plp = <bam_pileup1_t*>NULL             # <<<<<<<<<<<<<<
  * 
  *         if self.iterdata.seq != NULL:
  */
-    __pyx_v_self->plp = ((bam_pileup1_t *)NULL);
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
+  __pyx_v_self->plp = ((bam_pileup1_t *)NULL);
 
-  /* "pysam/csamfile.pyx":1777
- *             self.plp = <bam_pileup1_t*>NULL
+  /* "pysam/calignmentfile.pyx":1880
+ *         self.plp = <bam_pileup1_t*>NULL
  * 
  *         if self.iterdata.seq != NULL:             # <<<<<<<<<<<<<<
  *             free(self.iterdata.seq)
  *             self.iterdata.seq = NULL
  */
-  __pyx_t_1 = ((__pyx_v_self->iterdata.seq != NULL) != 0);
-  if (__pyx_t_1) {
+  __pyx_t_2 = ((__pyx_v_self->iterdata.seq != NULL) != 0);
+  if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":1778
+    /* "pysam/calignmentfile.pyx":1881
  * 
  *         if self.iterdata.seq != NULL:
  *             free(self.iterdata.seq)             # <<<<<<<<<<<<<<
@@ -19433,7 +20272,7 @@ static void __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(struct __pyx
  */
     free(__pyx_v_self->iterdata.seq);
 
-    /* "pysam/csamfile.pyx":1779
+    /* "pysam/calignmentfile.pyx":1882
  *         if self.iterdata.seq != NULL:
  *             free(self.iterdata.seq)
  *             self.iterdata.seq = NULL             # <<<<<<<<<<<<<<
@@ -19441,12 +20280,12 @@ static void __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(struct __pyx
  * 
  */
     __pyx_v_self->iterdata.seq = NULL;
-    goto __pyx_L4;
+    goto __pyx_L3;
   }
-  __pyx_L4:;
+  __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1768
- *         bam_plp_reset(self.pileup_iter)
+  /* "pysam/calignmentfile.pyx":1874
+ *             self.pileup_iter = <bam_plp_t>NULL
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
  *         # reset in order to avoid memory leak messages for iterators
@@ -19454,22 +20293,27 @@ static void __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(struct __pyx
  */
 
   /* function exit code */
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_WriteUnraisable("pysam.calignmentfile.IteratorColumn.__dealloc__", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/csamfile.pyx":1785
+/* "pysam/calignmentfile.pyx":1888
  *     '''iterates over a region only.
  *     '''
- *     def __cinit__(self, Samfile samfile,             # <<<<<<<<<<<<<<
+ *     def __cinit__(self, AlignmentFile samfile,             # <<<<<<<<<<<<<<
  *                   int tid = 0,
  *                   int start = 0,
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
+static int __pyx_pw_5pysam_14calignmentfile_20IteratorColumnRegion_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_20IteratorColumnRegion_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
   int __pyx_v_tid;
   int __pyx_v_start;
   int __pyx_v_end;
@@ -19525,7 +20369,7 @@ static int __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(PyObject
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1785; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1888; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -19538,27 +20382,27 @@ static int __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(PyObject
         default: goto __pyx_L5_argtuple_error;
       }
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
     if (values[1]) {
-      __pyx_v_tid = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_tid = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1889; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
       __pyx_v_tid = ((int)0);
     }
     if (values[2]) {
-      __pyx_v_start = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_start == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1787; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_start = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_start == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1890; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
       __pyx_v_start = ((int)0);
     }
     if (values[3]) {
-      __pyx_v_end = __Pyx_PyInt_As_int(values[3]); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1788; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_end = __Pyx_PyInt_As_int(values[3]); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1891; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
-      __pyx_v_end = __pyx_k__57;
+      __pyx_v_end = __pyx_k__62;
     }
     if (values[4]) {
-      __pyx_v_truncate = __Pyx_PyInt_As_int(values[4]); if (unlikely((__pyx_v_truncate == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1789; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_truncate = __Pyx_PyInt_As_int(values[4]); if (unlikely((__pyx_v_truncate == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1892; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
 
-      /* "pysam/csamfile.pyx":1789
+      /* "pysam/calignmentfile.pyx":1892
  *                   int start = 0,
  *                   int end = max_pos,
  *                   int truncate = False,             # <<<<<<<<<<<<<<
@@ -19570,20 +20414,20 @@ static int __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(PyObject
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 1, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1785; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 1, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1888; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnRegion.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumnRegion.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *)__pyx_v_self), __pyx_v_samfile, __pyx_v_tid, __pyx_v_start, __pyx_v_end, __pyx_v_truncate, __pyx_v_kwargs);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_20IteratorColumnRegion___cinit__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion *)__pyx_v_self), __pyx_v_samfile, __pyx_v_tid, __pyx_v_start, __pyx_v_end, __pyx_v_truncate, __pyx_v_kwargs);
 
-  /* "pysam/csamfile.pyx":1785
+  /* "pysam/calignmentfile.pyx":1888
  *     '''iterates over a region only.
  *     '''
- *     def __cinit__(self, Samfile samfile,             # <<<<<<<<<<<<<<
+ *     def __cinit__(self, AlignmentFile samfile,             # <<<<<<<<<<<<<<
  *                   int tid = 0,
  *                   int start = 0,
  */
@@ -19598,19 +20442,19 @@ static int __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(PyObject
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *__pyx_v_self, CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, int __pyx_v_truncate, CYTHON_UNUSED PyObject *__pyx_v_kwargs) {
+static int __pyx_pf_5pysam_14calignmentfile_20IteratorColumnRegion___cinit__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion *__pyx_v_self, CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, int __pyx_v_truncate, CYTHON_UNUSED PyObject *__pyx_v_kwargs) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData __pyx_t_2;
+  struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData __pyx_t_2;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__cinit__", 0);
-  __Pyx_TraceCall("__cinit__", __pyx_f[0], 1785);
+  __Pyx_TraceCall("__cinit__", __pyx_f[0], 1888);
 
-  /* "pysam/csamfile.pyx":1793
+  /* "pysam/calignmentfile.pyx":1896
  * 
  *         # initialize iterator
  *         self.setupIteratorData( tid, start, end, 1 )             # <<<<<<<<<<<<<<
@@ -19618,12 +20462,12 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __p
  *         self.end = end
  */
   __pyx_t_2.__pyx_n = 1;
-  __pyx_t_2.reopen = 1;
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), __pyx_v_tid, __pyx_v_start, __pyx_v_end, &__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2.multiple_iterators = 1;
+  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnRegion *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self), __pyx_v_tid, __pyx_v_start, __pyx_v_end, &__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1896; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1794
+  /* "pysam/calignmentfile.pyx":1897
  *         # initialize iterator
  *         self.setupIteratorData( tid, start, end, 1 )
  *         self.start = start             # <<<<<<<<<<<<<<
@@ -19632,7 +20476,7 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __p
  */
   __pyx_v_self->start = __pyx_v_start;
 
-  /* "pysam/csamfile.pyx":1795
+  /* "pysam/calignmentfile.pyx":1898
  *         self.setupIteratorData( tid, start, end, 1 )
  *         self.start = start
  *         self.end = end             # <<<<<<<<<<<<<<
@@ -19641,7 +20485,7 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __p
  */
   __pyx_v_self->end = __pyx_v_end;
 
-  /* "pysam/csamfile.pyx":1796
+  /* "pysam/calignmentfile.pyx":1899
  *         self.start = start
  *         self.end = end
  *         self.truncate = truncate             # <<<<<<<<<<<<<<
@@ -19650,10 +20494,10 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __p
  */
   __pyx_v_self->truncate = __pyx_v_truncate;
 
-  /* "pysam/csamfile.pyx":1785
+  /* "pysam/calignmentfile.pyx":1888
  *     '''iterates over a region only.
  *     '''
- *     def __cinit__(self, Samfile samfile,             # <<<<<<<<<<<<<<
+ *     def __cinit__(self, AlignmentFile samfile,             # <<<<<<<<<<<<<<
  *                   int tid = 0,
  *                   int start = 0,
  */
@@ -19663,7 +20507,7 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __p
   goto __pyx_L0;
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnRegion.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumnRegion.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -19671,7 +20515,7 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1798
+/* "pysam/calignmentfile.pyx":1901
  *         self.truncate = truncate
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -19680,23 +20524,23 @@ static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_3__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_20IteratorColumnRegion_2__next__[] = "python version of next().\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_20IteratorColumnRegion_3__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__[] = "python version of next().\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_20IteratorColumnRegion_2__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_3__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_20IteratorColumnRegion_3__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -19706,9 +20550,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1798);
+  __Pyx_TraceCall("__next__", __pyx_f[0], 1901);
 
-  /* "pysam/csamfile.pyx":1802
+  /* "pysam/calignmentfile.pyx":1905
  *         """
  * 
  *         while 1:             # <<<<<<<<<<<<<<
@@ -19717,16 +20561,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
  */
   while (1) {
 
-    /* "pysam/csamfile.pyx":1803
+    /* "pysam/calignmentfile.pyx":1906
  * 
  *         while 1:
  *             self.cnext()             # <<<<<<<<<<<<<<
  *             if self.n_plp < 0:
  *                 raise ValueError("error during iteration" )
  */
-    ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.cnext(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
+    ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnRegion *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.cnext(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self));
 
-    /* "pysam/csamfile.pyx":1804
+    /* "pysam/calignmentfile.pyx":1907
  *         while 1:
  *             self.cnext()
  *             if self.n_plp < 0:             # <<<<<<<<<<<<<<
@@ -19736,21 +20580,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
     __pyx_t_1 = ((__pyx_v_self->__pyx_base.n_plp < 0) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1805
+      /* "pysam/calignmentfile.pyx":1908
  *             self.cnext()
  *             if self.n_plp < 0:
  *                 raise ValueError("error during iteration" )             # <<<<<<<<<<<<<<
  * 
  *             if self.plp == NULL:
  */
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__58, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__63, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_Raise(__pyx_t_2, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":1807
+    /* "pysam/calignmentfile.pyx":1910
  *                 raise ValueError("error during iteration" )
  * 
  *             if self.plp == NULL:             # <<<<<<<<<<<<<<
@@ -19760,7 +20604,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
     __pyx_t_1 = ((__pyx_v_self->__pyx_base.plp == NULL) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1808
+      /* "pysam/calignmentfile.pyx":1911
  * 
  *             if self.plp == NULL:
  *                 raise StopIteration             # <<<<<<<<<<<<<<
@@ -19768,10 +20612,10 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
  *             if self.truncate:
  */
       __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1808; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":1810
+    /* "pysam/calignmentfile.pyx":1913
  *                 raise StopIteration
  * 
  *             if self.truncate:             # <<<<<<<<<<<<<<
@@ -19781,7 +20625,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
     __pyx_t_1 = (__pyx_v_self->truncate != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1811
+      /* "pysam/calignmentfile.pyx":1914
  * 
  *             if self.truncate:
  *                 if self.start > self.pos: continue             # <<<<<<<<<<<<<<
@@ -19793,39 +20637,39 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
         goto __pyx_L3_continue;
       }
 
-      /* "pysam/csamfile.pyx":1812
+      /* "pysam/calignmentfile.pyx":1915
  *             if self.truncate:
  *                 if self.start > self.pos: continue
  *                 if self.pos >= self.end: raise StopIteration             # <<<<<<<<<<<<<<
  * 
- *             return makePileupProxy(&self.plp,
+ *             return makePileupColumn(&self.plp,
  */
       __pyx_t_1 = ((__pyx_v_self->__pyx_base.pos >= __pyx_v_self->end) != 0);
       if (__pyx_t_1) {
         __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       goto __pyx_L7;
     }
     __pyx_L7:;
 
-    /* "pysam/csamfile.pyx":1814
+    /* "pysam/calignmentfile.pyx":1917
  *                 if self.pos >= self.end: raise StopIteration
  * 
- *             return makePileupProxy(&self.plp,             # <<<<<<<<<<<<<<
+ *             return makePileupColumn(&self.plp,             # <<<<<<<<<<<<<<
  *                                    self.tid,
  *                                    self.pos,
  */
     __Pyx_XDECREF(__pyx_r);
 
-    /* "pysam/csamfile.pyx":1817
+    /* "pysam/calignmentfile.pyx":1920
  *                                    self.tid,
  *                                    self.pos,
  *                                    self.n_plp)             # <<<<<<<<<<<<<<
  * 
  * 
  */
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makePileupProxy((&__pyx_v_self->__pyx_base.plp), __pyx_v_self->__pyx_base.tid, __pyx_v_self->__pyx_base.pos, __pyx_v_self->__pyx_base.n_plp); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __pyx_f_5pysam_14calignmentfile_makePileupColumn((&__pyx_v_self->__pyx_base.plp), __pyx_v_self->__pyx_base.tid, __pyx_v_self->__pyx_base.pos, __pyx_v_self->__pyx_base.n_plp); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1917; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -19833,7 +20677,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
     __pyx_L3_continue:;
   }
 
-  /* "pysam/csamfile.pyx":1798
+  /* "pysam/calignmentfile.pyx":1901
  *         self.truncate = truncate
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -19846,7 +20690,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
   goto __pyx_L0;
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnRegion.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumnRegion.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -19855,18 +20699,18 @@ static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(stru
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1824
+/* "pysam/calignmentfile.pyx":1927
  *     """
  * 
  *     def __cinit__(self,             # <<<<<<<<<<<<<<
- *                   Samfile samfile,
+ *                   AlignmentFile samfile,
  *                   **kwargs):
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
+static int __pyx_pw_5pysam_14calignmentfile_21IteratorColumnAllRefs_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_21IteratorColumnAllRefs_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
   CYTHON_UNUSED PyObject *__pyx_v_kwargs = 0;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
@@ -19894,26 +20738,26 @@ static int __pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_1__cinit__(PyObject
         else goto __pyx_L5_argtuple_error;
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1824; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 1) {
       goto __pyx_L5_argtuple_error;
     } else {
       values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1824; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnAllRefs.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumnAllRefs.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1825; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self), __pyx_v_samfile, __pyx_v_kwargs);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1928; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_21IteratorColumnAllRefs___cinit__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs *)__pyx_v_self), __pyx_v_samfile, __pyx_v_kwargs);
 
   /* function exit code */
   goto __pyx_L0;
@@ -19925,35 +20769,35 @@ static int __pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_1__cinit__(PyObject
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, CYTHON_UNUSED PyObject *__pyx_v_kwargs) {
+static int __pyx_pf_5pysam_14calignmentfile_21IteratorColumnAllRefs___cinit__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, CYTHON_UNUSED PyObject *__pyx_v_kwargs) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   int __pyx_t_2;
   int __pyx_t_3;
-  struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData __pyx_t_4;
+  struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData __pyx_t_4;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__cinit__", 0);
-  __Pyx_TraceCall("__cinit__", __pyx_f[0], 1824);
+  __Pyx_TraceCall("__cinit__", __pyx_f[0], 1927);
 
-  /* "pysam/csamfile.pyx":1829
+  /* "pysam/calignmentfile.pyx":1932
  * 
  *         # no iteration over empty files
  *         if not samfile.nreferences:             # <<<<<<<<<<<<<<
  *             raise StopIteration
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_t_3 = ((!__pyx_t_2) != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":1830
+    /* "pysam/calignmentfile.pyx":1933
  *         # no iteration over empty files
  *         if not samfile.nreferences:
  *             raise StopIteration             # <<<<<<<<<<<<<<
@@ -19961,10 +20805,10 @@ static int __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(struct __
  *         # initialize iterator
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1933; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":1833
+  /* "pysam/calignmentfile.pyx":1936
  * 
  *         # initialize iterator
  *         self.setupIteratorData(self.tid, 0, max_pos, 1)             # <<<<<<<<<<<<<<
@@ -19972,16 +20816,16 @@ static int __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(struct __
  *     def __next__(self):
  */
   __pyx_t_4.__pyx_n = 1;
-  __pyx_t_4.reopen = 1;
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), __pyx_v_self->__pyx_base.tid, 0, __pyx_v_5pysam_8csamfile_max_pos, &__pyx_t_4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4.multiple_iterators = 1;
+  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnAllRefs *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self), __pyx_v_self->__pyx_base.tid, 0, __pyx_v_5pysam_14calignmentfile_max_pos, &__pyx_t_4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1824
+  /* "pysam/calignmentfile.pyx":1927
  *     """
  * 
  *     def __cinit__(self,             # <<<<<<<<<<<<<<
- *                   Samfile samfile,
+ *                   AlignmentFile samfile,
  *                   **kwargs):
  */
 
@@ -19990,7 +20834,7 @@ static int __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(struct __
   goto __pyx_L0;
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnAllRefs.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumnAllRefs.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -19998,7 +20842,7 @@ static int __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1835
+/* "pysam/calignmentfile.pyx":1938
  *         self.setupIteratorData(self.tid, 0, max_pos, 1)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -20007,38 +20851,38 @@ static int __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(struct __
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_3__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__[] = "python version of next().\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_21IteratorColumnAllRefs_3__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__[] = "python version of next().\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_3__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_21IteratorColumnAllRefs_3__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   PyObject *__pyx_t_2 = NULL;
   PyObject *__pyx_t_3 = NULL;
   PyObject *__pyx_t_4 = NULL;
-  struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData __pyx_t_5;
+  struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData __pyx_t_5;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1835);
+  __Pyx_TraceCall("__next__", __pyx_f[0], 1938);
 
-  /* "pysam/csamfile.pyx":1839
+  /* "pysam/calignmentfile.pyx":1942
  *         """
  * 
  *         while 1:             # <<<<<<<<<<<<<<
@@ -20047,16 +20891,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(str
  */
   while (1) {
 
-    /* "pysam/csamfile.pyx":1840
+    /* "pysam/calignmentfile.pyx":1943
  * 
  *         while 1:
  *             self.cnext()             # <<<<<<<<<<<<<<
  * 
  *             if self.n_plp < 0:
  */
-    ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.cnext(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
+    ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnAllRefs *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.cnext(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self));
 
-    /* "pysam/csamfile.pyx":1842
+    /* "pysam/calignmentfile.pyx":1945
  *             self.cnext()
  * 
  *             if self.n_plp < 0:             # <<<<<<<<<<<<<<
@@ -20066,54 +20910,54 @@ static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(str
     __pyx_t_1 = ((__pyx_v_self->__pyx_base.n_plp < 0) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1843
+      /* "pysam/calignmentfile.pyx":1946
  * 
  *             if self.n_plp < 0:
  *                 raise ValueError("error during iteration" )             # <<<<<<<<<<<<<<
  * 
  *             # return result, if within same reference
  */
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__59, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__64, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_Raise(__pyx_t_2, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/csamfile.pyx":1846
+    /* "pysam/calignmentfile.pyx":1949
  * 
  *             # return result, if within same reference
  *             if self.plp != NULL:             # <<<<<<<<<<<<<<
- *                 return makePileupProxy(&self.plp,
+ *                 return makePileupColumn(&self.plp,
  *                                        self.tid,
  */
     __pyx_t_1 = ((__pyx_v_self->__pyx_base.plp != NULL) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1847
+      /* "pysam/calignmentfile.pyx":1950
  *             # return result, if within same reference
  *             if self.plp != NULL:
- *                 return makePileupProxy(&self.plp,             # <<<<<<<<<<<<<<
+ *                 return makePileupColumn(&self.plp,             # <<<<<<<<<<<<<<
  *                                        self.tid,
  *                                        self.pos,
  */
       __Pyx_XDECREF(__pyx_r);
 
-      /* "pysam/csamfile.pyx":1850
+      /* "pysam/calignmentfile.pyx":1953
  *                                        self.tid,
  *                                        self.pos,
  *                                        self.n_plp)             # <<<<<<<<<<<<<<
  * 
  *             # otherwise, proceed to next reference or stop
  */
-      __pyx_t_2 = __pyx_f_5pysam_8csamfile_makePileupProxy((&__pyx_v_self->__pyx_base.plp), __pyx_v_self->__pyx_base.tid, __pyx_v_self->__pyx_base.pos, __pyx_v_self->__pyx_base.n_plp); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1847; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __pyx_f_5pysam_14calignmentfile_makePileupColumn((&__pyx_v_self->__pyx_base.plp), __pyx_v_self->__pyx_base.tid, __pyx_v_self->__pyx_base.pos, __pyx_v_self->__pyx_base.n_plp); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __pyx_r = __pyx_t_2;
       __pyx_t_2 = 0;
       goto __pyx_L0;
     }
 
-    /* "pysam/csamfile.pyx":1853
+    /* "pysam/calignmentfile.pyx":1956
  * 
  *             # otherwise, proceed to next reference or stop
  *             self.tid += 1             # <<<<<<<<<<<<<<
@@ -20122,25 +20966,25 @@ static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(str
  */
     __pyx_v_self->__pyx_base.tid = (__pyx_v_self->__pyx_base.tid + 1);
 
-    /* "pysam/csamfile.pyx":1854
+    /* "pysam/calignmentfile.pyx":1957
  *             # otherwise, proceed to next reference or stop
  *             self.tid += 1
  *             if self.tid < self.samfile.nreferences:             # <<<<<<<<<<<<<<
  *                 self.setupIteratorData(self.tid, 0, max_pos, 0)
  *             else:
  */
-    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_self->__pyx_base.tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_self->__pyx_base.tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self->__pyx_base.samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self->__pyx_base.samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = PyObject_RichCompare(__pyx_t_2, __pyx_t_3, Py_LT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyObject_RichCompare(__pyx_t_2, __pyx_t_3, Py_LT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1855
+      /* "pysam/calignmentfile.pyx":1958
  *             self.tid += 1
  *             if self.tid < self.samfile.nreferences:
  *                 self.setupIteratorData(self.tid, 0, max_pos, 0)             # <<<<<<<<<<<<<<
@@ -20148,28 +20992,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(str
  *                 raise StopIteration
  */
       __pyx_t_5.__pyx_n = 1;
-      __pyx_t_5.reopen = 0;
-      __pyx_t_4 = ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), __pyx_v_self->__pyx_base.tid, 0, __pyx_v_5pysam_8csamfile_max_pos, &__pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5.multiple_iterators = 0;
+      __pyx_t_4 = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnAllRefs *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)__pyx_v_self), __pyx_v_self->__pyx_base.tid, 0, __pyx_v_5pysam_14calignmentfile_max_pos, &__pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
       goto __pyx_L7;
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":1857
+      /* "pysam/calignmentfile.pyx":1960
  *                 self.setupIteratorData(self.tid, 0, max_pos, 0)
  *             else:
  *                 raise StopIteration             # <<<<<<<<<<<<<<
  * 
- * ##-------------------------------------------------------------------
+ * 
  */
       __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     __pyx_L7:;
   }
 
-  /* "pysam/csamfile.pyx":1835
+  /* "pysam/calignmentfile.pyx":1938
  *         self.setupIteratorData(self.tid, 0, max_pos, 1)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -20184,7 +21028,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(str
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnAllRefs.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IteratorColumnAllRefs.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -20193,15 +21037,15 @@ static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(str
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1862
- * ##-------------------------------------------------------------------
- * ##-------------------------------------------------------------------
- * cdef inline int32_t query_start(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
+/* "pysam/calignmentfile.pyx":1963
+ * 
+ * 
+ * cdef inline int32_t _getQueryStart(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
  *     cdef uint32_t * cigar_p
  *     cdef uint32_t k, op
  */
 
-static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_v_src) {
+static CYTHON_INLINE int32_t __pyx_f_5pysam_14calignmentfile__getQueryStart(bam1_t *__pyx_v_src) {
   uint32_t *__pyx_v_cigar_p;
   uint32_t __pyx_v_k;
   uint32_t __pyx_v_op;
@@ -20213,10 +21057,10 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
   int __pyx_t_3;
   int __pyx_t_4;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("query_start", 0);
-  __Pyx_TraceCall("query_start", __pyx_f[0], 1862);
+  __Pyx_RefNannySetupContext("_getQueryStart", 0);
+  __Pyx_TraceCall("_getQueryStart", __pyx_f[0], 1963);
 
-  /* "pysam/csamfile.pyx":1865
+  /* "pysam/calignmentfile.pyx":1966
  *     cdef uint32_t * cigar_p
  *     cdef uint32_t k, op
  *     cdef uint32_t start_offset = 0             # <<<<<<<<<<<<<<
@@ -20225,7 +21069,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
  */
   __pyx_v_start_offset = 0;
 
-  /* "pysam/csamfile.pyx":1867
+  /* "pysam/calignmentfile.pyx":1968
  *     cdef uint32_t start_offset = 0
  * 
  *     if pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
@@ -20235,7 +21079,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
   __pyx_t_1 = (pysam_get_n_cigar(__pyx_v_src) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1868
+    /* "pysam/calignmentfile.pyx":1969
  * 
  *     if pysam_get_n_cigar(src):
  *         cigar_p = pysam_bam_get_cigar(src);             # <<<<<<<<<<<<<<
@@ -20244,7 +21088,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
  */
     __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
 
-    /* "pysam/csamfile.pyx":1869
+    /* "pysam/calignmentfile.pyx":1970
  *     if pysam_get_n_cigar(src):
  *         cigar_p = pysam_bam_get_cigar(src);
  *         for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
@@ -20254,7 +21098,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
     __pyx_t_2 = pysam_get_n_cigar(__pyx_v_src);
     for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_2; __pyx_v_k++) {
 
-      /* "pysam/csamfile.pyx":1870
+      /* "pysam/calignmentfile.pyx":1971
  *         cigar_p = pysam_bam_get_cigar(src);
  *         for k from 0 <= k < pysam_get_n_cigar(src):
  *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
@@ -20263,7 +21107,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
  */
       __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
 
-      /* "pysam/csamfile.pyx":1871
+      /* "pysam/calignmentfile.pyx":1972
  *         for k from 0 <= k < pysam_get_n_cigar(src):
  *             op = cigar_p[k] & BAM_CIGAR_MASK
  *             if op == BAM_CHARD_CLIP:             # <<<<<<<<<<<<<<
@@ -20273,7 +21117,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
       __pyx_t_1 = ((__pyx_v_op == BAM_CHARD_CLIP) != 0);
       if (__pyx_t_1) {
 
-        /* "pysam/csamfile.pyx":1872
+        /* "pysam/calignmentfile.pyx":1973
  *             op = cigar_p[k] & BAM_CIGAR_MASK
  *             if op == BAM_CHARD_CLIP:
  *                 if start_offset != 0 and start_offset != src.core.l_qseq:             # <<<<<<<<<<<<<<
@@ -20289,7 +21133,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
         }
         if (__pyx_t_4) {
 
-          /* "pysam/csamfile.pyx":1873
+          /* "pysam/calignmentfile.pyx":1974
  *             if op == BAM_CHARD_CLIP:
  *                 if start_offset != 0 and start_offset != src.core.l_qseq:
  *                     PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')             # <<<<<<<<<<<<<<
@@ -20298,7 +21142,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
  */
           PyErr_SetString(__pyx_builtin_ValueError, __pyx_k_Invalid_clipping_in_CIGAR_string);
 
-          /* "pysam/csamfile.pyx":1874
+          /* "pysam/calignmentfile.pyx":1975
  *                 if start_offset != 0 and start_offset != src.core.l_qseq:
  *                     PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')
  *                     return -1             # <<<<<<<<<<<<<<
@@ -20311,7 +21155,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
         goto __pyx_L6;
       }
 
-      /* "pysam/csamfile.pyx":1875
+      /* "pysam/calignmentfile.pyx":1976
  *                     PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')
  *                     return -1
  *             elif op == BAM_CSOFT_CLIP:             # <<<<<<<<<<<<<<
@@ -20321,7 +21165,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
       __pyx_t_4 = ((__pyx_v_op == BAM_CSOFT_CLIP) != 0);
       if (__pyx_t_4) {
 
-        /* "pysam/csamfile.pyx":1876
+        /* "pysam/calignmentfile.pyx":1977
  *                     return -1
  *             elif op == BAM_CSOFT_CLIP:
  *                 start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
@@ -20333,7 +21177,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
       }
       /*else*/ {
 
-        /* "pysam/csamfile.pyx":1878
+        /* "pysam/calignmentfile.pyx":1979
  *                 start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT
  *             else:
  *                 break             # <<<<<<<<<<<<<<
@@ -20349,20 +21193,20 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1880
+  /* "pysam/calignmentfile.pyx":1981
  *                 break
  * 
  *     return start_offset             # <<<<<<<<<<<<<<
  * 
- * ##-------------------------------------------------------------------
+ * 
  */
   __pyx_r = __pyx_v_start_offset;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1862
- * ##-------------------------------------------------------------------
- * ##-------------------------------------------------------------------
- * cdef inline int32_t query_start(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":1963
+ * 
+ * 
+ * cdef inline int32_t _getQueryStart(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
  *     cdef uint32_t * cigar_p
  *     cdef uint32_t k, op
  */
@@ -20374,15 +21218,15 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1885
- * ##-------------------------------------------------------------------
- * ##-------------------------------------------------------------------
- * cdef inline int32_t query_end(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
+/* "pysam/calignmentfile.pyx":1984
+ * 
+ * 
+ * cdef inline int32_t _getQueryEnd(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
  *     cdef uint32_t * cigar_p
  *     cdef uint32_t k, op
  */
 
-static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_src) {
+static CYTHON_INLINE int32_t __pyx_f_5pysam_14calignmentfile__getQueryEnd(bam1_t *__pyx_v_src) {
   uint32_t *__pyx_v_cigar_p;
   uint32_t __pyx_v_k;
   uint32_t __pyx_v_op;
@@ -20394,10 +21238,10 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
   int __pyx_t_3;
   int __pyx_t_4;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("query_end", 0);
-  __Pyx_TraceCall("query_end", __pyx_f[0], 1885);
+  __Pyx_RefNannySetupContext("_getQueryEnd", 0);
+  __Pyx_TraceCall("_getQueryEnd", __pyx_f[0], 1984);
 
-  /* "pysam/csamfile.pyx":1888
+  /* "pysam/calignmentfile.pyx":1987
  *     cdef uint32_t * cigar_p
  *     cdef uint32_t k, op
  *     cdef uint32_t end_offset = src.core.l_qseq             # <<<<<<<<<<<<<<
@@ -20407,7 +21251,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
   __pyx_t_1 = __pyx_v_src->core.l_qseq;
   __pyx_v_end_offset = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":1890
+  /* "pysam/calignmentfile.pyx":1989
  *     cdef uint32_t end_offset = src.core.l_qseq
  * 
  *     if pysam_get_n_cigar(src) > 1:             # <<<<<<<<<<<<<<
@@ -20417,7 +21261,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
   __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) > 1) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":1891
+    /* "pysam/calignmentfile.pyx":1990
  * 
  *     if pysam_get_n_cigar(src) > 1:
  *         cigar_p = pysam_bam_get_cigar(src);             # <<<<<<<<<<<<<<
@@ -20426,7 +21270,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
  */
     __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
 
-    /* "pysam/csamfile.pyx":1892
+    /* "pysam/calignmentfile.pyx":1991
  *     if pysam_get_n_cigar(src) > 1:
  *         cigar_p = pysam_bam_get_cigar(src);
  *         for k from pysam_get_n_cigar(src) > k >= 1:             # <<<<<<<<<<<<<<
@@ -20435,7 +21279,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
  */
     for (__pyx_v_k = pysam_get_n_cigar(__pyx_v_src)-1; __pyx_v_k >= 1; __pyx_v_k--) {
 
-      /* "pysam/csamfile.pyx":1893
+      /* "pysam/calignmentfile.pyx":1992
  *         cigar_p = pysam_bam_get_cigar(src);
  *         for k from pysam_get_n_cigar(src) > k >= 1:
  *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
@@ -20444,7 +21288,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
  */
       __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
 
-      /* "pysam/csamfile.pyx":1894
+      /* "pysam/calignmentfile.pyx":1993
  *         for k from pysam_get_n_cigar(src) > k >= 1:
  *             op = cigar_p[k] & BAM_CIGAR_MASK
  *             if op == BAM_CHARD_CLIP:             # <<<<<<<<<<<<<<
@@ -20454,7 +21298,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
       __pyx_t_2 = ((__pyx_v_op == BAM_CHARD_CLIP) != 0);
       if (__pyx_t_2) {
 
-        /* "pysam/csamfile.pyx":1895
+        /* "pysam/calignmentfile.pyx":1994
  *             op = cigar_p[k] & BAM_CIGAR_MASK
  *             if op == BAM_CHARD_CLIP:
  *                 if end_offset != 0 and end_offset != src.core.l_qseq:             # <<<<<<<<<<<<<<
@@ -20470,7 +21314,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
         }
         if (__pyx_t_4) {
 
-          /* "pysam/csamfile.pyx":1896
+          /* "pysam/calignmentfile.pyx":1995
  *             if op == BAM_CHARD_CLIP:
  *                 if end_offset != 0 and end_offset != src.core.l_qseq:
  *                     PyErr_SetString(ValueError,             # <<<<<<<<<<<<<<
@@ -20479,7 +21323,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
  */
           PyErr_SetString(__pyx_builtin_ValueError, __pyx_k_Invalid_clipping_in_CIGAR_string);
 
-          /* "pysam/csamfile.pyx":1898
+          /* "pysam/calignmentfile.pyx":1997
  *                     PyErr_SetString(ValueError,
  *                                     'Invalid clipping in CIGAR string')
  *                     return -1             # <<<<<<<<<<<<<<
@@ -20492,7 +21336,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
         goto __pyx_L6;
       }
 
-      /* "pysam/csamfile.pyx":1899
+      /* "pysam/calignmentfile.pyx":1998
  *                                     'Invalid clipping in CIGAR string')
  *                     return -1
  *             elif op == BAM_CSOFT_CLIP:             # <<<<<<<<<<<<<<
@@ -20502,7 +21346,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
       __pyx_t_4 = ((__pyx_v_op == BAM_CSOFT_CLIP) != 0);
       if (__pyx_t_4) {
 
-        /* "pysam/csamfile.pyx":1900
+        /* "pysam/calignmentfile.pyx":1999
  *                     return -1
  *             elif op == BAM_CSOFT_CLIP:
  *                 end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
@@ -20514,7 +21358,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
       }
       /*else*/ {
 
-        /* "pysam/csamfile.pyx":1902
+        /* "pysam/calignmentfile.pyx":2001
  *                 end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT
  *             else:
  *                 break             # <<<<<<<<<<<<<<
@@ -20530,7 +21374,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1904
+  /* "pysam/calignmentfile.pyx":2003
  *                 break
  * 
  *     if end_offset == 0:             # <<<<<<<<<<<<<<
@@ -20540,7 +21384,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
   __pyx_t_4 = ((__pyx_v_end_offset == 0) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":1905
+    /* "pysam/calignmentfile.pyx":2004
  * 
  *     if end_offset == 0:
  *         end_offset = src.core.l_qseq             # <<<<<<<<<<<<<<
@@ -20553,7 +21397,7 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
   }
   __pyx_L8:;
 
-  /* "pysam/csamfile.pyx":1907
+  /* "pysam/calignmentfile.pyx":2006
  *         end_offset = src.core.l_qseq
  * 
  *     return end_offset             # <<<<<<<<<<<<<<
@@ -20563,10 +21407,10 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
   __pyx_r = __pyx_v_end_offset;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1885
- * ##-------------------------------------------------------------------
- * ##-------------------------------------------------------------------
- * cdef inline int32_t query_end(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":1984
+ * 
+ * 
+ * cdef inline int32_t _getQueryEnd(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
  *     cdef uint32_t * cigar_p
  *     cdef uint32_t k, op
  */
@@ -20578,15 +21422,15 @@ static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1910
+/* "pysam/calignmentfile.pyx":2009
  * 
  * 
- * cdef inline object get_seq_range(bam1_t *src, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
+ * cdef inline object _getSequenceRange(bam1_t *src,             # <<<<<<<<<<<<<<
+ *                                      uint32_t start, uint32_t end):
  *     cdef uint8_t * p
- *     cdef uint32_t k
  */
 
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *__pyx_v_src, uint32_t __pyx_v_start, uint32_t __pyx_v_end) {
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_14calignmentfile__getSequenceRange(bam1_t *__pyx_v_src, uint32_t __pyx_v_start, uint32_t __pyx_v_end) {
   uint8_t *__pyx_v_p;
   uint32_t __pyx_v_k;
   char *__pyx_v_s;
@@ -20601,10 +21445,10 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *__
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("get_seq_range", 0);
-  __Pyx_TraceCall("get_seq_range", __pyx_f[0], 1910);
+  __Pyx_RefNannySetupContext("_getSequenceRange", 0);
+  __Pyx_TraceCall("_getSequenceRange", __pyx_f[0], 2009);
 
-  /* "pysam/csamfile.pyx":1915
+  /* "pysam/calignmentfile.pyx":2015
  *     cdef char * s
  * 
  *     if not src.core.l_qseq:             # <<<<<<<<<<<<<<
@@ -20614,7 +21458,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *__
   __pyx_t_1 = ((!(__pyx_v_src->core.l_qseq != 0)) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1916
+    /* "pysam/calignmentfile.pyx":2016
  * 
  *     if not src.core.l_qseq:
  *         return None             # <<<<<<<<<<<<<<
@@ -20627,29 +21471,29 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *__
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":1918
+  /* "pysam/calignmentfile.pyx":2018
  *         return None
  * 
  *     seq = PyBytes_FromStringAndSize(NULL, end - start)             # <<<<<<<<<<<<<<
  *     s   = <char*>seq
  *     p   = pysam_bam_get_seq(src)
  */
-  __pyx_t_2 = PyBytes_FromStringAndSize(NULL, (__pyx_v_end - __pyx_v_start)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyBytes_FromStringAndSize(NULL, (__pyx_v_end - __pyx_v_start)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_seq = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":1919
+  /* "pysam/calignmentfile.pyx":2019
  * 
  *     seq = PyBytes_FromStringAndSize(NULL, end - start)
  *     s   = <char*>seq             # <<<<<<<<<<<<<<
  *     p   = pysam_bam_get_seq(src)
  * 
  */
-  __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_v_seq); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1919; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_v_seq); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_s = ((char *)__pyx_t_3);
 
-  /* "pysam/csamfile.pyx":1920
+  /* "pysam/calignmentfile.pyx":2020
  *     seq = PyBytes_FromStringAndSize(NULL, end - start)
  *     s   = <char*>seq
  *     p   = pysam_bam_get_seq(src)             # <<<<<<<<<<<<<<
@@ -20658,7 +21502,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *__
  */
   __pyx_v_p = pysam_bam_get_seq(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":1922
+  /* "pysam/calignmentfile.pyx":2022
  *     p   = pysam_bam_get_seq(src)
  * 
  *     for k from start <= k < end:             # <<<<<<<<<<<<<<
@@ -20668,40 +21512,43 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *__
   __pyx_t_4 = __pyx_v_end;
   for (__pyx_v_k = __pyx_v_start; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
 
-    /* "pysam/csamfile.pyx":1925
+    /* "pysam/calignmentfile.pyx":2025
  *         # equivalent to seq_nt16_str[bam1_seqi(s, i)] (see bam.c)
  *         # note: do not use string literal as it will be a python string
  *         s[k-start] = seq_nt16_str[p[k/2] >> 4 * (1 - k%2) & 0xf]             # <<<<<<<<<<<<<<
  * 
- *     return seq
+ *     return _charptr_to_str(seq)
  */
     (__pyx_v_s[(__pyx_v_k - __pyx_v_start)]) = (seq_nt16_str[(((__pyx_v_p[__Pyx_div_long(__pyx_v_k, 2)]) >> (4 * (1 - __Pyx_mod_long(__pyx_v_k, 2)))) & 0xf)]);
   }
 
-  /* "pysam/csamfile.pyx":1927
+  /* "pysam/calignmentfile.pyx":2027
  *         s[k-start] = seq_nt16_str[p[k/2] >> 4 * (1 - k%2) & 0xf]
  * 
- *     return seq             # <<<<<<<<<<<<<<
+ *     return _charptr_to_str(seq)             # <<<<<<<<<<<<<<
  * 
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_seq);
-  __pyx_r = __pyx_v_seq;
+  __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_v_seq); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __pyx_f_5pysam_14calignmentfile__charptr_to_str(__pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1910
+  /* "pysam/calignmentfile.pyx":2009
  * 
  * 
- * cdef inline object get_seq_range(bam1_t *src, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
+ * cdef inline object _getSequenceRange(bam1_t *src,             # <<<<<<<<<<<<<<
+ *                                      uint32_t start, uint32_t end):
  *     cdef uint8_t * p
- *     cdef uint32_t k
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.get_seq_range", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile._getSequenceRange", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_seq);
@@ -20711,34 +21558,32 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *__
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1930
+/* "pysam/calignmentfile.pyx":2030
  * 
  * 
- * cdef inline object get_qual_range(bam1_t *src, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
- *     cdef uint8_t * p
- *     cdef uint32_t k
+ * cdef inline object _getQualitiesRange(bam1_t *src,             # <<<<<<<<<<<<<<
+ *                                       uint32_t start,
+ *                                       uint32_t end):
  */
 
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_qual_range(bam1_t *__pyx_v_src, uint32_t __pyx_v_start, uint32_t __pyx_v_end) {
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_14calignmentfile__getQualitiesRange(bam1_t *__pyx_v_src, uint32_t __pyx_v_start, uint32_t __pyx_v_end) {
   uint8_t *__pyx_v_p;
-  uint32_t __pyx_v_k;
-  char *__pyx_v_q;
-  PyObject *__pyx_v_qual = NULL;
+  arrayobject *__pyx_v_result = 0;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   PyObject *__pyx_t_2 = NULL;
-  char *__pyx_t_3;
-  uint32_t __pyx_t_4;
+  PyObject *__pyx_t_3 = NULL;
+  int __pyx_t_4;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("get_qual_range", 0);
-  __Pyx_TraceCall("get_qual_range", __pyx_f[0], 1930);
+  __Pyx_RefNannySetupContext("_getQualitiesRange", 0);
+  __Pyx_TraceCall("_getQualitiesRange", __pyx_f[0], 2030);
 
-  /* "pysam/csamfile.pyx":1935
- *     cdef char * q
+  /* "pysam/calignmentfile.pyx":2038
+ *     cdef uint32_t k
  * 
  *     p = pysam_bam_get_qual(src)             # <<<<<<<<<<<<<<
  *     if p[0] == 0xff:
@@ -20746,7 +21591,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_qual_range(bam1_t *_
  */
   __pyx_v_p = pysam_bam_get_qual(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":1936
+  /* "pysam/calignmentfile.pyx":2039
  * 
  *     p = pysam_bam_get_qual(src)
  *     if p[0] == 0xff:             # <<<<<<<<<<<<<<
@@ -20756,12 +21601,12 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_qual_range(bam1_t *_
   __pyx_t_1 = (((__pyx_v_p[0]) == 0xff) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":1937
+    /* "pysam/calignmentfile.pyx":2040
  *     p = pysam_bam_get_qual(src)
  *     if p[0] == 0xff:
  *         return None             # <<<<<<<<<<<<<<
  * 
- *     qual = PyBytes_FromStringAndSize(NULL, end - start)
+ *     # 'B': unsigned char
  */
     __Pyx_XDECREF(__pyx_r);
     __Pyx_INCREF(Py_None);
@@ -20769,90 +21614,418 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_qual_range(bam1_t *_
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":1939
- *         return None
+  /* "pysam/calignmentfile.pyx":2043
  * 
- *     qual = PyBytes_FromStringAndSize(NULL, end - start)             # <<<<<<<<<<<<<<
- *     q    = <char*>qual
+ *     # 'B': unsigned char
+ *     cdef array.array result = array.array('B', [0])             # <<<<<<<<<<<<<<
+ *     array.resize(result, end - start)
  * 
  */
-  __pyx_t_2 = PyBytes_FromStringAndSize(NULL, (__pyx_v_end - __pyx_v_start)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_v_qual = ((PyObject*)__pyx_t_2);
+  __Pyx_INCREF(__pyx_int_0);
+  PyList_SET_ITEM(__pyx_t_2, 0, __pyx_int_0);
+  __Pyx_GIVEREF(__pyx_int_0);
+  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_INCREF(__pyx_n_s_B);
+  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_n_s_B);
+  __Pyx_GIVEREF(__pyx_n_s_B);
+  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_2);
+  __Pyx_GIVEREF(__pyx_t_2);
+  __pyx_t_2 = 0;
+  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_7cpython_5array_array)), __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_v_result = ((arrayobject *)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":1940
+  /* "pysam/calignmentfile.pyx":2044
+ *     # 'B': unsigned char
+ *     cdef array.array result = array.array('B', [0])
+ *     array.resize(result, end - start)             # <<<<<<<<<<<<<<
  * 
- *     qual = PyBytes_FromStringAndSize(NULL, end - start)
- *     q    = <char*>qual             # <<<<<<<<<<<<<<
+ *     # copy data
+ */
+  __pyx_t_4 = resize(__pyx_v_result, (__pyx_v_end - __pyx_v_start)); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":2047
  * 
- *     for k from start <= k < end:
+ *     # copy data
+ *     memcpy(result.data.as_voidptr, <void*>&p[start], end - start)             # <<<<<<<<<<<<<<
+ * 
+ *     return result
  */
-  __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_v_qual); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_q = ((char *)__pyx_t_3);
+  memcpy(__pyx_v_result->data.as_voidptr, ((void *)(&(__pyx_v_p[__pyx_v_start]))), (__pyx_v_end - __pyx_v_start));
 
-  /* "pysam/csamfile.pyx":1942
- *     q    = <char*>qual
+  /* "pysam/calignmentfile.pyx":2049
+ *     memcpy(result.data.as_voidptr, <void*>&p[start], end - start)
+ * 
+ *     return result             # <<<<<<<<<<<<<<
+ * 
  * 
- *     for k from start <= k < end:             # <<<<<<<<<<<<<<
- *         ## equivalent to t[i] + 33 (see bam.c)
- *         q[k-start] = p[k] + 33
  */
-  __pyx_t_4 = __pyx_v_end;
-  for (__pyx_v_k = __pyx_v_start; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
+  __Pyx_XDECREF(__pyx_r);
+  __Pyx_INCREF(((PyObject *)__pyx_v_result));
+  __pyx_r = ((PyObject *)__pyx_v_result);
+  goto __pyx_L0;
 
-    /* "pysam/csamfile.pyx":1944
- *     for k from start <= k < end:
- *         ## equivalent to t[i] + 33 (see bam.c)
- *         q[k-start] = p[k] + 33             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2030
+ * 
  * 
- *     return qual
+ * cdef inline object _getQualitiesRange(bam1_t *src,             # <<<<<<<<<<<<<<
+ *                                       uint32_t start,
+ *                                       uint32_t end):
  */
-    (__pyx_v_q[(__pyx_v_k - __pyx_v_start)]) = ((__pyx_v_p[__pyx_v_k]) + 33);
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile._getQualitiesRange", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = 0;
+  __pyx_L0:;
+  __Pyx_XDECREF((PyObject *)__pyx_v_result);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2052
+ * 
+ * 
+ * def toQualityString(qualities):             # <<<<<<<<<<<<<<
+ *     '''convert a list of quality score to the string
+ *     representation used in the SAM format.'''
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_1toQualityString(PyObject *__pyx_self, PyObject *__pyx_v_qualities); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_toQualityString[] = "toQualityString(qualities)\nconvert a list of quality score to the string\n    representation used in the SAM format.";
+static PyMethodDef __pyx_mdef_5pysam_14calignmentfile_1toQualityString = {__Pyx_NAMESTR("toQualityString"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_1toQualityString, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_toQualityString)};
+static PyObject *__pyx_pw_5pysam_14calignmentfile_1toQualityString(PyObject *__pyx_self, PyObject *__pyx_v_qualities) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("toQualityString (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_toQualityString(__pyx_self, ((PyObject *)__pyx_v_qualities));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_toQualityString(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_qualities) {
+  PyObject *__pyx_v_x = NULL;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  int __pyx_t_1;
+  int __pyx_t_2;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
+  Py_ssize_t __pyx_t_5;
+  PyObject *(*__pyx_t_6)(PyObject *);
+  PyObject *__pyx_t_7 = NULL;
+  PyObject *__pyx_t_8 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("toQualityString", 0);
+  __Pyx_TraceCall("toQualityString", __pyx_f[0], 2052);
+
+  /* "pysam/calignmentfile.pyx":2055
+ *     '''convert a list of quality score to the string
+ *     representation used in the SAM format.'''
+ *     if qualities is None:             # <<<<<<<<<<<<<<
+ *         return None
+ *     return "".join([chr(x+33) for x in qualities])
+ */
+  __pyx_t_1 = (__pyx_v_qualities == Py_None);
+  __pyx_t_2 = (__pyx_t_1 != 0);
+  if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":2056
+ *     representation used in the SAM format.'''
+ *     if qualities is None:
+ *         return None             # <<<<<<<<<<<<<<
+ *     return "".join([chr(x+33) for x in qualities])
+ * 
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":1946
- *         q[k-start] = p[k] + 33
+  /* "pysam/calignmentfile.pyx":2057
+ *     if qualities is None:
+ *         return None
+ *     return "".join([chr(x+33) for x in qualities])             # <<<<<<<<<<<<<<
  * 
- *     return qual             # <<<<<<<<<<<<<<
  * 
- * cdef inline uint8_t get_type_code(value, value_type = None):
  */
   __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_qual);
-  __pyx_r = __pyx_v_qual;
+  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  if (PyList_CheckExact(__pyx_v_qualities) || PyTuple_CheckExact(__pyx_v_qualities)) {
+    __pyx_t_4 = __pyx_v_qualities; __Pyx_INCREF(__pyx_t_4); __pyx_t_5 = 0;
+    __pyx_t_6 = NULL;
+  } else {
+    __pyx_t_5 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_v_qualities); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __pyx_t_6 = Py_TYPE(__pyx_t_4)->tp_iternext;
+  }
+  for (;;) {
+    if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_4)) {
+      if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_4)) break;
+      #if CYTHON_COMPILING_IN_CPYTHON
+      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #else
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #endif
+    } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_4)) {
+      if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_4)) break;
+      #if CYTHON_COMPILING_IN_CPYTHON
+      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #else
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #endif
+    } else {
+      __pyx_t_7 = __pyx_t_6(__pyx_t_4);
+      if (unlikely(!__pyx_t_7)) {
+        PyObject* exc_type = PyErr_Occurred();
+        if (exc_type) {
+          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        }
+        break;
+      }
+      __Pyx_GOTREF(__pyx_t_7);
+    }
+    __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_7);
+    __pyx_t_7 = 0;
+    __pyx_t_7 = PyNumber_Add(__pyx_v_x, __pyx_int_33); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_8);
+    PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_7);
+    __Pyx_GIVEREF(__pyx_t_7);
+    __pyx_t_7 = 0;
+    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_chr, __pyx_t_8, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+    if (unlikely(__Pyx_ListComp_Append(__pyx_t_3, (PyObject*)__pyx_t_7))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+  }
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  __pyx_t_4 = __Pyx_PyString_Join(__pyx_kp_s__7, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_4;
+  __pyx_t_4 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1930
+  /* "pysam/calignmentfile.pyx":2052
  * 
  * 
- * cdef inline object get_qual_range(bam1_t *src, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
- *     cdef uint8_t * p
- *     cdef uint32_t k
+ * def toQualityString(qualities):             # <<<<<<<<<<<<<<
+ *     '''convert a list of quality score to the string
+ *     representation used in the SAM format.'''
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.get_qual_range", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_7);
+  __Pyx_XDECREF(__pyx_t_8);
+  __Pyx_AddTraceback("pysam.calignmentfile.toQualityString", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_x);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2060
+ * 
+ * 
+ * def fromQualityString(quality_string):             # <<<<<<<<<<<<<<
+ *     '''return a list of quality scores from the
+ *     stringn representation of quality scores used
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_3fromQualityString(PyObject *__pyx_self, PyObject *__pyx_v_quality_string); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_2fromQualityString[] = "fromQualityString(quality_string)\nreturn a list of quality scores from the\n    stringn representation of quality scores used\n    in the SAM format.";
+static PyMethodDef __pyx_mdef_5pysam_14calignmentfile_3fromQualityString = {__Pyx_NAMESTR("fromQualityString"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_3fromQualityString, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_2fromQualityString)};
+static PyObject *__pyx_pw_5pysam_14calignmentfile_3fromQualityString(PyObject *__pyx_self, PyObject *__pyx_v_quality_string) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("fromQualityString (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_2fromQualityString(__pyx_self, ((PyObject *)__pyx_v_quality_string));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_2fromQualityString(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_quality_string) {
+  PyObject *__pyx_v_x = NULL;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  int __pyx_t_1;
+  int __pyx_t_2;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
+  Py_ssize_t __pyx_t_5;
+  PyObject *(*__pyx_t_6)(PyObject *);
+  PyObject *__pyx_t_7 = NULL;
+  PyObject *__pyx_t_8 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("fromQualityString", 0);
+  __Pyx_TraceCall("fromQualityString", __pyx_f[0], 2060);
+
+  /* "pysam/calignmentfile.pyx":2064
+ *     stringn representation of quality scores used
+ *     in the SAM format.'''
+ *     if quality_string is None:             # <<<<<<<<<<<<<<
+ *         return None
+ *     return array.array('B', [ord(x)-33 for x in quality_string])
+ */
+  __pyx_t_1 = (__pyx_v_quality_string == Py_None);
+  __pyx_t_2 = (__pyx_t_1 != 0);
+  if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":2065
+ *     in the SAM format.'''
+ *     if quality_string is None:
+ *         return None             # <<<<<<<<<<<<<<
+ *     return array.array('B', [ord(x)-33 for x in quality_string])
+ * 
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":2066
+ *     if quality_string is None:
+ *         return None
+ *     return array.array('B', [ord(x)-33 for x in quality_string])             # <<<<<<<<<<<<<<
+ * 
+ * 
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  if (PyList_CheckExact(__pyx_v_quality_string) || PyTuple_CheckExact(__pyx_v_quality_string)) {
+    __pyx_t_4 = __pyx_v_quality_string; __Pyx_INCREF(__pyx_t_4); __pyx_t_5 = 0;
+    __pyx_t_6 = NULL;
+  } else {
+    __pyx_t_5 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_v_quality_string); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __pyx_t_6 = Py_TYPE(__pyx_t_4)->tp_iternext;
+  }
+  for (;;) {
+    if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_4)) {
+      if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_4)) break;
+      #if CYTHON_COMPILING_IN_CPYTHON
+      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #else
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #endif
+    } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_4)) {
+      if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_4)) break;
+      #if CYTHON_COMPILING_IN_CPYTHON
+      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #else
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #endif
+    } else {
+      __pyx_t_7 = __pyx_t_6(__pyx_t_4);
+      if (unlikely(!__pyx_t_7)) {
+        PyObject* exc_type = PyErr_Occurred();
+        if (exc_type) {
+          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        }
+        break;
+      }
+      __Pyx_GOTREF(__pyx_t_7);
+    }
+    __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_7);
+    __pyx_t_7 = 0;
+    __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __Pyx_INCREF(__pyx_v_x);
+    PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_x);
+    __Pyx_GIVEREF(__pyx_v_x);
+    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_8);
+    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+    __pyx_t_7 = PyNumber_Subtract(__pyx_t_8, __pyx_int_33); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+    if (unlikely(__Pyx_ListComp_Append(__pyx_t_3, (PyObject*)__pyx_t_7))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+  }
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __Pyx_INCREF(__pyx_n_s_B);
+  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_n_s_B);
+  __Pyx_GIVEREF(__pyx_n_s_B);
+  PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_3);
+  __Pyx_GIVEREF(__pyx_t_3);
+  __pyx_t_3 = 0;
+  __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_7cpython_5array_array)), __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  __pyx_r = __pyx_t_3;
+  __pyx_t_3 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":2060
+ * 
+ * 
+ * def fromQualityString(quality_string):             # <<<<<<<<<<<<<<
+ *     '''return a list of quality scores from the
+ *     stringn representation of quality scores used
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_7);
+  __Pyx_XDECREF(__pyx_t_8);
+  __Pyx_AddTraceback("pysam.calignmentfile.fromQualityString", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_qual);
+  __Pyx_XDECREF(__pyx_v_x);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1948
- *     return qual
+/* "pysam/calignmentfile.pyx":2069
+ * 
  * 
- * cdef inline uint8_t get_type_code(value, value_type = None):             # <<<<<<<<<<<<<<
+ * cdef inline uint8_t _getTypeCode(value, value_type = None):             # <<<<<<<<<<<<<<
  *     '''guess type code for a *value*. If *value_type* is None,
  *     the type code will be inferred based on the Python type of
  */
 
-static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__pyx_v_value, struct __pyx_opt_args_5pysam_8csamfile_get_type_code *__pyx_optional_args) {
+static CYTHON_INLINE uint8_t __pyx_f_5pysam_14calignmentfile__getTypeCode(PyObject *__pyx_v_value, struct __pyx_opt_args_5pysam_14calignmentfile__getTypeCode *__pyx_optional_args) {
   PyObject *__pyx_v_value_type = ((PyObject *)Py_None);
   uint8_t __pyx_v_type_code;
   char *__pyx_v__char_type;
@@ -20866,8 +22039,8 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("get_type_code", 0);
-  __Pyx_TraceCall("get_type_code", __pyx_f[0], 1948);
+  __Pyx_RefNannySetupContext("_getTypeCode", 0);
+  __Pyx_TraceCall("_getTypeCode", __pyx_f[0], 2069);
   if (__pyx_optional_args) {
     if (__pyx_optional_args->__pyx_n > 0) {
       __pyx_v_value_type = __pyx_optional_args->value_type;
@@ -20875,7 +22048,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
   }
   __Pyx_INCREF(__pyx_v_value_type);
 
-  /* "pysam/csamfile.pyx":1955
+  /* "pysam/calignmentfile.pyx":2076
  *     cdef char * _char_type
  * 
  *     if value_type is None:             # <<<<<<<<<<<<<<
@@ -20886,7 +22059,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":1956
+    /* "pysam/calignmentfile.pyx":2077
  * 
  *     if value_type is None:
  *         if isinstance(value, int):             # <<<<<<<<<<<<<<
@@ -20897,7 +22070,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
     __pyx_t_1 = (__pyx_t_2 != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1957
+      /* "pysam/calignmentfile.pyx":2078
  *     if value_type is None:
  *         if isinstance(value, int):
  *             type_code = 'i'             # <<<<<<<<<<<<<<
@@ -20908,7 +22081,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
       goto __pyx_L4;
     }
 
-    /* "pysam/csamfile.pyx":1958
+    /* "pysam/calignmentfile.pyx":2079
  *         if isinstance(value, int):
  *             type_code = 'i'
  *         elif isinstance(value, float):             # <<<<<<<<<<<<<<
@@ -20919,7 +22092,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
     __pyx_t_2 = (__pyx_t_1 != 0);
     if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":1959
+      /* "pysam/calignmentfile.pyx":2080
  *             type_code = 'i'
  *         elif isinstance(value, float):
  *             type_code = 'd'             # <<<<<<<<<<<<<<
@@ -20930,7 +22103,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
       goto __pyx_L4;
     }
 
-    /* "pysam/csamfile.pyx":1960
+    /* "pysam/calignmentfile.pyx":2081
  *         elif isinstance(value, float):
  *             type_code = 'd'
  *         elif isinstance(value, str):             # <<<<<<<<<<<<<<
@@ -20941,7 +22114,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
     __pyx_t_1 = (__pyx_t_2 != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1961
+      /* "pysam/calignmentfile.pyx":2082
  *             type_code = 'd'
  *         elif isinstance(value, str):
  *             type_code = 'Z'             # <<<<<<<<<<<<<<
@@ -20952,7 +22125,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
       goto __pyx_L4;
     }
 
-    /* "pysam/csamfile.pyx":1962
+    /* "pysam/calignmentfile.pyx":2083
  *         elif isinstance(value, str):
  *             type_code = 'Z'
  *         elif isinstance(value, bytes):             # <<<<<<<<<<<<<<
@@ -20963,7 +22136,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
     __pyx_t_2 = (__pyx_t_1 != 0);
     if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":1963
+      /* "pysam/calignmentfile.pyx":2084
  *             type_code = 'Z'
  *         elif isinstance(value, bytes):
  *             type_code = 'Z'             # <<<<<<<<<<<<<<
@@ -20975,7 +22148,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":1965
+      /* "pysam/calignmentfile.pyx":2086
  *             type_code = 'Z'
  *         else:
  *             return 0             # <<<<<<<<<<<<<<
@@ -20990,18 +22163,18 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":1967
+    /* "pysam/calignmentfile.pyx":2088
  *             return 0
  *     else:
  *         if value_type not in 'Zidf':             # <<<<<<<<<<<<<<
  *             return 0
  *         value_type = _forceBytes( value_type )
  */
-    __pyx_t_2 = (__Pyx_PySequence_Contains(__pyx_v_value_type, __pyx_n_s_Zidf, Py_NE)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = (__Pyx_PySequence_Contains(__pyx_v_value_type, __pyx_n_s_Zidf, Py_NE)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2088; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_1 = (__pyx_t_2 != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/csamfile.pyx":1968
+      /* "pysam/calignmentfile.pyx":2089
  *     else:
  *         if value_type not in 'Zidf':
  *             return 0             # <<<<<<<<<<<<<<
@@ -21012,29 +22185,29 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
       goto __pyx_L0;
     }
 
-    /* "pysam/csamfile.pyx":1969
+    /* "pysam/calignmentfile.pyx":2090
  *         if value_type not in 'Zidf':
  *             return 0
  *         value_type = _forceBytes( value_type )             # <<<<<<<<<<<<<<
  *         _char_type = value_type
  *         type_code = (<uint8_t*>_char_type)[0]
  */
-    __pyx_t_3 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_value_type); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1969; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_value_type); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2090; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF_SET(__pyx_v_value_type, __pyx_t_3);
     __pyx_t_3 = 0;
 
-    /* "pysam/csamfile.pyx":1970
+    /* "pysam/calignmentfile.pyx":2091
  *             return 0
  *         value_type = _forceBytes( value_type )
  *         _char_type = value_type             # <<<<<<<<<<<<<<
  *         type_code = (<uint8_t*>_char_type)[0]
  * 
  */
-    __pyx_t_4 = __Pyx_PyObject_AsString(__pyx_v_value_type); if (unlikely((!__pyx_t_4) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1970; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_AsString(__pyx_v_value_type); if (unlikely((!__pyx_t_4) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v__char_type = __pyx_t_4;
 
-    /* "pysam/csamfile.pyx":1971
+    /* "pysam/calignmentfile.pyx":2092
  *         value_type = _forceBytes( value_type )
  *         _char_type = value_type
  *         type_code = (<uint8_t*>_char_type)[0]             # <<<<<<<<<<<<<<
@@ -21045,7 +22218,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1973
+  /* "pysam/calignmentfile.pyx":2094
  *         type_code = (<uint8_t*>_char_type)[0]
  * 
  *     return type_code             # <<<<<<<<<<<<<<
@@ -21055,10 +22228,10 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
   __pyx_r = __pyx_v_type_code;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":1948
- *     return qual
+  /* "pysam/calignmentfile.pyx":2069
  * 
- * cdef inline uint8_t get_type_code(value, value_type = None):             # <<<<<<<<<<<<<<
+ * 
+ * cdef inline uint8_t _getTypeCode(value, value_type = None):             # <<<<<<<<<<<<<<
  *     '''guess type code for a *value*. If *value_type* is None,
  *     the type code will be inferred based on the Python type of
  */
@@ -21066,7 +22239,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_WriteUnraisable("pysam.csamfile.get_type_code", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __Pyx_WriteUnraisable("pysam.calignmentfile._getTypeCode", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_value_type);
@@ -21075,7 +22248,7 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":1975
+/* "pysam/calignmentfile.pyx":2096
  *     return type_code
  * 
  * cdef inline convert_python_tag(pytag, value, fmts, args):             # <<<<<<<<<<<<<<
@@ -21083,14 +22256,13 @@ static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__
  *     if not type(pytag) is bytes:
  */
 
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObject *__pyx_v_pytag, PyObject *__pyx_v_value, PyObject *__pyx_v_fmts, PyObject *__pyx_v_args) {
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_14calignmentfile_convert_python_tag(PyObject *__pyx_v_pytag, PyObject *__pyx_v_value, PyObject *__pyx_v_fmts, PyObject *__pyx_v_args) {
   PyObject *__pyx_v_t = NULL;
   PyObject *__pyx_v_pytype = NULL;
   PyObject *__pyx_v_datafmt = NULL;
   PyObject *__pyx_v_datatype = NULL;
   PyObject *__pyx_v_mi = NULL;
   PyObject *__pyx_v_ma = NULL;
-  PyObject *__pyx_v_absmax = NULL;
   PyObject *__pyx_v_fmt = NULL;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -21100,8 +22272,8 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   PyObject *__pyx_t_4 = NULL;
   int __pyx_t_5;
   PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  Py_ssize_t __pyx_t_8;
+  Py_ssize_t __pyx_t_7;
+  PyObject *__pyx_t_8 = NULL;
   PyObject *__pyx_t_9 = NULL;
   PyObject *__pyx_t_10 = NULL;
   int __pyx_t_11;
@@ -21110,11 +22282,11 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("convert_python_tag", 0);
-  __Pyx_TraceCall("convert_python_tag", __pyx_f[0], 1975);
+  __Pyx_TraceCall("convert_python_tag", __pyx_f[0], 2096);
   __Pyx_INCREF(__pyx_v_pytag);
   __Pyx_INCREF(__pyx_v_value);
 
-  /* "pysam/csamfile.pyx":1977
+  /* "pysam/calignmentfile.pyx":2098
  * cdef inline convert_python_tag(pytag, value, fmts, args):
  * 
  *     if not type(pytag) is bytes:             # <<<<<<<<<<<<<<
@@ -21125,16 +22297,16 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":1978
+    /* "pysam/calignmentfile.pyx":2099
  * 
  *     if not type(pytag) is bytes:
  *         pytag = pytag.encode('ascii')             # <<<<<<<<<<<<<<
  *     t = type(value)
  * 
  */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytag, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1978; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytag, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__60, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1978; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__65, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF_SET(__pyx_v_pytag, __pyx_t_4);
@@ -21143,7 +22315,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":1979
+  /* "pysam/calignmentfile.pyx":2100
  *     if not type(pytag) is bytes:
  *         pytag = pytag.encode('ascii')
  *     t = type(value)             # <<<<<<<<<<<<<<
@@ -21153,7 +22325,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_value)));
   __pyx_v_t = ((PyObject*)((PyObject *)Py_TYPE(__pyx_v_value)));
 
-  /* "pysam/csamfile.pyx":1981
+  /* "pysam/calignmentfile.pyx":2102
  *     t = type(value)
  * 
  *     if t is tuple or t is list:             # <<<<<<<<<<<<<<
@@ -21169,32 +22341,32 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   }
   if (__pyx_t_5) {
 
-    /* "pysam/csamfile.pyx":1983
+    /* "pysam/calignmentfile.pyx":2104
  *     if t is tuple or t is list:
  *         # binary tags - treat separately
  *         pytype = 'B'             # <<<<<<<<<<<<<<
- *         # get data type - first value determines type
- *         if type(value[0]) is float:
+ *         # get data type - first value determines type. If there is a
+ *         # mix of types, the result is undefined.
  */
     __Pyx_INCREF(__pyx_n_s_B);
     __pyx_v_pytype = __pyx_n_s_B;
 
-    /* "pysam/csamfile.pyx":1985
- *         pytype = 'B'
- *         # get data type - first value determines type
+    /* "pysam/calignmentfile.pyx":2107
+ *         # get data type - first value determines type. If there is a
+ *         # mix of types, the result is undefined.
  *         if type(value[0]) is float:             # <<<<<<<<<<<<<<
  *             datafmt, datatype = "f", "f"
  *         else:
  */
-    __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_value, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1985; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_value, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2107; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_4);
     __pyx_t_5 = (((PyObject *)Py_TYPE(__pyx_t_4)) == ((PyObject *)((PyObject*)(&PyFloat_Type))));
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __pyx_t_2 = (__pyx_t_5 != 0);
     if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":1986
- *         # get data type - first value determines type
+      /* "pysam/calignmentfile.pyx":2108
+ *         # mix of types, the result is undefined.
  *         if type(value[0]) is float:
  *             datafmt, datatype = "f", "f"             # <<<<<<<<<<<<<<
  *         else:
@@ -21212,27 +22384,27 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":1988
+      /* "pysam/calignmentfile.pyx":2110
  *             datafmt, datatype = "f", "f"
  *         else:
  *             mi, ma = min(value), max(value)             # <<<<<<<<<<<<<<
- *             absmax = max( abs(mi), abs(ma) )
  *             # signed ints
+ *             if mi < 0:
  */
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_INCREF(__pyx_v_value);
       PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
       __Pyx_GIVEREF(__pyx_v_value);
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_min, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_min, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_INCREF(__pyx_v_value);
       PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
       __Pyx_GIVEREF(__pyx_v_value);
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_max, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_max, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       __pyx_v_mi = __pyx_t_4;
@@ -21240,223 +22412,250 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
       __pyx_v_ma = __pyx_t_6;
       __pyx_t_6 = 0;
 
-      /* "pysam/csamfile.pyx":1989
- *         else:
+      /* "pysam/calignmentfile.pyx":2112
  *             mi, ma = min(value), max(value)
- *             absmax = max( abs(mi), abs(ma) )             # <<<<<<<<<<<<<<
- *             # signed ints
- *             if mi < 0:
- */
-      __pyx_t_6 = PyNumber_Absolute(__pyx_v_ma); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_4 = PyNumber_Absolute(__pyx_v_mi); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_6, __pyx_t_4, Py_GT); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      if (__pyx_t_2) {
-        __Pyx_INCREF(__pyx_t_6);
-        __pyx_t_3 = __pyx_t_6;
-      } else {
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_3 = __pyx_t_4;
-      }
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __pyx_t_3;
-      __Pyx_INCREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_absmax = __pyx_t_6;
-      __pyx_t_6 = 0;
-
-      /* "pysam/csamfile.pyx":1991
- *             absmax = max( abs(mi), abs(ma) )
  *             # signed ints
  *             if mi < 0:             # <<<<<<<<<<<<<<
- *                 if mi >= -127: datafmt, datatype = "b", 'c'
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'
+ *                 if mi >= -128 and ma < 128:
+ *                     datafmt, datatype = "b", 'c'
  */
-      __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       if (__pyx_t_2) {
 
-        /* "pysam/csamfile.pyx":1992
+        /* "pysam/calignmentfile.pyx":2113
  *             # signed ints
  *             if mi < 0:
- *                 if mi >= -127: datafmt, datatype = "b", 'c'             # <<<<<<<<<<<<<<
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'
- *                 elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
+ *                 if mi >= -128 and ma < 128:             # <<<<<<<<<<<<<<
+ *                     datafmt, datatype = "b", 'c'
+ *                 elif mi >= -32768 and ma < 32768:
  */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_127, Py_GE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1992; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1992; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_128, Py_GE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         if (__pyx_t_2) {
+          __pyx_t_6 = PyObject_RichCompare(__pyx_v_ma, __pyx_int_128, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+          __pyx_t_1 = __pyx_t_5;
+        } else {
+          __pyx_t_1 = __pyx_t_2;
+        }
+        if (__pyx_t_1) {
+
+          /* "pysam/calignmentfile.pyx":2114
+ *             if mi < 0:
+ *                 if mi >= -128 and ma < 128:
+ *                     datafmt, datatype = "b", 'c'             # <<<<<<<<<<<<<<
+ *                 elif mi >= -32768 and ma < 32768:
+ *                     datafmt, datatype = "h", 's'
+ */
           __pyx_t_6 = __pyx_n_s_b;
           __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_c;
-          __Pyx_INCREF(__pyx_t_3);
+          __pyx_t_4 = __pyx_n_s_c;
+          __Pyx_INCREF(__pyx_t_4);
           __pyx_v_datafmt = __pyx_t_6;
           __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
+          __pyx_v_datatype = __pyx_t_4;
+          __pyx_t_4 = 0;
           goto __pyx_L7;
         }
 
-        /* "pysam/csamfile.pyx":1993
- *             if mi < 0:
- *                 if mi >= -127: datafmt, datatype = "b", 'c'
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'             # <<<<<<<<<<<<<<
- *                 elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: datafmt, datatype = "i", 'i'
+        /* "pysam/calignmentfile.pyx":2115
+ *                 if mi >= -128 and ma < 128:
+ *                     datafmt, datatype = "b", 'c'
+ *                 elif mi >= -32768 and ma < 32768:             # <<<<<<<<<<<<<<
+ *                     datafmt, datatype = "h", 's'
+ *                 elif mi < -2147483648 or ma >= 2147483648:
  */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_32767, Py_GE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __pyx_n_s_h;
-          __Pyx_INCREF(__pyx_t_3);
+        __pyx_t_4 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_32768, Py_GE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+        if (__pyx_t_1) {
+          __pyx_t_4 = PyObject_RichCompare(__pyx_v_ma, __pyx_int_32768, Py_LT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2115; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+          __pyx_t_5 = __pyx_t_2;
+        } else {
+          __pyx_t_5 = __pyx_t_1;
+        }
+        if (__pyx_t_5) {
+
+          /* "pysam/calignmentfile.pyx":2116
+ *                     datafmt, datatype = "b", 'c'
+ *                 elif mi >= -32768 and ma < 32768:
+ *                     datafmt, datatype = "h", 's'             # <<<<<<<<<<<<<<
+ *                 elif mi < -2147483648 or ma >= 2147483648:
+ *                     raise ValueError(
+ */
+          __pyx_t_4 = __pyx_n_s_h;
+          __Pyx_INCREF(__pyx_t_4);
           __pyx_t_6 = __pyx_n_s_s_2;
           __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
+          __pyx_v_datafmt = __pyx_t_4;
+          __pyx_t_4 = 0;
           __pyx_v_datatype = __pyx_t_6;
           __pyx_t_6 = 0;
           goto __pyx_L7;
         }
 
-        /* "pysam/csamfile.pyx":1994
- *                 if mi >= -127: datafmt, datatype = "b", 'c'
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'
- *                 elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                 else: datafmt, datatype = "i", 'i'
- * 
+        /* "pysam/calignmentfile.pyx":2117
+ *                 elif mi >= -32768 and ma < 32768:
+ *                     datafmt, datatype = "h", 's'
+ *                 elif mi < -2147483648 or ma >= 2147483648:             # <<<<<<<<<<<<<<
+ *                     raise ValueError(
+ *                         "at least one signed integer out of range of "
  */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+        if (!__pyx_t_5) {
+          __pyx_t_6 = PyObject_RichCompare(__pyx_v_ma, __pyx_int_2147483648, Py_GE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+          __pyx_t_2 = __pyx_t_1;
+        } else {
+          __pyx_t_2 = __pyx_t_5;
+        }
         if (__pyx_t_2) {
-          __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_6);
-          __Pyx_GIVEREF(__pyx_t_6);
-          __pyx_t_6 = 0;
-          __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+          /* "pysam/calignmentfile.pyx":2118
+ *                     datafmt, datatype = "h", 's'
+ *                 elif mi < -2147483648 or ma >= 2147483648:
+ *                     raise ValueError(             # <<<<<<<<<<<<<<
+ *                         "at least one signed integer out of range of "
+ *                         "BAM/SAM specification")
+ */
+          __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__66, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_6);
-          __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
           __Pyx_Raise(__pyx_t_6, 0, 0, 0);
           __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         /*else*/ {
 
-          /* "pysam/csamfile.pyx":1995
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'
- *                 elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
+          /* "pysam/calignmentfile.pyx":2121
+ *                         "at least one signed integer out of range of "
+ *                         "BAM/SAM specification")
  *                 else: datafmt, datatype = "i", 'i'             # <<<<<<<<<<<<<<
  * 
  *             # unsigned ints
  */
           __pyx_t_6 = __pyx_n_s_i;
           __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_i;
-          __Pyx_INCREF(__pyx_t_3);
+          __pyx_t_4 = __pyx_n_s_i;
+          __Pyx_INCREF(__pyx_t_4);
           __pyx_v_datafmt = __pyx_t_6;
           __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
+          __pyx_v_datatype = __pyx_t_4;
+          __pyx_t_4 = 0;
         }
         __pyx_L7:;
         goto __pyx_L6;
       }
       /*else*/ {
 
-        /* "pysam/csamfile.pyx":1999
+        /* "pysam/calignmentfile.pyx":2125
  *             # unsigned ints
  *             else:
- *                 if absmax <= 255: datafmt, datatype = "B", 'C'             # <<<<<<<<<<<<<<
- *                 elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                 elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
+ *                 if ma < 256:             # <<<<<<<<<<<<<<
+ *                     datafmt, datatype = "B", 'C'
+ *                 elif ma < 65536:
  */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_255, Py_LE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1999; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1999; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+        __pyx_t_4 = PyObject_RichCompare(__pyx_v_ma, __pyx_int_256, Py_LT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
         if (__pyx_t_2) {
-          __pyx_t_3 = __pyx_n_s_B;
-          __Pyx_INCREF(__pyx_t_3);
+
+          /* "pysam/calignmentfile.pyx":2126
+ *             else:
+ *                 if ma < 256:
+ *                     datafmt, datatype = "B", 'C'             # <<<<<<<<<<<<<<
+ *                 elif ma < 65536:
+ *                     datafmt, datatype = "H", 'S'
+ */
+          __pyx_t_4 = __pyx_n_s_B;
+          __Pyx_INCREF(__pyx_t_4);
           __pyx_t_6 = __pyx_n_s_C;
           __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
+          __pyx_v_datafmt = __pyx_t_4;
+          __pyx_t_4 = 0;
           __pyx_v_datatype = __pyx_t_6;
           __pyx_t_6 = 0;
           goto __pyx_L8;
         }
 
-        /* "pysam/csamfile.pyx":2000
- *             else:
- *                 if absmax <= 255: datafmt, datatype = "B", 'C'
- *                 elif absmax <= 65535: datafmt, datatype = "H", 'S'             # <<<<<<<<<<<<<<
- *                 elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: datafmt, datatype = "I", 'I'
+        /* "pysam/calignmentfile.pyx":2127
+ *                 if ma < 256:
+ *                     datafmt, datatype = "B", 'C'
+ *                 elif ma < 65536:             # <<<<<<<<<<<<<<
+ *                     datafmt, datatype = "H", 'S'
+ *                 elif ma >= 4294967296:
  */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_65535, Py_LE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyObject_RichCompare(__pyx_v_ma, __pyx_int_65536, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2127; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2127; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         if (__pyx_t_2) {
+
+          /* "pysam/calignmentfile.pyx":2128
+ *                     datafmt, datatype = "B", 'C'
+ *                 elif ma < 65536:
+ *                     datafmt, datatype = "H", 'S'             # <<<<<<<<<<<<<<
+ *                 elif ma >= 4294967296:
+ *                     raise ValueError(
+ */
           __pyx_t_6 = __pyx_n_s_H;
           __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_S;
-          __Pyx_INCREF(__pyx_t_3);
+          __pyx_t_4 = __pyx_n_s_S;
+          __Pyx_INCREF(__pyx_t_4);
           __pyx_v_datafmt = __pyx_t_6;
           __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
+          __pyx_v_datatype = __pyx_t_4;
+          __pyx_t_4 = 0;
           goto __pyx_L8;
         }
 
-        /* "pysam/csamfile.pyx":2001
- *                 if absmax <= 255: datafmt, datatype = "B", 'C'
- *                 elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                 elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                 else: datafmt, datatype = "I", 'I'
- * 
+        /* "pysam/calignmentfile.pyx":2129
+ *                 elif ma < 65536:
+ *                     datafmt, datatype = "H", 'S'
+ *                 elif ma >= 4294967296:             # <<<<<<<<<<<<<<
+ *                     raise ValueError(
+ *                         "at least one integer out of range of BAM/SAM specification")
  */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_4294967295, Py_GT); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+        __pyx_t_4 = PyObject_RichCompare(__pyx_v_ma, __pyx_int_4294967296, Py_GE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
         if (__pyx_t_2) {
-          __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_3);
-          __Pyx_GIVEREF(__pyx_t_3);
-          __pyx_t_3 = 0;
-          __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          __Pyx_Raise(__pyx_t_3, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+          /* "pysam/calignmentfile.pyx":2130
+ *                     datafmt, datatype = "H", 'S'
+ *                 elif ma >= 4294967296:
+ *                     raise ValueError(             # <<<<<<<<<<<<<<
+ *                         "at least one integer out of range of BAM/SAM specification")
+ *                 else:
+ */
+          __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__67, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __Pyx_GOTREF(__pyx_t_4);
+          __Pyx_Raise(__pyx_t_4, 0, 0, 0);
+          __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         /*else*/ {
 
-          /* "pysam/csamfile.pyx":2002
- *                 elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                 elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: datafmt, datatype = "I", 'I'             # <<<<<<<<<<<<<<
+          /* "pysam/calignmentfile.pyx":2133
+ *                         "at least one integer out of range of BAM/SAM specification")
+ *                 else:
+ *                     datafmt, datatype = "I", 'I'             # <<<<<<<<<<<<<<
  * 
  *         datafmt = "2sccI%i%s" % (len(value), datafmt)
  */
-          __pyx_t_3 = __pyx_n_s_I;
-          __Pyx_INCREF(__pyx_t_3);
+          __pyx_t_4 = __pyx_n_s_I;
+          __Pyx_INCREF(__pyx_t_4);
           __pyx_t_6 = __pyx_n_s_I;
           __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
+          __pyx_v_datafmt = __pyx_t_4;
+          __pyx_t_4 = 0;
           __pyx_v_datatype = __pyx_t_6;
           __pyx_t_6 = 0;
         }
@@ -21466,150 +22665,150 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
     }
     __pyx_L5:;
 
-    /* "pysam/csamfile.pyx":2004
- *                 else: datafmt, datatype = "I", 'I'
+    /* "pysam/calignmentfile.pyx":2135
+ *                     datafmt, datatype = "I", 'I'
  * 
  *         datafmt = "2sccI%i%s" % (len(value), datafmt)             # <<<<<<<<<<<<<<
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),
+ *         args.extend([pytag[:2],
+ *                      pytype.encode('ascii'),
  */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = PyInt_FromSsize_t(__pyx_t_8); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyInt_FromSsize_t(__pyx_t_7); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_6);
+    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_6);
     __Pyx_GIVEREF(__pyx_t_6);
     __Pyx_INCREF(__pyx_v_datafmt);
-    PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_v_datafmt);
+    PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_v_datafmt);
     __Pyx_GIVEREF(__pyx_v_datafmt);
     __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_2sccI_i_s, __pyx_t_3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_2sccI_i_s, __pyx_t_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_DECREF_SET(__pyx_v_datafmt, __pyx_t_6);
     __pyx_t_6 = 0;
 
-    /* "pysam/csamfile.pyx":2005
+    /* "pysam/calignmentfile.pyx":2136
  * 
  *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
+ *         args.extend([pytag[:2],             # <<<<<<<<<<<<<<
+ *                      pytype.encode('ascii'),
+ *                      datatype.encode('ascii'),
  */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__61, 0, 1, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
+    __pyx_t_4 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__68, 0, 1, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
 
-    /* "pysam/csamfile.pyx":2006
+    /* "pysam/calignmentfile.pyx":2137
  *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )
+ *         args.extend([pytag[:2],
+ *                      pytype.encode('ascii'),             # <<<<<<<<<<<<<<
+ *                      datatype.encode('ascii'),
+ *                      len(value)] + list(value))
  */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_tuple__62, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__69, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_8);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-    /* "pysam/csamfile.pyx":2007
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       len(value)] + list(value) )
+    /* "pysam/calignmentfile.pyx":2138
+ *         args.extend([pytag[:2],
+ *                      pytype.encode('ascii'),
+ *                      datatype.encode('ascii'),             # <<<<<<<<<<<<<<
+ *                      len(value)] + list(value))
  *         fmts.append( datafmt )
  */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_datatype, __pyx_n_s_encode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_tuple__63, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_datatype, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__70, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-    /* "pysam/csamfile.pyx":2008
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )             # <<<<<<<<<<<<<<
+    /* "pysam/calignmentfile.pyx":2139
+ *                      pytype.encode('ascii'),
+ *                      datatype.encode('ascii'),
+ *                      len(value)] + list(value))             # <<<<<<<<<<<<<<
  *         fmts.append( datafmt )
  *         return
  */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = PyInt_FromSsize_t(__pyx_t_8); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
+    __pyx_t_7 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyInt_FromSsize_t(__pyx_t_7); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
 
-    /* "pysam/csamfile.pyx":2005
+    /* "pysam/calignmentfile.pyx":2136
  * 
  *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
+ *         args.extend([pytag[:2],             # <<<<<<<<<<<<<<
+ *                      pytype.encode('ascii'),
+ *                      datatype.encode('ascii'),
  */
-    __pyx_t_10 = PyList_New(4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyList_New(4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    PyList_SET_ITEM(__pyx_t_10, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    PyList_SET_ITEM(__pyx_t_10, 1, __pyx_t_7);
-    __Pyx_GIVEREF(__pyx_t_7);
+    PyList_SET_ITEM(__pyx_t_10, 0, __pyx_t_4);
+    __Pyx_GIVEREF(__pyx_t_4);
+    PyList_SET_ITEM(__pyx_t_10, 1, __pyx_t_8);
+    __Pyx_GIVEREF(__pyx_t_8);
     PyList_SET_ITEM(__pyx_t_10, 2, __pyx_t_9);
     __Pyx_GIVEREF(__pyx_t_9);
-    PyList_SET_ITEM(__pyx_t_10, 3, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_3 = 0;
-    __pyx_t_7 = 0;
-    __pyx_t_9 = 0;
+    PyList_SET_ITEM(__pyx_t_10, 3, __pyx_t_3);
+    __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_4 = 0;
+    __pyx_t_8 = 0;
+    __pyx_t_9 = 0;
+    __pyx_t_3 = 0;
 
-    /* "pysam/csamfile.pyx":2008
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )             # <<<<<<<<<<<<<<
+    /* "pysam/calignmentfile.pyx":2139
+ *                      pytype.encode('ascii'),
+ *                      datatype.encode('ascii'),
+ *                      len(value)] + list(value))             # <<<<<<<<<<<<<<
  *         fmts.append( datafmt )
  *         return
  */
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_value);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_value);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
     __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_9 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_4, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = PyNumber_Add(__pyx_t_10, __pyx_t_9); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __pyx_t_3 = PyNumber_Add(__pyx_t_10, __pyx_t_9); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-    /* "pysam/csamfile.pyx":2005
+    /* "pysam/calignmentfile.pyx":2136
  * 
  *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
+ *         args.extend([pytag[:2],             # <<<<<<<<<<<<<<
+ *                      pytype.encode('ascii'),
+ *                      datatype.encode('ascii'),
  */
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
+    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_3);
+    __Pyx_GIVEREF(__pyx_t_3);
+    __pyx_t_3 = 0;
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-    /* "pysam/csamfile.pyx":2009
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )
+    /* "pysam/calignmentfile.pyx":2140
+ *                      datatype.encode('ascii'),
+ *                      len(value)] + list(value))
  *         fmts.append( datafmt )             # <<<<<<<<<<<<<<
  *         return
  * 
  */
-    __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_datafmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_datafmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":2010
- *                       len(value)] + list(value) )
+    /* "pysam/calignmentfile.pyx":2141
+ *                      len(value)] + list(value))
  *         fmts.append( datafmt )
  *         return             # <<<<<<<<<<<<<<
  * 
@@ -21620,7 +22819,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":2012
+  /* "pysam/calignmentfile.pyx":2143
  *         return
  * 
  *     if t is float:             # <<<<<<<<<<<<<<
@@ -21631,25 +22830,25 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   __pyx_t_5 = (__pyx_t_2 != 0);
   if (__pyx_t_5) {
 
-    /* "pysam/csamfile.pyx":2013
+    /* "pysam/calignmentfile.pyx":2144
  * 
  *     if t is float:
  *         fmt, pytype = "2scf", 'f'             # <<<<<<<<<<<<<<
  *     elif t is int:
  *         # negative values
  */
-    __pyx_t_4 = __pyx_kp_s_2scf;
-    __Pyx_INCREF(__pyx_t_4);
+    __pyx_t_3 = __pyx_kp_s_2scf;
+    __Pyx_INCREF(__pyx_t_3);
     __pyx_t_9 = __pyx_n_s_f;
     __Pyx_INCREF(__pyx_t_9);
-    __pyx_v_fmt = __pyx_t_4;
-    __pyx_t_4 = 0;
+    __pyx_v_fmt = __pyx_t_3;
+    __pyx_t_3 = 0;
     __pyx_v_pytype = __pyx_t_9;
     __pyx_t_9 = 0;
     goto __pyx_L9;
   }
 
-  /* "pysam/csamfile.pyx":2014
+  /* "pysam/calignmentfile.pyx":2145
  *     if t is float:
  *         fmt, pytype = "2scf", 'f'
  *     elif t is int:             # <<<<<<<<<<<<<<
@@ -21660,90 +22859,90 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   __pyx_t_2 = (__pyx_t_5 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":2016
+    /* "pysam/calignmentfile.pyx":2147
  *     elif t is int:
  *         # negative values
  *         if value < 0:             # <<<<<<<<<<<<<<
  *             if value >= -127: fmt, pytype = "2scb", 'c'
  *             elif value >= -32767: fmt, pytype = "2sch", 's'
  */
-    __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2016; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2016; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2147; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2147; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":2017
+      /* "pysam/calignmentfile.pyx":2148
  *         # negative values
  *         if value < 0:
  *             if value >= -127: fmt, pytype = "2scb", 'c'             # <<<<<<<<<<<<<<
  *             elif value >= -32767: fmt, pytype = "2sch", 's'
  *             elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
  */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_127, Py_GE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_127, Py_GE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2148; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2148; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       if (__pyx_t_2) {
         __pyx_t_9 = __pyx_kp_s_2scb;
         __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_c;
-        __Pyx_INCREF(__pyx_t_4);
+        __pyx_t_3 = __pyx_n_s_c;
+        __Pyx_INCREF(__pyx_t_3);
         __pyx_v_fmt = __pyx_t_9;
         __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
+        __pyx_v_pytype = __pyx_t_3;
+        __pyx_t_3 = 0;
         goto __pyx_L11;
       }
 
-      /* "pysam/csamfile.pyx":2018
+      /* "pysam/calignmentfile.pyx":2149
  *         if value < 0:
  *             if value >= -127: fmt, pytype = "2scb", 'c'
  *             elif value >= -32767: fmt, pytype = "2sch", 's'             # <<<<<<<<<<<<<<
  *             elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
  *             else: fmt, pytype = "2sci", 'i'
  */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_32767, Py_GE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+      __pyx_t_3 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_32767, Py_GE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       if (__pyx_t_2) {
-        __pyx_t_4 = __pyx_kp_s_2sch;
-        __Pyx_INCREF(__pyx_t_4);
+        __pyx_t_3 = __pyx_kp_s_2sch;
+        __Pyx_INCREF(__pyx_t_3);
         __pyx_t_9 = __pyx_n_s_s_2;
         __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
+        __pyx_v_fmt = __pyx_t_3;
+        __pyx_t_3 = 0;
         __pyx_v_pytype = __pyx_t_9;
         __pyx_t_9 = 0;
         goto __pyx_L11;
       }
 
-      /* "pysam/csamfile.pyx":2019
+      /* "pysam/calignmentfile.pyx":2150
  *             if value >= -127: fmt, pytype = "2scb", 'c'
  *             elif value >= -32767: fmt, pytype = "2sch", 's'
  *             elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
  *             else: fmt, pytype = "2sci", 'i'
  *         # positive values
  */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       if (__pyx_t_2) {
-        __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_9);
+        __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_3);
+        PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_9);
         __Pyx_GIVEREF(__pyx_t_9);
         __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
         __Pyx_Raise(__pyx_t_9, 0, 0, 0);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       /*else*/ {
 
-        /* "pysam/csamfile.pyx":2020
+        /* "pysam/calignmentfile.pyx":2151
  *             elif value >= -32767: fmt, pytype = "2sch", 's'
  *             elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
  *             else: fmt, pytype = "2sci", 'i'             # <<<<<<<<<<<<<<
@@ -21752,102 +22951,102 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
  */
         __pyx_t_9 = __pyx_kp_s_2sci;
         __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_i;
-        __Pyx_INCREF(__pyx_t_4);
+        __pyx_t_3 = __pyx_n_s_i;
+        __Pyx_INCREF(__pyx_t_3);
         __pyx_v_fmt = __pyx_t_9;
         __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
+        __pyx_v_pytype = __pyx_t_3;
+        __pyx_t_3 = 0;
       }
       __pyx_L11:;
       goto __pyx_L10;
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":2023
+      /* "pysam/calignmentfile.pyx":2154
  *         # positive values
  *         else:
  *             if value <= 255: fmt, pytype = "2scB", 'C'             # <<<<<<<<<<<<<<
  *             elif value <= 65535: fmt, pytype = "2scH", 'S'
  *             elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
  */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_255, Py_LE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2023; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2023; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+      __pyx_t_3 = PyObject_RichCompare(__pyx_v_value, __pyx_int_255, Py_LE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       if (__pyx_t_2) {
-        __pyx_t_4 = __pyx_kp_s_2scB;
-        __Pyx_INCREF(__pyx_t_4);
+        __pyx_t_3 = __pyx_kp_s_2scB;
+        __Pyx_INCREF(__pyx_t_3);
         __pyx_t_9 = __pyx_n_s_C;
         __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
+        __pyx_v_fmt = __pyx_t_3;
+        __pyx_t_3 = 0;
         __pyx_v_pytype = __pyx_t_9;
         __pyx_t_9 = 0;
         goto __pyx_L12;
       }
 
-      /* "pysam/csamfile.pyx":2024
+      /* "pysam/calignmentfile.pyx":2155
  *         else:
  *             if value <= 255: fmt, pytype = "2scB", 'C'
  *             elif value <= 65535: fmt, pytype = "2scH", 'S'             # <<<<<<<<<<<<<<
  *             elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
  *             else: fmt, pytype = "2scI", 'I'
  */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_65535, Py_LE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2024; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2024; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_65535, Py_LE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       if (__pyx_t_2) {
         __pyx_t_9 = __pyx_kp_s_2scH;
         __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_S;
-        __Pyx_INCREF(__pyx_t_4);
+        __pyx_t_3 = __pyx_n_s_S;
+        __Pyx_INCREF(__pyx_t_3);
         __pyx_v_fmt = __pyx_t_9;
         __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
+        __pyx_v_pytype = __pyx_t_3;
+        __pyx_t_3 = 0;
         goto __pyx_L12;
       }
 
-      /* "pysam/csamfile.pyx":2025
+      /* "pysam/calignmentfile.pyx":2156
  *             if value <= 255: fmt, pytype = "2scB", 'C'
  *             elif value <= 65535: fmt, pytype = "2scH", 'S'
  *             elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
  *             else: fmt, pytype = "2scI", 'I'
  *     else:
  */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_4294967295, Py_GT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+      __pyx_t_3 = PyObject_RichCompare(__pyx_v_value, __pyx_int_4294967295, Py_GT); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       if (__pyx_t_2) {
-        __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_3);
+        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_4);
-        __Pyx_GIVEREF(__pyx_t_4);
-        __pyx_t_4 = 0;
-        __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
+        PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_3);
+        __Pyx_GIVEREF(__pyx_t_3);
+        __pyx_t_3 = 0;
+        __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_3);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __Pyx_Raise(__pyx_t_4, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_Raise(__pyx_t_3, 0, 0, 0);
+        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       /*else*/ {
 
-        /* "pysam/csamfile.pyx":2026
+        /* "pysam/calignmentfile.pyx":2157
  *             elif value <= 65535: fmt, pytype = "2scH", 'S'
  *             elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
  *             else: fmt, pytype = "2scI", 'I'             # <<<<<<<<<<<<<<
  *     else:
  *         # Note: hex strings (H) are not supported yet
  */
-        __pyx_t_4 = __pyx_kp_s_2scI;
-        __Pyx_INCREF(__pyx_t_4);
+        __pyx_t_3 = __pyx_kp_s_2scI;
+        __Pyx_INCREF(__pyx_t_3);
         __pyx_t_9 = __pyx_n_s_I;
         __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
+        __pyx_v_fmt = __pyx_t_3;
+        __pyx_t_3 = 0;
         __pyx_v_pytype = __pyx_t_9;
         __pyx_t_9 = 0;
       }
@@ -21858,7 +23057,7 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":2029
+    /* "pysam/calignmentfile.pyx":2160
  *     else:
  *         # Note: hex strings (H) are not supported yet
  *         if t is not bytes:             # <<<<<<<<<<<<<<
@@ -21869,71 +23068,71 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
     __pyx_t_5 = (__pyx_t_2 != 0);
     if (__pyx_t_5) {
 
-      /* "pysam/csamfile.pyx":2030
+      /* "pysam/calignmentfile.pyx":2161
  *         # Note: hex strings (H) are not supported yet
  *         if t is not bytes:
  *             value = value.encode('ascii')             # <<<<<<<<<<<<<<
  *         if len(value) == 1:
  *             fmt, pytype = "2scc", 'A'
  */
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_value, __pyx_n_s_encode); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_value, __pyx_n_s_encode); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2161; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__64, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
+      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__71, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2161; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_4);
-      __pyx_t_4 = 0;
+      __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_3);
+      __pyx_t_3 = 0;
       goto __pyx_L13;
     }
     __pyx_L13:;
 
-    /* "pysam/csamfile.pyx":2031
+    /* "pysam/calignmentfile.pyx":2162
  *         if t is not bytes:
  *             value = value.encode('ascii')
  *         if len(value) == 1:             # <<<<<<<<<<<<<<
  *             fmt, pytype = "2scc", 'A'
  *         else:
  */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2031; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = ((__pyx_t_8 == 1) != 0);
+    __pyx_t_7 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = ((__pyx_t_7 == 1) != 0);
     if (__pyx_t_5) {
 
-      /* "pysam/csamfile.pyx":2032
+      /* "pysam/calignmentfile.pyx":2163
  *             value = value.encode('ascii')
  *         if len(value) == 1:
  *             fmt, pytype = "2scc", 'A'             # <<<<<<<<<<<<<<
  *         else:
  *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
  */
-      __pyx_t_4 = __pyx_kp_s_2scc;
-      __Pyx_INCREF(__pyx_t_4);
+      __pyx_t_3 = __pyx_kp_s_2scc;
+      __Pyx_INCREF(__pyx_t_3);
       __pyx_t_9 = __pyx_n_s_A;
       __Pyx_INCREF(__pyx_t_9);
-      __pyx_v_fmt = __pyx_t_4;
-      __pyx_t_4 = 0;
+      __pyx_v_fmt = __pyx_t_3;
+      __pyx_t_3 = 0;
       __pyx_v_pytype = __pyx_t_9;
       __pyx_t_9 = 0;
       goto __pyx_L14;
     }
     /*else*/ {
 
-      /* "pysam/csamfile.pyx":2034
+      /* "pysam/calignmentfile.pyx":2165
  *             fmt, pytype = "2scc", 'A'
  *         else:
  *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'             # <<<<<<<<<<<<<<
  * 
- *     args.extend( [pytag[:2],
+ *     args.extend([pytag[:2],
  */
-      __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_9 = PyInt_FromSsize_t((__pyx_t_8 + 1)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyInt_FromSsize_t((__pyx_t_7 + 1)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_2sc_is, __pyx_t_9); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
+      __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_2sc_is, __pyx_t_9); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       __pyx_t_9 = __pyx_n_s_Z;
       __Pyx_INCREF(__pyx_t_9);
-      __pyx_v_fmt = __pyx_t_4;
-      __pyx_t_4 = 0;
+      __pyx_v_fmt = __pyx_t_3;
+      __pyx_t_3 = 0;
       __pyx_v_pytype = __pyx_t_9;
       __pyx_t_9 = 0;
     }
@@ -21941,70 +23140,70 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   }
   __pyx_L9:;
 
-  /* "pysam/csamfile.pyx":2036
+  /* "pysam/calignmentfile.pyx":2167
  *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
  * 
- *     args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                   pytype.encode('ascii'),
- *                   value ] )
+ *     args.extend([pytag[:2],             # <<<<<<<<<<<<<<
+ *                  pytype.encode('ascii'),
+ *                  value])
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_4 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__65, 0, 1, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
+  __pyx_t_3 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__72, 0, 1, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
 
-  /* "pysam/csamfile.pyx":2037
+  /* "pysam/calignmentfile.pyx":2168
  * 
- *     args.extend( [pytag[:2],
- *                   pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                   value ] )
+ *     args.extend([pytag[:2],
+ *                  pytype.encode('ascii'),             # <<<<<<<<<<<<<<
+ *                  value])
  * 
  */
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__66, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__73, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-  /* "pysam/csamfile.pyx":2036
+  /* "pysam/calignmentfile.pyx":2167
  *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
  * 
- *     args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                   pytype.encode('ascii'),
- *                   value ] )
+ *     args.extend([pytag[:2],             # <<<<<<<<<<<<<<
+ *                  pytype.encode('ascii'),
+ *                  value])
  */
-  __pyx_t_6 = PyList_New(3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = PyList_New(3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
-  PyList_SET_ITEM(__pyx_t_6, 0, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
+  PyList_SET_ITEM(__pyx_t_6, 0, __pyx_t_3);
+  __Pyx_GIVEREF(__pyx_t_3);
   PyList_SET_ITEM(__pyx_t_6, 1, __pyx_t_10);
   __Pyx_GIVEREF(__pyx_t_10);
   __Pyx_INCREF(__pyx_v_value);
   PyList_SET_ITEM(__pyx_t_6, 2, __pyx_v_value);
   __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_4 = 0;
+  __pyx_t_3 = 0;
   __pyx_t_10 = 0;
-  __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_6);
   __Pyx_GIVEREF(__pyx_t_6);
   __pyx_t_6 = 0;
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_10, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_10, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-  /* "pysam/csamfile.pyx":2040
- *                   value ] )
+  /* "pysam/calignmentfile.pyx":2171
+ *                  value])
  * 
- *     fmts.append( fmt )             # <<<<<<<<<<<<<<
+ *     fmts.append(fmt)             # <<<<<<<<<<<<<<
  * 
  * ###########################################################
  */
-  __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_fmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2040; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_fmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":1975
+  /* "pysam/calignmentfile.pyx":2096
  *     return type_code
  * 
  * cdef inline convert_python_tag(pytag, value, fmts, args):             # <<<<<<<<<<<<<<
@@ -22019,10 +23218,10 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
   __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
+  __Pyx_XDECREF(__pyx_t_8);
   __Pyx_XDECREF(__pyx_t_9);
   __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_AddTraceback("pysam.csamfile.convert_python_tag", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.convert_python_tag", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_t);
@@ -22031,7 +23230,6 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   __Pyx_XDECREF(__pyx_v_datatype);
   __Pyx_XDECREF(__pyx_v_mi);
   __Pyx_XDECREF(__pyx_v_ma);
-  __Pyx_XDECREF(__pyx_v_absmax);
   __Pyx_XDECREF(__pyx_v_fmt);
   __Pyx_XDECREF(__pyx_v_pytag);
   __Pyx_XDECREF(__pyx_v_value);
@@ -22041,57 +23239,57 @@ static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObj
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2074
+/* "pysam/calignmentfile.pyx":2196
  * 
  *     # Now only called when instances are created from Python
  *     def __init__(self):             # <<<<<<<<<<<<<<
  *         # see bam_init1
- *         self._delegate = <bam1_t*>calloc( 1, sizeof( bam1_t) )
+ *         self._delegate = <bam1_t*>calloc(1, sizeof(bam1_t))
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   if (unlikely(PyTuple_GET_SIZE(__pyx_args) > 0)) {
     __Pyx_RaiseArgtupleInvalid("__init__", 1, 0, 0, PyTuple_GET_SIZE(__pyx_args)); return -1;}
   if (unlikely(__pyx_kwds) && unlikely(PyDict_Size(__pyx_kwds) > 0) && unlikely(!__Pyx_CheckKeywordStrings(__pyx_kwds, "__init__", 0))) return -1;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment___init__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment___init__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 2074);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 2196);
 
-  /* "pysam/csamfile.pyx":2076
+  /* "pysam/calignmentfile.pyx":2198
  *     def __init__(self):
  *         # see bam_init1
- *         self._delegate = <bam1_t*>calloc( 1, sizeof( bam1_t) )             # <<<<<<<<<<<<<<
- *         # allocate some memory
- *         # If size is 0, calloc does not return a pointer that can be passed to free()
+ *         self._delegate = <bam1_t*>calloc(1, sizeof(bam1_t))             # <<<<<<<<<<<<<<
+ *         # allocate some memory. If size is 0, calloc does not return a
+ *         # pointer that can be passed to free() so allocate 40 bytes
  */
   __pyx_v_self->_delegate = ((bam1_t *)calloc(1, (sizeof(bam1_t))));
 
-  /* "pysam/csamfile.pyx":2080
- *         # If size is 0, calloc does not return a pointer that can be passed to free()
- *         # so allocate 40 bytes for a new read
+  /* "pysam/calignmentfile.pyx":2202
+ *         # pointer that can be passed to free() so allocate 40 bytes
+ *         # for a new read
  *         self._delegate.m_data = 40             # <<<<<<<<<<<<<<
  *         self._delegate.data = <uint8_t *>calloc(
  *             self._delegate.m_data, 1)
  */
   __pyx_v_self->_delegate->m_data = 40;
 
-  /* "pysam/csamfile.pyx":2081
- *         # so allocate 40 bytes for a new read
+  /* "pysam/calignmentfile.pyx":2203
+ *         # for a new read
  *         self._delegate.m_data = 40
  *         self._delegate.data = <uint8_t *>calloc(             # <<<<<<<<<<<<<<
  *             self._delegate.m_data, 1)
@@ -22099,7 +23297,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(struct __pyx_obj_5py
  */
   __pyx_v_self->_delegate->data = ((uint8_t *)calloc(__pyx_v_self->_delegate->m_data, 1));
 
-  /* "pysam/csamfile.pyx":2083
+  /* "pysam/calignmentfile.pyx":2205
  *         self._delegate.data = <uint8_t *>calloc(
  *             self._delegate.m_data, 1)
  *         self._delegate.l_data = 0             # <<<<<<<<<<<<<<
@@ -22108,12 +23306,12 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(struct __pyx_obj_5py
  */
   __pyx_v_self->_delegate->l_data = 0;
 
-  /* "pysam/csamfile.pyx":2074
+  /* "pysam/calignmentfile.pyx":2196
  * 
  *     # Now only called when instances are created from Python
  *     def __init__(self):             # <<<<<<<<<<<<<<
  *         # see bam_init1
- *         self._delegate = <bam1_t*>calloc( 1, sizeof( bam1_t) )
+ *         self._delegate = <bam1_t*>calloc(1, sizeof(bam1_t))
  */
 
   /* function exit code */
@@ -22123,7 +23321,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2085
+/* "pysam/calignmentfile.pyx":2207
  *         self._delegate.l_data = 0
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -22132,23 +23330,23 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(struct __pyx_obj_5py
  */
 
 /* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_11AlignedRead_3__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_11AlignedRead_3__dealloc__(PyObject *__pyx_v_self) {
+static void __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3__dealloc__(PyObject *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_11AlignedRead_2__dealloc__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_2__dealloc__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
 }
 
-static void __pyx_pf_5pysam_8csamfile_11AlignedRead_2__dealloc__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static void __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_2__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 2085);
+  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 2207);
 
-  /* "pysam/csamfile.pyx":2086
+  /* "pysam/calignmentfile.pyx":2208
  * 
  *     def __dealloc__(self):
  *         bam_destroy1(self._delegate)             # <<<<<<<<<<<<<<
@@ -22157,7 +23355,7 @@ static void __pyx_pf_5pysam_8csamfile_11AlignedRead_2__dealloc__(struct __pyx_ob
  */
   bam_destroy1(__pyx_v_self->_delegate);
 
-  /* "pysam/csamfile.pyx":2085
+  /* "pysam/calignmentfile.pyx":2207
  *         self._delegate.l_data = 0
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -22170,7 +23368,7 @@ static void __pyx_pf_5pysam_8csamfile_11AlignedRead_2__dealloc__(struct __pyx_ob
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/csamfile.pyx":2088
+/* "pysam/calignmentfile.pyx":2210
  *         bam_destroy1(self._delegate)
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
@@ -22179,30 +23377,28 @@ static void __pyx_pf_5pysam_8csamfile_11AlignedRead_2__dealloc__(struct __pyx_ob
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5__str__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_4__str__[] = "return string representation of alignment.\n\n        The representation is an approximate :term:`sam` format.\n\n        An aligned read might not be associated with a :term:`Samfile`.\n        As a result :term:`tid` is shown instead of the reference name.\n\n        Similarly, the tags field is returned in its parsed state.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5__str__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_4__str__[] = "return string representation of alignment.\n\n        The representation is an approximate :term:`sam` format.\n\n        An aligned read might not be associated with a :term:`AlignmentFile`.\n        As a result :term:`tid` is shown instead of the reference name.\n\n        Similarly, the tags field is returned in its parsed state.\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_11AlignedRead_4__str__;
+struct wrapperbase __pyx_wrapperbase_5pysam_14calignmentfile_14AlignedSegment_4__str__;
 #endif
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5__str__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5__str__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4__str__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_v_seq = NULL;
-  PyObject *__pyx_v_qual = NULL;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4__str__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
+  PyObject *__pyx_t_3 = NULL;
   PyObject *__pyx_t_4 = NULL;
   PyObject *__pyx_t_5 = NULL;
   PyObject *__pyx_t_6 = NULL;
@@ -22212,237 +23408,171 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(struct __pyx_o
   PyObject *__pyx_t_10 = NULL;
   PyObject *__pyx_t_11 = NULL;
   PyObject *__pyx_t_12 = NULL;
+  PyObject *__pyx_t_13 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 2088);
+  __Pyx_TraceCall("__str__", __pyx_f[0], 2210);
 
-  /* "pysam/csamfile.pyx":2100
+  /* "pysam/calignmentfile.pyx":2222
  *         # sam-parsing is done in sam.c/bam_format1_core which
  *         # requires a valid header.
- *         if sys.version_info[0] < 3:             # <<<<<<<<<<<<<<
- *             seq = self.seq
- *             qual = self.qual
+ *         return "\t".join(map(str, (self.query_name,             # <<<<<<<<<<<<<<
+ *                                    self.flag,
+ *                                    self.reference_id,
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_version_info); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_GetItemInt(__pyx_t_2, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_name); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, __pyx_int_3, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":2101
+  /* "pysam/calignmentfile.pyx":2223
  *         # requires a valid header.
- *         if sys.version_info[0] < 3:
- *             seq = self.seq             # <<<<<<<<<<<<<<
- *             qual = self.qual
- *         else:
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_seq); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2101; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_v_seq = __pyx_t_2;
-    __pyx_t_2 = 0;
-
-    /* "pysam/csamfile.pyx":2102
- *         if sys.version_info[0] < 3:
- *             seq = self.seq
- *             qual = self.qual             # <<<<<<<<<<<<<<
- *         else:
- *             seq = self.seq.decode('ascii')
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_qual); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2102; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_v_qual = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L3;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":2104
- *             qual = self.qual
- *         else:
- *             seq = self.seq.decode('ascii')             # <<<<<<<<<<<<<<
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_seq); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_decode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__67, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_v_seq = __pyx_t_2;
-    __pyx_t_2 = 0;
-
-    /* "pysam/csamfile.pyx":2105
- *         else:
- *             seq = self.seq.decode('ascii')
- *             qual = self.qual.decode('ascii')             # <<<<<<<<<<<<<<
- *         return "\t".join(map(str, (self.qname,
- *                                    self.flag,
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_qual); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_decode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__68, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_v_qual = __pyx_t_2;
-    __pyx_t_2 = 0;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":2106
- *             seq = self.seq.decode('ascii')
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,             # <<<<<<<<<<<<<<
- *                                    self.flag,
- *                                    self.rname,
+ *         return "\t".join(map(str, (self.query_name,
+ *                                    self.flag,             # <<<<<<<<<<<<<<
+ *                                    self.reference_id,
+ *                                    self.reference_start,
  */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_qname); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
 
-  /* "pysam/csamfile.pyx":2107
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,
- *                                    self.flag,             # <<<<<<<<<<<<<<
- *                                    self.rname,
- *                                    self.pos,
+  /* "pysam/calignmentfile.pyx":2224
+ *         return "\t".join(map(str, (self.query_name,
+ *                                    self.flag,
+ *                                    self.reference_id,             # <<<<<<<<<<<<<<
+ *                                    self.reference_start,
+ *                                    self.mapping_quality,
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_id); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
 
-  /* "pysam/csamfile.pyx":2108
- *         return "\t".join(map(str, (self.qname,
+  /* "pysam/calignmentfile.pyx":2225
  *                                    self.flag,
- *                                    self.rname,             # <<<<<<<<<<<<<<
- *                                    self.pos,
- *                                    self.mapq,
+ *                                    self.reference_id,
+ *                                    self.reference_start,             # <<<<<<<<<<<<<<
+ *                                    self.mapping_quality,
+ *                                    self.cigarstring,
  */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_rname); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_start); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
 
-  /* "pysam/csamfile.pyx":2109
- *                                    self.flag,
- *                                    self.rname,
- *                                    self.pos,             # <<<<<<<<<<<<<<
- *                                    self.mapq,
- *                                    self.cigar,
+  /* "pysam/calignmentfile.pyx":2226
+ *                                    self.reference_id,
+ *                                    self.reference_start,
+ *                                    self.mapping_quality,             # <<<<<<<<<<<<<<
+ *                                    self.cigarstring,
+ *                                    self.next_reference_id,
  */
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_pos); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2109; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mapping_quality); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
 
-  /* "pysam/csamfile.pyx":2110
- *                                    self.rname,
- *                                    self.pos,
- *                                    self.mapq,             # <<<<<<<<<<<<<<
- *                                    self.cigar,
- *                                    self.mrnm,
+  /* "pysam/calignmentfile.pyx":2227
+ *                                    self.reference_start,
+ *                                    self.mapping_quality,
+ *                                    self.cigarstring,             # <<<<<<<<<<<<<<
+ *                                    self.next_reference_id,
+ *                                    self.next_reference_start,
  */
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mapq); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigarstring); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
 
-  /* "pysam/csamfile.pyx":2111
- *                                    self.pos,
- *                                    self.mapq,
- *                                    self.cigar,             # <<<<<<<<<<<<<<
- *                                    self.mrnm,
- *                                    self.mpos,
+  /* "pysam/calignmentfile.pyx":2228
+ *                                    self.mapping_quality,
+ *                                    self.cigarstring,
+ *                                    self.next_reference_id,             # <<<<<<<<<<<<<<
+ *                                    self.next_reference_start,
+ *                                    self.query_alignment_length,
  */
-  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigar); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2111; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_id); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2228; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
 
-  /* "pysam/csamfile.pyx":2112
- *                                    self.mapq,
- *                                    self.cigar,
- *                                    self.mrnm,             # <<<<<<<<<<<<<<
- *                                    self.mpos,
- *                                    self.rlen,
+  /* "pysam/calignmentfile.pyx":2229
+ *                                    self.cigarstring,
+ *                                    self.next_reference_id,
+ *                                    self.next_reference_start,             # <<<<<<<<<<<<<<
+ *                                    self.query_alignment_length,
+ *                                    self.query_sequence,
  */
-  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mrnm); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_start); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
 
-  /* "pysam/csamfile.pyx":2113
- *                                    self.cigar,
- *                                    self.mrnm,
- *                                    self.mpos,             # <<<<<<<<<<<<<<
- *                                    self.rlen,
- *                                    seq,
+  /* "pysam/calignmentfile.pyx":2230
+ *                                    self.next_reference_id,
+ *                                    self.next_reference_start,
+ *                                    self.query_alignment_length,             # <<<<<<<<<<<<<<
+ *                                    self.query_sequence,
+ *                                    self.query_qualities,
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mpos); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_length); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2230; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
 
-  /* "pysam/csamfile.pyx":2114
- *                                    self.mrnm,
- *                                    self.mpos,
- *                                    self.rlen,             # <<<<<<<<<<<<<<
- *                                    seq,
- *                                    qual,
+  /* "pysam/calignmentfile.pyx":2231
+ *                                    self.next_reference_start,
+ *                                    self.query_alignment_length,
+ *                                    self.query_sequence,             # <<<<<<<<<<<<<<
+ *                                    self.query_qualities,
+ *                                    self.tags)))
  */
-  __pyx_t_10 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_rlen); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2114; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_sequence); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2231; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
 
-  /* "pysam/csamfile.pyx":2117
- *                                    seq,
- *                                    qual,
- *                                    self.tags )))             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2232
+ *                                    self.query_alignment_length,
+ *                                    self.query_sequence,
+ *                                    self.query_qualities,             # <<<<<<<<<<<<<<
+ *                                    self.tags)))
  * 
- *     def compare(self, AlignedRead other):
  */
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_tags); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_qualities); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2232; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
 
-  /* "pysam/csamfile.pyx":2106
- *             seq = self.seq.decode('ascii')
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,             # <<<<<<<<<<<<<<
- *                                    self.flag,
- *                                    self.rname,
+  /* "pysam/calignmentfile.pyx":2233
+ *                                    self.query_sequence,
+ *                                    self.query_qualities,
+ *                                    self.tags)))             # <<<<<<<<<<<<<<
+ * 
+ *     def compare(self, AlignedSegment other):
  */
-  __pyx_t_12 = PyTuple_New(12); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_tags); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_12);
-  PyTuple_SET_ITEM(__pyx_t_12, 0, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_12, 1, __pyx_t_1);
+
+  /* "pysam/calignmentfile.pyx":2222
+ *         # sam-parsing is done in sam.c/bam_format1_core which
+ *         # requires a valid header.
+ *         return "\t".join(map(str, (self.query_name,             # <<<<<<<<<<<<<<
+ *                                    self.flag,
+ *                                    self.reference_id,
+ */
+  __pyx_t_13 = PyTuple_New(12); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_13);
+  PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_12, 2, __pyx_t_4);
+  PyTuple_SET_ITEM(__pyx_t_13, 1, __pyx_t_2);
+  __Pyx_GIVEREF(__pyx_t_2);
+  PyTuple_SET_ITEM(__pyx_t_13, 2, __pyx_t_3);
+  __Pyx_GIVEREF(__pyx_t_3);
+  PyTuple_SET_ITEM(__pyx_t_13, 3, __pyx_t_4);
   __Pyx_GIVEREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_12, 3, __pyx_t_5);
+  PyTuple_SET_ITEM(__pyx_t_13, 4, __pyx_t_5);
   __Pyx_GIVEREF(__pyx_t_5);
-  PyTuple_SET_ITEM(__pyx_t_12, 4, __pyx_t_6);
+  PyTuple_SET_ITEM(__pyx_t_13, 5, __pyx_t_6);
   __Pyx_GIVEREF(__pyx_t_6);
-  PyTuple_SET_ITEM(__pyx_t_12, 5, __pyx_t_7);
+  PyTuple_SET_ITEM(__pyx_t_13, 6, __pyx_t_7);
   __Pyx_GIVEREF(__pyx_t_7);
-  PyTuple_SET_ITEM(__pyx_t_12, 6, __pyx_t_8);
+  PyTuple_SET_ITEM(__pyx_t_13, 7, __pyx_t_8);
   __Pyx_GIVEREF(__pyx_t_8);
-  PyTuple_SET_ITEM(__pyx_t_12, 7, __pyx_t_9);
+  PyTuple_SET_ITEM(__pyx_t_13, 8, __pyx_t_9);
   __Pyx_GIVEREF(__pyx_t_9);
-  PyTuple_SET_ITEM(__pyx_t_12, 8, __pyx_t_10);
+  PyTuple_SET_ITEM(__pyx_t_13, 9, __pyx_t_10);
   __Pyx_GIVEREF(__pyx_t_10);
-  __Pyx_INCREF(__pyx_v_seq);
-  PyTuple_SET_ITEM(__pyx_t_12, 9, __pyx_v_seq);
-  __Pyx_GIVEREF(__pyx_v_seq);
-  __Pyx_INCREF(__pyx_v_qual);
-  PyTuple_SET_ITEM(__pyx_t_12, 10, __pyx_v_qual);
-  __Pyx_GIVEREF(__pyx_v_qual);
-  PyTuple_SET_ITEM(__pyx_t_12, 11, __pyx_t_11);
+  PyTuple_SET_ITEM(__pyx_t_13, 10, __pyx_t_11);
   __Pyx_GIVEREF(__pyx_t_11);
-  __pyx_t_2 = 0;
+  PyTuple_SET_ITEM(__pyx_t_13, 11, __pyx_t_12);
+  __Pyx_GIVEREF(__pyx_t_12);
   __pyx_t_1 = 0;
+  __pyx_t_2 = 0;
+  __pyx_t_3 = 0;
   __pyx_t_4 = 0;
   __pyx_t_5 = 0;
   __pyx_t_6 = 0;
@@ -22451,25 +23581,26 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(struct __pyx_o
   __pyx_t_9 = 0;
   __pyx_t_10 = 0;
   __pyx_t_11 = 0;
-  __pyx_t_11 = PyTuple_New(2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_11);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_11, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_11, 1, __pyx_t_12);
-  __Pyx_GIVEREF(__pyx_t_12);
   __pyx_t_12 = 0;
-  __pyx_t_12 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_11, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = PyTuple_New(2); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_12);
-  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-  __pyx_t_11 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_12); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_11);
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_12, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_12, 1, __pyx_t_13);
+  __Pyx_GIVEREF(__pyx_t_13);
+  __pyx_t_13 = 0;
+  __pyx_t_13 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_12, NULL); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_13);
   __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-  __pyx_r = __pyx_t_11;
-  __pyx_t_11 = 0;
+  __pyx_t_12 = __Pyx_PyString_Join(__pyx_kp_s__43, __pyx_t_13); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_12);
+  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
+  __pyx_r = __pyx_t_12;
+  __pyx_t_12 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2088
+  /* "pysam/calignmentfile.pyx":2210
  *         bam_destroy1(self._delegate)
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
@@ -22481,6 +23612,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(struct __pyx_o
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
   __Pyx_XDECREF(__pyx_t_5);
   __Pyx_XDECREF(__pyx_t_6);
@@ -22490,37 +23622,36 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(struct __pyx_o
   __Pyx_XDECREF(__pyx_t_10);
   __Pyx_XDECREF(__pyx_t_11);
   __Pyx_XDECREF(__pyx_t_12);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_13);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_seq);
-  __Pyx_XDECREF(__pyx_v_qual);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2119
- *                                    self.tags )))
+/* "pysam/calignmentfile.pyx":2235
+ *                                    self.tags)))
  * 
- *     def compare(self, AlignedRead other):             # <<<<<<<<<<<<<<
+ *     def compare(self, AlignedSegment other):             # <<<<<<<<<<<<<<
  *         '''return -1,0,1, if contents in this are binary <,=,> to *other*'''
  * 
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_7compare(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_6compare[] = "AlignedRead.compare(self, AlignedRead other)\nreturn -1,0,1, if contents in this are binary <,=,> to *other*";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_7compare(PyObject *__pyx_v_self, PyObject *__pyx_v_other) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_7compare(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_6compare[] = "AlignedSegment.compare(self, AlignedSegment other)\nreturn -1,0,1, if contents in this are binary <,=,> to *other*";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_7compare(PyObject *__pyx_v_self, PyObject *__pyx_v_other) {
   CYTHON_UNUSED int __pyx_lineno = 0;
   CYTHON_UNUSED const char *__pyx_filename = NULL;
   CYTHON_UNUSED int __pyx_clineno = 0;
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("compare (wrapper)", 0);
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_5pysam_8csamfile_AlignedRead, 1, "other", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2119; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_other));
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_5pysam_14calignmentfile_AlignedSegment, 1, "other", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6compare(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_other));
 
   /* function exit code */
   goto __pyx_L0;
@@ -22531,7 +23662,7 @@ static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_7compare(PyObject *__py
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_other) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6compare(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_other) {
   int __pyx_v_retval;
   bam1_t *__pyx_v_t;
   bam1_t *__pyx_v_o;
@@ -22545,9 +23676,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("compare", 0);
-  __Pyx_TraceCall("compare", __pyx_f[0], 2119);
+  __Pyx_TraceCall("compare", __pyx_f[0], 2235);
 
-  /* "pysam/csamfile.pyx":2126
+  /* "pysam/calignmentfile.pyx":2242
  *         cdef bam1_t *o
  * 
  *         t = self._delegate             # <<<<<<<<<<<<<<
@@ -22557,7 +23688,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_o
   __pyx_t_1 = __pyx_v_self->_delegate;
   __pyx_v_t = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2127
+  /* "pysam/calignmentfile.pyx":2243
  * 
  *         t = self._delegate
  *         o = other._delegate             # <<<<<<<<<<<<<<
@@ -22567,7 +23698,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_o
   __pyx_t_1 = __pyx_v_other->_delegate;
   __pyx_v_o = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2139
+  /* "pysam/calignmentfile.pyx":2255
  * 
  *         # Fast-path test for object identity
  *         if t == o:             # <<<<<<<<<<<<<<
@@ -22577,7 +23708,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_o
   __pyx_t_2 = ((__pyx_v_t == __pyx_v_o) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":2140
+    /* "pysam/calignmentfile.pyx":2256
  *         # Fast-path test for object identity
  *         if t == o:
  *             return 0             # <<<<<<<<<<<<<<
@@ -22590,76 +23721,92 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_o
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":2142
+  /* "pysam/calignmentfile.pyx":2258
  *             return 0
  * 
  *         retval = memcmp(&t.core, &o.core, sizeof(bam1_core_t))             # <<<<<<<<<<<<<<
  * 
- *         if retval: return retval
+ *         if retval:
  */
   __pyx_v_retval = memcmp((&__pyx_v_t->core), (&__pyx_v_o->core), (sizeof(bam1_core_t)));
 
-  /* "pysam/csamfile.pyx":2144
+  /* "pysam/calignmentfile.pyx":2260
  *         retval = memcmp(&t.core, &o.core, sizeof(bam1_core_t))
  * 
- *         if retval: return retval             # <<<<<<<<<<<<<<
- *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)
- *         if retval: return retval
+ *         if retval:             # <<<<<<<<<<<<<<
+ *             return retval
+ *         # cmp(t.l_data, o.l_data)
  */
   __pyx_t_2 = (__pyx_v_retval != 0);
   if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":2261
+ * 
+ *         if retval:
+ *             return retval             # <<<<<<<<<<<<<<
+ *         # cmp(t.l_data, o.l_data)
+ *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data)
+ */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_retval); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_retval); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2261; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_r = __pyx_t_3;
     __pyx_t_3 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":2145
- * 
- *         if retval: return retval
- *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)             # <<<<<<<<<<<<<<
- *         if retval: return retval
- *         return memcmp(t.data, o.data, t.l_data)
+  /* "pysam/calignmentfile.pyx":2263
+ *             return retval
+ *         # cmp(t.l_data, o.l_data)
+ *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data)             # <<<<<<<<<<<<<<
+ *         if retval:
+ *             return retval
  */
   __pyx_v_retval = ((__pyx_v_t->l_data > __pyx_v_o->l_data) - (__pyx_v_t->l_data < __pyx_v_o->l_data));
 
-  /* "pysam/csamfile.pyx":2146
- *         if retval: return retval
- *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)
- *         if retval: return retval             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2264
+ *         # cmp(t.l_data, o.l_data)
+ *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data)
+ *         if retval:             # <<<<<<<<<<<<<<
+ *             return retval
  *         return memcmp(t.data, o.data, t.l_data)
- * 
  */
   __pyx_t_2 = (__pyx_v_retval != 0);
   if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":2265
+ *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data)
+ *         if retval:
+ *             return retval             # <<<<<<<<<<<<<<
+ *         return memcmp(t.data, o.data, t.l_data)
+ * 
+ */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_retval); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2146; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_retval); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2265; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_r = __pyx_t_3;
     __pyx_t_3 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":2147
- *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)
- *         if retval: return retval
+  /* "pysam/calignmentfile.pyx":2266
+ *         if retval:
+ *             return retval
  *         return memcmp(t.data, o.data, t.l_data)             # <<<<<<<<<<<<<<
  * 
  *     # Disabled so long as __cmp__ is a special method
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __Pyx_PyInt_From_int(memcmp(__pyx_v_t->data, __pyx_v_o->data, __pyx_v_t->l_data)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2147; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyInt_From_int(memcmp(__pyx_v_t->data, __pyx_v_o->data, __pyx_v_t->l_data)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __pyx_r = __pyx_t_3;
   __pyx_t_3 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2119
- *                                    self.tags )))
+  /* "pysam/calignmentfile.pyx":2235
+ *                                    self.tags)))
  * 
- *     def compare(self, AlignedRead other):             # <<<<<<<<<<<<<<
+ *     def compare(self, AlignedSegment other):             # <<<<<<<<<<<<<<
  *         '''return -1,0,1, if contents in this are binary <,=,> to *other*'''
  * 
  */
@@ -22667,7 +23814,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_o
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.compare", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.compare", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -22676,7 +23823,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2150
+/* "pysam/calignmentfile.pyx":2269
  * 
  *     # Disabled so long as __cmp__ is a special method
  *     def __hash__(self):             # <<<<<<<<<<<<<<
@@ -22685,36 +23832,36 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_o
  */
 
 /* Python wrapper */
-static Py_hash_t __pyx_pw_5pysam_8csamfile_11AlignedRead_9__hash__(PyObject *__pyx_v_self); /*proto*/
-static Py_hash_t __pyx_pw_5pysam_8csamfile_11AlignedRead_9__hash__(PyObject *__pyx_v_self) {
+static Py_hash_t __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9__hash__(PyObject *__pyx_v_self); /*proto*/
+static Py_hash_t __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9__hash__(PyObject *__pyx_v_self) {
   Py_hash_t __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__hash__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8__hash__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8__hash__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static Py_hash_t __pyx_pf_5pysam_8csamfile_11AlignedRead_8__hash__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static Py_hash_t __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8__hash__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   Py_hash_t __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__hash__", 0);
-  __Pyx_TraceCall("__hash__", __pyx_f[0], 2150);
+  __Pyx_TraceCall("__hash__", __pyx_f[0], 2269);
 
-  /* "pysam/csamfile.pyx":2151
+  /* "pysam/calignmentfile.pyx":2270
  *     # Disabled so long as __cmp__ is a special method
  *     def __hash__(self):
  *         return _Py_HashPointer(<void *>self)             # <<<<<<<<<<<<<<
  * 
- *     def _convert_python_tag(self, pytag, value, fmts, args):
+ * 
  */
   __pyx_r = _Py_HashPointer(((void *)__pyx_v_self));
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2150
+  /* "pysam/calignmentfile.pyx":2269
  * 
  *     # Disabled so long as __cmp__ is a special method
  *     def __hash__(self):             # <<<<<<<<<<<<<<
@@ -22730,1146 +23877,544 @@ static Py_hash_t __pyx_pf_5pysam_8csamfile_11AlignedRead_8__hash__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2153
- *         return _Py_HashPointer(<void *>self)
- * 
- *     def _convert_python_tag(self, pytag, value, fmts, args):             # <<<<<<<<<<<<<<
- * 
- *         if not type(pytag) is bytes:
+/* "pysam/calignmentfile.pyx":2279
+ *     property query_name:
+ *         """the query template name (None if not present)"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             cdef bam1_t * src
+ *             src = self._delegate
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11_convert_python_tag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_10_convert_python_tag[] = "AlignedRead._convert_python_tag(self, pytag, value, fmts, args)";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11_convert_python_tag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_pytag = 0;
-  PyObject *__pyx_v_value = 0;
-  PyObject *__pyx_v_fmts = 0;
-  PyObject *__pyx_v_args = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10query_name_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10query_name_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("_convert_python_tag (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_pytag,&__pyx_n_s_value,&__pyx_n_s_fmts,&__pyx_n_s_args,0};
-    PyObject* values[4] = {0,0,0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_pytag)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("_convert_python_tag", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  2:
-        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_fmts)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("_convert_python_tag", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  3:
-        if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_args)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("_convert_python_tag", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_convert_python_tag") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
-      goto __pyx_L5_argtuple_error;
-    } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-      values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-    }
-    __pyx_v_pytag = values[0];
-    __pyx_v_value = values[1];
-    __pyx_v_fmts = values[2];
-    __pyx_v_args = values[3];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_convert_python_tag", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead._convert_python_tag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_10_convert_python_tag(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), __pyx_v_pytag, __pyx_v_value, __pyx_v_fmts, __pyx_v_args);
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10query_name___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_10_convert_python_tag(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_pytag, PyObject *__pyx_v_value, PyObject *__pyx_v_fmts, PyObject *__pyx_v_args) {
-  PyObject *__pyx_v_t = NULL;
-  PyObject *__pyx_v_pytype = NULL;
-  PyObject *__pyx_v_datafmt = NULL;
-  PyObject *__pyx_v_datatype = NULL;
-  PyObject *__pyx_v_mi = NULL;
-  PyObject *__pyx_v_ma = NULL;
-  PyObject *__pyx_v_absmax = NULL;
-  PyObject *__pyx_v_fmt = NULL;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10query_name___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  bam1_t *__pyx_v_src;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
+  bam1_t *__pyx_t_1;
   int __pyx_t_2;
   PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  Py_ssize_t __pyx_t_8;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  int __pyx_t_11;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_convert_python_tag", 0);
-  __Pyx_TraceCall("_convert_python_tag", __pyx_f[0], 2153);
-  __Pyx_INCREF(__pyx_v_pytag);
-  __Pyx_INCREF(__pyx_v_value);
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2279);
 
-  /* "pysam/csamfile.pyx":2155
- *     def _convert_python_tag(self, pytag, value, fmts, args):
- * 
- *         if not type(pytag) is bytes:             # <<<<<<<<<<<<<<
- *             pytag = pytag.encode('ascii')
- *         t = type(value)
+  /* "pysam/calignmentfile.pyx":2281
+ *         def __get__(self):
+ *             cdef bam1_t * src
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ *             if pysam_get_l_qname(src) == 0:
+ *                 return None
  */
-  __pyx_t_1 = (((PyObject *)Py_TYPE(__pyx_v_pytag)) != ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __pyx_t_2 = (__pyx_t_1 != 0);
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":2282
+ *             cdef bam1_t * src
+ *             src = self._delegate
+ *             if pysam_get_l_qname(src) == 0:             # <<<<<<<<<<<<<<
+ *                 return None
+ *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
+ */
+  __pyx_t_2 = ((pysam_get_l_qname(__pyx_v_src) == 0) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":2156
- * 
- *         if not type(pytag) is bytes:
- *             pytag = pytag.encode('ascii')             # <<<<<<<<<<<<<<
- *         t = type(value)
+    /* "pysam/calignmentfile.pyx":2283
+ *             src = self._delegate
+ *             if pysam_get_l_qname(src) == 0:
+ *                 return None             # <<<<<<<<<<<<<<
+ *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
  * 
  */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytag, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__69, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_DECREF_SET(__pyx_v_pytag, __pyx_t_4);
-    __pyx_t_4 = 0;
-    goto __pyx_L3;
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
   }
-  __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":2157
- *         if not type(pytag) is bytes:
- *             pytag = pytag.encode('ascii')
- *         t = type(value)             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2284
+ *             if pysam_get_l_qname(src) == 0:
+ *                 return None
+ *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))             # <<<<<<<<<<<<<<
  * 
- *         if t is tuple or t is list:
+ *         def __set__(self, qname):
  */
-  __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_value)));
-  __pyx_v_t = ((PyObject*)((PyObject *)Py_TYPE(__pyx_v_value)));
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__charptr_to_str(((char *)pysam_bam_get_qname(__pyx_v_src))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2284; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __pyx_r = __pyx_t_3;
+  __pyx_t_3 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2159
- *         t = type(value)
- * 
- *         if t is tuple or t is list:             # <<<<<<<<<<<<<<
- *             # binary tags - treat separately
- *             pytype = 'B'
+  /* "pysam/calignmentfile.pyx":2279
+ *     property query_name:
+ *         """the query template name (None if not present)"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             cdef bam1_t * src
+ *             src = self._delegate
  */
-  __pyx_t_2 = (__pyx_v_t == ((PyObject*)(&PyTuple_Type)));
-  if (!(__pyx_t_2 != 0)) {
-    __pyx_t_1 = (__pyx_v_t == ((PyObject*)(&PyList_Type)));
-    __pyx_t_5 = (__pyx_t_1 != 0);
-  } else {
-    __pyx_t_5 = (__pyx_t_2 != 0);
-  }
-  if (__pyx_t_5) {
 
-    /* "pysam/csamfile.pyx":2161
- *         if t is tuple or t is list:
- *             # binary tags - treat separately
- *             pytype = 'B'             # <<<<<<<<<<<<<<
- *             # get data type - first value determines type
- *             if type(value[0]) is float:
- */
-    __Pyx_INCREF(__pyx_n_s_B);
-    __pyx_v_pytype = __pyx_n_s_B;
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_name.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-    /* "pysam/csamfile.pyx":2163
- *             pytype = 'B'
- *             # get data type - first value determines type
- *             if type(value[0]) is float:             # <<<<<<<<<<<<<<
- *                 datafmt, datatype = "f", "f"
- *             else:
+/* "pysam/calignmentfile.pyx":2286
+ *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
+ * 
+ *         def __set__(self, qname):             # <<<<<<<<<<<<<<
+ *             if qname is None or len(qname) == 0:
+ *                 return
  */
-    __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_value, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2163; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = (((PyObject *)Py_TYPE(__pyx_t_4)) == ((PyObject *)((PyObject*)(&PyFloat_Type))));
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_2 = (__pyx_t_5 != 0);
-    if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":2164
- *             # get data type - first value determines type
- *             if type(value[0]) is float:
- *                 datafmt, datatype = "f", "f"             # <<<<<<<<<<<<<<
- *             else:
- *                 mi, ma = min(value), max(value)
- */
-      __pyx_t_4 = __pyx_n_s_f;
-      __Pyx_INCREF(__pyx_t_4);
-      __pyx_t_3 = __pyx_n_s_f;
-      __Pyx_INCREF(__pyx_t_3);
-      __pyx_v_datafmt = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_datatype = __pyx_t_3;
-      __pyx_t_3 = 0;
-      goto __pyx_L5;
-    }
-    /*else*/ {
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10query_name_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qname); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10query_name_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qname) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10query_name_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_qname));
 
-      /* "pysam/csamfile.pyx":2166
- *                 datafmt, datatype = "f", "f"
- *             else:
- *                 mi, ma = min(value), max(value)             # <<<<<<<<<<<<<<
- *                 absmax = max( abs(mi), abs(ma) )
- *                 # signed ints
- */
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_INCREF(__pyx_v_value);
-      PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
-      __Pyx_GIVEREF(__pyx_v_value);
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_min, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_INCREF(__pyx_v_value);
-      PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
-      __Pyx_GIVEREF(__pyx_v_value);
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_max, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_mi = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_ma = __pyx_t_6;
-      __pyx_t_6 = 0;
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-      /* "pysam/csamfile.pyx":2167
- *             else:
- *                 mi, ma = min(value), max(value)
- *                 absmax = max( abs(mi), abs(ma) )             # <<<<<<<<<<<<<<
- *                 # signed ints
- *                 if mi < 0:
- */
-      __pyx_t_6 = PyNumber_Absolute(__pyx_v_ma); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_4 = PyNumber_Absolute(__pyx_v_mi); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_6, __pyx_t_4, Py_GT); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      if (__pyx_t_2) {
-        __Pyx_INCREF(__pyx_t_6);
-        __pyx_t_3 = __pyx_t_6;
-      } else {
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_3 = __pyx_t_4;
-      }
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __pyx_t_3;
-      __Pyx_INCREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_absmax = __pyx_t_6;
-      __pyx_t_6 = 0;
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10query_name_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_qname) {
+  bam1_t *__pyx_v_src;
+  int __pyx_v_l;
+  char *__pyx_v_p;
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_t_1;
+  Py_ssize_t __pyx_t_2;
+  int __pyx_t_3;
+  int __pyx_t_4;
+  PyObject *__pyx_t_5 = NULL;
+  bam1_t *__pyx_t_6;
+  char const *__pyx_t_7;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2286);
+  __Pyx_INCREF(__pyx_v_qname);
 
-      /* "pysam/csamfile.pyx":2169
- *                 absmax = max( abs(mi), abs(ma) )
- *                 # signed ints
- *                 if mi < 0:             # <<<<<<<<<<<<<<
- *                     if mi >= -127: datafmt, datatype = "b", 'c'
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'
+  /* "pysam/calignmentfile.pyx":2287
+ * 
+ *         def __set__(self, qname):
+ *             if qname is None or len(qname) == 0:             # <<<<<<<<<<<<<<
+ *                 return
+ *             qname = _forceBytes(qname)
  */
-      __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      if (__pyx_t_2) {
+  __pyx_t_1 = (__pyx_v_qname == Py_None);
+  if (!(__pyx_t_1 != 0)) {
+    __pyx_t_2 = PyObject_Length(__pyx_v_qname); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2287; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = ((__pyx_t_2 == 0) != 0);
+    __pyx_t_4 = __pyx_t_3;
+  } else {
+    __pyx_t_4 = (__pyx_t_1 != 0);
+  }
+  if (__pyx_t_4) {
 
-        /* "pysam/csamfile.pyx":2170
- *                 # signed ints
- *                 if mi < 0:
- *                     if mi >= -127: datafmt, datatype = "b", 'c'             # <<<<<<<<<<<<<<
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'
- *                     elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
+    /* "pysam/calignmentfile.pyx":2288
+ *         def __set__(self, qname):
+ *             if qname is None or len(qname) == 0:
+ *                 return             # <<<<<<<<<<<<<<
+ *             qname = _forceBytes(qname)
+ *             cdef bam1_t * src
  */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_127, Py_GE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2170; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2170; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_6 = __pyx_n_s_b;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_c;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_v_datafmt = __pyx_t_6;
-          __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
-          goto __pyx_L7;
-        }
+    __pyx_r = 0;
+    goto __pyx_L0;
+  }
 
-        /* "pysam/csamfile.pyx":2171
- *                 if mi < 0:
- *                     if mi >= -127: datafmt, datatype = "b", 'c'
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'             # <<<<<<<<<<<<<<
- *                     elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                     else: datafmt, datatype = "i", 'i'
+  /* "pysam/calignmentfile.pyx":2289
+ *             if qname is None or len(qname) == 0:
+ *                 return
+ *             qname = _forceBytes(qname)             # <<<<<<<<<<<<<<
+ *             cdef bam1_t * src
+ *             cdef int l
  */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_32767, Py_GE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __pyx_n_s_h;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_t_6 = __pyx_n_s_s_2;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
-          __pyx_v_datatype = __pyx_t_6;
-          __pyx_t_6 = 0;
-          goto __pyx_L7;
-        }
+  __pyx_t_5 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_qname); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2289; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_5);
+  __Pyx_DECREF_SET(__pyx_v_qname, __pyx_t_5);
+  __pyx_t_5 = 0;
 
-        /* "pysam/csamfile.pyx":2172
- *                     if mi >= -127: datafmt, datatype = "b", 'c'
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'
- *                     elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                     else: datafmt, datatype = "i", 'i'
+  /* "pysam/calignmentfile.pyx":2294
+ *             cdef char * p
+ * 
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ *             p = pysam_bam_get_qname(src)
  * 
  */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_6);
-          __Pyx_GIVEREF(__pyx_t_6);
-          __pyx_t_6 = 0;
-          __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-          __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        /*else*/ {
+  __pyx_t_6 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_6;
 
-          /* "pysam/csamfile.pyx":2173
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'
- *                     elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                     else: datafmt, datatype = "i", 'i'             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2295
  * 
- *                 # unsigned ints
+ *             src = self._delegate
+ *             p = pysam_bam_get_qname(src)             # <<<<<<<<<<<<<<
+ * 
+ *             # the qname is \0 terminated
  */
-          __pyx_t_6 = __pyx_n_s_i;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_i;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_v_datafmt = __pyx_t_6;
-          __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
-        }
-        __pyx_L7:;
-        goto __pyx_L6;
-      }
-      /*else*/ {
+  __pyx_v_p = pysam_bam_get_qname(__pyx_v_src);
 
-        /* "pysam/csamfile.pyx":2177
- *                 # unsigned ints
- *                 else:
- *                     if absmax <= 255: datafmt, datatype = "B", 'C'             # <<<<<<<<<<<<<<
- *                     elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                     elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
+  /* "pysam/calignmentfile.pyx":2298
+ * 
+ *             # the qname is \0 terminated
+ *             l = len(qname) + 1             # <<<<<<<<<<<<<<
+ *             pysam_bam_update(src,
+ *                              pysam_get_l_qname(src),
  */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_255, Py_LE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __pyx_n_s_B;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_t_6 = __pyx_n_s_C;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
-          __pyx_v_datatype = __pyx_t_6;
-          __pyx_t_6 = 0;
-          goto __pyx_L8;
-        }
+  __pyx_t_2 = PyObject_Length(__pyx_v_qname); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2298; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_l = (__pyx_t_2 + 1);
 
-        /* "pysam/csamfile.pyx":2178
- *                 else:
- *                     if absmax <= 255: datafmt, datatype = "B", 'C'
- *                     elif absmax <= 65535: datafmt, datatype = "H", 'S'             # <<<<<<<<<<<<<<
- *                     elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                     else: datafmt, datatype = "I", 'I'
+  /* "pysam/calignmentfile.pyx":2299
+ *             # the qname is \0 terminated
+ *             l = len(qname) + 1
+ *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
+ *                              pysam_get_l_qname(src),
+ *                              l,
  */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_65535, Py_LE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_6 = __pyx_n_s_H;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_S;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_v_datafmt = __pyx_t_6;
-          __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
-          goto __pyx_L8;
-        }
+  pysam_bam_update(__pyx_v_src, pysam_get_l_qname(__pyx_v_src), __pyx_v_l, ((uint8_t *)__pyx_v_p));
 
-        /* "pysam/csamfile.pyx":2179
- *                     if absmax <= 255: datafmt, datatype = "B", 'C'
- *                     elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                     elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                     else: datafmt, datatype = "I", 'I'
+  /* "pysam/calignmentfile.pyx":2305
+ * 
  * 
+ *             pysam_set_l_qname(src, l)             # <<<<<<<<<<<<<<
+ * 
+ *             # re-acquire pointer to location in memory
  */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_4294967295, Py_GT); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_3);
-          __Pyx_GIVEREF(__pyx_t_3);
-          __pyx_t_3 = 0;
-          __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          __Pyx_Raise(__pyx_t_3, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        /*else*/ {
+  pysam_set_l_qname(__pyx_v_src, __pyx_v_l);
 
-          /* "pysam/csamfile.pyx":2180
- *                     elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                     elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                     else: datafmt, datatype = "I", 'I'             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2309
+ *             # re-acquire pointer to location in memory
+ *             # as it might have moved
+ *             p = pysam_bam_get_qname(src)             # <<<<<<<<<<<<<<
  * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
+ *             strncpy(p, qname, l)
  */
-          __pyx_t_3 = __pyx_n_s_I;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_t_6 = __pyx_n_s_I;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
-          __pyx_v_datatype = __pyx_t_6;
-          __pyx_t_6 = 0;
-        }
-        __pyx_L8:;
-      }
-      __pyx_L6:;
-    }
-    __pyx_L5:;
+  __pyx_v_p = pysam_bam_get_qname(__pyx_v_src);
 
-    /* "pysam/csamfile.pyx":2182
- *                     else: datafmt, datatype = "I", 'I'
+  /* "pysam/calignmentfile.pyx":2311
+ *             p = pysam_bam_get_qname(src)
+ * 
+ *             strncpy(p, qname, l)             # <<<<<<<<<<<<<<
  * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)             # <<<<<<<<<<<<<<
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),
+ *     property flag:
  */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = PyInt_FromSsize_t(__pyx_t_8); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_6);
-    __Pyx_GIVEREF(__pyx_t_6);
-    __Pyx_INCREF(__pyx_v_datafmt);
-    PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_v_datafmt);
-    __Pyx_GIVEREF(__pyx_v_datafmt);
-    __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_2sccI_i_s, __pyx_t_3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_DECREF_SET(__pyx_v_datafmt, __pyx_t_6);
-    __pyx_t_6 = 0;
+  __pyx_t_7 = __Pyx_PyObject_AsString(__pyx_v_qname); if (unlikely((!__pyx_t_7) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2311; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  strncpy(__pyx_v_p, __pyx_t_7, __pyx_v_l);
 
-    /* "pysam/csamfile.pyx":2183
+  /* "pysam/calignmentfile.pyx":2286
+ *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
  * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
+ *         def __set__(self, qname):             # <<<<<<<<<<<<<<
+ *             if qname is None or len(qname) == 0:
+ *                 return
  */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__70, 0, 1, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
 
-    /* "pysam/csamfile.pyx":2184
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )
- */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_tuple__71, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_5);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_name.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_qname);
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-    /* "pysam/csamfile.pyx":2185
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                           len(value)] + list(value) )
- *             fmts.append( datafmt )
+/* "pysam/calignmentfile.pyx":2315
+ *     property flag:
+ *         """properties flag"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return pysam_get_flag(self._delegate)
+ *         def __set__(self, flag):
  */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_datatype, __pyx_n_s_encode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_tuple__72, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-    /* "pysam/csamfile.pyx":2186
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )             # <<<<<<<<<<<<<<
- *             fmts.append( datafmt )
- *             return
- */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = PyInt_FromSsize_t(__pyx_t_8); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4flag_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4flag_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4flag___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
-    /* "pysam/csamfile.pyx":2183
- * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
- */
-    __pyx_t_10 = PyList_New(4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    PyList_SET_ITEM(__pyx_t_10, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    PyList_SET_ITEM(__pyx_t_10, 1, __pyx_t_7);
-    __Pyx_GIVEREF(__pyx_t_7);
-    PyList_SET_ITEM(__pyx_t_10, 2, __pyx_t_9);
-    __Pyx_GIVEREF(__pyx_t_9);
-    PyList_SET_ITEM(__pyx_t_10, 3, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_3 = 0;
-    __pyx_t_7 = 0;
-    __pyx_t_9 = 0;
-    __pyx_t_4 = 0;
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-    /* "pysam/csamfile.pyx":2186
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )             # <<<<<<<<<<<<<<
- *             fmts.append( datafmt )
- *             return
- */
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_INCREF(__pyx_v_value);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_value);
-    __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_9 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_4, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = PyNumber_Add(__pyx_t_10, __pyx_t_9); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4flag___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2315);
 
-    /* "pysam/csamfile.pyx":2183
- * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
+  /* "pysam/calignmentfile.pyx":2316
+ *         """properties flag"""
+ *         def __get__(self):
+ *             return pysam_get_flag(self._delegate)             # <<<<<<<<<<<<<<
+ *         def __set__(self, flag):
+ *             pysam_set_flag(self._delegate, flag)
  */
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyInt_From_uint16_t(pysam_get_flag(__pyx_v_self->_delegate)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-    /* "pysam/csamfile.pyx":2187
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )
- *             fmts.append( datafmt )             # <<<<<<<<<<<<<<
- *             return
- * 
+  /* "pysam/calignmentfile.pyx":2315
+ *     property flag:
+ *         """properties flag"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return pysam_get_flag(self._delegate)
+ *         def __set__(self, flag):
  */
-    __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_datafmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":2188
- *                           len(value)] + list(value) )
- *             fmts.append( datafmt )
- *             return             # <<<<<<<<<<<<<<
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.flag.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2317
+ *         def __get__(self):
+ *             return pysam_get_flag(self._delegate)
+ *         def __set__(self, flag):             # <<<<<<<<<<<<<<
+ *             pysam_set_flag(self._delegate, flag)
  * 
- *         if t is float:
  */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-    goto __pyx_L0;
-  }
 
-  /* "pysam/csamfile.pyx":2190
- *             return
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4flag_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_flag); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4flag_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_flag) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4flag_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_flag));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4flag_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_flag) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  uint16_t __pyx_t_1;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2317);
+
+  /* "pysam/calignmentfile.pyx":2318
+ *             return pysam_get_flag(self._delegate)
+ *         def __set__(self, flag):
+ *             pysam_set_flag(self._delegate, flag)             # <<<<<<<<<<<<<<
  * 
- *         if t is float:             # <<<<<<<<<<<<<<
- *             fmt, pytype = "2scf", 'f'
- *         elif t is int:
+ *     property reference_id:
  */
-  __pyx_t_2 = (__pyx_v_t == ((PyObject*)(&PyFloat_Type)));
-  __pyx_t_5 = (__pyx_t_2 != 0);
-  if (__pyx_t_5) {
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_flag); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_set_flag(__pyx_v_self->_delegate, __pyx_t_1);
 
-    /* "pysam/csamfile.pyx":2191
+  /* "pysam/calignmentfile.pyx":2317
+ *         def __get__(self):
+ *             return pysam_get_flag(self._delegate)
+ *         def __set__(self, flag):             # <<<<<<<<<<<<<<
+ *             pysam_set_flag(self._delegate, flag)
  * 
- *         if t is float:
- *             fmt, pytype = "2scf", 'f'             # <<<<<<<<<<<<<<
- *         elif t is int:
- *             # negative values
  */
-    __pyx_t_4 = __pyx_kp_s_2scf;
-    __Pyx_INCREF(__pyx_t_4);
-    __pyx_t_9 = __pyx_n_s_f;
-    __Pyx_INCREF(__pyx_t_9);
-    __pyx_v_fmt = __pyx_t_4;
-    __pyx_t_4 = 0;
-    __pyx_v_pytype = __pyx_t_9;
-    __pyx_t_9 = 0;
-    goto __pyx_L9;
-  }
-
-  /* "pysam/csamfile.pyx":2192
- *         if t is float:
- *             fmt, pytype = "2scf", 'f'
- *         elif t is int:             # <<<<<<<<<<<<<<
- *             # negative values
- *             if value < 0:
- */
-  __pyx_t_5 = (__pyx_v_t == ((PyObject*)(&PyInt_Type)));
-  __pyx_t_2 = (__pyx_t_5 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2194
- *         elif t is int:
- *             # negative values
- *             if value < 0:             # <<<<<<<<<<<<<<
- *                 if value >= -127: fmt, pytype = "2scb", 'c'
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'
- */
-    __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":2195
- *             # negative values
- *             if value < 0:
- *                 if value >= -127: fmt, pytype = "2scb", 'c'             # <<<<<<<<<<<<<<
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'
- *                 elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_127, Py_GE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2195; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2195; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __pyx_kp_s_2scb;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_c;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_v_fmt = __pyx_t_9;
-        __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
-        goto __pyx_L11;
-      }
-
-      /* "pysam/csamfile.pyx":2196
- *             if value < 0:
- *                 if value >= -127: fmt, pytype = "2scb", 'c'
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'             # <<<<<<<<<<<<<<
- *                 elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: fmt, pytype = "2sci", 'i'
- */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_32767, Py_GE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_4 = __pyx_kp_s_2sch;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_9 = __pyx_n_s_s_2;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
-        __pyx_v_pytype = __pyx_t_9;
-        __pyx_t_9 = 0;
-        goto __pyx_L11;
-      }
-
-      /* "pysam/csamfile.pyx":2197
- *                 if value >= -127: fmt, pytype = "2scb", 'c'
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'
- *                 elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                 else: fmt, pytype = "2sci", 'i'
- *             # positive values
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_9);
-        __Pyx_GIVEREF(__pyx_t_9);
-        __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-        __Pyx_Raise(__pyx_t_9, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":2198
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'
- *                 elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: fmt, pytype = "2sci", 'i'             # <<<<<<<<<<<<<<
- *             # positive values
- *             else:
- */
-        __pyx_t_9 = __pyx_kp_s_2sci;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_i;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_v_fmt = __pyx_t_9;
-        __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
-      }
-      __pyx_L11:;
-      goto __pyx_L10;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":2201
- *             # positive values
- *             else:
- *                 if value <= 255: fmt, pytype = "2scB", 'C'             # <<<<<<<<<<<<<<
- *                 elif value <= 65535: fmt, pytype = "2scH", 'S'
- *                 elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_255, Py_LE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_4 = __pyx_kp_s_2scB;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_9 = __pyx_n_s_C;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
-        __pyx_v_pytype = __pyx_t_9;
-        __pyx_t_9 = 0;
-        goto __pyx_L12;
-      }
-
-      /* "pysam/csamfile.pyx":2202
- *             else:
- *                 if value <= 255: fmt, pytype = "2scB", 'C'
- *                 elif value <= 65535: fmt, pytype = "2scH", 'S'             # <<<<<<<<<<<<<<
- *                 elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: fmt, pytype = "2scI", 'I'
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_65535, Py_LE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __pyx_kp_s_2scH;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_S;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_v_fmt = __pyx_t_9;
-        __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
-        goto __pyx_L12;
-      }
-
-      /* "pysam/csamfile.pyx":2203
- *                 if value <= 255: fmt, pytype = "2scB", 'C'
- *                 elif value <= 65535: fmt, pytype = "2scH", 'S'
- *                 elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                 else: fmt, pytype = "2scI", 'I'
- *         else:
- */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_4294967295, Py_GT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_4);
-        __Pyx_GIVEREF(__pyx_t_4);
-        __pyx_t_4 = 0;
-        __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __Pyx_Raise(__pyx_t_4, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":2204
- *                 elif value <= 65535: fmt, pytype = "2scH", 'S'
- *                 elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: fmt, pytype = "2scI", 'I'             # <<<<<<<<<<<<<<
- *         else:
- *             # Note: hex strings (H) are not supported yet
- */
-        __pyx_t_4 = __pyx_kp_s_2scI;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_9 = __pyx_n_s_I;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
-        __pyx_v_pytype = __pyx_t_9;
-        __pyx_t_9 = 0;
-      }
-      __pyx_L12:;
-    }
-    __pyx_L10:;
-    goto __pyx_L9;
-  }
-  /*else*/ {
 
-    /* "pysam/csamfile.pyx":2207
- *         else:
- *             # Note: hex strings (H) are not supported yet
- *             if t is not bytes:             # <<<<<<<<<<<<<<
- *                 value = value.encode('ascii')
- *             if len(value) == 1:
- */
-    __pyx_t_2 = (__pyx_v_t != ((PyObject*)(&PyBytes_Type)));
-    __pyx_t_5 = (__pyx_t_2 != 0);
-    if (__pyx_t_5) {
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.flag.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-      /* "pysam/csamfile.pyx":2208
- *             # Note: hex strings (H) are not supported yet
- *             if t is not bytes:
- *                 value = value.encode('ascii')             # <<<<<<<<<<<<<<
- *             if len(value) == 1:
- *                 fmt, pytype = "2scc", 'A'
+/* "pysam/calignmentfile.pyx":2331
+ * 
+ *         """
+ *         def __get__(self): return self._delegate.core.tid             # <<<<<<<<<<<<<<
+ *         def __set__(self, tid): self._delegate.core.tid = tid
+ * 
  */
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_value, __pyx_n_s_encode); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__73, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_4);
-      __pyx_t_4 = 0;
-      goto __pyx_L13;
-    }
-    __pyx_L13:;
 
-    /* "pysam/csamfile.pyx":2209
- *             if t is not bytes:
- *                 value = value.encode('ascii')
- *             if len(value) == 1:             # <<<<<<<<<<<<<<
- *                 fmt, pytype = "2scc", 'A'
- *             else:
- */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = ((__pyx_t_8 == 1) != 0);
-    if (__pyx_t_5) {
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12reference_id_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12reference_id_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12reference_id___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
-      /* "pysam/csamfile.pyx":2210
- *                 value = value.encode('ascii')
- *             if len(value) == 1:
- *                 fmt, pytype = "2scc", 'A'             # <<<<<<<<<<<<<<
- *             else:
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- */
-      __pyx_t_4 = __pyx_kp_s_2scc;
-      __Pyx_INCREF(__pyx_t_4);
-      __pyx_t_9 = __pyx_n_s_A;
-      __Pyx_INCREF(__pyx_t_9);
-      __pyx_v_fmt = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_pytype = __pyx_t_9;
-      __pyx_t_9 = 0;
-      goto __pyx_L14;
-    }
-    /*else*/ {
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-      /* "pysam/csamfile.pyx":2212
- *                 fmt, pytype = "2scc", 'A'
- *             else:
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'             # <<<<<<<<<<<<<<
- * 
- *         args.extend( [pytag[:2],
- */
-      __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_9 = PyInt_FromSsize_t((__pyx_t_8 + 1)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_2sc_is, __pyx_t_9); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_9 = __pyx_n_s_Z;
-      __Pyx_INCREF(__pyx_t_9);
-      __pyx_v_fmt = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_pytype = __pyx_t_9;
-      __pyx_t_9 = 0;
-    }
-    __pyx_L14:;
-  }
-  __pyx_L9:;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12reference_id___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2331);
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2214
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- * 
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       value ] )
- */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_4 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__74, 0, 1, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.reference_id.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":2215
- * 
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       value ] )
+/* "pysam/calignmentfile.pyx":2332
+ *         """
+ *         def __get__(self): return self._delegate.core.tid
+ *         def __set__(self, tid): self._delegate.core.tid = tid             # <<<<<<<<<<<<<<
  * 
+ *     property reference_start:
  */
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__75, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_10);
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-  /* "pysam/csamfile.pyx":2214
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- * 
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       value ] )
- */
-  __pyx_t_6 = PyList_New(3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  PyList_SET_ITEM(__pyx_t_6, 0, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  PyList_SET_ITEM(__pyx_t_6, 1, __pyx_t_10);
-  __Pyx_GIVEREF(__pyx_t_10);
-  __Pyx_INCREF(__pyx_v_value);
-  PyList_SET_ITEM(__pyx_t_6, 2, __pyx_v_value);
-  __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_4 = 0;
-  __pyx_t_10 = 0;
-  __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_10);
-  PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_6);
-  __Pyx_GIVEREF(__pyx_t_6);
-  __pyx_t_6 = 0;
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_10, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12reference_id_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12reference_id_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12reference_id_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_tid));
 
-  /* "pysam/csamfile.pyx":2218
- *                       value ] )
- * 
- *         fmts.append( fmt )             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_fmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2218; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":2153
- *         return _Py_HashPointer(<void *>self)
- * 
- *     def _convert_python_tag(self, pytag, value, fmts, args):             # <<<<<<<<<<<<<<
- * 
- *         if not type(pytag) is bytes:
- */
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12reference_id_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_tid) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int32_t __pyx_t_1;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2332);
+  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_tid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_self->_delegate->core.tid = __pyx_t_1;
 
   /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead._convert_python_tag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.reference_id.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_t);
-  __Pyx_XDECREF(__pyx_v_pytype);
-  __Pyx_XDECREF(__pyx_v_datafmt);
-  __Pyx_XDECREF(__pyx_v_datatype);
-  __Pyx_XDECREF(__pyx_v_mi);
-  __Pyx_XDECREF(__pyx_v_ma);
-  __Pyx_XDECREF(__pyx_v_absmax);
-  __Pyx_XDECREF(__pyx_v_fmt);
-  __Pyx_XDECREF(__pyx_v_pytag);
-  __Pyx_XDECREF(__pyx_v_value);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2227
- *     property qname:
- *         """the query name (None if not present)"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
+/* "pysam/calignmentfile.pyx":2336
+ *     property reference_start:
+ *         """0-based leftmost coordinate"""
+ *         def __get__(self): return self._delegate.core.pos             # <<<<<<<<<<<<<<
+ *         def __set__(self, pos):
+ *             ## setting the position requires updating the "bin" attribute
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15reference_start_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15reference_start_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5qname___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15reference_start___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5qname___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15reference_start___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_1 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2227);
-
-  /* "pysam/csamfile.pyx":2229
- *         def __get__(self):
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_l_qname(src) == 0:
- *                 return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2230
- *             cdef bam1_t * src
- *             src = self._delegate
- *             if pysam_get_l_qname(src) == 0:             # <<<<<<<<<<<<<<
- *                 return None
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
- */
-  __pyx_t_2 = ((pysam_get_l_qname(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2231
- *             src = self._delegate
- *             if pysam_get_l_qname(src) == 0:
- *                 return None             # <<<<<<<<<<<<<<
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
- * 
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2232
- *             if pysam_get_l_qname(src) == 0:
- *                 return None
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))             # <<<<<<<<<<<<<<
- * 
- *         def __set__(self, qname ):
- */
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2336);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile__charptr_to_str(((char *)pysam_bam_get_qname(__pyx_v_src))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2232; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
+  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2336; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2227
- *     property qname:
- *         """the query name (None if not present)"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qname.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.reference_start.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -23878,681 +24423,264 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5qname___get__(struct _
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2234
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
- * 
- *         def __set__(self, qname ):             # <<<<<<<<<<<<<<
- *             if qname == None or len(qname) == 0: return
- *             qname = _forceBytes(qname)
+/* "pysam/calignmentfile.pyx":2337
+ *         """0-based leftmost coordinate"""
+ *         def __get__(self): return self._delegate.core.pos
+ *         def __set__(self, pos):             # <<<<<<<<<<<<<<
+ *             ## setting the position requires updating the "bin" attribute
+ *             cdef bam1_t * src
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qname); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qname) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15reference_start_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_pos); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15reference_start_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_pos) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5qname_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_qname));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15reference_start_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_pos));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5qname_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qname) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15reference_start_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_pos) {
   bam1_t *__pyx_v_src;
-  int __pyx_v_l;
-  char *__pyx_v_p;
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_t_2;
-  Py_ssize_t __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  bam1_t *__pyx_t_6;
-  char const *__pyx_t_7;
+  bam1_t *__pyx_t_1;
+  int32_t __pyx_t_2;
+  int __pyx_t_3;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2234);
-  __Pyx_INCREF(__pyx_v_qname);
-
-  /* "pysam/csamfile.pyx":2235
- * 
- *         def __set__(self, qname ):
- *             if qname == None or len(qname) == 0: return             # <<<<<<<<<<<<<<
- *             qname = _forceBytes(qname)
- *             cdef bam1_t * src
- */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_qname, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (!__pyx_t_2) {
-    __pyx_t_3 = PyObject_Length(__pyx_v_qname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = (__pyx_t_3 == 0);
-    __pyx_t_5 = __pyx_t_4;
-  } else {
-    __pyx_t_5 = __pyx_t_2;
-  }
-  if (__pyx_t_5) {
-    __pyx_r = 0;
-    goto __pyx_L0;
-  }
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2337);
 
-  /* "pysam/csamfile.pyx":2236
- *         def __set__(self, qname ):
- *             if qname == None or len(qname) == 0: return
- *             qname = _forceBytes(qname)             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2340
+ *             ## setting the position requires updating the "bin" attribute
  *             cdef bam1_t * src
- *             cdef int l
- */
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_qname); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF_SET(__pyx_v_qname, __pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":2241
- *             cdef char * p
- * 
  *             src = self._delegate             # <<<<<<<<<<<<<<
- *             p = pysam_bam_get_qname( src )
- * 
+ *             src.core.pos = pos
+ *             if pysam_get_n_cigar(src):
  */
-  __pyx_t_6 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_6;
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2242
- * 
+  /* "pysam/calignmentfile.pyx":2341
+ *             cdef bam1_t * src
  *             src = self._delegate
- *             p = pysam_bam_get_qname( src )             # <<<<<<<<<<<<<<
- * 
- *             # the qname is \0 terminated
- */
-  __pyx_v_p = pysam_bam_get_qname(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2245
- * 
- *             # the qname is \0 terminated
- *             l = len(qname) + 1             # <<<<<<<<<<<<<<
- *             pysam_bam_update(src,
- *                              pysam_get_l_qname(src),
- */
-  __pyx_t_3 = PyObject_Length(__pyx_v_qname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_l = (__pyx_t_3 + 1);
-
-  /* "pysam/csamfile.pyx":2246
- *             # the qname is \0 terminated
- *             l = len(qname) + 1
- *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
- *                              pysam_get_l_qname(src),
- *                              l,
+ *             src.core.pos = pos             # <<<<<<<<<<<<<<
+ *             if pysam_get_n_cigar(src):
+ *                 pysam_set_bin(src,
  */
-  pysam_bam_update(__pyx_v_src, pysam_get_l_qname(__pyx_v_src), __pyx_v_l, ((uint8_t *)__pyx_v_p));
+  __pyx_t_2 = __Pyx_PyInt_As_int32_t(__pyx_v_pos); if (unlikely((__pyx_t_2 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_src->core.pos = __pyx_t_2;
 
-  /* "pysam/csamfile.pyx":2252
- * 
- * 
- *             pysam_set_l_qname(src, l)             # <<<<<<<<<<<<<<
- * 
- *             # re-acquire pointer to location in memory
+  /* "pysam/calignmentfile.pyx":2342
+ *             src = self._delegate
+ *             src.core.pos = pos
+ *             if pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
+ *                 pysam_set_bin(src,
+ *                               hts_reg2bin(
  */
-  pysam_set_l_qname(__pyx_v_src, __pyx_v_l);
+  __pyx_t_3 = (pysam_get_n_cigar(__pyx_v_src) != 0);
+  if (__pyx_t_3) {
 
-  /* "pysam/csamfile.pyx":2256
- *             # re-acquire pointer to location in memory
- *             # as it might have moved
- *             p = pysam_bam_get_qname(src)             # <<<<<<<<<<<<<<
- * 
- *             strncpy(p, qname, l)
+    /* "pysam/calignmentfile.pyx":2343
+ *             src.core.pos = pos
+ *             if pysam_get_n_cigar(src):
+ *                 pysam_set_bin(src,             # <<<<<<<<<<<<<<
+ *                               hts_reg2bin(
+ *                                   src.core.pos,
  */
-  __pyx_v_p = pysam_bam_get_qname(__pyx_v_src);
+    pysam_set_bin(__pyx_v_src, hts_reg2bin(__pyx_v_src->core.pos, bam_endpos(__pyx_v_src), 14, 5));
+    goto __pyx_L3;
+  }
+  /*else*/ {
 
-  /* "pysam/csamfile.pyx":2258
- *             p = pysam_bam_get_qname(src)
- * 
- *             strncpy(p, qname, l)             # <<<<<<<<<<<<<<
- * 
- *     property cigar:
+    /* "pysam/calignmentfile.pyx":2350
+ *                                   5))
+ *             else:
+ *                 pysam_set_bin(src,             # <<<<<<<<<<<<<<
+ *                               hts_reg2bin(
+ *                                   src.core.pos,
  */
-  __pyx_t_7 = __Pyx_PyObject_AsString(__pyx_v_qname); if (unlikely((!__pyx_t_7) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  strncpy(__pyx_v_p, __pyx_t_7, __pyx_v_l);
+    pysam_set_bin(__pyx_v_src, hts_reg2bin(__pyx_v_src->core.pos, (__pyx_v_src->core.pos + 1), 14, 5));
+  }
+  __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":2234
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
- * 
- *         def __set__(self, qname ):             # <<<<<<<<<<<<<<
- *             if qname == None or len(qname) == 0: return
- *             qname = _forceBytes(qname)
+  /* "pysam/calignmentfile.pyx":2337
+ *         """0-based leftmost coordinate"""
+ *         def __get__(self): return self._delegate.core.pos
+ *         def __set__(self, pos):             # <<<<<<<<<<<<<<
+ *             ## setting the position requires updating the "bin" attribute
+ *             cdef bam1_t * src
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qname.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.reference_start.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_qname);
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2299
- *         or None.
- *         """
+/* "pysam/calignmentfile.pyx":2359
+ *     property mapping_quality:
+ *         """mapping quality"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t * cigar_p
- *             cdef bam1_t * src
+ *             return pysam_get_qual(self._delegate)
+ *         def __set__(self, qual):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_op = 0;
-  PyObject *__pyx_v_l = 0;
-  PyObject *__pyx_v_cigar = 0;
-  int __pyx_v_k;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  bam1_t *__pyx_t_2;
-  int __pyx_t_3;
-  uint16_t __pyx_t_4;
-  int __pyx_t_5;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2299);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2359);
 
-  /* "pysam/csamfile.pyx":2304
- *             cdef op, l, cigar
- *             cdef int k
- *             cigar = []             # <<<<<<<<<<<<<<
- * 
- *             src = self._delegate
+  /* "pysam/calignmentfile.pyx":2360
+ *         """mapping quality"""
+ *         def __get__(self):
+ *             return pysam_get_qual(self._delegate)             # <<<<<<<<<<<<<<
+ *         def __set__(self, qual):
+ *             pysam_set_qual(self._delegate, qual)
  */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyInt_From_uint8_t(pysam_get_qual(__pyx_v_self->_delegate)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_cigar = __pyx_t_1;
+  __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2306
- *             cigar = []
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src) == 0:
- *                 return cigar
- */
-  __pyx_t_2 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_2;
-
-  /* "pysam/csamfile.pyx":2307
- * 
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return cigar
- * 
- */
-  __pyx_t_3 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":2308
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:
- *                 return cigar             # <<<<<<<<<<<<<<
- * 
- *             cigar_p = pysam_bam_get_cigar(src);
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_v_cigar);
-    __pyx_r = __pyx_v_cigar;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2310
- *                 return cigar
- * 
- *             cigar_p = pysam_bam_get_cigar(src);             # <<<<<<<<<<<<<<
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2311
- * 
- *             cigar_p = pysam_bam_get_cigar(src);
- *             for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-  __pyx_t_4 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":2312
- *             cigar_p = pysam_bam_get_cigar(src);
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 cigar.append((op, l))
- */
-    __pyx_t_1 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_XDECREF_SET(__pyx_v_op, __pyx_t_1);
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":2313
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- *                 cigar.append((op, l))
- *             return cigar
- */
-    __pyx_t_1 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2313; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_1);
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":2314
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 cigar.append((op, l))             # <<<<<<<<<<<<<<
- *             return cigar
- * 
- */
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_INCREF(__pyx_v_op);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_op);
-    __Pyx_GIVEREF(__pyx_v_op);
-    __Pyx_INCREF(__pyx_v_l);
-    PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_l);
-    __Pyx_GIVEREF(__pyx_v_l);
-    __pyx_t_5 = __Pyx_PyObject_Append(__pyx_v_cigar, __pyx_t_1); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  }
-
-  /* "pysam/csamfile.pyx":2315
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 cigar.append((op, l))
- *             return cigar             # <<<<<<<<<<<<<<
- * 
- *         def __set__(self, values):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_cigar);
-  __pyx_r = __pyx_v_cigar;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2299
- *         or None.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t * cigar_p
- *             cdef bam1_t * src
+  /* "pysam/calignmentfile.pyx":2359
+ *     property mapping_quality:
+ *         """mapping quality"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return pysam_get_qual(self._delegate)
+ *         def __set__(self, qual):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.cigar.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mapping_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_op);
-  __Pyx_XDECREF(__pyx_v_l);
-  __Pyx_XDECREF(__pyx_v_cigar);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2317
- *             return cigar
+/* "pysam/calignmentfile.pyx":2361
+ *         def __get__(self):
+ *             return pysam_get_qual(self._delegate)
+ *         def __set__(self, qual):             # <<<<<<<<<<<<<<
+ *             pysam_set_qual(self._delegate, qual)
  * 
- *         def __set__(self, values):             # <<<<<<<<<<<<<<
- *             cdef uint32_t * p
- *             cdef bam1_t * src
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_values); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_values) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_values));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_qual));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_values) {
-  uint32_t *__pyx_v_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_op = 0;
-  PyObject *__pyx_v_l = 0;
-  int __pyx_v_k;
-  int __pyx_v_ncigar;
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_qual) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  int __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  Py_ssize_t __pyx_t_5;
-  PyObject *(*__pyx_t_6)(PyObject *);
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  PyObject *(*__pyx_t_11)(PyObject *);
-  uint32_t __pyx_t_12;
+  uint8_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2317);
-  __Pyx_INCREF(__pyx_v_values);
-
-  /* "pysam/csamfile.pyx":2323
- *             cdef int k, ncigar
- * 
- *             k = 0             # <<<<<<<<<<<<<<
- * 
- *             src = self._delegate
- */
-  __pyx_v_k = 0;
-
-  /* "pysam/csamfile.pyx":2325
- *             k = 0
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             # get location of cigar string
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2328
- * 
- *             # get location of cigar string
- *             p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *             # empty values for cigar string
- */
-  __pyx_v_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2331
- * 
- *             # empty values for cigar string
- *             if values is None:             # <<<<<<<<<<<<<<
- *                 values = []
- * 
- */
-  __pyx_t_2 = (__pyx_v_values == Py_None);
-  __pyx_t_3 = (__pyx_t_2 != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":2332
- *             # empty values for cigar string
- *             if values is None:
- *                 values = []             # <<<<<<<<<<<<<<
- * 
- *             ncigar = len(values)
- */
-    __pyx_t_4 = PyList_New(0); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF_SET(__pyx_v_values, __pyx_t_4);
-    __pyx_t_4 = 0;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":2334
- *                 values = []
- * 
- *             ncigar = len(values)             # <<<<<<<<<<<<<<
- *             # create space for cigar data within src.data
- *             pysam_bam_update(src,
- */
-  __pyx_t_5 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_ncigar = __pyx_t_5;
-
-  /* "pysam/csamfile.pyx":2336
- *             ncigar = len(values)
- *             # create space for cigar data within src.data
- *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
- *                              pysam_get_n_cigar(src) * 4,
- *                              ncigar * 4,
- */
-  pysam_bam_update(__pyx_v_src, (pysam_get_n_cigar(__pyx_v_src) * 4), (__pyx_v_ncigar * 4), ((uint8_t *)__pyx_v_p));
-
-  /* "pysam/csamfile.pyx":2342
- * 
- *             # length is number of cigar operations, not bytes
- *             pysam_set_n_cigar(src, ncigar)             # <<<<<<<<<<<<<<
- * 
- *             # re-acquire pointer to location in memory
- */
-  pysam_set_n_cigar(__pyx_v_src, __pyx_v_ncigar);
-
-  /* "pysam/csamfile.pyx":2346
- *             # re-acquire pointer to location in memory
- *             # as it might have moved
- *             p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *             # insert cigar operations
- */
-  __pyx_v_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2349
- * 
- *             # insert cigar operations
- *             for op, l in values:             # <<<<<<<<<<<<<<
- *                 p[k] = l << BAM_CIGAR_SHIFT | op
- *                 k += 1
- */
-  if (PyList_CheckExact(__pyx_v_values) || PyTuple_CheckExact(__pyx_v_values)) {
-    __pyx_t_4 = __pyx_v_values; __Pyx_INCREF(__pyx_t_4); __pyx_t_5 = 0;
-    __pyx_t_6 = NULL;
-  } else {
-    __pyx_t_5 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_6 = Py_TYPE(__pyx_t_4)->tp_iternext;
-  }
-  for (;;) {
-    if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_4)) {
-      if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_4)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_4)) {
-      if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_4)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else {
-      __pyx_t_7 = __pyx_t_6(__pyx_t_4);
-      if (unlikely(!__pyx_t_7)) {
-        PyObject* exc_type = PyErr_Occurred();
-        if (exc_type) {
-          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        break;
-      }
-      __Pyx_GOTREF(__pyx_t_7);
-    }
-    if ((likely(PyTuple_CheckExact(__pyx_t_7))) || (PyList_CheckExact(__pyx_t_7))) {
-      PyObject* sequence = __pyx_t_7;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      Py_ssize_t size = Py_SIZE(sequence);
-      #else
-      Py_ssize_t size = PySequence_Size(sequence);
-      #endif
-      if (unlikely(size != 2)) {
-        if (size > 2) __Pyx_RaiseTooManyValuesError(2);
-        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      #if CYTHON_COMPILING_IN_CPYTHON
-      if (likely(PyTuple_CheckExact(sequence))) {
-        __pyx_t_8 = PyTuple_GET_ITEM(sequence, 0); 
-        __pyx_t_9 = PyTuple_GET_ITEM(sequence, 1); 
-      } else {
-        __pyx_t_8 = PyList_GET_ITEM(sequence, 0); 
-        __pyx_t_9 = PyList_GET_ITEM(sequence, 1); 
-      }
-      __Pyx_INCREF(__pyx_t_8);
-      __Pyx_INCREF(__pyx_t_9);
-      #else
-      __pyx_t_8 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_9 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      #endif
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    } else {
-      Py_ssize_t index = -1;
-      __pyx_t_10 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_10);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_11 = Py_TYPE(__pyx_t_10)->tp_iternext;
-      index = 0; __pyx_t_8 = __pyx_t_11(__pyx_t_10); if (unlikely(!__pyx_t_8)) goto __pyx_L6_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_8);
-      index = 1; __pyx_t_9 = __pyx_t_11(__pyx_t_10); if (unlikely(!__pyx_t_9)) goto __pyx_L6_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_9);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_11(__pyx_t_10), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_11 = NULL;
-      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      goto __pyx_L7_unpacking_done;
-      __pyx_L6_unpacking_failed:;
-      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      __pyx_t_11 = NULL;
-      if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_L7_unpacking_done:;
-    }
-    __Pyx_XDECREF_SET(__pyx_v_op, __pyx_t_8);
-    __pyx_t_8 = 0;
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_9);
-    __pyx_t_9 = 0;
-
-    /* "pysam/csamfile.pyx":2350
- *             # insert cigar operations
- *             for op, l in values:
- *                 p[k] = l << BAM_CIGAR_SHIFT | op             # <<<<<<<<<<<<<<
- *                 k += 1
- * 
- */
-    __pyx_t_7 = __Pyx_PyInt_From_int(BAM_CIGAR_SHIFT); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_9 = PyNumber_Lshift(__pyx_v_l, __pyx_t_7); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    __pyx_t_7 = PyNumber_Or(__pyx_t_9, __pyx_v_op); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_12 = __Pyx_PyInt_As_uint32_t(__pyx_t_7); if (unlikely((__pyx_t_12 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    (__pyx_v_p[__pyx_v_k]) = __pyx_t_12;
-
-    /* "pysam/csamfile.pyx":2351
- *             for op, l in values:
- *                 p[k] = l << BAM_CIGAR_SHIFT | op
- *                 k += 1             # <<<<<<<<<<<<<<
- * 
- *             ## setting the cigar string requires updating the bin
- */
-    __pyx_v_k = (__pyx_v_k + 1);
-  }
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2361);
 
-  /* "pysam/csamfile.pyx":2354
+  /* "pysam/calignmentfile.pyx":2362
+ *             return pysam_get_qual(self._delegate)
+ *         def __set__(self, qual):
+ *             pysam_set_qual(self._delegate, qual)             # <<<<<<<<<<<<<<
  * 
- *             ## setting the cigar string requires updating the bin
- *             pysam_set_bin(src,             # <<<<<<<<<<<<<<
- *                           hts_reg2bin(
- *                               src.core.pos,
+ *     property cigarstring:
  */
-  pysam_set_bin(__pyx_v_src, hts_reg2bin(__pyx_v_src->core.pos, bam_endpos(__pyx_v_src), 14, 5));
+  __pyx_t_1 = __Pyx_PyInt_As_uint8_t(__pyx_v_qual); if (unlikely((__pyx_t_1 == (uint8_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_set_qual(__pyx_v_self->_delegate, __pyx_t_1);
 
-  /* "pysam/csamfile.pyx":2317
- *             return cigar
+  /* "pysam/calignmentfile.pyx":2361
+ *         def __get__(self):
+ *             return pysam_get_qual(self._delegate)
+ *         def __set__(self, qual):             # <<<<<<<<<<<<<<
+ *             pysam_set_qual(self._delegate, qual)
  * 
- *         def __set__(self, values):             # <<<<<<<<<<<<<<
- *             cdef uint32_t * p
- *             cdef bam1_t * src
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.cigar.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mapping_quality.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_op);
-  __Pyx_XDECREF(__pyx_v_l);
-  __Pyx_XDECREF(__pyx_v_values);
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2378
+/* "pysam/calignmentfile.pyx":2381
  *         empty string.
  *         '''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             c = self.cigar
- *             if c == None: return ""
+ *             c = self.cigartuples
+ *             if c is None:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigarstring_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigarstring_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigarstring___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigarstring___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_v_c = NULL;
   PyObject *__pyx_v_x = NULL;
   PyObject *__pyx_v_y = NULL;
@@ -24560,99 +24688,107 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring___get__(s
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  Py_ssize_t __pyx_t_4;
-  PyObject *(*__pyx_t_5)(PyObject *);
-  PyObject *__pyx_t_6 = NULL;
+  int __pyx_t_3;
+  PyObject *__pyx_t_4 = NULL;
+  Py_ssize_t __pyx_t_5;
+  PyObject *(*__pyx_t_6)(PyObject *);
   PyObject *__pyx_t_7 = NULL;
   PyObject *__pyx_t_8 = NULL;
   PyObject *__pyx_t_9 = NULL;
-  PyObject *(*__pyx_t_10)(PyObject *);
-  Py_ssize_t __pyx_t_11;
+  PyObject *__pyx_t_10 = NULL;
+  PyObject *(*__pyx_t_11)(PyObject *);
+  Py_ssize_t __pyx_t_12;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2378);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2381);
 
-  /* "pysam/csamfile.pyx":2379
+  /* "pysam/calignmentfile.pyx":2382
  *         '''
  *         def __get__(self):
- *             c = self.cigar             # <<<<<<<<<<<<<<
- *             if c == None: return ""
- *             # reverse order
+ *             c = self.cigartuples             # <<<<<<<<<<<<<<
+ *             if c is None:
+ *                 return None
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigar); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigartuples); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_c = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":2380
+  /* "pysam/calignmentfile.pyx":2383
  *         def __get__(self):
- *             c = self.cigar
- *             if c == None: return ""             # <<<<<<<<<<<<<<
+ *             c = self.cigartuples
+ *             if c is None:             # <<<<<<<<<<<<<<
+ *                 return None
  *             # reverse order
- *             else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
  */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_c, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (__pyx_t_2) {
+  __pyx_t_2 = (__pyx_v_c == Py_None);
+  __pyx_t_3 = (__pyx_t_2 != 0);
+  if (__pyx_t_3) {
+
+    /* "pysam/calignmentfile.pyx":2384
+ *             c = self.cigartuples
+ *             if c is None:
+ *                 return None             # <<<<<<<<<<<<<<
+ *             # reverse order
+ *             else:
+ */
     __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_kp_s__9);
-    __pyx_r = __pyx_kp_s__9;
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":2382
- *             if c == None: return ""
+    /* "pysam/calignmentfile.pyx":2387
  *             # reverse order
- *             else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])             # <<<<<<<<<<<<<<
+ *             else:
+ *                 return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])             # <<<<<<<<<<<<<<
  * 
  *         def __set__(self, cigar):
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     if (PyList_CheckExact(__pyx_v_c) || PyTuple_CheckExact(__pyx_v_c)) {
-      __pyx_t_3 = __pyx_v_c; __Pyx_INCREF(__pyx_t_3); __pyx_t_4 = 0;
-      __pyx_t_5 = NULL;
+      __pyx_t_4 = __pyx_v_c; __Pyx_INCREF(__pyx_t_4); __pyx_t_5 = 0;
+      __pyx_t_6 = NULL;
     } else {
-      __pyx_t_4 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_v_c); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_5 = Py_TYPE(__pyx_t_3)->tp_iternext;
+      __pyx_t_5 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_v_c); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_4);
+      __pyx_t_6 = Py_TYPE(__pyx_t_4)->tp_iternext;
     }
     for (;;) {
-      if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_3)) {
-        if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_3)) break;
+      if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_4)) {
+        if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_4)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_6 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_6); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_6 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
-      } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_3)) {
-        if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_3)) break;
+      } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_4)) {
+        if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_4)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_6); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_6 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
-        __pyx_t_6 = __pyx_t_5(__pyx_t_3);
-        if (unlikely(!__pyx_t_6)) {
+        __pyx_t_7 = __pyx_t_6(__pyx_t_4);
+        if (unlikely(!__pyx_t_7)) {
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
-        __Pyx_GOTREF(__pyx_t_6);
+        __Pyx_GOTREF(__pyx_t_7);
       }
-      if ((likely(PyTuple_CheckExact(__pyx_t_6))) || (PyList_CheckExact(__pyx_t_6))) {
-        PyObject* sequence = __pyx_t_6;
+      if ((likely(PyTuple_CheckExact(__pyx_t_7))) || (PyList_CheckExact(__pyx_t_7))) {
+        PyObject* sequence = __pyx_t_7;
         #if CYTHON_COMPILING_IN_CPYTHON
         Py_ssize_t size = Py_SIZE(sequence);
         #else
@@ -24661,93 +24797,93 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring___get__(s
         if (unlikely(size != 2)) {
           if (size > 2) __Pyx_RaiseTooManyValuesError(2);
           else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         #if CYTHON_COMPILING_IN_CPYTHON
         if (likely(PyTuple_CheckExact(sequence))) {
-          __pyx_t_7 = PyTuple_GET_ITEM(sequence, 0); 
-          __pyx_t_8 = PyTuple_GET_ITEM(sequence, 1); 
+          __pyx_t_8 = PyTuple_GET_ITEM(sequence, 0); 
+          __pyx_t_9 = PyTuple_GET_ITEM(sequence, 1); 
         } else {
-          __pyx_t_7 = PyList_GET_ITEM(sequence, 0); 
-          __pyx_t_8 = PyList_GET_ITEM(sequence, 1); 
+          __pyx_t_8 = PyList_GET_ITEM(sequence, 0); 
+          __pyx_t_9 = PyList_GET_ITEM(sequence, 1); 
         }
-        __Pyx_INCREF(__pyx_t_7);
         __Pyx_INCREF(__pyx_t_8);
+        __Pyx_INCREF(__pyx_t_9);
         #else
-        __pyx_t_7 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_8 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
+        __pyx_t_9 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_9);
         #endif
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       } else {
         Py_ssize_t index = -1;
-        __pyx_t_9 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_10 = Py_TYPE(__pyx_t_9)->tp_iternext;
-        index = 0; __pyx_t_7 = __pyx_t_10(__pyx_t_9); if (unlikely(!__pyx_t_7)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_7);
-        index = 1; __pyx_t_8 = __pyx_t_10(__pyx_t_9); if (unlikely(!__pyx_t_8)) goto __pyx_L6_unpacking_failed;
+        __pyx_t_10 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_10);
+        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+        __pyx_t_11 = Py_TYPE(__pyx_t_10)->tp_iternext;
+        index = 0; __pyx_t_8 = __pyx_t_11(__pyx_t_10); if (unlikely(!__pyx_t_8)) goto __pyx_L6_unpacking_failed;
         __Pyx_GOTREF(__pyx_t_8);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_10(__pyx_t_9), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_10 = NULL;
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+        index = 1; __pyx_t_9 = __pyx_t_11(__pyx_t_10); if (unlikely(!__pyx_t_9)) goto __pyx_L6_unpacking_failed;
+        __Pyx_GOTREF(__pyx_t_9);
+        if (__Pyx_IternextUnpackEndCheck(__pyx_t_11(__pyx_t_10), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = NULL;
+        __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
         goto __pyx_L7_unpacking_done;
         __pyx_L6_unpacking_failed:;
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_10 = NULL;
+        __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
+        __pyx_t_11 = NULL;
         if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_L7_unpacking_done:;
       }
-      __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_7);
-      __pyx_t_7 = 0;
-      __Pyx_XDECREF_SET(__pyx_v_y, __pyx_t_8);
+      __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_8);
       __pyx_t_8 = 0;
-      __pyx_t_11 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_11 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_6 = __Pyx_PyInt_From_char((__pyx_v_5pysam_8csamfile_CODE2CIGAR[__pyx_t_11])); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
+      __Pyx_XDECREF_SET(__pyx_v_y, __pyx_t_9);
+      __pyx_t_9 = 0;
+      __pyx_t_12 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_12 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyInt_From_char((__pyx_v_5pysam_14calignmentfile_CODE2CIGAR[__pyx_t_12])); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_7);
+      __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_9);
       __Pyx_INCREF(__pyx_v_y);
-      PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_y);
+      PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_y);
       __Pyx_GIVEREF(__pyx_v_y);
-      PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_6);
-      __Pyx_GIVEREF(__pyx_t_6);
-      __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_i_c, __pyx_t_8); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+      PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_t_7);
+      __Pyx_GIVEREF(__pyx_t_7);
+      __pyx_t_7 = 0;
+      __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_i_c, __pyx_t_9); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_7);
+      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+      if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_7))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     }
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__9, __pyx_t_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    __pyx_t_4 = __Pyx_PyString_Join(__pyx_kp_s__7, __pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
+    __pyx_r = __pyx_t_4;
+    __pyx_t_4 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":2378
+  /* "pysam/calignmentfile.pyx":2381
  *         empty string.
  *         '''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             c = self.cigar
- *             if c == None: return ""
+ *             c = self.cigartuples
+ *             if c is None:
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
+  __Pyx_XDECREF(__pyx_t_4);
   __Pyx_XDECREF(__pyx_t_7);
   __Pyx_XDECREF(__pyx_t_8);
   __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.cigarstring.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_10);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.cigarstring.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_c);
@@ -24759,28 +24895,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring___get__(s
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2384
- *             else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
+/* "pysam/calignmentfile.pyx":2389
+ *                 return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
  * 
  *         def __set__(self, cigar):             # <<<<<<<<<<<<<<
  *             if cigar is None or len(cigar) == 0:
- *                 self.cigar = []
+ *                 self.cigartuples = []
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_cigar); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_cigar) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigarstring_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_cigar); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigarstring_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_cigar) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_cigar));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigarstring_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_cigar));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_cigar) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigarstring_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_cigar) {
   PyObject *__pyx_v_parts = NULL;
   PyObject *__pyx_v_x = NULL;
   PyObject *__pyx_v_y = NULL;
@@ -24803,18 +24939,18 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2384);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2389);
 
-  /* "pysam/csamfile.pyx":2385
+  /* "pysam/calignmentfile.pyx":2390
  * 
  *         def __set__(self, cigar):
  *             if cigar is None or len(cigar) == 0:             # <<<<<<<<<<<<<<
- *                 self.cigar = []
+ *                 self.cigartuples = []
  *             else:
  */
   __pyx_t_1 = (__pyx_v_cigar == Py_None);
   if (!(__pyx_t_1 != 0)) {
-    __pyx_t_2 = PyObject_Length(__pyx_v_cigar); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyObject_Length(__pyx_v_cigar); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_3 = ((__pyx_t_2 == 0) != 0);
     __pyx_t_4 = __pyx_t_3;
   } else {
@@ -24822,59 +24958,59 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
   }
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":2386
+    /* "pysam/calignmentfile.pyx":2391
  *         def __set__(self, cigar):
  *             if cigar is None or len(cigar) == 0:
- *                 self.cigar = []             # <<<<<<<<<<<<<<
+ *                 self.cigartuples = []             # <<<<<<<<<<<<<<
  *             else:
  *                 parts = CIGAR_REGEX.findall(cigar)
  */
-    __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigar, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigartuples, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     goto __pyx_L3;
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":2388
- *                 self.cigar = []
+    /* "pysam/calignmentfile.pyx":2393
+ *                 self.cigartuples = []
  *             else:
  *                 parts = CIGAR_REGEX.findall(cigar)             # <<<<<<<<<<<<<<
  *                 # reverse order
- *                 self.cigar = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]
+ *                 self.cigartuples = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]
  */
-    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_CIGAR_REGEX); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_CIGAR_REGEX); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_findall); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_findall); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_INCREF(__pyx_v_cigar);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_cigar);
     __Pyx_GIVEREF(__pyx_v_cigar);
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_5, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_5, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __pyx_v_parts = __pyx_t_7;
     __pyx_t_7 = 0;
 
-    /* "pysam/csamfile.pyx":2390
+    /* "pysam/calignmentfile.pyx":2395
  *                 parts = CIGAR_REGEX.findall(cigar)
  *                 # reverse order
- *                 self.cigar = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]             # <<<<<<<<<<<<<<
+ *                 self.cigartuples = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]             # <<<<<<<<<<<<<<
  * 
- *     property seq:
+ *     # TODO
  */
-    __pyx_t_7 = PyList_New(0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyList_New(0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     if (PyList_CheckExact(__pyx_v_parts) || PyTuple_CheckExact(__pyx_v_parts)) {
       __pyx_t_5 = __pyx_v_parts; __Pyx_INCREF(__pyx_t_5); __pyx_t_2 = 0;
       __pyx_t_8 = NULL;
     } else {
-      __pyx_t_2 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_v_parts); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_v_parts); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
       __pyx_t_8 = Py_TYPE(__pyx_t_5)->tp_iternext;
     }
@@ -24882,16 +25018,16 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
       if (!__pyx_t_8 && PyList_CheckExact(__pyx_t_5)) {
         if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_5)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_6 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_2); __Pyx_INCREF(__pyx_t_6); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_2); __Pyx_INCREF(__pyx_t_6); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_6 = PySequence_ITEM(__pyx_t_5, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PySequence_ITEM(__pyx_t_5, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_8 && PyTuple_CheckExact(__pyx_t_5)) {
         if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_5)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_2); __Pyx_INCREF(__pyx_t_6); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_2); __Pyx_INCREF(__pyx_t_6); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_6 = PySequence_ITEM(__pyx_t_5, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PySequence_ITEM(__pyx_t_5, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_6 = __pyx_t_8(__pyx_t_5);
@@ -24899,7 +25035,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -24915,7 +25051,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
         if (unlikely(size != 2)) {
           if (size > 2) __Pyx_RaiseTooManyValuesError(2);
           else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         #if CYTHON_COMPILING_IN_CPYTHON
         if (likely(PyTuple_CheckExact(sequence))) {
@@ -24928,15 +25064,15 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
         __Pyx_INCREF(__pyx_t_9);
         __Pyx_INCREF(__pyx_t_10);
         #else
-        __pyx_t_9 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_10 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
         #endif
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       } else {
         Py_ssize_t index = -1;
-        __pyx_t_11 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext;
@@ -24944,7 +25080,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
         __Pyx_GOTREF(__pyx_t_9);
         index = 1; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L6_unpacking_failed;
         __Pyx_GOTREF(__pyx_t_10);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_12 = NULL;
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         goto __pyx_L7_unpacking_done;
@@ -24952,30 +25088,30 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         __pyx_t_12 = NULL;
         if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_L7_unpacking_done:;
       }
       __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
       __pyx_t_9 = 0;
       __Pyx_XDECREF_SET(__pyx_v_y, __pyx_t_10);
       __pyx_t_10 = 0;
-      __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_CIGAR2CODE); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_CIGAR2CODE); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_INCREF(__pyx_v_y);
       PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_v_y);
       __Pyx_GIVEREF(__pyx_v_y);
-      __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_10, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_10, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      __pyx_t_10 = PyObject_GetItem(__pyx_t_6, __pyx_t_9); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_10 = PyObject_GetItem(__pyx_t_6, __pyx_t_9); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_9 = PyNumber_Int(__pyx_v_x); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyNumber_Int(__pyx_v_x); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_10);
       __Pyx_GIVEREF(__pyx_t_10);
@@ -24983,21 +25119,21 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
       __Pyx_GIVEREF(__pyx_t_9);
       __pyx_t_10 = 0;
       __pyx_t_9 = 0;
-      if (unlikely(__Pyx_ListComp_Append(__pyx_t_7, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(__Pyx_ListComp_Append(__pyx_t_7, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     }
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigar, __pyx_t_7) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigartuples, __pyx_t_7) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":2384
- *             else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
+  /* "pysam/calignmentfile.pyx":2389
+ *                 return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
  * 
  *         def __set__(self, cigar):             # <<<<<<<<<<<<<<
  *             if cigar is None or len(cigar) == 0:
- *                 self.cigar = []
+ *                 self.cigartuples = []
  */
 
   /* function exit code */
@@ -25010,7 +25146,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
   __Pyx_XDECREF(__pyx_t_9);
   __Pyx_XDECREF(__pyx_t_10);
   __Pyx_XDECREF(__pyx_t_11);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.cigarstring.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.cigarstring.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_parts);
@@ -25021,92 +25157,48 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2412
- *         sequence.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef char * s
+/* "pysam/calignmentfile.pyx":2403
+ *     property next_reference_id:
+ *         """the :term:`reference` id of the mate/next read."""
+ *         def __get__(self): return self._delegate.core.mtid             # <<<<<<<<<<<<<<
+ *         def __set__(self, mtid):
+ *             self._delegate.core.mtid = mtid
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3seq___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_1 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2412);
-
-  /* "pysam/csamfile.pyx":2415
- *             cdef bam1_t * src
- *             cdef char * s
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             if src.core.l_qseq == 0: return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2417
- *             src = self._delegate
- * 
- *             if src.core.l_qseq == 0: return None             # <<<<<<<<<<<<<<
- * 
- *             return get_seq_range(src, 0, src.core.l_qseq)
- */
-  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2419
- *             if src.core.l_qseq == 0: return None
- * 
- *             return get_seq_range(src, 0, src.core.l_qseq)             # <<<<<<<<<<<<<<
- * 
- *         def __set__(self, seq):
- */
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2403);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_get_seq_range(__pyx_v_src, 0, __pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2419; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
+  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mtid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2403; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2412
- *         sequence.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef char * s
- */
-
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.seq.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.next_reference_id.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -25115,101 +25207,579 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3seq___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2421
- *             return get_seq_range(src, 0, src.core.l_qseq)
+/* "pysam/calignmentfile.pyx":2404
+ *         """the :term:`reference` id of the mate/next read."""
+ *         def __get__(self): return self._delegate.core.mtid
+ *         def __set__(self, mtid):             # <<<<<<<<<<<<<<
+ *             self._delegate.core.mtid = mtid
  * 
- *         def __set__(self, seq):             # <<<<<<<<<<<<<<
- *             # samtools manages sequence and quality length memory together
- *             # if no quality information is present, the first byte says 0xff.
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_seq); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_seq) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_seq));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_mtid));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_seq) {
-  bam1_t *__pyx_v_src;
-  uint8_t *__pyx_v_p;
-  char *__pyx_v_s;
-  int __pyx_v_l;
-  int __pyx_v_k;
-  int __pyx_v_nbytes_new;
-  int __pyx_v_nbytes_old;
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_mtid) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_t_2;
-  Py_ssize_t __pyx_t_3;
-  bam1_t *__pyx_t_4;
-  int __pyx_t_5;
-  char *__pyx_t_6;
-  long __pyx_t_7;
+  int32_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2421);
-  __Pyx_INCREF(__pyx_v_seq);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2404);
 
-  /* "pysam/csamfile.pyx":2429
- *             cdef int l, k, nbytes_new, nbytes_old
+  /* "pysam/calignmentfile.pyx":2405
+ *         def __get__(self): return self._delegate.core.mtid
+ *         def __set__(self, mtid):
+ *             self._delegate.core.mtid = mtid             # <<<<<<<<<<<<<<
  * 
- *             if seq == None:             # <<<<<<<<<<<<<<
- *                 l = 0
- *             else:
+ *     property next_reference_start:
  */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_seq, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (__pyx_t_2) {
+  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mtid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_self->_delegate->core.mtid = __pyx_t_1;
 
-    /* "pysam/csamfile.pyx":2430
+  /* "pysam/calignmentfile.pyx":2404
+ *         """the :term:`reference` id of the mate/next read."""
+ *         def __get__(self): return self._delegate.core.mtid
+ *         def __set__(self, mtid):             # <<<<<<<<<<<<<<
+ *             self._delegate.core.mtid = mtid
  * 
- *             if seq == None:
- *                 l = 0             # <<<<<<<<<<<<<<
- *             else:
- *                 l = len(seq)
  */
-    __pyx_v_l = 0;
-    goto __pyx_L3;
-  }
-  /*else*/ {
 
-    /* "pysam/csamfile.pyx":2432
- *                 l = 0
- *             else:
- *                 l = len(seq)             # <<<<<<<<<<<<<<
- *                 seq = _forceBytes(seq)
- * 
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.next_reference_id.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2409
+ *     property next_reference_start:
+ *         """the position of the mate/next read."""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._delegate.core.mpos
+ *         def __set__(self, mpos):
  */
-    __pyx_t_3 = PyObject_Length(__pyx_v_seq); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_l = __pyx_t_3;
 
-    /* "pysam/csamfile.pyx":2433
- *             else:
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2409);
+
+  /* "pysam/calignmentfile.pyx":2410
+ *         """the position of the mate/next read."""
+ *         def __get__(self):
+ *             return self._delegate.core.mpos             # <<<<<<<<<<<<<<
+ *         def __set__(self, mpos):
+ *             self._delegate.core.mpos = mpos
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":2409
+ *     property next_reference_start:
+ *         """the position of the mate/next read."""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._delegate.core.mpos
+ *         def __set__(self, mpos):
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.next_reference_start.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2411
+ *         def __get__(self):
+ *             return self._delegate.core.mpos
+ *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
+ *             self._delegate.core.mpos = mpos
+ * 
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_mpos));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_mpos) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int32_t __pyx_t_1;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2411);
+
+  /* "pysam/calignmentfile.pyx":2412
+ *             return self._delegate.core.mpos
+ *         def __set__(self, mpos):
+ *             self._delegate.core.mpos = mpos             # <<<<<<<<<<<<<<
+ * 
+ *     property query_length:
+ */
+  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mpos); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2412; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_self->_delegate->core.mpos = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":2411
+ *         def __get__(self):
+ *             return self._delegate.core.mpos
+ *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
+ *             self._delegate.core.mpos = mpos
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.next_reference_start.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2425
+ *         This property can be set by providing a sequence.
+ *         """
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._delegate.core.l_qseq
+ * 
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12query_length_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12query_length_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12query_length___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12query_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2425);
+
+  /* "pysam/calignmentfile.pyx":2426
+ *         """
+ *         def __get__(self):
+ *             return self._delegate.core.l_qseq             # <<<<<<<<<<<<<<
+ * 
+ *     property template_length:
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.l_qseq); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":2425
+ *         This property can be set by providing a sequence.
+ *         """
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._delegate.core.l_qseq
+ * 
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_length.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2430
+ *     property template_length:
+ *         """the observed query template length"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._delegate.core.isize
+ *         def __set__(self, isize):
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15template_length_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15template_length_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15template_length___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15template_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2430);
+
+  /* "pysam/calignmentfile.pyx":2431
+ *         """the observed query template length"""
+ *         def __get__(self):
+ *             return self._delegate.core.isize             # <<<<<<<<<<<<<<
+ *         def __set__(self, isize):
+ *             self._delegate.core.isize = isize
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.isize); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":2430
+ *     property template_length:
+ *         """the observed query template length"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._delegate.core.isize
+ *         def __set__(self, isize):
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.template_length.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2432
+ *         def __get__(self):
+ *             return self._delegate.core.isize
+ *         def __set__(self, isize):             # <<<<<<<<<<<<<<
+ *             self._delegate.core.isize = isize
+ * 
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15template_length_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15template_length_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15template_length_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_isize));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15template_length_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_isize) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int32_t __pyx_t_1;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2432);
+
+  /* "pysam/calignmentfile.pyx":2433
+ *             return self._delegate.core.isize
+ *         def __set__(self, isize):
+ *             self._delegate.core.isize = isize             # <<<<<<<<<<<<<<
+ * 
+ *     property query_sequence:
+ */
+  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_isize); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_self->_delegate->core.isize = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":2432
+ *         def __get__(self):
+ *             return self._delegate.core.isize
+ *         def __set__(self, isize):             # <<<<<<<<<<<<<<
+ *             self._delegate.core.isize = isize
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.template_length.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2451
+ *         sequence.
+ *         """
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             cdef bam1_t * src
+ *             cdef char * s
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14query_sequence_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14query_sequence_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14query_sequence___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14query_sequence___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  bam1_t *__pyx_v_src;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  bam1_t *__pyx_t_1;
+  int __pyx_t_2;
+  PyObject *__pyx_t_3 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2451);
+
+  /* "pysam/calignmentfile.pyx":2454
+ *             cdef bam1_t * src
+ *             cdef char * s
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ * 
+ *             if src.core.l_qseq == 0: return None
+ */
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":2456
+ *             src = self._delegate
+ * 
+ *             if src.core.l_qseq == 0: return None             # <<<<<<<<<<<<<<
+ * 
+ *             return _getSequenceRange(src, 0, src.core.l_qseq)
+ */
+  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
+  if (__pyx_t_2) {
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":2458
+ *             if src.core.l_qseq == 0: return None
+ * 
+ *             return _getSequenceRange(src, 0, src.core.l_qseq)             # <<<<<<<<<<<<<<
+ * 
+ *         def __set__(self, seq):
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__getSequenceRange(__pyx_v_src, 0, __pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2458; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __pyx_r = __pyx_t_3;
+  __pyx_t_3 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":2451
+ *         sequence.
+ *         """
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             cdef bam1_t * src
+ *             cdef char * s
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_sequence.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":2460
+ *             return _getSequenceRange(src, 0, src.core.l_qseq)
+ * 
+ *         def __set__(self, seq):             # <<<<<<<<<<<<<<
+ *             # samtools manages sequence and quality length memory together
+ *             # if no quality information is present, the first byte says 0xff.
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14query_sequence_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_seq); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14query_sequence_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_seq) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14query_sequence_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_seq));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14query_sequence_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_seq) {
+  bam1_t *__pyx_v_src;
+  uint8_t *__pyx_v_p;
+  char *__pyx_v_s;
+  int __pyx_v_l;
+  int __pyx_v_k;
+  int __pyx_v_nbytes_new;
+  int __pyx_v_nbytes_old;
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_t_2;
+  Py_ssize_t __pyx_t_3;
+  bam1_t *__pyx_t_4;
+  int __pyx_t_5;
+  char *__pyx_t_6;
+  long __pyx_t_7;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2460);
+  __Pyx_INCREF(__pyx_v_seq);
+
+  /* "pysam/calignmentfile.pyx":2468
+ *             cdef int l, k, nbytes_new, nbytes_old
+ * 
+ *             if seq == None:             # <<<<<<<<<<<<<<
+ *                 l = 0
+ *             else:
+ */
+  __pyx_t_1 = PyObject_RichCompare(__pyx_v_seq, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":2469
+ * 
+ *             if seq == None:
+ *                 l = 0             # <<<<<<<<<<<<<<
+ *             else:
+ *                 l = len(seq)
+ */
+    __pyx_v_l = 0;
+    goto __pyx_L3;
+  }
+  /*else*/ {
+
+    /* "pysam/calignmentfile.pyx":2471
+ *                 l = 0
+ *             else:
+ *                 l = len(seq)             # <<<<<<<<<<<<<<
+ *                 seq = _forceBytes(seq)
+ * 
+ */
+    __pyx_t_3 = PyObject_Length(__pyx_v_seq); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_l = __pyx_t_3;
+
+    /* "pysam/calignmentfile.pyx":2472
+ *             else:
  *                 l = len(seq)
  *                 seq = _forceBytes(seq)             # <<<<<<<<<<<<<<
  * 
  *             src = self._delegate
  */
-    __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_seq); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_seq); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF_SET(__pyx_v_seq, __pyx_t_1);
     __pyx_t_1 = 0;
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":2435
+  /* "pysam/calignmentfile.pyx":2474
  *                 seq = _forceBytes(seq)
  * 
  *             src = self._delegate             # <<<<<<<<<<<<<<
@@ -25219,7 +25789,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
   __pyx_t_4 = __pyx_v_self->_delegate;
   __pyx_v_src = __pyx_t_4;
 
-  /* "pysam/csamfile.pyx":2439
+  /* "pysam/calignmentfile.pyx":2478
  *             # as the sequence is stored in half-bytes, the total length (sequence
  *             # plus quality scores) is (l+1)/2 + l
  *             nbytes_new = (l + 1) / 2 + l             # <<<<<<<<<<<<<<
@@ -25228,7 +25798,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
  */
   __pyx_v_nbytes_new = (__Pyx_div_long((__pyx_v_l + 1), 2) + __pyx_v_l);
 
-  /* "pysam/csamfile.pyx":2440
+  /* "pysam/calignmentfile.pyx":2479
  *             # plus quality scores) is (l+1)/2 + l
  *             nbytes_new = (l + 1) / 2 + l
  *             nbytes_old = (src.core.l_qseq + 1) / 2 + src.core.l_qseq             # <<<<<<<<<<<<<<
@@ -25237,7 +25807,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
  */
   __pyx_v_nbytes_old = (__Pyx_div_long((__pyx_v_src->core.l_qseq + 1), 2) + __pyx_v_src->core.l_qseq);
 
-  /* "pysam/csamfile.pyx":2443
+  /* "pysam/calignmentfile.pyx":2482
  * 
  *             # acquire pointer to location in memory
  *             p = pysam_bam_get_seq(src)             # <<<<<<<<<<<<<<
@@ -25246,7 +25816,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
  */
   __pyx_v_p = pysam_bam_get_seq(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":2444
+  /* "pysam/calignmentfile.pyx":2483
  *             # acquire pointer to location in memory
  *             p = pysam_bam_get_seq(src)
  *             src.core.l_qseq = l             # <<<<<<<<<<<<<<
@@ -25255,7 +25825,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
  */
   __pyx_v_src->core.l_qseq = __pyx_v_l;
 
-  /* "pysam/csamfile.pyx":2447
+  /* "pysam/calignmentfile.pyx":2486
  * 
  *             # change length of data field
  *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
@@ -25264,7 +25834,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
  */
   pysam_bam_update(__pyx_v_src, __pyx_v_nbytes_old, __pyx_v_nbytes_new, __pyx_v_p);
 
-  /* "pysam/csamfile.pyx":2452
+  /* "pysam/calignmentfile.pyx":2491
  *                              p)
  * 
  *             if l > 0:             # <<<<<<<<<<<<<<
@@ -25274,7 +25844,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
   __pyx_t_2 = ((__pyx_v_l > 0) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":2455
+    /* "pysam/calignmentfile.pyx":2494
  *                 # re-acquire pointer to location in memory
  *                 # as it might have moved
  *                 p = pysam_bam_get_seq(src)             # <<<<<<<<<<<<<<
@@ -25283,7 +25853,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
  */
     __pyx_v_p = pysam_bam_get_seq(__pyx_v_src);
 
-    /* "pysam/csamfile.pyx":2456
+    /* "pysam/calignmentfile.pyx":2495
  *                 # as it might have moved
  *                 p = pysam_bam_get_seq(src)
  *                 for k from 0 <= k < nbytes_new:             # <<<<<<<<<<<<<<
@@ -25293,7 +25863,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
     __pyx_t_5 = __pyx_v_nbytes_new;
     for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
 
-      /* "pysam/csamfile.pyx":2457
+      /* "pysam/calignmentfile.pyx":2496
  *                 p = pysam_bam_get_seq(src)
  *                 for k from 0 <= k < nbytes_new:
  *                     p[k] = 0             # <<<<<<<<<<<<<<
@@ -25303,17 +25873,17 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
       (__pyx_v_p[__pyx_v_k]) = 0;
     }
 
-    /* "pysam/csamfile.pyx":2459
+    /* "pysam/calignmentfile.pyx":2498
  *                     p[k] = 0
  *                 # convert to C string
  *                 s = seq             # <<<<<<<<<<<<<<
  *                 for k from 0 <= k < l:
  *                     p[k/2] |= seq_nt16_table[<unsigned char>s[k]] << 4 * (1 - k % 2)
  */
-    __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_seq); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_seq); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_s = __pyx_t_6;
 
-    /* "pysam/csamfile.pyx":2460
+    /* "pysam/calignmentfile.pyx":2499
  *                 # convert to C string
  *                 s = seq
  *                 for k from 0 <= k < l:             # <<<<<<<<<<<<<<
@@ -25323,7 +25893,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
     __pyx_t_5 = __pyx_v_l;
     for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
 
-      /* "pysam/csamfile.pyx":2461
+      /* "pysam/calignmentfile.pyx":2500
  *                 s = seq
  *                 for k from 0 <= k < l:
  *                     p[k/2] |= seq_nt16_table[<unsigned char>s[k]] << 4 * (1 - k % 2)             # <<<<<<<<<<<<<<
@@ -25334,7 +25904,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
       (__pyx_v_p[__pyx_t_7]) = ((__pyx_v_p[__pyx_t_7]) | ((seq_nt16_table[((unsigned char)(__pyx_v_s[__pyx_v_k]))]) << (4 * (1 - __Pyx_mod_long(__pyx_v_k, 2)))));
     }
 
-    /* "pysam/csamfile.pyx":2464
+    /* "pysam/calignmentfile.pyx":2503
  * 
  *                 # erase qualities
  *                 p = pysam_bam_get_qual(src)             # <<<<<<<<<<<<<<
@@ -25343,20 +25913,20 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
  */
     __pyx_v_p = pysam_bam_get_qual(__pyx_v_src);
 
-    /* "pysam/csamfile.pyx":2465
+    /* "pysam/calignmentfile.pyx":2504
  *                 # erase qualities
  *                 p = pysam_bam_get_qual(src)
  *                 p[0] = 0xff             # <<<<<<<<<<<<<<
  * 
- *     property qual:
+ *     property query_qualities:
  */
     (__pyx_v_p[0]) = 0xff;
     goto __pyx_L4;
   }
   __pyx_L4:;
 
-  /* "pysam/csamfile.pyx":2421
- *             return get_seq_range(src, 0, src.core.l_qseq)
+  /* "pysam/calignmentfile.pyx":2460
+ *             return _getSequenceRange(src, 0, src.core.l_qseq)
  * 
  *         def __set__(self, seq):             # <<<<<<<<<<<<<<
  *             # samtools manages sequence and quality length memory together
@@ -25368,7 +25938,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
   goto __pyx_L0;
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.seq.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_sequence.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_seq);
@@ -25377,8 +25947,8 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2482
- *         quality scores and the sequence are not the same.
+/* "pysam/calignmentfile.pyx":2523
+ * 
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
  * 
@@ -25386,19 +25956,19 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_ob
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15query_qualities_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15query_qualities_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4qual___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15query_qualities___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qual___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15query_qualities___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   bam1_t *__pyx_v_src;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -25410,49 +25980,57 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qual___get__(struct __
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2482);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2523);
 
-  /* "pysam/csamfile.pyx":2487
+  /* "pysam/calignmentfile.pyx":2528
  *             cdef char * q
  * 
  *             src = self._delegate             # <<<<<<<<<<<<<<
  * 
- *             if src.core.l_qseq == 0: return None
+ *             if src.core.l_qseq == 0:
  */
   __pyx_t_1 = __pyx_v_self->_delegate;
   __pyx_v_src = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2489
+  /* "pysam/calignmentfile.pyx":2530
  *             src = self._delegate
  * 
- *             if src.core.l_qseq == 0: return None             # <<<<<<<<<<<<<<
+ *             if src.core.l_qseq == 0:             # <<<<<<<<<<<<<<
+ *                 return None
  * 
- *             return get_qual_range(src, 0, src.core.l_qseq)
  */
   __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
   if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":2531
+ * 
+ *             if src.core.l_qseq == 0:
+ *                 return None             # <<<<<<<<<<<<<<
+ * 
+ *             return _getQualitiesRange(src, 0, src.core.l_qseq)
+ */
     __Pyx_XDECREF(__pyx_r);
     __Pyx_INCREF(Py_None);
     __pyx_r = Py_None;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":2491
- *             if src.core.l_qseq == 0: return None
+  /* "pysam/calignmentfile.pyx":2533
+ *                 return None
  * 
- *             return get_qual_range(src, 0, src.core.l_qseq)             # <<<<<<<<<<<<<<
+ *             return _getQualitiesRange(src, 0, src.core.l_qseq)             # <<<<<<<<<<<<<<
  * 
- *         def __set__(self,qual):
+ *         def __set__(self, qual):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_get_qual_range(__pyx_v_src, 0, __pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__getQualitiesRange(__pyx_v_src, 0, __pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __pyx_r = __pyx_t_3;
   __pyx_t_3 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2482
- *         quality scores and the sequence are not the same.
+  /* "pysam/calignmentfile.pyx":2523
+ * 
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
  * 
@@ -25462,7 +26040,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qual___get__(struct __
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qual.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_qualities.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -25471,701 +26049,231 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qual___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2493
- *             return get_qual_range(src, 0, src.core.l_qseq)
+/* "pysam/calignmentfile.pyx":2535
+ *             return _getQualitiesRange(src, 0, src.core.l_qseq)
  * 
- *         def __set__(self,qual):             # <<<<<<<<<<<<<<
- *             # note that space is already allocated via the sequences
- *             cdef bam1_t * src
+ *         def __set__(self, qual):             # <<<<<<<<<<<<<<
+ *             # note that memory is already allocated via setting the sequence
+ *             # hence length match of sequence and quality needs is checked.
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15query_qualities_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15query_qualities_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4qual_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_qual));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15query_qualities_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_qual));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4qual_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qual) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15query_qualities_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_qual) {
   bam1_t *__pyx_v_src;
   uint8_t *__pyx_v_p;
-  char *__pyx_v_q;
-  int __pyx_v_k;
   int __pyx_v_l;
+  arrayobject *__pyx_v_result = 0;
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   bam1_t *__pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  Py_ssize_t __pyx_t_4;
+  int __pyx_t_2;
+  Py_ssize_t __pyx_t_3;
+  int __pyx_t_4;
   int __pyx_t_5;
-  int __pyx_t_6;
-  char *__pyx_t_7;
+  PyObject *__pyx_t_6 = NULL;
+  PyObject *__pyx_t_7 = NULL;
   PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  int __pyx_t_10;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2493);
-  __Pyx_INCREF(__pyx_v_qual);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2535);
 
-  /* "pysam/csamfile.pyx":2500
- *             cdef int k
+  /* "pysam/calignmentfile.pyx":2542
+ *             cdef int l
  * 
  *             src = self._delegate             # <<<<<<<<<<<<<<
  *             p = pysam_bam_get_qual(src)
- *             if qual == None or len(qual) == 0:
+ *             if qual is None or len(qual) == 0:
  */
   __pyx_t_1 = __pyx_v_self->_delegate;
   __pyx_v_src = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2501
+  /* "pysam/calignmentfile.pyx":2543
  * 
  *             src = self._delegate
  *             p = pysam_bam_get_qual(src)             # <<<<<<<<<<<<<<
- *             if qual == None or len(qual) == 0:
+ *             if qual is None or len(qual) == 0:
  *                 # if absent - set to 0xff
  */
   __pyx_v_p = pysam_bam_get_qual(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":2502
+  /* "pysam/calignmentfile.pyx":2544
  *             src = self._delegate
  *             p = pysam_bam_get_qual(src)
- *             if qual == None or len(qual) == 0:             # <<<<<<<<<<<<<<
+ *             if qual is None or len(qual) == 0:             # <<<<<<<<<<<<<<
  *                 # if absent - set to 0xff
  *                 p[0] = 0xff
  */
-  __pyx_t_2 = PyObject_RichCompare(__pyx_v_qual, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (!__pyx_t_3) {
-    __pyx_t_4 = PyObject_Length(__pyx_v_qual); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = (__pyx_t_4 == 0);
-    __pyx_t_6 = __pyx_t_5;
+  __pyx_t_2 = (__pyx_v_qual == Py_None);
+  if (!(__pyx_t_2 != 0)) {
+    __pyx_t_3 = PyObject_Length(__pyx_v_qual); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = ((__pyx_t_3 == 0) != 0);
+    __pyx_t_5 = __pyx_t_4;
   } else {
-    __pyx_t_6 = __pyx_t_3;
+    __pyx_t_5 = (__pyx_t_2 != 0);
   }
-  if (__pyx_t_6) {
+  if (__pyx_t_5) {
 
-    /* "pysam/csamfile.pyx":2504
- *             if qual == None or len(qual) == 0:
+    /* "pysam/calignmentfile.pyx":2546
+ *             if qual is None or len(qual) == 0:
  *                 # if absent - set to 0xff
  *                 p[0] = 0xff             # <<<<<<<<<<<<<<
  *                 return
- *             qual = _forceBytes(qual)
+ * 
  */
     (__pyx_v_p[0]) = 0xff;
 
-    /* "pysam/csamfile.pyx":2505
+    /* "pysam/calignmentfile.pyx":2547
  *                 # if absent - set to 0xff
  *                 p[0] = 0xff
  *                 return             # <<<<<<<<<<<<<<
- *             qual = _forceBytes(qual)
- *             cdef int l
+ * 
+ *             # check for length match
  */
     __pyx_r = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":2506
- *                 p[0] = 0xff
- *                 return
- *             qual = _forceBytes(qual)             # <<<<<<<<<<<<<<
- *             cdef int l
- *             # convert to C string
+  /* "pysam/calignmentfile.pyx":2550
+ * 
+ *             # check for length match
+ *             l = len(qual)             # <<<<<<<<<<<<<<
+ *             if src.core.l_qseq != l:
+ *                 raise ValueError(
  */
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_qual); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF_SET(__pyx_v_qual, __pyx_t_2);
-  __pyx_t_2 = 0;
+  __pyx_t_3 = PyObject_Length(__pyx_v_qual); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2550; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_l = __pyx_t_3;
 
-  /* "pysam/csamfile.pyx":2509
- *             cdef int l
- *             # convert to C string
- *             q = qual             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2551
+ *             # check for length match
  *             l = len(qual)
- *             if src.core.l_qseq != l:
+ *             if src.core.l_qseq != l:             # <<<<<<<<<<<<<<
+ *                 raise ValueError(
+ *                     "quality and sequence mismatch: %i != %i" %
  */
-  __pyx_t_7 = __Pyx_PyObject_AsString(__pyx_v_qual); if (unlikely((!__pyx_t_7) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_q = __pyx_t_7;
+  __pyx_t_5 = ((__pyx_v_src->core.l_qseq != __pyx_v_l) != 0);
+  if (__pyx_t_5) {
 
-  /* "pysam/csamfile.pyx":2510
- *             # convert to C string
- *             q = qual
- *             l = len(qual)             # <<<<<<<<<<<<<<
- *             if src.core.l_qseq != l:
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
+    /* "pysam/calignmentfile.pyx":2554
+ *                 raise ValueError(
+ *                     "quality and sequence mismatch: %i != %i" %
+ *                     (l, src.core.l_qseq))             # <<<<<<<<<<<<<<
+ * 
+ *             # create a python array object filling it
  */
-  __pyx_t_4 = PyObject_Length(__pyx_v_qual); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_l = __pyx_t_4;
+    __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_l); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_6);
+    __pyx_t_7 = __Pyx_PyInt_From_int32_t(__pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_8);
+    PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_6);
+    __Pyx_GIVEREF(__pyx_t_6);
+    PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_7);
+    __Pyx_GIVEREF(__pyx_t_7);
+    __pyx_t_6 = 0;
+    __pyx_t_7 = 0;
 
-  /* "pysam/csamfile.pyx":2511
- *             q = qual
- *             l = len(qual)
- *             if src.core.l_qseq != l:             # <<<<<<<<<<<<<<
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
- *             assert src.core.l_qseq == l
+    /* "pysam/calignmentfile.pyx":2553
+ *             if src.core.l_qseq != l:
+ *                 raise ValueError(
+ *                     "quality and sequence mismatch: %i != %i" %             # <<<<<<<<<<<<<<
+ *                     (l, src.core.l_qseq))
+ * 
  */
-  __pyx_t_6 = ((__pyx_v_src->core.l_qseq != __pyx_v_l) != 0);
-  if (__pyx_t_6) {
+    __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_quality_and_sequence_mismatch_i, __pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
 
-    /* "pysam/csamfile.pyx":2512
+    /* "pysam/calignmentfile.pyx":2552
  *             l = len(qual)
  *             if src.core.l_qseq != l:
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))             # <<<<<<<<<<<<<<
- *             assert src.core.l_qseq == l
- *             for k from 0 <= k < l:
+ *                 raise ValueError(             # <<<<<<<<<<<<<<
+ *                     "quality and sequence mismatch: %i != %i" %
+ *                     (l, src.core.l_qseq))
  */
-    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_l); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_8 = __Pyx_PyInt_From_int32_t(__pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_2);
-    __Pyx_GIVEREF(__pyx_t_2);
-    PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_t_8);
-    __Pyx_GIVEREF(__pyx_t_8);
-    __pyx_t_2 = 0;
-    __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_quality_and_sequence_mismatch_i, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
-    __Pyx_GIVEREF(__pyx_t_8);
-    __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_Raise(__pyx_t_8, 0, 0, 0);
+    PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_7);
+    __Pyx_GIVEREF(__pyx_t_7);
+    __pyx_t_7 = 0;
+    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_8, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_Raise(__pyx_t_7, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":2513
- *             if src.core.l_qseq != l:
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
- *             assert src.core.l_qseq == l             # <<<<<<<<<<<<<<
- *             for k from 0 <= k < l:
- *                 p[k] = <uint8_t>q[k] - 33
+  /* "pysam/calignmentfile.pyx":2561
+ *             # NB: should avoid this copying if qual is
+ *             # already of the correct type.
+ *             cdef array.array result = array.array('B', qual)             # <<<<<<<<<<<<<<
+ * 
+ *             # copy data
  */
-  #ifndef CYTHON_WITHOUT_ASSERTIONS
-  if (unlikely(!Py_OptimizeFlag)) {
-    if (unlikely(!((__pyx_v_src->core.l_qseq == __pyx_v_l) != 0))) {
-      PyErr_SetNone(PyExc_AssertionError);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-  }
-  #endif
+  __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_7);
+  __Pyx_INCREF(__pyx_n_s_B);
+  PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_n_s_B);
+  __Pyx_GIVEREF(__pyx_n_s_B);
+  __Pyx_INCREF(__pyx_v_qual);
+  PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_v_qual);
+  __Pyx_GIVEREF(__pyx_v_qual);
+  __pyx_t_8 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_7cpython_5array_array)), __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_8);
+  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+  __pyx_v_result = ((arrayobject *)__pyx_t_8);
+  __pyx_t_8 = 0;
 
-  /* "pysam/csamfile.pyx":2514
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
- *             assert src.core.l_qseq == l
- *             for k from 0 <= k < l:             # <<<<<<<<<<<<<<
- *                 p[k] = <uint8_t>q[k] - 33
+  /* "pysam/calignmentfile.pyx":2564
  * 
+ *             # copy data
+ *             memcpy(p, result.data.as_voidptr, l)             # <<<<<<<<<<<<<<
+ * 
+ *     # TODO: opts object with mapping-like interface
  */
-  __pyx_t_10 = __pyx_v_l;
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_10; __pyx_v_k++) {
+  memcpy(__pyx_v_p, __pyx_v_result->data.as_voidptr, __pyx_v_l);
 
-    /* "pysam/csamfile.pyx":2515
- *             assert src.core.l_qseq == l
- *             for k from 0 <= k < l:
- *                 p[k] = <uint8_t>q[k] - 33             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2535
+ *             return _getQualitiesRange(src, 0, src.core.l_qseq)
  * 
- *     property query:
- */
-    (__pyx_v_p[__pyx_v_k]) = (((uint8_t)(__pyx_v_q[__pyx_v_k])) - 33);
-  }
-
-  /* "pysam/csamfile.pyx":2493
- *             return get_qual_range(src, 0, src.core.l_qseq)
- * 
- *         def __set__(self,qual):             # <<<<<<<<<<<<<<
- *             # note that space is already allocated via the sequences
- *             cdef bam1_t * src
+ *         def __set__(self, qual):             # <<<<<<<<<<<<<<
+ *             # note that memory is already allocated via setting the sequence
+ *             # hence length match of sequence and quality needs is checked.
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_6);
+  __Pyx_XDECREF(__pyx_t_7);
   __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qual.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_qualities.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_qual);
+  __Pyx_XDECREF((PyObject *)__pyx_v_result);
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2534
- *         were not considered for alignment may have been retained."""
- * 
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint32_t start, end
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5query_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5query_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5query___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5query___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  uint32_t __pyx_v_start;
-  uint32_t __pyx_v_end;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  int32_t __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2534);
-
-  /* "pysam/csamfile.pyx":2539
- *             cdef char * s
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             if src.core.l_qseq == 0: return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2541
- *             src = self._delegate
- * 
- *             if src.core.l_qseq == 0: return None             # <<<<<<<<<<<<<<
- * 
- *             start = query_start(src)
- */
-  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2543
- *             if src.core.l_qseq == 0: return None
- * 
- *             start = query_start(src)             # <<<<<<<<<<<<<<
- *             end   = query_end(src)
- * 
- */
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_start(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_start = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":2544
- * 
- *             start = query_start(src)
- *             end   = query_end(src)             # <<<<<<<<<<<<<<
- * 
- *             return get_seq_range(src, start, end)
- */
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_end(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_end = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":2546
- *             end   = query_end(src)
- * 
- *             return get_seq_range(src, start, end)             # <<<<<<<<<<<<<<
- * 
- *     property qqual:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_4 = __pyx_f_5pysam_8csamfile_get_seq_range(__pyx_v_src, __pyx_v_start, __pyx_v_end); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2534
- *         were not considered for alignment may have been retained."""
- * 
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint32_t start, end
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.query.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2557
- * 
- *         In Python 3, this property is of type bytes."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint32_t start, end
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5qqual_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5qqual_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5qqual___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5qqual___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  uint32_t __pyx_v_start;
-  uint32_t __pyx_v_end;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  int32_t __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2557);
-
-  /* "pysam/csamfile.pyx":2561
- *             cdef uint32_t start, end
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             if src.core.l_qseq == 0: return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2563
- *             src = self._delegate
- * 
- *             if src.core.l_qseq == 0: return None             # <<<<<<<<<<<<<<
- * 
- *             start = query_start(src)
- */
-  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2565
- *             if src.core.l_qseq == 0: return None
- * 
- *             start = query_start(src)             # <<<<<<<<<<<<<<
- *             end   = query_end(src)
- * 
- */
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_start(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_start = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":2566
- * 
- *             start = query_start(src)
- *             end   = query_end(src)             # <<<<<<<<<<<<<<
- * 
- *             return get_qual_range(src, start, end)
- */
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_end(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_end = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":2568
- *             end   = query_end(src)
- * 
- *             return get_qual_range(src, start, end)             # <<<<<<<<<<<<<<
- * 
- *     property qstart:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_4 = __pyx_f_5pysam_8csamfile_get_qual_range(__pyx_v_src, __pyx_v_start, __pyx_v_end); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2557
- * 
- *         In Python 3, this property is of type bytes."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint32_t start, end
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qqual.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2575
- *         This the index of the first base in :attr:`seq` that is not soft-clipped.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return query_start(self._delegate)
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_6qstart_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_6qstart_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_6qstart___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6qstart___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2575);
-
-  /* "pysam/csamfile.pyx":2576
- *         """
- *         def __get__(self):
- *             return query_start(self._delegate)             # <<<<<<<<<<<<<<
- * 
- *     property qend:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_query_start(__pyx_v_self->_delegate); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2575
- *         This the index of the first base in :attr:`seq` that is not soft-clipped.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return query_start(self._delegate)
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qstart.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2580
- *     property qend:
- *         """end index of the aligned query portion of the sequence (0-based, exclusive)"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return query_end(self._delegate)
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qend_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qend_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4qend___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qend___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2580);
-
-  /* "pysam/csamfile.pyx":2581
- *         """end index of the aligned query portion of the sequence (0-based, exclusive)"""
- *         def __get__(self):
- *             return query_end(self._delegate)             # <<<<<<<<<<<<<<
- * 
- *     property qlen:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_query_end(__pyx_v_self->_delegate); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2580
- *     property qend:
- *         """end index of the aligned query portion of the sequence (0-based, exclusive)"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return query_end(self._delegate)
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qend.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2587
- * 
- *         This is equal to :attr:`qend` - :attr:`qstart`"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qlen_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qlen_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4qlen___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int32_t __pyx_t_2;
-  int32_t __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2587);
-
-  /* "pysam/csamfile.pyx":2589
- *         def __get__(self):
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             return query_end(src)-query_start(src)
- * 
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2590
- *             cdef bam1_t * src
- *             src = self._delegate
- *             return query_end(src)-query_start(src)             # <<<<<<<<<<<<<<
- * 
- *     property tags:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile_query_end(__pyx_v_src); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_start(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_4 = __Pyx_PyInt_From_int((__pyx_t_2 - __pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2587
- * 
- *         This is equal to :attr:`qend` - :attr:`qstart`"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qlen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2606
+/* "pysam/calignmentfile.pyx":2581
  *         multiple times.
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -26174,19 +26282,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qlen___get__(struct __
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tags_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tags_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tags___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tags___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   bam1_t *__pyx_v_src;
   uint8_t *__pyx_v_s;
   char __pyx_v_auxtag[3];
@@ -26214,9 +26322,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2606);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2581);
 
-  /* "pysam/csamfile.pyx":2615
+  /* "pysam/calignmentfile.pyx":2590
  *             cdef int32_t nvalues
  * 
  *             src = self._delegate             # <<<<<<<<<<<<<<
@@ -26226,7 +26334,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
   __pyx_t_1 = __pyx_v_self->_delegate;
   __pyx_v_src = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2616
+  /* "pysam/calignmentfile.pyx":2591
  * 
  *             src = self._delegate
  *             if src.l_data == 0:             # <<<<<<<<<<<<<<
@@ -26236,7 +26344,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
   __pyx_t_2 = ((__pyx_v_src->l_data == 0) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":2617
+    /* "pysam/calignmentfile.pyx":2592
  *             src = self._delegate
  *             if src.l_data == 0:
  *                 return []             # <<<<<<<<<<<<<<
@@ -26244,14 +26352,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  *             result = []
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2592; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_r = __pyx_t_3;
     __pyx_t_3 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/csamfile.pyx":2618
+  /* "pysam/calignmentfile.pyx":2593
  *             if src.l_data == 0:
  *                 return []
  *             s = pysam_bam_get_aux(src)             # <<<<<<<<<<<<<<
@@ -26260,19 +26368,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
   __pyx_v_s = pysam_bam_get_aux(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":2619
+  /* "pysam/calignmentfile.pyx":2594
  *                 return []
  *             s = pysam_bam_get_aux(src)
  *             result = []             # <<<<<<<<<<<<<<
  *             auxtag[2] = 0
  *             while s < (src.data + src.l_data):
  */
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __pyx_v_result = ((PyObject*)__pyx_t_3);
   __pyx_t_3 = 0;
 
-  /* "pysam/csamfile.pyx":2620
+  /* "pysam/calignmentfile.pyx":2595
  *             s = pysam_bam_get_aux(src)
  *             result = []
  *             auxtag[2] = 0             # <<<<<<<<<<<<<<
@@ -26281,7 +26389,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
   (__pyx_v_auxtag[2]) = 0;
 
-  /* "pysam/csamfile.pyx":2621
+  /* "pysam/calignmentfile.pyx":2596
  *             result = []
  *             auxtag[2] = 0
  *             while s < (src.data + src.l_data):             # <<<<<<<<<<<<<<
@@ -26292,7 +26400,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
     __pyx_t_2 = ((__pyx_v_s < (__pyx_v_src->data + __pyx_v_src->l_data)) != 0);
     if (!__pyx_t_2) break;
 
-    /* "pysam/csamfile.pyx":2623
+    /* "pysam/calignmentfile.pyx":2598
  *             while s < (src.data + src.l_data):
  *                 # get tag
  *                 auxtag[0] = s[0]             # <<<<<<<<<<<<<<
@@ -26301,7 +26409,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
     (__pyx_v_auxtag[0]) = (__pyx_v_s[0]);
 
-    /* "pysam/csamfile.pyx":2624
+    /* "pysam/calignmentfile.pyx":2599
  *                 # get tag
  *                 auxtag[0] = s[0]
  *                 auxtag[1] = s[1]             # <<<<<<<<<<<<<<
@@ -26310,7 +26418,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
     (__pyx_v_auxtag[1]) = (__pyx_v_s[1]);
 
-    /* "pysam/csamfile.pyx":2625
+    /* "pysam/calignmentfile.pyx":2600
  *                 auxtag[0] = s[0]
  *                 auxtag[1] = s[1]
  *                 s += 2             # <<<<<<<<<<<<<<
@@ -26319,7 +26427,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
     __pyx_v_s = (__pyx_v_s + 2);
 
-    /* "pysam/csamfile.pyx":2626
+    /* "pysam/calignmentfile.pyx":2601
  *                 auxtag[1] = s[1]
  *                 s += 2
  *                 auxtype = s[0]             # <<<<<<<<<<<<<<
@@ -26328,7 +26436,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
     __pyx_v_auxtype = (__pyx_v_s[0]);
 
-    /* "pysam/csamfile.pyx":2649
+    /* "pysam/calignmentfile.pyx":2624
  *                     # +1 for NULL terminated string
  *                     s += len(value) + 1
  *                 elif auxtype == 'B':             # <<<<<<<<<<<<<<
@@ -26337,7 +26445,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
     switch (__pyx_v_auxtype) {
 
-      /* "pysam/csamfile.pyx":2627
+      /* "pysam/calignmentfile.pyx":2602
  *                 s += 2
  *                 auxtype = s[0]
  *                 if auxtype in ('c', 'C'):             # <<<<<<<<<<<<<<
@@ -26347,19 +26455,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       case 'c':
       case 'C':
 
-      /* "pysam/csamfile.pyx":2628
+      /* "pysam/calignmentfile.pyx":2603
  *                 auxtype = s[0]
  *                 if auxtype in ('c', 'C'):
  *                     value = <int>bam_aux2i(s)             # <<<<<<<<<<<<<<
  *                     s += 1
  *                 elif auxtype in ('s', 'S'):
  */
-      __pyx_t_3 = __Pyx_PyInt_From_int(((int)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyInt_From_int(((int)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
       __pyx_t_3 = 0;
 
-      /* "pysam/csamfile.pyx":2629
+      /* "pysam/calignmentfile.pyx":2604
  *                 if auxtype in ('c', 'C'):
  *                     value = <int>bam_aux2i(s)
  *                     s += 1             # <<<<<<<<<<<<<<
@@ -26369,7 +26477,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       __pyx_v_s = (__pyx_v_s + 1);
       break;
 
-      /* "pysam/csamfile.pyx":2630
+      /* "pysam/calignmentfile.pyx":2605
  *                     value = <int>bam_aux2i(s)
  *                     s += 1
  *                 elif auxtype in ('s', 'S'):             # <<<<<<<<<<<<<<
@@ -26379,19 +26487,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       case 's':
       case 'S':
 
-      /* "pysam/csamfile.pyx":2631
+      /* "pysam/calignmentfile.pyx":2606
  *                     s += 1
  *                 elif auxtype in ('s', 'S'):
  *                     value = <int>bam_aux2i(s)             # <<<<<<<<<<<<<<
  *                     s += 2
  *                 elif auxtype in ('i', 'I'):
  */
-      __pyx_t_3 = __Pyx_PyInt_From_int(((int)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyInt_From_int(((int)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
       __pyx_t_3 = 0;
 
-      /* "pysam/csamfile.pyx":2632
+      /* "pysam/calignmentfile.pyx":2607
  *                 elif auxtype in ('s', 'S'):
  *                     value = <int>bam_aux2i(s)
  *                     s += 2             # <<<<<<<<<<<<<<
@@ -26401,7 +26509,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       __pyx_v_s = (__pyx_v_s + 2);
       break;
 
-      /* "pysam/csamfile.pyx":2633
+      /* "pysam/calignmentfile.pyx":2608
  *                     value = <int>bam_aux2i(s)
  *                     s += 2
  *                 elif auxtype in ('i', 'I'):             # <<<<<<<<<<<<<<
@@ -26411,19 +26519,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       case 'i':
       case 'I':
 
-      /* "pysam/csamfile.pyx":2634
+      /* "pysam/calignmentfile.pyx":2609
  *                     s += 2
  *                 elif auxtype in ('i', 'I'):
  *                     value = <int32_t>bam_aux2i(s)             # <<<<<<<<<<<<<<
  *                     s += 4
  *                 elif auxtype == 'f':
  */
-      __pyx_t_3 = __Pyx_PyInt_From_int32_t(((int32_t)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyInt_From_int32_t(((int32_t)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
       __pyx_t_3 = 0;
 
-      /* "pysam/csamfile.pyx":2635
+      /* "pysam/calignmentfile.pyx":2610
  *                 elif auxtype in ('i', 'I'):
  *                     value = <int32_t>bam_aux2i(s)
  *                     s += 4             # <<<<<<<<<<<<<<
@@ -26433,7 +26541,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       __pyx_v_s = (__pyx_v_s + 4);
       break;
 
-      /* "pysam/csamfile.pyx":2636
+      /* "pysam/calignmentfile.pyx":2611
  *                     value = <int32_t>bam_aux2i(s)
  *                     s += 4
  *                 elif auxtype == 'f':             # <<<<<<<<<<<<<<
@@ -26442,19 +26550,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
       case 'f':
 
-      /* "pysam/csamfile.pyx":2637
+      /* "pysam/calignmentfile.pyx":2612
  *                     s += 4
  *                 elif auxtype == 'f':
  *                     value = <float>bam_aux2f(s)             # <<<<<<<<<<<<<<
  *                     s += 4
  *                 elif auxtype == 'd':
  */
-      __pyx_t_3 = PyFloat_FromDouble(((float)bam_aux2f(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyFloat_FromDouble(((float)bam_aux2f(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2612; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
       __pyx_t_3 = 0;
 
-      /* "pysam/csamfile.pyx":2638
+      /* "pysam/calignmentfile.pyx":2613
  *                 elif auxtype == 'f':
  *                     value = <float>bam_aux2f(s)
  *                     s += 4             # <<<<<<<<<<<<<<
@@ -26464,7 +26572,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       __pyx_v_s = (__pyx_v_s + 4);
       break;
 
-      /* "pysam/csamfile.pyx":2639
+      /* "pysam/calignmentfile.pyx":2614
  *                     value = <float>bam_aux2f(s)
  *                     s += 4
  *                 elif auxtype == 'd':             # <<<<<<<<<<<<<<
@@ -26473,19 +26581,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
       case 'd':
 
-      /* "pysam/csamfile.pyx":2640
+      /* "pysam/calignmentfile.pyx":2615
  *                     s += 4
  *                 elif auxtype == 'd':
  *                     value = <double>bam_aux2f(s)             # <<<<<<<<<<<<<<
  *                     s += 8
  *                 elif auxtype == 'A':
  */
-      __pyx_t_3 = PyFloat_FromDouble(((double)bam_aux2f(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyFloat_FromDouble(((double)bam_aux2f(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
       __pyx_t_3 = 0;
 
-      /* "pysam/csamfile.pyx":2641
+      /* "pysam/calignmentfile.pyx":2616
  *                 elif auxtype == 'd':
  *                     value = <double>bam_aux2f(s)
  *                     s += 8             # <<<<<<<<<<<<<<
@@ -26495,7 +26603,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       __pyx_v_s = (__pyx_v_s + 8);
       break;
 
-      /* "pysam/csamfile.pyx":2642
+      /* "pysam/calignmentfile.pyx":2617
  *                     value = <double>bam_aux2f(s)
  *                     s += 8
  *                 elif auxtype == 'A':             # <<<<<<<<<<<<<<
@@ -26504,22 +26612,22 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
       case 'A':
 
-      /* "pysam/csamfile.pyx":2643
+      /* "pysam/calignmentfile.pyx":2618
  *                     s += 8
  *                 elif auxtype == 'A':
  *                     value = "%c" % <char>bam_aux2A(s)             # <<<<<<<<<<<<<<
  *                     s += 1
  *                 elif auxtype in ('Z', 'H'):
  */
-      __pyx_t_3 = __Pyx_PyInt_From_char(((char)bam_aux2A(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2643; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyInt_From_char(((char)bam_aux2A(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_c_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2643; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_c_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_4);
       __pyx_t_4 = 0;
 
-      /* "pysam/csamfile.pyx":2644
+      /* "pysam/calignmentfile.pyx":2619
  *                 elif auxtype == 'A':
  *                     value = "%c" % <char>bam_aux2A(s)
  *                     s += 1             # <<<<<<<<<<<<<<
@@ -26529,7 +26637,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       __pyx_v_s = (__pyx_v_s + 1);
       break;
 
-      /* "pysam/csamfile.pyx":2645
+      /* "pysam/calignmentfile.pyx":2620
  *                     value = "%c" % <char>bam_aux2A(s)
  *                     s += 1
  *                 elif auxtype in ('Z', 'H'):             # <<<<<<<<<<<<<<
@@ -26539,30 +26647,30 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       case 'Z':
       case 'H':
 
-      /* "pysam/csamfile.pyx":2646
+      /* "pysam/calignmentfile.pyx":2621
  *                     s += 1
  *                 elif auxtype in ('Z', 'H'):
  *                     value = _charptr_to_str(<char*>bam_aux2Z(s))             # <<<<<<<<<<<<<<
  *                     # +1 for NULL terminated string
  *                     s += len(value) + 1
  */
-      __pyx_t_4 = __pyx_f_5pysam_8csamfile__charptr_to_str(((char *)bam_aux2Z(__pyx_v_s))); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __pyx_f_5pysam_14calignmentfile__charptr_to_str(((char *)bam_aux2Z(__pyx_v_s))); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_4);
       __pyx_t_4 = 0;
 
-      /* "pysam/csamfile.pyx":2648
+      /* "pysam/calignmentfile.pyx":2623
  *                     value = _charptr_to_str(<char*>bam_aux2Z(s))
  *                     # +1 for NULL terminated string
  *                     s += len(value) + 1             # <<<<<<<<<<<<<<
  *                 elif auxtype == 'B':
  *                     s += 1
  */
-      __pyx_t_5 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_v_s = (__pyx_v_s + (__pyx_t_5 + 1));
       break;
 
-      /* "pysam/csamfile.pyx":2649
+      /* "pysam/calignmentfile.pyx":2624
  *                     # +1 for NULL terminated string
  *                     s += len(value) + 1
  *                 elif auxtype == 'B':             # <<<<<<<<<<<<<<
@@ -26571,7 +26679,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
       case 'B':
 
-      /* "pysam/csamfile.pyx":2650
+      /* "pysam/calignmentfile.pyx":2625
  *                     s += len(value) + 1
  *                 elif auxtype == 'B':
  *                     s += 1             # <<<<<<<<<<<<<<
@@ -26580,14 +26688,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
       __pyx_v_s = (__pyx_v_s + 1);
 
-      /* "pysam/csamfile.pyx":2651
+      /* "pysam/calignmentfile.pyx":2626
  *                 elif auxtype == 'B':
  *                     s += 1
  *                     byte_size, nvalues, value = convertBinaryTagToList( s )             # <<<<<<<<<<<<<<
  *                     # 5 for 1 char and 1 int
  *                     s += 5 + ( nvalues * byte_size) - 1
  */
-      __pyx_t_4 = __pyx_f_5pysam_8csamfile_convertBinaryTagToList(__pyx_v_s); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __pyx_f_5pysam_14calignmentfile_convertBinaryTagToList(__pyx_v_s); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
       if ((likely(PyTuple_CheckExact(__pyx_t_4))) || (PyList_CheckExact(__pyx_t_4))) {
         PyObject* sequence = __pyx_t_4;
@@ -26599,7 +26707,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
         if (unlikely(size != 3)) {
           if (size > 3) __Pyx_RaiseTooManyValuesError(3);
           else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         #if CYTHON_COMPILING_IN_CPYTHON
         if (likely(PyTuple_CheckExact(sequence))) {
@@ -26615,17 +26723,17 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
         __Pyx_INCREF(__pyx_t_6);
         __Pyx_INCREF(__pyx_t_7);
         #else
-        __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_3);
-        __pyx_t_6 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_7 = PySequence_ITEM(sequence, 2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PySequence_ITEM(sequence, 2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         #endif
         __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
       } else {
         Py_ssize_t index = -1;
-        __pyx_t_8 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
         __pyx_t_9 = Py_TYPE(__pyx_t_8)->tp_iternext;
@@ -26635,7 +26743,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
         __Pyx_GOTREF(__pyx_t_6);
         index = 2; __pyx_t_7 = __pyx_t_9(__pyx_t_8); if (unlikely(!__pyx_t_7)) goto __pyx_L6_unpacking_failed;
         __Pyx_GOTREF(__pyx_t_7);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_9 = NULL;
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
         goto __pyx_L7_unpacking_done;
@@ -26643,19 +26751,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
         __pyx_t_9 = NULL;
         if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_L7_unpacking_done:;
       }
-      __pyx_t_10 = __Pyx_PyInt_As_uint8_t(__pyx_t_3); if (unlikely((__pyx_t_10 == (uint8_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyInt_As_uint8_t(__pyx_t_3); if (unlikely((__pyx_t_10 == (uint8_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_11 = __Pyx_PyInt_As_int32_t(__pyx_t_6); if (unlikely((__pyx_t_11 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyInt_As_int32_t(__pyx_t_6); if (unlikely((__pyx_t_11 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __pyx_v_byte_size = __pyx_t_10;
       __pyx_v_nvalues = __pyx_t_11;
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_7);
       __pyx_t_7 = 0;
 
-      /* "pysam/csamfile.pyx":2653
+      /* "pysam/calignmentfile.pyx":2628
  *                     byte_size, nvalues, value = convertBinaryTagToList( s )
  *                     # 5 for 1 char and 1 int
  *                     s += 5 + ( nvalues * byte_size) - 1             # <<<<<<<<<<<<<<
@@ -26666,33 +26774,33 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
       break;
       default:
 
-      /* "pysam/csamfile.pyx":2655
+      /* "pysam/calignmentfile.pyx":2630
  *                     s += 5 + ( nvalues * byte_size) - 1
  *                 else:
  *                     raise KeyError("unknown type '%s'" % auxtype)             # <<<<<<<<<<<<<<
  * 
  *                 s += 1
  */
-      __pyx_t_4 = __Pyx_PyInt_From_char(__pyx_v_auxtype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyInt_From_char(__pyx_v_auxtype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_unknown_type_s, __pyx_t_4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_unknown_type_s, __pyx_t_4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
       PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_7);
       __Pyx_GIVEREF(__pyx_t_7);
       __pyx_t_7 = 0;
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
       __Pyx_Raise(__pyx_t_7, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       break;
     }
 
-    /* "pysam/csamfile.pyx":2657
+    /* "pysam/calignmentfile.pyx":2632
  *                     raise KeyError("unknown type '%s'" % auxtype)
  * 
  *                 s += 1             # <<<<<<<<<<<<<<
@@ -26701,16 +26809,16 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
     __pyx_v_s = (__pyx_v_s + 1);
 
-    /* "pysam/csamfile.pyx":2659
+    /* "pysam/calignmentfile.pyx":2634
  *                 s += 1
  * 
  *                 result.append((_charptr_to_str(auxtag), value))             # <<<<<<<<<<<<<<
  * 
  *             return result
  */
-    __pyx_t_7 = __pyx_f_5pysam_8csamfile__charptr_to_str(__pyx_v_auxtag); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __pyx_f_5pysam_14calignmentfile__charptr_to_str(__pyx_v_auxtag); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_7);
     __Pyx_GIVEREF(__pyx_t_7);
@@ -26718,11 +26826,11 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
     PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_v_value);
     __Pyx_GIVEREF(__pyx_v_value);
     __pyx_t_7 = 0;
-    __pyx_t_12 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_4); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_12 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_4); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   }
 
-  /* "pysam/csamfile.pyx":2661
+  /* "pysam/calignmentfile.pyx":2636
  *                 result.append((_charptr_to_str(auxtag), value))
  * 
  *             return result             # <<<<<<<<<<<<<<
@@ -26734,7 +26842,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
   __pyx_r = __pyx_v_result;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2606
+  /* "pysam/calignmentfile.pyx":2581
  *         multiple times.
  *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -26749,7 +26857,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
   __Pyx_XDECREF(__pyx_t_6);
   __Pyx_XDECREF(__pyx_t_7);
   __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tags.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.tags.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_result);
@@ -26760,7 +26868,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2663
+/* "pysam/calignmentfile.pyx":2638
  *             return result
  * 
  *         def __set__(self, tags):             # <<<<<<<<<<<<<<
@@ -26769,19 +26877,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tags); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tags) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tags_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tags); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tags_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tags) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_tags));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tags_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_tags));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tags) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tags_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_tags) {
   bam1_t *__pyx_v_src;
   uint8_t *__pyx_v_s;
   char *__pyx_v_temp;
@@ -26815,9 +26923,9 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2663);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2638);
 
-  /* "pysam/csamfile.pyx":2667
+  /* "pysam/calignmentfile.pyx":2642
  *             cdef uint8_t * s
  *             cdef char * temp
  *             cdef int new_size = 0             # <<<<<<<<<<<<<<
@@ -26826,7 +26934,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
  */
   __pyx_v_new_size = 0;
 
-  /* "pysam/csamfile.pyx":2669
+  /* "pysam/calignmentfile.pyx":2644
  *             cdef int new_size = 0
  *             cdef int old_size
  *             src = self._delegate             # <<<<<<<<<<<<<<
@@ -26836,26 +26944,26 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
   __pyx_t_1 = __pyx_v_self->_delegate;
   __pyx_v_src = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2670
+  /* "pysam/calignmentfile.pyx":2645
  *             cdef int old_size
  *             src = self._delegate
  *             fmts, args = ["<"], []             # <<<<<<<<<<<<<<
  * 
  *             if tags is not None and len(tags) > 0:
  */
-  __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2670; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(__pyx_kp_s__76);
-  PyList_SET_ITEM(__pyx_t_2, 0, __pyx_kp_s__76);
-  __Pyx_GIVEREF(__pyx_kp_s__76);
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2670; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(__pyx_kp_s__74);
+  PyList_SET_ITEM(__pyx_t_2, 0, __pyx_kp_s__74);
+  __Pyx_GIVEREF(__pyx_kp_s__74);
+  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __pyx_v_fmts = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
   __pyx_v_args = ((PyObject*)__pyx_t_3);
   __pyx_t_3 = 0;
 
-  /* "pysam/csamfile.pyx":2672
+  /* "pysam/calignmentfile.pyx":2647
  *             fmts, args = ["<"], []
  * 
  *             if tags is not None and len(tags) > 0:             # <<<<<<<<<<<<<<
@@ -26864,7 +26972,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
  */
   __pyx_t_4 = (__pyx_v_tags != Py_None);
   if ((__pyx_t_4 != 0)) {
-    __pyx_t_5 = PyObject_Length(__pyx_v_tags); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2672; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyObject_Length(__pyx_v_tags); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_6 = ((__pyx_t_5 > 0) != 0);
     __pyx_t_7 = __pyx_t_6;
   } else {
@@ -26872,7 +26980,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
   }
   if (__pyx_t_7) {
 
-    /* "pysam/csamfile.pyx":2673
+    /* "pysam/calignmentfile.pyx":2648
  * 
  *             if tags is not None and len(tags) > 0:
  *                 for pytag, value in tags:             # <<<<<<<<<<<<<<
@@ -26883,7 +26991,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
       __pyx_t_3 = __pyx_v_tags; __Pyx_INCREF(__pyx_t_3); __pyx_t_5 = 0;
       __pyx_t_8 = NULL;
     } else {
-      __pyx_t_5 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_v_tags); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_v_tags); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __pyx_t_8 = Py_TYPE(__pyx_t_3)->tp_iternext;
     }
@@ -26891,16 +26999,16 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
       if (!__pyx_t_8 && PyList_CheckExact(__pyx_t_3)) {
         if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_3)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_8 && PyTuple_CheckExact(__pyx_t_3)) {
         if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_3)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_2 = __pyx_t_8(__pyx_t_3);
@@ -26908,7 +27016,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -26924,7 +27032,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
         if (unlikely(size != 2)) {
           if (size > 2) __Pyx_RaiseTooManyValuesError(2);
           else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         #if CYTHON_COMPILING_IN_CPYTHON
         if (likely(PyTuple_CheckExact(sequence))) {
@@ -26937,15 +27045,15 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
         __Pyx_INCREF(__pyx_t_9);
         __Pyx_INCREF(__pyx_t_10);
         #else
-        __pyx_t_9 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_10 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
         #endif
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       } else {
         Py_ssize_t index = -1;
-        __pyx_t_11 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext;
@@ -26953,7 +27061,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
         __Pyx_GOTREF(__pyx_t_9);
         index = 1; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L6_unpacking_failed;
         __Pyx_GOTREF(__pyx_t_10);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_12 = NULL;
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         goto __pyx_L7_unpacking_done;
@@ -26961,7 +27069,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         __pyx_t_12 = NULL;
         if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_L7_unpacking_done:;
       }
       __Pyx_XDECREF_SET(__pyx_v_pytag, __pyx_t_9);
@@ -26969,103 +27077,103 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_10);
       __pyx_t_10 = 0;
 
-      /* "pysam/csamfile.pyx":2674
+      /* "pysam/calignmentfile.pyx":2649
  *             if tags is not None and len(tags) > 0:
  *                 for pytag, value in tags:
  *                     convert_python_tag(pytag, value, fmts, args)             # <<<<<<<<<<<<<<
  *                 fmt = "".join(fmts)
  *                 new_size = struct.calcsize(fmt)
  */
-      __pyx_t_2 = __pyx_f_5pysam_8csamfile_convert_python_tag(__pyx_v_pytag, __pyx_v_value, __pyx_v_fmts, __pyx_v_args); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2674; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __pyx_f_5pysam_14calignmentfile_convert_python_tag(__pyx_v_pytag, __pyx_v_value, __pyx_v_fmts, __pyx_v_args); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2649; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     }
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-    /* "pysam/csamfile.pyx":2675
+    /* "pysam/calignmentfile.pyx":2650
  *                 for pytag, value in tags:
  *                     convert_python_tag(pytag, value, fmts, args)
  *                 fmt = "".join(fmts)             # <<<<<<<<<<<<<<
  *                 new_size = struct.calcsize(fmt)
  *                 buffer = ctypes.create_string_buffer(new_size)
  */
-    __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__9, __pyx_v_fmts); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__7, __pyx_v_fmts); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2650; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_v_fmt = ((PyObject*)__pyx_t_3);
     __pyx_t_3 = 0;
 
-    /* "pysam/csamfile.pyx":2676
+    /* "pysam/calignmentfile.pyx":2651
  *                     convert_python_tag(pytag, value, fmts, args)
  *                 fmt = "".join(fmts)
  *                 new_size = struct.calcsize(fmt)             # <<<<<<<<<<<<<<
  *                 buffer = ctypes.create_string_buffer(new_size)
  *                 struct.pack_into(fmt,
  */
-    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_struct); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_struct); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_calcsize); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_calcsize); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_fmt);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_fmt);
     __Pyx_GIVEREF(__pyx_v_fmt);
-    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_10); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_10); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     __pyx_v_new_size = __pyx_t_13;
 
-    /* "pysam/csamfile.pyx":2677
+    /* "pysam/calignmentfile.pyx":2652
  *                 fmt = "".join(fmts)
  *                 new_size = struct.calcsize(fmt)
  *                 buffer = ctypes.create_string_buffer(new_size)             # <<<<<<<<<<<<<<
  *                 struct.pack_into(fmt,
  *                                  buffer,
  */
-    __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_ctypes); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_ctypes); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_create_string_buffer); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_create_string_buffer); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __pyx_t_10 = __Pyx_PyInt_From_int(__pyx_v_new_size); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyInt_From_int(__pyx_v_new_size); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_10);
     __Pyx_GIVEREF(__pyx_t_10);
     __pyx_t_10 = 0;
-    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_v_buffer = __pyx_t_10;
     __pyx_t_10 = 0;
 
-    /* "pysam/csamfile.pyx":2678
+    /* "pysam/calignmentfile.pyx":2653
  *                 new_size = struct.calcsize(fmt)
  *                 buffer = ctypes.create_string_buffer(new_size)
  *                 struct.pack_into(fmt,             # <<<<<<<<<<<<<<
  *                                  buffer,
  *                                  0,
  */
-    __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_struct); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_struct); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_pack_into); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_pack_into); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
 
-    /* "pysam/csamfile.pyx":2679
+    /* "pysam/calignmentfile.pyx":2654
  *                 buffer = ctypes.create_string_buffer(new_size)
  *                 struct.pack_into(fmt,
  *                                  buffer,             # <<<<<<<<<<<<<<
  *                                  0,
  *                                  *args)
  */
-    __pyx_t_10 = PyTuple_New(3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyTuple_New(3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_INCREF(__pyx_v_fmt);
     PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_v_fmt);
@@ -27077,20 +27185,20 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
     PyTuple_SET_ITEM(__pyx_t_10, 2, __pyx_int_0);
     __Pyx_GIVEREF(__pyx_int_0);
 
-    /* "pysam/csamfile.pyx":2678
+    /* "pysam/calignmentfile.pyx":2653
  *                 new_size = struct.calcsize(fmt)
  *                 buffer = ctypes.create_string_buffer(new_size)
  *                 struct.pack_into(fmt,             # <<<<<<<<<<<<<<
  *                                  buffer,
  *                                  0,
  */
-    __pyx_t_3 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_9 = PyNumber_Add(__pyx_t_10, __pyx_t_3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyNumber_Add(__pyx_t_10, __pyx_t_3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -27099,7 +27207,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":2686
+  /* "pysam/calignmentfile.pyx":2661
  *             # If total_size == 0, the aux field will be
  *             # empty
  *             old_size = pysam_bam_get_l_aux(src)             # <<<<<<<<<<<<<<
@@ -27108,7 +27216,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
  */
   __pyx_v_old_size = pysam_bam_get_l_aux(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":2687
+  /* "pysam/calignmentfile.pyx":2662
  *             # empty
  *             old_size = pysam_bam_get_l_aux(src)
  *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
@@ -27117,7 +27225,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
  */
   pysam_bam_update(__pyx_v_src, __pyx_v_old_size, __pyx_v_new_size, pysam_bam_get_aux(__pyx_v_src));
 
-  /* "pysam/csamfile.pyx":2693
+  /* "pysam/calignmentfile.pyx":2668
  * 
  *             # copy data only if there is any
  *             if new_size > 0:             # <<<<<<<<<<<<<<
@@ -27127,7 +27235,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
   __pyx_t_7 = ((__pyx_v_new_size > 0) != 0);
   if (__pyx_t_7) {
 
-    /* "pysam/csamfile.pyx":2696
+    /* "pysam/calignmentfile.pyx":2671
  * 
  *                 # get location of new data
  *                 s = pysam_bam_get_aux(src)             # <<<<<<<<<<<<<<
@@ -27136,42 +27244,42 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
  */
     __pyx_v_s = pysam_bam_get_aux(__pyx_v_src);
 
-    /* "pysam/csamfile.pyx":2699
+    /* "pysam/calignmentfile.pyx":2674
  * 
  *                 # check if there is direct path from buffer.raw to tmp
  *                 p = buffer.raw             # <<<<<<<<<<<<<<
  *                 # create handle to make sure buffer stays alive long
  *                 # enough for memcpy, see issue 129
  */
-    if (unlikely(!__pyx_v_buffer)) { __Pyx_RaiseUnboundLocalError("buffer"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2699; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_buffer, __pyx_n_s_raw); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(!__pyx_v_buffer)) { __Pyx_RaiseUnboundLocalError("buffer"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2674; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_buffer, __pyx_n_s_raw); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2674; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_v_p = __pyx_t_3;
     __pyx_t_3 = 0;
 
-    /* "pysam/csamfile.pyx":2702
+    /* "pysam/calignmentfile.pyx":2677
  *                 # create handle to make sure buffer stays alive long
  *                 # enough for memcpy, see issue 129
  *                 temp = p             # <<<<<<<<<<<<<<
  *                 memcpy(s, temp, new_size)
  * 
  */
-    __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_p); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_p); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_temp = __pyx_t_14;
 
-    /* "pysam/csamfile.pyx":2703
+    /* "pysam/calignmentfile.pyx":2678
  *                 # enough for memcpy, see issue 129
  *                 temp = p
  *                 memcpy(s, temp, new_size)             # <<<<<<<<<<<<<<
  * 
- *     cpdef setTag(self, tag, value,
+ *     property bin:
  */
     memcpy(__pyx_v_s, __pyx_v_temp, __pyx_v_new_size);
     goto __pyx_L8;
   }
   __pyx_L8:;
 
-  /* "pysam/csamfile.pyx":2663
+  /* "pysam/calignmentfile.pyx":2638
  *             return result
  * 
  *         def __set__(self, tags):             # <<<<<<<<<<<<<<
@@ -27188,7 +27296,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
   __Pyx_XDECREF(__pyx_t_9);
   __Pyx_XDECREF(__pyx_t_10);
   __Pyx_XDECREF(__pyx_t_11);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tags.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.tags.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_fmts);
@@ -27203,675 +27311,344 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2705
- *                 memcpy(s, temp, new_size)
- * 
- *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
- *                  value_type = None,
- *                  replace = True):
+/* "pysam/calignmentfile.pyx":2682
+ *     property bin:
+ *         """properties bin"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return pysam_get_bin(self._delegate)
+ *         def __set__(self, bin):
  */
 
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_11AlignedRead_setTag(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag, PyObject *__pyx_v_value, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag *__pyx_optional_args) {
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3bin_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3bin_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3bin___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
-  /* "pysam/csamfile.pyx":2706
- * 
- *     cpdef setTag(self, tag, value,
- *                  value_type = None,             # <<<<<<<<<<<<<<
- *                  replace = True):
- *         '''
- */
-  PyObject *__pyx_v_value_type = ((PyObject *)Py_None);
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":2707
- *     cpdef setTag(self, tag, value,
- *                  value_type = None,
- *                  replace = True):             # <<<<<<<<<<<<<<
- *         '''
- *         Set optional field of alignment *tag* to *value*.  *value_type* may be specified,
- */
-  PyObject *__pyx_v_replace = ((PyObject *)Py_True);
-  int __pyx_v_value_size;
-  uint8_t *__pyx_v_value_ptr;
-  uint8_t *__pyx_v_existing_ptr;
-  uint8_t __pyx_v_type_code;
-  float __pyx_v_float_value;
-  double __pyx_v_double_value;
-  int32_t __pyx_v_int_value;
-  bam1_t *__pyx_v_src;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3bin___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  bam1_t *__pyx_t_4;
-  Py_ssize_t __pyx_t_5;
-  int __pyx_t_6;
-  uint8_t __pyx_t_7;
-  struct __pyx_opt_args_5pysam_8csamfile_get_type_code __pyx_t_8;
-  char *__pyx_t_9;
-  int32_t __pyx_t_10;
-  double __pyx_t_11;
-  float __pyx_t_12;
-  char const *__pyx_t_13;
-  char const *__pyx_t_14;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("setTag", 0);
-  __Pyx_TraceCall("setTag", __pyx_f[0], 2705);
-  if (__pyx_optional_args) {
-    if (__pyx_optional_args->__pyx_n > 0) {
-      __pyx_v_value_type = __pyx_optional_args->value_type;
-      if (__pyx_optional_args->__pyx_n > 1) {
-        __pyx_v_replace = __pyx_optional_args->replace;
-      }
-    }
-  }
-  __Pyx_INCREF(__pyx_v_tag);
-  __Pyx_INCREF(__pyx_v_value);
-
-  /* "pysam/csamfile.pyx":2705
- *                 memcpy(s, temp, new_size)
- * 
- *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
- *                  value_type = None,
- *                  replace = True):
- */
-  /* Check if called by wrapper */
-  if (unlikely(__pyx_skip_dispatch)) ;
-  /* Check if overridden in Python */
-  else if (unlikely(Py_TYPE(((PyObject *)__pyx_v_self))->tp_dictoffset != 0)) {
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setTag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    if (!PyCFunction_Check(__pyx_t_1) || (PyCFunction_GET_FUNCTION(__pyx_t_1) != (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag)) {
-      __Pyx_XDECREF(__pyx_r);
-      __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_INCREF(__pyx_v_tag);
-      PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_tag);
-      __Pyx_GIVEREF(__pyx_v_tag);
-      __Pyx_INCREF(__pyx_v_value);
-      PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value);
-      __Pyx_GIVEREF(__pyx_v_value);
-      __Pyx_INCREF(__pyx_v_value_type);
-      PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_value_type);
-      __Pyx_GIVEREF(__pyx_v_value_type);
-      __Pyx_INCREF(__pyx_v_replace);
-      PyTuple_SET_ITEM(__pyx_t_2, 3, __pyx_v_replace);
-      __Pyx_GIVEREF(__pyx_v_replace);
-      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_r = __pyx_t_3;
-      __pyx_t_3 = 0;
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      goto __pyx_L0;
-    }
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  }
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2682);
 
-  /* "pysam/csamfile.pyx":2723
- *         cdef double   double_value
- *         cdef int32_t  int_value
- *         cdef bam1_t * src = self._delegate             # <<<<<<<<<<<<<<
- *         cdef char * _value_type
- * 
+  /* "pysam/calignmentfile.pyx":2683
+ *         """properties bin"""
+ *         def __get__(self):
+ *             return pysam_get_bin(self._delegate)             # <<<<<<<<<<<<<<
+ *         def __set__(self, bin):
+ *             pysam_set_bin(self._delegate, bin)
  */
-  __pyx_t_4 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_4;
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyInt_From_uint16_t(pysam_get_bin(__pyx_v_self->_delegate)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2683; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2726
- *         cdef char * _value_type
- * 
- *         if len(tag) != 2:             # <<<<<<<<<<<<<<
- *             raise ValueError('Invalid tag: %s' % tag)
- * 
+  /* "pysam/calignmentfile.pyx":2682
+ *     property bin:
+ *         """properties bin"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return pysam_get_bin(self._delegate)
+ *         def __set__(self, bin):
  */
-  __pyx_t_5 = PyObject_Length(__pyx_v_tag); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_6 = ((__pyx_t_5 != 2) != 0);
-  if (__pyx_t_6) {
 
-    /* "pysam/csamfile.pyx":2727
- * 
- *         if len(tag) != 2:
- *             raise ValueError('Invalid tag: %s' % tag)             # <<<<<<<<<<<<<<
- * 
- *         type_code = get_type_code(value, value_type)
- */
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_Invalid_tag_s, __pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.bin.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":2729
- *             raise ValueError('Invalid tag: %s' % tag)
+/* "pysam/calignmentfile.pyx":2684
+ *         def __get__(self):
+ *             return pysam_get_bin(self._delegate)
+ *         def __set__(self, bin):             # <<<<<<<<<<<<<<
+ *             pysam_set_bin(self._delegate, bin)
  * 
- *         type_code = get_type_code(value, value_type)             # <<<<<<<<<<<<<<
- *         if type_code == 0:
- *             raise ValueError("can't guess type or invalid type code specified")
  */
-  __pyx_t_8.__pyx_n = 1;
-  __pyx_t_8.value_type = __pyx_v_value_type;
-  __pyx_t_7 = __pyx_f_5pysam_8csamfile_get_type_code(__pyx_v_value, &__pyx_t_8); 
-  __pyx_v_type_code = __pyx_t_7;
 
-  /* "pysam/csamfile.pyx":2730
- * 
- *         type_code = get_type_code(value, value_type)
- *         if type_code == 0:             # <<<<<<<<<<<<<<
- *             raise ValueError("can't guess type or invalid type code specified")
- * 
- */
-  __pyx_t_6 = ((__pyx_v_type_code == 0) != 0);
-  if (__pyx_t_6) {
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3bin_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_bin); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3bin_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_bin) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3bin_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_bin));
 
-    /* "pysam/csamfile.pyx":2731
- *         type_code = get_type_code(value, value_type)
- *         if type_code == 0:
- *             raise ValueError("can't guess type or invalid type code specified")             # <<<<<<<<<<<<<<
- * 
- *         # Not Endian-safe, but then again neither is samtools!
- */
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__77, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2731; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2731; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":2746
- *             value_ptr    = <uint8_t*>&double_value
- *             value_size   = sizeof(double)
- *         elif type_code == 'f':             # <<<<<<<<<<<<<<
- *             float_value  = value
- *             value_ptr    = <uint8_t*>&float_value
- */
-  switch (__pyx_v_type_code) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3bin_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_bin) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  uint16_t __pyx_t_1;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2684);
 
-    /* "pysam/csamfile.pyx":2734
+  /* "pysam/calignmentfile.pyx":2685
+ *             return pysam_get_bin(self._delegate)
+ *         def __set__(self, bin):
+ *             pysam_set_bin(self._delegate, bin)             # <<<<<<<<<<<<<<
  * 
- *         # Not Endian-safe, but then again neither is samtools!
- *         if type_code == 'Z':             # <<<<<<<<<<<<<<
- *             value = _forceBytes( value )
- *             value_ptr    = <uint8_t*><char*>value
- */
-    case 'Z':
-
-    /* "pysam/csamfile.pyx":2735
- *         # Not Endian-safe, but then again neither is samtools!
- *         if type_code == 'Z':
- *             value = _forceBytes( value )             # <<<<<<<<<<<<<<
- *             value_ptr    = <uint8_t*><char*>value
- *             value_size   = len(value)+1
- */
-    __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_value); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_1);
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":2736
- *         if type_code == 'Z':
- *             value = _forceBytes( value )
- *             value_ptr    = <uint8_t*><char*>value             # <<<<<<<<<<<<<<
- *             value_size   = len(value)+1
- *         elif type_code == 'i':
- */
-    __pyx_t_9 = __Pyx_PyObject_AsString(__pyx_v_value); if (unlikely((!__pyx_t_9) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_value_ptr = ((uint8_t *)((char *)__pyx_t_9));
-
-    /* "pysam/csamfile.pyx":2737
- *             value = _forceBytes( value )
- *             value_ptr    = <uint8_t*><char*>value
- *             value_size   = len(value)+1             # <<<<<<<<<<<<<<
- *         elif type_code == 'i':
- *             int_value    = value
- */
-    __pyx_t_5 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_value_size = (__pyx_t_5 + 1);
-    break;
-
-    /* "pysam/csamfile.pyx":2738
- *             value_ptr    = <uint8_t*><char*>value
- *             value_size   = len(value)+1
- *         elif type_code == 'i':             # <<<<<<<<<<<<<<
- *             int_value    = value
- *             value_ptr    = <uint8_t*>&int_value
- */
-    case 'i':
-
-    /* "pysam/csamfile.pyx":2739
- *             value_size   = len(value)+1
- *         elif type_code == 'i':
- *             int_value    = value             # <<<<<<<<<<<<<<
- *             value_ptr    = <uint8_t*>&int_value
- *             value_size   = sizeof(int32_t)
- */
-    __pyx_t_10 = __Pyx_PyInt_As_int32_t(__pyx_v_value); if (unlikely((__pyx_t_10 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2739; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_int_value = __pyx_t_10;
-
-    /* "pysam/csamfile.pyx":2740
- *         elif type_code == 'i':
- *             int_value    = value
- *             value_ptr    = <uint8_t*>&int_value             # <<<<<<<<<<<<<<
- *             value_size   = sizeof(int32_t)
- *         elif type_code == 'd':
- */
-    __pyx_v_value_ptr = ((uint8_t *)(&__pyx_v_int_value));
-
-    /* "pysam/csamfile.pyx":2741
- *             int_value    = value
- *             value_ptr    = <uint8_t*>&int_value
- *             value_size   = sizeof(int32_t)             # <<<<<<<<<<<<<<
- *         elif type_code == 'd':
- *             double_value = value
- */
-    __pyx_v_value_size = (sizeof(int32_t));
-    break;
-
-    /* "pysam/csamfile.pyx":2742
- *             value_ptr    = <uint8_t*>&int_value
- *             value_size   = sizeof(int32_t)
- *         elif type_code == 'd':             # <<<<<<<<<<<<<<
- *             double_value = value
- *             value_ptr    = <uint8_t*>&double_value
- */
-    case 'd':
-
-    /* "pysam/csamfile.pyx":2743
- *             value_size   = sizeof(int32_t)
- *         elif type_code == 'd':
- *             double_value = value             # <<<<<<<<<<<<<<
- *             value_ptr    = <uint8_t*>&double_value
- *             value_size   = sizeof(double)
- */
-    __pyx_t_11 = __pyx_PyFloat_AsDouble(__pyx_v_value); if (unlikely((__pyx_t_11 == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_double_value = __pyx_t_11;
-
-    /* "pysam/csamfile.pyx":2744
- *         elif type_code == 'd':
- *             double_value = value
- *             value_ptr    = <uint8_t*>&double_value             # <<<<<<<<<<<<<<
- *             value_size   = sizeof(double)
- *         elif type_code == 'f':
- */
-    __pyx_v_value_ptr = ((uint8_t *)(&__pyx_v_double_value));
-
-    /* "pysam/csamfile.pyx":2745
- *             double_value = value
- *             value_ptr    = <uint8_t*>&double_value
- *             value_size   = sizeof(double)             # <<<<<<<<<<<<<<
- *         elif type_code == 'f':
- *             float_value  = value
- */
-    __pyx_v_value_size = (sizeof(double));
-    break;
-
-    /* "pysam/csamfile.pyx":2746
- *             value_ptr    = <uint8_t*>&double_value
- *             value_size   = sizeof(double)
- *         elif type_code == 'f':             # <<<<<<<<<<<<<<
- *             float_value  = value
- *             value_ptr    = <uint8_t*>&float_value
- */
-    case 'f':
-
-    /* "pysam/csamfile.pyx":2747
- *             value_size   = sizeof(double)
- *         elif type_code == 'f':
- *             float_value  = value             # <<<<<<<<<<<<<<
- *             value_ptr    = <uint8_t*>&float_value
- *             value_size   = sizeof(float)
- */
-    __pyx_t_12 = __pyx_PyFloat_AsFloat(__pyx_v_value); if (unlikely((__pyx_t_12 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_float_value = __pyx_t_12;
-
-    /* "pysam/csamfile.pyx":2748
- *         elif type_code == 'f':
- *             float_value  = value
- *             value_ptr    = <uint8_t*>&float_value             # <<<<<<<<<<<<<<
- *             value_size   = sizeof(float)
- *         else:
- */
-    __pyx_v_value_ptr = ((uint8_t *)(&__pyx_v_float_value));
-
-    /* "pysam/csamfile.pyx":2749
- *             float_value  = value
- *             value_ptr    = <uint8_t*>&float_value
- *             value_size   = sizeof(float)             # <<<<<<<<<<<<<<
- *         else:
- *             raise ValueError('Unsupported value_type in set_option')
- */
-    __pyx_v_value_size = (sizeof(float));
-    break;
-    default:
-
-    /* "pysam/csamfile.pyx":2751
- *             value_size   = sizeof(float)
- *         else:
- *             raise ValueError('Unsupported value_type in set_option')             # <<<<<<<<<<<<<<
  * 
- *         tag = _forceBytes( tag )
  */
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__78, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    break;
-  }
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_bin); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_set_bin(__pyx_v_self->_delegate, __pyx_t_1);
 
-  /* "pysam/csamfile.pyx":2753
- *             raise ValueError('Unsupported value_type in set_option')
+  /* "pysam/calignmentfile.pyx":2684
+ *         def __get__(self):
+ *             return pysam_get_bin(self._delegate)
+ *         def __set__(self, bin):             # <<<<<<<<<<<<<<
+ *             pysam_set_bin(self._delegate, bin)
  * 
- *         tag = _forceBytes( tag )             # <<<<<<<<<<<<<<
- *         if replace:
- *             existing_ptr = bam_aux_get(src, tag)
  */
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2753; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF_SET(__pyx_v_tag, __pyx_t_1);
-  __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":2754
- * 
- *         tag = _forceBytes( tag )
- *         if replace:             # <<<<<<<<<<<<<<
- *             existing_ptr = bam_aux_get(src, tag)
- *             if existing_ptr:
- */
-  __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_v_replace); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2754; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_6) {
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.bin.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-    /* "pysam/csamfile.pyx":2755
- *         tag = _forceBytes( tag )
- *         if replace:
- *             existing_ptr = bam_aux_get(src, tag)             # <<<<<<<<<<<<<<
- *             if existing_ptr:
- *                 bam_aux_del(src, existing_ptr)
+/* "pysam/calignmentfile.pyx":2696
+ *     property is_paired:
+ *         """true if read is paired in sequencing"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FPAIRED) != 0
+ *         def __set__(self,val):
  */
-    __pyx_t_13 = __Pyx_PyObject_AsString(__pyx_v_tag); if (unlikely((!__pyx_t_13) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2755; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_existing_ptr = bam_aux_get(__pyx_v_src, __pyx_t_13);
 
-    /* "pysam/csamfile.pyx":2756
- *         if replace:
- *             existing_ptr = bam_aux_get(src, tag)
- *             if existing_ptr:             # <<<<<<<<<<<<<<
- *                 bam_aux_del(src, existing_ptr)
- * 
- */
-    __pyx_t_6 = (__pyx_v_existing_ptr != 0);
-    if (__pyx_t_6) {
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_paired_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_paired_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_paired___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
-      /* "pysam/csamfile.pyx":2757
- *             existing_ptr = bam_aux_get(src, tag)
- *             if existing_ptr:
- *                 bam_aux_del(src, existing_ptr)             # <<<<<<<<<<<<<<
- * 
- *         bam_aux_append(src,
- */
-      bam_aux_del(__pyx_v_src, __pyx_v_existing_ptr);
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-    goto __pyx_L5;
-  }
-  __pyx_L5:;
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":2760
- * 
- *         bam_aux_append(src,
- *                        tag,             # <<<<<<<<<<<<<<
- *                        type_code,
- *                        value_size,
- */
-  __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_tag); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_paired___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2696);
 
-  /* "pysam/csamfile.pyx":2759
- *                 bam_aux_del(src, existing_ptr)
- * 
- *         bam_aux_append(src,             # <<<<<<<<<<<<<<
- *                        tag,
- *                        type_code,
+  /* "pysam/calignmentfile.pyx":2697
+ *         """true if read is paired in sequencing"""
+ *         def __get__(self):
+ *             return (self.flag & BAM_FPAIRED) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
  */
-  bam_aux_append(__pyx_v_src, __pyx_t_14, __pyx_v_type_code, __pyx_v_value_size, __pyx_v_value_ptr);
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FPAIRED); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2705
- *                 memcpy(s, temp, new_size)
- * 
- *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
- *                  value_type = None,
- *                  replace = True):
+  /* "pysam/calignmentfile.pyx":2696
+ *     property is_paired:
+ *         """true if read is paired in sequencing"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FPAIRED) != 0
+ *         def __set__(self,val):
  */
 
   /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.setTag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_paired.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_tag);
-  __Pyx_XDECREF(__pyx_v_value);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
+/* "pysam/calignmentfile.pyx":2698
+ *         def __get__(self):
+ *             return (self.flag & BAM_FPAIRED) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
+ * 
+ */
+
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_12setTag[] = "AlignedRead.setTag(self, tag, value, value_type=None, replace=True)\n\n        Set optional field of alignment *tag* to *value*.  *value_type* may be specified,\n        but if not the type will be inferred based on the Python type of *value*\n\n        An existing value of the same tag will be overwritten unless\n        *replace* is set to False.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_tag = 0;
-  PyObject *__pyx_v_value = 0;
-  PyObject *__pyx_v_value_type = 0;
-  PyObject *__pyx_v_replace = 0;
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_paired_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_paired_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_paired_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_paired_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("setTag (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_tag,&__pyx_n_s_value,&__pyx_n_s_value_type,&__pyx_n_s_replace,0};
-    PyObject* values[4] = {0,0,0,0};
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2698);
 
-    /* "pysam/csamfile.pyx":2706
+  /* "pysam/calignmentfile.pyx":2699
+ *             return (self.flag & BAM_FPAIRED) != 0
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)             # <<<<<<<<<<<<<<
  * 
- *     cpdef setTag(self, tag, value,
- *                  value_type = None,             # <<<<<<<<<<<<<<
- *                  replace = True):
- *         '''
+ *     property is_proper_pair:
  */
-    values[2] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":2707
- *     cpdef setTag(self, tag, value,
- *                  value_type = None,
- *                  replace = True):             # <<<<<<<<<<<<<<
- *         '''
- *         Set optional field of alignment *tag* to *value*.  *value_type* may be specified,
- */
-    values[3] = ((PyObject *)Py_True);
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_tag)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("setTag", 0, 2, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value_type);
-          if (value) { values[2] = value; kw_args--; }
-        }
-        case  3:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_replace);
-          if (value) { values[3] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setTag") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_tag = values[0];
-    __pyx_v_value = values[1];
-    __pyx_v_value_type = values[2];
-    __pyx_v_replace = values[3];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setTag", 0, 2, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.setTag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12setTag(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), __pyx_v_tag, __pyx_v_value, __pyx_v_value_type, __pyx_v_replace);
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FPAIRED);
 
-  /* "pysam/csamfile.pyx":2705
- *                 memcpy(s, temp, new_size)
+  /* "pysam/calignmentfile.pyx":2698
+ *         def __get__(self):
+ *             return (self.flag & BAM_FPAIRED) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
  * 
- *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
- *                  value_type = None,
- *                  replace = True):
  */
 
   /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12setTag(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag, PyObject *__pyx_v_value, PyObject *__pyx_v_value_type, PyObject *__pyx_v_replace) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag __pyx_t_2;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("setTag", 0);
-  __Pyx_TraceCall("setTag", __pyx_f[0], 2705);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2.__pyx_n = 2;
-  __pyx_t_2.value_type = __pyx_v_value_type;
-  __pyx_t_2.replace = __pyx_v_replace;
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead *)__pyx_v_self->__pyx_vtab)->setTag(__pyx_v_self, __pyx_v_tag, __pyx_v_value, 1, &__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_r = 0;
   goto __pyx_L0;
-
-  /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.setTag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_paired.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
   __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2767
- *     property flag:
- *         """properties flag"""
+/* "pysam/calignmentfile.pyx":2703
+ *     property is_proper_pair:
+ *         """true if read is mapped in a proper pair"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):
+ *             return (self.flag & BAM_FPROPER_PAIR) != 0
+ *         def __set__(self,val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4flag___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4flag___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2767);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2703);
 
-  /* "pysam/csamfile.pyx":2768
- *         """properties flag"""
+  /* "pysam/calignmentfile.pyx":2704
+ *         """true if read is mapped in a proper pair"""
  *         def __get__(self):
- *             return pysam_get_flag(self._delegate)             # <<<<<<<<<<<<<<
- *         def __set__(self, flag):
- *             pysam_set_flag(self._delegate, flag)
+ *             return (self.flag & BAM_FPROPER_PAIR) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint16_t(pysam_get_flag(__pyx_v_self->_delegate)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2704; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FPROPER_PAIR); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2704; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2704; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2704; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2767
- *     property flag:
- *         """properties flag"""
+  /* "pysam/calignmentfile.pyx":2703
+ *     property is_proper_pair:
+ *         """true if read is mapped in a proper pair"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):
+ *             return (self.flag & BAM_FPROPER_PAIR) != 0
+ *         def __set__(self,val):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.flag.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_proper_pair.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -27880,61 +27657,61 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4flag___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2769
+/* "pysam/calignmentfile.pyx":2705
  *         def __get__(self):
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):             # <<<<<<<<<<<<<<
- *             pysam_set_flag(self._delegate, flag)
- * 
+ *             return (self.flag & BAM_FPROPER_PAIR) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
+ *     property is_unmapped:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_flag); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_flag) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4flag_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_flag));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4flag_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_flag) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  uint8_t __pyx_t_1;
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2769);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2705);
 
-  /* "pysam/csamfile.pyx":2770
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):
- *             pysam_set_flag(self._delegate, flag)             # <<<<<<<<<<<<<<
- * 
- *     property rname:
+  /* "pysam/calignmentfile.pyx":2706
+ *             return (self.flag & BAM_FPROPER_PAIR) != 0
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)             # <<<<<<<<<<<<<<
+ *     property is_unmapped:
+ *         """true if read itself is unmapped"""
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint8_t(__pyx_v_flag); if (unlikely((__pyx_t_1 == (uint8_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_set_flag(__pyx_v_self->_delegate, __pyx_t_1);
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FPROPER_PAIR);
 
-  /* "pysam/csamfile.pyx":2769
+  /* "pysam/calignmentfile.pyx":2705
  *         def __get__(self):
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):             # <<<<<<<<<<<<<<
- *             pysam_set_flag(self._delegate, flag)
- * 
+ *             return (self.flag & BAM_FPROPER_PAIR) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
+ *     property is_unmapped:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.flag.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_proper_pair.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -27942,48 +27719,76 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4flag_2__set__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2788
- * 
- *         """
- *         def __get__(self): return self._delegate.core.tid             # <<<<<<<<<<<<<<
- *         def __set__(self, tid): self._delegate.core.tid = tid
- * 
+/* "pysam/calignmentfile.pyx":2709
+ *     property is_unmapped:
+ *         """true if read itself is unmapped"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FUNMAP) != 0
+ *         def __set__(self, val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5rname___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rname___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2788);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2709);
+
+  /* "pysam/calignmentfile.pyx":2710
+ *         """true if read itself is unmapped"""
+ *         def __get__(self):
+ *             return (self.flag & BAM_FUNMAP) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)
+ */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FUNMAP); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
+  /* "pysam/calignmentfile.pyx":2709
+ *     property is_unmapped:
+ *         """true if read itself is unmapped"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FUNMAP) != 0
+ *         def __set__(self, val):
+ */
+
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rname.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_unmapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -27992,45 +27797,61 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rname___get__(struct _
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2789
- *         """
- *         def __get__(self): return self._delegate.core.tid
- *         def __set__(self, tid): self._delegate.core.tid = tid             # <<<<<<<<<<<<<<
- * 
- *     property tid:
+/* "pysam/calignmentfile.pyx":2711
+ *         def __get__(self):
+ *             return (self.flag & BAM_FUNMAP) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)
+ *     property mate_is_unmapped:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5rname_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_tid));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rname_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tid) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2789);
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_tid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2789; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.tid = __pyx_t_1;
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2711);
+
+  /* "pysam/calignmentfile.pyx":2712
+ *             return (self.flag & BAM_FUNMAP) != 0
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)             # <<<<<<<<<<<<<<
+ *     property mate_is_unmapped:
+ *         """true if the mate is unmapped"""
+ */
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FUNMAP);
+
+  /* "pysam/calignmentfile.pyx":2711
+ *         def __get__(self):
+ *             return (self.flag & BAM_FUNMAP) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)
+ *     property mate_is_unmapped:
+ */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rname.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_unmapped.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -28038,48 +27859,76 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rname_2__set__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2802
- * 
- *         """
- *         def __get__(self): return self._delegate.core.tid             # <<<<<<<<<<<<<<
- *         def __set__(self, tid): self._delegate.core.tid = tid
- * 
+/* "pysam/calignmentfile.pyx":2715
+ *     property mate_is_unmapped:
+ *         """true if the mate is unmapped"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FMUNMAP) != 0
+ *         def __set__(self,val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3tid___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3tid___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2802);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2715);
+
+  /* "pysam/calignmentfile.pyx":2716
+ *         """true if the mate is unmapped"""
+ *         def __get__(self):
+ *             return (self.flag & BAM_FMUNMAP) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
+ */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FMUNMAP); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
+  /* "pysam/calignmentfile.pyx":2715
+ *     property mate_is_unmapped:
+ *         """true if the mate is unmapped"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FMUNMAP) != 0
+ *         def __set__(self,val):
+ */
+
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tid.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mate_is_unmapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -28088,45 +27937,61 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3tid___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2803
- *         """
- *         def __get__(self): return self._delegate.core.tid
- *         def __set__(self, tid): self._delegate.core.tid = tid             # <<<<<<<<<<<<<<
- * 
- *     property pos:
+/* "pysam/calignmentfile.pyx":2717
+ *         def __get__(self):
+ *             return (self.flag & BAM_FMUNMAP) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
+ *     property is_reverse:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3tid_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_tid));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3tid_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tid) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2803);
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_tid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.tid = __pyx_t_1;
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2717);
+
+  /* "pysam/calignmentfile.pyx":2718
+ *             return (self.flag & BAM_FMUNMAP) != 0
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)             # <<<<<<<<<<<<<<
+ *     property is_reverse:
+ *         """true if read is mapped to reverse strand"""
+ */
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FMUNMAP);
+
+  /* "pysam/calignmentfile.pyx":2717
+ *         def __get__(self):
+ *             return (self.flag & BAM_FMUNMAP) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
+ *     property is_reverse:
+ */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tid.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mate_is_unmapped.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -28134,48 +27999,76 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3tid_2__set__(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2807
- *     property pos:
- *         """0-based leftmost coordinate"""
- *         def __get__(self): return self._delegate.core.pos             # <<<<<<<<<<<<<<
- *         def __set__(self, pos):
- *             ## setting the position requires updating the "bin" attribute
+/* "pysam/calignmentfile.pyx":2721
+ *     property is_reverse:
+ *         """true if read is mapped to reverse strand"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FREVERSE) != 0
+ *         def __set__(self,val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10is_reverse_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10is_reverse_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3pos___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10is_reverse___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3pos___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10is_reverse___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2807);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2721);
+
+  /* "pysam/calignmentfile.pyx":2722
+ *         """true if read is mapped to reverse strand"""
+ *         def __get__(self):
+ *             return (self.flag & BAM_FREVERSE) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)
+ */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FREVERSE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
+  /* "pysam/calignmentfile.pyx":2721
+ *     property is_reverse:
+ *         """true if read is mapped to reverse strand"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FREVERSE) != 0
+ *         def __set__(self,val):
+ */
+
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_reverse.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -28184,107 +28077,61 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3pos___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2808
- *         """0-based leftmost coordinate"""
- *         def __get__(self): return self._delegate.core.pos
- *         def __set__(self, pos):             # <<<<<<<<<<<<<<
- *             ## setting the position requires updating the "bin" attribute
- *             cdef bam1_t * src
+/* "pysam/calignmentfile.pyx":2723
+ *         def __get__(self):
+ *             return (self.flag & BAM_FREVERSE) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)
+ *     property mate_is_reverse:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_pos); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_pos) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3pos_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_pos));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10is_reverse_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3pos_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_pos) {
-  bam1_t *__pyx_v_src;
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10is_reverse_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int32_t __pyx_t_2;
-  int __pyx_t_3;
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2808);
-
-  /* "pysam/csamfile.pyx":2811
- *             ## setting the position requires updating the "bin" attribute
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             src.core.pos = pos
- *             if pysam_get_n_cigar(src):
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2812
- *             cdef bam1_t * src
- *             src = self._delegate
- *             src.core.pos = pos             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src):
- *                 pysam_set_bin(src,
- */
-  __pyx_t_2 = __Pyx_PyInt_As_int32_t(__pyx_v_pos); if (unlikely((__pyx_t_2 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_src->core.pos = __pyx_t_2;
-
-  /* "pysam/csamfile.pyx":2813
- *             src = self._delegate
- *             src.core.pos = pos
- *             if pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 pysam_set_bin(src,
- *                               hts_reg2bin(
- */
-  __pyx_t_3 = (pysam_get_n_cigar(__pyx_v_src) != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":2814
- *             src.core.pos = pos
- *             if pysam_get_n_cigar(src):
- *                 pysam_set_bin(src,             # <<<<<<<<<<<<<<
- *                               hts_reg2bin(
- *                                   src.core.pos,
- */
-    pysam_set_bin(__pyx_v_src, hts_reg2bin(__pyx_v_src->core.pos, bam_endpos(__pyx_v_src), 14, 5));
-    goto __pyx_L3;
-  }
-  /*else*/ {
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2723);
 
-    /* "pysam/csamfile.pyx":2821
- *                                   5))
- *             else:
- *                 pysam_set_bin(src,             # <<<<<<<<<<<<<<
- *                               hts_reg2bin(
- *                                   src.core.pos,
+  /* "pysam/calignmentfile.pyx":2724
+ *             return (self.flag & BAM_FREVERSE) != 0
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)             # <<<<<<<<<<<<<<
+ *     property mate_is_reverse:
+ *         """true is read is mapped to reverse strand"""
  */
-    pysam_set_bin(__pyx_v_src, hts_reg2bin(__pyx_v_src->core.pos, (__pyx_v_src->core.pos + 1), 14, 5));
-  }
-  __pyx_L3:;
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FREVERSE);
 
-  /* "pysam/csamfile.pyx":2808
- *         """0-based leftmost coordinate"""
- *         def __get__(self): return self._delegate.core.pos
- *         def __set__(self, pos):             # <<<<<<<<<<<<<<
- *             ## setting the position requires updating the "bin" attribute
- *             cdef bam1_t * src
+  /* "pysam/calignmentfile.pyx":2723
+ *         def __get__(self):
+ *             return (self.flag & BAM_FREVERSE) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)
+ *     property mate_is_reverse:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.pos.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_reverse.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -28292,64 +28139,76 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3pos_2__set__(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2830
- *     property bin:
- *         """properties bin"""
+/* "pysam/calignmentfile.pyx":2727
+ *     property mate_is_reverse:
+ *         """true is read is mapped to reverse strand"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):
+ *             return (self.flag & BAM_FMREVERSE) != 0
+ *         def __set__(self,val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3bin___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2830);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2727);
 
-  /* "pysam/csamfile.pyx":2831
- *         """properties bin"""
+  /* "pysam/calignmentfile.pyx":2728
+ *         """true is read is mapped to reverse strand"""
  *         def __get__(self):
- *             return pysam_get_bin(self._delegate)             # <<<<<<<<<<<<<<
- *         def __set__(self, bin):
- *             pysam_set_bin(self._delegate, bin)
+ *             return (self.flag & BAM_FMREVERSE) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint16_t(pysam_get_bin(__pyx_v_self->_delegate)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FMREVERSE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2830
- *     property bin:
- *         """properties bin"""
+  /* "pysam/calignmentfile.pyx":2727
+ *     property mate_is_reverse:
+ *         """true is read is mapped to reverse strand"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):
+ *             return (self.flag & BAM_FMREVERSE) != 0
+ *         def __set__(self,val):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.bin.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mate_is_reverse.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -28358,28 +28217,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3bin___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2832
+/* "pysam/calignmentfile.pyx":2729
  *         def __get__(self):
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):             # <<<<<<<<<<<<<<
- *             pysam_set_bin(self._delegate, bin)
- * 
+ *             return (self.flag & BAM_FMREVERSE) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
+ *     property is_read1:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_bin); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_bin) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_bin));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_bin) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   uint16_t __pyx_t_1;
@@ -28388,31 +28247,31 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin_2__set__(struct __pyx_ob
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2832);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2729);
 
-  /* "pysam/csamfile.pyx":2833
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):
- *             pysam_set_bin(self._delegate, bin)             # <<<<<<<<<<<<<<
- * 
- *     property rlen:
+  /* "pysam/calignmentfile.pyx":2730
+ *             return (self.flag & BAM_FMREVERSE) != 0
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)             # <<<<<<<<<<<<<<
+ *     property is_read1:
+ *         """true if this is read1"""
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_bin); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_set_bin(__pyx_v_self->_delegate, __pyx_t_1);
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2730; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FMREVERSE);
 
-  /* "pysam/csamfile.pyx":2832
+  /* "pysam/calignmentfile.pyx":2729
  *         def __get__(self):
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):             # <<<<<<<<<<<<<<
- *             pysam_set_bin(self._delegate, bin)
- * 
+ *             return (self.flag & BAM_FMREVERSE) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
+ *     property is_read1:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.bin.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mate_is_reverse.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -28420,64 +28279,76 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin_2__set__(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2842
- * 
- *         Returns 0 if not available."""
+/* "pysam/calignmentfile.pyx":2733
+ *     property is_read1:
+ *         """true if this is read1"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.l_qseq
- * 
+ *             return (self.flag & BAM_FREAD1) != 0
+ *         def __set__(self,val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4rlen_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4rlen_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read1_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read1_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4rlen___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read1___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4rlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read1___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2842);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2733);
 
-  /* "pysam/csamfile.pyx":2843
- *         Returns 0 if not available."""
+  /* "pysam/calignmentfile.pyx":2734
+ *         """true if this is read1"""
  *         def __get__(self):
- *             return self._delegate.core.l_qseq             # <<<<<<<<<<<<<<
- * 
- *     property aend:
+ *             return (self.flag & BAM_FREAD1) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FREAD1)
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.l_qseq); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FREAD1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2842
- * 
- *         Returns 0 if not available."""
+  /* "pysam/calignmentfile.pyx":2733
+ *     property is_read1:
+ *         """true if this is read1"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.l_qseq
- * 
+ *             return (self.flag & BAM_FREAD1) != 0
+ *         def __set__(self,val):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rlen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_read1.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -28486,318 +28357,138 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4rlen___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2850
- *         aend points to one past the last aligned residue.
- *         Returns None if not available.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
+/* "pysam/calignmentfile.pyx":2735
+ *         def __get__(self):
+ *             return (self.flag & BAM_FREAD1) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FREAD1)
+ *     property is_read2:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4aend_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4aend_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read1_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read1_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4aend___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read1_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4aend___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read1_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_t_7;
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2850);
-
-  /* "pysam/csamfile.pyx":2852
- *         def __get__(self):
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2853
- *             cdef bam1_t * src
- *             src = self._delegate
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return None
- *             return bam_endpos(src)
- */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyInt_From_int(BAM_FUNMAP); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyNumber_And(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (!__pyx_t_5) {
-    __pyx_t_6 = (pysam_get_n_cigar(__pyx_v_src) == 0);
-    __pyx_t_7 = __pyx_t_6;
-  } else {
-    __pyx_t_7 = __pyx_t_5;
-  }
-  if (__pyx_t_7) {
-
-    /* "pysam/csamfile.pyx":2854
- *             src = self._delegate
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None             # <<<<<<<<<<<<<<
- *             return bam_endpos(src)
- * 
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2735);
 
-  /* "pysam/csamfile.pyx":2855
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None
- *             return bam_endpos(src)             # <<<<<<<<<<<<<<
- * 
- *     property alen:
+  /* "pysam/calignmentfile.pyx":2736
+ *             return (self.flag & BAM_FREAD1) != 0
+ *         def __set__(self,val):
+ *             pysam_update_flag(self._delegate, val, BAM_FREAD1)             # <<<<<<<<<<<<<<
+ *     property is_read2:
+ *         """true if this is read2"""
  */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_4 = __Pyx_PyInt_From_int32_t(bam_endpos(__pyx_v_src)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
-  goto __pyx_L0;
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FREAD1);
 
-  /* "pysam/csamfile.pyx":2850
- *         aend points to one past the last aligned residue.
- *         Returns None if not available.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
+  /* "pysam/calignmentfile.pyx":2735
+ *         def __get__(self):
+ *             return (self.flag & BAM_FREAD1) != 0
+ *         def __set__(self,val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FREAD1)
+ *     property is_read2:
  */
 
   /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.aend.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_read1.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
   __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2861
- * 
- *         This is equal to `aend - pos`. Returns None if not available.'''
+/* "pysam/calignmentfile.pyx":2739
+ *     property is_read2:
+ *         """true if this is read2"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
+ *             return (self.flag & BAM_FREAD2) != 0
+ *         def __set__(self, val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4alen_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4alen_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read2_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read2_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4alen___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read2___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4alen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read2___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
+  PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
   PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_t_7;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2861);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2739);
 
-  /* "pysam/csamfile.pyx":2863
+  /* "pysam/calignmentfile.pyx":2740
+ *         """true if this is read2"""
  *         def __get__(self):
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2864
- *             cdef bam1_t * src
- *             src = self._delegate
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return None
- *             return bam_endpos(src) - \
+ *             return (self.flag & BAM_FREAD2) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FREAD2)
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FREAD2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyInt_From_int(BAM_FUNMAP); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyNumber_And(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (!__pyx_t_5) {
-    __pyx_t_6 = (pysam_get_n_cigar(__pyx_v_src) == 0);
-    __pyx_t_7 = __pyx_t_6;
-  } else {
-    __pyx_t_7 = __pyx_t_5;
-  }
-  if (__pyx_t_7) {
-
-    /* "pysam/csamfile.pyx":2865
- *             src = self._delegate
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None             # <<<<<<<<<<<<<<
- *             return bam_endpos(src) - \
- *                 self._delegate.core.pos
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2866
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None
- *             return bam_endpos(src) - \             # <<<<<<<<<<<<<<
- *                 self._delegate.core.pos
- * 
- */
-  __Pyx_XDECREF(__pyx_r);
-
-  /* "pysam/csamfile.pyx":2867
- *                 return None
- *             return bam_endpos(src) - \
- *                 self._delegate.core.pos             # <<<<<<<<<<<<<<
- * 
- *     property mapq:
- */
-  __pyx_t_4 = __Pyx_PyInt_From_int((bam_endpos(__pyx_v_src) - __pyx_v_self->_delegate->core.pos)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2861
- * 
- *         This is equal to `aend - pos`. Returns None if not available.'''
+  /* "pysam/calignmentfile.pyx":2739
+ *     property is_read2:
+ *         """true if this is read2"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
+ *             return (self.flag & BAM_FREAD2) != 0
+ *         def __set__(self, val):
  */
 
   /* function exit code */
   __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.alen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2871
- *     property mapq:
- *         """mapping quality"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2871);
-
-  /* "pysam/csamfile.pyx":2872
- *         """mapping quality"""
- *         def __get__(self):
- *             return pysam_get_qual(self._delegate)             # <<<<<<<<<<<<<<
- *         def __set__(self, qual):
- *             pysam_set_qual(self._delegate, qual)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint8_t(pysam_get_qual(__pyx_v_self->_delegate)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2871
- *     property mapq:
- *         """mapping quality"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mapq.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_read2.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -28806,61 +28497,61 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2873
+/* "pysam/calignmentfile.pyx":2741
  *         def __get__(self):
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):             # <<<<<<<<<<<<<<
- *             pysam_set_qual(self._delegate, qual)
- * 
+ *             return (self.flag & BAM_FREAD2) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FREAD2)
+ *     property is_secondary:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read2_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read2_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_qual));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read2_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qual) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_8is_read2_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  uint8_t __pyx_t_1;
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2873);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2741);
 
-  /* "pysam/csamfile.pyx":2874
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):
- *             pysam_set_qual(self._delegate, qual)             # <<<<<<<<<<<<<<
- * 
- *     property mrnm:
+  /* "pysam/calignmentfile.pyx":2742
+ *             return (self.flag & BAM_FREAD2) != 0
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FREAD2)             # <<<<<<<<<<<<<<
+ *     property is_secondary:
+ *         """true if not primary alignment"""
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint8_t(__pyx_v_qual); if (unlikely((__pyx_t_1 == (uint8_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_set_qual(__pyx_v_self->_delegate, __pyx_t_1);
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FREAD2);
 
-  /* "pysam/csamfile.pyx":2873
+  /* "pysam/calignmentfile.pyx":2741
  *         def __get__(self):
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):             # <<<<<<<<<<<<<<
- *             pysam_set_qual(self._delegate, qual)
- * 
+ *             return (self.flag & BAM_FREAD2) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FREAD2)
+ *     property is_secondary:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mapq.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_read2.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -28868,64 +28559,76 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq_2__set__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2880
- *         deprecated, use RNEXT instead.
- *         """
+/* "pysam/calignmentfile.pyx":2745
+ *     property is_secondary:
+ *         """true if not primary alignment"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):
+ *             return (self.flag & BAM_FSECONDARY) != 0
+ *         def __set__(self, val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_secondary_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_secondary_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_secondary___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_secondary___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2880);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2745);
 
-  /* "pysam/csamfile.pyx":2881
- *         """
+  /* "pysam/calignmentfile.pyx":2746
+ *         """true if not primary alignment"""
  *         def __get__(self):
- *             return self._delegate.core.mtid             # <<<<<<<<<<<<<<
- *         def __set__(self, mtid):
- *             self._delegate.core.mtid = mtid
+ *             return (self.flag & BAM_FSECONDARY) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mtid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2881; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2746; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FSECONDARY); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2746; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2746; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2746; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2880
- *         deprecated, use RNEXT instead.
- *         """
+  /* "pysam/calignmentfile.pyx":2745
+ *     property is_secondary:
+ *         """true if not primary alignment"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):
+ *             return (self.flag & BAM_FSECONDARY) != 0
+ *         def __set__(self, val):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mrnm.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_secondary.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -28934,61 +28637,61 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2882
+/* "pysam/calignmentfile.pyx":2747
  *         def __get__(self):
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mtid = mtid
- * 
+ *             return (self.flag & BAM_FSECONDARY) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
+ *     property is_qcfail:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_secondary_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_secondary_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_mtid));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_secondary_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mtid) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_secondary_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2882);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2747);
 
-  /* "pysam/csamfile.pyx":2883
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):
- *             self._delegate.core.mtid = mtid             # <<<<<<<<<<<<<<
- * 
- *     property rnext:
+  /* "pysam/calignmentfile.pyx":2748
+ *             return (self.flag & BAM_FSECONDARY) != 0
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)             # <<<<<<<<<<<<<<
+ *     property is_qcfail:
+ *         """true if QC failure"""
  */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mtid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2883; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.mtid = __pyx_t_1;
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FSECONDARY);
 
-  /* "pysam/csamfile.pyx":2882
+  /* "pysam/calignmentfile.pyx":2747
  *         def __get__(self):
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mtid = mtid
- * 
+ *             return (self.flag & BAM_FSECONDARY) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
+ *     property is_qcfail:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mrnm.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_secondary.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -28996,48 +28699,76 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm_2__set__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2887
- *     property rnext:
- *         """the :term:`reference` id of the mate """
- *         def __get__(self): return self._delegate.core.mtid             # <<<<<<<<<<<<<<
- *         def __set__(self, mtid): self._delegate.core.mtid = mtid
- * 
+/* "pysam/calignmentfile.pyx":2751
+ *     property is_qcfail:
+ *         """true if QC failure"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FQCFAIL) != 0
+ *         def __set__(self, val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2887);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2751);
+
+  /* "pysam/calignmentfile.pyx":2752
+ *         """true if QC failure"""
+ *         def __get__(self):
+ *             return (self.flag & BAM_FQCFAIL) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
+ */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mtid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2752; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FQCFAIL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2752; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2752; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2752; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
+  /* "pysam/calignmentfile.pyx":2751
+ *     property is_qcfail:
+ *         """true if QC failure"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return (self.flag & BAM_FQCFAIL) != 0
+ *         def __set__(self, val):
+ */
+
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rnext.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_qcfail.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -29046,45 +28777,61 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext___get__(struct _
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2888
- *         """the :term:`reference` id of the mate """
- *         def __get__(self): return self._delegate.core.mtid
- *         def __set__(self, mtid): self._delegate.core.mtid = mtid             # <<<<<<<<<<<<<<
- * 
- *     property mpos:
+/* "pysam/calignmentfile.pyx":2753
+ *         def __get__(self):
+ *             return (self.flag & BAM_FQCFAIL) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
+ *     property is_duplicate:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_mtid));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mtid) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2888);
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mtid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.mtid = __pyx_t_1;
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2753);
+
+  /* "pysam/calignmentfile.pyx":2754
+ *             return (self.flag & BAM_FQCFAIL) != 0
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)             # <<<<<<<<<<<<<<
+ *     property is_duplicate:
+ *         """true if optical or PCR duplicate"""
+ */
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2754; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FQCFAIL);
+
+  /* "pysam/calignmentfile.pyx":2753
+ *         def __get__(self):
+ *             return (self.flag & BAM_FQCFAIL) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
+ *     property is_duplicate:
+ */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rnext.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_qcfail.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -29092,64 +28839,76 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext_2__set__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2893
- *         """the position of the mate
- *         deprecated, use PNEXT instead."""
+/* "pysam/calignmentfile.pyx":2757
+ *     property is_duplicate:
+ *         """true if optical or PCR duplicate"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
+ *             return (self.flag & BAM_FDUP) != 0
+ *         def __set__(self, val):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2893);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2757);
 
-  /* "pysam/csamfile.pyx":2894
- *         deprecated, use PNEXT instead."""
+  /* "pysam/calignmentfile.pyx":2758
+ *         """true if optical or PCR duplicate"""
  *         def __get__(self):
- *             return self._delegate.core.mpos             # <<<<<<<<<<<<<<
- *         def __set__(self, mpos):
- *             self._delegate.core.mpos = mpos
+ *             return (self.flag & BAM_FDUP) != 0             # <<<<<<<<<<<<<<
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FDUP)
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2758; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FDUP); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2758; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2758; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2758; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2893
- *         """the position of the mate
- *         deprecated, use PNEXT instead."""
+  /* "pysam/calignmentfile.pyx":2757
+ *     property is_duplicate:
+ *         """true if optical or PCR duplicate"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
+ *             return (self.flag & BAM_FDUP) != 0
+ *         def __set__(self, val):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mpos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_duplicate.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -29158,53 +28917,53 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2895
+/* "pysam/calignmentfile.pyx":2759
  *         def __get__(self):
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mpos = mpos
+ *             return (self.flag & BAM_FDUP) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FDUP)
  * 
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_mpos));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_val));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mpos) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_val) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
+  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2895);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 2759);
 
-  /* "pysam/csamfile.pyx":2896
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
- *             self._delegate.core.mpos = mpos             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2760
+ *             return (self.flag & BAM_FDUP) != 0
+ *         def __set__(self, val):
+ *             pysam_update_flag(self._delegate, val, BAM_FDUP)             # <<<<<<<<<<<<<<
  * 
- *     property pnext:
+ *     # 2. Coordinates and lengths
  */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mpos); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2896; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.mpos = __pyx_t_1;
+  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FDUP);
 
-  /* "pysam/csamfile.pyx":2895
+  /* "pysam/calignmentfile.pyx":2759
  *         def __get__(self):
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mpos = mpos
+ *             return (self.flag & BAM_FDUP) != 0
+ *         def __set__(self, val):             # <<<<<<<<<<<<<<
+ *             pysam_update_flag(self._delegate, val, BAM_FDUP)
  * 
  */
 
@@ -29212,7 +28971,7 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos_2__set__(struct __pyx_o
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mpos.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.is_duplicate.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -29220,192 +28979,252 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos_2__set__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2900
- *     property pnext:
- *         """the position of the mate"""
+/* "pysam/calignmentfile.pyx":2768
+ *         aend points to one past the last aligned residue.
+ *         Returns None if not available.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
+ *             cdef bam1_t * src
+ *             src = self._delegate
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_13reference_end_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_13reference_end_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_13reference_end___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_13reference_end___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  bam1_t *__pyx_v_src;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
+  bam1_t *__pyx_t_1;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
+  int __pyx_t_5;
+  int __pyx_t_6;
+  int __pyx_t_7;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2900);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2768);
 
-  /* "pysam/csamfile.pyx":2901
- *         """the position of the mate"""
+  /* "pysam/calignmentfile.pyx":2770
  *         def __get__(self):
- *             return self._delegate.core.mpos             # <<<<<<<<<<<<<<
- *         def __set__(self, mpos):
- *             self._delegate.core.mpos = mpos
+ *             cdef bam1_t * src
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
+ *                 return None
  */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2901; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2900
- *     property pnext:
- *         """the position of the mate"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
+  /* "pysam/calignmentfile.pyx":2771
+ *             cdef bam1_t * src
+ *             src = self._delegate
+ *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
+ *                 return None
+ *             return bam_endpos(src)
  */
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = __Pyx_PyInt_From_int(BAM_FUNMAP); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __pyx_t_4 = PyNumber_And(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  if (!__pyx_t_5) {
+    __pyx_t_6 = (pysam_get_n_cigar(__pyx_v_src) == 0);
+    __pyx_t_7 = __pyx_t_6;
+  } else {
+    __pyx_t_7 = __pyx_t_5;
+  }
+  if (__pyx_t_7) {
 
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.pnext.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2902
- *         def __get__(self):
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mpos = mpos
+    /* "pysam/calignmentfile.pyx":2772
+ *             src = self._delegate
+ *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
+ *                 return None             # <<<<<<<<<<<<<<
+ *             return bam_endpos(src)
  * 
  */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
+  }
 
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_mpos));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mpos) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2902);
-
-  /* "pysam/csamfile.pyx":2903
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
- *             self._delegate.core.mpos = mpos             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":2773
+ *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
+ *                 return None
+ *             return bam_endpos(src)             # <<<<<<<<<<<<<<
  * 
- *     #######################################################################
+ *     property reference_length:
  */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mpos); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.mpos = __pyx_t_1;
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_4 = __Pyx_PyInt_From_int32_t(bam_endpos(__pyx_v_src)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __pyx_r = __pyx_t_4;
+  __pyx_t_4 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2902
- *         def __get__(self):
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mpos = mpos
- * 
+  /* "pysam/calignmentfile.pyx":2768
+ *         aend points to one past the last aligned residue.
+ *         Returns None if not available.'''
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             cdef bam1_t * src
+ *             src = self._delegate
  */
 
   /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.pnext.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.reference_end.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2912
- *         """the insert size
- *         deprecated: use tlen instead"""
+/* "pysam/calignmentfile.pyx":2779
+ * 
+ *         This is equal to `aend - pos`. Returns None if not available.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
+ *             cdef bam1_t * src
+ *             src = self._delegate
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_16reference_length_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_16reference_length_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5isize___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16reference_length___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5isize___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16reference_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  bam1_t *__pyx_v_src;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
+  bam1_t *__pyx_t_1;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
+  int __pyx_t_5;
+  int __pyx_t_6;
+  int __pyx_t_7;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2912);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2779);
 
-  /* "pysam/csamfile.pyx":2913
- *         deprecated: use tlen instead"""
+  /* "pysam/calignmentfile.pyx":2781
  *         def __get__(self):
- *             return self._delegate.core.isize             # <<<<<<<<<<<<<<
- *         def __set__(self, isize):
- *             self._delegate.core.isize = isize
+ *             cdef bam1_t * src
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
+ *                 return None
+ */
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":2782
+ *             cdef bam1_t * src
+ *             src = self._delegate
+ *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
+ *                 return None
+ *             return bam_endpos(src) - \
+ */
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_t_3 = __Pyx_PyInt_From_int(BAM_FUNMAP); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __pyx_t_4 = PyNumber_And(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  if (!__pyx_t_5) {
+    __pyx_t_6 = (pysam_get_n_cigar(__pyx_v_src) == 0);
+    __pyx_t_7 = __pyx_t_6;
+  } else {
+    __pyx_t_7 = __pyx_t_5;
+  }
+  if (__pyx_t_7) {
+
+    /* "pysam/calignmentfile.pyx":2783
+ *             src = self._delegate
+ *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
+ *                 return None             # <<<<<<<<<<<<<<
+ *             return bam_endpos(src) - \
+ *                 self._delegate.core.pos
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":2784
+ *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
+ *                 return None
+ *             return bam_endpos(src) - \             # <<<<<<<<<<<<<<
+ *                 self._delegate.core.pos
+ * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.isize); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2913; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":2785
+ *                 return None
+ *             return bam_endpos(src) - \
+ *                 self._delegate.core.pos             # <<<<<<<<<<<<<<
+ * 
+ *     property query_alignment_length:
+ */
+  __pyx_t_4 = __Pyx_PyInt_From_int((bam_endpos(__pyx_v_src) - __pyx_v_self->_delegate->core.pos)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2784; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __pyx_r = __pyx_t_4;
+  __pyx_t_4 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2912
- *         """the insert size
- *         deprecated: use tlen instead"""
+  /* "pysam/calignmentfile.pyx":2779
+ * 
+ *         This is equal to `aend - pos`. Returns None if not available.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
+ *             cdef bam1_t * src
+ *             src = self._delegate
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.isize.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.reference_length.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -29414,126 +29233,189 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5isize___get__(struct _
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2914
- *         def __get__(self):
- *             return self._delegate.core.isize
- *         def __set__(self, isize):             # <<<<<<<<<<<<<<
- *             self._delegate.core.isize = isize
- *     property tlen:
+/* "pysam/calignmentfile.pyx":2794
+ * 
+ *         Returns 0 if not available."""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._delegate.core.l_qseq
+ * 
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize) {
-  int __pyx_r;
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_22query_alignment_length_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_22query_alignment_length_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5isize_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_isize));
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_22query_alignment_length___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5isize_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_isize) {
-  int __pyx_r;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_22query_alignment_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
+  PyObject *__pyx_t_1 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2914);
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2794);
 
-  /* "pysam/csamfile.pyx":2915
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
- *             self._delegate.core.isize = isize             # <<<<<<<<<<<<<<
- *     property tlen:
- *         """the template length"""
+  /* "pysam/calignmentfile.pyx":2795
+ *         Returns 0 if not available."""
+ *         def __get__(self):
+ *             return self._delegate.core.l_qseq             # <<<<<<<<<<<<<<
+ * 
+ *     property query_alignment_sequence:
  */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_isize); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.isize = __pyx_t_1;
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.l_qseq); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2914
- *         def __get__(self):
- *             return self._delegate.core.isize
- *         def __set__(self, isize):             # <<<<<<<<<<<<<<
- *             self._delegate.core.isize = isize
- *     property tlen:
+  /* "pysam/calignmentfile.pyx":2794
+ * 
+ *         Returns 0 if not available."""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._delegate.core.l_qseq
+ * 
  */
 
   /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.isize.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_alignment_length.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2918
- *     property tlen:
- *         """the template length"""
+/* "pysam/calignmentfile.pyx":2814
+ *         """
+ * 
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
+ *             cdef bam1_t * src
+ *             cdef uint32_t start, end
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_24query_alignment_sequence_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_24query_alignment_sequence_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_24query_alignment_sequence___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_24query_alignment_sequence___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  bam1_t *__pyx_v_src;
+  uint32_t __pyx_v_start;
+  uint32_t __pyx_v_end;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
+  bam1_t *__pyx_t_1;
+  int __pyx_t_2;
+  int32_t __pyx_t_3;
+  PyObject *__pyx_t_4 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2918);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2814);
 
-  /* "pysam/csamfile.pyx":2919
- *         """the template length"""
- *         def __get__(self):
- *             return self._delegate.core.isize             # <<<<<<<<<<<<<<
- *         def __set__(self, isize):
- *             self._delegate.core.isize = isize
+  /* "pysam/calignmentfile.pyx":2818
+ *             cdef uint32_t start, end
+ * 
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ * 
+ *             if src.core.l_qseq == 0:
+ */
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":2820
+ *             src = self._delegate
+ * 
+ *             if src.core.l_qseq == 0:             # <<<<<<<<<<<<<<
+ *                 return None
+ * 
+ */
+  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
+  if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":2821
+ * 
+ *             if src.core.l_qseq == 0:
+ *                 return None             # <<<<<<<<<<<<<<
+ * 
+ *             start = _getQueryStart(src)
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":2823
+ *                 return None
+ * 
+ *             start = _getQueryStart(src)             # <<<<<<<<<<<<<<
+ *             end   = _getQueryEnd(src)
+ * 
+ */
+  __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__getQueryStart(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_start = __pyx_t_3;
+
+  /* "pysam/calignmentfile.pyx":2824
+ * 
+ *             start = _getQueryStart(src)
+ *             end   = _getQueryEnd(src)             # <<<<<<<<<<<<<<
+ * 
+ *             return _getSequenceRange(src, start, end)
+ */
+  __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__getQueryEnd(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_end = __pyx_t_3;
+
+  /* "pysam/calignmentfile.pyx":2826
+ *             end   = _getQueryEnd(src)
+ * 
+ *             return _getSequenceRange(src, start, end)             # <<<<<<<<<<<<<<
+ * 
+ *     property query_alignment_qualities:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.isize); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2919; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
+  __pyx_t_4 = __pyx_f_5pysam_14calignmentfile__getSequenceRange(__pyx_v_src, __pyx_v_start, __pyx_v_end); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __pyx_r = __pyx_t_4;
+  __pyx_t_4 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2918
- *     property tlen:
- *         """the template length"""
+  /* "pysam/calignmentfile.pyx":2814
+ *         """
+ * 
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
+ *             cdef bam1_t * src
+ *             cdef uint32_t start, end
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tlen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_alignment_sequence.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -29542,138 +29424,191 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2920
- *         def __get__(self):
- *             return self._delegate.core.isize
- *         def __set__(self, isize):             # <<<<<<<<<<<<<<
- *             self._delegate.core.isize = isize
- *     property is_paired:
+/* "pysam/calignmentfile.pyx":2842
+ * 
+ *         """
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             cdef bam1_t * src
+ *             cdef uint32_t start, end
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize) {
-  int __pyx_r;
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_25query_alignment_qualities_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_25query_alignment_qualities_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_isize));
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_25query_alignment_qualities___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_isize) {
-  int __pyx_r;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_25query_alignment_qualities___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  bam1_t *__pyx_v_src;
+  uint32_t __pyx_v_start;
+  uint32_t __pyx_v_end;
+  PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
+  bam1_t *__pyx_t_1;
+  int __pyx_t_2;
+  int32_t __pyx_t_3;
+  PyObject *__pyx_t_4 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2920);
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2842);
 
-  /* "pysam/csamfile.pyx":2921
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
- *             self._delegate.core.isize = isize             # <<<<<<<<<<<<<<
- *     property is_paired:
- *         """true if read is paired in sequencing"""
+  /* "pysam/calignmentfile.pyx":2846
+ *             cdef uint32_t start, end
+ * 
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ * 
+ *             if src.core.l_qseq == 0:
  */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_isize); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.isize = __pyx_t_1;
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2920
- *         def __get__(self):
- *             return self._delegate.core.isize
- *         def __set__(self, isize):             # <<<<<<<<<<<<<<
- *             self._delegate.core.isize = isize
- *     property is_paired:
+  /* "pysam/calignmentfile.pyx":2848
+ *             src = self._delegate
+ * 
+ *             if src.core.l_qseq == 0:             # <<<<<<<<<<<<<<
+ *                 return None
+ * 
  */
+  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
+  if (__pyx_t_2) {
 
-  /* function exit code */
-  __pyx_r = 0;
+    /* "pysam/calignmentfile.pyx":2849
+ * 
+ *             if src.core.l_qseq == 0:
+ *                 return None             # <<<<<<<<<<<<<<
+ * 
+ *             start = _getQueryStart(src)
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":2851
+ *                 return None
+ * 
+ *             start = _getQueryStart(src)             # <<<<<<<<<<<<<<
+ *             end   = _getQueryEnd(src)
+ * 
+ */
+  __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__getQueryStart(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_start = __pyx_t_3;
+
+  /* "pysam/calignmentfile.pyx":2852
+ * 
+ *             start = _getQueryStart(src)
+ *             end   = _getQueryEnd(src)             # <<<<<<<<<<<<<<
+ * 
+ *             return _getQualitiesRange(src, start, end)
+ */
+  __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__getQueryEnd(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2852; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_end = __pyx_t_3;
+
+  /* "pysam/calignmentfile.pyx":2854
+ *             end   = _getQueryEnd(src)
+ * 
+ *             return _getQualitiesRange(src, start, end)             # <<<<<<<<<<<<<<
+ * 
+ *     property query_alignment_start:
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_4 = __pyx_f_5pysam_14calignmentfile__getQualitiesRange(__pyx_v_src, __pyx_v_start, __pyx_v_end); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __pyx_r = __pyx_t_4;
+  __pyx_t_4 = 0;
   goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":2842
+ * 
+ *         """
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             cdef bam1_t * src
+ *             cdef uint32_t start, end
+ */
+
+  /* function exit code */
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tlen.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_alignment_qualities.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2924
- *     property is_paired:
- *         """true if read is paired in sequencing"""
+/* "pysam/calignmentfile.pyx":2864
+ * 
+ *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):
+ *             return _getQueryStart(self._delegate)
+ * 
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21query_alignment_start_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21query_alignment_start_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_21query_alignment_start___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_21query_alignment_start___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
+  int32_t __pyx_t_1;
   PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2924);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2864);
 
-  /* "pysam/csamfile.pyx":2925
- *         """true if read is paired in sequencing"""
+  /* "pysam/calignmentfile.pyx":2865
+ *         """
  *         def __get__(self):
- *             return (self.flag & BAM_FPAIRED) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
+ *             return _getQueryStart(self._delegate)             # <<<<<<<<<<<<<<
+ * 
+ *     property query_alignment_end:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FPAIRED); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_f_5pysam_14calignmentfile__getQueryStart(__pyx_v_self->_delegate); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2865; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2865; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2924
- *     property is_paired:
- *         """true if read is paired in sequencing"""
+  /* "pysam/calignmentfile.pyx":2864
+ * 
+ *         """
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):
+ *             return _getQueryStart(self._delegate)
+ * 
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_paired.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_alignment_start.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -29682,138 +29617,148 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired___get__(stru
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2926
- *         def __get__(self):
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
- *     property is_proper_pair:
+/* "pysam/calignmentfile.pyx":2870
+ *         """end index of the aligned query portion of the sequence (0-based,
+ *         exclusive)"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return _getQueryEnd(self._delegate)
+ * 
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_19query_alignment_end_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_19query_alignment_end_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_19query_alignment_end___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_19query_alignment_end___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
+  int32_t __pyx_t_1;
+  PyObject *__pyx_t_2 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2926);
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2870);
 
-  /* "pysam/csamfile.pyx":2927
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)             # <<<<<<<<<<<<<<
- *     property is_proper_pair:
- *         """true if read is mapped in a proper pair"""
+  /* "pysam/calignmentfile.pyx":2871
+ *         exclusive)"""
+ *         def __get__(self):
+ *             return _getQueryEnd(self._delegate)             # <<<<<<<<<<<<<<
+ * 
+ *     property query_aligment_length:
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FPAIRED);
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __pyx_f_5pysam_14calignmentfile__getQueryEnd(__pyx_v_self->_delegate); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2926
- *         def __get__(self):
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
- *     property is_proper_pair:
+  /* "pysam/calignmentfile.pyx":2870
+ *         """end index of the aligned query portion of the sequence (0-based,
+ *         exclusive)"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return _getQueryEnd(self._delegate)
+ * 
  */
 
   /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_paired.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_alignment_end.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2930
- *     property is_proper_pair:
- *         """true if read is mapped in a proper pair"""
+/* "pysam/calignmentfile.pyx":2877
+ * 
+ *         This is equal to :attr:`qend` - :attr:`qstart`"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):
+ *             cdef bam1_t * src
+ *             src = self._delegate
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21query_aligment_length_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21query_aligment_length_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_21query_aligment_length___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_21query_aligment_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  bam1_t *__pyx_v_src;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
+  bam1_t *__pyx_t_1;
+  int32_t __pyx_t_2;
+  int32_t __pyx_t_3;
+  PyObject *__pyx_t_4 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2930);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 2877);
 
-  /* "pysam/csamfile.pyx":2931
- *         """true if read is mapped in a proper pair"""
+  /* "pysam/calignmentfile.pyx":2879
  *         def __get__(self):
- *             return (self.flag & BAM_FPROPER_PAIR) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
+ *             cdef bam1_t * src
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ *             return _getQueryEnd(src) - _getQueryStart(src)
+ * 
+ */
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":2880
+ *             cdef bam1_t * src
+ *             src = self._delegate
+ *             return _getQueryEnd(src) - _getQueryStart(src)             # <<<<<<<<<<<<<<
+ * 
+ *     #####################################################
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2931; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FPROPER_PAIR); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2931; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2931; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2931; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_t_2 = __pyx_f_5pysam_14calignmentfile__getQueryEnd(__pyx_v_src); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2880; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__getQueryStart(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2880; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyInt_From_int((__pyx_t_2 - __pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2880; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __pyx_r = __pyx_t_4;
+  __pyx_t_4 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2930
- *     property is_proper_pair:
- *         """true if read is mapped in a proper pair"""
+  /* "pysam/calignmentfile.pyx":2877
+ * 
+ *         This is equal to :attr:`qend` - :attr:`qstart`"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):
+ *             cdef bam1_t * src
+ *             src = self._delegate
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_proper_pair.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query_aligment_length.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -29822,278 +29767,667 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair___get_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2932
- *         def __get__(self):
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
- *     property is_unmapped:
+/* "pysam/calignmentfile.pyx":2885
+ *     # Computed properties
+ * 
+ *     def get_reference_positions(self, full_length=False):             # <<<<<<<<<<<<<<
+ *         """a list of reference positions that this read aligns to.
+ * 
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11get_reference_positions(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_10get_reference_positions[] = "AlignedSegment.get_reference_positions(self, full_length=False)\na list of reference positions that this read aligns to.\n\n        By default, this method only returns positions in the\n        reference that are within the alignment. If *full_length* is\n        set, None values will be included for any soft-clipped or\n        unaligned positions within the read. The returned list will\n      [...]
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11get_reference_positions(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  PyObject *__pyx_v_full_length = 0;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("get_reference_positions (wrapper)", 0);
+  {
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_full_length,0};
+    PyObject* values[1] = {0};
+    values[0] = ((PyObject *)Py_False);
+    if (unlikely(__pyx_kwds)) {
+      Py_ssize_t kw_args;
+      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
+      switch (pos_args) {
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+      kw_args = PyDict_Size(__pyx_kwds);
+      switch (pos_args) {
+        case  0:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_full_length);
+          if (value) { values[0] = value; kw_args--; }
+        }
+      }
+      if (unlikely(kw_args > 0)) {
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "get_reference_positions") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2885; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+    } else {
+      switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+    }
+    __pyx_v_full_length = values[0];
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("get_reference_positions", 0, 0, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2885; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.get_reference_positions", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_RefNannyFinishContext();
+  return NULL;
+  __pyx_L4_argument_unpacking_done:;
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10get_reference_positions(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), __pyx_v_full_length);
+
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_10get_reference_positions(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_full_length) {
+  uint32_t __pyx_v_k;
+  uint32_t __pyx_v_i;
+  uint32_t __pyx_v_pos;
+  int __pyx_v_op;
+  uint32_t *__pyx_v_cigar_p;
+  bam1_t *__pyx_v_src;
+  int __pyx_v__full;
+  PyObject *__pyx_v_result = NULL;
+  PyObject *__pyx_v_l = NULL;
+  PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
+  int __pyx_t_1;
+  bam1_t *__pyx_t_2;
+  PyObject *__pyx_t_3 = NULL;
+  int32_t __pyx_t_4;
+  uint16_t __pyx_t_5;
+  int __pyx_t_6;
+  int __pyx_t_7;
+  uint32_t __pyx_t_8;
+  int __pyx_t_9;
+  PyObject *__pyx_t_10 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2932);
+  __Pyx_RefNannySetupContext("get_reference_positions", 0);
+  __Pyx_TraceCall("get_reference_positions", __pyx_f[0], 2885);
 
-  /* "pysam/csamfile.pyx":2933
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)             # <<<<<<<<<<<<<<
- *     property is_unmapped:
- *         """true if read itself is unmapped"""
+  /* "pysam/calignmentfile.pyx":2899
+ *         cdef uint32_t * cigar_p
+ *         cdef bam1_t * src
+ *         cdef bint _full = full_length             # <<<<<<<<<<<<<<
+ * 
+ *         src = self._delegate
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2933; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FPROPER_PAIR);
+  __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_full_length); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2899; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v__full = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2932
- *         def __get__(self):
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
- *     property is_unmapped:
+  /* "pysam/calignmentfile.pyx":2901
+ *         cdef bint _full = full_length
+ * 
+ *         src = self._delegate             # <<<<<<<<<<<<<<
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return []
  */
+  __pyx_t_2 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_2;
 
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_proper_pair.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
+  /* "pysam/calignmentfile.pyx":2902
+ * 
+ *         src = self._delegate
+ *         if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
+ *             return []
+ * 
+ */
+  __pyx_t_1 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
+  if (__pyx_t_1) {
 
-/* "pysam/csamfile.pyx":2936
- *     property is_unmapped:
- *         """true if read itself is unmapped"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):
+    /* "pysam/calignmentfile.pyx":2903
+ *         src = self._delegate
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return []             # <<<<<<<<<<<<<<
+ * 
+ *         result = []
  */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __pyx_r = __pyx_t_3;
+    __pyx_t_3 = 0;
+    goto __pyx_L0;
+  }
 
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  /* "pysam/calignmentfile.pyx":2905
+ *             return []
+ * 
+ *         result = []             # <<<<<<<<<<<<<<
+ *         pos = src.core.pos
+ *         cigar_p = pysam_bam_get_cigar(src)
+ */
+  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2905; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __pyx_v_result = ((PyObject*)__pyx_t_3);
+  __pyx_t_3 = 0;
 
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
+  /* "pysam/calignmentfile.pyx":2906
+ * 
+ *         result = []
+ *         pos = src.core.pos             # <<<<<<<<<<<<<<
+ *         cigar_p = pysam_bam_get_cigar(src)
+ * 
+ */
+  __pyx_t_4 = __pyx_v_src->core.pos;
+  __pyx_v_pos = __pyx_t_4;
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2936);
+  /* "pysam/calignmentfile.pyx":2907
+ *         result = []
+ *         pos = src.core.pos
+ *         cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ */
+  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":2937
- *         """true if read itself is unmapped"""
- *         def __get__(self):
- *             return (self.flag & BAM_FUNMAP) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)
+  /* "pysam/calignmentfile.pyx":2909
+ *         cigar_p = pysam_bam_get_cigar(src)
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ */
+  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
+  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
+
+    /* "pysam/calignmentfile.pyx":2910
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ * 
+ */
+    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+
+    /* "pysam/calignmentfile.pyx":2911
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
+ * 
+ *             if op == BAM_CSOFT_CLIP or op == BAM_CINS:
+ */
+    __pyx_t_3 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_3);
+    __pyx_t_3 = 0;
+
+    /* "pysam/calignmentfile.pyx":2913
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ * 
+ *             if op == BAM_CSOFT_CLIP or op == BAM_CINS:             # <<<<<<<<<<<<<<
+ *                 if _full:
+ *                     for i from 0 <= i < l:
+ */
+    __pyx_t_1 = ((__pyx_v_op == BAM_CSOFT_CLIP) != 0);
+    if (!__pyx_t_1) {
+      __pyx_t_6 = ((__pyx_v_op == BAM_CINS) != 0);
+      __pyx_t_7 = __pyx_t_6;
+    } else {
+      __pyx_t_7 = __pyx_t_1;
+    }
+    if (__pyx_t_7) {
+
+      /* "pysam/calignmentfile.pyx":2914
+ * 
+ *             if op == BAM_CSOFT_CLIP or op == BAM_CINS:
+ *                 if _full:             # <<<<<<<<<<<<<<
+ *                     for i from 0 <= i < l:
+ *                         result.append(None)
+ */
+      __pyx_t_7 = (__pyx_v__full != 0);
+      if (__pyx_t_7) {
+
+        /* "pysam/calignmentfile.pyx":2915
+ *             if op == BAM_CSOFT_CLIP or op == BAM_CINS:
+ *                 if _full:
+ *                     for i from 0 <= i < l:             # <<<<<<<<<<<<<<
+ *                         result.append(None)
+ *             elif op == BAM_CMATCH:
+ */
+        __pyx_t_8 = __Pyx_PyInt_As_uint32_t(__pyx_v_l); if (unlikely((__pyx_t_8 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_8; __pyx_v_i++) {
+
+          /* "pysam/calignmentfile.pyx":2916
+ *                 if _full:
+ *                     for i from 0 <= i < l:
+ *                         result.append(None)             # <<<<<<<<<<<<<<
+ *             elif op == BAM_CMATCH:
+ *                 for i from pos <= i < pos + l:
+ */
+          __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, Py_None); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2916; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        }
+        goto __pyx_L7;
+      }
+      __pyx_L7:;
+      goto __pyx_L6;
+    }
+
+    /* "pysam/calignmentfile.pyx":2917
+ *                     for i from 0 <= i < l:
+ *                         result.append(None)
+ *             elif op == BAM_CMATCH:             # <<<<<<<<<<<<<<
+ *                 for i from pos <= i < pos + l:
+ *                     result.append(i)
+ */
+    __pyx_t_7 = ((__pyx_v_op == BAM_CMATCH) != 0);
+    if (__pyx_t_7) {
+
+      /* "pysam/calignmentfile.pyx":2918
+ *                         result.append(None)
+ *             elif op == BAM_CMATCH:
+ *                 for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
+ *                     result.append(i)
+ *                 pos += l
+ */
+      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __pyx_t_10 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_10);
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __pyx_t_8 = __Pyx_PyInt_As_uint32_t(__pyx_t_10); if (unlikely((__pyx_t_8 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
+      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_8; __pyx_v_i++) {
+
+        /* "pysam/calignmentfile.pyx":2919
+ *             elif op == BAM_CMATCH:
+ *                 for i from pos <= i < pos + l:
+ *                     result.append(i)             # <<<<<<<<<<<<<<
+ *                 pos += l
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+ */
+        __pyx_t_10 = __Pyx_PyInt_From_uint32_t(__pyx_v_i); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2919; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_10);
+        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_10); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2919; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
+      }
+
+      /* "pysam/calignmentfile.pyx":2920
+ *                 for i from pos <= i < pos + l:
+ *                     result.append(i)
+ *                 pos += l             # <<<<<<<<<<<<<<
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+ *                 pos += l
+ */
+      __pyx_t_10 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_10);
+      __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_t_10, __pyx_v_l); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
+      __pyx_t_8 = __Pyx_PyInt_As_uint32_t(__pyx_t_3); if (unlikely((__pyx_t_8 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __pyx_v_pos = __pyx_t_8;
+      goto __pyx_L6;
+    }
+
+    /* "pysam/calignmentfile.pyx":2921
+ *                     result.append(i)
+ *                 pos += l
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
+ *                 pos += l
+ * 
+ */
+    __pyx_t_7 = ((__pyx_v_op == BAM_CDEL) != 0);
+    if (!__pyx_t_7) {
+      __pyx_t_1 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
+      __pyx_t_6 = __pyx_t_1;
+    } else {
+      __pyx_t_6 = __pyx_t_7;
+    }
+    if (__pyx_t_6) {
+
+      /* "pysam/calignmentfile.pyx":2922
+ *                 pos += l
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+ *                 pos += l             # <<<<<<<<<<<<<<
+ * 
+ *         return result
+ */
+      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __pyx_t_10 = PyNumber_InPlaceAdd(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_10);
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __pyx_t_8 = __Pyx_PyInt_As_uint32_t(__pyx_t_10); if (unlikely((__pyx_t_8 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
+      __pyx_v_pos = __pyx_t_8;
+      goto __pyx_L6;
+    }
+    __pyx_L6:;
+  }
+
+  /* "pysam/calignmentfile.pyx":2924
+ *                 pos += l
+ * 
+ *         return result             # <<<<<<<<<<<<<<
+ * 
+ *     def infer_query_length(self, always=True):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FUNMAP); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __Pyx_INCREF(__pyx_v_result);
+  __pyx_r = __pyx_v_result;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2936
- *     property is_unmapped:
- *         """true if read itself is unmapped"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):
+  /* "pysam/calignmentfile.pyx":2885
+ *     # Computed properties
+ * 
+ *     def get_reference_positions(self, full_length=False):             # <<<<<<<<<<<<<<
+ *         """a list of reference positions that this read aligns to.
+ * 
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_unmapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_10);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.get_reference_positions", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_result);
+  __Pyx_XDECREF(__pyx_v_l);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2938
- *         def __get__(self):
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)
- *     property mate_is_unmapped:
+/* "pysam/calignmentfile.pyx":2926
+ *         return result
+ * 
+ *     def infer_query_length(self, always=True):             # <<<<<<<<<<<<<<
+ *         """inferred read length from CIGAR string.
+ * 
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_13infer_query_length(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_12infer_query_length[] = "AlignedSegment.infer_query_length(self, always=True)\ninferred read length from CIGAR string.\n\n        If *always* is set to True, the read length\n        will be always inferred. If set to False, the length\n        of the read sequence will be returned if it is\n        available.\n\n        Returns None if CIGAR string is not present.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_13infer_query_length(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  PyObject *__pyx_v_always = 0;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __Pyx_RefNannySetupContext("infer_query_length (wrapper)", 0);
+  {
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_always,0};
+    PyObject* values[1] = {0};
+    values[0] = ((PyObject *)Py_True);
+    if (unlikely(__pyx_kwds)) {
+      Py_ssize_t kw_args;
+      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
+      switch (pos_args) {
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+      kw_args = PyDict_Size(__pyx_kwds);
+      switch (pos_args) {
+        case  0:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_always);
+          if (value) { values[0] = value; kw_args--; }
+        }
+      }
+      if (unlikely(kw_args > 0)) {
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "infer_query_length") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2926; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+    } else {
+      switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+    }
+    __pyx_v_always = values[0];
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("infer_query_length", 0, 0, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2926; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.infer_query_length", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_RefNannyFinishContext();
+  return NULL;
+  __pyx_L4_argument_unpacking_done:;
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12infer_query_length(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), __pyx_v_always);
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_12infer_query_length(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_always) {
+  uint32_t __pyx_v_k;
+  uint32_t __pyx_v_qpos;
+  int __pyx_v_op;
+  uint32_t *__pyx_v_cigar_p;
+  bam1_t *__pyx_v_src;
+  PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
+  bam1_t *__pyx_t_1;
+  int __pyx_t_2;
+  int __pyx_t_3;
+  PyObject *__pyx_t_4 = NULL;
+  uint16_t __pyx_t_5;
+  int __pyx_t_6;
+  int __pyx_t_7;
+  int __pyx_t_8;
+  int __pyx_t_9;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2938);
+  __Pyx_RefNannySetupContext("infer_query_length", 0);
+  __Pyx_TraceCall("infer_query_length", __pyx_f[0], 2926);
 
-  /* "pysam/csamfile.pyx":2939
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)             # <<<<<<<<<<<<<<
- *     property mate_is_unmapped:
- *         """true if the mate is unmapped"""
+  /* "pysam/calignmentfile.pyx":2941
+ *         cdef bam1_t * src
+ * 
+ *         src = self._delegate             # <<<<<<<<<<<<<<
+ * 
+ *         if not always and src.core.l_qseq:
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FUNMAP);
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
 
-  /* "pysam/csamfile.pyx":2938
- *         def __get__(self):
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)
- *     property mate_is_unmapped:
+  /* "pysam/calignmentfile.pyx":2943
+ *         src = self._delegate
+ * 
+ *         if not always and src.core.l_qseq:             # <<<<<<<<<<<<<<
+ *             return src.core.l_qseq
+ * 
  */
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_always); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2943; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (!__pyx_t_2);
+  if (__pyx_t_3) {
+    __pyx_t_2 = (__pyx_v_src->core.l_qseq != 0);
+  } else {
+    __pyx_t_2 = __pyx_t_3;
+  }
+  if (__pyx_t_2) {
 
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_unmapped.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
+    /* "pysam/calignmentfile.pyx":2944
+ * 
+ *         if not always and src.core.l_qseq:
+ *             return src.core.l_qseq             # <<<<<<<<<<<<<<
+ * 
+ *         if pysam_get_n_cigar(src) == 0:
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __pyx_r = __pyx_t_4;
+    __pyx_t_4 = 0;
+    goto __pyx_L0;
+  }
 
-/* "pysam/csamfile.pyx":2942
- *     property mate_is_unmapped:
- *         """true if the mate is unmapped"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):
+  /* "pysam/calignmentfile.pyx":2946
+ *             return src.core.l_qseq
+ * 
+ *         if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
+ *             return None
+ * 
  */
+  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
+  if (__pyx_t_2) {
 
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+    /* "pysam/calignmentfile.pyx":2947
+ * 
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return None             # <<<<<<<<<<<<<<
+ * 
+ *         qpos = 0
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
+  }
 
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
+  /* "pysam/calignmentfile.pyx":2949
+ *             return None
+ * 
+ *         qpos = 0             # <<<<<<<<<<<<<<
+ *         cigar_p = pysam_bam_get_cigar(src)
+ * 
+ */
+  __pyx_v_qpos = 0;
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2942);
+  /* "pysam/calignmentfile.pyx":2950
+ * 
+ *         qpos = 0
+ *         cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ */
+  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":2943
- *         """true if the mate is unmapped"""
- *         def __get__(self):
- *             return (self.flag & BAM_FMUNMAP) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
+  /* "pysam/calignmentfile.pyx":2952
+ *         cigar_p = pysam_bam_get_cigar(src)
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ * 
+ */
+  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
+  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
+
+    /* "pysam/calignmentfile.pyx":2953
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
+ * 
+ *             if op == BAM_CMATCH or op == BAM_CINS or \
+ */
+    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+
+    /* "pysam/calignmentfile.pyx":2955
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ * 
+ *             if op == BAM_CMATCH or op == BAM_CINS or \             # <<<<<<<<<<<<<<
+ *                op == BAM_CSOFT_CLIP or \
+ *                op == BAM_CEQUAL or op == BAM_CDIFF:
+ */
+    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
+    if (!__pyx_t_2) {
+      __pyx_t_3 = ((__pyx_v_op == BAM_CINS) != 0);
+      if (!__pyx_t_3) {
+
+        /* "pysam/calignmentfile.pyx":2956
+ * 
+ *             if op == BAM_CMATCH or op == BAM_CINS or \
+ *                op == BAM_CSOFT_CLIP or \             # <<<<<<<<<<<<<<
+ *                op == BAM_CEQUAL or op == BAM_CDIFF:
+ *                 qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
+ */
+        __pyx_t_6 = ((__pyx_v_op == BAM_CSOFT_CLIP) != 0);
+        if (!__pyx_t_6) {
+
+          /* "pysam/calignmentfile.pyx":2957
+ *             if op == BAM_CMATCH or op == BAM_CINS or \
+ *                op == BAM_CSOFT_CLIP or \
+ *                op == BAM_CEQUAL or op == BAM_CDIFF:             # <<<<<<<<<<<<<<
+ *                 qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
+ * 
+ */
+          __pyx_t_7 = ((__pyx_v_op == BAM_CEQUAL) != 0);
+          if (!__pyx_t_7) {
+            __pyx_t_8 = ((__pyx_v_op == BAM_CDIFF) != 0);
+            __pyx_t_9 = __pyx_t_8;
+          } else {
+            __pyx_t_9 = __pyx_t_7;
+          }
+          __pyx_t_7 = __pyx_t_9;
+        } else {
+          __pyx_t_7 = __pyx_t_6;
+        }
+        __pyx_t_6 = __pyx_t_7;
+      } else {
+        __pyx_t_6 = __pyx_t_3;
+      }
+      __pyx_t_3 = __pyx_t_6;
+    } else {
+      __pyx_t_3 = __pyx_t_2;
+    }
+    if (__pyx_t_3) {
+
+      /* "pysam/calignmentfile.pyx":2958
+ *                op == BAM_CSOFT_CLIP or \
+ *                op == BAM_CEQUAL or op == BAM_CDIFF:
+ *                 qpos += cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
+ * 
+ *         return qpos
+ */
+      __pyx_v_qpos = (__pyx_v_qpos + ((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT));
+      goto __pyx_L7;
+    }
+    __pyx_L7:;
+  }
+
+  /* "pysam/calignmentfile.pyx":2960
+ *                 qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
+ * 
+ *         return qpos             # <<<<<<<<<<<<<<
+ * 
+ *     def get_aligned_pairs(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2943; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FMUNMAP); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2943; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2943; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2943; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_t_4 = __Pyx_PyInt_From_uint32_t(__pyx_v_qpos); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __pyx_r = __pyx_t_4;
+  __pyx_t_4 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2942
- *     property mate_is_unmapped:
- *         """true if the mate is unmapped"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):
+  /* "pysam/calignmentfile.pyx":2926
+ *         return result
+ * 
+ *     def infer_query_length(self, always=True):             # <<<<<<<<<<<<<<
+ *         """inferred read length from CIGAR string.
+ * 
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mate_is_unmapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.infer_query_length", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -30102,61 +30436,3040 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped___ge
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2944
- *         def __get__(self):
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
- *     property is_reverse:
+/* "pysam/calignmentfile.pyx":2962
+ *         return qpos
+ * 
+ *     def get_aligned_pairs(self):             # <<<<<<<<<<<<<<
+ *         """a list of aligned read and reference positions.
+ *         """
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15get_aligned_pairs(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_14get_aligned_pairs[] = "AlignedSegment.get_aligned_pairs(self)\na list of aligned read and reference positions.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15get_aligned_pairs(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+  PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __Pyx_RefNannySetupContext("get_aligned_pairs (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14get_aligned_pairs(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_14get_aligned_pairs(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  uint32_t __pyx_v_k;
+  uint32_t __pyx_v_i;
+  uint32_t __pyx_v_pos;
+  uint32_t __pyx_v_qpos;
+  int __pyx_v_op;
+  uint32_t *__pyx_v_cigar_p;
+  bam1_t *__pyx_v_src;
+  PyObject *__pyx_v_result = NULL;
+  PyObject *__pyx_v_l = NULL;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  bam1_t *__pyx_t_1;
+  int __pyx_t_2;
+  PyObject *__pyx_t_3 = NULL;
+  int32_t __pyx_t_4;
+  uint16_t __pyx_t_5;
+  PyObject *__pyx_t_6 = NULL;
+  uint32_t __pyx_t_7;
+  PyObject *__pyx_t_8 = NULL;
+  int __pyx_t_9;
+  int __pyx_t_10;
+  int __pyx_t_11;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("get_aligned_pairs", 0);
+  __Pyx_TraceCall("get_aligned_pairs", __pyx_f[0], 2962);
+
+  /* "pysam/calignmentfile.pyx":2970
+ *         cdef bam1_t * src
+ * 
+ *         src = self._delegate             # <<<<<<<<<<<<<<
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return []
+ */
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":2971
+ * 
+ *         src = self._delegate
+ *         if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
+ *             return []
+ * 
+ */
+  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
+  if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":2972
+ *         src = self._delegate
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return []             # <<<<<<<<<<<<<<
+ * 
+ *         result = []
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2972; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __pyx_r = __pyx_t_3;
+    __pyx_t_3 = 0;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":2974
+ *             return []
+ * 
+ *         result = []             # <<<<<<<<<<<<<<
+ *         pos = src.core.pos
+ *         qpos = 0
+ */
+  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2974; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __pyx_v_result = ((PyObject*)__pyx_t_3);
+  __pyx_t_3 = 0;
+
+  /* "pysam/calignmentfile.pyx":2975
+ * 
+ *         result = []
+ *         pos = src.core.pos             # <<<<<<<<<<<<<<
+ *         qpos = 0
+ *         cigar_p = pysam_bam_get_cigar(src)
+ */
+  __pyx_t_4 = __pyx_v_src->core.pos;
+  __pyx_v_pos = __pyx_t_4;
+
+  /* "pysam/calignmentfile.pyx":2976
+ *         result = []
+ *         pos = src.core.pos
+ *         qpos = 0             # <<<<<<<<<<<<<<
+ *         cigar_p = pysam_bam_get_cigar(src)
+ * 
+ */
+  __pyx_v_qpos = 0;
+
+  /* "pysam/calignmentfile.pyx":2977
+ *         pos = src.core.pos
+ *         qpos = 0
+ *         cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ */
+  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
+
+  /* "pysam/calignmentfile.pyx":2979
+ *         cigar_p = pysam_bam_get_cigar(src)
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ */
+  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
+  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
+
+    /* "pysam/calignmentfile.pyx":2980
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ * 
+ */
+    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+
+    /* "pysam/calignmentfile.pyx":2981
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
+ * 
+ *             if op == BAM_CMATCH:
+ */
+    __pyx_t_3 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_3);
+    __pyx_t_3 = 0;
+
+    /* "pysam/calignmentfile.pyx":2983
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ * 
+ *             if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
+ *                 for i from pos <= i < pos + l:
+ *                     result.append((qpos, i))
+ */
+    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
+    if (__pyx_t_2) {
+
+      /* "pysam/calignmentfile.pyx":2984
+ * 
+ *             if op == BAM_CMATCH:
+ *                 for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
+ *                     result.append((qpos, i))
+ *                     qpos += 1
+ */
+      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2984; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __pyx_t_6 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2984; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_6);
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_6); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2984; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
+
+        /* "pysam/calignmentfile.pyx":2985
+ *             if op == BAM_CMATCH:
+ *                 for i from pos <= i < pos + l:
+ *                     result.append((qpos, i))             # <<<<<<<<<<<<<<
+ *                     qpos += 1
+ *                 pos += l
+ */
+        __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_qpos); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_6);
+        __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_i); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_3);
+        __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_8);
+        PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_6);
+        __Pyx_GIVEREF(__pyx_t_6);
+        PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_3);
+        __Pyx_GIVEREF(__pyx_t_3);
+        __pyx_t_6 = 0;
+        __pyx_t_3 = 0;
+        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+
+        /* "pysam/calignmentfile.pyx":2986
+ *                 for i from pos <= i < pos + l:
+ *                     result.append((qpos, i))
+ *                     qpos += 1             # <<<<<<<<<<<<<<
+ *                 pos += l
+ * 
+ */
+        __pyx_v_qpos = (__pyx_v_qpos + 1);
+      }
+
+      /* "pysam/calignmentfile.pyx":2987
+ *                     result.append((qpos, i))
+ *                     qpos += 1
+ *                 pos += l             # <<<<<<<<<<<<<<
+ * 
+ *             elif op == BAM_CINS:
+ */
+      __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_t_8, __pyx_v_l); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_3); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __pyx_v_pos = __pyx_t_7;
+      goto __pyx_L6;
+    }
+
+    /* "pysam/calignmentfile.pyx":2989
+ *                 pos += l
+ * 
+ *             elif op == BAM_CINS:             # <<<<<<<<<<<<<<
+ *                 for i from pos <= i < pos + l:
+ *                     result.append((qpos, None))
+ */
+    __pyx_t_2 = ((__pyx_v_op == BAM_CINS) != 0);
+    if (__pyx_t_2) {
+
+      /* "pysam/calignmentfile.pyx":2990
+ * 
+ *             elif op == BAM_CINS:
+ *                 for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
+ *                     result.append((qpos, None))
+ *                     qpos += 1
+ */
+      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2990; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __pyx_t_8 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2990; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2990; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
+
+        /* "pysam/calignmentfile.pyx":2991
+ *             elif op == BAM_CINS:
+ *                 for i from pos <= i < pos + l:
+ *                     result.append((qpos, None))             # <<<<<<<<<<<<<<
+ *                     qpos += 1
+ * 
+ */
+        __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_v_qpos); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_8);
+        __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_3);
+        PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_8);
+        __Pyx_GIVEREF(__pyx_t_8);
+        __Pyx_INCREF(Py_None);
+        PyTuple_SET_ITEM(__pyx_t_3, 1, Py_None);
+        __Pyx_GIVEREF(Py_None);
+        __pyx_t_8 = 0;
+        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_3); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+
+        /* "pysam/calignmentfile.pyx":2992
+ *                 for i from pos <= i < pos + l:
+ *                     result.append((qpos, None))
+ *                     qpos += 1             # <<<<<<<<<<<<<<
+ * 
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+ */
+        __pyx_v_qpos = (__pyx_v_qpos + 1);
+      }
+      goto __pyx_L6;
+    }
+
+    /* "pysam/calignmentfile.pyx":2994
+ *                     qpos += 1
+ * 
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
+ *                 for i from pos <= i < pos + l:
+ *                     result.append((None, i))
+ */
+    __pyx_t_2 = ((__pyx_v_op == BAM_CDEL) != 0);
+    if (!__pyx_t_2) {
+      __pyx_t_10 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
+      __pyx_t_11 = __pyx_t_10;
+    } else {
+      __pyx_t_11 = __pyx_t_2;
+    }
+    if (__pyx_t_11) {
+
+      /* "pysam/calignmentfile.pyx":2995
+ * 
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+ *                 for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
+ *                     result.append((None, i))
+ *                 pos += l
+ */
+      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2995; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __pyx_t_8 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2995; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2995; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
+
+        /* "pysam/calignmentfile.pyx":2996
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+ *                 for i from pos <= i < pos + l:
+ *                     result.append((None, i))             # <<<<<<<<<<<<<<
+ *                 pos += l
+ * 
+ */
+        __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_v_i); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_8);
+        __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_3);
+        __Pyx_INCREF(Py_None);
+        PyTuple_SET_ITEM(__pyx_t_3, 0, Py_None);
+        __Pyx_GIVEREF(Py_None);
+        PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_8);
+        __Pyx_GIVEREF(__pyx_t_8);
+        __pyx_t_8 = 0;
+        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_3); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      }
+
+      /* "pysam/calignmentfile.pyx":2997
+ *                 for i from pos <= i < pos + l:
+ *                     result.append((None, i))
+ *                 pos += l             # <<<<<<<<<<<<<<
+ * 
+ *         return result
+ */
+      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2997; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __pyx_t_8 = PyNumber_InPlaceAdd(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2997; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2997; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      __pyx_v_pos = __pyx_t_7;
+      goto __pyx_L6;
+    }
+    __pyx_L6:;
+  }
+
+  /* "pysam/calignmentfile.pyx":2999
+ *                 pos += l
+ * 
+ *         return result             # <<<<<<<<<<<<<<
+ * 
+ *     def get_blocks(self):
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __Pyx_INCREF(__pyx_v_result);
+  __pyx_r = __pyx_v_result;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":2962
+ *         return qpos
+ * 
+ *     def get_aligned_pairs(self):             # <<<<<<<<<<<<<<
+ *         """a list of aligned read and reference positions.
+ *         """
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_6);
+  __Pyx_XDECREF(__pyx_t_8);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.get_aligned_pairs", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_result);
+  __Pyx_XDECREF(__pyx_v_l);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3001
+ *         return result
+ * 
+ *     def get_blocks(self):             # <<<<<<<<<<<<<<
+ *         """ a list of start and end positions of
+ *         aligned gapless blocks.
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_17get_blocks(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_16get_blocks[] = "AlignedSegment.get_blocks(self)\n a list of start and end positions of\n        aligned gapless blocks.\n\n        The start and end positions are in genomic \n        coordinates. \n      \n        Blocks are not normalized, i.e. two blocks \n        might be directly adjacent. This happens if\n        the two blocks are separated by an insertion \n        in the read.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_17get_blocks(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("get_blocks (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16get_blocks(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_16get_blocks(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  uint32_t __pyx_v_k;
+  uint32_t __pyx_v_pos;
+  uint32_t __pyx_v_l;
+  int __pyx_v_op;
+  uint32_t *__pyx_v_cigar_p;
+  bam1_t *__pyx_v_src;
+  PyObject *__pyx_v_result = NULL;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  bam1_t *__pyx_t_1;
+  int __pyx_t_2;
+  PyObject *__pyx_t_3 = NULL;
+  int32_t __pyx_t_4;
+  uint16_t __pyx_t_5;
+  PyObject *__pyx_t_6 = NULL;
+  PyObject *__pyx_t_7 = NULL;
+  int __pyx_t_8;
+  int __pyx_t_9;
+  int __pyx_t_10;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("get_blocks", 0);
+  __Pyx_TraceCall("get_blocks", __pyx_f[0], 3001);
+
+  /* "pysam/calignmentfile.pyx":3019
+ *         cdef bam1_t * src
+ * 
+ *         src = self._delegate             # <<<<<<<<<<<<<<
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return []
+ */
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":3020
+ * 
+ *         src = self._delegate
+ *         if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
+ *             return []
+ * 
+ */
+  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
+  if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":3021
+ *         src = self._delegate
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return []             # <<<<<<<<<<<<<<
+ * 
+ *         result = []
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3021; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __pyx_r = __pyx_t_3;
+    __pyx_t_3 = 0;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3023
+ *             return []
+ * 
+ *         result = []             # <<<<<<<<<<<<<<
+ *         pos = src.core.pos
+ *         cigar_p = pysam_bam_get_cigar(src)
+ */
+  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3023; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __pyx_v_result = ((PyObject*)__pyx_t_3);
+  __pyx_t_3 = 0;
+
+  /* "pysam/calignmentfile.pyx":3024
+ * 
+ *         result = []
+ *         pos = src.core.pos             # <<<<<<<<<<<<<<
+ *         cigar_p = pysam_bam_get_cigar(src)
+ *         l = 0
+ */
+  __pyx_t_4 = __pyx_v_src->core.pos;
+  __pyx_v_pos = __pyx_t_4;
+
+  /* "pysam/calignmentfile.pyx":3025
+ *         result = []
+ *         pos = src.core.pos
+ *         cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
+ *         l = 0
+ * 
+ */
+  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
+
+  /* "pysam/calignmentfile.pyx":3026
+ *         pos = src.core.pos
+ *         cigar_p = pysam_bam_get_cigar(src)
+ *         l = 0             # <<<<<<<<<<<<<<
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ */
+  __pyx_v_l = 0;
+
+  /* "pysam/calignmentfile.pyx":3028
+ *         l = 0
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ */
+  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
+  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
+
+    /* "pysam/calignmentfile.pyx":3029
+ * 
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ *             if op == BAM_CMATCH:
+ */
+    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+
+    /* "pysam/calignmentfile.pyx":3030
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
+ *             if op == BAM_CMATCH:
+ *                 result.append((pos, pos + l))
+ */
+    __pyx_v_l = ((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT);
+
+    /* "pysam/calignmentfile.pyx":3031
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ *             if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
+ *                 result.append((pos, pos + l))
+ *                 pos += l
+ */
+    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
+    if (__pyx_t_2) {
+
+      /* "pysam/calignmentfile.pyx":3032
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ *             if op == BAM_CMATCH:
+ *                 result.append((pos, pos + l))             # <<<<<<<<<<<<<<
+ *                 pos += l
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+ */
+      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __pyx_t_6 = __Pyx_PyInt_From_uint32_t((__pyx_v_pos + __pyx_v_l)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_6);
+      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_7);
+      PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_3);
+      __Pyx_GIVEREF(__pyx_t_3);
+      PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_6);
+      __Pyx_GIVEREF(__pyx_t_6);
+      __pyx_t_3 = 0;
+      __pyx_t_6 = 0;
+      __pyx_t_8 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_7); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+
+      /* "pysam/calignmentfile.pyx":3033
+ *             if op == BAM_CMATCH:
+ *                 result.append((pos, pos + l))
+ *                 pos += l             # <<<<<<<<<<<<<<
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+ *                 pos += l
+ */
+      __pyx_v_pos = (__pyx_v_pos + __pyx_v_l);
+      goto __pyx_L6;
+    }
+
+    /* "pysam/calignmentfile.pyx":3034
+ *                 result.append((pos, pos + l))
+ *                 pos += l
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
+ *                 pos += l
+ * 
+ */
+    __pyx_t_2 = ((__pyx_v_op == BAM_CDEL) != 0);
+    if (!__pyx_t_2) {
+      __pyx_t_9 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
+      __pyx_t_10 = __pyx_t_9;
+    } else {
+      __pyx_t_10 = __pyx_t_2;
+    }
+    if (__pyx_t_10) {
+
+      /* "pysam/calignmentfile.pyx":3035
+ *                 pos += l
+ *             elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+ *                 pos += l             # <<<<<<<<<<<<<<
+ * 
+ *         return result
+ */
+      __pyx_v_pos = (__pyx_v_pos + __pyx_v_l);
+      goto __pyx_L6;
+    }
+    __pyx_L6:;
+  }
+
+  /* "pysam/calignmentfile.pyx":3037
+ *                 pos += l
+ * 
+ *         return result             # <<<<<<<<<<<<<<
+ * 
+ *     def get_overlap(self, uint32_t start, uint32_t end):
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __Pyx_INCREF(__pyx_v_result);
+  __pyx_r = __pyx_v_result;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3001
+ *         return result
+ * 
+ *     def get_blocks(self):             # <<<<<<<<<<<<<<
+ *         """ a list of start and end positions of
+ *         aligned gapless blocks.
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_6);
+  __Pyx_XDECREF(__pyx_t_7);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.get_blocks", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_result);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3039
+ *         return result
+ * 
+ *     def get_overlap(self, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
+ *         """return number of aligned bases of read overlapping the interval
+ *         *start* and *end* on the reference sequence.
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_19get_overlap(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_18get_overlap[] = "AlignedSegment.get_overlap(self, uint32_t start, uint32_t end)\nreturn number of aligned bases of read overlapping the interval\n        *start* and *end* on the reference sequence.\n\n        Return None if cigar alignment is not available.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_19get_overlap(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  uint32_t __pyx_v_start;
+  uint32_t __pyx_v_end;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("get_overlap (wrapper)", 0);
+  {
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_start,&__pyx_n_s_end,0};
+    PyObject* values[2] = {0,0};
+    if (unlikely(__pyx_kwds)) {
+      Py_ssize_t kw_args;
+      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
+      switch (pos_args) {
+        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+      kw_args = PyDict_Size(__pyx_kwds);
+      switch (pos_args) {
+        case  0:
+        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_start)) != 0)) kw_args--;
+        else goto __pyx_L5_argtuple_error;
+        case  1:
+        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end)) != 0)) kw_args--;
+        else {
+          __Pyx_RaiseArgtupleInvalid("get_overlap", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3039; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        }
+      }
+      if (unlikely(kw_args > 0)) {
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "get_overlap") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3039; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
+      goto __pyx_L5_argtuple_error;
+    } else {
+      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+    }
+    __pyx_v_start = __Pyx_PyInt_As_uint32_t(values[0]); if (unlikely((__pyx_v_start == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3039; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_end = __Pyx_PyInt_As_uint32_t(values[1]); if (unlikely((__pyx_v_end == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3039; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("get_overlap", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3039; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.get_overlap", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_RefNannyFinishContext();
+  return NULL;
+  __pyx_L4_argument_unpacking_done:;
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_18get_overlap(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), __pyx_v_start, __pyx_v_end);
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_18get_overlap(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, uint32_t __pyx_v_start, uint32_t __pyx_v_end) {
+  uint32_t __pyx_v_k;
+  uint32_t __pyx_v_pos;
+  uint32_t __pyx_v_overlap;
+  int __pyx_v_op;
+  int __pyx_v_o;
+  uint32_t *__pyx_v_cigar_p;
+  bam1_t *__pyx_v_src;
+  PyObject *__pyx_v_l = NULL;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  bam1_t *__pyx_t_1;
+  int __pyx_t_2;
+  int32_t __pyx_t_3;
+  uint16_t __pyx_t_4;
+  PyObject *__pyx_t_5 = NULL;
+  uint32_t __pyx_t_6;
+  PyObject *__pyx_t_7 = NULL;
+  PyObject *__pyx_t_8 = NULL;
+  PyObject *__pyx_t_9 = NULL;
+  uint32_t __pyx_t_10;
+  uint32_t __pyx_t_11;
+  int __pyx_t_12;
+  int __pyx_t_13;
+  int __pyx_t_14;
+  int __pyx_t_15;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("get_overlap", 0);
+  __Pyx_TraceCall("get_overlap", __pyx_f[0], 3039);
+
+  /* "pysam/calignmentfile.pyx":3050
+ *         cdef bam1_t * src
+ * 
+ *         overlap = 0             # <<<<<<<<<<<<<<
+ * 
+ *         src = self._delegate
+ */
+  __pyx_v_overlap = 0;
+
+  /* "pysam/calignmentfile.pyx":3052
+ *         overlap = 0
+ * 
+ *         src = self._delegate             # <<<<<<<<<<<<<<
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return None
+ */
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":3053
+ * 
+ *         src = self._delegate
+ *         if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
+ *             return None
+ *         pos = src.core.pos
+ */
+  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
+  if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":3054
+ *         src = self._delegate
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return None             # <<<<<<<<<<<<<<
+ *         pos = src.core.pos
+ *         o = 0
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3055
+ *         if pysam_get_n_cigar(src) == 0:
+ *             return None
+ *         pos = src.core.pos             # <<<<<<<<<<<<<<
+ *         o = 0
+ * 
+ */
+  __pyx_t_3 = __pyx_v_src->core.pos;
+  __pyx_v_pos = __pyx_t_3;
+
+  /* "pysam/calignmentfile.pyx":3056
+ *             return None
+ *         pos = src.core.pos
+ *         o = 0             # <<<<<<<<<<<<<<
+ * 
+ *         cigar_p = pysam_bam_get_cigar(src)
+ */
+  __pyx_v_o = 0;
+
+  /* "pysam/calignmentfile.pyx":3058
+ *         o = 0
+ * 
+ *         cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ */
+  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
+
+  /* "pysam/calignmentfile.pyx":3059
+ * 
+ *         cigar_p = pysam_bam_get_cigar(src)
+ *         for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ */
+  __pyx_t_4 = pysam_get_n_cigar(__pyx_v_src);
+  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
+
+    /* "pysam/calignmentfile.pyx":3060
+ *         cigar_p = pysam_bam_get_cigar(src)
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ * 
+ */
+    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+
+    /* "pysam/calignmentfile.pyx":3061
+ *         for k from 0 <= k < pysam_get_n_cigar(src):
+ *             op = cigar_p[k] & BAM_CIGAR_MASK
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
+ * 
+ *             if op == BAM_CMATCH:
+ */
+    __pyx_t_5 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3061; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
+    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_5);
+    __pyx_t_5 = 0;
+
+    /* "pysam/calignmentfile.pyx":3063
+ *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ * 
+ *             if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
+ *                 o = min( pos + l, end) - max( pos, start )
+ *                 if o > 0: overlap += o
+ */
+    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
+    if (__pyx_t_2) {
+
+      /* "pysam/calignmentfile.pyx":3064
+ * 
+ *             if op == BAM_CMATCH:
+ *                 o = min( pos + l, end) - max( pos, start )             # <<<<<<<<<<<<<<
+ *                 if o > 0: overlap += o
+ * 
+ */
+      __pyx_t_6 = __pyx_v_end;
+      __pyx_t_5 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
+      __pyx_t_7 = PyNumber_Add(__pyx_t_5, __pyx_v_l); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_7);
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+      __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_t_6); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      __pyx_t_9 = PyObject_RichCompare(__pyx_t_8, __pyx_t_7, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+      if (__pyx_t_2) {
+        __pyx_t_9 = __Pyx_PyInt_From_uint32_t(__pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __Pyx_GOTREF(__pyx_t_9);
+        __pyx_t_5 = __pyx_t_9;
+        __pyx_t_9 = 0;
+      } else {
+        __Pyx_INCREF(__pyx_t_7);
+        __pyx_t_5 = __pyx_t_7;
+      }
+      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+      __pyx_t_6 = __pyx_v_start;
+      __pyx_t_10 = __pyx_v_pos;
+      if (((__pyx_t_6 > __pyx_t_10) != 0)) {
+        __pyx_t_11 = __pyx_t_6;
+      } else {
+        __pyx_t_11 = __pyx_t_10;
+      }
+      __pyx_t_7 = __Pyx_PyInt_From_uint32_t(__pyx_t_11); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_7);
+      __pyx_t_9 = PyNumber_Subtract(__pyx_t_5, __pyx_t_7); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_9);
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+      __pyx_t_12 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_12 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+      __pyx_v_o = __pyx_t_12;
+
+      /* "pysam/calignmentfile.pyx":3065
+ *             if op == BAM_CMATCH:
+ *                 o = min( pos + l, end) - max( pos, start )
+ *                 if o > 0: overlap += o             # <<<<<<<<<<<<<<
+ * 
+ *             if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
+ */
+      __pyx_t_2 = ((__pyx_v_o > 0) != 0);
+      if (__pyx_t_2) {
+        __pyx_v_overlap = (__pyx_v_overlap + __pyx_v_o);
+        goto __pyx_L7;
+      }
+      __pyx_L7:;
+      goto __pyx_L6;
+    }
+    __pyx_L6:;
+
+    /* "pysam/calignmentfile.pyx":3067
+ *                 if o > 0: overlap += o
+ * 
+ *             if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
+ *                 pos += l
+ * 
+ */
+    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
+    if (!__pyx_t_2) {
+      __pyx_t_13 = ((__pyx_v_op == BAM_CDEL) != 0);
+      if (!__pyx_t_13) {
+        __pyx_t_14 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
+        __pyx_t_15 = __pyx_t_14;
+      } else {
+        __pyx_t_15 = __pyx_t_13;
+      }
+      __pyx_t_13 = __pyx_t_15;
+    } else {
+      __pyx_t_13 = __pyx_t_2;
+    }
+    if (__pyx_t_13) {
+
+      /* "pysam/calignmentfile.pyx":3068
+ * 
+ *             if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
+ *                 pos += l             # <<<<<<<<<<<<<<
+ * 
+ *         return overlap
+ */
+      __pyx_t_9 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3068; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_9);
+      __pyx_t_7 = PyNumber_InPlaceAdd(__pyx_t_9, __pyx_v_l); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3068; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_7);
+      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+      __pyx_t_11 = __Pyx_PyInt_As_uint32_t(__pyx_t_7); if (unlikely((__pyx_t_11 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3068; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+      __pyx_v_pos = __pyx_t_11;
+      goto __pyx_L8;
+    }
+    __pyx_L8:;
+  }
+
+  /* "pysam/calignmentfile.pyx":3070
+ *                 pos += l
+ * 
+ *         return overlap             # <<<<<<<<<<<<<<
+ * 
+ *     #####################################################
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_7 = __Pyx_PyInt_From_uint32_t(__pyx_v_overlap); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_7);
+  __pyx_r = __pyx_t_7;
+  __pyx_t_7 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3039
+ *         return result
+ * 
+ *     def get_overlap(self, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
+ *         """return number of aligned bases of read overlapping the interval
+ *         *start* and *end* on the reference sequence.
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_5);
+  __Pyx_XDECREF(__pyx_t_7);
+  __Pyx_XDECREF(__pyx_t_8);
+  __Pyx_XDECREF(__pyx_t_9);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.get_overlap", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_l);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3115
+ *         or None.
+ *         """
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             cdef uint32_t * cigar_p
+ *             cdef bam1_t * src
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigartuples_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigartuples_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigartuples___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigartuples___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  uint32_t *__pyx_v_cigar_p;
+  bam1_t *__pyx_v_src;
+  uint32_t __pyx_v_op;
+  uint32_t __pyx_v_l;
+  int __pyx_v_k;
+  PyObject *__pyx_v_cigar = NULL;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  bam1_t *__pyx_t_1;
+  int __pyx_t_2;
+  PyObject *__pyx_t_3 = NULL;
+  uint16_t __pyx_t_4;
+  PyObject *__pyx_t_5 = NULL;
+  PyObject *__pyx_t_6 = NULL;
+  int __pyx_t_7;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3115);
+
+  /* "pysam/calignmentfile.pyx":3121
+ *             cdef int k
+ * 
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ *             if pysam_get_n_cigar(src) == 0:
+ *                 return None
+ */
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":3122
+ * 
+ *             src = self._delegate
+ *             if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
+ *                 return None
+ * 
+ */
+  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
+  if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":3123
+ *             src = self._delegate
+ *             if pysam_get_n_cigar(src) == 0:
+ *                 return None             # <<<<<<<<<<<<<<
+ * 
+ *             cigar = []
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(Py_None);
+    __pyx_r = Py_None;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3125
+ *                 return None
+ * 
+ *             cigar = []             # <<<<<<<<<<<<<<
+ * 
+ *             cigar_p = pysam_bam_get_cigar(src);
+ */
+  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __pyx_v_cigar = ((PyObject*)__pyx_t_3);
+  __pyx_t_3 = 0;
+
+  /* "pysam/calignmentfile.pyx":3127
+ *             cigar = []
+ * 
+ *             cigar_p = pysam_bam_get_cigar(src);             # <<<<<<<<<<<<<<
+ *             for k from 0 <= k < pysam_get_n_cigar(src):
+ *                 op = cigar_p[k] & BAM_CIGAR_MASK
+ */
+  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
+
+  /* "pysam/calignmentfile.pyx":3128
+ * 
+ *             cigar_p = pysam_bam_get_cigar(src);
+ *             for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
+ *                 op = cigar_p[k] & BAM_CIGAR_MASK
+ *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ */
+  __pyx_t_4 = pysam_get_n_cigar(__pyx_v_src);
+  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
+
+    /* "pysam/calignmentfile.pyx":3129
+ *             cigar_p = pysam_bam_get_cigar(src);
+ *             for k from 0 <= k < pysam_get_n_cigar(src):
+ *                 op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
+ *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ *                 cigar.append((op, l))
+ */
+    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+
+    /* "pysam/calignmentfile.pyx":3130
+ *             for k from 0 <= k < pysam_get_n_cigar(src):
+ *                 op = cigar_p[k] & BAM_CIGAR_MASK
+ *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
+ *                 cigar.append((op, l))
+ *             return cigar
+ */
+    __pyx_v_l = ((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT);
+
+    /* "pysam/calignmentfile.pyx":3131
+ *                 op = cigar_p[k] & BAM_CIGAR_MASK
+ *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ *                 cigar.append((op, l))             # <<<<<<<<<<<<<<
+ *             return cigar
+ * 
+ */
+    __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_op); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3131; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __pyx_t_5 = __Pyx_PyInt_From_uint32_t(__pyx_v_l); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3131; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
+    __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3131; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_6);
+    PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_3);
+    __Pyx_GIVEREF(__pyx_t_3);
+    PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_t_5);
+    __Pyx_GIVEREF(__pyx_t_5);
+    __pyx_t_3 = 0;
+    __pyx_t_5 = 0;
+    __pyx_t_7 = __Pyx_PyList_Append(__pyx_v_cigar, __pyx_t_6); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3131; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3132
+ *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
+ *                 cigar.append((op, l))
+ *             return cigar             # <<<<<<<<<<<<<<
+ * 
+ *         def __set__(self, values):
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __Pyx_INCREF(__pyx_v_cigar);
+  __pyx_r = __pyx_v_cigar;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3115
+ *         or None.
+ *         """
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             cdef uint32_t * cigar_p
+ *             cdef bam1_t * src
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_5);
+  __Pyx_XDECREF(__pyx_t_6);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.cigartuples.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_cigar);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3134
+ *             return cigar
+ * 
+ *         def __set__(self, values):             # <<<<<<<<<<<<<<
+ *             cdef uint32_t * p
+ *             cdef bam1_t * src
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigartuples_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_values); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigartuples_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_values) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigartuples_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_values));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_11cigartuples_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_values) {
+  uint32_t *__pyx_v_p;
+  bam1_t *__pyx_v_src;
+  PyObject *__pyx_v_op = 0;
+  PyObject *__pyx_v_l = 0;
+  int __pyx_v_k;
+  int __pyx_v_ncigar;
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  bam1_t *__pyx_t_1;
+  int __pyx_t_2;
+  int __pyx_t_3;
+  PyObject *__pyx_t_4 = NULL;
+  Py_ssize_t __pyx_t_5;
+  PyObject *(*__pyx_t_6)(PyObject *);
+  PyObject *__pyx_t_7 = NULL;
+  PyObject *__pyx_t_8 = NULL;
+  PyObject *__pyx_t_9 = NULL;
+  PyObject *__pyx_t_10 = NULL;
+  PyObject *(*__pyx_t_11)(PyObject *);
+  uint32_t __pyx_t_12;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3134);
+  __Pyx_INCREF(__pyx_v_values);
+
+  /* "pysam/calignmentfile.pyx":3140
+ *             cdef int k, ncigar
+ * 
+ *             k = 0             # <<<<<<<<<<<<<<
+ * 
+ *             src = self._delegate
+ */
+  __pyx_v_k = 0;
+
+  /* "pysam/calignmentfile.pyx":3142
+ *             k = 0
+ * 
+ *             src = self._delegate             # <<<<<<<<<<<<<<
+ * 
+ *             # get location of cigar string
+ */
+  __pyx_t_1 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_1;
+
+  /* "pysam/calignmentfile.pyx":3145
+ * 
+ *             # get location of cigar string
+ *             p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
+ * 
+ *             # empty values for cigar string
+ */
+  __pyx_v_p = pysam_bam_get_cigar(__pyx_v_src);
+
+  /* "pysam/calignmentfile.pyx":3148
+ * 
+ *             # empty values for cigar string
+ *             if values is None:             # <<<<<<<<<<<<<<
+ *                 values = []
+ * 
+ */
+  __pyx_t_2 = (__pyx_v_values == Py_None);
+  __pyx_t_3 = (__pyx_t_2 != 0);
+  if (__pyx_t_3) {
+
+    /* "pysam/calignmentfile.pyx":3149
+ *             # empty values for cigar string
+ *             if values is None:
+ *                 values = []             # <<<<<<<<<<<<<<
+ * 
+ *             ncigar = len(values)
+ */
+    __pyx_t_4 = PyList_New(0); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_DECREF_SET(__pyx_v_values, __pyx_t_4);
+    __pyx_t_4 = 0;
+    goto __pyx_L3;
+  }
+  __pyx_L3:;
+
+  /* "pysam/calignmentfile.pyx":3151
+ *                 values = []
+ * 
+ *             ncigar = len(values)             # <<<<<<<<<<<<<<
+ *             # create space for cigar data within src.data
+ *             pysam_bam_update(src,
+ */
+  __pyx_t_5 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_ncigar = __pyx_t_5;
+
+  /* "pysam/calignmentfile.pyx":3153
+ *             ncigar = len(values)
+ *             # create space for cigar data within src.data
+ *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
+ *                              pysam_get_n_cigar(src) * 4,
+ *                              ncigar * 4,
+ */
+  pysam_bam_update(__pyx_v_src, (pysam_get_n_cigar(__pyx_v_src) * 4), (__pyx_v_ncigar * 4), ((uint8_t *)__pyx_v_p));
+
+  /* "pysam/calignmentfile.pyx":3159
+ * 
+ *             # length is number of cigar operations, not bytes
+ *             pysam_set_n_cigar(src, ncigar)             # <<<<<<<<<<<<<<
+ * 
+ *             # re-acquire pointer to location in memory
+ */
+  pysam_set_n_cigar(__pyx_v_src, __pyx_v_ncigar);
+
+  /* "pysam/calignmentfile.pyx":3163
+ *             # re-acquire pointer to location in memory
+ *             # as it might have moved
+ *             p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
+ * 
+ *             # insert cigar operations
+ */
+  __pyx_v_p = pysam_bam_get_cigar(__pyx_v_src);
+
+  /* "pysam/calignmentfile.pyx":3166
+ * 
+ *             # insert cigar operations
+ *             for op, l in values:             # <<<<<<<<<<<<<<
+ *                 p[k] = l << BAM_CIGAR_SHIFT | op
+ *                 k += 1
+ */
+  if (PyList_CheckExact(__pyx_v_values) || PyTuple_CheckExact(__pyx_v_values)) {
+    __pyx_t_4 = __pyx_v_values; __Pyx_INCREF(__pyx_t_4); __pyx_t_5 = 0;
+    __pyx_t_6 = NULL;
+  } else {
+    __pyx_t_5 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __pyx_t_6 = Py_TYPE(__pyx_t_4)->tp_iternext;
+  }
+  for (;;) {
+    if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_4)) {
+      if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_4)) break;
+      #if CYTHON_COMPILING_IN_CPYTHON
+      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #else
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #endif
+    } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_4)) {
+      if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_4)) break;
+      #if CYTHON_COMPILING_IN_CPYTHON
+      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #else
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      #endif
+    } else {
+      __pyx_t_7 = __pyx_t_6(__pyx_t_4);
+      if (unlikely(!__pyx_t_7)) {
+        PyObject* exc_type = PyErr_Occurred();
+        if (exc_type) {
+          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        }
+        break;
+      }
+      __Pyx_GOTREF(__pyx_t_7);
+    }
+    if ((likely(PyTuple_CheckExact(__pyx_t_7))) || (PyList_CheckExact(__pyx_t_7))) {
+      PyObject* sequence = __pyx_t_7;
+      #if CYTHON_COMPILING_IN_CPYTHON
+      Py_ssize_t size = Py_SIZE(sequence);
+      #else
+      Py_ssize_t size = PySequence_Size(sequence);
+      #endif
+      if (unlikely(size != 2)) {
+        if (size > 2) __Pyx_RaiseTooManyValuesError(2);
+        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      }
+      #if CYTHON_COMPILING_IN_CPYTHON
+      if (likely(PyTuple_CheckExact(sequence))) {
+        __pyx_t_8 = PyTuple_GET_ITEM(sequence, 0); 
+        __pyx_t_9 = PyTuple_GET_ITEM(sequence, 1); 
+      } else {
+        __pyx_t_8 = PyList_GET_ITEM(sequence, 0); 
+        __pyx_t_9 = PyList_GET_ITEM(sequence, 1); 
+      }
+      __Pyx_INCREF(__pyx_t_8);
+      __Pyx_INCREF(__pyx_t_9);
+      #else
+      __pyx_t_8 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      __pyx_t_9 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_9);
+      #endif
+      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+    } else {
+      Py_ssize_t index = -1;
+      __pyx_t_10 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_10);
+      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+      __pyx_t_11 = Py_TYPE(__pyx_t_10)->tp_iternext;
+      index = 0; __pyx_t_8 = __pyx_t_11(__pyx_t_10); if (unlikely(!__pyx_t_8)) goto __pyx_L6_unpacking_failed;
+      __Pyx_GOTREF(__pyx_t_8);
+      index = 1; __pyx_t_9 = __pyx_t_11(__pyx_t_10); if (unlikely(!__pyx_t_9)) goto __pyx_L6_unpacking_failed;
+      __Pyx_GOTREF(__pyx_t_9);
+      if (__Pyx_IternextUnpackEndCheck(__pyx_t_11(__pyx_t_10), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = NULL;
+      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
+      goto __pyx_L7_unpacking_done;
+      __pyx_L6_unpacking_failed:;
+      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
+      __pyx_t_11 = NULL;
+      if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_L7_unpacking_done:;
+    }
+    __Pyx_XDECREF_SET(__pyx_v_op, __pyx_t_8);
+    __pyx_t_8 = 0;
+    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_9);
+    __pyx_t_9 = 0;
+
+    /* "pysam/calignmentfile.pyx":3167
+ *             # insert cigar operations
+ *             for op, l in values:
+ *                 p[k] = l << BAM_CIGAR_SHIFT | op             # <<<<<<<<<<<<<<
+ *                 k += 1
+ * 
+ */
+    __pyx_t_7 = __Pyx_PyInt_From_int(BAM_CIGAR_SHIFT); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __pyx_t_9 = PyNumber_Lshift(__pyx_v_l, __pyx_t_7); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_9);
+    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+    __pyx_t_7 = PyNumber_Or(__pyx_t_9, __pyx_v_op); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+    __pyx_t_12 = __Pyx_PyInt_As_uint32_t(__pyx_t_7); if (unlikely((__pyx_t_12 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+    (__pyx_v_p[__pyx_v_k]) = __pyx_t_12;
+
+    /* "pysam/calignmentfile.pyx":3168
+ *             for op, l in values:
+ *                 p[k] = l << BAM_CIGAR_SHIFT | op
+ *                 k += 1             # <<<<<<<<<<<<<<
+ * 
+ *             ## setting the cigar string requires updating the bin
+ */
+    __pyx_v_k = (__pyx_v_k + 1);
+  }
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+
+  /* "pysam/calignmentfile.pyx":3171
+ * 
+ *             ## setting the cigar string requires updating the bin
+ *             pysam_set_bin(src,             # <<<<<<<<<<<<<<
+ *                           hts_reg2bin(
+ *                               src.core.pos,
+ */
+  pysam_set_bin(__pyx_v_src, hts_reg2bin(__pyx_v_src->core.pos, bam_endpos(__pyx_v_src), 14, 5));
+
+  /* "pysam/calignmentfile.pyx":3134
+ *             return cigar
+ * 
+ *         def __set__(self, values):             # <<<<<<<<<<<<<<
+ *             cdef uint32_t * p
+ *             cdef bam1_t * src
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_7);
+  __Pyx_XDECREF(__pyx_t_8);
+  __Pyx_XDECREF(__pyx_t_9);
+  __Pyx_XDECREF(__pyx_t_10);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.cigartuples.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_op);
+  __Pyx_XDECREF(__pyx_v_l);
+  __Pyx_XDECREF(__pyx_v_values);
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3180
+ * 
+ * 
+ *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
+ *                  value_type = None,
+ *                  replace = True):
+ */
+
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21setTag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static PyObject *__pyx_f_5pysam_14calignmentfile_14AlignedSegment_setTag(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_tag, PyObject *__pyx_v_value, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_14calignmentfile_14AlignedSegment_setTag *__pyx_optional_args) {
+
+  /* "pysam/calignmentfile.pyx":3181
+ * 
+ *     cpdef setTag(self, tag, value,
+ *                  value_type = None,             # <<<<<<<<<<<<<<
+ *                  replace = True):
+ *         '''
+ */
+  PyObject *__pyx_v_value_type = ((PyObject *)Py_None);
+
+  /* "pysam/calignmentfile.pyx":3182
+ *     cpdef setTag(self, tag, value,
+ *                  value_type = None,
+ *                  replace = True):             # <<<<<<<<<<<<<<
+ *         '''
+ *         Set optional field of alignment *tag* to *value*.  *value_type* may be specified,
+ */
+  PyObject *__pyx_v_replace = ((PyObject *)Py_True);
+  int __pyx_v_value_size;
+  uint8_t *__pyx_v_value_ptr;
+  uint8_t *__pyx_v_existing_ptr;
+  uint8_t __pyx_v_type_code;
+  float __pyx_v_float_value;
+  double __pyx_v_double_value;
+  int32_t __pyx_v_int_value;
+  bam1_t *__pyx_v_src;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  bam1_t *__pyx_t_4;
+  Py_ssize_t __pyx_t_5;
+  int __pyx_t_6;
+  uint8_t __pyx_t_7;
+  struct __pyx_opt_args_5pysam_14calignmentfile__getTypeCode __pyx_t_8;
+  char *__pyx_t_9;
+  int32_t __pyx_t_10;
+  double __pyx_t_11;
+  float __pyx_t_12;
+  char const *__pyx_t_13;
+  char const *__pyx_t_14;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("setTag", 0);
+  __Pyx_TraceCall("setTag", __pyx_f[0], 3180);
+  if (__pyx_optional_args) {
+    if (__pyx_optional_args->__pyx_n > 0) {
+      __pyx_v_value_type = __pyx_optional_args->value_type;
+      if (__pyx_optional_args->__pyx_n > 1) {
+        __pyx_v_replace = __pyx_optional_args->replace;
+      }
+    }
+  }
+  __Pyx_INCREF(__pyx_v_tag);
+  __Pyx_INCREF(__pyx_v_value);
+
+  /* "pysam/calignmentfile.pyx":3180
+ * 
+ * 
+ *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
+ *                  value_type = None,
+ *                  replace = True):
+ */
+  /* Check if called by wrapper */
+  if (unlikely(__pyx_skip_dispatch)) ;
+  /* Check if overridden in Python */
+  else if (unlikely(Py_TYPE(((PyObject *)__pyx_v_self))->tp_dictoffset != 0)) {
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setTag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    if (!PyCFunction_Check(__pyx_t_1) || (PyCFunction_GET_FUNCTION(__pyx_t_1) != (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21setTag)) {
+      __Pyx_XDECREF(__pyx_r);
+      __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_2);
+      __Pyx_INCREF(__pyx_v_tag);
+      PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_tag);
+      __Pyx_GIVEREF(__pyx_v_tag);
+      __Pyx_INCREF(__pyx_v_value);
+      PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value);
+      __Pyx_GIVEREF(__pyx_v_value);
+      __Pyx_INCREF(__pyx_v_value_type);
+      PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_value_type);
+      __Pyx_GIVEREF(__pyx_v_value_type);
+      __Pyx_INCREF(__pyx_v_replace);
+      PyTuple_SET_ITEM(__pyx_t_2, 3, __pyx_v_replace);
+      __Pyx_GIVEREF(__pyx_v_replace);
+      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+      __pyx_r = __pyx_t_3;
+      __pyx_t_3 = 0;
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      goto __pyx_L0;
+    }
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3198
+ *         cdef double   double_value
+ *         cdef int32_t  int_value
+ *         cdef bam1_t * src = self._delegate             # <<<<<<<<<<<<<<
+ *         cdef char * _value_type
+ * 
+ */
+  __pyx_t_4 = __pyx_v_self->_delegate;
+  __pyx_v_src = __pyx_t_4;
+
+  /* "pysam/calignmentfile.pyx":3201
+ *         cdef char * _value_type
+ * 
+ *         if len(tag) != 2:             # <<<<<<<<<<<<<<
+ *             raise ValueError('Invalid tag: %s' % tag)
+ * 
+ */
+  __pyx_t_5 = PyObject_Length(__pyx_v_tag); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = ((__pyx_t_5 != 2) != 0);
+  if (__pyx_t_6) {
+
+    /* "pysam/calignmentfile.pyx":3202
+ * 
+ *         if len(tag) != 2:
+ *             raise ValueError('Invalid tag: %s' % tag)             # <<<<<<<<<<<<<<
+ * 
+ *         type_code = _getTypeCode(value, value_type)
+ */
+    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_Invalid_tag_s, __pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
+    __Pyx_GIVEREF(__pyx_t_1);
+    __pyx_t_1 = 0;
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
+
+  /* "pysam/calignmentfile.pyx":3204
+ *             raise ValueError('Invalid tag: %s' % tag)
+ * 
+ *         type_code = _getTypeCode(value, value_type)             # <<<<<<<<<<<<<<
+ *         if type_code == 0:
+ *             raise ValueError("can't guess type or invalid type code specified")
+ */
+  __pyx_t_8.__pyx_n = 1;
+  __pyx_t_8.value_type = __pyx_v_value_type;
+  __pyx_t_7 = __pyx_f_5pysam_14calignmentfile__getTypeCode(__pyx_v_value, &__pyx_t_8); 
+  __pyx_v_type_code = __pyx_t_7;
+
+  /* "pysam/calignmentfile.pyx":3205
+ * 
+ *         type_code = _getTypeCode(value, value_type)
+ *         if type_code == 0:             # <<<<<<<<<<<<<<
+ *             raise ValueError("can't guess type or invalid type code specified")
+ * 
+ */
+  __pyx_t_6 = ((__pyx_v_type_code == 0) != 0);
+  if (__pyx_t_6) {
+
+    /* "pysam/calignmentfile.pyx":3206
+ *         type_code = _getTypeCode(value, value_type)
+ *         if type_code == 0:
+ *             raise ValueError("can't guess type or invalid type code specified")             # <<<<<<<<<<<<<<
+ * 
+ *         # Not Endian-safe, but then again neither is samtools!
+ */
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__75, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
+
+  /* "pysam/calignmentfile.pyx":3221
+ *             value_ptr    = <uint8_t*>&double_value
+ *             value_size   = sizeof(double)
+ *         elif type_code == 'f':             # <<<<<<<<<<<<<<
+ *             float_value  = value
+ *             value_ptr    = <uint8_t*>&float_value
+ */
+  switch (__pyx_v_type_code) {
+
+    /* "pysam/calignmentfile.pyx":3209
+ * 
+ *         # Not Endian-safe, but then again neither is samtools!
+ *         if type_code == 'Z':             # <<<<<<<<<<<<<<
+ *             value = _forceBytes( value )
+ *             value_ptr    = <uint8_t*><char*>value
+ */
+    case 'Z':
+
+    /* "pysam/calignmentfile.pyx":3210
+ *         # Not Endian-safe, but then again neither is samtools!
+ *         if type_code == 'Z':
+ *             value = _forceBytes( value )             # <<<<<<<<<<<<<<
+ *             value_ptr    = <uint8_t*><char*>value
+ *             value_size   = len(value)+1
+ */
+    __pyx_t_1 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_value); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_1);
+    __pyx_t_1 = 0;
+
+    /* "pysam/calignmentfile.pyx":3211
+ *         if type_code == 'Z':
+ *             value = _forceBytes( value )
+ *             value_ptr    = <uint8_t*><char*>value             # <<<<<<<<<<<<<<
+ *             value_size   = len(value)+1
+ *         elif type_code == 'i':
+ */
+    __pyx_t_9 = __Pyx_PyObject_AsString(__pyx_v_value); if (unlikely((!__pyx_t_9) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_value_ptr = ((uint8_t *)((char *)__pyx_t_9));
+
+    /* "pysam/calignmentfile.pyx":3212
+ *             value = _forceBytes( value )
+ *             value_ptr    = <uint8_t*><char*>value
+ *             value_size   = len(value)+1             # <<<<<<<<<<<<<<
+ *         elif type_code == 'i':
+ *             int_value    = value
+ */
+    __pyx_t_5 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_value_size = (__pyx_t_5 + 1);
+    break;
+
+    /* "pysam/calignmentfile.pyx":3213
+ *             value_ptr    = <uint8_t*><char*>value
+ *             value_size   = len(value)+1
+ *         elif type_code == 'i':             # <<<<<<<<<<<<<<
+ *             int_value    = value
+ *             value_ptr    = <uint8_t*>&int_value
+ */
+    case 'i':
+
+    /* "pysam/calignmentfile.pyx":3214
+ *             value_size   = len(value)+1
+ *         elif type_code == 'i':
+ *             int_value    = value             # <<<<<<<<<<<<<<
+ *             value_ptr    = <uint8_t*>&int_value
+ *             value_size   = sizeof(int32_t)
+ */
+    __pyx_t_10 = __Pyx_PyInt_As_int32_t(__pyx_v_value); if (unlikely((__pyx_t_10 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_int_value = __pyx_t_10;
+
+    /* "pysam/calignmentfile.pyx":3215
+ *         elif type_code == 'i':
+ *             int_value    = value
+ *             value_ptr    = <uint8_t*>&int_value             # <<<<<<<<<<<<<<
+ *             value_size   = sizeof(int32_t)
+ *         elif type_code == 'd':
+ */
+    __pyx_v_value_ptr = ((uint8_t *)(&__pyx_v_int_value));
+
+    /* "pysam/calignmentfile.pyx":3216
+ *             int_value    = value
+ *             value_ptr    = <uint8_t*>&int_value
+ *             value_size   = sizeof(int32_t)             # <<<<<<<<<<<<<<
+ *         elif type_code == 'd':
+ *             double_value = value
+ */
+    __pyx_v_value_size = (sizeof(int32_t));
+    break;
+
+    /* "pysam/calignmentfile.pyx":3217
+ *             value_ptr    = <uint8_t*>&int_value
+ *             value_size   = sizeof(int32_t)
+ *         elif type_code == 'd':             # <<<<<<<<<<<<<<
+ *             double_value = value
+ *             value_ptr    = <uint8_t*>&double_value
+ */
+    case 'd':
+
+    /* "pysam/calignmentfile.pyx":3218
+ *             value_size   = sizeof(int32_t)
+ *         elif type_code == 'd':
+ *             double_value = value             # <<<<<<<<<<<<<<
+ *             value_ptr    = <uint8_t*>&double_value
+ *             value_size   = sizeof(double)
+ */
+    __pyx_t_11 = __pyx_PyFloat_AsDouble(__pyx_v_value); if (unlikely((__pyx_t_11 == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3218; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_double_value = __pyx_t_11;
+
+    /* "pysam/calignmentfile.pyx":3219
+ *         elif type_code == 'd':
+ *             double_value = value
+ *             value_ptr    = <uint8_t*>&double_value             # <<<<<<<<<<<<<<
+ *             value_size   = sizeof(double)
+ *         elif type_code == 'f':
+ */
+    __pyx_v_value_ptr = ((uint8_t *)(&__pyx_v_double_value));
+
+    /* "pysam/calignmentfile.pyx":3220
+ *             double_value = value
+ *             value_ptr    = <uint8_t*>&double_value
+ *             value_size   = sizeof(double)             # <<<<<<<<<<<<<<
+ *         elif type_code == 'f':
+ *             float_value  = value
+ */
+    __pyx_v_value_size = (sizeof(double));
+    break;
+
+    /* "pysam/calignmentfile.pyx":3221
+ *             value_ptr    = <uint8_t*>&double_value
+ *             value_size   = sizeof(double)
+ *         elif type_code == 'f':             # <<<<<<<<<<<<<<
+ *             float_value  = value
+ *             value_ptr    = <uint8_t*>&float_value
+ */
+    case 'f':
+
+    /* "pysam/calignmentfile.pyx":3222
+ *             value_size   = sizeof(double)
+ *         elif type_code == 'f':
+ *             float_value  = value             # <<<<<<<<<<<<<<
+ *             value_ptr    = <uint8_t*>&float_value
+ *             value_size   = sizeof(float)
+ */
+    __pyx_t_12 = __pyx_PyFloat_AsFloat(__pyx_v_value); if (unlikely((__pyx_t_12 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_float_value = __pyx_t_12;
+
+    /* "pysam/calignmentfile.pyx":3223
+ *         elif type_code == 'f':
+ *             float_value  = value
+ *             value_ptr    = <uint8_t*>&float_value             # <<<<<<<<<<<<<<
+ *             value_size   = sizeof(float)
+ *         else:
+ */
+    __pyx_v_value_ptr = ((uint8_t *)(&__pyx_v_float_value));
+
+    /* "pysam/calignmentfile.pyx":3224
+ *             float_value  = value
+ *             value_ptr    = <uint8_t*>&float_value
+ *             value_size   = sizeof(float)             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise ValueError('Unsupported value_type in set_option')
+ */
+    __pyx_v_value_size = (sizeof(float));
+    break;
+    default:
+
+    /* "pysam/calignmentfile.pyx":3226
+ *             value_size   = sizeof(float)
+ *         else:
+ *             raise ValueError('Unsupported value_type in set_option')             # <<<<<<<<<<<<<<
+ * 
+ *         tag = _forceBytes( tag )
+ */
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__76, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    break;
+  }
+
+  /* "pysam/calignmentfile.pyx":3228
+ *             raise ValueError('Unsupported value_type in set_option')
+ * 
+ *         tag = _forceBytes( tag )             # <<<<<<<<<<<<<<
+ *         if replace:
+ *             existing_ptr = bam_aux_get(src, tag)
+ */
+  __pyx_t_1 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3228; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF_SET(__pyx_v_tag, __pyx_t_1);
+  __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":3229
+ * 
+ *         tag = _forceBytes( tag )
+ *         if replace:             # <<<<<<<<<<<<<<
+ *             existing_ptr = bam_aux_get(src, tag)
+ *             if existing_ptr:
+ */
+  __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_v_replace); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__pyx_t_6) {
+
+    /* "pysam/calignmentfile.pyx":3230
+ *         tag = _forceBytes( tag )
+ *         if replace:
+ *             existing_ptr = bam_aux_get(src, tag)             # <<<<<<<<<<<<<<
+ *             if existing_ptr:
+ *                 bam_aux_del(src, existing_ptr)
+ */
+    __pyx_t_13 = __Pyx_PyObject_AsString(__pyx_v_tag); if (unlikely((!__pyx_t_13) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3230; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_existing_ptr = bam_aux_get(__pyx_v_src, __pyx_t_13);
+
+    /* "pysam/calignmentfile.pyx":3231
+ *         if replace:
+ *             existing_ptr = bam_aux_get(src, tag)
+ *             if existing_ptr:             # <<<<<<<<<<<<<<
+ *                 bam_aux_del(src, existing_ptr)
+ * 
+ */
+    __pyx_t_6 = (__pyx_v_existing_ptr != 0);
+    if (__pyx_t_6) {
+
+      /* "pysam/calignmentfile.pyx":3232
+ *             existing_ptr = bam_aux_get(src, tag)
+ *             if existing_ptr:
+ *                 bam_aux_del(src, existing_ptr)             # <<<<<<<<<<<<<<
+ * 
+ *         bam_aux_append(src,
+ */
+      bam_aux_del(__pyx_v_src, __pyx_v_existing_ptr);
+      goto __pyx_L6;
+    }
+    __pyx_L6:;
+    goto __pyx_L5;
+  }
+  __pyx_L5:;
+
+  /* "pysam/calignmentfile.pyx":3235
+ * 
+ *         bam_aux_append(src,
+ *                        tag,             # <<<<<<<<<<<<<<
+ *                        type_code,
+ *                        value_size,
+ */
+  __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_tag); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3234
+ *                 bam_aux_del(src, existing_ptr)
+ * 
+ *         bam_aux_append(src,             # <<<<<<<<<<<<<<
+ *                        tag,
+ *                        type_code,
+ */
+  bam_aux_append(__pyx_v_src, __pyx_t_14, __pyx_v_type_code, __pyx_v_value_size, __pyx_v_value_ptr);
+
+  /* "pysam/calignmentfile.pyx":3180
+ * 
+ * 
+ *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
+ *                  value_type = None,
+ *                  replace = True):
+ */
+
+  /* function exit code */
+  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.setTag", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = 0;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_tag);
+  __Pyx_XDECREF(__pyx_v_value);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21setTag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_20setTag[] = "AlignedSegment.setTag(self, tag, value, value_type=None, replace=True)\n\n        Set optional field of alignment *tag* to *value*.  *value_type* may be specified,\n        but if not the type will be inferred based on the Python type of *value*\n\n        An existing value of the same tag will be overwritten unless\n        *replace* is set to False.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21setTag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  PyObject *__pyx_v_tag = 0;
+  PyObject *__pyx_v_value = 0;
+  PyObject *__pyx_v_value_type = 0;
+  PyObject *__pyx_v_replace = 0;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("setTag (wrapper)", 0);
+  {
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_tag,&__pyx_n_s_value,&__pyx_n_s_value_type,&__pyx_n_s_replace,0};
+    PyObject* values[4] = {0,0,0,0};
+
+    /* "pysam/calignmentfile.pyx":3181
+ * 
+ *     cpdef setTag(self, tag, value,
+ *                  value_type = None,             # <<<<<<<<<<<<<<
+ *                  replace = True):
+ *         '''
+ */
+    values[2] = ((PyObject *)Py_None);
+
+    /* "pysam/calignmentfile.pyx":3182
+ *     cpdef setTag(self, tag, value,
+ *                  value_type = None,
+ *                  replace = True):             # <<<<<<<<<<<<<<
+ *         '''
+ *         Set optional field of alignment *tag* to *value*.  *value_type* may be specified,
+ */
+    values[3] = ((PyObject *)Py_True);
+    if (unlikely(__pyx_kwds)) {
+      Py_ssize_t kw_args;
+      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
+      switch (pos_args) {
+        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
+        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
+        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+      kw_args = PyDict_Size(__pyx_kwds);
+      switch (pos_args) {
+        case  0:
+        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_tag)) != 0)) kw_args--;
+        else goto __pyx_L5_argtuple_error;
+        case  1:
+        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value)) != 0)) kw_args--;
+        else {
+          __Pyx_RaiseArgtupleInvalid("setTag", 0, 2, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3180; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        }
+        case  2:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value_type);
+          if (value) { values[2] = value; kw_args--; }
+        }
+        case  3:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_replace);
+          if (value) { values[3] = value; kw_args--; }
+        }
+      }
+      if (unlikely(kw_args > 0)) {
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setTag") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3180; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+    } else {
+      switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
+        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
+        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+    }
+    __pyx_v_tag = values[0];
+    __pyx_v_value = values[1];
+    __pyx_v_value_type = values[2];
+    __pyx_v_replace = values[3];
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("setTag", 0, 2, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3180; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.setTag", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_RefNannyFinishContext();
+  return NULL;
+  __pyx_L4_argument_unpacking_done:;
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_20setTag(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), __pyx_v_tag, __pyx_v_value, __pyx_v_value_type, __pyx_v_replace);
+
+  /* "pysam/calignmentfile.pyx":3180
+ * 
+ * 
+ *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
+ *                  value_type = None,
+ *                  replace = True):
+ */
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_20setTag(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_tag, PyObject *__pyx_v_value, PyObject *__pyx_v_value_type, PyObject *__pyx_v_replace) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  struct __pyx_opt_args_5pysam_14calignmentfile_14AlignedSegment_setTag __pyx_t_2;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("setTag", 0);
+  __Pyx_TraceCall("setTag", __pyx_f[0], 3180);
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_2.__pyx_n = 2;
+  __pyx_t_2.value_type = __pyx_v_value_type;
+  __pyx_t_2.replace = __pyx_v_replace;
+  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self->__pyx_vtab)->setTag(__pyx_v_self, __pyx_v_tag, __pyx_v_value, 1, &__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.setTag", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3246
+ *     #######################################################################
+ * 
+ *     def opt(self, tag):             # <<<<<<<<<<<<<<
+ *         """retrieves optional data given a two-letter *tag*"""
+ *         #see bam_aux.c: bam_aux_get() and bam_aux2i() etc
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_23opt(PyObject *__pyx_v_self, PyObject *__pyx_v_tag); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_22opt[] = "AlignedSegment.opt(self, tag)\nretrieves optional data given a two-letter *tag*";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_23opt(PyObject *__pyx_v_self, PyObject *__pyx_v_tag) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("opt (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_22opt(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_tag));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_22opt(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_tag) {
+  uint8_t *__pyx_v_v;
+  CYTHON_UNUSED int __pyx_v_nvalues;
+  PyObject *__pyx_v_btag = NULL;
+  PyObject *__pyx_v_auxtype = NULL;
+  CYTHON_UNUSED PyObject *__pyx_v_bytesize = NULL;
+  PyObject *__pyx_v_values = NULL;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  char const *__pyx_t_2;
+  int __pyx_t_3;
+  PyObject *__pyx_t_4 = NULL;
+  int __pyx_t_5;
+  int __pyx_t_6;
+  int __pyx_t_7;
+  int __pyx_t_8;
+  PyObject *__pyx_t_9 = NULL;
+  PyObject *__pyx_t_10 = NULL;
+  PyObject *__pyx_t_11 = NULL;
+  PyObject *(*__pyx_t_12)(PyObject *);
+  int __pyx_t_13;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("opt", 0);
+  __Pyx_TraceCall("opt", __pyx_f[0], 3246);
+
+  /* "pysam/calignmentfile.pyx":3251
+ *         cdef uint8_t * v
+ *         cdef int nvalues
+ *         btag = _forceBytes(tag)             # <<<<<<<<<<<<<<
+ *         v = bam_aux_get(self._delegate, btag)
+ *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
+ */
+  __pyx_t_1 = __pyx_f_5pysam_14calignmentfile__forceBytes(__pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_v_btag = ((PyObject*)__pyx_t_1);
+  __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":3252
+ *         cdef int nvalues
+ *         btag = _forceBytes(tag)
+ *         v = bam_aux_get(self._delegate, btag)             # <<<<<<<<<<<<<<
+ *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
+ *         auxtype = chr(v[0])
+ */
+  __pyx_t_2 = __Pyx_PyObject_AsString(__pyx_v_btag); if (unlikely((!__pyx_t_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_v = bam_aux_get(__pyx_v_self->_delegate, __pyx_t_2);
+
+  /* "pysam/calignmentfile.pyx":3253
+ *         btag = _forceBytes(tag)
+ *         v = bam_aux_get(self._delegate, btag)
+ *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )             # <<<<<<<<<<<<<<
+ *         auxtype = chr(v[0])
+ *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
+ */
+  __pyx_t_3 = ((__pyx_v_v == NULL) != 0);
+  if (__pyx_t_3) {
+    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_tag_s_not_present, __pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
+    __Pyx_GIVEREF(__pyx_t_1);
+    __pyx_t_1 = 0;
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
+
+  /* "pysam/calignmentfile.pyx":3254
+ *         v = bam_aux_get(self._delegate, btag)
+ *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
+ *         auxtype = chr(v[0])             # <<<<<<<<<<<<<<
+ *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
+ *             return <int>bam_aux2i(v)
+ */
+  __pyx_t_1 = __Pyx_PyInt_From_uint8_t((__pyx_v_v[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
+  __Pyx_GIVEREF(__pyx_t_1);
+  __pyx_t_1 = 0;
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_chr, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  __pyx_v_auxtype = __pyx_t_1;
+  __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":3255
+ *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
+ *         auxtype = chr(v[0])
+ *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':             # <<<<<<<<<<<<<<
+ *             return <int>bam_aux2i(v)
+ *         elif auxtype == 'i' or auxtype == 'I':
+ */
+  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_c, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3255; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!__pyx_t_3) {
+    __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_C, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3255; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!__pyx_t_5) {
+      __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_s_2, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3255; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (!__pyx_t_6) {
+        __pyx_t_7 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_S, Py_EQ)); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3255; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __pyx_t_7;
+      } else {
+        __pyx_t_8 = __pyx_t_6;
+      }
+      __pyx_t_6 = __pyx_t_8;
+    } else {
+      __pyx_t_6 = __pyx_t_5;
+    }
+    __pyx_t_5 = __pyx_t_6;
+  } else {
+    __pyx_t_5 = __pyx_t_3;
+  }
+  if (__pyx_t_5) {
+
+    /* "pysam/calignmentfile.pyx":3256
+ *         auxtype = chr(v[0])
+ *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
+ *             return <int>bam_aux2i(v)             # <<<<<<<<<<<<<<
+ *         elif auxtype == 'i' or auxtype == 'I':
+ *             return <int32_t>bam_aux2i(v)
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_1 = __Pyx_PyInt_From_int(((int)bam_aux2i(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_r = __pyx_t_1;
+    __pyx_t_1 = 0;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3257
+ *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
+ *             return <int>bam_aux2i(v)
+ *         elif auxtype == 'i' or auxtype == 'I':             # <<<<<<<<<<<<<<
+ *             return <int32_t>bam_aux2i(v)
+ *         elif auxtype == 'f' or auxtype == 'F':
+ */
+  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_i, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!__pyx_t_5) {
+    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_I, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __pyx_t_3;
+  } else {
+    __pyx_t_6 = __pyx_t_5;
+  }
+  if (__pyx_t_6) {
+
+    /* "pysam/calignmentfile.pyx":3258
+ *             return <int>bam_aux2i(v)
+ *         elif auxtype == 'i' or auxtype == 'I':
+ *             return <int32_t>bam_aux2i(v)             # <<<<<<<<<<<<<<
+ *         elif auxtype == 'f' or auxtype == 'F':
+ *             return <float>bam_aux2f(v)
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_1 = __Pyx_PyInt_From_int32_t(((int32_t)bam_aux2i(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_r = __pyx_t_1;
+    __pyx_t_1 = 0;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3259
+ *         elif auxtype == 'i' or auxtype == 'I':
+ *             return <int32_t>bam_aux2i(v)
+ *         elif auxtype == 'f' or auxtype == 'F':             # <<<<<<<<<<<<<<
+ *             return <float>bam_aux2f(v)
+ *         elif auxtype == 'd' or auxtype == 'D':
+ */
+  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_f, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!__pyx_t_6) {
+    __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_F, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __pyx_t_5;
+  } else {
+    __pyx_t_3 = __pyx_t_6;
+  }
+  if (__pyx_t_3) {
+
+    /* "pysam/calignmentfile.pyx":3260
+ *             return <int32_t>bam_aux2i(v)
+ *         elif auxtype == 'f' or auxtype == 'F':
+ *             return <float>bam_aux2f(v)             # <<<<<<<<<<<<<<
+ *         elif auxtype == 'd' or auxtype == 'D':
+ *             return <double>bam_aux2f(v)
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_1 = PyFloat_FromDouble(((float)bam_aux2f(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3260; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_r = __pyx_t_1;
+    __pyx_t_1 = 0;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3261
+ *         elif auxtype == 'f' or auxtype == 'F':
+ *             return <float>bam_aux2f(v)
+ *         elif auxtype == 'd' or auxtype == 'D':             # <<<<<<<<<<<<<<
+ *             return <double>bam_aux2f(v)
+ *         elif auxtype == 'A':
+ */
+  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_d, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3261; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!__pyx_t_3) {
+    __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_D, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3261; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __pyx_t_6;
+  } else {
+    __pyx_t_5 = __pyx_t_3;
+  }
+  if (__pyx_t_5) {
+
+    /* "pysam/calignmentfile.pyx":3262
+ *             return <float>bam_aux2f(v)
+ *         elif auxtype == 'd' or auxtype == 'D':
+ *             return <double>bam_aux2f(v)             # <<<<<<<<<<<<<<
+ *         elif auxtype == 'A':
+ *             # there might a more efficient way
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_1 = PyFloat_FromDouble(((double)bam_aux2f(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3262; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_r = __pyx_t_1;
+    __pyx_t_1 = 0;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3263
+ *         elif auxtype == 'd' or auxtype == 'D':
+ *             return <double>bam_aux2f(v)
+ *         elif auxtype == 'A':             # <<<<<<<<<<<<<<
+ *             # there might a more efficient way
+ *             # to convert a char into a string
+ */
+  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_A, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3263; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__pyx_t_5) {
+
+    /* "pysam/calignmentfile.pyx":3266
+ *             # there might a more efficient way
+ *             # to convert a char into a string
+ *             return '%c' % <char>bam_aux2A(v)             # <<<<<<<<<<<<<<
+ *         elif auxtype == 'Z':
+ *             return _charptr_to_str(<char*>bam_aux2Z(v))
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_1 = __Pyx_PyInt_From_char(((char)bam_aux2A(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_c_2, __pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    __pyx_r = __pyx_t_4;
+    __pyx_t_4 = 0;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3267
+ *             # to convert a char into a string
+ *             return '%c' % <char>bam_aux2A(v)
+ *         elif auxtype == 'Z':             # <<<<<<<<<<<<<<
+ *             return _charptr_to_str(<char*>bam_aux2Z(v))
+ *         elif auxtype == 'B':
+ */
+  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_Z, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__pyx_t_5) {
+
+    /* "pysam/calignmentfile.pyx":3268
+ *             return '%c' % <char>bam_aux2A(v)
+ *         elif auxtype == 'Z':
+ *             return _charptr_to_str(<char*>bam_aux2Z(v))             # <<<<<<<<<<<<<<
+ *         elif auxtype == 'B':
+ *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __pyx_t_4 = __pyx_f_5pysam_14calignmentfile__charptr_to_str(((char *)bam_aux2Z(__pyx_v_v))); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3268; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __pyx_r = __pyx_t_4;
+    __pyx_t_4 = 0;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/calignmentfile.pyx":3269
+ *         elif auxtype == 'Z':
+ *             return _charptr_to_str(<char*>bam_aux2Z(v))
+ *         elif auxtype == 'B':             # <<<<<<<<<<<<<<
+ *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
+ *             return values
+ */
+  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_B, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3269; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__pyx_t_5) {
+
+    /* "pysam/calignmentfile.pyx":3270
+ *             return _charptr_to_str(<char*>bam_aux2Z(v))
+ *         elif auxtype == 'B':
+ *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )             # <<<<<<<<<<<<<<
+ *             return values
+ *         else:
+ */
+    __pyx_t_4 = __pyx_f_5pysam_14calignmentfile_convertBinaryTagToList((__pyx_v_v + 1)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    if ((likely(PyTuple_CheckExact(__pyx_t_4))) || (PyList_CheckExact(__pyx_t_4))) {
+      PyObject* sequence = __pyx_t_4;
+      #if CYTHON_COMPILING_IN_CPYTHON
+      Py_ssize_t size = Py_SIZE(sequence);
+      #else
+      Py_ssize_t size = PySequence_Size(sequence);
+      #endif
+      if (unlikely(size != 3)) {
+        if (size > 3) __Pyx_RaiseTooManyValuesError(3);
+        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      }
+      #if CYTHON_COMPILING_IN_CPYTHON
+      if (likely(PyTuple_CheckExact(sequence))) {
+        __pyx_t_1 = PyTuple_GET_ITEM(sequence, 0); 
+        __pyx_t_9 = PyTuple_GET_ITEM(sequence, 1); 
+        __pyx_t_10 = PyTuple_GET_ITEM(sequence, 2); 
+      } else {
+        __pyx_t_1 = PyList_GET_ITEM(sequence, 0); 
+        __pyx_t_9 = PyList_GET_ITEM(sequence, 1); 
+        __pyx_t_10 = PyList_GET_ITEM(sequence, 2); 
+      }
+      __Pyx_INCREF(__pyx_t_1);
+      __Pyx_INCREF(__pyx_t_9);
+      __Pyx_INCREF(__pyx_t_10);
+      #else
+      __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_1);
+      __pyx_t_9 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_9);
+      __pyx_t_10 = PySequence_ITEM(sequence, 2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_10);
+      #endif
+      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    } else {
+      Py_ssize_t index = -1;
+      __pyx_t_11 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_11);
+      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+      __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext;
+      index = 0; __pyx_t_1 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_1)) goto __pyx_L5_unpacking_failed;
+      __Pyx_GOTREF(__pyx_t_1);
+      index = 1; __pyx_t_9 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_9)) goto __pyx_L5_unpacking_failed;
+      __Pyx_GOTREF(__pyx_t_9);
+      index = 2; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L5_unpacking_failed;
+      __Pyx_GOTREF(__pyx_t_10);
+      if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_12 = NULL;
+      __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
+      goto __pyx_L6_unpacking_done;
+      __pyx_L5_unpacking_failed:;
+      __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
+      __pyx_t_12 = NULL;
+      if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_L6_unpacking_done:;
+    }
+    __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+    __pyx_v_bytesize = __pyx_t_1;
+    __pyx_t_1 = 0;
+    __pyx_v_nvalues = __pyx_t_13;
+    __pyx_v_values = __pyx_t_10;
+    __pyx_t_10 = 0;
+
+    /* "pysam/calignmentfile.pyx":3271
+ *         elif auxtype == 'B':
+ *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
+ *             return values             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise ValueError("unknown auxilliary type '%s'" % auxtype)
+ */
+    __Pyx_XDECREF(__pyx_r);
+    __Pyx_INCREF(__pyx_v_values);
+    __pyx_r = __pyx_v_values;
+    goto __pyx_L0;
+  }
+  /*else*/ {
+
+    /* "pysam/calignmentfile.pyx":3273
+ *             return values
+ *         else:
+ *             raise ValueError("unknown auxilliary type '%s'" % auxtype)             # <<<<<<<<<<<<<<
+ * 
+ * 
+ */
+    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_unknown_auxilliary_type_s, __pyx_v_auxtype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3273; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3273; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_10);
+    PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_4);
+    __Pyx_GIVEREF(__pyx_t_4);
+    __pyx_t_4 = 0;
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_10, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3273; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
+    __Pyx_Raise(__pyx_t_4, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3273; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
+
+  /* "pysam/calignmentfile.pyx":3246
+ *     #######################################################################
+ * 
+ *     def opt(self, tag):             # <<<<<<<<<<<<<<
+ *         """retrieves optional data given a two-letter *tag*"""
+ *         #see bam_aux.c: bam_aux_get() and bam_aux2i() etc
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_9);
+  __Pyx_XDECREF(__pyx_t_10);
+  __Pyx_XDECREF(__pyx_t_11);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.opt", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_btag);
+  __Pyx_XDECREF(__pyx_v_auxtype);
+  __Pyx_XDECREF(__pyx_v_bytesize);
+  __Pyx_XDECREF(__pyx_v_values);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3290
+ *     ########################################################
+ *     property qname:
+ *         def __get__(self): return self.query_name             # <<<<<<<<<<<<<<
+ *         def __set__(self, v): self.query_name = v
+ *     property tid:
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qname_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qname_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qname___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qname___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3290);
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_name); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3290; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qname.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3291
+ *     property qname:
+ *         def __get__(self): return self.query_name
+ *         def __set__(self, v): self.query_name = v             # <<<<<<<<<<<<<<
+ *     property tid:
+ *         def __get__(self): return self.reference_id
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qname_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qname_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3291);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_name, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qname.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3293
+ *         def __set__(self, v): self.query_name = v
+ *     property tid:
+ *         def __get__(self): return self.reference_id             # <<<<<<<<<<<<<<
+ *         def __set__(self, v): self.reference_id = v
+ *     property pos:
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3tid_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3tid_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3tid___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3tid___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3293);
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3293; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.tid.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3294
+ *     property tid:
+ *         def __get__(self): return self.reference_id
+ *         def __set__(self, v): self.reference_id = v             # <<<<<<<<<<<<<<
+ *     property pos:
+ *         def __get__(self): return self.reference_start
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3tid_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3tid_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3tid_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3tid_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2944);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3294);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_id, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3294; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":2945
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)             # <<<<<<<<<<<<<<
- *     property is_reverse:
- *         """true if read is mapped to reverse strand"""
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.tid.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3296
+ *         def __set__(self, v): self.reference_id = v
+ *     property pos:
+ *         def __get__(self): return self.reference_start             # <<<<<<<<<<<<<<
+ *         def __set__(self, v): self.reference_start = v
+ *     property mapq:
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FMUNMAP);
 
-  /* "pysam/csamfile.pyx":2944
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3pos_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3pos_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3pos___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3pos___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3296);
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3297
+ *     property pos:
+ *         def __get__(self): return self.reference_start
+ *         def __set__(self, v): self.reference_start = v             # <<<<<<<<<<<<<<
+ *     property mapq:
+ *         def __get__(self): return self.mapping_quality
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3pos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3pos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3pos_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3pos_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3297);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_start, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.pos.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3299
+ *         def __set__(self, v): self.reference_start = v
+ *     property mapq:
+ *         def __get__(self): return self.mapping_quality             # <<<<<<<<<<<<<<
+ *         def __set__(self, v): self.mapping_quality = v
+ *     property rnext:
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mapq_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mapq_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mapq___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mapq___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3299);
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mapping_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3299; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mapq.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3300
+ *     property mapq:
+ *         def __get__(self): return self.mapping_quality
+ *         def __set__(self, v): self.mapping_quality = v             # <<<<<<<<<<<<<<
+ *     property rnext:
+ *         def __get__(self): return self.next_reference_id
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mapq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mapq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mapq_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mapq_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3300);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mapping_quality, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mapq.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3302
+ *         def __set__(self, v): self.mapping_quality = v
+ *     property rnext:
+ *         def __get__(self): return self.next_reference_id             # <<<<<<<<<<<<<<
+ *         def __set__(self, v): self.next_reference_id = v
+ *     property pnext:
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rnext_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rnext_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rnext___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rnext___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3302);
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3302; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.rnext.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3303
+ *     property rnext:
+ *         def __get__(self): return self.next_reference_id
+ *         def __set__(self, v): self.next_reference_id = v             # <<<<<<<<<<<<<<
+ *     property pnext:
  *         def __get__(self):
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
- *     property is_reverse:
  */
 
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rnext_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rnext_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3303);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_id, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mate_is_unmapped.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.rnext.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -30164,76 +33477,450 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_2__set__(s
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2948
- *     property is_reverse:
- *         """true if read is mapped to reverse strand"""
+/* "pysam/calignmentfile.pyx":3305
+ *         def __set__(self, v): self.next_reference_id = v
+ *     property pnext:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):
+ *             return self.next_reference_start
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5pnext_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5pnext_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5pnext___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5pnext___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2948);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3305);
 
-  /* "pysam/csamfile.pyx":2949
- *         """true if read is mapped to reverse strand"""
+  /* "pysam/calignmentfile.pyx":3306
+ *     property pnext:
  *         def __get__(self):
- *             return (self.flag & BAM_FREVERSE) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)
+ *             return self.next_reference_start             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.next_reference_start = v
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3306; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3305
+ *         def __set__(self, v): self.next_reference_id = v
+ *     property pnext:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.next_reference_start
+ *         def __set__(self, v):
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.pnext.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3307
+ *         def __get__(self):
+ *             return self.next_reference_start
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.next_reference_start = v
+ *     property cigar:
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5pnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5pnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5pnext_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5pnext_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3307);
+
+  /* "pysam/calignmentfile.pyx":3308
+ *             return self.next_reference_start
+ *         def __set__(self, v):
+ *             self.next_reference_start = v             # <<<<<<<<<<<<<<
+ *     property cigar:
+ *         def __get__(self):
+ */
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_start, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3308; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3307
+ *         def __get__(self):
+ *             return self.next_reference_start
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.next_reference_start = v
+ *     property cigar:
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.pnext.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3310
+ *             self.next_reference_start = v
+ *     property cigar:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             r = self.cigartuples
+ *             if r is None:
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5cigar_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5cigar_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5cigar___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5cigar___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_v_r = NULL;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_t_2;
+  int __pyx_t_3;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3310);
+
+  /* "pysam/calignmentfile.pyx":3311
+ *     property cigar:
+ *         def __get__(self):
+ *             r = self.cigartuples             # <<<<<<<<<<<<<<
+ *             if r is None:
+ *                 r = []
+ */
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigartuples); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3311; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_v_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":3312
+ *         def __get__(self):
+ *             r = self.cigartuples
+ *             if r is None:             # <<<<<<<<<<<<<<
+ *                 r = []
+ *             return r
+ */
+  __pyx_t_2 = (__pyx_v_r == Py_None);
+  __pyx_t_3 = (__pyx_t_2 != 0);
+  if (__pyx_t_3) {
+
+    /* "pysam/calignmentfile.pyx":3313
+ *             r = self.cigartuples
+ *             if r is None:
+ *                 r = []             # <<<<<<<<<<<<<<
+ *             return r
+ *         def __set__(self, v): self.cigartuples = v
+ */
+    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3313; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF_SET(__pyx_v_r, __pyx_t_1);
+    __pyx_t_1 = 0;
+    goto __pyx_L3;
+  }
+  __pyx_L3:;
+
+  /* "pysam/calignmentfile.pyx":3314
+ *             if r is None:
+ *                 r = []
+ *             return r             # <<<<<<<<<<<<<<
+ *         def __set__(self, v): self.cigartuples = v
+ *     property tlen:
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __Pyx_INCREF(__pyx_v_r);
+  __pyx_r = __pyx_v_r;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3310
+ *             self.next_reference_start = v
+ *     property cigar:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             r = self.cigartuples
+ *             if r is None:
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.cigar.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_r);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3315
+ *                 r = []
+ *             return r
+ *         def __set__(self, v): self.cigartuples = v             # <<<<<<<<<<<<<<
+ *     property tlen:
+ *         def __get__(self):
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5cigar_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5cigar_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5cigar_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5cigar_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3315);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigartuples, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3315; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.cigar.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3317
+ *         def __set__(self, v): self.cigartuples = v
+ *     property tlen:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.template_length
+ *         def __set__(self, v):
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tlen_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tlen_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tlen___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tlen___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3317);
+
+  /* "pysam/calignmentfile.pyx":3318
+ *     property tlen:
+ *         def __get__(self):
+ *             return self.template_length             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.template_length = v
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_template_length); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3317
+ *         def __set__(self, v): self.cigartuples = v
+ *     property tlen:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.template_length
+ *         def __set__(self, v):
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.tlen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3319
+ *         def __get__(self):
+ *             return self.template_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.template_length = v
+ *     property seq:
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tlen_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4tlen_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3319);
+
+  /* "pysam/calignmentfile.pyx":3320
+ *             return self.template_length
+ *         def __set__(self, v):
+ *             self.template_length = v             # <<<<<<<<<<<<<<
+ *     property seq:
+ *         def __get__(self): return self.query_sequence
+ */
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_template_length, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3319
+ *         def __get__(self):
+ *             return self.template_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.template_length = v
+ *     property seq:
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.tlen.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3322
+ *             self.template_length = v
+ *     property seq:
+ *         def __get__(self): return self.query_sequence             # <<<<<<<<<<<<<<
+ *         def __set__(self, v): self.query_sequence = v
+ *     property qual:
  */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3seq_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3seq_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3seq___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3seq___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3322);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_sequence); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FREVERSE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2948
- *     property is_reverse:
- *         """true if read is mapped to reverse strand"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):
- */
-
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_reverse.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.seq.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -30242,61 +33929,43 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse___get__(st
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2950
+/* "pysam/calignmentfile.pyx":3323
+ *     property seq:
+ *         def __get__(self): return self.query_sequence
+ *         def __set__(self, v): self.query_sequence = v             # <<<<<<<<<<<<<<
+ *     property qual:
  *         def __get__(self):
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)
- *     property mate_is_reverse:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3seq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3seq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3seq_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_3seq_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2950);
-
-  /* "pysam/csamfile.pyx":2951
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)             # <<<<<<<<<<<<<<
- *     property mate_is_reverse:
- *         """true is read is mapped to reverse strand"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2951; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FREVERSE);
-
-  /* "pysam/csamfile.pyx":2950
- *         def __get__(self):
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)
- *     property mate_is_reverse:
- */
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3323);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_sequence, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_reverse.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.seq.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -30304,28 +33973,28 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse_2__set__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2954
- *     property mate_is_reverse:
- *         """true is read is mapped to reverse strand"""
+/* "pysam/calignmentfile.pyx":3325
+ *         def __set__(self, v): self.query_sequence = v
+ *     property qual:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):
+ *             return toQualityString(self.query_qualities)
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qual_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qual_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qual___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qual___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -30336,36 +34005,39 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse___get
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2954);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3325);
 
-  /* "pysam/csamfile.pyx":2955
- *         """true is read is mapped to reverse strand"""
+  /* "pysam/calignmentfile.pyx":3326
+ *     property qual:
  *         def __get__(self):
- *             return (self.flag & BAM_FMREVERSE) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
+ *             return toQualityString(self.query_qualities)             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.query_qualities = fromQualityString(v)
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2955; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_toQualityString); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FMREVERSE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2955; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_qualities); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2955; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
+  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
+  __Pyx_GIVEREF(__pyx_t_2);
+  __pyx_t_2 = 0;
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2955; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2954
- *     property mate_is_reverse:
- *         """true is read is mapped to reverse strand"""
+  /* "pysam/calignmentfile.pyx":3325
+ *         def __set__(self, v): self.query_sequence = v
+ *     property qual:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):
+ *             return toQualityString(self.query_qualities)
+ *         def __set__(self, v):
  */
 
   /* function exit code */
@@ -30373,7 +34045,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse___get
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mate_is_reverse.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qual.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -30382,61 +34054,77 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse___get
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2956
+/* "pysam/calignmentfile.pyx":3327
  *         def __get__(self):
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
- *     property is_read1:
+ *             return toQualityString(self.query_qualities)
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_qualities = fromQualityString(v)
+ *     property alen:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qual_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qual_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qual_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qual_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2956);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3327);
 
-  /* "pysam/csamfile.pyx":2957
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)             # <<<<<<<<<<<<<<
- *     property is_read1:
- *         """true if this is read1"""
+  /* "pysam/calignmentfile.pyx":3328
+ *             return toQualityString(self.query_qualities)
+ *         def __set__(self, v):
+ *             self.query_qualities = fromQualityString(v)             # <<<<<<<<<<<<<<
+ *     property alen:
+ *         def __get__(self):
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FMREVERSE);
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_fromQualityString); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_INCREF(__pyx_v_v);
+  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_v);
+  __Pyx_GIVEREF(__pyx_v_v);
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_qualities, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/csamfile.pyx":2956
+  /* "pysam/calignmentfile.pyx":3327
  *         def __get__(self):
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
- *     property is_read1:
+ *             return toQualityString(self.query_qualities)
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_qualities = fromQualityString(v)
+ *     property alen:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mate_is_reverse.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qual.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -30444,76 +34132,64 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_2__set__(st
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2960
- *     property is_read1:
- *         """true if this is read1"""
+/* "pysam/calignmentfile.pyx":3330
+ *             self.query_qualities = fromQualityString(v)
+ *     property alen:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):
+ *             return self.reference_length
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4alen_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4alen_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4alen___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4alen___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2960);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3330);
 
-  /* "pysam/csamfile.pyx":2961
- *         """true if this is read1"""
+  /* "pysam/calignmentfile.pyx":3331
+ *     property alen:
  *         def __get__(self):
- *             return (self.flag & BAM_FREAD1) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FREAD1)
+ *             return self.reference_length             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.reference_length = v
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_length); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FREAD1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2960
- *     property is_read1:
- *         """true if this is read1"""
+  /* "pysam/calignmentfile.pyx":3330
+ *             self.query_qualities = fromQualityString(v)
+ *     property alen:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):
+ *             return self.reference_length
+ *         def __set__(self, v):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_read1.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.alen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -30522,61 +34198,59 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1___get__(struc
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2962
+/* "pysam/calignmentfile.pyx":3332
  *         def __get__(self):
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREAD1)
- *     property is_read2:
+ *             return self.reference_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_length = v
+ *     property aend:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4alen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4alen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4alen_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4alen_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2962);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3332);
 
-  /* "pysam/csamfile.pyx":2963
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FREAD1)             # <<<<<<<<<<<<<<
- *     property is_read2:
- *         """true if this is read2"""
+  /* "pysam/calignmentfile.pyx":3333
+ *             return self.reference_length
+ *         def __set__(self, v):
+ *             self.reference_length = v             # <<<<<<<<<<<<<<
+ *     property aend:
+ *         def __get__(self):
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2963; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FREAD1);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_length, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3333; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":2962
+  /* "pysam/calignmentfile.pyx":3332
  *         def __get__(self):
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREAD1)
- *     property is_read2:
+ *             return self.reference_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_length = v
+ *     property aend:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_read1.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.alen.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -30584,76 +34258,64 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1_2__set__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2966
- *     property is_read2:
- *         """true if this is read2"""
+/* "pysam/calignmentfile.pyx":3335
+ *             self.reference_length = v
+ *     property aend:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):
+ *             return self.reference_end
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4aend_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4aend_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4aend___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4aend___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2966);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3335);
 
-  /* "pysam/csamfile.pyx":2967
- *         """true if this is read2"""
+  /* "pysam/calignmentfile.pyx":3336
+ *     property aend:
  *         def __get__(self):
- *             return (self.flag & BAM_FREAD2) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FREAD2)
+ *             return self.reference_end             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.reference_end = v
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_end); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3336; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FREAD2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2966
- *     property is_read2:
- *         """true if this is read2"""
+  /* "pysam/calignmentfile.pyx":3335
+ *             self.reference_length = v
+ *     property aend:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):
+ *             return self.reference_end
+ *         def __set__(self, v):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_read2.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.aend.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -30662,61 +34324,59 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2___get__(struc
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2968
+/* "pysam/calignmentfile.pyx":3337
  *         def __get__(self):
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREAD2)
- *     property is_secondary:
+ *             return self.reference_end
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_end = v
+ *     property rlen:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4aend_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4aend_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4aend_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4aend_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2968);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3337);
 
-  /* "pysam/csamfile.pyx":2969
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FREAD2)             # <<<<<<<<<<<<<<
- *     property is_secondary:
- *         """true if not primary alignment"""
+  /* "pysam/calignmentfile.pyx":3338
+ *             return self.reference_end
+ *         def __set__(self, v):
+ *             self.reference_end = v             # <<<<<<<<<<<<<<
+ *     property rlen:
+ *         def __get__(self):
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2969; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FREAD2);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_end, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":2968
+  /* "pysam/calignmentfile.pyx":3337
  *         def __get__(self):
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREAD2)
- *     property is_secondary:
+ *             return self.reference_end
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_end = v
+ *     property rlen:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_read2.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.aend.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -30724,76 +34384,64 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2_2__set__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2972
- *     property is_secondary:
- *         """true if not primary alignment"""
+/* "pysam/calignmentfile.pyx":3340
+ *             self.reference_end = v
+ *     property rlen:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):
+ *             return self.query_length
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4rlen_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4rlen_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4rlen___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4rlen___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2972);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3340);
 
-  /* "pysam/csamfile.pyx":2973
- *         """true if not primary alignment"""
+  /* "pysam/calignmentfile.pyx":3341
+ *     property rlen:
  *         def __get__(self):
- *             return (self.flag & BAM_FSECONDARY) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
+ *             return self.query_length             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.query_length = v
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_length); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FSECONDARY); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2972
- *     property is_secondary:
- *         """true if not primary alignment"""
+  /* "pysam/calignmentfile.pyx":3340
+ *             self.reference_end = v
+ *     property rlen:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):
+ *             return self.query_length
+ *         def __set__(self, v):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_secondary.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.rlen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -30802,61 +34450,59 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary___get__(
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2974
+/* "pysam/calignmentfile.pyx":3342
  *         def __get__(self):
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
- *     property is_qcfail:
+ *             return self.query_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_length = v
+ *     property query:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4rlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4rlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4rlen_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4rlen_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2974);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3342);
 
-  /* "pysam/csamfile.pyx":2975
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)             # <<<<<<<<<<<<<<
- *     property is_qcfail:
- *         """true if QC failure"""
+  /* "pysam/calignmentfile.pyx":3343
+ *             return self.query_length
+ *         def __set__(self, v):
+ *             self.query_length = v             # <<<<<<<<<<<<<<
+ *     property query:
+ *         def __get__(self):
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2975; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FSECONDARY);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_length, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":2974
+  /* "pysam/calignmentfile.pyx":3342
  *         def __get__(self):
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
- *     property is_qcfail:
+ *             return self.query_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_length = v
+ *     property query:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_secondary.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.rlen.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -30864,76 +34510,64 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary_2__set__(struc
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2978
- *     property is_qcfail:
- *         """true if QC failure"""
+/* "pysam/calignmentfile.pyx":3345
+ *             self.query_length = v
+ *     property query:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):
+ *             return self.query_alignment_sequence
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5query_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5query_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5query___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5query___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2978);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3345);
 
-  /* "pysam/csamfile.pyx":2979
- *         """true if QC failure"""
+  /* "pysam/calignmentfile.pyx":3346
+ *     property query:
  *         def __get__(self):
- *             return (self.flag & BAM_FQCFAIL) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
+ *             return self.query_alignment_sequence             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.query_alignment_sequence = v
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_sequence); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FQCFAIL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2978
- *     property is_qcfail:
- *         """true if QC failure"""
+  /* "pysam/calignmentfile.pyx":3345
+ *             self.query_length = v
+ *     property query:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):
+ *             return self.query_alignment_sequence
+ *         def __set__(self, v):
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_qcfail.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -30942,61 +34576,59 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail___get__(stru
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2980
+/* "pysam/calignmentfile.pyx":3347
  *         def __get__(self):
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
- *     property is_duplicate:
+ *             return self.query_alignment_sequence
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_sequence = v
+ *     property qqual:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5query_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5query_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5query_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5query_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2980);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3347);
 
-  /* "pysam/csamfile.pyx":2981
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)             # <<<<<<<<<<<<<<
- *     property is_duplicate:
- *         """true if optical or PCR duplicate"""
+  /* "pysam/calignmentfile.pyx":3348
+ *             return self.query_alignment_sequence
+ *         def __set__(self, v):
+ *             self.query_alignment_sequence = v             # <<<<<<<<<<<<<<
+ *     property qqual:
+ *         def __get__(self):
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FQCFAIL);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_sequence, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":2980
+  /* "pysam/calignmentfile.pyx":3347
  *         def __get__(self):
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
- *     property is_duplicate:
+ *             return self.query_alignment_sequence
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_sequence = v
+ *     property qqual:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_qcfail.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.query.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -31004,28 +34636,28 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail_2__set__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2984
- *     property is_duplicate:
- *         """true if optical or PCR duplicate"""
+/* "pysam/calignmentfile.pyx":3350
+ *             self.query_alignment_sequence = v
+ *     property qqual:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):
+ *             return toQualityString(self.query_alignment_qualities)
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qqual_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qqual_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qqual___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qqual___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -31036,36 +34668,39 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate___get__(
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2984);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3350);
 
-  /* "pysam/csamfile.pyx":2985
- *         """true if optical or PCR duplicate"""
+  /* "pysam/calignmentfile.pyx":3351
+ *     property qqual:
  *         def __get__(self):
- *             return (self.flag & BAM_FDUP) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FDUP)
+ *             return toQualityString(self.query_alignment_qualities)             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.query_alignment_qualities = fromQualityString(v)
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_toQualityString); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FDUP); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_qualities); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
+  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
+  __Pyx_GIVEREF(__pyx_t_2);
+  __pyx_t_2 = 0;
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":2984
- *     property is_duplicate:
- *         """true if optical or PCR duplicate"""
+  /* "pysam/calignmentfile.pyx":3350
+ *             self.query_alignment_sequence = v
+ *     property qqual:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):
+ *             return toQualityString(self.query_alignment_qualities)
+ *         def __set__(self, v):
  */
 
   /* function exit code */
@@ -31073,7 +34708,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate___get__(
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_duplicate.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qqual.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -31082,61 +34717,77 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate___get__(
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2986
+/* "pysam/calignmentfile.pyx":3352
  *         def __get__(self):
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FDUP)
- * 
+ *             return toQualityString(self.query_alignment_qualities)
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_qualities = fromQualityString(v)
+ *     property qstart:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qqual_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qqual_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qqual_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5qqual_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2986);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3352);
 
-  /* "pysam/csamfile.pyx":2987
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FDUP)             # <<<<<<<<<<<<<<
- * 
- *     #######################################################################
+  /* "pysam/calignmentfile.pyx":3353
+ *             return toQualityString(self.query_alignment_qualities)
+ *         def __set__(self, v):
+ *             self.query_alignment_qualities = fromQualityString(v)             # <<<<<<<<<<<<<<
+ *     property qstart:
+ *         def __get__(self):
  */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FDUP);
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_fromQualityString); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_INCREF(__pyx_v_v);
+  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_v);
+  __Pyx_GIVEREF(__pyx_v_v);
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_qualities, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/csamfile.pyx":2986
+  /* "pysam/calignmentfile.pyx":3352
  *         def __get__(self):
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FDUP)
- * 
+ *             return toQualityString(self.query_alignment_qualities)
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_qualities = fromQualityString(v)
+ *     property qstart:
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_duplicate.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qqual.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -31144,468 +34795,316 @@ static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate_2__set__(struc
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":2995
- *     property positions:
- *         """a list of reference positions that this read aligns to."""
+/* "pysam/calignmentfile.pyx":3355
+ *             self.query_alignment_qualities = fromQualityString(v)
+ *     property qstart:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, i, pos
- *             cdef int op
+ *             return self.query_alignment_start
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9positions_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9positions_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_6qstart_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_6qstart_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9positions___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6qstart___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9positions___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_i;
-  uint32_t __pyx_v_pos;
-  int __pyx_v_op;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_result = NULL;
-  PyObject *__pyx_v_l = NULL;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6qstart___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int32_t __pyx_t_4;
-  uint16_t __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  uint32_t __pyx_t_7;
-  int __pyx_t_8;
-  int __pyx_t_9;
-  int __pyx_t_10;
-  int __pyx_t_11;
+  PyObject *__pyx_t_1 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2995);
-
-  /* "pysam/csamfile.pyx":3001
- *             cdef bam1_t * src
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":3002
- * 
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return []
- * 
- */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":3003
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []             # <<<<<<<<<<<<<<
- * 
- *             result = []
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3003; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3005
- *                 return []
- * 
- *             result = []             # <<<<<<<<<<<<<<
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)
- */
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_v_result = ((PyObject*)__pyx_t_3);
-  __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":3006
- * 
- *             result = []
- *             pos = src.core.pos             # <<<<<<<<<<<<<<
- *             cigar_p = pysam_bam_get_cigar(src)
- * 
- */
-  __pyx_t_4 = __pyx_v_src->core.pos;
-  __pyx_v_pos = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":3007
- *             result = []
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":3009
- *             cigar_p = pysam_bam_get_cigar(src)
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":3010
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:
- */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3355);
 
-    /* "pysam/csamfile.pyx":3011
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:
+  /* "pysam/calignmentfile.pyx":3356
+ *     property qstart:
+ *         def __get__(self):
+ *             return self.query_alignment_start             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.query_alignment_start = v
  */
-    __pyx_t_3 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_3);
-    __pyx_t_3 = 0;
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-    /* "pysam/csamfile.pyx":3012
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
- *                     for i from pos <= i < pos + l:
- *                         result.append( i )
+  /* "pysam/calignmentfile.pyx":3355
+ *             self.query_alignment_qualities = fromQualityString(v)
+ *     property qstart:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.query_alignment_start
+ *         def __set__(self, v):
  */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":3013
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
- *                         result.append( i )
- * 
- */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_6 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_6); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qstart.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-        /* "pysam/csamfile.pyx":3014
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:
- *                         result.append( i )             # <<<<<<<<<<<<<<
- * 
- *                 if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
+/* "pysam/calignmentfile.pyx":3357
+ *         def __get__(self):
+ *             return self.query_alignment_start
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_start = v
+ *     property qend:
  */
-        __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_i); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_8 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_6); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      }
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
 
-    /* "pysam/csamfile.pyx":3016
- *                         result.append( i )
- * 
- *                 if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
- *                     pos += l
- * 
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_9 = ((__pyx_v_op == BAM_CDEL) != 0);
-      if (!__pyx_t_9) {
-        __pyx_t_10 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
-        __pyx_t_11 = __pyx_t_10;
-      } else {
-        __pyx_t_11 = __pyx_t_9;
-      }
-      __pyx_t_9 = __pyx_t_11;
-    } else {
-      __pyx_t_9 = __pyx_t_2;
-    }
-    if (__pyx_t_9) {
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_6qstart_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_6qstart_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6qstart_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
-      /* "pysam/csamfile.pyx":3017
- * 
- *                 if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     pos += l             # <<<<<<<<<<<<<<
- * 
- *             return result
- */
-      __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_t_6, __pyx_v_l); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_3); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_pos = __pyx_t_7;
-      goto __pyx_L9;
-    }
-    __pyx_L9:;
-  }
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":3019
- *                     pos += l
- * 
- *             return result             # <<<<<<<<<<<<<<
- * 
- *     property inferred_length:
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6qstart_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3357);
+
+  /* "pysam/calignmentfile.pyx":3358
+ *             return self.query_alignment_start
+ *         def __set__(self, v):
+ *             self.query_alignment_start = v             # <<<<<<<<<<<<<<
+ *     property qend:
+ *         def __get__(self):
  */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_result);
-  __pyx_r = __pyx_v_result;
-  goto __pyx_L0;
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_start, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":2995
- *     property positions:
- *         """a list of reference positions that this read aligns to."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, i, pos
- *             cdef int op
+  /* "pysam/calignmentfile.pyx":3357
+ *         def __get__(self):
+ *             return self.query_alignment_start
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_start = v
+ *     property qend:
  */
 
   /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.positions.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qstart.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_result);
-  __Pyx_XDECREF(__pyx_v_l);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3026
- *         Returns 0 if CIGAR string is not present.
- *         """
+/* "pysam/calignmentfile.pyx":3360
+ *             self.query_alignment_start = v
+ *     property qend:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *            cdef uint32_t k, qpos
- *            cdef int op
+ *             return self.query_alignment_end
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15inferred_length_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15inferred_length_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qend_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qend_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_15inferred_length___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qend___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15inferred_length___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_qpos;
-  int __pyx_v_op;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qend___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  uint16_t __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_t_7;
-  int __pyx_t_8;
-  PyObject *__pyx_t_9 = NULL;
+  PyObject *__pyx_t_1 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3026);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3360);
 
-  /* "pysam/csamfile.pyx":3032
- *            cdef bam1_t * src
- * 
- *            src = self._delegate             # <<<<<<<<<<<<<<
- *            if pysam_get_n_cigar(src) == 0: return 0
- * 
+  /* "pysam/calignmentfile.pyx":3361
+ *     property qend:
+ *         def __get__(self):
+ *             return self.query_alignment_end             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.query_alignment_end = v
  */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_end); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3033
- * 
- *            src = self._delegate
- *            if pysam_get_n_cigar(src) == 0: return 0             # <<<<<<<<<<<<<<
- * 
- *            qpos = 0
+  /* "pysam/calignmentfile.pyx":3360
+ *             self.query_alignment_start = v
+ *     property qend:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.query_alignment_end
+ *         def __set__(self, v):
  */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_int_0);
-    __pyx_r = __pyx_int_0;
-    goto __pyx_L0;
-  }
 
-  /* "pysam/csamfile.pyx":3035
- *            if pysam_get_n_cigar(src) == 0: return 0
- * 
- *            qpos = 0             # <<<<<<<<<<<<<<
- *            cigar_p = pysam_bam_get_cigar(src)
- * 
- */
-  __pyx_v_qpos = 0;
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qend.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":3036
- * 
- *            qpos = 0
- *            cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *            for k from 0 <= k < pysam_get_n_cigar(src):
+/* "pysam/calignmentfile.pyx":3362
+ *         def __get__(self):
+ *             return self.query_alignment_end
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_end = v
+ *     property qlen:
  */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
 
-  /* "pysam/csamfile.pyx":3038
- *            cigar_p = pysam_bam_get_cigar(src)
- * 
- *            for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                op = cigar_p[k] & BAM_CIGAR_MASK
- * 
- */
-  __pyx_t_3 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_3; __pyx_v_k++) {
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qend_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qend_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qend_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
-    /* "pysam/csamfile.pyx":3039
- * 
- *            for k from 0 <= k < pysam_get_n_cigar(src):
- *                op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- * 
- *                if op == BAM_CMATCH or op == BAM_CINS or \
- */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-    /* "pysam/csamfile.pyx":3041
- *                op = cigar_p[k] & BAM_CIGAR_MASK
- * 
- *                if op == BAM_CMATCH or op == BAM_CINS or \             # <<<<<<<<<<<<<<
- *                   op == BAM_CSOFT_CLIP or \
- *                   op == BAM_CEQUAL or op == BAM_CDIFF:
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_4 = ((__pyx_v_op == BAM_CINS) != 0);
-      if (!__pyx_t_4) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qend_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3362);
 
-        /* "pysam/csamfile.pyx":3042
- * 
- *                if op == BAM_CMATCH or op == BAM_CINS or \
- *                   op == BAM_CSOFT_CLIP or \             # <<<<<<<<<<<<<<
- *                   op == BAM_CEQUAL or op == BAM_CDIFF:
- *                    qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
+  /* "pysam/calignmentfile.pyx":3363
+ *             return self.query_alignment_end
+ *         def __set__(self, v):
+ *             self.query_alignment_end = v             # <<<<<<<<<<<<<<
+ *     property qlen:
+ *         def __get__(self):
  */
-        __pyx_t_5 = ((__pyx_v_op == BAM_CSOFT_CLIP) != 0);
-        if (!__pyx_t_5) {
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_end, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-          /* "pysam/csamfile.pyx":3043
- *                if op == BAM_CMATCH or op == BAM_CINS or \
- *                   op == BAM_CSOFT_CLIP or \
- *                   op == BAM_CEQUAL or op == BAM_CDIFF:             # <<<<<<<<<<<<<<
- *                    qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
+  /* "pysam/calignmentfile.pyx":3362
+ *         def __get__(self):
+ *             return self.query_alignment_end
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_end = v
+ *     property qlen:
  */
-          __pyx_t_6 = ((__pyx_v_op == BAM_CEQUAL) != 0);
-          if (!__pyx_t_6) {
-            __pyx_t_7 = ((__pyx_v_op == BAM_CDIFF) != 0);
-            __pyx_t_8 = __pyx_t_7;
-          } else {
-            __pyx_t_8 = __pyx_t_6;
-          }
-          __pyx_t_6 = __pyx_t_8;
-        } else {
-          __pyx_t_6 = __pyx_t_5;
-        }
-        __pyx_t_5 = __pyx_t_6;
-      } else {
-        __pyx_t_5 = __pyx_t_4;
-      }
-      __pyx_t_4 = __pyx_t_5;
-    } else {
-      __pyx_t_4 = __pyx_t_2;
-    }
-    if (__pyx_t_4) {
 
-      /* "pysam/csamfile.pyx":3044
- *                   op == BAM_CSOFT_CLIP or \
- *                   op == BAM_CEQUAL or op == BAM_CDIFF:
- *                    qpos += cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- * 
- *            return qpos
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qend.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3365
+ *             self.query_alignment_end = v
+ *     property qlen:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.query_alignment_length
+ *         def __set__(self, v):
  */
-      __pyx_v_qpos = (__pyx_v_qpos + ((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT));
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-  }
 
-  /* "pysam/csamfile.pyx":3046
- *                    qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
- *            return qpos             # <<<<<<<<<<<<<<
- * 
- *     property aligned_pairs:
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qlen_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qlen_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qlen___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qlen___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3365);
+
+  /* "pysam/calignmentfile.pyx":3366
+ *     property qlen:
+ *         def __get__(self):
+ *             return self.query_alignment_length             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.query_alignment_length = v
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_9 = __Pyx_PyInt_From_uint32_t(__pyx_v_qpos); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3046; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_r = __pyx_t_9;
-  __pyx_t_9 = 0;
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_length); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3026
- *         Returns 0 if CIGAR string is not present.
- *         """
+  /* "pysam/calignmentfile.pyx":3365
+ *             self.query_alignment_end = v
+ *     property qlen:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *            cdef uint32_t k, qpos
- *            cdef int op
+ *             return self.query_alignment_length
+ *         def __set__(self, v):
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.inferred_length.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qlen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -31614,1810 +35113,951 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15inferred_length___get
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3053
- *         Unaligned position are marked by None.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, i, pos, qpos
- *             cdef int op
+/* "pysam/calignmentfile.pyx":3367
+ *         def __get__(self):
+ *             return self.query_alignment_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_length = v
+ *     property mrnm:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13aligned_pairs_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13aligned_pairs_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_13aligned_pairs___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qlen_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_13aligned_pairs___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_i;
-  uint32_t __pyx_v_pos;
-  uint32_t __pyx_v_qpos;
-  int __pyx_v_op;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_result = NULL;
-  PyObject *__pyx_v_l = NULL;
-  PyObject *__pyx_r = NULL;
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4qlen_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int32_t __pyx_t_4;
-  uint16_t __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  uint32_t __pyx_t_7;
-  PyObject *__pyx_t_8 = NULL;
-  int __pyx_t_9;
-  int __pyx_t_10;
-  int __pyx_t_11;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3053);
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3367);
 
-  /* "pysam/csamfile.pyx":3059
- *             cdef bam1_t * src
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []
+  /* "pysam/calignmentfile.pyx":3368
+ *             return self.query_alignment_length
+ *         def __set__(self, v):
+ *             self.query_alignment_length = v             # <<<<<<<<<<<<<<
+ *     property mrnm:
+ *         def __get__(self):
  */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_alignment_length, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":3060
- * 
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return []
- * 
+  /* "pysam/calignmentfile.pyx":3367
+ *         def __get__(self):
+ *             return self.query_alignment_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.query_alignment_length = v
+ *     property mrnm:
  */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
 
-    /* "pysam/csamfile.pyx":3061
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []             # <<<<<<<<<<<<<<
- * 
- *             result = []
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3061; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.qlen.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":3063
- *                 return []
- * 
- *             result = []             # <<<<<<<<<<<<<<
- *             pos = src.core.pos
- *             qpos = 0
+/* "pysam/calignmentfile.pyx":3370
+ *             self.query_alignment_length = v
+ *     property mrnm:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.next_reference_id
+ *         def __set__(self, v):
  */
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3063; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_v_result = ((PyObject*)__pyx_t_3);
-  __pyx_t_3 = 0;
 
-  /* "pysam/csamfile.pyx":3064
- * 
- *             result = []
- *             pos = src.core.pos             # <<<<<<<<<<<<<<
- *             qpos = 0
- *             cigar_p = pysam_bam_get_cigar(src)
- */
-  __pyx_t_4 = __pyx_v_src->core.pos;
-  __pyx_v_pos = __pyx_t_4;
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mrnm_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mrnm_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mrnm___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
-  /* "pysam/csamfile.pyx":3065
- *             result = []
- *             pos = src.core.pos
- *             qpos = 0             # <<<<<<<<<<<<<<
- *             cigar_p = pysam_bam_get_cigar(src)
- * 
- */
-  __pyx_v_qpos = 0;
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":3066
- *             pos = src.core.pos
- *             qpos = 0
- *             cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mrnm___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3370);
 
-  /* "pysam/csamfile.pyx":3068
- *             cigar_p = pysam_bam_get_cigar(src)
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
+  /* "pysam/calignmentfile.pyx":3371
+ *     property mrnm:
+ *         def __get__(self):
+ *             return self.next_reference_id             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.next_reference_id = v
  */
-  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-    /* "pysam/csamfile.pyx":3069
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
+  /* "pysam/calignmentfile.pyx":3370
+ *             self.query_alignment_length = v
+ *     property mrnm:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.next_reference_id
+ *         def __set__(self, v):
  */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
 
-    /* "pysam/csamfile.pyx":3070
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- * 
- *                 if op == BAM_CMATCH:
- */
-    __pyx_t_3 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_3);
-    __pyx_t_3 = 0;
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mrnm.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-    /* "pysam/csamfile.pyx":3072
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
- *                 if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, i) )
+/* "pysam/calignmentfile.pyx":3372
+ *         def __get__(self):
+ *             return self.next_reference_id
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.next_reference_id = v
+ *     property mpos:
  */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":3073
- * 
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
- *                         result.append( (qpos, i) )
- *                         qpos += 1
- */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3073; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_6 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3073; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_6); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3073; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mrnm_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mrnm_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mrnm_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
-        /* "pysam/csamfile.pyx":3074
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, i) )             # <<<<<<<<<<<<<<
- *                         qpos += 1
- *                     pos += l
- */
-        __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_qpos); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3074; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_i); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3074; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_3);
-        __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3074; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_6);
-        __Pyx_GIVEREF(__pyx_t_6);
-        PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_3);
-        __Pyx_GIVEREF(__pyx_t_3);
-        __pyx_t_6 = 0;
-        __pyx_t_3 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3074; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-        /* "pysam/csamfile.pyx":3075
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, i) )
- *                         qpos += 1             # <<<<<<<<<<<<<<
- *                     pos += l
- * 
- */
-        __pyx_v_qpos = (__pyx_v_qpos + 1);
-      }
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mrnm_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3372);
 
-      /* "pysam/csamfile.pyx":3076
- *                         result.append( (qpos, i) )
- *                         qpos += 1
- *                     pos += l             # <<<<<<<<<<<<<<
- * 
- *                 elif op == BAM_CINS:
+  /* "pysam/calignmentfile.pyx":3373
+ *             return self.next_reference_id
+ *         def __set__(self, v):
+ *             self.next_reference_id = v             # <<<<<<<<<<<<<<
+ *     property mpos:
+ *         def __get__(self):
  */
-      __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3076; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_t_8, __pyx_v_l); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3076; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_3); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3076; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_pos = __pyx_t_7;
-      goto __pyx_L6;
-    }
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_id, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":3078
- *                     pos += l
- * 
- *                 elif op == BAM_CINS:             # <<<<<<<<<<<<<<
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, None) )
+  /* "pysam/calignmentfile.pyx":3372
+ *         def __get__(self):
+ *             return self.next_reference_id
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.next_reference_id = v
+ *     property mpos:
  */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CINS) != 0);
-    if (__pyx_t_2) {
 
-      /* "pysam/csamfile.pyx":3079
- * 
- *                 elif op == BAM_CINS:
- *                     for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
- *                         result.append( (qpos, None) )
- *                         qpos += 1
- */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_8 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mrnm.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-        /* "pysam/csamfile.pyx":3080
- *                 elif op == BAM_CINS:
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, None) )             # <<<<<<<<<<<<<<
- *                         qpos += 1
- * 
+/* "pysam/calignmentfile.pyx":3375
+ *             self.next_reference_id = v
+ *     property mpos:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.next_reference_start
+ *         def __set__(self, v):
  */
-        __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_v_qpos); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_3);
-        PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_8);
-        __Pyx_GIVEREF(__pyx_t_8);
-        __Pyx_INCREF(Py_None);
-        PyTuple_SET_ITEM(__pyx_t_3, 1, Py_None);
-        __Pyx_GIVEREF(Py_None);
-        __pyx_t_8 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_3); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-        /* "pysam/csamfile.pyx":3081
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, None) )
- *                         qpos += 1             # <<<<<<<<<<<<<<
- * 
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- */
-        __pyx_v_qpos = (__pyx_v_qpos + 1);
-      }
-      goto __pyx_L6;
-    }
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mpos_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mpos_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mpos___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
-    /* "pysam/csamfile.pyx":3083
- *                         qpos += 1
- * 
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
- *                     for i from pos <= i < pos + l:
- *                         result.append( (None, i) )
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CDEL) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_10 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
-      __pyx_t_11 = __pyx_t_10;
-    } else {
-      __pyx_t_11 = __pyx_t_2;
-    }
-    if (__pyx_t_11) {
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-      /* "pysam/csamfile.pyx":3084
- * 
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
- *                         result.append( (None, i) )
- *                     pos += l
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mpos___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3375);
+
+  /* "pysam/calignmentfile.pyx":3376
+ *     property mpos:
+ *         def __get__(self):
+ *             return self.next_reference_start             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.next_reference_start = v
  */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3084; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_8 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3084; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3084; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-        /* "pysam/csamfile.pyx":3085
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     for i from pos <= i < pos + l:
- *                         result.append( (None, i) )             # <<<<<<<<<<<<<<
- *                     pos += l
- * 
+  /* "pysam/calignmentfile.pyx":3375
+ *             self.next_reference_id = v
+ *     property mpos:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.next_reference_start
+ *         def __set__(self, v):
  */
-        __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_v_i); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_3);
-        __Pyx_INCREF(Py_None);
-        PyTuple_SET_ITEM(__pyx_t_3, 0, Py_None);
-        __Pyx_GIVEREF(Py_None);
-        PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_8);
-        __Pyx_GIVEREF(__pyx_t_8);
-        __pyx_t_8 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_3); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      }
 
-      /* "pysam/csamfile.pyx":3086
- *                     for i from pos <= i < pos + l:
- *                         result.append( (None, i) )
- *                     pos += l             # <<<<<<<<<<<<<<
- * 
- *             return result
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mpos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3377
+ *         def __get__(self):
+ *             return self.next_reference_start
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.next_reference_start = v
+ *     property rname:
  */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_8 = PyNumber_InPlaceAdd(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_v_pos = __pyx_t_7;
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-  }
 
-  /* "pysam/csamfile.pyx":3088
- *                     pos += l
- * 
- *             return result             # <<<<<<<<<<<<<<
- * 
- *     property blocks:
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mpos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mpos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mpos_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_4mpos_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3377);
+
+  /* "pysam/calignmentfile.pyx":3378
+ *             return self.next_reference_start
+ *         def __set__(self, v):
+ *             self.next_reference_start = v             # <<<<<<<<<<<<<<
+ *     property rname:
+ *         def __get__(self):
  */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_result);
-  __pyx_r = __pyx_v_result;
-  goto __pyx_L0;
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next_reference_start, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":3053
- *         Unaligned position are marked by None.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, i, pos, qpos
- *             cdef int op
+  /* "pysam/calignmentfile.pyx":3377
+ *         def __get__(self):
+ *             return self.next_reference_start
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.next_reference_start = v
+ *     property rname:
  */
 
   /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.aligned_pairs.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.mpos.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_result);
-  __Pyx_XDECREF(__pyx_v_l);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3103
- *         """
- * 
+/* "pysam/calignmentfile.pyx":3380
+ *             self.next_reference_start = v
+ *     property rname:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, pos, l
- *             cdef int op
+ *             return self.reference_id
+ *         def __set__(self, v):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_6blocks_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_6blocks_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rname_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rname_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_6blocks___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rname___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6blocks___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_pos;
-  uint32_t __pyx_v_l;
-  int __pyx_v_op;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_result = NULL;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rname___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int32_t __pyx_t_4;
-  uint16_t __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  int __pyx_t_8;
-  int __pyx_t_9;
-  int __pyx_t_10;
+  PyObject *__pyx_t_1 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3103);
-
-  /* "pysam/csamfile.pyx":3109
- *             cdef bam1_t * src
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3380);
 
-  /* "pysam/csamfile.pyx":3110
- * 
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return []
- * 
+  /* "pysam/calignmentfile.pyx":3381
+ *     property rname:
+ *         def __get__(self):
+ *             return self.reference_id             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.reference_id = v
  */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
 
-    /* "pysam/csamfile.pyx":3111
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []             # <<<<<<<<<<<<<<
- * 
- *             result = []
+  /* "pysam/calignmentfile.pyx":3380
+ *             self.next_reference_start = v
+ *     property rname:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.reference_id
+ *         def __set__(self, v):
  */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3111; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
 
-  /* "pysam/csamfile.pyx":3113
- *                 return []
- * 
- *             result = []             # <<<<<<<<<<<<<<
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)
- */
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_v_result = ((PyObject*)__pyx_t_3);
-  __pyx_t_3 = 0;
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.rname.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":3114
- * 
- *             result = []
- *             pos = src.core.pos             # <<<<<<<<<<<<<<
- *             cigar_p = pysam_bam_get_cigar(src)
- *             l = 0
+/* "pysam/calignmentfile.pyx":3382
+ *         def __get__(self):
+ *             return self.reference_id
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_id = v
+ *     property isize:
  */
-  __pyx_t_4 = __pyx_v_src->core.pos;
-  __pyx_v_pos = __pyx_t_4;
 
-  /* "pysam/csamfile.pyx":3115
- *             result = []
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- *             l = 0
- * 
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rname_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
-  /* "pysam/csamfile.pyx":3116
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)
- *             l = 0             # <<<<<<<<<<<<<<
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- */
-  __pyx_v_l = 0;
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-  /* "pysam/csamfile.pyx":3118
- *             l = 0
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5rname_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3382);
 
-    /* "pysam/csamfile.pyx":3119
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:
+  /* "pysam/calignmentfile.pyx":3383
+ *             return self.reference_id
+ *         def __set__(self, v):
+ *             self.reference_id = v             # <<<<<<<<<<<<<<
+ *     property isize:
+ *         def __get__(self):
  */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_id, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":3120
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- *                 if op == BAM_CMATCH:
- *                     result.append((pos, pos + l))
+  /* "pysam/calignmentfile.pyx":3382
+ *         def __get__(self):
+ *             return self.reference_id
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_id = v
+ *     property isize:
  */
-    __pyx_v_l = ((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT);
 
-    /* "pysam/csamfile.pyx":3121
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
- *                     result.append((pos, pos + l))
- *                     pos += l
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (__pyx_t_2) {
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.rname.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-      /* "pysam/csamfile.pyx":3122
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:
- *                     result.append((pos, pos + l))             # <<<<<<<<<<<<<<
- *                     pos += l
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+/* "pysam/calignmentfile.pyx":3385
+ *             self.reference_id = v
+ *     property isize:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.template_length
+ *         def __set__(self, v):
  */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_6 = __Pyx_PyInt_From_uint32_t((__pyx_v_pos + __pyx_v_l)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_3);
-      __Pyx_GIVEREF(__pyx_t_3);
-      PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_6);
-      __Pyx_GIVEREF(__pyx_t_6);
-      __pyx_t_3 = 0;
-      __pyx_t_6 = 0;
-      __pyx_t_8 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_7); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
 
-      /* "pysam/csamfile.pyx":3123
- *                 if op == BAM_CMATCH:
- *                     result.append((pos, pos + l))
- *                     pos += l             # <<<<<<<<<<<<<<
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     pos += l
- */
-      __pyx_v_pos = (__pyx_v_pos + __pyx_v_l);
-      goto __pyx_L6;
-    }
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5isize_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5isize_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5isize___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
-    /* "pysam/csamfile.pyx":3124
- *                     result.append((pos, pos + l))
- *                     pos += l
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
- *                     pos += l
- * 
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CDEL) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_9 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
-      __pyx_t_10 = __pyx_t_9;
-    } else {
-      __pyx_t_10 = __pyx_t_2;
-    }
-    if (__pyx_t_10) {
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-      /* "pysam/csamfile.pyx":3125
- *                     pos += l
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     pos += l             # <<<<<<<<<<<<<<
- * 
- *             return result
- */
-      __pyx_v_pos = (__pyx_v_pos + __pyx_v_l);
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-  }
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5isize___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3385);
 
-  /* "pysam/csamfile.pyx":3127
- *                     pos += l
- * 
- *             return result             # <<<<<<<<<<<<<<
- * 
- *     #######################################################################
+  /* "pysam/calignmentfile.pyx":3386
+ *     property isize:
+ *         def __get__(self):
+ *             return self.template_length             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.template_length = v
  */
   __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_result);
-  __pyx_r = __pyx_v_result;
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_template_length); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3103
- *         """
- * 
+  /* "pysam/calignmentfile.pyx":3385
+ *             self.reference_id = v
+ *     property isize:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, pos, l
- *             cdef int op
+ *             return self.template_length
+ *         def __set__(self, v):
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.blocks.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.isize.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_result);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3133
- *     ##
- *     #######################################################################
- *     def overlap( self, uint32_t start, uint32_t end ):             # <<<<<<<<<<<<<<
- *         """return number of aligned bases of read overlapping the interval *start* and *end*
- *         on the reference sequence.
+/* "pysam/calignmentfile.pyx":3387
+ *         def __get__(self):
+ *             return self.template_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.template_length = v
+ *     property blocks:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15overlap(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_14overlap[] = "AlignedRead.overlap(self, uint32_t start, uint32_t end)\nreturn number of aligned bases of read overlapping the interval *start* and *end*\n        on the reference sequence.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15overlap(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  uint32_t __pyx_v_start;
-  uint32_t __pyx_v_end;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5isize_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5isize_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("overlap (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_start,&__pyx_n_s_end,0};
-    PyObject* values[2] = {0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_start)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("overlap", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "overlap") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
-      goto __pyx_L5_argtuple_error;
-    } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-    }
-    __pyx_v_start = __Pyx_PyInt_As_uint32_t(values[0]); if (unlikely((__pyx_v_start == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    __pyx_v_end = __Pyx_PyInt_As_uint32_t(values[1]); if (unlikely((__pyx_v_end == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("overlap", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.overlap", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_14overlap(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), __pyx_v_start, __pyx_v_end);
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5isize_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self), ((PyObject *)__pyx_v_v));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_14overlap(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, uint32_t __pyx_v_start, uint32_t __pyx_v_end) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_pos;
-  uint32_t __pyx_v_overlap;
-  int __pyx_v_op;
-  int __pyx_v_o;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_l = NULL;
-  PyObject *__pyx_r = NULL;
+static int __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_5isize_2__set__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  int32_t __pyx_t_3;
-  uint16_t __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  uint32_t __pyx_t_6;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  uint32_t __pyx_t_10;
-  uint32_t __pyx_t_11;
-  int __pyx_t_12;
-  int __pyx_t_13;
-  int __pyx_t_14;
-  int __pyx_t_15;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("overlap", 0);
-  __Pyx_TraceCall("overlap", __pyx_f[0], 3133);
-
-  /* "pysam/csamfile.pyx":3142
- *         cdef bam1_t * src
- * 
- *         overlap = 0             # <<<<<<<<<<<<<<
- * 
- *         src = self._delegate
- */
-  __pyx_v_overlap = 0;
-
-  /* "pysam/csamfile.pyx":3144
- *         overlap = 0
- * 
- *         src = self._delegate             # <<<<<<<<<<<<<<
- *         if pysam_get_n_cigar(src) == 0:
- *             return 0
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":3145
- * 
- *         src = self._delegate
- *         if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *             return 0
- *         pos = src.core.pos
- */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":3146
- *         src = self._delegate
- *         if pysam_get_n_cigar(src) == 0:
- *             return 0             # <<<<<<<<<<<<<<
- *         pos = src.core.pos
- *         o = 0
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_int_0);
-    __pyx_r = __pyx_int_0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3147
- *         if pysam_get_n_cigar(src) == 0:
- *             return 0
- *         pos = src.core.pos             # <<<<<<<<<<<<<<
- *         o = 0
- * 
- */
-  __pyx_t_3 = __pyx_v_src->core.pos;
-  __pyx_v_pos = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":3148
- *             return 0
- *         pos = src.core.pos
- *         o = 0             # <<<<<<<<<<<<<<
- * 
- *         cigar_p = pysam_bam_get_cigar(src)
- */
-  __pyx_v_o = 0;
-
-  /* "pysam/csamfile.pyx":3150
- *         o = 0
- * 
- *         cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":3151
- * 
- *         cigar_p = pysam_bam_get_cigar(src)
- *         for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-  __pyx_t_4 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3387);
 
-    /* "pysam/csamfile.pyx":3152
- *         cigar_p = pysam_bam_get_cigar(src)
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
+  /* "pysam/calignmentfile.pyx":3388
+ *             return self.template_length
+ *         def __set__(self, v):
+ *             self.template_length = v             # <<<<<<<<<<<<<<
+ *     property blocks:
+ *         def __get__(self):
  */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_template_length, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":3153
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- * 
- *             if op == BAM_CMATCH:
+  /* "pysam/calignmentfile.pyx":3387
+ *         def __get__(self):
+ *             return self.template_length
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.template_length = v
+ *     property blocks:
  */
-    __pyx_t_5 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_5);
-    __pyx_t_5 = 0;
 
-    /* "pysam/csamfile.pyx":3155
- *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
- *             if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
- *                 o = min( pos + l, end) - max( pos, start )
- *                 if o > 0: overlap += o
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (__pyx_t_2) {
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.isize.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-      /* "pysam/csamfile.pyx":3156
- * 
- *             if op == BAM_CMATCH:
- *                 o = min( pos + l, end) - max( pos, start )             # <<<<<<<<<<<<<<
- *                 if o > 0: overlap += o
- * 
+/* "pysam/calignmentfile.pyx":3390
+ *             self.template_length = v
+ *     property blocks:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.get_blocks()
+ *     property aligned_pairs:
  */
-      __pyx_t_6 = __pyx_v_end;
-      __pyx_t_5 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_7 = PyNumber_Add(__pyx_t_5, __pyx_v_l); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_t_6); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_9 = PyObject_RichCompare(__pyx_t_8, __pyx_t_7, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __Pyx_PyInt_From_uint32_t(__pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_5 = __pyx_t_9;
-        __pyx_t_9 = 0;
-      } else {
-        __Pyx_INCREF(__pyx_t_7);
-        __pyx_t_5 = __pyx_t_7;
-      }
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_6 = __pyx_v_start;
-      __pyx_t_10 = __pyx_v_pos;
-      if (((__pyx_t_6 > __pyx_t_10) != 0)) {
-        __pyx_t_11 = __pyx_t_6;
-      } else {
-        __pyx_t_11 = __pyx_t_10;
-      }
-      __pyx_t_7 = __Pyx_PyInt_From_uint32_t(__pyx_t_11); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_9 = PyNumber_Subtract(__pyx_t_5, __pyx_t_7); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_12 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_12 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_v_o = __pyx_t_12;
 
-      /* "pysam/csamfile.pyx":3157
- *             if op == BAM_CMATCH:
- *                 o = min( pos + l, end) - max( pos, start )
- *                 if o > 0: overlap += o             # <<<<<<<<<<<<<<
- * 
- *             if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
- */
-      __pyx_t_2 = ((__pyx_v_o > 0) != 0);
-      if (__pyx_t_2) {
-        __pyx_v_overlap = (__pyx_v_overlap + __pyx_v_o);
-        goto __pyx_L7;
-      }
-      __pyx_L7:;
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_6blocks_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_6blocks_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6blocks___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
-    /* "pysam/csamfile.pyx":3159
- *                 if o > 0: overlap += o
- * 
- *             if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
- *                 pos += l
- * 
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_13 = ((__pyx_v_op == BAM_CDEL) != 0);
-      if (!__pyx_t_13) {
-        __pyx_t_14 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
-        __pyx_t_15 = __pyx_t_14;
-      } else {
-        __pyx_t_15 = __pyx_t_13;
-      }
-      __pyx_t_13 = __pyx_t_15;
-    } else {
-      __pyx_t_13 = __pyx_t_2;
-    }
-    if (__pyx_t_13) {
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-      /* "pysam/csamfile.pyx":3160
- * 
- *             if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                 pos += l             # <<<<<<<<<<<<<<
- * 
- *         return overlap
- */
-      __pyx_t_9 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_7 = PyNumber_InPlaceAdd(__pyx_t_9, __pyx_v_l); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_11 = __Pyx_PyInt_As_uint32_t(__pyx_t_7); if (unlikely((__pyx_t_11 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_v_pos = __pyx_t_11;
-      goto __pyx_L8;
-    }
-    __pyx_L8:;
-  }
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_6blocks___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3390);
 
-  /* "pysam/csamfile.pyx":3162
- *                 pos += l
- * 
- *         return overlap             # <<<<<<<<<<<<<<
- * 
- *     def opt(self, tag):
+  /* "pysam/calignmentfile.pyx":3391
+ *     property blocks:
+ *         def __get__(self):
+ *             return self.get_blocks()             # <<<<<<<<<<<<<<
+ *     property aligned_pairs:
+ *         def __get__(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_7 = __Pyx_PyInt_From_uint32_t(__pyx_v_overlap); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-  __pyx_r = __pyx_t_7;
-  __pyx_t_7 = 0;
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_get_blocks); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3133
- *     ##
- *     #######################################################################
- *     def overlap( self, uint32_t start, uint32_t end ):             # <<<<<<<<<<<<<<
- *         """return number of aligned bases of read overlapping the interval *start* and *end*
- *         on the reference sequence.
+  /* "pysam/calignmentfile.pyx":3390
+ *             self.template_length = v
+ *     property blocks:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.get_blocks()
+ *     property aligned_pairs:
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.overlap", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.blocks.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_l);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3164
- *         return overlap
- * 
- *     def opt(self, tag):             # <<<<<<<<<<<<<<
- *         """retrieves optional data given a two-letter *tag*"""
- *         #see bam_aux.c: bam_aux_get() and bam_aux2i() etc
+/* "pysam/calignmentfile.pyx":3393
+ *             return self.get_blocks()
+ *     property aligned_pairs:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.get_aligned_pairs()
+ *     property inferred_length:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_17opt(PyObject *__pyx_v_self, PyObject *__pyx_v_tag); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_16opt[] = "AlignedRead.opt(self, tag)\nretrieves optional data given a two-letter *tag*";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_17opt(PyObject *__pyx_v_self, PyObject *__pyx_v_tag) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_13aligned_pairs_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_13aligned_pairs_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("opt (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_16opt(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_tag));
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_13aligned_pairs___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_16opt(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag) {
-  uint8_t *__pyx_v_v;
-  CYTHON_UNUSED int __pyx_v_nvalues;
-  PyObject *__pyx_v_btag = NULL;
-  PyObject *__pyx_v_auxtype = NULL;
-  CYTHON_UNUSED PyObject *__pyx_v_bytesize = NULL;
-  PyObject *__pyx_v_values = NULL;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_13aligned_pairs___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  char const *__pyx_t_2;
-  int __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_t_7;
-  int __pyx_t_8;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  PyObject *__pyx_t_11 = NULL;
-  PyObject *(*__pyx_t_12)(PyObject *);
-  int __pyx_t_13;
+  PyObject *__pyx_t_2 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("opt", 0);
-  __Pyx_TraceCall("opt", __pyx_f[0], 3164);
-
-  /* "pysam/csamfile.pyx":3169
- *         cdef uint8_t * v
- *         cdef int nvalues
- *         btag = _forceBytes(tag)             # <<<<<<<<<<<<<<
- *         v = bam_aux_get(self._delegate, btag)
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
- */
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_btag = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3170
- *         cdef int nvalues
- *         btag = _forceBytes(tag)
- *         v = bam_aux_get(self._delegate, btag)             # <<<<<<<<<<<<<<
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
- *         auxtype = chr(v[0])
- */
-  __pyx_t_2 = __Pyx_PyObject_AsString(__pyx_v_btag); if (unlikely((!__pyx_t_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3170; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_v = bam_aux_get(__pyx_v_self->_delegate, __pyx_t_2);
-
-  /* "pysam/csamfile.pyx":3171
- *         btag = _forceBytes(tag)
- *         v = bam_aux_get(self._delegate, btag)
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )             # <<<<<<<<<<<<<<
- *         auxtype = chr(v[0])
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
- */
-  __pyx_t_3 = ((__pyx_v_v == NULL) != 0);
-  if (__pyx_t_3) {
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_tag_s_not_present, __pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3393);
 
-  /* "pysam/csamfile.pyx":3172
- *         v = bam_aux_get(self._delegate, btag)
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
- *         auxtype = chr(v[0])             # <<<<<<<<<<<<<<
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
- *             return <int>bam_aux2i(v)
+  /* "pysam/calignmentfile.pyx":3394
+ *     property aligned_pairs:
+ *         def __get__(self):
+ *             return self.get_aligned_pairs()             # <<<<<<<<<<<<<<
+ *     property inferred_length:
+ *         def __get__(self):
  */
-  __pyx_t_1 = __Pyx_PyInt_From_uint8_t((__pyx_v_v[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_chr, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_get_aligned_pairs); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3394; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_v_auxtype = __pyx_t_1;
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3173
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
- *         auxtype = chr(v[0])
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':             # <<<<<<<<<<<<<<
- *             return <int>bam_aux2i(v)
- *         elif auxtype == 'i' or auxtype == 'I':
- */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_c, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_3) {
-    __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_C, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (!__pyx_t_5) {
-      __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_s_2, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      if (!__pyx_t_6) {
-        __pyx_t_7 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_S, Py_EQ)); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_8 = __pyx_t_7;
-      } else {
-        __pyx_t_8 = __pyx_t_6;
-      }
-      __pyx_t_6 = __pyx_t_8;
-    } else {
-      __pyx_t_6 = __pyx_t_5;
-    }
-    __pyx_t_5 = __pyx_t_6;
-  } else {
-    __pyx_t_5 = __pyx_t_3;
-  }
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":3174
- *         auxtype = chr(v[0])
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
- *             return <int>bam_aux2i(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'i' or auxtype == 'I':
- *             return <int32_t>bam_aux2i(v)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyInt_From_int(((int)bam_aux2i(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_r = __pyx_t_1;
-    __pyx_t_1 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3175
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
- *             return <int>bam_aux2i(v)
- *         elif auxtype == 'i' or auxtype == 'I':             # <<<<<<<<<<<<<<
- *             return <int32_t>bam_aux2i(v)
- *         elif auxtype == 'f' or auxtype == 'F':
- */
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_i, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_5) {
-    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_I, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = __pyx_t_3;
-  } else {
-    __pyx_t_6 = __pyx_t_5;
-  }
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":3176
- *             return <int>bam_aux2i(v)
- *         elif auxtype == 'i' or auxtype == 'I':
- *             return <int32_t>bam_aux2i(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'f' or auxtype == 'F':
- *             return <float>bam_aux2f(v)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyInt_From_int32_t(((int32_t)bam_aux2i(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_r = __pyx_t_1;
-    __pyx_t_1 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3177
- *         elif auxtype == 'i' or auxtype == 'I':
- *             return <int32_t>bam_aux2i(v)
- *         elif auxtype == 'f' or auxtype == 'F':             # <<<<<<<<<<<<<<
- *             return <float>bam_aux2f(v)
- *         elif auxtype == 'd' or auxtype == 'D':
- */
-  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_f, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_6) {
-    __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_F, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = __pyx_t_5;
-  } else {
-    __pyx_t_3 = __pyx_t_6;
-  }
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":3178
- *             return <int32_t>bam_aux2i(v)
- *         elif auxtype == 'f' or auxtype == 'F':
- *             return <float>bam_aux2f(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'd' or auxtype == 'D':
- *             return <double>bam_aux2f(v)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = PyFloat_FromDouble(((float)bam_aux2f(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_r = __pyx_t_1;
-    __pyx_t_1 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3179
- *         elif auxtype == 'f' or auxtype == 'F':
- *             return <float>bam_aux2f(v)
- *         elif auxtype == 'd' or auxtype == 'D':             # <<<<<<<<<<<<<<
- *             return <double>bam_aux2f(v)
- *         elif auxtype == 'A':
- */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_d, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_3) {
-    __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_D, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = __pyx_t_6;
-  } else {
-    __pyx_t_5 = __pyx_t_3;
-  }
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":3180
- *             return <float>bam_aux2f(v)
- *         elif auxtype == 'd' or auxtype == 'D':
- *             return <double>bam_aux2f(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'A':
- *             # there might a more efficient way
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = PyFloat_FromDouble(((double)bam_aux2f(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_r = __pyx_t_1;
-    __pyx_t_1 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3181
- *         elif auxtype == 'd' or auxtype == 'D':
- *             return <double>bam_aux2f(v)
- *         elif auxtype == 'A':             # <<<<<<<<<<<<<<
- *             # there might a more efficient way
- *             # to convert a char into a string
- */
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_A, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_5) {
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3394; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
+  goto __pyx_L0;
 
-    /* "pysam/csamfile.pyx":3184
- *             # there might a more efficient way
- *             # to convert a char into a string
- *             return '%c' % <char>bam_aux2A(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'Z':
- *             return _charptr_to_str(<char*>bam_aux2Z(v))
+  /* "pysam/calignmentfile.pyx":3393
+ *             return self.get_blocks()
+ *     property aligned_pairs:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.get_aligned_pairs()
+ *     property inferred_length:
  */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyInt_From_char(((char)bam_aux2A(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_c_2, __pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_r = __pyx_t_4;
-    __pyx_t_4 = 0;
-    goto __pyx_L0;
-  }
 
-  /* "pysam/csamfile.pyx":3185
- *             # to convert a char into a string
- *             return '%c' % <char>bam_aux2A(v)
- *         elif auxtype == 'Z':             # <<<<<<<<<<<<<<
- *             return _charptr_to_str(<char*>bam_aux2Z(v))
- *         elif auxtype == 'B':
- */
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_Z, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_5) {
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.aligned_pairs.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-    /* "pysam/csamfile.pyx":3186
- *             return '%c' % <char>bam_aux2A(v)
- *         elif auxtype == 'Z':
- *             return _charptr_to_str(<char*>bam_aux2Z(v))             # <<<<<<<<<<<<<<
- *         elif auxtype == 'B':
- *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
+/* "pysam/calignmentfile.pyx":3396
+ *             return self.get_aligned_pairs()
+ *     property inferred_length:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.infer_query_length()
+ *     property positions:
  */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_4 = __pyx_f_5pysam_8csamfile__charptr_to_str(((char *)bam_aux2Z(__pyx_v_v))); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_r = __pyx_t_4;
-    __pyx_t_4 = 0;
-    goto __pyx_L0;
-  }
 
-  /* "pysam/csamfile.pyx":3187
- *         elif auxtype == 'Z':
- *             return _charptr_to_str(<char*>bam_aux2Z(v))
- *         elif auxtype == 'B':             # <<<<<<<<<<<<<<
- *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
- *             return values
- */
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_B, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_5) {
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15inferred_length_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15inferred_length_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15inferred_length___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
-    /* "pysam/csamfile.pyx":3188
- *             return _charptr_to_str(<char*>bam_aux2Z(v))
- *         elif auxtype == 'B':
- *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )             # <<<<<<<<<<<<<<
- *             return values
- *         else:
- */
-    __pyx_t_4 = __pyx_f_5pysam_8csamfile_convertBinaryTagToList((__pyx_v_v + 1)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    if ((likely(PyTuple_CheckExact(__pyx_t_4))) || (PyList_CheckExact(__pyx_t_4))) {
-      PyObject* sequence = __pyx_t_4;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      Py_ssize_t size = Py_SIZE(sequence);
-      #else
-      Py_ssize_t size = PySequence_Size(sequence);
-      #endif
-      if (unlikely(size != 3)) {
-        if (size > 3) __Pyx_RaiseTooManyValuesError(3);
-        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      #if CYTHON_COMPILING_IN_CPYTHON
-      if (likely(PyTuple_CheckExact(sequence))) {
-        __pyx_t_1 = PyTuple_GET_ITEM(sequence, 0); 
-        __pyx_t_9 = PyTuple_GET_ITEM(sequence, 1); 
-        __pyx_t_10 = PyTuple_GET_ITEM(sequence, 2); 
-      } else {
-        __pyx_t_1 = PyList_GET_ITEM(sequence, 0); 
-        __pyx_t_9 = PyList_GET_ITEM(sequence, 1); 
-        __pyx_t_10 = PyList_GET_ITEM(sequence, 2); 
-      }
-      __Pyx_INCREF(__pyx_t_1);
-      __Pyx_INCREF(__pyx_t_9);
-      __Pyx_INCREF(__pyx_t_10);
-      #else
-      __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_9 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_10 = PySequence_ITEM(sequence, 2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_10);
-      #endif
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    } else {
-      Py_ssize_t index = -1;
-      __pyx_t_11 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_11);
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext;
-      index = 0; __pyx_t_1 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_1)) goto __pyx_L5_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_1);
-      index = 1; __pyx_t_9 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_9)) goto __pyx_L5_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_9);
-      index = 2; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L5_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_10);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_12 = NULL;
-      __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-      goto __pyx_L6_unpacking_done;
-      __pyx_L5_unpacking_failed:;
-      __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-      __pyx_t_12 = NULL;
-      if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_L6_unpacking_done:;
-    }
-    __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_v_bytesize = __pyx_t_1;
-    __pyx_t_1 = 0;
-    __pyx_v_nvalues = __pyx_t_13;
-    __pyx_v_values = __pyx_t_10;
-    __pyx_t_10 = 0;
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-    /* "pysam/csamfile.pyx":3189
- *         elif auxtype == 'B':
- *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
- *             return values             # <<<<<<<<<<<<<<
- *         else:
- *             raise ValueError("unknown auxilliary type '%s'" % auxtype)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_v_values);
-    __pyx_r = __pyx_v_values;
-    goto __pyx_L0;
-  }
-  /*else*/ {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_15inferred_length___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3396);
 
-    /* "pysam/csamfile.pyx":3191
- *             return values
- *         else:
- *             raise ValueError("unknown auxilliary type '%s'" % auxtype)             # <<<<<<<<<<<<<<
- * 
- * 
+  /* "pysam/calignmentfile.pyx":3397
+ *     property inferred_length:
+ *         def __get__(self):
+ *             return self.infer_query_length()             # <<<<<<<<<<<<<<
+ *     property positions:
+ *         def __get__(self):
  */
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_unknown_auxilliary_type_s, __pyx_v_auxtype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_10, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __Pyx_Raise(__pyx_t_4, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_infer_query_length); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3164
- *         return overlap
- * 
- *     def opt(self, tag):             # <<<<<<<<<<<<<<
- *         """retrieves optional data given a two-letter *tag*"""
- *         #see bam_aux.c: bam_aux_get() and bam_aux2i() etc
+  /* "pysam/calignmentfile.pyx":3396
+ *             return self.get_aligned_pairs()
+ *     property inferred_length:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.infer_query_length()
+ *     property positions:
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_XDECREF(__pyx_t_11);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.opt", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.inferred_length.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_btag);
-  __Pyx_XDECREF(__pyx_v_auxtype);
-  __Pyx_XDECREF(__pyx_v_bytesize);
-  __Pyx_XDECREF(__pyx_v_values);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3194
- * 
- * 
- *     def fancy_str (self):             # <<<<<<<<<<<<<<
- *         """returns list of fieldnames/values in pretty format for debugging.
- * 
+/* "pysam/calignmentfile.pyx":3399
+ *             return self.infer_query_length()
+ *     property positions:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.get_reference_positions()
+ *     def overlap(self):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_19fancy_str(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_18fancy_str[] = "AlignedRead.fancy_str(self)\nreturns list of fieldnames/values in pretty format for debugging.\n\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_19fancy_str(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9positions_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9positions_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("fancy_str (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_18fancy_str(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9positions___get__(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_18fancy_str(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_v_ret_string = NULL;
-  PyObject *__pyx_v_field_names = NULL;
-  PyObject *__pyx_v_fields_names_in_order = NULL;
-  PyObject *__pyx_v_f = NULL;
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_9positions___get__(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
-  Py_ssize_t __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  int __pyx_t_10;
+  PyObject *__pyx_t_2 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("fancy_str", 0);
-  __Pyx_TraceCall("fancy_str", __pyx_f[0], 3194);
-
-  /* "pysam/csamfile.pyx":3198
- * 
- *         """
- *         ret_string = []             # <<<<<<<<<<<<<<
- * 
- *         # Originally written by Leo. Note that not all of these fields
- */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_ret_string = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3202
- *         # Originally written by Leo. Note that not all of these fields
- *         # exist. Deprecate?
- *         field_names = {             # <<<<<<<<<<<<<<
- *            "tid":           "Contig index",
- *            "pos":           "Mapped position on contig",
- */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_tid, __pyx_kp_s_Contig_index) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_pos, __pyx_kp_s_Mapped_position_on_contig) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_mtid, __pyx_kp_s_Contig_index_for_mate_pair) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_mpos, __pyx_kp_s_Position_of_mate_pair) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_isize, __pyx_kp_s_Insert_size) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_flag, __pyx_kp_s_Binary_flag) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_n_cigar, __pyx_kp_s_Count_of_cigar_entries) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_cigar, __pyx_kp_s_Cigar_entries) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_qual, __pyx_kp_s_Mapping_quality) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_bin, __pyx_kp_s_Bam_index_bin_number) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_l_qname, __pyx_kp_s_Length_of_query_name) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_qname, __pyx_kp_s_Query_name) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_l_qseq, __pyx_kp_s_Length_of_query_sequence) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_qseq, __pyx_kp_s_Query_sequence) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_bqual, __pyx_kp_s_Quality_scores) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_l_data, __pyx_kp_s_Length_of_auxilary_data) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_m_data, __pyx_kp_s_Maximum_data_length) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_field_names = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3399);
 
-  /* "pysam/csamfile.pyx":3221
- *            "m_data":        "Maximum data length",
- *            }
- *         fields_names_in_order = ["tid", "pos", "mtid", "mpos", "isize", "flag",             # <<<<<<<<<<<<<<
- *                                  "n_cigar", "cigar", "qual", "bin", "l_qname", "qname",
- *                                  "l_qseq", "qseq", "bqual", "l_data", "m_data"]
+  /* "pysam/calignmentfile.pyx":3400
+ *     property positions:
+ *         def __get__(self):
+ *             return self.get_reference_positions()             # <<<<<<<<<<<<<<
+ *     def overlap(self):
+ *         return self.get_overlap()
  */
-  __pyx_t_1 = PyList_New(17); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_get_reference_positions); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(__pyx_n_s_tid);
-  PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_tid);
-  __Pyx_GIVEREF(__pyx_n_s_tid);
-  __Pyx_INCREF(__pyx_n_s_pos);
-  PyList_SET_ITEM(__pyx_t_1, 1, __pyx_n_s_pos);
-  __Pyx_GIVEREF(__pyx_n_s_pos);
-  __Pyx_INCREF(__pyx_n_s_mtid);
-  PyList_SET_ITEM(__pyx_t_1, 2, __pyx_n_s_mtid);
-  __Pyx_GIVEREF(__pyx_n_s_mtid);
-  __Pyx_INCREF(__pyx_n_s_mpos);
-  PyList_SET_ITEM(__pyx_t_1, 3, __pyx_n_s_mpos);
-  __Pyx_GIVEREF(__pyx_n_s_mpos);
-  __Pyx_INCREF(__pyx_n_s_isize);
-  PyList_SET_ITEM(__pyx_t_1, 4, __pyx_n_s_isize);
-  __Pyx_GIVEREF(__pyx_n_s_isize);
-  __Pyx_INCREF(__pyx_n_s_flag);
-  PyList_SET_ITEM(__pyx_t_1, 5, __pyx_n_s_flag);
-  __Pyx_GIVEREF(__pyx_n_s_flag);
-  __Pyx_INCREF(__pyx_n_s_n_cigar);
-  PyList_SET_ITEM(__pyx_t_1, 6, __pyx_n_s_n_cigar);
-  __Pyx_GIVEREF(__pyx_n_s_n_cigar);
-  __Pyx_INCREF(__pyx_n_s_cigar);
-  PyList_SET_ITEM(__pyx_t_1, 7, __pyx_n_s_cigar);
-  __Pyx_GIVEREF(__pyx_n_s_cigar);
-  __Pyx_INCREF(__pyx_n_s_qual);
-  PyList_SET_ITEM(__pyx_t_1, 8, __pyx_n_s_qual);
-  __Pyx_GIVEREF(__pyx_n_s_qual);
-  __Pyx_INCREF(__pyx_n_s_bin);
-  PyList_SET_ITEM(__pyx_t_1, 9, __pyx_n_s_bin);
-  __Pyx_GIVEREF(__pyx_n_s_bin);
-  __Pyx_INCREF(__pyx_n_s_l_qname);
-  PyList_SET_ITEM(__pyx_t_1, 10, __pyx_n_s_l_qname);
-  __Pyx_GIVEREF(__pyx_n_s_l_qname);
-  __Pyx_INCREF(__pyx_n_s_qname);
-  PyList_SET_ITEM(__pyx_t_1, 11, __pyx_n_s_qname);
-  __Pyx_GIVEREF(__pyx_n_s_qname);
-  __Pyx_INCREF(__pyx_n_s_l_qseq);
-  PyList_SET_ITEM(__pyx_t_1, 12, __pyx_n_s_l_qseq);
-  __Pyx_GIVEREF(__pyx_n_s_l_qseq);
-  __Pyx_INCREF(__pyx_n_s_qseq);
-  PyList_SET_ITEM(__pyx_t_1, 13, __pyx_n_s_qseq);
-  __Pyx_GIVEREF(__pyx_n_s_qseq);
-  __Pyx_INCREF(__pyx_n_s_bqual);
-  PyList_SET_ITEM(__pyx_t_1, 14, __pyx_n_s_bqual);
-  __Pyx_GIVEREF(__pyx_n_s_bqual);
-  __Pyx_INCREF(__pyx_n_s_l_data);
-  PyList_SET_ITEM(__pyx_t_1, 15, __pyx_n_s_l_data);
-  __Pyx_GIVEREF(__pyx_n_s_l_data);
-  __Pyx_INCREF(__pyx_n_s_m_data);
-  PyList_SET_ITEM(__pyx_t_1, 16, __pyx_n_s_m_data);
-  __Pyx_GIVEREF(__pyx_n_s_m_data);
-  __pyx_v_fields_names_in_order = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3225
- *                                  "l_qseq", "qseq", "bqual", "l_data", "m_data"]
- * 
- *         for f in fields_names_in_order:             # <<<<<<<<<<<<<<
- *             if f not in dir(self):
- *                 continue
- */
-  __pyx_t_1 = __pyx_v_fields_names_in_order; __Pyx_INCREF(__pyx_t_1); __pyx_t_2 = 0;
-  for (;;) {
-    if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_1)) break;
-    #if CYTHON_COMPILING_IN_CPYTHON
-    __pyx_t_3 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_3); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    #else
-    __pyx_t_3 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    #endif
-    __Pyx_XDECREF_SET(__pyx_v_f, __pyx_t_3);
-    __pyx_t_3 = 0;
-
-    /* "pysam/csamfile.pyx":3226
- * 
- *         for f in fields_names_in_order:
- *             if f not in dir(self):             # <<<<<<<<<<<<<<
- *                 continue
- *             ret_string.append("%-30s %-10s= %s" %
- */
-    __pyx_t_3 = PyObject_Dir(((PyObject *)__pyx_v_self)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = (__Pyx_PySequence_Contains(__pyx_v_f, __pyx_t_3, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_5 = (__pyx_t_4 != 0);
-    if (__pyx_t_5) {
-
-      /* "pysam/csamfile.pyx":3227
- *         for f in fields_names_in_order:
- *             if f not in dir(self):
- *                 continue             # <<<<<<<<<<<<<<
- *             ret_string.append("%-30s %-10s= %s" %
- *                               (field_names[f],
- */
-      goto __pyx_L3_continue;
-    }
-
-    /* "pysam/csamfile.pyx":3229
- *                 continue
- *             ret_string.append("%-30s %-10s= %s" %
- *                               (field_names[f],             # <<<<<<<<<<<<<<
- *                                "(" + f + ")",
- *                                self.__getattribute__(f)))
- */
-    __pyx_t_3 = __Pyx_PyDict_GetItem(__pyx_v_field_names, __pyx_v_f); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3229; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_3);
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
+  goto __pyx_L0;
 
-    /* "pysam/csamfile.pyx":3230
- *             ret_string.append("%-30s %-10s= %s" %
- *                               (field_names[f],
- *                                "(" + f + ")",             # <<<<<<<<<<<<<<
- *                                self.__getattribute__(f)))
- * 
+  /* "pysam/calignmentfile.pyx":3399
+ *             return self.infer_query_length()
+ *     property positions:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.get_reference_positions()
+ *     def overlap(self):
  */
-    __pyx_t_6 = PyNumber_Add(__pyx_kp_s__79, __pyx_v_f); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3230; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_7 = PyNumber_Add(__pyx_t_6, __pyx_kp_s__80); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3230; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-    /* "pysam/csamfile.pyx":3231
- *                               (field_names[f],
- *                                "(" + f + ")",
- *                                self.__getattribute__(f)))             # <<<<<<<<<<<<<<
- * 
- *         return ret_string
- */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getattribute); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3231; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3231; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_INCREF(__pyx_v_f);
-    PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_f);
-    __Pyx_GIVEREF(__pyx_v_f);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3231; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.positions.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
 
-    /* "pysam/csamfile.pyx":3229
- *                 continue
- *             ret_string.append("%-30s %-10s= %s" %
- *                               (field_names[f],             # <<<<<<<<<<<<<<
- *                                "(" + f + ")",
- *                                self.__getattribute__(f)))
+/* "pysam/calignmentfile.pyx":3401
+ *         def __get__(self):
+ *             return self.get_reference_positions()
+ *     def overlap(self):             # <<<<<<<<<<<<<<
+ *         return self.get_overlap()
+ * 
  */
-    __pyx_t_8 = PyTuple_New(3); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_7);
-    __Pyx_GIVEREF(__pyx_t_7);
-    PyTuple_SET_ITEM(__pyx_t_8, 2, __pyx_t_9);
-    __Pyx_GIVEREF(__pyx_t_9);
-    __pyx_t_3 = 0;
-    __pyx_t_7 = 0;
-    __pyx_t_9 = 0;
 
-    /* "pysam/csamfile.pyx":3228
- *             if f not in dir(self):
- *                 continue
- *             ret_string.append("%-30s %-10s= %s" %             # <<<<<<<<<<<<<<
- *                               (field_names[f],
- *                                "(" + f + ")",
- */
-    __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_30s_10s_s, __pyx_t_8); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3228; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    __pyx_t_10 = __Pyx_PyList_Append(__pyx_v_ret_string, __pyx_t_9); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3228; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_L3_continue:;
-  }
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_25overlap(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_24overlap[] = "AlignedSegment.overlap(self)";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_25overlap(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("overlap (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_14AlignedSegment_24overlap(((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_14AlignedSegment_24overlap(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("overlap", 0);
+  __Pyx_TraceCall("overlap", __pyx_f[0], 3401);
 
-  /* "pysam/csamfile.pyx":3233
- *                                self.__getattribute__(f)))
+  /* "pysam/calignmentfile.pyx":3402
+ *             return self.get_reference_positions()
+ *     def overlap(self):
+ *         return self.get_overlap()             # <<<<<<<<<<<<<<
  * 
- *         return ret_string             # <<<<<<<<<<<<<<
  * 
- * cdef class PileupProxy:
  */
   __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_ret_string);
-  __pyx_r = __pyx_v_ret_string;
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_get_overlap); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3402; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3402; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3194
- * 
- * 
- *     def fancy_str (self):             # <<<<<<<<<<<<<<
- *         """returns list of fieldnames/values in pretty format for debugging.
+  /* "pysam/calignmentfile.pyx":3401
+ *         def __get__(self):
+ *             return self.get_reference_positions()
+ *     def overlap(self):             # <<<<<<<<<<<<<<
+ *         return self.get_overlap()
  * 
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.fancy_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("pysam.calignmentfile.AlignedSegment.overlap", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_ret_string);
-  __Pyx_XDECREF(__pyx_v_field_names);
-  __Pyx_XDECREF(__pyx_v_fields_names_in_order);
-  __Pyx_XDECREF(__pyx_v_f);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3252
- *     will change.
+/* "pysam/calignmentfile.pyx":3415
+ * 
  *     '''
  *     def __init__(self):             # <<<<<<<<<<<<<<
- *         raise TypeError("This class cannot be instantiated from Python")
+ *         raise TypeError("this class cannot be instantiated from Python")
  * 
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11PileupProxy_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11PileupProxy_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   if (unlikely(PyTuple_GET_SIZE(__pyx_args) > 0)) {
     __Pyx_RaiseArgtupleInvalid("__init__", 1, 0, 0, PyTuple_GET_SIZE(__pyx_args)); return -1;}
   if (unlikely(__pyx_kwds) && unlikely(PyDict_Size(__pyx_kwds) > 0) && unlikely(!__Pyx_CheckKeywordStrings(__pyx_kwds, "__init__", 0))) return -1;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy___init__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn___init__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11PileupProxy___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -33426,61 +36066,61 @@ static int __pyx_pf_5pysam_8csamfile_11PileupProxy___init__(CYTHON_UNUSED struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 3252);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 3415);
 
-  /* "pysam/csamfile.pyx":3253
+  /* "pysam/calignmentfile.pyx":3416
  *     '''
  *     def __init__(self):
- *         raise TypeError("This class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
+ *         raise TypeError("this class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
  * 
  *     def __str__(self):
  */
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__81, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__77, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_Raise(__pyx_t_1, 0, 0, 0);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":3252
- *     will change.
+  /* "pysam/calignmentfile.pyx":3415
+ * 
  *     '''
  *     def __init__(self):             # <<<<<<<<<<<<<<
- *         raise TypeError("This class cannot be instantiated from Python")
+ *         raise TypeError("this class cannot be instantiated from Python")
  * 
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3255
- *         raise TypeError("This class cannot be instantiated from Python")
+/* "pysam/calignmentfile.pyx":3418
+ *         raise TypeError("this class cannot be instantiated from Python")
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" +\
+ *         return "\t".join(map(str,
+ *                               (self.reference_id, self.reference_pos,
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3__str__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3__str__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_3__str__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_3__str__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_2__str__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_2__str__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -33492,23 +36132,47 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 3255);
+  __Pyx_TraceCall("__str__", __pyx_f[0], 3418);
 
-  /* "pysam/csamfile.pyx":3256
+  /* "pysam/calignmentfile.pyx":3419
  * 
  *     def __str__(self):
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\             # <<<<<<<<<<<<<<
- *             "\n" +\
- *             "\n".join( map(str, self.pileups) )
+ *         return "\t".join(map(str,             # <<<<<<<<<<<<<<
+ *                               (self.reference_id, self.reference_pos,
+ *                                self.nsegmentes))) +\
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3420
+ *     def __str__(self):
+ *         return "\t".join(map(str,
+ *                               (self.reference_id, self.reference_pos,             # <<<<<<<<<<<<<<
+ *                                self.nsegmentes))) +\
+ *             "\n" +\
+ */
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_self->pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_n); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3421
+ *         return "\t".join(map(str,
+ *                               (self.reference_id, self.reference_pos,
+ *                                self.nsegmentes))) +\             # <<<<<<<<<<<<<<
+ *             "\n" +\
+ *             "\n".join(map(str, self.pileups))
+ */
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_nsegmentes); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyTuple_New(3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3420
+ *     def __str__(self):
+ *         return "\t".join(map(str,
+ *                               (self.reference_id, self.reference_pos,             # <<<<<<<<<<<<<<
+ *                                self.nsegmentes))) +\
+ *             "\n" +\
+ */
+  __pyx_t_4 = PyTuple_New(3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
@@ -33519,7 +36183,15 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_o
   __pyx_t_1 = 0;
   __pyx_t_2 = 0;
   __pyx_t_3 = 0;
-  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3419
+ * 
+ *     def __str__(self):
+ *         return "\t".join(map(str,             # <<<<<<<<<<<<<<
+ *                               (self.reference_id, self.reference_pos,
+ *                                self.nsegmentes))) +\
+ */
+  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3419; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
   PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
@@ -33527,26 +36199,34 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_o
   PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_4);
   __Pyx_GIVEREF(__pyx_t_4);
   __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3419; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__43, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3419; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = PyNumber_Add(__pyx_t_3, __pyx_kp_s__4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3421
+ *         return "\t".join(map(str,
+ *                               (self.reference_id, self.reference_pos,
+ *                                self.nsegmentes))) +\             # <<<<<<<<<<<<<<
+ *             "\n" +\
+ *             "\n".join(map(str, self.pileups))
+ */
+  __pyx_t_4 = PyNumber_Add(__pyx_t_3, __pyx_kp_s__38); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/csamfile.pyx":3258
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
+  /* "pysam/calignmentfile.pyx":3423
+ *                                self.nsegmentes))) +\
  *             "\n" +\
- *             "\n".join( map(str, self.pileups) )             # <<<<<<<<<<<<<<
+ *             "\n".join(map(str, self.pileups))             # <<<<<<<<<<<<<<
  * 
- *     property tid:
+ *     property reference_id:
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_pileups); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_pileups); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
   PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
@@ -33554,21 +36234,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_o
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_3);
   __Pyx_GIVEREF(__pyx_t_3);
   __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__4, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__38, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/csamfile.pyx":3257
- *     def __str__(self):
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
+  /* "pysam/calignmentfile.pyx":3422
+ *                               (self.reference_id, self.reference_pos,
+ *                                self.nsegmentes))) +\
  *             "\n" +\             # <<<<<<<<<<<<<<
- *             "\n".join( map(str, self.pileups) )
+ *             "\n".join(map(str, self.pileups))
  * 
  */
-  __pyx_t_3 = PyNumber_Add(__pyx_t_4, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyNumber_Add(__pyx_t_4, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -33576,12 +36256,12 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_o
   __pyx_t_3 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3255
- *         raise TypeError("This class cannot be instantiated from Python")
+  /* "pysam/calignmentfile.pyx":3418
+ *         raise TypeError("this class cannot be instantiated from Python")
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" +\
+ *         return "\t".join(map(str,
+ *                               (self.reference_id, self.reference_pos,
  */
 
   /* function exit code */
@@ -33590,7 +36270,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_o
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -33599,28 +36279,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3262
- *     property tid:
- *         '''the chromosome ID as is defined in the header'''
- *         def __get__(self): return self.tid             # <<<<<<<<<<<<<<
+/* "pysam/calignmentfile.pyx":3427
+ *     property reference_id:
+ *         '''the reference sequence number as defined in the header'''
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.tid
  * 
- *     property n:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3tid_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3tid_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_12reference_id_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_12reference_id_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_3tid___get__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_12reference_id___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3tid___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_12reference_id___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -33629,18 +36309,34 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3tid___get__(struct __p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3262);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3427);
+
+  /* "pysam/calignmentfile.pyx":3428
+ *         '''the reference sequence number as defined in the header'''
+ *         def __get__(self):
+ *             return self.tid             # <<<<<<<<<<<<<<
+ * 
+ *     property nsegments:
+ */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3262; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
+  /* "pysam/calignmentfile.pyx":3427
+ *     property reference_id:
+ *         '''the reference sequence number as defined in the header'''
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.tid
+ * 
+ */
+
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.tid.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.reference_id.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -33649,28 +36345,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3tid___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3266
- *     property n:
+/* "pysam/calignmentfile.pyx":3432
+ *     property nsegments:
  *         '''number of reads mapping to this column.'''
- *         def __get__(self): return self.n_pu             # <<<<<<<<<<<<<<
- *         def __set__(self, n): self.n_pu = n
- * 
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.n_pu
+ *         def __set__(self, n):
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_1n_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_1n_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_9nsegments_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_9nsegments_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_1n___get__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_9nsegments___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_1n___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_9nsegments___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -33679,18 +36375,34 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_1n___get__(struct __pyx
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3266);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3432);
+
+  /* "pysam/calignmentfile.pyx":3433
+ *         '''number of reads mapping to this column.'''
+ *         def __get__(self):
+ *             return self.n_pu             # <<<<<<<<<<<<<<
+ *         def __set__(self, n):
+ *             self.n_pu = n
+ */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->n_pu); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->n_pu); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
+  /* "pysam/calignmentfile.pyx":3432
+ *     property nsegments:
+ *         '''number of reads mapping to this column.'''
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.n_pu
+ *         def __set__(self, n):
+ */
+
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.n.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.nsegments.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -33699,28 +36411,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_1n___get__(struct __pyx
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3267
- *         '''number of reads mapping to this column.'''
- *         def __get__(self): return self.n_pu
- *         def __set__(self, n): self.n_pu = n             # <<<<<<<<<<<<<<
+/* "pysam/calignmentfile.pyx":3434
+ *         def __get__(self):
+ *             return self.n_pu
+ *         def __set__(self, n):             # <<<<<<<<<<<<<<
+ *             self.n_pu = n
  * 
- *     property pos:
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11PileupProxy_1n_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_n); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11PileupProxy_1n_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_n) {
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_9nsegments_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_n); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_9nsegments_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_n) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_1n_2__set__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self), ((PyObject *)__pyx_v_n));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_9nsegments_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self), ((PyObject *)__pyx_v_n));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_11PileupProxy_1n_2__set__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self, PyObject *__pyx_v_n) {
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn_9nsegments_2__set__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self, PyObject *__pyx_v_n) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -33729,15 +36441,31 @@ static int __pyx_pf_5pysam_8csamfile_11PileupProxy_1n_2__set__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 3267);
-  __pyx_t_1 = __Pyx_PyInt_As_int(__pyx_v_n); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3434);
+
+  /* "pysam/calignmentfile.pyx":3435
+ *             return self.n_pu
+ *         def __set__(self, n):
+ *             self.n_pu = n             # <<<<<<<<<<<<<<
+ * 
+ *     property reference_pos:
+ */
+  __pyx_t_1 = __Pyx_PyInt_As_int(__pyx_v_n); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_self->n_pu = __pyx_t_1;
 
+  /* "pysam/calignmentfile.pyx":3434
+ *         def __get__(self):
+ *             return self.n_pu
+ *         def __set__(self, n):             # <<<<<<<<<<<<<<
+ *             self.n_pu = n
+ * 
+ */
+
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.n.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.nsegments.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -33745,28 +36473,28 @@ static int __pyx_pf_5pysam_8csamfile_11PileupProxy_1n_2__set__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3270
- * 
- *     property pos:
- *         def __get__(self): return self.pos             # <<<<<<<<<<<<<<
+/* "pysam/calignmentfile.pyx":3439
+ *     property reference_pos:
+ *         '''the position in the reference sequence (0-based).'''
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.pos
  * 
- *     property pileups:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3pos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3pos_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_13reference_pos_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_13reference_pos_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_3pos___get__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_13reference_pos___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3pos___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_13reference_pos___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -33775,18 +36503,34 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3pos___get__(struct __p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3270);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3439);
+
+  /* "pysam/calignmentfile.pyx":3440
+ *         '''the position in the reference sequence (0-based).'''
+ *         def __get__(self):
+ *             return self.pos             # <<<<<<<<<<<<<<
+ * 
+ *     property pileups:
+ */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
+  /* "pysam/calignmentfile.pyx":3439
+ *     property reference_pos:
+ *         '''the position in the reference sequence (0-based).'''
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.pos
+ * 
+ */
+
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.reference_pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -33795,7 +36539,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3pos___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3274
+/* "pysam/calignmentfile.pyx":3444
  *     property pileups:
  *         '''list of reads (:class:`pysam.PileupRead`) aligned to this column'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -33804,19 +36548,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3pos___get__(struct __p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_7pileups_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_7pileups_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_7pileups_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_7pileups_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_7pileups___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_7pileups___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self) {
   int __pyx_v_x;
   PyObject *__pyx_v_pileups = NULL;
   PyObject *__pyx_r = NULL;
@@ -33832,25 +36576,25 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3274);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3444);
 
-  /* "pysam/csamfile.pyx":3276
+  /* "pysam/calignmentfile.pyx":3446
  *         def __get__(self):
  *             cdef int x
  *             pileups = []             # <<<<<<<<<<<<<<
  * 
  *             if self.plp == NULL or self.plp[0] == NULL:
  */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_pileups = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":3278
+  /* "pysam/calignmentfile.pyx":3448
  *             pileups = []
  * 
  *             if self.plp == NULL or self.plp[0] == NULL:             # <<<<<<<<<<<<<<
- *                 raise ValueError("PileupProxy accessed after iterator finished")
+ *                 raise ValueError("PileupColumn accessed after iterator finished")
  * 
  */
   __pyx_t_2 = ((__pyx_v_self->plp == NULL) != 0);
@@ -33862,21 +36606,21 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(struct
   }
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":3279
+    /* "pysam/calignmentfile.pyx":3449
  * 
  *             if self.plp == NULL or self.plp[0] == NULL:
- *                 raise ValueError("PileupProxy accessed after iterator finished")             # <<<<<<<<<<<<<<
+ *                 raise ValueError("PileupColumn accessed after iterator finished")             # <<<<<<<<<<<<<<
  * 
  *             # warning: there could be problems if self.n and self.buf are
  */
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__82, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__78, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_Raise(__pyx_t_1, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/csamfile.pyx":3283
+  /* "pysam/calignmentfile.pyx":3453
  *             # warning: there could be problems if self.n and self.buf are
  *             # out of sync.
  *             for x from 0 <= x < self.n_pu:             # <<<<<<<<<<<<<<
@@ -33886,32 +36630,32 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(struct
   __pyx_t_5 = __pyx_v_self->n_pu;
   for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_5; __pyx_v_x++) {
 
-    /* "pysam/csamfile.pyx":3284
+    /* "pysam/calignmentfile.pyx":3454
  *             # out of sync.
  *             for x from 0 <= x < self.n_pu:
  *                 pileups.append(makePileupRead(&(self.plp[0][x])))             # <<<<<<<<<<<<<<
  *             return pileups
  * 
  */
-    __pyx_t_1 = __pyx_f_5pysam_8csamfile_makePileupRead((&((__pyx_v_self->plp[0])[__pyx_v_x]))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3284; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __pyx_f_5pysam_14calignmentfile_makePileupRead((&((__pyx_v_self->plp[0])[__pyx_v_x]))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_pileups, __pyx_t_1); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3284; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_pileups, __pyx_t_1); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   }
 
-  /* "pysam/csamfile.pyx":3285
+  /* "pysam/calignmentfile.pyx":3455
  *             for x from 0 <= x < self.n_pu:
  *                 pileups.append(makePileupRead(&(self.plp[0][x])))
  *             return pileups             # <<<<<<<<<<<<<<
  * 
- * cdef class PileupRead:
+ *     ########################################################
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(__pyx_v_pileups);
   __pyx_r = __pyx_v_pileups;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3274
+  /* "pysam/calignmentfile.pyx":3444
  *     property pileups:
  *         '''list of reads (:class:`pysam.PileupRead`) aligned to this column'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -33922,7 +36666,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(struct
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.pileups.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.pileups.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_pileups);
@@ -33932,31 +36676,409 @@ static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3291
+/* "pysam/calignmentfile.pyx":3462
+ *     ########################################################
+ *     property pos:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.reference_pos
+ *         def __set__(self, v):
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_3pos_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_3pos_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_3pos___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_3pos___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3462);
+
+  /* "pysam/calignmentfile.pyx":3463
+ *     property pos:
+ *         def __get__(self):
+ *             return self.reference_pos             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.reference_pos = v
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3462
+ *     ########################################################
+ *     property pos:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.reference_pos
+ *         def __set__(self, v):
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3464
+ *         def __get__(self):
+ *             return self.reference_pos
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_pos = v
+ * 
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_3pos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_3pos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_3pos_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn_3pos_2__set__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3464);
+
+  /* "pysam/calignmentfile.pyx":3465
+ *             return self.reference_pos
+ *         def __set__(self, v):
+ *             self.reference_pos = v             # <<<<<<<<<<<<<<
+ * 
+ *     property tid:
+ */
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_pos, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3465; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3464
+ *         def __get__(self):
+ *             return self.reference_pos
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_pos = v
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.pos.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3468
+ * 
+ *     property tid:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.reference_id
+ *         def __set__(self, v):
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_3tid_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_3tid_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_3tid___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_3tid___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3468);
+
+  /* "pysam/calignmentfile.pyx":3469
+ *     property tid:
+ *         def __get__(self):
+ *             return self.reference_id             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.reference_id = v
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3469; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3468
+ * 
+ *     property tid:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.reference_id
+ *         def __set__(self, v):
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.tid.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3470
+ *         def __get__(self):
+ *             return self.reference_id
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_id = v
+ * 
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_3tid_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_3tid_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_3tid_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn_3tid_2__set__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3470);
+
+  /* "pysam/calignmentfile.pyx":3471
+ *             return self.reference_id
+ *         def __set__(self, v):
+ *             self.reference_id = v             # <<<<<<<<<<<<<<
+ * 
+ *     property n:
+ */
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_id, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3470
+ *         def __get__(self):
+ *             return self.reference_id
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.reference_id = v
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.tid.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3474
+ * 
+ *     property n:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.nsegments
+ *         def __set__(self, v):
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_1n_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12PileupColumn_1n_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_1n___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12PileupColumn_1n___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3474);
+
+  /* "pysam/calignmentfile.pyx":3475
+ *     property n:
+ *         def __get__(self):
+ *             return self.nsegments             # <<<<<<<<<<<<<<
+ *         def __set__(self, v):
+ *             self.nsegments = v
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_nsegments); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3474
+ * 
+ *     property n:
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self.nsegments
+ *         def __set__(self, v):
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.n.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3476
+ *         def __get__(self):
+ *             return self.nsegments
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.nsegments = v
+ * 
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_1n_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_12PileupColumn_1n_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12PileupColumn_1n_2__set__(((struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *)__pyx_v_self), ((PyObject *)__pyx_v_v));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_14calignmentfile_12PileupColumn_1n_2__set__(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn *__pyx_v_self, PyObject *__pyx_v_v) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__set__", 0);
+  __Pyx_TraceCall("__set__", __pyx_f[0], 3476);
+
+  /* "pysam/calignmentfile.pyx":3477
+ *             return self.nsegments
+ *         def __set__(self, v):
+ *             self.nsegments = v             # <<<<<<<<<<<<<<
+ * 
+ * 
+ */
+  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_nsegments, __pyx_v_v) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3476
+ *         def __get__(self):
+ *             return self.nsegments
+ *         def __set__(self, v):             # <<<<<<<<<<<<<<
+ *             self.nsegments = v
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupColumn.n.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3486
  *     '''
  * 
  *     def __init__(self):             # <<<<<<<<<<<<<<
- *         raise TypeError("This class cannot be instantiated from Python")
+ *         raise TypeError("this class cannot be instantiated from Python")
  * 
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_10PileupRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_10PileupRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static int __pyx_pw_5pysam_14calignmentfile_10PileupRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_10PileupRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   if (unlikely(PyTuple_GET_SIZE(__pyx_args) > 0)) {
     __Pyx_RaiseArgtupleInvalid("__init__", 1, 0, 0, PyTuple_GET_SIZE(__pyx_args)); return -1;}
   if (unlikely(__pyx_kwds) && unlikely(PyDict_Size(__pyx_kwds) > 0) && unlikely(!__Pyx_CheckKeywordStrings(__pyx_kwds, "__init__", 0))) return -1;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead___init__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead___init__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_10PileupRead___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
+static int __pyx_pf_5pysam_14calignmentfile_10PileupRead___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -33965,61 +37087,61 @@ static int __pyx_pf_5pysam_8csamfile_10PileupRead___init__(CYTHON_UNUSED struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 3291);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 3486);
 
-  /* "pysam/csamfile.pyx":3292
+  /* "pysam/calignmentfile.pyx":3487
  * 
  *     def __init__(self):
- *         raise TypeError("This class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
+ *         raise TypeError("this class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
  * 
  *     def __str__(self):
  */
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__83, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__79, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_Raise(__pyx_t_1, 0, 0, 0);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":3291
+  /* "pysam/calignmentfile.pyx":3486
  *     '''
  * 
  *     def __init__(self):             # <<<<<<<<<<<<<<
- *         raise TypeError("This class cannot be instantiated from Python")
+ *         raise TypeError("this class cannot be instantiated from Python")
  * 
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupRead.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3294
- *         raise TypeError("This class cannot be instantiated from Python")
+/* "pysam/calignmentfile.pyx":3489
+ *         raise TypeError("this class cannot be instantiated from Python")
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.alignment, self.qpos, self.indel, self.level, self.is_del, self.is_head, self.is_tail ) ) )
- * 
+ *         return "\t".join(
+ *             map(str,
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_3__str__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_3__str__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_3__str__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_3__str__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead_2__str__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_2__str__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34030,51 +37152,96 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(struct __pyx_ob
   PyObject *__pyx_t_6 = NULL;
   PyObject *__pyx_t_7 = NULL;
   PyObject *__pyx_t_8 = NULL;
+  PyObject *__pyx_t_9 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 3294);
+  __Pyx_TraceCall("__str__", __pyx_f[0], 3489);
 
-  /* "pysam/csamfile.pyx":3295
+  /* "pysam/calignmentfile.pyx":3490
  * 
  *     def __str__(self):
- *         return "\t".join( map(str, (self.alignment, self.qpos, self.indel, self.level, self.is_del, self.is_head, self.is_tail ) ) )             # <<<<<<<<<<<<<<
- * 
- *     property alignment:
+ *         return "\t".join(             # <<<<<<<<<<<<<<
+ *             map(str,
+ *                 (self.alignment, self.query_position,
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_alignment); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3492
+ *         return "\t".join(
+ *             map(str,
+ *                 (self.alignment, self.query_position,             # <<<<<<<<<<<<<<
+ *                  self.indel, self.level,
+ *                  self.is_del, self.is_head,
+ */
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_alignment); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_qpos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_query_position); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_indel); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3493
+ *             map(str,
+ *                 (self.alignment, self.query_position,
+ *                  self.indel, self.level,             # <<<<<<<<<<<<<<
+ *                  self.is_del, self.is_head,
+ *                  self.is_tail, self.is_refskip)))
+ */
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_indel); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3493; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_level); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_level); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3493; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_del); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3494
+ *                 (self.alignment, self.query_position,
+ *                  self.indel, self.level,
+ *                  self.is_del, self.is_head,             # <<<<<<<<<<<<<<
+ *                  self.is_tail, self.is_refskip)))
+ * 
+ */
+  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_del); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3494; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_head); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_head); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3494; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_tail); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3495
+ *                  self.indel, self.level,
+ *                  self.is_del, self.is_head,
+ *                  self.is_tail, self.is_refskip)))             # <<<<<<<<<<<<<<
+ * 
+ *     property alignment:
+ */
+  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_tail); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
-  __pyx_t_8 = PyTuple_New(7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_refskip); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
-  PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_1);
+
+  /* "pysam/calignmentfile.pyx":3492
+ *         return "\t".join(
+ *             map(str,
+ *                 (self.alignment, self.query_position,             # <<<<<<<<<<<<<<
+ *                  self.indel, self.level,
+ *                  self.is_del, self.is_head,
+ */
+  __pyx_t_9 = PyTuple_New(8); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_9);
+  PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_2);
+  PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_8, 2, __pyx_t_3);
+  PyTuple_SET_ITEM(__pyx_t_9, 2, __pyx_t_3);
   __Pyx_GIVEREF(__pyx_t_3);
-  PyTuple_SET_ITEM(__pyx_t_8, 3, __pyx_t_4);
+  PyTuple_SET_ITEM(__pyx_t_9, 3, __pyx_t_4);
   __Pyx_GIVEREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_8, 4, __pyx_t_5);
+  PyTuple_SET_ITEM(__pyx_t_9, 4, __pyx_t_5);
   __Pyx_GIVEREF(__pyx_t_5);
-  PyTuple_SET_ITEM(__pyx_t_8, 5, __pyx_t_6);
+  PyTuple_SET_ITEM(__pyx_t_9, 5, __pyx_t_6);
   __Pyx_GIVEREF(__pyx_t_6);
-  PyTuple_SET_ITEM(__pyx_t_8, 6, __pyx_t_7);
+  PyTuple_SET_ITEM(__pyx_t_9, 6, __pyx_t_7);
   __Pyx_GIVEREF(__pyx_t_7);
+  PyTuple_SET_ITEM(__pyx_t_9, 7, __pyx_t_8);
+  __Pyx_GIVEREF(__pyx_t_8);
   __pyx_t_1 = 0;
   __pyx_t_2 = 0;
   __pyx_t_3 = 0;
@@ -34082,30 +37249,47 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(struct __pyx_ob
   __pyx_t_5 = 0;
   __pyx_t_6 = 0;
   __pyx_t_7 = 0;
-  __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_7, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_8);
-  __Pyx_GIVEREF(__pyx_t_8);
   __pyx_t_8 = 0;
-  __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":3491
+ *     def __str__(self):
+ *         return "\t".join(
+ *             map(str,             # <<<<<<<<<<<<<<
+ *                 (self.alignment, self.query_position,
+ *                  self.indel, self.level,
+ */
+  __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
-  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-  __pyx_t_7 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_8, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
+  PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_9);
+  __Pyx_GIVEREF(__pyx_t_9);
+  __pyx_t_9 = 0;
+  __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_9);
   __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-  __pyx_r = __pyx_t_7;
-  __pyx_t_7 = 0;
+
+  /* "pysam/calignmentfile.pyx":3490
+ * 
+ *     def __str__(self):
+ *         return "\t".join(             # <<<<<<<<<<<<<<
+ *             map(str,
+ *                 (self.alignment, self.query_position,
+ */
+  __pyx_t_8 = __Pyx_PyString_Join(__pyx_kp_s__43, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_8);
+  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
+  __pyx_r = __pyx_t_8;
+  __pyx_t_8 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3294
- *         raise TypeError("This class cannot be instantiated from Python")
+  /* "pysam/calignmentfile.pyx":3489
+ *         raise TypeError("this class cannot be instantiated from Python")
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.alignment, self.qpos, self.indel, self.level, self.is_del, self.is_head, self.is_tail ) ) )
- * 
+ *         return "\t".join(
+ *             map(str,
  */
 
   /* function exit code */
@@ -34118,7 +37302,8 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(struct __pyx_ob
   __Pyx_XDECREF(__pyx_t_6);
   __Pyx_XDECREF(__pyx_t_7);
   __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_9);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupRead.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34127,52 +37312,52 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3299
+/* "pysam/calignmentfile.pyx":3499
  *     property alignment:
- *         """a :class:`pysam.AlignedRead` object of the aligned read"""
+ *         """a :class:`pysam.AlignedSegment` object of the aligned read"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             return self._alignment
- *     property qpos:
+ * 
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_9alignment_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_9alignment_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_9alignment_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_9alignment_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_9alignment___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead_9alignment___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_9alignment___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_9alignment___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3299);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3499);
 
-  /* "pysam/csamfile.pyx":3300
- *         """a :class:`pysam.AlignedRead` object of the aligned read"""
+  /* "pysam/calignmentfile.pyx":3500
+ *         """a :class:`pysam.AlignedSegment` object of the aligned read"""
  *         def __get__(self):
  *             return self._alignment             # <<<<<<<<<<<<<<
- *     property qpos:
- *         """position of the read base at the pileup site, 0-based"""
+ * 
+ *     property query_position:
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(((PyObject *)__pyx_v_self->_alignment));
   __pyx_r = ((PyObject *)__pyx_v_self->_alignment);
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3299
+  /* "pysam/calignmentfile.pyx":3499
  *     property alignment:
- *         """a :class:`pysam.AlignedRead` object of the aligned read"""
+ *         """a :class:`pysam.AlignedSegment` object of the aligned read"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             return self._alignment
- *     property qpos:
+ * 
  */
 
   /* function exit code */
@@ -34183,28 +37368,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_9alignment___get__(struc
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3303
- *     property qpos:
+/* "pysam/calignmentfile.pyx":3504
+ *     property query_position:
  *         """position of the read base at the pileup site, 0-based"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             return self._qpos
- *     property indel:
+ * 
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_4qpos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_4qpos_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_14query_position_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_14query_position_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_4qpos___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead_14query_position___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_4qpos___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_14query_position___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34213,34 +37398,34 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_4qpos___get__(struct __p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3303);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3504);
 
-  /* "pysam/csamfile.pyx":3304
+  /* "pysam/calignmentfile.pyx":3505
  *         """position of the read base at the pileup site, 0-based"""
  *         def __get__(self):
  *             return self._qpos             # <<<<<<<<<<<<<<
+ * 
  *     property indel:
- *         """indel length; 0 for no indel, positive for ins and negative for del"""
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_qpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_qpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3303
- *     property qpos:
+  /* "pysam/calignmentfile.pyx":3504
+ *     property query_position:
  *         """position of the read base at the pileup site, 0-based"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             return self._qpos
- *     property indel:
+ * 
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.qpos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupRead.query_position.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34249,28 +37434,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_4qpos___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3307
+/* "pysam/calignmentfile.pyx":3509
  *     property indel:
- *         """indel length; 0 for no indel, positive for ins and negative for del"""
+ *         """indel length; 0 for no indel, positive for ins and negative            for del"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             return self._indel
- *     property is_del:
+ *     property level:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_5indel_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_5indel_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_5indel_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_5indel_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_5indel___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead_5indel___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5indel___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_5indel___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34279,34 +37464,100 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5indel___get__(struct __
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3307);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3509);
 
-  /* "pysam/csamfile.pyx":3308
- *         """indel length; 0 for no indel, positive for ins and negative for del"""
+  /* "pysam/calignmentfile.pyx":3510
+ *         """indel length; 0 for no indel, positive for ins and negative            for del"""
  *         def __get__(self):
  *             return self._indel             # <<<<<<<<<<<<<<
- *     property is_del:
- *         """1 iff the base on the padded read is a deletion"""
+ *     property level:
+ *         """the level of the read in the "viewer" mode"""
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_indel); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3308; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_indel); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3307
+  /* "pysam/calignmentfile.pyx":3509
  *     property indel:
- *         """indel length; 0 for no indel, positive for ins and negative for del"""
+ *         """indel length; 0 for no indel, positive for ins and negative            for del"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             return self._indel
+ *     property level:
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupRead.indel.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3513
+ *     property level:
+ *         """the level of the read in the "viewer" mode"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._level
+ *     property is_del:
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_5level_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_5level_1__get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead_5level___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_5level___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__get__", 0);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3513);
+
+  /* "pysam/calignmentfile.pyx":3514
+ *         """the level of the read in the "viewer" mode"""
+ *         def __get__(self):
+ *             return self._level             # <<<<<<<<<<<<<<
+ *     property is_del:
+ *         """1 iff the base on the padded read is a deletion"""
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_level); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/calignmentfile.pyx":3513
+ *     property level:
+ *         """the level of the read in the "viewer" mode"""
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._level
  *     property is_del:
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.indel.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupRead.level.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34315,7 +37566,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5indel___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3311
+/* "pysam/calignmentfile.pyx":3517
  *     property is_del:
  *         """1 iff the base on the padded read is a deletion"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -34324,19 +37575,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5indel___get__(struct __
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_6is_del_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_6is_del_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_6is_del_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_6is_del_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead_6is_del___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_6is_del___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34345,9 +37596,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(struct _
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3311);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3517);
 
-  /* "pysam/csamfile.pyx":3312
+  /* "pysam/calignmentfile.pyx":3518
  *         """1 iff the base on the padded read is a deletion"""
  *         def __get__(self):
  *             return self._is_del             # <<<<<<<<<<<<<<
@@ -34355,13 +37606,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(struct _
  *         def __get__(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->_is_del); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->_is_del); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3311
+  /* "pysam/calignmentfile.pyx":3517
  *     property is_del:
  *         """1 iff the base on the padded read is a deletion"""
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -34372,7 +37623,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(struct _
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.is_del.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupRead.is_del.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34381,7 +37632,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(struct _
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3314
+/* "pysam/calignmentfile.pyx":3520
  *             return self._is_del
  *     property is_head:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -34390,19 +37641,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(struct _
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_7is_head_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_7is_head_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_7is_head_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_7is_head_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead_7is_head___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_7is_head___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34411,9 +37662,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3314);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3520);
 
-  /* "pysam/csamfile.pyx":3315
+  /* "pysam/calignmentfile.pyx":3521
  *     property is_head:
  *         def __get__(self):
  *             return self._is_head             # <<<<<<<<<<<<<<
@@ -34421,13 +37672,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(struct
  *         def __get__(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->_is_head); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3315; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->_is_head); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3314
+  /* "pysam/calignmentfile.pyx":3520
  *             return self._is_del
  *     property is_head:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -34438,7 +37689,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(struct
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.is_head.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupRead.is_head.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34447,28 +37698,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3317
+/* "pysam/calignmentfile.pyx":3523
  *             return self._is_head
  *     property is_tail:
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             return self._is_tail
- *     property level:
+ *     property is_refskip:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_7is_tail_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_7is_tail_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_7is_tail_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_7is_tail_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_7is_tail___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead_7is_tail___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_tail___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_7is_tail___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34477,34 +37728,34 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_tail___get__(struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3317);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3523);
 
-  /* "pysam/csamfile.pyx":3318
+  /* "pysam/calignmentfile.pyx":3524
  *     property is_tail:
  *         def __get__(self):
  *             return self._is_tail             # <<<<<<<<<<<<<<
- *     property level:
+ *     property is_refskip:
  *         def __get__(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->_is_tail); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->_is_tail); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3317
+  /* "pysam/calignmentfile.pyx":3523
  *             return self._is_head
  *     property is_tail:
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             return self._is_tail
- *     property level:
+ *     property is_refskip:
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.is_tail.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupRead.is_tail.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34513,28 +37764,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_tail___get__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3320
+/* "pysam/calignmentfile.pyx":3526
  *             return self._is_tail
- *     property level:
+ *     property is_refskip:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._level
+ *             return self._is_refskip
  * 
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_5level_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_5level_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_10is_refskip_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_10PileupRead_10is_refskip_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_5level___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_10PileupRead_10is_refskip___get__(((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5level___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_10PileupRead_10is_refskip___get__(struct __pyx_obj_5pysam_14calignmentfile_PileupRead *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34543,34 +37794,34 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5level___get__(struct __
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3320);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3526);
 
-  /* "pysam/csamfile.pyx":3321
- *     property level:
+  /* "pysam/calignmentfile.pyx":3527
+ *     property is_refskip:
  *         def __get__(self):
- *             return self._level             # <<<<<<<<<<<<<<
+ *             return self._is_refskip             # <<<<<<<<<<<<<<
  * 
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_level); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_refskip_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3320
+  /* "pysam/calignmentfile.pyx":3526
  *             return self._is_tail
- *     property level:
+ *     property is_refskip:
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._level
+ *             return self._is_refskip
  * 
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.level.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.PileupRead.is_refskip.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34579,7 +37830,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5level___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3337
+/* "pysam/calignmentfile.pyx":3543
  *     property tid:
  *         '''the chromosome ID as is defined in the header'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -34588,19 +37839,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5level___get__(struct __
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_3tid_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_3tid_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_3tid_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_3tid_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_7SNPCall_3tid___get__(((struct __pyx_obj_5pysam_14calignmentfile_SNPCall *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_3tid___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34609,9 +37860,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(struct __pyx_ob
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3337);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3543);
 
-  /* "pysam/csamfile.pyx":3338
+  /* "pysam/calignmentfile.pyx":3544
  *         '''the chromosome ID as is defined in the header'''
  *         def __get__(self):
  *             return self._tid             # <<<<<<<<<<<<<<
@@ -34619,13 +37870,13 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(struct __pyx_ob
  *     property pos:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3337
+  /* "pysam/calignmentfile.pyx":3543
  *     property tid:
  *         '''the chromosome ID as is defined in the header'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -34636,7 +37887,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(struct __pyx_ob
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.tid.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.SNPCall.tid.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34645,7 +37896,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3342
+/* "pysam/calignmentfile.pyx":3548
  *     property pos:
  *        '''nucleotide position of SNP.'''
  *        def __get__(self): return self._pos             # <<<<<<<<<<<<<<
@@ -34654,19 +37905,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(struct __pyx_ob
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_3pos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_3pos_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_3pos_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_3pos_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_7SNPCall_3pos___get__(((struct __pyx_obj_5pysam_14calignmentfile_SNPCall *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_3pos___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34675,9 +37926,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(struct __pyx_ob
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3342);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3548);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3342; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3548; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -34686,7 +37937,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(struct __pyx_ob
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.SNPCall.pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34695,7 +37946,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(struct __pyx_ob
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3346
+/* "pysam/calignmentfile.pyx":3552
  *     property reference_base:
  *        '''reference base at pos. ``N`` if no reference sequence supplied.'''
  *        def __get__(self): return from_string_and_size( &self._reference_base, 1 )             # <<<<<<<<<<<<<<
@@ -34704,19 +37955,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(struct __pyx_ob
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_14reference_base_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_14reference_base_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_14reference_base_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_14reference_base_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_7SNPCall_14reference_base___get__(((struct __pyx_obj_5pysam_14calignmentfile_SNPCall *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_14reference_base___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34725,9 +37976,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(str
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3346);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3552);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_from_string_and_size((&__pyx_v_self->_reference_base), 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_f_5pysam_14calignmentfile_from_string_and_size((&__pyx_v_self->_reference_base), 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -34736,7 +37987,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(str
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.reference_base.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.SNPCall.reference_base.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34745,7 +37996,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(str
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3350
+/* "pysam/calignmentfile.pyx":3556
  *     property genotype:
  *        '''the genotype called.'''
  *        def __get__(self): return from_string_and_size( &self._genotype, 1 )             # <<<<<<<<<<<<<<
@@ -34754,19 +38005,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(str
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_8genotype_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_8genotype_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_8genotype_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_8genotype_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_7SNPCall_8genotype___get__(((struct __pyx_obj_5pysam_14calignmentfile_SNPCall *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_8genotype___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34775,9 +38026,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(struct __p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3350);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3556);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_from_string_and_size((&__pyx_v_self->_genotype), 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_f_5pysam_14calignmentfile_from_string_and_size((&__pyx_v_self->_genotype), 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -34786,7 +38037,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(struct __p
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.genotype.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.SNPCall.genotype.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34795,7 +38046,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3354
+/* "pysam/calignmentfile.pyx":3560
  *     property consensus_quality:
  *        '''the genotype quality (Phred-scaled).'''
  *        def __get__(self): return self._consensus_quality             # <<<<<<<<<<<<<<
@@ -34804,19 +38055,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(struct __p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_17consensus_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_17consensus_quality_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_17consensus_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_17consensus_quality_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_17consensus_quality___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_7SNPCall_17consensus_quality___get__(((struct __pyx_obj_5pysam_14calignmentfile_SNPCall *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_17consensus_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_17consensus_quality___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34825,9 +38076,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_17consensus_quality___get__(
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3354);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3560);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_consensus_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_consensus_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -34836,7 +38087,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_17consensus_quality___get__(
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.consensus_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.SNPCall.consensus_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34845,28 +38096,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_17consensus_quality___get__(
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3358
- *     property snp_quality:
- *        '''the snp quality (Phred scaled) - probability of consensus being identical to reference sequence.'''
+/* "pysam/calignmentfile.pyx":3565
+ *        '''the snp quality (Phred scaled) - probability of consensus being
+ *        identical to reference sequence.'''
  *        def __get__(self): return self._snp_quality             # <<<<<<<<<<<<<<
  * 
  *     property mapping_quality:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_11snp_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_11snp_quality_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_11snp_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_11snp_quality_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_11snp_quality___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_7SNPCall_11snp_quality___get__(((struct __pyx_obj_5pysam_14calignmentfile_SNPCall *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_11snp_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_11snp_quality___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34875,9 +38126,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_11snp_quality___get__(struct
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3358);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3565);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_snp_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_snp_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -34886,7 +38137,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_11snp_quality___get__(struct
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.snp_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.SNPCall.snp_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34895,28 +38146,28 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_11snp_quality___get__(struct
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3362
- *     property mapping_quality:
- *        '''the root mean square (rms) of the mapping quality of all reads involved in the call.'''
+/* "pysam/calignmentfile.pyx":3570
+ *        '''the root mean square (rms) of the mapping quality of all reads
+ *        involved in the call.'''
  *        def __get__(self): return self._rms_mapping_quality             # <<<<<<<<<<<<<<
  * 
  *     property coverage:
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_15mapping_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_15mapping_quality_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_15mapping_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_15mapping_quality_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_7SNPCall_15mapping_quality___get__(((struct __pyx_obj_5pysam_14calignmentfile_SNPCall *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_15mapping_quality___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34925,9 +38176,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(st
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3362);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3570);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_rms_mapping_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_rms_mapping_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3570; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -34936,7 +38187,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(st
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.mapping_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.SNPCall.mapping_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34945,7 +38196,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(st
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3366
+/* "pysam/calignmentfile.pyx":3574
  *     property coverage:
  *        '''coverage or read depth - the number of reads involved in the call.'''
  *        def __get__(self): return self._coverage             # <<<<<<<<<<<<<<
@@ -34954,19 +38205,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(st
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_8coverage_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_8coverage_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_8coverage_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_8coverage_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_7SNPCall_8coverage___get__(((struct __pyx_obj_5pysam_14calignmentfile_SNPCall *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall_8coverage___get__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -34975,9 +38226,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(struct __p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3366);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 3574);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_coverage); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_coverage); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -34986,7 +38237,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(struct __p
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.coverage.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.SNPCall.coverage.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -34995,7 +38246,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3368
+/* "pysam/calignmentfile.pyx":3576
  *        def __get__(self): return self._coverage
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
@@ -35004,19 +38255,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(struct __p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_1__str__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_1__str__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_1__str__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_14calignmentfile_7SNPCall_1__str__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall___str__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_7SNPCall___str__(((struct __pyx_obj_5pysam_14calignmentfile_SNPCall *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_14calignmentfile_7SNPCall___str__(struct __pyx_obj_5pysam_14calignmentfile_SNPCall *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -35033,9 +38284,9 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 3368);
+  __Pyx_TraceCall("__str__", __pyx_f[0], 3576);
 
-  /* "pysam/csamfile.pyx":3370
+  /* "pysam/calignmentfile.pyx":3578
  *     def __str__(self):
  * 
  *         return "\t".join( map(str, (             # <<<<<<<<<<<<<<
@@ -35044,94 +38295,94 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5py
  */
   __Pyx_XDECREF(__pyx_r);
 
-  /* "pysam/csamfile.pyx":3371
+  /* "pysam/calignmentfile.pyx":3579
  * 
  *         return "\t".join( map(str, (
  *                     self.tid,             # <<<<<<<<<<<<<<
  *                     self.pos,
  *                     self.reference_base,
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
 
-  /* "pysam/csamfile.pyx":3372
+  /* "pysam/calignmentfile.pyx":3580
  *         return "\t".join( map(str, (
  *                     self.tid,
  *                     self.pos,             # <<<<<<<<<<<<<<
  *                     self.reference_base,
  *                     self.genotype,
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
 
-  /* "pysam/csamfile.pyx":3373
+  /* "pysam/calignmentfile.pyx":3581
  *                     self.tid,
  *                     self.pos,
  *                     self.reference_base,             # <<<<<<<<<<<<<<
  *                     self.genotype,
  *                     self.consensus_quality,
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_base); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_base); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
 
-  /* "pysam/csamfile.pyx":3374
+  /* "pysam/calignmentfile.pyx":3582
  *                     self.pos,
  *                     self.reference_base,
  *                     self.genotype,             # <<<<<<<<<<<<<<
  *                     self.consensus_quality,
  *                     self.snp_quality,
  */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_genotype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_genotype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
 
-  /* "pysam/csamfile.pyx":3375
+  /* "pysam/calignmentfile.pyx":3583
  *                     self.reference_base,
  *                     self.genotype,
  *                     self.consensus_quality,             # <<<<<<<<<<<<<<
  *                     self.snp_quality,
  *                     self.mapping_quality,
  */
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_consensus_quality); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_consensus_quality); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
 
-  /* "pysam/csamfile.pyx":3376
+  /* "pysam/calignmentfile.pyx":3584
  *                     self.genotype,
  *                     self.consensus_quality,
  *                     self.snp_quality,             # <<<<<<<<<<<<<<
  *                     self.mapping_quality,
  *                     self.coverage ) ) )
  */
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_snp_quality); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_snp_quality); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3584; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
 
-  /* "pysam/csamfile.pyx":3377
+  /* "pysam/calignmentfile.pyx":3585
  *                     self.consensus_quality,
  *                     self.snp_quality,
  *                     self.mapping_quality,             # <<<<<<<<<<<<<<
  *                     self.coverage ) ) )
  * 
  */
-  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mapping_quality); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mapping_quality); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
 
-  /* "pysam/csamfile.pyx":3378
+  /* "pysam/calignmentfile.pyx":3586
  *                     self.snp_quality,
  *                     self.mapping_quality,
  *                     self.coverage ) ) )             # <<<<<<<<<<<<<<
  * 
  * 
  */
-  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_coverage); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_coverage); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
 
-  /* "pysam/csamfile.pyx":3371
+  /* "pysam/calignmentfile.pyx":3579
  * 
  *         return "\t".join( map(str, (
  *                     self.tid,             # <<<<<<<<<<<<<<
  *                     self.pos,
  *                     self.reference_base,
  */
-  __pyx_t_9 = PyTuple_New(8); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = PyTuple_New(8); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
   PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
@@ -35158,14 +38409,14 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5py
   __pyx_t_7 = 0;
   __pyx_t_8 = 0;
 
-  /* "pysam/csamfile.pyx":3370
+  /* "pysam/calignmentfile.pyx":3578
  *     def __str__(self):
  * 
  *         return "\t".join( map(str, (             # <<<<<<<<<<<<<<
  *                     self.tid,
  *                     self.pos,
  */
-  __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
   PyTuple_SET_ITEM(__pyx_t_8, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
@@ -35173,17 +38424,17 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5py
   PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_9);
   __Pyx_GIVEREF(__pyx_t_9);
   __pyx_t_9 = 0;
-  __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
   __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-  __pyx_t_8 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyString_Join(__pyx_kp_s__43, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   __pyx_r = __pyx_t_8;
   __pyx_t_8 = 0;
   goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3368
+  /* "pysam/calignmentfile.pyx":3576
  *        def __get__(self): return self._coverage
  * 
  *     def __str__(self):             # <<<<<<<<<<<<<<
@@ -35202,7 +38453,7 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5py
   __Pyx_XDECREF(__pyx_t_7);
   __Pyx_XDECREF(__pyx_t_8);
   __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.SNPCall.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -35211,19 +38462,19 @@ static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3391
+/* "pysam/calignmentfile.pyx":3599
  *     """
  * 
- *     def __init__(self, Samfile samfile, int reopen=True):             # <<<<<<<<<<<<<<
+ *     def __init__(self, AlignmentFile samfile, int multiple_iterators=True):             # <<<<<<<<<<<<<<
  * 
  *         # makes sure that samfile stays alive as long as this
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  int __pyx_v_reopen;
+static int __pyx_pw_5pysam_14calignmentfile_12IndexedReads_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_14calignmentfile_12IndexedReads_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile = 0;
+  int __pyx_v_multiple_iterators;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -35231,7 +38482,7 @@ static int __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__(PyObject *__pyx_v_
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_reopen,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_multiple_iterators,0};
     PyObject* values[2] = {0,0};
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
@@ -35249,12 +38500,12 @@ static int __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__(PyObject *__pyx_v_
         else goto __pyx_L5_argtuple_error;
         case  1:
         if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
           if (value) { values[1] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3599; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -35264,23 +38515,23 @@ static int __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__(PyObject *__pyx_v_
         default: goto __pyx_L5_argtuple_error;
       }
     }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
+    __pyx_v_samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)values[0]);
     if (values[1]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_multiple_iterators = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_multiple_iterators == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3599; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
-      __pyx_v_reopen = ((int)1);
+      __pyx_v_multiple_iterators = ((int)1);
     }
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3599; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IndexedReads.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.calignmentfile.IndexedReads.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_12IndexedReads___init__(((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)__pyx_v_self), __pyx_v_samfile, __pyx_v_reopen);
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_14calignmentfile_AlignmentFile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3599; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12IndexedReads___init__(((struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *)__pyx_v_self), __pyx_v_samfile, __pyx_v_multiple_iterators);
 
   /* function exit code */
   goto __pyx_L0;
@@ -35291,21 +38542,22 @@ static int __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__(PyObject *__pyx_v_
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_8csamfile_12IndexedReads___init__(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen) {
+static int __pyx_pf_5pysam_14calignmentfile_12IndexedReads___init__(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *__pyx_v_self, struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *__pyx_v_samfile, int __pyx_v_multiple_iterators) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   char const *__pyx_t_2;
   htsFile *__pyx_t_3;
-  BGZF *__pyx_t_4;
+  bam_hdr_t *__pyx_t_4;
+  BGZF *__pyx_t_5;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 3391);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 3599);
 
-  /* "pysam/csamfile.pyx":3395
+  /* "pysam/calignmentfile.pyx":3603
  *         # makes sure that samfile stays alive as long as this
  *         # object is alive.
  *         self.samfile = samfile             # <<<<<<<<<<<<<<
@@ -35318,70 +38570,70 @@ static int __pyx_pf_5pysam_8csamfile_12IndexedReads___init__(struct __pyx_obj_5p
   __Pyx_DECREF(((PyObject *)__pyx_v_self->samfile));
   __pyx_v_self->samfile = __pyx_v_samfile;
 
-  /* "pysam/csamfile.pyx":3397
+  /* "pysam/calignmentfile.pyx":3605
  *         self.samfile = samfile
  * 
  *         assert samfile.isbam, "can only IndexReads on bam files"             # <<<<<<<<<<<<<<
  * 
- *         # reopen the file - note that this makes the iterator
+ *         # multiple_iterators the file - note that this makes the iterator
  */
   #ifndef CYTHON_WITHOUT_ASSERTIONS
   if (unlikely(!Py_OptimizeFlag)) {
     if (unlikely(!(__pyx_v_samfile->isbam != 0))) {
       PyErr_SetObject(PyExc_AssertionError, __pyx_kp_s_can_only_IndexReads_on_bam_files);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
   #endif
 
-  /* "pysam/csamfile.pyx":3401
- *         # reopen the file - note that this makes the iterator
+  /* "pysam/calignmentfile.pyx":3609
+ *         # multiple_iterators the file - note that this makes the iterator
  *         # slow and causes pileup to slow down significantly.
- *         if reopen:             # <<<<<<<<<<<<<<
+ *         if multiple_iterators:             # <<<<<<<<<<<<<<
  *             self.htsfile = hts_open(samfile._filename, 'r')
  *             assert self.htsfile != NULL
  */
-  __pyx_t_1 = (__pyx_v_reopen != 0);
+  __pyx_t_1 = (__pyx_v_multiple_iterators != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":3402
+    /* "pysam/calignmentfile.pyx":3610
  *         # slow and causes pileup to slow down significantly.
- *         if reopen:
+ *         if multiple_iterators:
  *             self.htsfile = hts_open(samfile._filename, 'r')             # <<<<<<<<<<<<<<
  *             assert self.htsfile != NULL
  *             # read header - required for accurate positioning
  */
-    __pyx_t_2 = __Pyx_PyObject_AsString(__pyx_v_samfile->_filename); if (unlikely((!__pyx_t_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3402; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_AsString(__pyx_v_samfile->_filename); if (unlikely((!__pyx_t_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3610; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_self->htsfile = hts_open(__pyx_t_2, __pyx_k_r);
 
-    /* "pysam/csamfile.pyx":3403
- *         if reopen:
+    /* "pysam/calignmentfile.pyx":3611
+ *         if multiple_iterators:
  *             self.htsfile = hts_open(samfile._filename, 'r')
  *             assert self.htsfile != NULL             # <<<<<<<<<<<<<<
  *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)
+ *             self.header = sam_hdr_read(self.htsfile)
  */
     #ifndef CYTHON_WITHOUT_ASSERTIONS
     if (unlikely(!Py_OptimizeFlag)) {
       if (unlikely(!((__pyx_v_self->htsfile != NULL) != 0))) {
         PyErr_SetNone(PyExc_AssertionError);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3403; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
     }
     #endif
 
-    /* "pysam/csamfile.pyx":3405
+    /* "pysam/calignmentfile.pyx":3613
  *             assert self.htsfile != NULL
  *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
+ *             self.header = sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
  *             self.owns_samfile = True
  *         else:
  */
-    sam_hdr_read(__pyx_v_self->htsfile);
+    __pyx_v_self->header = sam_hdr_read(__pyx_v_self->htsfile);
 
-    /* "pysam/csamfile.pyx":3406
+    /* "pysam/calignmentfile.pyx":3614
  *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)
+ *             self.header = sam_hdr_read(self.htsfile)
  *             self.owns_samfile = True             # <<<<<<<<<<<<<<
  *         else:
  *             self.htsfile = self.samfile.htsfile
@@ -35391,19 +38643,29 @@ static int __pyx_pf_5pysam_8csamfile_12IndexedReads___init__(struct __pyx_obj_5p
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":3408
+    /* "pysam/calignmentfile.pyx":3616
  *             self.owns_samfile = True
  *         else:
  *             self.htsfile = self.samfile.htsfile             # <<<<<<<<<<<<<<
+ *             self.header = self.samfile.header
  *             self.owns_samfile = False
- * 
  */
     __pyx_t_3 = __pyx_v_self->samfile->htsfile;
     __pyx_v_self->htsfile = __pyx_t_3;
 
-    /* "pysam/csamfile.pyx":3409
+    /* "pysam/calignmentfile.pyx":3617
  *         else:
  *             self.htsfile = self.samfile.htsfile
+ *             self.header = self.samfile.header             # <<<<<<<<<<<<<<
+ *             self.owns_samfile = False
+ * 
+ */
+    __pyx_t_4 = __pyx_v_self->samfile->header;
+    __pyx_v_self->header = __pyx_t_4;
+
+    /* "pysam/calignmentfile.pyx":3618
+ *             self.htsfile = self.samfile.htsfile
+ *             self.header = self.samfile.header
  *             self.owns_samfile = False             # <<<<<<<<<<<<<<
  * 
  *         # TODO: BAM file specific
@@ -35412,445 +38674,1099 @@ static int __pyx_pf_5pysam_8csamfile_12IndexedReads___init__(struct __pyx_obj_5p
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":3412
+  /* "pysam/calignmentfile.pyx":3621
  * 
  *         # TODO: BAM file specific
  *         self.fp = self.htsfile.fp.bgzf             # <<<<<<<<<<<<<<
  * 
  *     def build(self):
  */
-  __pyx_t_4 = __pyx_v_self->htsfile->fp.bgzf;
-  __pyx_v_self->fp = __pyx_t_4;
+  __pyx_t_5 = __pyx_v_self->htsfile->fp.bgzf;
+  __pyx_v_self->fp = __pyx_t_5;
+
+  /* "pysam/calignmentfile.pyx":3599
+ *     """
+ * 
+ *     def __init__(self, AlignmentFile samfile, int multiple_iterators=True):             # <<<<<<<<<<<<<<
+ * 
+ *         # makes sure that samfile stays alive as long as this
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("pysam.calignmentfile.IndexedReads.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3623
+ *         self.fp = self.htsfile.fp.bgzf
+ * 
+ *     def build(self):             # <<<<<<<<<<<<<<
+ *         '''build index.'''
+ * 
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12IndexedReads_3build(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_12IndexedReads_2build[] = "IndexedReads.build(self)\nbuild index.";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12IndexedReads_3build(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("build (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12IndexedReads_2build(((struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12IndexedReads_2build(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *__pyx_v_self) {
+  int __pyx_v_ret;
+  bam1_t *__pyx_v_b;
+  uint64_t __pyx_v_pos;
+  PyObject *__pyx_v_qname = NULL;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  int __pyx_t_4;
+  int __pyx_t_5;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("build", 0);
+  __Pyx_TraceCall("build", __pyx_f[0], 3623);
+
+  /* "pysam/calignmentfile.pyx":3626
+ *         '''build index.'''
+ * 
+ *         self.index = collections.defaultdict(list)             # <<<<<<<<<<<<<<
+ * 
+ *         # this method will start indexing from the current file position
+ */
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_collections); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_defaultdict); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyList_Type))));
+  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)((PyObject*)(&PyList_Type))));
+  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyList_Type))));
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_GIVEREF(__pyx_t_3);
+  __Pyx_GOTREF(__pyx_v_self->index);
+  __Pyx_DECREF(__pyx_v_self->index);
+  __pyx_v_self->index = __pyx_t_3;
+  __pyx_t_3 = 0;
+
+  /* "pysam/calignmentfile.pyx":3630
+ *         # this method will start indexing from the current file position
+ *         # if you decide
+ *         cdef int ret = 1             # <<<<<<<<<<<<<<
+ *         cdef bam1_t * b = <bam1_t*>calloc(1, sizeof( bam1_t))
+ * 
+ */
+  __pyx_v_ret = 1;
+
+  /* "pysam/calignmentfile.pyx":3631
+ *         # if you decide
+ *         cdef int ret = 1
+ *         cdef bam1_t * b = <bam1_t*>calloc(1, sizeof( bam1_t))             # <<<<<<<<<<<<<<
+ * 
+ *         cdef uint64_t pos
+ */
+  __pyx_v_b = ((bam1_t *)calloc(1, (sizeof(bam1_t))));
+
+  /* "pysam/calignmentfile.pyx":3635
+ *         cdef uint64_t pos
+ * 
+ *         while ret > 0:             # <<<<<<<<<<<<<<
+ *             pos = bgzf_tell(self.fp)
+ *             ret = sam_read1(self.htsfile,
+ */
+  while (1) {
+    __pyx_t_4 = ((__pyx_v_ret > 0) != 0);
+    if (!__pyx_t_4) break;
+
+    /* "pysam/calignmentfile.pyx":3636
+ * 
+ *         while ret > 0:
+ *             pos = bgzf_tell(self.fp)             # <<<<<<<<<<<<<<
+ *             ret = sam_read1(self.htsfile,
+ *                             self.samfile.header,
+ */
+    __pyx_v_pos = bgzf_tell(__pyx_v_self->fp);
+
+    /* "pysam/calignmentfile.pyx":3637
+ *         while ret > 0:
+ *             pos = bgzf_tell(self.fp)
+ *             ret = sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
+ *                             self.samfile.header,
+ *                             b)
+ */
+    __pyx_v_ret = sam_read1(__pyx_v_self->htsfile, __pyx_v_self->samfile->header, __pyx_v_b);
+
+    /* "pysam/calignmentfile.pyx":3640
+ *                             self.samfile.header,
+ *                             b)
+ *             if ret > 0:             # <<<<<<<<<<<<<<
+ *                 qname = _charptr_to_str(pysam_bam_get_qname(b))
+ *                 self.index[qname].append(pos)
+ */
+    __pyx_t_4 = ((__pyx_v_ret > 0) != 0);
+    if (__pyx_t_4) {
+
+      /* "pysam/calignmentfile.pyx":3641
+ *                             b)
+ *             if ret > 0:
+ *                 qname = _charptr_to_str(pysam_bam_get_qname(b))             # <<<<<<<<<<<<<<
+ *                 self.index[qname].append(pos)
+ * 
+ */
+      __pyx_t_3 = __pyx_f_5pysam_14calignmentfile__charptr_to_str(pysam_bam_get_qname(__pyx_v_b)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      __Pyx_XDECREF_SET(__pyx_v_qname, __pyx_t_3);
+      __pyx_t_3 = 0;
+
+      /* "pysam/calignmentfile.pyx":3642
+ *             if ret > 0:
+ *                 qname = _charptr_to_str(pysam_bam_get_qname(b))
+ *                 self.index[qname].append(pos)             # <<<<<<<<<<<<<<
+ * 
+ *         bam_destroy1(b)
+ */
+      __pyx_t_3 = PyObject_GetItem(__pyx_v_self->index, __pyx_v_qname); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3642; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __Pyx_GOTREF(__pyx_t_3);
+      __pyx_t_1 = __Pyx_PyInt_From_uint64_t(__pyx_v_pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3642; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_1);
+      __pyx_t_5 = __Pyx_PyObject_Append(__pyx_t_3, __pyx_t_1); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3642; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      goto __pyx_L5;
+    }
+    __pyx_L5:;
+  }
+
+  /* "pysam/calignmentfile.pyx":3644
+ *                 self.index[qname].append(pos)
+ * 
+ *         bam_destroy1(b)             # <<<<<<<<<<<<<<
+ * 
+ *     def find(self, query_name):
+ */
+  bam_destroy1(__pyx_v_b);
+
+  /* "pysam/calignmentfile.pyx":3623
+ *         self.fp = self.htsfile.fp.bgzf
+ * 
+ *     def build(self):             # <<<<<<<<<<<<<<
+ *         '''build index.'''
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.calignmentfile.IndexedReads.build", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_qname);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3646
+ *         bam_destroy1(b)
+ * 
+ *     def find(self, query_name):             # <<<<<<<<<<<<<<
+ *         '''find *query_name* in index.
+ * 
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12IndexedReads_5find(PyObject *__pyx_v_self, PyObject *__pyx_v_query_name); /*proto*/
+static char __pyx_doc_5pysam_14calignmentfile_12IndexedReads_4find[] = "IndexedReads.find(self, query_name)\nfind *query_name* in index.\n\n        Returns an iterator over all reads with query_name.\n\n        Raise a KeyError if the *query_name* is not in the index.\n        ";
+static PyObject *__pyx_pw_5pysam_14calignmentfile_12IndexedReads_5find(PyObject *__pyx_v_self, PyObject *__pyx_v_query_name) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("find (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_14calignmentfile_12IndexedReads_4find(((struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *)__pyx_v_self), ((PyObject *)__pyx_v_query_name));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_14calignmentfile_12IndexedReads_4find(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *__pyx_v_self, PyObject *__pyx_v_query_name) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  int __pyx_t_1;
+  int __pyx_t_2;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
+  PyObject *__pyx_t_5 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("find", 0);
+  __Pyx_TraceCall("find", __pyx_f[0], 3646);
+
+  /* "pysam/calignmentfile.pyx":3653
+ *         Raise a KeyError if the *query_name* is not in the index.
+ *         '''
+ *         if query_name in self.index:             # <<<<<<<<<<<<<<
+ *             return IteratorRowSelection(
+ *                 self.samfile,
+ */
+  __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_v_query_name, __pyx_v_self->index, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = (__pyx_t_1 != 0);
+  if (__pyx_t_2) {
+
+    /* "pysam/calignmentfile.pyx":3654
+ *         '''
+ *         if query_name in self.index:
+ *             return IteratorRowSelection(             # <<<<<<<<<<<<<<
+ *                 self.samfile,
+ *                 self.index[query_name],
+ */
+    __Pyx_XDECREF(__pyx_r);
+
+    /* "pysam/calignmentfile.pyx":3656
+ *             return IteratorRowSelection(
+ *                 self.samfile,
+ *                 self.index[query_name],             # <<<<<<<<<<<<<<
+ *                 multiple_iterators = False)
+ *         else:
+ */
+    __pyx_t_3 = PyObject_GetItem(__pyx_v_self->index, __pyx_v_query_name); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3656; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __Pyx_GOTREF(__pyx_t_3);
+
+    /* "pysam/calignmentfile.pyx":3654
+ *         '''
+ *         if query_name in self.index:
+ *             return IteratorRowSelection(             # <<<<<<<<<<<<<<
+ *                 self.samfile,
+ *                 self.index[query_name],
+ */
+    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3654; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_INCREF(((PyObject *)__pyx_v_self->samfile));
+    PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_v_self->samfile));
+    __Pyx_GIVEREF(((PyObject *)__pyx_v_self->samfile));
+    PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_3);
+    __Pyx_GIVEREF(__pyx_t_3);
+    __pyx_t_3 = 0;
+    __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3654; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+
+    /* "pysam/calignmentfile.pyx":3657
+ *                 self.samfile,
+ *                 self.index[query_name],
+ *                 multiple_iterators = False)             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise KeyError("read %s not found" % query_name)
+ */
+    if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_multiple_iterators, Py_False) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3654; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/calignmentfile.pyx":3654
+ *         '''
+ *         if query_name in self.index:
+ *             return IteratorRowSelection(             # <<<<<<<<<<<<<<
+ *                 self.samfile,
+ *                 self.index[query_name],
+ */
+    __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_14calignmentfile_IteratorRowSelection)), __pyx_t_4, __pyx_t_3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3654; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __pyx_r = __pyx_t_5;
+    __pyx_t_5 = 0;
+    goto __pyx_L0;
+  }
+  /*else*/ {
+
+    /* "pysam/calignmentfile.pyx":3659
+ *                 multiple_iterators = False)
+ *         else:
+ *             raise KeyError("read %s not found" % query_name)             # <<<<<<<<<<<<<<
+ * 
+ *     def __dealloc__(self):
+ */
+    __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_read_s_not_found, __pyx_v_query_name); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_5);
+    __Pyx_GIVEREF(__pyx_t_5);
+    __pyx_t_5 = 0;
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __Pyx_Raise(__pyx_t_5, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
+
+  /* "pysam/calignmentfile.pyx":3646
+ *         bam_destroy1(b)
+ * 
+ *     def find(self, query_name):             # <<<<<<<<<<<<<<
+ *         '''find *query_name* in index.
+ * 
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_5);
+  __Pyx_AddTraceback("pysam.calignmentfile.IndexedReads.find", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/calignmentfile.pyx":3661
+ *             raise KeyError("read %s not found" % query_name)
+ * 
+ *     def __dealloc__(self):             # <<<<<<<<<<<<<<
+ *         if self.owns_samfile:
+ *             hts_close(self.htsfile)
+ */
+
+/* Python wrapper */
+static void __pyx_pw_5pysam_14calignmentfile_12IndexedReads_7__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_14calignmentfile_12IndexedReads_7__dealloc__(PyObject *__pyx_v_self) {
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
+  __pyx_pf_5pysam_14calignmentfile_12IndexedReads_6__dealloc__(((struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+}
+
+static void __pyx_pf_5pysam_14calignmentfile_12IndexedReads_6__dealloc__(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *__pyx_v_self) {
+  __Pyx_RefNannyDeclarations
+  int __pyx_t_1;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__dealloc__", 0);
+  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 3661);
+
+  /* "pysam/calignmentfile.pyx":3662
+ * 
+ *     def __dealloc__(self):
+ *         if self.owns_samfile:             # <<<<<<<<<<<<<<
+ *             hts_close(self.htsfile)
+ *             bam_hdr_destroy(self.header)
+ */
+  __pyx_t_1 = (__pyx_v_self->owns_samfile != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/calignmentfile.pyx":3663
+ *     def __dealloc__(self):
+ *         if self.owns_samfile:
+ *             hts_close(self.htsfile)             # <<<<<<<<<<<<<<
+ *             bam_hdr_destroy(self.header)
+ * 
+ */
+    hts_close(__pyx_v_self->htsfile);
+
+    /* "pysam/calignmentfile.pyx":3664
+ *         if self.owns_samfile:
+ *             hts_close(self.htsfile)
+ *             bam_hdr_destroy(self.header)             # <<<<<<<<<<<<<<
+ * 
+ * __all__ = ["AlignmentFile",
+ */
+    bam_hdr_destroy(__pyx_v_self->header);
+    goto __pyx_L3;
+  }
+  __pyx_L3:;
+
+  /* "pysam/calignmentfile.pyx":3661
+ *             raise KeyError("read %s not found" % query_name)
+ * 
+ *     def __dealloc__(self):             # <<<<<<<<<<<<<<
+ *         if self.owns_samfile:
+ *             hts_close(self.htsfile)
+ */
+
+  /* function exit code */
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+}
+
+/* "array.pxd":91
+ *             __data_union data
+ * 
+ *         def __getbuffer__(self, Py_buffer* info, int flags):             # <<<<<<<<<<<<<<
+ *             # This implementation of getbuffer is geared towards Cython
+ *             # requirements, and does not yet fullfill the PEP.
+ */
+
+/* Python wrapper */
+static CYTHON_UNUSED int __pyx_pw_7cpython_5array_5array_1__getbuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); /*proto*/
+static CYTHON_UNUSED int __pyx_pw_7cpython_5array_5array_1__getbuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__getbuffer__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_7cpython_5array_5array___getbuffer__(((arrayobject *)__pyx_v_self), ((Py_buffer *)__pyx_v_info), ((int)__pyx_v_flags));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_7cpython_5array_5array___getbuffer__(arrayobject *__pyx_v_self, Py_buffer *__pyx_v_info, CYTHON_UNUSED int __pyx_v_flags) {
+  PyObject *__pyx_v_item_count = NULL;
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  char *__pyx_t_2;
+  int __pyx_t_3;
+  PyObject *__pyx_t_4 = NULL;
+  Py_ssize_t __pyx_t_5;
+  int __pyx_t_6;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("__getbuffer__", 0);
+  if (__pyx_v_info != NULL) {
+    __pyx_v_info->obj = Py_None; __Pyx_INCREF(Py_None);
+    __Pyx_GIVEREF(__pyx_v_info->obj);
+  }
+  __Pyx_TraceCall("__getbuffer__", __pyx_f[1], 91);
+
+  /* "array.pxd":96
+ *             # In particular strided access is always provided regardless
+ *             # of flags
+ *             item_count = Py_SIZE(self)             # <<<<<<<<<<<<<<
+ * 
+ *             info.suboffsets = NULL
+ */
+  __pyx_t_1 = PyInt_FromSsize_t(Py_SIZE(((PyObject *)__pyx_v_self))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_v_item_count = __pyx_t_1;
+  __pyx_t_1 = 0;
+
+  /* "array.pxd":98
+ *             item_count = Py_SIZE(self)
+ * 
+ *             info.suboffsets = NULL             # <<<<<<<<<<<<<<
+ *             info.buf = self.data.as_chars
+ *             info.readonly = 0
+ */
+  __pyx_v_info->suboffsets = NULL;
+
+  /* "array.pxd":99
+ * 
+ *             info.suboffsets = NULL
+ *             info.buf = self.data.as_chars             # <<<<<<<<<<<<<<
+ *             info.readonly = 0
+ *             info.ndim = 1
+ */
+  __pyx_t_2 = __pyx_v_self->data.as_chars;
+  __pyx_v_info->buf = __pyx_t_2;
+
+  /* "array.pxd":100
+ *             info.suboffsets = NULL
+ *             info.buf = self.data.as_chars
+ *             info.readonly = 0             # <<<<<<<<<<<<<<
+ *             info.ndim = 1
+ *             info.itemsize = self.ob_descr.itemsize   # e.g. sizeof(float)
+ */
+  __pyx_v_info->readonly = 0;
+
+  /* "array.pxd":101
+ *             info.buf = self.data.as_chars
+ *             info.readonly = 0
+ *             info.ndim = 1             # <<<<<<<<<<<<<<
+ *             info.itemsize = self.ob_descr.itemsize   # e.g. sizeof(float)
+ *             info.len = info.itemsize * item_count
+ */
+  __pyx_v_info->ndim = 1;
+
+  /* "array.pxd":102
+ *             info.readonly = 0
+ *             info.ndim = 1
+ *             info.itemsize = self.ob_descr.itemsize   # e.g. sizeof(float)             # <<<<<<<<<<<<<<
+ *             info.len = info.itemsize * item_count
+ * 
+ */
+  __pyx_t_3 = __pyx_v_self->ob_descr->itemsize;
+  __pyx_v_info->itemsize = __pyx_t_3;
+
+  /* "array.pxd":103
+ *             info.ndim = 1
+ *             info.itemsize = self.ob_descr.itemsize   # e.g. sizeof(float)
+ *             info.len = info.itemsize * item_count             # <<<<<<<<<<<<<<
+ * 
+ *             info.shape = <Py_ssize_t*> PyMem_Malloc(sizeof(Py_ssize_t) + 2)
+ */
+  __pyx_t_1 = PyInt_FromSsize_t(__pyx_v_info->itemsize); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 103; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_4 = PyNumber_Multiply(__pyx_t_1, __pyx_v_item_count); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 103; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_5 = __Pyx_PyIndex_AsSsize_t(__pyx_t_4); if (unlikely((__pyx_t_5 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 103; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+  __pyx_v_info->len = __pyx_t_5;
+
+  /* "array.pxd":105
+ *             info.len = info.itemsize * item_count
+ * 
+ *             info.shape = <Py_ssize_t*> PyMem_Malloc(sizeof(Py_ssize_t) + 2)             # <<<<<<<<<<<<<<
+ *             if not info.shape:
+ *                 raise MemoryError()
+ */
+  __pyx_v_info->shape = ((Py_ssize_t *)PyMem_Malloc(((sizeof(Py_ssize_t)) + 2)));
+
+  /* "array.pxd":106
+ * 
+ *             info.shape = <Py_ssize_t*> PyMem_Malloc(sizeof(Py_ssize_t) + 2)
+ *             if not info.shape:             # <<<<<<<<<<<<<<
+ *                 raise MemoryError()
+ *             info.shape[0] = item_count      # constant regardless of resizing
+ */
+  __pyx_t_6 = ((!(__pyx_v_info->shape != 0)) != 0);
+  if (__pyx_t_6) {
+
+    /* "array.pxd":107
+ *             info.shape = <Py_ssize_t*> PyMem_Malloc(sizeof(Py_ssize_t) + 2)
+ *             if not info.shape:
+ *                 raise MemoryError()             # <<<<<<<<<<<<<<
+ *             info.shape[0] = item_count      # constant regardless of resizing
+ *             info.strides = &info.itemsize
+ */
+    PyErr_NoMemory(); {__pyx_filename = __pyx_f[1]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
+
+  /* "array.pxd":108
+ *             if not info.shape:
+ *                 raise MemoryError()
+ *             info.shape[0] = item_count      # constant regardless of resizing             # <<<<<<<<<<<<<<
+ *             info.strides = &info.itemsize
+ * 
+ */
+  __pyx_t_5 = __Pyx_PyIndex_AsSsize_t(__pyx_v_item_count); if (unlikely((__pyx_t_5 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  (__pyx_v_info->shape[0]) = __pyx_t_5;
+
+  /* "array.pxd":109
+ *                 raise MemoryError()
+ *             info.shape[0] = item_count      # constant regardless of resizing
+ *             info.strides = &info.itemsize             # <<<<<<<<<<<<<<
+ * 
+ *             info.format = <char*> (info.shape + 1)
+ */
+  __pyx_v_info->strides = (&__pyx_v_info->itemsize);
+
+  /* "array.pxd":111
+ *             info.strides = &info.itemsize
+ * 
+ *             info.format = <char*> (info.shape + 1)             # <<<<<<<<<<<<<<
+ *             info.format[0] = self.ob_descr.typecode
+ *             info.format[1] = 0
+ */
+  __pyx_v_info->format = ((char *)(__pyx_v_info->shape + 1));
+
+  /* "array.pxd":112
+ * 
+ *             info.format = <char*> (info.shape + 1)
+ *             info.format[0] = self.ob_descr.typecode             # <<<<<<<<<<<<<<
+ *             info.format[1] = 0
+ *             info.obj = self
+ */
+  __pyx_t_3 = __pyx_v_self->ob_descr->typecode;
+  (__pyx_v_info->format[0]) = __pyx_t_3;
+
+  /* "array.pxd":113
+ *             info.format = <char*> (info.shape + 1)
+ *             info.format[0] = self.ob_descr.typecode
+ *             info.format[1] = 0             # <<<<<<<<<<<<<<
+ *             info.obj = self
+ * 
+ */
+  (__pyx_v_info->format[1]) = 0;
+
+  /* "array.pxd":114
+ *             info.format[0] = self.ob_descr.typecode
+ *             info.format[1] = 0
+ *             info.obj = self             # <<<<<<<<<<<<<<
+ * 
+ *         def __releasebuffer__(self, Py_buffer* info):
+ */
+  __Pyx_INCREF(((PyObject *)__pyx_v_self));
+  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
+  __Pyx_GOTREF(__pyx_v_info->obj);
+  __Pyx_DECREF(__pyx_v_info->obj);
+  __pyx_v_info->obj = ((PyObject *)__pyx_v_self);
 
-  /* "pysam/csamfile.pyx":3391
- *     """
- * 
- *     def __init__(self, Samfile samfile, int reopen=True):             # <<<<<<<<<<<<<<
+  /* "array.pxd":91
+ *             __data_union data
  * 
- *         # makes sure that samfile stays alive as long as this
+ *         def __getbuffer__(self, Py_buffer* info, int flags):             # <<<<<<<<<<<<<<
+ *             # This implementation of getbuffer is geared towards Cython
+ *             # requirements, and does not yet fullfill the PEP.
  */
 
   /* function exit code */
   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IndexedReads.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_AddTraceback("cpython.array.array.__getbuffer__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
+  if (__pyx_v_info != NULL && __pyx_v_info->obj != NULL) {
+    __Pyx_GOTREF(__pyx_v_info->obj);
+    __Pyx_DECREF(__pyx_v_info->obj); __pyx_v_info->obj = NULL;
+  }
+  goto __pyx_L2;
   __pyx_L0:;
+  if (__pyx_v_info != NULL && __pyx_v_info->obj == Py_None) {
+    __Pyx_GOTREF(Py_None);
+    __Pyx_DECREF(Py_None); __pyx_v_info->obj = NULL;
+  }
+  __pyx_L2:;
+  __Pyx_XDECREF(__pyx_v_item_count);
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3414
- *         self.fp = self.htsfile.fp.bgzf
+/* "array.pxd":116
+ *             info.obj = self
  * 
- *     def build(self):             # <<<<<<<<<<<<<<
- *         '''build index.'''
+ *         def __releasebuffer__(self, Py_buffer* info):             # <<<<<<<<<<<<<<
+ *             PyMem_Free(info.shape)
  * 
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_12IndexedReads_3build(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_12IndexedReads_2build[] = "IndexedReads.build(self)\nbuild index.";
-static PyObject *__pyx_pw_5pysam_8csamfile_12IndexedReads_3build(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
+static CYTHON_UNUSED void __pyx_pw_7cpython_5array_5array_3__releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info); /*proto*/
+static CYTHON_UNUSED void __pyx_pw_7cpython_5array_5array_3__releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info) {
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("build (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_12IndexedReads_2build(((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)__pyx_v_self));
+  __Pyx_RefNannySetupContext("__releasebuffer__ (wrapper)", 0);
+  __pyx_pf_7cpython_5array_5array_2__releasebuffer__(((arrayobject *)__pyx_v_self), ((Py_buffer *)__pyx_v_info));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
-  return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_12IndexedReads_2build(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self) {
-  int __pyx_v_ret;
-  bam1_t *__pyx_v_b;
-  uint64_t __pyx_v_pos;
-  PyObject *__pyx_v_qname = NULL;
-  PyObject *__pyx_r = NULL;
+static void __pyx_pf_7cpython_5array_5array_2__releasebuffer__(CYTHON_UNUSED arrayobject *__pyx_v_self, Py_buffer *__pyx_v_info) {
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("build", 0);
-  __Pyx_TraceCall("build", __pyx_f[0], 3414);
+  __Pyx_RefNannySetupContext("__releasebuffer__", 0);
+  __Pyx_TraceCall("__releasebuffer__", __pyx_f[1], 116);
 
-  /* "pysam/csamfile.pyx":3417
- *         '''build index.'''
+  /* "array.pxd":117
  * 
- *         self.index = collections.defaultdict(list)             # <<<<<<<<<<<<<<
+ *         def __releasebuffer__(self, Py_buffer* info):
+ *             PyMem_Free(info.shape)             # <<<<<<<<<<<<<<
  * 
- *         # this method will start indexing from the current file position
+ *     array newarrayobject(PyTypeObject* type, Py_ssize_t size, arraydescr *descr)
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_collections); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_defaultdict); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyList_Type))));
-  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)((PyObject*)(&PyList_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyList_Type))));
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_GIVEREF(__pyx_t_3);
-  __Pyx_GOTREF(__pyx_v_self->index);
-  __Pyx_DECREF(__pyx_v_self->index);
-  __pyx_v_self->index = __pyx_t_3;
-  __pyx_t_3 = 0;
+  PyMem_Free(__pyx_v_info->shape);
 
-  /* "pysam/csamfile.pyx":3421
- *         # this method will start indexing from the current file position
- *         # if you decide
- *         cdef int ret = 1             # <<<<<<<<<<<<<<
- *         cdef bam1_t * b = <bam1_t*>calloc(1, sizeof( bam1_t))
+  /* "array.pxd":116
+ *             info.obj = self
  * 
- */
-  __pyx_v_ret = 1;
-
-  /* "pysam/csamfile.pyx":3422
- *         # if you decide
- *         cdef int ret = 1
- *         cdef bam1_t * b = <bam1_t*>calloc(1, sizeof( bam1_t))             # <<<<<<<<<<<<<<
+ *         def __releasebuffer__(self, Py_buffer* info):             # <<<<<<<<<<<<<<
+ *             PyMem_Free(info.shape)
  * 
- *         cdef uint64_t pos
  */
-  __pyx_v_b = ((bam1_t *)calloc(1, (sizeof(bam1_t))));
 
-  /* "pysam/csamfile.pyx":3426
- *         cdef uint64_t pos
- * 
- *         while ret > 0:             # <<<<<<<<<<<<<<
- *             pos = bgzf_tell(self.fp)
- *             ret = sam_read1(self.htsfile,
- */
-  while (1) {
-    __pyx_t_4 = ((__pyx_v_ret > 0) != 0);
-    if (!__pyx_t_4) break;
+  /* function exit code */
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+}
 
-    /* "pysam/csamfile.pyx":3427
+/* "array.pxd":128
  * 
- *         while ret > 0:
- *             pos = bgzf_tell(self.fp)             # <<<<<<<<<<<<<<
- *             ret = sam_read1(self.htsfile,
- *                             self.samfile.header,
+ * 
+ * cdef inline array clone(array template, Py_ssize_t length, bint zero):             # <<<<<<<<<<<<<<
+ *     """ fast creation of a new array, given a template array.
+ *     type will be same as template.
  */
-    __pyx_v_pos = bgzf_tell(__pyx_v_self->fp);
 
-    /* "pysam/csamfile.pyx":3428
- *         while ret > 0:
- *             pos = bgzf_tell(self.fp)
- *             ret = sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                             self.samfile.header,
- *                             b)
- */
-    __pyx_v_ret = sam_read1(__pyx_v_self->htsfile, __pyx_v_self->samfile->header, __pyx_v_b);
+static CYTHON_INLINE arrayobject *__pyx_f_7cpython_5array_clone(arrayobject *__pyx_v_template, Py_ssize_t __pyx_v_length, int __pyx_v_zero) {
+  arrayobject *__pyx_v_op = NULL;
+  arrayobject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_t_2;
+  int __pyx_t_3;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("clone", 0);
+  __Pyx_TraceCall("clone", __pyx_f[1], 128);
 
-    /* "pysam/csamfile.pyx":3431
- *                             self.samfile.header,
- *                             b)
- *             if ret > 0:             # <<<<<<<<<<<<<<
- *                 qname = _charptr_to_str(pysam_bam_get_qname(b))
- *                 self.index[qname].append(pos)
+  /* "array.pxd":132
+ *     type will be same as template.
+ *     if zero is true, new array will be initialized with zeroes."""
+ *     op = newarrayobject(Py_TYPE(template), length, template.ob_descr)             # <<<<<<<<<<<<<<
+ *     if zero and op is not None:
+ *         memset(op.data.as_chars, 0, length * op.ob_descr.itemsize)
  */
-    __pyx_t_4 = ((__pyx_v_ret > 0) != 0);
-    if (__pyx_t_4) {
+  __pyx_t_1 = ((PyObject *)newarrayobject(Py_TYPE(((PyObject *)__pyx_v_template)), __pyx_v_length, __pyx_v_template->ob_descr)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_v_op = ((arrayobject *)__pyx_t_1);
+  __pyx_t_1 = 0;
 
-      /* "pysam/csamfile.pyx":3432
- *                             b)
- *             if ret > 0:
- *                 qname = _charptr_to_str(pysam_bam_get_qname(b))             # <<<<<<<<<<<<<<
- *                 self.index[qname].append(pos)
- * 
+  /* "array.pxd":133
+ *     if zero is true, new array will be initialized with zeroes."""
+ *     op = newarrayobject(Py_TYPE(template), length, template.ob_descr)
+ *     if zero and op is not None:             # <<<<<<<<<<<<<<
+ *         memset(op.data.as_chars, 0, length * op.ob_descr.itemsize)
+ *     return op
  */
-      __pyx_t_3 = __pyx_f_5pysam_8csamfile__charptr_to_str(pysam_bam_get_qname(__pyx_v_b)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_XDECREF_SET(__pyx_v_qname, __pyx_t_3);
-      __pyx_t_3 = 0;
+  if ((__pyx_v_zero != 0)) {
+    __pyx_t_2 = (((PyObject *)__pyx_v_op) != Py_None);
+    __pyx_t_3 = (__pyx_t_2 != 0);
+  } else {
+    __pyx_t_3 = (__pyx_v_zero != 0);
+  }
+  if (__pyx_t_3) {
 
-      /* "pysam/csamfile.pyx":3433
- *             if ret > 0:
- *                 qname = _charptr_to_str(pysam_bam_get_qname(b))
- *                 self.index[qname].append(pos)             # <<<<<<<<<<<<<<
+    /* "array.pxd":134
+ *     op = newarrayobject(Py_TYPE(template), length, template.ob_descr)
+ *     if zero and op is not None:
+ *         memset(op.data.as_chars, 0, length * op.ob_descr.itemsize)             # <<<<<<<<<<<<<<
+ *     return op
  * 
- *         bam_destroy1(b)
  */
-      __pyx_t_3 = PyObject_GetItem(__pyx_v_self->index, __pyx_v_qname); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3433; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_1 = __Pyx_PyInt_From_uint64_t(__pyx_v_pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_5 = __Pyx_PyObject_Append(__pyx_t_3, __pyx_t_1); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      goto __pyx_L5;
-    }
-    __pyx_L5:;
+    memset(__pyx_v_op->data.as_chars, 0, (__pyx_v_length * __pyx_v_op->ob_descr->itemsize));
+    goto __pyx_L3;
   }
+  __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":3435
- *                 self.index[qname].append(pos)
- * 
- *         bam_destroy1(b)             # <<<<<<<<<<<<<<
+  /* "array.pxd":135
+ *     if zero and op is not None:
+ *         memset(op.data.as_chars, 0, length * op.ob_descr.itemsize)
+ *     return op             # <<<<<<<<<<<<<<
  * 
- *     def find(self, qname):
+ * cdef inline array copy(array self):
  */
-  bam_destroy1(__pyx_v_b);
+  __Pyx_XDECREF(((PyObject *)__pyx_r));
+  __Pyx_INCREF(((PyObject *)__pyx_v_op));
+  __pyx_r = __pyx_v_op;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3414
- *         self.fp = self.htsfile.fp.bgzf
+  /* "array.pxd":128
  * 
- *     def build(self):             # <<<<<<<<<<<<<<
- *         '''build index.'''
  * 
+ * cdef inline array clone(array template, Py_ssize_t length, bint zero):             # <<<<<<<<<<<<<<
+ *     """ fast creation of a new array, given a template array.
+ *     type will be same as template.
  */
 
   /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.IndexedReads.build", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
+  __Pyx_AddTraceback("cpython.array.clone", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = 0;
   __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_qname);
-  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_XDECREF((PyObject *)__pyx_v_op);
+  __Pyx_XGIVEREF((PyObject *)__pyx_r);
   __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3437
- *         bam_destroy1(b)
+/* "array.pxd":137
+ *     return op
  * 
- *     def find(self, qname):             # <<<<<<<<<<<<<<
- *         if qname in self.index:
- *             return IteratorRowSelection(
+ * cdef inline array copy(array self):             # <<<<<<<<<<<<<<
+ *     """ make a copy of an array. """
+ *     op = newarrayobject(Py_TYPE(self), Py_SIZE(self), self.ob_descr)
  */
 
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_12IndexedReads_5find(PyObject *__pyx_v_self, PyObject *__pyx_v_qname); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_12IndexedReads_4find[] = "IndexedReads.find(self, qname)";
-static PyObject *__pyx_pw_5pysam_8csamfile_12IndexedReads_5find(PyObject *__pyx_v_self, PyObject *__pyx_v_qname) {
-  PyObject *__pyx_r = 0;
+static CYTHON_INLINE arrayobject *__pyx_f_7cpython_5array_copy(arrayobject *__pyx_v_self) {
+  arrayobject *__pyx_v_op = NULL;
+  arrayobject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("find (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_12IndexedReads_4find(((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)__pyx_v_self), ((PyObject *)__pyx_v_qname));
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("copy", 0);
+  __Pyx_TraceCall("copy", __pyx_f[1], 137);
+
+  /* "array.pxd":139
+ * cdef inline array copy(array self):
+ *     """ make a copy of an array. """
+ *     op = newarrayobject(Py_TYPE(self), Py_SIZE(self), self.ob_descr)             # <<<<<<<<<<<<<<
+ *     memcpy(op.data.as_chars, self.data.as_chars, Py_SIZE(op) * op.ob_descr.itemsize)
+ *     return op
+ */
+  __pyx_t_1 = ((PyObject *)newarrayobject(Py_TYPE(((PyObject *)__pyx_v_self)), Py_SIZE(((PyObject *)__pyx_v_self)), __pyx_v_self->ob_descr)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_v_op = ((arrayobject *)__pyx_t_1);
+  __pyx_t_1 = 0;
+
+  /* "array.pxd":140
+ *     """ make a copy of an array. """
+ *     op = newarrayobject(Py_TYPE(self), Py_SIZE(self), self.ob_descr)
+ *     memcpy(op.data.as_chars, self.data.as_chars, Py_SIZE(op) * op.ob_descr.itemsize)             # <<<<<<<<<<<<<<
+ *     return op
+ * 
+ */
+  memcpy(__pyx_v_op->data.as_chars, __pyx_v_self->data.as_chars, (Py_SIZE(((PyObject *)__pyx_v_op)) * __pyx_v_op->ob_descr->itemsize));
+
+  /* "array.pxd":141
+ *     op = newarrayobject(Py_TYPE(self), Py_SIZE(self), self.ob_descr)
+ *     memcpy(op.data.as_chars, self.data.as_chars, Py_SIZE(op) * op.ob_descr.itemsize)
+ *     return op             # <<<<<<<<<<<<<<
+ * 
+ * cdef inline int extend_buffer(array self, char* stuff, Py_ssize_t n) except -1:
+ */
+  __Pyx_XDECREF(((PyObject *)__pyx_r));
+  __Pyx_INCREF(((PyObject *)__pyx_v_op));
+  __pyx_r = __pyx_v_op;
+  goto __pyx_L0;
+
+  /* "array.pxd":137
+ *     return op
+ * 
+ * cdef inline array copy(array self):             # <<<<<<<<<<<<<<
+ *     """ make a copy of an array. """
+ *     op = newarrayobject(Py_TYPE(self), Py_SIZE(self), self.ob_descr)
+ */
 
   /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("cpython.array.copy", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = 0;
+  __pyx_L0:;
+  __Pyx_XDECREF((PyObject *)__pyx_v_op);
+  __Pyx_XGIVEREF((PyObject *)__pyx_r);
+  __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_8csamfile_12IndexedReads_4find(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self, PyObject *__pyx_v_qname) {
-  PyObject *__pyx_r = NULL;
+/* "array.pxd":143
+ *     return op
+ * 
+ * cdef inline int extend_buffer(array self, char* stuff, Py_ssize_t n) except -1:             # <<<<<<<<<<<<<<
+ *     """ efficent appending of new stuff of same type
+ *     (e.g. of same array type)
+ */
+
+static CYTHON_INLINE int __pyx_f_7cpython_5array_extend_buffer(arrayobject *__pyx_v_self, char *__pyx_v_stuff, Py_ssize_t __pyx_v_n) {
+  Py_ssize_t __pyx_v_itemsize;
+  Py_ssize_t __pyx_v_origsize;
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("find", 0);
-  __Pyx_TraceCall("find", __pyx_f[0], 3437);
-
-  /* "pysam/csamfile.pyx":3438
- * 
- *     def find(self, qname):
- *         if qname in self.index:             # <<<<<<<<<<<<<<
- *             return IteratorRowSelection(
- *                 self.samfile,
- */
-  __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_v_qname, __pyx_v_self->index, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3438; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":3439
- *     def find(self, qname):
- *         if qname in self.index:
- *             return IteratorRowSelection(             # <<<<<<<<<<<<<<
- *                 self.samfile,
- *                 self.index[qname],
- */
-    __Pyx_XDECREF(__pyx_r);
+  __Pyx_RefNannySetupContext("extend_buffer", 0);
+  __Pyx_TraceCall("extend_buffer", __pyx_f[1], 143);
 
-    /* "pysam/csamfile.pyx":3441
- *             return IteratorRowSelection(
- *                 self.samfile,
- *                 self.index[qname],             # <<<<<<<<<<<<<<
- *                 reopen = False)
- *         else:
+  /* "array.pxd":147
+ *     (e.g. of same array type)
+ *     n: number of elements (not number of bytes!) """
+ *     cdef Py_ssize_t itemsize = self.ob_descr.itemsize             # <<<<<<<<<<<<<<
+ *     cdef Py_ssize_t origsize = Py_SIZE(self)
+ *     resize_smart(self, origsize + n)
  */
-    __pyx_t_3 = PyObject_GetItem(__pyx_v_self->index, __pyx_v_qname); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3441; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_3);
+  __pyx_t_1 = __pyx_v_self->ob_descr->itemsize;
+  __pyx_v_itemsize = __pyx_t_1;
 
-    /* "pysam/csamfile.pyx":3439
- *     def find(self, qname):
- *         if qname in self.index:
- *             return IteratorRowSelection(             # <<<<<<<<<<<<<<
- *                 self.samfile,
- *                 self.index[qname],
+  /* "array.pxd":148
+ *     n: number of elements (not number of bytes!) """
+ *     cdef Py_ssize_t itemsize = self.ob_descr.itemsize
+ *     cdef Py_ssize_t origsize = Py_SIZE(self)             # <<<<<<<<<<<<<<
+ *     resize_smart(self, origsize + n)
+ *     memcpy(self.data.as_chars + origsize * itemsize, stuff, n * itemsize)
  */
-    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_INCREF(((PyObject *)__pyx_v_self->samfile));
-    PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_v_self->samfile));
-    __Pyx_GIVEREF(((PyObject *)__pyx_v_self->samfile));
-    PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    __pyx_t_3 = 0;
-    __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
+  __pyx_v_origsize = Py_SIZE(((PyObject *)__pyx_v_self));
 
-    /* "pysam/csamfile.pyx":3442
- *                 self.samfile,
- *                 self.index[qname],
- *                 reopen = False)             # <<<<<<<<<<<<<<
- *         else:
- *             raise KeyError("read %s not found" % qname)
+  /* "array.pxd":149
+ *     cdef Py_ssize_t itemsize = self.ob_descr.itemsize
+ *     cdef Py_ssize_t origsize = Py_SIZE(self)
+ *     resize_smart(self, origsize + n)             # <<<<<<<<<<<<<<
+ *     memcpy(self.data.as_chars + origsize * itemsize, stuff, n * itemsize)
+ *     return 0
  */
-    if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, Py_False) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = resize_smart(__pyx_v_self, (__pyx_v_origsize + __pyx_v_n)); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/csamfile.pyx":3439
- *     def find(self, qname):
- *         if qname in self.index:
- *             return IteratorRowSelection(             # <<<<<<<<<<<<<<
- *                 self.samfile,
- *                 self.index[qname],
+  /* "array.pxd":150
+ *     cdef Py_ssize_t origsize = Py_SIZE(self)
+ *     resize_smart(self, origsize + n)
+ *     memcpy(self.data.as_chars + origsize * itemsize, stuff, n * itemsize)             # <<<<<<<<<<<<<<
+ *     return 0
+ * 
  */
-    __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowSelection)), __pyx_t_4, __pyx_t_3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_r = __pyx_t_5;
-    __pyx_t_5 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
+  memcpy((__pyx_v_self->data.as_chars + (__pyx_v_origsize * __pyx_v_itemsize)), __pyx_v_stuff, (__pyx_v_n * __pyx_v_itemsize));
 
-    /* "pysam/csamfile.pyx":3444
- *                 reopen = False)
- *         else:
- *             raise KeyError("read %s not found" % qname)             # <<<<<<<<<<<<<<
+  /* "array.pxd":151
+ *     resize_smart(self, origsize + n)
+ *     memcpy(self.data.as_chars + origsize * itemsize, stuff, n * itemsize)
+ *     return 0             # <<<<<<<<<<<<<<
  * 
- *     def __dealloc__(self):
+ * cdef inline int extend(array self, array other) except -1:
  */
-    __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_read_s_not_found, __pyx_v_qname); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_5);
-    __Pyx_GIVEREF(__pyx_t_5);
-    __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_Raise(__pyx_t_5, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
+  __pyx_r = 0;
+  goto __pyx_L0;
 
-  /* "pysam/csamfile.pyx":3437
- *         bam_destroy1(b)
+  /* "array.pxd":143
+ *     return op
  * 
- *     def find(self, qname):             # <<<<<<<<<<<<<<
- *         if qname in self.index:
- *             return IteratorRowSelection(
+ * cdef inline int extend_buffer(array self, char* stuff, Py_ssize_t n) except -1:             # <<<<<<<<<<<<<<
+ *     """ efficent appending of new stuff of same type
+ *     (e.g. of same array type)
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.IndexedReads.find", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
+  __Pyx_AddTraceback("cpython.array.extend_buffer", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
   __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
+  __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/csamfile.pyx":3446
- *             raise KeyError("read %s not found" % qname)
+/* "array.pxd":153
+ *     return 0
  * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         if self.owns_samfile:
- *             hts_close(self.htsfile)
+ * cdef inline int extend(array self, array other) except -1:             # <<<<<<<<<<<<<<
+ *     """ extend array with data from another array; types must match. """
+ *     if self.ob_descr.typecode != other.ob_descr.typecode:
  */
 
-/* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_12IndexedReads_7__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_12IndexedReads_7__dealloc__(PyObject *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_12IndexedReads_6__dealloc__(((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-}
-
-static void __pyx_pf_5pysam_8csamfile_12IndexedReads_6__dealloc__(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self) {
+static CYTHON_INLINE int __pyx_f_7cpython_5array_extend(arrayobject *__pyx_v_self, arrayobject *__pyx_v_other) {
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
+  int __pyx_t_2;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 3446);
+  __Pyx_RefNannySetupContext("extend", 0);
+  __Pyx_TraceCall("extend", __pyx_f[1], 153);
 
-  /* "pysam/csamfile.pyx":3447
- * 
- *     def __dealloc__(self):
- *         if self.owns_samfile:             # <<<<<<<<<<<<<<
- *             hts_close(self.htsfile)
- * 
+  /* "array.pxd":155
+ * cdef inline int extend(array self, array other) except -1:
+ *     """ extend array with data from another array; types must match. """
+ *     if self.ob_descr.typecode != other.ob_descr.typecode:             # <<<<<<<<<<<<<<
+ *         PyErr_BadArgument()
+ *     return extend_buffer(self, other.data.as_chars, Py_SIZE(other))
  */
-  __pyx_t_1 = (__pyx_v_self->owns_samfile != 0);
+  __pyx_t_1 = ((__pyx_v_self->ob_descr->typecode != __pyx_v_other->ob_descr->typecode) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/csamfile.pyx":3448
- *     def __dealloc__(self):
- *         if self.owns_samfile:
- *             hts_close(self.htsfile)             # <<<<<<<<<<<<<<
+    /* "array.pxd":156
+ *     """ extend array with data from another array; types must match. """
+ *     if self.ob_descr.typecode != other.ob_descr.typecode:
+ *         PyErr_BadArgument()             # <<<<<<<<<<<<<<
+ *     return extend_buffer(self, other.data.as_chars, Py_SIZE(other))
  * 
- * __all__ = ["Samfile",
  */
-    hts_close(__pyx_v_self->htsfile);
+    __pyx_t_2 = PyErr_BadArgument(); if (unlikely(__pyx_t_2 == 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":3446
- *             raise KeyError("read %s not found" % qname)
+  /* "array.pxd":157
+ *     if self.ob_descr.typecode != other.ob_descr.typecode:
+ *         PyErr_BadArgument()
+ *     return extend_buffer(self, other.data.as_chars, Py_SIZE(other))             # <<<<<<<<<<<<<<
  * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         if self.owns_samfile:
- *             hts_close(self.htsfile)
+ * cdef inline void zero(array self):
+ */
+  __pyx_t_2 = __pyx_f_7cpython_5array_extend_buffer(__pyx_v_self, __pyx_v_other->data.as_chars, Py_SIZE(((PyObject *)__pyx_v_other))); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 157; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_r = __pyx_t_2;
+  goto __pyx_L0;
+
+  /* "array.pxd":153
+ *     return 0
+ * 
+ * cdef inline int extend(array self, array other) except -1:             # <<<<<<<<<<<<<<
+ *     """ extend array with data from another array; types must match. """
+ *     if self.ob_descr.typecode != other.ob_descr.typecode:
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_AddTraceback("cpython.array.extend", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = -1;
+  __pyx_L0:;
+  __Pyx_TraceReturn(Py_None);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "array.pxd":159
+ *     return extend_buffer(self, other.data.as_chars, Py_SIZE(other))
+ * 
+ * cdef inline void zero(array self):             # <<<<<<<<<<<<<<
+ *     """ set all elements of array to zero. """
+ *     memset(self.data.as_chars, 0, Py_SIZE(self) * self.ob_descr.itemsize)
+ */
+
+static CYTHON_INLINE void __pyx_f_7cpython_5array_zero(arrayobject *__pyx_v_self) {
+  __Pyx_RefNannyDeclarations
+  __Pyx_TraceDeclarations
+  __Pyx_RefNannySetupContext("zero", 0);
+  __Pyx_TraceCall("zero", __pyx_f[1], 159);
+
+  /* "array.pxd":161
+ * cdef inline void zero(array self):
+ *     """ set all elements of array to zero. """
+ *     memset(self.data.as_chars, 0, Py_SIZE(self) * self.ob_descr.itemsize)             # <<<<<<<<<<<<<<
+ */
+  memset(__pyx_v_self->data.as_chars, 0, (Py_SIZE(((PyObject *)__pyx_v_self)) * __pyx_v_self->ob_descr->itemsize));
+
+  /* "array.pxd":159
+ *     return extend_buffer(self, other.data.as_chars, Py_SIZE(other))
+ * 
+ * cdef inline void zero(array self):             # <<<<<<<<<<<<<<
+ *     """ set all elements of array to zero. """
+ *     memset(self.data.as_chars, 0, Py_SIZE(self) * self.ob_descr.itemsize)
  */
 
   /* function exit code */
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
 }
-static struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead __pyx_vtable_5pysam_8csamfile_AlignedRead;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment __pyx_vtable_5pysam_14calignmentfile_AlignedSegment;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_AlignedRead(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_AlignedRead *p;
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_AlignedSegment(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -35858,12 +39774,12 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile_AlignedRead(PyTypeObject *t, CYTH
     o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
   }
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_AlignedRead;
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)o);
+  p->__pyx_vtab = __pyx_vtabptr_5pysam_14calignmentfile_AlignedSegment;
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_AlignedRead(PyObject *o) {
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_AlignedSegment(PyObject *o) {
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -35873,20 +39789,206 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_AlignedRead(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_11AlignedRead_3__dealloc__(o);
+    __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qname(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_name(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10query_name_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_query_name(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10query_name_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_flag(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4flag_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_flag(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4flag_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_reference_id(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12reference_id_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_reference_id(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12reference_id_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_reference_start(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15reference_start_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_reference_start(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15reference_start_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mapping_quality(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mapping_quality(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mapping_quality_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_cigarstring(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigarstring_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_cigarstring(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigarstring_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_next_reference_id(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_next_reference_id(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_17next_reference_id_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_next_reference_start(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_next_reference_start(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_20next_reference_start_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_length(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12query_length_1__get__(o);
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_template_length(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15template_length_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_template_length(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15template_length_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_sequence(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14query_sequence_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_query_sequence(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14query_sequence_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_qualities(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15query_qualities_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_query_qualities(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15query_qualities_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_tags(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tags_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_tags(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tags_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_bin(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3bin_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_bin(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3bin_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_paired(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_paired_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_qname(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_paired(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_paired_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -35894,13 +39996,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_qname(PyObject *o, PyObj
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_cigar(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_proper_pair(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigar(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_proper_pair(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_14is_proper_pair_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -35908,13 +40010,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigar(PyObject *o, PyObj
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_cigarstring(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_unmapped(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigarstring(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_unmapped(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11is_unmapped_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -35922,13 +40024,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigarstring(PyObject *o,
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_seq(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mate_is_unmapped(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_seq(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mate_is_unmapped(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_16mate_is_unmapped_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -35936,13 +40038,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_seq(PyObject *o, PyObjec
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qual(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_reverse(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10is_reverse_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_qual(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_reverse(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_10is_reverse_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -35950,33 +40052,55 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_qual(PyObject *o, PyObje
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_query(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5query_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mate_is_reverse(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mate_is_reverse(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15mate_is_reverse_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qqual(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5qqual_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_read1(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read1_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qstart(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_6qstart_1__get__(o);
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_read1(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read1_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qend(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4qend_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_read2(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read2_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qlen(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4qlen_1__get__(o);
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_read2(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_8is_read2_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_tags(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_secondary(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_secondary_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_tags(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_secondary(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_secondary_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -35984,13 +40108,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_tags(PyObject *o, PyObje
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_flag(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_qcfail(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_flag(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_qcfail(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9is_qcfail_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -35998,13 +40122,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_flag(PyObject *o, PyObje
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_rname(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_duplicate(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_rname(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_duplicate(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_12is_duplicate_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36012,13 +40136,45 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_rname(PyObject *o, PyObj
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_tid(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_reference_end(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_13reference_end_1__get__(o);
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_reference_length(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_16reference_length_1__get__(o);
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_length(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_22query_alignment_length_1__get__(o);
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_sequence(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_24query_alignment_sequence_1__get__(o);
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_qualities(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_25query_alignment_qualities_1__get__(o);
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_start(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21query_alignment_start_1__get__(o);
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_end(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_19query_alignment_end_1__get__(o);
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_aligment_length(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21query_aligment_length_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_tid(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_cigartuples(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigartuples_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_cigartuples(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11cigartuples_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36026,13 +40182,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_tid(PyObject *o, PyObjec
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_pos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qname(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qname_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_pos(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qname(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qname_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36040,13 +40196,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_pos(PyObject *o, PyObjec
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_bin(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_tid(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3tid_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_bin(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_tid(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3tid_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36054,25 +40210,41 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_bin(PyObject *o, PyObjec
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_rlen(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4rlen_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_pos(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3pos_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_pos(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3pos_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_aend(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4aend_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mapq(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mapq_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_alen(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4alen_1__get__(o);
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mapq(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mapq_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mapq(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_rnext(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rnext_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mapq(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_rnext(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rnext_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36080,13 +40252,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mapq(PyObject *o, PyObje
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mrnm(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_pnext(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5pnext_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mrnm(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_pnext(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5pnext_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36094,13 +40266,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mrnm(PyObject *o, PyObje
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_rnext(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_cigar(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5cigar_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_rnext(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_cigar(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5cigar_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36108,13 +40280,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_rnext(PyObject *o, PyObj
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mpos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_tlen(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tlen_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mpos(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_tlen(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4tlen_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36122,13 +40294,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mpos(PyObject *o, PyObje
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_pnext(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_seq(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3seq_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_pnext(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_seq(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_3seq_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36136,13 +40308,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_pnext(PyObject *o, PyObj
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_isize(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qual(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qual_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_isize(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qual(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qual_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36150,13 +40322,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_isize(PyObject *o, PyObj
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_tlen(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_alen(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4alen_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_tlen(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_alen(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4alen_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36164,13 +40336,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_tlen(PyObject *o, PyObje
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_paired(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_aend(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4aend_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_paired(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_aend(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4aend_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36178,13 +40350,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_paired(PyObject *o, P
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_proper_pair(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_rlen(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4rlen_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_proper_pair(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_rlen(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4rlen_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36192,13 +40364,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_proper_pair(PyObject
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_unmapped(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5query_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_unmapped(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_query(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5query_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36206,13 +40378,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_unmapped(PyObject *o,
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mate_is_unmapped(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qqual(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qqual_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_unmapped(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qqual(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5qqual_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36220,13 +40392,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_unmapped(PyObjec
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_reverse(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qstart(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_6qstart_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_reverse(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qstart(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_6qstart_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36234,13 +40406,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_reverse(PyObject *o,
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mate_is_reverse(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qend(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qend_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_reverse(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qend(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qend_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36248,13 +40420,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_reverse(PyObject
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_read1(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qlen(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qlen_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read1(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qlen(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4qlen_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36262,13 +40434,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read1(PyObject *o, Py
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_read2(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mrnm(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mrnm_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read2(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mrnm(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mrnm_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36276,13 +40448,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read2(PyObject *o, Py
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_secondary(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mpos(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mpos_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_secondary(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mpos(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_4mpos_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36290,13 +40462,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_secondary(PyObject *o
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_qcfail(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_rname(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rname_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_qcfail(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_rname(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5rname_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36304,13 +40476,13 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_qcfail(PyObject *o, P
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_duplicate(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_isize(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5isize_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_duplicate(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_isize(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5isize_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36318,83 +40490,105 @@ static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_duplicate(PyObject *o
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_positions(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_9positions_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_blocks(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_6blocks_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_inferred_length(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_15inferred_length_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_aligned_pairs(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_13aligned_pairs_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_aligned_pairs(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_13aligned_pairs_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_inferred_length(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15inferred_length_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_blocks(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_6blocks_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_positions(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9positions_1__get__(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_AlignedRead[] = {
-  {__Pyx_NAMESTR("compare"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_7compare, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_6compare)},
-  {__Pyx_NAMESTR("_convert_python_tag"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_11_convert_python_tag, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_10_convert_python_tag)},
-  {__Pyx_NAMESTR("setTag"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_12setTag)},
-  {__Pyx_NAMESTR("overlap"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_15overlap, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_14overlap)},
-  {__Pyx_NAMESTR("opt"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_17opt, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_16opt)},
-  {__Pyx_NAMESTR("fancy_str"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_19fancy_str, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_18fancy_str)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_AlignedSegment[] = {
+  {__Pyx_NAMESTR("compare"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_7compare, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14AlignedSegment_6compare)},
+  {__Pyx_NAMESTR("get_reference_positions"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_11get_reference_positions, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14AlignedSegment_10get_reference_positions)},
+  {__Pyx_NAMESTR("infer_query_length"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_13infer_query_length, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14AlignedSegment_12infer_query_length)},
+  {__Pyx_NAMESTR("get_aligned_pairs"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_15get_aligned_pairs, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14AlignedSegment_14get_aligned_pairs)},
+  {__Pyx_NAMESTR("get_blocks"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_17get_blocks, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14AlignedSegment_16get_blocks)},
+  {__Pyx_NAMESTR("get_overlap"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_19get_overlap, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14AlignedSegment_18get_overlap)},
+  {__Pyx_NAMESTR("setTag"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_21setTag, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14AlignedSegment_20setTag)},
+  {__Pyx_NAMESTR("opt"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_23opt, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14AlignedSegment_22opt)},
+  {__Pyx_NAMESTR("overlap"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14AlignedSegment_25overlap, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14AlignedSegment_24overlap)},
   {0, 0, 0, 0}
 };
 
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_AlignedRead[] = {
-  {(char *)"qname", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qname, __pyx_setprop_5pysam_8csamfile_11AlignedRead_qname, __Pyx_DOCSTR(__pyx_k_the_query_name_None_if_not_prese), 0},
-  {(char *)"cigar", __pyx_getprop_5pysam_8csamfile_11AlignedRead_cigar, __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigar, __Pyx_DOCSTR(__pyx_k_the_term_cigar_alignment_The_ali), 0},
-  {(char *)"cigarstring", __pyx_getprop_5pysam_8csamfile_11AlignedRead_cigarstring, __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigarstring, __Pyx_DOCSTR(__pyx_k_the_term_cigar_alignment_as_a_st), 0},
-  {(char *)"seq", __pyx_getprop_5pysam_8csamfile_11AlignedRead_seq, __pyx_setprop_5pysam_8csamfile_11AlignedRead_seq, __Pyx_DOCSTR(__pyx_k_read_sequence_bases_including_te), 0},
-  {(char *)"qual", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qual, __pyx_setprop_5pysam_8csamfile_11AlignedRead_qual, __Pyx_DOCSTR(__pyx_k_read_sequence_base_qualities_inc), 0},
-  {(char *)"query", __pyx_getprop_5pysam_8csamfile_11AlignedRead_query, 0, __Pyx_DOCSTR(__pyx_k_aligned_portion_of_the_read_This), 0},
-  {(char *)"qqual", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qqual, 0, __Pyx_DOCSTR(__pyx_k_aligned_query_sequence_quality_v), 0},
-  {(char *)"qstart", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qstart, 0, __Pyx_DOCSTR(__pyx_k_start_index_of_the_aligned_query), 0},
-  {(char *)"qend", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qend, 0, __Pyx_DOCSTR(__pyx_k_end_index_of_the_aligned_query_p), 0},
-  {(char *)"qlen", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qlen, 0, __Pyx_DOCSTR(__pyx_k_length_of_the_aligned_query_sequ), 0},
-  {(char *)"tags", __pyx_getprop_5pysam_8csamfile_11AlignedRead_tags, __pyx_setprop_5pysam_8csamfile_11AlignedRead_tags, __Pyx_DOCSTR(__pyx_k_the_tags_in_the_AUX_field_This_p), 0},
-  {(char *)"flag", __pyx_getprop_5pysam_8csamfile_11AlignedRead_flag, __pyx_setprop_5pysam_8csamfile_11AlignedRead_flag, __Pyx_DOCSTR(__pyx_k_properties_flag), 0},
-  {(char *)"rname", __pyx_getprop_5pysam_8csamfile_11AlignedRead_rname, __pyx_setprop_5pysam_8csamfile_11AlignedRead_rname, __Pyx_DOCSTR(__pyx_k_term_target_ID_DEPRECATED_from), 0},
-  {(char *)"tid", __pyx_getprop_5pysam_8csamfile_11AlignedRead_tid, __pyx_setprop_5pysam_8csamfile_11AlignedRead_tid, __Pyx_DOCSTR(__pyx_k_term_target_ID_note_This_field), 0},
-  {(char *)"pos", __pyx_getprop_5pysam_8csamfile_11AlignedRead_pos, __pyx_setprop_5pysam_8csamfile_11AlignedRead_pos, __Pyx_DOCSTR(__pyx_k_0_based_leftmost_coordinate), 0},
-  {(char *)"bin", __pyx_getprop_5pysam_8csamfile_11AlignedRead_bin, __pyx_setprop_5pysam_8csamfile_11AlignedRead_bin, __Pyx_DOCSTR(__pyx_k_properties_bin), 0},
-  {(char *)"rlen", __pyx_getprop_5pysam_8csamfile_11AlignedRead_rlen, 0, __Pyx_DOCSTR(__pyx_k_length_of_the_read_This_includes), 0},
-  {(char *)"aend", __pyx_getprop_5pysam_8csamfile_11AlignedRead_aend, 0, __Pyx_DOCSTR(__pyx_k_aligned_reference_position_of_th), 0},
-  {(char *)"alen", __pyx_getprop_5pysam_8csamfile_11AlignedRead_alen, 0, __Pyx_DOCSTR(__pyx_k_aligned_length_of_the_read_on_th), 0},
-  {(char *)"mapq", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mapq, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mapq, __Pyx_DOCSTR(__pyx_k_mapping_quality_2), 0},
-  {(char *)"mrnm", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mrnm, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mrnm, __Pyx_DOCSTR(__pyx_k_the_term_reference_id_of_the_mat), 0},
-  {(char *)"rnext", __pyx_getprop_5pysam_8csamfile_11AlignedRead_rnext, __pyx_setprop_5pysam_8csamfile_11AlignedRead_rnext, __Pyx_DOCSTR(__pyx_k_the_term_reference_id_of_the_mat_2), 0},
-  {(char *)"mpos", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mpos, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mpos, __Pyx_DOCSTR(__pyx_k_the_position_of_the_mate_depreca), 0},
-  {(char *)"pnext", __pyx_getprop_5pysam_8csamfile_11AlignedRead_pnext, __pyx_setprop_5pysam_8csamfile_11AlignedRead_pnext, __Pyx_DOCSTR(__pyx_k_the_position_of_the_mate), 0},
-  {(char *)"isize", __pyx_getprop_5pysam_8csamfile_11AlignedRead_isize, __pyx_setprop_5pysam_8csamfile_11AlignedRead_isize, __Pyx_DOCSTR(__pyx_k_the_insert_size_deprecated_use_t), 0},
-  {(char *)"tlen", __pyx_getprop_5pysam_8csamfile_11AlignedRead_tlen, __pyx_setprop_5pysam_8csamfile_11AlignedRead_tlen, __Pyx_DOCSTR(__pyx_k_the_template_length), 0},
-  {(char *)"is_paired", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_paired, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_paired, __Pyx_DOCSTR(__pyx_k_true_if_read_is_paired_in_sequen), 0},
-  {(char *)"is_proper_pair", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_proper_pair, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_proper_pair, __Pyx_DOCSTR(__pyx_k_true_if_read_is_mapped_in_a_prop), 0},
-  {(char *)"is_unmapped", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_unmapped, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_unmapped, __Pyx_DOCSTR(__pyx_k_true_if_read_itself_is_unmapped), 0},
-  {(char *)"mate_is_unmapped", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mate_is_unmapped, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_unmapped, __Pyx_DOCSTR(__pyx_k_true_if_the_mate_is_unmapped), 0},
-  {(char *)"is_reverse", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_reverse, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_reverse, __Pyx_DOCSTR(__pyx_k_true_if_read_is_mapped_to_revers), 0},
-  {(char *)"mate_is_reverse", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mate_is_reverse, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_reverse, __Pyx_DOCSTR(__pyx_k_true_is_read_is_mapped_to_revers), 0},
-  {(char *)"is_read1", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_read1, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read1, __Pyx_DOCSTR(__pyx_k_true_if_this_is_read1), 0},
-  {(char *)"is_read2", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_read2, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read2, __Pyx_DOCSTR(__pyx_k_true_if_this_is_read2), 0},
-  {(char *)"is_secondary", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_secondary, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_secondary, __Pyx_DOCSTR(__pyx_k_true_if_not_primary_alignment), 0},
-  {(char *)"is_qcfail", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_qcfail, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_qcfail, __Pyx_DOCSTR(__pyx_k_true_if_QC_failure), 0},
-  {(char *)"is_duplicate", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_duplicate, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_duplicate, __Pyx_DOCSTR(__pyx_k_true_if_optical_or_PCR_duplicate), 0},
-  {(char *)"positions", __pyx_getprop_5pysam_8csamfile_11AlignedRead_positions, 0, __Pyx_DOCSTR(__pyx_k_a_list_of_reference_positions_th), 0},
-  {(char *)"inferred_length", __pyx_getprop_5pysam_8csamfile_11AlignedRead_inferred_length, 0, __Pyx_DOCSTR(__pyx_k_inferred_read_length_from_CIGAR), 0},
-  {(char *)"aligned_pairs", __pyx_getprop_5pysam_8csamfile_11AlignedRead_aligned_pairs, 0, __Pyx_DOCSTR(__pyx_k_a_list_of_aligned_read_and_refer), 0},
-  {(char *)"blocks", __pyx_getprop_5pysam_8csamfile_11AlignedRead_blocks, 0, __Pyx_DOCSTR(__pyx_k_a_list_of_start_and_end_positio), 0},
+static struct PyGetSetDef __pyx_getsets_5pysam_14calignmentfile_AlignedSegment[] = {
+  {(char *)"query_name", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_name, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_query_name, __Pyx_DOCSTR(__pyx_k_the_query_template_name_None_if), 0},
+  {(char *)"flag", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_flag, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_flag, __Pyx_DOCSTR(__pyx_k_properties_flag), 0},
+  {(char *)"reference_id", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_reference_id, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_reference_id, __Pyx_DOCSTR(__pyx_k_term_reference_ID_note_This_fie), 0},
+  {(char *)"reference_start", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_reference_start, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_reference_start, __Pyx_DOCSTR(__pyx_k_0_based_leftmost_coordinate), 0},
+  {(char *)"mapping_quality", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mapping_quality, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mapping_quality, __Pyx_DOCSTR(__pyx_k_mapping_quality_2), 0},
+  {(char *)"cigarstring", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_cigarstring, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_cigarstring, __Pyx_DOCSTR(__pyx_k_the_term_cigar_alignment_as_a_st), 0},
+  {(char *)"next_reference_id", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_next_reference_id, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_next_reference_id, __Pyx_DOCSTR(__pyx_k_the_term_reference_id_of_the_mat), 0},
+  {(char *)"next_reference_start", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_next_reference_start, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_next_reference_start, __Pyx_DOCSTR(__pyx_k_the_position_of_the_mate_next_re), 0},
+  {(char *)"query_length", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_length, 0, __Pyx_DOCSTR(__pyx_k_the_length_of_the_query_read_Thi), 0},
+  {(char *)"template_length", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_template_length, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_template_length, __Pyx_DOCSTR(__pyx_k_the_observed_query_template_leng), 0},
+  {(char *)"query_sequence", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_sequence, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_query_sequence, __Pyx_DOCSTR(__pyx_k_read_sequence_bases_including_te), 0},
+  {(char *)"query_qualities", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_qualities, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_query_qualities, __Pyx_DOCSTR(__pyx_k_read_sequence_base_qualities_inc), 0},
+  {(char *)"tags", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_tags, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_tags, __Pyx_DOCSTR(__pyx_k_the_tags_in_the_AUX_field_This_p), 0},
+  {(char *)"bin", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_bin, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_bin, __Pyx_DOCSTR(__pyx_k_properties_bin), 0},
+  {(char *)"is_paired", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_paired, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_paired, __Pyx_DOCSTR(__pyx_k_true_if_read_is_paired_in_sequen), 0},
+  {(char *)"is_proper_pair", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_proper_pair, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_proper_pair, __Pyx_DOCSTR(__pyx_k_true_if_read_is_mapped_in_a_prop), 0},
+  {(char *)"is_unmapped", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_unmapped, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_unmapped, __Pyx_DOCSTR(__pyx_k_true_if_read_itself_is_unmapped), 0},
+  {(char *)"mate_is_unmapped", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mate_is_unmapped, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mate_is_unmapped, __Pyx_DOCSTR(__pyx_k_true_if_the_mate_is_unmapped), 0},
+  {(char *)"is_reverse", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_reverse, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_reverse, __Pyx_DOCSTR(__pyx_k_true_if_read_is_mapped_to_revers), 0},
+  {(char *)"mate_is_reverse", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mate_is_reverse, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mate_is_reverse, __Pyx_DOCSTR(__pyx_k_true_is_read_is_mapped_to_revers), 0},
+  {(char *)"is_read1", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_read1, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_read1, __Pyx_DOCSTR(__pyx_k_true_if_this_is_read1), 0},
+  {(char *)"is_read2", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_read2, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_read2, __Pyx_DOCSTR(__pyx_k_true_if_this_is_read2), 0},
+  {(char *)"is_secondary", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_secondary, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_secondary, __Pyx_DOCSTR(__pyx_k_true_if_not_primary_alignment), 0},
+  {(char *)"is_qcfail", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_qcfail, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_qcfail, __Pyx_DOCSTR(__pyx_k_true_if_QC_failure), 0},
+  {(char *)"is_duplicate", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_is_duplicate, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_is_duplicate, __Pyx_DOCSTR(__pyx_k_true_if_optical_or_PCR_duplicate), 0},
+  {(char *)"reference_end", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_reference_end, 0, __Pyx_DOCSTR(__pyx_k_aligned_reference_position_of_th), 0},
+  {(char *)"reference_length", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_reference_length, 0, __Pyx_DOCSTR(__pyx_k_aligned_length_of_the_read_on_th), 0},
+  {(char *)"query_alignment_length", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_length, 0, __Pyx_DOCSTR(__pyx_k_length_of_the_query_template_Thi), 0},
+  {(char *)"query_alignment_sequence", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_sequence, 0, __Pyx_DOCSTR(__pyx_k_aligned_portion_of_the_read_This), 0},
+  {(char *)"query_alignment_qualities", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_qualities, 0, __Pyx_DOCSTR(__pyx_k_aligned_query_sequence_quality_v), 0},
+  {(char *)"query_alignment_start", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_start, 0, __Pyx_DOCSTR(__pyx_k_start_index_of_the_aligned_query), 0},
+  {(char *)"query_alignment_end", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_alignment_end, 0, __Pyx_DOCSTR(__pyx_k_end_index_of_the_aligned_query_p), 0},
+  {(char *)"query_aligment_length", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query_aligment_length, 0, __Pyx_DOCSTR(__pyx_k_length_of_the_aligned_query_sequ), 0},
+  {(char *)"cigartuples", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_cigartuples, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_cigartuples, __Pyx_DOCSTR(__pyx_k_the_term_cigar_alignment_The_ali), 0},
+  {(char *)"qname", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qname, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qname, 0, 0},
+  {(char *)"tid", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_tid, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_tid, 0, 0},
+  {(char *)"pos", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_pos, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_pos, 0, 0},
+  {(char *)"mapq", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mapq, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mapq, 0, 0},
+  {(char *)"rnext", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_rnext, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_rnext, 0, 0},
+  {(char *)"pnext", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_pnext, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_pnext, 0, 0},
+  {(char *)"cigar", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_cigar, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_cigar, 0, 0},
+  {(char *)"tlen", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_tlen, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_tlen, 0, 0},
+  {(char *)"seq", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_seq, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_seq, 0, 0},
+  {(char *)"qual", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qual, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qual, 0, 0},
+  {(char *)"alen", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_alen, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_alen, 0, 0},
+  {(char *)"aend", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_aend, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_aend, 0, 0},
+  {(char *)"rlen", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_rlen, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_rlen, 0, 0},
+  {(char *)"query", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_query, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_query, 0, 0},
+  {(char *)"qqual", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qqual, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qqual, 0, 0},
+  {(char *)"qstart", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qstart, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qstart, 0, 0},
+  {(char *)"qend", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qend, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qend, 0, 0},
+  {(char *)"qlen", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_qlen, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_qlen, 0, 0},
+  {(char *)"mrnm", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mrnm, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mrnm, 0, 0},
+  {(char *)"mpos", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_mpos, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_mpos, 0, 0},
+  {(char *)"rname", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_rname, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_rname, 0, 0},
+  {(char *)"isize", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_isize, __pyx_setprop_5pysam_14calignmentfile_14AlignedSegment_isize, 0, 0},
+  {(char *)"blocks", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_blocks, 0, 0, 0},
+  {(char *)"aligned_pairs", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_aligned_pairs, 0, 0, 0},
+  {(char *)"inferred_length", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_inferred_length, 0, 0, 0},
+  {(char *)"positions", __pyx_getprop_5pysam_14calignmentfile_14AlignedSegment_positions, 0, 0, 0},
   {0, 0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_AlignedRead = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_AlignedSegment = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.AlignedRead"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_AlignedRead), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.AlignedSegment"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_AlignedRead, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_AlignedSegment, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -36407,31 +40601,31 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_AlignedRead = {
   0, /*tp_as_number*/
   0, /*tp_as_sequence*/
   0, /*tp_as_mapping*/
-  __pyx_pw_5pysam_8csamfile_11AlignedRead_9__hash__, /*tp_hash*/
+  __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_9__hash__, /*tp_hash*/
   0, /*tp_call*/
-  __pyx_pw_5pysam_8csamfile_11AlignedRead_5__str__, /*tp_str*/
+  __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_5__str__, /*tp_str*/
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("AlignedRead()\n\n    Class representing an aligned read. See the SAM format specification for\n    the meaning of fields (http://samtools.sourceforge.net/).\n\n    This class stores a handle to the samtools C-structure representing\n    an aligned read. Member read access is forwarded to the C-structure\n    and converted into python objects. This implementation should be fast,\n    as only the data needed is converted.\n\n    For write access, the C-structure is updated  [...]
+  __Pyx_DOCSTR("AlignedSegment()\nClass representing an aligned segment. \n\n    This class stores a handle to the samtools C-structure representing\n    an aligned read. Member read access is forwarded to the C-structure\n    and converted into python objects. This implementation should be fast,\n    as only the data needed is converted.\n\n    For write access, the C-structure is updated in-place. This is\n    not the most efficient way to build BAM entries, as the variable\n    length [...]
   0, /*tp_traverse*/
   0, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
   0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_AlignedRead, /*tp_methods*/
+  __pyx_methods_5pysam_14calignmentfile_AlignedSegment, /*tp_methods*/
   0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_AlignedRead, /*tp_getset*/
+  __pyx_getsets_5pysam_14calignmentfile_AlignedSegment, /*tp_getset*/
   0, /*tp_base*/
   0, /*tp_dict*/
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_11AlignedRead_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_14AlignedSegment_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_AlignedRead, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_AlignedSegment, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -36447,10 +40641,10 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_AlignedRead = {
   0, /*tp_finalize*/
   #endif
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_Samfile __pyx_vtable_5pysam_8csamfile_Samfile;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile __pyx_vtable_5pysam_14calignmentfile_AlignmentFile;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_Samfile(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *p;
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_AlignmentFile(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -36458,17 +40652,17 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile_Samfile(PyTypeObject *t, PyObject
     o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
   }
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_Samfile;
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)o);
+  p->__pyx_vtab = __pyx_vtabptr_5pysam_14calignmentfile_AlignmentFile;
   p->_filename = Py_None; Py_INCREF(Py_None);
-  if (unlikely(__pyx_pw_5pysam_8csamfile_7Samfile_1__cinit__(o, a, k) < 0)) {
+  if (unlikely(__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_1__cinit__(o, a, k) < 0)) {
     Py_DECREF(o); o = 0;
   }
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_Samfile(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *p = (struct __pyx_obj_5pysam_8csamfile_Samfile *)o;
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_AlignmentFile(PyObject *o) {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *p = (struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -36479,7 +40673,7 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_Samfile(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_7Samfile_33__dealloc__(o);
+    __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_33__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
@@ -36487,104 +40681,104 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_Samfile(PyObject *o) {
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static int __pyx_tp_traverse_5pysam_8csamfile_Samfile(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_14calignmentfile_AlignmentFile(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *p = (struct __pyx_obj_5pysam_8csamfile_Samfile *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *p = (struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)o;
   if (p->_filename) {
     e = (*v)(p->_filename, a); if (e) return e;
   }
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_8csamfile_Samfile(PyObject *o) {
+static int __pyx_tp_clear_5pysam_14calignmentfile_AlignmentFile(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *p = (struct __pyx_obj_5pysam_8csamfile_Samfile *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *p = (struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)o;
   tmp = ((PyObject*)p->_filename);
   p->_filename = Py_None; Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   return 0;
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_filename(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_8filename_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_filename(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_8filename_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_nreferences(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_11nreferences_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_nreferences(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_11nreferences_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_references(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_10references_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_references(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_10references_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_lengths(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_7lengths_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_lengths(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_7lengths_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_mapped(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_6mapped_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_mapped(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_6mapped_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_unmapped(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_8unmapped_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_unmapped(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_8unmapped_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_nocoordinate(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_12nocoordinate_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_nocoordinate(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_12nocoordinate_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_text(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_4text_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_text(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_4text_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_header(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_6header_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_header(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_6header_1__get__(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_Samfile[] = {
-  {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_3_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_2_isOpen)},
-  {__Pyx_NAMESTR("_hasIndex"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_5_hasIndex, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_4_hasIndex)},
-  {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_7_open, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_6_open)},
-  {__Pyx_NAMESTR("gettid"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_9gettid, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_8gettid)},
-  {__Pyx_NAMESTR("getrname"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_11getrname, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_10getrname)},
-  {__Pyx_NAMESTR("_parseRegion"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_12_parseRegion)},
-  {__Pyx_NAMESTR("reset"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_15reset, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_14reset)},
-  {__Pyx_NAMESTR("seek"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_17seek, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_16seek)},
-  {__Pyx_NAMESTR("tell"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_19tell, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_18tell)},
-  {__Pyx_NAMESTR("fetch"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_21fetch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_20fetch)},
-  {__Pyx_NAMESTR("head"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_23head, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_22head)},
-  {__Pyx_NAMESTR("mate"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_25mate, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_24mate)},
-  {__Pyx_NAMESTR("count"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_27count, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_26count)},
-  {__Pyx_NAMESTR("pileup"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_29pileup, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_28pileup)},
-  {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_31close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_30close)},
-  {__Pyx_NAMESTR("write"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_35write, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_34write)},
-  {__Pyx_NAMESTR("__enter__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_37__enter__, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_36__enter__)},
-  {__Pyx_NAMESTR("__exit__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_39__exit__, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_38__exit__)},
-  {__Pyx_NAMESTR("_checkIndex"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_41_checkIndex, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_40_checkIndex)},
-  {__Pyx_NAMESTR("_buildLine"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_43_buildLine, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_42_buildLine)},
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_47__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_46__next__)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_AlignmentFile[] = {
+  {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_3_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_2_isOpen)},
+  {__Pyx_NAMESTR("_hasIndex"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_5_hasIndex, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_4_hasIndex)},
+  {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_7_open, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_6_open)},
+  {__Pyx_NAMESTR("gettid"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_9gettid, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_8gettid)},
+  {__Pyx_NAMESTR("getrname"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_11getrname, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_10getrname)},
+  {__Pyx_NAMESTR("_parseRegion"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_13_parseRegion, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_12_parseRegion)},
+  {__Pyx_NAMESTR("reset"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_15reset, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_14reset)},
+  {__Pyx_NAMESTR("seek"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_17seek, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_16seek)},
+  {__Pyx_NAMESTR("tell"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_19tell, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_18tell)},
+  {__Pyx_NAMESTR("fetch"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_21fetch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_20fetch)},
+  {__Pyx_NAMESTR("head"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_23head, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_22head)},
+  {__Pyx_NAMESTR("mate"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_25mate, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_24mate)},
+  {__Pyx_NAMESTR("count"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_27count, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_26count)},
+  {__Pyx_NAMESTR("pileup"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_29pileup, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_28pileup)},
+  {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_31close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_30close)},
+  {__Pyx_NAMESTR("write"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_35write, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_34write)},
+  {__Pyx_NAMESTR("__enter__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_37__enter__, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_36__enter__)},
+  {__Pyx_NAMESTR("__exit__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_39__exit__, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_38__exit__)},
+  {__Pyx_NAMESTR("_checkIndex"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_41_checkIndex, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_40_checkIndex)},
+  {__Pyx_NAMESTR("_buildLine"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_43_buildLine, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_42_buildLine)},
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_13AlignmentFile_47__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_13AlignmentFile_46__next__)},
   {0, 0, 0, 0}
 };
 
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_Samfile[] = {
-  {(char *)"filename", __pyx_getprop_5pysam_8csamfile_7Samfile_filename, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_filename_associat), 0},
-  {(char *)"nreferences", __pyx_getprop_5pysam_8csamfile_7Samfile_nreferences, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_reference_sequenc), 0},
-  {(char *)"references", __pyx_getprop_5pysam_8csamfile_7Samfile_references, 0, __Pyx_DOCSTR(__pyx_k_tuple_with_the_names_of_term_ref), 0},
-  {(char *)"lengths", __pyx_getprop_5pysam_8csamfile_7Samfile_lengths, 0, __Pyx_DOCSTR(__pyx_k_tuple_of_the_lengths_of_the_term), 0},
-  {(char *)"mapped", __pyx_getprop_5pysam_8csamfile_7Samfile_mapped, 0, __Pyx_DOCSTR(__pyx_k_total_number_of_mapped_alignment), 0},
-  {(char *)"unmapped", __pyx_getprop_5pysam_8csamfile_7Samfile_unmapped, 0, __Pyx_DOCSTR(__pyx_k_total_number_of_unmapped_reads_i), 0},
-  {(char *)"nocoordinate", __pyx_getprop_5pysam_8csamfile_7Samfile_nocoordinate, 0, __Pyx_DOCSTR(__pyx_k_total_number_of_reads_without_co), 0},
-  {(char *)"text", __pyx_getprop_5pysam_8csamfile_7Samfile_text, 0, __Pyx_DOCSTR(__pyx_k_full_contents_of_the_term_sam_fi), 0},
-  {(char *)"header", __pyx_getprop_5pysam_8csamfile_7Samfile_header, 0, __Pyx_DOCSTR(__pyx_k_header_information_within_the_te), 0},
+static struct PyGetSetDef __pyx_getsets_5pysam_14calignmentfile_AlignmentFile[] = {
+  {(char *)"filename", __pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_filename, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_filename_associat), 0},
+  {(char *)"nreferences", __pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_nreferences, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_reference_sequenc), 0},
+  {(char *)"references", __pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_references, 0, __Pyx_DOCSTR(__pyx_k_tuple_with_the_names_of_term_ref), 0},
+  {(char *)"lengths", __pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_lengths, 0, __Pyx_DOCSTR(__pyx_k_tuple_of_the_lengths_of_the_term), 0},
+  {(char *)"mapped", __pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_mapped, 0, __Pyx_DOCSTR(__pyx_k_total_number_of_mapped_alignment), 0},
+  {(char *)"unmapped", __pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_unmapped, 0, __Pyx_DOCSTR(__pyx_k_total_number_of_unmapped_reads_i), 0},
+  {(char *)"nocoordinate", __pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_nocoordinate, 0, __Pyx_DOCSTR(__pyx_k_total_number_of_reads_without_co), 0},
+  {(char *)"text", __pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_text, 0, __Pyx_DOCSTR(__pyx_k_full_contents_of_the_term_sam_fi), 0},
+  {(char *)"header", __pyx_getprop_5pysam_14calignmentfile_13AlignmentFile_header, 0, __Pyx_DOCSTR(__pyx_k_header_information_within_the_te), 0},
   {0, 0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_Samfile = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_AlignmentFile = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.Samfile"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_Samfile), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.AlignmentFile"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_Samfile, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_AlignmentFile, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -36604,16 +40798,16 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_Samfile = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("*(filename, mode=None, template = None,\n         referencenames = None, referencelengths = None,\n         text = NULL, header = None,\n         add_sq_text = False, check_header = True,\n         check_sq = True )*\n\n    A :term:`SAM`/:term:`BAM` formatted file. The file is\n    automatically opened.\n\n    *mode* should be ``r`` for reading or ``w`` for writing. The\n    default is text mode (:term:`SAM`). For binary (:term:`BAM`) I/O\n    you should append ``b`` for  [...]
-  __pyx_tp_traverse_5pysam_8csamfile_Samfile, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_Samfile, /*tp_clear*/
+  __Pyx_DOCSTR("*(filename, mode=None, template = None,\n         referencenames=None, referencelengths = None,\n         text=NULL, header=None,\n         add_sq_text=False, check_header=True,\n         check_sq=True)*\n\n    A :term:`SAM`/:term:`BAM` formatted file. The file is\n    automatically opened.\n\n    *mode* should be ``r`` for reading or ``w`` for writing. The\n    default is text mode (:term:`SAM`). For binary (:term:`BAM`) I/O\n    you should append ``b`` for compressed or [...]
+  __pyx_tp_traverse_5pysam_14calignmentfile_AlignmentFile, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_AlignmentFile, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_7Samfile_45__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_7Samfile_47__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_Samfile, /*tp_methods*/
+  __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_45__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_14calignmentfile_13AlignmentFile_47__next__, /*tp_iternext*/
+  __pyx_methods_5pysam_14calignmentfile_AlignmentFile, /*tp_methods*/
   0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_Samfile, /*tp_getset*/
+  __pyx_getsets_5pysam_14calignmentfile_AlignmentFile, /*tp_getset*/
   0, /*tp_base*/
   0, /*tp_dict*/
   0, /*tp_descr_get*/
@@ -36621,7 +40815,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_Samfile = {
   0, /*tp_dictoffset*/
   0, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_Samfile, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_AlignmentFile, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -36638,7 +40832,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_Samfile = {
   #endif
 };
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupProxy(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_PileupColumn(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -36649,7 +40843,7 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupProxy(PyTypeObject *t, CYTH
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_PileupProxy(PyObject *o) {
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_PileupColumn(PyObject *o) {
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -36658,17 +40852,17 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_PileupProxy(PyObject *o) {
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11PileupProxy_tid(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11PileupProxy_3tid_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_12PileupColumn_reference_id(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_12reference_id_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11PileupProxy_n(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11PileupProxy_1n_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_12PileupColumn_nsegments(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_9nsegments_1__get__(o);
 }
 
-static int __pyx_setprop_5pysam_8csamfile_11PileupProxy_n(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+static int __pyx_setprop_5pysam_14calignmentfile_12PileupColumn_nsegments(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
   if (v) {
-    return __pyx_pw_5pysam_8csamfile_11PileupProxy_1n_3__set__(o, v);
+    return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_9nsegments_3__set__(o, v);
   }
   else {
     PyErr_SetString(PyExc_NotImplementedError, "__del__");
@@ -36676,32 +40870,77 @@ static int __pyx_setprop_5pysam_8csamfile_11PileupProxy_n(PyObject *o, PyObject
   }
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11PileupProxy_pos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11PileupProxy_3pos_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_12PileupColumn_reference_pos(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_13reference_pos_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_11PileupProxy_pileups(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11PileupProxy_7pileups_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_12PileupColumn_pileups(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_7pileups_1__get__(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_PileupProxy[] = {
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_12PileupColumn_pos(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_3pos_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_12PileupColumn_pos(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_3pos_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_12PileupColumn_tid(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_3tid_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_12PileupColumn_tid(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_3tid_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_12PileupColumn_n(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_1n_1__get__(o);
+}
+
+static int __pyx_setprop_5pysam_14calignmentfile_12PileupColumn_n(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
+  if (v) {
+    return __pyx_pw_5pysam_14calignmentfile_12PileupColumn_1n_3__set__(o, v);
+  }
+  else {
+    PyErr_SetString(PyExc_NotImplementedError, "__del__");
+    return -1;
+  }
+}
+
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_PileupColumn[] = {
   {0, 0, 0, 0}
 };
 
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_PileupProxy[] = {
-  {(char *)"tid", __pyx_getprop_5pysam_8csamfile_11PileupProxy_tid, 0, __Pyx_DOCSTR(__pyx_k_the_chromosome_ID_as_is_defined), 0},
-  {(char *)"n", __pyx_getprop_5pysam_8csamfile_11PileupProxy_n, __pyx_setprop_5pysam_8csamfile_11PileupProxy_n, __Pyx_DOCSTR(__pyx_k_number_of_reads_mapping_to_this), 0},
-  {(char *)"pos", __pyx_getprop_5pysam_8csamfile_11PileupProxy_pos, 0, 0, 0},
-  {(char *)"pileups", __pyx_getprop_5pysam_8csamfile_11PileupProxy_pileups, 0, __Pyx_DOCSTR(__pyx_k_list_of_reads_class_pysam_Pileup), 0},
+static struct PyGetSetDef __pyx_getsets_5pysam_14calignmentfile_PileupColumn[] = {
+  {(char *)"reference_id", __pyx_getprop_5pysam_14calignmentfile_12PileupColumn_reference_id, 0, __Pyx_DOCSTR(__pyx_k_the_reference_sequence_number_as), 0},
+  {(char *)"nsegments", __pyx_getprop_5pysam_14calignmentfile_12PileupColumn_nsegments, __pyx_setprop_5pysam_14calignmentfile_12PileupColumn_nsegments, __Pyx_DOCSTR(__pyx_k_number_of_reads_mapping_to_this), 0},
+  {(char *)"reference_pos", __pyx_getprop_5pysam_14calignmentfile_12PileupColumn_reference_pos, 0, __Pyx_DOCSTR(__pyx_k_the_position_in_the_reference_se), 0},
+  {(char *)"pileups", __pyx_getprop_5pysam_14calignmentfile_12PileupColumn_pileups, 0, __Pyx_DOCSTR(__pyx_k_list_of_reads_class_pysam_Pileup), 0},
+  {(char *)"pos", __pyx_getprop_5pysam_14calignmentfile_12PileupColumn_pos, __pyx_setprop_5pysam_14calignmentfile_12PileupColumn_pos, 0, 0},
+  {(char *)"tid", __pyx_getprop_5pysam_14calignmentfile_12PileupColumn_tid, __pyx_setprop_5pysam_14calignmentfile_12PileupColumn_tid, 0, 0},
+  {(char *)"n", __pyx_getprop_5pysam_14calignmentfile_12PileupColumn_n, __pyx_setprop_5pysam_14calignmentfile_12PileupColumn_n, 0, 0},
   {0, 0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_PileupProxy = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_PileupColumn = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.PileupProxy"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_PileupProxy), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.PileupColumn"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_PileupProxy, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_PileupColumn, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -36716,29 +40955,29 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_PileupProxy = {
   0, /*tp_as_mapping*/
   0, /*tp_hash*/
   0, /*tp_call*/
-  __pyx_pw_5pysam_8csamfile_11PileupProxy_3__str__, /*tp_str*/
+  __pyx_pw_5pysam_14calignmentfile_12PileupColumn_3__str__, /*tp_str*/
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("PileupProxy()\nA pileup column. A pileup column contains\n    all the reads that map to a certain target base.\n\n    tid\n        chromosome ID as is defined in the header\n    pos\n        the target base coordinate (0-based)\n    n\n        number of reads mapping to this column\n    pileups\n        list of reads (:class:`pysam.PileupRead`) aligned to this column\n\n    This class is a proxy for results returned by the samtools pileup engine.\n    If the underlying en [...]
+  __Pyx_DOCSTR("PileupColumn()\nA pileup of reads at a particular reference sequence postion\n    (:term:`column`). A pileup column contains all the reads that map\n    to a certain target base.\n\n    This class is a proxy for results returned by the samtools pileup\n    engine.  If the underlying engine iterator advances, the results\n    of this column will change.\n\n    "), /*tp_doc*/
   0, /*tp_traverse*/
   0, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
   0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_PileupProxy, /*tp_methods*/
+  __pyx_methods_5pysam_14calignmentfile_PileupColumn, /*tp_methods*/
   0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_PileupProxy, /*tp_getset*/
+  __pyx_getsets_5pysam_14calignmentfile_PileupColumn, /*tp_getset*/
   0, /*tp_base*/
   0, /*tp_dict*/
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_11PileupProxy_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_12PileupColumn_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_PileupProxy, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_PileupColumn, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -36755,8 +40994,8 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_PileupProxy = {
   #endif
 };
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupRead(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *p;
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_PileupRead(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_PileupRead *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -36764,13 +41003,13 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupRead(PyTypeObject *t, CYTHO
     o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
   }
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_PileupRead *)o);
-  p->_alignment = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)Py_None); Py_INCREF(Py_None);
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)o);
+  p->_alignment = ((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)Py_None); Py_INCREF(Py_None);
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_PileupRead(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *p = (struct __pyx_obj_5pysam_8csamfile_PileupRead *)o;
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_PileupRead(PyObject *o) {
+  struct __pyx_obj_5pysam_14calignmentfile_PileupRead *p = (struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -36781,73 +41020,78 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_PileupRead(PyObject *o) {
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static int __pyx_tp_traverse_5pysam_8csamfile_PileupRead(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_14calignmentfile_PileupRead(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *p = (struct __pyx_obj_5pysam_8csamfile_PileupRead *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_PileupRead *p = (struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)o;
   if (p->_alignment) {
     e = (*v)(((PyObject*)p->_alignment), a); if (e) return e;
   }
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_8csamfile_PileupRead(PyObject *o) {
+static int __pyx_tp_clear_5pysam_14calignmentfile_PileupRead(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *p = (struct __pyx_obj_5pysam_8csamfile_PileupRead *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_PileupRead *p = (struct __pyx_obj_5pysam_14calignmentfile_PileupRead *)o;
   tmp = ((PyObject*)p->_alignment);
-  p->_alignment = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)Py_None); Py_INCREF(Py_None);
+  p->_alignment = ((struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *)Py_None); Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   return 0;
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_alignment(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_9alignment_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_10PileupRead_alignment(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_10PileupRead_9alignment_1__get__(o);
+}
+
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_10PileupRead_query_position(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_10PileupRead_14query_position_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_qpos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_4qpos_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_10PileupRead_indel(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_10PileupRead_5indel_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_indel(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_5indel_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_10PileupRead_level(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_10PileupRead_5level_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_is_del(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_6is_del_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_10PileupRead_is_del(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_10PileupRead_6is_del_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_is_head(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_7is_head_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_10PileupRead_is_head(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_10PileupRead_7is_head_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_is_tail(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_7is_tail_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_10PileupRead_is_tail(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_10PileupRead_7is_tail_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_level(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_5level_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_10PileupRead_is_refskip(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_10PileupRead_10is_refskip_1__get__(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_PileupRead[] = {
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_PileupRead[] = {
   {0, 0, 0, 0}
 };
 
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_PileupRead[] = {
-  {(char *)"alignment", __pyx_getprop_5pysam_8csamfile_10PileupRead_alignment, 0, __Pyx_DOCSTR(__pyx_k_a_class_pysam_AlignedRead_object), 0},
-  {(char *)"qpos", __pyx_getprop_5pysam_8csamfile_10PileupRead_qpos, 0, __Pyx_DOCSTR(__pyx_k_position_of_the_read_base_at_the), 0},
-  {(char *)"indel", __pyx_getprop_5pysam_8csamfile_10PileupRead_indel, 0, __Pyx_DOCSTR(__pyx_k_indel_length_0_for_no_indel_posi), 0},
-  {(char *)"is_del", __pyx_getprop_5pysam_8csamfile_10PileupRead_is_del, 0, __Pyx_DOCSTR(__pyx_k_1_iff_the_base_on_the_padded_rea), 0},
-  {(char *)"is_head", __pyx_getprop_5pysam_8csamfile_10PileupRead_is_head, 0, 0, 0},
-  {(char *)"is_tail", __pyx_getprop_5pysam_8csamfile_10PileupRead_is_tail, 0, 0, 0},
-  {(char *)"level", __pyx_getprop_5pysam_8csamfile_10PileupRead_level, 0, 0, 0},
+static struct PyGetSetDef __pyx_getsets_5pysam_14calignmentfile_PileupRead[] = {
+  {(char *)"alignment", __pyx_getprop_5pysam_14calignmentfile_10PileupRead_alignment, 0, __Pyx_DOCSTR(__pyx_k_a_class_pysam_AlignedSegment_obj), 0},
+  {(char *)"query_position", __pyx_getprop_5pysam_14calignmentfile_10PileupRead_query_position, 0, __Pyx_DOCSTR(__pyx_k_position_of_the_read_base_at_the), 0},
+  {(char *)"indel", __pyx_getprop_5pysam_14calignmentfile_10PileupRead_indel, 0, __Pyx_DOCSTR(__pyx_k_indel_length_0_for_no_indel_posi), 0},
+  {(char *)"level", __pyx_getprop_5pysam_14calignmentfile_10PileupRead_level, 0, __Pyx_DOCSTR(__pyx_k_the_level_of_the_read_in_the_vie), 0},
+  {(char *)"is_del", __pyx_getprop_5pysam_14calignmentfile_10PileupRead_is_del, 0, __Pyx_DOCSTR(__pyx_k_1_iff_the_base_on_the_padded_rea), 0},
+  {(char *)"is_head", __pyx_getprop_5pysam_14calignmentfile_10PileupRead_is_head, 0, 0, 0},
+  {(char *)"is_tail", __pyx_getprop_5pysam_14calignmentfile_10PileupRead_is_tail, 0, 0, 0},
+  {(char *)"is_refskip", __pyx_getprop_5pysam_14calignmentfile_10PileupRead_is_refskip, 0, 0, 0},
   {0, 0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_PileupRead = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_PileupRead = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.PileupRead"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_PileupRead), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.PileupRead"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_PileupRead), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_PileupRead, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_PileupRead, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -36862,29 +41106,29 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_PileupRead = {
   0, /*tp_as_mapping*/
   0, /*tp_hash*/
   0, /*tp_call*/
-  __pyx_pw_5pysam_8csamfile_10PileupRead_3__str__, /*tp_str*/
+  __pyx_pw_5pysam_14calignmentfile_10PileupRead_3__str__, /*tp_str*/
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("PileupRead()\nA read aligned to a column.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_PileupRead, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_PileupRead, /*tp_clear*/
+  __Pyx_DOCSTR("PileupRead()\nRepresentation of a read aligned to a particular position in the\n    reference sequence.\n\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_14calignmentfile_PileupRead, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_PileupRead, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
   0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_PileupRead, /*tp_methods*/
+  __pyx_methods_5pysam_14calignmentfile_PileupRead, /*tp_methods*/
   0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_PileupRead, /*tp_getset*/
+  __pyx_getsets_5pysam_14calignmentfile_PileupRead, /*tp_getset*/
   0, /*tp_base*/
   0, /*tp_dict*/
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_10PileupRead_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_10PileupRead_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_PileupRead, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_PileupRead, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -36901,8 +41145,8 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_PileupRead = {
   #endif
 };
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRow(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow *p;
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRow(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -36910,13 +41154,13 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRow(PyTypeObject *t, CYTH
     o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
   }
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRow *)o);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *)o);
+  p->samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)Py_None); Py_INCREF(Py_None);
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRow *)o;
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRow(PyObject *o) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -36927,7 +41171,7 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_11IteratorRow_3__dealloc__(o);
+    __pyx_pw_5pysam_14calignmentfile_11IteratorRow_3__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
@@ -36935,34 +41179,34 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(PyObject *o) {
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static int __pyx_tp_traverse_5pysam_8csamfile_IteratorRow(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRow(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRow *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *)o;
   if (p->samfile) {
     e = (*v)(((PyObject*)p->samfile), a); if (e) return e;
   }
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_8csamfile_IteratorRow(PyObject *o) {
+static int __pyx_tp_clear_5pysam_14calignmentfile_IteratorRow(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRow *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorRow *)o;
   tmp = ((PyObject*)p->samfile);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
+  p->samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)Py_None); Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   return 0;
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRow[] = {
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IteratorRow[] = {
   {0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRow = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IteratorRow = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRow"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRow), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IteratorRow"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRow), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRow, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -36982,14 +41226,14 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRow = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRow(Samfile samfile, int reopen=True)\nabstract base class for iterators over mapped reads.\n\n    Various iterators implement different behaviours for wrapping around\n    contig boundaries. Examples include:\n\n    :class:`pysam.IteratorRowRegion`\n        iterate within a single contig and a defined region.\n\n    :class:`pysam.IteratorRowAll`\n        iterate until EOF. This iterator will also include unmapped reads.\n\n    :class:`pysam.IteratorRowAllRefs`\n  [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRow, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow, /*tp_clear*/
+  __Pyx_DOCSTR("IteratorRow(AlignmentFile samfile, int multiple_iterators=False)\nabstract base class for iterators over mapped reads.\n\n    Various iterators implement different behaviours for wrapping around\n    contig boundaries. Examples include:\n\n    :class:`pysam.IteratorRowRegion`\n        iterate within a single contig and a defined region.\n\n    :class:`pysam.IteratorRowAll`\n        iterate until EOF. This iterator will also include unmapped reads.\n\n    :class:`pysam.Ite [...]
+  __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRow, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorRow, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
   0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRow, /*tp_methods*/
+  __pyx_methods_5pysam_14calignmentfile_IteratorRow, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
   0, /*tp_base*/
@@ -36997,9 +41241,9 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRow = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_11IteratorRow_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRow, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IteratorRow, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37015,18 +41259,18 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRow = {
   0, /*tp_finalize*/
   #endif
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion __pyx_vtable_5pysam_8csamfile_IteratorRowRegion;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion __pyx_vtable_5pysam_14calignmentfile_IteratorRowRegion;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowRegion(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowRegion(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *p;
+  PyObject *o = __pyx_tp_new_5pysam_14calignmentfile_IteratorRow(t, a, k);
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorRowRegion;
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)o);
+  p->__pyx_vtab = __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowRegion;
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowRegion(PyObject *o) {
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRowRegion(PyObject *o) {
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -37037,25 +41281,25 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowRegion(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_7__dealloc__(o);
+    __pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_7__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
   PyObject_GC_Track(o);
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(o);
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRow(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowRegion[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_17IteratorRowRegion_4__next__)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IteratorRowRegion[] = {
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_17IteratorRowRegion_4__next__)},
   {0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowRegion = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IteratorRowRegion = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowRegion"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IteratorRowRegion"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowRegion, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRowRegion, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -37075,14 +41319,14 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowRegion = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowRegion(Samfile samfile, int tid, int beg, int end, int reopen=True)\n*(Samfile samfile, int tid, int beg, int end, int reopen = True )*\n\n    iterate over mapped reads in a region.\n\n    By default, the file is re-openend to avoid conflicts between\n    multiple iterators working on the same file. Set *reopen* = False\n    to not re-open *samfile*.\n\n    The samtools iterators assume that the file\n    position between iterations do not change.\n    As a con [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRow, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow, /*tp_clear*/
+  __Pyx_DOCSTR("IteratorRowRegion(AlignmentFile samfile, int tid, int beg, int end, int multiple_iterators=False)\n*(AlignmentFile samfile, int tid, int beg, int end,\n    int multiple_iterators=False)*\n\n    iterate over mapped reads in a region.\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`AlignmentFile.fetch`.\n\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRow, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorRow, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_3__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_5__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowRegion, /*tp_methods*/
+  __pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_3__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_5__next__, /*tp_iternext*/
+  __pyx_methods_5pysam_14calignmentfile_IteratorRowRegion, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
   0, /*tp_base*/
@@ -37090,9 +41334,9 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowRegion = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_17IteratorRowRegion_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowRegion, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IteratorRowRegion, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37108,28 +41352,28 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowRegion = {
   0, /*tp_finalize*/
   #endif
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowHead __pyx_vtable_5pysam_8csamfile_IteratorRowHead;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowHead __pyx_vtable_5pysam_14calignmentfile_IteratorRowHead;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowHead(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowHead(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *p;
+  PyObject *o = __pyx_tp_new_5pysam_14calignmentfile_IteratorRow(t, a, k);
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorRowHead;
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *)o);
+  p->__pyx_vtab = __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowHead;
   return o;
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowHead[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_15IteratorRowHead_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_15IteratorRowHead_4__next__)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IteratorRowHead[] = {
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_15IteratorRowHead_4__next__)},
   {0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowHead = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IteratorRowHead = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowHead"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IteratorRowHead"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRow, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -37149,14 +41393,14 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowHead = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowHead(Samfile samfile, int n, int reopen=True)\n*(Samfile samfile, n, int reopen = True)*\n\n    iterate over first n reads in *samfile*\n\n    By default, the file is re-openend to avoid conflicts between\n    multiple iterators working on the same file. Set *reopen* = False\n    to not re-open *samfile*.\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`S [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRow, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow, /*tp_clear*/
+  __Pyx_DOCSTR("IteratorRowHead(AlignmentFile samfile, int n, int multiple_iterators=False)\n*(AlignmentFile samfile, n, int multiple_iterators=False)*\n\n    iterate over first n reads in *samfile*\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitly. It is returned as a result of call to a :meth:`AlignmentFile.head`.\n        \n\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRow, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorRow, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_15IteratorRowHead_3__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_15IteratorRowHead_5__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowHead, /*tp_methods*/
+  __pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_3__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_5__next__, /*tp_iternext*/
+  __pyx_methods_5pysam_14calignmentfile_IteratorRowHead, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
   0, /*tp_base*/
@@ -37164,9 +41408,9 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowHead = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_15IteratorRowHead_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowHead, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IteratorRowHead, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37182,28 +41426,28 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowHead = {
   0, /*tp_finalize*/
   #endif
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowAll __pyx_vtable_5pysam_8csamfile_IteratorRowAll;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowAll __pyx_vtable_5pysam_14calignmentfile_IteratorRowAll;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowAll(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowAll(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *p;
+  PyObject *o = __pyx_tp_new_5pysam_14calignmentfile_IteratorRow(t, a, k);
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorRowAll;
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *)o);
+  p->__pyx_vtab = __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowAll;
   return o;
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowAll[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_14IteratorRowAll_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_14IteratorRowAll_4__next__)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IteratorRowAll[] = {
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14IteratorRowAll_4__next__)},
   {0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAll = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IteratorRowAll = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowAll"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IteratorRowAll"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRow, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -37223,14 +41467,14 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAll = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowAll(Samfile samfile, int reopen=True)\n*(Samfile samfile, int reopen = True)*\n\n    iterate over all reads in *samfile*\n\n    By default, the file is re-openend to avoid conflicts between\n    multiple iterators working on the same file. Set *reopen* = False\n    to not re-open *samfile*.\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.\ [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRow, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow, /*tp_clear*/
+  __Pyx_DOCSTR("IteratorRowAll(AlignmentFile samfile, int multiple_iterators=False)\n*(AlignmentFile samfile, int multiple_iterators=False)*\n\n    iterate over all reads in *samfile*\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitly. It is returned as a result of call to a :meth:`AlignmentFile.fetch`.\n        \n\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRow, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorRow, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_14IteratorRowAll_3__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_14IteratorRowAll_5__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowAll, /*tp_methods*/
+  __pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_3__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_5__next__, /*tp_iternext*/
+  __pyx_methods_5pysam_14calignmentfile_IteratorRowAll, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
   0, /*tp_base*/
@@ -37238,9 +41482,9 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAll = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_14IteratorRowAll_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowAll, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IteratorRowAll, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37257,17 +41501,17 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAll = {
   #endif
 };
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowAllRefs(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowAllRefs(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *p;
+  PyObject *o = __pyx_tp_new_5pysam_14calignmentfile_IteratorRow(t, a, k);
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)o);
-  p->rowiter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *)o);
+  p->rowiter = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowAllRefs(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)o;
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRowAllRefs(PyObject *o) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -37276,41 +41520,41 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowAllRefs(PyObject *o) {
   PyObject_GC_UnTrack(o);
   Py_CLEAR(p->rowiter);
   PyObject_GC_Track(o);
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(o);
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRow(o);
 }
 
-static int __pyx_tp_traverse_5pysam_8csamfile_IteratorRowAllRefs(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRowAllRefs(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)o;
-  e = __pyx_tp_traverse_5pysam_8csamfile_IteratorRow(o, v, a); if (e) return e;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *)o;
+  e = __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRow(o, v, a); if (e) return e;
   if (p->rowiter) {
     e = (*v)(((PyObject*)p->rowiter), a); if (e) return e;
   }
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_8csamfile_IteratorRowAllRefs(PyObject *o) {
+static int __pyx_tp_clear_5pysam_14calignmentfile_IteratorRowAllRefs(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)o;
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow(o);
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs *)o;
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorRow(o);
   tmp = ((PyObject*)p->rowiter);
-  p->rowiter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
+  p->rowiter = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   return 0;
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowAllRefs[] = {
-  {__Pyx_NAMESTR("nextiter"), (PyCFunction)__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_3nextiter, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter)},
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_7__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_6__next__)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IteratorRowAllRefs[] = {
+  {__Pyx_NAMESTR("nextiter"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_3nextiter, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_18IteratorRowAllRefs_2nextiter)},
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_7__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__)},
   {0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAllRefs = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IteratorRowAllRefs = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowAllRefs"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IteratorRowAllRefs"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowAllRefs, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRowAllRefs, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -37330,14 +41574,14 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAllRefs = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowAllRefs(Samfile samfile, reopen=True)\niterates over all mapped reads by chaining iterators over each reference\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRowAllRefs, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRowAllRefs, /*tp_clear*/
+  __Pyx_DOCSTR("IteratorRowAllRefs(AlignmentFile samfile, multiple_iterators=False)\niterates over all mapped reads by chaining iterators over each reference\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`AlignmentFile.fetch`.\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRowAllRefs, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorRowAllRefs, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_5__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_7__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowAllRefs, /*tp_methods*/
+  __pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_5__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_7__next__, /*tp_iternext*/
+  __pyx_methods_5pysam_14calignmentfile_IteratorRowAllRefs, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
   0, /*tp_base*/
@@ -37345,9 +41589,9 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAllRefs = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_18IteratorRowAllRefs_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowAllRefs, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IteratorRowAllRefs, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37363,20 +41607,20 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAllRefs = {
   0, /*tp_finalize*/
   #endif
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection __pyx_vtable_5pysam_8csamfile_IteratorRowSelection;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowSelection __pyx_vtable_5pysam_14calignmentfile_IteratorRowSelection;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowSelection(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorRowSelection(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *p;
+  PyObject *o = __pyx_tp_new_5pysam_14calignmentfile_IteratorRow(t, a, k);
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorRowSelection;
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *)o);
+  p->__pyx_vtab = __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowSelection;
   p->positions = Py_None; Py_INCREF(Py_None);
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowSelection(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)o;
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRowSelection(PyObject *o) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -37385,40 +41629,40 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowSelection(PyObject *o)
   PyObject_GC_UnTrack(o);
   Py_CLEAR(p->positions);
   PyObject_GC_Track(o);
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(o);
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRow(o);
 }
 
-static int __pyx_tp_traverse_5pysam_8csamfile_IteratorRowSelection(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRowSelection(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)o;
-  e = __pyx_tp_traverse_5pysam_8csamfile_IteratorRow(o, v, a); if (e) return e;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *)o;
+  e = __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRow(o, v, a); if (e) return e;
   if (p->positions) {
     e = (*v)(p->positions, a); if (e) return e;
   }
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_8csamfile_IteratorRowSelection(PyObject *o) {
+static int __pyx_tp_clear_5pysam_14calignmentfile_IteratorRowSelection(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)o;
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow(o);
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *)o;
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorRow(o);
   tmp = ((PyObject*)p->positions);
   p->positions = Py_None; Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   return 0;
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowSelection[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_20IteratorRowSelection_4__next__)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IteratorRowSelection[] = {
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_20IteratorRowSelection_4__next__)},
   {0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowSelection = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IteratorRowSelection = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowSelection"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IteratorRowSelection"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowSelection, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorRowSelection, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -37438,14 +41682,14 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowSelection = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowSelection(Samfile samfile, positions, int reopen=True)\n*(Samfile samfile)*\n\n    iterate over reads in *samfile* at a given list of file positions.\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRowSelection, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRowSelection, /*tp_clear*/
+  __Pyx_DOCSTR("IteratorRowSelection(AlignmentFile samfile, positions, int multiple_iterators=True)\n*(AlignmentFile samfile)*\n\n    iterate over reads in *samfile* at a given list of file positions.\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`AlignmentFile.fetch`.\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_14calignmentfile_IteratorRowSelection, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorRowSelection, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_3__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_5__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowSelection, /*tp_methods*/
+  __pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_3__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_5__next__, /*tp_iternext*/
+  __pyx_methods_5pysam_14calignmentfile_IteratorRowSelection, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
   0, /*tp_base*/
@@ -37453,9 +41697,9 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowSelection = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_20IteratorRowSelection_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowSelection, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IteratorRowSelection, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37471,10 +41715,10 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowSelection = {
   0, /*tp_finalize*/
   #endif
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn __pyx_vtable_5pysam_8csamfile_IteratorColumn;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn __pyx_vtable_5pysam_14calignmentfile_IteratorColumn;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumn(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn *p;
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorColumn(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -37482,20 +41726,20 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumn(PyTypeObject *t, P
     o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
   }
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorColumn;
-  p->iter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)o);
+  p->__pyx_vtab = __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumn;
+  p->iter = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
+  p->samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)Py_None); Py_INCREF(Py_None);
   p->fastafile = ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)Py_None); Py_INCREF(Py_None);
   p->stepper = Py_None; Py_INCREF(Py_None);
-  if (unlikely(__pyx_pw_5pysam_8csamfile_14IteratorColumn_1__cinit__(o, a, k) < 0)) {
+  if (unlikely(__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_1__cinit__(o, a, k) < 0)) {
     Py_DECREF(o); o = 0;
   }
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn *p = (struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)o;
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorColumn(PyObject *o) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -37506,7 +41750,7 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_14IteratorColumn_9__dealloc__(o);
+    __pyx_pw_5pysam_14calignmentfile_14IteratorColumn_9__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
@@ -37517,9 +41761,9 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn(PyObject *o) {
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static int __pyx_tp_traverse_5pysam_8csamfile_IteratorColumn(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_14calignmentfile_IteratorColumn(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn *p = (struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)o;
   if (p->iter) {
     e = (*v)(((PyObject*)p->iter), a); if (e) return e;
   }
@@ -37535,14 +41779,14 @@ static int __pyx_tp_traverse_5pysam_8csamfile_IteratorColumn(PyObject *o, visitp
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_8csamfile_IteratorColumn(PyObject *o) {
+static int __pyx_tp_clear_5pysam_14calignmentfile_IteratorColumn(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn *p = (struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *p = (struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *)o;
   tmp = ((PyObject*)p->iter);
-  p->iter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
+  p->iter = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   tmp = ((PyObject*)p->samfile);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
+  p->samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)Py_None); Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   tmp = ((PyObject*)p->fastafile);
   p->fastafile = ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)Py_None); Py_INCREF(Py_None);
@@ -37553,27 +41797,27 @@ static int __pyx_tp_clear_5pysam_8csamfile_IteratorColumn(PyObject *o) {
   return 0;
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_14IteratorColumn_seq_len(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_14IteratorColumn_7seq_len_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_14IteratorColumn_seq_len(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_14IteratorColumn_7seq_len_1__get__(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorColumn[] = {
-  {__Pyx_NAMESTR("addReference"), (PyCFunction)__pyx_pw_5pysam_8csamfile_14IteratorColumn_5addReference, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_14IteratorColumn_4addReference)},
-  {__Pyx_NAMESTR("hasReference"), (PyCFunction)__pyx_pw_5pysam_8csamfile_14IteratorColumn_7hasReference, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_14IteratorColumn_6hasReference)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IteratorColumn[] = {
+  {__Pyx_NAMESTR("addReference"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_5addReference, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14IteratorColumn_4addReference)},
+  {__Pyx_NAMESTR("hasReference"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_14IteratorColumn_7hasReference, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_14IteratorColumn_6hasReference)},
   {0, 0, 0, 0}
 };
 
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_IteratorColumn[] = {
-  {(char *)"seq_len", __pyx_getprop_5pysam_8csamfile_14IteratorColumn_seq_len, 0, __Pyx_DOCSTR(__pyx_k_current_sequence_length), 0},
+static struct PyGetSetDef __pyx_getsets_5pysam_14calignmentfile_IteratorColumn[] = {
+  {(char *)"seq_len", __pyx_getprop_5pysam_14calignmentfile_14IteratorColumn_seq_len, 0, __Pyx_DOCSTR(__pyx_k_current_sequence_length), 0},
   {0, 0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumn = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IteratorColumn = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorColumn"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorColumn), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IteratorColumn"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorColumn, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -37593,16 +41837,16 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumn = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("abstract base class for iterators over columns.\n\n    IteratorColumn objects wrap the pileup functionality of samtools.\n\n    For reasons of efficiency, the iterator points to the current\n    pileup buffer. The pileup buffer is updated at every iteration.\n    This might cause some unexpected behavious. For example,\n    consider the conversion to a list::\n\n       f = Samfile(\"file.bam\", \"rb\")\n       result = list( f.pileup() )\n\n    Here, ``result`` will conta [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorColumn, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorColumn, /*tp_clear*/
+  __Pyx_DOCSTR("abstract base class for iterators over columns.\n\n    IteratorColumn objects wrap the pileup functionality of samtools.\n\n    For reasons of efficiency, the iterator points to the current\n    pileup buffer. The pileup buffer is updated at every iteration.\n    This might cause some unexpected behavious. For example,\n    consider the conversion to a list::\n\n       f = AlignmentFile(\"file.bam\", \"rb\")\n       result = list( f.pileup() )\n\n    Here, ``result`` will [...]
+  __pyx_tp_traverse_5pysam_14calignmentfile_IteratorColumn, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorColumn, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_14calignmentfile_14IteratorColumn_3__iter__, /*tp_iter*/
   0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorColumn, /*tp_methods*/
+  __pyx_methods_5pysam_14calignmentfile_IteratorColumn, /*tp_methods*/
   0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_IteratorColumn, /*tp_getset*/
+  __pyx_getsets_5pysam_14calignmentfile_IteratorColumn, /*tp_getset*/
   0, /*tp_base*/
   0, /*tp_dict*/
   0, /*tp_descr_get*/
@@ -37610,7 +41854,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumn = {
   0, /*tp_dictoffset*/
   0, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorColumn, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IteratorColumn, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37626,31 +41870,31 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumn = {
   0, /*tp_finalize*/
   #endif
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion __pyx_vtable_5pysam_8csamfile_IteratorColumnRegion;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnRegion __pyx_vtable_5pysam_14calignmentfile_IteratorColumnRegion;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumnRegion(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorColumn(t, a, k);
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorColumnRegion(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion *p;
+  PyObject *o = __pyx_tp_new_5pysam_14calignmentfile_IteratorColumn(t, a, k);
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *)o);
-  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn*)__pyx_vtabptr_5pysam_8csamfile_IteratorColumnRegion;
-  if (unlikely(__pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(o, a, k) < 0)) {
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion *)o);
+  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn*)__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnRegion;
+  if (unlikely(__pyx_pw_5pysam_14calignmentfile_20IteratorColumnRegion_1__cinit__(o, a, k) < 0)) {
     Py_DECREF(o); o = 0;
   }
   return o;
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorColumnRegion[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_3__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_20IteratorColumnRegion_2__next__)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IteratorColumnRegion[] = {
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_20IteratorColumnRegion_3__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__)},
   {0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnRegion = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IteratorColumnRegion = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorColumnRegion"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IteratorColumnRegion"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorColumn, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -37671,17 +41915,17 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnRegion = {
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
   __Pyx_DOCSTR("iterates over a region only.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorColumn, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorColumn, /*tp_clear*/
+  __pyx_tp_traverse_5pysam_14calignmentfile_IteratorColumn, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorColumn, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   #if CYTHON_COMPILING_IN_PYPY
-  __pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_14calignmentfile_14IteratorColumn_3__iter__, /*tp_iter*/
   #else
   0, /*tp_iter*/
   #endif
-  __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_3__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorColumnRegion, /*tp_methods*/
+  __pyx_pw_5pysam_14calignmentfile_20IteratorColumnRegion_3__next__, /*tp_iternext*/
+  __pyx_methods_5pysam_14calignmentfile_IteratorColumnRegion, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
   0, /*tp_base*/
@@ -37691,7 +41935,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnRegion = {
   0, /*tp_dictoffset*/
   0, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorColumnRegion, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IteratorColumnRegion, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37707,31 +41951,31 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnRegion = {
   0, /*tp_finalize*/
   #endif
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs __pyx_vtable_5pysam_8csamfile_IteratorColumnAllRefs;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnAllRefs __pyx_vtable_5pysam_14calignmentfile_IteratorColumnAllRefs;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumnAllRefs(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorColumn(t, a, k);
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IteratorColumnAllRefs(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs *p;
+  PyObject *o = __pyx_tp_new_5pysam_14calignmentfile_IteratorColumn(t, a, k);
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *)o);
-  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn*)__pyx_vtabptr_5pysam_8csamfile_IteratorColumnAllRefs;
-  if (unlikely(__pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_1__cinit__(o, a, k) < 0)) {
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs *)o);
+  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn*)__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnAllRefs;
+  if (unlikely(__pyx_pw_5pysam_14calignmentfile_21IteratorColumnAllRefs_1__cinit__(o, a, k) < 0)) {
     Py_DECREF(o); o = 0;
   }
   return o;
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorColumnAllRefs[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_3__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IteratorColumnAllRefs[] = {
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_21IteratorColumnAllRefs_3__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__)},
   {0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnAllRefs = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IteratorColumnAllRefs = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorColumnAllRefs"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IteratorColumnAllRefs"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IteratorColumn, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -37752,17 +41996,17 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnAllRefs = {
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
   __Pyx_DOCSTR("iterates over all columns by chaining iterators over each reference\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorColumn, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorColumn, /*tp_clear*/
+  __pyx_tp_traverse_5pysam_14calignmentfile_IteratorColumn, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IteratorColumn, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   #if CYTHON_COMPILING_IN_PYPY
-  __pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_14calignmentfile_14IteratorColumn_3__iter__, /*tp_iter*/
   #else
   0, /*tp_iter*/
   #endif
-  __pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_3__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorColumnAllRefs, /*tp_methods*/
+  __pyx_pw_5pysam_14calignmentfile_21IteratorColumnAllRefs_3__next__, /*tp_iternext*/
+  __pyx_methods_5pysam_14calignmentfile_IteratorColumnAllRefs, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
   0, /*tp_base*/
@@ -37772,7 +42016,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnAllRefs = {
   0, /*tp_dictoffset*/
   0, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorColumnAllRefs, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IteratorColumnAllRefs, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37789,8 +42033,8 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnAllRefs = {
   #endif
 };
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IndexedReads(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IndexedReads *p;
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_IndexedReads(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
+  struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -37798,14 +42042,14 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile_IndexedReads(PyTypeObject *t, CYT
     o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
   }
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)o);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
+  p = ((struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *)o);
+  p->samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)Py_None); Py_INCREF(Py_None);
   p->index = Py_None; Py_INCREF(Py_None);
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_IndexedReads(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IndexedReads *p = (struct __pyx_obj_5pysam_8csamfile_IndexedReads *)o;
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_IndexedReads(PyObject *o) {
+  struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *p = (struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -37816,7 +42060,7 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_IndexedReads(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_12IndexedReads_7__dealloc__(o);
+    __pyx_pw_5pysam_14calignmentfile_12IndexedReads_7__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
@@ -37825,9 +42069,9 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_IndexedReads(PyObject *o) {
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static int __pyx_tp_traverse_5pysam_8csamfile_IndexedReads(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_14calignmentfile_IndexedReads(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_8csamfile_IndexedReads *p = (struct __pyx_obj_5pysam_8csamfile_IndexedReads *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *p = (struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *)o;
   if (p->samfile) {
     e = (*v)(((PyObject*)p->samfile), a); if (e) return e;
   }
@@ -37837,11 +42081,11 @@ static int __pyx_tp_traverse_5pysam_8csamfile_IndexedReads(PyObject *o, visitpro
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_8csamfile_IndexedReads(PyObject *o) {
+static int __pyx_tp_clear_5pysam_14calignmentfile_IndexedReads(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IndexedReads *p = (struct __pyx_obj_5pysam_8csamfile_IndexedReads *)o;
+  struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *p = (struct __pyx_obj_5pysam_14calignmentfile_IndexedReads *)o;
   tmp = ((PyObject*)p->samfile);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
+  p->samfile = ((struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *)Py_None); Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   tmp = ((PyObject*)p->index);
   p->index = Py_None; Py_INCREF(Py_None);
@@ -37849,18 +42093,18 @@ static int __pyx_tp_clear_5pysam_8csamfile_IndexedReads(PyObject *o) {
   return 0;
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IndexedReads[] = {
-  {__Pyx_NAMESTR("build"), (PyCFunction)__pyx_pw_5pysam_8csamfile_12IndexedReads_3build, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_12IndexedReads_2build)},
-  {__Pyx_NAMESTR("find"), (PyCFunction)__pyx_pw_5pysam_8csamfile_12IndexedReads_5find, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_12IndexedReads_4find)},
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_IndexedReads[] = {
+  {__Pyx_NAMESTR("build"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_12IndexedReads_3build, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_12IndexedReads_2build)},
+  {__Pyx_NAMESTR("find"), (PyCFunction)__pyx_pw_5pysam_14calignmentfile_12IndexedReads_5find, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_14calignmentfile_12IndexedReads_4find)},
   {0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_IndexedReads = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_IndexedReads = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IndexedReads"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IndexedReads), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.IndexedReads"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IndexedReads, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_IndexedReads, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -37880,14 +42124,14 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IndexedReads = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IndexedReads(Samfile samfile, int reopen=True)\nindex a bamfile by read.\n\n    The index is kept in memory.\n\n    By default, the file is re-openend to avoid conflicts if\n    multiple operators work on the same file. Set *reopen* = False\n    to not re-open *samfile*.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IndexedReads, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IndexedReads, /*tp_clear*/
+  __Pyx_DOCSTR("IndexedReads(AlignmentFile samfile, int multiple_iterators=True)\nindex a Sam/BAM-file by query name.\n\n    The index is kept in memory and can be substantial.\n\n    By default, the file is re-openend to avoid conflicts if multiple\n    operators work on the same file. Set *multiple_iterators* = False\n    to not re-open *samfile*.\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_14calignmentfile_IndexedReads, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile_IndexedReads, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
   0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IndexedReads, /*tp_methods*/
+  __pyx_methods_5pysam_14calignmentfile_IndexedReads, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
   0, /*tp_base*/
@@ -37895,9 +42139,9 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IndexedReads = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__, /*tp_init*/
+  __pyx_pw_5pysam_14calignmentfile_12IndexedReads_1__init__, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IndexedReads, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_IndexedReads, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -37914,7 +42158,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_IndexedReads = {
   #endif
 };
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile_SNPCall(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile_SNPCall(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -37925,7 +42169,7 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile_SNPCall(PyTypeObject *t, CYTHON_U
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile_SNPCall(PyObject *o) {
+static void __pyx_tp_dealloc_5pysam_14calignmentfile_SNPCall(PyObject *o) {
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -37934,60 +42178,60 @@ static void __pyx_tp_dealloc_5pysam_8csamfile_SNPCall(PyObject *o) {
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_tid(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_3tid_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_7SNPCall_tid(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_7SNPCall_3tid_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_pos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_3pos_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_7SNPCall_pos(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_7SNPCall_3pos_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_reference_base(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_14reference_base_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_7SNPCall_reference_base(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_7SNPCall_14reference_base_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_genotype(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_8genotype_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_7SNPCall_genotype(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_7SNPCall_8genotype_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_consensus_quality(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_17consensus_quality_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_7SNPCall_consensus_quality(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_7SNPCall_17consensus_quality_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_snp_quality(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_11snp_quality_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_7SNPCall_snp_quality(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_7SNPCall_11snp_quality_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_mapping_quality(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_15mapping_quality_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_7SNPCall_mapping_quality(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_7SNPCall_15mapping_quality_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_coverage(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_8coverage_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_14calignmentfile_7SNPCall_coverage(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_14calignmentfile_7SNPCall_8coverage_1__get__(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_8csamfile_SNPCall[] = {
+static PyMethodDef __pyx_methods_5pysam_14calignmentfile_SNPCall[] = {
   {0, 0, 0, 0}
 };
 
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_SNPCall[] = {
-  {(char *)"tid", __pyx_getprop_5pysam_8csamfile_7SNPCall_tid, 0, __Pyx_DOCSTR(__pyx_k_the_chromosome_ID_as_is_defined), 0},
-  {(char *)"pos", __pyx_getprop_5pysam_8csamfile_7SNPCall_pos, 0, __Pyx_DOCSTR(__pyx_k_nucleotide_position_of_SNP), 0},
-  {(char *)"reference_base", __pyx_getprop_5pysam_8csamfile_7SNPCall_reference_base, 0, __Pyx_DOCSTR(__pyx_k_reference_base_at_pos_N_if_no_re), 0},
-  {(char *)"genotype", __pyx_getprop_5pysam_8csamfile_7SNPCall_genotype, 0, __Pyx_DOCSTR(__pyx_k_the_genotype_called), 0},
-  {(char *)"consensus_quality", __pyx_getprop_5pysam_8csamfile_7SNPCall_consensus_quality, 0, __Pyx_DOCSTR(__pyx_k_the_genotype_quality_Phred_scale), 0},
-  {(char *)"snp_quality", __pyx_getprop_5pysam_8csamfile_7SNPCall_snp_quality, 0, __Pyx_DOCSTR(__pyx_k_the_snp_quality_Phred_scaled_pro), 0},
-  {(char *)"mapping_quality", __pyx_getprop_5pysam_8csamfile_7SNPCall_mapping_quality, 0, __Pyx_DOCSTR(__pyx_k_the_root_mean_square_rms_of_the), 0},
-  {(char *)"coverage", __pyx_getprop_5pysam_8csamfile_7SNPCall_coverage, 0, __Pyx_DOCSTR(__pyx_k_coverage_or_read_depth_the_numbe), 0},
+static struct PyGetSetDef __pyx_getsets_5pysam_14calignmentfile_SNPCall[] = {
+  {(char *)"tid", __pyx_getprop_5pysam_14calignmentfile_7SNPCall_tid, 0, __Pyx_DOCSTR(__pyx_k_the_chromosome_ID_as_is_defined), 0},
+  {(char *)"pos", __pyx_getprop_5pysam_14calignmentfile_7SNPCall_pos, 0, __Pyx_DOCSTR(__pyx_k_nucleotide_position_of_SNP), 0},
+  {(char *)"reference_base", __pyx_getprop_5pysam_14calignmentfile_7SNPCall_reference_base, 0, __Pyx_DOCSTR(__pyx_k_reference_base_at_pos_N_if_no_re), 0},
+  {(char *)"genotype", __pyx_getprop_5pysam_14calignmentfile_7SNPCall_genotype, 0, __Pyx_DOCSTR(__pyx_k_the_genotype_called), 0},
+  {(char *)"consensus_quality", __pyx_getprop_5pysam_14calignmentfile_7SNPCall_consensus_quality, 0, __Pyx_DOCSTR(__pyx_k_the_genotype_quality_Phred_scale), 0},
+  {(char *)"snp_quality", __pyx_getprop_5pysam_14calignmentfile_7SNPCall_snp_quality, 0, __Pyx_DOCSTR(__pyx_k_the_snp_quality_Phred_scaled_pro), 0},
+  {(char *)"mapping_quality", __pyx_getprop_5pysam_14calignmentfile_7SNPCall_mapping_quality, 0, __Pyx_DOCSTR(__pyx_k_the_root_mean_square_rms_of_the), 0},
+  {(char *)"coverage", __pyx_getprop_5pysam_14calignmentfile_7SNPCall_coverage, 0, __Pyx_DOCSTR(__pyx_k_coverage_or_read_depth_the_numbe), 0},
   {0, 0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_8csamfile_SNPCall = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile_SNPCall = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.SNPCall"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_SNPCall), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.SNPCall"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile_SNPCall), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_SNPCall, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile_SNPCall, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -38002,7 +42246,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_SNPCall = {
   0, /*tp_as_mapping*/
   0, /*tp_hash*/
   0, /*tp_call*/
-  __pyx_pw_5pysam_8csamfile_7SNPCall_1__str__, /*tp_str*/
+  __pyx_pw_5pysam_14calignmentfile_7SNPCall_1__str__, /*tp_str*/
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
@@ -38014,9 +42258,9 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_SNPCall = {
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
   0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_SNPCall, /*tp_methods*/
+  __pyx_methods_5pysam_14calignmentfile_SNPCall, /*tp_methods*/
   0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_SNPCall, /*tp_getset*/
+  __pyx_getsets_5pysam_14calignmentfile_SNPCall, /*tp_getset*/
   0, /*tp_base*/
   0, /*tp_dict*/
   0, /*tp_descr_get*/
@@ -38024,7 +42268,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_SNPCall = {
   0, /*tp_dictoffset*/
   0, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_SNPCall, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile_SNPCall, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -38041,14 +42285,14 @@ static PyTypeObject __pyx_type_5pysam_8csamfile_SNPCall = {
   #endif
 };
 
-static struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *__pyx_freelist_5pysam_8csamfile___pyx_scope_struct__genexpr[8];
-static int __pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr = 0;
+static struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *__pyx_freelist_5pysam_14calignmentfile___pyx_scope_struct__genexpr[8];
+static int __pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct__genexpr = 0;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct__genexpr(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile___pyx_scope_struct__genexpr(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
   PyObject *o;
-  if (likely((__pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr > 0) & (t->tp_basicsize == sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr)))) {
-    o = (PyObject*)__pyx_freelist_5pysam_8csamfile___pyx_scope_struct__genexpr[--__pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr];
-    memset(o, 0, sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr));
+  if (likely((__pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct__genexpr > 0) & (t->tp_basicsize == sizeof(struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr)))) {
+    o = (PyObject*)__pyx_freelist_5pysam_14calignmentfile___pyx_scope_struct__genexpr[--__pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct__genexpr];
+    memset(o, 0, sizeof(struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr));
     (void) PyObject_INIT(o, t);
     PyObject_GC_Track(o);
   } else {
@@ -38058,23 +42302,23 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct__genexpr(PyTyp
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile___pyx_scope_struct__genexpr(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)o;
+static void __pyx_tp_dealloc_5pysam_14calignmentfile___pyx_scope_struct__genexpr(PyObject *o) {
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *p = (struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *)o;
   PyObject_GC_UnTrack(o);
   Py_CLEAR(p->__pyx_v_x);
   Py_CLEAR(p->__pyx_v_y);
   Py_CLEAR(p->__pyx_t_0);
   Py_CLEAR(p->__pyx_t_1);
-  if ((__pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr < 8) & (Py_TYPE(o)->tp_basicsize == sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr))) {
-    __pyx_freelist_5pysam_8csamfile___pyx_scope_struct__genexpr[__pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr++] = ((struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)o);
+  if ((__pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct__genexpr < 8) & (Py_TYPE(o)->tp_basicsize == sizeof(struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr))) {
+    __pyx_freelist_5pysam_14calignmentfile___pyx_scope_struct__genexpr[__pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct__genexpr++] = ((struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *)o);
   } else {
     (*Py_TYPE(o)->tp_free)(o);
   }
 }
 
-static int __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct__genexpr(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_14calignmentfile___pyx_scope_struct__genexpr(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)o;
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *p = (struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *)o;
   if (p->__pyx_v_x) {
     e = (*v)(p->__pyx_v_x, a); if (e) return e;
   }
@@ -38090,9 +42334,9 @@ static int __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct__genexpr(PyObje
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct__genexpr(PyObject *o) {
+static int __pyx_tp_clear_5pysam_14calignmentfile___pyx_scope_struct__genexpr(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)o;
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *p = (struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr *)o;
   tmp = ((PyObject*)p->__pyx_v_x);
   p->__pyx_v_x = Py_None; Py_INCREF(Py_None);
   Py_XDECREF(tmp);
@@ -38108,12 +42352,12 @@ static int __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct__genexpr(PyObject
   return 0;
 }
 
-static PyTypeObject __pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile___pyx_scope_struct__genexpr = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.__pyx_scope_struct__genexpr"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.__pyx_scope_struct__genexpr"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct__genexpr), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile___pyx_scope_struct__genexpr, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile___pyx_scope_struct__genexpr, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -38134,8 +42378,8 @@ static PyTypeObject __pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr = {
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
   0, /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct__genexpr, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct__genexpr, /*tp_clear*/
+  __pyx_tp_traverse_5pysam_14calignmentfile___pyx_scope_struct__genexpr, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile___pyx_scope_struct__genexpr, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
@@ -38150,7 +42394,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr = {
   0, /*tp_dictoffset*/
   0, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile___pyx_scope_struct__genexpr, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile___pyx_scope_struct__genexpr, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -38167,14 +42411,14 @@ static PyTypeObject __pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr = {
   #endif
 };
 
-static struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *__pyx_freelist_5pysam_8csamfile___pyx_scope_struct_1_genexpr[8];
-static int __pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr = 0;
+static struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *__pyx_freelist_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr[8];
+static int __pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr = 0;
 
-static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
+static PyObject *__pyx_tp_new_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
   PyObject *o;
-  if (likely((__pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr > 0) & (t->tp_basicsize == sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr)))) {
-    o = (PyObject*)__pyx_freelist_5pysam_8csamfile___pyx_scope_struct_1_genexpr[--__pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr];
-    memset(o, 0, sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr));
+  if (likely((__pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr > 0) & (t->tp_basicsize == sizeof(struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr)))) {
+    o = (PyObject*)__pyx_freelist_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr[--__pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr];
+    memset(o, 0, sizeof(struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr));
     (void) PyObject_INIT(o, t);
     PyObject_GC_Track(o);
   } else {
@@ -38184,23 +42428,23 @@ static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyTy
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)o;
+static void __pyx_tp_dealloc_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr(PyObject *o) {
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *p = (struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *)o;
   PyObject_GC_UnTrack(o);
   Py_CLEAR(p->__pyx_v_x);
   Py_CLEAR(p->__pyx_v_y);
   Py_CLEAR(p->__pyx_t_0);
   Py_CLEAR(p->__pyx_t_1);
-  if ((__pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr < 8) & (Py_TYPE(o)->tp_basicsize == sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr))) {
-    __pyx_freelist_5pysam_8csamfile___pyx_scope_struct_1_genexpr[__pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr++] = ((struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)o);
+  if ((__pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr < 8) & (Py_TYPE(o)->tp_basicsize == sizeof(struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr))) {
+    __pyx_freelist_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr[__pyx_freecount_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr++] = ((struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *)o);
   } else {
     (*Py_TYPE(o)->tp_free)(o);
   }
 }
 
-static int __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)o;
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *p = (struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *)o;
   if (p->__pyx_v_x) {
     e = (*v)(p->__pyx_v_x, a); if (e) return e;
   }
@@ -38216,9 +42460,9 @@ static int __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyObj
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyObject *o) {
+static int __pyx_tp_clear_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)o;
+  struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *p = (struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr *)o;
   tmp = ((PyObject*)p->__pyx_v_x);
   p->__pyx_v_x = Py_None; Py_INCREF(Py_None);
   Py_XDECREF(tmp);
@@ -38234,12 +42478,12 @@ static int __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyObject
   return 0;
 }
 
-static PyTypeObject __pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr = {
+static PyTypeObject __pyx_type_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.__pyx_scope_struct_1_genexpr"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.calignmentfile.__pyx_scope_struct_1_genexpr"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile___pyx_scope_struct_1_genexpr, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -38260,8 +42504,8 @@ static PyTypeObject __pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr = {
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
   0, /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct_1_genexpr, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct_1_genexpr, /*tp_clear*/
+  __pyx_tp_traverse_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
@@ -38276,7 +42520,7 @@ static PyTypeObject __pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr = {
   0, /*tp_dictoffset*/
   0, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile___pyx_scope_struct_1_genexpr, /*tp_new*/
+  __pyx_tp_new_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -38304,7 +42548,7 @@ static struct PyModuleDef __pyx_moduledef = {
   #else
     PyModuleDef_HEAD_INIT,
   #endif
-    __Pyx_NAMESTR("csamfile"),
+    __Pyx_NAMESTR("calignmentfile"),
     0, /* m_doc */
     -1, /* m_size */
     __pyx_methods /* m_methods */,
@@ -38326,27 +42570,21 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_kp_s_2scf, __pyx_k_2scf, sizeof(__pyx_k_2scf), 0, 0, 1, 0},
   {&__pyx_kp_s_2sch, __pyx_k_2sch, sizeof(__pyx_k_2sch), 0, 0, 1, 0},
   {&__pyx_kp_s_2sci, __pyx_k_2sci, sizeof(__pyx_k_2sci), 0, 0, 1, 0},
-  {&__pyx_kp_s_30s_10s_s, __pyx_k_30s_10s_s, sizeof(__pyx_k_30s_10s_s), 0, 0, 1, 0},
   {&__pyx_n_s_A, __pyx_k_A, sizeof(__pyx_k_A), 0, 0, 1, 1},
   {&__pyx_n_s_AS, __pyx_k_AS, sizeof(__pyx_k_AS), 0, 0, 1, 1},
-  {&__pyx_kp_s_A_pileup_column_A_pileup_column, __pyx_k_A_pileup_column_A_pileup_column, sizeof(__pyx_k_A_pileup_column_A_pileup_column), 0, 0, 1, 0},
-  {&__pyx_n_s_AlignedRead, __pyx_k_AlignedRead, sizeof(__pyx_k_AlignedRead), 0, 0, 1, 1},
+  {&__pyx_n_s_AlignedSegment, __pyx_k_AlignedSegment, sizeof(__pyx_k_AlignedSegment), 0, 0, 1, 1},
+  {&__pyx_n_s_AlignmentFile, __pyx_k_AlignmentFile, sizeof(__pyx_k_AlignmentFile), 0, 0, 1, 1},
+  {&__pyx_kp_s_AlignmentFile_mapped_only_availa, __pyx_k_AlignmentFile_mapped_only_availa, sizeof(__pyx_k_AlignmentFile_mapped_only_availa), 0, 0, 1, 0},
   {&__pyx_kp_u_Argument_must_be_string_bytes_or, __pyx_k_Argument_must_be_string_bytes_or, sizeof(__pyx_k_Argument_must_be_string_bytes_or), 0, 1, 0, 0},
   {&__pyx_kp_u_Argument_must_be_string_or_unico, __pyx_k_Argument_must_be_string_or_unico, sizeof(__pyx_k_Argument_must_be_string_or_unico), 0, 1, 0, 0},
   {&__pyx_n_s_AttributeError, __pyx_k_AttributeError, sizeof(__pyx_k_AttributeError), 0, 0, 1, 1},
   {&__pyx_n_s_B, __pyx_k_B, sizeof(__pyx_k_B), 0, 0, 1, 1},
-  {&__pyx_kp_s_Bam_index_bin_number, __pyx_k_Bam_index_bin_number, sizeof(__pyx_k_Bam_index_bin_number), 0, 0, 1, 0},
-  {&__pyx_kp_s_Binary_flag, __pyx_k_Binary_flag, sizeof(__pyx_k_Binary_flag), 0, 0, 1, 0},
   {&__pyx_n_s_C, __pyx_k_C, sizeof(__pyx_k_C), 0, 0, 1, 1},
   {&__pyx_n_s_CIGAR2CODE, __pyx_k_CIGAR2CODE, sizeof(__pyx_k_CIGAR2CODE), 0, 0, 1, 1},
   {&__pyx_n_s_CIGAR_REGEX, __pyx_k_CIGAR_REGEX, sizeof(__pyx_k_CIGAR_REGEX), 0, 0, 1, 1},
   {&__pyx_n_s_CL, __pyx_k_CL, sizeof(__pyx_k_CL), 0, 0, 1, 1},
   {&__pyx_n_s_CN, __pyx_k_CN, sizeof(__pyx_k_CN), 0, 0, 1, 1},
   {&__pyx_n_s_CO, __pyx_k_CO, sizeof(__pyx_k_CO), 0, 0, 1, 1},
-  {&__pyx_kp_s_Cigar_entries, __pyx_k_Cigar_entries, sizeof(__pyx_k_Cigar_entries), 0, 0, 1, 0},
-  {&__pyx_kp_s_Contig_index, __pyx_k_Contig_index, sizeof(__pyx_k_Contig_index), 0, 0, 1, 0},
-  {&__pyx_kp_s_Contig_index_for_mate_pair, __pyx_k_Contig_index_for_mate_pair, sizeof(__pyx_k_Contig_index_for_mate_pair), 0, 0, 1, 0},
-  {&__pyx_kp_s_Count_of_cigar_entries, __pyx_k_Count_of_cigar_entries, sizeof(__pyx_k_Count_of_cigar_entries), 0, 0, 1, 0},
   {&__pyx_n_s_D, __pyx_k_D, sizeof(__pyx_k_D), 0, 0, 1, 1},
   {&__pyx_n_s_DS, __pyx_k_DS, sizeof(__pyx_k_DS), 0, 0, 1, 1},
   {&__pyx_n_s_DT, __pyx_k_DT, sizeof(__pyx_k_DT), 0, 0, 1, 1},
@@ -38361,7 +42599,6 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_IS_PYTHON3, __pyx_k_IS_PYTHON3, sizeof(__pyx_k_IS_PYTHON3), 0, 0, 1, 1},
   {&__pyx_kp_s_I_O_operation_on_closed_file, __pyx_k_I_O_operation_on_closed_file, sizeof(__pyx_k_I_O_operation_on_closed_file), 0, 0, 1, 0},
   {&__pyx_n_s_IndexedReads, __pyx_k_IndexedReads, sizeof(__pyx_k_IndexedReads), 0, 0, 1, 1},
-  {&__pyx_kp_s_Insert_size, __pyx_k_Insert_size, sizeof(__pyx_k_Insert_size), 0, 0, 1, 0},
   {&__pyx_kp_s_Invalid_tag_s, __pyx_k_Invalid_tag_s, sizeof(__pyx_k_Invalid_tag_s), 0, 0, 1, 0},
   {&__pyx_n_s_IteratorColumn, __pyx_k_IteratorColumn, sizeof(__pyx_k_IteratorColumn), 0, 0, 1, 1},
   {&__pyx_n_s_IteratorRow, __pyx_k_IteratorRow, sizeof(__pyx_k_IteratorRow), 0, 0, 1, 1},
@@ -38369,13 +42606,8 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_KeyError, __pyx_k_KeyError, sizeof(__pyx_k_KeyError), 0, 0, 1, 1},
   {&__pyx_n_s_LB, __pyx_k_LB, sizeof(__pyx_k_LB), 0, 0, 1, 1},
   {&__pyx_n_s_LN, __pyx_k_LN, sizeof(__pyx_k_LN), 0, 0, 1, 1},
-  {&__pyx_kp_s_Length_of_auxilary_data, __pyx_k_Length_of_auxilary_data, sizeof(__pyx_k_Length_of_auxilary_data), 0, 0, 1, 0},
-  {&__pyx_kp_s_Length_of_query_name, __pyx_k_Length_of_query_name, sizeof(__pyx_k_Length_of_query_name), 0, 0, 1, 0},
-  {&__pyx_kp_s_Length_of_query_sequence, __pyx_k_Length_of_query_sequence, sizeof(__pyx_k_Length_of_query_sequence), 0, 0, 1, 0},
   {&__pyx_n_s_M5, __pyx_k_M5, sizeof(__pyx_k_M5), 0, 0, 1, 1},
-  {&__pyx_kp_s_Mapped_position_on_contig, __pyx_k_Mapped_position_on_contig, sizeof(__pyx_k_Mapped_position_on_contig), 0, 0, 1, 0},
-  {&__pyx_kp_s_Mapping_quality, __pyx_k_Mapping_quality, sizeof(__pyx_k_Mapping_quality), 0, 0, 1, 0},
-  {&__pyx_kp_s_Maximum_data_length, __pyx_k_Maximum_data_length, sizeof(__pyx_k_Maximum_data_length), 0, 0, 1, 0},
+  {&__pyx_n_s_MemoryError, __pyx_k_MemoryError, sizeof(__pyx_k_MemoryError), 0, 0, 1, 1},
   {&__pyx_n_s_NotImplementedError, __pyx_k_NotImplementedError, sizeof(__pyx_k_NotImplementedError), 0, 0, 1, 1},
   {&__pyx_n_s_OSError, __pyx_k_OSError, sizeof(__pyx_k_OSError), 0, 0, 1, 1},
   {&__pyx_n_s_OverflowError, __pyx_k_OverflowError, sizeof(__pyx_k_OverflowError), 0, 0, 1, 1},
@@ -38386,14 +42618,8 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_PP, __pyx_k_PP, sizeof(__pyx_k_PP), 0, 0, 1, 1},
   {&__pyx_n_s_PU, __pyx_k_PU, sizeof(__pyx_k_PU), 0, 0, 1, 1},
   {&__pyx_n_s_PileupColumn, __pyx_k_PileupColumn, sizeof(__pyx_k_PileupColumn), 0, 0, 1, 1},
-  {&__pyx_n_s_PileupColumn___str, __pyx_k_PileupColumn___str, sizeof(__pyx_k_PileupColumn___str), 0, 0, 1, 1},
-  {&__pyx_n_s_PileupProxy, __pyx_k_PileupProxy, sizeof(__pyx_k_PileupProxy), 0, 0, 1, 1},
-  {&__pyx_kp_s_PileupProxy_accessed_after_itera, __pyx_k_PileupProxy_accessed_after_itera, sizeof(__pyx_k_PileupProxy_accessed_after_itera), 0, 0, 1, 0},
+  {&__pyx_kp_s_PileupColumn_accessed_after_iter, __pyx_k_PileupColumn_accessed_after_iter, sizeof(__pyx_k_PileupColumn_accessed_after_iter), 0, 0, 1, 0},
   {&__pyx_n_s_PileupRead, __pyx_k_PileupRead, sizeof(__pyx_k_PileupRead), 0, 0, 1, 1},
-  {&__pyx_kp_s_Position_of_mate_pair, __pyx_k_Position_of_mate_pair, sizeof(__pyx_k_Position_of_mate_pair), 0, 0, 1, 0},
-  {&__pyx_kp_s_Quality_scores, __pyx_k_Quality_scores, sizeof(__pyx_k_Quality_scores), 0, 0, 1, 0},
-  {&__pyx_kp_s_Query_name, __pyx_k_Query_name, sizeof(__pyx_k_Query_name), 0, 0, 1, 0},
-  {&__pyx_kp_s_Query_sequence, __pyx_k_Query_sequence, sizeof(__pyx_k_Query_sequence), 0, 0, 1, 0},
   {&__pyx_n_s_RG, __pyx_k_RG, sizeof(__pyx_k_RG), 0, 0, 1, 1},
   {&__pyx_n_s_S, __pyx_k_S, sizeof(__pyx_k_S), 0, 0, 1, 1},
   {&__pyx_n_s_SM, __pyx_k_SM, sizeof(__pyx_k_SM), 0, 0, 1, 1},
@@ -38402,10 +42628,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_SP, __pyx_k_SP, sizeof(__pyx_k_SP), 0, 0, 1, 1},
   {&__pyx_n_s_SQ, __pyx_k_SQ, sizeof(__pyx_k_SQ), 0, 0, 1, 1},
   {&__pyx_kp_s_SQ_SN_s_LN_s, __pyx_k_SQ_SN_s_LN_s, sizeof(__pyx_k_SQ_SN_s_LN_s), 0, 0, 1, 0},
-  {&__pyx_n_s_Samfile, __pyx_k_Samfile, sizeof(__pyx_k_Samfile), 0, 0, 1, 1},
-  {&__pyx_kp_s_Samfile_mapped_only_available_in, __pyx_k_Samfile_mapped_only_available_in, sizeof(__pyx_k_Samfile_mapped_only_available_in), 0, 0, 1, 0},
   {&__pyx_n_s_StopIteration, __pyx_k_StopIteration, sizeof(__pyx_k_StopIteration), 0, 0, 1, 1},
-  {&__pyx_kp_s_This_class_cannot_be_instantiate, __pyx_k_This_class_cannot_be_instantiate, sizeof(__pyx_k_This_class_cannot_be_instantiate), 0, 0, 1, 0},
   {&__pyx_n_s_TypeError, __pyx_k_TypeError, sizeof(__pyx_k_TypeError), 0, 0, 1, 1},
   {&__pyx_n_s_UR, __pyx_k_UR, sizeof(__pyx_k_UR), 0, 0, 1, 1},
   {&__pyx_kp_s_Unsupported_value_type_in_set_op, __pyx_k_Unsupported_value_type_in_set_op, sizeof(__pyx_k_Unsupported_value_type_in_set_op), 0, 0, 1, 0},
@@ -38417,28 +42640,28 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_ValueError, __pyx_k_ValueError, sizeof(__pyx_k_ValueError), 0, 0, 1, 1},
   {&__pyx_n_s_Z, __pyx_k_Z, sizeof(__pyx_k_Z), 0, 0, 1, 1},
   {&__pyx_n_s_Zidf, __pyx_k_Zidf, sizeof(__pyx_k_Zidf), 0, 0, 1, 1},
-  {&__pyx_kp_s__14, __pyx_k__14, sizeof(__pyx_k__14), 0, 0, 1, 0},
-  {&__pyx_kp_s__3, __pyx_k__3, sizeof(__pyx_k__3), 0, 0, 1, 0},
-  {&__pyx_kp_s__4, __pyx_k__4, sizeof(__pyx_k__4), 0, 0, 1, 0},
-  {&__pyx_kp_s__41, __pyx_k__41, sizeof(__pyx_k__41), 0, 0, 1, 0},
+  {&__pyx_kp_s__12, __pyx_k__12, sizeof(__pyx_k__12), 0, 0, 1, 0},
+  {&__pyx_kp_s__38, __pyx_k__38, sizeof(__pyx_k__38), 0, 0, 1, 0},
+  {&__pyx_kp_b__4, __pyx_k__4, sizeof(__pyx_k__4), 0, 0, 0, 0},
+  {&__pyx_kp_s__40, __pyx_k__40, sizeof(__pyx_k__40), 0, 0, 1, 0},
+  {&__pyx_kp_s__43, __pyx_k__43, sizeof(__pyx_k__43), 0, 0, 1, 0},
   {&__pyx_kp_s__47, __pyx_k__47, sizeof(__pyx_k__47), 0, 0, 1, 0},
-  {&__pyx_kp_b__6, __pyx_k__6, sizeof(__pyx_k__6), 0, 0, 0, 0},
-  {&__pyx_kp_s__76, __pyx_k__76, sizeof(__pyx_k__76), 0, 0, 1, 0},
-  {&__pyx_kp_s__79, __pyx_k__79, sizeof(__pyx_k__79), 0, 0, 1, 0},
-  {&__pyx_kp_s__80, __pyx_k__80, sizeof(__pyx_k__80), 0, 0, 1, 0},
-  {&__pyx_kp_s__9, __pyx_k__9, sizeof(__pyx_k__9), 0, 0, 1, 0},
+  {&__pyx_kp_s__7, __pyx_k__7, sizeof(__pyx_k__7), 0, 0, 1, 0},
+  {&__pyx_kp_s__74, __pyx_k__74, sizeof(__pyx_k__74), 0, 0, 1, 0},
   {&__pyx_n_s_add_sq_text, __pyx_k_add_sq_text, sizeof(__pyx_k_add_sq_text), 0, 0, 1, 1},
   {&__pyx_n_s_alignment, __pyx_k_alignment, sizeof(__pyx_k_alignment), 0, 0, 1, 1},
   {&__pyx_n_s_all, __pyx_k_all, sizeof(__pyx_k_all), 0, 0, 1, 1},
   {&__pyx_n_s_all_2, __pyx_k_all_2, sizeof(__pyx_k_all_2), 0, 0, 1, 1},
+  {&__pyx_n_s_always, __pyx_k_always, sizeof(__pyx_k_always), 0, 0, 1, 1},
   {&__pyx_n_s_append, __pyx_k_append, sizeof(__pyx_k_append), 0, 0, 1, 1},
   {&__pyx_n_s_args, __pyx_k_args, sizeof(__pyx_k_args), 0, 0, 1, 1},
+  {&__pyx_n_s_array, __pyx_k_array, sizeof(__pyx_k_array), 0, 0, 1, 1},
   {&__pyx_n_s_ascii, __pyx_k_ascii, sizeof(__pyx_k_ascii), 0, 0, 1, 1},
+  {&__pyx_kp_s_at_least_one_integer_out_of_rang, __pyx_k_at_least_one_integer_out_of_rang, sizeof(__pyx_k_at_least_one_integer_out_of_rang), 0, 0, 1, 0},
+  {&__pyx_kp_s_at_least_one_signed_integer_out, __pyx_k_at_least_one_signed_integer_out, sizeof(__pyx_k_at_least_one_signed_integer_out), 0, 0, 1, 0},
   {&__pyx_n_s_b, __pyx_k_b, sizeof(__pyx_k_b), 0, 0, 1, 1},
   {&__pyx_kp_b_bai, __pyx_k_bai, sizeof(__pyx_k_bai), 0, 0, 0, 0},
   {&__pyx_n_s_beg, __pyx_k_beg, sizeof(__pyx_k_beg), 0, 0, 1, 1},
-  {&__pyx_n_s_bin, __pyx_k_bin, sizeof(__pyx_k_bin), 0, 0, 1, 1},
-  {&__pyx_n_s_bqual, __pyx_k_bqual, sizeof(__pyx_k_bqual), 0, 0, 1, 1},
   {&__pyx_n_s_buildLine, __pyx_k_buildLine, sizeof(__pyx_k_buildLine), 0, 0, 1, 1},
   {&__pyx_n_s_c, __pyx_k_c, sizeof(__pyx_k_c), 0, 0, 1, 1},
   {&__pyx_kp_s_c_2, __pyx_k_c_2, sizeof(__pyx_k_c_2), 0, 0, 1, 0},
@@ -38452,7 +42675,8 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_check_header, __pyx_k_check_header, sizeof(__pyx_k_check_header), 0, 0, 1, 1},
   {&__pyx_n_s_check_sq, __pyx_k_check_sq, sizeof(__pyx_k_check_sq), 0, 0, 1, 1},
   {&__pyx_n_s_chr, __pyx_k_chr, sizeof(__pyx_k_chr), 0, 0, 1, 1},
-  {&__pyx_n_s_cigar, __pyx_k_cigar, sizeof(__pyx_k_cigar), 0, 0, 1, 1},
+  {&__pyx_n_s_cigarstring, __pyx_k_cigarstring, sizeof(__pyx_k_cigarstring), 0, 0, 1, 1},
+  {&__pyx_n_s_cigartuples, __pyx_k_cigartuples, sizeof(__pyx_k_cigartuples), 0, 0, 1, 1},
   {&__pyx_n_s_close, __pyx_k_close, sizeof(__pyx_k_close), 0, 0, 1, 1},
   {&__pyx_n_s_collections, __pyx_k_collections, sizeof(__pyx_k_collections), 0, 0, 1, 1},
   {&__pyx_n_s_compile, __pyx_k_compile, sizeof(__pyx_k_compile), 0, 0, 1, 1},
@@ -38466,7 +42690,6 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_kp_s_d_MIDNSHP_X, __pyx_k_d_MIDNSHP_X, sizeof(__pyx_k_d_MIDNSHP_X), 0, 0, 1, 0},
   {&__pyx_n_s_decode, __pyx_k_decode, sizeof(__pyx_k_decode), 0, 0, 1, 1},
   {&__pyx_n_s_defaultdict, __pyx_k_defaultdict, sizeof(__pyx_k_defaultdict), 0, 0, 1, 1},
-  {&__pyx_n_s_doc, __pyx_k_doc, sizeof(__pyx_k_doc), 0, 0, 1, 1},
   {&__pyx_kp_s_either_supply_options_template_h, __pyx_k_either_supply_options_template_h, sizeof(__pyx_k_either_supply_options_template_h), 0, 0, 1, 0},
   {&__pyx_n_s_encode, __pyx_k_encode, sizeof(__pyx_k_encode), 0, 0, 1, 1},
   {&__pyx_n_s_end, __pyx_k_end, sizeof(__pyx_k_end), 0, 0, 1, 1},
@@ -38492,11 +42715,15 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_filename, __pyx_k_filename, sizeof(__pyx_k_filename), 0, 0, 1, 1},
   {&__pyx_n_s_findall, __pyx_k_findall, sizeof(__pyx_k_findall), 0, 0, 1, 1},
   {&__pyx_n_s_flag, __pyx_k_flag, sizeof(__pyx_k_flag), 0, 0, 1, 1},
-  {&__pyx_n_s_fmts, __pyx_k_fmts, sizeof(__pyx_k_fmts), 0, 0, 1, 1},
+  {&__pyx_n_s_fromQualityString, __pyx_k_fromQualityString, sizeof(__pyx_k_fromQualityString), 0, 0, 1, 1},
   {&__pyx_kp_b_ftp, __pyx_k_ftp, sizeof(__pyx_k_ftp), 0, 0, 0, 0},
+  {&__pyx_n_s_full_length, __pyx_k_full_length, sizeof(__pyx_k_full_length), 0, 0, 1, 1},
   {&__pyx_n_s_genotype, __pyx_k_genotype, sizeof(__pyx_k_genotype), 0, 0, 1, 1},
   {&__pyx_n_s_get, __pyx_k_get, sizeof(__pyx_k_get), 0, 0, 1, 1},
-  {&__pyx_n_s_getattribute, __pyx_k_getattribute, sizeof(__pyx_k_getattribute), 0, 0, 1, 1},
+  {&__pyx_n_s_get_aligned_pairs, __pyx_k_get_aligned_pairs, sizeof(__pyx_k_get_aligned_pairs), 0, 0, 1, 1},
+  {&__pyx_n_s_get_blocks, __pyx_k_get_blocks, sizeof(__pyx_k_get_blocks), 0, 0, 1, 1},
+  {&__pyx_n_s_get_overlap, __pyx_k_get_overlap, sizeof(__pyx_k_get_overlap), 0, 0, 1, 1},
+  {&__pyx_n_s_get_reference_positions, __pyx_k_get_reference_positions, sizeof(__pyx_k_get_reference_positions), 0, 0, 1, 1},
   {&__pyx_n_s_getdefaultencoding, __pyx_k_getdefaultencoding, sizeof(__pyx_k_getdefaultencoding), 0, 0, 1, 1},
   {&__pyx_n_s_getfilesystemencoding, __pyx_k_getfilesystemencoding, sizeof(__pyx_k_getfilesystemencoding), 0, 0, 1, 1},
   {&__pyx_n_s_gettid, __pyx_k_gettid, sizeof(__pyx_k_gettid), 0, 0, 1, 1},
@@ -38512,6 +42739,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_import, __pyx_k_import, sizeof(__pyx_k_import), 0, 0, 1, 1},
   {&__pyx_kp_s_incomplete_sequence_information, __pyx_k_incomplete_sequence_information, sizeof(__pyx_k_incomplete_sequence_information), 0, 0, 1, 0},
   {&__pyx_n_s_indel, __pyx_k_indel, sizeof(__pyx_k_indel), 0, 0, 1, 1},
+  {&__pyx_n_s_infer_query_length, __pyx_k_infer_query_length, sizeof(__pyx_k_infer_query_length), 0, 0, 1, 1},
   {&__pyx_n_s_init, __pyx_k_init, sizeof(__pyx_k_init), 0, 0, 1, 1},
   {&__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_k_integer_i_out_of_range_of_BAM_SA, sizeof(__pyx_k_integer_i_out_of_range_of_BAM_SA), 0, 0, 1, 0},
   {&__pyx_kp_s_invalid_coordinates_start_i_end, __pyx_k_invalid_coordinates_start_i_end, sizeof(__pyx_k_invalid_coordinates_start_i_end), 0, 0, 1, 0},
@@ -38521,45 +42749,40 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_isOpen, __pyx_k_isOpen, sizeof(__pyx_k_isOpen), 0, 0, 1, 1},
   {&__pyx_n_s_is_del, __pyx_k_is_del, sizeof(__pyx_k_is_del), 0, 0, 1, 1},
   {&__pyx_n_s_is_head, __pyx_k_is_head, sizeof(__pyx_k_is_head), 0, 0, 1, 1},
+  {&__pyx_n_s_is_refskip, __pyx_k_is_refskip, sizeof(__pyx_k_is_refskip), 0, 0, 1, 1},
+  {&__pyx_n_s_is_refskip_2, __pyx_k_is_refskip_2, sizeof(__pyx_k_is_refskip_2), 0, 0, 1, 1},
   {&__pyx_n_s_is_tail, __pyx_k_is_tail, sizeof(__pyx_k_is_tail), 0, 0, 1, 1},
-  {&__pyx_n_s_isize, __pyx_k_isize, sizeof(__pyx_k_isize), 0, 0, 1, 1},
   {&__pyx_n_s_islower, __pyx_k_islower, sizeof(__pyx_k_islower), 0, 0, 1, 1},
   {&__pyx_n_s_isupper, __pyx_k_isupper, sizeof(__pyx_k_isupper), 0, 0, 1, 1},
   {&__pyx_n_s_items, __pyx_k_items, sizeof(__pyx_k_items), 0, 0, 1, 1},
   {&__pyx_n_s_itertools, __pyx_k_itertools, sizeof(__pyx_k_itertools), 0, 0, 1, 1},
   {&__pyx_n_s_join, __pyx_k_join, sizeof(__pyx_k_join), 0, 0, 1, 1},
-  {&__pyx_n_s_l_data, __pyx_k_l_data, sizeof(__pyx_k_l_data), 0, 0, 1, 1},
-  {&__pyx_n_s_l_qname, __pyx_k_l_qname, sizeof(__pyx_k_l_qname), 0, 0, 1, 1},
-  {&__pyx_n_s_l_qseq, __pyx_k_l_qseq, sizeof(__pyx_k_l_qseq), 0, 0, 1, 1},
   {&__pyx_n_s_lengths, __pyx_k_lengths, sizeof(__pyx_k_lengths), 0, 0, 1, 1},
   {&__pyx_n_s_level, __pyx_k_level, sizeof(__pyx_k_level), 0, 0, 1, 1},
-  {&__pyx_n_s_m_data, __pyx_k_m_data, sizeof(__pyx_k_m_data), 0, 0, 1, 1},
   {&__pyx_n_s_main, __pyx_k_main, sizeof(__pyx_k_main), 0, 0, 1, 1},
   {&__pyx_kp_s_malformatted_header_no_in_field, __pyx_k_malformatted_header_no_in_field, sizeof(__pyx_k_malformatted_header_no_in_field), 0, 0, 1, 0},
   {&__pyx_n_s_map, __pyx_k_map, sizeof(__pyx_k_map), 0, 0, 1, 1},
   {&__pyx_kp_s_mapping_information_not_recorded, __pyx_k_mapping_information_not_recorded, sizeof(__pyx_k_mapping_information_not_recorded), 0, 0, 1, 0},
   {&__pyx_n_s_mapping_quality, __pyx_k_mapping_quality, sizeof(__pyx_k_mapping_quality), 0, 0, 1, 1},
-  {&__pyx_n_s_mapq, __pyx_k_mapq, sizeof(__pyx_k_mapq), 0, 0, 1, 1},
   {&__pyx_kp_s_mate_not_found, __pyx_k_mate_not_found, sizeof(__pyx_k_mate_not_found), 0, 0, 1, 0},
   {&__pyx_kp_s_mate_s_is_unmapped, __pyx_k_mate_s_is_unmapped, sizeof(__pyx_k_mate_s_is_unmapped), 0, 0, 1, 0},
   {&__pyx_n_s_max, __pyx_k_max, sizeof(__pyx_k_max), 0, 0, 1, 1},
   {&__pyx_n_s_max_depth, __pyx_k_max_depth, sizeof(__pyx_k_max_depth), 0, 0, 1, 1},
-  {&__pyx_n_s_metaclass, __pyx_k_metaclass, sizeof(__pyx_k_metaclass), 0, 0, 1, 1},
   {&__pyx_n_s_min, __pyx_k_min, sizeof(__pyx_k_min), 0, 0, 1, 1},
   {&__pyx_n_s_mode, __pyx_k_mode, sizeof(__pyx_k_mode), 0, 0, 1, 1},
-  {&__pyx_n_s_module, __pyx_k_module, sizeof(__pyx_k_module), 0, 0, 1, 1},
-  {&__pyx_n_s_mpos, __pyx_k_mpos, sizeof(__pyx_k_mpos), 0, 0, 1, 1},
-  {&__pyx_n_s_mrnm, __pyx_k_mrnm, sizeof(__pyx_k_mrnm), 0, 0, 1, 1},
-  {&__pyx_n_s_mtid, __pyx_k_mtid, sizeof(__pyx_k_mtid), 0, 0, 1, 1},
+  {&__pyx_n_s_multiple_iterators, __pyx_k_multiple_iterators, sizeof(__pyx_k_multiple_iterators), 0, 0, 1, 1},
   {&__pyx_kp_s_multiple_s_lines_are_not_permitt, __pyx_k_multiple_s_lines_are_not_permitt, sizeof(__pyx_k_multiple_s_lines_are_not_permitt), 0, 0, 1, 0},
   {&__pyx_n_s_n, __pyx_k_n, sizeof(__pyx_k_n), 0, 0, 1, 1},
-  {&__pyx_n_s_n_cigar, __pyx_k_n_cigar, sizeof(__pyx_k_n_cigar), 0, 0, 1, 1},
+  {&__pyx_n_s_next_reference_id, __pyx_k_next_reference_id, sizeof(__pyx_k_next_reference_id), 0, 0, 1, 1},
+  {&__pyx_n_s_next_reference_start, __pyx_k_next_reference_start, sizeof(__pyx_k_next_reference_start), 0, 0, 1, 1},
   {&__pyx_n_s_nextiter, __pyx_k_nextiter, sizeof(__pyx_k_nextiter), 0, 0, 1, 1},
   {&__pyx_kp_s_no_index_available_for_fetch, __pyx_k_no_index_available_for_fetch, sizeof(__pyx_k_no_index_available_for_fetch), 0, 0, 1, 0},
   {&__pyx_kp_s_no_index_available_for_iteration, __pyx_k_no_index_available_for_iteration, sizeof(__pyx_k_no_index_available_for_iteration), 0, 0, 1, 0},
   {&__pyx_kp_s_no_index_available_for_pileup, __pyx_k_no_index_available_for_pileup, sizeof(__pyx_k_no_index_available_for_pileup), 0, 0, 1, 0},
+  {&__pyx_n_s_nofilter, __pyx_k_nofilter, sizeof(__pyx_k_nofilter), 0, 0, 1, 1},
   {&__pyx_n_s_nreferences, __pyx_k_nreferences, sizeof(__pyx_k_nreferences), 0, 0, 1, 1},
-  {&__pyx_n_s_object, __pyx_k_object, sizeof(__pyx_k_object), 0, 0, 1, 1},
+  {&__pyx_n_s_nsegmentes, __pyx_k_nsegmentes, sizeof(__pyx_k_nsegmentes), 0, 0, 1, 1},
+  {&__pyx_n_s_nsegments, __pyx_k_nsegments, sizeof(__pyx_k_nsegments), 0, 0, 1, 1},
   {&__pyx_n_s_offset, __pyx_k_offset, sizeof(__pyx_k_offset), 0, 0, 1, 1},
   {&__pyx_n_s_open, __pyx_k_open, sizeof(__pyx_k_open), 0, 0, 1, 1},
   {&__pyx_n_s_ord, __pyx_k_ord, sizeof(__pyx_k_ord), 0, 0, 1, 1},
@@ -38573,16 +42796,21 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_port, __pyx_k_port, sizeof(__pyx_k_port), 0, 0, 1, 1},
   {&__pyx_n_s_pos, __pyx_k_pos, sizeof(__pyx_k_pos), 0, 0, 1, 1},
   {&__pyx_n_s_positions, __pyx_k_positions, sizeof(__pyx_k_positions), 0, 0, 1, 1},
-  {&__pyx_n_s_prepare, __pyx_k_prepare, sizeof(__pyx_k_prepare), 0, 0, 1, 1},
-  {&__pyx_n_s_pysam_csamfile, __pyx_k_pysam_csamfile, sizeof(__pyx_k_pysam_csamfile), 0, 0, 1, 1},
-  {&__pyx_n_s_pytag, __pyx_k_pytag, sizeof(__pyx_k_pytag), 0, 0, 1, 1},
+  {&__pyx_n_s_pysam_calignmentfile, __pyx_k_pysam_calignmentfile, sizeof(__pyx_k_pysam_calignmentfile), 0, 0, 1, 1},
   {&__pyx_n_s_pyx_vtable, __pyx_k_pyx_vtable, sizeof(__pyx_k_pyx_vtable), 0, 0, 1, 1},
-  {&__pyx_n_s_qname, __pyx_k_qname, sizeof(__pyx_k_qname), 0, 0, 1, 1},
-  {&__pyx_n_s_qpos, __pyx_k_qpos, sizeof(__pyx_k_qpos), 0, 0, 1, 1},
-  {&__pyx_n_s_qseq, __pyx_k_qseq, sizeof(__pyx_k_qseq), 0, 0, 1, 1},
-  {&__pyx_n_s_qual, __pyx_k_qual, sizeof(__pyx_k_qual), 0, 0, 1, 1},
+  {&__pyx_n_s_qualities, __pyx_k_qualities, sizeof(__pyx_k_qualities), 0, 0, 1, 1},
   {&__pyx_kp_s_quality_and_sequence_mismatch_i, __pyx_k_quality_and_sequence_mismatch_i, sizeof(__pyx_k_quality_and_sequence_mismatch_i), 0, 0, 1, 0},
-  {&__pyx_n_s_qualname, __pyx_k_qualname, sizeof(__pyx_k_qualname), 0, 0, 1, 1},
+  {&__pyx_n_s_quality_string, __pyx_k_quality_string, sizeof(__pyx_k_quality_string), 0, 0, 1, 1},
+  {&__pyx_n_s_query_alignment_end, __pyx_k_query_alignment_end, sizeof(__pyx_k_query_alignment_end), 0, 0, 1, 1},
+  {&__pyx_n_s_query_alignment_length, __pyx_k_query_alignment_length, sizeof(__pyx_k_query_alignment_length), 0, 0, 1, 1},
+  {&__pyx_n_s_query_alignment_qualities, __pyx_k_query_alignment_qualities, sizeof(__pyx_k_query_alignment_qualities), 0, 0, 1, 1},
+  {&__pyx_n_s_query_alignment_sequence, __pyx_k_query_alignment_sequence, sizeof(__pyx_k_query_alignment_sequence), 0, 0, 1, 1},
+  {&__pyx_n_s_query_alignment_start, __pyx_k_query_alignment_start, sizeof(__pyx_k_query_alignment_start), 0, 0, 1, 1},
+  {&__pyx_n_s_query_length, __pyx_k_query_length, sizeof(__pyx_k_query_length), 0, 0, 1, 1},
+  {&__pyx_n_s_query_name, __pyx_k_query_name, sizeof(__pyx_k_query_name), 0, 0, 1, 1},
+  {&__pyx_n_s_query_position, __pyx_k_query_position, sizeof(__pyx_k_query_position), 0, 0, 1, 1},
+  {&__pyx_n_s_query_qualities, __pyx_k_query_qualities, sizeof(__pyx_k_query_qualities), 0, 0, 1, 1},
+  {&__pyx_n_s_query_sequence, __pyx_k_query_sequence, sizeof(__pyx_k_query_sequence), 0, 0, 1, 1},
   {&__pyx_n_s_r, __pyx_k_r, sizeof(__pyx_k_r), 0, 0, 1, 1},
   {&__pyx_n_s_rU, __pyx_k_rU, sizeof(__pyx_k_rU), 0, 0, 1, 1},
   {&__pyx_n_s_raw, __pyx_k_raw, sizeof(__pyx_k_raw), 0, 0, 1, 1},
@@ -38593,15 +42821,18 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_record, __pyx_k_record, sizeof(__pyx_k_record), 0, 0, 1, 1},
   {&__pyx_n_s_reference, __pyx_k_reference, sizeof(__pyx_k_reference), 0, 0, 1, 1},
   {&__pyx_n_s_reference_base, __pyx_k_reference_base, sizeof(__pyx_k_reference_base), 0, 0, 1, 1},
+  {&__pyx_n_s_reference_end, __pyx_k_reference_end, sizeof(__pyx_k_reference_end), 0, 0, 1, 1},
+  {&__pyx_n_s_reference_id, __pyx_k_reference_id, sizeof(__pyx_k_reference_id), 0, 0, 1, 1},
+  {&__pyx_kp_s_reference_id_i_out_of_range_0_ti, __pyx_k_reference_id_i_out_of_range_0_ti, sizeof(__pyx_k_reference_id_i_out_of_range_0_ti), 0, 0, 1, 0},
+  {&__pyx_n_s_reference_length, __pyx_k_reference_length, sizeof(__pyx_k_reference_length), 0, 0, 1, 1},
+  {&__pyx_n_s_reference_pos, __pyx_k_reference_pos, sizeof(__pyx_k_reference_pos), 0, 0, 1, 1},
   {&__pyx_kp_s_reference_sequence_for_s_tid_i_n, __pyx_k_reference_sequence_for_s_tid_i_n, sizeof(__pyx_k_reference_sequence_for_s_tid_i_n), 0, 0, 1, 0},
+  {&__pyx_n_s_reference_start, __pyx_k_reference_start, sizeof(__pyx_k_reference_start), 0, 0, 1, 1},
   {&__pyx_n_s_referencelengths, __pyx_k_referencelengths, sizeof(__pyx_k_referencelengths), 0, 0, 1, 1},
   {&__pyx_n_s_referencenames, __pyx_k_referencenames, sizeof(__pyx_k_referencenames), 0, 0, 1, 1},
   {&__pyx_n_s_references, __pyx_k_references, sizeof(__pyx_k_references), 0, 0, 1, 1},
   {&__pyx_n_s_region, __pyx_k_region, sizeof(__pyx_k_region), 0, 0, 1, 1},
-  {&__pyx_n_s_reopen, __pyx_k_reopen, sizeof(__pyx_k_reopen), 0, 0, 1, 1},
   {&__pyx_n_s_replace, __pyx_k_replace, sizeof(__pyx_k_replace), 0, 0, 1, 1},
-  {&__pyx_n_s_rlen, __pyx_k_rlen, sizeof(__pyx_k_rlen), 0, 0, 1, 1},
-  {&__pyx_n_s_rname, __pyx_k_rname, sizeof(__pyx_k_rname), 0, 0, 1, 1},
   {&__pyx_kp_s_s, __pyx_k_s, sizeof(__pyx_k_s), 0, 0, 1, 0},
   {&__pyx_n_s_s_2, __pyx_k_s_2, sizeof(__pyx_k_s_2), 0, 0, 1, 1},
   {&__pyx_kp_s_s_s, __pyx_k_s_s, sizeof(__pyx_k_s_s), 0, 0, 1, 0},
@@ -38610,9 +42841,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_seek, __pyx_k_seek, sizeof(__pyx_k_seek), 0, 0, 1, 1},
   {&__pyx_kp_s_seek_no_available_in_streams, __pyx_k_seek_no_available_in_streams, sizeof(__pyx_k_seek_no_available_in_streams), 0, 0, 1, 0},
   {&__pyx_kp_s_seek_only_available_in_bam_files, __pyx_k_seek_only_available_in_bam_files, sizeof(__pyx_k_seek_only_available_in_bam_files), 0, 0, 1, 0},
-  {&__pyx_n_s_self, __pyx_k_self, sizeof(__pyx_k_self), 0, 0, 1, 1},
   {&__pyx_n_s_send, __pyx_k_send, sizeof(__pyx_k_send), 0, 0, 1, 1},
-  {&__pyx_n_s_seq, __pyx_k_seq, sizeof(__pyx_k_seq), 0, 0, 1, 1},
   {&__pyx_n_s_setTag, __pyx_k_setTag, sizeof(__pyx_k_setTag), 0, 0, 1, 1},
   {&__pyx_n_s_snp_quality, __pyx_k_snp_quality, sizeof(__pyx_k_snp_quality), 0, 0, 1, 1},
   {&__pyx_n_s_sorted, __pyx_k_sorted, sizeof(__pyx_k_sorted), 0, 0, 1, 1},
@@ -38621,7 +42850,6 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_kp_s_start_out_of_range_i, __pyx_k_start_out_of_range_i, sizeof(__pyx_k_start_out_of_range_i), 0, 0, 1, 0},
   {&__pyx_n_s_startswith, __pyx_k_startswith, sizeof(__pyx_k_startswith), 0, 0, 1, 1},
   {&__pyx_n_s_stepper, __pyx_k_stepper, sizeof(__pyx_k_stepper), 0, 0, 1, 1},
-  {&__pyx_n_s_str, __pyx_k_str, sizeof(__pyx_k_str), 0, 0, 1, 1},
   {&__pyx_n_s_strip, __pyx_k_strip, sizeof(__pyx_k_strip), 0, 0, 1, 1},
   {&__pyx_n_s_struct, __pyx_k_struct, sizeof(__pyx_k_struct), 0, 0, 1, 1},
   {&__pyx_n_s_sys, __pyx_k_sys, sizeof(__pyx_k_sys), 0, 0, 1, 1},
@@ -38630,13 +42858,17 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_tags, __pyx_k_tags, sizeof(__pyx_k_tags), 0, 0, 1, 1},
   {&__pyx_n_s_tempfile, __pyx_k_tempfile, sizeof(__pyx_k_tempfile), 0, 0, 1, 1},
   {&__pyx_n_s_template, __pyx_k_template, sizeof(__pyx_k_template), 0, 0, 1, 1},
+  {&__pyx_n_s_template_length, __pyx_k_template_length, sizeof(__pyx_k_template_length), 0, 0, 1, 1},
   {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1},
   {&__pyx_n_s_text, __pyx_k_text, sizeof(__pyx_k_text), 0, 0, 1, 1},
+  {&__pyx_kp_s_this_class_cannot_be_instantiate, __pyx_k_this_class_cannot_be_instantiate, sizeof(__pyx_k_this_class_cannot_be_instantiate), 0, 0, 1, 0},
   {&__pyx_n_s_throw, __pyx_k_throw, sizeof(__pyx_k_throw), 0, 0, 1, 1},
   {&__pyx_n_s_tid, __pyx_k_tid, sizeof(__pyx_k_tid), 0, 0, 1, 1},
   {&__pyx_kp_s_tid_i_out_of_range_0_tid_i, __pyx_k_tid_i_out_of_range_0_tid_i, sizeof(__pyx_k_tid_i_out_of_range_0_tid_i), 0, 0, 1, 0},
+  {&__pyx_n_s_toQualityString, __pyx_k_toQualityString, sizeof(__pyx_k_toQualityString), 0, 0, 1, 1},
   {&__pyx_n_s_traceback, __pyx_k_traceback, sizeof(__pyx_k_traceback), 0, 0, 1, 1},
   {&__pyx_n_s_truncate, __pyx_k_truncate, sizeof(__pyx_k_truncate), 0, 0, 1, 1},
+  {&__pyx_kp_s_truncated_file, __pyx_k_truncated_file, sizeof(__pyx_k_truncated_file), 0, 0, 1, 0},
   {&__pyx_n_s_types, __pyx_k_types, sizeof(__pyx_k_types), 0, 0, 1, 1},
   {&__pyx_kp_s_unable_to_open_index_for_s, __pyx_k_unable_to_open_index_for_s, sizeof(__pyx_k_unable_to_open_index_for_s), 0, 0, 1, 0},
   {&__pyx_kp_s_unequal_names_and_lengths_of_ref, __pyx_k_unequal_names_and_lengths_of_ref, sizeof(__pyx_k_unequal_names_and_lengths_of_ref), 0, 0, 1, 0},
@@ -38647,37 +42879,38 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_until_eof, __pyx_k_until_eof, sizeof(__pyx_k_until_eof), 0, 0, 1, 1},
   {&__pyx_n_s_value, __pyx_k_value, sizeof(__pyx_k_value), 0, 0, 1, 1},
   {&__pyx_n_s_value_type, __pyx_k_value_type, sizeof(__pyx_k_value_type), 0, 0, 1, 1},
-  {&__pyx_n_s_version_info, __pyx_k_version_info, sizeof(__pyx_k_version_info), 0, 0, 1, 1},
   {&__pyx_n_s_w, __pyx_k_w, sizeof(__pyx_k_w), 0, 0, 1, 1},
   {&__pyx_n_s_warn, __pyx_k_warn, sizeof(__pyx_k_warn), 0, 0, 1, 1},
   {&__pyx_n_s_warnings, __pyx_k_warnings, sizeof(__pyx_k_warnings), 0, 0, 1, 1},
   {&__pyx_n_s_wb, __pyx_k_wb, sizeof(__pyx_k_wb), 0, 0, 1, 1},
+  {&__pyx_n_s_wb0, __pyx_k_wb0, sizeof(__pyx_k_wb0), 0, 0, 1, 1},
   {&__pyx_n_s_wbu, __pyx_k_wbu, sizeof(__pyx_k_wbu), 0, 0, 1, 1},
   {&__pyx_n_s_wh, __pyx_k_wh, sizeof(__pyx_k_wh), 0, 0, 1, 1},
   {&__pyx_n_s_where, __pyx_k_where, sizeof(__pyx_k_where), 0, 0, 1, 1},
   {&__pyx_n_s_write, __pyx_k_write, sizeof(__pyx_k_write), 0, 0, 1, 1},
+  {&__pyx_n_s_x, __pyx_k_x, sizeof(__pyx_k_x), 0, 0, 1, 1},
   {&__pyx_n_s_zip, __pyx_k_zip, sizeof(__pyx_k_zip), 0, 0, 1, 1},
   {0, 0, 0, 0, 0, 0, 0}
 };
 static int __Pyx_InitCachedBuiltins(void) {
-  __pyx_builtin_object = __Pyx_GetBuiltinName(__pyx_n_s_object); if (!__pyx_builtin_object) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_TypeError = __Pyx_GetBuiltinName(__pyx_n_s_TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_enumerate = __Pyx_GetBuiltinName(__pyx_n_s_enumerate); if (!__pyx_builtin_enumerate) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_ord = __Pyx_GetBuiltinName(__pyx_n_s_ord); if (!__pyx_builtin_ord) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_map = __Pyx_GetBuiltinName(__pyx_n_s_map); if (!__pyx_builtin_map) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_IOError = __Pyx_GetBuiltinName(__pyx_n_s_IOError); if (!__pyx_builtin_IOError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_OverflowError = __Pyx_GetBuiltinName(__pyx_n_s_OverflowError); if (!__pyx_builtin_OverflowError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_NotImplementedError = __Pyx_GetBuiltinName(__pyx_n_s_NotImplementedError); if (!__pyx_builtin_NotImplementedError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_OSError = __Pyx_GetBuiltinName(__pyx_n_s_OSError); if (!__pyx_builtin_OSError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_AttributeError = __Pyx_GetBuiltinName(__pyx_n_s_AttributeError); if (!__pyx_builtin_AttributeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_zip = __Pyx_GetBuiltinName(__pyx_n_s_zip); if (!__pyx_builtin_zip) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_sorted = __Pyx_GetBuiltinName(__pyx_n_s_sorted); if (!__pyx_builtin_sorted) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_KeyError = __Pyx_GetBuiltinName(__pyx_n_s_KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_min = __Pyx_GetBuiltinName(__pyx_n_s_min); if (!__pyx_builtin_min) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_max = __Pyx_GetBuiltinName(__pyx_n_s_max); if (!__pyx_builtin_max) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_chr = __Pyx_GetBuiltinName(__pyx_n_s_chr); if (!__pyx_builtin_chr) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_TypeError = __Pyx_GetBuiltinName(__pyx_n_s_TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_enumerate = __Pyx_GetBuiltinName(__pyx_n_s_enumerate); if (!__pyx_builtin_enumerate) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_ord = __Pyx_GetBuiltinName(__pyx_n_s_ord); if (!__pyx_builtin_ord) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_IOError = __Pyx_GetBuiltinName(__pyx_n_s_IOError); if (!__pyx_builtin_IOError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_OverflowError = __Pyx_GetBuiltinName(__pyx_n_s_OverflowError); if (!__pyx_builtin_OverflowError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_NotImplementedError = __Pyx_GetBuiltinName(__pyx_n_s_NotImplementedError); if (!__pyx_builtin_NotImplementedError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_OSError = __Pyx_GetBuiltinName(__pyx_n_s_OSError); if (!__pyx_builtin_OSError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_AttributeError = __Pyx_GetBuiltinName(__pyx_n_s_AttributeError); if (!__pyx_builtin_AttributeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_zip = __Pyx_GetBuiltinName(__pyx_n_s_zip); if (!__pyx_builtin_zip) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_sorted = __Pyx_GetBuiltinName(__pyx_n_s_sorted); if (!__pyx_builtin_sorted) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_KeyError = __Pyx_GetBuiltinName(__pyx_n_s_KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_chr = __Pyx_GetBuiltinName(__pyx_n_s_chr); if (!__pyx_builtin_chr) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_min = __Pyx_GetBuiltinName(__pyx_n_s_min); if (!__pyx_builtin_min) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_max = __Pyx_GetBuiltinName(__pyx_n_s_max); if (!__pyx_builtin_max) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_map = __Pyx_GetBuiltinName(__pyx_n_s_map); if (!__pyx_builtin_map) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_MemoryError = __Pyx_GetBuiltinName(__pyx_n_s_MemoryError); if (!__pyx_builtin_MemoryError) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   return 0;
   __pyx_L1_error:;
   return -1;
@@ -38687,867 +42920,857 @@ static int __Pyx_InitCachedConstants(void) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0);
 
-  /* "pysam/csamfile.pyx":66
+  /* "pysam/calignmentfile.pyx":71
  *         return s
  *     elif PyUnicode_Check(s):
  *         return s.encode('ascii')             # <<<<<<<<<<<<<<
  *     else:
  *         raise TypeError, u"Argument must be string, bytes or unicode."
  */
-  __pyx_tuple_ = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple_ = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple_);
   __Pyx_GIVEREF(__pyx_tuple_);
 
-  /* "pysam/csamfile.pyx":87
+  /* "pysam/calignmentfile.pyx":92
  *         return s
  *     elif PyBytes_Check(s):
  *         return s.decode('ascii')             # <<<<<<<<<<<<<<
  *     else:
  *         # assume unicode
  */
-  __pyx_tuple__2 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__2 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__2);
   __Pyx_GIVEREF(__pyx_tuple__2);
 
-  /* "pysam/csamfile.pyx":379
- *             self.close()
+  /* "pysam/calignmentfile.pyx":371
+ *             mode = "wb0"
  * 
  *         cdef bytes bmode = mode.encode('ascii')             # <<<<<<<<<<<<<<
  *         self._filename = filename = _encodeFilename(filename)
  *         self.isstream = filename == b"-"
  */
-  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__5);
-  __Pyx_GIVEREF(__pyx_tuple__5);
+  __pyx_tuple__3 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__3);
+  __Pyx_GIVEREF(__pyx_tuple__3);
 
-  /* "pysam/csamfile.pyx":385
+  /* "pysam/calignmentfile.pyx":377
  *         self.isbam = len(mode) > 1 and mode[1] == 'b'
  * 
  *         self.isremote = filename.startswith(b"http:") or \             # <<<<<<<<<<<<<<
  *                         filename.startswith(b"ftp:")
  * 
  */
-  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_b_http); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__7);
-  __Pyx_GIVEREF(__pyx_tuple__7);
+  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_b_http); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__5);
+  __Pyx_GIVEREF(__pyx_tuple__5);
 
-  /* "pysam/csamfile.pyx":386
+  /* "pysam/calignmentfile.pyx":378
  * 
  *         self.isremote = filename.startswith(b"http:") or \
  *                         filename.startswith(b"ftp:")             # <<<<<<<<<<<<<<
  * 
  *         cdef char * ctext
  */
-  __pyx_tuple__8 = PyTuple_Pack(1, __pyx_kp_b_ftp); if (unlikely(!__pyx_tuple__8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__8);
-  __Pyx_GIVEREF(__pyx_tuple__8);
+  __pyx_tuple__6 = PyTuple_Pack(1, __pyx_kp_b_ftp); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__6);
+  __Pyx_GIVEREF(__pyx_tuple__6);
 
-  /* "pysam/csamfile.pyx":503
+  /* "pysam/calignmentfile.pyx":497
  *             if not self.isremote:
  *                 if not os.path.exists(filename + b".bai") \
  *                         and not os.path.exists( filename[:-4] + b".bai"):             # <<<<<<<<<<<<<<
  *                     self.index = NULL
  *                 else:
  */
-  __pyx_slice__10 = PySlice_New(Py_None, __pyx_int_neg_4, Py_None); if (unlikely(!__pyx_slice__10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__10);
-  __Pyx_GIVEREF(__pyx_slice__10);
+  __pyx_slice__8 = PySlice_New(Py_None, __pyx_int_neg_4, Py_None); if (unlikely(!__pyx_slice__8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_slice__8);
+  __Pyx_GIVEREF(__pyx_slice__8);
 
-  /* "pysam/csamfile.pyx":525
+  /* "pysam/calignmentfile.pyx":519
  *         '''
  *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  *         reference = _forceBytes(reference)
  *         return bam_name2id(self.header, reference)
  */
-  __pyx_tuple__11 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__11);
-  __Pyx_GIVEREF(__pyx_tuple__11);
+  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__9);
+  __Pyx_GIVEREF(__pyx_tuple__9);
 
-  /* "pysam/csamfile.pyx":533
+  /* "pysam/calignmentfile.pyx":527
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
+ *             raise ValueError("reference_id %i out of range 0<=tid<%i" %
  */
-  __pyx_tuple__12 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__12);
-  __Pyx_GIVEREF(__pyx_tuple__12);
+  __pyx_tuple__10 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__10);
+  __Pyx_GIVEREF(__pyx_tuple__10);
 
-  /* "pysam/csamfile.pyx":543
+  /* "pysam/calignmentfile.pyx":537
  *         convert numerical :term:`tid` into :term:`reference` name.'''
  *         if not self._isOpen():
  *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  * 
  *         if not 0 <= tid < self.header.n_targets:
  */
-  __pyx_tuple__13 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__13);
-  __Pyx_GIVEREF(__pyx_tuple__13);
+  __pyx_tuple__11 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__11);
+  __Pyx_GIVEREF(__pyx_tuple__11);
 
-  /* "pysam/csamfile.pyx":595
+  /* "pysam/calignmentfile.pyx":590
  * 
  *         if not reference:
  *             return 0, 0, 0, 0             # <<<<<<<<<<<<<<
  * 
  *         if tid is not None:
  */
-  __pyx_tuple__15 = PyTuple_Pack(4, __pyx_int_0, __pyx_int_0, __pyx_int_0, __pyx_int_0); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__15);
-  __Pyx_GIVEREF(__pyx_tuple__15);
+  __pyx_tuple__13 = PyTuple_Pack(4, __pyx_int_0, __pyx_int_0, __pyx_int_0, __pyx_int_0); if (unlikely(!__pyx_tuple__13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__13);
+  __Pyx_GIVEREF(__pyx_tuple__13);
 
-  /* "pysam/csamfile.pyx":625
+  /* "pysam/calignmentfile.pyx":621
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *         if not self.isbam:
  *             raise NotImplementedError("seek only available in bam files")
  */
-  __pyx_tuple__16 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__16);
-  __Pyx_GIVEREF(__pyx_tuple__16);
+  __pyx_tuple__14 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__14);
+  __Pyx_GIVEREF(__pyx_tuple__14);
 
-  /* "pysam/csamfile.pyx":627
+  /* "pysam/calignmentfile.pyx":623
  *             raise ValueError( "I/O operation on closed file" )
  *         if not self.isbam:
  *             raise NotImplementedError("seek only available in bam files")             # <<<<<<<<<<<<<<
  *         if self.isstream:
  *             raise OSError("seek no available in streams")
  */
-  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_kp_s_seek_only_available_in_bam_files); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__17);
-  __Pyx_GIVEREF(__pyx_tuple__17);
+  __pyx_tuple__15 = PyTuple_Pack(1, __pyx_kp_s_seek_only_available_in_bam_files); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__15);
+  __Pyx_GIVEREF(__pyx_tuple__15);
 
-  /* "pysam/csamfile.pyx":629
+  /* "pysam/calignmentfile.pyx":625
  *             raise NotImplementedError("seek only available in bam files")
  *         if self.isstream:
  *             raise OSError("seek no available in streams")             # <<<<<<<<<<<<<<
  * 
  *         return bgzf_seek(self.fp, offset, where)
  */
-  __pyx_tuple__18 = PyTuple_Pack(1, __pyx_kp_s_seek_no_available_in_streams); if (unlikely(!__pyx_tuple__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__18);
-  __Pyx_GIVEREF(__pyx_tuple__18);
+  __pyx_tuple__16 = PyTuple_Pack(1, __pyx_kp_s_seek_no_available_in_streams); if (unlikely(!__pyx_tuple__16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__16);
+  __Pyx_GIVEREF(__pyx_tuple__16);
 
-  /* "pysam/csamfile.pyx":638
+  /* "pysam/calignmentfile.pyx":634
  *         '''
  *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  *         if not self.isbam:
  *             raise NotImplementedError("seek only available in bam files")
  */
-  __pyx_tuple__19 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__19);
-  __Pyx_GIVEREF(__pyx_tuple__19);
+  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__17);
+  __Pyx_GIVEREF(__pyx_tuple__17);
 
-  /* "pysam/csamfile.pyx":640
- *             raise ValueError( "I/O operation on closed file" )
+  /* "pysam/calignmentfile.pyx":636
+ *             raise ValueError("I/O operation on closed file")
  *         if not self.isbam:
  *             raise NotImplementedError("seek only available in bam files")             # <<<<<<<<<<<<<<
  * 
  *         return bgzf_tell(self.fp)
  */
-  __pyx_tuple__20 = PyTuple_Pack(1, __pyx_kp_s_seek_only_available_in_bam_files); if (unlikely(!__pyx_tuple__20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__20);
-  __Pyx_GIVEREF(__pyx_tuple__20);
+  __pyx_tuple__18 = PyTuple_Pack(1, __pyx_kp_s_seek_only_available_in_bam_files); if (unlikely(!__pyx_tuple__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__18);
+  __Pyx_GIVEREF(__pyx_tuple__18);
 
-  /* "pysam/csamfile.pyx":686
+  /* "pysam/calignmentfile.pyx":679
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         has_coord, rtid, rstart, rend = self._parseRegion(reference,
  */
-  __pyx_tuple__21 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__21)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__21);
-  __Pyx_GIVEREF(__pyx_tuple__21);
+  __pyx_tuple__19 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 679; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__19);
+  __Pyx_GIVEREF(__pyx_tuple__19);
 
-  /* "pysam/csamfile.pyx":700
+  /* "pysam/calignmentfile.pyx":693
  *         if self.isbam:
  *             if not until_eof and not self._hasIndex() and not self.isremote:
- *                 raise ValueError( "fetch called on bamfile without index" )             # <<<<<<<<<<<<<<
+ *                 raise ValueError("fetch called on bamfile without index")             # <<<<<<<<<<<<<<
  * 
  *             if has_coord:
  */
-  __pyx_tuple__22 = PyTuple_Pack(1, __pyx_kp_s_fetch_called_on_bamfile_without); if (unlikely(!__pyx_tuple__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__22);
-  __Pyx_GIVEREF(__pyx_tuple__22);
+  __pyx_tuple__20 = PyTuple_Pack(1, __pyx_kp_s_fetch_called_on_bamfile_without); if (unlikely(!__pyx_tuple__20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__20);
+  __Pyx_GIVEREF(__pyx_tuple__20);
 
-  /* "pysam/csamfile.pyx":713
+  /* "pysam/calignmentfile.pyx":709
  *         else:
  *             if has_coord:
- *                 raise ValueError ("fetching by region is not available for sam files")             # <<<<<<<<<<<<<<
+ *                 raise ValueError(             # <<<<<<<<<<<<<<
+ *                     "fetching by region is not available for sam files")
  * 
- *             if callback:
  */
-  __pyx_tuple__23 = PyTuple_Pack(1, __pyx_kp_s_fetching_by_region_is_not_availa); if (unlikely(!__pyx_tuple__23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__23);
-  __Pyx_GIVEREF(__pyx_tuple__23);
+  __pyx_tuple__21 = PyTuple_Pack(1, __pyx_kp_s_fetching_by_region_is_not_availa); if (unlikely(!__pyx_tuple__21)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__21);
+  __Pyx_GIVEREF(__pyx_tuple__21);
 
-  /* "pysam/csamfile.pyx":716
+  /* "pysam/calignmentfile.pyx":713
  * 
  *             if callback:
  *                 raise NotImplementedError("callback not implemented yet")             # <<<<<<<<<<<<<<
  * 
  *             if self.header == NULL:
  */
-  __pyx_tuple__24 = PyTuple_Pack(1, __pyx_kp_s_callback_not_implemented_yet); if (unlikely(!__pyx_tuple__24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__24);
-  __Pyx_GIVEREF(__pyx_tuple__24);
+  __pyx_tuple__22 = PyTuple_Pack(1, __pyx_kp_s_callback_not_implemented_yet); if (unlikely(!__pyx_tuple__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__22);
+  __Pyx_GIVEREF(__pyx_tuple__22);
 
-  /* "pysam/csamfile.pyx":719
+  /* "pysam/calignmentfile.pyx":716
  * 
  *             if self.header == NULL:
  *                 raise ValueError("fetch called for htsfile without header")             # <<<<<<<<<<<<<<
  * 
  *             # check if targets are defined
  */
-  __pyx_tuple__25 = PyTuple_Pack(1, __pyx_kp_s_fetch_called_for_htsfile_without); if (unlikely(!__pyx_tuple__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__25);
-  __Pyx_GIVEREF(__pyx_tuple__25);
+  __pyx_tuple__23 = PyTuple_Pack(1, __pyx_kp_s_fetch_called_for_htsfile_without); if (unlikely(!__pyx_tuple__23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__23);
+  __Pyx_GIVEREF(__pyx_tuple__23);
 
-  /* "pysam/csamfile.pyx":724
+  /* "pysam/calignmentfile.pyx":721
  *             # give warning, sam_read1 segfaults
  *             if self.header.n_targets == 0:
  *                 warnings.warn("fetch called for htsfile without header")             # <<<<<<<<<<<<<<
  * 
- *             return IteratorRowAll(self, reopen=reopen)
+ *             return IteratorRowAll(self,
  */
-  __pyx_tuple__26 = PyTuple_Pack(1, __pyx_kp_s_fetch_called_for_htsfile_without); if (unlikely(!__pyx_tuple__26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__26);
-  __Pyx_GIVEREF(__pyx_tuple__26);
+  __pyx_tuple__24 = PyTuple_Pack(1, __pyx_kp_s_fetch_called_for_htsfile_without); if (unlikely(!__pyx_tuple__24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__24);
+  __Pyx_GIVEREF(__pyx_tuple__24);
 
-  /* "pysam/csamfile.pyx":777
+  /* "pysam/calignmentfile.pyx":785
  *                 break
  *         else:
  *             raise ValueError("mate not found")             # <<<<<<<<<<<<<<
  * 
  *         return mate
  */
-  __pyx_tuple__27 = PyTuple_Pack(1, __pyx_kp_s_mate_not_found); if (unlikely(!__pyx_tuple__27)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__27);
-  __Pyx_GIVEREF(__pyx_tuple__27);
+  __pyx_tuple__25 = PyTuple_Pack(1, __pyx_kp_s_mate_not_found); if (unlikely(!__pyx_tuple__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__25);
+  __Pyx_GIVEREF(__pyx_tuple__25);
 
-  /* "pysam/csamfile.pyx":802
+  /* "pysam/calignmentfile.pyx":810
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         for read in self.fetch(reference=reference,
  */
-  __pyx_tuple__28 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__28)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__28);
-  __Pyx_GIVEREF(__pyx_tuple__28);
+  __pyx_tuple__26 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__26);
+  __Pyx_GIVEREF(__pyx_tuple__26);
 
-  /* "pysam/csamfile.pyx":868
+  /* "pysam/calignmentfile.pyx":892
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         has_coord, rtid, rstart, rend = self._parseRegion(
  */
-  __pyx_tuple__29 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__29)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__29);
-  __Pyx_GIVEREF(__pyx_tuple__29);
+  __pyx_tuple__27 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__27)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__27);
+  __Pyx_GIVEREF(__pyx_tuple__27);
 
-  /* "pysam/csamfile.pyx":875
+  /* "pysam/calignmentfile.pyx":899
  *         if self.isbam:
  *             if not self._hasIndex():
  *                 raise ValueError("no index available for pileup")             # <<<<<<<<<<<<<<
  * 
  *             if has_coord:
  */
-  __pyx_tuple__30 = PyTuple_Pack(1, __pyx_kp_s_no_index_available_for_pileup); if (unlikely(!__pyx_tuple__30)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__30);
-  __Pyx_GIVEREF(__pyx_tuple__30);
+  __pyx_tuple__28 = PyTuple_Pack(1, __pyx_kp_s_no_index_available_for_pileup); if (unlikely(!__pyx_tuple__28)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 899; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__28);
+  __Pyx_GIVEREF(__pyx_tuple__28);
 
-  /* "pysam/csamfile.pyx":887
+  /* "pysam/calignmentfile.pyx":911
  * 
  *         else:
  *             raise NotImplementedError( "pileup of samfiles not implemented yet" )             # <<<<<<<<<<<<<<
  * 
  *     def close( self ):
  */
-  __pyx_tuple__31 = PyTuple_Pack(1, __pyx_kp_s_pileup_of_samfiles_not_implement); if (unlikely(!__pyx_tuple__31)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__31);
-  __Pyx_GIVEREF(__pyx_tuple__31);
+  __pyx_tuple__29 = PyTuple_Pack(1, __pyx_kp_s_pileup_of_samfiles_not_implement); if (unlikely(!__pyx_tuple__29)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__29);
+  __Pyx_GIVEREF(__pyx_tuple__29);
 
-  /* "pysam/csamfile.pyx":941
+  /* "pysam/calignmentfile.pyx":965
  *         '''number of :term:`reference` sequences in the file.'''
  *         def __get__(self):
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *             return self.header.n_targets
  * 
  */
-  __pyx_tuple__32 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__32);
-  __Pyx_GIVEREF(__pyx_tuple__32);
+  __pyx_tuple__30 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__30)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__30);
+  __Pyx_GIVEREF(__pyx_tuple__30);
 
-  /* "pysam/csamfile.pyx":947
+  /* "pysam/calignmentfile.pyx":971
  *         """tuple with the names of :term:`reference` sequences."""
  *         def __get__(self):
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *             t = []
  *             for x from 0 <= x < self.header.n_targets:
  */
-  __pyx_tuple__33 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__33);
-  __Pyx_GIVEREF(__pyx_tuple__33);
+  __pyx_tuple__31 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__31)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__31);
+  __Pyx_GIVEREF(__pyx_tuple__31);
 
-  /* "pysam/csamfile.pyx":958
+  /* "pysam/calignmentfile.pyx":984
  *         """
  *         def __get__(self):
  *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *             t = []
  *             for x from 0 <= x < self.header.n_targets:
  */
-  __pyx_tuple__34 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__34)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__34);
-  __Pyx_GIVEREF(__pyx_tuple__34);
+  __pyx_tuple__32 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__32);
+  __Pyx_GIVEREF(__pyx_tuple__32);
 
-  /* "pysam/csamfile.pyx":981
+  /* "pysam/calignmentfile.pyx":1007
  *         an error.'''
  *         if not self._isOpen():
  *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")
+ *             raise AttributeError("AlignmentFile.mapped only available in bam files")
  */
-  __pyx_tuple__35 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__35)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__35);
-  __Pyx_GIVEREF(__pyx_tuple__35);
+  __pyx_tuple__33 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__33);
+  __Pyx_GIVEREF(__pyx_tuple__33);
 
-  /* "pysam/csamfile.pyx":983
+  /* "pysam/calignmentfile.pyx":1009
  *             raise ValueError("I/O operation on closed file")
  *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")             # <<<<<<<<<<<<<<
+ *             raise AttributeError("AlignmentFile.mapped only available in bam files")             # <<<<<<<<<<<<<<
  *         if self.index == NULL:
  *             raise ValueError("mapping information not recorded in index "
  */
-  __pyx_tuple__36 = PyTuple_Pack(1, __pyx_kp_s_Samfile_mapped_only_available_in); if (unlikely(!__pyx_tuple__36)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__36);
-  __Pyx_GIVEREF(__pyx_tuple__36);
+  __pyx_tuple__34 = PyTuple_Pack(1, __pyx_kp_s_AlignmentFile_mapped_only_availa); if (unlikely(!__pyx_tuple__34)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__34);
+  __Pyx_GIVEREF(__pyx_tuple__34);
 
-  /* "pysam/csamfile.pyx":985
- *             raise AttributeError("Samfile.mapped only available in bam files")
+  /* "pysam/calignmentfile.pyx":1011
+ *             raise AttributeError("AlignmentFile.mapped only available in bam files")
  *         if self.index == NULL:
  *             raise ValueError("mapping information not recorded in index "             # <<<<<<<<<<<<<<
  *                                  "or index not available")
  * 
  */
-  __pyx_tuple__37 = PyTuple_Pack(1, __pyx_kp_s_mapping_information_not_recorded); if (unlikely(!__pyx_tuple__37)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__37);
-  __Pyx_GIVEREF(__pyx_tuple__37);
+  __pyx_tuple__35 = PyTuple_Pack(1, __pyx_kp_s_mapping_information_not_recorded); if (unlikely(!__pyx_tuple__35)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__35);
+  __Pyx_GIVEREF(__pyx_tuple__35);
 
-  /* "pysam/csamfile.pyx":1012
- *         '''full contents of the :term:`sam file` header as a string.'''
+  /* "pysam/calignmentfile.pyx":1044
  *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             if not self._isOpen():
+ *                 raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *             return from_string_and_size(self.header.text, self.header.l_text)
  * 
  */
-  __pyx_tuple__38 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__38)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__38);
-  __Pyx_GIVEREF(__pyx_tuple__38);
+  __pyx_tuple__36 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__36)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__36);
+  __Pyx_GIVEREF(__pyx_tuple__36);
 
-  /* "pysam/csamfile.pyx":1020
- *         '''
+  /* "pysam/calignmentfile.pyx":1070
  *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             if not self._isOpen():
+ *                 raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *             result = {}
  */
-  __pyx_tuple__39 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__39)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__39);
-  __Pyx_GIVEREF(__pyx_tuple__39);
+  __pyx_tuple__37 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__37)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__37);
+  __Pyx_GIVEREF(__pyx_tuple__37);
 
-  /* "pysam/csamfile.pyx":1027
- *                 # convert to python string (note: call self.text to create 0-terminated string)
+  /* "pysam/calignmentfile.pyx":1078
+ *                 # create 0-terminated string)
  *                 t = self.text
  *                 for line in t.split("\n"):             # <<<<<<<<<<<<<<
  *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
+ *                     assert line.startswith("@"), \
  */
-  __pyx_tuple__40 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__40)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__40);
-  __Pyx_GIVEREF(__pyx_tuple__40);
+  __pyx_tuple__39 = PyTuple_Pack(1, __pyx_kp_s__38); if (unlikely(!__pyx_tuple__39)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1078; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__39);
+  __Pyx_GIVEREF(__pyx_tuple__39);
 
-  /* "pysam/csamfile.pyx":1029
+  /* "pysam/calignmentfile.pyx":1080
  *                 for line in t.split("\n"):
  *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line             # <<<<<<<<<<<<<<
+ *                     assert line.startswith("@"), \             # <<<<<<<<<<<<<<
+ *                         "header line without '@': '%s'" % line
  *                     fields = line[1:].split("\t")
- *                     record = fields[0]
  */
-  __pyx_tuple__42 = PyTuple_Pack(1, __pyx_kp_s__41); if (unlikely(!__pyx_tuple__42)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__42);
-  __Pyx_GIVEREF(__pyx_tuple__42);
+  __pyx_tuple__41 = PyTuple_Pack(1, __pyx_kp_s__40); if (unlikely(!__pyx_tuple__41)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__41);
+  __Pyx_GIVEREF(__pyx_tuple__41);
 
-  /* "pysam/csamfile.pyx":1030
- *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
+  /* "pysam/calignmentfile.pyx":1082
+ *                     assert line.startswith("@"), \
+ *                         "header line without '@': '%s'" % line
  *                     fields = line[1:].split("\t")             # <<<<<<<<<<<<<<
  *                     record = fields[0]
- *                     assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)
+ *                     assert record in VALID_HEADER_TYPES, \
  */
-  __pyx_slice__43 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__43)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__43);
-  __Pyx_GIVEREF(__pyx_slice__43);
-  __pyx_tuple__44 = PyTuple_Pack(1, __pyx_kp_s__3); if (unlikely(!__pyx_tuple__44)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__42 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__42)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_slice__42);
+  __Pyx_GIVEREF(__pyx_slice__42);
+  __pyx_tuple__44 = PyTuple_Pack(1, __pyx_kp_s__43); if (unlikely(!__pyx_tuple__44)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__44);
   __Pyx_GIVEREF(__pyx_tuple__44);
 
-  /* "pysam/csamfile.pyx":1037
- *                     if record == "CO":
- *                         if record not in result: result[record] = []
- *                         result[record].append( "\t".join( fields[1:] ) )             # <<<<<<<<<<<<<<
+  /* "pysam/calignmentfile.pyx":1091
+ *                         if record not in result:
+ *                             result[record] = []
+ *                         result[record].append("\t".join( fields[1:]))             # <<<<<<<<<<<<<<
  *                         continue
  *                     # the following is clumsy as generators do not work?
  */
-  __pyx_slice__45 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__45)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__45 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__45)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__45);
   __Pyx_GIVEREF(__pyx_slice__45);
 
-  /* "pysam/csamfile.pyx":1041
- *                     # the following is clumsy as generators do not work?
+  /* "pysam/calignmentfile.pyx":1096
  *                     x = {}
- *                     for field in fields[1:]:             # <<<<<<<<<<<<<<
+ * 
+ *                     for idx, field in enumerate(fields[1:]):             # <<<<<<<<<<<<<<
  *                         if ":" not in field:
  *                             raise ValueError("malformatted header: no ':' in field" )
  */
-  __pyx_slice__46 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__46)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__46 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__46)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__46);
   __Pyx_GIVEREF(__pyx_slice__46);
 
-  /* "pysam/csamfile.pyx":1043
- *                     for field in fields[1:]:
+  /* "pysam/calignmentfile.pyx":1098
+ *                     for idx, field in enumerate(fields[1:]):
  *                         if ":" not in field:
  *                             raise ValueError("malformatted header: no ':' in field" )             # <<<<<<<<<<<<<<
- *                         key, value = field.split(":",1)
- *                         # uppercase keys must be valid
+ *                         key, value = field.split(":", 1)
+ *                         if key in ("CL",):
  */
-  __pyx_tuple__48 = PyTuple_Pack(1, __pyx_kp_s_malformatted_header_no_in_field); if (unlikely(!__pyx_tuple__48)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__48 = PyTuple_Pack(1, __pyx_kp_s_malformatted_header_no_in_field); if (unlikely(!__pyx_tuple__48)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1098; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__48);
   __Pyx_GIVEREF(__pyx_tuple__48);
 
-  /* "pysam/csamfile.pyx":1044
+  /* "pysam/calignmentfile.pyx":1099
  *                         if ":" not in field:
  *                             raise ValueError("malformatted header: no ':' in field" )
- *                         key, value = field.split(":",1)             # <<<<<<<<<<<<<<
- *                         # uppercase keys must be valid
- *                         # lowercase are permitted for user fields
+ *                         key, value = field.split(":", 1)             # <<<<<<<<<<<<<<
+ *                         if key in ("CL",):
+ *                             # special treatment for command line
  */
-  __pyx_tuple__49 = PyTuple_Pack(2, __pyx_kp_s__47, __pyx_int_1); if (unlikely(!__pyx_tuple__49)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__49 = PyTuple_Pack(2, __pyx_kp_s__47, __pyx_int_1); if (unlikely(!__pyx_tuple__49)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__49);
   __Pyx_GIVEREF(__pyx_tuple__49);
 
-  /* "pysam/csamfile.pyx":1144
+  /* "pysam/calignmentfile.pyx":1107
+ *                             # header. Thus, in contravention to the
+ *                             # SAM API, consume the rest of the line.
+ *                             key, value = "\t".join(fields[idx+1:]).split(":", 1)             # <<<<<<<<<<<<<<
+ *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
+ *                             break
+ */
+  __pyx_tuple__50 = PyTuple_Pack(2, __pyx_kp_s__47, __pyx_int_1); if (unlikely(!__pyx_tuple__50)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__50);
+  __Pyx_GIVEREF(__pyx_tuple__50);
+
+  /* "pysam/calignmentfile.pyx":1216
  *         dest.text = <char*>calloc( len(text), sizeof(char))
  *         dest.l_text = len(text)
  *         cdef bytes btext = text.encode('ascii')             # <<<<<<<<<<<<<<
  *         strncpy( dest.text, btext, dest.l_text )
  * 
  */
-  __pyx_tuple__50 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__50)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__50);
-  __Pyx_GIVEREF(__pyx_tuple__50);
+  __pyx_tuple__51 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__51)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__51);
+  __Pyx_GIVEREF(__pyx_tuple__51);
 
-  /* "pysam/csamfile.pyx":1164
- *                 seqname, seqlen = seqs[x]
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
+  /* "pysam/calignmentfile.pyx":1237
+ *                 dest.target_name[x] = <char*>calloc(
+ *                     len(seqname) + 1, sizeof(char))
  *                 bseqname = seqname.encode('ascii')             # <<<<<<<<<<<<<<
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
- *                 dest.target_len[x] = seqlen
+ *                 strncpy(dest.target_name[x], bseqname,
+ *                         len(seqname) + 1)
  */
-  __pyx_tuple__51 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__51)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__51);
-  __Pyx_GIVEREF(__pyx_tuple__51);
+  __pyx_tuple__52 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__52)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__52);
+  __Pyx_GIVEREF(__pyx_tuple__52);
 
-  /* "pysam/csamfile.pyx":1180
+  /* "pysam/calignmentfile.pyx":1254
  *     def __iter__(self):
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         if not self.isbam and self.header.n_targets == 0:
  */
-  __pyx_tuple__52 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__52)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__52);
-  __Pyx_GIVEREF(__pyx_tuple__52);
+  __pyx_tuple__53 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__53)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__53);
+  __Pyx_GIVEREF(__pyx_tuple__53);
 
-  /* "pysam/csamfile.pyx":1183
+  /* "pysam/calignmentfile.pyx":1257
  * 
  *         if not self.isbam and self.header.n_targets == 0:
  *             raise NotImplementedError(             # <<<<<<<<<<<<<<
  *                 "can not iterate over samfile without header")
  *         return self
  */
-  __pyx_tuple__53 = PyTuple_Pack(1, __pyx_kp_s_can_not_iterate_over_samfile_wit); if (unlikely(!__pyx_tuple__53)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__53);
-  __Pyx_GIVEREF(__pyx_tuple__53);
+  __pyx_tuple__54 = PyTuple_Pack(1, __pyx_kp_s_can_not_iterate_over_samfile_wit); if (unlikely(!__pyx_tuple__54)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__54);
+  __Pyx_GIVEREF(__pyx_tuple__54);
+
+  /* "pysam/calignmentfile.pyx":1280
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise StopIteration
+ */
+  __pyx_tuple__55 = PyTuple_Pack(1, __pyx_kp_s_truncated_file); if (unlikely(!__pyx_tuple__55)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1280; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__55);
+  __Pyx_GIVEREF(__pyx_tuple__55);
 
-  /* "pysam/csamfile.pyx":1239
+  /* "pysam/calignmentfile.pyx":1313
  * 
  *         if not samfile._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  * 
  *         # makes sure that samfile stays alive as long as the
  */
-  __pyx_tuple__54 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__54)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__54);
-  __Pyx_GIVEREF(__pyx_tuple__54);
+  __pyx_tuple__56 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__56)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1313; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__56);
+  __Pyx_GIVEREF(__pyx_tuple__56);
 
-  /* "pysam/csamfile.pyx":1298
+  /* "pysam/calignmentfile.pyx":1364
  * 
  *         if not samfile._hasIndex():
  *             raise ValueError( "no index available for iteration" )             # <<<<<<<<<<<<<<
  * 
  *         self.iter = sam_itr_queryi(
  */
-  __pyx_tuple__55 = PyTuple_Pack(1, __pyx_kp_s_no_index_available_for_iteration); if (unlikely(!__pyx_tuple__55)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1298; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__55);
-  __Pyx_GIVEREF(__pyx_tuple__55);
+  __pyx_tuple__57 = PyTuple_Pack(1, __pyx_kp_s_no_index_available_for_iteration); if (unlikely(!__pyx_tuple__57)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__57);
+  __Pyx_GIVEREF(__pyx_tuple__57);
+
+  /* "pysam/calignmentfile.pyx":1440
+ *             return makeAlignedSegment( self.b )
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise StopIteration
+ */
+  __pyx_tuple__58 = PyTuple_Pack(1, __pyx_kp_s_truncated_file); if (unlikely(!__pyx_tuple__58)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__58);
+  __Pyx_GIVEREF(__pyx_tuple__58);
+
+  /* "pysam/calignmentfile.pyx":1482
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise StopIteration
+ */
+  __pyx_tuple__59 = PyTuple_Pack(1, __pyx_kp_s_truncated_file); if (unlikely(!__pyx_tuple__59)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__59);
+  __Pyx_GIVEREF(__pyx_tuple__59);
 
-  /* "pysam/csamfile.pyx":1443
+  /* "pysam/calignmentfile.pyx":1501
  * 
  *         if not samfile._hasIndex():
  *             raise ValueError("no index available for fetch")             # <<<<<<<<<<<<<<
  * 
  *         self.tid = -1
  */
-  __pyx_tuple__56 = PyTuple_Pack(1, __pyx_kp_s_no_index_available_for_fetch); if (unlikely(!__pyx_tuple__56)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__56);
-  __Pyx_GIVEREF(__pyx_tuple__56);
+  __pyx_tuple__60 = PyTuple_Pack(1, __pyx_kp_s_no_index_available_for_fetch); if (unlikely(!__pyx_tuple__60)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__60);
+  __Pyx_GIVEREF(__pyx_tuple__60);
+
+  /* "pysam/calignmentfile.pyx":1600
+ *             return makeAlignedSegment(self.b)
+ *         elif (ret == -2):
+ *             raise IOError('truncated file')             # <<<<<<<<<<<<<<
+ *         else:
+ *             raise StopIteration
+ */
+  __pyx_tuple__61 = PyTuple_Pack(1, __pyx_kp_s_truncated_file); if (unlikely(!__pyx_tuple__61)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__61);
+  __Pyx_GIVEREF(__pyx_tuple__61);
 
-  /* "pysam/csamfile.pyx":1805
+  /* "pysam/calignmentfile.pyx":1908
  *             self.cnext()
  *             if self.n_plp < 0:
  *                 raise ValueError("error during iteration" )             # <<<<<<<<<<<<<<
  * 
  *             if self.plp == NULL:
  */
-  __pyx_tuple__58 = PyTuple_Pack(1, __pyx_kp_s_error_during_iteration); if (unlikely(!__pyx_tuple__58)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__58);
-  __Pyx_GIVEREF(__pyx_tuple__58);
+  __pyx_tuple__63 = PyTuple_Pack(1, __pyx_kp_s_error_during_iteration); if (unlikely(!__pyx_tuple__63)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__63);
+  __Pyx_GIVEREF(__pyx_tuple__63);
 
-  /* "pysam/csamfile.pyx":1843
+  /* "pysam/calignmentfile.pyx":1946
  * 
  *             if self.n_plp < 0:
  *                 raise ValueError("error during iteration" )             # <<<<<<<<<<<<<<
  * 
  *             # return result, if within same reference
  */
-  __pyx_tuple__59 = PyTuple_Pack(1, __pyx_kp_s_error_during_iteration); if (unlikely(!__pyx_tuple__59)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__59);
-  __Pyx_GIVEREF(__pyx_tuple__59);
+  __pyx_tuple__64 = PyTuple_Pack(1, __pyx_kp_s_error_during_iteration); if (unlikely(!__pyx_tuple__64)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__64);
+  __Pyx_GIVEREF(__pyx_tuple__64);
 
-  /* "pysam/csamfile.pyx":1978
+  /* "pysam/calignmentfile.pyx":2099
  * 
  *     if not type(pytag) is bytes:
  *         pytag = pytag.encode('ascii')             # <<<<<<<<<<<<<<
  *     t = type(value)
  * 
  */
-  __pyx_tuple__60 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__60)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1978; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__60);
-  __Pyx_GIVEREF(__pyx_tuple__60);
+  __pyx_tuple__65 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__65)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__65);
+  __Pyx_GIVEREF(__pyx_tuple__65);
 
-  /* "pysam/csamfile.pyx":2005
- * 
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
- */
-  __pyx_slice__61 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__61)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__61);
-  __Pyx_GIVEREF(__pyx_slice__61);
-
-  /* "pysam/csamfile.pyx":2006
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )
- */
-  __pyx_tuple__62 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__62)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__62);
-  __Pyx_GIVEREF(__pyx_tuple__62);
-
-  /* "pysam/csamfile.pyx":2007
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       len(value)] + list(value) )
- *         fmts.append( datafmt )
- */
-  __pyx_tuple__63 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__63)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__63);
-  __Pyx_GIVEREF(__pyx_tuple__63);
-
-  /* "pysam/csamfile.pyx":2030
- *         # Note: hex strings (H) are not supported yet
- *         if t is not bytes:
- *             value = value.encode('ascii')             # <<<<<<<<<<<<<<
- *         if len(value) == 1:
- *             fmt, pytype = "2scc", 'A'
- */
-  __pyx_tuple__64 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__64)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__64);
-  __Pyx_GIVEREF(__pyx_tuple__64);
-
-  /* "pysam/csamfile.pyx":2036
- *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- * 
- *     args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                   pytype.encode('ascii'),
- *                   value ] )
- */
-  __pyx_slice__65 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__65)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__65);
-  __Pyx_GIVEREF(__pyx_slice__65);
-
-  /* "pysam/csamfile.pyx":2037
- * 
- *     args.extend( [pytag[:2],
- *                   pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                   value ] )
- * 
+  /* "pysam/calignmentfile.pyx":2118
+ *                     datafmt, datatype = "h", 's'
+ *                 elif mi < -2147483648 or ma >= 2147483648:
+ *                     raise ValueError(             # <<<<<<<<<<<<<<
+ *                         "at least one signed integer out of range of "
+ *                         "BAM/SAM specification")
  */
-  __pyx_tuple__66 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__66)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__66 = PyTuple_Pack(1, __pyx_kp_s_at_least_one_signed_integer_out); if (unlikely(!__pyx_tuple__66)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__66);
   __Pyx_GIVEREF(__pyx_tuple__66);
 
-  /* "pysam/csamfile.pyx":2104
- *             qual = self.qual
- *         else:
- *             seq = self.seq.decode('ascii')             # <<<<<<<<<<<<<<
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,
+  /* "pysam/calignmentfile.pyx":2130
+ *                     datafmt, datatype = "H", 'S'
+ *                 elif ma >= 4294967296:
+ *                     raise ValueError(             # <<<<<<<<<<<<<<
+ *                         "at least one integer out of range of BAM/SAM specification")
+ *                 else:
  */
-  __pyx_tuple__67 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__67)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__67 = PyTuple_Pack(1, __pyx_kp_s_at_least_one_integer_out_of_rang); if (unlikely(!__pyx_tuple__67)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__67);
   __Pyx_GIVEREF(__pyx_tuple__67);
 
-  /* "pysam/csamfile.pyx":2105
- *         else:
- *             seq = self.seq.decode('ascii')
- *             qual = self.qual.decode('ascii')             # <<<<<<<<<<<<<<
- *         return "\t".join(map(str, (self.qname,
- *                                    self.flag,
+  /* "pysam/calignmentfile.pyx":2136
+ * 
+ *         datafmt = "2sccI%i%s" % (len(value), datafmt)
+ *         args.extend([pytag[:2],             # <<<<<<<<<<<<<<
+ *                      pytype.encode('ascii'),
+ *                      datatype.encode('ascii'),
  */
-  __pyx_tuple__68 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__68)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__68);
-  __Pyx_GIVEREF(__pyx_tuple__68);
+  __pyx_slice__68 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__68)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_slice__68);
+  __Pyx_GIVEREF(__pyx_slice__68);
 
-  /* "pysam/csamfile.pyx":2156
- * 
- *         if not type(pytag) is bytes:
- *             pytag = pytag.encode('ascii')             # <<<<<<<<<<<<<<
- *         t = type(value)
- * 
+  /* "pysam/calignmentfile.pyx":2137
+ *         datafmt = "2sccI%i%s" % (len(value), datafmt)
+ *         args.extend([pytag[:2],
+ *                      pytype.encode('ascii'),             # <<<<<<<<<<<<<<
+ *                      datatype.encode('ascii'),
+ *                      len(value)] + list(value))
  */
-  __pyx_tuple__69 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__69)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__69 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__69)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__69);
   __Pyx_GIVEREF(__pyx_tuple__69);
 
-  /* "pysam/csamfile.pyx":2183
- * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
+  /* "pysam/calignmentfile.pyx":2138
+ *         args.extend([pytag[:2],
+ *                      pytype.encode('ascii'),
+ *                      datatype.encode('ascii'),             # <<<<<<<<<<<<<<
+ *                      len(value)] + list(value))
+ *         fmts.append( datafmt )
  */
-  __pyx_slice__70 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__70)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__70);
-  __Pyx_GIVEREF(__pyx_slice__70);
+  __pyx_tuple__70 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__70)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__70);
+  __Pyx_GIVEREF(__pyx_tuple__70);
 
-  /* "pysam/csamfile.pyx":2184
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )
+  /* "pysam/calignmentfile.pyx":2161
+ *         # Note: hex strings (H) are not supported yet
+ *         if t is not bytes:
+ *             value = value.encode('ascii')             # <<<<<<<<<<<<<<
+ *         if len(value) == 1:
+ *             fmt, pytype = "2scc", 'A'
  */
-  __pyx_tuple__71 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__71)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__71 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__71)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2161; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__71);
   __Pyx_GIVEREF(__pyx_tuple__71);
 
-  /* "pysam/csamfile.pyx":2185
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                           len(value)] + list(value) )
- *             fmts.append( datafmt )
- */
-  __pyx_tuple__72 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__72)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__72);
-  __Pyx_GIVEREF(__pyx_tuple__72);
-
-  /* "pysam/csamfile.pyx":2208
- *             # Note: hex strings (H) are not supported yet
- *             if t is not bytes:
- *                 value = value.encode('ascii')             # <<<<<<<<<<<<<<
- *             if len(value) == 1:
- *                 fmt, pytype = "2scc", 'A'
- */
-  __pyx_tuple__73 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__73)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__73);
-  __Pyx_GIVEREF(__pyx_tuple__73);
-
-  /* "pysam/csamfile.pyx":2214
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
+  /* "pysam/calignmentfile.pyx":2167
+ *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
  * 
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       value ] )
+ *     args.extend([pytag[:2],             # <<<<<<<<<<<<<<
+ *                  pytype.encode('ascii'),
+ *                  value])
  */
-  __pyx_slice__74 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__74)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__74);
-  __Pyx_GIVEREF(__pyx_slice__74);
+  __pyx_slice__72 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__72)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_slice__72);
+  __Pyx_GIVEREF(__pyx_slice__72);
 
-  /* "pysam/csamfile.pyx":2215
+  /* "pysam/calignmentfile.pyx":2168
  * 
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       value ] )
+ *     args.extend([pytag[:2],
+ *                  pytype.encode('ascii'),             # <<<<<<<<<<<<<<
+ *                  value])
  * 
  */
-  __pyx_tuple__75 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__75)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__75);
-  __Pyx_GIVEREF(__pyx_tuple__75);
+  __pyx_tuple__73 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__73)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__73);
+  __Pyx_GIVEREF(__pyx_tuple__73);
 
-  /* "pysam/csamfile.pyx":2731
- *         type_code = get_type_code(value, value_type)
+  /* "pysam/calignmentfile.pyx":3206
+ *         type_code = _getTypeCode(value, value_type)
  *         if type_code == 0:
  *             raise ValueError("can't guess type or invalid type code specified")             # <<<<<<<<<<<<<<
  * 
  *         # Not Endian-safe, but then again neither is samtools!
  */
-  __pyx_tuple__77 = PyTuple_Pack(1, __pyx_kp_s_can_t_guess_type_or_invalid_type); if (unlikely(!__pyx_tuple__77)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2731; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__77);
-  __Pyx_GIVEREF(__pyx_tuple__77);
+  __pyx_tuple__75 = PyTuple_Pack(1, __pyx_kp_s_can_t_guess_type_or_invalid_type); if (unlikely(!__pyx_tuple__75)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__75);
+  __Pyx_GIVEREF(__pyx_tuple__75);
 
-  /* "pysam/csamfile.pyx":2751
+  /* "pysam/calignmentfile.pyx":3226
  *             value_size   = sizeof(float)
  *         else:
  *             raise ValueError('Unsupported value_type in set_option')             # <<<<<<<<<<<<<<
  * 
  *         tag = _forceBytes( tag )
  */
-  __pyx_tuple__78 = PyTuple_Pack(1, __pyx_kp_s_Unsupported_value_type_in_set_op); if (unlikely(!__pyx_tuple__78)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__78);
-  __Pyx_GIVEREF(__pyx_tuple__78);
+  __pyx_tuple__76 = PyTuple_Pack(1, __pyx_kp_s_Unsupported_value_type_in_set_op); if (unlikely(!__pyx_tuple__76)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__76);
+  __Pyx_GIVEREF(__pyx_tuple__76);
 
-  /* "pysam/csamfile.pyx":3253
+  /* "pysam/calignmentfile.pyx":3416
  *     '''
  *     def __init__(self):
- *         raise TypeError("This class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
+ *         raise TypeError("this class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
  * 
  *     def __str__(self):
  */
-  __pyx_tuple__81 = PyTuple_Pack(1, __pyx_kp_s_This_class_cannot_be_instantiate); if (unlikely(!__pyx_tuple__81)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__81);
-  __Pyx_GIVEREF(__pyx_tuple__81);
+  __pyx_tuple__77 = PyTuple_Pack(1, __pyx_kp_s_this_class_cannot_be_instantiate); if (unlikely(!__pyx_tuple__77)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__77);
+  __Pyx_GIVEREF(__pyx_tuple__77);
 
-  /* "pysam/csamfile.pyx":3279
+  /* "pysam/calignmentfile.pyx":3449
  * 
  *             if self.plp == NULL or self.plp[0] == NULL:
- *                 raise ValueError("PileupProxy accessed after iterator finished")             # <<<<<<<<<<<<<<
+ *                 raise ValueError("PileupColumn accessed after iterator finished")             # <<<<<<<<<<<<<<
  * 
  *             # warning: there could be problems if self.n and self.buf are
  */
-  __pyx_tuple__82 = PyTuple_Pack(1, __pyx_kp_s_PileupProxy_accessed_after_itera); if (unlikely(!__pyx_tuple__82)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__82);
-  __Pyx_GIVEREF(__pyx_tuple__82);
+  __pyx_tuple__78 = PyTuple_Pack(1, __pyx_kp_s_PileupColumn_accessed_after_iter); if (unlikely(!__pyx_tuple__78)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__78);
+  __Pyx_GIVEREF(__pyx_tuple__78);
 
-  /* "pysam/csamfile.pyx":3292
+  /* "pysam/calignmentfile.pyx":3487
  * 
  *     def __init__(self):
- *         raise TypeError("This class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
+ *         raise TypeError("this class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
  * 
  *     def __str__(self):
  */
-  __pyx_tuple__83 = PyTuple_Pack(1, __pyx_kp_s_This_class_cannot_be_instantiate); if (unlikely(!__pyx_tuple__83)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__83);
-  __Pyx_GIVEREF(__pyx_tuple__83);
+  __pyx_tuple__79 = PyTuple_Pack(1, __pyx_kp_s_this_class_cannot_be_instantiate); if (unlikely(!__pyx_tuple__79)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__79);
+  __Pyx_GIVEREF(__pyx_tuple__79);
 
-  /* "pysam/csamfile.pyx":108
+  /* "pysam/calignmentfile.pyx":113
  * else:
  *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
  * CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )             # <<<<<<<<<<<<<<
  * 
  * #####################################################################
  */
-  __pyx_tuple__84 = PyTuple_Pack(1, __pyx_kp_s_d_MIDNSHP_X); if (unlikely(!__pyx_tuple__84)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__84);
-  __Pyx_GIVEREF(__pyx_tuple__84);
-
-  /* "pysam/csamfile.pyx":210
- *         list of reads (:class:`pysam.PileupRead`) aligned to this column
- *     '''
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )
- */
-  __pyx_tuple__85 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__85)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__85);
-  __Pyx_GIVEREF(__pyx_tuple__85);
-  __pyx_codeobj__86 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__85, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_str, 210, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__86)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__80 = PyTuple_Pack(1, __pyx_kp_s_d_MIDNSHP_X); if (unlikely(!__pyx_tuple__80)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__80);
+  __Pyx_GIVEREF(__pyx_tuple__80);
 
-  /* "pysam/csamfile.pyx":223
+  /* "pysam/calignmentfile.pyx":211
  * 
- * # order of records within sam headers
+ * # order of records within SAM headers
  * VALID_HEADERS = ("HD", "SQ", "RG", "PG", "CO")             # <<<<<<<<<<<<<<
  * 
- * # type conversions within sam header records
+ * # default type conversions within SAM header records
  */
-  __pyx_tuple__87 = PyTuple_Pack(5, __pyx_n_s_HD, __pyx_n_s_SQ, __pyx_n_s_RG, __pyx_n_s_PG, __pyx_n_s_CO); if (unlikely(!__pyx_tuple__87)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__87);
-  __Pyx_GIVEREF(__pyx_tuple__87);
+  __pyx_tuple__81 = PyTuple_Pack(5, __pyx_n_s_HD, __pyx_n_s_SQ, __pyx_n_s_RG, __pyx_n_s_PG, __pyx_n_s_CO); if (unlikely(!__pyx_tuple__81)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__81);
+  __Pyx_GIVEREF(__pyx_tuple__81);
 
-  /* "pysam/csamfile.pyx":237
+  /* "pysam/calignmentfile.pyx":225
  * 
  * # output order of fields within records
  * VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),             # <<<<<<<<<<<<<<
  *                       "SQ" : ("SN", "LN", "AS", "M5",
  *                                "UR", "SP"),
  */
-  __pyx_tuple__88 = PyTuple_Pack(3, __pyx_n_s_VN, __pyx_n_s_SO, __pyx_n_s_GO); if (unlikely(!__pyx_tuple__88)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__88);
-  __Pyx_GIVEREF(__pyx_tuple__88);
+  __pyx_tuple__82 = PyTuple_Pack(3, __pyx_n_s_VN, __pyx_n_s_SO, __pyx_n_s_GO); if (unlikely(!__pyx_tuple__82)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__82);
+  __Pyx_GIVEREF(__pyx_tuple__82);
 
-  /* "pysam/csamfile.pyx":238
+  /* "pysam/calignmentfile.pyx":226
  * # output order of fields within records
  * VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),
  *                       "SQ" : ("SN", "LN", "AS", "M5",             # <<<<<<<<<<<<<<
  *                                "UR", "SP"),
  *                       "RG" : ("ID", "SM", "LB", "DS",
  */
-  __pyx_tuple__89 = PyTuple_Pack(6, __pyx_n_s_SN, __pyx_n_s_LN, __pyx_n_s_AS, __pyx_n_s_M5, __pyx_n_s_UR, __pyx_n_s_SP); if (unlikely(!__pyx_tuple__89)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__89);
-  __Pyx_GIVEREF(__pyx_tuple__89);
+  __pyx_tuple__83 = PyTuple_Pack(6, __pyx_n_s_SN, __pyx_n_s_LN, __pyx_n_s_AS, __pyx_n_s_M5, __pyx_n_s_UR, __pyx_n_s_SP); if (unlikely(!__pyx_tuple__83)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__83);
+  __Pyx_GIVEREF(__pyx_tuple__83);
 
-  /* "pysam/csamfile.pyx":240
+  /* "pysam/calignmentfile.pyx":228
  *                       "SQ" : ("SN", "LN", "AS", "M5",
  *                                "UR", "SP"),
  *                       "RG" : ("ID", "SM", "LB", "DS",             # <<<<<<<<<<<<<<
  *                               "PU", "PI", "CN", "DT",
  *                               "PL", "FO", "KS", "PG"),
  */
-  __pyx_tuple__90 = PyTuple_Pack(12, __pyx_n_s_ID, __pyx_n_s_SM, __pyx_n_s_LB, __pyx_n_s_DS, __pyx_n_s_PU, __pyx_n_s_PI, __pyx_n_s_CN, __pyx_n_s_DT, __pyx_n_s_PL, __pyx_n_s_FO, __pyx_n_s_KS, __pyx_n_s_PG); if (unlikely(!__pyx_tuple__90)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 240; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__90);
-  __Pyx_GIVEREF(__pyx_tuple__90);
+  __pyx_tuple__84 = PyTuple_Pack(12, __pyx_n_s_ID, __pyx_n_s_SM, __pyx_n_s_LB, __pyx_n_s_DS, __pyx_n_s_PU, __pyx_n_s_PI, __pyx_n_s_CN, __pyx_n_s_DT, __pyx_n_s_PL, __pyx_n_s_FO, __pyx_n_s_KS, __pyx_n_s_PG); if (unlikely(!__pyx_tuple__84)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 228; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__84);
+  __Pyx_GIVEREF(__pyx_tuple__84);
 
-  /* "pysam/csamfile.pyx":243
+  /* "pysam/calignmentfile.pyx":231
  *                               "PU", "PI", "CN", "DT",
  *                               "PL", "FO", "KS", "PG"),
  *                       "PG" : ("PN", "ID", "VN", "CL",             # <<<<<<<<<<<<<<
  *                               "PP"),}
  * 
  */
-  __pyx_tuple__91 = PyTuple_Pack(5, __pyx_n_s_PN, __pyx_n_s_ID, __pyx_n_s_VN, __pyx_n_s_CL, __pyx_n_s_PP); if (unlikely(!__pyx_tuple__91)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__91);
-  __Pyx_GIVEREF(__pyx_tuple__91);
+  __pyx_tuple__85 = PyTuple_Pack(5, __pyx_n_s_PN, __pyx_n_s_ID, __pyx_n_s_VN, __pyx_n_s_CL, __pyx_n_s_PP); if (unlikely(!__pyx_tuple__85)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 231; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__85);
+  __Pyx_GIVEREF(__pyx_tuple__85);
+
+  /* "pysam/calignmentfile.pyx":2052
+ * 
+ * 
+ * def toQualityString(qualities):             # <<<<<<<<<<<<<<
+ *     '''convert a list of quality score to the string
+ *     representation used in the SAM format.'''
+ */
+  __pyx_tuple__86 = PyTuple_Pack(2, __pyx_n_s_qualities, __pyx_n_s_x); if (unlikely(!__pyx_tuple__86)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__86);
+  __Pyx_GIVEREF(__pyx_tuple__86);
+  __pyx_codeobj__87 = (PyObject*)__Pyx_PyCode_New(1, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__86, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_toQualityString, 2052, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__87)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/calignmentfile.pyx":2060
+ * 
+ * 
+ * def fromQualityString(quality_string):             # <<<<<<<<<<<<<<
+ *     '''return a list of quality scores from the
+ *     stringn representation of quality scores used
+ */
+  __pyx_tuple__88 = PyTuple_Pack(2, __pyx_n_s_quality_string, __pyx_n_s_x); if (unlikely(!__pyx_tuple__88)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2060; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__88);
+  __Pyx_GIVEREF(__pyx_tuple__88);
+  __pyx_codeobj__89 = (PyObject*)__Pyx_PyCode_New(1, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__88, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_fromQualityString, 2060, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__89)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2060; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_RefNannyFinishContext();
   return 0;
   __pyx_L1_error:;
@@ -39560,15 +43783,23 @@ static int __Pyx_InitGlobals(void) {
   __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_error;}
   __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_error;}
   __pyx_int_2 = PyInt_FromLong(2); if (unlikely(!__pyx_int_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_3 = PyInt_FromLong(3); if (unlikely(!__pyx_int_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_int_33 = PyInt_FromLong(33); if (unlikely(!__pyx_int_33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_int_128 = PyInt_FromLong(128); if (unlikely(!__pyx_int_128)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_int_255 = PyInt_FromLong(255); if (unlikely(!__pyx_int_255)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_int_256 = PyInt_FromLong(256); if (unlikely(!__pyx_int_256)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_int_8000 = PyInt_FromLong(8000); if (unlikely(!__pyx_int_8000)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_int_32768 = PyInt_FromLong(32768L); if (unlikely(!__pyx_int_32768)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_int_65535 = PyInt_FromLong(65535L); if (unlikely(!__pyx_int_65535)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_int_65536 = PyInt_FromLong(65536L); if (unlikely(!__pyx_int_65536)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_int_536870912 = PyInt_FromLong(536870912L); if (unlikely(!__pyx_int_536870912)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_int_2147483648 = PyInt_FromString((char *)"2147483648", 0, 0); if (unlikely(!__pyx_int_2147483648)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_int_4294967295 = PyInt_FromString((char *)"4294967295", 0, 0); if (unlikely(!__pyx_int_4294967295)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_int_4294967296 = PyInt_FromString((char *)"4294967296", 0, 0); if (unlikely(!__pyx_int_4294967296)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_int_neg_4 = PyInt_FromLong(-4); if (unlikely(!__pyx_int_neg_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_int_neg_127 = PyInt_FromLong(-127); if (unlikely(!__pyx_int_neg_127)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_int_neg_128 = PyInt_FromLong(-128); if (unlikely(!__pyx_int_neg_128)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_int_neg_32767 = PyInt_FromLong(-32767L); if (unlikely(!__pyx_int_neg_32767)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_int_neg_32768 = PyInt_FromLong(-32768L); if (unlikely(!__pyx_int_neg_32768)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_int_neg_2147483648 = PyInt_FromLong(-2147483648L); if (unlikely(!__pyx_int_neg_2147483648)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   return 0;
   __pyx_L1_error:;
@@ -39576,19 +43807,17 @@ static int __Pyx_InitGlobals(void) {
 }
 
 #if PY_MAJOR_VERSION < 3
-PyMODINIT_FUNC initcsamfile(void); /*proto*/
-PyMODINIT_FUNC initcsamfile(void)
+PyMODINIT_FUNC initcalignmentfile(void); /*proto*/
+PyMODINIT_FUNC initcalignmentfile(void)
 #else
-PyMODINIT_FUNC PyInit_csamfile(void); /*proto*/
-PyMODINIT_FUNC PyInit_csamfile(void)
+PyMODINIT_FUNC PyInit_calignmentfile(void); /*proto*/
+PyMODINIT_FUNC PyInit_calignmentfile(void)
 #endif
 {
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
   int __pyx_t_3;
   int __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -39602,7 +43831,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
           Py_FatalError("failed to import 'refnanny' module");
   }
   #endif
-  __Pyx_RefNannySetupContext("PyMODINIT_FUNC PyInit_csamfile(void)", 0);
+  __Pyx_RefNannySetupContext("PyMODINIT_FUNC PyInit_calignmentfile(void)", 0);
   if ( __Pyx_check_binary_version() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __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_error;}
   __pyx_empty_bytes = PyBytes_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_bytes)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
@@ -39624,7 +43853,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   #endif
   /*--- Module creation code ---*/
   #if PY_MAJOR_VERSION < 3
-  __pyx_m = Py_InitModule4(__Pyx_NAMESTR("csamfile"), __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m);
+  __pyx_m = Py_InitModule4(__Pyx_NAMESTR("calignmentfile"), __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m);
   #else
   __pyx_m = PyModule_Create(&__pyx_moduledef);
   #endif
@@ -39641,14 +43870,14 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT)
   if (__Pyx_init_sys_getdefaultencoding_params() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   #endif
-  if (__pyx_module_is_main_pysam__csamfile) {
+  if (__pyx_module_is_main_pysam__calignmentfile) {
     if (__Pyx_SetAttrString(__pyx_m, "__name__", __pyx_n_s_main) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   }
   #if PY_MAJOR_VERSION >= 3
   {
     PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (!PyDict_GetItemString(modules, "pysam.csamfile")) {
-      if (unlikely(PyDict_SetItemString(modules, "pysam.csamfile", __pyx_m) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!PyDict_GetItemString(modules, "pysam.calignmentfile")) {
+      if (unlikely(PyDict_SetItemString(modules, "pysam.calignmentfile", __pyx_m) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
   #endif
@@ -39657,232 +43886,238 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   /*--- Constants init code ---*/
   if (unlikely(__Pyx_InitCachedConstants() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   /*--- Global init code ---*/
-  __pyx_v_5pysam_8csamfile__FILENAME_ENCODING = ((PyObject*)Py_None); Py_INCREF(Py_None);
+  __pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING = ((PyObject*)Py_None); Py_INCREF(Py_None);
   /*--- Variable export code ---*/
   /*--- Function export code ---*/
   /*--- Type init code ---*/
-  __pyx_vtabptr_5pysam_8csamfile_AlignedRead = &__pyx_vtable_5pysam_8csamfile_AlignedRead;
-  __pyx_vtable_5pysam_8csamfile_AlignedRead.setTag = (PyObject *(*)(struct __pyx_obj_5pysam_8csamfile_AlignedRead *, PyObject *, PyObject *, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag *__pyx_optional_args))__pyx_f_5pysam_8csamfile_11AlignedRead_setTag;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_AlignedRead.tp_print = 0;
+  __pyx_vtabptr_5pysam_14calignmentfile_AlignedSegment = &__pyx_vtable_5pysam_14calignmentfile_AlignedSegment;
+  __pyx_vtable_5pysam_14calignmentfile_AlignedSegment.setTag = (PyObject *(*)(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *, PyObject *, PyObject *, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_14calignmentfile_14AlignedSegment_setTag *__pyx_optional_args))__pyx_f_5pysam_14calignmentfile_14AlignedSegment_setTag;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_AlignedSegment) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_AlignedSegment.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_AlignedRead, "__str__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_14calignmentfile_AlignedSegment, "__str__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_11AlignedRead_4__str__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_11AlignedRead_4__str__.doc = __pyx_doc_5pysam_8csamfile_11AlignedRead_4__str__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_11AlignedRead_4__str__;
+      __pyx_wrapperbase_5pysam_14calignmentfile_14AlignedSegment_4__str__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_14calignmentfile_14AlignedSegment_4__str__.doc = __pyx_doc_5pysam_14calignmentfile_14AlignedSegment_4__str__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_14calignmentfile_14AlignedSegment_4__str__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_AlignedRead.tp_dict, __pyx_vtabptr_5pysam_8csamfile_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "AlignedRead", (PyObject *)&__pyx_type_5pysam_8csamfile_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_AlignedRead = &__pyx_type_5pysam_8csamfile_AlignedRead;
-  __pyx_vtabptr_5pysam_8csamfile_Samfile = &__pyx_vtable_5pysam_8csamfile_Samfile;
-  __pyx_vtable_5pysam_8csamfile_Samfile._buildHeader = (bam_hdr_t *(*)(struct __pyx_obj_5pysam_8csamfile_Samfile *, PyObject *))__pyx_f_5pysam_8csamfile_7Samfile__buildHeader;
-  __pyx_vtable_5pysam_8csamfile_Samfile.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_Samfile *))__pyx_f_5pysam_8csamfile_7Samfile_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_Samfile.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_Samfile *))__pyx_f_5pysam_8csamfile_7Samfile_cnext;
-  __pyx_vtable_5pysam_8csamfile_Samfile.write = (int (*)(struct __pyx_obj_5pysam_8csamfile_Samfile *, struct __pyx_obj_5pysam_8csamfile_AlignedRead *, int __pyx_skip_dispatch))__pyx_f_5pysam_8csamfile_7Samfile_write;
-  __pyx_vtable_5pysam_8csamfile_Samfile._getrname = (char *(*)(struct __pyx_obj_5pysam_8csamfile_Samfile *, int))__pyx_f_5pysam_8csamfile_7Samfile__getrname;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_Samfile.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_14calignmentfile_AlignedSegment.tp_dict, __pyx_vtabptr_5pysam_14calignmentfile_AlignedSegment) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "AlignedSegment", (PyObject *)&__pyx_type_5pysam_14calignmentfile_AlignedSegment) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_AlignedSegment = &__pyx_type_5pysam_14calignmentfile_AlignedSegment;
+  __pyx_vtabptr_5pysam_14calignmentfile_AlignmentFile = &__pyx_vtable_5pysam_14calignmentfile_AlignmentFile;
+  __pyx_vtable_5pysam_14calignmentfile_AlignmentFile._buildHeader = (bam_hdr_t *(*)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *, PyObject *))__pyx_f_5pysam_14calignmentfile_13AlignmentFile__buildHeader;
+  __pyx_vtable_5pysam_14calignmentfile_AlignmentFile.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *))__pyx_f_5pysam_14calignmentfile_13AlignmentFile_getCurrent;
+  __pyx_vtable_5pysam_14calignmentfile_AlignmentFile.cnext = (int (*)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *))__pyx_f_5pysam_14calignmentfile_13AlignmentFile_cnext;
+  __pyx_vtable_5pysam_14calignmentfile_AlignmentFile.write = (int (*)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *, int __pyx_skip_dispatch))__pyx_f_5pysam_14calignmentfile_13AlignmentFile_write;
+  __pyx_vtable_5pysam_14calignmentfile_AlignmentFile._getrname = (char *(*)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *, int))__pyx_f_5pysam_14calignmentfile_13AlignmentFile__getrname;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_AlignmentFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_AlignmentFile.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_Samfile, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_14calignmentfile_AlignmentFile, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_7Samfile_46__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_7Samfile_46__next__.doc = __pyx_doc_5pysam_8csamfile_7Samfile_46__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_7Samfile_46__next__;
+      __pyx_wrapperbase_5pysam_14calignmentfile_13AlignmentFile_46__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_14calignmentfile_13AlignmentFile_46__next__.doc = __pyx_doc_5pysam_14calignmentfile_13AlignmentFile_46__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_14calignmentfile_13AlignmentFile_46__next__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_Samfile.tp_dict, __pyx_vtabptr_5pysam_8csamfile_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "Samfile", (PyObject *)&__pyx_type_5pysam_8csamfile_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_Samfile = &__pyx_type_5pysam_8csamfile_Samfile;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_PileupProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_PileupProxy.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "PileupProxy", (PyObject *)&__pyx_type_5pysam_8csamfile_PileupProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_PileupProxy = &__pyx_type_5pysam_8csamfile_PileupProxy;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_PileupRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3287; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_PileupRead.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "PileupRead", (PyObject *)&__pyx_type_5pysam_8csamfile_PileupRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3287; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_PileupRead = &__pyx_type_5pysam_8csamfile_PileupRead;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRow) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRow.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRow", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRow) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRow = &__pyx_type_5pysam_8csamfile_IteratorRow;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorRowRegion = &__pyx_vtable_5pysam_8csamfile_IteratorRowRegion;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowRegion.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *))__pyx_f_5pysam_8csamfile_17IteratorRowRegion_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowRegion.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *))__pyx_f_5pysam_8csamfile_17IteratorRowRegion_cnext;
-  __pyx_type_5pysam_8csamfile_IteratorRowRegion.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowRegion.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_14calignmentfile_AlignmentFile.tp_dict, __pyx_vtabptr_5pysam_14calignmentfile_AlignmentFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "AlignmentFile", (PyObject *)&__pyx_type_5pysam_14calignmentfile_AlignmentFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_AlignmentFile = &__pyx_type_5pysam_14calignmentfile_AlignmentFile;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_PileupColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_PileupColumn.tp_print = 0;
+  if (__Pyx_SetAttrString(__pyx_m, "PileupColumn", (PyObject *)&__pyx_type_5pysam_14calignmentfile_PileupColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_PileupColumn = &__pyx_type_5pysam_14calignmentfile_PileupColumn;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_PileupRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_PileupRead.tp_print = 0;
+  if (__Pyx_SetAttrString(__pyx_m, "PileupRead", (PyObject *)&__pyx_type_5pysam_14calignmentfile_PileupRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_PileupRead = &__pyx_type_5pysam_14calignmentfile_PileupRead;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IteratorRow) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1287; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IteratorRow.tp_print = 0;
+  if (__Pyx_SetAttrString(__pyx_m, "IteratorRow", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRow) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1287; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRow = &__pyx_type_5pysam_14calignmentfile_IteratorRow;
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowRegion = &__pyx_vtable_5pysam_14calignmentfile_IteratorRowRegion;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorRowRegion.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *))__pyx_f_5pysam_14calignmentfile_17IteratorRowRegion_getCurrent;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorRowRegion.cnext = (int (*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *))__pyx_f_5pysam_14calignmentfile_17IteratorRowRegion_cnext;
+  __pyx_type_5pysam_14calignmentfile_IteratorRowRegion.tp_base = __pyx_ptype_5pysam_14calignmentfile_IteratorRow;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IteratorRowRegion.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowRegion, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowRegion, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_17IteratorRowRegion_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_17IteratorRowRegion_4__next__.doc = __pyx_doc_5pysam_8csamfile_17IteratorRowRegion_4__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_17IteratorRowRegion_4__next__;
+      __pyx_wrapperbase_5pysam_14calignmentfile_17IteratorRowRegion_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_14calignmentfile_17IteratorRowRegion_4__next__.doc = __pyx_doc_5pysam_14calignmentfile_17IteratorRowRegion_4__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_14calignmentfile_17IteratorRowRegion_4__next__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorRowRegion.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowRegion", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowRegion = &__pyx_type_5pysam_8csamfile_IteratorRowRegion;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorRowHead = &__pyx_vtable_5pysam_8csamfile_IteratorRowHead;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowHead.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *))__pyx_f_5pysam_8csamfile_15IteratorRowHead_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowHead.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *))__pyx_f_5pysam_8csamfile_15IteratorRowHead_cnext;
-  __pyx_type_5pysam_8csamfile_IteratorRowHead.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowHead.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_14calignmentfile_IteratorRowRegion.tp_dict, __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowRegion", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion = &__pyx_type_5pysam_14calignmentfile_IteratorRowRegion;
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowHead = &__pyx_vtable_5pysam_14calignmentfile_IteratorRowHead;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorRowHead.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *))__pyx_f_5pysam_14calignmentfile_15IteratorRowHead_getCurrent;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorRowHead.cnext = (int (*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *))__pyx_f_5pysam_14calignmentfile_15IteratorRowHead_cnext;
+  __pyx_type_5pysam_14calignmentfile_IteratorRowHead.tp_base = __pyx_ptype_5pysam_14calignmentfile_IteratorRow;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IteratorRowHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1396; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IteratorRowHead.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowHead, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowHead, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1396; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_15IteratorRowHead_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_15IteratorRowHead_4__next__.doc = __pyx_doc_5pysam_8csamfile_15IteratorRowHead_4__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_15IteratorRowHead_4__next__;
+      __pyx_wrapperbase_5pysam_14calignmentfile_15IteratorRowHead_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_14calignmentfile_15IteratorRowHead_4__next__.doc = __pyx_doc_5pysam_14calignmentfile_15IteratorRowHead_4__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_14calignmentfile_15IteratorRowHead_4__next__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorRowHead.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorRowHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowHead", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowHead = &__pyx_type_5pysam_8csamfile_IteratorRowHead;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorRowAll = &__pyx_vtable_5pysam_8csamfile_IteratorRowAll;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowAll.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *))__pyx_f_5pysam_8csamfile_14IteratorRowAll_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowAll.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *))__pyx_f_5pysam_8csamfile_14IteratorRowAll_cnext;
-  __pyx_type_5pysam_8csamfile_IteratorRowAll.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowAll.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_14calignmentfile_IteratorRowHead.tp_dict, __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1396; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowHead", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1396; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowHead = &__pyx_type_5pysam_14calignmentfile_IteratorRowHead;
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowAll = &__pyx_vtable_5pysam_14calignmentfile_IteratorRowAll;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorRowAll.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *))__pyx_f_5pysam_14calignmentfile_14IteratorRowAll_getCurrent;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorRowAll.cnext = (int (*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *))__pyx_f_5pysam_14calignmentfile_14IteratorRowAll_cnext;
+  __pyx_type_5pysam_14calignmentfile_IteratorRowAll.tp_base = __pyx_ptype_5pysam_14calignmentfile_IteratorRow;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IteratorRowAll.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowAll, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowAll, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_14IteratorRowAll_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_14IteratorRowAll_4__next__.doc = __pyx_doc_5pysam_8csamfile_14IteratorRowAll_4__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_14IteratorRowAll_4__next__;
+      __pyx_wrapperbase_5pysam_14calignmentfile_14IteratorRowAll_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_14calignmentfile_14IteratorRowAll_4__next__.doc = __pyx_doc_5pysam_14calignmentfile_14IteratorRowAll_4__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_14calignmentfile_14IteratorRowAll_4__next__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorRowAll.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowAll", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowAll = &__pyx_type_5pysam_8csamfile_IteratorRowAll;
-  __pyx_type_5pysam_8csamfile_IteratorRowAllRefs.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowAllRefs.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_14calignmentfile_IteratorRowAll.tp_dict, __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowAll", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowAll = &__pyx_type_5pysam_14calignmentfile_IteratorRowAll;
+  __pyx_type_5pysam_14calignmentfile_IteratorRowAllRefs.tp_base = __pyx_ptype_5pysam_14calignmentfile_IteratorRow;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IteratorRowAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IteratorRowAllRefs.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowAllRefs, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowAllRefs, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_18IteratorRowAllRefs_6__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_18IteratorRowAllRefs_6__next__.doc = __pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_6__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_18IteratorRowAllRefs_6__next__;
+      __pyx_wrapperbase_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__.doc = __pyx_doc_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_14calignmentfile_18IteratorRowAllRefs_6__next__;
     }
   }
   #endif
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowAllRefs", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowAllRefs = &__pyx_type_5pysam_8csamfile_IteratorRowAllRefs;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorRowSelection = &__pyx_vtable_5pysam_8csamfile_IteratorRowSelection;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowSelection.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *))__pyx_f_5pysam_8csamfile_20IteratorRowSelection_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowSelection.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *))__pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext;
-  __pyx_type_5pysam_8csamfile_IteratorRowSelection.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowSelection) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowSelection.tp_print = 0;
+  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowAllRefs", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowAllRefs = &__pyx_type_5pysam_14calignmentfile_IteratorRowAllRefs;
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowSelection = &__pyx_vtable_5pysam_14calignmentfile_IteratorRowSelection;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorRowSelection.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *))__pyx_f_5pysam_14calignmentfile_20IteratorRowSelection_getCurrent;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorRowSelection.cnext = (int (*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *))__pyx_f_5pysam_14calignmentfile_20IteratorRowSelection_cnext;
+  __pyx_type_5pysam_14calignmentfile_IteratorRowSelection.tp_base = __pyx_ptype_5pysam_14calignmentfile_IteratorRow;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IteratorRowSelection) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IteratorRowSelection.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowSelection, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowSelection, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_20IteratorRowSelection_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_20IteratorRowSelection_4__next__.doc = __pyx_doc_5pysam_8csamfile_20IteratorRowSelection_4__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_20IteratorRowSelection_4__next__;
+      __pyx_wrapperbase_5pysam_14calignmentfile_20IteratorRowSelection_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_14calignmentfile_20IteratorRowSelection_4__next__.doc = __pyx_doc_5pysam_14calignmentfile_20IteratorRowSelection_4__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_14calignmentfile_20IteratorRowSelection_4__next__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorRowSelection.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorRowSelection) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowSelection", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowSelection) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowSelection = &__pyx_type_5pysam_8csamfile_IteratorRowSelection;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorColumn = &__pyx_vtable_5pysam_8csamfile_IteratorColumn;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *))__pyx_f_5pysam_8csamfile_14IteratorColumn_cnext;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.getSequence = (char *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *))__pyx_f_5pysam_8csamfile_14IteratorColumn_getSequence;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.setMask = (PyObject *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, PyObject *))__pyx_f_5pysam_8csamfile_14IteratorColumn_setMask;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.setupIteratorData = (PyObject *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, int, int, int, struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData *__pyx_optional_args))__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.reset = (PyObject *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, PyObject *, PyObject *, PyObject *))__pyx_f_5pysam_8csamfile_14IteratorColumn_reset;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorColumn.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorColumn.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorColumn", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorColumn = &__pyx_type_5pysam_8csamfile_IteratorColumn;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorColumnRegion = &__pyx_vtable_5pysam_8csamfile_IteratorColumnRegion;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumnRegion.__pyx_base = *__pyx_vtabptr_5pysam_8csamfile_IteratorColumn;
-  __pyx_type_5pysam_8csamfile_IteratorColumnRegion.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorColumn;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorColumnRegion.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_14calignmentfile_IteratorRowSelection.tp_dict, __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowSelection) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowSelection", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorRowSelection) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowSelection = &__pyx_type_5pysam_14calignmentfile_IteratorRowSelection;
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumn = &__pyx_vtable_5pysam_14calignmentfile_IteratorColumn;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorColumn.cnext = (int (*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *))__pyx_f_5pysam_14calignmentfile_14IteratorColumn_cnext;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorColumn.getSequence = (char *(*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *))__pyx_f_5pysam_14calignmentfile_14IteratorColumn_getSequence;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorColumn.setMask = (PyObject *(*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *, PyObject *))__pyx_f_5pysam_14calignmentfile_14IteratorColumn_setMask;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorColumn.setupIteratorData = (PyObject *(*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *, int, int, int, struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData *__pyx_optional_args))__pyx_f_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorColumn.reset = (PyObject *(*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *, PyObject *, PyObject *, PyObject *))__pyx_f_5pysam_14calignmentfile_14IteratorColumn_reset;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorColumn._free_pileup_iter = (PyObject *(*)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *))__pyx_f_5pysam_14calignmentfile_14IteratorColumn__free_pileup_iter;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IteratorColumn.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_14calignmentfile_IteratorColumn.tp_dict, __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "IteratorColumn", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorColumn = &__pyx_type_5pysam_14calignmentfile_IteratorColumn;
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnRegion = &__pyx_vtable_5pysam_14calignmentfile_IteratorColumnRegion;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorColumnRegion.__pyx_base = *__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumn;
+  __pyx_type_5pysam_14calignmentfile_IteratorColumnRegion.tp_base = __pyx_ptype_5pysam_14calignmentfile_IteratorColumn;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IteratorColumnRegion.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumnRegion, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorColumnRegion, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_20IteratorColumnRegion_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_20IteratorColumnRegion_2__next__.doc = __pyx_doc_5pysam_8csamfile_20IteratorColumnRegion_2__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_20IteratorColumnRegion_2__next__;
+      __pyx_wrapperbase_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__.doc = __pyx_doc_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_14calignmentfile_20IteratorColumnRegion_2__next__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorColumnRegion.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorColumnRegion", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorColumnRegion = &__pyx_type_5pysam_8csamfile_IteratorColumnRegion;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorColumnAllRefs = &__pyx_vtable_5pysam_8csamfile_IteratorColumnAllRefs;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumnAllRefs.__pyx_base = *__pyx_vtabptr_5pysam_8csamfile_IteratorColumn;
-  __pyx_type_5pysam_8csamfile_IteratorColumnAllRefs.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorColumn;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorColumnAllRefs.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_14calignmentfile_IteratorColumnRegion.tp_dict, __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "IteratorColumnRegion", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorColumnRegion = &__pyx_type_5pysam_14calignmentfile_IteratorColumnRegion;
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnAllRefs = &__pyx_vtable_5pysam_14calignmentfile_IteratorColumnAllRefs;
+  __pyx_vtable_5pysam_14calignmentfile_IteratorColumnAllRefs.__pyx_base = *__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumn;
+  __pyx_type_5pysam_14calignmentfile_IteratorColumnAllRefs.tp_base = __pyx_ptype_5pysam_14calignmentfile_IteratorColumn;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IteratorColumnAllRefs.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorColumnAllRefs, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__.doc = __pyx_doc_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__;
+      __pyx_wrapperbase_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__.doc = __pyx_doc_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_14calignmentfile_21IteratorColumnAllRefs_2__next__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorColumnAllRefs", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorColumnAllRefs = &__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IndexedReads) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IndexedReads.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "IndexedReads", (PyObject *)&__pyx_type_5pysam_8csamfile_IndexedReads) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IndexedReads = &__pyx_type_5pysam_8csamfile_IndexedReads;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_SNPCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_SNPCall.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "SNPCall", (PyObject *)&__pyx_type_5pysam_8csamfile_SNPCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_SNPCall = &__pyx_type_5pysam_8csamfile_SNPCall;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr.tp_print = 0;
-  __pyx_ptype_5pysam_8csamfile___pyx_scope_struct__genexpr = &__pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr.tp_print = 0;
-  __pyx_ptype_5pysam_8csamfile___pyx_scope_struct_1_genexpr = &__pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_14calignmentfile_IteratorColumnAllRefs.tp_dict, __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "IteratorColumnAllRefs", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorColumnAllRefs = &__pyx_type_5pysam_14calignmentfile_IteratorColumnAllRefs;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_IndexedReads) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_IndexedReads.tp_print = 0;
+  if (__Pyx_SetAttrString(__pyx_m, "IndexedReads", (PyObject *)&__pyx_type_5pysam_14calignmentfile_IndexedReads) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IndexedReads = &__pyx_type_5pysam_14calignmentfile_IndexedReads;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile_SNPCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile_SNPCall.tp_print = 0;
+  if (__Pyx_SetAttrString(__pyx_m, "SNPCall", (PyObject *)&__pyx_type_5pysam_14calignmentfile_SNPCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_SNPCall = &__pyx_type_5pysam_14calignmentfile_SNPCall;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile___pyx_scope_struct__genexpr) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile___pyx_scope_struct__genexpr.tp_print = 0;
+  __pyx_ptype_5pysam_14calignmentfile___pyx_scope_struct__genexpr = &__pyx_type_5pysam_14calignmentfile___pyx_scope_struct__genexpr;
+  if (PyType_Ready(&__pyx_type_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr.tp_print = 0;
+  __pyx_ptype_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr = &__pyx_type_5pysam_14calignmentfile___pyx_scope_struct_1_genexpr;
   /*--- Type import code ---*/
-  __pyx_ptype_5pysam_6cfaidx_Fastafile = __Pyx_ImportType("pysam.cfaidx", "Fastafile", sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastafile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_Fastafile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6cfaidx_Fastafile = (struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_Fastafile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_Fastafile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6cfaidx_FastqProxy = __Pyx_ImportType("pysam.cfaidx", "FastqProxy", sizeof(struct __pyx_obj_5pysam_6cfaidx_FastqProxy), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_FastqProxy)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6cfaidx_Fastqfile = __Pyx_ImportType("pysam.cfaidx", "Fastqfile", sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastqfile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_Fastqfile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6cfaidx_Fastqfile = (struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_Fastqfile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_Fastqfile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_FastaFile = __Pyx_ImportType("pysam.cfaidx", "FastaFile", sizeof(struct __pyx_obj_5pysam_6cfaidx_FastaFile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_FastaFile)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6cfaidx_FastaFile = (struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_FastaFile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_FastaFile)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_FastqProxy = __Pyx_ImportType("pysam.cfaidx", "FastqProxy", sizeof(struct __pyx_obj_5pysam_6cfaidx_FastqProxy), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_FastqProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_FastqFile = __Pyx_ImportType("pysam.cfaidx", "FastqFile", sizeof(struct __pyx_obj_5pysam_6cfaidx_FastqFile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_FastqFile)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6cfaidx_FastqFile = (struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_FastqFile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_FastqFile)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_Fastafile = __Pyx_ImportType("pysam.cfaidx", "Fastafile", sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastafile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_Fastafile)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 35; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6cfaidx_Fastafile = (struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_Fastafile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_Fastafile)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 35; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_Fastqfile = __Pyx_ImportType("pysam.cfaidx", "Fastqfile", sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastqfile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_Fastqfile)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6cfaidx_Fastqfile = (struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_Fastqfile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_Fastqfile)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "type", 
   #if CYTHON_COMPILING_IN_PYPY
   sizeof(PyTypeObject),
   #else
   sizeof(PyHeapTypeObject),
   #endif
-  0); if (unlikely(!__pyx_ptype_7cpython_4type_type)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_7cpython_4bool_bool = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "bool", sizeof(PyBoolObject), 0); if (unlikely(!__pyx_ptype_7cpython_4bool_bool)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_7cpython_7complex_complex = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "complex", sizeof(PyComplexObject), 0); if (unlikely(!__pyx_ptype_7cpython_7complex_complex)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  0); if (unlikely(!__pyx_ptype_7cpython_4type_type)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_7cpython_4bool_bool = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "bool", sizeof(PyBoolObject), 0); if (unlikely(!__pyx_ptype_7cpython_4bool_bool)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_7cpython_7complex_complex = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "complex", sizeof(PyComplexObject), 0); if (unlikely(!__pyx_ptype_7cpython_7complex_complex)) {__pyx_filename = __pyx_f[5]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_7cpython_5array_array = __Pyx_ImportType("array", "array", sizeof(arrayobject), 0); if (unlikely(!__pyx_ptype_7cpython_5array_array)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   /*--- Variable import code ---*/
   /*--- Function import code ---*/
   /*--- Execution code ---*/
 
-  /* "pysam/csamfile.pyx":4
+  /* "pysam/calignmentfile.pyx":4
  * # cython: profile=True
  * # adds doc-strings for sphinx
  * import tempfile             # <<<<<<<<<<<<<<
@@ -39894,7 +44129,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_tempfile, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":5
+  /* "pysam/calignmentfile.pyx":5
  * # adds doc-strings for sphinx
  * import tempfile
  * import os             # <<<<<<<<<<<<<<
@@ -39906,7 +44141,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_os, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":6
+  /* "pysam/calignmentfile.pyx":6
  * import tempfile
  * import os
  * import sys             # <<<<<<<<<<<<<<
@@ -39918,7 +44153,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_sys, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":7
+  /* "pysam/calignmentfile.pyx":7
  * import os
  * import sys
  * import types             # <<<<<<<<<<<<<<
@@ -39930,7 +44165,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_types, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":8
+  /* "pysam/calignmentfile.pyx":8
  * import sys
  * import types
  * import itertools             # <<<<<<<<<<<<<<
@@ -39942,7 +44177,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_itertools, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":9
+  /* "pysam/calignmentfile.pyx":9
  * import types
  * import itertools
  * import struct             # <<<<<<<<<<<<<<
@@ -39954,7 +44189,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_struct, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":10
+  /* "pysam/calignmentfile.pyx":10
  * import itertools
  * import struct
  * import ctypes             # <<<<<<<<<<<<<<
@@ -39966,7 +44201,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_ctypes, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":11
+  /* "pysam/calignmentfile.pyx":11
  * import struct
  * import ctypes
  * import collections             # <<<<<<<<<<<<<<
@@ -39978,7 +44213,7 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_collections, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":12
+  /* "pysam/calignmentfile.pyx":12
  * import ctypes
  * import collections
  * import re             # <<<<<<<<<<<<<<
@@ -39990,110 +44225,122 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_re, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":13
+  /* "pysam/calignmentfile.pyx":13
  * import collections
  * import re
  * import platform             # <<<<<<<<<<<<<<
  * import warnings
- * from cpython cimport PyErr_SetString, \
+ * import array
  */
   __pyx_t_1 = __Pyx_Import(__pyx_n_s_platform, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_platform, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":14
+  /* "pysam/calignmentfile.pyx":14
  * import re
  * import platform
  * import warnings             # <<<<<<<<<<<<<<
- * from cpython cimport PyErr_SetString, \
- *     PyBytes_Check, \
+ * import array
+ * 
  */
   __pyx_t_1 = __Pyx_Import(__pyx_n_s_warnings, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_warnings, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":27
+  /* "pysam/calignmentfile.pyx":15
+ * import platform
+ * import warnings
+ * import array             # <<<<<<<<<<<<<<
+ * 
+ * from cpython cimport PyErr_SetString, \
+ */
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_array, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_array, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":31
  * ## Python 3 compatibility functions
  * ########################################################################
  * IS_PYTHON3 = PY_MAJOR_VERSION >= 3             # <<<<<<<<<<<<<<
  * cdef from_string_and_size(char* s, size_t length):
  *     if PY_MAJOR_VERSION < 3:
  */
-  __pyx_t_1 = __Pyx_PyBool_FromLong((PY_MAJOR_VERSION >= 3)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 27; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBool_FromLong((PY_MAJOR_VERSION >= 3)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_IS_PYTHON3, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 27; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_IS_PYTHON3, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":36
+  /* "pysam/calignmentfile.pyx":40
  * # filename encoding (copied from lxml.etree.pyx)
  * cdef str _FILENAME_ENCODING
  * _FILENAME_ENCODING = sys.getfilesystemencoding()             # <<<<<<<<<<<<<<
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getfilesystemencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getfilesystemencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_XGOTREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-  __Pyx_DECREF_SET(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
+  if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_XGOTREF(__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING);
+  __Pyx_DECREF_SET(__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
   __Pyx_GIVEREF(__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":37
+  /* "pysam/calignmentfile.pyx":41
  * cdef str _FILENAME_ENCODING
  * _FILENAME_ENCODING = sys.getfilesystemencoding()
  * if _FILENAME_ENCODING is None:             # <<<<<<<<<<<<<<
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  * if _FILENAME_ENCODING is None:
  */
-  __pyx_t_3 = (__pyx_v_5pysam_8csamfile__FILENAME_ENCODING == ((PyObject*)Py_None));
+  __pyx_t_3 = (__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING == ((PyObject*)Py_None));
   __pyx_t_4 = (__pyx_t_3 != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/csamfile.pyx":38
+    /* "pysam/calignmentfile.pyx":42
  * _FILENAME_ENCODING = sys.getfilesystemencoding()
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = sys.getdefaultencoding()             # <<<<<<<<<<<<<<
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = 'ascii'
  */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getdefaultencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getdefaultencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_XGOTREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    __Pyx_DECREF_SET(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
+    if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_XGOTREF(__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING);
+    __Pyx_DECREF_SET(__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
     goto __pyx_L2;
   }
   __pyx_L2:;
 
-  /* "pysam/csamfile.pyx":39
+  /* "pysam/calignmentfile.pyx":43
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  * if _FILENAME_ENCODING is None:             # <<<<<<<<<<<<<<
  *     _FILENAME_ENCODING = 'ascii'
  * 
  */
-  __pyx_t_4 = (__pyx_v_5pysam_8csamfile__FILENAME_ENCODING == ((PyObject*)Py_None));
+  __pyx_t_4 = (__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING == ((PyObject*)Py_None));
   __pyx_t_3 = (__pyx_t_4 != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":40
+    /* "pysam/calignmentfile.pyx":44
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = 'ascii'             # <<<<<<<<<<<<<<
@@ -40101,414 +44348,397 @@ PyMODINIT_FUNC PyInit_csamfile(void)
  * #cdef char* _C_FILENAME_ENCODING
  */
     __Pyx_INCREF(__pyx_n_s_ascii);
-    __Pyx_XGOTREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    __Pyx_DECREF_SET(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING, __pyx_n_s_ascii);
+    __Pyx_XGOTREF(__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING);
+    __Pyx_DECREF_SET(__pyx_v_5pysam_14calignmentfile__FILENAME_ENCODING, __pyx_n_s_ascii);
     __Pyx_GIVEREF(__pyx_n_s_ascii);
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/csamfile.pyx":103
+  /* "pysam/calignmentfile.pyx":108
  * DEF SEEK_END = 2
  * 
  * cdef char* CODE2CIGAR= "MIDNSHP=X"             # <<<<<<<<<<<<<<
  * if IS_PYTHON3:
  *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
  */
-  __pyx_v_5pysam_8csamfile_CODE2CIGAR = __pyx_k_MIDNSHP_X;
+  __pyx_v_5pysam_14calignmentfile_CODE2CIGAR = __pyx_k_MIDNSHP_X;
 
-  /* "pysam/csamfile.pyx":104
+  /* "pysam/calignmentfile.pyx":109
  * 
  * cdef char* CODE2CIGAR= "MIDNSHP=X"
  * if IS_PYTHON3:             # <<<<<<<<<<<<<<
  *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
  * else:
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_IS_PYTHON3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_IS_PYTHON3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_3) {
 
-    /* "pysam/csamfile.pyx":105
+    /* "pysam/calignmentfile.pyx":110
  * cdef char* CODE2CIGAR= "MIDNSHP=X"
  * if IS_PYTHON3:
  *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
  * else:
  *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
  */
-    __pyx_t_1 = __pyx_pf_5pysam_8csamfile_genexpr(NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __pyx_pf_5pysam_14calignmentfile_4genexpr(NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyDict_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyDict_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (PyDict_SetItem(__pyx_d, __pyx_n_s_CIGAR2CODE, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_d, __pyx_n_s_CIGAR2CODE, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     goto __pyx_L4;
   }
   /*else*/ {
 
-    /* "pysam/csamfile.pyx":107
+    /* "pysam/calignmentfile.pyx":112
  *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
  * else:
  *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
  * CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )
  * 
  */
-    __pyx_t_1 = __pyx_pf_5pysam_8csamfile_3genexpr(NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __pyx_pf_5pysam_14calignmentfile_7genexpr(NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyDict_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyDict_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (PyDict_SetItem(__pyx_d, __pyx_n_s_CIGAR2CODE, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_d, __pyx_n_s_CIGAR2CODE, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   }
   __pyx_L4:;
 
-  /* "pysam/csamfile.pyx":108
+  /* "pysam/calignmentfile.pyx":113
  * else:
  *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
  * CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )             # <<<<<<<<<<<<<<
  * 
  * #####################################################################
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_compile); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_compile); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__84, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__80, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_CIGAR_REGEX, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_CIGAR_REGEX, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":112
+  /* "pysam/calignmentfile.pyx":117
  * #####################################################################
  * # hard-coded constants
  * cdef int max_pos = 2 << 29             # <<<<<<<<<<<<<<
  * 
  * #####################################################################
  */
-  __pyx_v_5pysam_8csamfile_max_pos = 1073741824;
+  __pyx_v_5pysam_14calignmentfile_max_pos = 1073741824;
 
-  /* "pysam/csamfile.pyx":197
- * 
+  /* "pysam/calignmentfile.pyx":204
  * 
- * class PileupColumn(object):             # <<<<<<<<<<<<<<
- *     '''A pileup column. A pileup column contains
- *     all the reads that map to a certain target base.
- */
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(__pyx_builtin_object);
-  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_builtin_object);
-  __Pyx_GIVEREF(__pyx_builtin_object);
-  __pyx_t_2 = __Pyx_CalculateMetaclass(NULL, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_5 = __Pyx_Py3MetaclassPrepare(__pyx_t_2, __pyx_t_1, __pyx_n_s_PileupColumn, __pyx_n_s_PileupColumn, (PyObject *) NULL, __pyx_n_s_pysam_csamfile, __pyx_kp_s_A_pileup_column_A_pileup_column); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-
-  /* "pysam/csamfile.pyx":210
- *         list of reads (:class:`pysam.PileupRead`) aligned to this column
- *     '''
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )
- */
-  __pyx_t_6 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_8csamfile_12PileupColumn_1__str__, 0, __pyx_n_s_PileupColumn___str, NULL, __pyx_n_s_pysam_csamfile, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__86)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  if (PyObject_SetItem(__pyx_t_5, __pyx_n_s_str, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-  /* "pysam/csamfile.pyx":197
- * 
- * 
- * class PileupColumn(object):             # <<<<<<<<<<<<<<
- *     '''A pileup column. A pileup column contains
- *     all the reads that map to a certain target base.
- */
-  __pyx_t_6 = __Pyx_Py3ClassCreate(__pyx_t_2, __pyx_n_s_PileupColumn, __pyx_t_1, __pyx_t_5, NULL, 0, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_PileupColumn, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":216
- * 
- * # valid types for sam headers
+ * # valid types for SAM headers
  * VALID_HEADER_TYPES = {"HD" : dict,             # <<<<<<<<<<<<<<
  *                       "SQ" : list,
  *                       "RG" : list,
  */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_HD, ((PyObject *)((PyObject*)(&PyDict_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_HD, ((PyObject *)((PyObject*)(&PyDict_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":217
- * # valid types for sam headers
+  /* "pysam/calignmentfile.pyx":205
+ * # valid types for SAM headers
  * VALID_HEADER_TYPES = {"HD" : dict,
  *                       "SQ" : list,             # <<<<<<<<<<<<<<
  *                       "RG" : list,
  *                       "PG" : list,
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_SQ, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_SQ, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":218
+  /* "pysam/calignmentfile.pyx":206
  * VALID_HEADER_TYPES = {"HD" : dict,
  *                       "SQ" : list,
  *                       "RG" : list,             # <<<<<<<<<<<<<<
  *                       "PG" : list,
  *                       "CO" : list}
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_RG, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_RG, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":219
+  /* "pysam/calignmentfile.pyx":207
  *                       "SQ" : list,
  *                       "RG" : list,
  *                       "PG" : list,             # <<<<<<<<<<<<<<
  *                       "CO" : list}
  * 
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_PG, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_PG, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":220
+  /* "pysam/calignmentfile.pyx":208
  *                       "RG" : list,
  *                       "PG" : list,
  *                       "CO" : list}             # <<<<<<<<<<<<<<
  * 
- * # order of records within sam headers
+ * # order of records within SAM headers
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_CO, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADER_TYPES, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_CO, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADER_TYPES, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":223
+  /* "pysam/calignmentfile.pyx":211
  * 
- * # order of records within sam headers
+ * # order of records within SAM headers
  * VALID_HEADERS = ("HD", "SQ", "RG", "PG", "CO")             # <<<<<<<<<<<<<<
  * 
- * # type conversions within sam header records
+ * # default type conversions within SAM header records
  */
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADERS, __pyx_tuple__87) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADERS, __pyx_tuple__81) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":226
+  /* "pysam/calignmentfile.pyx":214
  * 
- * # type conversions within sam header records
+ * # default type conversions within SAM header records
  * VALID_HEADER_FIELDS = {"HD" : {"VN" : str, "SO" : str, "GO" : str},             # <<<<<<<<<<<<<<
  *                        "SQ" : {"SN" : str, "LN" : int, "AS" : str,
  *                                "M5" : str, "SP" : str, "UR" : str,},
  */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_VN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SO, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_GO, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_HD, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_VN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SO, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_GO, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_HD, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":227
- * # type conversions within sam header records
+  /* "pysam/calignmentfile.pyx":215
+ * # default type conversions within SAM header records
  * VALID_HEADER_FIELDS = {"HD" : {"VN" : str, "SO" : str, "GO" : str},
  *                        "SQ" : {"SN" : str, "LN" : int, "AS" : str,             # <<<<<<<<<<<<<<
  *                                "M5" : str, "SP" : str, "UR" : str,},
  *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,
  */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_LN, ((PyObject *)((PyObject*)(&PyInt_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_AS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_LN, ((PyObject *)((PyObject*)(&PyInt_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_AS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":228
+  /* "pysam/calignmentfile.pyx":216
  * VALID_HEADER_FIELDS = {"HD" : {"VN" : str, "SO" : str, "GO" : str},
  *                        "SQ" : {"SN" : str, "LN" : int, "AS" : str,
  *                                "M5" : str, "SP" : str, "UR" : str,},             # <<<<<<<<<<<<<<
  *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,
  *                                "DT" : str, "FO" : str, "KS" : str,
  */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_M5, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SP, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_UR, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_SQ, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_M5, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SP, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_UR, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_SQ, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":229
+  /* "pysam/calignmentfile.pyx":217
  *                        "SQ" : {"SN" : str, "LN" : int, "AS" : str,
  *                                "M5" : str, "SP" : str, "UR" : str,},
  *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,             # <<<<<<<<<<<<<<
  *                                "DT" : str, "FO" : str, "KS" : str,
  *                                "LB" : str, "PG" : str, "PI" : str,
  */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_ID, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_CN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_DS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_ID, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_CN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_DS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":230
+  /* "pysam/calignmentfile.pyx":218
  *                                "M5" : str, "SP" : str, "UR" : str,},
  *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,
  *                                "DT" : str, "FO" : str, "KS" : str,             # <<<<<<<<<<<<<<
  *                                "LB" : str, "PG" : str, "PI" : str,
  *                                "PL" : str, "PU" : str, "SM" : str,},
  */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_DT, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_FO, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_KS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_DT, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_FO, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_KS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":231
+  /* "pysam/calignmentfile.pyx":219
  *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,
  *                                "DT" : str, "FO" : str, "KS" : str,
  *                                "LB" : str, "PG" : str, "PI" : str,             # <<<<<<<<<<<<<<
  *                                "PL" : str, "PU" : str, "SM" : str,},
  *                        "PG" : {"ID" : str, "PN" : str, "CL" : str,
  */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_LB, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PG, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PI, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_LB, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PG, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PI, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":232
+  /* "pysam/calignmentfile.pyx":220
  *                                "DT" : str, "FO" : str, "KS" : str,
  *                                "LB" : str, "PG" : str, "PI" : str,
  *                                "PL" : str, "PU" : str, "SM" : str,},             # <<<<<<<<<<<<<<
  *                        "PG" : {"ID" : str, "PN" : str, "CL" : str,
  *                                "PP" : str, "DS" : str, "VN" : str,},}
  */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PL, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PU, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SM, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_RG, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PL, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PU, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SM, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_RG, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/csamfile.pyx":233
+  /* "pysam/calignmentfile.pyx":221
  *                                "LB" : str, "PG" : str, "PI" : str,
  *                                "PL" : str, "PU" : str, "SM" : str,},
  *                        "PG" : {"ID" : str, "PN" : str, "CL" : str,             # <<<<<<<<<<<<<<
  *                                "PP" : str, "DS" : str, "VN" : str,},}
  * 
  */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_ID, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_CL, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_ID, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_CL, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":234
+  /* "pysam/calignmentfile.pyx":222
  *                                "PL" : str, "PU" : str, "SM" : str,},
  *                        "PG" : {"ID" : str, "PN" : str, "CL" : str,
  *                                "PP" : str, "DS" : str, "VN" : str,},}             # <<<<<<<<<<<<<<
  * 
  * # output order of fields within records
  */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PP, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_DS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_VN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_PG, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PP, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_DS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_VN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_PG, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADER_FIELDS, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADER_FIELDS, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":237
+  /* "pysam/calignmentfile.pyx":225
  * 
  * # output order of fields within records
  * VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),             # <<<<<<<<<<<<<<
  *                       "SQ" : ("SN", "LN", "AS", "M5",
  *                                "UR", "SP"),
  */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_HD, __pyx_tuple__88) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_HD, __pyx_tuple__82) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":238
+  /* "pysam/calignmentfile.pyx":226
  * # output order of fields within records
  * VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),
  *                       "SQ" : ("SN", "LN", "AS", "M5",             # <<<<<<<<<<<<<<
  *                                "UR", "SP"),
  *                       "RG" : ("ID", "SM", "LB", "DS",
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_SQ, __pyx_tuple__89) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_SQ, __pyx_tuple__83) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":240
+  /* "pysam/calignmentfile.pyx":228
  *                       "SQ" : ("SN", "LN", "AS", "M5",
  *                                "UR", "SP"),
  *                       "RG" : ("ID", "SM", "LB", "DS",             # <<<<<<<<<<<<<<
  *                               "PU", "PI", "CN", "DT",
  *                               "PL", "FO", "KS", "PG"),
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_RG, __pyx_tuple__90) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_RG, __pyx_tuple__84) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/csamfile.pyx":243
+  /* "pysam/calignmentfile.pyx":231
  *                               "PU", "PI", "CN", "DT",
  *                               "PL", "FO", "KS", "PG"),
  *                       "PG" : ("PN", "ID", "VN", "CL",             # <<<<<<<<<<<<<<
  *                               "PP"),}
  * 
  */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_PG, __pyx_tuple__91) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADER_ORDER, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_PG, __pyx_tuple__85) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADER_ORDER, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1788
+  /* "pysam/calignmentfile.pyx":1891
  *                   int tid = 0,
  *                   int start = 0,
  *                   int end = max_pos,             # <<<<<<<<<<<<<<
  *                   int truncate = False,
  *                   **kwargs ):
  */
-  __pyx_k__57 = __pyx_v_5pysam_8csamfile_max_pos;
+  __pyx_k__62 = __pyx_v_5pysam_14calignmentfile_max_pos;
 
-  /* "pysam/csamfile.pyx":3450
- *             hts_close(self.htsfile)
+  /* "pysam/calignmentfile.pyx":2052
+ * 
+ * 
+ * def toQualityString(qualities):             # <<<<<<<<<<<<<<
+ *     '''convert a list of quality score to the string
+ *     representation used in the SAM format.'''
+ */
+  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_14calignmentfile_1toQualityString, NULL, __pyx_n_s_pysam_calignmentfile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_toQualityString, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":2060
+ * 
+ * 
+ * def fromQualityString(quality_string):             # <<<<<<<<<<<<<<
+ *     '''return a list of quality scores from the
+ *     stringn representation of quality scores used
+ */
+  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_14calignmentfile_3fromQualityString, NULL, __pyx_n_s_pysam_calignmentfile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2060; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_fromQualityString, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2060; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+
+  /* "pysam/calignmentfile.pyx":3666
+ *             bam_hdr_destroy(self.header)
  * 
- * __all__ = ["Samfile",             # <<<<<<<<<<<<<<
+ * __all__ = ["AlignmentFile",             # <<<<<<<<<<<<<<
  *            "IteratorRow",
  *            "IteratorColumn",
  */
-  __pyx_t_1 = PyList_New(8); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(__pyx_n_s_Samfile);
-  PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_Samfile);
-  __Pyx_GIVEREF(__pyx_n_s_Samfile);
+  __Pyx_INCREF(__pyx_n_s_AlignmentFile);
+  PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_AlignmentFile);
+  __Pyx_GIVEREF(__pyx_n_s_AlignmentFile);
   __Pyx_INCREF(__pyx_n_s_IteratorRow);
   PyList_SET_ITEM(__pyx_t_1, 1, __pyx_n_s_IteratorRow);
   __Pyx_GIVEREF(__pyx_n_s_IteratorRow);
   __Pyx_INCREF(__pyx_n_s_IteratorColumn);
   PyList_SET_ITEM(__pyx_t_1, 2, __pyx_n_s_IteratorColumn);
   __Pyx_GIVEREF(__pyx_n_s_IteratorColumn);
-  __Pyx_INCREF(__pyx_n_s_AlignedRead);
-  PyList_SET_ITEM(__pyx_t_1, 3, __pyx_n_s_AlignedRead);
-  __Pyx_GIVEREF(__pyx_n_s_AlignedRead);
+  __Pyx_INCREF(__pyx_n_s_AlignedSegment);
+  PyList_SET_ITEM(__pyx_t_1, 3, __pyx_n_s_AlignedSegment);
+  __Pyx_GIVEREF(__pyx_n_s_AlignedSegment);
   __Pyx_INCREF(__pyx_n_s_PileupColumn);
   PyList_SET_ITEM(__pyx_t_1, 4, __pyx_n_s_PileupColumn);
   __Pyx_GIVEREF(__pyx_n_s_PileupColumn);
-  __Pyx_INCREF(__pyx_n_s_PileupProxy);
-  PyList_SET_ITEM(__pyx_t_1, 5, __pyx_n_s_PileupProxy);
-  __Pyx_GIVEREF(__pyx_n_s_PileupProxy);
   __Pyx_INCREF(__pyx_n_s_PileupRead);
-  PyList_SET_ITEM(__pyx_t_1, 6, __pyx_n_s_PileupRead);
+  PyList_SET_ITEM(__pyx_t_1, 5, __pyx_n_s_PileupRead);
   __Pyx_GIVEREF(__pyx_n_s_PileupRead);
   __Pyx_INCREF(__pyx_n_s_IndexedReads);
-  PyList_SET_ITEM(__pyx_t_1, 7, __pyx_n_s_IndexedReads);
+  PyList_SET_ITEM(__pyx_t_1, 6, __pyx_n_s_IndexedReads);
   __Pyx_GIVEREF(__pyx_n_s_IndexedReads);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_INCREF(__pyx_n_s_toQualityString);
+  PyList_SET_ITEM(__pyx_t_1, 7, __pyx_n_s_toQualityString);
+  __Pyx_GIVEREF(__pyx_n_s_toQualityString);
+  __Pyx_INCREF(__pyx_n_s_fromQualityString);
+  PyList_SET_ITEM(__pyx_t_1, 8, __pyx_n_s_fromQualityString);
+  __Pyx_GIVEREF(__pyx_n_s_fromQualityString);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":1
+  /* "pysam/calignmentfile.pyx":1
  * # cython: embedsignature=True             # <<<<<<<<<<<<<<
  * # cython: profile=True
  * # adds doc-strings for sphinx
@@ -40517,17 +44747,23 @@ PyMODINIT_FUNC PyInit_csamfile(void)
   __Pyx_GOTREF(__pyx_t_1);
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+
+  /* "array.pxd":159
+ *     return extend_buffer(self, other.data.as_chars, Py_SIZE(other))
+ * 
+ * cdef inline void zero(array self):             # <<<<<<<<<<<<<<
+ *     """ set all elements of array to zero. """
+ *     memset(self.data.as_chars, 0, Py_SIZE(self) * self.ob_descr.itemsize)
+ */
   goto __pyx_L0;
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
   if (__pyx_m) {
-    __Pyx_AddTraceback("init pysam.csamfile", __pyx_clineno, __pyx_lineno, __pyx_filename);
+    __Pyx_AddTraceback("init pysam.calignmentfile", __pyx_clineno, __pyx_lineno, __pyx_filename);
     Py_DECREF(__pyx_m); __pyx_m = 0;
   } else if (!PyErr_Occurred()) {
-    PyErr_SetString(PyExc_ImportError, "init pysam.csamfile");
+    PyErr_SetString(PyExc_ImportError, "init pysam.calignmentfile");
   }
   __pyx_L0:;
   __Pyx_RefNannyFinishContext();
@@ -40906,12 +45142,6 @@ static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
     return 0;
 }
 
-#if !CYTHON_COMPILING_IN_CPYTHON
-static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* values) {
-    return PyObject_CallMethodObjArgs(sep, __pyx_n_s_join, values, NULL)
-}
-#endif
-
 static CYTHON_INLINE int __Pyx_CheckKeywordStrings(
     PyObject *kwdict,
     const char* function_name,
@@ -41416,6 +45646,12 @@ static CYTHON_INLINE int __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
     return 0;
 }
 
+#if !CYTHON_COMPILING_IN_CPYTHON
+static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* values) {
+    return PyObject_CallMethodObjArgs(sep, __pyx_n_s_join, values, NULL)
+}
+#endif
+
 static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {
     PyObject *result;
 #if CYTHON_COMPILING_IN_CPYTHON
@@ -41535,852 +45771,163 @@ static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno,
                                   int full_traceback) {
     PyObject *old_exc, *old_val, *old_tb;
     PyObject *ctx;
-    __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
-    if (full_traceback) {
-        Py_XINCREF(old_exc);
-        Py_XINCREF(old_val);
-        Py_XINCREF(old_tb);
-        __Pyx_ErrRestore(old_exc, old_val, old_tb);
-        PyErr_PrintEx(1);
-    }
-    #if PY_MAJOR_VERSION < 3
-    ctx = PyString_FromString(name);
-    #else
-    ctx = PyUnicode_FromString(name);
-    #endif
-    __Pyx_ErrRestore(old_exc, old_val, old_tb);
-    if (!ctx) {
-        PyErr_WriteUnraisable(Py_None);
-    } else {
-        PyErr_WriteUnraisable(ctx);
-        Py_DECREF(ctx);
-    }
-}
-
-static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
-    PyErr_Format(PyExc_ValueError,
-                 "too many values to unpack (expected %" CYTHON_FORMAT_SSIZE_T "d)", expected);
-}
-
-static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
-    PyErr_Format(PyExc_ValueError,
-                 "need more than %" CYTHON_FORMAT_SSIZE_T "d value%.1s to unpack",
-                 index, (index == 1) ? "" : "s");
-}
-
-static CYTHON_INLINE int __Pyx_IterFinish(void) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyThreadState *tstate = PyThreadState_GET();
-    PyObject* exc_type = tstate->curexc_type;
-    if (unlikely(exc_type)) {
-        if (likely(exc_type == PyExc_StopIteration) || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration)) {
-            PyObject *exc_value, *exc_tb;
-            exc_value = tstate->curexc_value;
-            exc_tb = tstate->curexc_traceback;
-            tstate->curexc_type = 0;
-            tstate->curexc_value = 0;
-            tstate->curexc_traceback = 0;
-            Py_DECREF(exc_type);
-            Py_XDECREF(exc_value);
-            Py_XDECREF(exc_tb);
-            return 0;
-        } else {
-            return -1;
-        }
-    }
-    return 0;
-#else
-    if (unlikely(PyErr_Occurred())) {
-        if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) {
-            PyErr_Clear();
-            return 0;
-        } else {
-            return -1;
-        }
-    }
-    return 0;
-#endif
-}
-
-static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected) {
-    if (unlikely(retval)) {
-        Py_DECREF(retval);
-        __Pyx_RaiseTooManyValuesError(expected);
-        return -1;
-    } else {
-        return __Pyx_IterFinish();
-    }
-    return 0;
-}
-
-static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObject* default_value) {
-    PyObject* value;
-#if PY_MAJOR_VERSION >= 3
-    value = PyDict_GetItemWithError(d, key);
-    if (unlikely(!value)) {
-        if (unlikely(PyErr_Occurred()))
-            return NULL;
-        value = default_value;
-    }
-    Py_INCREF(value);
-#else
-    if (PyString_CheckExact(key) || PyUnicode_CheckExact(key) || PyInt_CheckExact(key)) {
-        value = PyDict_GetItem(d, key);
-        if (unlikely(!value)) {
-            value = default_value;
-        }
-        Py_INCREF(value);
-    } else {
-        if (default_value == Py_None)
-            default_value = NULL;
-        value = PyObject_CallMethodObjArgs(
-            d, __pyx_n_s_get, key, default_value, NULL);
-    }
-#endif
-    return value;
-}
-
-static CYTHON_INLINE long __Pyx_div_long(long a, long b) {
-    long q = a / b;
-    long r = a - q*b;
-    q -= ((r != 0) & ((r ^ b) < 0));
-    return q;
-}
-
-static CYTHON_INLINE long __Pyx_mod_long(long a, long b) {
-    long r = a % b;
-    r += ((r != 0) & ((r ^ b) < 0)) * b;
-    return r;
-}
-
-static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname) {
-    PyErr_Format(PyExc_UnboundLocalError, "local variable '%s' referenced before assignment", varname);
-}
-
-static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
-#if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
-    PyObject *ob = PyCapsule_New(vtable, 0, 0);
-#else
-    PyObject *ob = PyCObject_FromVoidPtr(vtable, 0);
-#endif
-    if (!ob)
-        goto bad;
-    if (PyDict_SetItem(dict, __pyx_n_s_pyx_vtable, ob) < 0)
-        goto bad;
-    Py_DECREF(ob);
-    return 0;
-bad:
-    Py_XDECREF(ob);
-    return -1;
-}
-
-static void* __Pyx_GetVtable(PyObject *dict) {
-    void* ptr;
-    PyObject *ob = PyObject_GetItem(dict, __pyx_n_s_pyx_vtable);
-    if (!ob)
-        goto bad;
-#if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
-    ptr = PyCapsule_GetPointer(ob, 0);
-#else
-    ptr = PyCObject_AsVoidPtr(ob);
-#endif
-    if (!ptr && !PyErr_Occurred())
-        PyErr_SetString(PyExc_RuntimeError, "invalid vtable found for imported type");
-    Py_DECREF(ob);
-    return ptr;
-bad:
-    Py_XDECREF(ob);
-    return NULL;
-}
-
-static PyObject *__Pyx_CalculateMetaclass(PyTypeObject *metaclass, PyObject *bases) {
-    Py_ssize_t i, nbases = PyTuple_GET_SIZE(bases);
-    for (i=0; i < nbases; i++) {
-        PyTypeObject *tmptype;
-        PyObject *tmp = PyTuple_GET_ITEM(bases, i);
-        tmptype = Py_TYPE(tmp);
-#if PY_MAJOR_VERSION < 3
-        if (tmptype == &PyClass_Type)
-            continue;
-#endif
-        if (!metaclass) {
-            metaclass = tmptype;
-            continue;
-        }
-        if (PyType_IsSubtype(metaclass, tmptype))
-            continue;
-        if (PyType_IsSubtype(tmptype, metaclass)) {
-            metaclass = tmptype;
-            continue;
-        }
-        PyErr_SetString(PyExc_TypeError,
-                        "metaclass conflict: "
-                        "the metaclass of a derived class "
-                        "must be a (non-strict) subclass "
-                        "of the metaclasses of all its bases");
-        return NULL;
-    }
-    if (!metaclass) {
-#if PY_MAJOR_VERSION < 3
-        metaclass = &PyClass_Type;
-#else
-        metaclass = &PyType_Type;
-#endif
-    }
-    Py_INCREF((PyObject*) metaclass);
-    return (PyObject*) metaclass;
-}
-
-static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type) {
-    PyObject* fake_module;
-    PyTypeObject* cached_type = NULL;
-    fake_module = PyImport_AddModule((char*) "_cython_" CYTHON_ABI);
-    if (!fake_module) return NULL;
-    Py_INCREF(fake_module);
-    cached_type = (PyTypeObject*) PyObject_GetAttrString(fake_module, type->tp_name);
-    if (cached_type) {
-        if (!PyType_Check((PyObject*)cached_type)) {
-            PyErr_Format(PyExc_TypeError,
-                "Shared Cython type %.200s is not a type object",
-                type->tp_name);
-            goto bad;
-        }
-        if (cached_type->tp_basicsize != type->tp_basicsize) {
-            PyErr_Format(PyExc_TypeError,
-                "Shared Cython type %.200s has the wrong size, try recompiling",
-                type->tp_name);
-            goto bad;
-        }
-    } else {
-        if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad;
-        PyErr_Clear();
-        if (PyType_Ready(type) < 0) goto bad;
-        if (PyObject_SetAttrString(fake_module, type->tp_name, (PyObject*) type) < 0)
-            goto bad;
-        Py_INCREF(type);
-        cached_type = type;
-    }
-done:
-    Py_DECREF(fake_module);
-    return cached_type;
-bad:
-    Py_XDECREF(cached_type);
-    cached_type = NULL;
-    goto done;
-}
-
-static PyObject *
-__Pyx_CyFunction_get_doc(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *closure)
-{
-    if (unlikely(op->func_doc == NULL)) {
-        if (op->func.m_ml->ml_doc) {
-#if PY_MAJOR_VERSION >= 3
-            op->func_doc = PyUnicode_FromString(op->func.m_ml->ml_doc);
-#else
-            op->func_doc = PyString_FromString(op->func.m_ml->ml_doc);
-#endif
-            if (unlikely(op->func_doc == NULL))
-                return NULL;
-        } else {
-            Py_INCREF(Py_None);
-            return Py_None;
-        }
-    }
-    Py_INCREF(op->func_doc);
-    return op->func_doc;
-}
-static int
-__Pyx_CyFunction_set_doc(__pyx_CyFunctionObject *op, PyObject *value)
-{
-    PyObject *tmp = op->func_doc;
-    if (value == NULL)
-        value = Py_None; /* Mark as deleted */
-    Py_INCREF(value);
-    op->func_doc = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_name(__pyx_CyFunctionObject *op)
-{
-    if (unlikely(op->func_name == NULL)) {
-#if PY_MAJOR_VERSION >= 3
-        op->func_name = PyUnicode_InternFromString(op->func.m_ml->ml_name);
-#else
-        op->func_name = PyString_InternFromString(op->func.m_ml->ml_name);
-#endif
-        if (unlikely(op->func_name == NULL))
-            return NULL;
-    }
-    Py_INCREF(op->func_name);
-    return op->func_name;
-}
-static int
-__Pyx_CyFunction_set_name(__pyx_CyFunctionObject *op, PyObject *value)
-{
-    PyObject *tmp;
-#if PY_MAJOR_VERSION >= 3
-    if (unlikely(value == NULL || !PyUnicode_Check(value))) {
-#else
-    if (unlikely(value == NULL || !PyString_Check(value))) {
-#endif
-        PyErr_SetString(PyExc_TypeError,
-                        "__name__ must be set to a string object");
-        return -1;
-    }
-    tmp = op->func_name;
-    Py_INCREF(value);
-    op->func_name = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_qualname(__pyx_CyFunctionObject *op)
-{
-    Py_INCREF(op->func_qualname);
-    return op->func_qualname;
-}
-static int
-__Pyx_CyFunction_set_qualname(__pyx_CyFunctionObject *op, PyObject *value)
-{
-    PyObject *tmp;
-#if PY_MAJOR_VERSION >= 3
-    if (unlikely(value == NULL || !PyUnicode_Check(value))) {
-#else
-    if (unlikely(value == NULL || !PyString_Check(value))) {
-#endif
-        PyErr_SetString(PyExc_TypeError,
-                        "__qualname__ must be set to a string object");
-        return -1;
-    }
-    tmp = op->func_qualname;
-    Py_INCREF(value);
-    op->func_qualname = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_self(__pyx_CyFunctionObject *m, CYTHON_UNUSED void *closure)
-{
-    PyObject *self;
-    self = m->func_closure;
-    if (self == NULL)
-        self = Py_None;
-    Py_INCREF(self);
-    return self;
-}
-static PyObject *
-__Pyx_CyFunction_get_dict(__pyx_CyFunctionObject *op)
-{
-    if (unlikely(op->func_dict == NULL)) {
-        op->func_dict = PyDict_New();
-        if (unlikely(op->func_dict == NULL))
-            return NULL;
-    }
-    Py_INCREF(op->func_dict);
-    return op->func_dict;
-}
-static int
-__Pyx_CyFunction_set_dict(__pyx_CyFunctionObject *op, PyObject *value)
-{
-    PyObject *tmp;
-    if (unlikely(value == NULL)) {
-        PyErr_SetString(PyExc_TypeError,
-               "function's dictionary may not be deleted");
-        return -1;
+    __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
+    if (full_traceback) {
+        Py_XINCREF(old_exc);
+        Py_XINCREF(old_val);
+        Py_XINCREF(old_tb);
+        __Pyx_ErrRestore(old_exc, old_val, old_tb);
+        PyErr_PrintEx(1);
     }
-    if (unlikely(!PyDict_Check(value))) {
-        PyErr_SetString(PyExc_TypeError,
-               "setting function's dictionary to a non-dict");
-        return -1;
+    #if PY_MAJOR_VERSION < 3
+    ctx = PyString_FromString(name);
+    #else
+    ctx = PyUnicode_FromString(name);
+    #endif
+    __Pyx_ErrRestore(old_exc, old_val, old_tb);
+    if (!ctx) {
+        PyErr_WriteUnraisable(Py_None);
+    } else {
+        PyErr_WriteUnraisable(ctx);
+        Py_DECREF(ctx);
     }
-    tmp = op->func_dict;
-    Py_INCREF(value);
-    op->func_dict = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_globals(__pyx_CyFunctionObject *op)
-{
-    Py_INCREF(op->func_globals);
-    return op->func_globals;
 }
-static PyObject *
-__Pyx_CyFunction_get_closure(CYTHON_UNUSED __pyx_CyFunctionObject *op)
-{
-    Py_INCREF(Py_None);
-    return Py_None;
-}
-static PyObject *
-__Pyx_CyFunction_get_code(__pyx_CyFunctionObject *op)
-{
-    PyObject* result = (op->func_code) ? op->func_code : Py_None;
-    Py_INCREF(result);
-    return result;
-}
-static int
-__Pyx_CyFunction_init_defaults(__pyx_CyFunctionObject *op) {
-    PyObject *res = op->defaults_getter((PyObject *) op);
-    if (unlikely(!res))
-        return -1;
-    op->defaults_tuple = PyTuple_GET_ITEM(res, 0);
-    Py_INCREF(op->defaults_tuple);
-    op->defaults_kwdict = PyTuple_GET_ITEM(res, 1);
-    Py_INCREF(op->defaults_kwdict);
-    Py_DECREF(res);
-    return 0;
+
+static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
+    PyErr_Format(PyExc_ValueError,
+                 "too many values to unpack (expected %" CYTHON_FORMAT_SSIZE_T "d)", expected);
 }
-static int
-__Pyx_CyFunction_set_defaults(__pyx_CyFunctionObject *op, PyObject* value) {
-    PyObject* tmp;
-    if (!value) {
-        value = Py_None;
-    } else if (value != Py_None && !PyTuple_Check(value)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "__defaults__ must be set to a tuple object");
-        return -1;
-    }
-    Py_INCREF(value);
-    tmp = op->defaults_tuple;
-    op->defaults_tuple = value;
-    Py_XDECREF(tmp);
-    return 0;
+
+static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
+    PyErr_Format(PyExc_ValueError,
+                 "need more than %" CYTHON_FORMAT_SSIZE_T "d value%.1s to unpack",
+                 index, (index == 1) ? "" : "s");
 }
-static PyObject *
-__Pyx_CyFunction_get_defaults(__pyx_CyFunctionObject *op) {
-    PyObject* result = op->defaults_tuple;
-    if (unlikely(!result)) {
-        if (op->defaults_getter) {
-            if (__Pyx_CyFunction_init_defaults(op) < 0) return NULL;
-            result = op->defaults_tuple;
+
+static CYTHON_INLINE int __Pyx_IterFinish(void) {
+#if CYTHON_COMPILING_IN_CPYTHON
+    PyThreadState *tstate = PyThreadState_GET();
+    PyObject* exc_type = tstate->curexc_type;
+    if (unlikely(exc_type)) {
+        if (likely(exc_type == PyExc_StopIteration) || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration)) {
+            PyObject *exc_value, *exc_tb;
+            exc_value = tstate->curexc_value;
+            exc_tb = tstate->curexc_traceback;
+            tstate->curexc_type = 0;
+            tstate->curexc_value = 0;
+            tstate->curexc_traceback = 0;
+            Py_DECREF(exc_type);
+            Py_XDECREF(exc_value);
+            Py_XDECREF(exc_tb);
+            return 0;
         } else {
-            result = Py_None;
+            return -1;
         }
     }
-    Py_INCREF(result);
-    return result;
-}
-static int
-__Pyx_CyFunction_set_kwdefaults(__pyx_CyFunctionObject *op, PyObject* value) {
-    PyObject* tmp;
-    if (!value) {
-        value = Py_None;
-    } else if (value != Py_None && !PyDict_Check(value)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "__kwdefaults__ must be set to a dict object");
-        return -1;
-    }
-    Py_INCREF(value);
-    tmp = op->defaults_kwdict;
-    op->defaults_kwdict = value;
-    Py_XDECREF(tmp);
     return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_kwdefaults(__pyx_CyFunctionObject *op) {
-    PyObject* result = op->defaults_kwdict;
-    if (unlikely(!result)) {
-        if (op->defaults_getter) {
-            if (__Pyx_CyFunction_init_defaults(op) < 0) return NULL;
-            result = op->defaults_kwdict;
+#else
+    if (unlikely(PyErr_Occurred())) {
+        if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) {
+            PyErr_Clear();
+            return 0;
         } else {
-            result = Py_None;
+            return -1;
         }
     }
-    Py_INCREF(result);
-    return result;
+    return 0;
+#endif
 }
-static int
-__Pyx_CyFunction_set_annotations(__pyx_CyFunctionObject *op, PyObject* value) {
-    PyObject* tmp;
-    if (!value || value == Py_None) {
-        value = NULL;
-    } else if (!PyDict_Check(value)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "__annotations__ must be set to a dict object");
+
+static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected) {
+    if (unlikely(retval)) {
+        Py_DECREF(retval);
+        __Pyx_RaiseTooManyValuesError(expected);
         return -1;
+    } else {
+        return __Pyx_IterFinish();
     }
-    Py_XINCREF(value);
-    tmp = op->func_annotations;
-    op->func_annotations = value;
-    Py_XDECREF(tmp);
     return 0;
 }
-static PyObject *
-__Pyx_CyFunction_get_annotations(__pyx_CyFunctionObject *op) {
-    PyObject* result = op->func_annotations;
-    if (unlikely(!result)) {
-        result = PyDict_New();
-        if (unlikely(!result)) return NULL;
-        op->func_annotations = result;
-    }
-    Py_INCREF(result);
-    return result;
-}
-static PyGetSetDef __pyx_CyFunction_getsets[] = {
-    {(char *) "func_doc", (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
-    {(char *) "__doc__",  (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
-    {(char *) "func_name", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0},
-    {(char *) "__name__", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0},
-    {(char *) "__qualname__", (getter)__Pyx_CyFunction_get_qualname, (setter)__Pyx_CyFunction_set_qualname, 0, 0},
-    {(char *) "__self__", (getter)__Pyx_CyFunction_get_self, 0, 0, 0},
-    {(char *) "func_dict", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0},
-    {(char *) "__dict__", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0},
-    {(char *) "func_globals", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0},
-    {(char *) "__globals__", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0},
-    {(char *) "func_closure", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0},
-    {(char *) "__closure__", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0},
-    {(char *) "func_code", (getter)__Pyx_CyFunction_get_code, 0, 0, 0},
-    {(char *) "__code__", (getter)__Pyx_CyFunction_get_code, 0, 0, 0},
-    {(char *) "func_defaults", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0},
-    {(char *) "__defaults__", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0},
-    {(char *) "__kwdefaults__", (getter)__Pyx_CyFunction_get_kwdefaults, (setter)__Pyx_CyFunction_set_kwdefaults, 0, 0},
-    {(char *) "__annotations__", (getter)__Pyx_CyFunction_get_annotations, (setter)__Pyx_CyFunction_set_annotations, 0, 0},
-    {0, 0, 0, 0, 0}
-};
-#ifndef PY_WRITE_RESTRICTED /* < Py2.5 */
-#define PY_WRITE_RESTRICTED WRITE_RESTRICTED
-#endif
-static PyMemberDef __pyx_CyFunction_members[] = {
-    {(char *) "__module__", T_OBJECT, offsetof(__pyx_CyFunctionObject, func.m_module), PY_WRITE_RESTRICTED, 0},
-    {0, 0, 0,  0, 0}
-};
-static PyObject *
-__Pyx_CyFunction_reduce(__pyx_CyFunctionObject *m, CYTHON_UNUSED PyObject *args)
-{
+
+static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObject* default_value) {
+    PyObject* value;
 #if PY_MAJOR_VERSION >= 3
-    return PyUnicode_FromString(m->func.m_ml->ml_name);
+    value = PyDict_GetItemWithError(d, key);
+    if (unlikely(!value)) {
+        if (unlikely(PyErr_Occurred()))
+            return NULL;
+        value = default_value;
+    }
+    Py_INCREF(value);
 #else
-    return PyString_FromString(m->func.m_ml->ml_name);
+    if (PyString_CheckExact(key) || PyUnicode_CheckExact(key) || PyInt_CheckExact(key)) {
+        value = PyDict_GetItem(d, key);
+        if (unlikely(!value)) {
+            value = default_value;
+        }
+        Py_INCREF(value);
+    } else {
+        if (default_value == Py_None)
+            default_value = NULL;
+        value = PyObject_CallMethodObjArgs(
+            d, __pyx_n_s_get, key, default_value, NULL);
+    }
 #endif
+    return value;
 }
-static PyMethodDef __pyx_CyFunction_methods[] = {
-    {__Pyx_NAMESTR("__reduce__"), (PyCFunction)__Pyx_CyFunction_reduce, METH_VARARGS, 0},
-    {0, 0, 0, 0}
-};
-static PyObject *__Pyx_CyFunction_New(PyTypeObject *type, PyMethodDef *ml, int flags, PyObject* qualname,
-                                      PyObject *closure, PyObject *module, PyObject* globals, PyObject* code) {
-    __pyx_CyFunctionObject *op = PyObject_GC_New(__pyx_CyFunctionObject, type);
-    if (op == NULL)
-        return NULL;
-    op->flags = flags;
-    op->func_weakreflist = NULL;
-    op->func.m_ml = ml;
-    op->func.m_self = (PyObject *) op;
-    Py_XINCREF(closure);
-    op->func_closure = closure;
-    Py_XINCREF(module);
-    op->func.m_module = module;
-    op->func_dict = NULL;
-    op->func_name = NULL;
-    Py_INCREF(qualname);
-    op->func_qualname = qualname;
-    op->func_doc = NULL;
-    op->func_classobj = NULL;
-    op->func_globals = globals;
-    Py_INCREF(op->func_globals);
-    Py_XINCREF(code);
-    op->func_code = code;
-    op->defaults_pyobjects = 0;
-    op->defaults = NULL;
-    op->defaults_tuple = NULL;
-    op->defaults_kwdict = NULL;
-    op->defaults_getter = NULL;
-    op->func_annotations = NULL;
-    PyObject_GC_Track(op);
-    return (PyObject *) op;
-}
-static int
-__Pyx_CyFunction_clear(__pyx_CyFunctionObject *m)
-{
-    Py_CLEAR(m->func_closure);
-    Py_CLEAR(m->func.m_module);
-    Py_CLEAR(m->func_dict);
-    Py_CLEAR(m->func_name);
-    Py_CLEAR(m->func_qualname);
-    Py_CLEAR(m->func_doc);
-    Py_CLEAR(m->func_globals);
-    Py_CLEAR(m->func_code);
-    Py_CLEAR(m->func_classobj);
-    Py_CLEAR(m->defaults_tuple);
-    Py_CLEAR(m->defaults_kwdict);
-    Py_CLEAR(m->func_annotations);
-    if (m->defaults) {
-        PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
-        int i;
-        for (i = 0; i < m->defaults_pyobjects; i++)
-            Py_XDECREF(pydefaults[i]);
-        PyMem_Free(m->defaults);
-        m->defaults = NULL;
-    }
-    return 0;
+
+static CYTHON_INLINE long __Pyx_div_long(long a, long b) {
+    long q = a / b;
+    long r = a - q*b;
+    q -= ((r != 0) & ((r ^ b) < 0));
+    return q;
 }
-static void __Pyx_CyFunction_dealloc(__pyx_CyFunctionObject *m)
-{
-    PyObject_GC_UnTrack(m);
-    if (m->func_weakreflist != NULL)
-        PyObject_ClearWeakRefs((PyObject *) m);
-    __Pyx_CyFunction_clear(m);
-    PyObject_GC_Del(m);
+
+static CYTHON_INLINE long __Pyx_mod_long(long a, long b) {
+    long r = a % b;
+    r += ((r != 0) & ((r ^ b) < 0)) * b;
+    return r;
 }
-static int __Pyx_CyFunction_traverse(__pyx_CyFunctionObject *m, visitproc visit, void *arg)
-{
-    Py_VISIT(m->func_closure);
-    Py_VISIT(m->func.m_module);
-    Py_VISIT(m->func_dict);
-    Py_VISIT(m->func_name);
-    Py_VISIT(m->func_qualname);
-    Py_VISIT(m->func_doc);
-    Py_VISIT(m->func_globals);
-    Py_VISIT(m->func_code);
-    Py_VISIT(m->func_classobj);
-    Py_VISIT(m->defaults_tuple);
-    Py_VISIT(m->defaults_kwdict);
-    if (m->defaults) {
-        PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
-        int i;
-        for (i = 0; i < m->defaults_pyobjects; i++)
-            Py_VISIT(pydefaults[i]);
-    }
-    return 0;
+
+static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname) {
+    PyErr_Format(PyExc_UnboundLocalError, "local variable '%s' referenced before assignment", varname);
 }
-static PyObject *__Pyx_CyFunction_descr_get(PyObject *func, PyObject *obj, PyObject *type)
-{
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    if (m->flags & __Pyx_CYFUNCTION_STATICMETHOD) {
-        Py_INCREF(func);
-        return func;
-    }
-    if (m->flags & __Pyx_CYFUNCTION_CLASSMETHOD) {
-        if (type == NULL)
-            type = (PyObject *)(Py_TYPE(obj));
-        return PyMethod_New(func,
-                            type, (PyObject *)(Py_TYPE(type)));
-    }
-    if (obj == Py_None)
-        obj = NULL;
-    return PyMethod_New(func, obj, type);
-}
-static PyObject*
-__Pyx_CyFunction_repr(__pyx_CyFunctionObject *op)
-{
-#if PY_MAJOR_VERSION >= 3
-    return PyUnicode_FromFormat("<cyfunction %U at %p>",
-                                op->func_qualname, (void *)op);
+
+static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
+#if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
+    PyObject *ob = PyCapsule_New(vtable, 0, 0);
 #else
-    return PyString_FromFormat("<cyfunction %s at %p>",
-                               PyString_AsString(op->func_qualname), (void *)op);
+    PyObject *ob = PyCObject_FromVoidPtr(vtable, 0);
 #endif
+    if (!ob)
+        goto bad;
+    if (PyDict_SetItem(dict, __pyx_n_s_pyx_vtable, ob) < 0)
+        goto bad;
+    Py_DECREF(ob);
+    return 0;
+bad:
+    Py_XDECREF(ob);
+    return -1;
 }
-#if CYTHON_COMPILING_IN_PYPY
-static PyObject * __Pyx_CyFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) {
-    PyCFunctionObject* f = (PyCFunctionObject*)func;
-    PyCFunction meth = PyCFunction_GET_FUNCTION(func);
-    PyObject *self = PyCFunction_GET_SELF(func);
-    Py_ssize_t size;
-    switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
-    case METH_VARARGS:
-        if (likely(kw == NULL) || PyDict_Size(kw) == 0)
-            return (*meth)(self, arg);
-        break;
-    case METH_VARARGS | METH_KEYWORDS:
-        return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
-    case METH_NOARGS:
-        if (likely(kw == NULL) || PyDict_Size(kw) == 0) {
-            size = PyTuple_GET_SIZE(arg);
-            if (size == 0)
-                return (*meth)(self, NULL);
-            PyErr_Format(PyExc_TypeError,
-                "%.200s() takes no arguments (%zd given)",
-                f->m_ml->ml_name, size);
-            return NULL;
-        }
-        break;
-    case METH_O:
-        if (likely(kw == NULL) || PyDict_Size(kw) == 0) {
-            size = PyTuple_GET_SIZE(arg);
-            if (size == 1)
-                return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
-            PyErr_Format(PyExc_TypeError,
-                "%.200s() takes exactly one argument (%zd given)",
-                f->m_ml->ml_name, size);
-            return NULL;
-        }
-        break;
-    default:
-        PyErr_SetString(PyExc_SystemError, "Bad call flags in "
-                        "__Pyx_CyFunction_Call. METH_OLDARGS is no "
-                        "longer supported!");
-        return NULL;
-    }
-    PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
-                 f->m_ml->ml_name);
-    return NULL;
-}
-#else
-static PyObject * __Pyx_CyFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) {
-	return PyCFunction_Call(func, arg, kw);
-}
-#endif
-static PyTypeObject __pyx_CyFunctionType_type = {
-    PyVarObject_HEAD_INIT(0, 0)
-    __Pyx_NAMESTR("cython_function_or_method"), /*tp_name*/
-    sizeof(__pyx_CyFunctionObject),   /*tp_basicsize*/
-    0,                                  /*tp_itemsize*/
-    (destructor) __Pyx_CyFunction_dealloc, /*tp_dealloc*/
-    0,                                  /*tp_print*/
-    0,                                  /*tp_getattr*/
-    0,                                  /*tp_setattr*/
-#if PY_MAJOR_VERSION < 3
-    0,                                  /*tp_compare*/
+
+static void* __Pyx_GetVtable(PyObject *dict) {
+    void* ptr;
+    PyObject *ob = PyObject_GetItem(dict, __pyx_n_s_pyx_vtable);
+    if (!ob)
+        goto bad;
+#if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
+    ptr = PyCapsule_GetPointer(ob, 0);
 #else
-    0,                                  /*reserved*/
-#endif
-    (reprfunc) __Pyx_CyFunction_repr,   /*tp_repr*/
-    0,                                  /*tp_as_number*/
-    0,                                  /*tp_as_sequence*/
-    0,                                  /*tp_as_mapping*/
-    0,                                  /*tp_hash*/
-    __Pyx_CyFunction_Call,              /*tp_call*/
-    0,                                  /*tp_str*/
-    0,                                  /*tp_getattro*/
-    0,                                  /*tp_setattro*/
-    0,                                  /*tp_as_buffer*/
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags*/
-    0,                                  /*tp_doc*/
-    (traverseproc) __Pyx_CyFunction_traverse,   /*tp_traverse*/
-    (inquiry) __Pyx_CyFunction_clear,   /*tp_clear*/
-    0,                                  /*tp_richcompare*/
-    offsetof(__pyx_CyFunctionObject, func_weakreflist), /* tp_weaklistoffse */
-    0,                                  /*tp_iter*/
-    0,                                  /*tp_iternext*/
-    __pyx_CyFunction_methods,           /*tp_methods*/
-    __pyx_CyFunction_members,           /*tp_members*/
-    __pyx_CyFunction_getsets,           /*tp_getset*/
-    0,                                  /*tp_base*/
-    0,                                  /*tp_dict*/
-    __Pyx_CyFunction_descr_get,         /*tp_descr_get*/
-    0,                                  /*tp_descr_set*/
-    offsetof(__pyx_CyFunctionObject, func_dict),/*tp_dictoffset*/
-    0,                                  /*tp_init*/
-    0,                                  /*tp_alloc*/
-    0,                                  /*tp_new*/
-    0,                                  /*tp_free*/
-    0,                                  /*tp_is_gc*/
-    0,                                  /*tp_bases*/
-    0,                                  /*tp_mro*/
-    0,                                  /*tp_cache*/
-    0,                                  /*tp_subclasses*/
-    0,                                  /*tp_weaklist*/
-    0,                                  /*tp_del*/
-#if PY_VERSION_HEX >= 0x02060000
-    0,                                  /*tp_version_tag*/
-#endif
-#if PY_VERSION_HEX >= 0x030400a1
-    0,                                  /*tp_finalize*/
-#endif
-};
-static int __Pyx_CyFunction_init(void) {
-#if !CYTHON_COMPILING_IN_PYPY
-    __pyx_CyFunctionType_type.tp_call = PyCFunction_Call;
+    ptr = PyCObject_AsVoidPtr(ob);
 #endif
-    __pyx_CyFunctionType = __Pyx_FetchCommonType(&__pyx_CyFunctionType_type);
-    if (__pyx_CyFunctionType == NULL) {
-        return -1;
-    }
-    return 0;
-}
-static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *func, size_t size, int pyobjects) {
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    m->defaults = PyMem_Malloc(size);
-    if (!m->defaults)
-        return PyErr_NoMemory();
-    memset(m->defaults, 0, size);
-    m->defaults_pyobjects = pyobjects;
-    return m->defaults;
-}
-static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *func, PyObject *tuple) {
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    m->defaults_tuple = tuple;
-    Py_INCREF(tuple);
-}
-static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *func, PyObject *dict) {
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    m->defaults_kwdict = dict;
-    Py_INCREF(dict);
-}
-static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *func, PyObject *dict) {
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    m->func_annotations = dict;
-    Py_INCREF(dict);
-}
-
-static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name,
-                                           PyObject *qualname, PyObject *mkw, PyObject *modname, PyObject *doc) {
-    PyObject *ns;
-    if (metaclass) {
-        PyObject *prep = __Pyx_PyObject_GetAttrStr(metaclass, __pyx_n_s_prepare);
-        if (prep) {
-            PyObject *pargs = PyTuple_Pack(2, name, bases);
-            if (unlikely(!pargs)) {
-                Py_DECREF(prep);
-                return NULL;
-            }
-            ns = PyObject_Call(prep, pargs, mkw);
-            Py_DECREF(prep);
-            Py_DECREF(pargs);
-        } else {
-            if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError)))
-                return NULL;
-            PyErr_Clear();
-            ns = PyDict_New();
-        }
-    } else {
-        ns = PyDict_New();
-    }
-    if (unlikely(!ns))
-        return NULL;
-    if (unlikely(PyObject_SetItem(ns, __pyx_n_s_module, modname) < 0)) goto bad;
-    if (unlikely(PyObject_SetItem(ns, __pyx_n_s_qualname, qualname) < 0)) goto bad;
-    if (unlikely(doc && PyObject_SetItem(ns, __pyx_n_s_doc, doc) < 0)) goto bad;
-    return ns;
+    if (!ptr && !PyErr_Occurred())
+        PyErr_SetString(PyExc_RuntimeError, "invalid vtable found for imported type");
+    Py_DECREF(ob);
+    return ptr;
 bad:
-    Py_DECREF(ns);
+    Py_XDECREF(ob);
     return NULL;
 }
-static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases,
-                                      PyObject *dict, PyObject *mkw,
-                                      int calculate_metaclass, int allow_py2_metaclass) {
-    PyObject *result, *margs;
-    PyObject *owned_metaclass = NULL;
-    if (allow_py2_metaclass) {
-        owned_metaclass = PyObject_GetItem(dict, __pyx_n_s_metaclass);
-        if (owned_metaclass) {
-            metaclass = owned_metaclass;
-        } else if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) {
-            PyErr_Clear();
-        } else {
-            return NULL;
-        }
-    }
-    if (calculate_metaclass && (!metaclass || PyType_Check(metaclass))) {
-        metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases);
-        Py_XDECREF(owned_metaclass);
-        if (unlikely(!metaclass))
-            return NULL;
-        owned_metaclass = metaclass;
-    }
-    margs = PyTuple_Pack(3, name, bases, dict);
-    if (unlikely(!margs)) {
-        result = NULL;
-    } else {
-        result = PyObject_Call(metaclass, margs, mkw);
-        Py_DECREF(margs);
-    }
-    Py_XDECREF(owned_metaclass);
-    return result;
-}
 
 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) {
     PyObject *empty_list = 0;
@@ -42780,32 +46327,6 @@ static CYTHON_INLINE uint32_t __Pyx_PyInt_As_uint32_t(PyObject *x) {
     }
 }
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value) {
-    const int neg_one = (int) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(int) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(int) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(int) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(int),
-                                     little, !is_unsigned);
-    }
-}
-
 static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int8_t(int8_t value) {
     const int8_t neg_one = (int8_t) -1, const_zero = 0;
     const int is_unsigned = neg_one > const_zero;
@@ -43088,6 +46609,32 @@ static CYTHON_INLINE size_t __Pyx_PyInt_As_size_t(PyObject *x) {
     }
 }
 
+static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value) {
+    const int neg_one = (int) -1, const_zero = 0;
+    const int is_unsigned = neg_one > const_zero;
+    if (is_unsigned) {
+        if (sizeof(int) < sizeof(long)) {
+            return PyInt_FromLong((long) value);
+        } else if (sizeof(int) <= sizeof(unsigned long)) {
+            return PyLong_FromUnsignedLong((unsigned long) value);
+        } else if (sizeof(int) <= sizeof(unsigned long long)) {
+            return PyLong_FromUnsignedLongLong((unsigned long long) value);
+        }
+    } else {
+        if (sizeof(int) <= sizeof(long)) {
+            return PyInt_FromLong((long) value);
+        } else if (sizeof(int) <= sizeof(long long)) {
+            return PyLong_FromLongLong((long long) value);
+        }
+    }
+    {
+        int one = 1; int little = (int)*(unsigned char *)&one;
+        unsigned char *bytes = (unsigned char *)&value;
+        return _PyLong_FromByteArray(bytes, sizeof(int),
+                                     little, !is_unsigned);
+    }
+}
+
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
   #include "longintrepr.h"
@@ -43366,52 +46913,26 @@ static CYTHON_INLINE int64_t __Pyx_PyInt_As_int64_t(PyObject *x) {
     }
 }
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_char(char value) {
-    const char neg_one = (char) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(char) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(char) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(char) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(char) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(char) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(char),
-                                     little, !is_unsigned);
-    }
-}
-
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
   #include "longintrepr.h"
  #endif
 #endif
-static CYTHON_INLINE uint8_t __Pyx_PyInt_As_uint8_t(PyObject *x) {
-    const uint8_t neg_one = (uint8_t) -1, const_zero = 0;
+static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
+    const uint16_t neg_one = (uint16_t) -1, const_zero = 0;
     const int is_unsigned = neg_one > const_zero;
 #if PY_MAJOR_VERSION < 3
     if (likely(PyInt_Check(x))) {
-        if (sizeof(uint8_t) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(uint8_t, long, PyInt_AS_LONG)
+        if (sizeof(uint16_t) < sizeof(long)) {
+            __PYX_VERIFY_RETURN_INT(uint16_t, long, PyInt_AS_LONG)
         } else {
             long val = PyInt_AS_LONG(x);
             if (is_unsigned && unlikely(val < 0)) {
                 PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint8_t");
-                return (uint8_t) -1;
+                                "can't convert negative value to uint16_t");
+                return (uint16_t) -1;
             }
-            return (uint8_t) val;
+            return (uint16_t) val;
         }
     } else
 #endif
@@ -43419,40 +46940,40 @@ static CYTHON_INLINE uint8_t __Pyx_PyInt_As_uint8_t(PyObject *x) {
         if (is_unsigned) {
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint8_t)) {
+            if (sizeof(digit) <= sizeof(uint16_t)) {
                 switch (Py_SIZE(x)) {
                     case  0: return 0;
-                    case  1: return (uint8_t) ((PyLongObject*)x)->ob_digit[0];
+                    case  1: return (uint16_t) ((PyLongObject*)x)->ob_digit[0];
                 }
             }
  #endif
 #endif
             if (unlikely(Py_SIZE(x) < 0)) {
                 PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint8_t");
-                return (uint8_t) -1;
+                                "can't convert negative value to uint16_t");
+                return (uint16_t) -1;
             }
-            if (sizeof(uint8_t) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(uint8_t, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(uint8_t) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(uint8_t, unsigned long long, PyLong_AsUnsignedLongLong)
+            if (sizeof(uint16_t) <= sizeof(unsigned long)) {
+                __PYX_VERIFY_RETURN_INT(uint16_t, unsigned long, PyLong_AsUnsignedLong)
+            } else if (sizeof(uint16_t) <= sizeof(unsigned long long)) {
+                __PYX_VERIFY_RETURN_INT(uint16_t, unsigned long long, PyLong_AsUnsignedLongLong)
             }
         } else {
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint8_t)) {
+            if (sizeof(digit) <= sizeof(uint16_t)) {
                 switch (Py_SIZE(x)) {
                     case  0: return 0;
-                    case  1: return +(uint8_t) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(uint8_t) ((PyLongObject*)x)->ob_digit[0];
+                    case  1: return +(uint16_t) ((PyLongObject*)x)->ob_digit[0];
+                    case -1: return -(uint16_t) ((PyLongObject*)x)->ob_digit[0];
                 }
             }
  #endif
 #endif
-            if (sizeof(uint8_t) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(uint8_t, long, PyLong_AsLong)
-            } else if (sizeof(uint8_t) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(uint8_t, long long, PyLong_AsLongLong)
+            if (sizeof(uint16_t) <= sizeof(long)) {
+                __PYX_VERIFY_RETURN_INT(uint16_t, long, PyLong_AsLong)
+            } else if (sizeof(uint16_t) <= sizeof(long long)) {
+                __PYX_VERIFY_RETURN_INT(uint16_t, long long, PyLong_AsLongLong)
             }
         }
         {
@@ -43460,7 +46981,7 @@ static CYTHON_INLINE uint8_t __Pyx_PyInt_As_uint8_t(PyObject *x) {
             PyErr_SetString(PyExc_RuntimeError,
                             "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
 #else
-            uint8_t val;
+            uint16_t val;
             PyObject *v = __Pyx_PyNumber_Int(x);
  #if PY_MAJOR_VERSION < 3
             if (likely(v) && !PyLong_Check(v)) {
@@ -43480,13 +47001,13 @@ static CYTHON_INLINE uint8_t __Pyx_PyInt_As_uint8_t(PyObject *x) {
                     return val;
             }
 #endif
-            return (uint8_t) -1;
+            return (uint16_t) -1;
         }
     } else {
-        uint8_t val;
+        uint16_t val;
         PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (uint8_t) -1;
-        val = __Pyx_PyInt_As_uint8_t(tmp);
+        if (!tmp) return (uint16_t) -1;
+        val = __Pyx_PyInt_As_uint16_t(tmp);
         Py_DECREF(tmp);
         return val;
     }
@@ -43597,21 +47118,21 @@ static CYTHON_INLINE int32_t __Pyx_PyInt_As_int32_t(PyObject *x) {
   #include "longintrepr.h"
  #endif
 #endif
-static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
-    const uint16_t neg_one = (uint16_t) -1, const_zero = 0;
+static CYTHON_INLINE uint8_t __Pyx_PyInt_As_uint8_t(PyObject *x) {
+    const uint8_t neg_one = (uint8_t) -1, const_zero = 0;
     const int is_unsigned = neg_one > const_zero;
 #if PY_MAJOR_VERSION < 3
     if (likely(PyInt_Check(x))) {
-        if (sizeof(uint16_t) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(uint16_t, long, PyInt_AS_LONG)
+        if (sizeof(uint8_t) < sizeof(long)) {
+            __PYX_VERIFY_RETURN_INT(uint8_t, long, PyInt_AS_LONG)
         } else {
             long val = PyInt_AS_LONG(x);
             if (is_unsigned && unlikely(val < 0)) {
                 PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint16_t");
-                return (uint16_t) -1;
+                                "can't convert negative value to uint8_t");
+                return (uint8_t) -1;
             }
-            return (uint16_t) val;
+            return (uint8_t) val;
         }
     } else
 #endif
@@ -43619,40 +47140,40 @@ static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
         if (is_unsigned) {
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint16_t)) {
+            if (sizeof(digit) <= sizeof(uint8_t)) {
                 switch (Py_SIZE(x)) {
                     case  0: return 0;
-                    case  1: return (uint16_t) ((PyLongObject*)x)->ob_digit[0];
+                    case  1: return (uint8_t) ((PyLongObject*)x)->ob_digit[0];
                 }
             }
  #endif
 #endif
             if (unlikely(Py_SIZE(x) < 0)) {
                 PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint16_t");
-                return (uint16_t) -1;
+                                "can't convert negative value to uint8_t");
+                return (uint8_t) -1;
             }
-            if (sizeof(uint16_t) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(uint16_t, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(uint16_t) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(uint16_t, unsigned long long, PyLong_AsUnsignedLongLong)
+            if (sizeof(uint8_t) <= sizeof(unsigned long)) {
+                __PYX_VERIFY_RETURN_INT(uint8_t, unsigned long, PyLong_AsUnsignedLong)
+            } else if (sizeof(uint8_t) <= sizeof(unsigned long long)) {
+                __PYX_VERIFY_RETURN_INT(uint8_t, unsigned long long, PyLong_AsUnsignedLongLong)
             }
         } else {
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint16_t)) {
+            if (sizeof(digit) <= sizeof(uint8_t)) {
                 switch (Py_SIZE(x)) {
                     case  0: return 0;
-                    case  1: return +(uint16_t) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(uint16_t) ((PyLongObject*)x)->ob_digit[0];
+                    case  1: return +(uint8_t) ((PyLongObject*)x)->ob_digit[0];
+                    case -1: return -(uint8_t) ((PyLongObject*)x)->ob_digit[0];
                 }
             }
  #endif
 #endif
-            if (sizeof(uint16_t) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(uint16_t, long, PyLong_AsLong)
-            } else if (sizeof(uint16_t) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(uint16_t, long long, PyLong_AsLongLong)
+            if (sizeof(uint8_t) <= sizeof(long)) {
+                __PYX_VERIFY_RETURN_INT(uint8_t, long, PyLong_AsLong)
+            } else if (sizeof(uint8_t) <= sizeof(long long)) {
+                __PYX_VERIFY_RETURN_INT(uint8_t, long long, PyLong_AsLongLong)
             }
         }
         {
@@ -43660,7 +47181,7 @@ static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
             PyErr_SetString(PyExc_RuntimeError,
                             "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
 #else
-            uint16_t val;
+            uint8_t val;
             PyObject *v = __Pyx_PyNumber_Int(x);
  #if PY_MAJOR_VERSION < 3
             if (likely(v) && !PyLong_Check(v)) {
@@ -43680,18 +47201,44 @@ static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
                     return val;
             }
 #endif
-            return (uint16_t) -1;
+            return (uint8_t) -1;
         }
     } else {
-        uint16_t val;
+        uint8_t val;
         PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (uint16_t) -1;
-        val = __Pyx_PyInt_As_uint16_t(tmp);
+        if (!tmp) return (uint8_t) -1;
+        val = __Pyx_PyInt_As_uint8_t(tmp);
         Py_DECREF(tmp);
         return val;
     }
 }
 
+static CYTHON_INLINE PyObject* __Pyx_PyInt_From_char(char value) {
+    const char neg_one = (char) -1, const_zero = 0;
+    const int is_unsigned = neg_one > const_zero;
+    if (is_unsigned) {
+        if (sizeof(char) < sizeof(long)) {
+            return PyInt_FromLong((long) value);
+        } else if (sizeof(char) <= sizeof(unsigned long)) {
+            return PyLong_FromUnsignedLong((unsigned long) value);
+        } else if (sizeof(char) <= sizeof(unsigned long long)) {
+            return PyLong_FromUnsignedLongLong((unsigned long long) value);
+        }
+    } else {
+        if (sizeof(char) <= sizeof(long)) {
+            return PyInt_FromLong((long) value);
+        } else if (sizeof(char) <= sizeof(long long)) {
+            return PyLong_FromLongLong((long long) value);
+        }
+    }
+    {
+        int one = 1; int little = (int)*(unsigned char *)&one;
+        unsigned char *bytes = (unsigned char *)&value;
+        return _PyLong_FromByteArray(bytes, sizeof(char),
+                                     little, !is_unsigned);
+    }
+}
+
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
   #include "longintrepr.h"
@@ -43792,6 +47339,44 @@ static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {
     }
 }
 
+static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type) {
+    PyObject* fake_module;
+    PyTypeObject* cached_type = NULL;
+    fake_module = PyImport_AddModule((char*) "_cython_" CYTHON_ABI);
+    if (!fake_module) return NULL;
+    Py_INCREF(fake_module);
+    cached_type = (PyTypeObject*) PyObject_GetAttrString(fake_module, type->tp_name);
+    if (cached_type) {
+        if (!PyType_Check((PyObject*)cached_type)) {
+            PyErr_Format(PyExc_TypeError,
+                "Shared Cython type %.200s is not a type object",
+                type->tp_name);
+            goto bad;
+        }
+        if (cached_type->tp_basicsize != type->tp_basicsize) {
+            PyErr_Format(PyExc_TypeError,
+                "Shared Cython type %.200s has the wrong size, try recompiling",
+                type->tp_name);
+            goto bad;
+        }
+    } else {
+        if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad;
+        PyErr_Clear();
+        if (PyType_Ready(type) < 0) goto bad;
+        if (PyObject_SetAttrString(fake_module, type->tp_name, (PyObject*) type) < 0)
+            goto bad;
+        Py_INCREF(type);
+        cached_type = type;
+    }
+done:
+    Py_DECREF(fake_module);
+    return cached_type;
+bad:
+    Py_XDECREF(cached_type);
+    cached_type = NULL;
+    goto done;
+}
+
 static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, PyObject **tb) {
     PyObject *tmp_type, *tmp_value, *tmp_tb;
 #if CYTHON_COMPILING_IN_CPYTHON
diff --git a/pysam/csamfile.pxd b/pysam/calignmentfile.pxd
similarity index 86%
copy from pysam/csamfile.pxd
copy to pysam/calignmentfile.pxd
index 28cabd1..1eb925e 100644
--- a/pysam/csamfile.pxd
+++ b/pysam/calignmentfile.pxd
@@ -13,10 +13,10 @@ cdef extern from *:
 cdef extern from "htslib_util.h":
 
     # add *nbytes* into the variable length data of *src* at *pos*
-    bam1_t * pysam_bam_update( bam1_t * b,
-                               size_t nbytes_old,
-                               size_t nbytes_new,
-                               uint8_t * pos )
+    bam1_t * pysam_bam_update(bam1_t * b,
+                              size_t nbytes_old,
+                              size_t nbytes_new,
+                              uint8_t * pos)
 
     # now: static
     int aux_type2size(int)
@@ -37,7 +37,7 @@ cdef extern from "htslib_util.h":
     void pysam_set_bin(bam1_t * b, uint16_t v)
     void pysam_set_qual(bam1_t * b, uint8_t v)
     void pysam_set_l_qname(bam1_t * b, uint8_t v)
-    void pysam_set_flag(bam1_t * b, uint8_t v)
+    void pysam_set_flag(bam1_t * b, uint16_t v)
     void pysam_set_n_cigar(bam1_t * b, uint16_t v)
     void pysam_update_flag(bam1_t * b, uint16_t v, uint16_t flag)
 
@@ -47,7 +47,6 @@ cdef extern from "samfile_util.h":
     int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
     int bam_prob_realn(bam1_t *b, const char *ref)
 
-
 ####################################################################
 # Utility types
 
@@ -63,16 +62,16 @@ ctypedef struct __iterdata:
 # Exposing pysam extension classes
 #
 # Note: need to declare all C fields and methods here
-cdef class AlignedRead:
+cdef class AlignedSegment:
 
-    # object that this AlignedRead represents
+    # object that this AlignedSegment represents
     cdef bam1_t * _delegate
 
-    # add an alignment tag with value to the AlignedRead
+    # add an alignment tag with value to the AlignedSegment
     # an existing tag of the same name will be replaced.
     cpdef setTag( self, tag, value, value_type = ?, replace = ? )
 
-cdef class Samfile:
+cdef class AlignmentFile:
 
     cdef object _filename
 
@@ -105,18 +104,18 @@ cdef class Samfile:
     cdef int cnext(self)
 
     # write an aligned read
-    cpdef int write(self, AlignedRead read)
+    cpdef int write(self, AlignedSegment read)
 
     cdef char * _getrname(self, int tid)
 
-cdef class PileupProxy:
+cdef class PileupColumn:
     cdef bam_pileup1_t ** plp
     cdef int tid
     cdef int pos
     cdef int n_pu
 
 cdef class PileupRead:
-    cdef AlignedRead _alignment
+    cdef AlignedSegment _alignment
     cdef int32_t  _qpos
     cdef int _indel
     cdef int _level
@@ -127,8 +126,9 @@ cdef class PileupRead:
 cdef class IteratorRow:
     cdef int retval
     cdef bam1_t * b
-    cdef Samfile samfile
+    cdef AlignmentFile samfile
     cdef htsFile * htsfile
+    cdef bam_hdr_t * header
     cdef int owns_samfile
 
 cdef class IteratorRowRegion(IteratorRow):
@@ -168,7 +168,7 @@ cdef class IteratorColumn:
     cdef bam_pileup1_t * plp
     cdef bam_plp_t pileup_iter
     cdef __iterdata iterdata
-    cdef Samfile samfile
+    cdef AlignmentFile samfile
     cdef Fastafile fastafile
     cdef stepper
     cdef int max_depth
@@ -180,9 +180,10 @@ cdef class IteratorColumn:
                            int tid,
                            int start,
                            int end,
-                           int reopen = ?)
+                           int multiple_iterators = ?)
 
     cdef reset(self, tid, start, end)
+    cdef _free_pileup_iter(self)
 
 cdef class IteratorColumnRegion(IteratorColumn):
     cdef int start
@@ -193,8 +194,9 @@ cdef class IteratorColumnAllRefs(IteratorColumn):
     pass
 
 cdef class IndexedReads:
-    cdef Samfile samfile
+    cdef AlignmentFile samfile
     cdef htsFile * htsfile
     cdef index
     cdef int owns_samfile
     cdef BGZF * fp
+    cdef bam_hdr_t * header
diff --git a/pysam/csamfile.pyx b/pysam/calignmentfile.pyx
similarity index 70%
copy from pysam/csamfile.pyx
copy to pysam/calignmentfile.pyx
index e91f7b8..c9b01a8 100644
--- a/pysam/csamfile.pyx
+++ b/pysam/calignmentfile.pyx
@@ -12,11 +12,15 @@ import collections
 import re
 import platform
 import warnings
+import array
+
 from cpython cimport PyErr_SetString, \
     PyBytes_Check, \
     PyUnicode_Check, \
     PyBytes_FromStringAndSize
 
+from cpython cimport array
+
 from cpython.version cimport PY_MAJOR_VERSION
 
 ########################################################################
@@ -54,7 +58,8 @@ cdef bytes _encodeFilename(object filename):
         raise TypeError, u"Argument must be string or unicode."
 
 cdef bytes _forceBytes(object s):
-    u"""convert string or unicode object to bytes, assuming ascii encoding.
+    u"""convert string or unicode object to bytes, assuming
+    ascii encoding.
     """
     if PY_MAJOR_VERSION < 3:
         return s
@@ -116,29 +121,30 @@ cdef int max_pos = 2 << 29
 #####################################################################
 ## private factory methods
 #####################################################################
-cdef class AlignedRead
-cdef makeAlignedRead(bam1_t * src):
-    '''enter src into AlignedRead.'''
+cdef class AlignedSegment
+cdef object makeAlignedSegment(bam1_t * src):
+    '''enter src into AlignedSegment.'''
     # note that the following does not call __init__
-    cdef AlignedRead dest = AlignedRead.__new__(AlignedRead)
+    cdef AlignedSegment dest = AlignedSegment.__new__(AlignedSegment)
     dest._delegate = bam_dup1(src)
     return dest
 
-cdef class PileupProxy
-cdef makePileupProxy(bam_pileup1_t ** plp, int tid, int pos, int n):
+
+cdef class PileupColumn
+cdef makePileupColumn(bam_pileup1_t ** plp, int tid, int pos, int n_pu):
     # note that the following does not call __init__
-     cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
+     cdef PileupColumn dest = PileupColumn.__new__(PileupColumn)
      dest.plp = plp
      dest.tid = tid
      dest.pos = pos
-     dest.n = n
+     dest.n_pu = n_pu
      return dest
 
 cdef class PileupRead
-cdef makePileupRead( bam_pileup1_t * src ):
+cdef makePileupRead(bam_pileup1_t * src):
     '''fill a  PileupRead object from a bam_pileup1_t * object.'''
     cdef PileupRead dest = PileupRead.__new__(PileupRead)
-    dest._alignment = makeAlignedRead( src.b )
+    dest._alignment = makeAlignedSegment(src.b)
     dest._qpos = src.qpos
     dest._indel = src.indel
     dest._level = src.level
@@ -194,35 +200,17 @@ cdef convertBinaryTagToList( uint8_t * s ):
     return byte_size, nvalues, values
 
 
-class PileupColumn(object):
-    '''A pileup column. A pileup column contains
-    all the reads that map to a certain target base.
-
-    tid
-        chromosome ID as is defined in the header
-    pos
-        the target base coordinate (0-based)
-    n
-        number of reads mapping to this column
-    pileups
-        list of reads (:class:`pysam.PileupRead`) aligned to this column
-    '''
-    def __str__(self):
-        return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
-            "\n" + "\n".join( map(str, self.pileups) )
-
-
-# valid types for sam headers
+# valid types for SAM headers
 VALID_HEADER_TYPES = {"HD" : dict,
                       "SQ" : list,
                       "RG" : list,
                       "PG" : list,
                       "CO" : list}
 
-# order of records within sam headers
+# order of records within SAM headers
 VALID_HEADERS = ("HD", "SQ", "RG", "PG", "CO")
 
-# type conversions within sam header records
+# default type conversions within SAM header records
 VALID_HEADER_FIELDS = {"HD" : {"VN" : str, "SO" : str, "GO" : str},
                        "SQ" : {"SN" : str, "LN" : int, "AS" : str, 
                                "M5" : str, "SP" : str, "UR" : str,},
@@ -244,12 +232,12 @@ VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),
                               "PP"),}
 
 
-cdef class Samfile:
+cdef class AlignmentFile:
     '''*(filename, mode=None, template = None,
-         referencenames = None, referencelengths = None,
-         text = NULL, header = None,
-         add_sq_text = False, check_header = True,
-         check_sq = True )*
+         referencenames=None, referencelengths = None,
+         text=NULL, header=None,
+         add_sq_text=False, check_header=True,
+         check_sq=True)*
 
     A :term:`SAM`/:term:`BAM` formatted file. The file is
     automatically opened.
@@ -261,17 +249,17 @@ cdef class Samfile:
     text (:term:`TAM`) mode.
 
     If ``b`` is present, it must immediately follow ``r`` or ``w``.
-    Valid modes are ``r``, ``w``, ``wh``, ``rb``, ``wb`` and
-    ``wbu``. For instance, to open a :term:`BAM` formatted file for
+    Valid modes are ``r``, ``w``, ``wh``, ``rb``, ``wb``, ``wbu`` and
+    ``wb0``. For instance, to open a :term:`BAM` formatted file for
     reading, type::
 
-        f = pysam.Samfile('ex1.bam','rb')
+        f = pysam.AlignmentFile('ex1.bam','rb')
 
     If mode is not specified, we will try to auto-detect in the order
     'rb', 'r', thus both the following should work::
 
-        f1 = pysam.Samfile('ex1.bam' )
-        f2 = pysam.Samfile('ex1.sam' )
+        f1 = pysam.AlignmentFile('ex1.bam')
+        f2 = pysam.AlignmentFile('ex1.sam')
 
     If an index for a BAM file exists (.bai), it will be opened
     automatically. Without an index random access to reads via
@@ -282,7 +270,7 @@ cdef class Samfile:
     specification):
 
         1. If *template* is given, the header is copied from a another
-           *Samfile* (*template* must be of type *Samfile*).
+           *AlignmentFile* (*template* must be of type *AlignmentFile*).
 
         2. If *header* is given, the header is built from a
            multi-level dictionary. The first level are the four types
@@ -327,7 +315,7 @@ cdef class Samfile:
     def _open(self,
               filename,
               mode=None,
-              Samfile template=None,
+              AlignmentFile template=None,
               referencenames=None,
               referencelengths=None,
               text=None,
@@ -369,13 +357,17 @@ cdef class Samfile:
                        check_sq=check_sq)
             return
 
-        assert mode in ( "r","w","rb","wb", "wh", "wbu", "rU" ), \
+        assert mode in ("r","w","rb","wb", "wh", "wbu", "rU", "wb0"), \
             "invalid file opening mode `%s`" % mode
 
         # close a previously opened file
         if self.htsfile != NULL:
             self.close()
 
+        # for htslib, wbu seems to not work
+        if mode == "wbu":
+            mode = "wb0"
+
         cdef bytes bmode = mode.encode('ascii')
         self._filename = filename = _encodeFilename(filename)
         self.isstream = filename == b"-"
@@ -412,8 +404,10 @@ cdef class Samfile:
                 n = 0
                 for x in referencenames:
                     n += len(x) + 1
-                self.header.target_name = <char**>calloc(n, sizeof(char*))
-                self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
+                self.header.target_name = <char**>calloc(
+                    n, sizeof(char*))
+                self.header.target_len = <uint32_t*>calloc(
+                    n, sizeof(uint32_t))
                 for x from 0 <= x < self.header.n_targets:
                     self.header.target_len[x] = referencelengths[x]
                     name = referencenames[x]
@@ -431,7 +425,7 @@ cdef class Samfile:
                                      referencelengths[x]))
                     text = ''.join(text)
 
-                if text != None:
+                if text is not None:
                     # copy without \0
                     text = _forceBytes(text)
                     ctext = text
@@ -515,28 +509,28 @@ cdef class Samfile:
             if not self.isstream:
                 self.start_offset = bgzf_tell(self.fp)
 
-    def gettid( self, reference ):
+    def gettid(self, reference):
         '''
         convert :term:`reference` name into numerical :term:`tid`
 
         returns -1 if reference is not known.
         '''
         if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
+            raise ValueError("I/O operation on closed file")
         reference = _forceBytes(reference)
         return bam_name2id(self.header, reference)
 
-    def getrname( self, tid ):
+    def getrname(self, tid):
         '''
         convert numerical :term:`tid` into :term:`reference` name.'''
         if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
+            raise ValueError("I/O operation on closed file")
         if not 0 <= tid < self.header.n_targets:
-            raise ValueError("tid %i out of range 0<=tid<%i" % 
-                             (tid, self.header.n_targets ) )
+            raise ValueError("reference_id %i out of range 0<=tid<%i" % 
+                             (tid, self.header.n_targets))
         return _charptr_to_str(self.header.target_name[tid])
 
-    cdef char * _getrname( self, int tid ): # TODO unused
+    cdef char * _getrname(self, int tid):   # TODO unused
         '''
         convert numerical :term:`tid` into :term:`reference` name.'''
         if not self._isOpen():
@@ -553,15 +547,16 @@ cdef class Samfile:
                      end=None,
                      region=None,
                      tid=None):
-        '''
-        parse region information.
+        '''parse region information.
 
-        raise ValueError for for invalid regions.
+        Raises ValueError for invalid regions.
 
-        returns a tuple of flag, tid, start and end. Flag indicates
-        whether some coordinates were supplied.
+        Returns a tuple of a flag, :term:`tid`, start and end. The
+        flag indicates whether some coordinates were supplied.
+
+        Note that region strings are 1-based, while *start* and *end* denote
+        an interval in python coordinates.
 
-        Note that regions are 1-based, while start,end are python coordinates.
         '''
         cdef int rtid
         cdef long long rstart
@@ -574,13 +569,13 @@ cdef class Samfile:
             try:
                 rstart = start
             except OverflowError:
-                raise ValueError( 'start out of range (%i)' % start )
+                raise ValueError('start out of range (%i)' % start)
 
         if end != None:
             try:
                 rend = end
             except OverflowError:
-                raise ValueError( 'end out of range (%i)' % end )
+                raise ValueError('end out of range (%i)' % end)
 
         if region:
             region = _forceStr(region)
@@ -613,12 +608,13 @@ cdef class Samfile:
         return 1, rtid, rstart, rend
 
     def reset(self):
-        '''reset file position to beginning of read section.'''
+        '''reset file position to beginning of file just after
+        the header.'''
         return self.seek(self.start_offset, 0)
 
     def seek(self, uint64_t offset, int where = 0):
         '''
-        move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`.
+        move file pointer to position *offset*, see :meth:`pysam.AlignmentFile.tell`.
         '''
 
         if not self._isOpen():
@@ -632,10 +628,10 @@ cdef class Samfile:
 
     def tell(self):
         '''
-        return current file position
+        return current file position.
         '''
         if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
+            raise ValueError("I/O operation on closed file")
         if not self.isbam:
             raise NotImplementedError("seek only available in bam files")
 
@@ -649,7 +645,7 @@ cdef class Samfile:
               tid=None,
               callback=None,
               until_eof=False,
-              reopen=True):
+              multiple_iterators=False):
         '''fetch aligned reads in a :term:`region` using 0-based indexing. The
         region is specified by :term:`reference`, *start* and
         *end*. Alternatively, a samtools :term:`region` string can be
@@ -664,14 +660,11 @@ cdef class Samfile:
         position will be returned in order as they are within the
         file. Using this option will also fetch unmapped reads.
 
-        If *reopen* is set to true, the iterator returned will receive
-        its own filehandle to the htsfile effectively opening its own
-        copy of the file. The default behaviour is to re-open in order
-        to safely work with multiple concurrent iterators on the same
-        file. Re-opening a htsfile creates some overhead, so when
-        using many calls to fetch() *reopen* can be set to False to
-        gain some speed. Also, the tell() method will only work if
-        *reopen* is set to False.
+        Set *multiple_iterators* to true if you will be using multiple
+        iterators on the same file at the same time. The iterator returned
+        will receive its own copy of a filehandle to the file effectively
+        re-opening the file. Re-opening a file creates some
+        overhead, so beware.
 
         If only *reference* is set, all reads aligned to *reference*
         will be fetched.
@@ -693,24 +686,28 @@ cdef class Samfile:
 
         # Turn of re-opening if htsfile is a stream
         if self.isstream:
-            reopen = False
+            multiple_iterators = False
 
         if self.isbam:
             if not until_eof and not self._hasIndex() and not self.isremote:
-                raise ValueError( "fetch called on bamfile without index" )
+                raise ValueError("fetch called on bamfile without index")
 
             if has_coord:
-                return IteratorRowRegion(self, rtid, rstart, rend, 
-                                         reopen=reopen)
+                return IteratorRowRegion(
+                    self, rtid, rstart, rend, 
+                    multiple_iterators=multiple_iterators)
             else:
                 if until_eof:
-                    return IteratorRowAll(self, reopen=reopen)
+                    return IteratorRowAll(self,
+                                          multiple_iterators=multiple_iterators)
                 else:
-                    # AH: check - reason why no reopen for AllRefs?
-                    return IteratorRowAllRefs(self) # , reopen=reopen )
+                    # AH: check - reason why no multiple_iterators for AllRefs?
+                    return IteratorRowAllRefs(self,
+                                              multiple_iterators=multiple_iterators)
         else:
             if has_coord:
-                raise ValueError ("fetching by region is not available for sam files")
+                raise ValueError(
+                    "fetching by region is not available for sam files")
 
             if callback:
                 raise NotImplementedError("callback not implemented yet")
@@ -723,19 +720,23 @@ cdef class Samfile:
             if self.header.n_targets == 0:
                 warnings.warn("fetch called for htsfile without header")
                 
-            return IteratorRowAll(self, reopen=reopen)
+            return IteratorRowAll(self,
+                                  multiple_iterators=multiple_iterators)
 
-    def head(self, n):
-        '''
-        return iterator over the first n alignments. 
+    def head(self, n, multiple_iterators=True):
+        '''return iterator over the first n alignments. 
 
         This is useful for inspecting the bam-file.
+
+        *multiple_iterators* is set to True by default in order to
+        avoid changing the current file position.
         '''
-        return IteratorRowHead(self, n)
+        return IteratorRowHead(self, n,
+                               multiple_iterators=multiple_iterators)
 
     def mate(self,
-             AlignedRead read):
-        '''return the mate of :class:`AlignedRead` *read*.
+             AlignedSegment read):
+        '''return the mate of :class:`AlignedSegment` *read*.
 
         Throws a ValueError if read is unpaired or the mate
         is unmapped.
@@ -756,22 +757,29 @@ cdef class Samfile:
         cdef uint32_t flag = read._delegate.core.flag
 
         if flag & BAM_FPAIRED == 0:
-            raise ValueError("read %s: is unpaired" % (read.qname))
+            raise ValueError("read %s: is unpaired" %
+                             (read.query_name))
         if flag & BAM_FMUNMAP != 0:
-            raise ValueError("mate %s: is unmapped" % (read.qname))
+            raise ValueError("mate %s: is unmapped" %
+                             (read.query_name))
 
         # xor flags to get the other mate
         cdef int x = BAM_FREAD1 + BAM_FREAD2
         flag = (flag ^ x) & x
 
-        # the following code is not using the C API and
-        # could thus be made much quicker
+        # Make sure to use a separate file to jump around
+        # to mate as otherwise the original file position
+        # will be lost
+        # The following code is not using the C API and
+        # could thus be made much quicker, for example
+        # by using tell and seek.
         for mate in self.fetch(
                 read._delegate.core.mpos,
                 read._delegate.core.mpos + 1,
-                tid=read._delegate.core.mtid):
+                tid=read._delegate.core.mtid,
+                multiple_iterators=True):
             if mate.flag & flag != 0 and \
-               mate.qname == read.qname:
+               mate.query_name == read.query_name:
                 break
         else:
             raise ValueError("mate not found")
@@ -792,10 +800,10 @@ cdef class Samfile:
         *end*. Alternatively, a samtools :term:`region` string can be
         supplied.
 
-        Note that a :term:`TAM` file does not allow random access. If
+        Note that a :term:`SAM` file does not allow random access. If
         *region* or *reference* are given, an exception is raised.
         '''
-        cdef AlignedRead read
+        cdef AlignedSegment read
         cdef long counter = 0
 
         if not self._isOpen():
@@ -816,20 +824,24 @@ cdef class Samfile:
                 end = None,
                 region = None,
                 **kwargs ):
-        '''
-        perform a :term:`pileup` within a :term:`region`. The region is specified by
-        :term:`reference`, *start* and *end* (using 0-based indexing).
-        Alternatively, a samtools *region* string can be supplied.
+        '''perform a :term:`pileup` within a :term:`region`. The region is
+        specified by :term:`reference`, *start* and *end* (using
+        0-based indexing).  Alternatively, a samtools *region* string
+        can be supplied.
 
-        Without *reference* or *region* all reads will be used for the pileup. The reads will be returned
-        ordered by :term:`reference` sequence, which will not necessarily be the order within the file.
+        Without *reference* or *region* all reads will be used for the
+        pileup. The reads will be returned ordered by
+        :term:`reference` sequence, which will not necessarily be the
+        order within the file.
 
-        The method returns an iterator of type :class:`pysam.IteratorColumn` unless
-        a *callback is provided. If a *callback* is given, the callback will be executed
-        for each column within the :term:`region`.
+        The method returns an iterator of type
+        :class:`pysam.IteratorColumn` unless a *callback is
+        provided. If a *callback* is given, the callback will be
+        executed for each column within the :term:`region`.
 
-        Note that :term:`SAM` formatted files do not allow random access.
-        In these files, if a *region* or *reference* are given an exception is raised.
+        Note that :term:`SAM` formatted files do not allow random
+        access.  In these files, if a *region* or *reference* are
+        given an exception is raised.
 
         Optional *kwargs* to the iterator:
 
@@ -839,27 +851,39 @@ cdef class Samfile:
 
            ``all``
               use all reads for pileup.
+
+           ``pass``
+              skip reads in which any of the following flags are set:
+              BAM_FUNMAP, BAM_FSECONDARY, BAM_FQCFAIL, BAM_FDUP
+
            ``samtools``
-              same filter and read processing as in :term:`csamtools` pileup
+              same filter and read processing as in :term:`csamtools`
+              pileup. This requires a *fastafile* to be given.
+
 
         fastafile
-           A :class:`FastaFile` object
+           A :class:`~pysam.FastaFile` object. This is required for
+           some of the steppers.
 
-         mask
+        mask
            Skip all reads with bits set in mask if mask=True.
 
-         max_depth
+        max_depth
            Maximum read depth permitted. The default limit is *8000*.
 
-         truncate
-           By default, the samtools pileup engine outputs all reads overlapping a region (see note below).
-           If truncate is True and a region is given, only output columns in the exact region
-           specificied.
+        truncate
+
+           By default, the samtools pileup engine outputs all reads
+           overlapping a region (see note below).  If truncate is True
+           and a region is given, only output columns in the exact
+           region specificied.
 
         .. note::
 
-            *all* reads which overlap the region are returned. The first base returned will be the
-            first base of the first read *not* necessarily the first base of the region used in the query.
+            *all* reads which overlap the region are returned. The
+            first base returned will be the first base of the first
+            read *not* necessarily the first base of the region used
+            in the query.
 
         '''
         cdef int rtid, rstart, rend, has_coord
@@ -888,7 +912,7 @@ cdef class Samfile:
 
     def close( self ):
         '''
-        closes the :class:`pysam.Samfile`.'''
+        closes the :class:`pysam.AlignmentFile`.'''
         if self.htsfile != NULL:
             hts_close(self.htsfile)
             hts_idx_destroy(self.index);
@@ -903,9 +927,9 @@ cdef class Samfile:
         if self.header != NULL:
             bam_hdr_destroy(self.header)
             
-    cpdef int write( self, AlignedRead read ) except -1:
+    cpdef int write(self, AlignedSegment read) except -1:
         '''
-        write a single :class:`pysam.AlignedRead` to disk.
+        write a single :class:`pysam.AlignedSegment` to disk.
 
         returns the number of bytes written.
         '''
@@ -951,8 +975,10 @@ cdef class Samfile:
             return tuple(t)
 
     property lengths:
-        """tuple of the lengths of the :term:`reference` sequences. The lengths are in the same order as
-        :attr:`pysam.Samfile.references`
+        """tuple of the lengths of the :term:`reference` sequences. The
+        lengths are in the same order as
+        :attr:`pysam.AlignmentFile.references`
+
         """
         def __get__(self):
             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
@@ -980,7 +1006,7 @@ cdef class Samfile:
         if not self._isOpen():
             raise ValueError("I/O operation on closed file")
         if not self.isbam:
-            raise AttributeError("Samfile.mapped only available in bam files")
+            raise AttributeError("AlignmentFile.mapped only available in bam files")
         if self.index == NULL:
             raise ValueError("mapping information not recorded in index "
                                  "or index not available")
@@ -1007,98 +1033,144 @@ cdef class Samfile:
             return hts_idx_get_n_no_coor(self.index)
 
     property text:
-        '''full contents of the :term:`sam file` header as a string.'''
+        '''full contents of the :term:`sam file` header as a string
+    
+        See :attr:`pysam.AlignmentFile.header` to get a parsed
+        representation of the header.
+
+        '''
         def __get__(self):
-            if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
+            if not self._isOpen():
+                raise ValueError( "I/O operation on closed file" )
             return from_string_and_size(self.header.text, self.header.l_text)
 
     property header:
-        '''header information within the :term:`sam file`. The records and fields are returned as
-        a two-level dictionary.
+        '''header information within the :term:`sam file`. The records and
+        fields are returned as a two-level dictionary. 
+
+        The first level contains the record (``HD``, ``SQ``, etc) and
+        the second level contains the fields (``VN``, ``LN``, etc).
+        
+        The parser is validating and will raise an AssertionError if
+        if encounters any record or field tags that are not part of
+        the SAM specification. Use the
+        :attr:`pysam.AlignmentFile.text` attribute to get the unparsed
+        header.
+
+        The parsing follows the SAM format specification with the
+        exception of the ``CL`` field. This option will consume the
+        rest of a header line irrespective of any additional fields.
+        This behaviour has been added to accommodate command line
+        options that contain characters that are not valid field
+        separators.
+
         '''
         def __get__(self):
-            if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
+            if not self._isOpen():
+                raise ValueError( "I/O operation on closed file" )
 
             result = {}
             
             if self.header.text != NULL:
-                # convert to python string (note: call self.text to create 0-terminated string)
+                # convert to python string (note: call self.text to
+                # create 0-terminated string)
                 t = self.text
                 for line in t.split("\n"):
                     if not line.strip(): continue
-                    assert line.startswith("@"), "header line without '@': '%s'" % line
+                    assert line.startswith("@"), \
+                        "header line without '@': '%s'" % line
                     fields = line[1:].split("\t")
                     record = fields[0]
-                    assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)
+                    assert record in VALID_HEADER_TYPES, \
+                        "header line with invalid type '%s': '%s'" % (record, line)
 
                     # treat comments
                     if record == "CO":
-                        if record not in result: result[record] = []
-                        result[record].append( "\t".join( fields[1:] ) )
+                        if record not in result:
+                            result[record] = []
+                        result[record].append("\t".join( fields[1:]))
                         continue
                     # the following is clumsy as generators do not work?
                     x = {}
-                    for field in fields[1:]:
+
+                    for idx, field in enumerate(fields[1:]):
                         if ":" not in field: 
                             raise ValueError("malformatted header: no ':' in field" )
-                        key, value = field.split(":",1)
+                        key, value = field.split(":", 1)
+                        if key in ("CL",):
+                            # special treatment for command line
+                            # statements (CL). These might contain
+                            # characters that are non-conformant with
+                            # the valid field separators in the SAM
+                            # header. Thus, in contravention to the
+                            # SAM API, consume the rest of the line.
+                            key, value = "\t".join(fields[idx+1:]).split(":", 1)
+                            x[key] = VALID_HEADER_FIELDS[record][key](value)
+                            break
+
                         # uppercase keys must be valid
-                        # lowercase are permitted for user fields
                         if key in VALID_HEADER_FIELDS[record]:
                             x[key] = VALID_HEADER_FIELDS[record][key](value)
+                        # lowercase are permitted for user fields
                         elif not key.isupper():
                             x[key] = value
                         else:
-                            raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) )
+                            raise ValueError(
+                                "unknown field code '%s' in record '%s'" %
+                                (key, record))
 
                     if VALID_HEADER_TYPES[record] == dict:
                         if record in result:
-                            raise ValueError( "multiple '%s' lines are not permitted" % record )
+                            raise ValueError(
+                                "multiple '%s' lines are not permitted" % record)
+
                         result[record] = x
                     elif VALID_HEADER_TYPES[record] == list:
                         if record not in result: result[record] = []
-                        result[record].append( x )
+                        result[record].append(x)
 
                 # if there are no SQ lines in the header, add the reference names
                 # from the information in the bam file.
-                # Background: c-samtools keeps the textual part of the header separate from
-                # the list of reference names and lengths. Thus, if a header contains only 
-                # SQ lines, the SQ information is not part of the textual header and thus
-                # are missing from the output. See issue 84.
+                #
+                # Background: c-samtools keeps the textual part of the
+                # header separate from the list of reference names and
+                # lengths. Thus, if a header contains only SQ lines,
+                # the SQ information is not part of the textual header
+                # and thus are missing from the output. See issue 84.
                 if "SQ" not in result:
                     sq = []
-                    for ref, length in zip( self.references, self.lengths ):
-                        sq.append( {'LN': length, 'SN': ref } )
+                    for ref, length in zip(self.references, self.lengths):
+                        sq.append({'LN': length, 'SN': ref })
                     result["SQ"] = sq
 
             return result
 
-    def _buildLine( self, fields, record ):
+    def _buildLine(self, fields, record):
         '''build a header line from *fields* dictionary for *record*'''
 
         # TODO: add checking for field and sort order
-        line = ["@%s" % record ]
+        line = ["@%s" % record]
         # comment
         if record == "CO":
-            line.append( fields )
+            line.append(fields)
         # user tags
         elif record.islower():
             for key in sorted(fields):
-                line.append( "%s:%s" % (key, str(fields[key])))
+                line.append("%s:%s" % (key, str(fields[key])))
         # defined tags
         else:
             # write fields of the specification
             for key in VALID_HEADER_ORDER[record]:
                 if key in fields:
-                    line.append( "%s:%s" % (key, str(fields[key])))
+                    line.append("%s:%s" % (key, str(fields[key])))
             # write user fields
             for key in fields:
                 if not key.isupper():
-                    line.append( "%s:%s" % (key, str(fields[key])))
+                    line.append("%s:%s" % (key, str(fields[key])))
 
-        return "\t".join( line )
+        return "\t".join(line)
 
-    cdef bam_hdr_t * _buildHeader( self, new_header ):
+    cdef bam_hdr_t * _buildHeader(self, new_header):
         '''return a new header built from a dictionary in *new_header*.
 
         This method inserts the text field, target_name and target_len.
@@ -1155,14 +1227,16 @@ cdef class Samfile:
                     raise KeyError( "incomplete sequence information in '%s'" % str(fields))
 
             dest.n_targets = len(seqs)
-            dest.target_name = <char**>calloc( dest.n_targets, sizeof(char*) )
-            dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )
+            dest.target_name = <char**>calloc(dest.n_targets, sizeof(char*))
+            dest.target_len = <uint32_t*>calloc(dest.n_targets, sizeof(uint32_t))
 
             for x from 0 <= x < dest.n_targets:
                 seqname, seqlen = seqs[x]
-                dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
+                dest.target_name[x] = <char*>calloc(
+                    len(seqname) + 1, sizeof(char))
                 bseqname = seqname.encode('ascii')
-                strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
+                strncpy(dest.target_name[x], bseqname,
+                        len(seqname) + 1)
                 dest.target_len[x] = seqlen
 
         return dest
@@ -1172,7 +1246,7 @@ cdef class Samfile:
     ###############################################################
     ## file-object like iterator access
     ## note: concurrent access will cause errors (see IteratorRow
-    ## and reopen)
+    ## and multiple_iterators)
     ## Possible solutions: deprecate or open new file handle
     ###############################################################
     def __iter__(self):
@@ -1189,9 +1263,8 @@ cdef class Samfile:
 
     cdef int cnext(self):
         '''
-        cversion of iterator. Used by :class:`pysam.Samfile.IteratorColumn`.
+        cversion of iterator. Used by :class:`pysam.AlignmentFile.IteratorColumn`.
         '''
-        cdef int ret
         return sam_read1(self.htsfile,
                          self.header,
                          self.b)
@@ -1200,10 +1273,11 @@ cdef class Samfile:
         """
         python version of next().
         """
-        cdef int ret
-        ret = sam_read1(self.htsfile, self.header, self.b)
+        cdef int ret = self.cnext()
         if (ret >= 0):
-            return makeAlignedRead(self.b)
+            return makeAlignedSegment(self.b)
+        elif (ret == -2):
+            raise IOError('truncated file')
         else:
             raise StopIteration
 
@@ -1225,18 +1299,18 @@ cdef class IteratorRow:
     :class:`pysam.IteratorRowAllRefs`
         iterate over all reads in all reference sequences.
 
-    The method :meth:`Samfile.fetch` returns an IteratorRow.
+    The method :meth:`AlignmentFile.fetch` returns an IteratorRow.
 
     .. note::
         It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
+        explicitely. It is returned as a result of call to a :meth:`AlignmentFile.fetch`.
 
     '''
 
-    def __init__(self, Samfile samfile, int reopen=True):
+    def __init__(self, AlignmentFile samfile, int multiple_iterators=False):
         
         if not samfile._isOpen():
-            raise ValueError( "I/O operation on closed file" )
+            raise ValueError("I/O operation on closed file")
 
         # makes sure that samfile stays alive as long as the
         # iterator is alive
@@ -1244,15 +1318,18 @@ cdef class IteratorRow:
 
         # reopen the file - note that this makes the iterator
         # slow and causes pileup to slow down significantly.
-        if reopen:
+        if multiple_iterators:
             self.htsfile = hts_open(samfile._filename, 'r')
             assert self.htsfile != NULL
             # read header - required for accurate positioning
-            sam_hdr_read(self.htsfile)
+            # could a tell/seek work?
+            self.header = sam_hdr_read(self.htsfile)
+            assert self.header != NULL
             self.owns_samfile = True
         else:
             self.htsfile = self.samfile.htsfile
             self.owns_samfile = False
+            self.header = self.samfile.header
 
         self.retval = 0
 
@@ -1262,37 +1339,26 @@ cdef class IteratorRow:
         bam_destroy1(self.b)
         if self.owns_samfile:
             hts_close(self.htsfile)
+            bam_hdr_destroy(self.header)
+
 
 cdef class IteratorRowRegion(IteratorRow):
-    """*(Samfile samfile, int tid, int beg, int end, int reopen = True )*
+    """*(AlignmentFile samfile, int tid, int beg, int end,
+    int multiple_iterators=False)*
 
     iterate over mapped reads in a region.
 
-    By default, the file is re-openend to avoid conflicts between
-    multiple iterators working on the same file. Set *reopen* = False
-    to not re-open *samfile*.
-
-    The samtools iterators assume that the file
-    position between iterations do not change.
-    As a consequence, no two iterators can work
-    on the same file. To permit this, each iterator
-    creates its own file handle by re-opening the
-    file.
-
-    Note that the index will be shared between
-    samfile and the iterator.
-
     .. note::
         It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
+        explicitely. It is returned as a result of call to a :meth:`AlignmentFile.fetch`.
 
     """
 
-    def __init__(self, Samfile samfile,
+    def __init__(self, AlignmentFile samfile,
                  int tid, int beg, int end,
-                 int reopen=True):
+                 int multiple_iterators=False):
 
-        IteratorRow.__init__(self, samfile, reopen=reopen)
+        IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
 
         if not samfile._hasIndex():
             raise ValueError( "no index available for iteration" )
@@ -1306,7 +1372,7 @@ cdef class IteratorRowRegion(IteratorRow):
     def __iter__(self):
         return self
 
-    cdef bam1_t * getCurrent( self ):
+    cdef bam1_t * getCurrent(self):
         return self.b
 
     cdef int cnext(self):
@@ -1322,30 +1388,26 @@ cdef class IteratorRowRegion(IteratorRow):
         self.cnext()
         if self.retval < 0:
             raise StopIteration
-        return makeAlignedRead(self.b)
+        return makeAlignedSegment(self.b)
 
     def __dealloc__(self):
         hts_itr_destroy(self.iter)
 
 cdef class IteratorRowHead(IteratorRow):
-    """*(Samfile samfile, n, int reopen = True)*
+    """*(AlignmentFile samfile, n, int multiple_iterators=False)*
 
     iterate over first n reads in *samfile*
 
-    By default, the file is re-openend to avoid conflicts between
-    multiple iterators working on the same file. Set *reopen* = False
-    to not re-open *samfile*.
-
     .. note::
         It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.head`.
+        explicitly. It is returned as a result of call to a :meth:`AlignmentFile.head`.
         
 
     """
 
-    def __init__(self, Samfile samfile, int n, int reopen=True):
+    def __init__(self, AlignmentFile samfile, int n, int multiple_iterators=False):
 
-        IteratorRow.__init__(self, samfile, reopen=reopen)
+        IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
 
         self.max_rows = n
         self.current_row = 0
@@ -1370,35 +1432,31 @@ cdef class IteratorRowHead(IteratorRow):
         if self.current_row >= self.max_rows:
             raise StopIteration
 
-        cdef int ret
-        ret = sam_read1(self.htsfile,
-                        self.samfile.header, self.b)
+        cdef int ret = self.cnext()
         if (ret >= 0):
             self.current_row += 1
-            return makeAlignedRead( self.b )
+            return makeAlignedSegment( self.b )
+        elif (ret == -2):
+            raise IOError('truncated file')
         else:
             raise StopIteration
 
 
 cdef class IteratorRowAll(IteratorRow):
-    """*(Samfile samfile, int reopen = True)*
+    """*(AlignmentFile samfile, int multiple_iterators=False)*
 
     iterate over all reads in *samfile*
 
-    By default, the file is re-openend to avoid conflicts between
-    multiple iterators working on the same file. Set *reopen* = False
-    to not re-open *samfile*.
-
     .. note::
         It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
+        explicitly. It is returned as a result of call to a :meth:`AlignmentFile.fetch`.
         
 
     """
 
-    def __init__(self, Samfile samfile, int reopen = True):
+    def __init__(self, AlignmentFile samfile, int multiple_iterators=False):
 
-        IteratorRow.__init__(self, samfile, reopen=reopen)
+        IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
 
     def __iter__(self):
         return self
@@ -1417,12 +1475,11 @@ cdef class IteratorRowAll(IteratorRow):
 
         pyrex uses this non-standard name instead of next()
         """
-        cdef int ret
-        ret = sam_read1(self.htsfile,
-                        self.samfile.header,
-                        self.b)
+        cdef int ret = self.cnext()
         if (ret >= 0):
-            return makeAlignedRead(self.b)
+            return makeAlignedSegment(self.b)
+        elif (ret == -2):
+            raise IOError('truncated file')
         else:
             raise StopIteration
 
@@ -1432,12 +1489,13 @@ cdef class IteratorRowAllRefs(IteratorRow):
 
     .. note::
         It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
+        explicitely. It is returned as a result of call to a :meth:`AlignmentFile.fetch`.
     """
 
-    def __init__(self, Samfile samfile, reopen=True):
+    def __init__(self, AlignmentFile samfile, multiple_iterators=False):
 
-        IteratorRow.__init__(self, samfile, reopen=reopen)
+        IteratorRow.__init__(self, samfile,
+                             multiple_iterators=multiple_iterators)
 
         if not samfile._hasIndex():
             raise ValueError("no index available for fetch")
@@ -1445,10 +1503,20 @@ cdef class IteratorRowAllRefs(IteratorRow):
         self.tid = -1
 
     def nextiter(self):
+        # get a new iterator for a chromosome. The file
+        # will not be re-opened.
         self.rowiter = IteratorRowRegion(self.samfile,
                                          self.tid,
                                          0,
                                          1<<29)
+        # set htsfile and header of the rowiter
+        # to the values in this iterator to reflect multiple_iterators
+        self.rowiter.htsfile = self.htsfile
+        self.rowiter.header = self.header
+
+        # make sure the iterator understand that IteratorRowAllRefs
+        # has ownership
+        self.rowiter.owns_samfile = False
 
     def __iter__(self):
         return self
@@ -1469,8 +1537,8 @@ cdef class IteratorRowAllRefs(IteratorRow):
             self.rowiter.cnext()
 
             # If current iterator is not exhausted, return aligned read
-            if self.rowiter.retval>0:
-                return makeAlignedRead(self.rowiter.b)
+            if self.rowiter.retval > 0:
+                return makeAlignedSegment(self.rowiter.b)
 
             self.tid += 1
 
@@ -1482,18 +1550,18 @@ cdef class IteratorRowAllRefs(IteratorRow):
 
 
 cdef class IteratorRowSelection(IteratorRow):
-    """*(Samfile samfile)*
+    """*(AlignmentFile samfile)*
 
     iterate over reads in *samfile* at a given list of file positions.
 
     .. note::
         It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
+        explicitely. It is returned as a result of call to a :meth:`AlignmentFile.fetch`.
     """
 
-    def __init__(self, Samfile samfile, positions, int reopen=True):
+    def __init__(self, AlignmentFile samfile, positions, int multiple_iterators=True):
 
-        IteratorRow.__init__(self, samfile, reopen=reopen)
+        IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
 
         self.positions = positions
         self.current_pos = 0
@@ -1525,15 +1593,16 @@ cdef class IteratorRowSelection(IteratorRow):
 
         pyrex uses this non-standard name instead of next()
         """
-
         cdef int ret = self.cnext()
         if (ret >= 0):
-            return makeAlignedRead(self.b)
+            return makeAlignedSegment(self.b)
+        elif (ret == -2):
+            raise IOError('truncated file')
         else:
             raise StopIteration
 
 
-cdef int __advance_all(void *data, bam1_t *b):
+cdef int __advance_nofilter(void *data, bam1_t *b):
     '''advance without any read filtering.
     '''
     cdef __iterdata * d
@@ -1541,6 +1610,23 @@ cdef int __advance_all(void *data, bam1_t *b):
     return sam_itr_next(d.htsfile, d.iter, b)
 
 
+cdef int __advance_all(void *data, bam1_t *b):
+    '''only use reads for pileup passing basic
+    filters:
+
+    BAM_FUNMAP, BAM_FSECONDARY, BAM_FQCFAIL, BAM_FDUP
+    '''
+
+    cdef __iterdata * d
+    cdef mask = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP
+    d = <__iterdata*>data
+    cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
+    while ret >= 0 and b.core.flag & mask:
+        ret = sam_itr_next(d.htsfile, d.iter, b)
+
+    return ret
+
+
 cdef int __advance_snpcalls(void * data, bam1_t * b):
     '''advance using same filter and read processing as in
     the samtools pileup.
@@ -1579,7 +1665,6 @@ cdef int __advance_snpcalls(void * data, bam1_t * b):
                 (d.header.target_name[d.tid],
                  d.tid))
 
-
     while ret >= 0:
         skip = 0
 
@@ -1598,7 +1683,8 @@ cdef int __advance_snpcalls(void * data, bam1_t * b):
         elif b.core.flag & 1 and not b.core.flag & 2:
             skip = 1
 
-        if not skip: break
+        if not skip:
+            break
         # additional filters
 
         ret = sam_itr_next(d.htsfile, d.iter, b)
@@ -1615,18 +1701,19 @@ cdef class IteratorColumn:
     This might cause some unexpected behavious. For example,
     consider the conversion to a list::
 
-       f = Samfile("file.bam", "rb")
+       f = AlignmentFile("file.bam", "rb")
        result = list( f.pileup() )
 
     Here, ``result`` will contain ``n`` objects of type
-    :class:`PileupProxy` for ``n`` columns, but each object in
+    :class:`PileupColumn` for ``n`` columns, but each object in
     ``result`` will contain the same information.
 
     The desired behaviour can be achieved by list comprehension::
 
        result = [ x.pileups() for x in f.pileup() ]
 
-    ``result`` will be a list of ``n`` lists of objects of type :class:`PileupRead`.
+    ``result`` will be a list of ``n`` lists of objects of type
+    :class:`PileupRead`.
 
     If the iterator is associated with a :class:`Fastafile` using the
     :meth:`addReference` method, then the iterator will export the
@@ -1655,7 +1742,7 @@ cdef class IteratorColumn:
 
     '''
 
-    def __cinit__( self, Samfile samfile, **kwargs ):
+    def __cinit__( self, AlignmentFile samfile, **kwargs ):
         self.samfile = samfile
         # TODO
         # self.mask = kwargs.get("mask", BAM_DEF_MASK )
@@ -1715,25 +1802,33 @@ cdef class IteratorColumn:
                             int tid,
                             int start,
                             int end,
-                            int reopen = 0 ):
+                            int multiple_iterators = 0 ):
         '''setup the iterator structure'''
 
-        self.iter = IteratorRowRegion(self.samfile, tid, start, end, reopen)
+        self.iter = IteratorRowRegion(self.samfile, tid, start, end, multiple_iterators)
         self.iterdata.htsfile = self.samfile.htsfile
         self.iterdata.iter = self.iter.iter
         self.iterdata.seq = NULL
         self.iterdata.tid = -1
         self.iterdata.header = self.samfile.header
 
-        if self.fastafile != None:
+        if self.fastafile is not None:
             self.iterdata.fastafile = self.fastafile.fastafile
         else:
             self.iterdata.fastafile = NULL
 
-        if self.stepper == None or self.stepper == "all":
+        # Free any previously allocated memory before reassigning
+        # pileup_iter
+        self._free_pileup_iter()
+
+        if self.stepper is None or self.stepper == "all":
             self.pileup_iter = bam_plp_init(
                 <bam_plp_auto_f>&__advance_all,
                 &self.iterdata)
+        elif self.stepper == "nofilter":
+            self.pileup_iter = bam_plp_init(
+                <bam_plp_auto_f>&__advance_nofilter,
+                &self.iterdata)
         elif self.stepper == "samtools":
             self.pileup_iter = bam_plp_init(
                 <bam_plp_auto_f>&__advance_snpcalls,
@@ -1753,7 +1848,7 @@ cdef class IteratorColumn:
         This permits using the iterator multiple times without
         having to incur the full set-up costs.
         '''
-        self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen = 0 )
+        self.iter = IteratorRowRegion( self.samfile, tid, start, end, multiple_iterators = 0 )
         self.iterdata.iter = self.iter.iter
 
         # invalidate sequence if different tid
@@ -1765,14 +1860,22 @@ cdef class IteratorColumn:
         # self.pileup_iter = bam_plp_init( &__advancepileup, &self.iterdata )
         bam_plp_reset(self.pileup_iter)
 
-    def __dealloc__(self):
-        # reset in order to avoid memory leak messages for iterators 
-        # that have not been fully consumed
+    cdef _free_pileup_iter(self):
+        '''free the memory alloc'd by bam_plp_init.
+
+        This is needed before setupIteratorData allocates
+        another pileup_iter, or else memory will be lost.
+        '''
         if self.pileup_iter != <bam_plp_t>NULL:
             bam_plp_reset(self.pileup_iter)
             bam_plp_destroy(self.pileup_iter)
             self.pileup_iter = <bam_plp_t>NULL
-            self.plp = <bam_pileup1_t*>NULL
+
+    def __dealloc__(self):
+        # reset in order to avoid memory leak messages for iterators
+        # that have not been fully consumed
+        self._free_pileup_iter()
+        self.plp = <bam_pileup1_t*>NULL
 
         if self.iterdata.seq != NULL:
             free(self.iterdata.seq)
@@ -1782,7 +1885,7 @@ cdef class IteratorColumn:
 cdef class IteratorColumnRegion(IteratorColumn):
     '''iterates over a region only.
     '''
-    def __cinit__(self, Samfile samfile,
+    def __cinit__(self, AlignmentFile samfile,
                   int tid = 0,
                   int start = 0,
                   int end = max_pos,
@@ -1811,7 +1914,7 @@ cdef class IteratorColumnRegion(IteratorColumn):
                 if self.start > self.pos: continue
                 if self.pos >= self.end: raise StopIteration
 
-            return makePileupProxy(&self.plp,
+            return makePileupColumn(&self.plp,
                                    self.tid,
                                    self.pos,
                                    self.n_plp)
@@ -1822,7 +1925,7 @@ cdef class IteratorColumnAllRefs(IteratorColumn):
     """
 
     def __cinit__(self,
-                  Samfile samfile,
+                  AlignmentFile samfile,
                   **kwargs):
 
         # no iteration over empty files
@@ -1844,7 +1947,7 @@ cdef class IteratorColumnAllRefs(IteratorColumn):
 
             # return result, if within same reference
             if self.plp != NULL:
-                return makePileupProxy(&self.plp,
+                return makePileupColumn(&self.plp,
                                        self.tid,
                                        self.pos,
                                        self.n_plp)
@@ -1856,10 +1959,8 @@ cdef class IteratorColumnAllRefs(IteratorColumn):
             else:
                 raise StopIteration
 
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-cdef inline int32_t query_start(bam1_t *src) except -1:
+
+cdef inline int32_t _getQueryStart(bam1_t *src) except -1:
     cdef uint32_t * cigar_p
     cdef uint32_t k, op
     cdef uint32_t start_offset = 0
@@ -1879,10 +1980,8 @@ cdef inline int32_t query_start(bam1_t *src) except -1:
 
     return start_offset
 
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-cdef inline int32_t query_end(bam1_t *src) except -1:
+
+cdef inline int32_t _getQueryEnd(bam1_t *src) except -1:
     cdef uint32_t * cigar_p
     cdef uint32_t k, op
     cdef uint32_t end_offset = src.core.l_qseq
@@ -1907,7 +2006,8 @@ cdef inline int32_t query_end(bam1_t *src) except -1:
     return end_offset
 
 
-cdef inline object get_seq_range(bam1_t *src, uint32_t start, uint32_t end):
+cdef inline object _getSequenceRange(bam1_t *src,
+                                     uint32_t start, uint32_t end):
     cdef uint8_t * p
     cdef uint32_t k
     cdef char * s
@@ -1924,28 +2024,49 @@ cdef inline object get_seq_range(bam1_t *src, uint32_t start, uint32_t end):
         # note: do not use string literal as it will be a python string
         s[k-start] = seq_nt16_str[p[k/2] >> 4 * (1 - k%2) & 0xf]
 
-    return seq
+    return _charptr_to_str(seq)
+
 
+cdef inline object _getQualitiesRange(bam1_t *src,
+                                      uint32_t start, 
+                                      uint32_t end):
+    '''return an array of quality values.'''
 
-cdef inline object get_qual_range(bam1_t *src, uint32_t start, uint32_t end):
     cdef uint8_t * p
     cdef uint32_t k
-    cdef char * q
 
     p = pysam_bam_get_qual(src)
     if p[0] == 0xff:
         return None
 
-    qual = PyBytes_FromStringAndSize(NULL, end - start)
-    q    = <char*>qual
+    # 'B': unsigned char
+    cdef array.array result = array.array('B', [0])
+    array.resize(result, end - start)
 
-    for k from start <= k < end:
-        ## equivalent to t[i] + 33 (see bam.c)
-        q[k-start] = p[k] + 33
+    # copy data
+    memcpy(result.data.as_voidptr, <void*>&p[start], end - start) 
+
+    return result
 
-    return qual
 
-cdef inline uint8_t get_type_code(value, value_type = None):
+def toQualityString(qualities):
+    '''convert a list of quality score to the string
+    representation used in the SAM format.'''
+    if qualities is None:
+        return None
+    return "".join([chr(x+33) for x in qualities])
+    
+
+def fromQualityString(quality_string):
+    '''return a list of quality scores from the
+    stringn representation of quality scores used
+    in the SAM format.'''
+    if quality_string is None:
+        return None
+    return array.array('B', [ord(x)-33 for x in quality_string])
+
+
+cdef inline uint8_t _getTypeCode(value, value_type = None):
     '''guess type code for a *value*. If *value_type* is None,
     the type code will be inferred based on the Python type of
     *value*'''
@@ -1981,31 +2102,41 @@ cdef inline convert_python_tag(pytag, value, fmts, args):
     if t is tuple or t is list:
         # binary tags - treat separately
         pytype = 'B'
-        # get data type - first value determines type
+        # get data type - first value determines type. If there is a
+        # mix of types, the result is undefined.
         if type(value[0]) is float:
             datafmt, datatype = "f", "f"
         else:
             mi, ma = min(value), max(value)
-            absmax = max( abs(mi), abs(ma) )
             # signed ints
             if mi < 0: 
-                if mi >= -127: datafmt, datatype = "b", 'c'
-                elif mi >= -32767: datafmt, datatype = "h", 's'
-                elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
+                if mi >= -128 and ma < 128:
+                    datafmt, datatype = "b", 'c'
+                elif mi >= -32768 and ma < 32768:
+                    datafmt, datatype = "h", 's'
+                elif mi < -2147483648 or ma >= 2147483648:
+                    raise ValueError(
+                        "at least one signed integer out of range of "
+                        "BAM/SAM specification")
                 else: datafmt, datatype = "i", 'i'
 
             # unsigned ints
             else:
-                if absmax <= 255: datafmt, datatype = "B", 'C'
-                elif absmax <= 65535: datafmt, datatype = "H", 'S'
-                elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                else: datafmt, datatype = "I", 'I'
+                if ma < 256:
+                    datafmt, datatype = "B", 'C'
+                elif ma < 65536:
+                    datafmt, datatype = "H", 'S'
+                elif ma >= 4294967296:
+                    raise ValueError(
+                        "at least one integer out of range of BAM/SAM specification")
+                else:
+                    datafmt, datatype = "I", 'I'
 
         datafmt = "2sccI%i%s" % (len(value), datafmt)
-        args.extend( [pytag[:2], 
-                      pytype.encode('ascii'),
-                      datatype.encode('ascii'),
-                      len(value)] + list(value) )
+        args.extend([pytag[:2], 
+                     pytype.encode('ascii'),
+                     datatype.encode('ascii'),
+                     len(value)] + list(value))
         fmts.append( datafmt )
         return
 
@@ -2033,19 +2164,17 @@ cdef inline convert_python_tag(pytag, value, fmts, args):
         else:
             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
 
-    args.extend( [pytag[:2],
-                  pytype.encode('ascii'),
-                  value ] )
+    args.extend([pytag[:2],
+                 pytype.encode('ascii'),
+                 value])
 
-    fmts.append( fmt )
+    fmts.append(fmt)
     
 ###########################################################
 ###########################################################
 ###########################################################
-cdef class AlignedRead:
-    '''
-    Class representing an aligned read. See the SAM format specification for
-    the meaning of fields (http://samtools.sourceforge.net/).
+cdef class AlignedSegment:
+    '''Class representing an aligned segment. 
 
     This class stores a handle to the samtools C-structure representing
     an aligned read. Member read access is forwarded to the C-structure
@@ -2061,22 +2190,15 @@ cdef class AlignedRead:
     One issue to look out for is that the sequence should always
     be set *before* the quality scores. Setting the sequence will
     also erase any quality scores that were set previously.
-
-    In Python 3, the fields containing sequence and quality
-    (seq, query, qual and qqual) data are of type bytes. Other
-    string data, such as the qname field and strings in the
-    tags tuple, is represented as unicode strings. On assignment,
-    both bytes and unicode objects are allowed, but unicode strings
-    must contain only ASCII characters.
     '''
 
     # Now only called when instances are created from Python
     def __init__(self):
         # see bam_init1
-        self._delegate = <bam1_t*>calloc( 1, sizeof( bam1_t) )
-        # allocate some memory
-        # If size is 0, calloc does not return a pointer that can be passed to free()
-        # so allocate 40 bytes for a new read
+        self._delegate = <bam1_t*>calloc(1, sizeof(bam1_t))
+        # allocate some memory. If size is 0, calloc does not return a
+        # pointer that can be passed to free() so allocate 40 bytes
+        # for a new read
         self._delegate.m_data = 40
         self._delegate.data = <uint8_t *>calloc(
             self._delegate.m_data, 1)
@@ -2090,33 +2212,27 @@ cdef class AlignedRead:
 
         The representation is an approximate :term:`sam` format.
 
-        An aligned read might not be associated with a :term:`Samfile`.
+        An aligned read might not be associated with a :term:`AlignmentFile`.
         As a result :term:`tid` is shown instead of the reference name.
 
         Similarly, the tags field is returned in its parsed state.
         """
         # sam-parsing is done in sam.c/bam_format1_core which
         # requires a valid header.
-        if sys.version_info[0] < 3:
-            seq = self.seq
-            qual = self.qual
-        else:
-            seq = self.seq.decode('ascii')
-            qual = self.qual.decode('ascii')
-        return "\t".join(map(str, (self.qname,
+        return "\t".join(map(str, (self.query_name,
                                    self.flag,
-                                   self.rname,
-                                   self.pos,
-                                   self.mapq,
-                                   self.cigar,
-                                   self.mrnm,
-                                   self.mpos,
-                                   self.rlen,
-                                   seq,
-                                   qual,
-                                   self.tags )))
-
-    def compare(self, AlignedRead other):
+                                   self.reference_id,
+                                   self.reference_start,
+                                   self.mapping_quality,
+                                   self.cigarstring,
+                                   self.next_reference_id,
+                                   self.next_reference_start,
+                                   self.query_alignment_length,
+                                   self.query_sequence,
+                                   self.query_qualities,
+                                   self.tags)))
+
+    def compare(self, AlignedSegment other):
         '''return -1,0,1, if contents in this are binary <,=,> to *other*'''
 
         cdef int retval, x
@@ -2141,89 +2257,25 @@ cdef class AlignedRead:
 
         retval = memcmp(&t.core, &o.core, sizeof(bam1_core_t))
 
-        if retval: return retval
-        retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)
-        if retval: return retval
+        if retval:
+            return retval
+        # cmp(t.l_data, o.l_data)
+        retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) 
+        if retval:
+            return retval
         return memcmp(t.data, o.data, t.l_data)
 
     # Disabled so long as __cmp__ is a special method
     def __hash__(self):
         return _Py_HashPointer(<void *>self)
 
-    def _convert_python_tag(self, pytag, value, fmts, args):
-
-        if not type(pytag) is bytes:
-            pytag = pytag.encode('ascii')
-        t = type(value)
-
-        if t is tuple or t is list:
-            # binary tags - treat separately
-            pytype = 'B'
-            # get data type - first value determines type
-            if type(value[0]) is float:
-                datafmt, datatype = "f", "f"
-            else:
-                mi, ma = min(value), max(value)
-                absmax = max( abs(mi), abs(ma) )
-                # signed ints
-                if mi < 0: 
-                    if mi >= -127: datafmt, datatype = "b", 'c'
-                    elif mi >= -32767: datafmt, datatype = "h", 's'
-                    elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                    else: datafmt, datatype = "i", 'i'
-
-                # unsigned ints
-                else:
-                    if absmax <= 255: datafmt, datatype = "B", 'C'
-                    elif absmax <= 65535: datafmt, datatype = "H", 'S'
-                    elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                    else: datafmt, datatype = "I", 'I'
-                    
-            datafmt = "2sccI%i%s" % (len(value), datafmt)
-            args.extend( [pytag[:2], 
-                          pytype.encode('ascii'),
-                          datatype.encode('ascii'),
-                          len(value)] + list(value) )
-            fmts.append( datafmt )
-            return
-
-        if t is float:
-            fmt, pytype = "2scf", 'f'
-        elif t is int:
-            # negative values
-            if value < 0:
-                if value >= -127: fmt, pytype = "2scb", 'c'
-                elif value >= -32767: fmt, pytype = "2sch", 's'
-                elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                else: fmt, pytype = "2sci", 'i'
-            # positive values
-            else:
-                if value <= 255: fmt, pytype = "2scB", 'C'
-                elif value <= 65535: fmt, pytype = "2scH", 'S'
-                elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                else: fmt, pytype = "2scI", 'I'
-        else:
-            # Note: hex strings (H) are not supported yet
-            if t is not bytes:
-                value = value.encode('ascii')
-            if len(value) == 1:
-                fmt, pytype = "2scc", 'A'
-            else:
-                fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
-
-        args.extend( [pytag[:2],
-                      pytype.encode('ascii'),
-                      value ] )
-        
-        fmts.append( fmt )
-
 
     #######################################################################
     #######################################################################
-    ## Basic properties
+    ## Basic attributes in order of appearance in SAM format
     #######################################################################
-    property qname:
-        """the query name (None if not present)"""
+    property query_name:
+        """the query template name (None if not present)"""
         def __get__(self):
             cdef bam1_t * src
             src = self._delegate
@@ -2231,15 +2283,16 @@ cdef class AlignedRead:
                 return None
             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
 
-        def __set__(self, qname ):
-            if qname == None or len(qname) == 0: return
+        def __set__(self, qname):
+            if qname is None or len(qname) == 0:
+                return
             qname = _forceBytes(qname)
             cdef bam1_t * src
             cdef int l
             cdef char * p
 
             src = self._delegate
-            p = pysam_bam_get_qname( src )
+            p = pysam_bam_get_qname(src)
 
             # the qname is \0 terminated
             l = len(qname) + 1
@@ -2257,106 +2310,56 @@ cdef class AlignedRead:
 
             strncpy(p, qname, l)
 
-    property cigar:
-        """the :term:`cigar` alignment. The alignment
-        is returned as a list of tuples of (operation, length). 
-
-        If the alignment is not present, an empty list is
-        returned.
-
-        The operations are:
+    property flag:
+        """properties flag"""
+        def __get__(self):
+            return pysam_get_flag(self._delegate)
+        def __set__(self, flag):
+            pysam_set_flag(self._delegate, flag)
 
-        +-----+--------------+-----+
-        |M    |BAM_CMATCH    |0    |
-        +-----+--------------+-----+
-        |I    |BAM_CINS      |1    |
-        +-----+--------------+-----+
-        |D    |BAM_CDEL      |2    |
-        +-----+--------------+-----+
-        |N    |BAM_CREF_SKIP |3    |
-        +-----+--------------+-----+
-        |S    |BAM_CSOFT_CLIP|4    |
-        +-----+--------------+-----+
-        |H    |BAM_CHARD_CLIP|5    |
-        +-----+--------------+-----+
-        |P    |BAM_CPAD      |6    |
-        +-----+--------------+-----+
-        |=    |BAM_CEQUAL    |7    |
-        +-----+--------------+-----+
-        |X    |BAM_CDIFF     |8    |
-        +-----+--------------+-----+
+    property reference_id:
+        """:term:`reference` ID
 
         .. note::
-            The output is a list of (operation, length) tuples, such as
-            ``[(0, 30)]``.
-            This is different from the SAM specification and
-            the :attr:`cigarstring` property, which uses a
-            (length, operation) order, for example: ``30M``.
 
-        To unset the cigar property, assign an empty list
-        or None.
-        """
-        def __get__(self):
-            cdef uint32_t * cigar_p
-            cdef bam1_t * src
-            cdef op, l, cigar
-            cdef int k
-            cigar = []
+            This field contains the index of the reference sequence in
+            the sequence dictionary. To obtain the name of the
+            reference sequence, use
+            :meth:`pysam.AlignmentFile.getrname()`
 
-            src = self._delegate
-            if pysam_get_n_cigar(src) == 0:
-                return cigar
-
-            cigar_p = pysam_bam_get_cigar(src);
-            for k from 0 <= k < pysam_get_n_cigar(src):
-                op = cigar_p[k] & BAM_CIGAR_MASK
-                l = cigar_p[k] >> BAM_CIGAR_SHIFT
-                cigar.append((op, l))
-            return cigar
+        """
+        def __get__(self): return self._delegate.core.tid
+        def __set__(self, tid): self._delegate.core.tid = tid
 
-        def __set__(self, values):
-            cdef uint32_t * p
+    property reference_start:
+        """0-based leftmost coordinate"""
+        def __get__(self): return self._delegate.core.pos
+        def __set__(self, pos):
+            ## setting the position requires updating the "bin" attribute
             cdef bam1_t * src
-            cdef op, l
-            cdef int k, ncigar
-
-            k = 0
-
             src = self._delegate
+            src.core.pos = pos
+            if pysam_get_n_cigar(src):
+                pysam_set_bin(src, 
+                              hts_reg2bin(
+                                  src.core.pos,
+                                  bam_endpos(src),
+                                  14,
+                                  5))
+            else:
+                pysam_set_bin(src,
+                              hts_reg2bin(
+                                  src.core.pos,
+                                  src.core.pos + 1,
+                                  14,
+                                  5))
 
-            # get location of cigar string
-            p = pysam_bam_get_cigar(src)
-
-            # empty values for cigar string
-            if values is None:
-                values = []
-
-            ncigar = len(values)
-            # create space for cigar data within src.data
-            pysam_bam_update(src,
-                             pysam_get_n_cigar(src) * 4,
-                             ncigar * 4,
-                             <uint8_t*>p)
-
-            # length is number of cigar operations, not bytes
-            pysam_set_n_cigar(src, ncigar)
-
-            # re-acquire pointer to location in memory
-            # as it might have moved
-            p = pysam_bam_get_cigar(src)
-
-            # insert cigar operations
-            for op, l in values:
-                p[k] = l << BAM_CIGAR_SHIFT | op
-                k += 1
-
-            ## setting the cigar string requires updating the bin
-            pysam_set_bin(src,
-                          hts_reg2bin(
-                              src.core.pos,
-                              bam_endpos(src),
-                              14,
-                              5))
+    property mapping_quality:
+        """mapping quality"""
+        def __get__(self):
+            return pysam_get_qual(self._delegate)
+        def __set__(self, qual):
+            pysam_set_qual(self._delegate, qual)
 
     property cigarstring:
         '''the :term:`cigar` alignment as a string.
@@ -2370,36 +2373,72 @@ cdef class AlignedRead:
             SAM format. It is different from the order of
             the :attr:`cigar` property.
 
-        Returns the empty string if not present.
+        Returns None if not present.
 
         To unset the cigarstring, assign None or the
         empty string.
         '''
         def __get__(self):
-            c = self.cigar
-            if c == None: return ""
+            c = self.cigartuples
+            if c is None:
+                return None
             # reverse order
-            else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
+            else:
+                return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
             
         def __set__(self, cigar):
             if cigar is None or len(cigar) == 0:
-                self.cigar = []
+                self.cigartuples = []
             else:
                 parts = CIGAR_REGEX.findall(cigar)
                 # reverse order
-                self.cigar = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]
+                self.cigartuples = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]
 
-    property seq:
-        """read sequence bases, including :term:`soft clipped` bases 
-        (None if not present).
+    # TODO
+    # property cigar:
+    #     """the cigar alignment"""
 
-        In Python 3, this property is of type bytes and assigning a
-        unicode string to it consisting of ASCII characters only will
-        work, but is inefficient.
+    property next_reference_id:
+        """the :term:`reference` id of the mate/next read."""
+        def __get__(self): return self._delegate.core.mtid
+        def __set__(self, mtid):
+            self._delegate.core.mtid = mtid
 
-        Note that assigning to seq will invalidate any quality scores.
-        Thus, to in-place edit the sequence and quality scores, copies of
-        the quality scores need to be taken. Consider trimming for example::
+    property next_reference_start:
+        """the position of the mate/next read."""
+        def __get__(self):
+            return self._delegate.core.mpos
+        def __set__(self, mpos):
+            self._delegate.core.mpos = mpos
+
+    property query_length:
+        """the length of the query/read.
+
+        This value corresponds to the length of the sequence supplied
+        in the BAM/SAM file. The length of a query is 0 if there is no
+        sequence in the BAM/SAM file. In those cases, the read length
+        can be inferred from the CIGAR alignment, see
+        :meth:`pysam.AlignmentFile.infer_query_length.`.
+
+        This property can be set by providing a sequence.
+        """
+        def __get__(self):
+            return self._delegate.core.l_qseq
+
+    property template_length:
+        """the observed query template length"""
+        def __get__(self):
+            return self._delegate.core.isize
+        def __set__(self, isize):
+            self._delegate.core.isize = isize
+
+    property query_sequence:
+        """read sequence bases, including :term:`soft clipped` bases 
+        (None if not present).
+
+        Note that assigning to seq will invalidate any quality scores.
+        Thus, to in-place edit the sequence and quality scores, copies of
+        the quality scores need to be taken. Consider trimming for example::
 
            q = read.qual
            read.seq = read.seq[5:10]
@@ -2416,7 +2455,7 @@ cdef class AlignedRead:
 
             if src.core.l_qseq == 0: return None
 
-            return get_seq_range(src, 0, src.core.l_qseq)
+            return _getSequenceRange(src, 0, src.core.l_qseq)
 
         def __set__(self, seq):
             # samtools manages sequence and quality length memory together
@@ -2464,20 +2503,22 @@ cdef class AlignedRead:
                 p = pysam_bam_get_qual(src)
                 p[0] = 0xff
 
-    property qual:
+    property query_qualities:
         """read sequence base qualities, including :term:`soft
         clipped` bases (None if not present).
 
-        In Python 3, this property is of type bytes and assigning a
-        unicode string to it consisting of ASCII characters only will
-        work, but is inefficient.
+        Quality scores are returned as a python array of unsigned
+        chars. Note that this is not the ASCII-encoded value typically
+        seen in FASTQ or SAM formatted files. Thus, no offset of 33
+        needs to be subtracted.
 
         Note that to set quality scores the sequence has to be set
-        previously as this will determine the permitted length of
-        the quality score array.
+        beforehand as this will determine the expected length of the
+        quality score array.
 
         This method raises a ValueError if the length of the 
         quality scores and the sequence are not the same.
+
         """
         def __get__(self):
 
@@ -2486,109 +2527,43 @@ cdef class AlignedRead:
 
             src = self._delegate
 
-            if src.core.l_qseq == 0: return None
+            if src.core.l_qseq == 0:
+                return None
 
-            return get_qual_range(src, 0, src.core.l_qseq)
+            return _getQualitiesRange(src, 0, src.core.l_qseq)
 
-        def __set__(self,qual):
-            # note that space is already allocated via the sequences
+        def __set__(self, qual):
+            # note that memory is already allocated via setting the sequence
+            # hence length match of sequence and quality needs is checked.
             cdef bam1_t * src
             cdef uint8_t * p
-            cdef char * q
-            cdef int k
+            cdef int l
 
             src = self._delegate
             p = pysam_bam_get_qual(src)
-            if qual == None or len(qual) == 0:
+            if qual is None or len(qual) == 0:
                 # if absent - set to 0xff
                 p[0] = 0xff
                 return
-            qual = _forceBytes(qual)
-            cdef int l
-            # convert to C string
-            q = qual
+            
+            # check for length match
             l = len(qual)
             if src.core.l_qseq != l:
-                raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
-            assert src.core.l_qseq == l
-            for k from 0 <= k < l:
-                p[k] = <uint8_t>q[k] - 33
-
-    property query:
-        """aligned portion of the read.
-
-        This is a substring of :attr:`seq` that excludes flanking bases that were
-        :term:`soft clipped` (None if not present). It is equal to ``seq[qstart:qend]``.
-
-        In Python 3, this property is of type bytes. Assigning a
-        unicode string to it consisting of ASCII characters only will
-        work, but is inefficient.
-
-        SAM/BAM files may include extra flanking bases that are
-        not part of the alignment.  These bases may be the result of the
-        Smith-Waterman or other algorithms, which may not require alignments
-        that begin at the first residue or end at the last.  In addition,
-        extra sequencing adapters, multiplex identifiers, and low-quality bases that
-        were not considered for alignment may have been retained."""
-
-        def __get__(self):
-            cdef bam1_t * src
-            cdef uint32_t start, end
-            cdef char * s
-
-            src = self._delegate
-
-            if src.core.l_qseq == 0: return None
-
-            start = query_start(src)
-            end   = query_end(src)
-
-            return get_seq_range(src, start, end)
-
-    property qqual:
-        """aligned query sequence quality values (None if not
-        present). These are the quality values that correspond to :attr:`query`, that is,
-        they exclude qualities of :term:`soft clipped` bases. This is equal to
-        ``qual[qstart:qend]``.
-
-        This property is read-only.
-
-        In Python 3, this property is of type bytes."""
-        def __get__(self):
-            cdef bam1_t * src
-            cdef uint32_t start, end
-
-            src = self._delegate
-
-            if src.core.l_qseq == 0: return None
-
-            start = query_start(src)
-            end   = query_end(src)
-
-            return get_qual_range(src, start, end)
-
-    property qstart:
-        """start index of the aligned query portion of the sequence (0-based, inclusive).
-
-        This the index of the first base in :attr:`seq` that is not soft-clipped.
-        """
-        def __get__(self):
-            return query_start(self._delegate)
-
-    property qend:
-        """end index of the aligned query portion of the sequence (0-based, exclusive)"""
-        def __get__(self):
-            return query_end(self._delegate)
+                raise ValueError(
+                    "quality and sequence mismatch: %i != %i" %
+                    (l, src.core.l_qseq))
 
-    property qlen:
-        """length of the aligned query sequence.
+            # create a python array object filling it
+            # with the quality scores
 
-        This is equal to :attr:`qend` - :attr:`qstart`"""
-        def __get__(self):
-            cdef bam1_t * src
-            src = self._delegate
-            return query_end(src)-query_start(src)
+            # NB: should avoid this copying if qual is
+            # already of the correct type.
+            cdef array.array result = array.array('B', qual)
 
+            # copy data
+            memcpy(p, result.data.as_voidptr, l)
+    
+    # TODO: opts object with mapping-like interface
     property tags:
         """the tags in the AUX field.
 
@@ -2701,130 +2676,7 @@ cdef class AlignedRead:
                 # enough for memcpy, see issue 129
                 temp = p
                 memcpy(s, temp, new_size)
-
-    cpdef setTag(self, tag, value, 
-                 value_type = None, 
-                 replace = True):
-        '''
-        Set optional field of alignment *tag* to *value*.  *value_type* may be specified,
-        but if not the type will be inferred based on the Python type of *value*
-
-        An existing value of the same tag will be overwritten unless
-        *replace* is set to False.
-        '''
-
-        cdef int      value_size
-        cdef uint8_t * value_ptr
-        cdef uint8_t *existing_ptr
-        cdef uint8_t  type_code
-        cdef float    float_value
-        cdef double   double_value
-        cdef int32_t  int_value
-        cdef bam1_t * src = self._delegate
-        cdef char * _value_type
-        
-        if len(tag) != 2:
-            raise ValueError('Invalid tag: %s' % tag)
-        
-        type_code = get_type_code(value, value_type)
-        if type_code == 0:
-            raise ValueError("can't guess type or invalid type code specified")
-
-        # Not Endian-safe, but then again neither is samtools!
-        if type_code == 'Z':
-            value = _forceBytes( value )
-            value_ptr    = <uint8_t*><char*>value
-            value_size   = len(value)+1
-        elif type_code == 'i':
-            int_value    = value
-            value_ptr    = <uint8_t*>&int_value
-            value_size   = sizeof(int32_t)
-        elif type_code == 'd':
-            double_value = value
-            value_ptr    = <uint8_t*>&double_value
-            value_size   = sizeof(double)
-        elif type_code == 'f':
-            float_value  = value
-            value_ptr    = <uint8_t*>&float_value
-            value_size   = sizeof(float)
-        else:
-            raise ValueError('Unsupported value_type in set_option')
-
-        tag = _forceBytes( tag )
-        if replace:
-            existing_ptr = bam_aux_get(src, tag)
-            if existing_ptr:
-                bam_aux_del(src, existing_ptr)
-
-        bam_aux_append(src,
-                       tag,
-                       type_code, 
-                       value_size,
-                       value_ptr)
-
-    property flag:
-        """properties flag"""
-        def __get__(self):
-            return pysam_get_flag(self._delegate)
-        def __set__(self, flag):
-            pysam_set_flag(self._delegate, flag)
-
-    property rname:
-        """
-        :term:`target` ID
-
-        DEPRECATED from pysam-0.4 - use tid in the future.
-        The rname field caused a lot of confusion as it returns
-        the :term:`target` ID instead of the reference sequence
-        name.
-
-        .. note::
-
-            This field contains the index of the reference sequence
-            in the sequence dictionary. To obtain the name
-            of the reference sequence, use :meth:`pysam.Samfile.getrname()`
-
-        """
-        def __get__(self): return self._delegate.core.tid
-        def __set__(self, tid): self._delegate.core.tid = tid
-
-    property tid:
-        """
-        :term:`target` ID
-
-        .. note::
-
-            This field contains the index of the reference sequence
-            in the sequence dictionary. To obtain the name
-            of the reference sequence, use :meth:`pysam.Samfile.getrname()`
-
-        """
-        def __get__(self): return self._delegate.core.tid
-        def __set__(self, tid): self._delegate.core.tid = tid
-
-    property pos:
-        """0-based leftmost coordinate"""
-        def __get__(self): return self._delegate.core.pos
-        def __set__(self, pos):
-            ## setting the position requires updating the "bin" attribute
-            cdef bam1_t * src
-            src = self._delegate
-            src.core.pos = pos
-            if pysam_get_n_cigar(src):
-                pysam_set_bin(src, 
-                              hts_reg2bin(
-                                  src.core.pos,
-                                  bam_endpos(src),
-                                  14,
-                                  5))
-            else:
-                pysam_set_bin(src,
-                              hts_reg2bin(
-                                  src.core.pos,
-                                  src.core.pos + 1,
-                                  14,
-                                  5))
-
+                
     property bin:
         """properties bin"""
         def __get__(self):
@@ -2832,99 +2684,20 @@ cdef class AlignedRead:
         def __set__(self, bin):
             pysam_set_bin(self._delegate, bin)
 
-    property rlen:
-        """length of the read. This includes soft-clipped bases
-        and is equal to ``len(seq)``.
-
-        This property is read-only.
-
-        Returns 0 if not available."""
-        def __get__(self):
-            return self._delegate.core.l_qseq
-
-    property aend:
-        '''aligned reference position of the read on the reference genome.  
-        
-        aend points to one past the last aligned residue.
-        Returns None if not available.'''
-        def __get__(self):
-            cdef bam1_t * src
-            src = self._delegate
-            if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
-                return None
-            return bam_endpos(src)
-
-    property alen:
-        '''aligned length of the read on the reference genome.
-
-        This is equal to `aend - pos`. Returns None if not available.'''
-        def __get__(self):
-            cdef bam1_t * src
-            src = self._delegate
-            if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
-                return None
-            return bam_endpos(src) - \
-                self._delegate.core.pos
-
-    property mapq:
-        """mapping quality"""
-        def __get__(self):
-            return pysam_get_qual(self._delegate)
-        def __set__(self, qual):
-            pysam_set_qual(self._delegate, qual)
-
-    property mrnm:
-        """the :term:`reference` id of the mate
-        deprecated, use RNEXT instead.
-        """
-        def __get__(self):
-            return self._delegate.core.mtid
-        def __set__(self, mtid):
-            self._delegate.core.mtid = mtid
-
-    property rnext:
-        """the :term:`reference` id of the mate """
-        def __get__(self): return self._delegate.core.mtid
-        def __set__(self, mtid): self._delegate.core.mtid = mtid
-
-    property mpos:
-        """the position of the mate
-        deprecated, use PNEXT instead."""
-        def __get__(self):
-            return self._delegate.core.mpos
-        def __set__(self, mpos):
-            self._delegate.core.mpos = mpos
-
-    property pnext:
-        """the position of the mate"""
-        def __get__(self):
-            return self._delegate.core.mpos
-        def __set__(self, mpos):
-            self._delegate.core.mpos = mpos
 
-    #######################################################################
-    #######################################################################
-    ## Flags
-    #######################################################################
-    property isize:
-        """the insert size
-        deprecated: use tlen instead"""
-        def __get__(self):
-            return self._delegate.core.isize
-        def __set__(self, isize):
-            self._delegate.core.isize = isize
-    property tlen:
-        """the template length"""
-        def __get__(self):
-            return self._delegate.core.isize
-        def __set__(self, isize):
-            self._delegate.core.isize = isize
+    ##########################################################
+    # Derived simple attributes. These are simple attributes of 
+    # AlignedSegment getting and setting values.
+    ##########################################################
+    # 1. Flags
+    ##########################################################
     property is_paired:
         """true if read is paired in sequencing"""
         def __get__(self):
             return (self.flag & BAM_FPAIRED) != 0
         def __set__(self,val):
             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
+
     property is_proper_pair:
         """true if read is mapped in a proper pair"""
         def __get__(self):
@@ -2986,180 +2759,489 @@ cdef class AlignedRead:
         def __set__(self, val):
             pysam_update_flag(self._delegate, val, BAM_FDUP)
 
-    #######################################################################
-    #######################################################################
-    ## Derived properties
-    #######################################################################
-    property positions:
-        """a list of reference positions that this read aligns to."""
+    # 2. Coordinates and lengths
+    property reference_end:
+        '''aligned reference position of the read on the reference genome.  
+        
+        aend points to one past the last aligned residue.
+        Returns None if not available.'''
+        def __get__(self):
+            cdef bam1_t * src
+            src = self._delegate
+            if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
+                return None
+            return bam_endpos(src)
+
+    property reference_length:
+        '''aligned length of the read on the reference genome.
+
+        This is equal to `aend - pos`. Returns None if not available.'''
+        def __get__(self):
+            cdef bam1_t * src
+            src = self._delegate
+            if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
+                return None
+            return bam_endpos(src) - \
+                self._delegate.core.pos
+    
+    property query_alignment_length:
+        """length of the query template. This includes soft-clipped bases
+        and is equal to ``len(seq)``.
+
+        This property is read-only.
+
+        Returns 0 if not available."""
+        def __get__(self):
+            return self._delegate.core.l_qseq
+
+    property query_alignment_sequence:
+        """aligned portion of the read.
+
+        This is a substring of :attr:`seq` that excludes flanking
+        bases that were :term:`soft clipped` (None if not present). It
+        is equal to ``seq[qstart:qend]``.
+
+        SAM/BAM files may include extra flanking bases that are not
+        part of the alignment.  These bases may be the result of the
+        Smith-Waterman or other algorithms, which may not require
+        alignments that begin at the first residue or end at the last.
+        In addition, extra sequencing adapters, multiplex identifiers,
+        and low-quality bases that were not considered for alignment
+        may have been retained.
+
+        """
+
+        def __get__(self):
+            cdef bam1_t * src
+            cdef uint32_t start, end
+
+            src = self._delegate
+
+            if src.core.l_qseq == 0:
+                return None
+
+            start = _getQueryStart(src)
+            end   = _getQueryEnd(src)
+
+            return _getSequenceRange(src, start, end)
+
+    property query_alignment_qualities:
+        """aligned query sequence quality values (None if not present). These
+        are the quality values that correspond to :attr:`query`, that
+        is, they exclude qualities of :term:`soft clipped` bases. This
+        is equal to ``qual[qstart:qend]``.
+
+        Quality scores are returned as a python array of unsigned
+        chars. Note that this is not the ASCII-encoded value typically
+        seen in FASTQ or SAM formatted files. Thus, no offset of 33
+        needs to be subtracted.
+
+        This property is read-only.
+
+        """
+        def __get__(self):
+            cdef bam1_t * src
+            cdef uint32_t start, end
+
+            src = self._delegate
+
+            if src.core.l_qseq == 0:
+                return None
+
+            start = _getQueryStart(src)
+            end   = _getQueryEnd(src)
+
+            return _getQualitiesRange(src, start, end)
+
+    property query_alignment_start:
+        """start index of the aligned query portion of the sequence (0-based,
+        inclusive).
+
+        This the index of the first base in :attr:`seq` that is not
+        soft-clipped.
+
+        """
+        def __get__(self):
+            return _getQueryStart(self._delegate)
+
+    property query_alignment_end:
+        """end index of the aligned query portion of the sequence (0-based,
+        exclusive)"""
+        def __get__(self):
+            return _getQueryEnd(self._delegate)
+
+    property query_aligment_length:
+        """length of the aligned query sequence.
+
+        This is equal to :attr:`qend` - :attr:`qstart`"""
+        def __get__(self):
+            cdef bam1_t * src
+            src = self._delegate
+            return _getQueryEnd(src) - _getQueryStart(src)
+
+    #####################################################
+    # Computed properties
+
+    def get_reference_positions(self, full_length=False):
+        """a list of reference positions that this read aligns to.
+
+        By default, this method only returns positions in the
+        reference that are within the alignment. If *full_length* is
+        set, None values will be included for any soft-clipped or
+        unaligned positions within the read. The returned list will
+        thus be of the same length as the read.
+
+        """
+        cdef uint32_t k, i, pos
+        cdef int op
+        cdef uint32_t * cigar_p
+        cdef bam1_t * src
+        cdef bint _full = full_length
+
+        src = self._delegate
+        if pysam_get_n_cigar(src) == 0:
+            return []
+
+        result = []
+        pos = src.core.pos
+        cigar_p = pysam_bam_get_cigar(src)
+
+        for k from 0 <= k < pysam_get_n_cigar(src):
+            op = cigar_p[k] & BAM_CIGAR_MASK
+            l = cigar_p[k] >> BAM_CIGAR_SHIFT
+
+            if op == BAM_CSOFT_CLIP or op == BAM_CINS:
+                if _full:
+                    for i from 0 <= i < l:
+                        result.append(None)
+            elif op == BAM_CMATCH:
+                for i from pos <= i < pos + l:
+                    result.append(i)
+                pos += l
+            elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+                pos += l
+
+        return result
+
+    def infer_query_length(self, always=True):
+        """inferred read length from CIGAR string.
+
+        If *always* is set to True, the read length
+        will be always inferred. If set to False, the length
+        of the read sequence will be returned if it is
+        available.
+
+        Returns None if CIGAR string is not present.
+        """
+        cdef uint32_t k, qpos
+        cdef int op
+        cdef uint32_t * cigar_p
+        cdef bam1_t * src 
+
+        src = self._delegate
+
+        if not always and src.core.l_qseq:
+            return src.core.l_qseq
+
+        if pysam_get_n_cigar(src) == 0:
+            return None
+
+        qpos = 0
+        cigar_p = pysam_bam_get_cigar(src)
+
+        for k from 0 <= k < pysam_get_n_cigar(src):
+            op = cigar_p[k] & BAM_CIGAR_MASK
+
+            if op == BAM_CMATCH or op == BAM_CINS or \
+               op == BAM_CSOFT_CLIP or \
+               op == BAM_CEQUAL or op == BAM_CDIFF:
+                qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
+
+        return qpos
+            
+    def get_aligned_pairs(self):
+        """a list of aligned read and reference positions.
+        """
+        cdef uint32_t k, i, pos, qpos
+        cdef int op
+        cdef uint32_t * cigar_p
+        cdef bam1_t * src 
+
+        src = self._delegate
+        if pysam_get_n_cigar(src) == 0:
+            return []
+
+        result = []
+        pos = src.core.pos
+        qpos = 0
+        cigar_p = pysam_bam_get_cigar(src)
+
+        for k from 0 <= k < pysam_get_n_cigar(src):
+            op = cigar_p[k] & BAM_CIGAR_MASK
+            l = cigar_p[k] >> BAM_CIGAR_SHIFT
+
+            if op == BAM_CMATCH:
+                for i from pos <= i < pos + l:
+                    result.append((qpos, i))
+                    qpos += 1
+                pos += l
+
+            elif op == BAM_CINS:
+                for i from pos <= i < pos + l:
+                    result.append((qpos, None))
+                    qpos += 1
+
+            elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+                for i from pos <= i < pos + l:
+                    result.append((None, i))
+                pos += l
+
+        return result
+
+    def get_blocks(self):
+        """ a list of start and end positions of
+        aligned gapless blocks.
+
+        The start and end positions are in genomic 
+        coordinates. 
+      
+        Blocks are not normalized, i.e. two blocks 
+        might be directly adjacent. This happens if
+        the two blocks are separated by an insertion 
+        in the read.
+        """
+
+        cdef uint32_t k, pos, l
+        cdef int op
+        cdef uint32_t * cigar_p
+        cdef bam1_t * src
+
+        src = self._delegate
+        if pysam_get_n_cigar(src) == 0:
+            return []
+
+        result = []
+        pos = src.core.pos
+        cigar_p = pysam_bam_get_cigar(src)
+        l = 0
+
+        for k from 0 <= k < pysam_get_n_cigar(src):
+            op = cigar_p[k] & BAM_CIGAR_MASK
+            l = cigar_p[k] >> BAM_CIGAR_SHIFT
+            if op == BAM_CMATCH:
+                result.append((pos, pos + l))
+                pos += l
+            elif op == BAM_CDEL or op == BAM_CREF_SKIP:
+                pos += l
+
+        return result
+
+    def get_overlap(self, uint32_t start, uint32_t end):
+        """return number of aligned bases of read overlapping the interval
+        *start* and *end* on the reference sequence.
+
+        Return None if cigar alignment is not available.
+        """
+        cdef uint32_t k, i, pos, overlap
+        cdef int op, o
+        cdef uint32_t * cigar_p
+        cdef bam1_t * src
+
+        overlap = 0
+
+        src = self._delegate
+        if pysam_get_n_cigar(src) == 0:
+            return None
+        pos = src.core.pos
+        o = 0
+
+        cigar_p = pysam_bam_get_cigar(src)
+        for k from 0 <= k < pysam_get_n_cigar(src):
+            op = cigar_p[k] & BAM_CIGAR_MASK
+            l = cigar_p[k] >> BAM_CIGAR_SHIFT
+
+            if op == BAM_CMATCH:
+                o = min( pos + l, end) - max( pos, start )
+                if o > 0: overlap += o
+
+            if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
+                pos += l
+
+        return overlap
+
+    #####################################################
+    ## Unsorted as yet
+
+
+    # TODO: capture in CIGAR object
+    property cigartuples:
+        """the :term:`cigar` alignment. The alignment
+        is returned as a list of tuples of (operation, length). 
+
+        If the alignment is not present, None is returned.
+
+        The operations are:
+
+        +-----+--------------+-----+
+        |M    |BAM_CMATCH    |0    |
+        +-----+--------------+-----+
+        |I    |BAM_CINS      |1    |
+        +-----+--------------+-----+
+        |D    |BAM_CDEL      |2    |
+        +-----+--------------+-----+
+        |N    |BAM_CREF_SKIP |3    |
+        +-----+--------------+-----+
+        |S    |BAM_CSOFT_CLIP|4    |
+        +-----+--------------+-----+
+        |H    |BAM_CHARD_CLIP|5    |
+        +-----+--------------+-----+
+        |P    |BAM_CPAD      |6    |
+        +-----+--------------+-----+
+        |=    |BAM_CEQUAL    |7    |
+        +-----+--------------+-----+
+        |X    |BAM_CDIFF     |8    |
+        +-----+--------------+-----+
+
+        .. note::
+            The output is a list of (operation, length) tuples, such as
+            ``[(0, 30)]``.
+            This is different from the SAM specification and
+            the :attr:`cigarstring` property, which uses a
+            (length, operation) order, for example: ``30M``.
+
+        To unset the cigar property, assign an empty list
+        or None.
+        """
         def __get__(self):
-            cdef uint32_t k, i, pos
-            cdef int op
             cdef uint32_t * cigar_p
             cdef bam1_t * src
+            cdef uint32_t op, l
+            cdef int k
 
             src = self._delegate
             if pysam_get_n_cigar(src) == 0:
-                return []
+                return None
 
-            result = []
-            pos = src.core.pos
-            cigar_p = pysam_bam_get_cigar(src)
+            cigar = []
 
+            cigar_p = pysam_bam_get_cigar(src);
             for k from 0 <= k < pysam_get_n_cigar(src):
                 op = cigar_p[k] & BAM_CIGAR_MASK
                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
-                if op == BAM_CMATCH:
-                    for i from pos <= i < pos + l:
-                        result.append( i )
-
-                if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
-                    pos += l
+                cigar.append((op, l))
+            return cigar
 
-            return result
+        def __set__(self, values):
+            cdef uint32_t * p
+            cdef bam1_t * src
+            cdef op, l
+            cdef int k, ncigar
 
-    property inferred_length:
-        """inferred read length from CIGAR string.
+            k = 0
 
-        Returns 0 if CIGAR string is not present.
-        """
-        def __get__(self):
-           cdef uint32_t k, qpos
-           cdef int op
-           cdef uint32_t * cigar_p
-           cdef bam1_t * src 
+            src = self._delegate
 
-           src = self._delegate
-           if pysam_get_n_cigar(src) == 0: return 0
+            # get location of cigar string
+            p = pysam_bam_get_cigar(src)
 
-           qpos = 0
-           cigar_p = pysam_bam_get_cigar(src)
+            # empty values for cigar string
+            if values is None:
+                values = []
 
-           for k from 0 <= k < pysam_get_n_cigar(src):
-               op = cigar_p[k] & BAM_CIGAR_MASK
+            ncigar = len(values)
+            # create space for cigar data within src.data
+            pysam_bam_update(src,
+                             pysam_get_n_cigar(src) * 4,
+                             ncigar * 4,
+                             <uint8_t*>p)
 
-               if op == BAM_CMATCH or op == BAM_CINS or \
-                  op == BAM_CSOFT_CLIP or \
-                  op == BAM_CEQUAL or op == BAM_CDIFF:
-                   qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
+            # length is number of cigar operations, not bytes
+            pysam_set_n_cigar(src, ncigar)
 
-           return qpos
-            
-    property aligned_pairs:
-        """a list of aligned read and reference positions.
+            # re-acquire pointer to location in memory
+            # as it might have moved
+            p = pysam_bam_get_cigar(src)
 
-        Unaligned position are marked by None.
-        """
-        def __get__(self):
-            cdef uint32_t k, i, pos, qpos
-            cdef int op
-            cdef uint32_t * cigar_p
-            cdef bam1_t * src 
+            # insert cigar operations
+            for op, l in values:
+                p[k] = l << BAM_CIGAR_SHIFT | op
+                k += 1
 
-            src = self._delegate
-            if pysam_get_n_cigar(src) == 0:
-                return []
+            ## setting the cigar string requires updating the bin
+            pysam_set_bin(src,
+                          hts_reg2bin(
+                              src.core.pos,
+                              bam_endpos(src),
+                              14,
+                              5))
 
-            result = []
-            pos = src.core.pos
-            qpos = 0
-            cigar_p = pysam_bam_get_cigar(src)
 
-            for k from 0 <= k < pysam_get_n_cigar(src):
-                op = cigar_p[k] & BAM_CIGAR_MASK
-                l = cigar_p[k] >> BAM_CIGAR_SHIFT
 
-                if op == BAM_CMATCH:
-                    for i from pos <= i < pos + l:
-                        result.append( (qpos, i) )
-                        qpos += 1
-                    pos += l
-
-                elif op == BAM_CINS:
-                    for i from pos <= i < pos + l:
-                        result.append( (qpos, None) )
-                        qpos += 1
-
-                elif op == BAM_CDEL or op == BAM_CREF_SKIP:
-                    for i from pos <= i < pos + l:
-                        result.append( (None, i) )
-                    pos += l
-                       
-            return result
+    cpdef setTag(self, tag, value, 
+                 value_type = None, 
+                 replace = True):
+        '''
+        Set optional field of alignment *tag* to *value*.  *value_type* may be specified,
+        but if not the type will be inferred based on the Python type of *value*
 
-    property blocks:
-        """ a list of start and end positions of
-        aligned gapless blocks.
+        An existing value of the same tag will be overwritten unless
+        *replace* is set to False.
+        '''
 
-        The start and end positions are in genomic 
-        coordinates. 
-      
-        Blocks are not normalized, i.e. two blocks 
-        might be directly adjacent. This happens if
-        the two blocks are separated by an insertion 
-        in the read.
-        """
+        cdef int      value_size
+        cdef uint8_t * value_ptr
+        cdef uint8_t *existing_ptr
+        cdef uint8_t  type_code
+        cdef float    float_value
+        cdef double   double_value
+        cdef int32_t  int_value
+        cdef bam1_t * src = self._delegate
+        cdef char * _value_type
+        
+        if len(tag) != 2:
+            raise ValueError('Invalid tag: %s' % tag)
+        
+        type_code = _getTypeCode(value, value_type)
+        if type_code == 0:
+            raise ValueError("can't guess type or invalid type code specified")
 
-        def __get__(self):
-            cdef uint32_t k, pos, l
-            cdef int op
-            cdef uint32_t * cigar_p
-            cdef bam1_t * src
+        # Not Endian-safe, but then again neither is samtools!
+        if type_code == 'Z':
+            value = _forceBytes( value )
+            value_ptr    = <uint8_t*><char*>value
+            value_size   = len(value)+1
+        elif type_code == 'i':
+            int_value    = value
+            value_ptr    = <uint8_t*>&int_value
+            value_size   = sizeof(int32_t)
+        elif type_code == 'd':
+            double_value = value
+            value_ptr    = <uint8_t*>&double_value
+            value_size   = sizeof(double)
+        elif type_code == 'f':
+            float_value  = value
+            value_ptr    = <uint8_t*>&float_value
+            value_size   = sizeof(float)
+        else:
+            raise ValueError('Unsupported value_type in set_option')
 
-            src = self._delegate
-            if pysam_get_n_cigar(src) == 0:
-                return []
-            
-            result = []
-            pos = src.core.pos
-            cigar_p = pysam_bam_get_cigar(src)
-            l = 0
+        tag = _forceBytes( tag )
+        if replace:
+            existing_ptr = bam_aux_get(src, tag)
+            if existing_ptr:
+                bam_aux_del(src, existing_ptr)
 
-            for k from 0 <= k < pysam_get_n_cigar(src):
-                op = cigar_p[k] & BAM_CIGAR_MASK
-                l = cigar_p[k] >> BAM_CIGAR_SHIFT
-                if op == BAM_CMATCH:
-                    result.append((pos, pos + l))
-                    pos += l
-                elif op == BAM_CDEL or op == BAM_CREF_SKIP:
-                    pos += l
+        bam_aux_append(src,
+                       tag,
+                       type_code, 
+                       value_size,
+                       value_ptr)
 
-            return result
 
     #######################################################################
     #######################################################################
-    ## 
+    ## Derived properties
     #######################################################################
-    def overlap( self, uint32_t start, uint32_t end ):
-        """return number of aligned bases of read overlapping the interval *start* and *end*
-        on the reference sequence.
-        """
-        cdef uint32_t k, i, pos, overlap
-        cdef int op, o
-        cdef uint32_t * cigar_p
-        cdef bam1_t * src
-
-        overlap = 0
-
-        src = self._delegate
-        if pysam_get_n_cigar(src) == 0:
-            return 0
-        pos = src.core.pos
-        o = 0
-
-        cigar_p = pysam_bam_get_cigar(src)
-        for k from 0 <= k < pysam_get_n_cigar(src):
-            op = cigar_p[k] & BAM_CIGAR_MASK
-            l = cigar_p[k] >> BAM_CIGAR_SHIFT
-
-            if op == BAM_CMATCH:
-                o = min( pos + l, end) - max( pos, start )
-                if o > 0: overlap += o
-
-            if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
-                pos += l
-
-        return overlap
 
     def opt(self, tag):
         """retrieves optional data given a two-letter *tag*"""
@@ -3191,83 +3273,171 @@ cdef class AlignedRead:
             raise ValueError("unknown auxilliary type '%s'" % auxtype)
 
 
-    def fancy_str (self):
-        """returns list of fieldnames/values in pretty format for debugging.
+    ########################################################
+    # Compatibility Accessors
+    # Functions, properties for compatibility with pysam < 0.8
+    #
+    # Several options
+    #     change the factory functions according to API
+    #         * requires code changes throughout, incl passing
+    #           handles to factory functions
+    #     subclass functions and add attributes at runtime
+    #         e.g.: AlignedSegments.qname = AlignedSegments.query_name
+    #         * will slow down the default interface
+    #     explicit declaration of getters/setters
+    ########################################################
+    property qname:
+        def __get__(self): return self.query_name
+        def __set__(self, v): self.query_name = v
+    property tid:
+        def __get__(self): return self.reference_id
+        def __set__(self, v): self.reference_id = v
+    property pos:
+        def __get__(self): return self.reference_start
+        def __set__(self, v): self.reference_start = v
+    property mapq:
+        def __get__(self): return self.mapping_quality
+        def __set__(self, v): self.mapping_quality = v
+    property rnext:
+        def __get__(self): return self.next_reference_id
+        def __set__(self, v): self.next_reference_id = v
+    property pnext:
+        def __get__(self):
+            return self.next_reference_start
+        def __set__(self, v):
+            self.next_reference_start = v
+    property cigar:
+        def __get__(self):
+            r = self.cigartuples
+            if r is None:
+                r = []
+            return r
+        def __set__(self, v): self.cigartuples = v
+    property tlen:
+        def __get__(self):
+            return self.template_length
+        def __set__(self, v):
+            self.template_length = v
+    property seq:
+        def __get__(self): return self.query_sequence
+        def __set__(self, v): self.query_sequence = v
+    property qual:
+        def __get__(self):
+            return toQualityString(self.query_qualities)
+        def __set__(self, v):
+            self.query_qualities = fromQualityString(v)
+    property alen:
+        def __get__(self):
+            return self.reference_length
+        def __set__(self, v):
+            self.reference_length = v
+    property aend:
+        def __get__(self):
+            return self.reference_end
+        def __set__(self, v):
+            self.reference_end = v
+    property rlen:
+        def __get__(self):
+            return self.query_length
+        def __set__(self, v):
+            self.query_length = v
+    property query:
+        def __get__(self):
+            return self.query_alignment_sequence
+        def __set__(self, v):
+            self.query_alignment_sequence = v
+    property qqual:
+        def __get__(self):
+            return toQualityString(self.query_alignment_qualities)
+        def __set__(self, v):
+            self.query_alignment_qualities = fromQualityString(v)
+    property qstart:
+        def __get__(self):
+            return self.query_alignment_start
+        def __set__(self, v):
+            self.query_alignment_start = v
+    property qend:
+        def __get__(self):
+            return self.query_alignment_end
+        def __set__(self, v):
+            self.query_alignment_end = v
+    property qlen:
+        def __get__(self):
+            return self.query_alignment_length
+        def __set__(self, v):
+            self.query_alignment_length = v
+    property mrnm:
+        def __get__(self):
+            return self.next_reference_id
+        def __set__(self, v):
+            self.next_reference_id = v
+    property mpos:
+        def __get__(self):
+            return self.next_reference_start
+        def __set__(self, v):
+            self.next_reference_start = v
+    property rname:
+        def __get__(self):
+            return self.reference_id
+        def __set__(self, v):
+            self.reference_id = v
+    property isize:
+        def __get__(self):
+            return self.template_length
+        def __set__(self, v):
+            self.template_length = v
+    property blocks:
+        def __get__(self):
+            return self.get_blocks()
+    property aligned_pairs:
+        def __get__(self):
+            return self.get_aligned_pairs()
+    property inferred_length:
+        def __get__(self):
+            return self.infer_query_length()
+    property positions:
+        def __get__(self):
+            return self.get_reference_positions()
+    def overlap(self):
+        return self.get_overlap()
+
+
+cdef class PileupColumn:
+    '''A pileup of reads at a particular reference sequence postion
+    (:term:`column`). A pileup column contains all the reads that map
+    to a certain target base.
+
+    This class is a proxy for results returned by the samtools pileup
+    engine.  If the underlying engine iterator advances, the results
+    of this column will change.
 
-        """
-        ret_string = []
-
-        # Originally written by Leo. Note that not all of these fields
-        # exist. Deprecate?
-        field_names = {
-           "tid":           "Contig index",
-           "pos":           "Mapped position on contig",
-           "mtid":          "Contig index for mate pair",
-           "mpos":          "Position of mate pair",
-           "isize":         "Insert size",
-           "flag":          "Binary flag",
-           "n_cigar":       "Count of cigar entries",
-           "cigar":         "Cigar entries",
-           "qual":          "Mapping quality",
-           "bin":           "Bam index bin number",
-           "l_qname":       "Length of query name",
-           "qname":         "Query name",
-           "l_qseq":        "Length of query sequence",
-           "qseq":          "Query sequence",
-           "bqual":         "Quality scores",
-           "l_data":         "Length of auxilary data",
-           "m_data":        "Maximum data length",
-           }
-        fields_names_in_order = ["tid", "pos", "mtid", "mpos", "isize", "flag",
-                                 "n_cigar", "cigar", "qual", "bin", "l_qname", "qname",
-                                 "l_qseq", "qseq", "bqual", "l_data", "m_data"]
-
-        for f in fields_names_in_order:
-            if f not in dir(self):
-                continue
-            ret_string.append("%-30s %-10s= %s" %
-                              (field_names[f],
-                               "(" + f + ")",
-                               self.__getattribute__(f)))
-
-        return ret_string
-
-cdef class PileupProxy:
-    '''A pileup column. A pileup column contains
-    all the reads that map to a certain target base.
-
-    tid
-        chromosome ID as is defined in the header
-    pos
-        the target base coordinate (0-based)
-    n
-        number of reads mapping to this column
-    pileups
-        list of reads (:class:`pysam.PileupRead`) aligned to this column
-
-    This class is a proxy for results returned by the samtools pileup engine.
-    If the underlying engine iterator advances, the results of this column
-    will change.
     '''
     def __init__(self):
-        raise TypeError("This class cannot be instantiated from Python")
+        raise TypeError("this class cannot be instantiated from Python")
 
     def __str__(self):
-        return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
+        return "\t".join(map(str, 
+                              (self.reference_id, self.reference_pos, 
+                               self.nsegmentes))) +\
             "\n" +\
-            "\n".join( map(str, self.pileups) )
+            "\n".join(map(str, self.pileups))
 
-    property tid:
-        '''the chromosome ID as is defined in the header'''
-        def __get__(self): return self.tid
+    property reference_id:
+        '''the reference sequence number as defined in the header'''
+        def __get__(self):
+            return self.tid
 
-    property n:
+    property nsegments:
         '''number of reads mapping to this column.'''
-        def __get__(self): return self.n_pu
-        def __set__(self, n): self.n_pu = n
+        def __get__(self):
+            return self.n_pu
+        def __set__(self, n):
+            self.n_pu = n
 
-    property pos:
-        def __get__(self): return self.pos
+    property reference_pos:
+        '''the position in the reference sequence (0-based).'''
+        def __get__(self):
+            return self.pos
 
     property pileups:
         '''list of reads (:class:`pysam.PileupRead`) aligned to this column'''
@@ -3276,7 +3446,7 @@ cdef class PileupProxy:
             pileups = []
 
             if self.plp == NULL or self.plp[0] == NULL:
-                raise ValueError("PileupProxy accessed after iterator finished")
+                raise ValueError("PileupColumn accessed after iterator finished")
 
             # warning: there could be problems if self.n and self.buf are
             # out of sync.
@@ -3284,28 +3454,64 @@ cdef class PileupProxy:
                 pileups.append(makePileupRead(&(self.plp[0][x])))
             return pileups
 
+    ########################################################
+    # Compatibility Accessors
+    # Functions, properties for compatibility with pysam < 0.8
+    ########################################################
+    property pos:
+        def __get__(self):
+            return self.reference_pos
+        def __set__(self, v):
+            self.reference_pos = v
+
+    property tid:
+        def __get__(self):
+            return self.reference_id
+        def __set__(self, v):
+            self.reference_id = v
+    
+    property n:
+        def __get__(self):
+            return self.nsegments
+        def __set__(self, v):
+            self.nsegments = v
+            
+
 cdef class PileupRead:
-    '''A read aligned to a column.
+    '''Representation of a read aligned to a particular position in the
+    reference sequence.
+
     '''
 
     def __init__(self):
-        raise TypeError("This class cannot be instantiated from Python")
+        raise TypeError("this class cannot be instantiated from Python")
 
     def __str__(self):
-        return "\t".join( map(str, (self.alignment, self.qpos, self.indel, self.level, self.is_del, self.is_head, self.is_tail ) ) )
+        return "\t".join(
+            map(str,
+                (self.alignment, self.query_position,
+                 self.indel, self.level,
+                 self.is_del, self.is_head,
+                 self.is_tail, self.is_refskip)))
 
     property alignment:
-        """a :class:`pysam.AlignedRead` object of the aligned read"""
+        """a :class:`pysam.AlignedSegment` object of the aligned read"""
         def __get__(self):
             return self._alignment
-    property qpos:
+
+    property query_position:
         """position of the read base at the pileup site, 0-based"""
         def __get__(self):
             return self._qpos
+
     property indel:
-        """indel length; 0 for no indel, positive for ins and negative for del"""
+        """indel length; 0 for no indel, positive for ins and negative            for del"""
         def __get__(self):
             return self._indel
+    property level:
+        """the level of the read in the "viewer" mode"""
+        def __get__(self):
+            return self._level
     property is_del:
         """1 iff the base on the padded read is a deletion"""
         def __get__(self):
@@ -3316,9 +3522,9 @@ cdef class PileupRead:
     property is_tail:
         def __get__(self):
             return self._is_tail
-    property level:
+    property is_refskip:
         def __get__(self):
-            return self._level
+            return self._is_refskip
 
 
 cdef class SNPCall:
@@ -3354,11 +3560,13 @@ cdef class SNPCall:
        def __get__(self): return self._consensus_quality
 
     property snp_quality:
-       '''the snp quality (Phred scaled) - probability of consensus being identical to reference sequence.'''
+       '''the snp quality (Phred scaled) - probability of consensus being
+       identical to reference sequence.'''
        def __get__(self): return self._snp_quality
 
     property mapping_quality:
-       '''the root mean square (rms) of the mapping quality of all reads involved in the call.'''
+       '''the root mean square (rms) of the mapping quality of all reads
+       involved in the call.'''
        def __get__(self): return self._rms_mapping_quality
 
     property coverage:
@@ -3379,16 +3587,16 @@ cdef class SNPCall:
 
 
 cdef class IndexedReads:
-    """index a bamfile by read.
+    """index a Sam/BAM-file by query name.
 
-    The index is kept in memory.
+    The index is kept in memory and can be substantial.
 
-    By default, the file is re-openend to avoid conflicts if
-    multiple operators work on the same file. Set *reopen* = False
+    By default, the file is re-openend to avoid conflicts if multiple
+    operators work on the same file. Set *multiple_iterators* = False
     to not re-open *samfile*.
     """
 
-    def __init__(self, Samfile samfile, int reopen=True):
+    def __init__(self, AlignmentFile samfile, int multiple_iterators=True):
 
         # makes sure that samfile stays alive as long as this
         # object is alive.
@@ -3396,16 +3604,17 @@ cdef class IndexedReads:
 
         assert samfile.isbam, "can only IndexReads on bam files"
 
-        # reopen the file - note that this makes the iterator
+        # multiple_iterators the file - note that this makes the iterator
         # slow and causes pileup to slow down significantly.
-        if reopen:
+        if multiple_iterators:
             self.htsfile = hts_open(samfile._filename, 'r')
             assert self.htsfile != NULL
             # read header - required for accurate positioning
-            sam_hdr_read(self.htsfile)
+            self.header = sam_hdr_read(self.htsfile)
             self.owns_samfile = True
         else:
             self.htsfile = self.samfile.htsfile
+            self.header = self.samfile.header
             self.owns_samfile = False
 
         # TODO: BAM file specific
@@ -3434,27 +3643,35 @@ cdef class IndexedReads:
 
         bam_destroy1(b)
 
-    def find(self, qname):
-        if qname in self.index:
+    def find(self, query_name):
+        '''find *query_name* in index.
+
+        Returns an iterator over all reads with query_name.
+
+        Raise a KeyError if the *query_name* is not in the index.
+        '''
+        if query_name in self.index:
             return IteratorRowSelection(
                 self.samfile,
-                self.index[qname],
-                reopen = False)
+                self.index[query_name],
+                multiple_iterators = False)
         else:
-            raise KeyError("read %s not found" % qname)
+            raise KeyError("read %s not found" % query_name)
 
     def __dealloc__(self):
         if self.owns_samfile:
             hts_close(self.htsfile)
+            bam_hdr_destroy(self.header)
 
-__all__ = ["Samfile",
+__all__ = ["AlignmentFile",
            "IteratorRow",
            "IteratorColumn",
-           "AlignedRead",
+           "AlignedSegment",
            "PileupColumn",
-           "PileupProxy",
            "PileupRead",
-           "IndexedReads" ]
+           "IndexedReads",
+           "toQualityString",
+           "fromQualityString"]
            # "IteratorSNPCalls",
            # "SNPCaller",
            # "IndelCaller",
diff --git a/pysam/cfaidx.c b/pysam/cfaidx.c
index ebe7b75..80a55a2 100644
--- a/pysam/cfaidx.c
+++ b/pysam/cfaidx.c
@@ -1,4 +1,4 @@
-/* Generated by Cython 0.20.1 on Tue Jul 15 21:20:07 2014 */
+/* Generated by Cython 0.20.1 on Fri Nov 21 19:49:04 2014 */
 
 #define PY_SSIZE_T_CLEAN
 #ifndef CYTHON_USE_PYLONG_INTERNALS
@@ -546,8 +546,10 @@ static const char *__pyx_f[] = {
 };
 
 /*--- Type declarations ---*/
-struct __pyx_obj_5pysam_6cfaidx_Fastafile;
+struct __pyx_obj_5pysam_6cfaidx_FastaFile;
 struct __pyx_obj_5pysam_6cfaidx_FastqProxy;
+struct __pyx_obj_5pysam_6cfaidx_FastqFile;
+struct __pyx_obj_5pysam_6cfaidx_Fastafile;
 struct __pyx_obj_5pysam_6cfaidx_Fastqfile;
 struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct___open;
 struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct_1_genexpr;
@@ -556,13 +558,13 @@ struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct_2_genexpr;
 /* "pysam/cfaidx.pxd":14
  *         char *s
  * 
- * cdef class Fastafile:             # <<<<<<<<<<<<<<
+ * cdef class FastaFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename, _references, _lengths, reference2length
  *     cdef faidx_t* fastafile
  */
-struct __pyx_obj_5pysam_6cfaidx_Fastafile {
+struct __pyx_obj_5pysam_6cfaidx_FastaFile {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile *__pyx_vtab;
   PyObject *_filename;
   PyObject *_references;
   PyObject *_lengths;
@@ -587,21 +589,44 @@ struct __pyx_obj_5pysam_6cfaidx_FastqProxy {
 /* "pysam/cfaidx.pxd":25
  * 
  * 
- * cdef class Fastqfile:             # <<<<<<<<<<<<<<
+ * cdef class FastqFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename
  *     cdef gzFile fastqfile
  */
-struct __pyx_obj_5pysam_6cfaidx_Fastqfile {
+struct __pyx_obj_5pysam_6cfaidx_FastqFile {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile *__pyx_vtab;
   PyObject *_filename;
   gzFile fastqfile;
   kseq_t *entry;
 };
 
 
+/* "pysam/cfaidx.pxd":35
+ * 
+ * # Compatibility Layer for pysam < 0.8
+ * cdef class Fastafile(FastaFile):             # <<<<<<<<<<<<<<
+ *     pass
+ * 
+ */
+struct __pyx_obj_5pysam_6cfaidx_Fastafile {
+  struct __pyx_obj_5pysam_6cfaidx_FastaFile __pyx_base;
+};
+
+
+/* "pysam/cfaidx.pxd":38
+ *     pass
+ * 
+ * cdef class Fastqfile(FastqFile):             # <<<<<<<<<<<<<<
+ *     pass
+ */
+struct __pyx_obj_5pysam_6cfaidx_Fastqfile {
+  struct __pyx_obj_5pysam_6cfaidx_FastqFile __pyx_base;
+};
+
+
 /* "pysam/cfaidx.pyx":93
- *         return faidx_fetch_nseq(self.fastafile)
+ *         return faidx_nseq(self.fastafile)
  * 
  *     def _open(self, filename):             # <<<<<<<<<<<<<<
  *         '''open an indexed fasta file.
@@ -646,31 +671,59 @@ struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct_2_genexpr {
 
 
 
-/* "pysam/cfaidx.pyx":63
- * cdef int max_pos = 2 << 29
+/* "pysam/cfaidx.pyx":66
+ * ##        add automatic indexing.
+ * ##        add function to get sequence names.
+ * cdef class FastaFile:             # <<<<<<<<<<<<<<
+ *     '''*(filename)*
  * 
- * cdef class Fastafile:             # <<<<<<<<<<<<<<
+ */
+
+struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile {
+  char *(*_fetch)(struct __pyx_obj_5pysam_6cfaidx_FastaFile *, char *, int, int, int *);
+};
+static struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile *__pyx_vtabptr_5pysam_6cfaidx_FastaFile;
+
+
+/* "pysam/cfaidx.pyx":269
+ *             else: return None
+ * 
+ * cdef class FastqFile:             # <<<<<<<<<<<<<<
  *     '''*(filename)*
  * 
  */
 
+struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile {
+  kseq_t *(*getCurrent)(struct __pyx_obj_5pysam_6cfaidx_FastqFile *);
+  int (*cnext)(struct __pyx_obj_5pysam_6cfaidx_FastqFile *);
+};
+static struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile *__pyx_vtabptr_5pysam_6cfaidx_FastqFile;
+
+
+/* "pysam/cfaidx.pyx":344
+ * 
+ * # Compatibility Layer for pysam < 0.8
+ * cdef class Fastafile(FastaFile):             # <<<<<<<<<<<<<<
+ *     pass
+ * 
+ */
+
 struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile {
-  char *(*_fetch)(struct __pyx_obj_5pysam_6cfaidx_Fastafile *, char *, int, int, int *);
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile __pyx_base;
 };
 static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile *__pyx_vtabptr_5pysam_6cfaidx_Fastafile;
 
 
-/* "pysam/cfaidx.pyx":268
- *             else: return None
+/* "pysam/cfaidx.pyx":347
+ *     pass
  * 
- * cdef class Fastqfile:             # <<<<<<<<<<<<<<
- *     '''*(filename)*
+ * cdef class Fastqfile(FastqFile):             # <<<<<<<<<<<<<<
+ *     pass
  * 
  */
 
 struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile {
-  kseq_t *(*getCurrent)(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *);
-  int (*cnext)(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *);
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile __pyx_base;
 };
 static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile *__pyx_vtabptr_5pysam_6cfaidx_Fastqfile;
 #ifndef CYTHON_REFNANNY
@@ -1127,8 +1180,10 @@ static PyTypeObject *__pyx_ptype_7cpython_7complex_complex = 0;
 /* Module declarations from 'cpython' */
 
 /* Module declarations from 'pysam.cfaidx' */
-static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastafile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastaFile = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastqProxy = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastqFile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastafile = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastqfile = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6cfaidx___pyx_scope_struct___open = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6cfaidx___pyx_scope_struct_1_genexpr = 0;
@@ -1148,37 +1203,39 @@ static PyObject *__pyx_builtin_open;
 static PyObject *__pyx_builtin_zip;
 static PyObject *__pyx_builtin_IndexError;
 static PyObject *__pyx_builtin_StopIteration;
-static int __pyx_pf_5pysam_6cfaidx_9Fastafile___cinit__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_2_isOpen(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self); /* proto */
-static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9Fastafile_4__len__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_genexpr(PyObject *__pyx_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_3genexpr(PyObject *__pyx_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_6_open(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_filename); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_8close(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_6cfaidx_9Fastafile_10__dealloc__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_8filename___get__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_10references___get__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_11nreferences___get__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_7lengths___get__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_14getReferenceLength(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_reference); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_16__getitem__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_reference); /* proto */
-static int __pyx_pf_5pysam_6cfaidx_9Fastafile_18__contains__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_reference); /* proto */
+static int __pyx_pf_5pysam_6cfaidx_9FastaFile___cinit__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_2_isOpen(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self); /* proto */
+static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9FastaFile_4__len__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_5_open_genexpr(PyObject *__pyx_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_5_open_3genexpr(PyObject *__pyx_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_6_open(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_filename); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_8close(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self); /* proto */
+static void __pyx_pf_5pysam_6cfaidx_9FastaFile_10__dealloc__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_8filename___get__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_10references___get__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_11nreferences___get__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_7lengths___get__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_12fetch(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_14get_reference_length(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_reference); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_16__getitem__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_reference); /* proto */
+static int __pyx_pf_5pysam_6cfaidx_9FastaFile_18__contains__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_reference); /* proto */
 static int __pyx_pf_5pysam_6cfaidx_10FastqProxy___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_6cfaidx_FastqProxy *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_4name___get__(struct __pyx_obj_5pysam_6cfaidx_FastqProxy *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_8sequence___get__(struct __pyx_obj_5pysam_6cfaidx_FastqProxy *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7comment___get__(struct __pyx_obj_5pysam_6cfaidx_FastqProxy *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7quality___get__(struct __pyx_obj_5pysam_6cfaidx_FastqProxy *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_6cfaidx_9Fastqfile___cinit__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_2_isOpen(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_4_open(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self, PyObject *__pyx_v_filename); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_6cfaidx_9Fastqfile_8__dealloc__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_8filename___get__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_10__iter__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_tp_new_5pysam_6cfaidx_Fastafile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static int __pyx_pf_5pysam_6cfaidx_9FastqFile___cinit__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_2_isOpen(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_4_open(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self, PyObject *__pyx_v_filename); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_6close(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self); /* proto */
+static void __pyx_pf_5pysam_6cfaidx_9FastqFile_8__dealloc__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_8filename___get__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_10__iter__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_12__next__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_tp_new_5pysam_6cfaidx_FastaFile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_6cfaidx_FastqProxy(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_6cfaidx_FastqFile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_6cfaidx_Fastafile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_6cfaidx_Fastqfile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_6cfaidx___pyx_scope_struct___open(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_6cfaidx___pyx_scope_struct_1_genexpr(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
@@ -1214,7 +1271,9 @@ static char __pyx_k_isOpen[] = "_isOpen";
 static char __pyx_k_open_2[] = "_open";
 static char __pyx_k_region[] = "region";
 static char __pyx_k_IOError[] = "IOError";
+static char __pyx_k_FastaFile[] = "FastaFile";
 static char __pyx_k_Fastafile[] = "Fastafile";
+static char __pyx_k_FastqFile[] = "FastqFile";
 static char __pyx_k_Fastqfile[] = "Fastqfile";
 static char __pyx_k_TypeError[] = "TypeError";
 static char __pyx_k_reference[] = "reference";
@@ -1241,7 +1300,9 @@ static char __pyx_k_number_of_term_reference_sequenc[] = "number of :term:`refer
 static char __pyx_k_tuple_with_the_lengths_of_term_r[] = "tuple with the lengths of :term:`reference` sequences.";
 static char __pyx_k_tuple_with_the_names_of_term_ref[] = "tuple with the names of :term:`reference` sequences.";
 static PyObject *__pyx_kp_u_Argument_must_be_string_or_unico;
+static PyObject *__pyx_n_s_FastaFile;
 static PyObject *__pyx_n_s_Fastafile;
+static PyObject *__pyx_n_s_FastqFile;
 static PyObject *__pyx_n_s_Fastqfile;
 static PyObject *__pyx_n_s_IOError;
 static PyObject *__pyx_n_s_IS_PYTHON3;
@@ -1527,8 +1588,8 @@ static PyObject *__pyx_f_5pysam_6cfaidx__encodeFilename(PyObject *__pyx_v_filena
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_6cfaidx_9Fastafile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_6cfaidx_9Fastafile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static int __pyx_pw_5pysam_6cfaidx_9FastaFile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_6cfaidx_9FastaFile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_args = 0;
   PyObject *__pyx_v_kwargs = 0;
   int __pyx_r;
@@ -1540,7 +1601,7 @@ static int __pyx_pw_5pysam_6cfaidx_9Fastafile_1__cinit__(PyObject *__pyx_v_self,
   __Pyx_GOTREF(__pyx_v_kwargs);
   __Pyx_INCREF(__pyx_args);
   __pyx_v_args = __pyx_args;
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile___cinit__(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self), __pyx_v_args, __pyx_v_kwargs);
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile___cinit__(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self), __pyx_v_args, __pyx_v_kwargs);
 
   /* function exit code */
   __Pyx_XDECREF(__pyx_v_args);
@@ -1549,7 +1610,7 @@ static int __pyx_pw_5pysam_6cfaidx_9Fastafile_1__cinit__(PyObject *__pyx_v_self,
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_6cfaidx_9Fastafile___cinit__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs) {
+static int __pyx_pf_5pysam_6cfaidx_9FastaFile___cinit__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -1660,7 +1721,7 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastafile___cinit__(struct __pyx_obj_5pysam_
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -1677,20 +1738,20 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastafile___cinit__(struct __pyx_obj_5pysam_
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastafile_2_isOpen[] = "Fastafile._isOpen(self)\nreturn true if samfile has been opened.";
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastaFile_2_isOpen[] = "FastaFile._isOpen(self)\nreturn true if samfile has been opened.";
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("_isOpen (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_2_isOpen(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_2_isOpen(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_2_isOpen(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_2_isOpen(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -1726,7 +1787,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_2_isOpen(struct __pyx_obj_5p
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile._isOpen", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile._isOpen", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -1740,23 +1801,23 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_2_isOpen(struct __pyx_obj_5p
  * 
  *     def __len__(self):             # <<<<<<<<<<<<<<
  *         if self.fastafile == NULL:
- *             raise ValueError( "calling len() on closed file" )
+ *             raise ValueError("calling len() on closed file")
  */
 
 /* Python wrapper */
-static Py_ssize_t __pyx_pw_5pysam_6cfaidx_9Fastafile_5__len__(PyObject *__pyx_v_self); /*proto*/
-static Py_ssize_t __pyx_pw_5pysam_6cfaidx_9Fastafile_5__len__(PyObject *__pyx_v_self) {
+static Py_ssize_t __pyx_pw_5pysam_6cfaidx_9FastaFile_5__len__(PyObject *__pyx_v_self); /*proto*/
+static Py_ssize_t __pyx_pw_5pysam_6cfaidx_9FastaFile_5__len__(PyObject *__pyx_v_self) {
   Py_ssize_t __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__len__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_4__len__(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_4__len__(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9Fastafile_4__len__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self) {
+static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9FastaFile_4__len__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self) {
   Py_ssize_t __pyx_r;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -1772,7 +1833,7 @@ static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9Fastafile_4__len__(struct __pyx_obj_5
  * 
  *     def __len__(self):
  *         if self.fastafile == NULL:             # <<<<<<<<<<<<<<
- *             raise ValueError( "calling len() on closed file" )
+ *             raise ValueError("calling len() on closed file")
  * 
  */
   __pyx_t_1 = ((__pyx_v_self->fastafile == NULL) != 0);
@@ -1781,9 +1842,9 @@ static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9Fastafile_4__len__(struct __pyx_obj_5
     /* "pysam/cfaidx.pyx":89
  *     def __len__(self):
  *         if self.fastafile == NULL:
- *             raise ValueError( "calling len() on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("calling len() on closed file")             # <<<<<<<<<<<<<<
  * 
- *         return faidx_fetch_nseq(self.fastafile)
+ *         return faidx_nseq(self.fastafile)
  */
     __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple_, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 89; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
@@ -1793,13 +1854,13 @@ static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9Fastafile_4__len__(struct __pyx_obj_5
   }
 
   /* "pysam/cfaidx.pyx":91
- *             raise ValueError( "calling len() on closed file" )
+ *             raise ValueError("calling len() on closed file")
  * 
- *         return faidx_fetch_nseq(self.fastafile)             # <<<<<<<<<<<<<<
+ *         return faidx_nseq(self.fastafile)             # <<<<<<<<<<<<<<
  * 
  *     def _open(self, filename):
  */
-  __pyx_r = faidx_fetch_nseq(__pyx_v_self->fastafile);
+  __pyx_r = faidx_nseq(__pyx_v_self->fastafile);
   goto __pyx_L0;
 
   /* "pysam/cfaidx.pyx":87
@@ -1807,13 +1868,13 @@ static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9Fastafile_4__len__(struct __pyx_obj_5
  * 
  *     def __len__(self):             # <<<<<<<<<<<<<<
  *         if self.fastafile == NULL:
- *             raise ValueError( "calling len() on closed file" )
+ *             raise ValueError("calling len() on closed file")
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile.__len__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile.__len__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -1822,7 +1883,7 @@ static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9Fastafile_4__len__(struct __pyx_obj_5
 }
 
 /* "pysam/cfaidx.pyx":93
- *         return faidx_fetch_nseq(self.fastafile)
+ *         return faidx_nseq(self.fastafile)
  * 
  *     def _open(self, filename):             # <<<<<<<<<<<<<<
  *         '''open an indexed fasta file.
@@ -1830,19 +1891,19 @@ static Py_ssize_t __pyx_pf_5pysam_6cfaidx_9Fastafile_4__len__(struct __pyx_obj_5
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_v_filename); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastafile_6_open[] = "Fastafile._open(self, filename)\nopen an indexed fasta file.\n\n        This method expects an indexed fasta file.\n        ";
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_v_filename) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_v_filename); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastaFile_6_open[] = "FastaFile._open(self, filename)\nopen an indexed fasta file.\n\n        This method expects an indexed fasta file.\n        ";
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_v_filename) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("_open (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_6_open(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self), ((PyObject *)__pyx_v_filename));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_6_open(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self), ((PyObject *)__pyx_v_filename));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
-static PyObject *__pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_2generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
+static PyObject *__pyx_gb_5pysam_6cfaidx_9FastaFile_5_open_2generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
 
 /* "pysam/cfaidx.pyx":113
  *         with open( self._filename + b".fai" ) as inf:
@@ -1852,7 +1913,7 @@ static PyObject *__pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_2generator(__pyx_Gene
  *             self.reference2length = dict(zip(self._references, self._lengths))
  */
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_genexpr(PyObject *__pyx_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_5_open_genexpr(PyObject *__pyx_self) {
   struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct_1_genexpr *__pyx_cur_scope;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -1872,7 +1933,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_genexpr(PyObject *__p
   __Pyx_GIVEREF(__pyx_cur_scope->__pyx_outer_scope);
   __Pyx_TraceCall("genexpr", __pyx_f[0], 113);
   {
-    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_2generator, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_6cfaidx_9FastaFile_5_open_2generator, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_cur_scope);
     __Pyx_RefNannyFinishContext();
     return (PyObject *) gen;
@@ -1882,7 +1943,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_genexpr(PyObject *__p
   __pyx_r = Py_None; __Pyx_INCREF(Py_None);
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile._open.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile._open.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_DECREF(((PyObject *)__pyx_cur_scope));
@@ -1892,7 +1953,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_genexpr(PyObject *__p
   return __pyx_r;
 }
 
-static PyObject *__pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_2generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
+static PyObject *__pyx_gb_5pysam_6cfaidx_9FastaFile_5_open_2generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
 {
   struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct_1_genexpr *__pyx_cur_scope = ((struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct_1_genexpr *)__pyx_generator->closure);
   PyObject *__pyx_r = NULL;
@@ -1965,7 +2026,7 @@ static PyObject *__pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_2generator(__pyx_Gene
   __Pyx_RefNannyFinishContext();
   return NULL;
 }
-static PyObject *__pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_5generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
+static PyObject *__pyx_gb_5pysam_6cfaidx_9FastaFile_5_open_5generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
 
 /* "pysam/cfaidx.pyx":114
  *             data = [ x.split("\t") for x in inf ]
@@ -1975,7 +2036,7 @@ static PyObject *__pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_5generator1(__pyx_Gen
  * 
  */
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_3genexpr(PyObject *__pyx_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_5_open_3genexpr(PyObject *__pyx_self) {
   struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct_2_genexpr *__pyx_cur_scope;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -1995,7 +2056,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_3genexpr(PyObject *__
   __Pyx_GIVEREF(__pyx_cur_scope->__pyx_outer_scope);
   __Pyx_TraceCall("genexpr", __pyx_f[0], 114);
   {
-    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_5generator1, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 114; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_6cfaidx_9FastaFile_5_open_5generator1, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 114; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_cur_scope);
     __Pyx_RefNannyFinishContext();
     return (PyObject *) gen;
@@ -2005,7 +2066,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_3genexpr(PyObject *__
   __pyx_r = Py_None; __Pyx_INCREF(Py_None);
   goto __pyx_L0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile._open.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile._open.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_DECREF(((PyObject *)__pyx_cur_scope));
@@ -2015,7 +2076,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_3genexpr(PyObject *__
   return __pyx_r;
 }
 
-static PyObject *__pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_5generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
+static PyObject *__pyx_gb_5pysam_6cfaidx_9FastaFile_5_open_5generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
 {
   struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct_2_genexpr *__pyx_cur_scope = ((struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct_2_genexpr *)__pyx_generator->closure);
   PyObject *__pyx_r = NULL;
@@ -2095,14 +2156,14 @@ static PyObject *__pyx_gb_5pysam_6cfaidx_9Fastafile_5_open_5generator1(__pyx_Gen
 }
 
 /* "pysam/cfaidx.pyx":93
- *         return faidx_fetch_nseq(self.fastafile)
+ *         return faidx_nseq(self.fastafile)
  * 
  *     def _open(self, filename):             # <<<<<<<<<<<<<<
  *         '''open an indexed fasta file.
  * 
  */
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_6_open(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_filename) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_6_open(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_filename) {
   struct __pyx_obj_5pysam_6cfaidx___pyx_scope_struct___open *__pyx_cur_scope;
   PyObject *__pyx_v_inf = NULL;
   PyObject *__pyx_v_x = NULL;
@@ -2359,7 +2420,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_6_open(struct __pyx_obj_5pys
  *             self._lengths = tuple(int(x[1]) for x in data)
  *             self.reference2length = dict(zip(self._references, self._lengths))
  */
-          __pyx_t_3 = __pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_genexpr(((PyObject*)__pyx_cur_scope)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
+          __pyx_t_3 = __pyx_pf_5pysam_6cfaidx_9FastaFile_5_open_genexpr(((PyObject*)__pyx_cur_scope)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
           __Pyx_GOTREF(__pyx_t_3);
           __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
           __Pyx_GOTREF(__pyx_t_2);
@@ -2382,7 +2443,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_6_open(struct __pyx_obj_5pys
  *             self.reference2length = dict(zip(self._references, self._lengths))
  * 
  */
-          __pyx_t_3 = __pyx_pf_5pysam_6cfaidx_9Fastafile_5_open_3genexpr(((PyObject*)__pyx_cur_scope)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 114; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
+          __pyx_t_3 = __pyx_pf_5pysam_6cfaidx_9FastaFile_5_open_3genexpr(((PyObject*)__pyx_cur_scope)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 114; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
           __Pyx_GOTREF(__pyx_t_3);
           __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 114; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
           __Pyx_GOTREF(__pyx_t_2);
@@ -2448,7 +2509,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_6_open(struct __pyx_obj_5pys
  *             self._references = tuple(x[0] for x in data)
  */
         /*except:*/ {
-          __Pyx_AddTraceback("pysam.cfaidx.Fastafile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
+          __Pyx_AddTraceback("pysam.cfaidx.FastaFile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
           if (__Pyx_GetException(&__pyx_t_2, &__pyx_t_3, &__pyx_t_13) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_2);
           __Pyx_GOTREF(__pyx_t_3);
@@ -2512,7 +2573,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_6_open(struct __pyx_obj_5pys
   }
 
   /* "pysam/cfaidx.pyx":93
- *         return faidx_fetch_nseq(self.fastafile)
+ *         return faidx_nseq(self.fastafile)
  * 
  *     def _open(self, filename):             # <<<<<<<<<<<<<<
  *         '''open an indexed fasta file.
@@ -2527,7 +2588,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_6_open(struct __pyx_obj_5pys
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_5);
   __Pyx_XDECREF(__pyx_t_13);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_inf);
@@ -2548,20 +2609,20 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_6_open(struct __pyx_obj_5pys
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_9close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastafile_8close[] = "Fastafile.close(self)";
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_9close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_9close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastaFile_8close[] = "FastaFile.close(self)";
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_9close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("close (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_8close(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_8close(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_8close(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_8close(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -2625,17 +2686,17 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_8close(struct __pyx_obj_5pys
  */
 
 /* Python wrapper */
-static void __pyx_pw_5pysam_6cfaidx_9Fastafile_11__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_6cfaidx_9Fastafile_11__dealloc__(PyObject *__pyx_v_self) {
+static void __pyx_pw_5pysam_6cfaidx_9FastaFile_11__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_6cfaidx_9FastaFile_11__dealloc__(PyObject *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_6cfaidx_9Fastafile_10__dealloc__(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self));
+  __pyx_pf_5pysam_6cfaidx_9FastaFile_10__dealloc__(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
 }
 
-static void __pyx_pf_5pysam_6cfaidx_9Fastafile_10__dealloc__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self) {
+static void __pyx_pf_5pysam_6cfaidx_9FastaFile_10__dealloc__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
@@ -2673,7 +2734,7 @@ static void __pyx_pf_5pysam_6cfaidx_9Fastafile_10__dealloc__(struct __pyx_obj_5p
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_WriteUnraisable("pysam.cfaidx.Fastafile.__dealloc__", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __Pyx_WriteUnraisable("pysam.cfaidx.FastaFile.__dealloc__", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
@@ -2688,19 +2749,19 @@ static void __pyx_pf_5pysam_6cfaidx_9Fastafile_10__dealloc__(struct __pyx_obj_5p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_8filename_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_8filename_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_8filename_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_8filename_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_8filename___get__(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_8filename___get__(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_8filename___get__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_8filename___get__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
@@ -2744,19 +2805,19 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_8filename___get__(struct __p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_10references_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_10references_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_10references_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_10references_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_10references___get__(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_10references___get__(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_10references___get__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_10references___get__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
@@ -2800,19 +2861,19 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_10references___get__(struct
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_11nreferences_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_11nreferences_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_11nreferences_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_11nreferences_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_11nreferences___get__(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_11nreferences___get__(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_11nreferences___get__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_11nreferences___get__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -2867,7 +2928,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_11nreferences___get__(struct
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile.nreferences.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile.nreferences.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -2885,19 +2946,19 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_11nreferences___get__(struct
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_7lengths_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_7lengths_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_7lengths_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_7lengths_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_7lengths___get__(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_7lengths___get__(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_7lengths___get__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_7lengths___get__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
@@ -2909,7 +2970,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_7lengths___get__(struct __py
  *         def __get__(self):
  *             return self._lengths             # <<<<<<<<<<<<<<
  * 
- *     def fetch( self,
+ *     def fetch(self,
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(__pyx_v_self->_lengths);
@@ -2935,15 +2996,15 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_7lengths___get__(struct __py
 /* "pysam/cfaidx.pyx":145
  *             return self._lengths
  * 
- *     def fetch( self,             # <<<<<<<<<<<<<<
- *                reference = None,
- *                start = None,
+ *     def fetch(self,             # <<<<<<<<<<<<<<
+ *               reference=None,
+ *               start=None,
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_13fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastafile_12fetch[] = "Fastafile.fetch(self, reference=None, start=None, end=None, region=None)\n*(reference = None, start = None, end = None, region = None)*\n\n        fetch :class:`AlignedRead` objects in a :term:`region` using 0-based indexing.\n\n        The region is specified by :term:`reference`, *start* and *end*.\n\n        fetch returns an empty string if the region is out of range or addresses an unknown *reference*.\n\n        If *refere [...]
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_13fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_13fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastaFile_12fetch[] = "FastaFile.fetch(self, reference=None, start=None, end=None, region=None)\n*(reference = None, start = None, end = None, region = None)*\n\n        fetch sequences in a :term:`region` using 0-based indexing.\n\n        The region is specified by :term:`reference`, *start* and *end*.\n\n        fetch returns an empty string if the region is out of range or\n        addresses an unknown *reference*.\n\n        If *reference* is gi [...]
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_13fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_reference = 0;
   PyObject *__pyx_v_start = 0;
   PyObject *__pyx_v_end = 0;
@@ -2960,35 +3021,35 @@ static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_13fetch(PyObject *__pyx_v_se
 
     /* "pysam/cfaidx.pyx":146
  * 
- *     def fetch( self,
- *                reference = None,             # <<<<<<<<<<<<<<
- *                start = None,
- *                end = None,
+ *     def fetch(self,
+ *               reference=None,             # <<<<<<<<<<<<<<
+ *               start=None,
+ *               end=None,
  */
     values[0] = ((PyObject *)Py_None);
 
     /* "pysam/cfaidx.pyx":147
- *     def fetch( self,
- *                reference = None,
- *                start = None,             # <<<<<<<<<<<<<<
- *                end = None,
- *                region = None):
+ *     def fetch(self,
+ *               reference=None,
+ *               start=None,             # <<<<<<<<<<<<<<
+ *               end=None,
+ *               region=None):
  */
     values[1] = ((PyObject *)Py_None);
 
     /* "pysam/cfaidx.pyx":148
- *                reference = None,
- *                start = None,
- *                end = None,             # <<<<<<<<<<<<<<
- *                region = None):
+ *               reference=None,
+ *               start=None,
+ *               end=None,             # <<<<<<<<<<<<<<
+ *               region=None):
  * 
  */
     values[2] = ((PyObject *)Py_None);
 
     /* "pysam/cfaidx.pyx":149
- *                start = None,
- *                end = None,
- *                region = None):             # <<<<<<<<<<<<<<
+ *               start=None,
+ *               end=None,
+ *               region=None):             # <<<<<<<<<<<<<<
  * 
  *         '''*(reference = None, start = None, end = None, region = None)*
  */
@@ -3049,18 +3110,18 @@ static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_13fetch(PyObject *__pyx_v_se
   __pyx_L5_argtuple_error:;
   __Pyx_RaiseArgtupleInvalid("fetch", 0, 0, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 145; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region);
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_12fetch(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region);
 
   /* "pysam/cfaidx.pyx":145
  *             return self._lengths
  * 
- *     def fetch( self,             # <<<<<<<<<<<<<<
- *                reference = None,
- *                start = None,
+ *     def fetch(self,             # <<<<<<<<<<<<<<
+ *               reference=None,
+ *               start=None,
  */
 
   /* function exit code */
@@ -3068,7 +3129,7 @@ static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_13fetch(PyObject *__pyx_v_se
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_12fetch(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region) {
   int __pyx_v_length;
   char *__pyx_v_seq;
   PyObject *__pyx_v_py_seq = NULL;
@@ -3098,49 +3159,49 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
   __Pyx_INCREF(__pyx_v_end);
   __Pyx_INCREF(__pyx_v_region);
 
-  /* "pysam/cfaidx.pyx":166
+  /* "pysam/cfaidx.pyx":167
  *         '''
  * 
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError( "I/O operation on closed file" )
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/cfaidx.pyx":167
+    /* "pysam/cfaidx.pyx":168
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         cdef int length
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/cfaidx.pyx":172
+  /* "pysam/cfaidx.pyx":173
  *         cdef char * seq
  * 
  *         if not region:             # <<<<<<<<<<<<<<
  *             if reference is None:
  *                 raise ValueError('no sequence/region supplied.')
  */
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_region); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_region); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_3 = ((!__pyx_t_4) != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/cfaidx.pyx":173
+    /* "pysam/cfaidx.pyx":174
  * 
  *         if not region:
  *             if reference is None:             # <<<<<<<<<<<<<<
@@ -3151,21 +3212,21 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
     __pyx_t_4 = (__pyx_t_3 != 0);
     if (__pyx_t_4) {
 
-      /* "pysam/cfaidx.pyx":174
+      /* "pysam/cfaidx.pyx":175
  *         if not region:
  *             if reference is None:
  *                 raise ValueError('no sequence/region supplied.')             # <<<<<<<<<<<<<<
  *             if start is None:
  *                 start = 0
  */
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_Raise(__pyx_t_2, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/cfaidx.pyx":175
+    /* "pysam/cfaidx.pyx":176
  *             if reference is None:
  *                 raise ValueError('no sequence/region supplied.')
  *             if start is None:             # <<<<<<<<<<<<<<
@@ -3176,7 +3237,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
     __pyx_t_3 = (__pyx_t_4 != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/cfaidx.pyx":176
+      /* "pysam/cfaidx.pyx":177
  *                 raise ValueError('no sequence/region supplied.')
  *             if start is None:
  *                 start = 0             # <<<<<<<<<<<<<<
@@ -3189,7 +3250,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
     }
     __pyx_L6:;
 
-    /* "pysam/cfaidx.pyx":177
+    /* "pysam/cfaidx.pyx":178
  *             if start is None:
  *                 start = 0
  *             if end is None:             # <<<<<<<<<<<<<<
@@ -3200,14 +3261,14 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
     __pyx_t_4 = (__pyx_t_3 != 0);
     if (__pyx_t_4) {
 
-      /* "pysam/cfaidx.pyx":178
+      /* "pysam/cfaidx.pyx":179
  *                 start = 0
  *             if end is None:
  *                 end = max_pos - 1             # <<<<<<<<<<<<<<
  * 
  *             if start > end:
  */
-      __pyx_t_2 = __Pyx_PyInt_From_long((__pyx_v_5pysam_6cfaidx_max_pos - 1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyInt_From_long((__pyx_v_5pysam_6cfaidx_max_pos - 1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF_SET(__pyx_v_end, __pyx_t_2);
       __pyx_t_2 = 0;
@@ -3215,26 +3276,26 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
     }
     __pyx_L7:;
 
-    /* "pysam/cfaidx.pyx":180
+    /* "pysam/cfaidx.pyx":181
  *                 end = max_pos - 1
  * 
  *             if start > end:             # <<<<<<<<<<<<<<
  *                 raise ValueError(
  *                     'invalid region: start (%i) > end (%i)' % (start, end))
  */
-    __pyx_t_2 = PyObject_RichCompare(__pyx_v_start, __pyx_v_end, Py_GT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyObject_RichCompare(__pyx_v_start, __pyx_v_end, Py_GT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (__pyx_t_4) {
 
-      /* "pysam/cfaidx.pyx":182
+      /* "pysam/cfaidx.pyx":183
  *             if start > end:
  *                 raise ValueError(
  *                     'invalid region: start (%i) > end (%i)' % (start, end))             # <<<<<<<<<<<<<<
  *             if start == end:
  *                 return b""
  */
-      __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_INCREF(__pyx_v_start);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_start);
@@ -3242,43 +3303,43 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
       __Pyx_INCREF(__pyx_v_end);
       PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_end);
       __Pyx_GIVEREF(__pyx_v_end);
-      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_invalid_region_start_i_end_i, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_invalid_region_start_i_end_i, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-      /* "pysam/cfaidx.pyx":181
+      /* "pysam/cfaidx.pyx":182
  * 
  *             if start > end:
  *                 raise ValueError(             # <<<<<<<<<<<<<<
  *                     'invalid region: start (%i) > end (%i)' % (start, end))
  *             if start == end:
  */
-      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
       __Pyx_GIVEREF(__pyx_t_1);
       __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_Raise(__pyx_t_1, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/cfaidx.pyx":183
+    /* "pysam/cfaidx.pyx":184
  *                 raise ValueError(
  *                     'invalid region: start (%i) > end (%i)' % (start, end))
  *             if start == end:             # <<<<<<<<<<<<<<
  *                 return b""
  *             # valid ranges are from 0 to 2^29-1
  */
-    __pyx_t_1 = PyObject_RichCompare(__pyx_v_start, __pyx_v_end, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyObject_RichCompare(__pyx_v_start, __pyx_v_end, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (__pyx_t_4) {
 
-      /* "pysam/cfaidx.pyx":184
+      /* "pysam/cfaidx.pyx":185
  *                     'invalid region: start (%i) > end (%i)' % (start, end))
  *             if start == end:
  *                 return b""             # <<<<<<<<<<<<<<
@@ -3291,100 +3352,100 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
       goto __pyx_L0;
     }
 
-    /* "pysam/cfaidx.pyx":186
+    /* "pysam/cfaidx.pyx":187
  *                 return b""
  *             # valid ranges are from 0 to 2^29-1
  *             if not 0 <= start < max_pos:             # <<<<<<<<<<<<<<
  *                 raise IndexError('start out of range (%i)' % start)
  *             if not 0 <= end < max_pos:
  */
-    __pyx_t_1 = PyObject_RichCompare(__pyx_int_0, __pyx_v_start, Py_LE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyObject_RichCompare(__pyx_int_0, __pyx_v_start, Py_LE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__Pyx_PyObject_IsTrue(__pyx_t_1)) {
       __Pyx_DECREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_5pysam_6cfaidx_max_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_5pysam_6cfaidx_max_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = PyObject_RichCompare(__pyx_v_start, __pyx_t_2, Py_LT); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyObject_RichCompare(__pyx_v_start, __pyx_t_2, Py_LT); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     }
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_t_3 = ((!__pyx_t_4) != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/cfaidx.pyx":187
+      /* "pysam/cfaidx.pyx":188
  *             # valid ranges are from 0 to 2^29-1
  *             if not 0 <= start < max_pos:
  *                 raise IndexError('start out of range (%i)' % start)             # <<<<<<<<<<<<<<
  *             if not 0 <= end < max_pos:
  *                 raise IndexError('end out of range (%i)' % end)
  */
-      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_start_out_of_range_i, __pyx_v_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_start_out_of_range_i, __pyx_v_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
       __Pyx_GIVEREF(__pyx_t_1);
       __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_Raise(__pyx_t_1, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/cfaidx.pyx":188
+    /* "pysam/cfaidx.pyx":189
  *             if not 0 <= start < max_pos:
  *                 raise IndexError('start out of range (%i)' % start)
  *             if not 0 <= end < max_pos:             # <<<<<<<<<<<<<<
  *                 raise IndexError('end out of range (%i)' % end)
  *             # note: faidx_fetch_seq has a bug such that out-of-range access
  */
-    __pyx_t_1 = PyObject_RichCompare(__pyx_int_0, __pyx_v_end, Py_LE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyObject_RichCompare(__pyx_int_0, __pyx_v_end, Py_LE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__Pyx_PyObject_IsTrue(__pyx_t_1)) {
       __Pyx_DECREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_5pysam_6cfaidx_max_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_5pysam_6cfaidx_max_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = PyObject_RichCompare(__pyx_v_end, __pyx_t_2, Py_LT); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyObject_RichCompare(__pyx_v_end, __pyx_t_2, Py_LT); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     }
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_t_4 = ((!__pyx_t_3) != 0);
     if (__pyx_t_4) {
 
-      /* "pysam/cfaidx.pyx":189
+      /* "pysam/cfaidx.pyx":190
  *                 raise IndexError('start out of range (%i)' % start)
  *             if not 0 <= end < max_pos:
  *                 raise IndexError('end out of range (%i)' % end)             # <<<<<<<<<<<<<<
  *             # note: faidx_fetch_seq has a bug such that out-of-range access
  *             # always returns the last residue. Hence do not use faidx_fetch_seq,
  */
-      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_end_out_of_range_i, __pyx_v_end); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_end_out_of_range_i, __pyx_v_end); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
       __Pyx_GIVEREF(__pyx_t_1);
       __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_IndexError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_Raise(__pyx_t_1, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/cfaidx.pyx":198
+    /* "pysam/cfaidx.pyx":199
  *             #                       end-1,
  *             #                       &length)
  *             region = "%s:%i-%i" % (reference, start+1, end)             # <<<<<<<<<<<<<<
  *             if PY_MAJOR_VERSION >= 3:
  *                 region = region.encode('ascii')
  */
-    __pyx_t_1 = PyNumber_Add(__pyx_v_start, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_Add(__pyx_v_start, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 199; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 199; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_v_reference);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_reference);
@@ -3395,13 +3456,13 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
     PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_end);
     __Pyx_GIVEREF(__pyx_v_end);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_s_i_i, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_s_i_i, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 199; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF_SET(__pyx_v_region, __pyx_t_1);
     __pyx_t_1 = 0;
 
-    /* "pysam/cfaidx.pyx":199
+    /* "pysam/cfaidx.pyx":200
  *             #                       &length)
  *             region = "%s:%i-%i" % (reference, start+1, end)
  *             if PY_MAJOR_VERSION >= 3:             # <<<<<<<<<<<<<<
@@ -3411,16 +3472,16 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
     __pyx_t_4 = ((PY_MAJOR_VERSION >= 3) != 0);
     if (__pyx_t_4) {
 
-      /* "pysam/cfaidx.pyx":200
+      /* "pysam/cfaidx.pyx":201
  *             region = "%s:%i-%i" % (reference, start+1, end)
  *             if PY_MAJOR_VERSION >= 3:
  *                 region = region.encode('ascii')             # <<<<<<<<<<<<<<
  *             seq = fai_fetch( self.fastafile,
  *                              region,
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_region, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_region, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF_SET(__pyx_v_region, __pyx_t_2);
@@ -3429,16 +3490,16 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
     }
     __pyx_L12:;
 
-    /* "pysam/cfaidx.pyx":202
+    /* "pysam/cfaidx.pyx":203
  *                 region = region.encode('ascii')
  *             seq = fai_fetch( self.fastafile,
  *                              region,             # <<<<<<<<<<<<<<
  *                              &length )
  *         else:
  */
-    __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_region); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_region); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/cfaidx.pyx":201
+    /* "pysam/cfaidx.pyx":202
  *             if PY_MAJOR_VERSION >= 3:
  *                 region = region.encode('ascii')
  *             seq = fai_fetch( self.fastafile,             # <<<<<<<<<<<<<<
@@ -3450,19 +3511,19 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
   }
   /*else*/ {
 
-    /* "pysam/cfaidx.pyx":206
+    /* "pysam/cfaidx.pyx":207
  *         else:
  *             # samtools adds a '\0' at the end
  *             seq = fai_fetch( self.fastafile, region, &length )             # <<<<<<<<<<<<<<
  * 
  *         # copy to python
  */
-    __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_region); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_region); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_v_seq = fai_fetch(__pyx_v_self->fastafile, __pyx_t_5, (&__pyx_v_length));
   }
   __pyx_L4:;
 
-  /* "pysam/cfaidx.pyx":209
+  /* "pysam/cfaidx.pyx":210
  * 
  *         # copy to python
  *         if seq == NULL:             # <<<<<<<<<<<<<<
@@ -3472,7 +3533,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
   __pyx_t_4 = ((__pyx_v_seq == NULL) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/cfaidx.pyx":210
+    /* "pysam/cfaidx.pyx":211
  *         # copy to python
  *         if seq == NULL:
  *             return b""             # <<<<<<<<<<<<<<
@@ -3486,7 +3547,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
   }
   /*else*/ {
 
-    /* "pysam/cfaidx.pyx":212
+    /* "pysam/cfaidx.pyx":213
  *             return b""
  *         else:
  *             try:             # <<<<<<<<<<<<<<
@@ -3495,20 +3556,20 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
  */
     /*try:*/ {
 
-      /* "pysam/cfaidx.pyx":213
+      /* "pysam/cfaidx.pyx":214
  *         else:
  *             try:
  *                 py_seq = seq[:length]             # <<<<<<<<<<<<<<
  *             finally:
  *                 free(seq)
  */
-      __pyx_t_2 = __Pyx_PyBytes_FromStringAndSize(__pyx_v_seq + 0, __pyx_v_length - 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L15_error;}
+      __pyx_t_2 = __Pyx_PyBytes_FromStringAndSize(__pyx_v_seq + 0, __pyx_v_length - 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L15_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __pyx_v_py_seq = ((PyObject*)__pyx_t_2);
       __pyx_t_2 = 0;
     }
 
-    /* "pysam/cfaidx.pyx":215
+    /* "pysam/cfaidx.pyx":216
  *                 py_seq = seq[:length]
  *             finally:
  *                 free(seq)             # <<<<<<<<<<<<<<
@@ -3555,7 +3616,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
     }
   }
 
-  /* "pysam/cfaidx.pyx":217
+  /* "pysam/cfaidx.pyx":218
  *                 free(seq)
  * 
  *         return py_seq             # <<<<<<<<<<<<<<
@@ -3570,16 +3631,16 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
   /* "pysam/cfaidx.pyx":145
  *             return self._lengths
  * 
- *     def fetch( self,             # <<<<<<<<<<<<<<
- *                reference = None,
- *                start = None,
+ *     def fetch(self,             # <<<<<<<<<<<<<<
+ *               reference=None,
+ *               start=None,
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_py_seq);
@@ -3592,7 +3653,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":219
+/* "pysam/cfaidx.pyx":220
  *         return py_seq
  * 
  *     cdef char * _fetch( self, char * reference, int start, int end, int * length ):             # <<<<<<<<<<<<<<
@@ -3600,14 +3661,14 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_12fetch(struct __pyx_obj_5py
  * 
  */
 
-static char *__pyx_f_5pysam_6cfaidx_9Fastafile__fetch(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, char *__pyx_v_reference, int __pyx_v_start, int __pyx_v_end, int *__pyx_v_length) {
+static char *__pyx_f_5pysam_6cfaidx_9FastaFile__fetch(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, char *__pyx_v_reference, int __pyx_v_start, int __pyx_v_end, int *__pyx_v_length) {
   char *__pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_fetch", 0);
-  __Pyx_TraceCall("_fetch", __pyx_f[0], 219);
+  __Pyx_TraceCall("_fetch", __pyx_f[0], 220);
 
-  /* "pysam/cfaidx.pyx":222
+  /* "pysam/cfaidx.pyx":223
  *         '''fetch sequence for reference, start and end'''
  * 
  *         return faidx_fetch_seq(self.fastafile,             # <<<<<<<<<<<<<<
@@ -3617,7 +3678,7 @@ static char *__pyx_f_5pysam_6cfaidx_9Fastafile__fetch(struct __pyx_obj_5pysam_6c
   __pyx_r = faidx_fetch_seq(__pyx_v_self->fastafile, __pyx_v_reference, __pyx_v_start, (__pyx_v_end - 1), __pyx_v_length);
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":219
+  /* "pysam/cfaidx.pyx":220
  *         return py_seq
  * 
  *     cdef char * _fetch( self, char * reference, int start, int end, int * length ):             # <<<<<<<<<<<<<<
@@ -3632,29 +3693,29 @@ static char *__pyx_f_5pysam_6cfaidx_9Fastafile__fetch(struct __pyx_obj_5pysam_6c
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":228
+/* "pysam/cfaidx.pyx":229
  *                                length )
  * 
- *     def getReferenceLength( self, reference ):             # <<<<<<<<<<<<<<
+ *     def get_reference_length(self, reference):             # <<<<<<<<<<<<<<
  *         '''return the length of reference.'''
  *         return self.reference2length[reference]
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_15getReferenceLength(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastafile_14getReferenceLength[] = "Fastafile.getReferenceLength(self, reference)\nreturn the length of reference.";
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_15getReferenceLength(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_15get_reference_length(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastaFile_14get_reference_length[] = "FastaFile.get_reference_length(self, reference)\nreturn the length of reference.";
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_15get_reference_length(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("getReferenceLength (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_14getReferenceLength(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self), ((PyObject *)__pyx_v_reference));
+  __Pyx_RefNannySetupContext("get_reference_length (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_14get_reference_length(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self), ((PyObject *)__pyx_v_reference));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_14getReferenceLength(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_reference) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_14get_reference_length(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_reference) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -3662,27 +3723,27 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_14getReferenceLength(struct
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("getReferenceLength", 0);
-  __Pyx_TraceCall("getReferenceLength", __pyx_f[0], 228);
+  __Pyx_RefNannySetupContext("get_reference_length", 0);
+  __Pyx_TraceCall("get_reference_length", __pyx_f[0], 229);
 
-  /* "pysam/cfaidx.pyx":230
- *     def getReferenceLength( self, reference ):
+  /* "pysam/cfaidx.pyx":231
+ *     def get_reference_length(self, reference):
  *         '''return the length of reference.'''
  *         return self.reference2length[reference]             # <<<<<<<<<<<<<<
  * 
  *     def __getitem__(self, reference):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = PyObject_GetItem(__pyx_v_self->reference2length, __pyx_v_reference); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 230; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_1 = PyObject_GetItem(__pyx_v_self->reference2length, __pyx_v_reference); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 231; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":228
+  /* "pysam/cfaidx.pyx":229
  *                                length )
  * 
- *     def getReferenceLength( self, reference ):             # <<<<<<<<<<<<<<
+ *     def get_reference_length(self, reference):             # <<<<<<<<<<<<<<
  *         '''return the length of reference.'''
  *         return self.reference2length[reference]
  */
@@ -3690,7 +3751,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_14getReferenceLength(struct
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile.getReferenceLength", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile.get_reference_length", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -3699,7 +3760,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_14getReferenceLength(struct
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":232
+/* "pysam/cfaidx.pyx":233
  *         return self.reference2length[reference]
  * 
  *     def __getitem__(self, reference):             # <<<<<<<<<<<<<<
@@ -3708,19 +3769,19 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_14getReferenceLength(struct
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_17__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastafile_17__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_17__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastaFile_17__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__getitem__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_16__getitem__(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self), ((PyObject *)__pyx_v_reference));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_16__getitem__(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self), ((PyObject *)__pyx_v_reference));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_16__getitem__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_reference) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastaFile_16__getitem__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_reference) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -3731,24 +3792,24 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_16__getitem__(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__getitem__", 0);
-  __Pyx_TraceCall("__getitem__", __pyx_f[0], 232);
+  __Pyx_TraceCall("__getitem__", __pyx_f[0], 233);
 
-  /* "pysam/cfaidx.pyx":233
+  /* "pysam/cfaidx.pyx":234
  * 
  *     def __getitem__(self, reference):
  *         return self.fetch(reference)             # <<<<<<<<<<<<<<
  * 
- *     def __contains__( self, reference ):
+ *     def __contains__(self, reference):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fetch); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fetch); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_reference);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_reference);
   __Pyx_GIVEREF(__pyx_v_reference);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -3756,7 +3817,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_16__getitem__(struct __pyx_o
   __pyx_t_3 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":232
+  /* "pysam/cfaidx.pyx":233
  *         return self.reference2length[reference]
  * 
  *     def __getitem__(self, reference):             # <<<<<<<<<<<<<<
@@ -3769,7 +3830,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_16__getitem__(struct __pyx_o
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -3778,32 +3839,32 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastafile_16__getitem__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":235
+/* "pysam/cfaidx.pyx":236
  *         return self.fetch(reference)
  * 
- *     def __contains__( self, reference ):             # <<<<<<<<<<<<<<
+ *     def __contains__(self, reference):             # <<<<<<<<<<<<<<
  *         '''return true if reference in fasta file.'''
  *         return reference in self.reference2length
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_6cfaidx_9Fastafile_19__contains__(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastafile_18__contains__[] = "return true if reference in fasta file.";
+static int __pyx_pw_5pysam_6cfaidx_9FastaFile_19__contains__(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastaFile_18__contains__[] = "return true if reference in fasta file.";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_6cfaidx_9Fastafile_18__contains__;
+struct wrapperbase __pyx_wrapperbase_5pysam_6cfaidx_9FastaFile_18__contains__;
 #endif
-static int __pyx_pw_5pysam_6cfaidx_9Fastafile_19__contains__(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) {
+static int __pyx_pw_5pysam_6cfaidx_9FastaFile_19__contains__(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__contains__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastafile_18__contains__(((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_self), ((PyObject *)__pyx_v_reference));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastaFile_18__contains__(((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)__pyx_v_self), ((PyObject *)__pyx_v_reference));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_6cfaidx_9Fastafile_18__contains__(struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_self, PyObject *__pyx_v_reference) {
+static int __pyx_pf_5pysam_6cfaidx_9FastaFile_18__contains__(struct __pyx_obj_5pysam_6cfaidx_FastaFile *__pyx_v_self, PyObject *__pyx_v_reference) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -3812,30 +3873,30 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastafile_18__contains__(struct __pyx_obj_5p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__contains__", 0);
-  __Pyx_TraceCall("__contains__", __pyx_f[0], 235);
+  __Pyx_TraceCall("__contains__", __pyx_f[0], 236);
 
-  /* "pysam/cfaidx.pyx":237
- *     def __contains__( self, reference ):
+  /* "pysam/cfaidx.pyx":238
+ *     def __contains__(self, reference):
  *         '''return true if reference in fasta file.'''
  *         return reference in self.reference2length             # <<<<<<<<<<<<<<
  * 
  * ######################################################################
  */
-  __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_v_reference, __pyx_v_self->reference2length, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_v_reference, __pyx_v_self->reference2length, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_r = __pyx_t_1;
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":235
+  /* "pysam/cfaidx.pyx":236
  *         return self.fetch(reference)
  * 
- *     def __contains__( self, reference ):             # <<<<<<<<<<<<<<
+ *     def __contains__(self, reference):             # <<<<<<<<<<<<<<
  *         '''return true if reference in fasta file.'''
  *         return reference in self.reference2length
  */
 
   /* function exit code */
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.cfaidx.Fastafile.__contains__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastaFile.__contains__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -3843,7 +3904,7 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastafile_18__contains__(struct __pyx_obj_5p
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":246
+/* "pysam/cfaidx.pyx":247
  * 
  * cdef class FastqProxy:
  *     def __init__(self): pass             # <<<<<<<<<<<<<<
@@ -3872,7 +3933,7 @@ static int __pyx_pf_5pysam_6cfaidx_10FastqProxy___init__(CYTHON_UNUSED struct __
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 246);
+  __Pyx_TraceCall("__init__", __pyx_f[0], 247);
 
   /* function exit code */
   __pyx_r = 0;
@@ -3881,7 +3942,7 @@ static int __pyx_pf_5pysam_6cfaidx_10FastqProxy___init__(CYTHON_UNUSED struct __
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":249
+/* "pysam/cfaidx.pyx":250
  * 
  *     property name:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -3911,9 +3972,9 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_4name___get__(struct __pyx
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 249);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 250);
 
-  /* "pysam/cfaidx.pyx":250
+  /* "pysam/cfaidx.pyx":251
  *     property name:
  *         def __get__(self):
  *             return self._delegate.name.s             # <<<<<<<<<<<<<<
@@ -3921,13 +3982,13 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_4name___get__(struct __pyx
  *     property sequence:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_self->_delegate->name.s); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 250; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_self->_delegate->name.s); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":249
+  /* "pysam/cfaidx.pyx":250
  * 
  *     property name:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -3947,7 +4008,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_4name___get__(struct __pyx
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":253
+/* "pysam/cfaidx.pyx":254
  * 
  *     property sequence:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -3977,9 +4038,9 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_8sequence___get__(struct _
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 253);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 254);
 
-  /* "pysam/cfaidx.pyx":254
+  /* "pysam/cfaidx.pyx":255
  *     property sequence:
  *         def __get__(self):
  *             return self._delegate.seq.s             # <<<<<<<<<<<<<<
@@ -3987,13 +4048,13 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_8sequence___get__(struct _
  *     property comment:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_self->_delegate->seq.s); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_self->_delegate->seq.s); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 255; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":253
+  /* "pysam/cfaidx.pyx":254
  * 
  *     property sequence:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4013,7 +4074,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_8sequence___get__(struct _
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":257
+/* "pysam/cfaidx.pyx":258
  * 
  *     property comment:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4044,9 +4105,9 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7comment___get__(struct __
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 257);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 258);
 
-  /* "pysam/cfaidx.pyx":258
+  /* "pysam/cfaidx.pyx":259
  *     property comment:
  *         def __get__(self):
  *             if self._delegate.comment.l:             # <<<<<<<<<<<<<<
@@ -4056,7 +4117,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7comment___get__(struct __
   __pyx_t_1 = (__pyx_v_self->_delegate->comment.l != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/cfaidx.pyx":259
+    /* "pysam/cfaidx.pyx":260
  *         def __get__(self):
  *             if self._delegate.comment.l:
  *                 return self._delegate.comment.s             # <<<<<<<<<<<<<<
@@ -4064,7 +4125,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7comment___get__(struct __
  * 
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->_delegate->comment.s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->_delegate->comment.s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 260; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -4072,7 +4133,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7comment___get__(struct __
   }
   /*else*/ {
 
-    /* "pysam/cfaidx.pyx":260
+    /* "pysam/cfaidx.pyx":261
  *             if self._delegate.comment.l:
  *                 return self._delegate.comment.s
  *             else: return None             # <<<<<<<<<<<<<<
@@ -4085,7 +4146,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7comment___get__(struct __
     goto __pyx_L0;
   }
 
-  /* "pysam/cfaidx.pyx":257
+  /* "pysam/cfaidx.pyx":258
  * 
  *     property comment:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4105,7 +4166,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7comment___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":263
+/* "pysam/cfaidx.pyx":264
  * 
  *     property quality:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4136,9 +4197,9 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7quality___get__(struct __
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 263);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 264);
 
-  /* "pysam/cfaidx.pyx":264
+  /* "pysam/cfaidx.pyx":265
  *     property quality:
  *         def __get__(self):
  *             if self._delegate.qual.l:             # <<<<<<<<<<<<<<
@@ -4148,7 +4209,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7quality___get__(struct __
   __pyx_t_1 = (__pyx_v_self->_delegate->qual.l != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/cfaidx.pyx":265
+    /* "pysam/cfaidx.pyx":266
  *         def __get__(self):
  *             if self._delegate.qual.l:
  *                 return self._delegate.qual.s             # <<<<<<<<<<<<<<
@@ -4156,7 +4217,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7quality___get__(struct __
  * 
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->_delegate->qual.s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 265; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->_delegate->qual.s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -4164,12 +4225,12 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7quality___get__(struct __
   }
   /*else*/ {
 
-    /* "pysam/cfaidx.pyx":266
+    /* "pysam/cfaidx.pyx":267
  *             if self._delegate.qual.l:
  *                 return self._delegate.qual.s
  *             else: return None             # <<<<<<<<<<<<<<
  * 
- * cdef class Fastqfile:
+ * cdef class FastqFile:
  */
     __Pyx_XDECREF(__pyx_r);
     __Pyx_INCREF(Py_None);
@@ -4177,7 +4238,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7quality___get__(struct __
     goto __pyx_L0;
   }
 
-  /* "pysam/cfaidx.pyx":263
+  /* "pysam/cfaidx.pyx":264
  * 
  *     property quality:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4197,7 +4258,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7quality___get__(struct __
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":274
+/* "pysam/cfaidx.pyx":278
  * 
  *     '''
  *     def __cinit__(self, *args, **kwargs):             # <<<<<<<<<<<<<<
@@ -4206,8 +4267,8 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_10FastqProxy_7quality___get__(struct __
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_6cfaidx_9Fastqfile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_6cfaidx_9Fastqfile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static int __pyx_pw_5pysam_6cfaidx_9FastqFile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_6cfaidx_9FastqFile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_args = 0;
   PyObject *__pyx_v_kwargs = 0;
   int __pyx_r;
@@ -4219,7 +4280,7 @@ static int __pyx_pw_5pysam_6cfaidx_9Fastqfile_1__cinit__(PyObject *__pyx_v_self,
   __Pyx_GOTREF(__pyx_v_kwargs);
   __Pyx_INCREF(__pyx_args);
   __pyx_v_args = __pyx_args;
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastqfile___cinit__(((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)__pyx_v_self), __pyx_v_args, __pyx_v_kwargs);
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastqFile___cinit__(((struct __pyx_obj_5pysam_6cfaidx_FastqFile *)__pyx_v_self), __pyx_v_args, __pyx_v_kwargs);
 
   /* function exit code */
   __Pyx_XDECREF(__pyx_v_args);
@@ -4228,7 +4289,7 @@ static int __pyx_pw_5pysam_6cfaidx_9Fastqfile_1__cinit__(PyObject *__pyx_v_self,
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_6cfaidx_9Fastqfile___cinit__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs) {
+static int __pyx_pf_5pysam_6cfaidx_9FastqFile___cinit__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4240,9 +4301,9 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastqfile___cinit__(struct __pyx_obj_5pysam_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__cinit__", 0);
-  __Pyx_TraceCall("__cinit__", __pyx_f[0], 274);
+  __Pyx_TraceCall("__cinit__", __pyx_f[0], 278);
 
-  /* "pysam/cfaidx.pyx":276
+  /* "pysam/cfaidx.pyx":280
  *     def __cinit__(self, *args, **kwargs):
  *         # self.fastqfile = <gzFile*>NULL
  *         self._filename = None             # <<<<<<<<<<<<<<
@@ -4255,7 +4316,7 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastqfile___cinit__(struct __pyx_obj_5pysam_
   __Pyx_DECREF(__pyx_v_self->_filename);
   __pyx_v_self->_filename = Py_None;
 
-  /* "pysam/cfaidx.pyx":277
+  /* "pysam/cfaidx.pyx":281
  *         # self.fastqfile = <gzFile*>NULL
  *         self._filename = None
  *         self.entry = NULL             # <<<<<<<<<<<<<<
@@ -4264,27 +4325,27 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastqfile___cinit__(struct __pyx_obj_5pysam_
  */
   __pyx_v_self->entry = NULL;
 
-  /* "pysam/cfaidx.pyx":278
+  /* "pysam/cfaidx.pyx":282
  *         self._filename = None
  *         self.entry = NULL
  *         self._open(*args, **kwargs)             # <<<<<<<<<<<<<<
  * 
  *     def _isOpen( self ):
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 278; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 278; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_t_3 = __pyx_v_kwargs;
   __Pyx_INCREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 278; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/cfaidx.pyx":274
+  /* "pysam/cfaidx.pyx":278
  * 
  *     '''
  *     def __cinit__(self, *args, **kwargs):             # <<<<<<<<<<<<<<
@@ -4300,7 +4361,7 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastqfile___cinit__(struct __pyx_obj_5pysam_
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastqfile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastqFile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
@@ -4308,7 +4369,7 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastqfile___cinit__(struct __pyx_obj_5pysam_
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":280
+/* "pysam/cfaidx.pyx":284
  *         self._open(*args, **kwargs)
  * 
  *     def _isOpen( self ):             # <<<<<<<<<<<<<<
@@ -4317,20 +4378,20 @@ static int __pyx_pf_5pysam_6cfaidx_9Fastqfile___cinit__(struct __pyx_obj_5pysam_
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastqfile_2_isOpen[] = "Fastqfile._isOpen(self)\nreturn true if samfile has been opened.";
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastqFile_2_isOpen[] = "FastqFile._isOpen(self)\nreturn true if samfile has been opened.";
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("_isOpen (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastqfile_2_isOpen(((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastqFile_2_isOpen(((struct __pyx_obj_5pysam_6cfaidx_FastqFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_2_isOpen(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_2_isOpen(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4339,9 +4400,9 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_2_isOpen(struct __pyx_obj_5p
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_isOpen", 0);
-  __Pyx_TraceCall("_isOpen", __pyx_f[0], 280);
+  __Pyx_TraceCall("_isOpen", __pyx_f[0], 284);
 
-  /* "pysam/cfaidx.pyx":282
+  /* "pysam/cfaidx.pyx":286
  *     def _isOpen( self ):
  *         '''return true if samfile has been opened.'''
  *         return self.entry != NULL             # <<<<<<<<<<<<<<
@@ -4349,13 +4410,13 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_2_isOpen(struct __pyx_obj_5p
  *     def _open(self, filename):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->entry != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->entry != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 286; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":280
+  /* "pysam/cfaidx.pyx":284
  *         self._open(*args, **kwargs)
  * 
  *     def _isOpen( self ):             # <<<<<<<<<<<<<<
@@ -4366,7 +4427,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_2_isOpen(struct __pyx_obj_5p
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastqfile._isOpen", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastqFile._isOpen", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -4375,7 +4436,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_2_isOpen(struct __pyx_obj_5p
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":284
+/* "pysam/cfaidx.pyx":288
  *         return self.entry != NULL
  * 
  *     def _open(self, filename):             # <<<<<<<<<<<<<<
@@ -4384,20 +4445,20 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_2_isOpen(struct __pyx_obj_5p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_5_open(PyObject *__pyx_v_self, PyObject *__pyx_v_filename); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastqfile_4_open[] = "Fastqfile._open(self, filename)\nopen an indexed fasta file.\n\n        This method expects an indexed fasta file.\n        ";
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_5_open(PyObject *__pyx_v_self, PyObject *__pyx_v_filename) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_5_open(PyObject *__pyx_v_self, PyObject *__pyx_v_filename); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastqFile_4_open[] = "FastqFile._open(self, filename)\nopen an indexed fasta file.\n\n        This method expects an indexed fasta file.\n        ";
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_5_open(PyObject *__pyx_v_self, PyObject *__pyx_v_filename) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("_open (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastqfile_4_open(((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)__pyx_v_self), ((PyObject *)__pyx_v_filename));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastqFile_4_open(((struct __pyx_obj_5pysam_6cfaidx_FastqFile *)__pyx_v_self), ((PyObject *)__pyx_v_filename));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_4_open(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self, PyObject *__pyx_v_filename) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_4_open(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self, PyObject *__pyx_v_filename) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4411,97 +4472,97 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_4_open(struct __pyx_obj_5pys
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("_open", 0);
-  __Pyx_TraceCall("_open", __pyx_f[0], 284);
+  __Pyx_TraceCall("_open", __pyx_f[0], 288);
   __Pyx_INCREF(__pyx_v_filename);
 
-  /* "pysam/cfaidx.pyx":289
+  /* "pysam/cfaidx.pyx":293
  *         This method expects an indexed fasta file.
  *         '''
  *         self.close()             # <<<<<<<<<<<<<<
  * 
  *         if not os.path.exists(filename):
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 289; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 293; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 289; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 293; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cfaidx.pyx":291
+  /* "pysam/cfaidx.pyx":295
  *         self.close()
  * 
  *         if not os.path.exists(filename):             # <<<<<<<<<<<<<<
  *             raise IOError("No such file or directory: %s" % filename)
  * 
  */
-  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_path); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_path); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_exists); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_exists); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_v_filename);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_filename);
   __Pyx_GIVEREF(__pyx_v_filename);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_t_5 = ((!__pyx_t_4) != 0);
   if (__pyx_t_5) {
 
-    /* "pysam/cfaidx.pyx":292
+    /* "pysam/cfaidx.pyx":296
  * 
  *         if not os.path.exists(filename):
  *             raise IOError("No such file or directory: %s" % filename)             # <<<<<<<<<<<<<<
  * 
  *         filename = _encodeFilename(filename)
  */
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_No_such_file_or_directory_s, __pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_No_such_file_or_directory_s, __pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_3);
     __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/cfaidx.pyx":294
+  /* "pysam/cfaidx.pyx":298
  *             raise IOError("No such file or directory: %s" % filename)
  * 
  *         filename = _encodeFilename(filename)             # <<<<<<<<<<<<<<
  *         self.fastqfile = gzopen(filename, "r")
  *         self.entry = kseq_init(self.fastqfile)
  */
-  __pyx_t_3 = __pyx_f_5pysam_6cfaidx__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 294; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __pyx_f_5pysam_6cfaidx__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 298; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF_SET(__pyx_v_filename, __pyx_t_3);
   __pyx_t_3 = 0;
 
-  /* "pysam/cfaidx.pyx":295
+  /* "pysam/cfaidx.pyx":299
  * 
  *         filename = _encodeFilename(filename)
  *         self.fastqfile = gzopen(filename, "r")             # <<<<<<<<<<<<<<
  *         self.entry = kseq_init(self.fastqfile)
  *         self._filename = filename
  */
-  __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 299; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_self->fastqfile = gzopen(__pyx_t_6, __pyx_k_r);
 
-  /* "pysam/cfaidx.pyx":296
+  /* "pysam/cfaidx.pyx":300
  *         filename = _encodeFilename(filename)
  *         self.fastqfile = gzopen(filename, "r")
  *         self.entry = kseq_init(self.fastqfile)             # <<<<<<<<<<<<<<
@@ -4510,7 +4571,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_4_open(struct __pyx_obj_5pys
  */
   __pyx_v_self->entry = kseq_init(__pyx_v_self->fastqfile);
 
-  /* "pysam/cfaidx.pyx":297
+  /* "pysam/cfaidx.pyx":301
  *         self.fastqfile = gzopen(filename, "r")
  *         self.entry = kseq_init(self.fastqfile)
  *         self._filename = filename             # <<<<<<<<<<<<<<
@@ -4523,7 +4584,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_4_open(struct __pyx_obj_5pys
   __Pyx_DECREF(__pyx_v_self->_filename);
   __pyx_v_self->_filename = __pyx_v_filename;
 
-  /* "pysam/cfaidx.pyx":284
+  /* "pysam/cfaidx.pyx":288
  *         return self.entry != NULL
  * 
  *     def _open(self, filename):             # <<<<<<<<<<<<<<
@@ -4538,7 +4599,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_4_open(struct __pyx_obj_5pys
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastqfile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastqFile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_filename);
@@ -4548,7 +4609,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_4_open(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":299
+/* "pysam/cfaidx.pyx":303
  *         self._filename = filename
  * 
  *     def close( self ):             # <<<<<<<<<<<<<<
@@ -4557,28 +4618,28 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_4_open(struct __pyx_obj_5pys
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_7close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastqfile_6close[] = "Fastqfile.close(self)\nclose file.";
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_7close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_7close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastqFile_6close[] = "FastqFile.close(self)\nclose file.";
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_7close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("close (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastqFile_6close(((struct __pyx_obj_5pysam_6cfaidx_FastqFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_6close(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("close", 0);
-  __Pyx_TraceCall("close", __pyx_f[0], 299);
+  __Pyx_TraceCall("close", __pyx_f[0], 303);
 
-  /* "pysam/cfaidx.pyx":301
+  /* "pysam/cfaidx.pyx":305
  *     def close( self ):
  *         '''close file.'''
  *         if self.entry != NULL:             # <<<<<<<<<<<<<<
@@ -4588,7 +4649,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(struct __pyx_obj_5pys
   __pyx_t_1 = ((__pyx_v_self->entry != NULL) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/cfaidx.pyx":302
+    /* "pysam/cfaidx.pyx":306
  *         '''close file.'''
  *         if self.entry != NULL:
  *             gzclose(self.fastqfile)             # <<<<<<<<<<<<<<
@@ -4597,7 +4658,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(struct __pyx_obj_5pys
  */
     gzclose(__pyx_v_self->fastqfile);
 
-    /* "pysam/cfaidx.pyx":303
+    /* "pysam/cfaidx.pyx":307
  *         if self.entry != NULL:
  *             gzclose(self.fastqfile)
  *             if self.entry:             # <<<<<<<<<<<<<<
@@ -4607,7 +4668,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(struct __pyx_obj_5pys
     __pyx_t_1 = (__pyx_v_self->entry != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/cfaidx.pyx":304
+      /* "pysam/cfaidx.pyx":308
  *             gzclose(self.fastqfile)
  *             if self.entry:
  *                 kseq_destroy(self.entry)             # <<<<<<<<<<<<<<
@@ -4616,7 +4677,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(struct __pyx_obj_5pys
  */
       kseq_destroy(__pyx_v_self->entry);
 
-      /* "pysam/cfaidx.pyx":305
+      /* "pysam/cfaidx.pyx":309
  *             if self.entry:
  *                 kseq_destroy(self.entry)
  *                 self.entry = NULL             # <<<<<<<<<<<<<<
@@ -4631,7 +4692,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(struct __pyx_obj_5pys
   }
   __pyx_L3:;
 
-  /* "pysam/cfaidx.pyx":299
+  /* "pysam/cfaidx.pyx":303
  *         self._filename = filename
  * 
  *     def close( self ):             # <<<<<<<<<<<<<<
@@ -4647,7 +4708,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":307
+/* "pysam/cfaidx.pyx":311
  *                 self.entry = NULL
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -4656,17 +4717,17 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_6close(struct __pyx_obj_5pys
  */
 
 /* Python wrapper */
-static void __pyx_pw_5pysam_6cfaidx_9Fastqfile_9__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_6cfaidx_9Fastqfile_9__dealloc__(PyObject *__pyx_v_self) {
+static void __pyx_pw_5pysam_6cfaidx_9FastqFile_9__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_6cfaidx_9FastqFile_9__dealloc__(PyObject *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_6cfaidx_9Fastqfile_8__dealloc__(((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)__pyx_v_self));
+  __pyx_pf_5pysam_6cfaidx_9FastqFile_8__dealloc__(((struct __pyx_obj_5pysam_6cfaidx_FastqFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
 }
 
-static void __pyx_pf_5pysam_6cfaidx_9Fastqfile_8__dealloc__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self) {
+static void __pyx_pf_5pysam_6cfaidx_9FastqFile_8__dealloc__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
@@ -4675,23 +4736,23 @@ static void __pyx_pf_5pysam_6cfaidx_9Fastqfile_8__dealloc__(struct __pyx_obj_5py
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 307);
+  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 311);
 
-  /* "pysam/cfaidx.pyx":308
+  /* "pysam/cfaidx.pyx":312
  * 
  *     def __dealloc__(self):
  *         self.close()             # <<<<<<<<<<<<<<
  * 
  *     property filename:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 308; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 308; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cfaidx.pyx":307
+  /* "pysam/cfaidx.pyx":311
  *                 self.entry = NULL
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -4704,13 +4765,13 @@ static void __pyx_pf_5pysam_6cfaidx_9Fastqfile_8__dealloc__(struct __pyx_obj_5py
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_WriteUnraisable("pysam.cfaidx.Fastqfile.__dealloc__", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
+  __Pyx_WriteUnraisable("pysam.cfaidx.FastqFile.__dealloc__", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
   __pyx_L0:;
   __Pyx_TraceReturn(Py_None);
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/cfaidx.pyx":312
+/* "pysam/cfaidx.pyx":316
  *     property filename:
  *         '''number of :term:`filename` associated with this object.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4719,26 +4780,26 @@ static void __pyx_pf_5pysam_6cfaidx_9Fastqfile_8__dealloc__(struct __pyx_obj_5py
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_8filename_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_8filename_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_8filename_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_8filename_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastqfile_8filename___get__(((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastqFile_8filename___get__(((struct __pyx_obj_5pysam_6cfaidx_FastqFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_8filename___get__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_8filename___get__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 312);
+  __Pyx_TraceCall("__get__", __pyx_f[0], 316);
 
-  /* "pysam/cfaidx.pyx":313
+  /* "pysam/cfaidx.pyx":317
  *         '''number of :term:`filename` associated with this object.'''
  *         def __get__(self):
  *             return self._filename             # <<<<<<<<<<<<<<
@@ -4750,7 +4811,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_8filename___get__(struct __p
   __pyx_r = __pyx_v_self->_filename;
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":312
+  /* "pysam/cfaidx.pyx":316
  *     property filename:
  *         '''number of :term:`filename` associated with this object.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4766,7 +4827,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_8filename___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":315
+/* "pysam/cfaidx.pyx":319
  *             return self._filename
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -4775,19 +4836,19 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_8filename___get__(struct __p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_11__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_11__iter__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_11__iter__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_11__iter__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastqfile_10__iter__(((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastqFile_10__iter__(((struct __pyx_obj_5pysam_6cfaidx_FastqFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_10__iter__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_10__iter__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4799,40 +4860,40 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_10__iter__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 315);
+  __Pyx_TraceCall("__iter__", __pyx_f[0], 319);
 
-  /* "pysam/cfaidx.pyx":316
+  /* "pysam/cfaidx.pyx":320
  * 
  *     def __iter__(self):
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError( "I/O operation on closed file" )
  *         return self
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/cfaidx.pyx":317
+    /* "pysam/cfaidx.pyx":321
  *     def __iter__(self):
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *         return self
  * 
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__10, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 317; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__10, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 317; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/cfaidx.pyx":318
+  /* "pysam/cfaidx.pyx":322
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )
  *         return self             # <<<<<<<<<<<<<<
@@ -4844,7 +4905,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_10__iter__(struct __pyx_obj_
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":315
+  /* "pysam/cfaidx.pyx":319
  *             return self._filename
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -4856,7 +4917,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_10__iter__(struct __pyx_obj_
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastqfile.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastqFile.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -4865,7 +4926,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_10__iter__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":320
+/* "pysam/cfaidx.pyx":324
  *         return self
  * 
  *     cdef kseq_t * getCurrent(self):             # <<<<<<<<<<<<<<
@@ -4873,14 +4934,14 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_10__iter__(struct __pyx_obj_
  * 
  */
 
-static kseq_t *__pyx_f_5pysam_6cfaidx_9Fastqfile_getCurrent(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self) {
+static kseq_t *__pyx_f_5pysam_6cfaidx_9FastqFile_getCurrent(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self) {
   kseq_t *__pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 320);
+  __Pyx_TraceCall("getCurrent", __pyx_f[0], 324);
 
-  /* "pysam/cfaidx.pyx":321
+  /* "pysam/cfaidx.pyx":325
  * 
  *     cdef kseq_t * getCurrent(self):
  *         return self.entry             # <<<<<<<<<<<<<<
@@ -4890,7 +4951,7 @@ static kseq_t *__pyx_f_5pysam_6cfaidx_9Fastqfile_getCurrent(struct __pyx_obj_5py
   __pyx_r = __pyx_v_self->entry;
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":320
+  /* "pysam/cfaidx.pyx":324
  *         return self
  * 
  *     cdef kseq_t * getCurrent(self):             # <<<<<<<<<<<<<<
@@ -4905,7 +4966,7 @@ static kseq_t *__pyx_f_5pysam_6cfaidx_9Fastqfile_getCurrent(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":323
+/* "pysam/cfaidx.pyx":327
  *         return self.entry
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -4913,14 +4974,14 @@ static kseq_t *__pyx_f_5pysam_6cfaidx_9Fastqfile_getCurrent(struct __pyx_obj_5py
  *         '''
  */
 
-static int __pyx_f_5pysam_6cfaidx_9Fastqfile_cnext(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self) {
+static int __pyx_f_5pysam_6cfaidx_9FastqFile_cnext(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 323);
+  __Pyx_TraceCall("cnext", __pyx_f[0], 327);
 
-  /* "pysam/cfaidx.pyx":326
+  /* "pysam/cfaidx.pyx":330
  *         '''C version of iterator
  *         '''
  *         return kseq_read(self.entry)             # <<<<<<<<<<<<<<
@@ -4930,7 +4991,7 @@ static int __pyx_f_5pysam_6cfaidx_9Fastqfile_cnext(struct __pyx_obj_5pysam_6cfai
   __pyx_r = kseq_read(__pyx_v_self->entry);
   goto __pyx_L0;
 
-  /* "pysam/cfaidx.pyx":323
+  /* "pysam/cfaidx.pyx":327
  *         return self.entry
  * 
  *     cdef int cnext(self):             # <<<<<<<<<<<<<<
@@ -4945,7 +5006,7 @@ static int __pyx_f_5pysam_6cfaidx_9Fastqfile_cnext(struct __pyx_obj_5pysam_6cfai
   return __pyx_r;
 }
 
-/* "pysam/cfaidx.pyx":328
+/* "pysam/cfaidx.pyx":332
  *         return kseq_read(self.entry)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -4954,23 +5015,23 @@ static int __pyx_f_5pysam_6cfaidx_9Fastqfile_cnext(struct __pyx_obj_5pysam_6cfai
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_13__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_6cfaidx_9Fastqfile_12__next__[] = "\n        python version of next().\n        ";
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_13__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_6cfaidx_9FastqFile_12__next__[] = "\n        python version of next().\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_6cfaidx_9Fastqfile_12__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_6cfaidx_9FastqFile_12__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_6cfaidx_9Fastqfile_13__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6cfaidx_9FastqFile_13__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6cfaidx_9FastqFile_12__next__(((struct __pyx_obj_5pysam_6cfaidx_FastqFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6cfaidx_9FastqFile_12__next__(struct __pyx_obj_5pysam_6cfaidx_FastqFile *__pyx_v_self) {
   int __pyx_v_l;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -4981,9 +5042,9 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_TraceDeclarations
   __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 328);
+  __Pyx_TraceCall("__next__", __pyx_f[0], 332);
 
-  /* "pysam/cfaidx.pyx":333
+  /* "pysam/cfaidx.pyx":337
  *         """
  *         cdef int l
  *         l = kseq_read( self.entry)             # <<<<<<<<<<<<<<
@@ -4992,7 +5053,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(struct __pyx_obj_
  */
   __pyx_v_l = kseq_read(__pyx_v_self->entry);
 
-  /* "pysam/cfaidx.pyx":334
+  /* "pysam/cfaidx.pyx":338
  *         cdef int l
  *         l = kseq_read( self.entry)
  *         if (l > 0):             # <<<<<<<<<<<<<<
@@ -5002,7 +5063,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(struct __pyx_obj_
   __pyx_t_1 = ((__pyx_v_l > 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/cfaidx.pyx":335
+    /* "pysam/cfaidx.pyx":339
  *         l = kseq_read( self.entry)
  *         if (l > 0):
  *             return makeFastqProxy( self.entry )             # <<<<<<<<<<<<<<
@@ -5010,7 +5071,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(struct __pyx_obj_
  *             raise StopIteration
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __pyx_f_5pysam_6cfaidx_makeFastqProxy(__pyx_v_self->entry); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 335; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __pyx_f_5pysam_6cfaidx_makeFastqProxy(__pyx_v_self->entry); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -5018,18 +5079,18 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(struct __pyx_obj_
   }
   /*else*/ {
 
-    /* "pysam/cfaidx.pyx":337
+    /* "pysam/cfaidx.pyx":341
  *             return makeFastqProxy( self.entry )
  *         else:
  *             raise StopIteration             # <<<<<<<<<<<<<<
  * 
- * __all__ = ["Fastafile",
+ * # Compatibility Layer for pysam < 0.8
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 337; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/cfaidx.pyx":328
+  /* "pysam/cfaidx.pyx":332
  *         return kseq_read(self.entry)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -5040,7 +5101,7 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(struct __pyx_obj_
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.cfaidx.Fastqfile.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.cfaidx.FastqFile.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -5048,10 +5109,10 @@ static PyObject *__pyx_pf_5pysam_6cfaidx_9Fastqfile_12__next__(struct __pyx_obj_
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
-static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile __pyx_vtable_5pysam_6cfaidx_Fastafile;
+static struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile __pyx_vtable_5pysam_6cfaidx_FastaFile;
 
-static PyObject *__pyx_tp_new_5pysam_6cfaidx_Fastafile(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_6cfaidx_Fastafile *p;
+static PyObject *__pyx_tp_new_5pysam_6cfaidx_FastaFile(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_6cfaidx_FastaFile *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -5059,20 +5120,20 @@ static PyObject *__pyx_tp_new_5pysam_6cfaidx_Fastafile(PyTypeObject *t, PyObject
     o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
   }
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_6cfaidx_Fastafile;
+  p = ((struct __pyx_obj_5pysam_6cfaidx_FastaFile *)o);
+  p->__pyx_vtab = __pyx_vtabptr_5pysam_6cfaidx_FastaFile;
   p->_filename = Py_None; Py_INCREF(Py_None);
   p->_references = Py_None; Py_INCREF(Py_None);
   p->_lengths = Py_None; Py_INCREF(Py_None);
   p->reference2length = Py_None; Py_INCREF(Py_None);
-  if (unlikely(__pyx_pw_5pysam_6cfaidx_9Fastafile_1__cinit__(o, a, k) < 0)) {
+  if (unlikely(__pyx_pw_5pysam_6cfaidx_9FastaFile_1__cinit__(o, a, k) < 0)) {
     Py_DECREF(o); o = 0;
   }
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_6cfaidx_Fastafile(PyObject *o) {
-  struct __pyx_obj_5pysam_6cfaidx_Fastafile *p = (struct __pyx_obj_5pysam_6cfaidx_Fastafile *)o;
+static void __pyx_tp_dealloc_5pysam_6cfaidx_FastaFile(PyObject *o) {
+  struct __pyx_obj_5pysam_6cfaidx_FastaFile *p = (struct __pyx_obj_5pysam_6cfaidx_FastaFile *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -5083,7 +5144,7 @@ static void __pyx_tp_dealloc_5pysam_6cfaidx_Fastafile(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_6cfaidx_9Fastafile_11__dealloc__(o);
+    __pyx_pw_5pysam_6cfaidx_9FastaFile_11__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
@@ -5094,9 +5155,9 @@ static void __pyx_tp_dealloc_5pysam_6cfaidx_Fastafile(PyObject *o) {
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static int __pyx_tp_traverse_5pysam_6cfaidx_Fastafile(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_6cfaidx_FastaFile(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_6cfaidx_Fastafile *p = (struct __pyx_obj_5pysam_6cfaidx_Fastafile *)o;
+  struct __pyx_obj_5pysam_6cfaidx_FastaFile *p = (struct __pyx_obj_5pysam_6cfaidx_FastaFile *)o;
   if (p->_filename) {
     e = (*v)(p->_filename, a); if (e) return e;
   }
@@ -5112,9 +5173,9 @@ static int __pyx_tp_traverse_5pysam_6cfaidx_Fastafile(PyObject *o, visitproc v,
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_6cfaidx_Fastafile(PyObject *o) {
+static int __pyx_tp_clear_5pysam_6cfaidx_FastaFile(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_6cfaidx_Fastafile *p = (struct __pyx_obj_5pysam_6cfaidx_Fastafile *)o;
+  struct __pyx_obj_5pysam_6cfaidx_FastaFile *p = (struct __pyx_obj_5pysam_6cfaidx_FastaFile *)o;
   tmp = ((PyObject*)p->_filename);
   p->_filename = Py_None; Py_INCREF(Py_None);
   Py_XDECREF(tmp);
@@ -5129,7 +5190,7 @@ static int __pyx_tp_clear_5pysam_6cfaidx_Fastafile(PyObject *o) {
   Py_XDECREF(tmp);
   return 0;
 }
-static PyObject *__pyx_sq_item_5pysam_6cfaidx_Fastafile(PyObject *o, Py_ssize_t i) {
+static PyObject *__pyx_sq_item_5pysam_6cfaidx_FastaFile(PyObject *o, Py_ssize_t i) {
   PyObject *r;
   PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0;
   r = Py_TYPE(o)->tp_as_mapping->mp_subscript(o, x);
@@ -5137,64 +5198,64 @@ static PyObject *__pyx_sq_item_5pysam_6cfaidx_Fastafile(PyObject *o, Py_ssize_t
   return r;
 }
 
-static PyObject *__pyx_getprop_5pysam_6cfaidx_9Fastafile_filename(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_6cfaidx_9Fastafile_8filename_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_6cfaidx_9FastaFile_filename(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_6cfaidx_9FastaFile_8filename_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_6cfaidx_9Fastafile_references(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_6cfaidx_9Fastafile_10references_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_6cfaidx_9FastaFile_references(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_6cfaidx_9FastaFile_10references_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_6cfaidx_9Fastafile_nreferences(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_6cfaidx_9Fastafile_11nreferences_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_6cfaidx_9FastaFile_nreferences(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_6cfaidx_9FastaFile_11nreferences_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_6cfaidx_9Fastafile_lengths(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_6cfaidx_9Fastafile_7lengths_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_6cfaidx_9FastaFile_lengths(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_6cfaidx_9FastaFile_7lengths_1__get__(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_6cfaidx_Fastafile[] = {
-  {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9Fastafile_3_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9Fastafile_2_isOpen)},
-  {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9Fastafile_7_open, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9Fastafile_6_open)},
-  {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9Fastafile_9close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9Fastafile_8close)},
-  {__Pyx_NAMESTR("fetch"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9Fastafile_13fetch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9Fastafile_12fetch)},
-  {__Pyx_NAMESTR("getReferenceLength"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9Fastafile_15getReferenceLength, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9Fastafile_14getReferenceLength)},
+static PyMethodDef __pyx_methods_5pysam_6cfaidx_FastaFile[] = {
+  {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9FastaFile_3_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9FastaFile_2_isOpen)},
+  {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9FastaFile_7_open, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9FastaFile_6_open)},
+  {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9FastaFile_9close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9FastaFile_8close)},
+  {__Pyx_NAMESTR("fetch"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9FastaFile_13fetch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9FastaFile_12fetch)},
+  {__Pyx_NAMESTR("get_reference_length"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9FastaFile_15get_reference_length, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9FastaFile_14get_reference_length)},
   {0, 0, 0, 0}
 };
 
-static struct PyGetSetDef __pyx_getsets_5pysam_6cfaidx_Fastafile[] = {
-  {(char *)"filename", __pyx_getprop_5pysam_6cfaidx_9Fastafile_filename, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_filename_associat), 0},
-  {(char *)"references", __pyx_getprop_5pysam_6cfaidx_9Fastafile_references, 0, __Pyx_DOCSTR(__pyx_k_tuple_with_the_names_of_term_ref), 0},
-  {(char *)"nreferences", __pyx_getprop_5pysam_6cfaidx_9Fastafile_nreferences, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_reference_sequenc), 0},
-  {(char *)"lengths", __pyx_getprop_5pysam_6cfaidx_9Fastafile_lengths, 0, __Pyx_DOCSTR(__pyx_k_tuple_with_the_lengths_of_term_r), 0},
+static struct PyGetSetDef __pyx_getsets_5pysam_6cfaidx_FastaFile[] = {
+  {(char *)"filename", __pyx_getprop_5pysam_6cfaidx_9FastaFile_filename, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_filename_associat), 0},
+  {(char *)"references", __pyx_getprop_5pysam_6cfaidx_9FastaFile_references, 0, __Pyx_DOCSTR(__pyx_k_tuple_with_the_names_of_term_ref), 0},
+  {(char *)"nreferences", __pyx_getprop_5pysam_6cfaidx_9FastaFile_nreferences, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_reference_sequenc), 0},
+  {(char *)"lengths", __pyx_getprop_5pysam_6cfaidx_9FastaFile_lengths, 0, __Pyx_DOCSTR(__pyx_k_tuple_with_the_lengths_of_term_r), 0},
   {0, 0, 0, 0, 0}
 };
 
-static PySequenceMethods __pyx_tp_as_sequence_Fastafile = {
-  __pyx_pw_5pysam_6cfaidx_9Fastafile_5__len__, /*sq_length*/
+static PySequenceMethods __pyx_tp_as_sequence_FastaFile = {
+  __pyx_pw_5pysam_6cfaidx_9FastaFile_5__len__, /*sq_length*/
   0, /*sq_concat*/
   0, /*sq_repeat*/
-  __pyx_sq_item_5pysam_6cfaidx_Fastafile, /*sq_item*/
+  __pyx_sq_item_5pysam_6cfaidx_FastaFile, /*sq_item*/
   0, /*sq_slice*/
   0, /*sq_ass_item*/
   0, /*sq_ass_slice*/
-  __pyx_pw_5pysam_6cfaidx_9Fastafile_19__contains__, /*sq_contains*/
+  __pyx_pw_5pysam_6cfaidx_9FastaFile_19__contains__, /*sq_contains*/
   0, /*sq_inplace_concat*/
   0, /*sq_inplace_repeat*/
 };
 
-static PyMappingMethods __pyx_tp_as_mapping_Fastafile = {
-  __pyx_pw_5pysam_6cfaidx_9Fastafile_5__len__, /*mp_length*/
-  __pyx_pw_5pysam_6cfaidx_9Fastafile_17__getitem__, /*mp_subscript*/
+static PyMappingMethods __pyx_tp_as_mapping_FastaFile = {
+  __pyx_pw_5pysam_6cfaidx_9FastaFile_5__len__, /*mp_length*/
+  __pyx_pw_5pysam_6cfaidx_9FastaFile_17__getitem__, /*mp_subscript*/
   0, /*mp_ass_subscript*/
 };
 
-static PyTypeObject __pyx_type_5pysam_6cfaidx_Fastafile = {
+static PyTypeObject __pyx_type_5pysam_6cfaidx_FastaFile = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.cfaidx.Fastafile"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastafile), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.cfaidx.FastaFile"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_6cfaidx_FastaFile), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_6cfaidx_Fastafile, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_6cfaidx_FastaFile, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -5205,8 +5266,8 @@ static PyTypeObject __pyx_type_5pysam_6cfaidx_Fastafile = {
   #endif
   0, /*tp_repr*/
   0, /*tp_as_number*/
-  &__pyx_tp_as_sequence_Fastafile, /*tp_as_sequence*/
-  &__pyx_tp_as_mapping_Fastafile, /*tp_as_mapping*/
+  &__pyx_tp_as_sequence_FastaFile, /*tp_as_sequence*/
+  &__pyx_tp_as_mapping_FastaFile, /*tp_as_mapping*/
   0, /*tp_hash*/
   0, /*tp_call*/
   0, /*tp_str*/
@@ -5214,16 +5275,16 @@ static PyTypeObject __pyx_type_5pysam_6cfaidx_Fastafile = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("*(filename)*\n\n    A *FASTA* file. The file is automatically opened.\n\n    The file expects an indexed fasta file.\n\n    TODO:\n        add automatic indexing.\n        add function to get sequence names.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_6cfaidx_Fastafile, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_6cfaidx_Fastafile, /*tp_clear*/
+  __Pyx_DOCSTR("*(filename)*\n\n    A *FASTA* file. The file is automatically opened.\n\n    This class expects an indexed fasta file and permits\n    random access to fasta sequences.\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_6cfaidx_FastaFile, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6cfaidx_FastaFile, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
   0, /*tp_iternext*/
-  __pyx_methods_5pysam_6cfaidx_Fastafile, /*tp_methods*/
+  __pyx_methods_5pysam_6cfaidx_FastaFile, /*tp_methods*/
   0, /*tp_members*/
-  __pyx_getsets_5pysam_6cfaidx_Fastafile, /*tp_getset*/
+  __pyx_getsets_5pysam_6cfaidx_FastaFile, /*tp_getset*/
   0, /*tp_base*/
   0, /*tp_dict*/
   0, /*tp_descr_get*/
@@ -5231,7 +5292,7 @@ static PyTypeObject __pyx_type_5pysam_6cfaidx_Fastafile = {
   0, /*tp_dictoffset*/
   0, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_6cfaidx_Fastafile, /*tp_new*/
+  __pyx_tp_new_5pysam_6cfaidx_FastaFile, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -5354,10 +5415,10 @@ static PyTypeObject __pyx_type_5pysam_6cfaidx_FastqProxy = {
   0, /*tp_finalize*/
   #endif
 };
-static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile __pyx_vtable_5pysam_6cfaidx_Fastqfile;
+static struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile __pyx_vtable_5pysam_6cfaidx_FastqFile;
 
-static PyObject *__pyx_tp_new_5pysam_6cfaidx_Fastqfile(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_6cfaidx_Fastqfile *p;
+static PyObject *__pyx_tp_new_5pysam_6cfaidx_FastqFile(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_6cfaidx_FastqFile *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -5365,17 +5426,17 @@ static PyObject *__pyx_tp_new_5pysam_6cfaidx_Fastqfile(PyTypeObject *t, PyObject
     o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
   }
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_6cfaidx_Fastqfile;
+  p = ((struct __pyx_obj_5pysam_6cfaidx_FastqFile *)o);
+  p->__pyx_vtab = __pyx_vtabptr_5pysam_6cfaidx_FastqFile;
   p->_filename = Py_None; Py_INCREF(Py_None);
-  if (unlikely(__pyx_pw_5pysam_6cfaidx_9Fastqfile_1__cinit__(o, a, k) < 0)) {
+  if (unlikely(__pyx_pw_5pysam_6cfaidx_9FastqFile_1__cinit__(o, a, k) < 0)) {
     Py_DECREF(o); o = 0;
   }
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_6cfaidx_Fastqfile(PyObject *o) {
-  struct __pyx_obj_5pysam_6cfaidx_Fastqfile *p = (struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)o;
+static void __pyx_tp_dealloc_5pysam_6cfaidx_FastqFile(PyObject *o) {
+  struct __pyx_obj_5pysam_6cfaidx_FastqFile *p = (struct __pyx_obj_5pysam_6cfaidx_FastqFile *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -5386,7 +5447,7 @@ static void __pyx_tp_dealloc_5pysam_6cfaidx_Fastqfile(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_6cfaidx_9Fastqfile_9__dealloc__(o);
+    __pyx_pw_5pysam_6cfaidx_9FastqFile_9__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
@@ -5394,47 +5455,185 @@ static void __pyx_tp_dealloc_5pysam_6cfaidx_Fastqfile(PyObject *o) {
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static int __pyx_tp_traverse_5pysam_6cfaidx_Fastqfile(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_6cfaidx_FastqFile(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_6cfaidx_Fastqfile *p = (struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)o;
+  struct __pyx_obj_5pysam_6cfaidx_FastqFile *p = (struct __pyx_obj_5pysam_6cfaidx_FastqFile *)o;
   if (p->_filename) {
     e = (*v)(p->_filename, a); if (e) return e;
   }
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_6cfaidx_Fastqfile(PyObject *o) {
+static int __pyx_tp_clear_5pysam_6cfaidx_FastqFile(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_6cfaidx_Fastqfile *p = (struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)o;
+  struct __pyx_obj_5pysam_6cfaidx_FastqFile *p = (struct __pyx_obj_5pysam_6cfaidx_FastqFile *)o;
   tmp = ((PyObject*)p->_filename);
   p->_filename = Py_None; Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   return 0;
 }
 
-static PyObject *__pyx_getprop_5pysam_6cfaidx_9Fastqfile_filename(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_6cfaidx_9Fastqfile_8filename_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_6cfaidx_9FastqFile_filename(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_6cfaidx_9FastqFile_8filename_1__get__(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_6cfaidx_Fastqfile[] = {
-  {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9Fastqfile_3_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9Fastqfile_2_isOpen)},
-  {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9Fastqfile_5_open, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9Fastqfile_4_open)},
-  {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9Fastqfile_7close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9Fastqfile_6close)},
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9Fastqfile_13__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9Fastqfile_12__next__)},
+static PyMethodDef __pyx_methods_5pysam_6cfaidx_FastqFile[] = {
+  {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9FastqFile_3_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9FastqFile_2_isOpen)},
+  {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9FastqFile_5_open, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9FastqFile_4_open)},
+  {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9FastqFile_7close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9FastqFile_6close)},
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_6cfaidx_9FastqFile_13__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_6cfaidx_9FastqFile_12__next__)},
   {0, 0, 0, 0}
 };
 
-static struct PyGetSetDef __pyx_getsets_5pysam_6cfaidx_Fastqfile[] = {
-  {(char *)"filename", __pyx_getprop_5pysam_6cfaidx_9Fastqfile_filename, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_filename_associat), 0},
+static struct PyGetSetDef __pyx_getsets_5pysam_6cfaidx_FastqFile[] = {
+  {(char *)"filename", __pyx_getprop_5pysam_6cfaidx_9FastqFile_filename, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_filename_associat), 0},
   {0, 0, 0, 0, 0}
 };
 
+static PyTypeObject __pyx_type_5pysam_6cfaidx_FastqFile = {
+  PyVarObject_HEAD_INIT(0, 0)
+  __Pyx_NAMESTR("pysam.cfaidx.FastqFile"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_6cfaidx_FastqFile), /*tp_basicsize*/
+  0, /*tp_itemsize*/
+  __pyx_tp_dealloc_5pysam_6cfaidx_FastqFile, /*tp_dealloc*/
+  0, /*tp_print*/
+  0, /*tp_getattr*/
+  0, /*tp_setattr*/
+  #if PY_MAJOR_VERSION < 3
+  0, /*tp_compare*/
+  #else
+  0, /*reserved*/
+  #endif
+  0, /*tp_repr*/
+  0, /*tp_as_number*/
+  0, /*tp_as_sequence*/
+  0, /*tp_as_mapping*/
+  0, /*tp_hash*/
+  0, /*tp_call*/
+  0, /*tp_str*/
+  0, /*tp_getattro*/
+  0, /*tp_setattro*/
+  0, /*tp_as_buffer*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  __Pyx_DOCSTR("*(filename)*\n\n    A *FASTQ* file. The file is automatically opened.\n\n    This file object permits iterating over all entries in\n    a fastq file. Random access is not implemented.\n\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_6cfaidx_FastqFile, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6cfaidx_FastqFile, /*tp_clear*/
+  0, /*tp_richcompare*/
+  0, /*tp_weaklistoffset*/
+  __pyx_pw_5pysam_6cfaidx_9FastqFile_11__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_6cfaidx_9FastqFile_13__next__, /*tp_iternext*/
+  __pyx_methods_5pysam_6cfaidx_FastqFile, /*tp_methods*/
+  0, /*tp_members*/
+  __pyx_getsets_5pysam_6cfaidx_FastqFile, /*tp_getset*/
+  0, /*tp_base*/
+  0, /*tp_dict*/
+  0, /*tp_descr_get*/
+  0, /*tp_descr_set*/
+  0, /*tp_dictoffset*/
+  0, /*tp_init*/
+  0, /*tp_alloc*/
+  __pyx_tp_new_5pysam_6cfaidx_FastqFile, /*tp_new*/
+  0, /*tp_free*/
+  0, /*tp_is_gc*/
+  0, /*tp_bases*/
+  0, /*tp_mro*/
+  0, /*tp_cache*/
+  0, /*tp_subclasses*/
+  0, /*tp_weaklist*/
+  0, /*tp_del*/
+  #if PY_VERSION_HEX >= 0x02060000
+  0, /*tp_version_tag*/
+  #endif
+  #if PY_VERSION_HEX >= 0x030400a1
+  0, /*tp_finalize*/
+  #endif
+};
+static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile __pyx_vtable_5pysam_6cfaidx_Fastafile;
+
+static PyObject *__pyx_tp_new_5pysam_6cfaidx_Fastafile(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_6cfaidx_Fastafile *p;
+  PyObject *o = __pyx_tp_new_5pysam_6cfaidx_FastaFile(t, a, k);
+  if (unlikely(!o)) return 0;
+  p = ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)o);
+  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile*)__pyx_vtabptr_5pysam_6cfaidx_Fastafile;
+  return o;
+}
+
+static PyTypeObject __pyx_type_5pysam_6cfaidx_Fastafile = {
+  PyVarObject_HEAD_INIT(0, 0)
+  __Pyx_NAMESTR("pysam.cfaidx.Fastafile"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastafile), /*tp_basicsize*/
+  0, /*tp_itemsize*/
+  __pyx_tp_dealloc_5pysam_6cfaidx_FastaFile, /*tp_dealloc*/
+  0, /*tp_print*/
+  0, /*tp_getattr*/
+  0, /*tp_setattr*/
+  #if PY_MAJOR_VERSION < 3
+  0, /*tp_compare*/
+  #else
+  0, /*reserved*/
+  #endif
+  0, /*tp_repr*/
+  0, /*tp_as_number*/
+  0, /*tp_as_sequence*/
+  0, /*tp_as_mapping*/
+  0, /*tp_hash*/
+  0, /*tp_call*/
+  0, /*tp_str*/
+  0, /*tp_getattro*/
+  0, /*tp_setattro*/
+  0, /*tp_as_buffer*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  0, /*tp_doc*/
+  __pyx_tp_traverse_5pysam_6cfaidx_FastaFile, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6cfaidx_FastaFile, /*tp_clear*/
+  0, /*tp_richcompare*/
+  0, /*tp_weaklistoffset*/
+  0, /*tp_iter*/
+  0, /*tp_iternext*/
+  0, /*tp_methods*/
+  0, /*tp_members*/
+  0, /*tp_getset*/
+  0, /*tp_base*/
+  0, /*tp_dict*/
+  0, /*tp_descr_get*/
+  0, /*tp_descr_set*/
+  0, /*tp_dictoffset*/
+  0, /*tp_init*/
+  0, /*tp_alloc*/
+  __pyx_tp_new_5pysam_6cfaidx_Fastafile, /*tp_new*/
+  0, /*tp_free*/
+  0, /*tp_is_gc*/
+  0, /*tp_bases*/
+  0, /*tp_mro*/
+  0, /*tp_cache*/
+  0, /*tp_subclasses*/
+  0, /*tp_weaklist*/
+  0, /*tp_del*/
+  #if PY_VERSION_HEX >= 0x02060000
+  0, /*tp_version_tag*/
+  #endif
+  #if PY_VERSION_HEX >= 0x030400a1
+  0, /*tp_finalize*/
+  #endif
+};
+static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile __pyx_vtable_5pysam_6cfaidx_Fastqfile;
+
+static PyObject *__pyx_tp_new_5pysam_6cfaidx_Fastqfile(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_6cfaidx_Fastqfile *p;
+  PyObject *o = __pyx_tp_new_5pysam_6cfaidx_FastqFile(t, a, k);
+  if (unlikely(!o)) return 0;
+  p = ((struct __pyx_obj_5pysam_6cfaidx_Fastqfile *)o);
+  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile*)__pyx_vtabptr_5pysam_6cfaidx_Fastqfile;
+  return o;
+}
+
 static PyTypeObject __pyx_type_5pysam_6cfaidx_Fastqfile = {
   PyVarObject_HEAD_INIT(0, 0)
   __Pyx_NAMESTR("pysam.cfaidx.Fastqfile"), /*tp_name*/
   sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastqfile), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_6cfaidx_Fastqfile, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_6cfaidx_FastqFile, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -5454,16 +5653,24 @@ static PyTypeObject __pyx_type_5pysam_6cfaidx_Fastqfile = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("*(filename)*\n\n    A *FASTQ* file. The file is automatically opened.\n\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_6cfaidx_Fastqfile, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_6cfaidx_Fastqfile, /*tp_clear*/
+  0, /*tp_doc*/
+  __pyx_tp_traverse_5pysam_6cfaidx_FastqFile, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6cfaidx_FastqFile, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_6cfaidx_9Fastqfile_11__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_6cfaidx_9Fastqfile_13__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_6cfaidx_Fastqfile, /*tp_methods*/
+  #if CYTHON_COMPILING_IN_PYPY
+  __pyx_pw_5pysam_6cfaidx_9FastqFile_11__iter__, /*tp_iter*/
+  #else
+  0, /*tp_iter*/
+  #endif
+  #if CYTHON_COMPILING_IN_PYPY
+  __pyx_pw_5pysam_6cfaidx_9FastqFile_13__next__, /*tp_iternext*/
+  #else
+  0, /*tp_iternext*/
+  #endif
+  0, /*tp_methods*/
   0, /*tp_members*/
-  __pyx_getsets_5pysam_6cfaidx_Fastqfile, /*tp_getset*/
+  0, /*tp_getset*/
   0, /*tp_base*/
   0, /*tp_dict*/
   0, /*tp_descr_get*/
@@ -5855,7 +6062,9 @@ static struct PyModuleDef __pyx_moduledef = {
 
 static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_kp_u_Argument_must_be_string_or_unico, __pyx_k_Argument_must_be_string_or_unico, sizeof(__pyx_k_Argument_must_be_string_or_unico), 0, 1, 0, 0},
+  {&__pyx_n_s_FastaFile, __pyx_k_FastaFile, sizeof(__pyx_k_FastaFile), 0, 0, 1, 1},
   {&__pyx_n_s_Fastafile, __pyx_k_Fastafile, sizeof(__pyx_k_Fastafile), 0, 0, 1, 1},
+  {&__pyx_n_s_FastqFile, __pyx_k_FastqFile, sizeof(__pyx_k_FastqFile), 0, 0, 1, 1},
   {&__pyx_n_s_Fastqfile, __pyx_k_Fastqfile, sizeof(__pyx_k_Fastqfile), 0, 0, 1, 1},
   {&__pyx_n_s_IOError, __pyx_k_IOError, sizeof(__pyx_k_IOError), 0, 0, 1, 1},
   {&__pyx_n_s_IS_PYTHON3, __pyx_k_IS_PYTHON3, sizeof(__pyx_k_IS_PYTHON3), 0, 0, 1, 1},
@@ -5914,8 +6123,8 @@ static int __Pyx_InitCachedBuiltins(void) {
   __pyx_builtin_IOError = __Pyx_GetBuiltinName(__pyx_n_s_IOError); if (!__pyx_builtin_IOError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_builtin_open = __Pyx_GetBuiltinName(__pyx_n_s_open); if (!__pyx_builtin_open) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_builtin_zip = __Pyx_GetBuiltinName(__pyx_n_s_zip); if (!__pyx_builtin_zip) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_IndexError = __Pyx_GetBuiltinName(__pyx_n_s_IndexError); if (!__pyx_builtin_IndexError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 337; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_IndexError = __Pyx_GetBuiltinName(__pyx_n_s_IndexError); if (!__pyx_builtin_IndexError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   return 0;
   __pyx_L1_error:;
   return -1;
@@ -5928,9 +6137,9 @@ static int __Pyx_InitCachedConstants(void) {
   /* "pysam/cfaidx.pyx":89
  *     def __len__(self):
  *         if self.fastafile == NULL:
- *             raise ValueError( "calling len() on closed file" )             # <<<<<<<<<<<<<<
+ *             raise ValueError("calling len() on closed file")             # <<<<<<<<<<<<<<
  * 
- *         return faidx_fetch_nseq(self.fastafile)
+ *         return faidx_nseq(self.fastafile)
  */
   __pyx_tuple_ = PyTuple_Pack(1, __pyx_kp_s_calling_len_on_closed_file); if (unlikely(!__pyx_tuple_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 89; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple_);
@@ -5969,47 +6178,47 @@ static int __Pyx_InitCachedConstants(void) {
   __Pyx_GOTREF(__pyx_tuple__5);
   __Pyx_GIVEREF(__pyx_tuple__5);
 
-  /* "pysam/cfaidx.pyx":167
+  /* "pysam/cfaidx.pyx":168
  * 
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  * 
  *         cdef int length
  */
-  __pyx_tuple__6 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__6 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__6);
   __Pyx_GIVEREF(__pyx_tuple__6);
 
-  /* "pysam/cfaidx.pyx":174
+  /* "pysam/cfaidx.pyx":175
  *         if not region:
  *             if reference is None:
  *                 raise ValueError('no sequence/region supplied.')             # <<<<<<<<<<<<<<
  *             if start is None:
  *                 start = 0
  */
-  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_s_no_sequence_region_supplied); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_s_no_sequence_region_supplied); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__7);
   __Pyx_GIVEREF(__pyx_tuple__7);
 
-  /* "pysam/cfaidx.pyx":200
+  /* "pysam/cfaidx.pyx":201
  *             region = "%s:%i-%i" % (reference, start+1, end)
  *             if PY_MAJOR_VERSION >= 3:
  *                 region = region.encode('ascii')             # <<<<<<<<<<<<<<
  *             seq = fai_fetch( self.fastafile,
  *                              region,
  */
-  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 200; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__9);
   __Pyx_GIVEREF(__pyx_tuple__9);
 
-  /* "pysam/cfaidx.pyx":317
+  /* "pysam/cfaidx.pyx":321
  *     def __iter__(self):
  *         if not self._isOpen():
  *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
  *         return self
  * 
  */
-  __pyx_tuple__10 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 317; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__10 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__10);
   __Pyx_GIVEREF(__pyx_tuple__10);
   __Pyx_RefNannyFinishContext();
@@ -6112,44 +6321,60 @@ PyMODINIT_FUNC PyInit_cfaidx(void)
   /*--- Variable export code ---*/
   /*--- Function export code ---*/
   /*--- Type init code ---*/
-  __pyx_vtabptr_5pysam_6cfaidx_Fastafile = &__pyx_vtable_5pysam_6cfaidx_Fastafile;
-  __pyx_vtable_5pysam_6cfaidx_Fastafile._fetch = (char *(*)(struct __pyx_obj_5pysam_6cfaidx_Fastafile *, char *, int, int, int *))__pyx_f_5pysam_6cfaidx_9Fastafile__fetch;
-  if (PyType_Ready(&__pyx_type_5pysam_6cfaidx_Fastafile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_6cfaidx_Fastafile.tp_print = 0;
+  __pyx_vtabptr_5pysam_6cfaidx_FastaFile = &__pyx_vtable_5pysam_6cfaidx_FastaFile;
+  __pyx_vtable_5pysam_6cfaidx_FastaFile._fetch = (char *(*)(struct __pyx_obj_5pysam_6cfaidx_FastaFile *, char *, int, int, int *))__pyx_f_5pysam_6cfaidx_9FastaFile__fetch;
+  if (PyType_Ready(&__pyx_type_5pysam_6cfaidx_FastaFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_6cfaidx_FastaFile.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6cfaidx_Fastafile, "__contains__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6cfaidx_FastaFile, "__contains__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_6cfaidx_9Fastafile_18__contains__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_6cfaidx_9Fastafile_18__contains__.doc = __pyx_doc_5pysam_6cfaidx_9Fastafile_18__contains__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_6cfaidx_9Fastafile_18__contains__;
+      __pyx_wrapperbase_5pysam_6cfaidx_9FastaFile_18__contains__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_6cfaidx_9FastaFile_18__contains__.doc = __pyx_doc_5pysam_6cfaidx_9FastaFile_18__contains__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_6cfaidx_9FastaFile_18__contains__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6cfaidx_Fastafile.tp_dict, __pyx_vtabptr_5pysam_6cfaidx_Fastafile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "Fastafile", (PyObject *)&__pyx_type_5pysam_6cfaidx_Fastafile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6cfaidx_Fastafile = &__pyx_type_5pysam_6cfaidx_Fastafile;
-  if (PyType_Ready(&__pyx_type_5pysam_6cfaidx_FastqProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6cfaidx_FastaFile.tp_dict, __pyx_vtabptr_5pysam_6cfaidx_FastaFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "FastaFile", (PyObject *)&__pyx_type_5pysam_6cfaidx_FastaFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_FastaFile = &__pyx_type_5pysam_6cfaidx_FastaFile;
+  if (PyType_Ready(&__pyx_type_5pysam_6cfaidx_FastqProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6cfaidx_FastqProxy.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "FastqProxy", (PyObject *)&__pyx_type_5pysam_6cfaidx_FastqProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "FastqProxy", (PyObject *)&__pyx_type_5pysam_6cfaidx_FastqProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6cfaidx_FastqProxy = &__pyx_type_5pysam_6cfaidx_FastqProxy;
-  __pyx_vtabptr_5pysam_6cfaidx_Fastqfile = &__pyx_vtable_5pysam_6cfaidx_Fastqfile;
-  __pyx_vtable_5pysam_6cfaidx_Fastqfile.getCurrent = (kseq_t *(*)(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *))__pyx_f_5pysam_6cfaidx_9Fastqfile_getCurrent;
-  __pyx_vtable_5pysam_6cfaidx_Fastqfile.cnext = (int (*)(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *))__pyx_f_5pysam_6cfaidx_9Fastqfile_cnext;
-  if (PyType_Ready(&__pyx_type_5pysam_6cfaidx_Fastqfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 268; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_6cfaidx_Fastqfile.tp_print = 0;
+  __pyx_vtabptr_5pysam_6cfaidx_FastqFile = &__pyx_vtable_5pysam_6cfaidx_FastqFile;
+  __pyx_vtable_5pysam_6cfaidx_FastqFile.getCurrent = (kseq_t *(*)(struct __pyx_obj_5pysam_6cfaidx_FastqFile *))__pyx_f_5pysam_6cfaidx_9FastqFile_getCurrent;
+  __pyx_vtable_5pysam_6cfaidx_FastqFile.cnext = (int (*)(struct __pyx_obj_5pysam_6cfaidx_FastqFile *))__pyx_f_5pysam_6cfaidx_9FastqFile_cnext;
+  if (PyType_Ready(&__pyx_type_5pysam_6cfaidx_FastqFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 269; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_6cfaidx_FastqFile.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6cfaidx_Fastqfile, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 268; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6cfaidx_FastqFile, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 269; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_6cfaidx_9Fastqfile_12__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_6cfaidx_9Fastqfile_12__next__.doc = __pyx_doc_5pysam_6cfaidx_9Fastqfile_12__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_6cfaidx_9Fastqfile_12__next__;
+      __pyx_wrapperbase_5pysam_6cfaidx_9FastqFile_12__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_6cfaidx_9FastqFile_12__next__.doc = __pyx_doc_5pysam_6cfaidx_9FastqFile_12__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_6cfaidx_9FastqFile_12__next__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6cfaidx_Fastqfile.tp_dict, __pyx_vtabptr_5pysam_6cfaidx_Fastqfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 268; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "Fastqfile", (PyObject *)&__pyx_type_5pysam_6cfaidx_Fastqfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 268; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6cfaidx_FastqFile.tp_dict, __pyx_vtabptr_5pysam_6cfaidx_FastqFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 269; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "FastqFile", (PyObject *)&__pyx_type_5pysam_6cfaidx_FastqFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 269; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_FastqFile = &__pyx_type_5pysam_6cfaidx_FastqFile;
+  __pyx_vtabptr_5pysam_6cfaidx_Fastafile = &__pyx_vtable_5pysam_6cfaidx_Fastafile;
+  __pyx_vtable_5pysam_6cfaidx_Fastafile.__pyx_base = *__pyx_vtabptr_5pysam_6cfaidx_FastaFile;
+  __pyx_type_5pysam_6cfaidx_Fastafile.tp_base = __pyx_ptype_5pysam_6cfaidx_FastaFile;
+  if (PyType_Ready(&__pyx_type_5pysam_6cfaidx_Fastafile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_6cfaidx_Fastafile.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6cfaidx_Fastafile.tp_dict, __pyx_vtabptr_5pysam_6cfaidx_Fastafile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "Fastafile", (PyObject *)&__pyx_type_5pysam_6cfaidx_Fastafile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_Fastafile = &__pyx_type_5pysam_6cfaidx_Fastafile;
+  __pyx_vtabptr_5pysam_6cfaidx_Fastqfile = &__pyx_vtable_5pysam_6cfaidx_Fastqfile;
+  __pyx_vtable_5pysam_6cfaidx_Fastqfile.__pyx_base = *__pyx_vtabptr_5pysam_6cfaidx_FastqFile;
+  __pyx_type_5pysam_6cfaidx_Fastqfile.tp_base = __pyx_ptype_5pysam_6cfaidx_FastqFile;
+  if (PyType_Ready(&__pyx_type_5pysam_6cfaidx_Fastqfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 347; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_6cfaidx_Fastqfile.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6cfaidx_Fastqfile.tp_dict, __pyx_vtabptr_5pysam_6cfaidx_Fastqfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 347; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "Fastqfile", (PyObject *)&__pyx_type_5pysam_6cfaidx_Fastqfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 347; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6cfaidx_Fastqfile = &__pyx_type_5pysam_6cfaidx_Fastqfile;
   if (PyType_Ready(&__pyx_type_5pysam_6cfaidx___pyx_scope_struct___open) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 93; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6cfaidx___pyx_scope_struct___open.tp_print = 0;
@@ -6297,25 +6522,32 @@ PyMODINIT_FUNC PyInit_cfaidx(void)
  * # hard-coded constants
  * cdef int max_pos = 2 << 29             # <<<<<<<<<<<<<<
  * 
- * cdef class Fastafile:
+ * ## TODO:
  */
   __pyx_v_5pysam_6cfaidx_max_pos = 1073741824;
 
-  /* "pysam/cfaidx.pyx":339
- *             raise StopIteration
+  /* "pysam/cfaidx.pyx":350
+ *     pass
  * 
- * __all__ = ["Fastafile",             # <<<<<<<<<<<<<<
- *            "Fastqfile"]
+ * __all__ = ["FastaFile",             # <<<<<<<<<<<<<<
+ *            "FastqFile",
+ *            "Fastafile",
  */
-  __pyx_t_1 = PyList_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_INCREF(__pyx_n_s_FastaFile);
+  PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_FastaFile);
+  __Pyx_GIVEREF(__pyx_n_s_FastaFile);
+  __Pyx_INCREF(__pyx_n_s_FastqFile);
+  PyList_SET_ITEM(__pyx_t_1, 1, __pyx_n_s_FastqFile);
+  __Pyx_GIVEREF(__pyx_n_s_FastqFile);
   __Pyx_INCREF(__pyx_n_s_Fastafile);
-  PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_Fastafile);
+  PyList_SET_ITEM(__pyx_t_1, 2, __pyx_n_s_Fastafile);
   __Pyx_GIVEREF(__pyx_n_s_Fastafile);
   __Pyx_INCREF(__pyx_n_s_Fastqfile);
-  PyList_SET_ITEM(__pyx_t_1, 1, __pyx_n_s_Fastqfile);
+  PyList_SET_ITEM(__pyx_t_1, 3, __pyx_n_s_Fastqfile);
   __Pyx_GIVEREF(__pyx_n_s_Fastqfile);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
   /* "pysam/cfaidx.pyx":1
diff --git a/pysam/cfaidx.pxd b/pysam/cfaidx.pxd
index a3501fe..2539340 100644
--- a/pysam/cfaidx.pxd
+++ b/pysam/cfaidx.pxd
@@ -11,7 +11,7 @@ cdef extern from "htslib/kstring.h" nogil:
         size_t l, m
         char *s
 
-cdef class Fastafile:
+cdef class FastaFile:
     cdef object _filename, _references, _lengths, reference2length
     cdef faidx_t* fastafile
     cdef char* _fetch(self, char* reference,
@@ -22,7 +22,7 @@ cdef class FastqProxy:
     cdef kseq_t * _delegate
 
 
-cdef class Fastqfile:
+cdef class FastqFile:
     cdef object _filename
     cdef gzFile fastqfile
     cdef kseq_t * entry
@@ -30,3 +30,10 @@ cdef class Fastqfile:
     cdef kseq_t * getCurrent( self )
     cdef int cnext(self)
 
+
+# Compatibility Layer for pysam < 0.8
+cdef class Fastafile(FastaFile):
+    pass
+
+cdef class Fastqfile(FastqFile):
+    pass
diff --git a/pysam/cfaidx.pyx b/pysam/cfaidx.pyx
index 5e3c473..d5ce6a3 100644
--- a/pysam/cfaidx.pyx
+++ b/pysam/cfaidx.pyx
@@ -20,7 +20,7 @@ from cpython cimport PyErr_SetString, \
 from cpython.version cimport PY_MAJOR_VERSION
 
 from chtslib cimport \
-    faidx_fetch_nseq, fai_load, fai_destroy, fai_fetch, \
+    faidx_nseq, fai_load, fai_destroy, fai_fetch, \
     faidx_fetch_seq, gzopen, gzclose, \
     kseq_init, kseq_destroy, kseq_read
 
@@ -60,16 +60,16 @@ cdef bytes _encodeFilename(object filename):
 # hard-coded constants
 cdef int max_pos = 2 << 29
 
-cdef class Fastafile:
+## TODO:
+##        add automatic indexing.
+##        add function to get sequence names.
+cdef class FastaFile:
     '''*(filename)*
 
     A *FASTA* file. The file is automatically opened.
 
-    The file expects an indexed fasta file.
-
-    TODO:
-        add automatic indexing.
-        add function to get sequence names.
+    This class expects an indexed fasta file and permits
+    random access to fasta sequences.
     '''
 
     def __cinit__(self, *args, **kwargs ):
@@ -86,9 +86,9 @@ cdef class Fastafile:
 
     def __len__(self):
         if self.fastafile == NULL:
-            raise ValueError( "calling len() on closed file" )
+            raise ValueError("calling len() on closed file")
 
-        return faidx_fetch_nseq(self.fastafile)
+        return faidx_nseq(self.fastafile)
 
     def _open(self, filename):
         '''open an indexed fasta file.
@@ -142,19 +142,20 @@ cdef class Fastafile:
         def __get__(self):
             return self._lengths
 
-    def fetch( self,
-               reference = None,
-               start = None,
-               end = None,
-               region = None):
+    def fetch(self,
+              reference=None,
+              start=None,
+              end=None,
+              region=None):
 
         '''*(reference = None, start = None, end = None, region = None)*
 
-        fetch :class:`AlignedRead` objects in a :term:`region` using 0-based indexing.
+        fetch sequences in a :term:`region` using 0-based indexing.
 
         The region is specified by :term:`reference`, *start* and *end*.
 
-        fetch returns an empty string if the region is out of range or addresses an unknown *reference*.
+        fetch returns an empty string if the region is out of range or
+        addresses an unknown *reference*.
 
         If *reference* is given and *start* is None, the sequence from the
         first base is returned. Similarly, if *end* is None, the sequence
@@ -225,14 +226,14 @@ cdef class Fastafile:
                                end-1,
                                length )
 
-    def getReferenceLength( self, reference ):
+    def get_reference_length(self, reference):
         '''return the length of reference.'''
         return self.reference2length[reference]
 
     def __getitem__(self, reference):
         return self.fetch(reference)
 
-    def __contains__( self, reference ):
+    def __contains__(self, reference):
         '''return true if reference in fasta file.'''
         return reference in self.reference2length
 
@@ -265,11 +266,14 @@ cdef class FastqProxy:
                 return self._delegate.qual.s
             else: return None
 
-cdef class Fastqfile:
+cdef class FastqFile:
     '''*(filename)*
 
     A *FASTQ* file. The file is automatically opened.
 
+    This file object permits iterating over all entries in
+    a fastq file. Random access is not implemented.
+
     '''
     def __cinit__(self, *args, **kwargs):
         # self.fastqfile = <gzFile*>NULL
@@ -336,5 +340,14 @@ cdef class Fastqfile:
         else:
             raise StopIteration
 
-__all__ = ["Fastafile",
+# Compatibility Layer for pysam < 0.8
+cdef class Fastafile(FastaFile):
+    pass
+
+cdef class Fastqfile(FastqFile):
+    pass
+
+__all__ = ["FastaFile",
+           "FastqFile",
+           "Fastafile",
            "Fastqfile"]
diff --git a/pysam/chtslib.c b/pysam/chtslib.c
index 41a3b84..0c05607 100644
--- a/pysam/chtslib.c
+++ b/pysam/chtslib.c
@@ -1,4 +1,4 @@
-/* Generated by Cython 0.20.1 on Tue Jul 15 21:20:07 2014 */
+/* Generated by Cython 0.20.1 on Fri Nov 21 19:49:04 2014 */
 
 #define PY_SSIZE_T_CLEAN
 #ifndef CYTHON_USE_PYLONG_INTERNALS
diff --git a/pysam/chtslib.pxd b/pysam/chtslib.pxd
index f566b46..d62e281 100644
--- a/pysam/chtslib.pxd
+++ b/pysam/chtslib.pxd
@@ -711,6 +711,11 @@ cdef extern from "htslib/sam.h" nogil:
     # set bam_pileup1_t::level, while the later does. Level helps the
     # implementation of alignment viewers, but calculating this has some
     # overhead.
+    # 
+    # is_del, is_head, etc are a bit field, declaring as below should
+    # work as expected, see
+    # https://groups.google.com/forum/#!msg/cython-users/24tD1kwRY7A/pmoPuSmanM0J
+
     ctypedef struct bam_pileup1_t:
         bam1_t *b
         int32_t qpos
@@ -776,6 +781,7 @@ cdef extern from "pysam_stream.h" nogil:
     int gzclose(gzFile)
 
     kstream_t * ks_init(gzFile)
+    void ks_destroy(kstream_t *)
 
     # Retrieve characters from stream until delimiter
     # is reached placing results in str.
@@ -786,27 +792,30 @@ cdef extern from "pysam_stream.h" nogil:
 
 cdef extern from "htslib/faidx.h":
 
-   ctypedef struct faidx_t:
-      pass
+    ctypedef struct faidx_t:
+       pass
+
+    int fai_build(char *fn)
 
-   int fai_build(char *fn)
+    void fai_destroy(faidx_t *fai)
 
-   void fai_destroy(faidx_t *fai)
+    faidx_t *fai_load(char *fn)
 
-   faidx_t *fai_load(char *fn)
+    char *fai_fetch(faidx_t *fai,
+                    char *reg,
+                    int *len)
 
-   char *fai_fetch(faidx_t *fai,
-                   char *reg,
-                   int *len)
+    int faidx_nseq(faidx_t *fai)
 
-   int faidx_fetch_nseq(faidx_t *fai)
+    int faidx_has_seq(faidx_t *fai, const char *seq)
 
-   char *faidx_fetch_seq(faidx_t *fai,
+    char *faidx_fetch_seq(faidx_t *fai,
                          char *c_name,
                          int p_beg_i,
                          int p_end_i,
                          int *len)
 
+    int faidx_seq_len(faidx_t *fai, const char *seq)
 
 # tabix support
 cdef extern from "htslib/tbx.h" nogil:
diff --git a/pysam/csamfile.c b/pysam/csamfile.c
index 5b338b9..c3e3252 100644
--- a/pysam/csamfile.c
+++ b/pysam/csamfile.c
@@ -1,4 +1,4 @@
-/* Generated by Cython 0.20.1 on Wed Jul 16 21:19:35 2014 */
+/* Generated by Cython 0.20.1 on Fri Nov 21 19:49:04 2014 */
 
 #define PY_SSIZE_T_CLEAN
 #ifndef CYTHON_USE_PYLONG_INTERNALS
@@ -543,46 +543,47 @@ static const char *__pyx_filename;
 static const char *__pyx_f[] = {
   "csamfile.pyx",
   "cfaidx.pxd",
+  "calignmentfile.pxd",
   "type.pxd",
   "bool.pxd",
   "complex.pxd",
 };
 
 /*--- Type declarations ---*/
-struct __pyx_obj_5pysam_6cfaidx_Fastafile;
+struct __pyx_obj_5pysam_6cfaidx_FastaFile;
 struct __pyx_obj_5pysam_6cfaidx_FastqProxy;
+struct __pyx_obj_5pysam_6cfaidx_FastqFile;
+struct __pyx_obj_5pysam_6cfaidx_Fastafile;
 struct __pyx_obj_5pysam_6cfaidx_Fastqfile;
+struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment;
+struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile;
+struct __pyx_obj_5pysam_14calignmentfile_PileupColumn;
+struct __pyx_obj_5pysam_14calignmentfile_PileupRead;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRow;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs;
+struct __pyx_obj_5pysam_14calignmentfile_IndexedReads;
 struct __pyx_obj_5pysam_8csamfile_AlignedRead;
 struct __pyx_obj_5pysam_8csamfile_Samfile;
-struct __pyx_obj_5pysam_8csamfile_PileupProxy;
-struct __pyx_obj_5pysam_8csamfile_PileupRead;
-struct __pyx_obj_5pysam_8csamfile_IteratorRow;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowHead;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowAll;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs;
-struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection;
-struct __pyx_obj_5pysam_8csamfile_IteratorColumn;
-struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion;
-struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs;
-struct __pyx_obj_5pysam_8csamfile_IndexedReads;
-struct __pyx_obj_5pysam_8csamfile_SNPCall;
-struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr;
-struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr;
-struct __pyx_t_5pysam_8csamfile___iterdata;
-typedef struct __pyx_t_5pysam_8csamfile___iterdata __pyx_t_5pysam_8csamfile___iterdata;
-struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag;
-struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData;
-struct __pyx_opt_args_5pysam_8csamfile_get_type_code;
+struct __pyx_t_5pysam_14calignmentfile___iterdata;
+typedef struct __pyx_t_5pysam_14calignmentfile___iterdata __pyx_t_5pysam_14calignmentfile___iterdata;
+struct __pyx_opt_args_5pysam_14calignmentfile_14AlignedSegment_setTag;
+struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData;
 
-/* "pysam/csamfile.pxd":54
+/* "calignmentfile.pxd":53
  * # Utility types
  * 
  * ctypedef struct __iterdata:             # <<<<<<<<<<<<<<
  *     htsFile * htsfile
  *     bam_hdr_t * header
  */
-struct __pyx_t_5pysam_8csamfile___iterdata {
+struct __pyx_t_5pysam_14calignmentfile___iterdata {
   htsFile *htsfile;
   bam_hdr_t *header;
   hts_itr_t *iter;
@@ -592,53 +593,41 @@ struct __pyx_t_5pysam_8csamfile___iterdata {
   int seq_len;
 };
 
-/* "pysam/csamfile.pxd":73
- *     # add an alignment tag with value to the AlignedRead
+/* "calignmentfile.pxd":72
+ *     # add an alignment tag with value to the AlignedSegment
  *     # an existing tag of the same name will be replaced.
  *     cpdef setTag( self, tag, value, value_type = ?, replace = ? )             # <<<<<<<<<<<<<<
  * 
- * cdef class Samfile:
+ * cdef class AlignmentFile:
  */
-struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag {
+struct __pyx_opt_args_5pysam_14calignmentfile_14AlignedSegment_setTag {
   int __pyx_n;
   PyObject *value_type;
   PyObject *replace;
 };
 
-/* "pysam/csamfile.pxd":179
+/* "calignmentfile.pxd":179
  *     cdef char * getSequence( self )
  *     cdef setMask(self, mask)
  *     cdef setupIteratorData(self,             # <<<<<<<<<<<<<<
  *                            int tid,
  *                            int start,
  */
-struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData {
-  int __pyx_n;
-  int reopen;
-};
-
-/* "pysam/csamfile.pyx":1948
- *     return qual
- * 
- * cdef inline uint8_t get_type_code(value, value_type = None):             # <<<<<<<<<<<<<<
- *     '''guess type code for a *value*. If *value_type* is None,
- *     the type code will be inferred based on the Python type of
- */
-struct __pyx_opt_args_5pysam_8csamfile_get_type_code {
+struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData {
   int __pyx_n;
-  PyObject *value_type;
+  int multiple_iterators;
 };
 
 /* "cfaidx.pxd":14
  *         char *s
  * 
- * cdef class Fastafile:             # <<<<<<<<<<<<<<
+ * cdef class FastaFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename, _references, _lengths, reference2length
  *     cdef faidx_t* fastafile
  */
-struct __pyx_obj_5pysam_6cfaidx_Fastafile {
+struct __pyx_obj_5pysam_6cfaidx_FastaFile {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile *__pyx_vtab;
   PyObject *_filename;
   PyObject *_references;
   PyObject *_lengths;
@@ -663,43 +652,66 @@ struct __pyx_obj_5pysam_6cfaidx_FastqProxy {
 /* "cfaidx.pxd":25
  * 
  * 
- * cdef class Fastqfile:             # <<<<<<<<<<<<<<
+ * cdef class FastqFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename
  *     cdef gzFile fastqfile
  */
-struct __pyx_obj_5pysam_6cfaidx_Fastqfile {
+struct __pyx_obj_5pysam_6cfaidx_FastqFile {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile *__pyx_vtab;
   PyObject *_filename;
   gzFile fastqfile;
   kseq_t *entry;
 };
 
 
-/* "pysam/csamfile.pxd":66
+/* "cfaidx.pxd":35
+ * 
+ * # Compatibility Layer for pysam < 0.8
+ * cdef class Fastafile(FastaFile):             # <<<<<<<<<<<<<<
+ *     pass
+ * 
+ */
+struct __pyx_obj_5pysam_6cfaidx_Fastafile {
+  struct __pyx_obj_5pysam_6cfaidx_FastaFile __pyx_base;
+};
+
+
+/* "cfaidx.pxd":38
+ *     pass
+ * 
+ * cdef class Fastqfile(FastqFile):             # <<<<<<<<<<<<<<
+ *     pass
+ */
+struct __pyx_obj_5pysam_6cfaidx_Fastqfile {
+  struct __pyx_obj_5pysam_6cfaidx_FastqFile __pyx_base;
+};
+
+
+/* "calignmentfile.pxd":65
  * #
  * # Note: need to declare all C fields and methods here
- * cdef class AlignedRead:             # <<<<<<<<<<<<<<
+ * cdef class AlignedSegment:             # <<<<<<<<<<<<<<
  * 
- *     # object that this AlignedRead represents
+ *     # object that this AlignedSegment represents
  */
-struct __pyx_obj_5pysam_8csamfile_AlignedRead {
+struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment *__pyx_vtab;
   bam1_t *_delegate;
 };
 
 
-/* "pysam/csamfile.pxd":75
+/* "calignmentfile.pxd":74
  *     cpdef setTag( self, tag, value, value_type = ?, replace = ? )
  * 
- * cdef class Samfile:             # <<<<<<<<<<<<<<
+ * cdef class AlignmentFile:             # <<<<<<<<<<<<<<
  * 
  *     cdef object _filename
  */
-struct __pyx_obj_5pysam_8csamfile_Samfile {
+struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_8csamfile_Samfile *__pyx_vtab;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile *__pyx_vtab;
   PyObject *_filename;
   htsFile *htsfile;
   BGZF *fp;
@@ -714,14 +726,14 @@ struct __pyx_obj_5pysam_8csamfile_Samfile {
 };
 
 
-/* "pysam/csamfile.pxd":112
+/* "calignmentfile.pxd":111
  *     cdef char * _getrname(self, int tid)
  * 
- * cdef class PileupProxy:             # <<<<<<<<<<<<<<
+ * cdef class PileupColumn:             # <<<<<<<<<<<<<<
  *     cdef bam_pileup1_t ** plp
  *     cdef int tid
  */
-struct __pyx_obj_5pysam_8csamfile_PileupProxy {
+struct __pyx_obj_5pysam_14calignmentfile_PileupColumn {
   PyObject_HEAD
   bam_pileup1_t **plp;
   int tid;
@@ -730,16 +742,16 @@ struct __pyx_obj_5pysam_8csamfile_PileupProxy {
 };
 
 
-/* "pysam/csamfile.pxd":118
+/* "calignmentfile.pxd":117
  *     cdef int n_pu
  * 
  * cdef class PileupRead:             # <<<<<<<<<<<<<<
- *     cdef AlignedRead _alignment
+ *     cdef AlignedSegment _alignment
  *     cdef int32_t  _qpos
  */
-struct __pyx_obj_5pysam_8csamfile_PileupRead {
+struct __pyx_obj_5pysam_14calignmentfile_PileupRead {
   PyObject_HEAD
-  struct __pyx_obj_5pysam_8csamfile_AlignedRead *_alignment;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *_alignment;
   int32_t _qpos;
   int _indel;
   int _level;
@@ -749,215 +761,187 @@ struct __pyx_obj_5pysam_8csamfile_PileupRead {
 };
 
 
-/* "pysam/csamfile.pxd":127
+/* "calignmentfile.pxd":126
  *     cdef uint32_t _is_tail
  * 
  * cdef class IteratorRow:             # <<<<<<<<<<<<<<
  *     cdef int retval
  *     cdef bam1_t * b
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRow {
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRow {
   PyObject_HEAD
   int retval;
   bam1_t *b;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *samfile;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *samfile;
   htsFile *htsfile;
+  bam_hdr_t *header;
   int owns_samfile;
 };
 
 
-/* "pysam/csamfile.pxd":134
+/* "calignmentfile.pxd":134
  *     cdef int owns_samfile
  * 
  * cdef class IteratorRowRegion(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef hts_itr_t * iter
  *     cdef bam1_t * getCurrent( self )
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion *__pyx_vtab;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion *__pyx_vtab;
   hts_itr_t *iter;
 };
 
 
-/* "pysam/csamfile.pxd":139
+/* "calignmentfile.pxd":139
  *     cdef int cnext(self)
  * 
  * cdef class IteratorRowHead(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef int max_rows
  *     cdef int current_row
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowHead {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowHead *__pyx_vtab;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowHead *__pyx_vtab;
   int max_rows;
   int current_row;
 };
 
 
-/* "pysam/csamfile.pxd":145
+/* "calignmentfile.pxd":145
  *     cdef int cnext(self)
  * 
  * cdef class IteratorRowAll(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef bam1_t * getCurrent( self )
  *     cdef int cnext(self)
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowAll {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowAll *__pyx_vtab;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowAll *__pyx_vtab;
 };
 
 
-/* "pysam/csamfile.pxd":149
+/* "calignmentfile.pxd":149
  *     cdef int cnext(self)
  * 
  * cdef class IteratorRowAllRefs(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef int         tid
  *     cdef IteratorRowRegion rowiter
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
   int tid;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *rowiter;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *rowiter;
 };
 
 
-/* "pysam/csamfile.pxd":153
+/* "calignmentfile.pxd":153
  *     cdef IteratorRowRegion rowiter
  * 
  * cdef class IteratorRowSelection(IteratorRow):             # <<<<<<<<<<<<<<
  *     cdef int current_pos
  *     cdef positions
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow __pyx_base;
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection *__pyx_vtab;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRow __pyx_base;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowSelection *__pyx_vtab;
   int current_pos;
   PyObject *positions;
   BGZF *fp;
 };
 
 
-/* "pysam/csamfile.pxd":160
+/* "calignmentfile.pxd":160
  *     cdef BGZF * fp
  * 
  * cdef class IteratorColumn:             # <<<<<<<<<<<<<<
  * 
  *     # result of the last plbuf_push
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorColumn {
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn {
   PyObject_HEAD
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn *__pyx_vtab;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *iter;
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn *__pyx_vtab;
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *iter;
   int tid;
   int pos;
   int n_plp;
   int mask;
   bam_pileup1_t *plp;
   bam_plp_t pileup_iter;
-  __pyx_t_5pysam_8csamfile___iterdata iterdata;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *samfile;
+  __pyx_t_5pysam_14calignmentfile___iterdata iterdata;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *samfile;
   struct __pyx_obj_5pysam_6cfaidx_Fastafile *fastafile;
   PyObject *stepper;
   int max_depth;
 };
 
 
-/* "pysam/csamfile.pxd":187
- *     cdef reset(self, tid, start, end)
+/* "calignmentfile.pxd":188
+ *     cdef _free_pileup_iter(self)
  * 
  * cdef class IteratorColumnRegion(IteratorColumn):             # <<<<<<<<<<<<<<
  *     cdef int start
  *     cdef int end
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn __pyx_base;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn __pyx_base;
   int start;
   int end;
   int truncate;
 };
 
 
-/* "pysam/csamfile.pxd":192
+/* "calignmentfile.pxd":193
  *     cdef int truncate
  * 
  * cdef class IteratorColumnAllRefs(IteratorColumn):             # <<<<<<<<<<<<<<
  *     pass
  * 
  */
-struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn __pyx_base;
+struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs {
+  struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn __pyx_base;
 };
 
 
-/* "pysam/csamfile.pxd":195
+/* "calignmentfile.pxd":196
  *     pass
  * 
  * cdef class IndexedReads:             # <<<<<<<<<<<<<<
- *     cdef Samfile samfile
+ *     cdef AlignmentFile samfile
  *     cdef htsFile * htsfile
  */
-struct __pyx_obj_5pysam_8csamfile_IndexedReads {
+struct __pyx_obj_5pysam_14calignmentfile_IndexedReads {
   PyObject_HEAD
-  struct __pyx_obj_5pysam_8csamfile_Samfile *samfile;
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *samfile;
   htsFile *htsfile;
   PyObject *index;
   int owns_samfile;
   BGZF *fp;
+  bam_hdr_t *header;
 };
 
 
-/* "pysam/csamfile.pyx":3324
+/* "pysam/csamfile.pxd":9
+ * from chtslib cimport *
  * 
+ * cdef class AlignedRead(AlignedSegment):             # <<<<<<<<<<<<<<
+ *     pass
  * 
- * cdef class SNPCall:             # <<<<<<<<<<<<<<
- *     '''the results of a SNP call.'''
- *     cdef int _tid
- */
-struct __pyx_obj_5pysam_8csamfile_SNPCall {
-  PyObject_HEAD
-  int _tid;
-  int _pos;
-  char _reference_base;
-  char _genotype;
-  int _consensus_quality;
-  int _snp_quality;
-  int _rms_mapping_quality;
-  int _coverage;
-};
-
-
-/* "pysam/csamfile.pyx":105
- * cdef char* CODE2CIGAR= "MIDNSHP=X"
- * if IS_PYTHON3:
- *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
- * else:
- *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
  */
-struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr {
-  PyObject_HEAD
-  PyObject *__pyx_v_x;
-  PyObject *__pyx_v_y;
-  PyObject *__pyx_t_0;
-  PyObject *__pyx_t_1;
-  PyObject *(*__pyx_t_2)(PyObject *);
+struct __pyx_obj_5pysam_8csamfile_AlignedRead {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment __pyx_base;
 };
 
 
-/* "pysam/csamfile.pyx":107
- *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
- * else:
- *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
- * CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )
+/* "pysam/csamfile.pxd":12
+ *     pass
+ * 
+ * cdef class Samfile(AlignmentFile):             # <<<<<<<<<<<<<<
+ *     pass
  * 
  */
-struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr {
-  PyObject_HEAD
-  PyObject *__pyx_v_x;
-  PyObject *__pyx_v_y;
-  PyObject *__pyx_t_0;
-  PyObject *__pyx_t_1;
-  PyObject *(*__pyx_t_2)(PyObject *);
+struct __pyx_obj_5pysam_8csamfile_Samfile {
+  struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile __pyx_base;
 };
 
 
@@ -965,168 +949,224 @@ struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr {
 /* "cfaidx.pxd":14
  *         char *s
  * 
- * cdef class Fastafile:             # <<<<<<<<<<<<<<
+ * cdef class FastaFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename, _references, _lengths, reference2length
  *     cdef faidx_t* fastafile
  */
 
-struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile {
-  char *(*_fetch)(struct __pyx_obj_5pysam_6cfaidx_Fastafile *, char *, int, int, int *);
+struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile {
+  char *(*_fetch)(struct __pyx_obj_5pysam_6cfaidx_FastaFile *, char *, int, int, int *);
 };
-static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile *__pyx_vtabptr_5pysam_6cfaidx_Fastafile;
+static struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile *__pyx_vtabptr_5pysam_6cfaidx_FastaFile;
 
 
 /* "cfaidx.pxd":25
  * 
  * 
- * cdef class Fastqfile:             # <<<<<<<<<<<<<<
+ * cdef class FastqFile:             # <<<<<<<<<<<<<<
  *     cdef object _filename
  *     cdef gzFile fastqfile
  */
 
+struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile {
+  kseq_t *(*getCurrent)(struct __pyx_obj_5pysam_6cfaidx_FastqFile *);
+  int (*cnext)(struct __pyx_obj_5pysam_6cfaidx_FastqFile *);
+};
+static struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile *__pyx_vtabptr_5pysam_6cfaidx_FastqFile;
+
+
+/* "cfaidx.pxd":35
+ * 
+ * # Compatibility Layer for pysam < 0.8
+ * cdef class Fastafile(FastaFile):             # <<<<<<<<<<<<<<
+ *     pass
+ * 
+ */
+
+struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile {
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile __pyx_base;
+};
+static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile *__pyx_vtabptr_5pysam_6cfaidx_Fastafile;
+
+
+/* "cfaidx.pxd":38
+ *     pass
+ * 
+ * cdef class Fastqfile(FastqFile):             # <<<<<<<<<<<<<<
+ *     pass
+ */
+
 struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile {
-  kseq_t *(*getCurrent)(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *);
-  int (*cnext)(struct __pyx_obj_5pysam_6cfaidx_Fastqfile *);
+  struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile __pyx_base;
 };
 static struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile *__pyx_vtabptr_5pysam_6cfaidx_Fastqfile;
 
 
-/* "pysam/csamfile.pyx":2045
- * ###########################################################
- * ###########################################################
- * cdef class AlignedRead:             # <<<<<<<<<<<<<<
- *     '''
- *     Class representing an aligned read. See the SAM format specification for
+/* "calignmentfile.pxd":65
+ * #
+ * # Note: need to declare all C fields and methods here
+ * cdef class AlignedSegment:             # <<<<<<<<<<<<<<
+ * 
+ *     # object that this AlignedSegment represents
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead {
-  PyObject *(*setTag)(struct __pyx_obj_5pysam_8csamfile_AlignedRead *, PyObject *, PyObject *, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag *__pyx_optional_args);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment {
+  PyObject *(*setTag)(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *, PyObject *, PyObject *, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_14calignmentfile_14AlignedSegment_setTag *__pyx_optional_args);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead *__pyx_vtabptr_5pysam_8csamfile_AlignedRead;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment *__pyx_vtabptr_5pysam_14calignmentfile_AlignedSegment;
 
 
-/* "pysam/csamfile.pyx":247
+/* "calignmentfile.pxd":74
+ *     cpdef setTag( self, tag, value, value_type = ?, replace = ? )
  * 
+ * cdef class AlignmentFile:             # <<<<<<<<<<<<<<
  * 
- * cdef class Samfile:             # <<<<<<<<<<<<<<
- *     '''*(filename, mode=None, template = None,
- *          referencenames = None, referencelengths = None,
+ *     cdef object _filename
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_Samfile {
-  bam_hdr_t *(*_buildHeader)(struct __pyx_obj_5pysam_8csamfile_Samfile *, PyObject *);
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_Samfile *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_Samfile *);
-  int (*write)(struct __pyx_obj_5pysam_8csamfile_Samfile *, struct __pyx_obj_5pysam_8csamfile_AlignedRead *, int __pyx_skip_dispatch);
-  char *(*_getrname)(struct __pyx_obj_5pysam_8csamfile_Samfile *, int);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile {
+  bam_hdr_t *(*_buildHeader)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *, PyObject *);
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *);
+  int (*write)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *, struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment *, int __pyx_skip_dispatch);
+  char *(*_getrname)(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile *, int);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_Samfile *__pyx_vtabptr_5pysam_8csamfile_Samfile;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile *__pyx_vtabptr_5pysam_14calignmentfile_AlignmentFile;
 
 
-/* "pysam/csamfile.pyx":1266
- *             hts_close(self.htsfile)
+/* "calignmentfile.pxd":134
+ *     cdef int owns_samfile
  * 
  * cdef class IteratorRowRegion(IteratorRow):             # <<<<<<<<<<<<<<
- *     """*(Samfile samfile, int tid, int beg, int end, int reopen = True )*
- * 
+ *     cdef hts_itr_t * iter
+ *     cdef bam1_t * getCurrent( self )
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion {
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion {
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion *);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion *__pyx_vtabptr_5pysam_8csamfile_IteratorRowRegion;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion *__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowRegion;
 
 
-/* "pysam/csamfile.pyx":1330
- *         hts_itr_destroy(self.iter)
+/* "calignmentfile.pxd":139
+ *     cdef int cnext(self)
  * 
  * cdef class IteratorRowHead(IteratorRow):             # <<<<<<<<<<<<<<
- *     """*(Samfile samfile, n, int reopen = True)*
- * 
+ *     cdef int max_rows
+ *     cdef int current_row
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowHead {
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowHead {
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead *);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowHead *__pyx_vtabptr_5pysam_8csamfile_IteratorRowHead;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowHead *__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowHead;
 
 
-/* "pysam/csamfile.pyx":1383
- * 
+/* "calignmentfile.pxd":145
+ *     cdef int cnext(self)
  * 
  * cdef class IteratorRowAll(IteratorRow):             # <<<<<<<<<<<<<<
- *     """*(Samfile samfile, int reopen = True)*
+ *     cdef bam1_t * getCurrent( self )
+ *     cdef int cnext(self)
+ */
+
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowAll {
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll *);
+};
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowAll *__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowAll;
+
+
+/* "calignmentfile.pxd":153
+ *     cdef IteratorRowRegion rowiter
  * 
+ * cdef class IteratorRowSelection(IteratorRow):             # <<<<<<<<<<<<<<
+ *     cdef int current_pos
+ *     cdef positions
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowAll {
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowSelection {
+  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *);
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection *);
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowAll *__pyx_vtabptr_5pysam_8csamfile_IteratorRowAll;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowSelection *__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowSelection;
 
 
-/* "pysam/csamfile.pyx":1484
+/* "calignmentfile.pxd":160
+ *     cdef BGZF * fp
  * 
+ * cdef class IteratorColumn:             # <<<<<<<<<<<<<<
  * 
- * cdef class IteratorRowSelection(IteratorRow):             # <<<<<<<<<<<<<<
- *     """*(Samfile samfile)*
+ *     # result of the last plbuf_push
+ */
+
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn {
+  int (*cnext)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *);
+  char *(*getSequence)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *);
+  PyObject *(*setMask)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *, PyObject *);
+  PyObject *(*setupIteratorData)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *, int, int, int, struct __pyx_opt_args_5pysam_14calignmentfile_14IteratorColumn_setupIteratorData *__pyx_optional_args);
+  PyObject *(*reset)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *, PyObject *, PyObject *, PyObject *);
+  PyObject *(*_free_pileup_iter)(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn *);
+};
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn *__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumn;
+
+
+/* "calignmentfile.pxd":188
+ *     cdef _free_pileup_iter(self)
  * 
+ * cdef class IteratorColumnRegion(IteratorColumn):             # <<<<<<<<<<<<<<
+ *     cdef int start
+ *     cdef int end
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection {
-  bam1_t *(*getCurrent)(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *);
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnRegion {
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn __pyx_base;
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection *__pyx_vtabptr_5pysam_8csamfile_IteratorRowSelection;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnRegion *__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnRegion;
 
 
-/* "pysam/csamfile.pyx":1608
- *     return ret
+/* "calignmentfile.pxd":193
+ *     cdef int truncate
  * 
- * cdef class IteratorColumn:             # <<<<<<<<<<<<<<
- *     '''abstract base class for iterators over columns.
+ * cdef class IteratorColumnAllRefs(IteratorColumn):             # <<<<<<<<<<<<<<
+ *     pass
  * 
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn {
-  int (*cnext)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *);
-  char *(*getSequence)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *);
-  PyObject *(*setMask)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, PyObject *);
-  PyObject *(*setupIteratorData)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, int, int, int, struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData *__pyx_optional_args);
-  PyObject *(*reset)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, PyObject *, PyObject *, PyObject *);
+struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnAllRefs {
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn __pyx_base;
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn *__pyx_vtabptr_5pysam_8csamfile_IteratorColumn;
+static struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnAllRefs *__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnAllRefs;
 
 
-/* "pysam/csamfile.pyx":1782
+/* "pysam/csamfile.pyx":33
  * 
  * 
- * cdef class IteratorColumnRegion(IteratorColumn):             # <<<<<<<<<<<<<<
- *     '''iterates over a region only.
- *     '''
+ * cdef class AlignedRead(AlignedSegment):             # <<<<<<<<<<<<<<
+ *     '''Deprecated alternative for :class:`~pysam.AlignedSegment`
+ * 
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion {
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn __pyx_base;
+struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead {
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment __pyx_base;
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion *__pyx_vtabptr_5pysam_8csamfile_IteratorColumnRegion;
+static struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead *__pyx_vtabptr_5pysam_8csamfile_AlignedRead;
 
 
-/* "pysam/csamfile.pyx":1820
+/* "pysam/csamfile.pyx":25
  * 
  * 
- * cdef class IteratorColumnAllRefs(IteratorColumn):             # <<<<<<<<<<<<<<
- *     """iterates over all columns by chaining iterators over each reference
- *     """
+ * cdef class Samfile(AlignmentFile):             # <<<<<<<<<<<<<<
+ *     '''Deprecated alternative for :class:`~pysam.AlignmentFile`
+ * 
  */
 
-struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs {
-  struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn __pyx_base;
+struct __pyx_vtabstruct_5pysam_8csamfile_Samfile {
+  struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile __pyx_base;
 };
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_vtabptr_5pysam_8csamfile_IteratorColumnAllRefs;
+static struct __pyx_vtabstruct_5pysam_8csamfile_Samfile *__pyx_vtabptr_5pysam_8csamfile_Samfile;
 #ifndef CYTHON_REFNANNY
   #define CYTHON_REFNANNY 0
 #endif
@@ -1189,6 +1229,16 @@ static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_vta
 #define __Pyx_CLEAR(r)    do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0)
 #define __Pyx_XCLEAR(r)   do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0)
 
+static void __Pyx_call_next_tp_dealloc(PyObject* obj, destructor current_tp_dealloc);
+
+static int __Pyx_call_next_tp_traverse(PyObject* obj, visitproc v, void *a, traverseproc current_tp_traverse);
+
+static void __Pyx_call_next_tp_clear(PyObject* obj, inquiry current_tp_dealloc);
+
+static void* __Pyx_GetVtable(PyObject *dict); /*proto*/
+
+static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
+
 #if CYTHON_COMPILING_IN_CPYTHON
 static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) {
     PyTypeObject* tp = Py_TYPE(obj);
@@ -1204,561 +1254,149 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject
 #define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n)
 #endif
 
-static PyObject *__Pyx_GetBuiltinName(PyObject *name); /*proto*/
-
-#ifndef CYTHON_PROFILE
-  #define CYTHON_PROFILE 1
-#endif
-#ifndef CYTHON_TRACE
-  #define CYTHON_TRACE 0
-#endif
-#if CYTHON_TRACE
-  #undef CYTHON_PROFILE_REUSE_FRAME
-#endif
-#ifndef CYTHON_PROFILE_REUSE_FRAME
-  #define CYTHON_PROFILE_REUSE_FRAME 0
-#endif
-#if CYTHON_PROFILE || CYTHON_TRACE
-  #include "compile.h"
-  #include "frameobject.h"
-  #include "traceback.h"
-  #if CYTHON_PROFILE_REUSE_FRAME
-    #define CYTHON_FRAME_MODIFIER static
-    #define CYTHON_FRAME_DEL
-  #else
-    #define CYTHON_FRAME_MODIFIER
-    #define CYTHON_FRAME_DEL Py_CLEAR(__pyx_frame)
-  #endif
-  #define __Pyx_TraceDeclarations                                     \
-  static PyCodeObject *__pyx_frame_code = NULL;                      \
-  CYTHON_FRAME_MODIFIER PyFrameObject *__pyx_frame = NULL;           \
-  int __Pyx_use_tracing = 0;
-  #define __Pyx_TraceCall(funcname, srcfile, firstlineno)                            \
-  if (unlikely(PyThreadState_GET()->use_tracing &&                                   \
-          (PyThreadState_GET()->c_profilefunc || (CYTHON_TRACE && PyThreadState_GET()->c_tracefunc)))) {      \
-      __Pyx_use_tracing = __Pyx_TraceSetupAndCall(&__pyx_frame_code, &__pyx_frame, funcname, srcfile, firstlineno);  \
-  }
-  #define __Pyx_TraceException()                                                           \
-  if (unlikely(__Pyx_use_tracing) && PyThreadState_GET()->use_tracing &&                   \
-          (PyThreadState_GET()->c_profilefunc || (CYTHON_TRACE && PyThreadState_GET()->c_tracefunc))) {  \
-      PyThreadState* tstate = PyThreadState_GET();                                         \
-      tstate->use_tracing = 0;                                                             \
-      PyObject *exc_info = __Pyx_GetExceptionTuple();                                      \
-      if (exc_info) {                                                                      \
-          if (CYTHON_TRACE && tstate->c_tracefunc)                                         \
-              tstate->c_tracefunc(                                                         \
-                  tstate->c_traceobj, __pyx_frame, PyTrace_EXCEPTION, exc_info);          \
-          tstate->c_profilefunc(                                                           \
-              tstate->c_profileobj, __pyx_frame, PyTrace_EXCEPTION, exc_info);            \
-          Py_DECREF(exc_info);                                                             \
-      }                                                                                    \
-      tstate->use_tracing = 1;                                                             \
-  }
-  #define __Pyx_TraceReturn(result)                                                  \
-  if (unlikely(__Pyx_use_tracing) && PyThreadState_GET()->use_tracing) {             \
-      PyThreadState* tstate = PyThreadState_GET();                                   \
-      tstate->use_tracing = 0;                                                        \
-      if (CYTHON_TRACE && tstate->c_tracefunc)                                       \
-          tstate->c_tracefunc(                                                       \
-              tstate->c_traceobj, __pyx_frame, PyTrace_RETURN, (PyObject*)result);  \
-      if (tstate->c_profilefunc)                                                     \
-          tstate->c_profilefunc(                                                     \
-              tstate->c_profileobj, __pyx_frame, PyTrace_RETURN, (PyObject*)result);  \
-      CYTHON_FRAME_DEL;                                                              \
-      tstate->use_tracing = 1;                                                       \
-  }
-  static PyCodeObject *__Pyx_createFrameCodeObject(const char *funcname, const char *srcfile, int firstlineno); /*proto*/
-  static int __Pyx_TraceSetupAndCall(PyCodeObject** code, PyFrameObject** frame, const char *funcname, const char *srcfile, int firstlineno); /*proto*/
-#else
-  #define __Pyx_TraceDeclarations
-  #define __Pyx_TraceCall(funcname, srcfile, firstlineno)
-  #define __Pyx_TraceException()
-  #define __Pyx_TraceReturn(result)
-#endif /* CYTHON_PROFILE */
-#if CYTHON_TRACE
-  #define __Pyx_TraceLine(lineno)                                                          \
-  if (unlikely(__Pyx_use_tracing) && unlikely(PyThreadState_GET()->use_tracing && PyThreadState_GET()->c_tracefunc)) {    \
-      PyThreadState* tstate = PyThreadState_GET();                                         \
-      __pyx_frame->f_lineno = lineno;                                                     \
-      tstate->use_tracing = 0;                                                             \
-      tstate->c_tracefunc(tstate->c_traceobj, __pyx_frame, PyTrace_LINE, NULL);           \
-      tstate->use_tracing = 1;                                                             \
-  }
-#else
-  #define __Pyx_TraceLine(lineno)
-#endif
-
-#if CYTHON_COMPILING_IN_CPYTHON
-static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw); /*proto*/
-#else
-#define __Pyx_PyObject_Call(func, arg, kw) PyObject_Call(func, arg, kw)
-#endif
-
-#include <string.h>
+static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); /*proto*/
 
-static CYTHON_INLINE PyObject* __Pyx_decode_c_string(
-         const char* cstring, Py_ssize_t start, Py_ssize_t stop,
-         const char* encoding, const char* errors,
-         PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors));
+static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value);
 
-static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
-static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
+static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *);
 
-static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/
+static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *);
 
-static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
+static int __Pyx_check_binary_version(void);
 
-#if CYTHON_COMPILING_IN_CPYTHON
-#define __Pyx_PyObject_DelAttrStr(o,n) __Pyx_PyObject_SetAttrStr(o,n,NULL)
-static CYTHON_INLINE int __Pyx_PyObject_SetAttrStr(PyObject* obj, PyObject* attr_name, PyObject* value) {
-    PyTypeObject* tp = Py_TYPE(obj);
-    if (likely(tp->tp_setattro))
-        return tp->tp_setattro(obj, attr_name, value);
+#if !defined(__Pyx_PyIdentifier_FromString)
 #if PY_MAJOR_VERSION < 3
-    if (likely(tp->tp_setattr))
-        return tp->tp_setattr(obj, PyString_AS_STRING(attr_name), value);
-#endif
-    return PyObject_SetAttr(obj, attr_name, value);
-}
+  #define __Pyx_PyIdentifier_FromString(s) PyString_FromString(s)
 #else
-#define __Pyx_PyObject_DelAttrStr(o,n)   PyObject_DelAttr(o,n)
-#define __Pyx_PyObject_SetAttrStr(o,n,v) PyObject_SetAttr(o,n,v)
+  #define __Pyx_PyIdentifier_FromString(s) PyUnicode_FromString(s)
 #endif
-
-#if CYTHON_COMPILING_IN_CPYTHON
-static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {
-    PyListObject* L = (PyListObject*) list;
-    Py_ssize_t len = Py_SIZE(list);
-    if (likely(L->allocated > len) & likely(len > (L->allocated >> 1))) {
-        Py_INCREF(x);
-        PyList_SET_ITEM(list, len, x);
-        Py_SIZE(list) = len+1;
-        return 0;
-    }
-    return PyList_Append(list, x);
-}
-#else
-#define __Pyx_PyList_Append(L,x) PyList_Append(L,x)
 #endif
 
-#if PY_MAJOR_VERSION < 3
-#define __Pyx_PyString_Join __Pyx_PyBytes_Join
-#define __Pyx_PyBaseString_Join(s, v) (PyUnicode_CheckExact(s) ? PyUnicode_Join(s, v) : __Pyx_PyBytes_Join(s, v))
-#else
-#define __Pyx_PyString_Join PyUnicode_Join
-#define __Pyx_PyBaseString_Join PyUnicode_Join
-#endif
-#if CYTHON_COMPILING_IN_CPYTHON
-    #if PY_MAJOR_VERSION < 3
-    #define __Pyx_PyBytes_Join _PyString_Join
-    #else
-    #define __Pyx_PyBytes_Join _PyBytes_Join
-    #endif
-#else
-static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* values); /*proto*/
-#endif
+static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
 
-static CYTHON_INLINE int __Pyx_CheckKeywordStrings(PyObject *kwdict, const char* function_name, int kw_allowed); /*proto*/
+static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, size_t size, int strict);  /*proto*/
 
-static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/
+typedef struct {
+    int code_line;
+    PyCodeObject* code_object;
+} __Pyx_CodeObjectCacheEntry;
+struct __Pyx_CodeObjectCache {
+    int count;
+    int max_count;
+    __Pyx_CodeObjectCacheEntry* entries;
+};
+static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL};
+static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line);
+static PyCodeObject *__pyx_find_code_object(int code_line);
+static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object);
 
-static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \
-    PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \
-    const char* function_name); /*proto*/
+static void __Pyx_AddTraceback(const char *funcname, int c_line,
+                               int py_line, const char *filename); /*proto*/
 
-static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
-    Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/
+static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
 
-static CYTHON_INLINE int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
-    const char *name, int exact); /*proto*/
 
-static CYTHON_INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
-static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
+/* Module declarations from 'libc.stdint' */
 
-static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
+/* Module declarations from 'libc.string' */
 
-static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
+/* Module declarations from 'libc.stdlib' */
 
-static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
+/* Module declarations from 'libc.stdio' */
 
-#if PY_MAJOR_VERSION >= 3
-#define __Pyx_PyString_Equals __Pyx_PyUnicode_Equals
-#else
-#define __Pyx_PyString_Equals __Pyx_PyBytes_Equals
-#endif
+/* Module declarations from 'pysam.chtslib' */
 
-#define __Pyx_GetItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
-    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
-    __Pyx_GetItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound, boundscheck) : \
-    (is_list ? (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL) : \
-               __Pyx_GetItemInt_Generic(o, to_py_func(i))))
-#define __Pyx_GetItemInt_List(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
-    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
-    __Pyx_GetItemInt_List_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) : \
-    (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL))
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i,
-                                                              int wraparound, int boundscheck);
-#define __Pyx_GetItemInt_Tuple(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
-    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
-    __Pyx_GetItemInt_Tuple_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) : \
-    (PyErr_SetString(PyExc_IndexError, "tuple index out of range"), (PyObject*)NULL))
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i,
-                                                              int wraparound, int boundscheck);
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j);
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
-                                                     int is_list, int wraparound, int boundscheck);
+/* Module declarations from 'pysam.cfaidx' */
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastaFile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastqProxy = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastqFile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastafile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastqfile = 0;
 
-#if CYTHON_COMPILING_IN_CPYTHON
-static CYTHON_INLINE int __Pyx_ListComp_Append(PyObject* list, PyObject* x) {
-    PyListObject* L = (PyListObject*) list;
-    Py_ssize_t len = Py_SIZE(list);
-    if (likely(L->allocated > len)) {
-        Py_INCREF(x);
-        PyList_SET_ITEM(list, len, x);
-        Py_SIZE(list) = len+1;
-        return 0;
-    }
-    return PyList_Append(list, x);
-}
-#else
-#define __Pyx_ListComp_Append(L,x) PyList_Append(L,x)
-#endif
+/* Module declarations from 'pysam.calignmentfile' */
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_AlignedSegment = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_AlignmentFile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_PileupColumn = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_PileupRead = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRow = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowHead = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowAll = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowAllRefs = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorRowSelection = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorColumn = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorColumnRegion = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IteratorColumnAllRefs = 0;
+static PyTypeObject *__pyx_ptype_5pysam_14calignmentfile_IndexedReads = 0;
 
-static PyObject* __Pyx_PyObject_CallMethodTuple(PyObject* obj, PyObject* method_name, PyObject* args) {
-    PyObject *method, *result = NULL;
-    if (unlikely(!args)) return NULL;
-    method = __Pyx_PyObject_GetAttrStr(obj, method_name);
-    if (unlikely(!method)) goto bad;
-    result = __Pyx_PyObject_Call(method, args, NULL);
-    Py_DECREF(method);
-bad:
-    Py_DECREF(args);
-    return result;
-}
-#define __Pyx_PyObject_CallMethod3(obj, name, arg1, arg2, arg3) \
-    __Pyx_PyObject_CallMethodTuple(obj, name, PyTuple_Pack(3, arg1, arg2, arg3))
-#define __Pyx_PyObject_CallMethod2(obj, name, arg1, arg2) \
-    __Pyx_PyObject_CallMethodTuple(obj, name, PyTuple_Pack(2, arg1, arg2))
-#define __Pyx_PyObject_CallMethod1(obj, name, arg1) \
-    __Pyx_PyObject_CallMethodTuple(obj, name, PyTuple_Pack(1, arg1))
-#define __Pyx_PyObject_CallMethod0(obj, name) \
-    __Pyx_PyObject_CallMethodTuple(obj, name, (Py_INCREF(__pyx_empty_tuple), __pyx_empty_tuple))
+/* Module declarations from 'cpython.version' */
 
-static CYTHON_INLINE int __Pyx_PyObject_Append(PyObject* L, PyObject* x); /*proto*/
+/* Module declarations from 'cpython.ref' */
 
-static CYTHON_INLINE int __Pyx_PySequence_Contains(PyObject* item, PyObject* seq, int eq) {
-    int result = PySequence_Contains(seq, item);
-    return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
-}
+/* Module declarations from 'cpython.exc' */
 
-static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name); /*proto*/
+/* Module declarations from 'cpython.module' */
 
-static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(
-        PyObject* obj, Py_ssize_t cstart, Py_ssize_t cstop,
-        PyObject** py_start, PyObject** py_stop, PyObject** py_slice,
-        int has_cstart, int has_cstop, int wraparound);
+/* Module declarations from 'cpython.mem' */
 
-static void __Pyx_WriteUnraisable(const char *name, int clineno,
-                                  int lineno, const char *filename,
-                                  int full_traceback); /*proto*/
+/* Module declarations from 'cpython.tuple' */
 
-static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);
+/* Module declarations from 'cpython.list' */
 
-static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);
+/* Module declarations from 'cpython.object' */
 
-static CYTHON_INLINE int __Pyx_IterFinish(void); /*proto*/
+/* Module declarations from 'cpython.sequence' */
 
-static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected); /*proto*/
+/* Module declarations from 'cpython.mapping' */
 
-static CYTHON_INLINE int __Pyx_PyDict_Contains(PyObject* item, PyObject* dict, int eq) {
-    int result = PyDict_Contains(dict, item);
-    return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
-}
+/* Module declarations from 'cpython.iterator' */
 
-#if PY_MAJOR_VERSION >= 3
-static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
-    PyObject *value;
-    value = PyDict_GetItemWithError(d, key);
-    if (unlikely(!value)) {
-        if (!PyErr_Occurred()) {
-            PyObject* args = PyTuple_Pack(1, key);
-            if (likely(args))
-                PyErr_SetObject(PyExc_KeyError, args);
-            Py_XDECREF(args);
-        }
-        return NULL;
-    }
-    Py_INCREF(value);
-    return value;
-}
-#else
-    #define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key)
-#endif
+/* Module declarations from '__builtin__' */
 
-static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObject* default_value); /*proto*/
+/* Module declarations from 'cpython.type' */
+static PyTypeObject *__pyx_ptype_7cpython_4type_type = 0;
 
-static CYTHON_INLINE long __Pyx_div_long(long, long); /* proto */
+/* Module declarations from 'cpython.number' */
 
-static CYTHON_INLINE long __Pyx_mod_long(long, long); /* proto */
+/* Module declarations from 'cpython.int' */
 
-static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname);
+/* Module declarations from '__builtin__' */
 
-static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
+/* Module declarations from 'cpython.bool' */
+static PyTypeObject *__pyx_ptype_7cpython_4bool_bool = 0;
 
-static void* __Pyx_GetVtable(PyObject *dict); /*proto*/
+/* Module declarations from 'cpython.long' */
 
-static PyObject *__Pyx_CalculateMetaclass(PyTypeObject *metaclass, PyObject *bases);
+/* Module declarations from 'cpython.float' */
 
-static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type);
+/* Module declarations from '__builtin__' */
 
-#define __Pyx_CyFunction_USED 1
-#include <structmember.h>
-#define __Pyx_CYFUNCTION_STATICMETHOD  0x01
-#define __Pyx_CYFUNCTION_CLASSMETHOD   0x02
-#define __Pyx_CYFUNCTION_CCLASS        0x04
-#define __Pyx_CyFunction_GetClosure(f) \
-    (((__pyx_CyFunctionObject *) (f))->func_closure)
-#define __Pyx_CyFunction_GetClassObj(f) \
-    (((__pyx_CyFunctionObject *) (f))->func_classobj)
-#define __Pyx_CyFunction_Defaults(type, f) \
-    ((type *)(((__pyx_CyFunctionObject *) (f))->defaults))
-#define __Pyx_CyFunction_SetDefaultsGetter(f, g) \
-    ((__pyx_CyFunctionObject *) (f))->defaults_getter = (g)
-typedef struct {
-    PyCFunctionObject func;
-    PyObject *func_dict;
-    PyObject *func_weakreflist;
-    PyObject *func_name;
-    PyObject *func_qualname;
-    PyObject *func_doc;
-    PyObject *func_globals;
-    PyObject *func_code;
-    PyObject *func_closure;
-    PyObject *func_classobj; /* No-args super() class cell */
-    void *defaults;
-    int defaults_pyobjects;
-    int flags;
-    PyObject *defaults_tuple;   /* Const defaults tuple */
-    PyObject *defaults_kwdict;  /* Const kwonly defaults dict */
-    PyObject *(*defaults_getter)(PyObject *);
-    PyObject *func_annotations; /* function annotations dict */
-} __pyx_CyFunctionObject;
-static PyTypeObject *__pyx_CyFunctionType = 0;
-#define __Pyx_CyFunction_NewEx(ml, flags, qualname, self, module, globals, code) \
-    __Pyx_CyFunction_New(__pyx_CyFunctionType, ml, flags, qualname, self, module, globals, code)
-static PyObject *__Pyx_CyFunction_New(PyTypeObject *, PyMethodDef *ml,
-                                      int flags, PyObject* qualname,
-                                      PyObject *self,
-                                      PyObject *module, PyObject *globals,
-                                      PyObject* code);
-static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *m,
-                                                         size_t size,
-                                                         int pyobjects);
-static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *m,
-                                                            PyObject *tuple);
-static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *m,
-                                                             PyObject *dict);
-static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *m,
-                                                              PyObject *dict);
-static int __Pyx_CyFunction_init(void);
+/* Module declarations from 'cpython.complex' */
+static PyTypeObject *__pyx_ptype_7cpython_7complex_complex = 0;
 
-static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *qualname,
-                                           PyObject *mkw, PyObject *modname, PyObject *doc); /*proto*/
-static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict,
-                                      PyObject *mkw, int calculate_metaclass, int allow_py2_metaclass); /*proto*/
+/* Module declarations from 'cpython.string' */
 
-static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); /*proto*/
+/* Module declarations from 'cpython.unicode' */
 
-static CYTHON_INLINE uint64_t __Pyx_PyInt_As_uint64_t(PyObject *);
+/* Module declarations from 'cpython.dict' */
 
-static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *);
+/* Module declarations from 'cpython.instance' */
 
-static CYTHON_INLINE uint32_t __Pyx_PyInt_As_uint32_t(PyObject *);
+/* Module declarations from 'cpython.function' */
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value);
+/* Module declarations from 'cpython.method' */
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int8_t(int8_t value);
+/* Module declarations from 'cpython.weakref' */
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint8_t(uint8_t value);
+/* Module declarations from 'cpython.getargs' */
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int16_t(int16_t value);
+/* Module declarations from 'cpython.pythread' */
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint16_t(uint16_t value);
+/* Module declarations from 'cpython.pystate' */
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int32_t(int32_t value);
+/* Module declarations from 'cpython.cobject' */
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint32_t(uint32_t value);
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value);
-
-static CYTHON_INLINE size_t __Pyx_PyInt_As_size_t(PyObject *);
-
-static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_As_PY_LONG_LONG(PyObject *);
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_PY_LONG_LONG(PY_LONG_LONG value);
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int64_t(int64_t value);
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint64_t(uint64_t value);
-
-static CYTHON_INLINE int64_t __Pyx_PyInt_As_int64_t(PyObject *);
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_char(char value);
-
-static CYTHON_INLINE uint8_t __Pyx_PyInt_As_uint8_t(PyObject *);
-
-static CYTHON_INLINE int32_t __Pyx_PyInt_As_int32_t(PyObject *);
-
-static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *);
-
-static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *);
-
-static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
-
-#define __Pyx_Generator_USED
-#include <structmember.h>
-#include <frameobject.h>
-typedef PyObject *(*__pyx_generator_body_t)(PyObject *, PyObject *);
-typedef struct {
-    PyObject_HEAD
-    __pyx_generator_body_t body;
-    PyObject *closure;
-    PyObject *exc_type;
-    PyObject *exc_value;
-    PyObject *exc_traceback;
-    PyObject *gi_weakreflist;
-    PyObject *classobj;
-    PyObject *yieldfrom;
-    int resume_label;
-    char is_running;
-} __pyx_GeneratorObject;
-static __pyx_GeneratorObject *__Pyx_Generator_New(__pyx_generator_body_t body,
-                                                  PyObject *closure);
-static int __pyx_Generator_init(void);
-static int __Pyx_Generator_clear(PyObject* self);
-#if 1 || PY_VERSION_HEX < 0x030300B0
-static int __Pyx_PyGen_FetchStopIterationValue(PyObject **pvalue);
-#else
-#define __Pyx_PyGen_FetchStopIterationValue(pvalue) PyGen_FetchStopIterationValue(pvalue)
-#endif
-
-static int __Pyx_check_binary_version(void);
-
-#if !defined(__Pyx_PyIdentifier_FromString)
-#if PY_MAJOR_VERSION < 3
-  #define __Pyx_PyIdentifier_FromString(s) PyString_FromString(s)
-#else
-  #define __Pyx_PyIdentifier_FromString(s) PyUnicode_FromString(s)
-#endif
-#endif
-
-static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
-
-static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, size_t size, int strict);  /*proto*/
-
-typedef struct {
-    int code_line;
-    PyCodeObject* code_object;
-} __Pyx_CodeObjectCacheEntry;
-struct __Pyx_CodeObjectCache {
-    int count;
-    int max_count;
-    __Pyx_CodeObjectCacheEntry* entries;
-};
-static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL};
-static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line);
-static PyCodeObject *__pyx_find_code_object(int code_line);
-static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object);
-
-static void __Pyx_AddTraceback(const char *funcname, int c_line,
-                               int py_line, const char *filename); /*proto*/
-
-static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
-
-
-/* Module declarations from 'libc.stdint' */
-
-/* Module declarations from 'libc.string' */
-
-/* Module declarations from 'libc.stdlib' */
-
-/* Module declarations from 'libc.stdio' */
-
-/* Module declarations from 'pysam.chtslib' */
-
-/* Module declarations from 'pysam.cfaidx' */
-static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastafile = 0;
-static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_FastqProxy = 0;
-static PyTypeObject *__pyx_ptype_5pysam_6cfaidx_Fastqfile = 0;
-
-/* Module declarations from 'cpython.version' */
-
-/* Module declarations from 'cpython.ref' */
-
-/* Module declarations from 'cpython.exc' */
-
-/* Module declarations from 'cpython.module' */
-
-/* Module declarations from 'cpython.mem' */
-
-/* Module declarations from 'cpython.tuple' */
-
-/* Module declarations from 'cpython.list' */
-
-/* Module declarations from 'cpython.object' */
-
-/* Module declarations from 'cpython.sequence' */
-
-/* Module declarations from 'cpython.mapping' */
-
-/* Module declarations from 'cpython.iterator' */
-
-/* Module declarations from '__builtin__' */
-
-/* Module declarations from 'cpython.type' */
-static PyTypeObject *__pyx_ptype_7cpython_4type_type = 0;
-
-/* Module declarations from 'cpython.number' */
-
-/* Module declarations from 'cpython.int' */
-
-/* Module declarations from '__builtin__' */
-
-/* Module declarations from 'cpython.bool' */
-static PyTypeObject *__pyx_ptype_7cpython_4bool_bool = 0;
-
-/* Module declarations from 'cpython.long' */
-
-/* Module declarations from 'cpython.float' */
-
-/* Module declarations from '__builtin__' */
-
-/* Module declarations from 'cpython.complex' */
-static PyTypeObject *__pyx_ptype_7cpython_7complex_complex = 0;
-
-/* Module declarations from 'cpython.string' */
-
-/* Module declarations from 'cpython.unicode' */
-
-/* Module declarations from 'cpython.dict' */
-
-/* Module declarations from 'cpython.instance' */
-
-/* Module declarations from 'cpython.function' */
-
-/* Module declarations from 'cpython.method' */
-
-/* Module declarations from 'cpython.weakref' */
-
-/* Module declarations from 'cpython.getargs' */
-
-/* Module declarations from 'cpython.pythread' */
-
-/* Module declarations from 'cpython.pystate' */
-
-/* Module declarations from 'cpython.cobject' */
-
-/* Module declarations from 'cpython.oldbuffer' */
+/* Module declarations from 'cpython.oldbuffer' */
 
 /* Module declarations from 'cpython.set' */
 
@@ -1773,41845 +1411,826 @@ static PyTypeObject *__pyx_ptype_7cpython_7complex_complex = 0;
 /* Module declarations from 'pysam.csamfile' */
 static PyTypeObject *__pyx_ptype_5pysam_8csamfile_AlignedRead = 0;
 static PyTypeObject *__pyx_ptype_5pysam_8csamfile_Samfile = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_PileupProxy = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_PileupRead = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRow = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowRegion = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowHead = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowAll = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowAllRefs = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorRowSelection = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorColumn = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorColumnRegion = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IteratorColumnAllRefs = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_IndexedReads = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile_SNPCall = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile___pyx_scope_struct__genexpr = 0;
-static PyTypeObject *__pyx_ptype_5pysam_8csamfile___pyx_scope_struct_1_genexpr = 0;
-static PyObject *__pyx_v_5pysam_8csamfile__FILENAME_ENCODING = 0;
-static char *__pyx_v_5pysam_8csamfile_CODE2CIGAR;
-static int __pyx_v_5pysam_8csamfile_max_pos;
-static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *, size_t); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_makeAlignedRead(bam1_t *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_makePileupProxy(bam_pileup1_t **, int, int, int); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *); /*proto*/
-static int __pyx_f_5pysam_8csamfile___advance_all(void *, bam1_t *); /*proto*/
-static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *, bam1_t *); /*proto*/
-static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *); /*proto*/
-static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *); /*proto*/
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *, uint32_t, uint32_t); /*proto*/
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_qual_range(bam1_t *, uint32_t, uint32_t); /*proto*/
-static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *, struct __pyx_opt_args_5pysam_8csamfile_get_type_code *__pyx_optional_args); /*proto*/
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObject *, PyObject *, PyObject *, PyObject *); /*proto*/
 #define __Pyx_MODULE_NAME "pysam.csamfile"
 int __pyx_module_is_main_pysam__csamfile = 0;
 
 /* Implementation of 'pysam.csamfile' */
-static PyObject *__pyx_builtin_object;
-static PyObject *__pyx_builtin_TypeError;
-static PyObject *__pyx_builtin_enumerate;
-static PyObject *__pyx_builtin_ord;
-static PyObject *__pyx_builtin_map;
-static PyObject *__pyx_builtin_ValueError;
-static PyObject *__pyx_builtin_IOError;
-static PyObject *__pyx_builtin_OverflowError;
-static PyObject *__pyx_builtin_NotImplementedError;
-static PyObject *__pyx_builtin_OSError;
-static PyObject *__pyx_builtin_AttributeError;
-static PyObject *__pyx_builtin_zip;
-static PyObject *__pyx_builtin_sorted;
-static PyObject *__pyx_builtin_KeyError;
-static PyObject *__pyx_builtin_StopIteration;
-static PyObject *__pyx_builtin_min;
-static PyObject *__pyx_builtin_max;
-static PyObject *__pyx_builtin_chr;
-static PyObject *__pyx_pf_5pysam_8csamfile_genexpr(CYTHON_UNUSED PyObject *__pyx_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_3genexpr(CYTHON_UNUSED PyObject *__pyx_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_12PileupColumn___str__(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_template, PyObject *__pyx_v_referencenames, PyObject *__pyx_v_referencelengths, PyObject *__pyx_v_text, PyObject *__pyx_v_header, PyObject *__pyx_v_port, PyObject *__pyx_v_add_sq_text, PyObject *__pyx_v_check_header, PyObject *__pyx_v_check_sq); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8gettid(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10getrname(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_tid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_14reset(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_16seek(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, uint64_t __pyx_v_offset, int __pyx_v_where); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_18tell(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid, PyObject *__pyx_v_callback, PyObject *__pyx_v_until_eof, PyObject *__pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_22head(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_n); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_until_eof); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_34write(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_36__enter__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_exc_type, CYTHON_UNUSED PyObject *__pyx_v_exc_value, CYTHON_UNUSED PyObject *__pyx_v_traceback); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8filename___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4text___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_fields, PyObject *__pyx_v_record); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_46__next__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRow *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen); /* proto */
-static void __pyx_pf_5pysam_8csamfile_11IteratorRow_2__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorRow *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_beg, int __pyx_v_end, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_6__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_n, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_4__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_positions, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_fastafile); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_6hasReference(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *__pyx_v_self, CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, int __pyx_v_truncate, CYTHON_UNUSED PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, CYTHON_UNUSED PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_8csamfile_11AlignedRead_2__dealloc__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_other); /* proto */
-static Py_hash_t __pyx_pf_5pysam_8csamfile_11AlignedRead_8__hash__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_10_convert_python_tag(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_pytag, PyObject *__pyx_v_value, PyObject *__pyx_v_fmts, PyObject *__pyx_v_args); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5qname___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5qname_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qname); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_values); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_cigar); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3seq___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_seq); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qual___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4qual_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qual); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5query___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5qqual___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6qstart___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qend___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tags); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12setTag(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag, PyObject *__pyx_v_value, PyObject *__pyx_v_value_type, PyObject *__pyx_v_replace); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4flag___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4flag_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_flag); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rname___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rname_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3tid___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3tid_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3pos___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3pos_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_pos); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3bin___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_bin); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4rlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4aend___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4alen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qual); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mtid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mtid); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mpos); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mpos); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5isize___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5isize_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_isize); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_isize); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9positions___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15inferred_length___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_13aligned_pairs___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6blocks___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_14overlap(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, uint32_t __pyx_v_start, uint32_t __pyx_v_end); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_16opt(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_18fancy_str(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11PileupProxy___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3tid___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_1n___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_11PileupProxy_1n_2__set__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self, PyObject *__pyx_v_n); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3pos___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_10PileupRead___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_9alignment___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_4qpos___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5indel___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_tail___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5level___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_17consensus_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_11snp_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_8csamfile_12IndexedReads___init__(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_12IndexedReads_2build(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_8csamfile_12IndexedReads_4find(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self, PyObject *__pyx_v_qname); /* proto */
-static void __pyx_pf_5pysam_8csamfile_12IndexedReads_6__dealloc__(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self); /* proto */
 static PyObject *__pyx_tp_new_5pysam_8csamfile_AlignedRead(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_8csamfile_Samfile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupProxy(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupRead(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRow(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowRegion(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowHead(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowAll(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowAllRefs(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowSelection(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumn(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumnRegion(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumnAllRefs(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IndexedReads(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile_SNPCall(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct__genexpr(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static char __pyx_k_A[] = "A";
-static char __pyx_k_B[] = "B";
-static char __pyx_k_C[] = "C";
-static char __pyx_k_D[] = "D";
-static char __pyx_k_F[] = "F";
-static char __pyx_k_H[] = "H";
-static char __pyx_k_I[] = "I";
-static char __pyx_k_S[] = "S";
-static char __pyx_k_Z[] = "Z";
-static char __pyx_k_b[] = "b";
-static char __pyx_k_c[] = "c";
-static char __pyx_k_d[] = "d";
-static char __pyx_k_f[] = "f";
-static char __pyx_k_h[] = "h";
-static char __pyx_k_i[] = "i";
-static char __pyx_k_n[] = "n";
-static char __pyx_k_r[] = "r";
-static char __pyx_k_s[] = "@%s";
-static char __pyx_k_w[] = "w";
-static char __pyx_k_AS[] = "AS";
-static char __pyx_k_CL[] = "CL";
-static char __pyx_k_CN[] = "CN";
-static char __pyx_k_CO[] = "CO";
-static char __pyx_k_DS[] = "DS";
-static char __pyx_k_DT[] = "DT";
-static char __pyx_k_FO[] = "FO";
-static char __pyx_k_GO[] = "GO";
-static char __pyx_k_HD[] = "HD";
-static char __pyx_k_ID[] = "ID";
-static char __pyx_k_KS[] = "KS";
-static char __pyx_k_LB[] = "LB";
-static char __pyx_k_LN[] = "LN";
-static char __pyx_k_M5[] = "M5";
-static char __pyx_k_PG[] = "PG";
-static char __pyx_k_PI[] = "PI";
-static char __pyx_k_PL[] = "PL";
-static char __pyx_k_PN[] = "PN";
-static char __pyx_k_PP[] = "PP";
-static char __pyx_k_PU[] = "PU";
-static char __pyx_k_RG[] = "RG";
-static char __pyx_k_SM[] = "SM";
-static char __pyx_k_SN[] = "SN";
-static char __pyx_k_SO[] = "SO";
-static char __pyx_k_SP[] = "SP";
-static char __pyx_k_SQ[] = "SQ";
-static char __pyx_k_UR[] = "UR";
-static char __pyx_k_VN[] = "VN";
-static char __pyx_k__3[] = "\t";
-static char __pyx_k__4[] = "\n";
-static char __pyx_k__6[] = "-";
-static char __pyx_k__9[] = "";
 static char __pyx_k_os[] = "os";
-static char __pyx_k_rU[] = "rU";
-static char __pyx_k_rb[] = "rb";
 static char __pyx_k_re[] = "re";
-static char __pyx_k_wb[] = "wb";
-static char __pyx_k_wh[] = "wh";
-static char __pyx_k__14[] = "[:-]";
-static char __pyx_k__41[] = "@";
-static char __pyx_k__47[] = ":";
-static char __pyx_k__76[] = "<";
-static char __pyx_k__79[] = "(";
-static char __pyx_k__80[] = ")";
-static char __pyx_k_all[] = "all";
-static char __pyx_k_bai[] = ".bai";
-static char __pyx_k_beg[] = "beg";
-static char __pyx_k_bin[] = "bin";
-static char __pyx_k_c_2[] = "%c";
-static char __pyx_k_chr[] = "chr";
-static char __pyx_k_doc[] = "__doc__";
-static char __pyx_k_end[] = "end";
-static char __pyx_k_ftp[] = "ftp:";
-static char __pyx_k_get[] = "get";
-static char __pyx_k_i_c[] = "%i%c";
-static char __pyx_k_map[] = "map";
-static char __pyx_k_max[] = "max";
-static char __pyx_k_min[] = "min";
-static char __pyx_k_ord[] = "ord";
-static char __pyx_k_pos[] = "pos";
-static char __pyx_k_raw[] = "raw";
-static char __pyx_k_s_2[] = "s";
-static char __pyx_k_s_s[] = "%s:%s";
-static char __pyx_k_seq[] = "seq";
-static char __pyx_k_str[] = "__str__";
+static char __pyx_k_all[] = "__all__";
 static char __pyx_k_sys[] = "sys";
-static char __pyx_k_tag[] = "tag";
-static char __pyx_k_tid[] = "tid";
-static char __pyx_k_wbu[] = "wbu";
-static char __pyx_k_zip[] = "zip";
-static char __pyx_k_2scB[] = "2scB";
-static char __pyx_k_2scH[] = "2scH";
-static char __pyx_k_2scI[] = "2scI";
-static char __pyx_k_2scb[] = "2scb";
-static char __pyx_k_2scc[] = "2scc";
-static char __pyx_k_2scf[] = "2scf";
-static char __pyx_k_2sch[] = "2sch";
-static char __pyx_k_2sci[] = "2sci";
-static char __pyx_k_Zidf[] = "Zidf";
-static char __pyx_k_args[] = "args";
-static char __pyx_k_flag[] = "flag";
-static char __pyx_k_fmts[] = "fmts";
-static char __pyx_k_http[] = "http:";
-static char __pyx_k_init[] = "__init__";
-static char __pyx_k_join[] = "join";
 static char __pyx_k_main[] = "__main__";
-static char __pyx_k_mapq[] = "mapq";
-static char __pyx_k_mode[] = "mode";
-static char __pyx_k_mpos[] = "mpos";
-static char __pyx_k_mrnm[] = "mrnm";
-static char __pyx_k_mtid[] = "mtid";
-static char __pyx_k_open[] = "_open";
-static char __pyx_k_path[] = "path";
-static char __pyx_k_port[] = "port";
-static char __pyx_k_qpos[] = "qpos";
-static char __pyx_k_qseq[] = "qseq";
-static char __pyx_k_qual[] = "qual";
-static char __pyx_k_rlen[] = "rlen";
-static char __pyx_k_seek[] = "seek";
-static char __pyx_k_self[] = "self";
-static char __pyx_k_send[] = "send";
-static char __pyx_k_tags[] = "tags";
 static char __pyx_k_test[] = "__test__";
-static char __pyx_k_text[] = "text";
-static char __pyx_k_warn[] = "warn";
-static char __pyx_k_all_2[] = "__all__";
-static char __pyx_k_ascii[] = "ascii";
-static char __pyx_k_bqual[] = "bqual";
-static char __pyx_k_cigar[] = "cigar";
-static char __pyx_k_close[] = "close";
-static char __pyx_k_fetch[] = "fetch";
-static char __pyx_k_indel[] = "indel";
-static char __pyx_k_isize[] = "isize";
-static char __pyx_k_items[] = "items";
-static char __pyx_k_level[] = "level";
-static char __pyx_k_pytag[] = "pytag";
-static char __pyx_k_qname[] = "qname";
-static char __pyx_k_rname[] = "rname";
-static char __pyx_k_split[] = "split";
-static char __pyx_k_start[] = "start";
-static char __pyx_k_strip[] = "strip";
-static char __pyx_k_throw[] = "throw";
 static char __pyx_k_types[] = "types";
-static char __pyx_k_value[] = "value";
-static char __pyx_k_where[] = "where";
-static char __pyx_k_write[] = "write";
-static char __pyx_k_2sc_is[] = "2sc%is";
-static char __pyx_k_append[] = "append";
 static char __pyx_k_ctypes[] = "ctypes";
-static char __pyx_k_decode[] = "decode";
-static char __pyx_k_encode[] = "encode";
-static char __pyx_k_exists[] = "exists";
-static char __pyx_k_extend[] = "extend";
-static char __pyx_k_fields[] = "fields";
-static char __pyx_k_gettid[] = "gettid";
-static char __pyx_k_header[] = "header";
 static char __pyx_k_import[] = "__import__";
-static char __pyx_k_isOpen[] = "_isOpen";
-static char __pyx_k_is_del[] = "is_del";
-static char __pyx_k_l_data[] = "l_data";
-static char __pyx_k_l_qseq[] = "l_qseq";
-static char __pyx_k_m_data[] = "m_data";
-static char __pyx_k_module[] = "__module__";
-static char __pyx_k_object[] = "object";
-static char __pyx_k_offset[] = "offset";
-static char __pyx_k_record[] = "record";
-static char __pyx_k_region[] = "region";
-static char __pyx_k_reopen[] = "reopen";
-static char __pyx_k_setTag[] = "setTag";
-static char __pyx_k_sorted[] = "sorted";
 static char __pyx_k_struct[] = "struct";
-static char __pyx_k_IOError[] = "IOError";
-static char __pyx_k_OSError[] = "OSError";
 static char __pyx_k_Samfile[] = "Samfile";
-static char __pyx_k_compile[] = "compile";
-static char __pyx_k_findall[] = "findall";
-static char __pyx_k_is_head[] = "is_head";
-static char __pyx_k_is_tail[] = "is_tail";
-static char __pyx_k_islower[] = "islower";
-static char __pyx_k_isupper[] = "isupper";
-static char __pyx_k_l_qname[] = "l_qname";
-static char __pyx_k_lengths[] = "lengths";
-static char __pyx_k_n_cigar[] = "n_cigar";
-static char __pyx_k_pileups[] = "pileups";
-static char __pyx_k_prepare[] = "__prepare__";
-static char __pyx_k_replace[] = "replace";
-static char __pyx_k_samfile[] = "samfile";
-static char __pyx_k_stepper[] = "stepper";
-static char __pyx_k_KeyError[] = "KeyError";
-static char __pyx_k_calcsize[] = "calcsize";
-static char __pyx_k_callback[] = "callback";
-static char __pyx_k_check_sq[] = "check_sq";
-static char __pyx_k_coverage[] = "coverage";
-static char __pyx_k_exc_type[] = "exc_type";
-static char __pyx_k_filename[] = "filename";
-static char __pyx_k_genotype[] = "genotype";
-static char __pyx_k_hasIndex[] = "_hasIndex";
-static char __pyx_k_nextiter[] = "nextiter";
 static char __pyx_k_platform[] = "platform";
-static char __pyx_k_qualname[] = "__qualname__";
-static char __pyx_k_samtools[] = "samtools";
 static char __pyx_k_tempfile[] = "tempfile";
-static char __pyx_k_template[] = "template";
-static char __pyx_k_truncate[] = "truncate";
 static char __pyx_k_warnings[] = "warnings";
-static char __pyx_k_2sccI_i_s[] = "2sccI%i%s";
-static char __pyx_k_30s_10s_s[] = "%-30s %-10s= %s";
-static char __pyx_k_MIDNSHP_X[] = "MIDNSHP=X";
-static char __pyx_k_TypeError[] = "TypeError";
-static char __pyx_k_alignment[] = "alignment";
-static char __pyx_k_buildLine[] = "_buildLine";
-static char __pyx_k_enumerate[] = "enumerate";
-static char __pyx_k_exc_value[] = "exc_value";
-static char __pyx_k_fastafile[] = "fastafile";
 static char __pyx_k_itertools[] = "itertools";
-static char __pyx_k_max_depth[] = "max_depth";
-static char __pyx_k_metaclass[] = "__metaclass__";
-static char __pyx_k_pack_into[] = "pack_into";
-static char __pyx_k_positions[] = "positions";
-static char __pyx_k_reference[] = "reference";
-static char __pyx_k_traceback[] = "traceback";
-static char __pyx_k_until_eof[] = "until_eof";
-static char __pyx_k_CIGAR2CODE[] = "CIGAR2CODE";
-static char __pyx_k_IS_PYTHON3[] = "IS_PYTHON3";
-static char __pyx_k_PileupRead[] = "PileupRead";
-static char __pyx_k_Query_name[] = "Query name";
-static char __pyx_k_ValueError[] = "ValueError";
-static char __pyx_k_checkIndex[] = "_checkIndex";
 static char __pyx_k_pyx_vtable[] = "__pyx_vtable__";
-static char __pyx_k_references[] = "references";
-static char __pyx_k_startswith[] = "startswith";
-static char __pyx_k_value_type[] = "value_type";
 static char __pyx_k_AlignedRead[] = "AlignedRead";
-static char __pyx_k_Binary_flag[] = "Binary flag";
-static char __pyx_k_CIGAR_REGEX[] = "CIGAR_REGEX";
-static char __pyx_k_Insert_size[] = "Insert size";
-static char __pyx_k_IteratorRow[] = "IteratorRow";
-static char __pyx_k_PileupProxy[] = "PileupProxy";
-static char __pyx_k_add_sq_text[] = "add_sq_text";
 static char __pyx_k_collections[] = "collections";
-static char __pyx_k_d_MIDNSHP_X[] = "(\\d+)([MIDNSHP=X])";
-static char __pyx_k_defaultdict[] = "defaultdict";
-static char __pyx_k_nreferences[] = "nreferences";
-static char __pyx_k_parseRegion[] = "_parseRegion";
-static char __pyx_k_snp_quality[] = "snp_quality";
-static char __pyx_k_Contig_index[] = "Contig index";
-static char __pyx_k_IndexedReads[] = "IndexedReads";
-static char __pyx_k_PileupColumn[] = "PileupColumn";
-static char __pyx_k_SQ_SN_s_LN_s[] = "@SQ\tSN:%s\tLN:%s\n";
-static char __pyx_k_check_header[] = "check_header";
-static char __pyx_k_getattribute[] = "__getattribute__";
-static char __pyx_k_version_info[] = "version_info";
-static char __pyx_k_Cigar_entries[] = "Cigar entries";
-static char __pyx_k_Invalid_tag_s[] = "Invalid tag: %s";
-static char __pyx_k_OverflowError[] = "OverflowError";
-static char __pyx_k_StopIteration[] = "StopIteration";
-static char __pyx_k_VALID_HEADERS[] = "VALID_HEADERS";
-static char __pyx_k_AttributeError[] = "AttributeError";
-static char __pyx_k_IteratorColumn[] = "IteratorColumn";
-static char __pyx_k_Quality_scores[] = "Quality scores";
-static char __pyx_k_Query_sequence[] = "Query sequence";
-static char __pyx_k_mate_not_found[] = "mate not found";
-static char __pyx_k_properties_bin[] = "properties bin";
-static char __pyx_k_pysam_csamfile[] = "pysam.csamfile";
-static char __pyx_k_reference_base[] = "reference_base";
-static char __pyx_k_referencenames[] = "referencenames";
-static char __pyx_k_unknown_type_s[] = "unknown type '%s'";
-static char __pyx_k_Mapping_quality[] = "Mapping quality";
-static char __pyx_k_mapping_quality[] = "mapping_quality";
-static char __pyx_k_properties_flag[] = "properties flag";
-static char __pyx_k_file_s_not_found[] = "file `%s` not found";
-static char __pyx_k_read_s_not_found[] = "read %s not found";
-static char __pyx_k_referencelengths[] = "referencelengths";
-static char __pyx_k_consensus_quality[] = "consensus_quality";
-static char __pyx_k_mapping_quality_2[] = "mapping quality";
-static char __pyx_k_tag_s_not_present[] = "tag '%s' not present";
-static char __pyx_k_PileupColumn___str[] = "PileupColumn.__str__";
-static char __pyx_k_VALID_HEADER_ORDER[] = "VALID_HEADER_ORDER";
-static char __pyx_k_VALID_HEADER_TYPES[] = "VALID_HEADER_TYPES";
-static char __pyx_k_end_out_of_range_i[] = "end out of range (%i)";
-static char __pyx_k_getdefaultencoding[] = "getdefaultencoding";
-static char __pyx_k_mate_s_is_unmapped[] = "mate %s: is unmapped";
-static char __pyx_k_read_s_is_unpaired[] = "read %s: is unpaired";
-static char __pyx_k_true_if_QC_failure[] = "true if QC failure";
-static char __pyx_k_Maximum_data_length[] = "Maximum data length";
-static char __pyx_k_NotImplementedError[] = "NotImplementedError";
-static char __pyx_k_VALID_HEADER_FIELDS[] = "VALID_HEADER_FIELDS";
-static char __pyx_k_invalid_reference_s[] = "invalid reference `%s`";
-static char __pyx_k_the_genotype_called[] = "the genotype called.";
-static char __pyx_k_the_template_length[] = "the template length";
-static char __pyx_k_Bam_index_bin_number[] = "Bam index bin number";
-static char __pyx_k_Length_of_query_name[] = "Length of query name";
-static char __pyx_k_create_string_buffer[] = "create_string_buffer";
-static char __pyx_k_start_out_of_range_i[] = "start out of range (%i)";
-static char __pyx_k_Position_of_mate_pair[] = "Position of mate pair";
-static char __pyx_k_could_not_open_file_s[] = "could not open file `%s`";
-static char __pyx_k_getfilesystemencoding[] = "getfilesystemencoding";
-static char __pyx_k_header_line_without_s[] = "header line without '@': '%s'";
-static char __pyx_k_true_if_this_is_read1[] = "true if this is read1";
-static char __pyx_k_true_if_this_is_read2[] = "true if this is read2";
-static char __pyx_k_Count_of_cigar_entries[] = "Count of cigar entries";
-static char __pyx_k_error_during_iteration[] = "error during iteration";
-static char __pyx_k_Length_of_auxilary_data[] = "Length of auxilary data";
-static char __pyx_k_current_sequence_length[] = "current sequence length.";
-static char __pyx_k_Length_of_query_sequence[] = "Length of query sequence";
-static char __pyx_k_the_position_of_the_mate[] = "the position of the mate";
-static char __pyx_k_Mapped_position_on_contig[] = "Mapped position on contig";
-static char __pyx_k_unknown_auxilliary_type_s[] = "unknown auxilliary type '%s'";
-static char __pyx_k_Contig_index_for_mate_pair[] = "Contig index for mate pair";
-static char __pyx_k_nucleotide_position_of_SNP[] = "nucleotide position of SNP.";
-static char __pyx_k_tid_i_out_of_range_0_tid_i[] = "tid %i out of range 0<=tid<%i";
-static char __pyx_k_unable_to_open_index_for_s[] = "unable to open index for `%s` ";
-static char __pyx_k_0_based_leftmost_coordinate[] = "0-based leftmost coordinate";
-static char __pyx_k_error_while_opening_index_s[] = "error while opening index `%s` ";
-static char __pyx_k_invalid_file_opening_mode_s[] = "invalid file opening mode `%s`";
-static char __pyx_k_I_O_operation_on_closed_file[] = "I/O operation on closed file";
-static char __pyx_k_callback_not_implemented_yet[] = "callback not implemented yet";
-static char __pyx_k_no_index_available_for_fetch[] = "no index available for fetch";
-static char __pyx_k_seek_no_available_in_streams[] = "seek no available in streams";
-static char __pyx_k_true_if_the_mate_is_unmapped[] = "true if the mate is unmapped";
-static char __pyx_k_no_index_available_for_pileup[] = "no index available for pileup";
-static char __pyx_k_true_if_not_primary_alignment[] = "true if not primary alignment";
-static char __pyx_k_home_andreas_devel_pysam_pysam[] = "/home/andreas/devel/pysam/pysam/csamfile.pyx";
-static char __pyx_k_term_target_ID_DEPRECATED_from[] = "\n        :term:`target` ID\n\n        DEPRECATED from pysam-0.4 - use tid in the future.\n        The rname field caused a lot of confusion as it returns\n        the :term:`target` ID instead of the reference sequence\n        name.\n\n        .. note::\n\n            This field contains the index of the reference sequence\n            in the sequence dictionary. To obtain the name\n            of the reference sequence, use :meth [...]
-static char __pyx_k_term_target_ID_note_This_field[] = "\n        :term:`target` ID\n\n        .. note::\n\n            This field contains the index of the reference sequence\n            in the sequence dictionary. To obtain the name\n            of the reference sequence, use :meth:`pysam.Samfile.getrname()`\n\n        ";
-static char __pyx_k_A_pileup_column_A_pileup_column[] = "A pileup column. A pileup column contains\n    all the reads that map to a certain target base.\n\n    tid\n        chromosome ID as is defined in the header\n    pos\n        the target base coordinate (0-based)\n    n\n        number of reads mapping to this column\n    pileups\n        list of reads (:class:`pysam.PileupRead`) aligned to this column\n    ";
-static char __pyx_k_a_list_of_start_and_end_positio[] = " a list of start and end positions of\n        aligned gapless blocks.\n\n        The start and end positions are in genomic \n        coordinates. \n      \n        Blocks are not normalized, i.e. two blocks \n        might be directly adjacent. This happens if\n        the two blocks are separated by an insertion \n        in the read.\n        ";
-static char __pyx_k_fetch_called_on_bamfile_without[] = "fetch called on bamfile without index";
-static char __pyx_k_file_does_not_have_valid_header[] = "file does not have valid header (mode='%s') - is it BAM format?";
-static char __pyx_k_header_line_with_invalid_type_s[] = "header line with invalid type '%s': '%s'";
-static char __pyx_k_incomplete_sequence_information[] = "incomplete sequence information in '%s'";
-static char __pyx_k_inferred_read_length_from_CIGAR[] = "inferred read length from CIGAR string.\n\n        Returns 0 if CIGAR string is not present.\n        ";
-static char __pyx_k_invalid_coordinates_start_i_end[] = "invalid coordinates: start (%i) > end (%i)";
-static char __pyx_k_malformatted_header_no_in_field[] = "malformatted header: no ':' in field";
-static char __pyx_k_number_of_reads_mapping_to_this[] = "number of reads mapping to this column.";
-static char __pyx_k_quality_and_sequence_mismatch_i[] = "quality and sequence mismatch: %i != %i";
-static char __pyx_k_the_chromosome_ID_as_is_defined[] = "the chromosome ID as is defined in the header";
-static char __pyx_k_the_root_mean_square_rms_of_the[] = "the root mean square (rms) of the mapping quality of all reads involved in the call.";
-static char __pyx_k_true_if_read_itself_is_unmapped[] = "true if read itself is unmapped";
-static char __pyx_k_1_iff_the_base_on_the_padded_rea[] = "1 iff the base on the padded read is a deletion";
-static char __pyx_k_Argument_must_be_string_bytes_or[] = "Argument must be string, bytes or unicode.";
-static char __pyx_k_Argument_must_be_string_or_unico[] = "Argument must be string or unicode.";
-static char __pyx_k_Invalid_clipping_in_CIGAR_string[] = "Invalid clipping in CIGAR string";
-static char __pyx_k_PileupProxy_accessed_after_itera[] = "PileupProxy accessed after iterator finished";
-static char __pyx_k_Samfile_mapped_only_available_in[] = "Samfile.mapped only available in bam files";
-static char __pyx_k_This_class_cannot_be_instantiate[] = "This class cannot be instantiated from Python";
-static char __pyx_k_Unsupported_value_type_in_set_op[] = "Unsupported value_type in set_option";
-static char __pyx_k_a_class_pysam_AlignedRead_object[] = "a :class:`pysam.AlignedRead` object of the aligned read";
-static char __pyx_k_a_list_of_aligned_read_and_refer[] = "a list of aligned read and reference positions.\n\n        Unaligned position are marked by None.\n        ";
-static char __pyx_k_a_list_of_reference_positions_th[] = "a list of reference positions that this read aligns to.";
-static char __pyx_k_aligned_length_of_the_read_on_th[] = "aligned length of the read on the reference genome.\n\n        This is equal to `aend - pos`. Returns None if not available.";
-static char __pyx_k_aligned_portion_of_the_read_This[] = "aligned portion of the read.\n\n        This is a substring of :attr:`seq` that excludes flanking bases that were\n        :term:`soft clipped` (None if not present). It is equal to ``seq[qstart:qend]``.\n\n        In Python 3, this property is of type bytes. Assigning a\n        unicode string to it consisting of ASCII characters only will\n        work, but is inefficient.\n\n        SAM/BAM files may include extra flanking base [...]
-static char __pyx_k_aligned_query_sequence_quality_v[] = "aligned query sequence quality values (None if not\n        present). These are the quality values that correspond to :attr:`query`, that is,\n        they exclude qualities of :term:`soft clipped` bases. This is equal to\n        ``qual[qstart:qend]``.\n\n        This property is read-only.\n\n        In Python 3, this property is of type bytes.";
-static char __pyx_k_aligned_reference_position_of_th[] = "aligned reference position of the read on the reference genome.  \n        \n        aend points to one past the last aligned residue.\n        Returns None if not available.";
-static char __pyx_k_can_not_iterate_over_samfile_wit[] = "can not iterate over samfile without header";
-static char __pyx_k_can_only_IndexReads_on_bam_files[] = "can only IndexReads on bam files";
-static char __pyx_k_can_t_guess_type_or_invalid_type[] = "can't guess type or invalid type code specified";
-static char __pyx_k_could_not_open_file_mode_s_is_it[] = "could not open file (mode='%s') - is it SAM/BAM format?";
-static char __pyx_k_coverage_or_read_depth_the_numbe[] = "coverage or read depth - the number of reads involved in the call.";
-static char __pyx_k_either_supply_options_template_h[] = "either supply options `template`, `header` or  both `referencenames` and `referencelengths` for writing";
-static char __pyx_k_end_index_of_the_aligned_query_p[] = "end index of the aligned query portion of the sequence (0-based, exclusive)";
-static char __pyx_k_fetch_called_for_htsfile_without[] = "fetch called for htsfile without header";
-static char __pyx_k_fetching_by_region_is_not_availa[] = "fetching by region is not available for sam files";
-static char __pyx_k_file_header_is_empty_mode_s_is_i[] = "file header is empty (mode='%s') - is it SAM/BAM format?";
-static char __pyx_k_full_contents_of_the_term_sam_fi[] = "full contents of the :term:`sam file` header as a string.";
-static char __pyx_k_header_information_within_the_te[] = "header information within the :term:`sam file`. The records and fields are returned as\n        a two-level dictionary.\n        ";
-static char __pyx_k_indel_length_0_for_no_indel_posi[] = "indel length; 0 for no indel, positive for ins and negative for del";
-static char __pyx_k_integer_i_out_of_range_of_BAM_SA[] = "integer %i out of range of BAM/SAM specification";
-static char __pyx_k_invalid_type_for_record_s_s_expe[] = "invalid type for record %s: %s, expected %s";
-static char __pyx_k_length_of_the_aligned_query_sequ[] = "length of the aligned query sequence.\n\n        This is equal to :attr:`qend` - :attr:`qstart`";
-static char __pyx_k_length_of_the_read_This_includes[] = "length of the read. This includes soft-clipped bases\n        and is equal to ``len(seq)``.\n\n        This property is read-only.\n\n        Returns 0 if not available.";
-static char __pyx_k_list_of_reads_class_pysam_Pileup[] = "list of reads (:class:`pysam.PileupRead`) aligned to this column";
-static char __pyx_k_mapping_information_not_recorded[] = "mapping information not recorded in index or index not available";
-static char __pyx_k_multiple_s_lines_are_not_permitt[] = "multiple '%s' lines are not permitted";
-static char __pyx_k_no_index_available_for_iteration[] = "no index available for iteration";
-static char __pyx_k_number_of_term_filename_associat[] = "number of :term:`filename` associated with this object.";
-static char __pyx_k_number_of_term_reference_sequenc[] = "number of :term:`reference` sequences in the file.";
-static char __pyx_k_pileup_of_samfiles_not_implement[] = "pileup of samfiles not implemented yet";
-static char __pyx_k_position_of_the_read_base_at_the[] = "position of the read base at the pileup site, 0-based";
-static char __pyx_k_read_sequence_base_qualities_inc[] = "read sequence base qualities, including :term:`soft\n        clipped` bases (None if not present).\n\n        In Python 3, this property is of type bytes and assigning a\n        unicode string to it consisting of ASCII characters only will\n        work, but is inefficient.\n\n        Note that to set quality scores the sequence has to be set\n        previously as this will determine the permitted length of\n        the quality  [...]
-static char __pyx_k_read_sequence_bases_including_te[] = "read sequence bases, including :term:`soft clipped` bases \n        (None if not present).\n\n        In Python 3, this property is of type bytes and assigning a\n        unicode string to it consisting of ASCII characters only will\n        work, but is inefficient.\n\n        Note that assigning to seq will invalidate any quality scores.\n        Thus, to in-place edit the sequence and quality scores, copies of\n        the qual [...]
-static char __pyx_k_reference_base_at_pos_N_if_no_re[] = "reference base at pos. ``N`` if no reference sequence supplied.";
-static char __pyx_k_reference_sequence_for_s_tid_i_n[] = "reference sequence for '%s' (tid=%i) not found";
-static char __pyx_k_seek_only_available_in_bam_files[] = "seek only available in bam files";
-static char __pyx_k_start_index_of_the_aligned_query[] = "start index of the aligned query portion of the sequence (0-based, inclusive).\n\n        This the index of the first base in :attr:`seq` that is not soft-clipped.\n        ";
-static char __pyx_k_the_genotype_quality_Phred_scale[] = "the genotype quality (Phred-scaled).";
-static char __pyx_k_the_insert_size_deprecated_use_t[] = "the insert size\n        deprecated: use tlen instead";
-static char __pyx_k_the_position_of_the_mate_depreca[] = "the position of the mate\n        deprecated, use PNEXT instead.";
-static char __pyx_k_the_query_name_None_if_not_prese[] = "the query name (None if not present)";
-static char __pyx_k_the_snp_quality_Phred_scaled_pro[] = "the snp quality (Phred scaled) - probability of consensus being identical to reference sequence.";
-static char __pyx_k_the_tags_in_the_AUX_field_This_p[] = "the tags in the AUX field.\n\n        This property permits convenience access to\n        the tags. Changes it the returned list will\n        not update the tags automatically. Instead,\n        the following is required for adding a\n        new tag::\n\n            read.tags = read.tags + [(\"RG\",0)]\n\n        This method will happily write the same tag\n        multiple times.\n        ";
-static char __pyx_k_the_term_cigar_alignment_The_ali[] = "the :term:`cigar` alignment. The alignment\n        is returned as a list of tuples of (operation, length). \n\n        If the alignment is not present, an empty list is\n        returned.\n\n        The operations are:\n\n        +-----+--------------+-----+\n        |M    |BAM_CMATCH    |0    |\n        +-----+--------------+-----+\n        |I    |BAM_CINS      |1    |\n        +-----+--------------+-----+\n        |D    |BAM_CD [...]
-static char __pyx_k_the_term_cigar_alignment_as_a_st[] = "the :term:`cigar` alignment as a string.\n        \n        The cigar string is a string of alternating integers\n        and characters denoting the length and the type of\n        an operation.\n\n        .. note::\n            The order length,operation is specified in the\n            SAM format. It is different from the order of\n            the :attr:`cigar` property.\n\n        Returns the empty string if not present.\n\n   [...]
-static char __pyx_k_the_term_reference_id_of_the_mat[] = "the :term:`reference` id of the mate\n        deprecated, use RNEXT instead.\n        ";
-static char __pyx_k_total_number_of_mapped_alignment[] = "total number of mapped alignments in file.\n        ";
-static char __pyx_k_total_number_of_reads_without_co[] = "total number of reads without coordinates\n        ";
-static char __pyx_k_total_number_of_unmapped_reads_i[] = "total number of unmapped reads in file.\n        ";
-static char __pyx_k_true_if_optical_or_PCR_duplicate[] = "true if optical or PCR duplicate";
-static char __pyx_k_true_if_read_is_mapped_in_a_prop[] = "true if read is mapped in a proper pair";
-static char __pyx_k_true_if_read_is_mapped_to_revers[] = "true if read is mapped to reverse strand";
-static char __pyx_k_true_if_read_is_paired_in_sequen[] = "true if read is paired in sequencing";
-static char __pyx_k_true_is_read_is_mapped_to_revers[] = "true is read is mapped to reverse strand";
-static char __pyx_k_tuple_of_the_lengths_of_the_term[] = "tuple of the lengths of the :term:`reference` sequences. The lengths are in the same order as\n        :attr:`pysam.Samfile.references`\n        ";
-static char __pyx_k_tuple_with_the_names_of_term_ref[] = "tuple with the names of :term:`reference` sequences.";
-static char __pyx_k_unequal_names_and_lengths_of_ref[] = "unequal names and lengths of reference sequences";
-static char __pyx_k_unknown_field_code_s_in_record_s[] = "unknown field code '%s' in record '%s'";
-static char __pyx_k_unknown_stepper_option_s_in_Iter[] = "unknown stepper option `%s` in IteratorColumn";
-static char __pyx_k_file_does_not_have_valid_header_2[] = "file does not have valid header (mode='%s') - is it SAM format?";
-static char __pyx_k_the_term_reference_id_of_the_mat_2[] = "the :term:`reference` id of the mate ";
-static PyObject *__pyx_kp_s_2scB;
-static PyObject *__pyx_kp_s_2scH;
-static PyObject *__pyx_kp_s_2scI;
-static PyObject *__pyx_kp_s_2sc_is;
-static PyObject *__pyx_kp_s_2scb;
-static PyObject *__pyx_kp_s_2scc;
-static PyObject *__pyx_kp_s_2sccI_i_s;
-static PyObject *__pyx_kp_s_2scf;
-static PyObject *__pyx_kp_s_2sch;
-static PyObject *__pyx_kp_s_2sci;
-static PyObject *__pyx_kp_s_30s_10s_s;
-static PyObject *__pyx_n_s_A;
-static PyObject *__pyx_n_s_AS;
-static PyObject *__pyx_kp_s_A_pileup_column_A_pileup_column;
 static PyObject *__pyx_n_s_AlignedRead;
-static PyObject *__pyx_kp_u_Argument_must_be_string_bytes_or;
-static PyObject *__pyx_kp_u_Argument_must_be_string_or_unico;
-static PyObject *__pyx_n_s_AttributeError;
-static PyObject *__pyx_n_s_B;
-static PyObject *__pyx_kp_s_Bam_index_bin_number;
-static PyObject *__pyx_kp_s_Binary_flag;
-static PyObject *__pyx_n_s_C;
-static PyObject *__pyx_n_s_CIGAR2CODE;
-static PyObject *__pyx_n_s_CIGAR_REGEX;
-static PyObject *__pyx_n_s_CL;
-static PyObject *__pyx_n_s_CN;
-static PyObject *__pyx_n_s_CO;
-static PyObject *__pyx_kp_s_Cigar_entries;
-static PyObject *__pyx_kp_s_Contig_index;
-static PyObject *__pyx_kp_s_Contig_index_for_mate_pair;
-static PyObject *__pyx_kp_s_Count_of_cigar_entries;
-static PyObject *__pyx_n_s_D;
-static PyObject *__pyx_n_s_DS;
-static PyObject *__pyx_n_s_DT;
-static PyObject *__pyx_n_s_F;
-static PyObject *__pyx_n_s_FO;
-static PyObject *__pyx_n_s_GO;
-static PyObject *__pyx_n_s_H;
-static PyObject *__pyx_n_s_HD;
-static PyObject *__pyx_n_s_I;
-static PyObject *__pyx_n_s_ID;
-static PyObject *__pyx_n_s_IOError;
-static PyObject *__pyx_n_s_IS_PYTHON3;
-static PyObject *__pyx_kp_s_I_O_operation_on_closed_file;
-static PyObject *__pyx_n_s_IndexedReads;
-static PyObject *__pyx_kp_s_Insert_size;
-static PyObject *__pyx_kp_s_Invalid_tag_s;
-static PyObject *__pyx_n_s_IteratorColumn;
-static PyObject *__pyx_n_s_IteratorRow;
-static PyObject *__pyx_n_s_KS;
-static PyObject *__pyx_n_s_KeyError;
-static PyObject *__pyx_n_s_LB;
-static PyObject *__pyx_n_s_LN;
-static PyObject *__pyx_kp_s_Length_of_auxilary_data;
-static PyObject *__pyx_kp_s_Length_of_query_name;
-static PyObject *__pyx_kp_s_Length_of_query_sequence;
-static PyObject *__pyx_n_s_M5;
-static PyObject *__pyx_kp_s_Mapped_position_on_contig;
-static PyObject *__pyx_kp_s_Mapping_quality;
-static PyObject *__pyx_kp_s_Maximum_data_length;
-static PyObject *__pyx_n_s_NotImplementedError;
-static PyObject *__pyx_n_s_OSError;
-static PyObject *__pyx_n_s_OverflowError;
-static PyObject *__pyx_n_s_PG;
-static PyObject *__pyx_n_s_PI;
-static PyObject *__pyx_n_s_PL;
-static PyObject *__pyx_n_s_PN;
-static PyObject *__pyx_n_s_PP;
-static PyObject *__pyx_n_s_PU;
-static PyObject *__pyx_n_s_PileupColumn;
-static PyObject *__pyx_n_s_PileupColumn___str;
-static PyObject *__pyx_n_s_PileupProxy;
-static PyObject *__pyx_kp_s_PileupProxy_accessed_after_itera;
-static PyObject *__pyx_n_s_PileupRead;
-static PyObject *__pyx_kp_s_Position_of_mate_pair;
-static PyObject *__pyx_kp_s_Quality_scores;
-static PyObject *__pyx_kp_s_Query_name;
-static PyObject *__pyx_kp_s_Query_sequence;
-static PyObject *__pyx_n_s_RG;
-static PyObject *__pyx_n_s_S;
-static PyObject *__pyx_n_s_SM;
-static PyObject *__pyx_n_s_SN;
-static PyObject *__pyx_n_s_SO;
-static PyObject *__pyx_n_s_SP;
-static PyObject *__pyx_n_s_SQ;
-static PyObject *__pyx_kp_s_SQ_SN_s_LN_s;
 static PyObject *__pyx_n_s_Samfile;
-static PyObject *__pyx_kp_s_Samfile_mapped_only_available_in;
-static PyObject *__pyx_n_s_StopIteration;
-static PyObject *__pyx_kp_s_This_class_cannot_be_instantiate;
-static PyObject *__pyx_n_s_TypeError;
-static PyObject *__pyx_n_s_UR;
-static PyObject *__pyx_kp_s_Unsupported_value_type_in_set_op;
-static PyObject *__pyx_n_s_VALID_HEADERS;
-static PyObject *__pyx_n_s_VALID_HEADER_FIELDS;
-static PyObject *__pyx_n_s_VALID_HEADER_ORDER;
-static PyObject *__pyx_n_s_VALID_HEADER_TYPES;
-static PyObject *__pyx_n_s_VN;
-static PyObject *__pyx_n_s_ValueError;
-static PyObject *__pyx_n_s_Z;
-static PyObject *__pyx_n_s_Zidf;
-static PyObject *__pyx_kp_s__14;
-static PyObject *__pyx_kp_s__3;
-static PyObject *__pyx_kp_s__4;
-static PyObject *__pyx_kp_s__41;
-static PyObject *__pyx_kp_s__47;
-static PyObject *__pyx_kp_b__6;
-static PyObject *__pyx_kp_s__76;
-static PyObject *__pyx_kp_s__79;
-static PyObject *__pyx_kp_s__80;
-static PyObject *__pyx_kp_s__9;
-static PyObject *__pyx_n_s_add_sq_text;
-static PyObject *__pyx_n_s_alignment;
 static PyObject *__pyx_n_s_all;
-static PyObject *__pyx_n_s_all_2;
-static PyObject *__pyx_n_s_append;
-static PyObject *__pyx_n_s_args;
-static PyObject *__pyx_n_s_ascii;
-static PyObject *__pyx_n_s_b;
-static PyObject *__pyx_kp_b_bai;
-static PyObject *__pyx_n_s_beg;
-static PyObject *__pyx_n_s_bin;
-static PyObject *__pyx_n_s_bqual;
-static PyObject *__pyx_n_s_buildLine;
-static PyObject *__pyx_n_s_c;
-static PyObject *__pyx_kp_s_c_2;
-static PyObject *__pyx_n_s_calcsize;
-static PyObject *__pyx_n_s_callback;
-static PyObject *__pyx_kp_s_callback_not_implemented_yet;
-static PyObject *__pyx_kp_s_can_not_iterate_over_samfile_wit;
-static PyObject *__pyx_kp_s_can_only_IndexReads_on_bam_files;
-static PyObject *__pyx_kp_s_can_t_guess_type_or_invalid_type;
-static PyObject *__pyx_n_s_checkIndex;
-static PyObject *__pyx_n_s_check_header;
-static PyObject *__pyx_n_s_check_sq;
-static PyObject *__pyx_n_s_chr;
-static PyObject *__pyx_n_s_cigar;
-static PyObject *__pyx_n_s_close;
 static PyObject *__pyx_n_s_collections;
-static PyObject *__pyx_n_s_compile;
-static PyObject *__pyx_n_s_consensus_quality;
-static PyObject *__pyx_kp_s_could_not_open_file_mode_s_is_it;
-static PyObject *__pyx_kp_s_could_not_open_file_s;
-static PyObject *__pyx_n_s_coverage;
-static PyObject *__pyx_n_s_create_string_buffer;
 static PyObject *__pyx_n_s_ctypes;
-static PyObject *__pyx_n_s_d;
-static PyObject *__pyx_kp_s_d_MIDNSHP_X;
-static PyObject *__pyx_n_s_decode;
-static PyObject *__pyx_n_s_defaultdict;
-static PyObject *__pyx_n_s_doc;
-static PyObject *__pyx_kp_s_either_supply_options_template_h;
-static PyObject *__pyx_n_s_encode;
-static PyObject *__pyx_n_s_end;
-static PyObject *__pyx_kp_s_end_out_of_range_i;
-static PyObject *__pyx_n_s_enumerate;
-static PyObject *__pyx_kp_s_error_during_iteration;
-static PyObject *__pyx_kp_s_error_while_opening_index_s;
-static PyObject *__pyx_n_s_exc_type;
-static PyObject *__pyx_n_s_exc_value;
-static PyObject *__pyx_n_s_exists;
-static PyObject *__pyx_n_s_extend;
-static PyObject *__pyx_n_s_f;
-static PyObject *__pyx_n_s_fastafile;
-static PyObject *__pyx_n_s_fetch;
-static PyObject *__pyx_kp_s_fetch_called_for_htsfile_without;
-static PyObject *__pyx_kp_s_fetch_called_on_bamfile_without;
-static PyObject *__pyx_kp_s_fetching_by_region_is_not_availa;
-static PyObject *__pyx_n_s_fields;
-static PyObject *__pyx_kp_s_file_does_not_have_valid_header;
-static PyObject *__pyx_kp_s_file_does_not_have_valid_header_2;
-static PyObject *__pyx_kp_s_file_header_is_empty_mode_s_is_i;
-static PyObject *__pyx_kp_s_file_s_not_found;
-static PyObject *__pyx_n_s_filename;
-static PyObject *__pyx_n_s_findall;
-static PyObject *__pyx_n_s_flag;
-static PyObject *__pyx_n_s_fmts;
-static PyObject *__pyx_kp_b_ftp;
-static PyObject *__pyx_n_s_genotype;
-static PyObject *__pyx_n_s_get;
-static PyObject *__pyx_n_s_getattribute;
-static PyObject *__pyx_n_s_getdefaultencoding;
-static PyObject *__pyx_n_s_getfilesystemencoding;
-static PyObject *__pyx_n_s_gettid;
-static PyObject *__pyx_n_s_h;
-static PyObject *__pyx_n_s_hasIndex;
-static PyObject *__pyx_n_s_header;
-static PyObject *__pyx_kp_s_header_line_with_invalid_type_s;
-static PyObject *__pyx_kp_s_header_line_without_s;
-static PyObject *__pyx_kp_s_home_andreas_devel_pysam_pysam;
-static PyObject *__pyx_kp_b_http;
-static PyObject *__pyx_n_s_i;
-static PyObject *__pyx_kp_s_i_c;
 static PyObject *__pyx_n_s_import;
-static PyObject *__pyx_kp_s_incomplete_sequence_information;
-static PyObject *__pyx_n_s_indel;
-static PyObject *__pyx_n_s_init;
-static PyObject *__pyx_kp_s_integer_i_out_of_range_of_BAM_SA;
-static PyObject *__pyx_kp_s_invalid_coordinates_start_i_end;
-static PyObject *__pyx_kp_s_invalid_file_opening_mode_s;
-static PyObject *__pyx_kp_s_invalid_reference_s;
-static PyObject *__pyx_kp_s_invalid_type_for_record_s_s_expe;
-static PyObject *__pyx_n_s_isOpen;
-static PyObject *__pyx_n_s_is_del;
-static PyObject *__pyx_n_s_is_head;
-static PyObject *__pyx_n_s_is_tail;
-static PyObject *__pyx_n_s_isize;
-static PyObject *__pyx_n_s_islower;
-static PyObject *__pyx_n_s_isupper;
-static PyObject *__pyx_n_s_items;
 static PyObject *__pyx_n_s_itertools;
-static PyObject *__pyx_n_s_join;
-static PyObject *__pyx_n_s_l_data;
-static PyObject *__pyx_n_s_l_qname;
-static PyObject *__pyx_n_s_l_qseq;
-static PyObject *__pyx_n_s_lengths;
-static PyObject *__pyx_n_s_level;
-static PyObject *__pyx_n_s_m_data;
 static PyObject *__pyx_n_s_main;
-static PyObject *__pyx_kp_s_malformatted_header_no_in_field;
-static PyObject *__pyx_n_s_map;
-static PyObject *__pyx_kp_s_mapping_information_not_recorded;
-static PyObject *__pyx_n_s_mapping_quality;
-static PyObject *__pyx_n_s_mapq;
-static PyObject *__pyx_kp_s_mate_not_found;
-static PyObject *__pyx_kp_s_mate_s_is_unmapped;
-static PyObject *__pyx_n_s_max;
-static PyObject *__pyx_n_s_max_depth;
-static PyObject *__pyx_n_s_metaclass;
-static PyObject *__pyx_n_s_min;
-static PyObject *__pyx_n_s_mode;
-static PyObject *__pyx_n_s_module;
-static PyObject *__pyx_n_s_mpos;
-static PyObject *__pyx_n_s_mrnm;
-static PyObject *__pyx_n_s_mtid;
-static PyObject *__pyx_kp_s_multiple_s_lines_are_not_permitt;
-static PyObject *__pyx_n_s_n;
-static PyObject *__pyx_n_s_n_cigar;
-static PyObject *__pyx_n_s_nextiter;
-static PyObject *__pyx_kp_s_no_index_available_for_fetch;
-static PyObject *__pyx_kp_s_no_index_available_for_iteration;
-static PyObject *__pyx_kp_s_no_index_available_for_pileup;
-static PyObject *__pyx_n_s_nreferences;
-static PyObject *__pyx_n_s_object;
-static PyObject *__pyx_n_s_offset;
-static PyObject *__pyx_n_s_open;
-static PyObject *__pyx_n_s_ord;
 static PyObject *__pyx_n_s_os;
-static PyObject *__pyx_n_s_pack_into;
-static PyObject *__pyx_n_s_parseRegion;
-static PyObject *__pyx_n_s_path;
-static PyObject *__pyx_kp_s_pileup_of_samfiles_not_implement;
-static PyObject *__pyx_n_s_pileups;
 static PyObject *__pyx_n_s_platform;
-static PyObject *__pyx_n_s_port;
-static PyObject *__pyx_n_s_pos;
-static PyObject *__pyx_n_s_positions;
-static PyObject *__pyx_n_s_prepare;
-static PyObject *__pyx_n_s_pysam_csamfile;
-static PyObject *__pyx_n_s_pytag;
 static PyObject *__pyx_n_s_pyx_vtable;
-static PyObject *__pyx_n_s_qname;
-static PyObject *__pyx_n_s_qpos;
-static PyObject *__pyx_n_s_qseq;
-static PyObject *__pyx_n_s_qual;
-static PyObject *__pyx_kp_s_quality_and_sequence_mismatch_i;
-static PyObject *__pyx_n_s_qualname;
-static PyObject *__pyx_n_s_r;
-static PyObject *__pyx_n_s_rU;
-static PyObject *__pyx_n_s_raw;
-static PyObject *__pyx_n_s_rb;
 static PyObject *__pyx_n_s_re;
-static PyObject *__pyx_kp_s_read_s_is_unpaired;
-static PyObject *__pyx_kp_s_read_s_not_found;
-static PyObject *__pyx_n_s_record;
-static PyObject *__pyx_n_s_reference;
-static PyObject *__pyx_n_s_reference_base;
-static PyObject *__pyx_kp_s_reference_sequence_for_s_tid_i_n;
-static PyObject *__pyx_n_s_referencelengths;
-static PyObject *__pyx_n_s_referencenames;
-static PyObject *__pyx_n_s_references;
-static PyObject *__pyx_n_s_region;
-static PyObject *__pyx_n_s_reopen;
-static PyObject *__pyx_n_s_replace;
-static PyObject *__pyx_n_s_rlen;
-static PyObject *__pyx_n_s_rname;
-static PyObject *__pyx_kp_s_s;
-static PyObject *__pyx_n_s_s_2;
-static PyObject *__pyx_kp_s_s_s;
-static PyObject *__pyx_n_s_samfile;
-static PyObject *__pyx_n_s_samtools;
-static PyObject *__pyx_n_s_seek;
-static PyObject *__pyx_kp_s_seek_no_available_in_streams;
-static PyObject *__pyx_kp_s_seek_only_available_in_bam_files;
-static PyObject *__pyx_n_s_self;
-static PyObject *__pyx_n_s_send;
-static PyObject *__pyx_n_s_seq;
-static PyObject *__pyx_n_s_setTag;
-static PyObject *__pyx_n_s_snp_quality;
-static PyObject *__pyx_n_s_sorted;
-static PyObject *__pyx_n_s_split;
-static PyObject *__pyx_n_s_start;
-static PyObject *__pyx_kp_s_start_out_of_range_i;
-static PyObject *__pyx_n_s_startswith;
-static PyObject *__pyx_n_s_stepper;
-static PyObject *__pyx_n_s_str;
-static PyObject *__pyx_n_s_strip;
 static PyObject *__pyx_n_s_struct;
 static PyObject *__pyx_n_s_sys;
-static PyObject *__pyx_n_s_tag;
-static PyObject *__pyx_kp_s_tag_s_not_present;
-static PyObject *__pyx_n_s_tags;
 static PyObject *__pyx_n_s_tempfile;
-static PyObject *__pyx_n_s_template;
 static PyObject *__pyx_n_s_test;
-static PyObject *__pyx_n_s_text;
-static PyObject *__pyx_n_s_throw;
-static PyObject *__pyx_n_s_tid;
-static PyObject *__pyx_kp_s_tid_i_out_of_range_0_tid_i;
-static PyObject *__pyx_n_s_traceback;
-static PyObject *__pyx_n_s_truncate;
 static PyObject *__pyx_n_s_types;
-static PyObject *__pyx_kp_s_unable_to_open_index_for_s;
-static PyObject *__pyx_kp_s_unequal_names_and_lengths_of_ref;
-static PyObject *__pyx_kp_s_unknown_auxilliary_type_s;
-static PyObject *__pyx_kp_s_unknown_field_code_s_in_record_s;
-static PyObject *__pyx_kp_s_unknown_stepper_option_s_in_Iter;
-static PyObject *__pyx_kp_s_unknown_type_s;
-static PyObject *__pyx_n_s_until_eof;
-static PyObject *__pyx_n_s_value;
-static PyObject *__pyx_n_s_value_type;
-static PyObject *__pyx_n_s_version_info;
-static PyObject *__pyx_n_s_w;
-static PyObject *__pyx_n_s_warn;
 static PyObject *__pyx_n_s_warnings;
-static PyObject *__pyx_n_s_wb;
-static PyObject *__pyx_n_s_wbu;
-static PyObject *__pyx_n_s_wh;
-static PyObject *__pyx_n_s_where;
-static PyObject *__pyx_n_s_write;
-static PyObject *__pyx_n_s_zip;
-static PyObject *__pyx_int_0;
-static PyObject *__pyx_int_1;
-static PyObject *__pyx_int_2;
-static PyObject *__pyx_int_3;
-static PyObject *__pyx_int_255;
-static PyObject *__pyx_int_8000;
-static PyObject *__pyx_int_65535;
-static PyObject *__pyx_int_536870912;
-static PyObject *__pyx_int_4294967295;
-static PyObject *__pyx_int_neg_4;
-static PyObject *__pyx_int_neg_127;
-static PyObject *__pyx_int_neg_32767;
-static PyObject *__pyx_int_neg_2147483648;
-static int __pyx_k__57;
-static PyObject *__pyx_tuple_;
-static PyObject *__pyx_tuple__2;
-static PyObject *__pyx_tuple__5;
-static PyObject *__pyx_tuple__7;
-static PyObject *__pyx_tuple__8;
-static PyObject *__pyx_slice__10;
-static PyObject *__pyx_slice__43;
-static PyObject *__pyx_slice__45;
-static PyObject *__pyx_slice__46;
-static PyObject *__pyx_slice__61;
-static PyObject *__pyx_slice__65;
-static PyObject *__pyx_slice__70;
-static PyObject *__pyx_slice__74;
-static PyObject *__pyx_tuple__11;
-static PyObject *__pyx_tuple__12;
-static PyObject *__pyx_tuple__13;
-static PyObject *__pyx_tuple__15;
-static PyObject *__pyx_tuple__16;
-static PyObject *__pyx_tuple__17;
-static PyObject *__pyx_tuple__18;
-static PyObject *__pyx_tuple__19;
-static PyObject *__pyx_tuple__20;
-static PyObject *__pyx_tuple__21;
-static PyObject *__pyx_tuple__22;
-static PyObject *__pyx_tuple__23;
-static PyObject *__pyx_tuple__24;
-static PyObject *__pyx_tuple__25;
-static PyObject *__pyx_tuple__26;
-static PyObject *__pyx_tuple__27;
-static PyObject *__pyx_tuple__28;
-static PyObject *__pyx_tuple__29;
-static PyObject *__pyx_tuple__30;
-static PyObject *__pyx_tuple__31;
-static PyObject *__pyx_tuple__32;
-static PyObject *__pyx_tuple__33;
-static PyObject *__pyx_tuple__34;
-static PyObject *__pyx_tuple__35;
-static PyObject *__pyx_tuple__36;
-static PyObject *__pyx_tuple__37;
-static PyObject *__pyx_tuple__38;
-static PyObject *__pyx_tuple__39;
-static PyObject *__pyx_tuple__40;
-static PyObject *__pyx_tuple__42;
-static PyObject *__pyx_tuple__44;
-static PyObject *__pyx_tuple__48;
-static PyObject *__pyx_tuple__49;
-static PyObject *__pyx_tuple__50;
-static PyObject *__pyx_tuple__51;
-static PyObject *__pyx_tuple__52;
-static PyObject *__pyx_tuple__53;
-static PyObject *__pyx_tuple__54;
-static PyObject *__pyx_tuple__55;
-static PyObject *__pyx_tuple__56;
-static PyObject *__pyx_tuple__58;
-static PyObject *__pyx_tuple__59;
-static PyObject *__pyx_tuple__60;
-static PyObject *__pyx_tuple__62;
-static PyObject *__pyx_tuple__63;
-static PyObject *__pyx_tuple__64;
-static PyObject *__pyx_tuple__66;
-static PyObject *__pyx_tuple__67;
-static PyObject *__pyx_tuple__68;
-static PyObject *__pyx_tuple__69;
-static PyObject *__pyx_tuple__71;
-static PyObject *__pyx_tuple__72;
-static PyObject *__pyx_tuple__73;
-static PyObject *__pyx_tuple__75;
-static PyObject *__pyx_tuple__77;
-static PyObject *__pyx_tuple__78;
-static PyObject *__pyx_tuple__81;
-static PyObject *__pyx_tuple__82;
-static PyObject *__pyx_tuple__83;
-static PyObject *__pyx_tuple__84;
-static PyObject *__pyx_tuple__85;
-static PyObject *__pyx_tuple__87;
-static PyObject *__pyx_tuple__88;
-static PyObject *__pyx_tuple__89;
-static PyObject *__pyx_tuple__90;
-static PyObject *__pyx_tuple__91;
-static PyObject *__pyx_codeobj__86;
-static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
+static struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead __pyx_vtable_5pysam_8csamfile_AlignedRead;
 
-/* "pysam/csamfile.pyx":105
- * cdef char* CODE2CIGAR= "MIDNSHP=X"
- * if IS_PYTHON3:
- *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
- * else:
- *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
- */
+static PyObject *__pyx_tp_new_5pysam_8csamfile_AlignedRead(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_8csamfile_AlignedRead *p;
+  PyObject *o = __pyx_ptype_5pysam_14calignmentfile_AlignedSegment->tp_new(t, a, k);
+  if (unlikely(!o)) return 0;
+  p = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)o);
+  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment*)__pyx_vtabptr_5pysam_8csamfile_AlignedRead;
+  return o;
+}
 
-static PyObject *__pyx_pf_5pysam_8csamfile_genexpr(CYTHON_UNUSED PyObject *__pyx_self) {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *__pyx_cur_scope;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("genexpr", 0);
-  __pyx_cur_scope = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct__genexpr(__pyx_ptype_5pysam_8csamfile___pyx_scope_struct__genexpr, __pyx_empty_tuple, NULL);
-  if (unlikely(!__pyx_cur_scope)) {
-    __Pyx_RefNannyFinishContext();
-    return NULL;
-  }
-  __Pyx_GOTREF(__pyx_cur_scope);
-  __Pyx_TraceCall("genexpr", __pyx_f[0], 105);
-  {
-    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_8csamfile_2generator, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_cur_scope);
-    __Pyx_RefNannyFinishContext();
-    return (PyObject *) gen;
+static void __pyx_tp_dealloc_5pysam_8csamfile_AlignedRead(PyObject *o) {
+  #if PY_VERSION_HEX >= 0x030400a1
+  if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
+    if (PyObject_CallFinalizerFromDealloc(o)) return;
   }
+  #endif
+  if (likely(__pyx_ptype_5pysam_14calignmentfile_AlignedSegment)) __pyx_ptype_5pysam_14calignmentfile_AlignedSegment->tp_dealloc(o); else __Pyx_call_next_tp_dealloc(o, __pyx_tp_dealloc_5pysam_8csamfile_AlignedRead);
+}
 
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_DECREF(((PyObject *)__pyx_cur_scope));
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
+static PyTypeObject __pyx_type_5pysam_8csamfile_AlignedRead = {
+  PyVarObject_HEAD_INIT(0, 0)
+  __Pyx_NAMESTR("pysam.csamfile.AlignedRead"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_8csamfile_AlignedRead), /*tp_basicsize*/
+  0, /*tp_itemsize*/
+  __pyx_tp_dealloc_5pysam_8csamfile_AlignedRead, /*tp_dealloc*/
+  0, /*tp_print*/
+  0, /*tp_getattr*/
+  0, /*tp_setattr*/
+  #if PY_MAJOR_VERSION < 3
+  0, /*tp_compare*/
+  #else
+  0, /*reserved*/
+  #endif
+  0, /*tp_repr*/
+  0, /*tp_as_number*/
+  0, /*tp_as_sequence*/
+  0, /*tp_as_mapping*/
+  0, /*tp_hash*/
+  0, /*tp_call*/
+  0, /*tp_str*/
+  0, /*tp_getattro*/
+  0, /*tp_setattro*/
+  0, /*tp_as_buffer*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+  __Pyx_DOCSTR("Deprecated alternative for :class:`~pysam.AlignedSegment`\n\n    Added for backwards compatibility with pysam <= 0.8.0\n    "), /*tp_doc*/
+  0, /*tp_traverse*/
+  0, /*tp_clear*/
+  0, /*tp_richcompare*/
+  0, /*tp_weaklistoffset*/
+  0, /*tp_iter*/
+  0, /*tp_iternext*/
+  0, /*tp_methods*/
+  0, /*tp_members*/
+  0, /*tp_getset*/
+  0, /*tp_base*/
+  0, /*tp_dict*/
+  0, /*tp_descr_get*/
+  0, /*tp_descr_set*/
+  0, /*tp_dictoffset*/
+  0, /*tp_init*/
+  0, /*tp_alloc*/
+  __pyx_tp_new_5pysam_8csamfile_AlignedRead, /*tp_new*/
+  0, /*tp_free*/
+  0, /*tp_is_gc*/
+  0, /*tp_bases*/
+  0, /*tp_mro*/
+  0, /*tp_cache*/
+  0, /*tp_subclasses*/
+  0, /*tp_weaklist*/
+  0, /*tp_del*/
+  #if PY_VERSION_HEX >= 0x02060000
+  0, /*tp_version_tag*/
+  #endif
+  #if PY_VERSION_HEX >= 0x030400a1
+  0, /*tp_finalize*/
+  #endif
+};
+static struct __pyx_vtabstruct_5pysam_8csamfile_Samfile __pyx_vtable_5pysam_8csamfile_Samfile;
+
+static PyObject *__pyx_tp_new_5pysam_8csamfile_Samfile(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_8csamfile_Samfile *p;
+  PyObject *o = __pyx_ptype_5pysam_14calignmentfile_AlignmentFile->tp_new(t, a, k);
+  if (unlikely(!o)) return 0;
+  p = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)o);
+  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile*)__pyx_vtabptr_5pysam_8csamfile_Samfile;
+  return o;
 }
 
-static PyObject *__pyx_gb_5pysam_8csamfile_2generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
-{
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *__pyx_cur_scope = ((struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)__pyx_generator->closure);
-  PyObject *__pyx_r = NULL;
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *(*__pyx_t_4)(PyObject *);
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("None", 0);
-  switch (__pyx_generator->resume_label) {
-    case 0: goto __pyx_L3_first_run;
-    case 1: goto __pyx_L6_resume_from_yield;
-    default: /* CPython raises the right error here */
-    __Pyx_RefNannyFinishContext();
-    return NULL;
-  }
-  __pyx_L3_first_run:;
-  if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_INCREF(__pyx_int_0);
-  __pyx_t_1 = __pyx_int_0;
-  __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_5pysam_8csamfile_CODE2CIGAR); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = Py_TYPE(__pyx_t_3)->tp_iternext;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  for (;;) {
-    {
-      __pyx_t_2 = __pyx_t_4(__pyx_t_3);
-      if (unlikely(!__pyx_t_2)) {
-        PyObject* exc_type = PyErr_Occurred();
-        if (exc_type) {
-          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        break;
-      }
-      __Pyx_GOTREF(__pyx_t_2);
-    }
-    __Pyx_XGOTREF(__pyx_cur_scope->__pyx_v_y);
-    __Pyx_XDECREF_SET(__pyx_cur_scope->__pyx_v_y, __pyx_t_2);
-    __Pyx_GIVEREF(__pyx_t_2);
-    __pyx_t_2 = 0;
-    __Pyx_INCREF(__pyx_t_1);
-    __Pyx_XGOTREF(__pyx_cur_scope->__pyx_v_x);
-    __Pyx_XDECREF_SET(__pyx_cur_scope->__pyx_v_x, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1);
-    __pyx_t_1 = __pyx_t_2;
-    __pyx_t_2 = 0;
-    __pyx_t_2 = PyList_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_INCREF(__pyx_cur_scope->__pyx_v_y);
-    PyList_SET_ITEM(__pyx_t_2, 0, __pyx_cur_scope->__pyx_v_y);
-    __Pyx_GIVEREF(__pyx_cur_scope->__pyx_v_y);
-    __Pyx_INCREF(__pyx_cur_scope->__pyx_v_x);
-    PyList_SET_ITEM(__pyx_t_2, 1, __pyx_cur_scope->__pyx_v_x);
-    __Pyx_GIVEREF(__pyx_cur_scope->__pyx_v_x);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    __Pyx_XGIVEREF(__pyx_t_1);
-    __pyx_cur_scope->__pyx_t_0 = __pyx_t_1;
-    __Pyx_XGIVEREF(__pyx_t_3);
-    __pyx_cur_scope->__pyx_t_1 = __pyx_t_3;
-    __pyx_cur_scope->__pyx_t_2 = __pyx_t_4;
-    __Pyx_XGIVEREF(__pyx_r);
-    __Pyx_RefNannyFinishContext();
-    /* return from generator, yielding value */
-    __pyx_generator->resume_label = 1;
-    return __pyx_r;
-    __pyx_L6_resume_from_yield:;
-    __pyx_t_1 = __pyx_cur_scope->__pyx_t_0;
-    __pyx_cur_scope->__pyx_t_0 = 0;
-    __Pyx_XGOTREF(__pyx_t_1);
-    __pyx_t_3 = __pyx_cur_scope->__pyx_t_1;
-    __pyx_cur_scope->__pyx_t_1 = 0;
-    __Pyx_XGOTREF(__pyx_t_3);
-    __pyx_t_4 = __pyx_cur_scope->__pyx_t_2;
-    if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+static void __pyx_tp_dealloc_5pysam_8csamfile_Samfile(PyObject *o) {
+  #if PY_VERSION_HEX >= 0x030400a1
+  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
+    if (PyObject_CallFinalizerFromDealloc(o)) return;
   }
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* function exit code */
-  PyErr_SetNone(PyExc_StopIteration);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_generator->resume_label = -1;
-  __Pyx_Generator_clear((PyObject*)__pyx_generator);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
+  #endif
+  PyObject_GC_UnTrack(o);
+  PyObject_GC_Track(o);
+  if (likely(__pyx_ptype_5pysam_14calignmentfile_AlignmentFile)) __pyx_ptype_5pysam_14calignmentfile_AlignmentFile->tp_dealloc(o); else __Pyx_call_next_tp_dealloc(o, __pyx_tp_dealloc_5pysam_8csamfile_Samfile);
 }
-static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
 
-/* "pysam/csamfile.pyx":107
- *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
- * else:
- *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
- * CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )
- * 
- */
+static int __pyx_tp_traverse_5pysam_8csamfile_Samfile(PyObject *o, visitproc v, void *a) {
+  int e;
+  e = ((likely(__pyx_ptype_5pysam_14calignmentfile_AlignmentFile)) ? ((__pyx_ptype_5pysam_14calignmentfile_AlignmentFile->tp_traverse) ? __pyx_ptype_5pysam_14calignmentfile_AlignmentFile->tp_traverse(o, v, a) : 0) : __Pyx_call_next_tp_traverse(o, v, a, __pyx_tp_traverse_5pysam_8csamfile_Samfile)); if (e) return e;
+  return 0;
+}
 
-static PyObject *__pyx_pf_5pysam_8csamfile_3genexpr(CYTHON_UNUSED PyObject *__pyx_self) {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *__pyx_cur_scope;
-  PyObject *__pyx_r = NULL;
+static int __pyx_tp_clear_5pysam_8csamfile_Samfile(PyObject *o) {
+  if (likely(__pyx_ptype_5pysam_14calignmentfile_AlignmentFile)) { if (__pyx_ptype_5pysam_14calignmentfile_AlignmentFile->tp_clear) __pyx_ptype_5pysam_14calignmentfile_AlignmentFile->tp_clear(o); } else __Pyx_call_next_tp_clear(o, __pyx_tp_clear_5pysam_8csamfile_Samfile);
+  return 0;
+}
+
+static PyTypeObject __pyx_type_5pysam_8csamfile_Samfile = {
+  PyVarObject_HEAD_INIT(0, 0)
+  __Pyx_NAMESTR("pysam.csamfile.Samfile"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_8csamfile_Samfile), /*tp_basicsize*/
+  0, /*tp_itemsize*/
+  __pyx_tp_dealloc_5pysam_8csamfile_Samfile, /*tp_dealloc*/
+  0, /*tp_print*/
+  0, /*tp_getattr*/
+  0, /*tp_setattr*/
+  #if PY_MAJOR_VERSION < 3
+  0, /*tp_compare*/
+  #else
+  0, /*reserved*/
+  #endif
+  0, /*tp_repr*/
+  0, /*tp_as_number*/
+  0, /*tp_as_sequence*/
+  0, /*tp_as_mapping*/
+  0, /*tp_hash*/
+  0, /*tp_call*/
+  0, /*tp_str*/
+  0, /*tp_getattro*/
+  0, /*tp_setattro*/
+  0, /*tp_as_buffer*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  __Pyx_DOCSTR("Deprecated alternative for :class:`~pysam.AlignmentFile`\n\n    Added for backwards compatibility with pysam <= 0.8.0\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_8csamfile_Samfile, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_8csamfile_Samfile, /*tp_clear*/
+  0, /*tp_richcompare*/
+  0, /*tp_weaklistoffset*/
+  0, /*tp_iter*/
+  0, /*tp_iternext*/
+  0, /*tp_methods*/
+  0, /*tp_members*/
+  0, /*tp_getset*/
+  0, /*tp_base*/
+  0, /*tp_dict*/
+  0, /*tp_descr_get*/
+  0, /*tp_descr_set*/
+  0, /*tp_dictoffset*/
+  0, /*tp_init*/
+  0, /*tp_alloc*/
+  __pyx_tp_new_5pysam_8csamfile_Samfile, /*tp_new*/
+  0, /*tp_free*/
+  0, /*tp_is_gc*/
+  0, /*tp_bases*/
+  0, /*tp_mro*/
+  0, /*tp_cache*/
+  0, /*tp_subclasses*/
+  0, /*tp_weaklist*/
+  0, /*tp_del*/
+  #if PY_VERSION_HEX >= 0x02060000
+  0, /*tp_version_tag*/
+  #endif
+  #if PY_VERSION_HEX >= 0x030400a1
+  0, /*tp_finalize*/
+  #endif
+};
+
+static PyMethodDef __pyx_methods[] = {
+  {0, 0, 0, 0}
+};
+
+#if PY_MAJOR_VERSION >= 3
+static struct PyModuleDef __pyx_moduledef = {
+  #if PY_VERSION_HEX < 0x03020000
+    { PyObject_HEAD_INIT(NULL) NULL, 0, NULL },
+  #else
+    PyModuleDef_HEAD_INIT,
+  #endif
+    __Pyx_NAMESTR("csamfile"),
+    0, /* m_doc */
+    -1, /* m_size */
+    __pyx_methods /* m_methods */,
+    NULL, /* m_reload */
+    NULL, /* m_traverse */
+    NULL, /* m_clear */
+    NULL /* m_free */
+};
+#endif
+
+static __Pyx_StringTabEntry __pyx_string_tab[] = {
+  {&__pyx_n_s_AlignedRead, __pyx_k_AlignedRead, sizeof(__pyx_k_AlignedRead), 0, 0, 1, 1},
+  {&__pyx_n_s_Samfile, __pyx_k_Samfile, sizeof(__pyx_k_Samfile), 0, 0, 1, 1},
+  {&__pyx_n_s_all, __pyx_k_all, sizeof(__pyx_k_all), 0, 0, 1, 1},
+  {&__pyx_n_s_collections, __pyx_k_collections, sizeof(__pyx_k_collections), 0, 0, 1, 1},
+  {&__pyx_n_s_ctypes, __pyx_k_ctypes, sizeof(__pyx_k_ctypes), 0, 0, 1, 1},
+  {&__pyx_n_s_import, __pyx_k_import, sizeof(__pyx_k_import), 0, 0, 1, 1},
+  {&__pyx_n_s_itertools, __pyx_k_itertools, sizeof(__pyx_k_itertools), 0, 0, 1, 1},
+  {&__pyx_n_s_main, __pyx_k_main, sizeof(__pyx_k_main), 0, 0, 1, 1},
+  {&__pyx_n_s_os, __pyx_k_os, sizeof(__pyx_k_os), 0, 0, 1, 1},
+  {&__pyx_n_s_platform, __pyx_k_platform, sizeof(__pyx_k_platform), 0, 0, 1, 1},
+  {&__pyx_n_s_pyx_vtable, __pyx_k_pyx_vtable, sizeof(__pyx_k_pyx_vtable), 0, 0, 1, 1},
+  {&__pyx_n_s_re, __pyx_k_re, sizeof(__pyx_k_re), 0, 0, 1, 1},
+  {&__pyx_n_s_struct, __pyx_k_struct, sizeof(__pyx_k_struct), 0, 0, 1, 1},
+  {&__pyx_n_s_sys, __pyx_k_sys, sizeof(__pyx_k_sys), 0, 0, 1, 1},
+  {&__pyx_n_s_tempfile, __pyx_k_tempfile, sizeof(__pyx_k_tempfile), 0, 0, 1, 1},
+  {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1},
+  {&__pyx_n_s_types, __pyx_k_types, sizeof(__pyx_k_types), 0, 0, 1, 1},
+  {&__pyx_n_s_warnings, __pyx_k_warnings, sizeof(__pyx_k_warnings), 0, 0, 1, 1},
+  {0, 0, 0, 0, 0, 0, 0}
+};
+static int __Pyx_InitCachedBuiltins(void) {
+  return 0;
+}
+
+static int __Pyx_InitCachedConstants(void) {
   __Pyx_RefNannyDeclarations
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("genexpr", 0);
-  __pyx_cur_scope = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct_1_genexpr(__pyx_ptype_5pysam_8csamfile___pyx_scope_struct_1_genexpr, __pyx_empty_tuple, NULL);
-  if (unlikely(!__pyx_cur_scope)) {
-    __Pyx_RefNannyFinishContext();
-    return NULL;
-  }
-  __Pyx_GOTREF(__pyx_cur_scope);
-  __Pyx_TraceCall("genexpr", __pyx_f[0], 107);
-  {
-    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_8csamfile_5generator1, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_cur_scope);
-    __Pyx_RefNannyFinishContext();
-    return (PyObject *) gen;
-  }
+  __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0);
+  __Pyx_RefNannyFinishContext();
+  return 0;
+}
 
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
+static int __Pyx_InitGlobals(void) {
+  if (__Pyx_InitStrings(__pyx_string_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  return 0;
   __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_DECREF(((PyObject *)__pyx_cur_scope));
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
+  return -1;
 }
 
-static PyObject *__pyx_gb_5pysam_8csamfile_5generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
+#if PY_MAJOR_VERSION < 3
+PyMODINIT_FUNC initcsamfile(void); /*proto*/
+PyMODINIT_FUNC initcsamfile(void)
+#else
+PyMODINIT_FUNC PyInit_csamfile(void); /*proto*/
+PyMODINIT_FUNC PyInit_csamfile(void)
+#endif
 {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *__pyx_cur_scope = ((struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)__pyx_generator->closure);
-  PyObject *__pyx_r = NULL;
   PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *(*__pyx_t_4)(PyObject *);
-  PyObject *__pyx_t_5 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("None", 0);
-  switch (__pyx_generator->resume_label) {
-    case 0: goto __pyx_L3_first_run;
-    case 1: goto __pyx_L6_resume_from_yield;
-    default: /* CPython raises the right error here */
-    __Pyx_RefNannyFinishContext();
-    return NULL;
+  #if CYTHON_REFNANNY
+  __Pyx_RefNanny = __Pyx_RefNannyImportAPI("refnanny");
+  if (!__Pyx_RefNanny) {
+      PyErr_Clear();
+      __Pyx_RefNanny = __Pyx_RefNannyImportAPI("Cython.Runtime.refnanny");
+      if (!__Pyx_RefNanny)
+          Py_FatalError("failed to import 'refnanny' module");
   }
-  __pyx_L3_first_run:;
-  if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_INCREF(__pyx_int_0);
-  __pyx_t_1 = __pyx_int_0;
-  __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_5pysam_8csamfile_CODE2CIGAR); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = Py_TYPE(__pyx_t_3)->tp_iternext;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  for (;;) {
-    {
-      __pyx_t_2 = __pyx_t_4(__pyx_t_3);
-      if (unlikely(!__pyx_t_2)) {
-        PyObject* exc_type = PyErr_Occurred();
-        if (exc_type) {
-          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        break;
-      }
-      __Pyx_GOTREF(__pyx_t_2);
+  #endif
+  __Pyx_RefNannySetupContext("PyMODINIT_FUNC PyInit_csamfile(void)", 0);
+  if ( __Pyx_check_binary_version() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __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_error;}
+  __pyx_empty_bytes = PyBytes_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_bytes)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  #ifdef __Pyx_CyFunction_USED
+  if (__Pyx_CyFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  #endif
+  #ifdef __Pyx_FusedFunction_USED
+  if (__pyx_FusedFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  #endif
+  #ifdef __Pyx_Generator_USED
+  if (__pyx_Generator_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  #endif
+  /*--- Library function declarations ---*/
+  /*--- Threads initialization code ---*/
+  #if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS
+  #ifdef WITH_THREAD /* Python build with threading support? */
+  PyEval_InitThreads();
+  #endif
+  #endif
+  /*--- Module creation code ---*/
+  #if PY_MAJOR_VERSION < 3
+  __pyx_m = Py_InitModule4(__Pyx_NAMESTR("csamfile"), __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m);
+  #else
+  __pyx_m = PyModule_Create(&__pyx_moduledef);
+  #endif
+  if (unlikely(!__pyx_m)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  Py_INCREF(__pyx_d);
+  __pyx_b = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME)); if (unlikely(!__pyx_b)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  #if CYTHON_COMPILING_IN_PYPY
+  Py_INCREF(__pyx_b);
+  #endif
+  if (__Pyx_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  /*--- Initialize various global constants etc. ---*/
+  if (unlikely(__Pyx_InitGlobals() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT)
+  if (__Pyx_init_sys_getdefaultencoding_params() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  #endif
+  if (__pyx_module_is_main_pysam__csamfile) {
+    if (__Pyx_SetAttrString(__pyx_m, "__name__", __pyx_n_s_main) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  }
+  #if PY_MAJOR_VERSION >= 3
+  {
+    PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!PyDict_GetItemString(modules, "pysam.csamfile")) {
+      if (unlikely(PyDict_SetItemString(modules, "pysam.csamfile", __pyx_m) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
-    __Pyx_XGOTREF(__pyx_cur_scope->__pyx_v_y);
-    __Pyx_XDECREF_SET(__pyx_cur_scope->__pyx_v_y, __pyx_t_2);
-    __Pyx_GIVEREF(__pyx_t_2);
-    __pyx_t_2 = 0;
-    __Pyx_INCREF(__pyx_t_1);
-    __Pyx_XGOTREF(__pyx_cur_scope->__pyx_v_x);
-    __Pyx_XDECREF_SET(__pyx_cur_scope->__pyx_v_x, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1);
-    __pyx_t_1 = __pyx_t_2;
-    __pyx_t_2 = 0;
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_INCREF(__pyx_cur_scope->__pyx_v_y);
-    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_cur_scope->__pyx_v_y);
-    __Pyx_GIVEREF(__pyx_cur_scope->__pyx_v_y);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = PyList_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    PyList_SET_ITEM(__pyx_t_2, 0, __pyx_t_5);
-    __Pyx_GIVEREF(__pyx_t_5);
-    __Pyx_INCREF(__pyx_cur_scope->__pyx_v_x);
-    PyList_SET_ITEM(__pyx_t_2, 1, __pyx_cur_scope->__pyx_v_x);
-    __Pyx_GIVEREF(__pyx_cur_scope->__pyx_v_x);
-    __pyx_t_5 = 0;
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    __Pyx_XGIVEREF(__pyx_t_1);
-    __pyx_cur_scope->__pyx_t_0 = __pyx_t_1;
-    __Pyx_XGIVEREF(__pyx_t_3);
-    __pyx_cur_scope->__pyx_t_1 = __pyx_t_3;
-    __pyx_cur_scope->__pyx_t_2 = __pyx_t_4;
-    __Pyx_XGIVEREF(__pyx_r);
-    __Pyx_RefNannyFinishContext();
-    /* return from generator, yielding value */
-    __pyx_generator->resume_label = 1;
-    return __pyx_r;
-    __pyx_L6_resume_from_yield:;
-    __pyx_t_1 = __pyx_cur_scope->__pyx_t_0;
-    __pyx_cur_scope->__pyx_t_0 = 0;
-    __Pyx_XGOTREF(__pyx_t_1);
-    __pyx_t_3 = __pyx_cur_scope->__pyx_t_1;
-    __pyx_cur_scope->__pyx_t_1 = 0;
-    __Pyx_XGOTREF(__pyx_t_3);
-    __pyx_t_4 = __pyx_cur_scope->__pyx_t_2;
-    if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* function exit code */
-  PyErr_SetNone(PyExc_StopIteration);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("genexpr", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_generator->resume_label = -1;
-  __Pyx_Generator_clear((PyObject*)__pyx_generator);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-}
-
-/* "pysam/csamfile.pyx":28
- * ########################################################################
- * IS_PYTHON3 = PY_MAJOR_VERSION >= 3
- * cdef from_string_and_size(char* s, size_t length):             # <<<<<<<<<<<<<<
- *     if PY_MAJOR_VERSION < 3:
- *         return s[:length]
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile_from_string_and_size(char *__pyx_v_s, size_t __pyx_v_length) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("from_string_and_size", 0);
-  __Pyx_TraceCall("from_string_and_size", __pyx_f[0], 28);
+  #endif
+  /*--- Builtin init code ---*/
+  if (unlikely(__Pyx_InitCachedBuiltins() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  /*--- Constants init code ---*/
+  if (unlikely(__Pyx_InitCachedConstants() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  /*--- Global init code ---*/
+  /*--- Variable export code ---*/
+  /*--- Function export code ---*/
+  /*--- Type init code ---*/
+  __pyx_ptype_5pysam_14calignmentfile_AlignedSegment = __Pyx_ImportType("pysam.calignmentfile", "AlignedSegment", sizeof(struct __pyx_obj_5pysam_14calignmentfile_AlignedSegment), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_AlignedSegment)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_14calignmentfile_AlignedSegment = (struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignedSegment*)__Pyx_GetVtable(__pyx_ptype_5pysam_14calignmentfile_AlignedSegment->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_14calignmentfile_AlignedSegment)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_8csamfile_AlignedRead = &__pyx_vtable_5pysam_8csamfile_AlignedRead;
+  __pyx_vtable_5pysam_8csamfile_AlignedRead.__pyx_base = *__pyx_vtabptr_5pysam_14calignmentfile_AlignedSegment;
+  __pyx_type_5pysam_8csamfile_AlignedRead.tp_base = __pyx_ptype_5pysam_14calignmentfile_AlignedSegment;
+  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_8csamfile_AlignedRead.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_AlignedRead.tp_dict, __pyx_vtabptr_5pysam_8csamfile_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "AlignedRead", (PyObject *)&__pyx_type_5pysam_8csamfile_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_8csamfile_AlignedRead = &__pyx_type_5pysam_8csamfile_AlignedRead;
+  __pyx_ptype_5pysam_14calignmentfile_AlignmentFile = __Pyx_ImportType("pysam.calignmentfile", "AlignmentFile", sizeof(struct __pyx_obj_5pysam_14calignmentfile_AlignmentFile), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_AlignmentFile)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_14calignmentfile_AlignmentFile = (struct __pyx_vtabstruct_5pysam_14calignmentfile_AlignmentFile*)__Pyx_GetVtable(__pyx_ptype_5pysam_14calignmentfile_AlignmentFile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_14calignmentfile_AlignmentFile)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_8csamfile_Samfile = &__pyx_vtable_5pysam_8csamfile_Samfile;
+  __pyx_vtable_5pysam_8csamfile_Samfile.__pyx_base = *__pyx_vtabptr_5pysam_14calignmentfile_AlignmentFile;
+  __pyx_type_5pysam_8csamfile_Samfile.tp_base = __pyx_ptype_5pysam_14calignmentfile_AlignmentFile;
+  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_8csamfile_Samfile.tp_print = 0;
+  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_Samfile.tp_dict, __pyx_vtabptr_5pysam_8csamfile_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "Samfile", (PyObject *)&__pyx_type_5pysam_8csamfile_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_8csamfile_Samfile = &__pyx_type_5pysam_8csamfile_Samfile;
+  /*--- Type import code ---*/
+  __pyx_ptype_5pysam_6cfaidx_FastaFile = __Pyx_ImportType("pysam.cfaidx", "FastaFile", sizeof(struct __pyx_obj_5pysam_6cfaidx_FastaFile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_FastaFile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6cfaidx_FastaFile = (struct __pyx_vtabstruct_5pysam_6cfaidx_FastaFile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_FastaFile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_FastaFile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_FastqProxy = __Pyx_ImportType("pysam.cfaidx", "FastqProxy", sizeof(struct __pyx_obj_5pysam_6cfaidx_FastqProxy), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_FastqProxy)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_FastqFile = __Pyx_ImportType("pysam.cfaidx", "FastqFile", sizeof(struct __pyx_obj_5pysam_6cfaidx_FastqFile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_FastqFile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6cfaidx_FastqFile = (struct __pyx_vtabstruct_5pysam_6cfaidx_FastqFile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_FastqFile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_FastqFile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_Fastafile = __Pyx_ImportType("pysam.cfaidx", "Fastafile", sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastafile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_Fastafile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 35; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6cfaidx_Fastafile = (struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_Fastafile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_Fastafile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 35; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6cfaidx_Fastqfile = __Pyx_ImportType("pysam.cfaidx", "Fastqfile", sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastqfile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_Fastqfile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6cfaidx_Fastqfile = (struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_Fastqfile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_Fastqfile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_PileupColumn = __Pyx_ImportType("pysam.calignmentfile", "PileupColumn", sizeof(struct __pyx_obj_5pysam_14calignmentfile_PileupColumn), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_PileupColumn)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_PileupRead = __Pyx_ImportType("pysam.calignmentfile", "PileupRead", sizeof(struct __pyx_obj_5pysam_14calignmentfile_PileupRead), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_PileupRead)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRow = __Pyx_ImportType("pysam.calignmentfile", "IteratorRow", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRow), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IteratorRow)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion = __Pyx_ImportType("pysam.calignmentfile", "IteratorRowRegion", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowRegion), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 134; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowRegion = (struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowRegion*)__Pyx_GetVtable(__pyx_ptype_5pysam_14calignmentfile_IteratorRowRegion->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowRegion)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 134; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowHead = __Pyx_ImportType("pysam.calignmentfile", "IteratorRowHead", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowHead), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IteratorRowHead)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowHead = (struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowHead*)__Pyx_GetVtable(__pyx_ptype_5pysam_14calignmentfile_IteratorRowHead->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowHead)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowAll = __Pyx_ImportType("pysam.calignmentfile", "IteratorRowAll", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAll), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IteratorRowAll)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 145; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowAll = (struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowAll*)__Pyx_GetVtable(__pyx_ptype_5pysam_14calignmentfile_IteratorRowAll->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowAll)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 145; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowAllRefs = __Pyx_ImportType("pysam.calignmentfile", "IteratorRowAllRefs", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowAllRefs), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IteratorRowAllRefs)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorRowSelection = __Pyx_ImportType("pysam.calignmentfile", "IteratorRowSelection", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorRowSelection), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IteratorRowSelection)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorRowSelection = (struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorRowSelection*)__Pyx_GetVtable(__pyx_ptype_5pysam_14calignmentfile_IteratorRowSelection->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_14calignmentfile_IteratorRowSelection)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorColumn = __Pyx_ImportType("pysam.calignmentfile", "IteratorColumn", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumn), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IteratorColumn)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumn = (struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumn*)__Pyx_GetVtable(__pyx_ptype_5pysam_14calignmentfile_IteratorColumn->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumn)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorColumnRegion = __Pyx_ImportType("pysam.calignmentfile", "IteratorColumnRegion", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnRegion), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IteratorColumnRegion)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnRegion = (struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnRegion*)__Pyx_GetVtable(__pyx_ptype_5pysam_14calignmentfile_IteratorColumnRegion->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnRegion)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IteratorColumnAllRefs = __Pyx_ImportType("pysam.calignmentfile", "IteratorColumnAllRefs", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IteratorColumnAllRefs), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IteratorColumnAllRefs)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 193; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnAllRefs = (struct __pyx_vtabstruct_5pysam_14calignmentfile_IteratorColumnAllRefs*)__Pyx_GetVtable(__pyx_ptype_5pysam_14calignmentfile_IteratorColumnAllRefs->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_14calignmentfile_IteratorColumnAllRefs)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 193; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_14calignmentfile_IndexedReads = __Pyx_ImportType("pysam.calignmentfile", "IndexedReads", sizeof(struct __pyx_obj_5pysam_14calignmentfile_IndexedReads), 1); if (unlikely(!__pyx_ptype_5pysam_14calignmentfile_IndexedReads)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "type", 
+  #if CYTHON_COMPILING_IN_PYPY
+  sizeof(PyTypeObject),
+  #else
+  sizeof(PyHeapTypeObject),
+  #endif
+  0); if (unlikely(!__pyx_ptype_7cpython_4type_type)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_7cpython_4bool_bool = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "bool", sizeof(PyBoolObject), 0); if (unlikely(!__pyx_ptype_7cpython_4bool_bool)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_7cpython_7complex_complex = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "complex", sizeof(PyComplexObject), 0); if (unlikely(!__pyx_ptype_7cpython_7complex_complex)) {__pyx_filename = __pyx_f[5]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  /*--- Variable import code ---*/
+  /*--- Function import code ---*/
+  /*--- Execution code ---*/
 
-  /* "pysam/csamfile.pyx":29
- * IS_PYTHON3 = PY_MAJOR_VERSION >= 3
- * cdef from_string_and_size(char* s, size_t length):
- *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
- *         return s[:length]
- *     else:
+  /* "pysam/csamfile.pyx":4
+ * # cython: profile=True
+ * # adds doc-strings for sphinx
+ * import tempfile             # <<<<<<<<<<<<<<
+ * import os
+ * import sys
  */
-  __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
-  if (__pyx_t_1) {
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_tempfile, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tempfile, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":30
- * cdef from_string_and_size(char* s, size_t length):
- *     if PY_MAJOR_VERSION < 3:
- *         return s[:length]             # <<<<<<<<<<<<<<
- *     else:
- *         return s[:length].decode("ascii")
+  /* "pysam/csamfile.pyx":5
+ * # adds doc-strings for sphinx
+ * import tempfile
+ * import os             # <<<<<<<<<<<<<<
+ * import sys
+ * import types
  */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromStringAndSize(__pyx_v_s + 0, __pyx_v_length - 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_os, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_os, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":32
- *         return s[:length]
- *     else:
- *         return s[:length].decode("ascii")             # <<<<<<<<<<<<<<
- * 
- * # filename encoding (copied from lxml.etree.pyx)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, __pyx_v_length, NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":28
- * ########################################################################
- * IS_PYTHON3 = PY_MAJOR_VERSION >= 3
- * cdef from_string_and_size(char* s, size_t length):             # <<<<<<<<<<<<<<
- *     if PY_MAJOR_VERSION < 3:
- *         return s[:length]
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.from_string_and_size", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":45
- * #_C_FILENAME_ENCODING = <char*>_FILENAME_ENCODING
- * 
- * cdef bytes _encodeFilename(object filename):             # <<<<<<<<<<<<<<
- *     """Make sure a filename is 8-bit encoded (or None)."""
- *     if filename is None:
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile__encodeFilename(PyObject *__pyx_v_filename) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_encodeFilename", 0);
-  __Pyx_TraceCall("_encodeFilename", __pyx_f[0], 45);
-
-  /* "pysam/csamfile.pyx":47
- * cdef bytes _encodeFilename(object filename):
- *     """Make sure a filename is 8-bit encoded (or None)."""
- *     if filename is None:             # <<<<<<<<<<<<<<
- *         return None
- *     elif PyBytes_Check(filename):
- */
-  __pyx_t_1 = (__pyx_v_filename == Py_None);
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":48
- *     """Make sure a filename is 8-bit encoded (or None)."""
- *     if filename is None:
- *         return None             # <<<<<<<<<<<<<<
- *     elif PyBytes_Check(filename):
- *         return filename
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = ((PyObject*)Py_None);
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":49
- *     if filename is None:
- *         return None
- *     elif PyBytes_Check(filename):             # <<<<<<<<<<<<<<
- *         return filename
- *     elif PyUnicode_Check(filename):
- */
-  __pyx_t_2 = (PyBytes_Check(__pyx_v_filename) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":50
- *         return None
- *     elif PyBytes_Check(filename):
- *         return filename             # <<<<<<<<<<<<<<
- *     elif PyUnicode_Check(filename):
- *         return filename.encode(_FILENAME_ENCODING)
- */
-    __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_filename))||((__pyx_v_filename) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_filename)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_INCREF(__pyx_v_filename);
-    __pyx_r = ((PyObject*)__pyx_v_filename);
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":51
- *     elif PyBytes_Check(filename):
- *         return filename
- *     elif PyUnicode_Check(filename):             # <<<<<<<<<<<<<<
- *         return filename.encode(_FILENAME_ENCODING)
- *     else:
- */
-  __pyx_t_2 = (PyUnicode_Check(__pyx_v_filename) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":52
- *         return filename
- *     elif PyUnicode_Check(filename):
- *         return filename.encode(_FILENAME_ENCODING)             # <<<<<<<<<<<<<<
- *     else:
- *         raise TypeError, u"Argument must be string or unicode."
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_INCREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    __Pyx_GIVEREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    if (!(likely(PyBytes_CheckExact(__pyx_t_5))||((__pyx_t_5) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_5)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_r = ((PyObject*)__pyx_t_5);
-    __pyx_t_5 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":54
- *         return filename.encode(_FILENAME_ENCODING)
- *     else:
- *         raise TypeError, u"Argument must be string or unicode."             # <<<<<<<<<<<<<<
- * 
- * cdef bytes _forceBytes(object s):
- */
-    __Pyx_Raise(__pyx_builtin_TypeError, __pyx_kp_u_Argument_must_be_string_or_unico, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":45
- * #_C_FILENAME_ENCODING = <char*>_FILENAME_ENCODING
- * 
- * cdef bytes _encodeFilename(object filename):             # <<<<<<<<<<<<<<
- *     """Make sure a filename is 8-bit encoded (or None)."""
- *     if filename is None:
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile._encodeFilename", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":56
- *         raise TypeError, u"Argument must be string or unicode."
- * 
- * cdef bytes _forceBytes(object s):             # <<<<<<<<<<<<<<
- *     u"""convert string or unicode object to bytes, assuming ascii encoding.
- *     """
+  /* "pysam/csamfile.pyx":6
+ * import tempfile
+ * import os
+ * import sys             # <<<<<<<<<<<<<<
+ * import types
+ * import itertools
  */
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_sys, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_sys, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-static PyObject *__pyx_f_5pysam_8csamfile__forceBytes(PyObject *__pyx_v_s) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_forceBytes", 0);
-  __Pyx_TraceCall("_forceBytes", __pyx_f[0], 56);
-
-  /* "pysam/csamfile.pyx":59
- *     u"""convert string or unicode object to bytes, assuming ascii encoding.
- *     """
- *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
- *         return s
- *     elif s is None:
+  /* "pysam/csamfile.pyx":7
+ * import os
+ * import sys
+ * import types             # <<<<<<<<<<<<<<
+ * import itertools
+ * import struct
  */
-  __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
-  if (__pyx_t_1) {
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_types, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_types, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":60
- *     """
- *     if PY_MAJOR_VERSION < 3:
- *         return s             # <<<<<<<<<<<<<<
- *     elif s is None:
- *         return None
+  /* "pysam/csamfile.pyx":8
+ * import sys
+ * import types
+ * import itertools             # <<<<<<<<<<<<<<
+ * import struct
+ * import ctypes
  */
-    __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_INCREF(__pyx_v_s);
-    __pyx_r = ((PyObject*)__pyx_v_s);
-    goto __pyx_L0;
-  }
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_itertools, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_itertools, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":61
- *     if PY_MAJOR_VERSION < 3:
- *         return s
- *     elif s is None:             # <<<<<<<<<<<<<<
- *         return None
- *     elif PyBytes_Check(s):
+  /* "pysam/csamfile.pyx":9
+ * import types
+ * import itertools
+ * import struct             # <<<<<<<<<<<<<<
+ * import ctypes
+ * import collections
  */
-  __pyx_t_1 = (__pyx_v_s == Py_None);
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_struct, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_struct, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":62
- *         return s
- *     elif s is None:
- *         return None             # <<<<<<<<<<<<<<
- *     elif PyBytes_Check(s):
- *         return s
+  /* "pysam/csamfile.pyx":10
+ * import itertools
+ * import struct
+ * import ctypes             # <<<<<<<<<<<<<<
+ * import collections
+ * import re
  */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = ((PyObject*)Py_None);
-    goto __pyx_L0;
-  }
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_ctypes, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_ctypes, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":63
- *     elif s is None:
- *         return None
- *     elif PyBytes_Check(s):             # <<<<<<<<<<<<<<
- *         return s
- *     elif PyUnicode_Check(s):
+  /* "pysam/csamfile.pyx":11
+ * import struct
+ * import ctypes
+ * import collections             # <<<<<<<<<<<<<<
+ * import re
+ * import platform
  */
-  __pyx_t_2 = (PyBytes_Check(__pyx_v_s) != 0);
-  if (__pyx_t_2) {
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_collections, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_collections, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":64
- *         return None
- *     elif PyBytes_Check(s):
- *         return s             # <<<<<<<<<<<<<<
- *     elif PyUnicode_Check(s):
- *         return s.encode('ascii')
+  /* "pysam/csamfile.pyx":12
+ * import ctypes
+ * import collections
+ * import re             # <<<<<<<<<<<<<<
+ * import platform
+ * import warnings
  */
-    __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_INCREF(__pyx_v_s);
-    __pyx_r = ((PyObject*)__pyx_v_s);
-    goto __pyx_L0;
-  }
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_re, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_re, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/csamfile.pyx":65
- *     elif PyBytes_Check(s):
- *         return s
- *     elif PyUnicode_Check(s):             # <<<<<<<<<<<<<<
- *         return s.encode('ascii')
- *     else:
+  /* "pysam/csamfile.pyx":13
+ * import collections
+ * import re
+ * import platform             # <<<<<<<<<<<<<<
+ * import warnings
+ * from cpython cimport PyErr_SetString, \
  */
-  __pyx_t_2 = (PyUnicode_Check(__pyx_v_s) != 0);
-  if (__pyx_t_2) {
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_platform, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_platform, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":66
- *         return s
- *     elif PyUnicode_Check(s):
- *         return s.encode('ascii')             # <<<<<<<<<<<<<<
- *     else:
- *         raise TypeError, u"Argument must be string, bytes or unicode."
+  /* "pysam/csamfile.pyx":14
+ * import re
+ * import platform
+ * import warnings             # <<<<<<<<<<<<<<
+ * from cpython cimport PyErr_SetString, \
+ *     PyBytes_Check, \
  */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple_, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    if (!(likely(PyBytes_CheckExact(__pyx_t_4))||((__pyx_t_4) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_4)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_r = ((PyObject*)__pyx_t_4);
-    __pyx_t_4 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
+  __pyx_t_1 = __Pyx_Import(__pyx_n_s_warnings, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_warnings, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/csamfile.pyx":68
- *         return s.encode('ascii')
- *     else:
- *         raise TypeError, u"Argument must be string, bytes or unicode."             # <<<<<<<<<<<<<<
+  /* "pysam/csamfile.pyx":41
  * 
- * cdef inline bytes _forceCmdlineBytes(object s):
- */
-    __Pyx_Raise(__pyx_builtin_TypeError, __pyx_kp_u_Argument_must_be_string_bytes_or, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 68; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":56
- *         raise TypeError, u"Argument must be string or unicode."
  * 
- * cdef bytes _forceBytes(object s):             # <<<<<<<<<<<<<<
- *     u"""convert string or unicode object to bytes, assuming ascii encoding.
- *     """
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile._forceBytes", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":70
- *         raise TypeError, u"Argument must be string, bytes or unicode."
+ * __all__ = ['Samfile', 'AlignedRead']             # <<<<<<<<<<<<<<
  * 
- * cdef inline bytes _forceCmdlineBytes(object s):             # <<<<<<<<<<<<<<
- *     return _forceBytes(s)
  * 
  */
+  __pyx_t_1 = PyList_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 41; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_INCREF(__pyx_n_s_Samfile);
+  PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_Samfile);
+  __Pyx_GIVEREF(__pyx_n_s_Samfile);
+  __Pyx_INCREF(__pyx_n_s_AlignedRead);
+  PyList_SET_ITEM(__pyx_t_1, 1, __pyx_n_s_AlignedRead);
+  __Pyx_GIVEREF(__pyx_n_s_AlignedRead);
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 41; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile__forceCmdlineBytes(PyObject *__pyx_v_s) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_forceCmdlineBytes", 0);
-  __Pyx_TraceCall("_forceCmdlineBytes", __pyx_f[0], 70);
-
-  /* "pysam/csamfile.pyx":71
- * 
- * cdef inline bytes _forceCmdlineBytes(object s):
- *     return _forceBytes(s)             # <<<<<<<<<<<<<<
- * 
- * cdef _charptr_to_str(char* s):
+  /* "pysam/csamfile.pyx":1
+ * # cython: embedsignature=True             # <<<<<<<<<<<<<<
+ * # cython: profile=True
+ * # adds doc-strings for sphinx
  */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_s); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":70
- *         raise TypeError, u"Argument must be string, bytes or unicode."
- * 
- * cdef inline bytes _forceCmdlineBytes(object s):             # <<<<<<<<<<<<<<
- *     return _forceBytes(s)
- * 
- */
-
-  /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile._forceCmdlineBytes", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
+  if (__pyx_m) {
+    __Pyx_AddTraceback("init pysam.csamfile", __pyx_clineno, __pyx_lineno, __pyx_filename);
+    Py_DECREF(__pyx_m); __pyx_m = 0;
+  } else if (!PyErr_Occurred()) {
+    PyErr_SetString(PyExc_ImportError, "init pysam.csamfile");
+  }
   __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
   __Pyx_RefNannyFinishContext();
-  return __pyx_r;
+  #if PY_MAJOR_VERSION < 3
+  return;
+  #else
+  return __pyx_m;
+  #endif
 }
 
-/* "pysam/csamfile.pyx":73
- *     return _forceBytes(s)
- * 
- * cdef _charptr_to_str(char* s):             # <<<<<<<<<<<<<<
- *     if PY_MAJOR_VERSION < 3:
- *         return s
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile__charptr_to_str(char *__pyx_v_s) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_charptr_to_str", 0);
-  __Pyx_TraceCall("_charptr_to_str", __pyx_f[0], 73);
-
-  /* "pysam/csamfile.pyx":74
- * 
- * cdef _charptr_to_str(char* s):
- *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
- *         return s
- *     else:
- */
-  __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":75
- * cdef _charptr_to_str(char* s):
- *     if PY_MAJOR_VERSION < 3:
- *         return s             # <<<<<<<<<<<<<<
- *     else:
- *         return s.decode("ascii")
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 75; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":77
- *         return s
- *     else:
- *         return s.decode("ascii")             # <<<<<<<<<<<<<<
- * 
- * cdef _forceStr(object s):
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, strlen(__pyx_v_s), NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L0;
-  }
+/* Runtime support code */
+#if CYTHON_REFNANNY
+static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) {
+    PyObject *m = NULL, *p = NULL;
+    void *r = NULL;
+    m = PyImport_ImportModule((char *)modname);
+    if (!m) goto end;
+    p = PyObject_GetAttrString(m, (char *)"RefNannyAPI");
+    if (!p) goto end;
+    r = PyLong_AsVoidPtr(p);
+end:
+    Py_XDECREF(p);
+    Py_XDECREF(m);
+    return (__Pyx_RefNannyAPIStruct *)r;
+}
+#endif /* CYTHON_REFNANNY */
 
-  /* "pysam/csamfile.pyx":73
- *     return _forceBytes(s)
- * 
- * cdef _charptr_to_str(char* s):             # <<<<<<<<<<<<<<
- *     if PY_MAJOR_VERSION < 3:
- *         return s
- */
+static void __Pyx_call_next_tp_dealloc(PyObject* obj, destructor current_tp_dealloc) {
+    PyTypeObject* type = Py_TYPE(obj);
+    while (type && type->tp_dealloc != current_tp_dealloc)
+        type = type->tp_base;
+    while (type && type->tp_dealloc == current_tp_dealloc)
+        type = type->tp_base;
+    if (type)
+        type->tp_dealloc(obj);
+}
+
+static int __Pyx_call_next_tp_traverse(PyObject* obj, visitproc v, void *a, traverseproc current_tp_traverse) {
+    PyTypeObject* type = Py_TYPE(obj);
+    while (type && type->tp_traverse != current_tp_traverse)
+        type = type->tp_base;
+    while (type && type->tp_traverse == current_tp_traverse)
+        type = type->tp_base;
+    if (type && type->tp_traverse)
+        return type->tp_traverse(obj, v, a);
+    return 0;
+}
 
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile._charptr_to_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
+static void __Pyx_call_next_tp_clear(PyObject* obj, inquiry current_tp_clear) {
+    PyTypeObject* type = Py_TYPE(obj);
+    while (type && type->tp_clear != current_tp_clear)
+        type = type->tp_base;
+    while (type && type->tp_clear == current_tp_clear)
+        type = type->tp_base;
+    if (type && type->tp_clear)
+        type->tp_clear(obj);
 }
 
-/* "pysam/csamfile.pyx":79
- *         return s.decode("ascii")
- * 
- * cdef _forceStr(object s):             # <<<<<<<<<<<<<<
- *     """Return s converted to str type of current Python
- *     (bytes in Py2, unicode in Py3)"""
- */
+static void* __Pyx_GetVtable(PyObject *dict) {
+    void* ptr;
+    PyObject *ob = PyObject_GetItem(dict, __pyx_n_s_pyx_vtable);
+    if (!ob)
+        goto bad;
+#if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
+    ptr = PyCapsule_GetPointer(ob, 0);
+#else
+    ptr = PyCObject_AsVoidPtr(ob);
+#endif
+    if (!ptr && !PyErr_Occurred())
+        PyErr_SetString(PyExc_RuntimeError, "invalid vtable found for imported type");
+    Py_DECREF(ob);
+    return ptr;
+bad:
+    Py_XDECREF(ob);
+    return NULL;
+}
 
-static PyObject *__pyx_f_5pysam_8csamfile__forceStr(PyObject *__pyx_v_s) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_forceStr", 0);
-  __Pyx_TraceCall("_forceStr", __pyx_f[0], 79);
-
-  /* "pysam/csamfile.pyx":82
- *     """Return s converted to str type of current Python
- *     (bytes in Py2, unicode in Py3)"""
- *     if s is None:             # <<<<<<<<<<<<<<
- *         return None
- *     if PY_MAJOR_VERSION < 3:
- */
-  __pyx_t_1 = (__pyx_v_s == Py_None);
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":83
- *     (bytes in Py2, unicode in Py3)"""
- *     if s is None:
- *         return None             # <<<<<<<<<<<<<<
- *     if PY_MAJOR_VERSION < 3:
- *         return s
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":84
- *     if s is None:
- *         return None
- *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
- *         return s
- *     elif PyBytes_Check(s):
- */
-  __pyx_t_2 = ((PY_MAJOR_VERSION < 3) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":85
- *         return None
- *     if PY_MAJOR_VERSION < 3:
- *         return s             # <<<<<<<<<<<<<<
- *     elif PyBytes_Check(s):
- *         return s.decode('ascii')
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_v_s);
-    __pyx_r = __pyx_v_s;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":86
- *     if PY_MAJOR_VERSION < 3:
- *         return s
- *     elif PyBytes_Check(s):             # <<<<<<<<<<<<<<
- *         return s.decode('ascii')
- *     else:
- */
-  __pyx_t_2 = (PyBytes_Check(__pyx_v_s) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":87
- *         return s
- *     elif PyBytes_Check(s):
- *         return s.decode('ascii')             # <<<<<<<<<<<<<<
- *     else:
- *         # assume unicode
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_decode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_r = __pyx_t_4;
-    __pyx_t_4 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":90
- *     else:
- *         # assume unicode
- *         return s             # <<<<<<<<<<<<<<
- * 
- * ########################################################################
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_v_s);
-    __pyx_r = __pyx_v_s;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":79
- *         return s.decode("ascii")
- * 
- * cdef _forceStr(object s):             # <<<<<<<<<<<<<<
- *     """Return s converted to str type of current Python
- *     (bytes in Py2, unicode in Py3)"""
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile._forceStr", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":120
- * #####################################################################
- * cdef class AlignedRead
- * cdef makeAlignedRead(bam1_t * src):             # <<<<<<<<<<<<<<
- *     '''enter src into AlignedRead.'''
- *     # note that the following does not call __init__
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile_makeAlignedRead(bam1_t *__pyx_v_src) {
-  struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_dest = 0;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("makeAlignedRead", 0);
-  __Pyx_TraceCall("makeAlignedRead", __pyx_f[0], 120);
-
-  /* "pysam/csamfile.pyx":123
- *     '''enter src into AlignedRead.'''
- *     # note that the following does not call __init__
- *     cdef AlignedRead dest = AlignedRead.__new__(AlignedRead)             # <<<<<<<<<<<<<<
- *     dest._delegate = bam_dup1(src)
- *     return dest
- */
-  __pyx_t_1 = __pyx_tp_new_5pysam_8csamfile_AlignedRead(((PyTypeObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_AlignedRead)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_8csamfile_AlignedRead)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_dest = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":124
- *     # note that the following does not call __init__
- *     cdef AlignedRead dest = AlignedRead.__new__(AlignedRead)
- *     dest._delegate = bam_dup1(src)             # <<<<<<<<<<<<<<
- *     return dest
- * 
- */
-  __pyx_v_dest->_delegate = bam_dup1(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":125
- *     cdef AlignedRead dest = AlignedRead.__new__(AlignedRead)
- *     dest._delegate = bam_dup1(src)
- *     return dest             # <<<<<<<<<<<<<<
- * 
- * cdef class PileupProxy
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_dest));
-  __pyx_r = ((PyObject *)__pyx_v_dest);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":120
- * #####################################################################
- * cdef class AlignedRead
- * cdef makeAlignedRead(bam1_t * src):             # <<<<<<<<<<<<<<
- *     '''enter src into AlignedRead.'''
- *     # note that the following does not call __init__
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.makeAlignedRead", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF((PyObject *)__pyx_v_dest);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":128
- * 
- * cdef class PileupProxy
- * cdef makePileupProxy(bam_pileup1_t ** plp, int tid, int pos, int n):             # <<<<<<<<<<<<<<
- *     # note that the following does not call __init__
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile_makePileupProxy(bam_pileup1_t **__pyx_v_plp, int __pyx_v_tid, int __pyx_v_pos, int __pyx_v_n) {
-  struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_dest = 0;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("makePileupProxy", 0);
-  __Pyx_TraceCall("makePileupProxy", __pyx_f[0], 128);
-
-  /* "pysam/csamfile.pyx":130
- * cdef makePileupProxy(bam_pileup1_t ** plp, int tid, int pos, int n):
- *     # note that the following does not call __init__
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)             # <<<<<<<<<<<<<<
- *      dest.plp = plp
- *      dest.tid = tid
- */
-  __pyx_t_1 = __pyx_tp_new_5pysam_8csamfile_PileupProxy(((PyTypeObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_PileupProxy)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_8csamfile_PileupProxy)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_dest = ((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":131
- *     # note that the following does not call __init__
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
- *      dest.plp = plp             # <<<<<<<<<<<<<<
- *      dest.tid = tid
- *      dest.pos = pos
- */
-  __pyx_v_dest->plp = __pyx_v_plp;
-
-  /* "pysam/csamfile.pyx":132
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
- *      dest.plp = plp
- *      dest.tid = tid             # <<<<<<<<<<<<<<
- *      dest.pos = pos
- *      dest.n = n
- */
-  __pyx_v_dest->tid = __pyx_v_tid;
-
-  /* "pysam/csamfile.pyx":133
- *      dest.plp = plp
- *      dest.tid = tid
- *      dest.pos = pos             # <<<<<<<<<<<<<<
- *      dest.n = n
- *      return dest
- */
-  __pyx_v_dest->pos = __pyx_v_pos;
-
-  /* "pysam/csamfile.pyx":134
- *      dest.tid = tid
- *      dest.pos = pos
- *      dest.n = n             # <<<<<<<<<<<<<<
- *      return dest
- * 
- */
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_n); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 134; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_dest), __pyx_n_s_n, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 134; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":135
- *      dest.pos = pos
- *      dest.n = n
- *      return dest             # <<<<<<<<<<<<<<
- * 
- * cdef class PileupRead
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_dest));
-  __pyx_r = ((PyObject *)__pyx_v_dest);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":128
- * 
- * cdef class PileupProxy
- * cdef makePileupProxy(bam_pileup1_t ** plp, int tid, int pos, int n):             # <<<<<<<<<<<<<<
- *     # note that the following does not call __init__
- *      cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.makePileupProxy", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF((PyObject *)__pyx_v_dest);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":138
- * 
- * cdef class PileupRead
- * cdef makePileupRead( bam_pileup1_t * src ):             # <<<<<<<<<<<<<<
- *     '''fill a  PileupRead object from a bam_pileup1_t * object.'''
- *     cdef PileupRead dest = PileupRead.__new__(PileupRead)
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile_makePileupRead(bam_pileup1_t *__pyx_v_src) {
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_dest = 0;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int32_t __pyx_t_2;
-  int __pyx_t_3;
-  uint32_t __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("makePileupRead", 0);
-  __Pyx_TraceCall("makePileupRead", __pyx_f[0], 138);
-
-  /* "pysam/csamfile.pyx":140
- * cdef makePileupRead( bam_pileup1_t * src ):
- *     '''fill a  PileupRead object from a bam_pileup1_t * object.'''
- *     cdef PileupRead dest = PileupRead.__new__(PileupRead)             # <<<<<<<<<<<<<<
- *     dest._alignment = makeAlignedRead( src.b )
- *     dest._qpos = src.qpos
- */
-  __pyx_t_1 = __pyx_tp_new_5pysam_8csamfile_PileupRead(((PyTypeObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_PileupRead)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_8csamfile_PileupRead)))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_dest = ((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":141
- *     '''fill a  PileupRead object from a bam_pileup1_t * object.'''
- *     cdef PileupRead dest = PileupRead.__new__(PileupRead)
- *     dest._alignment = makeAlignedRead( src.b )             # <<<<<<<<<<<<<<
- *     dest._qpos = src.qpos
- *     dest._indel = src.indel
- */
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_src->b); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_8csamfile_AlignedRead))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GIVEREF(__pyx_t_1);
-  __Pyx_GOTREF(__pyx_v_dest->_alignment);
-  __Pyx_DECREF(((PyObject *)__pyx_v_dest->_alignment));
-  __pyx_v_dest->_alignment = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":142
- *     cdef PileupRead dest = PileupRead.__new__(PileupRead)
- *     dest._alignment = makeAlignedRead( src.b )
- *     dest._qpos = src.qpos             # <<<<<<<<<<<<<<
- *     dest._indel = src.indel
- *     dest._level = src.level
- */
-  __pyx_t_2 = __pyx_v_src->qpos;
-  __pyx_v_dest->_qpos = __pyx_t_2;
-
-  /* "pysam/csamfile.pyx":143
- *     dest._alignment = makeAlignedRead( src.b )
- *     dest._qpos = src.qpos
- *     dest._indel = src.indel             # <<<<<<<<<<<<<<
- *     dest._level = src.level
- *     dest._is_del = src.is_del
- */
-  __pyx_t_3 = __pyx_v_src->indel;
-  __pyx_v_dest->_indel = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":144
- *     dest._qpos = src.qpos
- *     dest._indel = src.indel
- *     dest._level = src.level             # <<<<<<<<<<<<<<
- *     dest._is_del = src.is_del
- *     dest._is_head = src.is_head
- */
-  __pyx_t_3 = __pyx_v_src->level;
-  __pyx_v_dest->_level = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":145
- *     dest._indel = src.indel
- *     dest._level = src.level
- *     dest._is_del = src.is_del             # <<<<<<<<<<<<<<
- *     dest._is_head = src.is_head
- *     dest._is_tail = src.is_tail
- */
-  __pyx_t_4 = __pyx_v_src->is_del;
-  __pyx_v_dest->_is_del = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":146
- *     dest._level = src.level
- *     dest._is_del = src.is_del
- *     dest._is_head = src.is_head             # <<<<<<<<<<<<<<
- *     dest._is_tail = src.is_tail
- *     return dest
- */
-  __pyx_t_4 = __pyx_v_src->is_head;
-  __pyx_v_dest->_is_head = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":147
- *     dest._is_del = src.is_del
- *     dest._is_head = src.is_head
- *     dest._is_tail = src.is_tail             # <<<<<<<<<<<<<<
- *     return dest
- * 
- */
-  __pyx_t_4 = __pyx_v_src->is_tail;
-  __pyx_v_dest->_is_tail = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":148
- *     dest._is_head = src.is_head
- *     dest._is_tail = src.is_tail
- *     return dest             # <<<<<<<<<<<<<<
- * 
- * cdef convertBinaryTagToList( uint8_t * s ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_dest));
-  __pyx_r = ((PyObject *)__pyx_v_dest);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":138
- * 
- * cdef class PileupRead
- * cdef makePileupRead( bam_pileup1_t * src ):             # <<<<<<<<<<<<<<
- *     '''fill a  PileupRead object from a bam_pileup1_t * object.'''
- *     cdef PileupRead dest = PileupRead.__new__(PileupRead)
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.makePileupRead", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF((PyObject *)__pyx_v_dest);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":150
- *     return dest
- * 
- * cdef convertBinaryTagToList( uint8_t * s ):             # <<<<<<<<<<<<<<
- *     """return bytesize, number of values list of values in s."""
- *     cdef char auxtype
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile_convertBinaryTagToList(uint8_t *__pyx_v_s) {
-  char __pyx_v_auxtype;
-  uint8_t __pyx_v_byte_size;
-  int32_t __pyx_v_nvalues;
-  PyObject *__pyx_v_values = NULL;
-  CYTHON_UNUSED long __pyx_v_x;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int32_t __pyx_t_2;
-  int __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("convertBinaryTagToList", 0);
-  __Pyx_TraceCall("convertBinaryTagToList", __pyx_f[0], 150);
-
-  /* "pysam/csamfile.pyx":157
- * 
- *     # get byte size
- *     auxtype = s[0]             # <<<<<<<<<<<<<<
- *     byte_size = aux_type2size( auxtype )
- *     s += 1
- */
-  __pyx_v_auxtype = (__pyx_v_s[0]);
-
-  /* "pysam/csamfile.pyx":158
- *     # get byte size
- *     auxtype = s[0]
- *     byte_size = aux_type2size( auxtype )             # <<<<<<<<<<<<<<
- *     s += 1
- *     # get number of values in array
- */
-  __pyx_v_byte_size = aux_type2size(__pyx_v_auxtype);
-
-  /* "pysam/csamfile.pyx":159
- *     auxtype = s[0]
- *     byte_size = aux_type2size( auxtype )
- *     s += 1             # <<<<<<<<<<<<<<
- *     # get number of values in array
- *     nvalues = (<int32_t*>s)[0]
- */
-  __pyx_v_s = (__pyx_v_s + 1);
-
-  /* "pysam/csamfile.pyx":161
- *     s += 1
- *     # get number of values in array
- *     nvalues = (<int32_t*>s)[0]             # <<<<<<<<<<<<<<
- *     s += 4
- *     # get values
- */
-  __pyx_v_nvalues = (((int32_t *)__pyx_v_s)[0]);
-
-  /* "pysam/csamfile.pyx":162
- *     # get number of values in array
- *     nvalues = (<int32_t*>s)[0]
- *     s += 4             # <<<<<<<<<<<<<<
- *     # get values
- *     values = []
- */
-  __pyx_v_s = (__pyx_v_s + 4);
-
-  /* "pysam/csamfile.pyx":164
- *     s += 4
- *     # get values
- *     values = []             # <<<<<<<<<<<<<<
- *     if auxtype == 'c':
- *         for x from 0 <= x < nvalues:
- */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_values = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":189
- *             values.append((<uint32_t*>s)[0])
- *             s += 4
- *     elif auxtype == 'f':             # <<<<<<<<<<<<<<
- *         for x from 0 <= x < nvalues:
- *             values.append((<float*>s)[0])
- */
-  switch (__pyx_v_auxtype) {
-
-    /* "pysam/csamfile.pyx":165
- *     # get values
- *     values = []
- *     if auxtype == 'c':             # <<<<<<<<<<<<<<
- *         for x from 0 <= x < nvalues:
- *             values.append((<int8_t*>s)[0])
- */
-    case 'c':
-
-    /* "pysam/csamfile.pyx":166
- *     values = []
- *     if auxtype == 'c':
- *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
- *             values.append((<int8_t*>s)[0])
- *             s += 1
- */
-    __pyx_t_2 = __pyx_v_nvalues;
-    for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
-
-      /* "pysam/csamfile.pyx":167
- *     if auxtype == 'c':
- *         for x from 0 <= x < nvalues:
- *             values.append((<int8_t*>s)[0])             # <<<<<<<<<<<<<<
- *             s += 1
- *     elif auxtype == 'C':
- */
-      __pyx_t_1 = __Pyx_PyInt_From_int8_t((((int8_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":168
- *         for x from 0 <= x < nvalues:
- *             values.append((<int8_t*>s)[0])
- *             s += 1             # <<<<<<<<<<<<<<
- *     elif auxtype == 'C':
- *         for x from 0 <= x < nvalues:
- */
-      __pyx_v_s = (__pyx_v_s + 1);
-    }
-    break;
-
-    /* "pysam/csamfile.pyx":169
- *             values.append((<int8_t*>s)[0])
- *             s += 1
- *     elif auxtype == 'C':             # <<<<<<<<<<<<<<
- *         for x from 0 <= x < nvalues:
- *             values.append((<uint8_t*>s)[0])
- */
-    case 'C':
-
-    /* "pysam/csamfile.pyx":170
- *             s += 1
- *     elif auxtype == 'C':
- *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
- *             values.append((<uint8_t*>s)[0])
- *             s += 1
- */
-    __pyx_t_2 = __pyx_v_nvalues;
-    for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
-
-      /* "pysam/csamfile.pyx":171
- *     elif auxtype == 'C':
- *         for x from 0 <= x < nvalues:
- *             values.append((<uint8_t*>s)[0])             # <<<<<<<<<<<<<<
- *             s += 1
- *     elif auxtype == 's':
- */
-      __pyx_t_1 = __Pyx_PyInt_From_uint8_t((((uint8_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":172
- *         for x from 0 <= x < nvalues:
- *             values.append((<uint8_t*>s)[0])
- *             s += 1             # <<<<<<<<<<<<<<
- *     elif auxtype == 's':
- *         for x from 0 <= x < nvalues:
- */
-      __pyx_v_s = (__pyx_v_s + 1);
-    }
-    break;
-
-    /* "pysam/csamfile.pyx":173
- *             values.append((<uint8_t*>s)[0])
- *             s += 1
- *     elif auxtype == 's':             # <<<<<<<<<<<<<<
- *         for x from 0 <= x < nvalues:
- *             values.append((<int16_t*>s)[0])
- */
-    case 's':
-
-    /* "pysam/csamfile.pyx":174
- *             s += 1
- *     elif auxtype == 's':
- *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
- *             values.append((<int16_t*>s)[0])
- *             s += 2
- */
-    __pyx_t_2 = __pyx_v_nvalues;
-    for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
-
-      /* "pysam/csamfile.pyx":175
- *     elif auxtype == 's':
- *         for x from 0 <= x < nvalues:
- *             values.append((<int16_t*>s)[0])             # <<<<<<<<<<<<<<
- *             s += 2
- *     elif auxtype == 'S':
- */
-      __pyx_t_1 = __Pyx_PyInt_From_int16_t((((int16_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":176
- *         for x from 0 <= x < nvalues:
- *             values.append((<int16_t*>s)[0])
- *             s += 2             # <<<<<<<<<<<<<<
- *     elif auxtype == 'S':
- *         for x from 0 <= x < nvalues:
- */
-      __pyx_v_s = (__pyx_v_s + 2);
-    }
-    break;
-
-    /* "pysam/csamfile.pyx":177
- *             values.append((<int16_t*>s)[0])
- *             s += 2
- *     elif auxtype == 'S':             # <<<<<<<<<<<<<<
- *         for x from 0 <= x < nvalues:
- *             values.append((<uint16_t*>s)[0])
- */
-    case 'S':
-
-    /* "pysam/csamfile.pyx":178
- *             s += 2
- *     elif auxtype == 'S':
- *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
- *             values.append((<uint16_t*>s)[0])
- *             s += 2
- */
-    __pyx_t_2 = __pyx_v_nvalues;
-    for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
-
-      /* "pysam/csamfile.pyx":179
- *     elif auxtype == 'S':
- *         for x from 0 <= x < nvalues:
- *             values.append((<uint16_t*>s)[0])             # <<<<<<<<<<<<<<
- *             s += 2
- *     elif auxtype == 'i':
- */
-      __pyx_t_1 = __Pyx_PyInt_From_uint16_t((((uint16_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":180
- *         for x from 0 <= x < nvalues:
- *             values.append((<uint16_t*>s)[0])
- *             s += 2             # <<<<<<<<<<<<<<
- *     elif auxtype == 'i':
- *         for x from 0 <= x < nvalues:
- */
-      __pyx_v_s = (__pyx_v_s + 2);
-    }
-    break;
-
-    /* "pysam/csamfile.pyx":181
- *             values.append((<uint16_t*>s)[0])
- *             s += 2
- *     elif auxtype == 'i':             # <<<<<<<<<<<<<<
- *         for x from 0 <= x < nvalues:
- *             values.append((<int32_t*>s)[0])
- */
-    case 'i':
-
-    /* "pysam/csamfile.pyx":182
- *             s += 2
- *     elif auxtype == 'i':
- *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
- *             values.append((<int32_t*>s)[0])
- *             s += 4
- */
-    __pyx_t_2 = __pyx_v_nvalues;
-    for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
-
-      /* "pysam/csamfile.pyx":183
- *     elif auxtype == 'i':
- *         for x from 0 <= x < nvalues:
- *             values.append((<int32_t*>s)[0])             # <<<<<<<<<<<<<<
- *             s += 4
- *     elif auxtype == 'I':
- */
-      __pyx_t_1 = __Pyx_PyInt_From_int32_t((((int32_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":184
- *         for x from 0 <= x < nvalues:
- *             values.append((<int32_t*>s)[0])
- *             s += 4             # <<<<<<<<<<<<<<
- *     elif auxtype == 'I':
- *         for x from 0 <= x < nvalues:
- */
-      __pyx_v_s = (__pyx_v_s + 4);
-    }
-    break;
-
-    /* "pysam/csamfile.pyx":185
- *             values.append((<int32_t*>s)[0])
- *             s += 4
- *     elif auxtype == 'I':             # <<<<<<<<<<<<<<
- *         for x from 0 <= x < nvalues:
- *             values.append((<uint32_t*>s)[0])
- */
-    case 'I':
-
-    /* "pysam/csamfile.pyx":186
- *             s += 4
- *     elif auxtype == 'I':
- *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
- *             values.append((<uint32_t*>s)[0])
- *             s += 4
- */
-    __pyx_t_2 = __pyx_v_nvalues;
-    for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
-
-      /* "pysam/csamfile.pyx":187
- *     elif auxtype == 'I':
- *         for x from 0 <= x < nvalues:
- *             values.append((<uint32_t*>s)[0])             # <<<<<<<<<<<<<<
- *             s += 4
- *     elif auxtype == 'f':
- */
-      __pyx_t_1 = __Pyx_PyInt_From_uint32_t((((uint32_t *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":188
- *         for x from 0 <= x < nvalues:
- *             values.append((<uint32_t*>s)[0])
- *             s += 4             # <<<<<<<<<<<<<<
- *     elif auxtype == 'f':
- *         for x from 0 <= x < nvalues:
- */
-      __pyx_v_s = (__pyx_v_s + 4);
-    }
-    break;
-
-    /* "pysam/csamfile.pyx":189
- *             values.append((<uint32_t*>s)[0])
- *             s += 4
- *     elif auxtype == 'f':             # <<<<<<<<<<<<<<
- *         for x from 0 <= x < nvalues:
- *             values.append((<float*>s)[0])
- */
-    case 'f':
-
-    /* "pysam/csamfile.pyx":190
- *             s += 4
- *     elif auxtype == 'f':
- *         for x from 0 <= x < nvalues:             # <<<<<<<<<<<<<<
- *             values.append((<float*>s)[0])
- *             s += 4
- */
-    __pyx_t_2 = __pyx_v_nvalues;
-    for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
-
-      /* "pysam/csamfile.pyx":191
- *     elif auxtype == 'f':
- *         for x from 0 <= x < nvalues:
- *             values.append((<float*>s)[0])             # <<<<<<<<<<<<<<
- *             s += 4
- * 
- */
-      __pyx_t_1 = PyFloat_FromDouble((((float *)__pyx_v_s)[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":192
- *         for x from 0 <= x < nvalues:
- *             values.append((<float*>s)[0])
- *             s += 4             # <<<<<<<<<<<<<<
- * 
- *     return byte_size, nvalues, values
- */
-      __pyx_v_s = (__pyx_v_s + 4);
-    }
-    break;
-    default: break;
-  }
-
-  /* "pysam/csamfile.pyx":194
- *             s += 4
- * 
- *     return byte_size, nvalues, values             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint8_t(__pyx_v_byte_size); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_nvalues); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = PyTuple_New(3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-  PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  __Pyx_INCREF(__pyx_v_values);
-  PyTuple_SET_ITEM(__pyx_t_5, 2, __pyx_v_values);
-  __Pyx_GIVEREF(__pyx_v_values);
-  __pyx_t_1 = 0;
-  __pyx_t_4 = 0;
-  __pyx_r = __pyx_t_5;
-  __pyx_t_5 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":150
- *     return dest
- * 
- * cdef convertBinaryTagToList( uint8_t * s ):             # <<<<<<<<<<<<<<
- *     """return bytesize, number of values list of values in s."""
- *     cdef char auxtype
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.convertBinaryTagToList", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_values);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":210
- *         list of reads (:class:`pysam.PileupRead`) aligned to this column
- *     '''
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_12PileupColumn_1__str__(PyObject *__pyx_self, PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_12PileupColumn___str__[] = "PileupColumn.__str__(self)";
-static PyMethodDef __pyx_mdef_5pysam_8csamfile_12PileupColumn_1__str__ = {__Pyx_NAMESTR("__str__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_12PileupColumn_1__str__, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_12PileupColumn___str__)};
-static PyObject *__pyx_pw_5pysam_8csamfile_12PileupColumn_1__str__(PyObject *__pyx_self, PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_12PileupColumn___str__(__pyx_self, ((PyObject *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_12PileupColumn___str__(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 210);
-
-  /* "pysam/csamfile.pyx":211
- *     '''
- *     def __str__(self):
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\             # <<<<<<<<<<<<<<
- *             "\n" + "\n".join( map(str, self.pileups) )
- * 
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_n); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyTuple_New(3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_4, 2, __pyx_t_3);
-  __Pyx_GIVEREF(__pyx_t_3);
-  __pyx_t_1 = 0;
-  __pyx_t_2 = 0;
-  __pyx_t_3 = 0;
-  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = PyNumber_Add(__pyx_t_3, __pyx_kp_s__4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":212
- *     def __str__(self):
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_pileups); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_3);
-  __Pyx_GIVEREF(__pyx_t_3);
-  __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__4, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = PyNumber_Add(__pyx_t_4, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":210
- *         list of reads (:class:`pysam.PileupRead`) aligned to this column
- *     '''
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.PileupColumn.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":309
- *     '''
- * 
- *     def __cinit__(self, *args, **kwargs ):             # <<<<<<<<<<<<<<
- *         self.htsfile = NULL
- *         self._filename = None
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_7Samfile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_7Samfile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_args = 0;
-  PyObject *__pyx_v_kwargs = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__cinit__ (wrapper)", 0);
-  if (unlikely(__pyx_kwds) && unlikely(!__Pyx_CheckKeywordStrings(__pyx_kwds, "__cinit__", 1))) return -1;
-  __pyx_v_kwargs = (__pyx_kwds) ? PyDict_Copy(__pyx_kwds) : PyDict_New();
-  if (unlikely(!__pyx_v_kwargs)) return -1;
-  __Pyx_GOTREF(__pyx_v_kwargs);
-  __Pyx_INCREF(__pyx_args);
-  __pyx_v_args = __pyx_args;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_args, __pyx_v_kwargs);
-
-  /* function exit code */
-  __Pyx_XDECREF(__pyx_v_args);
-  __Pyx_XDECREF(__pyx_v_kwargs);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_7Samfile___cinit__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__cinit__", 0);
-  __Pyx_TraceCall("__cinit__", __pyx_f[0], 309);
-
-  /* "pysam/csamfile.pyx":310
- * 
- *     def __cinit__(self, *args, **kwargs ):
- *         self.htsfile = NULL             # <<<<<<<<<<<<<<
- *         self._filename = None
- *         self.isbam = False
- */
-  __pyx_v_self->htsfile = NULL;
-
-  /* "pysam/csamfile.pyx":311
- *     def __cinit__(self, *args, **kwargs ):
- *         self.htsfile = NULL
- *         self._filename = None             # <<<<<<<<<<<<<<
- *         self.isbam = False
- *         self.isstream = False
- */
-  __Pyx_INCREF(Py_None);
-  __Pyx_GIVEREF(Py_None);
-  __Pyx_GOTREF(__pyx_v_self->_filename);
-  __Pyx_DECREF(__pyx_v_self->_filename);
-  __pyx_v_self->_filename = Py_None;
-
-  /* "pysam/csamfile.pyx":312
- *         self.htsfile = NULL
- *         self._filename = None
- *         self.isbam = False             # <<<<<<<<<<<<<<
- *         self.isstream = False
- *         self._open(*args, **kwargs)
- */
-  __pyx_v_self->isbam = 0;
-
-  /* "pysam/csamfile.pyx":313
- *         self._filename = None
- *         self.isbam = False
- *         self.isstream = False             # <<<<<<<<<<<<<<
- *         self._open(*args, **kwargs)
- * 
- */
-  __pyx_v_self->isstream = 0;
-
-  /* "pysam/csamfile.pyx":314
- *         self.isbam = False
- *         self.isstream = False
- *         self._open(*args, **kwargs)             # <<<<<<<<<<<<<<
- * 
- *         # allocate memory for iterator
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __pyx_v_kwargs;
-  __Pyx_INCREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-  /* "pysam/csamfile.pyx":317
- * 
- *         # allocate memory for iterator
- *         self.b = <bam1_t*>calloc(1, sizeof(bam1_t))             # <<<<<<<<<<<<<<
- * 
- *     def _isOpen( self ):
- */
-  __pyx_v_self->b = ((bam1_t *)calloc(1, (sizeof(bam1_t))));
-
-  /* "pysam/csamfile.pyx":309
- *     '''
- * 
- *     def __cinit__(self, *args, **kwargs ):             # <<<<<<<<<<<<<<
- *         self.htsfile = NULL
- *         self._filename = None
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":319
- *         self.b = <bam1_t*>calloc(1, sizeof(bam1_t))
- * 
- *     def _isOpen( self ):             # <<<<<<<<<<<<<<
- *         '''return true if htsfile has been opened.'''
- *         return self.htsfile != NULL
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_2_isOpen[] = "Samfile._isOpen(self)\nreturn true if htsfile has been opened.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_3_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("_isOpen (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_2_isOpen(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_isOpen", 0);
-  __Pyx_TraceCall("_isOpen", __pyx_f[0], 319);
-
-  /* "pysam/csamfile.pyx":321
- *     def _isOpen( self ):
- *         '''return true if htsfile has been opened.'''
- *         return self.htsfile != NULL             # <<<<<<<<<<<<<<
- * 
- *     def _hasIndex( self ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->htsfile != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":319
- *         self.b = <bam1_t*>calloc(1, sizeof(bam1_t))
- * 
- *     def _isOpen( self ):             # <<<<<<<<<<<<<<
- *         '''return true if htsfile has been opened.'''
- *         return self.htsfile != NULL
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._isOpen", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":323
- *         return self.htsfile != NULL
- * 
- *     def _hasIndex( self ):             # <<<<<<<<<<<<<<
- *         '''return true if htsfile has an existing (and opened) index.'''
- *         return self.index != NULL
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_5_hasIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_4_hasIndex[] = "Samfile._hasIndex(self)\nreturn true if htsfile has an existing (and opened) index.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_5_hasIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("_hasIndex (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4_hasIndex(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_hasIndex", 0);
-  __Pyx_TraceCall("_hasIndex", __pyx_f[0], 323);
-
-  /* "pysam/csamfile.pyx":325
- *     def _hasIndex( self ):
- *         '''return true if htsfile has an existing (and opened) index.'''
- *         return self.index != NULL             # <<<<<<<<<<<<<<
- * 
- *     def _open(self,
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->index != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":323
- *         return self.htsfile != NULL
- * 
- *     def _hasIndex( self ):             # <<<<<<<<<<<<<<
- *         '''return true if htsfile has an existing (and opened) index.'''
- *         return self.index != NULL
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._hasIndex", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":327
- *         return self.index != NULL
- * 
- *     def _open(self,             # <<<<<<<<<<<<<<
- *               filename,
- *               mode=None,
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_6_open[] = "Samfile._open(self, filename, mode=None, Samfile template=None, referencenames=None, referencelengths=None, text=None, header=None, port=None, add_sq_text=True, check_header=True, check_sq=True)\nopen a sam/bam file.\n\n        If _open is called on an existing bamfile, the current file will be\n        closed and a new file will be opened.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_filename = 0;
-  PyObject *__pyx_v_mode = 0;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_template = 0;
-  PyObject *__pyx_v_referencenames = 0;
-  PyObject *__pyx_v_referencelengths = 0;
-  PyObject *__pyx_v_text = 0;
-  PyObject *__pyx_v_header = 0;
-  PyObject *__pyx_v_port = 0;
-  PyObject *__pyx_v_add_sq_text = 0;
-  PyObject *__pyx_v_check_header = 0;
-  PyObject *__pyx_v_check_sq = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("_open (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_filename,&__pyx_n_s_mode,&__pyx_n_s_template,&__pyx_n_s_referencenames,&__pyx_n_s_referencelengths,&__pyx_n_s_text,&__pyx_n_s_header,&__pyx_n_s_port,&__pyx_n_s_add_sq_text,&__pyx_n_s_check_header,&__pyx_n_s_check_sq,0};
-    PyObject* values[11] = {0,0,0,0,0,0,0,0,0,0,0};
-
-    /* "pysam/csamfile.pyx":329
- *     def _open(self,
- *               filename,
- *               mode=None,             # <<<<<<<<<<<<<<
- *               Samfile template=None,
- *               referencenames=None,
- */
-    values[1] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":330
- *               filename,
- *               mode=None,
- *               Samfile template=None,             # <<<<<<<<<<<<<<
- *               referencenames=None,
- *               referencelengths=None,
- */
-    values[2] = (PyObject *)((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None);
-
-    /* "pysam/csamfile.pyx":331
- *               mode=None,
- *               Samfile template=None,
- *               referencenames=None,             # <<<<<<<<<<<<<<
- *               referencelengths=None,
- *               text=None,
- */
-    values[3] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":332
- *               Samfile template=None,
- *               referencenames=None,
- *               referencelengths=None,             # <<<<<<<<<<<<<<
- *               text=None,
- *               header=None,
- */
-    values[4] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":333
- *               referencenames=None,
- *               referencelengths=None,
- *               text=None,             # <<<<<<<<<<<<<<
- *               header=None,
- *               port=None,
- */
-    values[5] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":334
- *               referencelengths=None,
- *               text=None,
- *               header=None,             # <<<<<<<<<<<<<<
- *               port=None,
- *               add_sq_text=True,
- */
-    values[6] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":335
- *               text=None,
- *               header=None,
- *               port=None,             # <<<<<<<<<<<<<<
- *               add_sq_text=True,
- *               check_header=True,
- */
-    values[7] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":336
- *               header=None,
- *               port=None,
- *               add_sq_text=True,             # <<<<<<<<<<<<<<
- *               check_header=True,
- *               check_sq=True):
- */
-    values[8] = ((PyObject *)Py_True);
-
-    /* "pysam/csamfile.pyx":337
- *               port=None,
- *               add_sq_text=True,
- *               check_header=True,             # <<<<<<<<<<<<<<
- *               check_sq=True):
- *         '''open a sam/bam file.
- */
-    values[9] = ((PyObject *)Py_True);
-
-    /* "pysam/csamfile.pyx":338
- *               add_sq_text=True,
- *               check_header=True,
- *               check_sq=True):             # <<<<<<<<<<<<<<
- *         '''open a sam/bam file.
- * 
- */
-    values[10] = ((PyObject *)Py_True);
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case 11: values[10] = PyTuple_GET_ITEM(__pyx_args, 10);
-        case 10: values[9] = PyTuple_GET_ITEM(__pyx_args, 9);
-        case  9: values[8] = PyTuple_GET_ITEM(__pyx_args, 8);
-        case  8: values[7] = PyTuple_GET_ITEM(__pyx_args, 7);
-        case  7: values[6] = PyTuple_GET_ITEM(__pyx_args, 6);
-        case  6: values[5] = PyTuple_GET_ITEM(__pyx_args, 5);
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_filename)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_mode);
-          if (value) { values[1] = value; kw_args--; }
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_template);
-          if (value) { values[2] = value; kw_args--; }
-        }
-        case  3:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_referencenames);
-          if (value) { values[3] = value; kw_args--; }
-        }
-        case  4:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_referencelengths);
-          if (value) { values[4] = value; kw_args--; }
-        }
-        case  5:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_text);
-          if (value) { values[5] = value; kw_args--; }
-        }
-        case  6:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_header);
-          if (value) { values[6] = value; kw_args--; }
-        }
-        case  7:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_port);
-          if (value) { values[7] = value; kw_args--; }
-        }
-        case  8:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_add_sq_text);
-          if (value) { values[8] = value; kw_args--; }
-        }
-        case  9:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_check_header);
-          if (value) { values[9] = value; kw_args--; }
-        }
-        case 10:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_check_sq);
-          if (value) { values[10] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_open") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case 11: values[10] = PyTuple_GET_ITEM(__pyx_args, 10);
-        case 10: values[9] = PyTuple_GET_ITEM(__pyx_args, 9);
-        case  9: values[8] = PyTuple_GET_ITEM(__pyx_args, 8);
-        case  8: values[7] = PyTuple_GET_ITEM(__pyx_args, 7);
-        case  7: values[6] = PyTuple_GET_ITEM(__pyx_args, 6);
-        case  6: values[5] = PyTuple_GET_ITEM(__pyx_args, 5);
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_filename = values[0];
-    __pyx_v_mode = values[1];
-    __pyx_v_template = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[2]);
-    __pyx_v_referencenames = values[3];
-    __pyx_v_referencelengths = values[4];
-    __pyx_v_text = values[5];
-    __pyx_v_header = values[6];
-    __pyx_v_port = values[7];
-    __pyx_v_add_sq_text = values[8];
-    __pyx_v_check_header = values[9];
-    __pyx_v_check_sq = values[10];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_open", 0, 1, 11, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_template), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "template", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_6_open(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_filename, __pyx_v_mode, __pyx_v_template, __pyx_v_referencenames, __pyx_v_referencelengths, __pyx_v_text, __pyx_v_header, __pyx_v_port, __pyx_v_add_sq_text, __pyx_v_check_header, __pyx_v_check_sq);
-
-  /* "pysam/csamfile.pyx":327
- *         return self.index != NULL
- * 
- *     def _open(self,             # <<<<<<<<<<<<<<
- *               filename,
- *               mode=None,
- */
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6_open(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_template, PyObject *__pyx_v_referencenames, PyObject *__pyx_v_referencelengths, PyObject *__pyx_v_text, PyObject *__pyx_v_header, PyObject *__pyx_v_port, PyObject *__pyx_v_add_sq_text, PyObject *__pyx_v_check_header, PyObject *__pyx_v_check_sq) {
-  CYTHON_UNUSED PyObject *__pyx_v_msg = NULL;
-  PyObject *__pyx_v_bmode = 0;
-  char *__pyx_v_ctext;
-  PyObject *__pyx_v_n = NULL;
-  PyObject *__pyx_v_x = NULL;
-  PyObject *__pyx_v_name = NULL;
-  PyObject *__pyx_v_ref = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  int __pyx_t_10;
-  int __pyx_t_11;
-  Py_ssize_t __pyx_t_12;
-  Py_ssize_t __pyx_t_13;
-  PyObject *(*__pyx_t_14)(PyObject *);
-  size_t __pyx_t_15;
-  int32_t __pyx_t_16;
-  long __pyx_t_17;
-  uint32_t __pyx_t_18;
-  char const *__pyx_t_19;
-  int __pyx_t_20;
-  char *__pyx_t_21;
-  char const *__pyx_t_22;
-  char const *__pyx_t_23;
-  int __pyx_t_24;
-  BGZF *__pyx_t_25;
-  char const *__pyx_t_26;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_open", 0);
-  __Pyx_TraceCall("_open", __pyx_f[0], 327);
-  __Pyx_INCREF(__pyx_v_filename);
-  __Pyx_INCREF(__pyx_v_referencenames);
-  __Pyx_INCREF(__pyx_v_text);
-
-  /* "pysam/csamfile.pyx":346
- * 
- *         # read mode autodetection
- *         if mode is None:             # <<<<<<<<<<<<<<
- *             try:
- *                 self._open(filename, 'rb',
- */
-  __pyx_t_1 = (__pyx_v_mode == Py_None);
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":347
- *         # read mode autodetection
- *         if mode is None:
- *             try:             # <<<<<<<<<<<<<<
- *                 self._open(filename, 'rb',
- *                            template=template,
- */
-    {
-      __Pyx_ExceptionSave(&__pyx_t_3, &__pyx_t_4, &__pyx_t_5);
-      __Pyx_XGOTREF(__pyx_t_3);
-      __Pyx_XGOTREF(__pyx_t_4);
-      __Pyx_XGOTREF(__pyx_t_5);
-      /*try:*/ {
-
-        /* "pysam/csamfile.pyx":348
- *         if mode is None:
- *             try:
- *                 self._open(filename, 'rb',             # <<<<<<<<<<<<<<
- *                            template=template,
- *                            referencenames=referencenames,
- */
-        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __Pyx_INCREF(__pyx_v_filename);
-        PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_filename);
-        __Pyx_GIVEREF(__pyx_v_filename);
-        __Pyx_INCREF(__pyx_n_s_rb);
-        PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_n_s_rb);
-        __Pyx_GIVEREF(__pyx_n_s_rb);
-        __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-
-        /* "pysam/csamfile.pyx":349
- *             try:
- *                 self._open(filename, 'rb',
- *                            template=template,             # <<<<<<<<<<<<<<
- *                            referencenames=referencenames,
- *                            referencelengths=referencelengths,
- */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_template, ((PyObject *)__pyx_v_template)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-
-        /* "pysam/csamfile.pyx":350
- *                 self._open(filename, 'rb',
- *                            template=template,
- *                            referencenames=referencenames,             # <<<<<<<<<<<<<<
- *                            referencelengths=referencelengths,
- *                            text=text,
- */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_referencenames, __pyx_v_referencenames) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-
-        /* "pysam/csamfile.pyx":351
- *                            template=template,
- *                            referencenames=referencenames,
- *                            referencelengths=referencelengths,             # <<<<<<<<<<<<<<
- *                            text=text,
- *                            header=header,
- */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_referencelengths, __pyx_v_referencelengths) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-
-        /* "pysam/csamfile.pyx":352
- *                            referencenames=referencenames,
- *                            referencelengths=referencelengths,
- *                            text=text,             # <<<<<<<<<<<<<<
- *                            header=header,
- *                            port=port,
- */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_text, __pyx_v_text) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-
-        /* "pysam/csamfile.pyx":353
- *                            referencelengths=referencelengths,
- *                            text=text,
- *                            header=header,             # <<<<<<<<<<<<<<
- *                            port=port,
- *                            check_header=check_header,
- */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_header, __pyx_v_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-
-        /* "pysam/csamfile.pyx":354
- *                            text=text,
- *                            header=header,
- *                            port=port,             # <<<<<<<<<<<<<<
- *                            check_header=check_header,
- *                            check_sq=check_sq)
- */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_port, __pyx_v_port) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-
-        /* "pysam/csamfile.pyx":355
- *                            header=header,
- *                            port=port,
- *                            check_header=check_header,             # <<<<<<<<<<<<<<
- *                            check_sq=check_sq)
- *                 return
- */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_check_header, __pyx_v_check_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-
-        /* "pysam/csamfile.pyx":356
- *                            port=port,
- *                            check_header=check_header,
- *                            check_sq=check_sq)             # <<<<<<<<<<<<<<
- *                 return
- *             except ValueError, msg:
- */
-        if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_check_sq, __pyx_v_check_sq) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-
-        /* "pysam/csamfile.pyx":348
- *         if mode is None:
- *             try:
- *                 self._open(filename, 'rb',             # <<<<<<<<<<<<<<
- *                            template=template,
- *                            referencenames=referencenames,
- */
-        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_7, __pyx_t_8); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-
-        /* "pysam/csamfile.pyx":357
- *                            check_header=check_header,
- *                            check_sq=check_sq)
- *                 return             # <<<<<<<<<<<<<<
- *             except ValueError, msg:
- *                 pass
- */
-        __Pyx_XDECREF(__pyx_r);
-        __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-        goto __pyx_L8_try_return;
-      }
-      __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0;
-      goto __pyx_L11_try_end;
-      __pyx_L4_error:;
-      __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __Pyx_XDECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;
-
-      /* "pysam/csamfile.pyx":358
- *                            check_sq=check_sq)
- *                 return
- *             except ValueError, msg:             # <<<<<<<<<<<<<<
- *                 pass
- * 
- */
-      __pyx_t_10 = PyErr_ExceptionMatches(__pyx_builtin_ValueError);
-      if (__pyx_t_10) {
-        __Pyx_AddTraceback("pysam.csamfile.Samfile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_8, &__pyx_t_7) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_GOTREF(__pyx_t_7);
-        __Pyx_INCREF(__pyx_t_8);
-        __pyx_v_msg = __pyx_t_8;
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        goto __pyx_L5_exception_handled;
-      }
-      goto __pyx_L6_except_error;
-      __pyx_L6_except_error:;
-      __Pyx_XGIVEREF(__pyx_t_3);
-      __Pyx_XGIVEREF(__pyx_t_4);
-      __Pyx_XGIVEREF(__pyx_t_5);
-      __Pyx_ExceptionReset(__pyx_t_3, __pyx_t_4, __pyx_t_5);
-      goto __pyx_L1_error;
-      __pyx_L8_try_return:;
-      __Pyx_XGIVEREF(__pyx_t_3);
-      __Pyx_XGIVEREF(__pyx_t_4);
-      __Pyx_XGIVEREF(__pyx_t_5);
-      __Pyx_ExceptionReset(__pyx_t_3, __pyx_t_4, __pyx_t_5);
-      goto __pyx_L0;
-      __pyx_L5_exception_handled:;
-      __Pyx_XGIVEREF(__pyx_t_3);
-      __Pyx_XGIVEREF(__pyx_t_4);
-      __Pyx_XGIVEREF(__pyx_t_5);
-      __Pyx_ExceptionReset(__pyx_t_3, __pyx_t_4, __pyx_t_5);
-      __pyx_L11_try_end:;
-    }
-
-    /* "pysam/csamfile.pyx":361
- *                 pass
- * 
- *             self._open(filename, 'r',             # <<<<<<<<<<<<<<
- *                        template=template,
- *                        referencenames=referencenames,
- */
-    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_INCREF(__pyx_v_filename);
-    PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_filename);
-    __Pyx_GIVEREF(__pyx_v_filename);
-    __Pyx_INCREF(__pyx_n_s_r);
-    PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_n_s_r);
-    __Pyx_GIVEREF(__pyx_n_s_r);
-    __pyx_t_9 = PyDict_New(); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-
-    /* "pysam/csamfile.pyx":362
- * 
- *             self._open(filename, 'r',
- *                        template=template,             # <<<<<<<<<<<<<<
- *                        referencenames=referencenames,
- *                        referencelengths=referencelengths,
- */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_template, ((PyObject *)__pyx_v_template)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":363
- *             self._open(filename, 'r',
- *                        template=template,
- *                        referencenames=referencenames,             # <<<<<<<<<<<<<<
- *                        referencelengths=referencelengths,
- *                        text=text,
- */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_referencenames, __pyx_v_referencenames) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":364
- *                        template=template,
- *                        referencenames=referencenames,
- *                        referencelengths=referencelengths,             # <<<<<<<<<<<<<<
- *                        text=text,
- *                        header=header,
- */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_referencelengths, __pyx_v_referencelengths) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":365
- *                        referencenames=referencenames,
- *                        referencelengths=referencelengths,
- *                        text=text,             # <<<<<<<<<<<<<<
- *                        header=header,
- *                        port=port,
- */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_text, __pyx_v_text) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":366
- *                        referencelengths=referencelengths,
- *                        text=text,
- *                        header=header,             # <<<<<<<<<<<<<<
- *                        port=port,
- *                        check_header=check_header,
- */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_header, __pyx_v_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":367
- *                        text=text,
- *                        header=header,
- *                        port=port,             # <<<<<<<<<<<<<<
- *                        check_header=check_header,
- *                        check_sq=check_sq)
- */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_port, __pyx_v_port) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":368
- *                        header=header,
- *                        port=port,
- *                        check_header=check_header,             # <<<<<<<<<<<<<<
- *                        check_sq=check_sq)
- *             return
- */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_check_header, __pyx_v_check_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":369
- *                        port=port,
- *                        check_header=check_header,
- *                        check_sq=check_sq)             # <<<<<<<<<<<<<<
- *             return
- * 
- */
-    if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_check_sq, __pyx_v_check_sq) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":361
- *                 pass
- * 
- *             self._open(filename, 'r',             # <<<<<<<<<<<<<<
- *                        template=template,
- *                        referencenames=referencenames,
- */
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_8, __pyx_t_9); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-    /* "pysam/csamfile.pyx":370
- *                        check_header=check_header,
- *                        check_sq=check_sq)
- *             return             # <<<<<<<<<<<<<<
- * 
- *         assert mode in ( "r","w","rb","wb", "wh", "wbu", "rU" ), \
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":372
- *             return
- * 
- *         assert mode in ( "r","w","rb","wb", "wh", "wbu", "rU" ), \             # <<<<<<<<<<<<<<
- *             "invalid file opening mode `%s`" % mode
- * 
- */
-  #ifndef CYTHON_WITHOUT_ASSERTIONS
-  if (unlikely(!Py_OptimizeFlag)) {
-    __Pyx_INCREF(__pyx_v_mode);
-    __pyx_t_6 = __pyx_v_mode;
-    __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (!__pyx_t_2) {
-      __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_w, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_11 = __pyx_t_1;
-    } else {
-      __pyx_t_11 = __pyx_t_2;
-    }
-    if (!__pyx_t_11) {
-      __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_rb, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_1 = __pyx_t_2;
-    } else {
-      __pyx_t_1 = __pyx_t_11;
-    }
-    if (!__pyx_t_1) {
-      __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wb, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __pyx_t_11;
-    } else {
-      __pyx_t_2 = __pyx_t_1;
-    }
-    if (!__pyx_t_2) {
-      __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wh, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_11 = __pyx_t_1;
-    } else {
-      __pyx_t_11 = __pyx_t_2;
-    }
-    if (!__pyx_t_11) {
-      __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_wbu, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_1 = __pyx_t_2;
-    } else {
-      __pyx_t_1 = __pyx_t_11;
-    }
-    if (!__pyx_t_1) {
-      __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_6, __pyx_n_s_rU, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __pyx_t_11;
-    } else {
-      __pyx_t_2 = __pyx_t_1;
-    }
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    if (unlikely(!(__pyx_t_2 != 0))) {
-
-      /* "pysam/csamfile.pyx":373
- * 
- *         assert mode in ( "r","w","rb","wb", "wh", "wbu", "rU" ), \
- *             "invalid file opening mode `%s`" % mode             # <<<<<<<<<<<<<<
- * 
- *         # close a previously opened file
- */
-      __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_invalid_file_opening_mode_s, __pyx_v_mode); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      PyErr_SetObject(PyExc_AssertionError, __pyx_t_6);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-  }
-  #endif
-
-  /* "pysam/csamfile.pyx":376
- * 
- *         # close a previously opened file
- *         if self.htsfile != NULL:             # <<<<<<<<<<<<<<
- *             self.close()
- * 
- */
-  __pyx_t_2 = ((__pyx_v_self->htsfile != NULL) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":377
- *         # close a previously opened file
- *         if self.htsfile != NULL:
- *             self.close()             # <<<<<<<<<<<<<<
- * 
- *         cdef bytes bmode = mode.encode('ascii')
- */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    goto __pyx_L14;
-  }
-  __pyx_L14:;
-
-  /* "pysam/csamfile.pyx":379
- *             self.close()
- * 
- *         cdef bytes bmode = mode.encode('ascii')             # <<<<<<<<<<<<<<
- *         self._filename = filename = _encodeFilename(filename)
- *         self.isstream = filename == b"-"
- */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_mode, __pyx_n_s_encode); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  if (!(likely(PyBytes_CheckExact(__pyx_t_6))||((__pyx_t_6) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_6)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_bmode = ((PyObject*)__pyx_t_6);
-  __pyx_t_6 = 0;
-
-  /* "pysam/csamfile.pyx":380
- * 
- *         cdef bytes bmode = mode.encode('ascii')
- *         self._filename = filename = _encodeFilename(filename)             # <<<<<<<<<<<<<<
- *         self.isstream = filename == b"-"
- * 
- */
-  __pyx_t_6 = __pyx_f_5pysam_8csamfile__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __Pyx_INCREF(__pyx_t_6);
-  __Pyx_GIVEREF(__pyx_t_6);
-  __Pyx_GOTREF(__pyx_v_self->_filename);
-  __Pyx_DECREF(__pyx_v_self->_filename);
-  __pyx_v_self->_filename = __pyx_t_6;
-  __Pyx_INCREF(__pyx_t_6);
-  __Pyx_DECREF_SET(__pyx_v_filename, __pyx_t_6);
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-  /* "pysam/csamfile.pyx":381
- *         cdef bytes bmode = mode.encode('ascii')
- *         self._filename = filename = _encodeFilename(filename)
- *         self.isstream = filename == b"-"             # <<<<<<<<<<<<<<
- * 
- *         self.isbam = len(mode) > 1 and mode[1] == 'b'
- */
-  __pyx_t_6 = PyObject_RichCompare(__pyx_v_filename, __pyx_kp_b__6, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_6); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __pyx_v_self->isstream = __pyx_t_10;
-
-  /* "pysam/csamfile.pyx":383
- *         self.isstream = filename == b"-"
- * 
- *         self.isbam = len(mode) > 1 and mode[1] == 'b'             # <<<<<<<<<<<<<<
- * 
- *         self.isremote = filename.startswith(b"http:") or \
- */
-  __pyx_t_12 = PyObject_Length(__pyx_v_mode); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_6 = __Pyx_PyBool_FromLong((__pyx_t_12 > 1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_2) {
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_mode, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_8 = PyObject_RichCompare(__pyx_t_9, __pyx_n_s_b, Py_EQ); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = __pyx_t_8;
-    __pyx_t_8 = 0;
-  } else {
-    __pyx_t_9 = __pyx_t_6;
-    __pyx_t_6 = 0;
-  }
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  __pyx_v_self->isbam = __pyx_t_10;
-
-  /* "pysam/csamfile.pyx":385
- *         self.isbam = len(mode) > 1 and mode[1] == 'b'
- * 
- *         self.isremote = filename.startswith(b"http:") or \             # <<<<<<<<<<<<<<
- *                         filename.startswith(b"ftp:")
- * 
- */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_2) {
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-    /* "pysam/csamfile.pyx":386
- * 
- *         self.isremote = filename.startswith(b"http:") or \
- *                         filename.startswith(b"ftp:")             # <<<<<<<<<<<<<<
- * 
- *         cdef char * ctext
- */
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__8, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = __pyx_t_8;
-    __pyx_t_8 = 0;
-  } else {
-    __pyx_t_9 = __pyx_t_6;
-    __pyx_t_6 = 0;
-  }
-
-  /* "pysam/csamfile.pyx":385
- *         self.isbam = len(mode) > 1 and mode[1] == 'b'
- * 
- *         self.isremote = filename.startswith(b"http:") or \             # <<<<<<<<<<<<<<
- *                         filename.startswith(b"ftp:")
- * 
- */
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  __pyx_v_self->isremote = __pyx_t_10;
-
-  /* "pysam/csamfile.pyx":389
- * 
- *         cdef char * ctext
- *         ctext = NULL             # <<<<<<<<<<<<<<
- * 
- *         if mode[0] == 'w':
- */
-  __pyx_v_ctext = NULL;
-
-  /* "pysam/csamfile.pyx":391
- *         ctext = NULL
- * 
- *         if mode[0] == 'w':             # <<<<<<<<<<<<<<
- *             # open file for writing
- * 
- */
-  __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_w, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":395
- * 
- *             # header structure (used for writing)
- *             if template:             # <<<<<<<<<<<<<<
- *                 self.header = bam_hdr_dup(template.header)
- *             elif header:
- */
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(((PyObject *)__pyx_v_template)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":396
- *             # header structure (used for writing)
- *             if template:
- *                 self.header = bam_hdr_dup(template.header)             # <<<<<<<<<<<<<<
- *             elif header:
- *                 self.header = self._buildHeader(header)
- */
-      __pyx_v_self->header = bam_hdr_dup(__pyx_v_template->header);
-      goto __pyx_L16;
-    }
-
-    /* "pysam/csamfile.pyx":397
- *             if template:
- *                 self.header = bam_hdr_dup(template.header)
- *             elif header:             # <<<<<<<<<<<<<<
- *                 self.header = self._buildHeader(header)
- *             else:
- */
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_header); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":398
- *                 self.header = bam_hdr_dup(template.header)
- *             elif header:
- *                 self.header = self._buildHeader(header)             # <<<<<<<<<<<<<<
- *             else:
- *                 # build header from a target names and lengths
- */
-      __pyx_v_self->header = ((struct __pyx_vtabstruct_5pysam_8csamfile_Samfile *)__pyx_v_self->__pyx_vtab)->_buildHeader(__pyx_v_self, __pyx_v_header);
-      goto __pyx_L16;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":401
- *             else:
- *                 # build header from a target names and lengths
- *                 assert referencenames and referencelengths, \             # <<<<<<<<<<<<<<
- *                     ("either supply options `template`, `header` "
- *                      "or  both `referencenames` and `referencelengths` "
- */
-      #ifndef CYTHON_WITHOUT_ASSERTIONS
-      if (unlikely(!Py_OptimizeFlag)) {
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_referencenames); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        if (__pyx_t_2) {
-          __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_referencelengths); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_t_11 = __pyx_t_1;
-        } else {
-          __pyx_t_11 = __pyx_t_2;
-        }
-        if (unlikely(!__pyx_t_11)) {
-          PyErr_SetObject(PyExc_AssertionError, __pyx_kp_s_either_supply_options_template_h);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-      }
-      #endif
-
-      /* "pysam/csamfile.pyx":405
- *                      "or  both `referencenames` and `referencelengths` "
- *                      "for writing")
- *                 assert len(referencenames) == len(referencelengths), \             # <<<<<<<<<<<<<<
- *                     "unequal names and lengths of reference sequences"
- * 
- */
-      #ifndef CYTHON_WITHOUT_ASSERTIONS
-      if (unlikely(!Py_OptimizeFlag)) {
-        __pyx_t_12 = PyObject_Length(__pyx_v_referencenames); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_13 = PyObject_Length(__pyx_v_referencelengths); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        if (unlikely(!((__pyx_t_12 == __pyx_t_13) != 0))) {
-          PyErr_SetObject(PyExc_AssertionError, __pyx_kp_s_unequal_names_and_lengths_of_ref);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-      }
-      #endif
-
-      /* "pysam/csamfile.pyx":409
- * 
- *                 # allocate and fill header
- *                 referencenames = [_forceBytes(ref) for ref in referencenames]             # <<<<<<<<<<<<<<
- *                 self.header = bam_hdr_init()
- *                 self.header.n_targets = len(referencenames)
- */
-      __pyx_t_9 = PyList_New(0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      if (PyList_CheckExact(__pyx_v_referencenames) || PyTuple_CheckExact(__pyx_v_referencenames)) {
-        __pyx_t_6 = __pyx_v_referencenames; __Pyx_INCREF(__pyx_t_6); __pyx_t_13 = 0;
-        __pyx_t_14 = NULL;
-      } else {
-        __pyx_t_13 = -1; __pyx_t_6 = PyObject_GetIter(__pyx_v_referencenames); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_14 = Py_TYPE(__pyx_t_6)->tp_iternext;
-      }
-      for (;;) {
-        if (!__pyx_t_14 && PyList_CheckExact(__pyx_t_6)) {
-          if (__pyx_t_13 >= PyList_GET_SIZE(__pyx_t_6)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_8 = PyList_GET_ITEM(__pyx_t_6, __pyx_t_13); __Pyx_INCREF(__pyx_t_8); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_8 = PySequence_ITEM(__pyx_t_6, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else if (!__pyx_t_14 && PyTuple_CheckExact(__pyx_t_6)) {
-          if (__pyx_t_13 >= PyTuple_GET_SIZE(__pyx_t_6)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_6, __pyx_t_13); __Pyx_INCREF(__pyx_t_8); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_8 = PySequence_ITEM(__pyx_t_6, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else {
-          __pyx_t_8 = __pyx_t_14(__pyx_t_6);
-          if (unlikely(!__pyx_t_8)) {
-            PyObject* exc_type = PyErr_Occurred();
-            if (exc_type) {
-              if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-            }
-            break;
-          }
-          __Pyx_GOTREF(__pyx_t_8);
-        }
-        __Pyx_XDECREF_SET(__pyx_v_ref, __pyx_t_8);
-        __pyx_t_8 = 0;
-        __pyx_t_8 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_ref); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        if (unlikely(__Pyx_ListComp_Append(__pyx_t_9, (PyObject*)__pyx_t_8))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      }
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __Pyx_DECREF_SET(__pyx_v_referencenames, __pyx_t_9);
-      __pyx_t_9 = 0;
-
-      /* "pysam/csamfile.pyx":410
- *                 # allocate and fill header
- *                 referencenames = [_forceBytes(ref) for ref in referencenames]
- *                 self.header = bam_hdr_init()             # <<<<<<<<<<<<<<
- *                 self.header.n_targets = len(referencenames)
- *                 n = 0
- */
-      __pyx_v_self->header = bam_hdr_init();
-
-      /* "pysam/csamfile.pyx":411
- *                 referencenames = [_forceBytes(ref) for ref in referencenames]
- *                 self.header = bam_hdr_init()
- *                 self.header.n_targets = len(referencenames)             # <<<<<<<<<<<<<<
- *                 n = 0
- *                 for x in referencenames:
- */
-      __pyx_t_13 = PyObject_Length(__pyx_v_referencenames); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_v_self->header->n_targets = __pyx_t_13;
-
-      /* "pysam/csamfile.pyx":412
- *                 self.header = bam_hdr_init()
- *                 self.header.n_targets = len(referencenames)
- *                 n = 0             # <<<<<<<<<<<<<<
- *                 for x in referencenames:
- *                     n += len(x) + 1
- */
-      __Pyx_INCREF(__pyx_int_0);
-      __pyx_v_n = __pyx_int_0;
-
-      /* "pysam/csamfile.pyx":413
- *                 self.header.n_targets = len(referencenames)
- *                 n = 0
- *                 for x in referencenames:             # <<<<<<<<<<<<<<
- *                     n += len(x) + 1
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
- */
-      if (PyList_CheckExact(__pyx_v_referencenames) || PyTuple_CheckExact(__pyx_v_referencenames)) {
-        __pyx_t_9 = __pyx_v_referencenames; __Pyx_INCREF(__pyx_t_9); __pyx_t_13 = 0;
-        __pyx_t_14 = NULL;
-      } else {
-        __pyx_t_13 = -1; __pyx_t_9 = PyObject_GetIter(__pyx_v_referencenames); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_14 = Py_TYPE(__pyx_t_9)->tp_iternext;
-      }
-      for (;;) {
-        if (!__pyx_t_14 && PyList_CheckExact(__pyx_t_9)) {
-          if (__pyx_t_13 >= PyList_GET_SIZE(__pyx_t_9)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_6 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_13); __Pyx_INCREF(__pyx_t_6); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_6 = PySequence_ITEM(__pyx_t_9, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else if (!__pyx_t_14 && PyTuple_CheckExact(__pyx_t_9)) {
-          if (__pyx_t_13 >= PyTuple_GET_SIZE(__pyx_t_9)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_9, __pyx_t_13); __Pyx_INCREF(__pyx_t_6); __pyx_t_13++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_6 = PySequence_ITEM(__pyx_t_9, __pyx_t_13); __pyx_t_13++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else {
-          __pyx_t_6 = __pyx_t_14(__pyx_t_9);
-          if (unlikely(!__pyx_t_6)) {
-            PyObject* exc_type = PyErr_Occurred();
-            if (exc_type) {
-              if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-            }
-            break;
-          }
-          __Pyx_GOTREF(__pyx_t_6);
-        }
-        __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_6);
-        __pyx_t_6 = 0;
-
-        /* "pysam/csamfile.pyx":414
- *                 n = 0
- *                 for x in referencenames:
- *                     n += len(x) + 1             # <<<<<<<<<<<<<<
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
- */
-        __pyx_t_12 = PyObject_Length(__pyx_v_x); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_6 = PyInt_FromSsize_t((__pyx_t_12 + 1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_8 = PyNumber_InPlaceAdd(__pyx_v_n, __pyx_t_6); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __Pyx_DECREF_SET(__pyx_v_n, __pyx_t_8);
-        __pyx_t_8 = 0;
-      }
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-
-      /* "pysam/csamfile.pyx":415
- *                 for x in referencenames:
- *                     n += len(x) + 1
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))             # <<<<<<<<<<<<<<
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
- *                 for x from 0 <= x < self.header.n_targets:
- */
-      __pyx_t_15 = __Pyx_PyInt_As_size_t(__pyx_v_n); if (unlikely((__pyx_t_15 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_v_self->header->target_name = ((char **)calloc(__pyx_t_15, (sizeof(char *))));
-
-      /* "pysam/csamfile.pyx":416
- *                     n += len(x) + 1
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))             # <<<<<<<<<<<<<<
- *                 for x from 0 <= x < self.header.n_targets:
- *                     self.header.target_len[x] = referencelengths[x]
- */
-      __pyx_t_15 = __Pyx_PyInt_As_size_t(__pyx_v_n); if (unlikely((__pyx_t_15 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_v_self->header->target_len = ((uint32_t *)calloc(__pyx_t_15, (sizeof(uint32_t))));
-
-      /* "pysam/csamfile.pyx":417
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
- *                 for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
- *                     self.header.target_len[x] = referencelengths[x]
- *                     name = referencenames[x]
- */
-      __pyx_t_16 = __pyx_v_self->header->n_targets;
-      for (__pyx_t_17 = 0; __pyx_t_17 < __pyx_t_16; __pyx_t_17++) {
-        __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
-        __pyx_t_9 = 0;
-
-        /* "pysam/csamfile.pyx":418
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
- *                 for x from 0 <= x < self.header.n_targets:
- *                     self.header.target_len[x] = referencelengths[x]             # <<<<<<<<<<<<<<
- *                     name = referencenames[x]
- *                     self.header.target_name[x] = <char*>calloc(
- */
-        __pyx_t_9 = PyObject_GetItem(__pyx_v_referencelengths, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_18 = __Pyx_PyInt_As_uint32_t(__pyx_t_9); if (unlikely((__pyx_t_18 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_13 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_13 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        (__pyx_v_self->header->target_len[__pyx_t_13]) = __pyx_t_18;
-
-        /* "pysam/csamfile.pyx":419
- *                 for x from 0 <= x < self.header.n_targets:
- *                     self.header.target_len[x] = referencelengths[x]
- *                     name = referencenames[x]             # <<<<<<<<<<<<<<
- *                     self.header.target_name[x] = <char*>calloc(
- *                         len(name) + 1, sizeof(char))
- */
-        __pyx_t_9 = PyObject_GetItem(__pyx_v_referencenames, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 419; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_XDECREF_SET(__pyx_v_name, __pyx_t_9);
-        __pyx_t_9 = 0;
-
-        /* "pysam/csamfile.pyx":421
- *                     name = referencenames[x]
- *                     self.header.target_name[x] = <char*>calloc(
- *                         len(name) + 1, sizeof(char))             # <<<<<<<<<<<<<<
- *                     strncpy(self.header.target_name[x], name, len(name))
- * 
- */
-        __pyx_t_13 = PyObject_Length(__pyx_v_name); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-        /* "pysam/csamfile.pyx":420
- *                     self.header.target_len[x] = referencelengths[x]
- *                     name = referencenames[x]
- *                     self.header.target_name[x] = <char*>calloc(             # <<<<<<<<<<<<<<
- *                         len(name) + 1, sizeof(char))
- *                     strncpy(self.header.target_name[x], name, len(name))
- */
-        __pyx_t_12 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_12 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        (__pyx_v_self->header->target_name[__pyx_t_12]) = ((char *)calloc((__pyx_t_13 + 1), (sizeof(char))));
-
-        /* "pysam/csamfile.pyx":422
- *                     self.header.target_name[x] = <char*>calloc(
- *                         len(name) + 1, sizeof(char))
- *                     strncpy(self.header.target_name[x], name, len(name))             # <<<<<<<<<<<<<<
- * 
- *                 # Optionally, if there is no text, add a SAM
- */
-        __pyx_t_13 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_13 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_19 = __Pyx_PyObject_AsString(__pyx_v_name); if (unlikely((!__pyx_t_19) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_12 = PyObject_Length(__pyx_v_name); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        strncpy((__pyx_v_self->header->target_name[__pyx_t_13]), __pyx_t_19, __pyx_t_12);
-        __pyx_t_17 = __Pyx_PyInt_As_long(__pyx_v_x); if (unlikely((__pyx_t_17 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-
-      /* "pysam/csamfile.pyx":417
- *                 self.header.target_name = <char**>calloc(n, sizeof(char*))
- *                 self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
- *                 for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
- *                     self.header.target_len[x] = referencelengths[x]
- *                     name = referencenames[x]
- */
-      __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
-      __pyx_t_9 = 0;
-
-      /* "pysam/csamfile.pyx":426
- *                 # Optionally, if there is no text, add a SAM
- *                 # compatible header to output file.
- *                 if text is None and add_sq_text:             # <<<<<<<<<<<<<<
- *                     text = []
- *                     for x from 0 <= x < self.header.n_targets:
- */
-      __pyx_t_11 = (__pyx_v_text == Py_None);
-      if (__pyx_t_11) {
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_add_sq_text); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_1 = __pyx_t_2;
-      } else {
-        __pyx_t_1 = __pyx_t_11;
-      }
-      if (__pyx_t_1) {
-
-        /* "pysam/csamfile.pyx":427
- *                 # compatible header to output file.
- *                 if text is None and add_sq_text:
- *                     text = []             # <<<<<<<<<<<<<<
- *                     for x from 0 <= x < self.header.n_targets:
- *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
- */
-        __pyx_t_9 = PyList_New(0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF_SET(__pyx_v_text, __pyx_t_9);
-        __pyx_t_9 = 0;
-
-        /* "pysam/csamfile.pyx":428
- *                 if text is None and add_sq_text:
- *                     text = []
- *                     for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
- *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
- *                                     (_forceStr(referencenames[x]),
- */
-        __pyx_t_16 = __pyx_v_self->header->n_targets;
-        for (__pyx_t_17 = 0; __pyx_t_17 < __pyx_t_16; __pyx_t_17++) {
-          __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_9);
-          __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
-          __pyx_t_9 = 0;
-
-          /* "pysam/csamfile.pyx":430
- *                     for x from 0 <= x < self.header.n_targets:
- *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
- *                                     (_forceStr(referencenames[x]),             # <<<<<<<<<<<<<<
- *                                      referencelengths[x]))
- *                     text = ''.join(text)
- */
-          __pyx_t_9 = PyObject_GetItem(__pyx_v_referencenames, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-          __Pyx_GOTREF(__pyx_t_9);
-          __pyx_t_8 = __pyx_f_5pysam_8csamfile__forceStr(__pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-
-          /* "pysam/csamfile.pyx":431
- *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
- *                                     (_forceStr(referencenames[x]),
- *                                      referencelengths[x]))             # <<<<<<<<<<<<<<
- *                     text = ''.join(text)
- * 
- */
-          __pyx_t_9 = PyObject_GetItem(__pyx_v_referencelengths, __pyx_v_x); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-          __Pyx_GOTREF(__pyx_t_9);
-
-          /* "pysam/csamfile.pyx":430
- *                     for x from 0 <= x < self.header.n_targets:
- *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
- *                                     (_forceStr(referencenames[x]),             # <<<<<<<<<<<<<<
- *                                      referencelengths[x]))
- *                     text = ''.join(text)
- */
-          __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
-          __Pyx_GIVEREF(__pyx_t_8);
-          PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_t_9);
-          __Pyx_GIVEREF(__pyx_t_9);
-          __pyx_t_8 = 0;
-          __pyx_t_9 = 0;
-
-          /* "pysam/csamfile.pyx":429
- *                     text = []
- *                     for x from 0 <= x < self.header.n_targets:
- *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \             # <<<<<<<<<<<<<<
- *                                     (_forceStr(referencenames[x]),
- *                                      referencelengths[x]))
- */
-          __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_SQ_SN_s_LN_s, __pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_9);
-          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          __pyx_t_20 = __Pyx_PyObject_Append(__pyx_v_text, __pyx_t_9); if (unlikely(__pyx_t_20 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-          __pyx_t_17 = __Pyx_PyInt_As_long(__pyx_v_x); if (unlikely((__pyx_t_17 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-
-        /* "pysam/csamfile.pyx":428
- *                 if text is None and add_sq_text:
- *                     text = []
- *                     for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
- *                         text.append("@SQ\tSN:%s\tLN:%s\n" % \
- *                                     (_forceStr(referencenames[x]),
- */
-        __pyx_t_9 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
-        __pyx_t_9 = 0;
-
-        /* "pysam/csamfile.pyx":432
- *                                     (_forceStr(referencenames[x]),
- *                                      referencelengths[x]))
- *                     text = ''.join(text)             # <<<<<<<<<<<<<<
- * 
- *                 if text != None:
- */
-        __pyx_t_9 = __Pyx_PyString_Join(__pyx_kp_s__9, __pyx_v_text); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF_SET(__pyx_v_text, __pyx_t_9);
-        __pyx_t_9 = 0;
-        goto __pyx_L23;
-      }
-      __pyx_L23:;
-
-      /* "pysam/csamfile.pyx":434
- *                     text = ''.join(text)
- * 
- *                 if text != None:             # <<<<<<<<<<<<<<
- *                     # copy without \0
- *                     text = _forceBytes(text)
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_text, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_1) {
-
-        /* "pysam/csamfile.pyx":436
- *                 if text != None:
- *                     # copy without \0
- *                     text = _forceBytes(text)             # <<<<<<<<<<<<<<
- *                     ctext = text
- *                     self.header.l_text = strlen(ctext)
- */
-        __pyx_t_9 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_text); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF_SET(__pyx_v_text, __pyx_t_9);
-        __pyx_t_9 = 0;
-
-        /* "pysam/csamfile.pyx":437
- *                     # copy without \0
- *                     text = _forceBytes(text)
- *                     ctext = text             # <<<<<<<<<<<<<<
- *                     self.header.l_text = strlen(ctext)
- *                     self.header.text = <char*>calloc(
- */
-        __pyx_t_21 = __Pyx_PyObject_AsString(__pyx_v_text); if (unlikely((!__pyx_t_21) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_v_ctext = __pyx_t_21;
-
-        /* "pysam/csamfile.pyx":438
- *                     text = _forceBytes(text)
- *                     ctext = text
- *                     self.header.l_text = strlen(ctext)             # <<<<<<<<<<<<<<
- *                     self.header.text = <char*>calloc(
- *                         strlen(ctext), sizeof(char))
- */
-        __pyx_v_self->header->l_text = strlen(__pyx_v_ctext);
-
-        /* "pysam/csamfile.pyx":439
- *                     ctext = text
- *                     self.header.l_text = strlen(ctext)
- *                     self.header.text = <char*>calloc(             # <<<<<<<<<<<<<<
- *                         strlen(ctext), sizeof(char))
- *                     memcpy(self.header.text, ctext, strlen(ctext))
- */
-        __pyx_v_self->header->text = ((char *)calloc(strlen(__pyx_v_ctext), (sizeof(char))));
-
-        /* "pysam/csamfile.pyx":441
- *                     self.header.text = <char*>calloc(
- *                         strlen(ctext), sizeof(char))
- *                     memcpy(self.header.text, ctext, strlen(ctext))             # <<<<<<<<<<<<<<
- * 
- *             # open file. Header gets written to file at the same time for bam files
- */
-        memcpy(__pyx_v_self->header->text, __pyx_v_ctext, strlen(__pyx_v_ctext));
-        goto __pyx_L26;
-      }
-      __pyx_L26:;
-    }
-    __pyx_L16:;
-
-    /* "pysam/csamfile.pyx":445
- *             # open file. Header gets written to file at the same time for bam files
- *             # and sam files (in the latter case, the mode needs to be wh)
- *             self.htsfile = hts_open(filename, bmode)             # <<<<<<<<<<<<<<
- * 
- *             # for compatibility - "w" writes sam file without header
- */
-    __pyx_t_22 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_22) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_23 = __Pyx_PyObject_AsString(__pyx_v_bmode); if (unlikely((!__pyx_t_23) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_self->htsfile = hts_open(__pyx_t_22, __pyx_t_23);
-
-    /* "pysam/csamfile.pyx":448
- * 
- *             # for compatibility - "w" writes sam file without header
- *             if self.isbam or "h" in mode:             # <<<<<<<<<<<<<<
- *                 # write header to htsfile
- *                 sam_hdr_write(self.htsfile, self.header)
- */
-    if (!(__pyx_v_self->isbam != 0)) {
-      __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_n_s_h, __pyx_v_mode, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_11 = __pyx_t_1;
-    } else {
-      __pyx_t_11 = (__pyx_v_self->isbam != 0);
-    }
-    if (__pyx_t_11) {
-
-      /* "pysam/csamfile.pyx":450
- *             if self.isbam or "h" in mode:
- *                 # write header to htsfile
- *                 sam_hdr_write(self.htsfile, self.header)             # <<<<<<<<<<<<<<
- * 
- *         elif mode[0] == "r":
- */
-      sam_hdr_write(__pyx_v_self->htsfile, __pyx_v_self->header);
-      goto __pyx_L27;
-    }
-    __pyx_L27:;
-    goto __pyx_L15;
-  }
-
-  /* "pysam/csamfile.pyx":452
- *                 sam_hdr_write(self.htsfile, self.header)
- * 
- *         elif mode[0] == "r":             # <<<<<<<<<<<<<<
- *             # open file for reading
- *             if (filename != b"-"
- */
-  __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 452; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 452; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  if (__pyx_t_11) {
-
-    /* "pysam/csamfile.pyx":454
- *         elif mode[0] == "r":
- *             # open file for reading
- *             if (filename != b"-"             # <<<<<<<<<<<<<<
- *                 and not self.isremote
- *                 and not os.path.exists(filename)):
- */
-    __pyx_t_11 = (__Pyx_PyBytes_Equals(__pyx_v_filename, __pyx_kp_b__6, Py_NE)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (__pyx_t_11) {
-
-      /* "pysam/csamfile.pyx":455
- *             # open file for reading
- *             if (filename != b"-"
- *                 and not self.isremote             # <<<<<<<<<<<<<<
- *                 and not os.path.exists(filename)):
- *                 raise IOError("file `%s` not found" % filename)
- */
-      __pyx_t_1 = (!(__pyx_v_self->isremote != 0));
-      if (__pyx_t_1) {
-
-        /* "pysam/csamfile.pyx":456
- *             if (filename != b"-"
- *                 and not self.isremote
- *                 and not os.path.exists(filename)):             # <<<<<<<<<<<<<<
- *                 raise IOError("file `%s` not found" % filename)
- * 
- */
-        __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_path); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_exists); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __Pyx_INCREF(__pyx_v_filename);
-        PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_filename);
-        __Pyx_GIVEREF(__pyx_v_filename);
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_24 = (!__pyx_t_2);
-        __pyx_t_2 = __pyx_t_24;
-      } else {
-        __pyx_t_2 = __pyx_t_1;
-      }
-      __pyx_t_1 = __pyx_t_2;
-    } else {
-      __pyx_t_1 = __pyx_t_11;
-    }
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":457
- *                 and not self.isremote
- *                 and not os.path.exists(filename)):
- *                 raise IOError("file `%s` not found" % filename)             # <<<<<<<<<<<<<<
- * 
- *             # try to detect errors
- */
-      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_s_not_found, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
-      __Pyx_GIVEREF(__pyx_t_8);
-      __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":460
- * 
- *             # try to detect errors
- *             self.htsfile = hts_open(filename, bmode)             # <<<<<<<<<<<<<<
- *             if self.htsfile == NULL:
- *                 raise ValueError(
- */
-    __pyx_t_22 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_22) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 460; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_23 = __Pyx_PyObject_AsString(__pyx_v_bmode); if (unlikely((!__pyx_t_23) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 460; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_self->htsfile = hts_open(__pyx_t_22, __pyx_t_23);
-
-    /* "pysam/csamfile.pyx":461
- *             # try to detect errors
- *             self.htsfile = hts_open(filename, bmode)
- *             if self.htsfile == NULL:             # <<<<<<<<<<<<<<
- *                 raise ValueError(
- *                     "could not open file (mode='%s') - "
- */
-    __pyx_t_1 = ((__pyx_v_self->htsfile == NULL) != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":464
- *                 raise ValueError(
- *                     "could not open file (mode='%s') - "
- *                     "is it SAM/BAM format?" % mode)             # <<<<<<<<<<<<<<
- * 
- *             # get file pointer
- */
-      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_file_mode_s_is_it, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 464; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-
-      /* "pysam/csamfile.pyx":462
- *             self.htsfile = hts_open(filename, bmode)
- *             if self.htsfile == NULL:
- *                 raise ValueError(             # <<<<<<<<<<<<<<
- *                     "could not open file (mode='%s') - "
- *                     "is it SAM/BAM format?" % mode)
- */
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
-      __Pyx_GIVEREF(__pyx_t_8);
-      __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":469
- *             # TODO: this is specific to BAM files
- *             #       refactor to make generalizable
- *             self.fp = self.htsfile.fp.bgzf             # <<<<<<<<<<<<<<
- * 
- *             # bam files require a valid header
- */
-    __pyx_t_25 = __pyx_v_self->htsfile->fp.bgzf;
-    __pyx_v_self->fp = __pyx_t_25;
-
-    /* "pysam/csamfile.pyx":472
- * 
- *             # bam files require a valid header
- *             if self.isbam:             # <<<<<<<<<<<<<<
- *                 self.header = sam_hdr_read(self.htsfile)
- *                 if self.header == NULL:
- */
-    __pyx_t_1 = (__pyx_v_self->isbam != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":473
- *             # bam files require a valid header
- *             if self.isbam:
- *                 self.header = sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
- *                 if self.header == NULL:
- *                     raise ValueError(
- */
-      __pyx_v_self->header = sam_hdr_read(__pyx_v_self->htsfile);
-
-      /* "pysam/csamfile.pyx":474
- *             if self.isbam:
- *                 self.header = sam_hdr_read(self.htsfile)
- *                 if self.header == NULL:             # <<<<<<<<<<<<<<
- *                     raise ValueError(
- *                         "file does not have valid header (mode='%s') "
- */
-      __pyx_t_1 = ((__pyx_v_self->header == NULL) != 0);
-      if (__pyx_t_1) {
-
-        /* "pysam/csamfile.pyx":477
- *                     raise ValueError(
- *                         "file does not have valid header (mode='%s') "
- *                         "- is it BAM format?" % mode )             # <<<<<<<<<<<<<<
- *             else:
- *                 # in sam files it is optional (htsfile full of unmapped reads)
- */
-        __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_does_not_have_valid_header, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-
-        /* "pysam/csamfile.pyx":475
- *                 self.header = sam_hdr_read(self.htsfile)
- *                 if self.header == NULL:
- *                     raise ValueError(             # <<<<<<<<<<<<<<
- *                         "file does not have valid header (mode='%s') "
- *                         "- is it BAM format?" % mode )
- */
-        __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
-        __Pyx_GIVEREF(__pyx_t_8);
-        __pyx_t_8 = 0;
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      goto __pyx_L30;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":480
- *             else:
- *                 # in sam files it is optional (htsfile full of unmapped reads)
- *                 if check_header:             # <<<<<<<<<<<<<<
- *                     self.header = sam_hdr_read(self.htsfile)
- *                     if self.header == NULL:
- */
-      __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_check_header); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      if (__pyx_t_1) {
-
-        /* "pysam/csamfile.pyx":481
- *                 # in sam files it is optional (htsfile full of unmapped reads)
- *                 if check_header:
- *                     self.header = sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
- *                     if self.header == NULL:
- *                         raise ValueError(
- */
-        __pyx_v_self->header = sam_hdr_read(__pyx_v_self->htsfile);
-
-        /* "pysam/csamfile.pyx":482
- *                 if check_header:
- *                     self.header = sam_hdr_read(self.htsfile)
- *                     if self.header == NULL:             # <<<<<<<<<<<<<<
- *                         raise ValueError(
- *                             "file does not have valid header (mode='%s') "
- */
-        __pyx_t_1 = ((__pyx_v_self->header == NULL) != 0);
-        if (__pyx_t_1) {
-
-          /* "pysam/csamfile.pyx":485
- *                         raise ValueError(
- *                             "file does not have valid header (mode='%s') "
- *                             "- is it SAM format?" % mode )             # <<<<<<<<<<<<<<
- *                     # self.header.ignore_sam_err = True
- * 
- */
-          __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_does_not_have_valid_header_2, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-
-          /* "pysam/csamfile.pyx":483
- *                     self.header = sam_hdr_read(self.htsfile)
- *                     if self.header == NULL:
- *                         raise ValueError(             # <<<<<<<<<<<<<<
- *                             "file does not have valid header (mode='%s') "
- *                             "- is it SAM format?" % mode )
- */
-          __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
-          __Pyx_GIVEREF(__pyx_t_8);
-          __pyx_t_8 = 0;
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        goto __pyx_L32;
-      }
-      __pyx_L32:;
-    }
-    __pyx_L30:;
-
-    /* "pysam/csamfile.pyx":490
- *             # disabled for autodetection to work needs to be disabled
- *             # so that reading from sam-files without headers works
- *             if check_sq and self.header.n_targets == 0:             # <<<<<<<<<<<<<<
- *                 raise ValueError(
- *                     ("file header is empty (mode='%s') - "
- */
-    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_check_sq); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (__pyx_t_1) {
-      __pyx_t_11 = (__pyx_v_self->header->n_targets == 0);
-      __pyx_t_2 = __pyx_t_11;
-    } else {
-      __pyx_t_2 = __pyx_t_1;
-    }
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":493
- *                 raise ValueError(
- *                     ("file header is empty (mode='%s') - "
- *                      "is it SAM/BAM format?") % mode)             # <<<<<<<<<<<<<<
- * 
- *         if self.htsfile == NULL:
- */
-      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_file_header_is_empty_mode_s_is_i, __pyx_v_mode); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 493; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-
-      /* "pysam/csamfile.pyx":491
- *             # so that reading from sam-files without headers works
- *             if check_sq and self.header.n_targets == 0:
- *                 raise ValueError(             # <<<<<<<<<<<<<<
- *                     ("file header is empty (mode='%s') - "
- *                      "is it SAM/BAM format?") % mode)
- */
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
-      __Pyx_GIVEREF(__pyx_t_8);
-      __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-    goto __pyx_L15;
-  }
-  __pyx_L15:;
-
-  /* "pysam/csamfile.pyx":495
- *                      "is it SAM/BAM format?") % mode)
- * 
- *         if self.htsfile == NULL:             # <<<<<<<<<<<<<<
- *             raise IOError("could not open file `%s`" % filename )
- * 
- */
-  __pyx_t_2 = ((__pyx_v_self->htsfile == NULL) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":496
- * 
- *         if self.htsfile == NULL:
- *             raise IOError("could not open file `%s`" % filename )             # <<<<<<<<<<<<<<
- * 
- *         # check for index and open if present
- */
-    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_file_s, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
-    __Pyx_GIVEREF(__pyx_t_8);
-    __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":499
- * 
- *         # check for index and open if present
- *         if mode[0] == "r" and self.isbam:             # <<<<<<<<<<<<<<
- * 
- *             if not self.isremote:
- */
-  __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  __Pyx_GOTREF(__pyx_t_8);
-  __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_8, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-  if (__pyx_t_2) {
-    __pyx_t_1 = (__pyx_v_self->isbam != 0);
-  } else {
-    __pyx_t_1 = __pyx_t_2;
-  }
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":501
- *         if mode[0] == "r" and self.isbam:
- * 
- *             if not self.isremote:             # <<<<<<<<<<<<<<
- *                 if not os.path.exists(filename + b".bai") \
- *                         and not os.path.exists( filename[:-4] + b".bai"):
- */
-    __pyx_t_1 = ((!(__pyx_v_self->isremote != 0)) != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":502
- * 
- *             if not self.isremote:
- *                 if not os.path.exists(filename + b".bai") \             # <<<<<<<<<<<<<<
- *                         and not os.path.exists( filename[:-4] + b".bai"):
- *                     self.index = NULL
- */
-      __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_8, __pyx_n_s_path); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_exists); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = PyNumber_Add(__pyx_v_filename, __pyx_kp_b_bai); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_6);
-      __Pyx_GIVEREF(__pyx_t_6);
-      __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_9, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_2 = ((!__pyx_t_1) != 0);
-      if (__pyx_t_2) {
-
-        /* "pysam/csamfile.pyx":503
- *             if not self.isremote:
- *                 if not os.path.exists(filename + b".bai") \
- *                         and not os.path.exists( filename[:-4] + b".bai"):             # <<<<<<<<<<<<<<
- *                     self.index = NULL
- *                 else:
- */
-        __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_path); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_exists); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_GetSlice(__pyx_v_filename, 0, -4, NULL, NULL, &__pyx_slice__10, 0, 1, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_8 = PyNumber_Add(__pyx_t_9, __pyx_kp_b_bai); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
-        __Pyx_GIVEREF(__pyx_t_8);
-        __pyx_t_8 = 0;
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_11 = ((!__pyx_t_1) != 0);
-        __pyx_t_1 = __pyx_t_11;
-      } else {
-        __pyx_t_1 = __pyx_t_2;
-      }
-      if (__pyx_t_1) {
-
-        /* "pysam/csamfile.pyx":504
- *                 if not os.path.exists(filename + b".bai") \
- *                         and not os.path.exists( filename[:-4] + b".bai"):
- *                     self.index = NULL             # <<<<<<<<<<<<<<
- *                 else:
- *                     # returns NULL if there is no index or index could not be opened
- */
-        __pyx_v_self->index = NULL;
-        goto __pyx_L38;
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":507
- *                 else:
- *                     # returns NULL if there is no index or index could not be opened
- *                     self.index = hts_idx_load(filename, HTS_FMT_BAI)             # <<<<<<<<<<<<<<
- *                     if self.index == NULL:
- *                         raise IOError("error while opening index `%s` " % filename )
- */
-        __pyx_t_26 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_26) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_v_self->index = hts_idx_load(__pyx_t_26, HTS_FMT_BAI);
-
-        /* "pysam/csamfile.pyx":508
- *                     # returns NULL if there is no index or index could not be opened
- *                     self.index = hts_idx_load(filename, HTS_FMT_BAI)
- *                     if self.index == NULL:             # <<<<<<<<<<<<<<
- *                         raise IOError("error while opening index `%s` " % filename )
- *             else:
- */
-        __pyx_t_1 = ((__pyx_v_self->index == NULL) != 0);
-        if (__pyx_t_1) {
-
-          /* "pysam/csamfile.pyx":509
- *                     self.index = hts_idx_load(filename, HTS_FMT_BAI)
- *                     if self.index == NULL:
- *                         raise IOError("error while opening index `%s` " % filename )             # <<<<<<<<<<<<<<
- *             else:
- *                 self.index = hts_idx_load(filename, HTS_FMT_BAI)
- */
-          __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_error_while_opening_index_s, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_9);
-          PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
-          __Pyx_GIVEREF(__pyx_t_8);
-          __pyx_t_8 = 0;
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-          __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-      }
-      __pyx_L38:;
-      goto __pyx_L37;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":511
- *                         raise IOError("error while opening index `%s` " % filename )
- *             else:
- *                 self.index = hts_idx_load(filename, HTS_FMT_BAI)             # <<<<<<<<<<<<<<
- *                 if self.index == NULL:
- *                     warnings.warn("unable to open index for `%s` " % filename)
- */
-      __pyx_t_26 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_26) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_v_self->index = hts_idx_load(__pyx_t_26, HTS_FMT_BAI);
-
-      /* "pysam/csamfile.pyx":512
- *             else:
- *                 self.index = hts_idx_load(filename, HTS_FMT_BAI)
- *                 if self.index == NULL:             # <<<<<<<<<<<<<<
- *                     warnings.warn("unable to open index for `%s` " % filename)
- * 
- */
-      __pyx_t_1 = ((__pyx_v_self->index == NULL) != 0);
-      if (__pyx_t_1) {
-
-        /* "pysam/csamfile.pyx":513
- *                 self.index = hts_idx_load(filename, HTS_FMT_BAI)
- *                 if self.index == NULL:
- *                     warnings.warn("unable to open index for `%s` " % filename)             # <<<<<<<<<<<<<<
- * 
- *             if not self.isstream:
- */
-        __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_warnings); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_8, __pyx_n_s_warn); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_unable_to_open_index_for_s, __pyx_v_filename); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_8);
-        __Pyx_GIVEREF(__pyx_t_8);
-        __pyx_t_8 = 0;
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        goto __pyx_L40;
-      }
-      __pyx_L40:;
-    }
-    __pyx_L37:;
-
-    /* "pysam/csamfile.pyx":515
- *                     warnings.warn("unable to open index for `%s` " % filename)
- * 
- *             if not self.isstream:             # <<<<<<<<<<<<<<
- *                 self.start_offset = bgzf_tell(self.fp)
- * 
- */
-    __pyx_t_1 = ((!(__pyx_v_self->isstream != 0)) != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":516
- * 
- *             if not self.isstream:
- *                 self.start_offset = bgzf_tell(self.fp)             # <<<<<<<<<<<<<<
- * 
- *     def gettid( self, reference ):
- */
-      __pyx_v_self->start_offset = bgzf_tell(__pyx_v_self->fp);
-      goto __pyx_L41;
-    }
-    __pyx_L41:;
-    goto __pyx_L36;
-  }
-  __pyx_L36:;
-
-  /* "pysam/csamfile.pyx":327
- *         return self.index != NULL
- * 
- *     def _open(self,             # <<<<<<<<<<<<<<
- *               filename,
- *               mode=None,
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_msg);
-  __Pyx_XDECREF(__pyx_v_bmode);
-  __Pyx_XDECREF(__pyx_v_n);
-  __Pyx_XDECREF(__pyx_v_x);
-  __Pyx_XDECREF(__pyx_v_name);
-  __Pyx_XDECREF(__pyx_v_ref);
-  __Pyx_XDECREF(__pyx_v_filename);
-  __Pyx_XDECREF(__pyx_v_referencenames);
-  __Pyx_XDECREF(__pyx_v_text);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":518
- *                 self.start_offset = bgzf_tell(self.fp)
- * 
- *     def gettid( self, reference ):             # <<<<<<<<<<<<<<
- *         '''
- *         convert :term:`reference` name into numerical :term:`tid`
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_9gettid(PyObject *__pyx_v_self, PyObject *__pyx_v_reference); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_8gettid[] = "Samfile.gettid(self, reference)\n\n        convert :term:`reference` name into numerical :term:`tid`\n\n        returns -1 if reference is not known.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_9gettid(PyObject *__pyx_v_self, PyObject *__pyx_v_reference) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("gettid (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_8gettid(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((PyObject *)__pyx_v_reference));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8gettid(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  char const *__pyx_t_5;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("gettid", 0);
-  __Pyx_TraceCall("gettid", __pyx_f[0], 518);
-  __Pyx_INCREF(__pyx_v_reference);
-
-  /* "pysam/csamfile.pyx":524
- *         returns -1 if reference is not known.
- *         '''
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
- *         reference = _forceBytes(reference)
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":525
- *         '''
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *         reference = _forceBytes(reference)
- *         return bam_name2id(self.header, reference)
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__11, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":526
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )
- *         reference = _forceBytes(reference)             # <<<<<<<<<<<<<<
- *         return bam_name2id(self.header, reference)
- * 
- */
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_reference); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 526; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF_SET(__pyx_v_reference, __pyx_t_2);
-  __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":527
- *             raise ValueError( "I/O operation on closed file" )
- *         reference = _forceBytes(reference)
- *         return bam_name2id(self.header, reference)             # <<<<<<<<<<<<<<
- * 
- *     def getrname( self, tid ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_reference); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyInt_From_int(bam_name2id(__pyx_v_self->header, __pyx_t_5)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":518
- *                 self.start_offset = bgzf_tell(self.fp)
- * 
- *     def gettid( self, reference ):             # <<<<<<<<<<<<<<
- *         '''
- *         convert :term:`reference` name into numerical :term:`tid`
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.gettid", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_reference);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":529
- *         return bam_name2id(self.header, reference)
- * 
- *     def getrname( self, tid ):             # <<<<<<<<<<<<<<
- *         '''
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_11getrname(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_10getrname[] = "Samfile.getrname(self, tid)\n\n        convert numerical :term:`tid` into :term:`reference` name.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_11getrname(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("getrname (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_10getrname(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((PyObject *)__pyx_v_tid));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10getrname(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_tid) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  Py_ssize_t __pyx_t_5;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("getrname", 0);
-  __Pyx_TraceCall("getrname", __pyx_f[0], 529);
-
-  /* "pysam/csamfile.pyx":532
- *         '''
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
- *         if not 0 <= tid < self.header.n_targets:
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 532; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 532; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 532; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":533
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__12, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":534
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )
- *         if not 0 <= tid < self.header.n_targets:             # <<<<<<<<<<<<<<
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- *                              (tid, self.header.n_targets ) )
- */
-  __pyx_t_2 = PyObject_RichCompare(__pyx_int_0, __pyx_v_tid, Py_LE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_PyObject_IsTrue(__pyx_t_2)) {
-    __Pyx_DECREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyObject_RichCompare(__pyx_v_tid, __pyx_t_1, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  }
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_3 = ((!__pyx_t_4) != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":536
- *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- *                              (tid, self.header.n_targets ) )             # <<<<<<<<<<<<<<
- *         return _charptr_to_str(self.header.target_name[tid])
- * 
- */
-    __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_INCREF(__pyx_v_tid);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_tid);
-    __Pyx_GIVEREF(__pyx_v_tid);
-    PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_2);
-    __Pyx_GIVEREF(__pyx_t_2);
-    __pyx_t_2 = 0;
-
-    /* "pysam/csamfile.pyx":535
- *             raise ValueError( "I/O operation on closed file" )
- *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %             # <<<<<<<<<<<<<<
- *                              (tid, self.header.n_targets ) )
- *         return _charptr_to_str(self.header.target_name[tid])
- */
-    __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_tid_i_out_of_range_0_tid_i, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
-    __Pyx_GIVEREF(__pyx_t_2);
-    __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":537
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- *                              (tid, self.header.n_targets ) )
- *         return _charptr_to_str(self.header.target_name[tid])             # <<<<<<<<<<<<<<
- * 
- *     cdef char * _getrname( self, int tid ): # TODO unused
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_5 = __Pyx_PyIndex_AsSsize_t(__pyx_v_tid); if (unlikely((__pyx_t_5 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile__charptr_to_str((__pyx_v_self->header->target_name[__pyx_t_5])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":529
- *         return bam_name2id(self.header, reference)
- * 
- *     def getrname( self, tid ):             # <<<<<<<<<<<<<<
- *         '''
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.getrname", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":539
- *         return _charptr_to_str(self.header.target_name[tid])
- * 
- *     cdef char * _getrname( self, int tid ): # TODO unused             # <<<<<<<<<<<<<<
- *         '''
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- */
-
-static char *__pyx_f_5pysam_8csamfile_7Samfile__getrname(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, int __pyx_v_tid) {
-  char *__pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_getrname", 0);
-  __Pyx_TraceCall("_getrname", __pyx_f[0], 539);
-
-  /* "pysam/csamfile.pyx":542
- *         '''
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError("I/O operation on closed file")
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":543
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- *         if not self._isOpen():
- *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
- * 
- *         if not 0 <= tid < self.header.n_targets:
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__13, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":545
- *             raise ValueError("I/O operation on closed file")
- * 
- *         if not 0 <= tid < self.header.n_targets:             # <<<<<<<<<<<<<<
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- *                              (tid, self.header.n_targets ))
- */
-  __pyx_t_4 = (0 <= __pyx_v_tid);
-  if (__pyx_t_4) {
-    __pyx_t_4 = (__pyx_v_tid < __pyx_v_self->header->n_targets);
-  }
-  __pyx_t_3 = ((!(__pyx_t_4 != 0)) != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":547
- *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- *                              (tid, self.header.n_targets ))             # <<<<<<<<<<<<<<
- *         return self.header.target_name[tid]
- * 
- */
-    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_2);
-    __Pyx_GIVEREF(__pyx_t_2);
-    PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_2 = 0;
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":546
- * 
- *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %             # <<<<<<<<<<<<<<
- *                              (tid, self.header.n_targets ))
- *         return self.header.target_name[tid]
- */
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_tid_i_out_of_range_0_tid_i, __pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":548
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- *                              (tid, self.header.n_targets ))
- *         return self.header.target_name[tid]             # <<<<<<<<<<<<<<
- * 
- *     def _parseRegion(self,
- */
-  __pyx_r = (__pyx_v_self->header->target_name[__pyx_v_tid]);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":539
- *         return _charptr_to_str(self.header.target_name[tid])
- * 
- *     cdef char * _getrname( self, int tid ): # TODO unused             # <<<<<<<<<<<<<<
- *         '''
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_WriteUnraisable("pysam.csamfile.Samfile._getrname", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":550
- *         return self.header.target_name[tid]
- * 
- *     def _parseRegion(self,             # <<<<<<<<<<<<<<
- *                      reference=None,
- *                      start=None,
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_12_parseRegion[] = "Samfile._parseRegion(self, reference=None, start=None, end=None, region=None, tid=None)\n\n        parse region information.\n\n        raise ValueError for for invalid regions.\n\n        returns a tuple of flag, tid, start and end. Flag indicates\n        whether some coordinates were supplied.\n\n        Note that regions are 1-based, while start,end are python coordinates.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_reference = 0;
-  PyObject *__pyx_v_start = 0;
-  PyObject *__pyx_v_end = 0;
-  PyObject *__pyx_v_region = 0;
-  PyObject *__pyx_v_tid = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("_parseRegion (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,&__pyx_n_s_tid,0};
-    PyObject* values[5] = {0,0,0,0,0};
-
-    /* "pysam/csamfile.pyx":551
- * 
- *     def _parseRegion(self,
- *                      reference=None,             # <<<<<<<<<<<<<<
- *                      start=None,
- *                      end=None,
- */
-    values[0] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":552
- *     def _parseRegion(self,
- *                      reference=None,
- *                      start=None,             # <<<<<<<<<<<<<<
- *                      end=None,
- *                      region=None,
- */
-    values[1] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":553
- *                      reference=None,
- *                      start=None,
- *                      end=None,             # <<<<<<<<<<<<<<
- *                      region=None,
- *                      tid=None):
- */
-    values[2] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":554
- *                      start=None,
- *                      end=None,
- *                      region=None,             # <<<<<<<<<<<<<<
- *                      tid=None):
- *         '''
- */
-    values[3] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":555
- *                      end=None,
- *                      region=None,
- *                      tid=None):             # <<<<<<<<<<<<<<
- *         '''
- *         parse region information.
- */
-    values[4] = ((PyObject *)Py_None);
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reference);
-          if (value) { values[0] = value; kw_args--; }
-        }
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_start);
-          if (value) { values[1] = value; kw_args--; }
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end);
-          if (value) { values[2] = value; kw_args--; }
-        }
-        case  3:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_region);
-          if (value) { values[3] = value; kw_args--; }
-        }
-        case  4:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_tid);
-          if (value) { values[4] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_parseRegion") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_reference = values[0];
-    __pyx_v_start = values[1];
-    __pyx_v_end = values[2];
-    __pyx_v_region = values[3];
-    __pyx_v_tid = values[4];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_parseRegion", 0, 0, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_tid);
-
-  /* "pysam/csamfile.pyx":550
- *         return self.header.target_name[tid]
- * 
- *     def _parseRegion(self,             # <<<<<<<<<<<<<<
- *                      reference=None,
- *                      start=None,
- */
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12_parseRegion(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid) {
-  int __pyx_v_rtid;
-  PY_LONG_LONG __pyx_v_rstart;
-  PY_LONG_LONG __pyx_v_rend;
-  PyObject *__pyx_v_parts = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  PY_LONG_LONG __pyx_t_6;
-  int __pyx_t_7;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  PyObject *__pyx_t_11 = NULL;
-  Py_ssize_t __pyx_t_12;
-  int __pyx_t_13;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_parseRegion", 0);
-  __Pyx_TraceCall("_parseRegion", __pyx_f[0], 550);
-  __Pyx_INCREF(__pyx_v_reference);
-  __Pyx_INCREF(__pyx_v_region);
-
-  /* "pysam/csamfile.pyx":570
- *         cdef long long rend
- * 
- *         rtid = -1             # <<<<<<<<<<<<<<
- *         rstart = 0
- *         rend = max_pos
- */
-  __pyx_v_rtid = -1;
-
-  /* "pysam/csamfile.pyx":571
- * 
- *         rtid = -1
- *         rstart = 0             # <<<<<<<<<<<<<<
- *         rend = max_pos
- *         if start != None:
- */
-  __pyx_v_rstart = 0;
-
-  /* "pysam/csamfile.pyx":572
- *         rtid = -1
- *         rstart = 0
- *         rend = max_pos             # <<<<<<<<<<<<<<
- *         if start != None:
- *             try:
- */
-  __pyx_v_rend = __pyx_v_5pysam_8csamfile_max_pos;
-
-  /* "pysam/csamfile.pyx":573
- *         rstart = 0
- *         rend = max_pos
- *         if start != None:             # <<<<<<<<<<<<<<
- *             try:
- *                 rstart = start
- */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_start, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":574
- *         rend = max_pos
- *         if start != None:
- *             try:             # <<<<<<<<<<<<<<
- *                 rstart = start
- *             except OverflowError:
- */
-    {
-      __Pyx_ExceptionSave(&__pyx_t_3, &__pyx_t_4, &__pyx_t_5);
-      __Pyx_XGOTREF(__pyx_t_3);
-      __Pyx_XGOTREF(__pyx_t_4);
-      __Pyx_XGOTREF(__pyx_t_5);
-      /*try:*/ {
-
-        /* "pysam/csamfile.pyx":575
- *         if start != None:
- *             try:
- *                 rstart = start             # <<<<<<<<<<<<<<
- *             except OverflowError:
- *                 raise ValueError( 'start out of range (%i)' % start )
- */
-        __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_v_start); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-        __pyx_v_rstart = __pyx_t_6;
-      }
-      __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0;
-      goto __pyx_L11_try_end;
-      __pyx_L4_error:;
-      __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":576
- *             try:
- *                 rstart = start
- *             except OverflowError:             # <<<<<<<<<<<<<<
- *                 raise ValueError( 'start out of range (%i)' % start )
- * 
- */
-      __pyx_t_7 = PyErr_ExceptionMatches(__pyx_builtin_OverflowError);
-      if (__pyx_t_7) {
-        __Pyx_AddTraceback("pysam.csamfile.Samfile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_1, &__pyx_t_8, &__pyx_t_9) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_GOTREF(__pyx_t_9);
-
-        /* "pysam/csamfile.pyx":577
- *                 rstart = start
- *             except OverflowError:
- *                 raise ValueError( 'start out of range (%i)' % start )             # <<<<<<<<<<<<<<
- * 
- *         if end != None:
- */
-        __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_start_out_of_range_i, __pyx_v_start); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
-        __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
-        __Pyx_GOTREF(__pyx_t_11);
-        PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_10);
-        __Pyx_GIVEREF(__pyx_t_10);
-        __pyx_t_10 = 0;
-        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
-        __Pyx_GOTREF(__pyx_t_10);
-        __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-        __Pyx_Raise(__pyx_t_10, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        goto __pyx_L5_exception_handled;
-      }
-      goto __pyx_L6_except_error;
-      __pyx_L6_except_error:;
-      __Pyx_XGIVEREF(__pyx_t_3);
-      __Pyx_XGIVEREF(__pyx_t_4);
-      __Pyx_XGIVEREF(__pyx_t_5);
-      __Pyx_ExceptionReset(__pyx_t_3, __pyx_t_4, __pyx_t_5);
-      goto __pyx_L1_error;
-      __pyx_L5_exception_handled:;
-      __Pyx_XGIVEREF(__pyx_t_3);
-      __Pyx_XGIVEREF(__pyx_t_4);
-      __Pyx_XGIVEREF(__pyx_t_5);
-      __Pyx_ExceptionReset(__pyx_t_3, __pyx_t_4, __pyx_t_5);
-      __pyx_L11_try_end:;
-    }
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":579
- *                 raise ValueError( 'start out of range (%i)' % start )
- * 
- *         if end != None:             # <<<<<<<<<<<<<<
- *             try:
- *                 rend = end
- */
-  __pyx_t_9 = PyObject_RichCompare(__pyx_v_end, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":580
- * 
- *         if end != None:
- *             try:             # <<<<<<<<<<<<<<
- *                 rend = end
- *             except OverflowError:
- */
-    {
-      __Pyx_ExceptionSave(&__pyx_t_5, &__pyx_t_4, &__pyx_t_3);
-      __Pyx_XGOTREF(__pyx_t_5);
-      __Pyx_XGOTREF(__pyx_t_4);
-      __Pyx_XGOTREF(__pyx_t_3);
-      /*try:*/ {
-
-        /* "pysam/csamfile.pyx":581
- *         if end != None:
- *             try:
- *                 rend = end             # <<<<<<<<<<<<<<
- *             except OverflowError:
- *                 raise ValueError( 'end out of range (%i)' % end )
- */
-        __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_v_end); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L15_error;}
-        __pyx_v_rend = __pyx_t_6;
-      }
-      __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
-      goto __pyx_L22_try_end;
-      __pyx_L15_error:;
-      __Pyx_XDECREF(__pyx_t_11); __pyx_t_11 = 0;
-      __Pyx_XDECREF(__pyx_t_10); __pyx_t_10 = 0;
-      __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __Pyx_XDECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;
-
-      /* "pysam/csamfile.pyx":582
- *             try:
- *                 rend = end
- *             except OverflowError:             # <<<<<<<<<<<<<<
- *                 raise ValueError( 'end out of range (%i)' % end )
- * 
- */
-      __pyx_t_7 = PyErr_ExceptionMatches(__pyx_builtin_OverflowError);
-      if (__pyx_t_7) {
-        __Pyx_AddTraceback("pysam.csamfile.Samfile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_8, &__pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_GOTREF(__pyx_t_1);
-
-        /* "pysam/csamfile.pyx":583
- *                 rend = end
- *             except OverflowError:
- *                 raise ValueError( 'end out of range (%i)' % end )             # <<<<<<<<<<<<<<
- * 
- *         if region:
- */
-        __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_end_out_of_range_i, __pyx_v_end); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
-        __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
-        __Pyx_GOTREF(__pyx_t_11);
-        PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_10);
-        __Pyx_GIVEREF(__pyx_t_10);
-        __pyx_t_10 = 0;
-        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
-        __Pyx_GOTREF(__pyx_t_10);
-        __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-        __Pyx_Raise(__pyx_t_10, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L17_except_error;}
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        goto __pyx_L16_exception_handled;
-      }
-      goto __pyx_L17_except_error;
-      __pyx_L17_except_error:;
-      __Pyx_XGIVEREF(__pyx_t_5);
-      __Pyx_XGIVEREF(__pyx_t_4);
-      __Pyx_XGIVEREF(__pyx_t_3);
-      __Pyx_ExceptionReset(__pyx_t_5, __pyx_t_4, __pyx_t_3);
-      goto __pyx_L1_error;
-      __pyx_L16_exception_handled:;
-      __Pyx_XGIVEREF(__pyx_t_5);
-      __Pyx_XGIVEREF(__pyx_t_4);
-      __Pyx_XGIVEREF(__pyx_t_3);
-      __Pyx_ExceptionReset(__pyx_t_5, __pyx_t_4, __pyx_t_3);
-      __pyx_L22_try_end:;
-    }
-    goto __pyx_L14;
-  }
-  __pyx_L14:;
-
-  /* "pysam/csamfile.pyx":585
- *                 raise ValueError( 'end out of range (%i)' % end )
- * 
- *         if region:             # <<<<<<<<<<<<<<
- *             region = _forceStr(region)
- *             parts = re.split("[:-]", region)
- */
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_region); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":586
- * 
- *         if region:
- *             region = _forceStr(region)             # <<<<<<<<<<<<<<
- *             parts = re.split("[:-]", region)
- *             reference = parts[0]
- */
-    __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceStr(__pyx_v_region); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF_SET(__pyx_v_region, __pyx_t_1);
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":587
- *         if region:
- *             region = _forceStr(region)
- *             parts = re.split("[:-]", region)             # <<<<<<<<<<<<<<
- *             reference = parts[0]
- *             if len(parts) >= 2:
- */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_INCREF(__pyx_kp_s__14);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_kp_s__14);
-    __Pyx_GIVEREF(__pyx_kp_s__14);
-    __Pyx_INCREF(__pyx_v_region);
-    PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_region);
-    __Pyx_GIVEREF(__pyx_v_region);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_v_parts = __pyx_t_9;
-    __pyx_t_9 = 0;
-
-    /* "pysam/csamfile.pyx":588
- *             region = _forceStr(region)
- *             parts = re.split("[:-]", region)
- *             reference = parts[0]             # <<<<<<<<<<<<<<
- *             if len(parts) >= 2:
- *                 rstart = int(parts[1]) - 1
- */
-    __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_parts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF_SET(__pyx_v_reference, __pyx_t_9);
-    __pyx_t_9 = 0;
-
-    /* "pysam/csamfile.pyx":589
- *             parts = re.split("[:-]", region)
- *             reference = parts[0]
- *             if len(parts) >= 2:             # <<<<<<<<<<<<<<
- *                 rstart = int(parts[1]) - 1
- *             if len(parts) >= 3:
- */
-    __pyx_t_12 = PyObject_Length(__pyx_v_parts); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_2 = ((__pyx_t_12 >= 2) != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":590
- *             reference = parts[0]
- *             if len(parts) >= 2:
- *                 rstart = int(parts[1]) - 1             # <<<<<<<<<<<<<<
- *             if len(parts) >= 3:
- *                 rend = int(parts[2])
- */
-      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_parts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_1 = PyNumber_Int(__pyx_t_9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_9 = PyNumber_Subtract(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_t_9); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_v_rstart = __pyx_t_6;
-      goto __pyx_L26;
-    }
-    __pyx_L26:;
-
-    /* "pysam/csamfile.pyx":591
- *             if len(parts) >= 2:
- *                 rstart = int(parts[1]) - 1
- *             if len(parts) >= 3:             # <<<<<<<<<<<<<<
- *                 rend = int(parts[2])
- * 
- */
-    __pyx_t_12 = PyObject_Length(__pyx_v_parts); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_2 = ((__pyx_t_12 >= 3) != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":592
- *                 rstart = int(parts[1]) - 1
- *             if len(parts) >= 3:
- *                 rend = int(parts[2])             # <<<<<<<<<<<<<<
- * 
- *         if not reference:
- */
-      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_parts, 2, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_1 = PyNumber_Int(__pyx_t_9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_6 = __Pyx_PyInt_As_PY_LONG_LONG(__pyx_t_1); if (unlikely((__pyx_t_6 == (PY_LONG_LONG)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_v_rend = __pyx_t_6;
-      goto __pyx_L27;
-    }
-    __pyx_L27:;
-    goto __pyx_L25;
-  }
-  __pyx_L25:;
-
-  /* "pysam/csamfile.pyx":594
- *                 rend = int(parts[2])
- * 
- *         if not reference:             # <<<<<<<<<<<<<<
- *             return 0, 0, 0, 0
- * 
- */
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_reference); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_13 = ((!__pyx_t_2) != 0);
-  if (__pyx_t_13) {
-
-    /* "pysam/csamfile.pyx":595
- * 
- *         if not reference:
- *             return 0, 0, 0, 0             # <<<<<<<<<<<<<<
- * 
- *         if tid is not None:
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_tuple__15);
-    __pyx_r = __pyx_tuple__15;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":597
- *             return 0, 0, 0, 0
- * 
- *         if tid is not None:             # <<<<<<<<<<<<<<
- *             rtid = tid
- *         else:
- */
-  __pyx_t_13 = (__pyx_v_tid != Py_None);
-  __pyx_t_2 = (__pyx_t_13 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":598
- * 
- *         if tid is not None:
- *             rtid = tid             # <<<<<<<<<<<<<<
- *         else:
- *             rtid = self.gettid(reference)
- */
-    __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_v_tid); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_rtid = __pyx_t_7;
-    goto __pyx_L29;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":600
- *             rtid = tid
- *         else:
- *             rtid = self.gettid(reference)             # <<<<<<<<<<<<<<
- * 
- *         if rtid < 0:
- */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_gettid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_INCREF(__pyx_v_reference);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_reference);
-    __Pyx_GIVEREF(__pyx_v_reference);
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_t_8); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    __pyx_v_rtid = __pyx_t_7;
-  }
-  __pyx_L29:;
-
-  /* "pysam/csamfile.pyx":602
- *             rtid = self.gettid(reference)
- * 
- *         if rtid < 0:             # <<<<<<<<<<<<<<
- *             raise ValueError(
- *                 "invalid reference `%s`" % reference)
- */
-  __pyx_t_2 = ((__pyx_v_rtid < 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":604
- *         if rtid < 0:
- *             raise ValueError(
- *                 "invalid reference `%s`" % reference)             # <<<<<<<<<<<<<<
- *         if rstart > rend:
- *             raise ValueError(
- */
-    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_invalid_reference_s, __pyx_v_reference); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-
-    /* "pysam/csamfile.pyx":603
- * 
- *         if rtid < 0:
- *             raise ValueError(             # <<<<<<<<<<<<<<
- *                 "invalid reference `%s`" % reference)
- *         if rstart > rend:
- */
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
-    __Pyx_GIVEREF(__pyx_t_8);
-    __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":605
- *             raise ValueError(
- *                 "invalid reference `%s`" % reference)
- *         if rstart > rend:             # <<<<<<<<<<<<<<
- *             raise ValueError(
- *                 'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))
- */
-  __pyx_t_2 = ((__pyx_v_rstart > __pyx_v_rend) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":607
- *         if rstart > rend:
- *             raise ValueError(
- *                 'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))             # <<<<<<<<<<<<<<
- *         if not 0 <= rstart < max_pos:
- *             raise ValueError('start out of range (%i)' % rstart)
- */
-    __pyx_t_8 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rstart); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_9 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rend); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_8);
-    __Pyx_GIVEREF(__pyx_t_8);
-    PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_9);
-    __Pyx_GIVEREF(__pyx_t_9);
-    __pyx_t_8 = 0;
-    __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_invalid_coordinates_start_i_end, __pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":606
- *                 "invalid reference `%s`" % reference)
- *         if rstart > rend:
- *             raise ValueError(             # <<<<<<<<<<<<<<
- *                 'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))
- *         if not 0 <= rstart < max_pos:
- */
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_9);
-    __Pyx_GIVEREF(__pyx_t_9);
-    __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __Pyx_Raise(__pyx_t_9, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":608
- *             raise ValueError(
- *                 'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))
- *         if not 0 <= rstart < max_pos:             # <<<<<<<<<<<<<<
- *             raise ValueError('start out of range (%i)' % rstart)
- *         if not 0 <= rend <= max_pos:
- */
-  __pyx_t_2 = (0 <= __pyx_v_rstart);
-  if (__pyx_t_2) {
-    __pyx_t_2 = (__pyx_v_rstart < __pyx_v_5pysam_8csamfile_max_pos);
-  }
-  __pyx_t_13 = ((!(__pyx_t_2 != 0)) != 0);
-  if (__pyx_t_13) {
-
-    /* "pysam/csamfile.pyx":609
- *                 'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))
- *         if not 0 <= rstart < max_pos:
- *             raise ValueError('start out of range (%i)' % rstart)             # <<<<<<<<<<<<<<
- *         if not 0 <= rend <= max_pos:
- *             raise ValueError('end out of range (%i)' % rend)
- */
-    __pyx_t_9 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rstart); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_start_out_of_range_i, __pyx_t_9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":610
- *         if not 0 <= rstart < max_pos:
- *             raise ValueError('start out of range (%i)' % rstart)
- *         if not 0 <= rend <= max_pos:             # <<<<<<<<<<<<<<
- *             raise ValueError('end out of range (%i)' % rend)
- * 
- */
-  __pyx_t_13 = (0 <= __pyx_v_rend);
-  if (__pyx_t_13) {
-    __pyx_t_13 = (__pyx_v_rend <= __pyx_v_5pysam_8csamfile_max_pos);
-  }
-  __pyx_t_2 = ((!(__pyx_t_13 != 0)) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":611
- *             raise ValueError('start out of range (%i)' % rstart)
- *         if not 0 <= rend <= max_pos:
- *             raise ValueError('end out of range (%i)' % rend)             # <<<<<<<<<<<<<<
- * 
- *         return 1, rtid, rstart, rend
- */
-    __pyx_t_1 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rend); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_end_out_of_range_i, __pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_9);
-    __Pyx_GIVEREF(__pyx_t_9);
-    __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __Pyx_Raise(__pyx_t_9, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":613
- *             raise ValueError('end out of range (%i)' % rend)
- * 
- *         return 1, rtid, rstart, rend             # <<<<<<<<<<<<<<
- * 
- *     def reset(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_9 = __Pyx_PyInt_From_int(__pyx_v_rtid); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_1 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rstart); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_8 = __Pyx_PyInt_From_PY_LONG_LONG(__pyx_v_rend); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_8);
-  __pyx_t_10 = PyTuple_New(4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_10);
-  __Pyx_INCREF(__pyx_int_1);
-  PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_int_1);
-  __Pyx_GIVEREF(__pyx_int_1);
-  PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_t_9);
-  __Pyx_GIVEREF(__pyx_t_9);
-  PyTuple_SET_ITEM(__pyx_t_10, 2, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_10, 3, __pyx_t_8);
-  __Pyx_GIVEREF(__pyx_t_8);
-  __pyx_t_9 = 0;
-  __pyx_t_1 = 0;
-  __pyx_t_8 = 0;
-  __pyx_r = __pyx_t_10;
-  __pyx_t_10 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":550
- *         return self.header.target_name[tid]
- * 
- *     def _parseRegion(self,             # <<<<<<<<<<<<<<
- *                      reference=None,
- *                      start=None,
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_XDECREF(__pyx_t_11);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._parseRegion", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_parts);
-  __Pyx_XDECREF(__pyx_v_reference);
-  __Pyx_XDECREF(__pyx_v_region);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":615
- *         return 1, rtid, rstart, rend
- * 
- *     def reset(self):             # <<<<<<<<<<<<<<
- *         '''reset file position to beginning of read section.'''
- *         return self.seek(self.start_offset, 0)
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_15reset(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_14reset[] = "Samfile.reset(self)\nreset file position to beginning of read section.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_15reset(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("reset (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_14reset(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_14reset(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("reset", 0);
-  __Pyx_TraceCall("reset", __pyx_f[0], 615);
-
-  /* "pysam/csamfile.pyx":617
- *     def reset(self):
- *         '''reset file position to beginning of read section.'''
- *         return self.seek(self.start_offset, 0)             # <<<<<<<<<<<<<<
- * 
- *     def seek(self, uint64_t offset, int where = 0):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_seek); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int64_t(__pyx_v_self->start_offset); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  __Pyx_INCREF(__pyx_int_0);
-  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_int_0);
-  __Pyx_GIVEREF(__pyx_int_0);
-  __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":615
- *         return 1, rtid, rstart, rend
- * 
- *     def reset(self):             # <<<<<<<<<<<<<<
- *         '''reset file position to beginning of read section.'''
- *         return self.seek(self.start_offset, 0)
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.reset", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":619
- *         return self.seek(self.start_offset, 0)
- * 
- *     def seek(self, uint64_t offset, int where = 0):             # <<<<<<<<<<<<<<
- *         '''
- *         move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`.
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_17seek(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_16seek[] = "Samfile.seek(self, uint64_t offset, int where=0)\n\n        move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_17seek(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  uint64_t __pyx_v_offset;
-  int __pyx_v_where;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("seek (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_offset,&__pyx_n_s_where,0};
-    PyObject* values[2] = {0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_offset)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_where);
-          if (value) { values[1] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "seek") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_offset = __Pyx_PyInt_As_uint64_t(values[0]); if (unlikely((__pyx_v_offset == (uint64_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    if (values[1]) {
-      __pyx_v_where = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_where == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-      __pyx_v_where = ((int)0);
-    }
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("seek", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.seek", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_16seek(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_offset, __pyx_v_where);
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_16seek(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, uint64_t __pyx_v_offset, int __pyx_v_where) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("seek", 0);
-  __Pyx_TraceCall("seek", __pyx_f[0], 619);
-
-  /* "pysam/csamfile.pyx":624
- *         '''
- * 
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
- *         if not self.isbam:
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":625
- * 
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *         if not self.isbam:
- *             raise NotImplementedError("seek only available in bam files")
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":626
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )
- *         if not self.isbam:             # <<<<<<<<<<<<<<
- *             raise NotImplementedError("seek only available in bam files")
- *         if self.isstream:
- */
-  __pyx_t_4 = ((!(__pyx_v_self->isbam != 0)) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":627
- *             raise ValueError( "I/O operation on closed file" )
- *         if not self.isbam:
- *             raise NotImplementedError("seek only available in bam files")             # <<<<<<<<<<<<<<
- *         if self.isstream:
- *             raise OSError("seek no available in streams")
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":628
- *         if not self.isbam:
- *             raise NotImplementedError("seek only available in bam files")
- *         if self.isstream:             # <<<<<<<<<<<<<<
- *             raise OSError("seek no available in streams")
- * 
- */
-  __pyx_t_4 = (__pyx_v_self->isstream != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":629
- *             raise NotImplementedError("seek only available in bam files")
- *         if self.isstream:
- *             raise OSError("seek no available in streams")             # <<<<<<<<<<<<<<
- * 
- *         return bgzf_seek(self.fp, offset, where)
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_OSError, __pyx_tuple__18, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":631
- *             raise OSError("seek no available in streams")
- * 
- *         return bgzf_seek(self.fp, offset, where)             # <<<<<<<<<<<<<<
- * 
- *     def tell(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_int64_t(bgzf_seek(__pyx_v_self->fp, __pyx_v_offset, __pyx_v_where)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":619
- *         return self.seek(self.start_offset, 0)
- * 
- *     def seek(self, uint64_t offset, int where = 0):             # <<<<<<<<<<<<<<
- *         '''
- *         move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`.
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.seek", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":633
- *         return bgzf_seek(self.fp, offset, where)
- * 
- *     def tell(self):             # <<<<<<<<<<<<<<
- *         '''
- *         return current file position
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_19tell(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_18tell[] = "Samfile.tell(self)\n\n        return current file position\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_19tell(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("tell (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_18tell(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_18tell(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("tell", 0);
-  __Pyx_TraceCall("tell", __pyx_f[0], 633);
-
-  /* "pysam/csamfile.pyx":637
- *         return current file position
- *         '''
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
- *         if not self.isbam:
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":638
- *         '''
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *         if not self.isbam:
- *             raise NotImplementedError("seek only available in bam files")
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__19, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":639
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )
- *         if not self.isbam:             # <<<<<<<<<<<<<<
- *             raise NotImplementedError("seek only available in bam files")
- * 
- */
-  __pyx_t_4 = ((!(__pyx_v_self->isbam != 0)) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":640
- *             raise ValueError( "I/O operation on closed file" )
- *         if not self.isbam:
- *             raise NotImplementedError("seek only available in bam files")             # <<<<<<<<<<<<<<
- * 
- *         return bgzf_tell(self.fp)
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__20, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":642
- *             raise NotImplementedError("seek only available in bam files")
- * 
- *         return bgzf_tell(self.fp)             # <<<<<<<<<<<<<<
- * 
- *     def fetch(self,
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_int64_t(bgzf_tell(__pyx_v_self->fp)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":633
- *         return bgzf_seek(self.fp, offset, where)
- * 
- *     def tell(self):             # <<<<<<<<<<<<<<
- *         '''
- *         return current file position
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.tell", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":644
- *         return bgzf_tell(self.fp)
- * 
- *     def fetch(self,             # <<<<<<<<<<<<<<
- *               reference=None,
- *               start=None,
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_20fetch[] = "Samfile.fetch(self, reference=None, start=None, end=None, region=None, tid=None, callback=None, until_eof=False, reopen=True)\nfetch aligned reads in a :term:`region` using 0-based indexing. The\n        region is specified by :term:`reference`, *start* and\n        *end*. Alternatively, a samtools :term:`region` string can be\n        supplied.\n\n        Without *reference* or *region* all mapped reads will be\n        fetche [...]
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_21fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_reference = 0;
-  PyObject *__pyx_v_start = 0;
-  PyObject *__pyx_v_end = 0;
-  PyObject *__pyx_v_region = 0;
-  PyObject *__pyx_v_tid = 0;
-  PyObject *__pyx_v_callback = 0;
-  PyObject *__pyx_v_until_eof = 0;
-  PyObject *__pyx_v_reopen = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("fetch (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,&__pyx_n_s_tid,&__pyx_n_s_callback,&__pyx_n_s_until_eof,&__pyx_n_s_reopen,0};
-    PyObject* values[8] = {0,0,0,0,0,0,0,0};
-
-    /* "pysam/csamfile.pyx":645
- * 
- *     def fetch(self,
- *               reference=None,             # <<<<<<<<<<<<<<
- *               start=None,
- *               end=None,
- */
-    values[0] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":646
- *     def fetch(self,
- *               reference=None,
- *               start=None,             # <<<<<<<<<<<<<<
- *               end=None,
- *               region=None,
- */
-    values[1] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":647
- *               reference=None,
- *               start=None,
- *               end=None,             # <<<<<<<<<<<<<<
- *               region=None,
- *               tid=None,
- */
-    values[2] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":648
- *               start=None,
- *               end=None,
- *               region=None,             # <<<<<<<<<<<<<<
- *               tid=None,
- *               callback=None,
- */
-    values[3] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":649
- *               end=None,
- *               region=None,
- *               tid=None,             # <<<<<<<<<<<<<<
- *               callback=None,
- *               until_eof=False,
- */
-    values[4] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":650
- *               region=None,
- *               tid=None,
- *               callback=None,             # <<<<<<<<<<<<<<
- *               until_eof=False,
- *               reopen=True):
- */
-    values[5] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":651
- *               tid=None,
- *               callback=None,
- *               until_eof=False,             # <<<<<<<<<<<<<<
- *               reopen=True):
- *         '''fetch aligned reads in a :term:`region` using 0-based indexing. The
- */
-    values[6] = ((PyObject *)Py_False);
-
-    /* "pysam/csamfile.pyx":652
- *               callback=None,
- *               until_eof=False,
- *               reopen=True):             # <<<<<<<<<<<<<<
- *         '''fetch aligned reads in a :term:`region` using 0-based indexing. The
- *         region is specified by :term:`reference`, *start* and
- */
-    values[7] = ((PyObject *)Py_True);
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  8: values[7] = PyTuple_GET_ITEM(__pyx_args, 7);
-        case  7: values[6] = PyTuple_GET_ITEM(__pyx_args, 6);
-        case  6: values[5] = PyTuple_GET_ITEM(__pyx_args, 5);
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reference);
-          if (value) { values[0] = value; kw_args--; }
-        }
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_start);
-          if (value) { values[1] = value; kw_args--; }
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end);
-          if (value) { values[2] = value; kw_args--; }
-        }
-        case  3:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_region);
-          if (value) { values[3] = value; kw_args--; }
-        }
-        case  4:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_tid);
-          if (value) { values[4] = value; kw_args--; }
-        }
-        case  5:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_callback);
-          if (value) { values[5] = value; kw_args--; }
-        }
-        case  6:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_until_eof);
-          if (value) { values[6] = value; kw_args--; }
-        }
-        case  7:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
-          if (value) { values[7] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "fetch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  8: values[7] = PyTuple_GET_ITEM(__pyx_args, 7);
-        case  7: values[6] = PyTuple_GET_ITEM(__pyx_args, 6);
-        case  6: values[5] = PyTuple_GET_ITEM(__pyx_args, 5);
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_reference = values[0];
-    __pyx_v_start = values[1];
-    __pyx_v_end = values[2];
-    __pyx_v_region = values[3];
-    __pyx_v_tid = values[4];
-    __pyx_v_callback = values[5];
-    __pyx_v_until_eof = values[6];
-    __pyx_v_reopen = values[7];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("fetch", 0, 0, 8, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_20fetch(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_tid, __pyx_v_callback, __pyx_v_until_eof, __pyx_v_reopen);
-
-  /* "pysam/csamfile.pyx":644
- *         return bgzf_tell(self.fp)
- * 
- *     def fetch(self,             # <<<<<<<<<<<<<<
- *               reference=None,
- *               start=None,
- */
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_20fetch(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_tid, PyObject *__pyx_v_callback, PyObject *__pyx_v_until_eof, PyObject *__pyx_v_reopen) {
-  int __pyx_v_rtid;
-  int __pyx_v_rstart;
-  int __pyx_v_rend;
-  int __pyx_v_has_coord;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *(*__pyx_t_9)(PyObject *);
-  int __pyx_t_10;
-  int __pyx_t_11;
-  int __pyx_t_12;
-  int __pyx_t_13;
-  int __pyx_t_14;
-  int __pyx_t_15;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("fetch", 0);
-  __Pyx_TraceCall("fetch", __pyx_f[0], 644);
-  __Pyx_INCREF(__pyx_v_reopen);
-
-  /* "pysam/csamfile.pyx":685
- *         cdef int rtid, rstart, rend, has_coord
- * 
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":686
- * 
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         has_coord, rtid, rstart, rend = self._parseRegion(reference,
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__21, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":688
- *             raise ValueError( "I/O operation on closed file" )
- * 
- *         has_coord, rtid, rstart, rend = self._parseRegion(reference,             # <<<<<<<<<<<<<<
- *                                                           start,
- *                                                           end,
- */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_parseRegion); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-
-  /* "pysam/csamfile.pyx":692
- *                                                           end,
- *                                                           region,
- *                                                           tid)             # <<<<<<<<<<<<<<
- * 
- *         # Turn of re-opening if htsfile is a stream
- */
-  __pyx_t_1 = PyTuple_New(5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(__pyx_v_reference);
-  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_reference);
-  __Pyx_GIVEREF(__pyx_v_reference);
-  __Pyx_INCREF(__pyx_v_start);
-  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_start);
-  __Pyx_GIVEREF(__pyx_v_start);
-  __Pyx_INCREF(__pyx_v_end);
-  PyTuple_SET_ITEM(__pyx_t_1, 2, __pyx_v_end);
-  __Pyx_GIVEREF(__pyx_v_end);
-  __Pyx_INCREF(__pyx_v_region);
-  PyTuple_SET_ITEM(__pyx_t_1, 3, __pyx_v_region);
-  __Pyx_GIVEREF(__pyx_v_region);
-  __Pyx_INCREF(__pyx_v_tid);
-  PyTuple_SET_ITEM(__pyx_t_1, 4, __pyx_v_tid);
-  __Pyx_GIVEREF(__pyx_v_tid);
-
-  /* "pysam/csamfile.pyx":688
- *             raise ValueError( "I/O operation on closed file" )
- * 
- *         has_coord, rtid, rstart, rend = self._parseRegion(reference,             # <<<<<<<<<<<<<<
- *                                                           start,
- *                                                           end,
- */
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if ((likely(PyTuple_CheckExact(__pyx_t_5))) || (PyList_CheckExact(__pyx_t_5))) {
-    PyObject* sequence = __pyx_t_5;
-    #if CYTHON_COMPILING_IN_CPYTHON
-    Py_ssize_t size = Py_SIZE(sequence);
-    #else
-    Py_ssize_t size = PySequence_Size(sequence);
-    #endif
-    if (unlikely(size != 4)) {
-      if (size > 4) __Pyx_RaiseTooManyValuesError(4);
-      else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-    #if CYTHON_COMPILING_IN_CPYTHON
-    if (likely(PyTuple_CheckExact(sequence))) {
-      __pyx_t_1 = PyTuple_GET_ITEM(sequence, 0); 
-      __pyx_t_2 = PyTuple_GET_ITEM(sequence, 1); 
-      __pyx_t_6 = PyTuple_GET_ITEM(sequence, 2); 
-      __pyx_t_7 = PyTuple_GET_ITEM(sequence, 3); 
-    } else {
-      __pyx_t_1 = PyList_GET_ITEM(sequence, 0); 
-      __pyx_t_2 = PyList_GET_ITEM(sequence, 1); 
-      __pyx_t_6 = PyList_GET_ITEM(sequence, 2); 
-      __pyx_t_7 = PyList_GET_ITEM(sequence, 3); 
-    }
-    __Pyx_INCREF(__pyx_t_1);
-    __Pyx_INCREF(__pyx_t_2);
-    __Pyx_INCREF(__pyx_t_6);
-    __Pyx_INCREF(__pyx_t_7);
-    #else
-    {
-      Py_ssize_t i;
-      PyObject** temps[4] = {&__pyx_t_1,&__pyx_t_2,&__pyx_t_6,&__pyx_t_7};
-      for (i=0; i < 4; i++) {
-        PyObject* item = PySequence_ITEM(sequence, i); if (unlikely(!item)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(item);
-        *(temps[i]) = item;
-      }
-    }
-    #endif
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  } else {
-    Py_ssize_t index = -1;
-    PyObject** temps[4] = {&__pyx_t_1,&__pyx_t_2,&__pyx_t_6,&__pyx_t_7};
-    __pyx_t_8 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_9 = Py_TYPE(__pyx_t_8)->tp_iternext;
-    for (index=0; index < 4; index++) {
-      PyObject* item = __pyx_t_9(__pyx_t_8); if (unlikely(!item)) goto __pyx_L4_unpacking_failed;
-      __Pyx_GOTREF(item);
-      *(temps[index]) = item;
-    }
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_9 = NULL;
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    goto __pyx_L5_unpacking_done;
-    __pyx_L4_unpacking_failed:;
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    __pyx_t_9 = NULL;
-    if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_L5_unpacking_done:;
-  }
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_11 = __Pyx_PyInt_As_int(__pyx_t_2); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_12 = __Pyx_PyInt_As_int(__pyx_t_6); if (unlikely((__pyx_t_12 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_7); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 688; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-  __pyx_v_has_coord = __pyx_t_10;
-  __pyx_v_rtid = __pyx_t_11;
-  __pyx_v_rstart = __pyx_t_12;
-  __pyx_v_rend = __pyx_t_13;
-
-  /* "pysam/csamfile.pyx":695
- * 
- *         # Turn of re-opening if htsfile is a stream
- *         if self.isstream:             # <<<<<<<<<<<<<<
- *             reopen = False
- * 
- */
-  __pyx_t_4 = (__pyx_v_self->isstream != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":696
- *         # Turn of re-opening if htsfile is a stream
- *         if self.isstream:
- *             reopen = False             # <<<<<<<<<<<<<<
- * 
- *         if self.isbam:
- */
-    __Pyx_INCREF(Py_False);
-    __Pyx_DECREF_SET(__pyx_v_reopen, Py_False);
-    goto __pyx_L6;
-  }
-  __pyx_L6:;
-
-  /* "pysam/csamfile.pyx":698
- *             reopen = False
- * 
- *         if self.isbam:             # <<<<<<<<<<<<<<
- *             if not until_eof and not self._hasIndex() and not self.isremote:
- *                 raise ValueError( "fetch called on bamfile without index" )
- */
-  __pyx_t_4 = (__pyx_v_self->isbam != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":699
- * 
- *         if self.isbam:
- *             if not until_eof and not self._hasIndex() and not self.isremote:             # <<<<<<<<<<<<<<
- *                 raise ValueError( "fetch called on bamfile without index" )
- * 
- */
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_until_eof); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = ((!__pyx_t_4) != 0);
-    if (__pyx_t_3) {
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_14 = ((!__pyx_t_4) != 0);
-      if (__pyx_t_14) {
-        __pyx_t_4 = ((!(__pyx_v_self->isremote != 0)) != 0);
-        __pyx_t_15 = __pyx_t_4;
-      } else {
-        __pyx_t_15 = __pyx_t_14;
-      }
-      __pyx_t_14 = __pyx_t_15;
-    } else {
-      __pyx_t_14 = __pyx_t_3;
-    }
-    if (__pyx_t_14) {
-
-      /* "pysam/csamfile.pyx":700
- *         if self.isbam:
- *             if not until_eof and not self._hasIndex() and not self.isremote:
- *                 raise ValueError( "fetch called on bamfile without index" )             # <<<<<<<<<<<<<<
- * 
- *             if has_coord:
- */
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__22, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_Raise(__pyx_t_7, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":702
- *                 raise ValueError( "fetch called on bamfile without index" )
- * 
- *             if has_coord:             # <<<<<<<<<<<<<<
- *                 return IteratorRowRegion(self, rtid, rstart, rend,
- *                                          reopen=reopen)
- */
-    __pyx_t_14 = (__pyx_v_has_coord != 0);
-    if (__pyx_t_14) {
-
-      /* "pysam/csamfile.pyx":703
- * 
- *             if has_coord:
- *                 return IteratorRowRegion(self, rtid, rstart, rend,             # <<<<<<<<<<<<<<
- *                                          reopen=reopen)
- *             else:
- */
-      __Pyx_XDECREF(__pyx_r);
-      __pyx_t_7 = __Pyx_PyInt_From_int(__pyx_v_rtid); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_5 = __Pyx_PyInt_From_int(__pyx_v_rstart); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_INCREF(((PyObject *)__pyx_v_self));
-      PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
-      __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-      PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_7);
-      __Pyx_GIVEREF(__pyx_t_7);
-      PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_t_5);
-      __Pyx_GIVEREF(__pyx_t_5);
-      PyTuple_SET_ITEM(__pyx_t_2, 3, __pyx_t_6);
-      __Pyx_GIVEREF(__pyx_t_6);
-      __pyx_t_7 = 0;
-      __pyx_t_5 = 0;
-      __pyx_t_6 = 0;
-      __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-
-      /* "pysam/csamfile.pyx":704
- *             if has_coord:
- *                 return IteratorRowRegion(self, rtid, rstart, rend,
- *                                          reopen=reopen)             # <<<<<<<<<<<<<<
- *             else:
- *                 if until_eof:
- */
-      if (PyDict_SetItem(__pyx_t_6, __pyx_n_s_reopen, __pyx_v_reopen) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-      /* "pysam/csamfile.pyx":703
- * 
- *             if has_coord:
- *                 return IteratorRowRegion(self, rtid, rstart, rend,             # <<<<<<<<<<<<<<
- *                                          reopen=reopen)
- *             else:
- */
-      __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowRegion)), __pyx_t_2, __pyx_t_6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_r = __pyx_t_5;
-      __pyx_t_5 = 0;
-      goto __pyx_L0;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":706
- *                                          reopen=reopen)
- *             else:
- *                 if until_eof:             # <<<<<<<<<<<<<<
- *                     return IteratorRowAll(self, reopen=reopen)
- *                 else:
- */
-      __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_until_eof); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      if (__pyx_t_14) {
-
-        /* "pysam/csamfile.pyx":707
- *             else:
- *                 if until_eof:
- *                     return IteratorRowAll(self, reopen=reopen)             # <<<<<<<<<<<<<<
- *                 else:
- *                     # AH: check - reason why no reopen for AllRefs?
- */
-        __Pyx_XDECREF(__pyx_r);
-        __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_5);
-        __Pyx_INCREF(((PyObject *)__pyx_v_self));
-        PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_v_self));
-        __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-        __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        if (PyDict_SetItem(__pyx_t_6, __pyx_n_s_reopen, __pyx_v_reopen) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowAll)), __pyx_t_5, __pyx_t_6); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_2);
-        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_r = __pyx_t_2;
-        __pyx_t_2 = 0;
-        goto __pyx_L0;
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":710
- *                 else:
- *                     # AH: check - reason why no reopen for AllRefs?
- *                     return IteratorRowAllRefs(self) # , reopen=reopen )             # <<<<<<<<<<<<<<
- *         else:
- *             if has_coord:
- */
-        __Pyx_XDECREF(__pyx_r);
-        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_2);
-        __Pyx_INCREF(((PyObject *)__pyx_v_self));
-        PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
-        __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-        __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowAllRefs)), __pyx_t_2, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_r = __pyx_t_6;
-        __pyx_t_6 = 0;
-        goto __pyx_L0;
-      }
-    }
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":712
- *                     return IteratorRowAllRefs(self) # , reopen=reopen )
- *         else:
- *             if has_coord:             # <<<<<<<<<<<<<<
- *                 raise ValueError ("fetching by region is not available for sam files")
- * 
- */
-    __pyx_t_14 = (__pyx_v_has_coord != 0);
-    if (__pyx_t_14) {
-
-      /* "pysam/csamfile.pyx":713
- *         else:
- *             if has_coord:
- *                 raise ValueError ("fetching by region is not available for sam files")             # <<<<<<<<<<<<<<
- * 
- *             if callback:
- */
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__23, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":715
- *                 raise ValueError ("fetching by region is not available for sam files")
- * 
- *             if callback:             # <<<<<<<<<<<<<<
- *                 raise NotImplementedError("callback not implemented yet")
- * 
- */
-    __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_callback); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (__pyx_t_14) {
-
-      /* "pysam/csamfile.pyx":716
- * 
- *             if callback:
- *                 raise NotImplementedError("callback not implemented yet")             # <<<<<<<<<<<<<<
- * 
- *             if self.header == NULL:
- */
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__24, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":718
- *                 raise NotImplementedError("callback not implemented yet")
- * 
- *             if self.header == NULL:             # <<<<<<<<<<<<<<
- *                 raise ValueError("fetch called for htsfile without header")
- * 
- */
-    __pyx_t_14 = ((__pyx_v_self->header == NULL) != 0);
-    if (__pyx_t_14) {
-
-      /* "pysam/csamfile.pyx":719
- * 
- *             if self.header == NULL:
- *                 raise ValueError("fetch called for htsfile without header")             # <<<<<<<<<<<<<<
- * 
- *             # check if targets are defined
- */
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__25, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":723
- *             # check if targets are defined
- *             # give warning, sam_read1 segfaults
- *             if self.header.n_targets == 0:             # <<<<<<<<<<<<<<
- *                 warnings.warn("fetch called for htsfile without header")
- * 
- */
-    __pyx_t_14 = ((__pyx_v_self->header->n_targets == 0) != 0);
-    if (__pyx_t_14) {
-
-      /* "pysam/csamfile.pyx":724
- *             # give warning, sam_read1 segfaults
- *             if self.header.n_targets == 0:
- *                 warnings.warn("fetch called for htsfile without header")             # <<<<<<<<<<<<<<
- * 
- *             return IteratorRowAll(self, reopen=reopen)
- */
-      __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_warnings); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_warn); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__26, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      goto __pyx_L14;
-    }
-    __pyx_L14:;
-
-    /* "pysam/csamfile.pyx":726
- *                 warnings.warn("fetch called for htsfile without header")
- * 
- *             return IteratorRowAll(self, reopen=reopen)             # <<<<<<<<<<<<<<
- * 
- *     def head(self, n):
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __Pyx_INCREF(((PyObject *)__pyx_v_self));
-    PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_v_self));
-    __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-    __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_reopen, __pyx_v_reopen) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowAll)), __pyx_t_6, __pyx_t_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_r = __pyx_t_5;
-    __pyx_t_5 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":644
- *         return bgzf_tell(self.fp)
- * 
- *     def fetch(self,             # <<<<<<<<<<<<<<
- *               reference=None,
- *               start=None,
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_reopen);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":728
- *             return IteratorRowAll(self, reopen=reopen)
- * 
- *     def head(self, n):             # <<<<<<<<<<<<<<
- *         '''
- *         return iterator over the first n alignments.
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_23head(PyObject *__pyx_v_self, PyObject *__pyx_v_n); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_22head[] = "Samfile.head(self, n)\n\n        return iterator over the first n alignments. \n\n        This is useful for inspecting the bam-file.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_23head(PyObject *__pyx_v_self, PyObject *__pyx_v_n) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("head (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_22head(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((PyObject *)__pyx_v_n));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_22head(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_n) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("head", 0);
-  __Pyx_TraceCall("head", __pyx_f[0], 728);
-
-  /* "pysam/csamfile.pyx":734
- *         This is useful for inspecting the bam-file.
- *         '''
- *         return IteratorRowHead(self, n)             # <<<<<<<<<<<<<<
- * 
- *     def mate(self,
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_self));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-  __Pyx_INCREF(__pyx_v_n);
-  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_n);
-  __Pyx_GIVEREF(__pyx_v_n);
-  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowHead)), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":728
- *             return IteratorRowAll(self, reopen=reopen)
- * 
- *     def head(self, n):             # <<<<<<<<<<<<<<
- *         '''
- *         return iterator over the first n alignments.
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.head", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":736
- *         return IteratorRowHead(self, n)
- * 
- *     def mate(self,             # <<<<<<<<<<<<<<
- *              AlignedRead read):
- *         '''return the mate of :class:`AlignedRead` *read*.
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_25mate(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_24mate[] = "Samfile.mate(self, AlignedRead read)\nreturn the mate of :class:`AlignedRead` *read*.\n\n        Throws a ValueError if read is unpaired or the mate\n        is unmapped.\n\n        .. note::\n\n            Calling this method will change the file position.\n            This might interfere with any iterators that have\n            not re-opened the file.\n\n        .. note::\n  \n           This method is too slow for high-thro [...]
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_25mate(PyObject *__pyx_v_self, PyObject *__pyx_v_read) {
-  CYTHON_UNUSED int __pyx_lineno = 0;
-  CYTHON_UNUSED const char *__pyx_filename = NULL;
-  CYTHON_UNUSED int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("mate (wrapper)", 0);
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_read), __pyx_ptype_5pysam_8csamfile_AlignedRead, 1, "read", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_24mate(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_read));
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_24mate(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read) {
-  uint32_t __pyx_v_flag;
-  int __pyx_v_x;
-  PyObject *__pyx_v_mate = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  Py_ssize_t __pyx_t_7;
-  PyObject *(*__pyx_t_8)(PyObject *);
-  int __pyx_t_9;
-  int __pyx_t_10;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("mate", 0);
-  __Pyx_TraceCall("mate", __pyx_f[0], 736);
-
-  /* "pysam/csamfile.pyx":756
- * 
- *         '''
- *         cdef uint32_t flag = read._delegate.core.flag             # <<<<<<<<<<<<<<
- * 
- *         if flag & BAM_FPAIRED == 0:
- */
-  __pyx_t_1 = __pyx_v_read->_delegate->core.flag;
-  __pyx_v_flag = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":758
- *         cdef uint32_t flag = read._delegate.core.flag
- * 
- *         if flag & BAM_FPAIRED == 0:             # <<<<<<<<<<<<<<
- *             raise ValueError("read %s: is unpaired" % (read.qname))
- *         if flag & BAM_FMUNMAP != 0:
- */
-  __pyx_t_2 = (((__pyx_v_flag & BAM_FPAIRED) == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":759
- * 
- *         if flag & BAM_FPAIRED == 0:
- *             raise ValueError("read %s: is unpaired" % (read.qname))             # <<<<<<<<<<<<<<
- *         if flag & BAM_FMUNMAP != 0:
- *             raise ValueError("mate %s: is unmapped" % (read.qname))
- */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_read), __pyx_n_s_qname); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_read_s_is_unpaired, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_Raise(__pyx_t_4, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":760
- *         if flag & BAM_FPAIRED == 0:
- *             raise ValueError("read %s: is unpaired" % (read.qname))
- *         if flag & BAM_FMUNMAP != 0:             # <<<<<<<<<<<<<<
- *             raise ValueError("mate %s: is unmapped" % (read.qname))
- * 
- */
-  __pyx_t_2 = (((__pyx_v_flag & BAM_FMUNMAP) != 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":761
- *             raise ValueError("read %s: is unpaired" % (read.qname))
- *         if flag & BAM_FMUNMAP != 0:
- *             raise ValueError("mate %s: is unmapped" % (read.qname))             # <<<<<<<<<<<<<<
- * 
- *         # xor flags to get the other mate
- */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_read), __pyx_n_s_qname); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_mate_s_is_unmapped, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __Pyx_Raise(__pyx_t_3, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":764
- * 
- *         # xor flags to get the other mate
- *         cdef int x = BAM_FREAD1 + BAM_FREAD2             # <<<<<<<<<<<<<<
- *         flag = (flag ^ x) & x
- * 
- */
-  __pyx_v_x = (BAM_FREAD1 + BAM_FREAD2);
-
-  /* "pysam/csamfile.pyx":765
- *         # xor flags to get the other mate
- *         cdef int x = BAM_FREAD1 + BAM_FREAD2
- *         flag = (flag ^ x) & x             # <<<<<<<<<<<<<<
- * 
- *         # the following code is not using the C API and
- */
-  __pyx_v_flag = ((__pyx_v_flag ^ __pyx_v_x) & __pyx_v_x);
-
-  /* "pysam/csamfile.pyx":769
- *         # the following code is not using the C API and
- *         # could thus be made much quicker
- *         for mate in self.fetch(             # <<<<<<<<<<<<<<
- *                 read._delegate.core.mpos,
- *                 read._delegate.core.mpos + 1,
- */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fetch); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-
-  /* "pysam/csamfile.pyx":770
- *         # could thus be made much quicker
- *         for mate in self.fetch(
- *                 read._delegate.core.mpos,             # <<<<<<<<<<<<<<
- *                 read._delegate.core.mpos + 1,
- *                 tid=read._delegate.core.mtid):
- */
-  __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_read->_delegate->core.mpos); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-
-  /* "pysam/csamfile.pyx":771
- *         for mate in self.fetch(
- *                 read._delegate.core.mpos,
- *                 read._delegate.core.mpos + 1,             # <<<<<<<<<<<<<<
- *                 tid=read._delegate.core.mtid):
- *             if mate.flag & flag != 0 and \
- */
-  __pyx_t_5 = __Pyx_PyInt_From_long((__pyx_v_read->_delegate->core.mpos + 1)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-
-  /* "pysam/csamfile.pyx":769
- *         # the following code is not using the C API and
- *         # could thus be made much quicker
- *         for mate in self.fetch(             # <<<<<<<<<<<<<<
- *                 read._delegate.core.mpos,
- *                 read._delegate.core.mpos + 1,
- */
-  __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_t_5);
-  __Pyx_GIVEREF(__pyx_t_5);
-  __pyx_t_4 = 0;
-  __pyx_t_5 = 0;
-  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-
-  /* "pysam/csamfile.pyx":772
- *                 read._delegate.core.mpos,
- *                 read._delegate.core.mpos + 1,
- *                 tid=read._delegate.core.mtid):             # <<<<<<<<<<<<<<
- *             if mate.flag & flag != 0 and \
- *                mate.qname == read.qname:
- */
-  __pyx_t_4 = __Pyx_PyInt_From_int32_t(__pyx_v_read->_delegate->core.mtid); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_tid, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-  /* "pysam/csamfile.pyx":769
- *         # the following code is not using the C API and
- *         # could thus be made much quicker
- *         for mate in self.fetch(             # <<<<<<<<<<<<<<
- *                 read._delegate.core.mpos,
- *                 read._delegate.core.mpos + 1,
- */
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_6, __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  if (PyList_CheckExact(__pyx_t_4) || PyTuple_CheckExact(__pyx_t_4)) {
-    __pyx_t_5 = __pyx_t_4; __Pyx_INCREF(__pyx_t_5); __pyx_t_7 = 0;
-    __pyx_t_8 = NULL;
-  } else {
-    __pyx_t_7 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_8 = Py_TYPE(__pyx_t_5)->tp_iternext;
-  }
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  for (;;) {
-    if (!__pyx_t_8 && PyList_CheckExact(__pyx_t_5)) {
-      if (__pyx_t_7 >= PyList_GET_SIZE(__pyx_t_5)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_4 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_7); __Pyx_INCREF(__pyx_t_4); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_4 = PySequence_ITEM(__pyx_t_5, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else if (!__pyx_t_8 && PyTuple_CheckExact(__pyx_t_5)) {
-      if (__pyx_t_7 >= PyTuple_GET_SIZE(__pyx_t_5)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_7); __Pyx_INCREF(__pyx_t_4); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_4 = PySequence_ITEM(__pyx_t_5, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else {
-      __pyx_t_4 = __pyx_t_8(__pyx_t_5);
-      if (unlikely(!__pyx_t_4)) {
-        PyObject* exc_type = PyErr_Occurred();
-        if (exc_type) {
-          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        break;
-      }
-      __Pyx_GOTREF(__pyx_t_4);
-    }
-    __Pyx_XDECREF_SET(__pyx_v_mate, __pyx_t_4);
-    __pyx_t_4 = 0;
-
-    /* "pysam/csamfile.pyx":773
- *                 read._delegate.core.mpos + 1,
- *                 tid=read._delegate.core.mtid):
- *             if mate.flag & flag != 0 and \             # <<<<<<<<<<<<<<
- *                mate.qname == read.qname:
- *                 break
- */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_mate, __pyx_n_s_flag); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_flag); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = PyNumber_And(__pyx_t_4, __pyx_t_6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":774
- *                 tid=read._delegate.core.mtid):
- *             if mate.flag & flag != 0 and \
- *                mate.qname == read.qname:             # <<<<<<<<<<<<<<
- *                 break
- *         else:
- */
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_mate, __pyx_n_s_qname); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_read), __pyx_n_s_qname); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_4 = PyObject_RichCompare(__pyx_t_6, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_9 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __pyx_t_10 = __pyx_t_9;
-    } else {
-      __pyx_t_10 = __pyx_t_2;
-    }
-    if (__pyx_t_10) {
-
-      /* "pysam/csamfile.pyx":775
- *             if mate.flag & flag != 0 and \
- *                mate.qname == read.qname:
- *                 break             # <<<<<<<<<<<<<<
- *         else:
- *             raise ValueError("mate not found")
- */
-      goto __pyx_L6_break;
-    }
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":777
- *                 break
- *         else:
- *             raise ValueError("mate not found")             # <<<<<<<<<<<<<<
- * 
- *         return mate
- */
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__27, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_Raise(__pyx_t_4, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-  __pyx_L6_break:;
-  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-
-  /* "pysam/csamfile.pyx":779
- *             raise ValueError("mate not found")
- * 
- *         return mate             # <<<<<<<<<<<<<<
- * 
- *     def count(self,
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_mate);
-  __pyx_r = __pyx_v_mate;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":736
- *         return IteratorRowHead(self, n)
- * 
- *     def mate(self,             # <<<<<<<<<<<<<<
- *              AlignedRead read):
- *         '''return the mate of :class:`AlignedRead` *read*.
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.mate", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_mate);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":781
- *         return mate
- * 
- *     def count(self,             # <<<<<<<<<<<<<<
- *               reference=None,
- *               start=None,
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_26count[] = "Samfile.count(self, reference=None, start=None, end=None, region=None, until_eof=False)\n*(reference = None, start = None, end = None,\n        region = None, callback = None, until_eof = False)*\n\n        count reads :term:`region` using 0-based indexing. The region\n        is specified by :term:`reference`, *start* and\n        *end*. Alternatively, a samtools :term:`region` string can be\n        supplied.\n\n        Note  [...]
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_27count(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_reference = 0;
-  PyObject *__pyx_v_start = 0;
-  PyObject *__pyx_v_end = 0;
-  PyObject *__pyx_v_region = 0;
-  PyObject *__pyx_v_until_eof = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("count (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,&__pyx_n_s_until_eof,0};
-    PyObject* values[5] = {0,0,0,0,0};
-
-    /* "pysam/csamfile.pyx":782
- * 
- *     def count(self,
- *               reference=None,             # <<<<<<<<<<<<<<
- *               start=None,
- *               end=None,
- */
-    values[0] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":783
- *     def count(self,
- *               reference=None,
- *               start=None,             # <<<<<<<<<<<<<<
- *               end=None,
- *               region=None,
- */
-    values[1] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":784
- *               reference=None,
- *               start=None,
- *               end=None,             # <<<<<<<<<<<<<<
- *               region=None,
- *               until_eof=False):
- */
-    values[2] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":785
- *               start=None,
- *               end=None,
- *               region=None,             # <<<<<<<<<<<<<<
- *               until_eof=False):
- *         '''*(reference = None, start = None, end = None,
- */
-    values[3] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":786
- *               end=None,
- *               region=None,
- *               until_eof=False):             # <<<<<<<<<<<<<<
- *         '''*(reference = None, start = None, end = None,
- *         region = None, callback = None, until_eof = False)*
- */
-    values[4] = ((PyObject *)Py_False);
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reference);
-          if (value) { values[0] = value; kw_args--; }
-        }
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_start);
-          if (value) { values[1] = value; kw_args--; }
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end);
-          if (value) { values[2] = value; kw_args--; }
-        }
-        case  3:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_region);
-          if (value) { values[3] = value; kw_args--; }
-        }
-        case  4:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_until_eof);
-          if (value) { values[4] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "count") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_reference = values[0];
-    __pyx_v_start = values[1];
-    __pyx_v_end = values[2];
-    __pyx_v_region = values[3];
-    __pyx_v_until_eof = values[4];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("count", 0, 0, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.count", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_26count(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_until_eof);
-
-  /* "pysam/csamfile.pyx":781
- *         return mate
- * 
- *     def count(self,             # <<<<<<<<<<<<<<
- *               reference=None,
- *               start=None,
- */
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_26count(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_until_eof) {
-  CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read = 0;
-  long __pyx_v_counter;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  Py_ssize_t __pyx_t_6;
-  PyObject *(*__pyx_t_7)(PyObject *);
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("count", 0);
-  __Pyx_TraceCall("count", __pyx_f[0], 781);
-
-  /* "pysam/csamfile.pyx":799
- *         '''
- *         cdef AlignedRead read
- *         cdef long counter = 0             # <<<<<<<<<<<<<<
- * 
- *         if not self._isOpen():
- */
-  __pyx_v_counter = 0;
-
-  /* "pysam/csamfile.pyx":801
- *         cdef long counter = 0
- * 
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":802
- * 
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         for read in self.fetch(reference=reference,
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__28, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":804
- *             raise ValueError( "I/O operation on closed file" )
- * 
- *         for read in self.fetch(reference=reference,             # <<<<<<<<<<<<<<
- *                                start=start,
- *                                end=end,
- */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_fetch); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_reference, __pyx_v_reference) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":805
- * 
- *         for read in self.fetch(reference=reference,
- *                                start=start,             # <<<<<<<<<<<<<<
- *                                end=end,
- *                                region=region,
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_start, __pyx_v_start) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":806
- *         for read in self.fetch(reference=reference,
- *                                start=start,
- *                                end=end,             # <<<<<<<<<<<<<<
- *                                region=region,
- *                                until_eof=until_eof):
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_end, __pyx_v_end) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":807
- *                                start=start,
- *                                end=end,
- *                                region=region,             # <<<<<<<<<<<<<<
- *                                until_eof=until_eof):
- *             counter += 1
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_region, __pyx_v_region) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":808
- *                                end=end,
- *                                region=region,
- *                                until_eof=until_eof):             # <<<<<<<<<<<<<<
- *             counter += 1
- * 
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_until_eof, __pyx_v_until_eof) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":804
- *             raise ValueError( "I/O operation on closed file" )
- * 
- *         for read in self.fetch(reference=reference,             # <<<<<<<<<<<<<<
- *                                start=start,
- *                                end=end,
- */
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, __pyx_t_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (PyList_CheckExact(__pyx_t_5) || PyTuple_CheckExact(__pyx_t_5)) {
-    __pyx_t_1 = __pyx_t_5; __Pyx_INCREF(__pyx_t_1); __pyx_t_6 = 0;
-    __pyx_t_7 = NULL;
-  } else {
-    __pyx_t_6 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_7 = Py_TYPE(__pyx_t_1)->tp_iternext;
-  }
-  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  for (;;) {
-    if (!__pyx_t_7 && PyList_CheckExact(__pyx_t_1)) {
-      if (__pyx_t_6 >= PyList_GET_SIZE(__pyx_t_1)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_5 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_6); __Pyx_INCREF(__pyx_t_5); __pyx_t_6++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_5 = PySequence_ITEM(__pyx_t_1, __pyx_t_6); __pyx_t_6++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else if (!__pyx_t_7 && PyTuple_CheckExact(__pyx_t_1)) {
-      if (__pyx_t_6 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_5 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_6); __Pyx_INCREF(__pyx_t_5); __pyx_t_6++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_5 = PySequence_ITEM(__pyx_t_1, __pyx_t_6); __pyx_t_6++; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else {
-      __pyx_t_5 = __pyx_t_7(__pyx_t_1);
-      if (unlikely(!__pyx_t_5)) {
-        PyObject* exc_type = PyErr_Occurred();
-        if (exc_type) {
-          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        break;
-      }
-      __Pyx_GOTREF(__pyx_t_5);
-    }
-    if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5pysam_8csamfile_AlignedRead))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_XDECREF_SET(__pyx_v_read, ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_t_5));
-    __pyx_t_5 = 0;
-
-    /* "pysam/csamfile.pyx":809
- *                                region=region,
- *                                until_eof=until_eof):
- *             counter += 1             # <<<<<<<<<<<<<<
- * 
- *         return counter
- */
-    __pyx_v_counter = (__pyx_v_counter + 1);
-  }
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":811
- *             counter += 1
- * 
- *         return counter             # <<<<<<<<<<<<<<
- * 
- *     def pileup( self,
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_long(__pyx_v_counter); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":781
- *         return mate
- * 
- *     def count(self,             # <<<<<<<<<<<<<<
- *               reference=None,
- *               start=None,
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.count", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF((PyObject *)__pyx_v_read);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":813
- *         return counter
- * 
- *     def pileup( self,             # <<<<<<<<<<<<<<
- *                 reference = None,
- *                 start = None,
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_28pileup[] = "Samfile.pileup(self, reference=None, start=None, end=None, region=None, **kwargs)\n\n        perform a :term:`pileup` within a :term:`region`. The region is specified by\n        :term:`reference`, *start* and *end* (using 0-based indexing).\n        Alternatively, a samtools *region* string can be supplied.\n\n        Without *reference* or *region* all reads will be used for the pileup. The reads will be returned\n        or [...]
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_29pileup(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_reference = 0;
-  PyObject *__pyx_v_start = 0;
-  PyObject *__pyx_v_end = 0;
-  PyObject *__pyx_v_region = 0;
-  PyObject *__pyx_v_kwargs = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("pileup (wrapper)", 0);
-  __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return NULL;
-  __Pyx_GOTREF(__pyx_v_kwargs);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,0};
-    PyObject* values[4] = {0,0,0,0};
-
-    /* "pysam/csamfile.pyx":814
- * 
- *     def pileup( self,
- *                 reference = None,             # <<<<<<<<<<<<<<
- *                 start = None,
- *                 end = None,
- */
-    values[0] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":815
- *     def pileup( self,
- *                 reference = None,
- *                 start = None,             # <<<<<<<<<<<<<<
- *                 end = None,
- *                 region = None,
- */
-    values[1] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":816
- *                 reference = None,
- *                 start = None,
- *                 end = None,             # <<<<<<<<<<<<<<
- *                 region = None,
- *                 **kwargs ):
- */
-    values[2] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":817
- *                 start = None,
- *                 end = None,
- *                 region = None,             # <<<<<<<<<<<<<<
- *                 **kwargs ):
- *         '''
- */
-    values[3] = ((PyObject *)Py_None);
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reference);
-          if (value) { values[0] = value; kw_args--; }
-        }
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_start);
-          if (value) { values[1] = value; kw_args--; }
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end);
-          if (value) { values[2] = value; kw_args--; }
-        }
-        case  3:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_region);
-          if (value) { values[3] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "pileup") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_reference = values[0];
-    __pyx_v_start = values[1];
-    __pyx_v_end = values[2];
-    __pyx_v_region = values[3];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("pileup", 0, 0, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.pileup", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_28pileup(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_kwargs);
-
-  /* "pysam/csamfile.pyx":813
- *         return counter
- * 
- *     def pileup( self,             # <<<<<<<<<<<<<<
- *                 reference = None,
- *                 start = None,
- */
-
-  /* function exit code */
-  __Pyx_XDECREF(__pyx_v_kwargs);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_28pileup(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_kwargs) {
-  int __pyx_v_rtid;
-  int __pyx_v_rstart;
-  int __pyx_v_rend;
-  int __pyx_v_has_coord;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *(*__pyx_t_9)(PyObject *);
-  int __pyx_t_10;
-  int __pyx_t_11;
-  int __pyx_t_12;
-  int __pyx_t_13;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("pileup", 0);
-  __Pyx_TraceCall("pileup", __pyx_f[0], 813);
-
-  /* "pysam/csamfile.pyx":867
- *         cdef int rtid, rstart, rend, has_coord
- * 
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":868
- * 
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         has_coord, rtid, rstart, rend = self._parseRegion(
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__29, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":870
- *             raise ValueError( "I/O operation on closed file" )
- * 
- *         has_coord, rtid, rstart, rend = self._parseRegion(             # <<<<<<<<<<<<<<
- *             reference, start, end, region )
- * 
- */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_parseRegion); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-
-  /* "pysam/csamfile.pyx":871
- * 
- *         has_coord, rtid, rstart, rend = self._parseRegion(
- *             reference, start, end, region )             # <<<<<<<<<<<<<<
- * 
- *         if self.isbam:
- */
-  __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(__pyx_v_reference);
-  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_reference);
-  __Pyx_GIVEREF(__pyx_v_reference);
-  __Pyx_INCREF(__pyx_v_start);
-  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_start);
-  __Pyx_GIVEREF(__pyx_v_start);
-  __Pyx_INCREF(__pyx_v_end);
-  PyTuple_SET_ITEM(__pyx_t_1, 2, __pyx_v_end);
-  __Pyx_GIVEREF(__pyx_v_end);
-  __Pyx_INCREF(__pyx_v_region);
-  PyTuple_SET_ITEM(__pyx_t_1, 3, __pyx_v_region);
-  __Pyx_GIVEREF(__pyx_v_region);
-
-  /* "pysam/csamfile.pyx":870
- *             raise ValueError( "I/O operation on closed file" )
- * 
- *         has_coord, rtid, rstart, rend = self._parseRegion(             # <<<<<<<<<<<<<<
- *             reference, start, end, region )
- * 
- */
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if ((likely(PyTuple_CheckExact(__pyx_t_5))) || (PyList_CheckExact(__pyx_t_5))) {
-    PyObject* sequence = __pyx_t_5;
-    #if CYTHON_COMPILING_IN_CPYTHON
-    Py_ssize_t size = Py_SIZE(sequence);
-    #else
-    Py_ssize_t size = PySequence_Size(sequence);
-    #endif
-    if (unlikely(size != 4)) {
-      if (size > 4) __Pyx_RaiseTooManyValuesError(4);
-      else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-    #if CYTHON_COMPILING_IN_CPYTHON
-    if (likely(PyTuple_CheckExact(sequence))) {
-      __pyx_t_1 = PyTuple_GET_ITEM(sequence, 0); 
-      __pyx_t_2 = PyTuple_GET_ITEM(sequence, 1); 
-      __pyx_t_6 = PyTuple_GET_ITEM(sequence, 2); 
-      __pyx_t_7 = PyTuple_GET_ITEM(sequence, 3); 
-    } else {
-      __pyx_t_1 = PyList_GET_ITEM(sequence, 0); 
-      __pyx_t_2 = PyList_GET_ITEM(sequence, 1); 
-      __pyx_t_6 = PyList_GET_ITEM(sequence, 2); 
-      __pyx_t_7 = PyList_GET_ITEM(sequence, 3); 
-    }
-    __Pyx_INCREF(__pyx_t_1);
-    __Pyx_INCREF(__pyx_t_2);
-    __Pyx_INCREF(__pyx_t_6);
-    __Pyx_INCREF(__pyx_t_7);
-    #else
-    {
-      Py_ssize_t i;
-      PyObject** temps[4] = {&__pyx_t_1,&__pyx_t_2,&__pyx_t_6,&__pyx_t_7};
-      for (i=0; i < 4; i++) {
-        PyObject* item = PySequence_ITEM(sequence, i); if (unlikely(!item)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(item);
-        *(temps[i]) = item;
-      }
-    }
-    #endif
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  } else {
-    Py_ssize_t index = -1;
-    PyObject** temps[4] = {&__pyx_t_1,&__pyx_t_2,&__pyx_t_6,&__pyx_t_7};
-    __pyx_t_8 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_9 = Py_TYPE(__pyx_t_8)->tp_iternext;
-    for (index=0; index < 4; index++) {
-      PyObject* item = __pyx_t_9(__pyx_t_8); if (unlikely(!item)) goto __pyx_L4_unpacking_failed;
-      __Pyx_GOTREF(item);
-      *(temps[index]) = item;
-    }
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_9 = NULL;
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    goto __pyx_L5_unpacking_done;
-    __pyx_L4_unpacking_failed:;
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    __pyx_t_9 = NULL;
-    if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_L5_unpacking_done:;
-  }
-  __pyx_t_10 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_10 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_11 = __Pyx_PyInt_As_int(__pyx_t_2); if (unlikely((__pyx_t_11 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_12 = __Pyx_PyInt_As_int(__pyx_t_6); if (unlikely((__pyx_t_12 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_7); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-  __pyx_v_has_coord = __pyx_t_10;
-  __pyx_v_rtid = __pyx_t_11;
-  __pyx_v_rstart = __pyx_t_12;
-  __pyx_v_rend = __pyx_t_13;
-
-  /* "pysam/csamfile.pyx":873
- *             reference, start, end, region )
- * 
- *         if self.isbam:             # <<<<<<<<<<<<<<
- *             if not self._hasIndex():
- *                 raise ValueError("no index available for pileup")
- */
-  __pyx_t_4 = (__pyx_v_self->isbam != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":874
- * 
- *         if self.isbam:
- *             if not self._hasIndex():             # <<<<<<<<<<<<<<
- *                 raise ValueError("no index available for pileup")
- * 
- */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    __pyx_t_3 = ((!__pyx_t_4) != 0);
-    if (__pyx_t_3) {
-
-      /* "pysam/csamfile.pyx":875
- *         if self.isbam:
- *             if not self._hasIndex():
- *                 raise ValueError("no index available for pileup")             # <<<<<<<<<<<<<<
- * 
- *             if has_coord:
- */
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__30, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_Raise(__pyx_t_7, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":877
- *                 raise ValueError("no index available for pileup")
- * 
- *             if has_coord:             # <<<<<<<<<<<<<<
- *                 return IteratorColumnRegion(self,
- *                                             tid = rtid,
- */
-    __pyx_t_3 = (__pyx_v_has_coord != 0);
-    if (__pyx_t_3) {
-
-      /* "pysam/csamfile.pyx":878
- * 
- *             if has_coord:
- *                 return IteratorColumnRegion(self,             # <<<<<<<<<<<<<<
- *                                             tid = rtid,
- *                                             start = rstart,
- */
-      __Pyx_XDECREF(__pyx_r);
-      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_INCREF(((PyObject *)__pyx_v_self));
-      PyTuple_SET_ITEM(__pyx_t_7, 0, ((PyObject *)__pyx_v_self));
-      __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-
-      /* "pysam/csamfile.pyx":882
- *                                             start = rstart,
- *                                             end = rend,
- *                                             **kwargs )             # <<<<<<<<<<<<<<
- *             else:
- *                 return IteratorColumnAllRefs(self, **kwargs )
- */
-      __pyx_t_5 = PyDict_Copy(__pyx_v_kwargs); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-
-      /* "pysam/csamfile.pyx":879
- *             if has_coord:
- *                 return IteratorColumnRegion(self,
- *                                             tid = rtid,             # <<<<<<<<<<<<<<
- *                                             start = rstart,
- *                                             end = rend,
- */
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rtid); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 879; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      if (unlikely(PyDict_GetItem(__pyx_t_5, __pyx_n_s_tid))) {
-        __Pyx_RaiseDoubleKeywordsError("function", __pyx_n_s_tid); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_tid, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-      /* "pysam/csamfile.pyx":880
- *                 return IteratorColumnRegion(self,
- *                                             tid = rtid,
- *                                             start = rstart,             # <<<<<<<<<<<<<<
- *                                             end = rend,
- *                                             **kwargs )
- */
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rstart); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 880; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      if (unlikely(PyDict_GetItem(__pyx_t_5, __pyx_n_s_start))) {
-        __Pyx_RaiseDoubleKeywordsError("function", __pyx_n_s_start); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_start, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-      /* "pysam/csamfile.pyx":881
- *                                             tid = rtid,
- *                                             start = rstart,
- *                                             end = rend,             # <<<<<<<<<<<<<<
- *                                             **kwargs )
- *             else:
- */
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_rend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 881; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      if (unlikely(PyDict_GetItem(__pyx_t_5, __pyx_n_s_end))) {
-        __Pyx_RaiseDoubleKeywordsError("function", __pyx_n_s_end); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_end, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-      /* "pysam/csamfile.pyx":878
- * 
- *             if has_coord:
- *                 return IteratorColumnRegion(self,             # <<<<<<<<<<<<<<
- *                                             tid = rtid,
- *                                             start = rstart,
- */
-      __pyx_t_6 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorColumnRegion)), __pyx_t_7, __pyx_t_5); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __pyx_r = __pyx_t_6;
-      __pyx_t_6 = 0;
-      goto __pyx_L0;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":884
- *                                             **kwargs )
- *             else:
- *                 return IteratorColumnAllRefs(self, **kwargs )             # <<<<<<<<<<<<<<
- * 
- *         else:
- */
-      __Pyx_XDECREF(__pyx_r);
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_INCREF(((PyObject *)__pyx_v_self));
-      PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_v_self));
-      __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-      __pyx_t_5 = __pyx_v_kwargs;
-      __Pyx_INCREF(__pyx_t_5);
-      __pyx_t_7 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorColumnAllRefs)), __pyx_t_6, __pyx_t_5); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __pyx_r = __pyx_t_7;
-      __pyx_t_7 = 0;
-      goto __pyx_L0;
-    }
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":887
- * 
- *         else:
- *             raise NotImplementedError( "pileup of samfiles not implemented yet" )             # <<<<<<<<<<<<<<
- * 
- *     def close( self ):
- */
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__31, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_Raise(__pyx_t_7, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":813
- *         return counter
- * 
- *     def pileup( self,             # <<<<<<<<<<<<<<
- *                 reference = None,
- *                 start = None,
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.pileup", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":889
- *             raise NotImplementedError( "pileup of samfiles not implemented yet" )
- * 
- *     def close( self ):             # <<<<<<<<<<<<<<
- *         '''
- *         closes the :class:`pysam.Samfile`.'''
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_31close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_30close[] = "Samfile.close(self)\n\n        closes the :class:`pysam.Samfile`.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_31close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("close (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_30close(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_30close(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("close", 0);
-  __Pyx_TraceCall("close", __pyx_f[0], 889);
-
-  /* "pysam/csamfile.pyx":892
- *         '''
- *         closes the :class:`pysam.Samfile`.'''
- *         if self.htsfile != NULL:             # <<<<<<<<<<<<<<
- *             hts_close(self.htsfile)
- *             hts_idx_destroy(self.index);
- */
-  __pyx_t_1 = ((__pyx_v_self->htsfile != NULL) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":893
- *         closes the :class:`pysam.Samfile`.'''
- *         if self.htsfile != NULL:
- *             hts_close(self.htsfile)             # <<<<<<<<<<<<<<
- *             hts_idx_destroy(self.index);
- *             self.htsfile = NULL
- */
-    hts_close(__pyx_v_self->htsfile);
-
-    /* "pysam/csamfile.pyx":894
- *         if self.htsfile != NULL:
- *             hts_close(self.htsfile)
- *             hts_idx_destroy(self.index);             # <<<<<<<<<<<<<<
- *             self.htsfile = NULL
- * 
- */
-    hts_idx_destroy(__pyx_v_self->index);
-
-    /* "pysam/csamfile.pyx":895
- *             hts_close(self.htsfile)
- *             hts_idx_destroy(self.index);
- *             self.htsfile = NULL             # <<<<<<<<<<<<<<
- * 
- *     def __dealloc__( self ):
- */
-    __pyx_v_self->htsfile = NULL;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":889
- *             raise NotImplementedError( "pileup of samfiles not implemented yet" )
- * 
- *     def close( self ):             # <<<<<<<<<<<<<<
- *         '''
- *         closes the :class:`pysam.Samfile`.'''
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":897
- *             self.htsfile = NULL
- * 
- *     def __dealloc__( self ):             # <<<<<<<<<<<<<<
- *         # remember: dealloc cannot call other methods
- *         # note: no doc string
- */
-
-/* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_7Samfile_33__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_7Samfile_33__dealloc__(PyObject *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-}
-
-static void __pyx_pf_5pysam_8csamfile_7Samfile_32__dealloc__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 897);
-
-  /* "pysam/csamfile.pyx":901
- *         # note: no doc string
- *         # note: __del__ is not called.
- *         self.close()             # <<<<<<<<<<<<<<
- *         bam_destroy1(self.b)
- *         if self.header != NULL:
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 901; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 901; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":902
- *         # note: __del__ is not called.
- *         self.close()
- *         bam_destroy1(self.b)             # <<<<<<<<<<<<<<
- *         if self.header != NULL:
- *             bam_hdr_destroy(self.header)
- */
-  bam_destroy1(__pyx_v_self->b);
-
-  /* "pysam/csamfile.pyx":903
- *         self.close()
- *         bam_destroy1(self.b)
- *         if self.header != NULL:             # <<<<<<<<<<<<<<
- *             bam_hdr_destroy(self.header)
- * 
- */
-  __pyx_t_3 = ((__pyx_v_self->header != NULL) != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":904
- *         bam_destroy1(self.b)
- *         if self.header != NULL:
- *             bam_hdr_destroy(self.header)             # <<<<<<<<<<<<<<
- * 
- *     cpdef int write( self, AlignedRead read ) except -1:
- */
-    bam_hdr_destroy(__pyx_v_self->header);
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":897
- *             self.htsfile = NULL
- * 
- *     def __dealloc__( self ):             # <<<<<<<<<<<<<<
- *         # remember: dealloc cannot call other methods
- *         # note: no doc string
- */
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_WriteUnraisable("pysam.csamfile.Samfile.__dealloc__", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-}
-
-/* "pysam/csamfile.pyx":906
- *             bam_hdr_destroy(self.header)
- * 
- *     cpdef int write( self, AlignedRead read ) except -1:             # <<<<<<<<<<<<<<
- *         '''
- *         write a single :class:`pysam.AlignedRead` to disk.
- */
-
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_35write(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/
-static int __pyx_f_5pysam_8csamfile_7Samfile_write(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read, int __pyx_skip_dispatch) {
-  int __pyx_v_x;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("write", 0);
-  __Pyx_TraceCall("write", __pyx_f[0], 906);
-  /* Check if called by wrapper */
-  if (unlikely(__pyx_skip_dispatch)) ;
-  /* Check if overridden in Python */
-  else if (unlikely(Py_TYPE(((PyObject *)__pyx_v_self))->tp_dictoffset != 0)) {
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_write); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    if (!PyCFunction_Check(__pyx_t_1) || (PyCFunction_GET_FUNCTION(__pyx_t_1) != (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_35write)) {
-      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_INCREF(((PyObject *)__pyx_v_read));
-      PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_read));
-      __Pyx_GIVEREF(((PyObject *)__pyx_v_read));
-      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_4 = __Pyx_PyInt_As_int(__pyx_t_3); if (unlikely((__pyx_t_4 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_r = __pyx_t_4;
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      goto __pyx_L0;
-    }
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  }
-
-  /* "pysam/csamfile.pyx":912
- *         returns the number of bytes written.
- *         '''
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             return 0
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_6 = ((!__pyx_t_5) != 0);
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":913
- *         '''
- *         if not self._isOpen():
- *             return 0             # <<<<<<<<<<<<<<
- * 
- *         x = sam_write1(self.htsfile,
- */
-    __pyx_r = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":915
- *             return 0
- * 
- *         x = sam_write1(self.htsfile,             # <<<<<<<<<<<<<<
- *                        self.header,
- *                        read._delegate)
- */
-  __pyx_v_x = sam_write1(__pyx_v_self->htsfile, __pyx_v_self->header, __pyx_v_read->_delegate);
-
-  /* "pysam/csamfile.pyx":919
- *                        read._delegate)
- * 
- *         return x             # <<<<<<<<<<<<<<
- * 
- *     def __enter__(self):
- */
-  __pyx_r = __pyx_v_x;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":906
- *             bam_hdr_destroy(self.header)
- * 
- *     cpdef int write( self, AlignedRead read ) except -1:             # <<<<<<<<<<<<<<
- *         '''
- *         write a single :class:`pysam.AlignedRead` to disk.
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_WriteUnraisable("pysam.csamfile.Samfile.write", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_35write(PyObject *__pyx_v_self, PyObject *__pyx_v_read); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_34write[] = "Samfile.write(self, AlignedRead read) -> int\n\n        write a single :class:`pysam.AlignedRead` to disk.\n\n        returns the number of bytes written.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_35write(PyObject *__pyx_v_self, PyObject *__pyx_v_read) {
-  CYTHON_UNUSED int __pyx_lineno = 0;
-  CYTHON_UNUSED const char *__pyx_filename = NULL;
-  CYTHON_UNUSED int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("write (wrapper)", 0);
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_read), __pyx_ptype_5pysam_8csamfile_AlignedRead, 1, "read", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_34write(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_read));
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_34write(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_read) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("write", 0);
-  __Pyx_TraceCall("write", __pyx_f[0], 906);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(((struct __pyx_vtabstruct_5pysam_8csamfile_Samfile *)__pyx_v_self->__pyx_vtab)->write(__pyx_v_self, __pyx_v_read, 1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.write", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":921
- *         return x
- * 
- *     def __enter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_37__enter__(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_36__enter__[] = "Samfile.__enter__(self)";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_37__enter__(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__enter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_36__enter__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_36__enter__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__enter__", 0);
-  __Pyx_TraceCall("__enter__", __pyx_f[0], 921);
-
-  /* "pysam/csamfile.pyx":922
- * 
- *     def __enter__(self):
- *         return self             # <<<<<<<<<<<<<<
- * 
- *     def __exit__(self, exc_type, exc_value, traceback):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  __pyx_r = ((PyObject *)__pyx_v_self);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":921
- *         return x
- * 
- *     def __enter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":924
- *         return self
- * 
- *     def __exit__(self, exc_type, exc_value, traceback):             # <<<<<<<<<<<<<<
- *         self.close()
- *         return False
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_39__exit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_38__exit__[] = "Samfile.__exit__(self, exc_type, exc_value, traceback)";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_39__exit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  CYTHON_UNUSED PyObject *__pyx_v_exc_type = 0;
-  CYTHON_UNUSED PyObject *__pyx_v_exc_value = 0;
-  CYTHON_UNUSED PyObject *__pyx_v_traceback = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__exit__ (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_exc_type,&__pyx_n_s_exc_value,&__pyx_n_s_traceback,0};
-    PyObject* values[3] = {0,0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_exc_type)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_exc_value)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  2:
-        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_traceback)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__exit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
-      goto __pyx_L5_argtuple_error;
-    } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-    }
-    __pyx_v_exc_type = values[0];
-    __pyx_v_exc_value = values[1];
-    __pyx_v_traceback = values[2];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__exit__", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__exit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_exc_type, __pyx_v_exc_value, __pyx_v_traceback);
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_38__exit__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_exc_type, CYTHON_UNUSED PyObject *__pyx_v_exc_value, CYTHON_UNUSED PyObject *__pyx_v_traceback) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__exit__", 0);
-  __Pyx_TraceCall("__exit__", __pyx_f[0], 924);
-
-  /* "pysam/csamfile.pyx":925
- * 
- *     def __exit__(self, exc_type, exc_value, traceback):
- *         self.close()             # <<<<<<<<<<<<<<
- *         return False
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":926
- *     def __exit__(self, exc_type, exc_value, traceback):
- *         self.close()
- *         return False             # <<<<<<<<<<<<<<
- * 
- *     ###############################################################
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(Py_False);
-  __pyx_r = Py_False;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":924
- *         return self
- * 
- *     def __exit__(self, exc_type, exc_value, traceback):             # <<<<<<<<<<<<<<
- *         self.close()
- *         return False
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__exit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":935
- *     property filename:
- *         '''number of :term:`filename` associated with this object.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._filename
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_8filename_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_8filename_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_8filename___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8filename___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 935);
-
-  /* "pysam/csamfile.pyx":936
- *         '''number of :term:`filename` associated with this object.'''
- *         def __get__(self):
- *             return self._filename             # <<<<<<<<<<<<<<
- * 
- *     property nreferences:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_self->_filename);
-  __pyx_r = __pyx_v_self->_filename;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":935
- *     property filename:
- *         '''number of :term:`filename` associated with this object.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._filename
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":940
- *     property nreferences:
- *         '''number of :term:`reference` sequences in the file.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return self.header.n_targets
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_11nreferences_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_11nreferences_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_11nreferences___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 940);
-
-  /* "pysam/csamfile.pyx":941
- *         '''number of :term:`reference` sequences in the file.'''
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *             return self.header.n_targets
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__32, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":942
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return self.header.n_targets             # <<<<<<<<<<<<<<
- * 
- *     property references:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_v_self->header->n_targets); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":940
- *     property nreferences:
- *         '''number of :term:`reference` sequences in the file.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return self.header.n_targets
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.nreferences.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":946
- *     property references:
- *         """tuple with the names of :term:`reference` sequences."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             t = []
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_10references_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_10references_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_10references___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_v_t = NULL;
-  long __pyx_v_x;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  int32_t __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 946);
-
-  /* "pysam/csamfile.pyx":947
- *         """tuple with the names of :term:`reference` sequences."""
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *             t = []
- *             for x from 0 <= x < self.header.n_targets:
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__33, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":948
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             t = []             # <<<<<<<<<<<<<<
- *             for x from 0 <= x < self.header.n_targets:
- *                 t.append(_charptr_to_str(self.header.target_name[x]))
- */
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_v_t = ((PyObject*)__pyx_t_2);
-  __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":949
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             t = []
- *             for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
- *                 t.append(_charptr_to_str(self.header.target_name[x]))
- *             return tuple(t)
- */
-  __pyx_t_5 = __pyx_v_self->header->n_targets;
-  for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_5; __pyx_v_x++) {
-
-    /* "pysam/csamfile.pyx":950
- *             t = []
- *             for x from 0 <= x < self.header.n_targets:
- *                 t.append(_charptr_to_str(self.header.target_name[x]))             # <<<<<<<<<<<<<<
- *             return tuple(t)
- * 
- */
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile__charptr_to_str((__pyx_v_self->header->target_name[__pyx_v_x])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_t, __pyx_t_2); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  }
-
-  /* "pysam/csamfile.pyx":951
- *             for x from 0 <= x < self.header.n_targets:
- *                 t.append(_charptr_to_str(self.header.target_name[x]))
- *             return tuple(t)             # <<<<<<<<<<<<<<
- * 
- *     property lengths:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = PyList_AsTuple(__pyx_v_t); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 951; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":946
- *     property references:
- *         """tuple with the names of :term:`reference` sequences."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             t = []
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.references.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_t);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":957
- *         :attr:`pysam.Samfile.references`
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             t = []
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7lengths_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_7lengths_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_7lengths___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_v_t = NULL;
-  long __pyx_v_x;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  int32_t __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 957);
-
-  /* "pysam/csamfile.pyx":958
- *         """
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *             t = []
- *             for x from 0 <= x < self.header.n_targets:
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__34, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":959
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             t = []             # <<<<<<<<<<<<<<
- *             for x from 0 <= x < self.header.n_targets:
- *                 t.append( self.header.target_len[x] )
- */
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 959; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_v_t = ((PyObject*)__pyx_t_2);
-  __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":960
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             t = []
- *             for x from 0 <= x < self.header.n_targets:             # <<<<<<<<<<<<<<
- *                 t.append( self.header.target_len[x] )
- *             return tuple(t)
- */
-  __pyx_t_5 = __pyx_v_self->header->n_targets;
-  for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_5; __pyx_v_x++) {
-
-    /* "pysam/csamfile.pyx":961
- *             t = []
- *             for x from 0 <= x < self.header.n_targets:
- *                 t.append( self.header.target_len[x] )             # <<<<<<<<<<<<<<
- *             return tuple(t)
- * 
- */
-    __pyx_t_2 = __Pyx_PyInt_From_uint32_t((__pyx_v_self->header->target_len[__pyx_v_x])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_t, __pyx_t_2); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  }
-
-  /* "pysam/csamfile.pyx":962
- *             for x from 0 <= x < self.header.n_targets:
- *                 t.append( self.header.target_len[x] )
- *             return tuple(t)             # <<<<<<<<<<<<<<
- * 
- *     property mapped:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = PyList_AsTuple(__pyx_v_t); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 962; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":957
- *         :attr:`pysam.Samfile.references`
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             t = []
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.lengths.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_t);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":967
- *         """total number of mapped alignments in file.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             self._checkIndex()
- *             cdef int tid
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_6mapped_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_6mapped_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6mapped___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  int __pyx_v_tid;
-  uint64_t __pyx_v_total;
-  uint64_t __pyx_v_mapped;
-  uint64_t __pyx_v_unmapped;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int32_t __pyx_t_3;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 967);
-
-  /* "pysam/csamfile.pyx":968
- *         """
- *         def __get__(self):
- *             self._checkIndex()             # <<<<<<<<<<<<<<
- *             cdef int tid
- *             cdef uint64_t total = 0
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_checkIndex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":970
- *             self._checkIndex()
- *             cdef int tid
- *             cdef uint64_t total = 0             # <<<<<<<<<<<<<<
- *             cdef uint64_t mapped, unmapped
- *             for tid from 0 <= tid < self.header.n_targets:
- */
-  __pyx_v_total = 0;
-
-  /* "pysam/csamfile.pyx":972
- *             cdef uint64_t total = 0
- *             cdef uint64_t mapped, unmapped
- *             for tid from 0 <= tid < self.header.n_targets:             # <<<<<<<<<<<<<<
- *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
- *                 total += mapped
- */
-  __pyx_t_3 = __pyx_v_self->header->n_targets;
-  for (__pyx_v_tid = 0; __pyx_v_tid < __pyx_t_3; __pyx_v_tid++) {
-
-    /* "pysam/csamfile.pyx":973
- *             cdef uint64_t mapped, unmapped
- *             for tid from 0 <= tid < self.header.n_targets:
- *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)             # <<<<<<<<<<<<<<
- *                 total += mapped
- *             return total
- */
-    hts_idx_get_stat(__pyx_v_self->index, __pyx_v_tid, (&__pyx_v_mapped), (&__pyx_v_unmapped));
-
-    /* "pysam/csamfile.pyx":974
- *             for tid from 0 <= tid < self.header.n_targets:
- *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
- *                 total += mapped             # <<<<<<<<<<<<<<
- *             return total
- * 
- */
-    __pyx_v_total = (__pyx_v_total + __pyx_v_mapped);
-  }
-
-  /* "pysam/csamfile.pyx":975
- *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
- *                 total += mapped
- *             return total             # <<<<<<<<<<<<<<
- * 
- *     def _checkIndex(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_uint64_t(__pyx_v_total); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":967
- *         """total number of mapped alignments in file.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             self._checkIndex()
- *             cdef int tid
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.mapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":977
- *             return total
- * 
- *     def _checkIndex(self):             # <<<<<<<<<<<<<<
- *         '''check if index is present. Otherwise raise
- *         an error.'''
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_41_checkIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_40_checkIndex[] = "Samfile._checkIndex(self)\ncheck if index is present. Otherwise raise\n        an error.";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_41_checkIndex(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("_checkIndex (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_40_checkIndex(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_checkIndex", 0);
-  __Pyx_TraceCall("_checkIndex", __pyx_f[0], 977);
-
-  /* "pysam/csamfile.pyx":980
- *         '''check if index is present. Otherwise raise
- *         an error.'''
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError("I/O operation on closed file")
- *         if not self.isbam:
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":981
- *         an error.'''
- *         if not self._isOpen():
- *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
- *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__35, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":982
- *         if not self._isOpen():
- *             raise ValueError("I/O operation on closed file")
- *         if not self.isbam:             # <<<<<<<<<<<<<<
- *             raise AttributeError("Samfile.mapped only available in bam files")
- *         if self.index == NULL:
- */
-  __pyx_t_4 = ((!(__pyx_v_self->isbam != 0)) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":983
- *             raise ValueError("I/O operation on closed file")
- *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")             # <<<<<<<<<<<<<<
- *         if self.index == NULL:
- *             raise ValueError("mapping information not recorded in index "
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_AttributeError, __pyx_tuple__36, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":984
- *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")
- *         if self.index == NULL:             # <<<<<<<<<<<<<<
- *             raise ValueError("mapping information not recorded in index "
- *                                  "or index not available")
- */
-  __pyx_t_4 = ((__pyx_v_self->index == NULL) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":985
- *             raise AttributeError("Samfile.mapped only available in bam files")
- *         if self.index == NULL:
- *             raise ValueError("mapping information not recorded in index "             # <<<<<<<<<<<<<<
- *                                  "or index not available")
- * 
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__37, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":977
- *             return total
- * 
- *     def _checkIndex(self):             # <<<<<<<<<<<<<<
- *         '''check if index is present. Otherwise raise
- *         an error.'''
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._checkIndex", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":992
- *         """total number of unmapped reads in file.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             self._checkIndex()
- *             cdef int tid
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_8unmapped_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_8unmapped_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_8unmapped___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  int __pyx_v_tid;
-  uint64_t __pyx_v_total;
-  uint64_t __pyx_v_mapped;
-  uint64_t __pyx_v_unmapped;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int32_t __pyx_t_3;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 992);
-
-  /* "pysam/csamfile.pyx":993
- *         """
- *         def __get__(self):
- *             self._checkIndex()             # <<<<<<<<<<<<<<
- *             cdef int tid
- *             cdef uint64_t total = 0
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_checkIndex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":995
- *             self._checkIndex()
- *             cdef int tid
- *             cdef uint64_t total = 0             # <<<<<<<<<<<<<<
- *             cdef uint64_t mapped, unmapped
- *             for tid from 0 <= tid < self.header.n_targets:
- */
-  __pyx_v_total = 0;
-
-  /* "pysam/csamfile.pyx":997
- *             cdef uint64_t total = 0
- *             cdef uint64_t mapped, unmapped
- *             for tid from 0 <= tid < self.header.n_targets:             # <<<<<<<<<<<<<<
- *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
- *                 total += unmapped
- */
-  __pyx_t_3 = __pyx_v_self->header->n_targets;
-  for (__pyx_v_tid = 0; __pyx_v_tid < __pyx_t_3; __pyx_v_tid++) {
-
-    /* "pysam/csamfile.pyx":998
- *             cdef uint64_t mapped, unmapped
- *             for tid from 0 <= tid < self.header.n_targets:
- *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)             # <<<<<<<<<<<<<<
- *                 total += unmapped
- *             return total
- */
-    hts_idx_get_stat(__pyx_v_self->index, __pyx_v_tid, (&__pyx_v_mapped), (&__pyx_v_unmapped));
-
-    /* "pysam/csamfile.pyx":999
- *             for tid from 0 <= tid < self.header.n_targets:
- *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
- *                 total += unmapped             # <<<<<<<<<<<<<<
- *             return total
- * 
- */
-    __pyx_v_total = (__pyx_v_total + __pyx_v_unmapped);
-  }
-
-  /* "pysam/csamfile.pyx":1000
- *                 hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
- *                 total += unmapped
- *             return total             # <<<<<<<<<<<<<<
- * 
- *     property nocoordinate:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_uint64_t(__pyx_v_total); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":992
- *         """total number of unmapped reads in file.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             self._checkIndex()
- *             cdef int tid
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.unmapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1005
- *         """total number of reads without coordinates
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             self._checkIndex()
- *             return hts_idx_get_n_no_coor(self.index)
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_12nocoordinate_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_12nocoordinate_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_12nocoordinate___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 1005);
-
-  /* "pysam/csamfile.pyx":1006
- *         """
- *         def __get__(self):
- *             self._checkIndex()             # <<<<<<<<<<<<<<
- *             return hts_idx_get_n_no_coor(self.index)
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_checkIndex); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":1007
- *         def __get__(self):
- *             self._checkIndex()
- *             return hts_idx_get_n_no_coor(self.index)             # <<<<<<<<<<<<<<
- * 
- *     property text:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyInt_From_uint64_t(hts_idx_get_n_no_coor(__pyx_v_self->index)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1005
- *         """total number of reads without coordinates
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             self._checkIndex()
- *             return hts_idx_get_n_no_coor(self.index)
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.nocoordinate.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1011
- *     property text:
- *         '''full contents of the :term:`sam file` header as a string.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return from_string_and_size(self.header.text, self.header.l_text)
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_4text_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_4text_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_4text___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_4text___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 1011);
-
-  /* "pysam/csamfile.pyx":1012
- *         '''full contents of the :term:`sam file` header as a string.'''
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *             return from_string_and_size(self.header.text, self.header.l_text)
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__38, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1013
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return from_string_and_size(self.header.text, self.header.l_text)             # <<<<<<<<<<<<<<
- * 
- *     property header:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile_from_string_and_size(__pyx_v_self->header->text, __pyx_v_self->header->l_text); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1011
- *     property text:
- *         '''full contents of the :term:`sam file` header as a string.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return from_string_and_size(self.header.text, self.header.l_text)
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.text.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1019
- *         a two-level dictionary.
- *         '''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_6header_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_6header_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_6header___get__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_v_result = NULL;
-  PyObject *__pyx_v_t = NULL;
-  PyObject *__pyx_v_line = NULL;
-  PyObject *__pyx_v_fields = NULL;
-  PyObject *__pyx_v_record = NULL;
-  PyObject *__pyx_v_x = NULL;
-  PyObject *__pyx_v_field = NULL;
-  PyObject *__pyx_v_key = NULL;
-  PyObject *__pyx_v_value = NULL;
-  PyObject *__pyx_v_sq = NULL;
-  PyObject *__pyx_v_ref = NULL;
-  PyObject *__pyx_v_length = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  Py_ssize_t __pyx_t_5;
-  PyObject *(*__pyx_t_6)(PyObject *);
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  int __pyx_t_9;
-  Py_ssize_t __pyx_t_10;
-  PyObject *(*__pyx_t_11)(PyObject *);
-  PyObject *__pyx_t_12 = NULL;
-  PyObject *__pyx_t_13 = NULL;
-  PyObject *(*__pyx_t_14)(PyObject *);
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 1019);
-
-  /* "pysam/csamfile.pyx":1020
- *         '''
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *             result = {}
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__39, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1022
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- * 
- *             result = {}             # <<<<<<<<<<<<<<
- * 
- *             if self.header.text != NULL:
- */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_v_result = ((PyObject*)__pyx_t_2);
-  __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":1024
- *             result = {}
- * 
- *             if self.header.text != NULL:             # <<<<<<<<<<<<<<
- *                 # convert to python string (note: call self.text to create 0-terminated string)
- *                 t = self.text
- */
-  __pyx_t_4 = ((__pyx_v_self->header->text != NULL) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":1026
- *             if self.header.text != NULL:
- *                 # convert to python string (note: call self.text to create 0-terminated string)
- *                 t = self.text             # <<<<<<<<<<<<<<
- *                 for line in t.split("\n"):
- *                     if not line.strip(): continue
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_text); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_v_t = __pyx_t_2;
-    __pyx_t_2 = 0;
-
-    /* "pysam/csamfile.pyx":1027
- *                 # convert to python string (note: call self.text to create 0-terminated string)
- *                 t = self.text
- *                 for line in t.split("\n"):             # <<<<<<<<<<<<<<
- *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_t, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__40, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (PyList_CheckExact(__pyx_t_1) || PyTuple_CheckExact(__pyx_t_1)) {
-      __pyx_t_2 = __pyx_t_1; __Pyx_INCREF(__pyx_t_2); __pyx_t_5 = 0;
-      __pyx_t_6 = NULL;
-    } else {
-      __pyx_t_5 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_6 = Py_TYPE(__pyx_t_2)->tp_iternext;
-    }
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    for (;;) {
-      if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_2)) {
-        if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_2)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_2)) {
-        if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else {
-        __pyx_t_1 = __pyx_t_6(__pyx_t_2);
-        if (unlikely(!__pyx_t_1)) {
-          PyObject* exc_type = PyErr_Occurred();
-          if (exc_type) {
-            if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          break;
-        }
-        __Pyx_GOTREF(__pyx_t_1);
-      }
-      __Pyx_XDECREF_SET(__pyx_v_line, __pyx_t_1);
-      __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":1028
- *                 t = self.text
- *                 for line in t.split("\n"):
- *                     if not line.strip(): continue             # <<<<<<<<<<<<<<
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
- *                     fields = line[1:].split("\t")
- */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_3 = ((!__pyx_t_4) != 0);
-      if (__pyx_t_3) {
-        goto __pyx_L5_continue;
-      }
-
-      /* "pysam/csamfile.pyx":1029
- *                 for line in t.split("\n"):
- *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line             # <<<<<<<<<<<<<<
- *                     fields = line[1:].split("\t")
- *                     record = fields[0]
- */
-      #ifndef CYTHON_WITHOUT_ASSERTIONS
-      if (unlikely(!Py_OptimizeFlag)) {
-        __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_tuple__42, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        if (unlikely(!__pyx_t_3)) {
-          __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_header_line_without_s, __pyx_v_line); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
-          PyErr_SetObject(PyExc_AssertionError, __pyx_t_1);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-      }
-      #endif
-
-      /* "pysam/csamfile.pyx":1030
- *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
- *                     fields = line[1:].split("\t")             # <<<<<<<<<<<<<<
- *                     record = fields[0]
- *                     assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)
- */
-      __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_line, 1, 0, NULL, NULL, &__pyx_slice__43, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_tuple__44, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __Pyx_XDECREF_SET(__pyx_v_fields, __pyx_t_1);
-      __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":1031
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
- *                     fields = line[1:].split("\t")
- *                     record = fields[0]             # <<<<<<<<<<<<<<
- *                     assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)
- * 
- */
-      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_fields, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_1);
-      __Pyx_XDECREF_SET(__pyx_v_record, __pyx_t_1);
-      __pyx_t_1 = 0;
-
-      /* "pysam/csamfile.pyx":1032
- *                     fields = line[1:].split("\t")
- *                     record = fields[0]
- *                     assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)             # <<<<<<<<<<<<<<
- * 
- *                     # treat comments
- */
-      #ifndef CYTHON_WITHOUT_ASSERTIONS
-      if (unlikely(!Py_OptimizeFlag)) {
-        __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_record, __pyx_t_1, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        if (unlikely(!(__pyx_t_3 != 0))) {
-          __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
-          __Pyx_INCREF(__pyx_v_record);
-          PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_record);
-          __Pyx_GIVEREF(__pyx_v_record);
-          __Pyx_INCREF(__pyx_v_line);
-          PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_line);
-          __Pyx_GIVEREF(__pyx_v_line);
-          __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_header_line_with_invalid_type_s, __pyx_t_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          PyErr_SetObject(PyExc_AssertionError, __pyx_t_7);
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-      }
-      #endif
-
-      /* "pysam/csamfile.pyx":1035
- * 
- *                     # treat comments
- *                     if record == "CO":             # <<<<<<<<<<<<<<
- *                         if record not in result: result[record] = []
- *                         result[record].append( "\t".join( fields[1:] ) )
- */
-      __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_record, __pyx_n_s_CO, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      if (__pyx_t_3) {
-
-        /* "pysam/csamfile.pyx":1036
- *                     # treat comments
- *                     if record == "CO":
- *                         if record not in result: result[record] = []             # <<<<<<<<<<<<<<
- *                         result[record].append( "\t".join( fields[1:] ) )
- *                         continue
- */
-        __pyx_t_3 = (__Pyx_PyDict_Contains(__pyx_v_record, __pyx_v_result, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_4 = (__pyx_t_3 != 0);
-        if (__pyx_t_4) {
-          __pyx_t_7 = PyList_New(0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_record, __pyx_t_7) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          goto __pyx_L9;
-        }
-        __pyx_L9:;
-
-        /* "pysam/csamfile.pyx":1037
- *                     if record == "CO":
- *                         if record not in result: result[record] = []
- *                         result[record].append( "\t".join( fields[1:] ) )             # <<<<<<<<<<<<<<
- *                         continue
- *                     # the following is clumsy as generators do not work?
- */
-        __pyx_t_7 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_fields, 1, 0, NULL, NULL, &__pyx_slice__45, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_8 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_9 = __Pyx_PyObject_Append(__pyx_t_7, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-
-        /* "pysam/csamfile.pyx":1038
- *                         if record not in result: result[record] = []
- *                         result[record].append( "\t".join( fields[1:] ) )
- *                         continue             # <<<<<<<<<<<<<<
- *                     # the following is clumsy as generators do not work?
- *                     x = {}
- */
-        goto __pyx_L5_continue;
-      }
-
-      /* "pysam/csamfile.pyx":1040
- *                         continue
- *                     # the following is clumsy as generators do not work?
- *                     x = {}             # <<<<<<<<<<<<<<
- *                     for field in fields[1:]:
- *                         if ":" not in field:
- */
-      __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1040; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_XDECREF_SET(__pyx_v_x, ((PyObject*)__pyx_t_8));
-      __pyx_t_8 = 0;
-
-      /* "pysam/csamfile.pyx":1041
- *                     # the following is clumsy as generators do not work?
- *                     x = {}
- *                     for field in fields[1:]:             # <<<<<<<<<<<<<<
- *                         if ":" not in field:
- *                             raise ValueError("malformatted header: no ':' in field" )
- */
-      __pyx_t_8 = __Pyx_PyObject_GetSlice(__pyx_v_fields, 1, 0, NULL, NULL, &__pyx_slice__46, 1, 0, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      if (PyList_CheckExact(__pyx_t_8) || PyTuple_CheckExact(__pyx_t_8)) {
-        __pyx_t_7 = __pyx_t_8; __Pyx_INCREF(__pyx_t_7); __pyx_t_10 = 0;
-        __pyx_t_11 = NULL;
-      } else {
-        __pyx_t_10 = -1; __pyx_t_7 = PyObject_GetIter(__pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_11 = Py_TYPE(__pyx_t_7)->tp_iternext;
-      }
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      for (;;) {
-        if (!__pyx_t_11 && PyList_CheckExact(__pyx_t_7)) {
-          if (__pyx_t_10 >= PyList_GET_SIZE(__pyx_t_7)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_8 = PyList_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else if (!__pyx_t_11 && PyTuple_CheckExact(__pyx_t_7)) {
-          if (__pyx_t_10 >= PyTuple_GET_SIZE(__pyx_t_7)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else {
-          __pyx_t_8 = __pyx_t_11(__pyx_t_7);
-          if (unlikely(!__pyx_t_8)) {
-            PyObject* exc_type = PyErr_Occurred();
-            if (exc_type) {
-              if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-            }
-            break;
-          }
-          __Pyx_GOTREF(__pyx_t_8);
-        }
-        __Pyx_XDECREF_SET(__pyx_v_field, __pyx_t_8);
-        __pyx_t_8 = 0;
-
-        /* "pysam/csamfile.pyx":1042
- *                     x = {}
- *                     for field in fields[1:]:
- *                         if ":" not in field:             # <<<<<<<<<<<<<<
- *                             raise ValueError("malformatted header: no ':' in field" )
- *                         key, value = field.split(":",1)
- */
-        __pyx_t_4 = (__Pyx_PySequence_Contains(__pyx_kp_s__47, __pyx_v_field, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_3 = (__pyx_t_4 != 0);
-        if (__pyx_t_3) {
-
-          /* "pysam/csamfile.pyx":1043
- *                     for field in fields[1:]:
- *                         if ":" not in field:
- *                             raise ValueError("malformatted header: no ':' in field" )             # <<<<<<<<<<<<<<
- *                         key, value = field.split(":",1)
- *                         # uppercase keys must be valid
- */
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__48, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-
-        /* "pysam/csamfile.pyx":1044
- *                         if ":" not in field:
- *                             raise ValueError("malformatted header: no ':' in field" )
- *                         key, value = field.split(":",1)             # <<<<<<<<<<<<<<
- *                         # uppercase keys must be valid
- *                         # lowercase are permitted for user fields
- */
-        __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_field, __pyx_n_s_split); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_tuple__49, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        if ((likely(PyTuple_CheckExact(__pyx_t_1))) || (PyList_CheckExact(__pyx_t_1))) {
-          PyObject* sequence = __pyx_t_1;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          Py_ssize_t size = Py_SIZE(sequence);
-          #else
-          Py_ssize_t size = PySequence_Size(sequence);
-          #endif
-          if (unlikely(size != 2)) {
-            if (size > 2) __Pyx_RaiseTooManyValuesError(2);
-            else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-            {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          #if CYTHON_COMPILING_IN_CPYTHON
-          if (likely(PyTuple_CheckExact(sequence))) {
-            __pyx_t_8 = PyTuple_GET_ITEM(sequence, 0); 
-            __pyx_t_12 = PyTuple_GET_ITEM(sequence, 1); 
-          } else {
-            __pyx_t_8 = PyList_GET_ITEM(sequence, 0); 
-            __pyx_t_12 = PyList_GET_ITEM(sequence, 1); 
-          }
-          __Pyx_INCREF(__pyx_t_8);
-          __Pyx_INCREF(__pyx_t_12);
-          #else
-          __pyx_t_8 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __pyx_t_12 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_12);
-          #endif
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        } else {
-          Py_ssize_t index = -1;
-          __pyx_t_13 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_13);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __pyx_t_14 = Py_TYPE(__pyx_t_13)->tp_iternext;
-          index = 0; __pyx_t_8 = __pyx_t_14(__pyx_t_13); if (unlikely(!__pyx_t_8)) goto __pyx_L13_unpacking_failed;
-          __Pyx_GOTREF(__pyx_t_8);
-          index = 1; __pyx_t_12 = __pyx_t_14(__pyx_t_13); if (unlikely(!__pyx_t_12)) goto __pyx_L13_unpacking_failed;
-          __Pyx_GOTREF(__pyx_t_12);
-          if (__Pyx_IternextUnpackEndCheck(__pyx_t_14(__pyx_t_13), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_t_14 = NULL;
-          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
-          goto __pyx_L14_unpacking_done;
-          __pyx_L13_unpacking_failed:;
-          __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
-          __pyx_t_14 = NULL;
-          if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_L14_unpacking_done:;
-        }
-        __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_8);
-        __pyx_t_8 = 0;
-        __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_12);
-        __pyx_t_12 = 0;
-
-        /* "pysam/csamfile.pyx":1047
- *                         # uppercase keys must be valid
- *                         # lowercase are permitted for user fields
- *                         if key in VALID_HEADER_FIELDS[record]:             # <<<<<<<<<<<<<<
- *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
- *                         elif not key.isupper():
- */
-        __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_FIELDS); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1047; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_12 = PyObject_GetItem(__pyx_t_1, __pyx_v_record); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1047; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_12);
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_key, __pyx_t_12, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1047; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-        __pyx_t_4 = (__pyx_t_3 != 0);
-        if (__pyx_t_4) {
-
-          /* "pysam/csamfile.pyx":1048
- *                         # lowercase are permitted for user fields
- *                         if key in VALID_HEADER_FIELDS[record]:
- *                             x[key] = VALID_HEADER_FIELDS[record][key](value)             # <<<<<<<<<<<<<<
- *                         elif not key.isupper():
- *                             x[key] = value
- */
-          __pyx_t_12 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_FIELDS); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_12);
-          __pyx_t_1 = PyObject_GetItem(__pyx_t_12, __pyx_v_record); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-          __Pyx_GOTREF(__pyx_t_1);
-          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          __pyx_t_12 = PyObject_GetItem(__pyx_t_1, __pyx_v_key); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-          __Pyx_GOTREF(__pyx_t_12);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
-          __Pyx_INCREF(__pyx_v_value);
-          PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_value);
-          __Pyx_GIVEREF(__pyx_v_value);
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          if (unlikely(PyDict_SetItem(__pyx_v_x, __pyx_v_key, __pyx_t_8) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          goto __pyx_L15;
-        }
-
-        /* "pysam/csamfile.pyx":1049
- *                         if key in VALID_HEADER_FIELDS[record]:
- *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
- *                         elif not key.isupper():             # <<<<<<<<<<<<<<
- *                             x[key] = value
- *                         else:
- */
-        __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_isupper); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1049; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1049; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1049; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_3 = ((!__pyx_t_4) != 0);
-        if (__pyx_t_3) {
-
-          /* "pysam/csamfile.pyx":1050
- *                             x[key] = VALID_HEADER_FIELDS[record][key](value)
- *                         elif not key.isupper():
- *                             x[key] = value             # <<<<<<<<<<<<<<
- *                         else:
- *                             raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) )
- */
-          if (unlikely(PyDict_SetItem(__pyx_v_x, __pyx_v_key, __pyx_v_value) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1050; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          goto __pyx_L15;
-        }
-        /*else*/ {
-
-          /* "pysam/csamfile.pyx":1052
- *                             x[key] = value
- *                         else:
- *                             raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) )             # <<<<<<<<<<<<<<
- * 
- *                     if VALID_HEADER_TYPES[record] == dict:
- */
-          __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
-          __Pyx_INCREF(__pyx_v_key);
-          PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_key);
-          __Pyx_GIVEREF(__pyx_v_key);
-          __Pyx_INCREF(__pyx_v_record);
-          PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_record);
-          __Pyx_GIVEREF(__pyx_v_record);
-          __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_unknown_field_code_s_in_record_s, __pyx_t_1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
-          PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_8);
-          __Pyx_GIVEREF(__pyx_t_8);
-          __pyx_t_8 = 0;
-          __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1052; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        __pyx_L15:;
-      }
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-
-      /* "pysam/csamfile.pyx":1054
- *                             raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) )
- * 
- *                     if VALID_HEADER_TYPES[record] == dict:             # <<<<<<<<<<<<<<
- *                         if record in result:
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )
- */
-      __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_8 = PyObject_GetItem(__pyx_t_7, __pyx_v_record); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_8, ((PyObject *)((PyObject*)(&PyDict_Type))), Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1054; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      if (__pyx_t_3) {
-
-        /* "pysam/csamfile.pyx":1055
- * 
- *                     if VALID_HEADER_TYPES[record] == dict:
- *                         if record in result:             # <<<<<<<<<<<<<<
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )
- *                         result[record] = x
- */
-        __pyx_t_3 = (__Pyx_PyDict_Contains(__pyx_v_record, __pyx_v_result, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1055; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_4 = (__pyx_t_3 != 0);
-        if (__pyx_t_4) {
-
-          /* "pysam/csamfile.pyx":1056
- *                     if VALID_HEADER_TYPES[record] == dict:
- *                         if record in result:
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )             # <<<<<<<<<<<<<<
- *                         result[record] = x
- *                     elif VALID_HEADER_TYPES[record] == list:
- */
-          __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_multiple_s_lines_are_not_permitt, __pyx_v_record); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_7);
-          __Pyx_GIVEREF(__pyx_t_7);
-          __pyx_t_7 = 0;
-          __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_8, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          __Pyx_Raise(__pyx_t_7, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-
-        /* "pysam/csamfile.pyx":1057
- *                         if record in result:
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )
- *                         result[record] = x             # <<<<<<<<<<<<<<
- *                     elif VALID_HEADER_TYPES[record] == list:
- *                         if record not in result: result[record] = []
- */
-        if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_record, __pyx_v_x) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        goto __pyx_L16;
-      }
-
-      /* "pysam/csamfile.pyx":1058
- *                             raise ValueError( "multiple '%s' lines are not permitted" % record )
- *                         result[record] = x
- *                     elif VALID_HEADER_TYPES[record] == list:             # <<<<<<<<<<<<<<
- *                         if record not in result: result[record] = []
- *                         result[record].append( x )
- */
-      __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1058; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_8 = PyObject_GetItem(__pyx_t_7, __pyx_v_record); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1058; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_8, ((PyObject *)((PyObject*)(&PyList_Type))), Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1058; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1058; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      if (__pyx_t_4) {
-
-        /* "pysam/csamfile.pyx":1059
- *                         result[record] = x
- *                     elif VALID_HEADER_TYPES[record] == list:
- *                         if record not in result: result[record] = []             # <<<<<<<<<<<<<<
- *                         result[record].append( x )
- * 
- */
-        __pyx_t_4 = (__Pyx_PyDict_Contains(__pyx_v_record, __pyx_v_result, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_3 = (__pyx_t_4 != 0);
-        if (__pyx_t_3) {
-          __pyx_t_7 = PyList_New(0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_v_record, __pyx_t_7) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          goto __pyx_L18;
-        }
-        __pyx_L18:;
-
-        /* "pysam/csamfile.pyx":1060
- *                     elif VALID_HEADER_TYPES[record] == list:
- *                         if record not in result: result[record] = []
- *                         result[record].append( x )             # <<<<<<<<<<<<<<
- * 
- *                 # if there are no SQ lines in the header, add the reference names
- */
-        __pyx_t_7 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1060; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_9 = __Pyx_PyObject_Append(__pyx_t_7, __pyx_v_x); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1060; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        goto __pyx_L16;
-      }
-      __pyx_L16:;
-      __pyx_L5_continue:;
-    }
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-    /* "pysam/csamfile.pyx":1068
- *                 # SQ lines, the SQ information is not part of the textual header and thus
- *                 # are missing from the output. See issue 84.
- *                 if "SQ" not in result:             # <<<<<<<<<<<<<<
- *                     sq = []
- *                     for ref, length in zip( self.references, self.lengths ):
- */
-    __pyx_t_3 = (__Pyx_PyDict_Contains(__pyx_n_s_SQ, __pyx_v_result, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1068; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = (__pyx_t_3 != 0);
-    if (__pyx_t_4) {
-
-      /* "pysam/csamfile.pyx":1069
- *                 # are missing from the output. See issue 84.
- *                 if "SQ" not in result:
- *                     sq = []             # <<<<<<<<<<<<<<
- *                     for ref, length in zip( self.references, self.lengths ):
- *                         sq.append( {'LN': length, 'SN': ref } )
- */
-      __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __pyx_v_sq = ((PyObject*)__pyx_t_2);
-      __pyx_t_2 = 0;
-
-      /* "pysam/csamfile.pyx":1070
- *                 if "SQ" not in result:
- *                     sq = []
- *                     for ref, length in zip( self.references, self.lengths ):             # <<<<<<<<<<<<<<
- *                         sq.append( {'LN': length, 'SN': ref } )
- *                     result["SQ"] = sq
- */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_references); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_lengths); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_2);
-      __Pyx_GIVEREF(__pyx_t_2);
-      PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_7);
-      __Pyx_GIVEREF(__pyx_t_7);
-      __pyx_t_2 = 0;
-      __pyx_t_7 = 0;
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_zip, __pyx_t_8, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      if (PyList_CheckExact(__pyx_t_7) || PyTuple_CheckExact(__pyx_t_7)) {
-        __pyx_t_8 = __pyx_t_7; __Pyx_INCREF(__pyx_t_8); __pyx_t_5 = 0;
-        __pyx_t_6 = NULL;
-      } else {
-        __pyx_t_5 = -1; __pyx_t_8 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_6 = Py_TYPE(__pyx_t_8)->tp_iternext;
-      }
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      for (;;) {
-        if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_8)) {
-          if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_8)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_7 = PyList_GET_ITEM(__pyx_t_8, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_7 = PySequence_ITEM(__pyx_t_8, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_8)) {
-          if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_8)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_8, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_7 = PySequence_ITEM(__pyx_t_8, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else {
-          __pyx_t_7 = __pyx_t_6(__pyx_t_8);
-          if (unlikely(!__pyx_t_7)) {
-            PyObject* exc_type = PyErr_Occurred();
-            if (exc_type) {
-              if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-            }
-            break;
-          }
-          __Pyx_GOTREF(__pyx_t_7);
-        }
-        if ((likely(PyTuple_CheckExact(__pyx_t_7))) || (PyList_CheckExact(__pyx_t_7))) {
-          PyObject* sequence = __pyx_t_7;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          Py_ssize_t size = Py_SIZE(sequence);
-          #else
-          Py_ssize_t size = PySequence_Size(sequence);
-          #endif
-          if (unlikely(size != 2)) {
-            if (size > 2) __Pyx_RaiseTooManyValuesError(2);
-            else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-            {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          #if CYTHON_COMPILING_IN_CPYTHON
-          if (likely(PyTuple_CheckExact(sequence))) {
-            __pyx_t_2 = PyTuple_GET_ITEM(sequence, 0); 
-            __pyx_t_1 = PyTuple_GET_ITEM(sequence, 1); 
-          } else {
-            __pyx_t_2 = PyList_GET_ITEM(sequence, 0); 
-            __pyx_t_1 = PyList_GET_ITEM(sequence, 1); 
-          }
-          __Pyx_INCREF(__pyx_t_2);
-          __Pyx_INCREF(__pyx_t_1);
-          #else
-          __pyx_t_2 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_2);
-          __pyx_t_1 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
-          #endif
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        } else {
-          Py_ssize_t index = -1;
-          __pyx_t_12 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_12);
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          __pyx_t_14 = Py_TYPE(__pyx_t_12)->tp_iternext;
-          index = 0; __pyx_t_2 = __pyx_t_14(__pyx_t_12); if (unlikely(!__pyx_t_2)) goto __pyx_L22_unpacking_failed;
-          __Pyx_GOTREF(__pyx_t_2);
-          index = 1; __pyx_t_1 = __pyx_t_14(__pyx_t_12); if (unlikely(!__pyx_t_1)) goto __pyx_L22_unpacking_failed;
-          __Pyx_GOTREF(__pyx_t_1);
-          if (__Pyx_IternextUnpackEndCheck(__pyx_t_14(__pyx_t_12), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_t_14 = NULL;
-          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          goto __pyx_L23_unpacking_done;
-          __pyx_L22_unpacking_failed:;
-          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          __pyx_t_14 = NULL;
-          if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_L23_unpacking_done:;
-        }
-        __Pyx_XDECREF_SET(__pyx_v_ref, __pyx_t_2);
-        __pyx_t_2 = 0;
-        __Pyx_XDECREF_SET(__pyx_v_length, __pyx_t_1);
-        __pyx_t_1 = 0;
-
-        /* "pysam/csamfile.pyx":1071
- *                     sq = []
- *                     for ref, length in zip( self.references, self.lengths ):
- *                         sq.append( {'LN': length, 'SN': ref } )             # <<<<<<<<<<<<<<
- *                     result["SQ"] = sq
- * 
- */
-        __pyx_t_7 = PyDict_New(); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        if (PyDict_SetItem(__pyx_t_7, __pyx_n_s_LN, __pyx_v_length) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        if (PyDict_SetItem(__pyx_t_7, __pyx_n_s_SN, __pyx_v_ref) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_sq, __pyx_t_7); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      }
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-
-      /* "pysam/csamfile.pyx":1072
- *                     for ref, length in zip( self.references, self.lengths ):
- *                         sq.append( {'LN': length, 'SN': ref } )
- *                     result["SQ"] = sq             # <<<<<<<<<<<<<<
- * 
- *             return result
- */
-      if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_n_s_SQ, __pyx_v_sq) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1072; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      goto __pyx_L19;
-    }
-    __pyx_L19:;
-    goto __pyx_L4;
-  }
-  __pyx_L4:;
-
-  /* "pysam/csamfile.pyx":1074
- *                     result["SQ"] = sq
- * 
- *             return result             # <<<<<<<<<<<<<<
- * 
- *     def _buildLine( self, fields, record ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_result);
-  __pyx_r = __pyx_v_result;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1019
- *         a two-level dictionary.
- *         '''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_12);
-  __Pyx_XDECREF(__pyx_t_13);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.header.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_result);
-  __Pyx_XDECREF(__pyx_v_t);
-  __Pyx_XDECREF(__pyx_v_line);
-  __Pyx_XDECREF(__pyx_v_fields);
-  __Pyx_XDECREF(__pyx_v_record);
-  __Pyx_XDECREF(__pyx_v_x);
-  __Pyx_XDECREF(__pyx_v_field);
-  __Pyx_XDECREF(__pyx_v_key);
-  __Pyx_XDECREF(__pyx_v_value);
-  __Pyx_XDECREF(__pyx_v_sq);
-  __Pyx_XDECREF(__pyx_v_ref);
-  __Pyx_XDECREF(__pyx_v_length);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1076
- *             return result
- * 
- *     def _buildLine( self, fields, record ):             # <<<<<<<<<<<<<<
- *         '''build a header line from *fields* dictionary for *record*'''
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_43_buildLine(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_42_buildLine[] = "Samfile._buildLine(self, fields, record)\nbuild a header line from *fields* dictionary for *record*";
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_43_buildLine(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_fields = 0;
-  PyObject *__pyx_v_record = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("_buildLine (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_fields,&__pyx_n_s_record,0};
-    PyObject* values[2] = {0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_fields)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_record)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("_buildLine", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1076; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_buildLine") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1076; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
-      goto __pyx_L5_argtuple_error;
-    } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-    }
-    __pyx_v_fields = values[0];
-    __pyx_v_record = values[1];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_buildLine", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1076; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._buildLine", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self), __pyx_v_fields, __pyx_v_record);
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_42_buildLine(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_fields, PyObject *__pyx_v_record) {
-  PyObject *__pyx_v_line = NULL;
-  PyObject *__pyx_v_key = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  Py_ssize_t __pyx_t_5;
-  PyObject *(*__pyx_t_6)(PyObject *);
-  PyObject *__pyx_t_7 = NULL;
-  int __pyx_t_8;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_buildLine", 0);
-  __Pyx_TraceCall("_buildLine", __pyx_f[0], 1076);
-
-  /* "pysam/csamfile.pyx":1080
- * 
- *         # TODO: add checking for field and sort order
- *         line = ["@%s" % record ]             # <<<<<<<<<<<<<<
- *         # comment
- *         if record == "CO":
- */
-  __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_s, __pyx_v_record); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  PyList_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  __pyx_t_1 = 0;
-  __pyx_v_line = ((PyObject*)__pyx_t_2);
-  __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":1082
- *         line = ["@%s" % record ]
- *         # comment
- *         if record == "CO":             # <<<<<<<<<<<<<<
- *             line.append( fields )
- *         # user tags
- */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_record, __pyx_n_s_CO, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":1083
- *         # comment
- *         if record == "CO":
- *             line.append( fields )             # <<<<<<<<<<<<<<
- *         # user tags
- *         elif record.islower():
- */
-    __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_v_fields); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1083; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    goto __pyx_L3;
-  }
-
-  /* "pysam/csamfile.pyx":1085
- *             line.append( fields )
- *         # user tags
- *         elif record.islower():             # <<<<<<<<<<<<<<
- *             for key in sorted(fields):
- *                 line.append( "%s:%s" % (key, str(fields[key])))
- */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_record, __pyx_n_s_islower); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":1086
- *         # user tags
- *         elif record.islower():
- *             for key in sorted(fields):             # <<<<<<<<<<<<<<
- *                 line.append( "%s:%s" % (key, str(fields[key])))
- *         # defined tags
- */
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_INCREF(__pyx_v_fields);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_fields);
-    __Pyx_GIVEREF(__pyx_v_fields);
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_sorted, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) {
-      __pyx_t_1 = __pyx_t_2; __Pyx_INCREF(__pyx_t_1); __pyx_t_5 = 0;
-      __pyx_t_6 = NULL;
-    } else {
-      __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_6 = Py_TYPE(__pyx_t_1)->tp_iternext;
-    }
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    for (;;) {
-      if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_1)) {
-        if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_1)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_1)) {
-        if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else {
-        __pyx_t_2 = __pyx_t_6(__pyx_t_1);
-        if (unlikely(!__pyx_t_2)) {
-          PyObject* exc_type = PyErr_Occurred();
-          if (exc_type) {
-            if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          break;
-        }
-        __Pyx_GOTREF(__pyx_t_2);
-      }
-      __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_2);
-      __pyx_t_2 = 0;
-
-      /* "pysam/csamfile.pyx":1087
- *         elif record.islower():
- *             for key in sorted(fields):
- *                 line.append( "%s:%s" % (key, str(fields[key])))             # <<<<<<<<<<<<<<
- *         # defined tags
- *         else:
- */
-      __pyx_t_2 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_2);
-      __Pyx_GIVEREF(__pyx_t_2);
-      __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_INCREF(__pyx_v_key);
-      PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_key);
-      __Pyx_GIVEREF(__pyx_v_key);
-      PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_2);
-      __Pyx_GIVEREF(__pyx_t_2);
-      __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    }
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    goto __pyx_L3;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":1091
- *         else:
- *             # write fields of the specification
- *             for key in VALID_HEADER_ORDER[record]:             # <<<<<<<<<<<<<<
- *                 if key in fields:
- *                     line.append( "%s:%s" % (key, str(fields[key])))
- */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_ORDER); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyObject_GetItem(__pyx_t_1, __pyx_v_record); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) {
-      __pyx_t_1 = __pyx_t_2; __Pyx_INCREF(__pyx_t_1); __pyx_t_5 = 0;
-      __pyx_t_6 = NULL;
-    } else {
-      __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_6 = Py_TYPE(__pyx_t_1)->tp_iternext;
-    }
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    for (;;) {
-      if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_1)) {
-        if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_1)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_1)) {
-        if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else {
-        __pyx_t_2 = __pyx_t_6(__pyx_t_1);
-        if (unlikely(!__pyx_t_2)) {
-          PyObject* exc_type = PyErr_Occurred();
-          if (exc_type) {
-            if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          break;
-        }
-        __Pyx_GOTREF(__pyx_t_2);
-      }
-      __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_2);
-      __pyx_t_2 = 0;
-
-      /* "pysam/csamfile.pyx":1092
- *             # write fields of the specification
- *             for key in VALID_HEADER_ORDER[record]:
- *                 if key in fields:             # <<<<<<<<<<<<<<
- *                     line.append( "%s:%s" % (key, str(fields[key])))
- *             # write user fields
- */
-      __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_key, __pyx_v_fields, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1092; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_8 = (__pyx_t_3 != 0);
-      if (__pyx_t_8) {
-
-        /* "pysam/csamfile.pyx":1093
- *             for key in VALID_HEADER_ORDER[record]:
- *                 if key in fields:
- *                     line.append( "%s:%s" % (key, str(fields[key])))             # <<<<<<<<<<<<<<
- *             # write user fields
- *             for key in fields:
- */
-        __pyx_t_2 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_2);
-        __Pyx_GIVEREF(__pyx_t_2);
-        __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_2);
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __Pyx_INCREF(__pyx_v_key);
-        PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_key);
-        __Pyx_GIVEREF(__pyx_v_key);
-        PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_2);
-        __Pyx_GIVEREF(__pyx_t_2);
-        __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_2);
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1093; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        goto __pyx_L8;
-      }
-      __pyx_L8:;
-    }
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":1095
- *                     line.append( "%s:%s" % (key, str(fields[key])))
- *             # write user fields
- *             for key in fields:             # <<<<<<<<<<<<<<
- *                 if not key.isupper():
- *                     line.append( "%s:%s" % (key, str(fields[key])))
- */
-    if (PyList_CheckExact(__pyx_v_fields) || PyTuple_CheckExact(__pyx_v_fields)) {
-      __pyx_t_1 = __pyx_v_fields; __Pyx_INCREF(__pyx_t_1); __pyx_t_5 = 0;
-      __pyx_t_6 = NULL;
-    } else {
-      __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_fields); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_6 = Py_TYPE(__pyx_t_1)->tp_iternext;
-    }
-    for (;;) {
-      if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_1)) {
-        if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_1)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_1)) {
-        if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else {
-        __pyx_t_2 = __pyx_t_6(__pyx_t_1);
-        if (unlikely(!__pyx_t_2)) {
-          PyObject* exc_type = PyErr_Occurred();
-          if (exc_type) {
-            if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1095; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          break;
-        }
-        __Pyx_GOTREF(__pyx_t_2);
-      }
-      __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_2);
-      __pyx_t_2 = 0;
-
-      /* "pysam/csamfile.pyx":1096
- *             # write user fields
- *             for key in fields:
- *                 if not key.isupper():             # <<<<<<<<<<<<<<
- *                     line.append( "%s:%s" % (key, str(fields[key])))
- * 
- */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_isupper); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1096; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_3 = ((!__pyx_t_8) != 0);
-      if (__pyx_t_3) {
-
-        /* "pysam/csamfile.pyx":1097
- *             for key in fields:
- *                 if not key.isupper():
- *                     line.append( "%s:%s" % (key, str(fields[key])))             # <<<<<<<<<<<<<<
- * 
- *         return "\t".join( line )
- */
-        __pyx_t_7 = PyObject_GetItem(__pyx_v_fields, __pyx_v_key); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_2);
-        PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_7);
-        __Pyx_GIVEREF(__pyx_t_7);
-        __pyx_t_7 = 0;
-        __pyx_t_7 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_2);
-        __Pyx_INCREF(__pyx_v_key);
-        PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_key);
-        __Pyx_GIVEREF(__pyx_v_key);
-        PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_7);
-        __Pyx_GIVEREF(__pyx_t_7);
-        __pyx_t_7 = 0;
-        __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_line, __pyx_t_7); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        goto __pyx_L11;
-      }
-      __pyx_L11:;
-    }
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1099
- *                     line.append( "%s:%s" % (key, str(fields[key])))
- * 
- *         return "\t".join( line )             # <<<<<<<<<<<<<<
- * 
- *     cdef bam_hdr_t * _buildHeader( self, new_header ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_v_line); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1099; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1076
- *             return result
- * 
- *     def _buildLine( self, fields, record ):             # <<<<<<<<<<<<<<
- *         '''build a header line from *fields* dictionary for *record*'''
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile._buildLine", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_line);
-  __Pyx_XDECREF(__pyx_v_key);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1101
- *         return "\t".join( line )
- * 
- *     cdef bam_hdr_t * _buildHeader( self, new_header ):             # <<<<<<<<<<<<<<
- *         '''return a new header built from a dictionary in *new_header*.
- * 
- */
-
-static bam_hdr_t *__pyx_f_5pysam_8csamfile_7Samfile__buildHeader(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self, PyObject *__pyx_v_new_header) {
-  PyObject *__pyx_v_lines = NULL;
-  bam_hdr_t *__pyx_v_dest;
-  PyObject *__pyx_v_record = NULL;
-  PyObject *__pyx_v_ttype = NULL;
-  PyObject *__pyx_v_data = NULL;
-  PyObject *__pyx_v_fields = NULL;
-  PyObject *__pyx_v_text = NULL;
-  PyObject *__pyx_v_btext = 0;
-  PyObject *__pyx_v_bseqname = 0;
-  PyObject *__pyx_v_seqs = NULL;
-  long __pyx_v_x;
-  PyObject *__pyx_v_seqname = NULL;
-  PyObject *__pyx_v_seqlen = NULL;
-  bam_hdr_t *__pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  Py_ssize_t __pyx_t_3;
-  PyObject *(*__pyx_t_4)(PyObject *);
-  int __pyx_t_5;
-  int __pyx_t_6;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  int __pyx_t_9;
-  Py_ssize_t __pyx_t_10;
-  PyObject *(*__pyx_t_11)(PyObject *);
-  PyObject *__pyx_t_12 = NULL;
-  PyObject *(*__pyx_t_13)(PyObject *);
-  char const *__pyx_t_14;
-  PyObject *__pyx_t_15 = NULL;
-  PyObject *__pyx_t_16 = NULL;
-  PyObject *__pyx_t_17 = NULL;
-  int __pyx_t_18;
-  PyObject *__pyx_t_19 = NULL;
-  int32_t __pyx_t_20;
-  uint32_t __pyx_t_21;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_buildHeader", 0);
-  __Pyx_TraceCall("_buildHeader", __pyx_f[0], 1101);
-
-  /* "pysam/csamfile.pyx":1107
- *         '''
- * 
- *         lines = []             # <<<<<<<<<<<<<<
- * 
- *         # check if hash exists
- */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_lines = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1114
- *         cdef bam_hdr_t * dest
- * 
- *         dest = bam_hdr_init()             # <<<<<<<<<<<<<<
- * 
- *         # first: defined tags
- */
-  __pyx_v_dest = bam_hdr_init();
-
-  /* "pysam/csamfile.pyx":1117
- * 
- *         # first: defined tags
- *         for record in VALID_HEADERS:             # <<<<<<<<<<<<<<
- *             if record in new_header:
- *                 ttype = VALID_HEADER_TYPES[record]
- */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADERS); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyList_CheckExact(__pyx_t_1) || PyTuple_CheckExact(__pyx_t_1)) {
-    __pyx_t_2 = __pyx_t_1; __Pyx_INCREF(__pyx_t_2); __pyx_t_3 = 0;
-    __pyx_t_4 = NULL;
-  } else {
-    __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_4 = Py_TYPE(__pyx_t_2)->tp_iternext;
-  }
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  for (;;) {
-    if (!__pyx_t_4 && PyList_CheckExact(__pyx_t_2)) {
-      if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_2)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else if (!__pyx_t_4 && PyTuple_CheckExact(__pyx_t_2)) {
-      if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else {
-      __pyx_t_1 = __pyx_t_4(__pyx_t_2);
-      if (unlikely(!__pyx_t_1)) {
-        PyObject* exc_type = PyErr_Occurred();
-        if (exc_type) {
-          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        break;
-      }
-      __Pyx_GOTREF(__pyx_t_1);
-    }
-    __Pyx_XDECREF_SET(__pyx_v_record, __pyx_t_1);
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":1118
- *         # first: defined tags
- *         for record in VALID_HEADERS:
- *             if record in new_header:             # <<<<<<<<<<<<<<
- *                 ttype = VALID_HEADER_TYPES[record]
- *                 data = new_header[record]
- */
-    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_v_record, __pyx_v_new_header, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = (__pyx_t_5 != 0);
-    if (__pyx_t_6) {
-
-      /* "pysam/csamfile.pyx":1119
- *         for record in VALID_HEADERS:
- *             if record in new_header:
- *                 ttype = VALID_HEADER_TYPES[record]             # <<<<<<<<<<<<<<
- *                 data = new_header[record]
- *                 if type(data) != type(ttype()):
- */
-      __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADER_TYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1119; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_7 = PyObject_GetItem(__pyx_t_1, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1119; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __Pyx_XDECREF_SET(__pyx_v_ttype, __pyx_t_7);
-      __pyx_t_7 = 0;
-
-      /* "pysam/csamfile.pyx":1120
- *             if record in new_header:
- *                 ttype = VALID_HEADER_TYPES[record]
- *                 data = new_header[record]             # <<<<<<<<<<<<<<
- *                 if type(data) != type(ttype()):
- *                     raise ValueError(
- */
-      __pyx_t_7 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1120; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_XDECREF_SET(__pyx_v_data, __pyx_t_7);
-      __pyx_t_7 = 0;
-
-      /* "pysam/csamfile.pyx":1121
- *                 ttype = VALID_HEADER_TYPES[record]
- *                 data = new_header[record]
- *                 if type(data) != type(ttype()):             # <<<<<<<<<<<<<<
- *                     raise ValueError(
- *                         "invalid type for record %s: %s, expected %s" %
- */
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_v_ttype, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1121; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_1 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_v_data)), ((PyObject *)Py_TYPE(__pyx_t_7)), Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1121; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1121; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      if (__pyx_t_6) {
-
-        /* "pysam/csamfile.pyx":1124
- *                     raise ValueError(
- *                         "invalid type for record %s: %s, expected %s" %
- *                         (record, type(data), type(ttype())))             # <<<<<<<<<<<<<<
- *                 if type(data) is dict:
- *                     lines.append(self._buildLine(data, record))
- */
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_v_ttype, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_7 = PyTuple_New(3); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __Pyx_INCREF(__pyx_v_record);
-        PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_record);
-        __Pyx_GIVEREF(__pyx_v_record);
-        __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_data)));
-        PyTuple_SET_ITEM(__pyx_t_7, 1, ((PyObject *)Py_TYPE(__pyx_v_data)));
-        __Pyx_GIVEREF(((PyObject *)Py_TYPE(__pyx_v_data)));
-        __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_t_1)));
-        PyTuple_SET_ITEM(__pyx_t_7, 2, ((PyObject *)Py_TYPE(__pyx_t_1)));
-        __Pyx_GIVEREF(((PyObject *)Py_TYPE(__pyx_t_1)));
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-        /* "pysam/csamfile.pyx":1123
- *                 if type(data) != type(ttype()):
- *                     raise ValueError(
- *                         "invalid type for record %s: %s, expected %s" %             # <<<<<<<<<<<<<<
- *                         (record, type(data), type(ttype())))
- *                 if type(data) is dict:
- */
-        __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_invalid_type_for_record_s_s_expe, __pyx_t_7); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-
-        /* "pysam/csamfile.pyx":1122
- *                 data = new_header[record]
- *                 if type(data) != type(ttype()):
- *                     raise ValueError(             # <<<<<<<<<<<<<<
- *                         "invalid type for record %s: %s, expected %s" %
- *                         (record, type(data), type(ttype())))
- */
-        __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_1);
-        __Pyx_GIVEREF(__pyx_t_1);
-        __pyx_t_1 = 0;
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_7, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-
-      /* "pysam/csamfile.pyx":1125
- *                         "invalid type for record %s: %s, expected %s" %
- *                         (record, type(data), type(ttype())))
- *                 if type(data) is dict:             # <<<<<<<<<<<<<<
- *                     lines.append(self._buildLine(data, record))
- *                 else:
- */
-      __pyx_t_6 = (((PyObject *)Py_TYPE(__pyx_v_data)) == ((PyObject *)((PyObject*)(&PyDict_Type))));
-      __pyx_t_5 = (__pyx_t_6 != 0);
-      if (__pyx_t_5) {
-
-        /* "pysam/csamfile.pyx":1126
- *                         (record, type(data), type(ttype())))
- *                 if type(data) is dict:
- *                     lines.append(self._buildLine(data, record))             # <<<<<<<<<<<<<<
- *                 else:
- *                     for fields in new_header[record]:
- */
-        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __Pyx_INCREF(__pyx_v_data);
-        PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_data);
-        __Pyx_GIVEREF(__pyx_v_data);
-        __Pyx_INCREF(__pyx_v_record);
-        PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_v_record);
-        __Pyx_GIVEREF(__pyx_v_record);
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        goto __pyx_L7;
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":1128
- *                     lines.append(self._buildLine(data, record))
- *                 else:
- *                     for fields in new_header[record]:             # <<<<<<<<<<<<<<
- *                         lines.append(self._buildLine(fields, record))
- * 
- */
-        __pyx_t_8 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-        __Pyx_GOTREF(__pyx_t_8);
-        if (PyList_CheckExact(__pyx_t_8) || PyTuple_CheckExact(__pyx_t_8)) {
-          __pyx_t_7 = __pyx_t_8; __Pyx_INCREF(__pyx_t_7); __pyx_t_10 = 0;
-          __pyx_t_11 = NULL;
-        } else {
-          __pyx_t_10 = -1; __pyx_t_7 = PyObject_GetIter(__pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          __pyx_t_11 = Py_TYPE(__pyx_t_7)->tp_iternext;
-        }
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        for (;;) {
-          if (!__pyx_t_11 && PyList_CheckExact(__pyx_t_7)) {
-            if (__pyx_t_10 >= PyList_GET_SIZE(__pyx_t_7)) break;
-            #if CYTHON_COMPILING_IN_CPYTHON
-            __pyx_t_8 = PyList_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-            #else
-            __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-            #endif
-          } else if (!__pyx_t_11 && PyTuple_CheckExact(__pyx_t_7)) {
-            if (__pyx_t_10 >= PyTuple_GET_SIZE(__pyx_t_7)) break;
-            #if CYTHON_COMPILING_IN_CPYTHON
-            __pyx_t_8 = PyTuple_GET_ITEM(__pyx_t_7, __pyx_t_10); __Pyx_INCREF(__pyx_t_8); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-            #else
-            __pyx_t_8 = PySequence_ITEM(__pyx_t_7, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-            #endif
-          } else {
-            __pyx_t_8 = __pyx_t_11(__pyx_t_7);
-            if (unlikely(!__pyx_t_8)) {
-              PyObject* exc_type = PyErr_Occurred();
-              if (exc_type) {
-                if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-                else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1128; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-              }
-              break;
-            }
-            __Pyx_GOTREF(__pyx_t_8);
-          }
-          __Pyx_XDECREF_SET(__pyx_v_fields, __pyx_t_8);
-          __pyx_t_8 = 0;
-
-          /* "pysam/csamfile.pyx":1129
- *                 else:
- *                     for fields in new_header[record]:
- *                         lines.append(self._buildLine(fields, record))             # <<<<<<<<<<<<<<
- * 
- *         # then: user tags (lower case), sorted alphabetically
- */
-          __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_8);
-          __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_1);
-          __Pyx_INCREF(__pyx_v_fields);
-          PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_fields);
-          __Pyx_GIVEREF(__pyx_v_fields);
-          __Pyx_INCREF(__pyx_v_record);
-          PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_record);
-          __Pyx_GIVEREF(__pyx_v_record);
-          __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_1, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_12);
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-          __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_12); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-        }
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      }
-      __pyx_L7:;
-      goto __pyx_L5;
-    }
-    __pyx_L5:;
-  }
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":1132
- * 
- *         # then: user tags (lower case), sorted alphabetically
- *         for record, data in sorted(new_header.items()):             # <<<<<<<<<<<<<<
- *             if record in VALID_HEADERS: continue
- *             if type( data ) is dict:
- */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_new_header, __pyx_n_s_items); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_7);
-  __Pyx_GIVEREF(__pyx_t_7);
-  __pyx_t_7 = 0;
-  __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_sorted, __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (PyList_CheckExact(__pyx_t_7) || PyTuple_CheckExact(__pyx_t_7)) {
-    __pyx_t_2 = __pyx_t_7; __Pyx_INCREF(__pyx_t_2); __pyx_t_3 = 0;
-    __pyx_t_4 = NULL;
-  } else {
-    __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_4 = Py_TYPE(__pyx_t_2)->tp_iternext;
-  }
-  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-  for (;;) {
-    if (!__pyx_t_4 && PyList_CheckExact(__pyx_t_2)) {
-      if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_2)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_7); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else if (!__pyx_t_4 && PyTuple_CheckExact(__pyx_t_2)) {
-      if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_7); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else {
-      __pyx_t_7 = __pyx_t_4(__pyx_t_2);
-      if (unlikely(!__pyx_t_7)) {
-        PyObject* exc_type = PyErr_Occurred();
-        if (exc_type) {
-          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        break;
-      }
-      __Pyx_GOTREF(__pyx_t_7);
-    }
-    if ((likely(PyTuple_CheckExact(__pyx_t_7))) || (PyList_CheckExact(__pyx_t_7))) {
-      PyObject* sequence = __pyx_t_7;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      Py_ssize_t size = Py_SIZE(sequence);
-      #else
-      Py_ssize_t size = PySequence_Size(sequence);
-      #endif
-      if (unlikely(size != 2)) {
-        if (size > 2) __Pyx_RaiseTooManyValuesError(2);
-        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      #if CYTHON_COMPILING_IN_CPYTHON
-      if (likely(PyTuple_CheckExact(sequence))) {
-        __pyx_t_12 = PyTuple_GET_ITEM(sequence, 0); 
-        __pyx_t_1 = PyTuple_GET_ITEM(sequence, 1); 
-      } else {
-        __pyx_t_12 = PyList_GET_ITEM(sequence, 0); 
-        __pyx_t_1 = PyList_GET_ITEM(sequence, 1); 
-      }
-      __Pyx_INCREF(__pyx_t_12);
-      __Pyx_INCREF(__pyx_t_1);
-      #else
-      __pyx_t_12 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_12);
-      __pyx_t_1 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      #endif
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    } else {
-      Py_ssize_t index = -1;
-      __pyx_t_8 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_13 = Py_TYPE(__pyx_t_8)->tp_iternext;
-      index = 0; __pyx_t_12 = __pyx_t_13(__pyx_t_8); if (unlikely(!__pyx_t_12)) goto __pyx_L12_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_12);
-      index = 1; __pyx_t_1 = __pyx_t_13(__pyx_t_8); if (unlikely(!__pyx_t_1)) goto __pyx_L12_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_1);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_13(__pyx_t_8), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_13 = NULL;
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      goto __pyx_L13_unpacking_done;
-      __pyx_L12_unpacking_failed:;
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_13 = NULL;
-      if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_L13_unpacking_done:;
-    }
-    __Pyx_XDECREF_SET(__pyx_v_record, __pyx_t_12);
-    __pyx_t_12 = 0;
-    __Pyx_XDECREF_SET(__pyx_v_data, __pyx_t_1);
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":1133
- *         # then: user tags (lower case), sorted alphabetically
- *         for record, data in sorted(new_header.items()):
- *             if record in VALID_HEADERS: continue             # <<<<<<<<<<<<<<
- *             if type( data ) is dict:
- *                 lines.append( self._buildLine( data, record ) )
- */
-    __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_VALID_HEADERS); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1133; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_v_record, __pyx_t_7, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1133; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    __pyx_t_6 = (__pyx_t_5 != 0);
-    if (__pyx_t_6) {
-      goto __pyx_L10_continue;
-    }
-
-    /* "pysam/csamfile.pyx":1134
- *         for record, data in sorted(new_header.items()):
- *             if record in VALID_HEADERS: continue
- *             if type( data ) is dict:             # <<<<<<<<<<<<<<
- *                 lines.append( self._buildLine( data, record ) )
- *             else:
- */
-    __pyx_t_6 = (((PyObject *)Py_TYPE(__pyx_v_data)) == ((PyObject *)((PyObject*)(&PyDict_Type))));
-    __pyx_t_5 = (__pyx_t_6 != 0);
-    if (__pyx_t_5) {
-
-      /* "pysam/csamfile.pyx":1135
- *             if record in VALID_HEADERS: continue
- *             if type( data ) is dict:
- *                 lines.append( self._buildLine( data, record ) )             # <<<<<<<<<<<<<<
- *             else:
- *                 for fields in new_header[record]:
- */
-      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __Pyx_INCREF(__pyx_v_data);
-      PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_data);
-      __Pyx_GIVEREF(__pyx_v_data);
-      __Pyx_INCREF(__pyx_v_record);
-      PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_record);
-      __Pyx_GIVEREF(__pyx_v_record);
-      __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_1, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_12);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_12); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1135; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-      goto __pyx_L15;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":1137
- *                 lines.append( self._buildLine( data, record ) )
- *             else:
- *                 for fields in new_header[record]:             # <<<<<<<<<<<<<<
- *                     lines.append( self._buildLine( fields, record ) )
- * 
- */
-      __pyx_t_12 = PyObject_GetItem(__pyx_v_new_header, __pyx_v_record); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_12);
-      if (PyList_CheckExact(__pyx_t_12) || PyTuple_CheckExact(__pyx_t_12)) {
-        __pyx_t_1 = __pyx_t_12; __Pyx_INCREF(__pyx_t_1); __pyx_t_10 = 0;
-        __pyx_t_11 = NULL;
-      } else {
-        __pyx_t_10 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_12); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_11 = Py_TYPE(__pyx_t_1)->tp_iternext;
-      }
-      __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-      for (;;) {
-        if (!__pyx_t_11 && PyList_CheckExact(__pyx_t_1)) {
-          if (__pyx_t_10 >= PyList_GET_SIZE(__pyx_t_1)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_12 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_10); __Pyx_INCREF(__pyx_t_12); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_12 = PySequence_ITEM(__pyx_t_1, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else if (!__pyx_t_11 && PyTuple_CheckExact(__pyx_t_1)) {
-          if (__pyx_t_10 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
-          #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_12 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_10); __Pyx_INCREF(__pyx_t_12); __pyx_t_10++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #else
-          __pyx_t_12 = PySequence_ITEM(__pyx_t_1, __pyx_t_10); __pyx_t_10++; if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          #endif
-        } else {
-          __pyx_t_12 = __pyx_t_11(__pyx_t_1);
-          if (unlikely(!__pyx_t_12)) {
-            PyObject* exc_type = PyErr_Occurred();
-            if (exc_type) {
-              if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-            }
-            break;
-          }
-          __Pyx_GOTREF(__pyx_t_12);
-        }
-        __Pyx_XDECREF_SET(__pyx_v_fields, __pyx_t_12);
-        __pyx_t_12 = 0;
-
-        /* "pysam/csamfile.pyx":1138
- *             else:
- *                 for fields in new_header[record]:
- *                     lines.append( self._buildLine( fields, record ) )             # <<<<<<<<<<<<<<
- * 
- *         text = "\n".join(lines) + "\n"
- */
-        __pyx_t_12 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_buildLine); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_12);
-        __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __Pyx_INCREF(__pyx_v_fields);
-        PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_fields);
-        __Pyx_GIVEREF(__pyx_v_fields);
-        __Pyx_INCREF(__pyx_v_record);
-        PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_v_record);
-        __Pyx_GIVEREF(__pyx_v_record);
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_lines, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      }
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    }
-    __pyx_L15:;
-    __pyx_L10_continue:;
-  }
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":1140
- *                     lines.append( self._buildLine( fields, record ) )
- * 
- *         text = "\n".join(lines) + "\n"             # <<<<<<<<<<<<<<
- *         if dest.text != NULL: free( dest.text )
- *         dest.text = <char*>calloc( len(text), sizeof(char))
- */
-  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__4, __pyx_v_lines); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyNumber_Add(__pyx_t_2, __pyx_kp_s__4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_v_text = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1141
- * 
- *         text = "\n".join(lines) + "\n"
- *         if dest.text != NULL: free( dest.text )             # <<<<<<<<<<<<<<
- *         dest.text = <char*>calloc( len(text), sizeof(char))
- *         dest.l_text = len(text)
- */
-  __pyx_t_5 = ((__pyx_v_dest->text != NULL) != 0);
-  if (__pyx_t_5) {
-    free(__pyx_v_dest->text);
-    goto __pyx_L18;
-  }
-  __pyx_L18:;
-
-  /* "pysam/csamfile.pyx":1142
- *         text = "\n".join(lines) + "\n"
- *         if dest.text != NULL: free( dest.text )
- *         dest.text = <char*>calloc( len(text), sizeof(char))             # <<<<<<<<<<<<<<
- *         dest.l_text = len(text)
- *         cdef bytes btext = text.encode('ascii')
- */
-  __pyx_t_3 = PyObject_Length(__pyx_v_text); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_dest->text = ((char *)calloc(__pyx_t_3, (sizeof(char))));
-
-  /* "pysam/csamfile.pyx":1143
- *         if dest.text != NULL: free( dest.text )
- *         dest.text = <char*>calloc( len(text), sizeof(char))
- *         dest.l_text = len(text)             # <<<<<<<<<<<<<<
- *         cdef bytes btext = text.encode('ascii')
- *         strncpy( dest.text, btext, dest.l_text )
- */
-  __pyx_t_3 = PyObject_Length(__pyx_v_text); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1143; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_dest->l_text = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":1144
- *         dest.text = <char*>calloc( len(text), sizeof(char))
- *         dest.l_text = len(text)
- *         cdef bytes btext = text.encode('ascii')             # <<<<<<<<<<<<<<
- *         strncpy( dest.text, btext, dest.l_text )
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_text, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__50, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (!(likely(PyBytes_CheckExact(__pyx_t_2))||((__pyx_t_2) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_2)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_btext = ((PyObject*)__pyx_t_2);
-  __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":1145
- *         dest.l_text = len(text)
- *         cdef bytes btext = text.encode('ascii')
- *         strncpy( dest.text, btext, dest.l_text )             # <<<<<<<<<<<<<<
- * 
- *         cdef bytes bseqname
- */
-  __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_btext); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1145; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  strncpy(__pyx_v_dest->text, __pyx_t_14, __pyx_v_dest->l_text);
-
-  /* "pysam/csamfile.pyx":1149
- *         cdef bytes bseqname
- *         # collect targets
- *         if "SQ" in new_header:             # <<<<<<<<<<<<<<
- *             seqs = []
- *             for fields in new_header["SQ"]:
- */
-  __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_n_s_SQ, __pyx_v_new_header, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_6 = (__pyx_t_5 != 0);
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":1150
- *         # collect targets
- *         if "SQ" in new_header:
- *             seqs = []             # <<<<<<<<<<<<<<
- *             for fields in new_header["SQ"]:
- *                 try:
- */
-    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_v_seqs = ((PyObject*)__pyx_t_2);
-    __pyx_t_2 = 0;
-
-    /* "pysam/csamfile.pyx":1151
- *         if "SQ" in new_header:
- *             seqs = []
- *             for fields in new_header["SQ"]:             # <<<<<<<<<<<<<<
- *                 try:
- *                     seqs.append( (fields["SN"], fields["LN"] ) )
- */
-    __pyx_t_2 = PyObject_GetItem(__pyx_v_new_header, __pyx_n_s_SQ); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_2);
-    if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) {
-      __pyx_t_1 = __pyx_t_2; __Pyx_INCREF(__pyx_t_1); __pyx_t_3 = 0;
-      __pyx_t_4 = NULL;
-    } else {
-      __pyx_t_3 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_4 = Py_TYPE(__pyx_t_1)->tp_iternext;
-    }
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    for (;;) {
-      if (!__pyx_t_4 && PyList_CheckExact(__pyx_t_1)) {
-        if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_1)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else if (!__pyx_t_4 && PyTuple_CheckExact(__pyx_t_1)) {
-        if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else {
-        __pyx_t_2 = __pyx_t_4(__pyx_t_1);
-        if (unlikely(!__pyx_t_2)) {
-          PyObject* exc_type = PyErr_Occurred();
-          if (exc_type) {
-            if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          break;
-        }
-        __Pyx_GOTREF(__pyx_t_2);
-      }
-      __Pyx_XDECREF_SET(__pyx_v_fields, __pyx_t_2);
-      __pyx_t_2 = 0;
-
-      /* "pysam/csamfile.pyx":1152
- *             seqs = []
- *             for fields in new_header["SQ"]:
- *                 try:             # <<<<<<<<<<<<<<
- *                     seqs.append( (fields["SN"], fields["LN"] ) )
- *                 except KeyError:
- */
-      {
-        __Pyx_ExceptionSave(&__pyx_t_15, &__pyx_t_16, &__pyx_t_17);
-        __Pyx_XGOTREF(__pyx_t_15);
-        __Pyx_XGOTREF(__pyx_t_16);
-        __Pyx_XGOTREF(__pyx_t_17);
-        /*try:*/ {
-
-          /* "pysam/csamfile.pyx":1153
- *             for fields in new_header["SQ"]:
- *                 try:
- *                     seqs.append( (fields["SN"], fields["LN"] ) )             # <<<<<<<<<<<<<<
- *                 except KeyError:
- *                     raise KeyError( "incomplete sequence information in '%s'" % str(fields))
- */
-          __pyx_t_2 = PyObject_GetItem(__pyx_v_fields, __pyx_n_s_SN); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1153; __pyx_clineno = __LINE__; goto __pyx_L22_error;};
-          __Pyx_GOTREF(__pyx_t_2);
-          __pyx_t_8 = PyObject_GetItem(__pyx_v_fields, __pyx_n_s_LN); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1153; __pyx_clineno = __LINE__; goto __pyx_L22_error;};
-          __Pyx_GOTREF(__pyx_t_8);
-          __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1153; __pyx_clineno = __LINE__; goto __pyx_L22_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_2);
-          __Pyx_GIVEREF(__pyx_t_2);
-          PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_8);
-          __Pyx_GIVEREF(__pyx_t_8);
-          __pyx_t_2 = 0;
-          __pyx_t_8 = 0;
-          __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_seqs, __pyx_t_7); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1153; __pyx_clineno = __LINE__; goto __pyx_L22_error;}
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        }
-        __Pyx_XDECREF(__pyx_t_15); __pyx_t_15 = 0;
-        __Pyx_XDECREF(__pyx_t_16); __pyx_t_16 = 0;
-        __Pyx_XDECREF(__pyx_t_17); __pyx_t_17 = 0;
-        goto __pyx_L29_try_end;
-        __pyx_L22_error:;
-        __Pyx_XDECREF(__pyx_t_12); __pyx_t_12 = 0;
-        __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __Pyx_XDECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0;
-
-        /* "pysam/csamfile.pyx":1154
- *                 try:
- *                     seqs.append( (fields["SN"], fields["LN"] ) )
- *                 except KeyError:             # <<<<<<<<<<<<<<
- *                     raise KeyError( "incomplete sequence information in '%s'" % str(fields))
- * 
- */
-        __pyx_t_18 = PyErr_ExceptionMatches(__pyx_builtin_KeyError);
-        if (__pyx_t_18) {
-          __Pyx_AddTraceback("pysam.csamfile.Samfile._buildHeader", __pyx_clineno, __pyx_lineno, __pyx_filename);
-          if (__Pyx_GetException(&__pyx_t_7, &__pyx_t_8, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1154; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
-          __Pyx_GOTREF(__pyx_t_7);
-          __Pyx_GOTREF(__pyx_t_8);
-          __Pyx_GOTREF(__pyx_t_2);
-
-          /* "pysam/csamfile.pyx":1155
- *                     seqs.append( (fields["SN"], fields["LN"] ) )
- *                 except KeyError:
- *                     raise KeyError( "incomplete sequence information in '%s'" % str(fields))             # <<<<<<<<<<<<<<
- * 
- *             dest.n_targets = len(seqs)
- */
-          __pyx_t_12 = PyTuple_New(1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
-          __Pyx_GOTREF(__pyx_t_12);
-          __Pyx_INCREF(__pyx_v_fields);
-          PyTuple_SET_ITEM(__pyx_t_12, 0, __pyx_v_fields);
-          __Pyx_GIVEREF(__pyx_v_fields);
-          __pyx_t_19 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_12, NULL); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
-          __Pyx_GOTREF(__pyx_t_19);
-          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          __pyx_t_12 = __Pyx_PyString_Format(__pyx_kp_s_incomplete_sequence_information, __pyx_t_19); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
-          __Pyx_GOTREF(__pyx_t_12);
-          __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
-          __pyx_t_19 = PyTuple_New(1); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
-          __Pyx_GOTREF(__pyx_t_19);
-          PyTuple_SET_ITEM(__pyx_t_19, 0, __pyx_t_12);
-          __Pyx_GIVEREF(__pyx_t_12);
-          __pyx_t_12 = 0;
-          __pyx_t_12 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_19, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
-          __Pyx_GOTREF(__pyx_t_12);
-          __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
-          __Pyx_Raise(__pyx_t_12, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L24_except_error;}
-          __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-          __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-          goto __pyx_L23_exception_handled;
-        }
-        goto __pyx_L24_except_error;
-        __pyx_L24_except_error:;
-        __Pyx_XGIVEREF(__pyx_t_15);
-        __Pyx_XGIVEREF(__pyx_t_16);
-        __Pyx_XGIVEREF(__pyx_t_17);
-        __Pyx_ExceptionReset(__pyx_t_15, __pyx_t_16, __pyx_t_17);
-        goto __pyx_L1_error;
-        __pyx_L23_exception_handled:;
-        __Pyx_XGIVEREF(__pyx_t_15);
-        __Pyx_XGIVEREF(__pyx_t_16);
-        __Pyx_XGIVEREF(__pyx_t_17);
-        __Pyx_ExceptionReset(__pyx_t_15, __pyx_t_16, __pyx_t_17);
-        __pyx_L29_try_end:;
-      }
-    }
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":1157
- *                     raise KeyError( "incomplete sequence information in '%s'" % str(fields))
- * 
- *             dest.n_targets = len(seqs)             # <<<<<<<<<<<<<<
- *             dest.target_name = <char**>calloc( dest.n_targets, sizeof(char*) )
- *             dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )
- */
-    __pyx_t_3 = PyList_GET_SIZE(__pyx_v_seqs); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1157; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_dest->n_targets = __pyx_t_3;
-
-    /* "pysam/csamfile.pyx":1158
- * 
- *             dest.n_targets = len(seqs)
- *             dest.target_name = <char**>calloc( dest.n_targets, sizeof(char*) )             # <<<<<<<<<<<<<<
- *             dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )
- * 
- */
-    __pyx_v_dest->target_name = ((char **)calloc(__pyx_v_dest->n_targets, (sizeof(char *))));
-
-    /* "pysam/csamfile.pyx":1159
- *             dest.n_targets = len(seqs)
- *             dest.target_name = <char**>calloc( dest.n_targets, sizeof(char*) )
- *             dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )             # <<<<<<<<<<<<<<
- * 
- *             for x from 0 <= x < dest.n_targets:
- */
-    __pyx_v_dest->target_len = ((uint32_t *)calloc(__pyx_v_dest->n_targets, (sizeof(uint32_t))));
-
-    /* "pysam/csamfile.pyx":1161
- *             dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )
- * 
- *             for x from 0 <= x < dest.n_targets:             # <<<<<<<<<<<<<<
- *                 seqname, seqlen = seqs[x]
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
- */
-    __pyx_t_20 = __pyx_v_dest->n_targets;
-    for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_20; __pyx_v_x++) {
-
-      /* "pysam/csamfile.pyx":1162
- * 
- *             for x from 0 <= x < dest.n_targets:
- *                 seqname, seqlen = seqs[x]             # <<<<<<<<<<<<<<
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
- *                 bseqname = seqname.encode('ascii')
- */
-      __pyx_t_1 = __Pyx_GetItemInt_List(__pyx_v_seqs, __pyx_v_x, long, 1, __Pyx_PyInt_From_long, 1, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_1);
-      if ((likely(PyTuple_CheckExact(__pyx_t_1))) || (PyList_CheckExact(__pyx_t_1))) {
-        PyObject* sequence = __pyx_t_1;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        Py_ssize_t size = Py_SIZE(sequence);
-        #else
-        Py_ssize_t size = PySequence_Size(sequence);
-        #endif
-        if (unlikely(size != 2)) {
-          if (size > 2) __Pyx_RaiseTooManyValuesError(2);
-          else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        #if CYTHON_COMPILING_IN_CPYTHON
-        if (likely(PyTuple_CheckExact(sequence))) {
-          __pyx_t_2 = PyTuple_GET_ITEM(sequence, 0); 
-          __pyx_t_8 = PyTuple_GET_ITEM(sequence, 1); 
-        } else {
-          __pyx_t_2 = PyList_GET_ITEM(sequence, 0); 
-          __pyx_t_8 = PyList_GET_ITEM(sequence, 1); 
-        }
-        __Pyx_INCREF(__pyx_t_2);
-        __Pyx_INCREF(__pyx_t_8);
-        #else
-        __pyx_t_2 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_8 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        #endif
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      } else {
-        Py_ssize_t index = -1;
-        __pyx_t_7 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_13 = Py_TYPE(__pyx_t_7)->tp_iternext;
-        index = 0; __pyx_t_2 = __pyx_t_13(__pyx_t_7); if (unlikely(!__pyx_t_2)) goto __pyx_L34_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_2);
-        index = 1; __pyx_t_8 = __pyx_t_13(__pyx_t_7); if (unlikely(!__pyx_t_8)) goto __pyx_L34_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_8);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_13(__pyx_t_7), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_13 = NULL;
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        goto __pyx_L35_unpacking_done;
-        __pyx_L34_unpacking_failed:;
-        __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_13 = NULL;
-        if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_L35_unpacking_done:;
-      }
-      __Pyx_XDECREF_SET(__pyx_v_seqname, __pyx_t_2);
-      __pyx_t_2 = 0;
-      __Pyx_XDECREF_SET(__pyx_v_seqlen, __pyx_t_8);
-      __pyx_t_8 = 0;
-
-      /* "pysam/csamfile.pyx":1163
- *             for x from 0 <= x < dest.n_targets:
- *                 seqname, seqlen = seqs[x]
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )             # <<<<<<<<<<<<<<
- *                 bseqname = seqname.encode('ascii')
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
- */
-      __pyx_t_3 = PyObject_Length(__pyx_v_seqname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      (__pyx_v_dest->target_name[__pyx_v_x]) = ((char *)calloc((__pyx_t_3 + 1), (sizeof(char))));
-
-      /* "pysam/csamfile.pyx":1164
- *                 seqname, seqlen = seqs[x]
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
- *                 bseqname = seqname.encode('ascii')             # <<<<<<<<<<<<<<
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
- *                 dest.target_len[x] = seqlen
- */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_seqname, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__51, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      if (!(likely(PyBytes_CheckExact(__pyx_t_8))||((__pyx_t_8) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_8)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_XDECREF_SET(__pyx_v_bseqname, ((PyObject*)__pyx_t_8));
-      __pyx_t_8 = 0;
-
-      /* "pysam/csamfile.pyx":1165
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
- *                 bseqname = seqname.encode('ascii')
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )             # <<<<<<<<<<<<<<
- *                 dest.target_len[x] = seqlen
- * 
- */
-      __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_bseqname); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_3 = PyObject_Length(__pyx_v_seqname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      strncpy((__pyx_v_dest->target_name[__pyx_v_x]), __pyx_t_14, (__pyx_t_3 + 1));
-
-      /* "pysam/csamfile.pyx":1166
- *                 bseqname = seqname.encode('ascii')
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
- *                 dest.target_len[x] = seqlen             # <<<<<<<<<<<<<<
- * 
- *         return dest
- */
-      __pyx_t_21 = __Pyx_PyInt_As_uint32_t(__pyx_v_seqlen); if (unlikely((__pyx_t_21 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      (__pyx_v_dest->target_len[__pyx_v_x]) = __pyx_t_21;
-    }
-    goto __pyx_L19;
-  }
-  __pyx_L19:;
-
-  /* "pysam/csamfile.pyx":1168
- *                 dest.target_len[x] = seqlen
- * 
- *         return dest             # <<<<<<<<<<<<<<
- * 
- *     ###############################################################
- */
-  __pyx_r = __pyx_v_dest;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1101
- *         return "\t".join( line )
- * 
- *     cdef bam_hdr_t * _buildHeader( self, new_header ):             # <<<<<<<<<<<<<<
- *         '''return a new header built from a dictionary in *new_header*.
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_12);
-  __Pyx_XDECREF(__pyx_t_19);
-  __Pyx_WriteUnraisable("pysam.csamfile.Samfile._buildHeader", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_lines);
-  __Pyx_XDECREF(__pyx_v_record);
-  __Pyx_XDECREF(__pyx_v_ttype);
-  __Pyx_XDECREF(__pyx_v_data);
-  __Pyx_XDECREF(__pyx_v_fields);
-  __Pyx_XDECREF(__pyx_v_text);
-  __Pyx_XDECREF(__pyx_v_btext);
-  __Pyx_XDECREF(__pyx_v_bseqname);
-  __Pyx_XDECREF(__pyx_v_seqs);
-  __Pyx_XDECREF(__pyx_v_seqname);
-  __Pyx_XDECREF(__pyx_v_seqlen);
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1178
- *     ## Possible solutions: deprecate or open new file handle
- *     ###############################################################
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_45__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_45__iter__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_44__iter__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1178);
-
-  /* "pysam/csamfile.pyx":1179
- *     ###############################################################
- *     def __iter__(self):
- *         if not self._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":1180
- *     def __iter__(self):
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         if not self.isbam and self.header.n_targets == 0:
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__52, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1182
- *             raise ValueError( "I/O operation on closed file" )
- * 
- *         if not self.isbam and self.header.n_targets == 0:             # <<<<<<<<<<<<<<
- *             raise NotImplementedError(
- *                 "can not iterate over samfile without header")
- */
-  __pyx_t_4 = ((!(__pyx_v_self->isbam != 0)) != 0);
-  if (__pyx_t_4) {
-    __pyx_t_3 = ((__pyx_v_self->header->n_targets == 0) != 0);
-    __pyx_t_5 = __pyx_t_3;
-  } else {
-    __pyx_t_5 = __pyx_t_4;
-  }
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":1183
- * 
- *         if not self.isbam and self.header.n_targets == 0:
- *             raise NotImplementedError(             # <<<<<<<<<<<<<<
- *                 "can not iterate over samfile without header")
- *         return self
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__53, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1185
- *             raise NotImplementedError(
- *                 "can not iterate over samfile without header")
- *         return self             # <<<<<<<<<<<<<<
- * 
- *     cdef bam1_t * getCurrent( self ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  __pyx_r = ((PyObject *)__pyx_v_self);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1178
- *     ## Possible solutions: deprecate or open new file handle
- *     ###############################################################
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1187
- *         return self
- * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-static bam1_t *__pyx_f_5pysam_8csamfile_7Samfile_getCurrent(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  bam1_t *__pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1187);
-
-  /* "pysam/csamfile.pyx":1188
- * 
- *     cdef bam1_t * getCurrent( self ):
- *         return self.b             # <<<<<<<<<<<<<<
- * 
- *     cdef int cnext(self):
- */
-  __pyx_r = __pyx_v_self->b;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1187
- *         return self
- * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1190
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''
- *         cversion of iterator. Used by :class:`pysam.Samfile.IteratorColumn`.
- */
-
-static int __pyx_f_5pysam_8csamfile_7Samfile_cnext(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1190);
-
-  /* "pysam/csamfile.pyx":1195
- *         '''
- *         cdef int ret
- *         return sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                          self.header,
- *                          self.b)
- */
-  __pyx_r = sam_read1(__pyx_v_self->htsfile, __pyx_v_self->header, __pyx_v_self->b);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1190
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''
- *         cversion of iterator. Used by :class:`pysam.Samfile.IteratorColumn`.
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1199
- *                          self.b)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """
- *         python version of next().
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_47__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_7Samfile_46__next__[] = "\n        python version of next().\n        ";
-#if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_7Samfile_46__next__;
-#endif
-static PyObject *__pyx_pw_5pysam_8csamfile_7Samfile_47__next__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7Samfile_46__next__(((struct __pyx_obj_5pysam_8csamfile_Samfile *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7Samfile_46__next__(struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_self) {
-  int __pyx_v_ret;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1199);
-
-  /* "pysam/csamfile.pyx":1204
- *         """
- *         cdef int ret
- *         ret = sam_read1(self.htsfile, self.header, self.b)             # <<<<<<<<<<<<<<
- *         if (ret >= 0):
- *             return makeAlignedRead(self.b)
- */
-  __pyx_v_ret = sam_read1(__pyx_v_self->htsfile, __pyx_v_self->header, __pyx_v_self->b);
-
-  /* "pysam/csamfile.pyx":1205
- *         cdef int ret
- *         ret = sam_read1(self.htsfile, self.header, self.b)
- *         if (ret >= 0):             # <<<<<<<<<<<<<<
- *             return makeAlignedRead(self.b)
- *         else:
- */
-  __pyx_t_1 = ((__pyx_v_ret >= 0) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1206
- *         ret = sam_read1(self.htsfile, self.header, self.b)
- *         if (ret >= 0):
- *             return makeAlignedRead(self.b)             # <<<<<<<<<<<<<<
- *         else:
- *             raise StopIteration
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":1208
- *             return makeAlignedRead(self.b)
- *         else:
- *             raise StopIteration             # <<<<<<<<<<<<<<
- * 
- * ##-------------------------------------------------------------------
- */
-    __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1199
- *                          self.b)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """
- *         python version of next().
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.Samfile.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1236
- *     '''
- * 
- *     def __init__(self, Samfile samfile, int reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         if not samfile._isOpen():
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  int __pyx_v_reopen;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_reopen,0};
-    PyObject* values[2] = {0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
-          if (value) { values[1] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1236; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    if (values[1]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1236; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-      __pyx_v_reopen = ((int)1);
-    }
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1236; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRow.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRow *)__pyx_v_self), __pyx_v_samfile, __pyx_v_reopen);
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11IteratorRow___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRow *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  char const *__pyx_t_5;
-  htsFile *__pyx_t_6;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1236);
-
-  /* "pysam/csamfile.pyx":1238
- *     def __init__(self, Samfile samfile, int reopen=True):
- * 
- *         if not samfile._isOpen():             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file" )
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = ((!__pyx_t_3) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":1239
- * 
- *         if not samfile._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         # makes sure that samfile stays alive as long as the
- */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__54, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1243
- *         # makes sure that samfile stays alive as long as the
- *         # iterator is alive
- *         self.samfile = samfile             # <<<<<<<<<<<<<<
- * 
- *         # reopen the file - note that this makes the iterator
- */
-  __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __Pyx_GOTREF(__pyx_v_self->samfile);
-  __Pyx_DECREF(((PyObject *)__pyx_v_self->samfile));
-  __pyx_v_self->samfile = __pyx_v_samfile;
-
-  /* "pysam/csamfile.pyx":1247
- *         # reopen the file - note that this makes the iterator
- *         # slow and causes pileup to slow down significantly.
- *         if reopen:             # <<<<<<<<<<<<<<
- *             self.htsfile = hts_open(samfile._filename, 'r')
- *             assert self.htsfile != NULL
- */
-  __pyx_t_4 = (__pyx_v_reopen != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":1248
- *         # slow and causes pileup to slow down significantly.
- *         if reopen:
- *             self.htsfile = hts_open(samfile._filename, 'r')             # <<<<<<<<<<<<<<
- *             assert self.htsfile != NULL
- *             # read header - required for accurate positioning
- */
-    __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_samfile->_filename); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_self->htsfile = hts_open(__pyx_t_5, __pyx_k_r);
-
-    /* "pysam/csamfile.pyx":1249
- *         if reopen:
- *             self.htsfile = hts_open(samfile._filename, 'r')
- *             assert self.htsfile != NULL             # <<<<<<<<<<<<<<
- *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)
- */
-    #ifndef CYTHON_WITHOUT_ASSERTIONS
-    if (unlikely(!Py_OptimizeFlag)) {
-      if (unlikely(!((__pyx_v_self->htsfile != NULL) != 0))) {
-        PyErr_SetNone(PyExc_AssertionError);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1249; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-    }
-    #endif
-
-    /* "pysam/csamfile.pyx":1251
- *             assert self.htsfile != NULL
- *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
- *             self.owns_samfile = True
- *         else:
- */
-    sam_hdr_read(__pyx_v_self->htsfile);
-
-    /* "pysam/csamfile.pyx":1252
- *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)
- *             self.owns_samfile = True             # <<<<<<<<<<<<<<
- *         else:
- *             self.htsfile = self.samfile.htsfile
- */
-    __pyx_v_self->owns_samfile = 1;
-    goto __pyx_L4;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":1254
- *             self.owns_samfile = True
- *         else:
- *             self.htsfile = self.samfile.htsfile             # <<<<<<<<<<<<<<
- *             self.owns_samfile = False
- * 
- */
-    __pyx_t_6 = __pyx_v_self->samfile->htsfile;
-    __pyx_v_self->htsfile = __pyx_t_6;
-
-    /* "pysam/csamfile.pyx":1255
- *         else:
- *             self.htsfile = self.samfile.htsfile
- *             self.owns_samfile = False             # <<<<<<<<<<<<<<
- * 
- *         self.retval = 0
- */
-    __pyx_v_self->owns_samfile = 0;
-  }
-  __pyx_L4:;
-
-  /* "pysam/csamfile.pyx":1257
- *             self.owns_samfile = False
- * 
- *         self.retval = 0             # <<<<<<<<<<<<<<
- * 
- *         self.b = bam_init1()
- */
-  __pyx_v_self->retval = 0;
-
-  /* "pysam/csamfile.pyx":1259
- *         self.retval = 0
- * 
- *         self.b = bam_init1()             # <<<<<<<<<<<<<<
- * 
- *     def __dealloc__(self):
- */
-  __pyx_v_self->b = bam_init1();
-
-  /* "pysam/csamfile.pyx":1236
- *     '''
- * 
- *     def __init__(self, Samfile samfile, int reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         if not samfile._isOpen():
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRow.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1261
- *         self.b = bam_init1()
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         bam_destroy1(self.b)
- *         if self.owns_samfile:
- */
-
-/* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_11IteratorRow_3__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_11IteratorRow_3__dealloc__(PyObject *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_11IteratorRow_2__dealloc__(((struct __pyx_obj_5pysam_8csamfile_IteratorRow *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-}
-
-static void __pyx_pf_5pysam_8csamfile_11IteratorRow_2__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorRow *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1261);
-
-  /* "pysam/csamfile.pyx":1262
- * 
- *     def __dealloc__(self):
- *         bam_destroy1(self.b)             # <<<<<<<<<<<<<<
- *         if self.owns_samfile:
- *             hts_close(self.htsfile)
- */
-  bam_destroy1(__pyx_v_self->b);
-
-  /* "pysam/csamfile.pyx":1263
- *     def __dealloc__(self):
- *         bam_destroy1(self.b)
- *         if self.owns_samfile:             # <<<<<<<<<<<<<<
- *             hts_close(self.htsfile)
- * 
- */
-  __pyx_t_1 = (__pyx_v_self->owns_samfile != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1264
- *         bam_destroy1(self.b)
- *         if self.owns_samfile:
- *             hts_close(self.htsfile)             # <<<<<<<<<<<<<<
- * 
- * cdef class IteratorRowRegion(IteratorRow):
- */
-    hts_close(__pyx_v_self->htsfile);
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1261
- *         self.b = bam_init1()
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         bam_destroy1(self.b)
- *         if self.owns_samfile:
- */
-
-  /* function exit code */
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-}
-
-/* "pysam/csamfile.pyx":1291
- *     """
- * 
- *     def __init__(self, Samfile samfile,             # <<<<<<<<<<<<<<
- *                  int tid, int beg, int end,
- *                  int reopen=True):
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  int __pyx_v_tid;
-  int __pyx_v_beg;
-  int __pyx_v_end;
-  int __pyx_v_reopen;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_tid,&__pyx_n_s_beg,&__pyx_n_s_end,&__pyx_n_s_reopen,0};
-    PyObject* values[5] = {0,0,0,0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_tid)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  2:
-        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_beg)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  3:
-        if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  4:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
-          if (value) { values[4] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    __pyx_v_tid = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    __pyx_v_beg = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_beg == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    __pyx_v_end = __Pyx_PyInt_As_int(values[3]); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1292; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    if (values[4]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[4]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1293; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-
-      /* "pysam/csamfile.pyx":1293
- *     def __init__(self, Samfile samfile,
- *                  int tid, int beg, int end,
- *                  int reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- */
-      __pyx_v_reopen = ((int)1);
-    }
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 4, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowRegion.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self), __pyx_v_samfile, __pyx_v_tid, __pyx_v_beg, __pyx_v_end, __pyx_v_reopen);
-
-  /* "pysam/csamfile.pyx":1291
- *     """
- * 
- *     def __init__(self, Samfile samfile,             # <<<<<<<<<<<<<<
- *                  int tid, int beg, int end,
- *                  int reopen=True):
- */
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_17IteratorRowRegion___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_beg, int __pyx_v_end, int __pyx_v_reopen) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1291);
-
-  /* "pysam/csamfile.pyx":1295
- *                  int reopen=True):
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
- * 
- *         if not samfile._hasIndex():
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-  __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-  /* "pysam/csamfile.pyx":1297
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- * 
- *         if not samfile._hasIndex():             # <<<<<<<<<<<<<<
- *             raise ValueError( "no index available for iteration" )
- * 
- */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_6 = ((!__pyx_t_5) != 0);
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":1298
- * 
- *         if not samfile._hasIndex():
- *             raise ValueError( "no index available for iteration" )             # <<<<<<<<<<<<<<
- * 
- *         self.iter = sam_itr_queryi(
- */
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__55, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1298; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_Raise(__pyx_t_3, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1298; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1300
- *             raise ValueError( "no index available for iteration" )
- * 
- *         self.iter = sam_itr_queryi(             # <<<<<<<<<<<<<<
- *             self.samfile.index,
- *             tid,
- */
-  __pyx_v_self->iter = sam_itr_queryi(__pyx_v_self->__pyx_base.samfile->index, __pyx_v_tid, __pyx_v_beg, __pyx_v_end);
-
-  /* "pysam/csamfile.pyx":1291
- *     """
- * 
- *     def __init__(self, Samfile samfile,             # <<<<<<<<<<<<<<
- *                  int tid, int beg, int end,
- *                  int reopen=True):
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowRegion.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1306
- *             end)
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_3__iter__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1306);
-
-  /* "pysam/csamfile.pyx":1307
- * 
- *     def __iter__(self):
- *         return self             # <<<<<<<<<<<<<<
- * 
- *     cdef bam1_t * getCurrent( self ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  __pyx_r = ((PyObject *)__pyx_v_self);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1306
- *             end)
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1309
- *         return self
- * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-static bam1_t *__pyx_f_5pysam_8csamfile_17IteratorRowRegion_getCurrent(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
-  bam1_t *__pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1309);
-
-  /* "pysam/csamfile.pyx":1310
- * 
- *     cdef bam1_t * getCurrent( self ):
- *         return self.b             # <<<<<<<<<<<<<<
- * 
- *     cdef int cnext(self):
- */
-  __pyx_r = __pyx_v_self->__pyx_base.b;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1309
- *         return self
- * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1312
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''cversion of iterator. Used by IteratorColumn'''
- *         self.retval = hts_itr_next(self.htsfile.fp.bgzf,
- */
-
-static int __pyx_f_5pysam_8csamfile_17IteratorRowRegion_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1312);
-
-  /* "pysam/csamfile.pyx":1314
- *     cdef int cnext(self):
- *         '''cversion of iterator. Used by IteratorColumn'''
- *         self.retval = hts_itr_next(self.htsfile.fp.bgzf,             # <<<<<<<<<<<<<<
- *                                    self.iter,
- *                                    self.b,
- */
-  __pyx_v_self->__pyx_base.retval = hts_itr_next(__pyx_v_self->__pyx_base.htsfile->fp.bgzf, __pyx_v_self->iter, __pyx_v_self->__pyx_base.b, NULL);
-
-  /* "pysam/csamfile.pyx":1312
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''cversion of iterator. Used by IteratorColumn'''
- *         self.retval = hts_itr_next(self.htsfile.fp.bgzf,
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1319
- *                                    NULL)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- *         """
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_5__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_17IteratorRowRegion_4__next__[] = "python version of next().\n        ";
-#if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_17IteratorRowRegion_4__next__;
-#endif
-static PyObject *__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_5__next__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_17IteratorRowRegion_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1319);
-
-  /* "pysam/csamfile.pyx":1322
- *         """python version of next().
- *         """
- *         self.cnext()             # <<<<<<<<<<<<<<
- *         if self.retval < 0:
- *             raise StopIteration
- */
-  ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self->__pyx_vtab)->cnext(__pyx_v_self);
-
-  /* "pysam/csamfile.pyx":1323
- *         """
- *         self.cnext()
- *         if self.retval < 0:             # <<<<<<<<<<<<<<
- *             raise StopIteration
- *         return makeAlignedRead(self.b)
- */
-  __pyx_t_1 = ((__pyx_v_self->__pyx_base.retval < 0) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1324
- *         self.cnext()
- *         if self.retval < 0:
- *             raise StopIteration             # <<<<<<<<<<<<<<
- *         return makeAlignedRead(self.b)
- * 
- */
-    __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1325
- *         if self.retval < 0:
- *             raise StopIteration
- *         return makeAlignedRead(self.b)             # <<<<<<<<<<<<<<
- * 
- *     def __dealloc__(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1319
- *                                    NULL)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- *         """
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowRegion.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1327
- *         return makeAlignedRead(self.b)
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         hts_itr_destroy(self.iter)
- * 
- */
-
-/* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_7__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_7__dealloc__(PyObject *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_6__dealloc__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-}
-
-static void __pyx_pf_5pysam_8csamfile_17IteratorRowRegion_6__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1327);
-
-  /* "pysam/csamfile.pyx":1328
- * 
- *     def __dealloc__(self):
- *         hts_itr_destroy(self.iter)             # <<<<<<<<<<<<<<
- * 
- * cdef class IteratorRowHead(IteratorRow):
- */
-  hts_itr_destroy(__pyx_v_self->iter);
-
-  /* "pysam/csamfile.pyx":1327
- *         return makeAlignedRead(self.b)
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         hts_itr_destroy(self.iter)
- * 
- */
-
-  /* function exit code */
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-}
-
-/* "pysam/csamfile.pyx":1346
- *     """
- * 
- *     def __init__(self, Samfile samfile, int n, int reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  int __pyx_v_n;
-  int __pyx_v_reopen;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_n,&__pyx_n_s_reopen,0};
-    PyObject* values[3] = {0,0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_n)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
-          if (value) { values[2] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    __pyx_v_n = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_n == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    if (values[2]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-      __pyx_v_reopen = ((int)1);
-    }
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowHead.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *)__pyx_v_self), __pyx_v_samfile, __pyx_v_n, __pyx_v_reopen);
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_15IteratorRowHead___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_n, int __pyx_v_reopen) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1346);
-
-  /* "pysam/csamfile.pyx":1348
- *     def __init__(self, Samfile samfile, int n, int reopen=True):
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
- * 
- *         self.max_rows = n
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-  __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-  /* "pysam/csamfile.pyx":1350
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- * 
- *         self.max_rows = n             # <<<<<<<<<<<<<<
- *         self.current_row = 0
- * 
- */
-  __pyx_v_self->max_rows = __pyx_v_n;
-
-  /* "pysam/csamfile.pyx":1351
- * 
- *         self.max_rows = n
- *         self.current_row = 0             # <<<<<<<<<<<<<<
- * 
- *     def __iter__(self):
- */
-  __pyx_v_self->current_row = 0;
-
-  /* "pysam/csamfile.pyx":1346
- *     """
- * 
- *     def __init__(self, Samfile samfile, int n, int reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowHead.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1353
- *         self.current_row = 0
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_15IteratorRowHead_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_15IteratorRowHead_3__iter__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_15IteratorRowHead_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1353);
-
-  /* "pysam/csamfile.pyx":1354
- * 
- *     def __iter__(self):
- *         return self             # <<<<<<<<<<<<<<
- * 
- *     cdef bam1_t * getCurrent( self ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  __pyx_r = ((PyObject *)__pyx_v_self);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1353
- *         self.current_row = 0
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1356
- *         return self
- * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-static bam1_t *__pyx_f_5pysam_8csamfile_15IteratorRowHead_getCurrent(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self) {
-  bam1_t *__pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1356);
-
-  /* "pysam/csamfile.pyx":1357
- * 
- *     cdef bam1_t * getCurrent( self ):
- *         return self.b             # <<<<<<<<<<<<<<
- * 
- *     cdef int cnext(self):
- */
-  __pyx_r = __pyx_v_self->__pyx_base.b;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1356
- *         return self
- * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1359
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''cversion of iterator. Used by IteratorColumn'''
- *         return sam_read1(self.htsfile,
- */
-
-static int __pyx_f_5pysam_8csamfile_15IteratorRowHead_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1359);
-
-  /* "pysam/csamfile.pyx":1361
- *     cdef int cnext(self):
- *         '''cversion of iterator. Used by IteratorColumn'''
- *         return sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                          self.samfile.header,
- *                          self.b)
- */
-  __pyx_r = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1359
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''cversion of iterator. Used by IteratorColumn'''
- *         return sam_read1(self.htsfile,
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1365
- *                          self.b)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_15IteratorRowHead_5__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_15IteratorRowHead_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
-#if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_15IteratorRowHead_4__next__;
-#endif
-static PyObject *__pyx_pw_5pysam_8csamfile_15IteratorRowHead_5__next__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_15IteratorRowHead_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *__pyx_v_self) {
-  int __pyx_v_ret;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1365);
-
-  /* "pysam/csamfile.pyx":1370
- *         pyrex uses this non-standard name instead of next()
- *         """
- *         if self.current_row >= self.max_rows:             # <<<<<<<<<<<<<<
- *             raise StopIteration
- * 
- */
-  __pyx_t_1 = ((__pyx_v_self->current_row >= __pyx_v_self->max_rows) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1371
- *         """
- *         if self.current_row >= self.max_rows:
- *             raise StopIteration             # <<<<<<<<<<<<<<
- * 
- *         cdef int ret
- */
-    __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1374
- * 
- *         cdef int ret
- *         ret = sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                         self.samfile.header, self.b)
- *         if (ret >= 0):
- */
-  __pyx_v_ret = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
-
-  /* "pysam/csamfile.pyx":1376
- *         ret = sam_read1(self.htsfile,
- *                         self.samfile.header, self.b)
- *         if (ret >= 0):             # <<<<<<<<<<<<<<
- *             self.current_row += 1
- *             return makeAlignedRead( self.b )
- */
-  __pyx_t_1 = ((__pyx_v_ret >= 0) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1377
- *                         self.samfile.header, self.b)
- *         if (ret >= 0):
- *             self.current_row += 1             # <<<<<<<<<<<<<<
- *             return makeAlignedRead( self.b )
- *         else:
- */
-    __pyx_v_self->current_row = (__pyx_v_self->current_row + 1);
-
-    /* "pysam/csamfile.pyx":1378
- *         if (ret >= 0):
- *             self.current_row += 1
- *             return makeAlignedRead( self.b )             # <<<<<<<<<<<<<<
- *         else:
- *             raise StopIteration
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":1380
- *             return makeAlignedRead( self.b )
- *         else:
- *             raise StopIteration             # <<<<<<<<<<<<<<
- * 
- * 
- */
-    __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1365
- *                          self.b)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowHead.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1399
- *     """
- * 
- *     def __init__(self, Samfile samfile, int reopen = True):             # <<<<<<<<<<<<<<
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  int __pyx_v_reopen;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_reopen,0};
-    PyObject* values[2] = {0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
-          if (value) { values[1] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1399; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    if (values[1]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1399; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-      __pyx_v_reopen = ((int)1);
-    }
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1399; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAll.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1399; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *)__pyx_v_self), __pyx_v_samfile, __pyx_v_reopen);
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_14IteratorRowAll___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1399);
-
-  /* "pysam/csamfile.pyx":1401
- *     def __init__(self, Samfile samfile, int reopen = True):
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
- * 
- *     def __iter__(self):
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-  __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-  /* "pysam/csamfile.pyx":1399
- *     """
- * 
- *     def __init__(self, Samfile samfile, int reopen = True):             # <<<<<<<<<<<<<<
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAll.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1403
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorRowAll_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorRowAll_3__iter__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorRowAll_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1403);
-
-  /* "pysam/csamfile.pyx":1404
- * 
- *     def __iter__(self):
- *         return self             # <<<<<<<<<<<<<<
- * 
- *     cdef bam1_t * getCurrent( self ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  __pyx_r = ((PyObject *)__pyx_v_self);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1403
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1406
- *         return self
- * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-static bam1_t *__pyx_f_5pysam_8csamfile_14IteratorRowAll_getCurrent(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self) {
-  bam1_t *__pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1406);
-
-  /* "pysam/csamfile.pyx":1407
- * 
- *     cdef bam1_t * getCurrent( self ):
- *         return self.b             # <<<<<<<<<<<<<<
- * 
- *     cdef int cnext(self):
- */
-  __pyx_r = __pyx_v_self->__pyx_base.b;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1406
- *         return self
- * 
- *     cdef bam1_t * getCurrent( self ):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1409
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''cversion of iterator. Used by IteratorColumn'''
- *         return sam_read1(self.htsfile,
- */
-
-static int __pyx_f_5pysam_8csamfile_14IteratorRowAll_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1409);
-
-  /* "pysam/csamfile.pyx":1411
- *     cdef int cnext(self):
- *         '''cversion of iterator. Used by IteratorColumn'''
- *         return sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                          self.samfile.header,
- *                          self.b)
- */
-  __pyx_r = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1409
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''cversion of iterator. Used by IteratorColumn'''
- *         return sam_read1(self.htsfile,
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1415
- *                          self.b)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorRowAll_5__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_14IteratorRowAll_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
-#if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_14IteratorRowAll_4__next__;
-#endif
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorRowAll_5__next__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorRowAll_4__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorRowAll_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *__pyx_v_self) {
-  int __pyx_v_ret;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1415);
-
-  /* "pysam/csamfile.pyx":1421
- *         """
- *         cdef int ret
- *         ret = sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                         self.samfile.header,
- *                         self.b)
- */
-  __pyx_v_ret = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
-
-  /* "pysam/csamfile.pyx":1424
- *                         self.samfile.header,
- *                         self.b)
- *         if (ret >= 0):             # <<<<<<<<<<<<<<
- *             return makeAlignedRead(self.b)
- *         else:
- */
-  __pyx_t_1 = ((__pyx_v_ret >= 0) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1425
- *                         self.b)
- *         if (ret >= 0):
- *             return makeAlignedRead(self.b)             # <<<<<<<<<<<<<<
- *         else:
- *             raise StopIteration
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":1427
- *             return makeAlignedRead(self.b)
- *         else:
- *             raise StopIteration             # <<<<<<<<<<<<<<
- * 
- * 
- */
-    __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1415
- *                          self.b)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAll.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1438
- *     """
- * 
- *     def __init__(self, Samfile samfile, reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  PyObject *__pyx_v_reopen = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_reopen,0};
-    PyObject* values[2] = {0,0};
-    values[1] = ((PyObject *)Py_True);
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
-          if (value) { values[1] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1438; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    __pyx_v_reopen = values[1];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1438; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAllRefs.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1438; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)__pyx_v_self), __pyx_v_samfile, __pyx_v_reopen);
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_reopen) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1438);
-
-  /* "pysam/csamfile.pyx":1440
- *     def __init__(self, Samfile samfile, reopen=True):
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
- * 
- *         if not samfile._hasIndex():
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-  __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_v_reopen) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-  /* "pysam/csamfile.pyx":1442
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- * 
- *         if not samfile._hasIndex():             # <<<<<<<<<<<<<<
- *             raise ValueError("no index available for fetch")
- * 
- */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_hasIndex); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_6 = ((!__pyx_t_5) != 0);
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":1443
- * 
- *         if not samfile._hasIndex():
- *             raise ValueError("no index available for fetch")             # <<<<<<<<<<<<<<
- * 
- *         self.tid = -1
- */
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__56, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_Raise(__pyx_t_3, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1445
- *             raise ValueError("no index available for fetch")
- * 
- *         self.tid = -1             # <<<<<<<<<<<<<<
- * 
- *     def nextiter(self):
- */
-  __pyx_v_self->tid = -1;
-
-  /* "pysam/csamfile.pyx":1438
- *     """
- * 
- *     def __init__(self, Samfile samfile, reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAllRefs.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1447
- *         self.tid = -1
- * 
- *     def nextiter(self):             # <<<<<<<<<<<<<<
- *         self.rowiter = IteratorRowRegion(self.samfile,
- *                                          self.tid,
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_3nextiter(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter[] = "IteratorRowAllRefs.nextiter(self)";
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_3nextiter(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("nextiter (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("nextiter", 0);
-  __Pyx_TraceCall("nextiter", __pyx_f[0], 1447);
-
-  /* "pysam/csamfile.pyx":1449
- *     def nextiter(self):
- *         self.rowiter = IteratorRowRegion(self.samfile,
- *                                          self.tid,             # <<<<<<<<<<<<<<
- *                                          0,
- *                                          1<<29)
- */
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-
-  /* "pysam/csamfile.pyx":1448
- * 
- *     def nextiter(self):
- *         self.rowiter = IteratorRowRegion(self.samfile,             # <<<<<<<<<<<<<<
- *                                          self.tid,
- *                                          0,
- */
-  __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self->__pyx_base.samfile));
-  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self->__pyx_base.samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self->__pyx_base.samfile));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  __Pyx_INCREF(__pyx_int_0);
-  PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_int_0);
-  __Pyx_GIVEREF(__pyx_int_0);
-  __Pyx_INCREF(__pyx_int_536870912);
-  PyTuple_SET_ITEM(__pyx_t_2, 3, __pyx_int_536870912);
-  __Pyx_GIVEREF(__pyx_int_536870912);
-  __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowRegion)), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_GIVEREF(__pyx_t_1);
-  __Pyx_GOTREF(__pyx_v_self->rowiter);
-  __Pyx_DECREF(((PyObject *)__pyx_v_self->rowiter));
-  __pyx_v_self->rowiter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1447
- *         self.tid = -1
- * 
- *     def nextiter(self):             # <<<<<<<<<<<<<<
- *         self.rowiter = IteratorRowRegion(self.samfile,
- *                                          self.tid,
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAllRefs.nextiter", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1453
- *                                          1<<29)
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_5__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_5__iter__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_4__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_4__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1453);
-
-  /* "pysam/csamfile.pyx":1454
- * 
- *     def __iter__(self):
- *         return self             # <<<<<<<<<<<<<<
- * 
- *     def __next__(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  __pyx_r = ((PyObject *)__pyx_v_self);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1453
- *                                          1<<29)
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1456
- *         return self
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_7__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_6__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
-#if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_18IteratorRowAllRefs_6__next__;
-#endif
-static PyObject *__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_7__next__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_18IteratorRowAllRefs_6__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1456);
-
-  /* "pysam/csamfile.pyx":1462
- *         """
- *         # Create an initial iterator
- *         if self.tid == -1:             # <<<<<<<<<<<<<<
- *             if not self.samfile.nreferences:
- *                 raise StopIteration
- */
-  __pyx_t_1 = ((__pyx_v_self->tid == -1) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1463
- *         # Create an initial iterator
- *         if self.tid == -1:
- *             if not self.samfile.nreferences:             # <<<<<<<<<<<<<<
- *                 raise StopIteration
- *             self.tid = 0
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self->__pyx_base.samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_3 = ((!__pyx_t_1) != 0);
-    if (__pyx_t_3) {
-
-      /* "pysam/csamfile.pyx":1464
- *         if self.tid == -1:
- *             if not self.samfile.nreferences:
- *                 raise StopIteration             # <<<<<<<<<<<<<<
- *             self.tid = 0
- *             self.nextiter()
- */
-      __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1464; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":1465
- *             if not self.samfile.nreferences:
- *                 raise StopIteration
- *             self.tid = 0             # <<<<<<<<<<<<<<
- *             self.nextiter()
- * 
- */
-    __pyx_v_self->tid = 0;
-
-    /* "pysam/csamfile.pyx":1466
- *                 raise StopIteration
- *             self.tid = 0
- *             self.nextiter()             # <<<<<<<<<<<<<<
- * 
- *         while 1:
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_nextiter); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1468
- *             self.nextiter()
- * 
- *         while 1:             # <<<<<<<<<<<<<<
- *             self.rowiter.cnext()
- * 
- */
-  while (1) {
-
-    /* "pysam/csamfile.pyx":1469
- * 
- *         while 1:
- *             self.rowiter.cnext()             # <<<<<<<<<<<<<<
- * 
- *             # If current iterator is not exhausted, return aligned read
- */
-    ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion *)__pyx_v_self->rowiter->__pyx_vtab)->cnext(__pyx_v_self->rowiter);
-
-    /* "pysam/csamfile.pyx":1472
- * 
- *             # If current iterator is not exhausted, return aligned read
- *             if self.rowiter.retval>0:             # <<<<<<<<<<<<<<
- *                 return makeAlignedRead(self.rowiter.b)
- * 
- */
-    __pyx_t_3 = ((__pyx_v_self->rowiter->__pyx_base.retval > 0) != 0);
-    if (__pyx_t_3) {
-
-      /* "pysam/csamfile.pyx":1473
- *             # If current iterator is not exhausted, return aligned read
- *             if self.rowiter.retval>0:
- *                 return makeAlignedRead(self.rowiter.b)             # <<<<<<<<<<<<<<
- * 
- *             self.tid += 1
- */
-      __Pyx_XDECREF(__pyx_r);
-      __pyx_t_4 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->rowiter->__pyx_base.b); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1473; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __pyx_r = __pyx_t_4;
-      __pyx_t_4 = 0;
-      goto __pyx_L0;
-    }
-
-    /* "pysam/csamfile.pyx":1475
- *                 return makeAlignedRead(self.rowiter.b)
- * 
- *             self.tid += 1             # <<<<<<<<<<<<<<
- * 
- *             # Otherwise, proceed to next reference or stop
- */
-    __pyx_v_self->tid = (__pyx_v_self->tid + 1);
-
-    /* "pysam/csamfile.pyx":1478
- * 
- *             # Otherwise, proceed to next reference or stop
- *             if self.tid < self.samfile.nreferences:             # <<<<<<<<<<<<<<
- *                 self.nextiter()
- *             else:
- */
-    __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self->__pyx_base.samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = PyObject_RichCompare(__pyx_t_4, __pyx_t_2, Py_LT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    if (__pyx_t_3) {
-
-      /* "pysam/csamfile.pyx":1479
- *             # Otherwise, proceed to next reference or stop
- *             if self.tid < self.samfile.nreferences:
- *                 self.nextiter()             # <<<<<<<<<<<<<<
- *             else:
- *                 raise StopIteration
- */
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_nextiter); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      goto __pyx_L8;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":1481
- *                 self.nextiter()
- *             else:
- *                 raise StopIteration             # <<<<<<<<<<<<<<
- * 
- * 
- */
-      __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-    __pyx_L8:;
-  }
-
-  /* "pysam/csamfile.pyx":1456
- *         return self
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- * 
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowAllRefs.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1494
- *     """
- * 
- *     def __init__(self, Samfile samfile, positions, int reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  PyObject *__pyx_v_positions = 0;
-  int __pyx_v_reopen;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_positions,&__pyx_n_s_reopen,0};
-    PyObject* values[3] = {0,0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_positions)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
-          if (value) { values[2] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    __pyx_v_positions = values[1];
-    if (values[2]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-      __pyx_v_reopen = ((int)1);
-    }
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowSelection.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1494; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)__pyx_v_self), __pyx_v_samfile, __pyx_v_positions, __pyx_v_reopen);
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_20IteratorRowSelection___init__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_positions, int __pyx_v_reopen) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  BGZF *__pyx_t_5;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 1494);
-
-  /* "pysam/csamfile.pyx":1496
- *     def __init__(self, Samfile samfile, positions, int reopen=True):
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)             # <<<<<<<<<<<<<<
- * 
- *         self.positions = positions
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRow)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-  __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, ((PyObject *)__pyx_v_samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-  /* "pysam/csamfile.pyx":1498
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- * 
- *         self.positions = positions             # <<<<<<<<<<<<<<
- *         self.current_pos = 0
- * 
- */
-  __Pyx_INCREF(__pyx_v_positions);
-  __Pyx_GIVEREF(__pyx_v_positions);
-  __Pyx_GOTREF(__pyx_v_self->positions);
-  __Pyx_DECREF(__pyx_v_self->positions);
-  __pyx_v_self->positions = __pyx_v_positions;
-
-  /* "pysam/csamfile.pyx":1499
- * 
- *         self.positions = positions
- *         self.current_pos = 0             # <<<<<<<<<<<<<<
- * 
- *         self.fp = self.htsfile.fp.bgzf
- */
-  __pyx_v_self->current_pos = 0;
-
-  /* "pysam/csamfile.pyx":1501
- *         self.current_pos = 0
- * 
- *         self.fp = self.htsfile.fp.bgzf             # <<<<<<<<<<<<<<
- * 
- *     def __iter__(self):
- */
-  __pyx_t_5 = __pyx_v_self->__pyx_base.htsfile->fp.bgzf;
-  __pyx_v_self->fp = __pyx_t_5;
-
-  /* "pysam/csamfile.pyx":1494
- *     """
- * 
- *     def __init__(self, Samfile samfile, positions, int reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         IteratorRow.__init__(self, samfile, reopen=reopen)
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowSelection.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1503
- *         self.fp = self.htsfile.fp.bgzf
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_3__iter__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorRowSelection_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1503);
-
-  /* "pysam/csamfile.pyx":1504
- * 
- *     def __iter__(self):
- *         return self             # <<<<<<<<<<<<<<
- * 
- *     cdef bam1_t * getCurrent(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  __pyx_r = ((PyObject *)__pyx_v_self);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1503
- *         self.fp = self.htsfile.fp.bgzf
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1506
- *         return self
- * 
- *     cdef bam1_t * getCurrent(self):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-static bam1_t *__pyx_f_5pysam_8csamfile_20IteratorRowSelection_getCurrent(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self) {
-  bam1_t *__pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("getCurrent", 0);
-  __Pyx_TraceCall("getCurrent", __pyx_f[0], 1506);
-
-  /* "pysam/csamfile.pyx":1507
- * 
- *     cdef bam1_t * getCurrent(self):
- *         return self.b             # <<<<<<<<<<<<<<
- * 
- *     cdef int cnext(self):
- */
-  __pyx_r = __pyx_v_self->__pyx_base.b;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1506
- *         return self
- * 
- *     cdef bam1_t * getCurrent(self):             # <<<<<<<<<<<<<<
- *         return self.b
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1509
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''cversion of iterator'''
- * 
- */
-
-static int __pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  Py_ssize_t __pyx_t_2;
-  int __pyx_t_3;
-  int64_t __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1509);
-
-  /* "pysam/csamfile.pyx":1513
- * 
- *         # end iteration if out of positions
- *         if self.current_pos >= len(self.positions): return -1             # <<<<<<<<<<<<<<
- * 
- *         bgzf_seek(self.fp,
- */
-  __pyx_t_1 = __pyx_v_self->positions;
-  __Pyx_INCREF(__pyx_t_1);
-  __pyx_t_2 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = ((__pyx_v_self->current_pos >= __pyx_t_2) != 0);
-  if (__pyx_t_3) {
-    __pyx_r = -1;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":1516
- * 
- *         bgzf_seek(self.fp,
- *                   self.positions[self.current_pos],             # <<<<<<<<<<<<<<
- *                   0)
- *         self.current_pos += 1
- */
-  __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_self->positions, __pyx_v_self->current_pos, int, 1, __Pyx_PyInt_From_int, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1516; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_4 = __Pyx_PyInt_As_int64_t(__pyx_t_1); if (unlikely((__pyx_t_4 == (int64_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1515
- *         if self.current_pos >= len(self.positions): return -1
- * 
- *         bgzf_seek(self.fp,             # <<<<<<<<<<<<<<
- *                   self.positions[self.current_pos],
- *                   0)
- */
-  bgzf_seek(__pyx_v_self->fp, __pyx_t_4, 0);
-
-  /* "pysam/csamfile.pyx":1518
- *                   self.positions[self.current_pos],
- *                   0)
- *         self.current_pos += 1             # <<<<<<<<<<<<<<
- *         return sam_read1(self.htsfile,
- *                          self.samfile.header,
- */
-  __pyx_v_self->current_pos = (__pyx_v_self->current_pos + 1);
-
-  /* "pysam/csamfile.pyx":1519
- *                   0)
- *         self.current_pos += 1
- *         return sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                          self.samfile.header,
- *                          self.b)
- */
-  __pyx_r = sam_read1(__pyx_v_self->__pyx_base.htsfile, __pyx_v_self->__pyx_base.samfile->header, __pyx_v_self->__pyx_base.b);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1509
- *         return self.b
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''cversion of iterator'''
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_WriteUnraisable("pysam.csamfile.IteratorRowSelection.cnext", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1523
- *                          self.b)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_5__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_20IteratorRowSelection_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
-#if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_20IteratorRowSelection_4__next__;
-#endif
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_5__next__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorRowSelection_4__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorRowSelection_4__next__(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *__pyx_v_self) {
-  int __pyx_v_ret;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1523);
-
-  /* "pysam/csamfile.pyx":1529
- *         """
- * 
- *         cdef int ret = self.cnext()             # <<<<<<<<<<<<<<
- *         if (ret >= 0):
- *             return makeAlignedRead(self.b)
- */
-  __pyx_v_ret = ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection *)__pyx_v_self->__pyx_vtab)->cnext(__pyx_v_self);
-
-  /* "pysam/csamfile.pyx":1530
- * 
- *         cdef int ret = self.cnext()
- *         if (ret >= 0):             # <<<<<<<<<<<<<<
- *             return makeAlignedRead(self.b)
- *         else:
- */
-  __pyx_t_1 = ((__pyx_v_ret >= 0) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1531
- *         cdef int ret = self.cnext()
- *         if (ret >= 0):
- *             return makeAlignedRead(self.b)             # <<<<<<<<<<<<<<
- *         else:
- *             raise StopIteration
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makeAlignedRead(__pyx_v_self->__pyx_base.b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":1533
- *             return makeAlignedRead(self.b)
- *         else:
- *             raise StopIteration             # <<<<<<<<<<<<<<
- * 
- * 
- */
-    __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1523
- *                          self.b)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorRowSelection.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1536
- * 
- * 
- * cdef int __advance_all(void *data, bam1_t *b):             # <<<<<<<<<<<<<<
- *     '''advance without any read filtering.
- *     '''
- */
-
-static int __pyx_f_5pysam_8csamfile___advance_all(void *__pyx_v_data, bam1_t *__pyx_v_b) {
-  __pyx_t_5pysam_8csamfile___iterdata *__pyx_v_d;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__advance_all", 0);
-  __Pyx_TraceCall("__advance_all", __pyx_f[0], 1536);
-
-  /* "pysam/csamfile.pyx":1540
- *     '''
- *     cdef __iterdata * d
- *     d = <__iterdata*>data             # <<<<<<<<<<<<<<
- *     return sam_itr_next(d.htsfile, d.iter, b)
- * 
- */
-  __pyx_v_d = ((__pyx_t_5pysam_8csamfile___iterdata *)__pyx_v_data);
-
-  /* "pysam/csamfile.pyx":1541
- *     cdef __iterdata * d
- *     d = <__iterdata*>data
- *     return sam_itr_next(d.htsfile, d.iter, b)             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __pyx_r = sam_itr_next(__pyx_v_d->htsfile, __pyx_v_d->iter, __pyx_v_b);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1536
- * 
- * 
- * cdef int __advance_all(void *data, bam1_t *b):             # <<<<<<<<<<<<<<
- *     '''advance without any read filtering.
- *     '''
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1544
- * 
- * 
- * cdef int __advance_snpcalls(void * data, bam1_t * b):             # <<<<<<<<<<<<<<
- *     '''advance using same filter and read processing as in
- *     the samtools pileup.
- */
-
-static int __pyx_f_5pysam_8csamfile___advance_snpcalls(void *__pyx_v_data, bam1_t *__pyx_v_b) {
-  __pyx_t_5pysam_8csamfile___iterdata *__pyx_v_d;
-  int __pyx_v_ret;
-  int __pyx_v_skip;
-  int __pyx_v_q;
-  int __pyx_v_is_cns;
-  int __pyx_v_is_nobaq;
-  int __pyx_v_capQ_thres;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  int __pyx_t_3;
-  int32_t __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__advance_snpcalls", 0);
-  __Pyx_TraceCall("__advance_snpcalls", __pyx_f[0], 1544);
-
-  /* "pysam/csamfile.pyx":1556
- *     # 2. bam_cap_mapQ
- *     cdef __iterdata * d
- *     d = <__iterdata*>data             # <<<<<<<<<<<<<<
- * 
- *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
- */
-  __pyx_v_d = ((__pyx_t_5pysam_8csamfile___iterdata *)__pyx_v_data);
-
-  /* "pysam/csamfile.pyx":1558
- *     d = <__iterdata*>data
- * 
- *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)             # <<<<<<<<<<<<<<
- *     cdef int skip = 0
- *     cdef int q
- */
-  __pyx_v_ret = sam_itr_next(__pyx_v_d->htsfile, __pyx_v_d->iter, __pyx_v_b);
-
-  /* "pysam/csamfile.pyx":1559
- * 
- *     cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
- *     cdef int skip = 0             # <<<<<<<<<<<<<<
- *     cdef int q
- *     cdef int is_cns = 1
- */
-  __pyx_v_skip = 0;
-
-  /* "pysam/csamfile.pyx":1561
- *     cdef int skip = 0
- *     cdef int q
- *     cdef int is_cns = 1             # <<<<<<<<<<<<<<
- *     cdef int is_nobaq = 0
- *     cdef int capQ_thres = 0
- */
-  __pyx_v_is_cns = 1;
-
-  /* "pysam/csamfile.pyx":1562
- *     cdef int q
- *     cdef int is_cns = 1
- *     cdef int is_nobaq = 0             # <<<<<<<<<<<<<<
- *     cdef int capQ_thres = 0
- * 
- */
-  __pyx_v_is_nobaq = 0;
-
-  /* "pysam/csamfile.pyx":1563
- *     cdef int is_cns = 1
- *     cdef int is_nobaq = 0
- *     cdef int capQ_thres = 0             # <<<<<<<<<<<<<<
- * 
- *     # reload sequence
- */
-  __pyx_v_capQ_thres = 0;
-
-  /* "pysam/csamfile.pyx":1566
- * 
- *     # reload sequence
- *     if d.fastafile != NULL and b.core.tid != d.tid:             # <<<<<<<<<<<<<<
- *         if d.seq != NULL:
- *             free(d.seq)
- */
-  __pyx_t_1 = ((__pyx_v_d->fastafile != NULL) != 0);
-  if (__pyx_t_1) {
-    __pyx_t_2 = ((__pyx_v_b->core.tid != __pyx_v_d->tid) != 0);
-    __pyx_t_3 = __pyx_t_2;
-  } else {
-    __pyx_t_3 = __pyx_t_1;
-  }
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":1567
- *     # reload sequence
- *     if d.fastafile != NULL and b.core.tid != d.tid:
- *         if d.seq != NULL:             # <<<<<<<<<<<<<<
- *             free(d.seq)
- *         d.tid = b.core.tid
- */
-    __pyx_t_3 = ((__pyx_v_d->seq != NULL) != 0);
-    if (__pyx_t_3) {
-
-      /* "pysam/csamfile.pyx":1568
- *     if d.fastafile != NULL and b.core.tid != d.tid:
- *         if d.seq != NULL:
- *             free(d.seq)             # <<<<<<<<<<<<<<
- *         d.tid = b.core.tid
- *         d.seq = faidx_fetch_seq(
- */
-      free(__pyx_v_d->seq);
-      goto __pyx_L4;
-    }
-    __pyx_L4:;
-
-    /* "pysam/csamfile.pyx":1569
- *         if d.seq != NULL:
- *             free(d.seq)
- *         d.tid = b.core.tid             # <<<<<<<<<<<<<<
- *         d.seq = faidx_fetch_seq(
- *             d.fastafile,
- */
-    __pyx_t_4 = __pyx_v_b->core.tid;
-    __pyx_v_d->tid = __pyx_t_4;
-
-    /* "pysam/csamfile.pyx":1570
- *             free(d.seq)
- *         d.tid = b.core.tid
- *         d.seq = faidx_fetch_seq(             # <<<<<<<<<<<<<<
- *             d.fastafile,
- *             d.header.target_name[d.tid],
- */
-    __pyx_v_d->seq = faidx_fetch_seq(__pyx_v_d->fastafile, (__pyx_v_d->header->target_name[__pyx_v_d->tid]), 0, __pyx_v_5pysam_8csamfile_max_pos, (&__pyx_v_d->seq_len));
-
-    /* "pysam/csamfile.pyx":1576
- *             &d.seq_len)
- * 
- *         if d.seq == NULL:             # <<<<<<<<<<<<<<
- *             raise ValueError(
- *                 "reference sequence for '%s' (tid=%i) not found" % \
- */
-    __pyx_t_3 = ((__pyx_v_d->seq == NULL) != 0);
-    if (__pyx_t_3) {
-
-      /* "pysam/csamfile.pyx":1579
- *             raise ValueError(
- *                 "reference sequence for '%s' (tid=%i) not found" % \
- *                 (d.header.target_name[d.tid],             # <<<<<<<<<<<<<<
- *                  d.tid))
- * 
- */
-      __pyx_t_5 = __Pyx_PyBytes_FromString((__pyx_v_d->header->target_name[__pyx_v_d->tid])); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-
-      /* "pysam/csamfile.pyx":1580
- *                 "reference sequence for '%s' (tid=%i) not found" % \
- *                 (d.header.target_name[d.tid],
- *                  d.tid))             # <<<<<<<<<<<<<<
- * 
- * 
- */
-      __pyx_t_6 = __Pyx_PyInt_From_int(__pyx_v_d->tid); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-
-      /* "pysam/csamfile.pyx":1579
- *             raise ValueError(
- *                 "reference sequence for '%s' (tid=%i) not found" % \
- *                 (d.header.target_name[d.tid],             # <<<<<<<<<<<<<<
- *                  d.tid))
- * 
- */
-      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_5);
-      __Pyx_GIVEREF(__pyx_t_5);
-      PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_6);
-      __Pyx_GIVEREF(__pyx_t_6);
-      __pyx_t_5 = 0;
-      __pyx_t_6 = 0;
-
-      /* "pysam/csamfile.pyx":1578
- *         if d.seq == NULL:
- *             raise ValueError(
- *                 "reference sequence for '%s' (tid=%i) not found" % \             # <<<<<<<<<<<<<<
- *                 (d.header.target_name[d.tid],
- *                  d.tid))
- */
-      __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_reference_sequence_for_s_tid_i_n, __pyx_t_7); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-
-      /* "pysam/csamfile.pyx":1577
- * 
- *         if d.seq == NULL:
- *             raise ValueError(             # <<<<<<<<<<<<<<
- *                 "reference sequence for '%s' (tid=%i) not found" % \
- *                 (d.header.target_name[d.tid],
- */
-      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1577; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_6);
-      __Pyx_GIVEREF(__pyx_t_6);
-      __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_7, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1577; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1577; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1583
- * 
- * 
- *     while ret >= 0:             # <<<<<<<<<<<<<<
- *         skip = 0
- * 
- */
-  while (1) {
-    __pyx_t_3 = ((__pyx_v_ret >= 0) != 0);
-    if (!__pyx_t_3) break;
-
-    /* "pysam/csamfile.pyx":1584
- * 
- *     while ret >= 0:
- *         skip = 0             # <<<<<<<<<<<<<<
- * 
- *         # realign read - changes base qualities
- */
-    __pyx_v_skip = 0;
-
-    /* "pysam/csamfile.pyx":1587
- * 
- *         # realign read - changes base qualities
- *         if d.seq != NULL and is_cns and not is_nobaq:             # <<<<<<<<<<<<<<
- *             bam_prob_realn(b, d.seq)
- * 
- */
-    __pyx_t_3 = (__pyx_v_d->seq != NULL);
-    if (__pyx_t_3) {
-      if ((__pyx_v_is_cns != 0)) {
-        __pyx_t_1 = (!(__pyx_v_is_nobaq != 0));
-        __pyx_t_2 = __pyx_t_1;
-      } else {
-        __pyx_t_2 = (__pyx_v_is_cns != 0);
-      }
-      __pyx_t_1 = __pyx_t_2;
-    } else {
-      __pyx_t_1 = __pyx_t_3;
-    }
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1588
- *         # realign read - changes base qualities
- *         if d.seq != NULL and is_cns and not is_nobaq:
- *             bam_prob_realn(b, d.seq)             # <<<<<<<<<<<<<<
- * 
- *         if d.seq != NULL and capQ_thres > 10:
- */
-      bam_prob_realn(__pyx_v_b, __pyx_v_d->seq);
-      goto __pyx_L8;
-    }
-    __pyx_L8:;
-
-    /* "pysam/csamfile.pyx":1590
- *             bam_prob_realn(b, d.seq)
- * 
- *         if d.seq != NULL and capQ_thres > 10:             # <<<<<<<<<<<<<<
- *             q = bam_cap_mapQ(b, d.seq, capQ_thres)
- *             if q < 0:
- */
-    __pyx_t_1 = ((__pyx_v_d->seq != NULL) != 0);
-    if (__pyx_t_1) {
-      __pyx_t_3 = ((__pyx_v_capQ_thres > 10) != 0);
-      __pyx_t_2 = __pyx_t_3;
-    } else {
-      __pyx_t_2 = __pyx_t_1;
-    }
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":1591
- * 
- *         if d.seq != NULL and capQ_thres > 10:
- *             q = bam_cap_mapQ(b, d.seq, capQ_thres)             # <<<<<<<<<<<<<<
- *             if q < 0:
- *                 skip = 1
- */
-      __pyx_v_q = bam_cap_mapQ(__pyx_v_b, __pyx_v_d->seq, __pyx_v_capQ_thres);
-
-      /* "pysam/csamfile.pyx":1592
- *         if d.seq != NULL and capQ_thres > 10:
- *             q = bam_cap_mapQ(b, d.seq, capQ_thres)
- *             if q < 0:             # <<<<<<<<<<<<<<
- *                 skip = 1
- *             elif b.core.qual > q:
- */
-      __pyx_t_2 = ((__pyx_v_q < 0) != 0);
-      if (__pyx_t_2) {
-
-        /* "pysam/csamfile.pyx":1593
- *             q = bam_cap_mapQ(b, d.seq, capQ_thres)
- *             if q < 0:
- *                 skip = 1             # <<<<<<<<<<<<<<
- *             elif b.core.qual > q:
- *                 b.core.qual = q
- */
-        __pyx_v_skip = 1;
-        goto __pyx_L10;
-      }
-
-      /* "pysam/csamfile.pyx":1594
- *             if q < 0:
- *                 skip = 1
- *             elif b.core.qual > q:             # <<<<<<<<<<<<<<
- *                 b.core.qual = q
- *         if b.core.flag & BAM_FUNMAP:
- */
-      __pyx_t_2 = ((__pyx_v_b->core.qual > __pyx_v_q) != 0);
-      if (__pyx_t_2) {
-
-        /* "pysam/csamfile.pyx":1595
- *                 skip = 1
- *             elif b.core.qual > q:
- *                 b.core.qual = q             # <<<<<<<<<<<<<<
- *         if b.core.flag & BAM_FUNMAP:
- *             skip = 1
- */
-        __pyx_v_b->core.qual = __pyx_v_q;
-        goto __pyx_L10;
-      }
-      __pyx_L10:;
-      goto __pyx_L9;
-    }
-    __pyx_L9:;
-
-    /* "pysam/csamfile.pyx":1596
- *             elif b.core.qual > q:
- *                 b.core.qual = q
- *         if b.core.flag & BAM_FUNMAP:             # <<<<<<<<<<<<<<
- *             skip = 1
- *         elif b.core.flag & 1 and not b.core.flag & 2:
- */
-    __pyx_t_2 = ((__pyx_v_b->core.flag & BAM_FUNMAP) != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":1597
- *                 b.core.qual = q
- *         if b.core.flag & BAM_FUNMAP:
- *             skip = 1             # <<<<<<<<<<<<<<
- *         elif b.core.flag & 1 and not b.core.flag & 2:
- *             skip = 1
- */
-      __pyx_v_skip = 1;
-      goto __pyx_L11;
-    }
-
-    /* "pysam/csamfile.pyx":1598
- *         if b.core.flag & BAM_FUNMAP:
- *             skip = 1
- *         elif b.core.flag & 1 and not b.core.flag & 2:             # <<<<<<<<<<<<<<
- *             skip = 1
- * 
- */
-    if (((__pyx_v_b->core.flag & 1) != 0)) {
-      __pyx_t_2 = (!((__pyx_v_b->core.flag & 2) != 0));
-      __pyx_t_1 = __pyx_t_2;
-    } else {
-      __pyx_t_1 = ((__pyx_v_b->core.flag & 1) != 0);
-    }
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1599
- *             skip = 1
- *         elif b.core.flag & 1 and not b.core.flag & 2:
- *             skip = 1             # <<<<<<<<<<<<<<
- * 
- *         if not skip: break
- */
-      __pyx_v_skip = 1;
-      goto __pyx_L11;
-    }
-    __pyx_L11:;
-
-    /* "pysam/csamfile.pyx":1601
- *             skip = 1
- * 
- *         if not skip: break             # <<<<<<<<<<<<<<
- *         # additional filters
- * 
- */
-    __pyx_t_1 = ((!(__pyx_v_skip != 0)) != 0);
-    if (__pyx_t_1) {
-      goto __pyx_L7_break;
-    }
-
-    /* "pysam/csamfile.pyx":1604
- *         # additional filters
- * 
- *         ret = sam_itr_next(d.htsfile, d.iter, b)             # <<<<<<<<<<<<<<
- * 
- *     return ret
- */
-    __pyx_v_ret = sam_itr_next(__pyx_v_d->htsfile, __pyx_v_d->iter, __pyx_v_b);
-  }
-  __pyx_L7_break:;
-
-  /* "pysam/csamfile.pyx":1606
- *         ret = sam_itr_next(d.htsfile, d.iter, b)
- * 
- *     return ret             # <<<<<<<<<<<<<<
- * 
- * cdef class IteratorColumn:
- */
-  __pyx_r = __pyx_v_ret;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1544
- * 
- * 
- * cdef int __advance_snpcalls(void * data, bam1_t * b):             # <<<<<<<<<<<<<<
- *     '''advance using same filter and read processing as in
- *     the samtools pileup.
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_WriteUnraisable("pysam.csamfile.__advance_snpcalls", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1658
- *     '''
- * 
- *     def __cinit__( self, Samfile samfile, **kwargs ):             # <<<<<<<<<<<<<<
- *         self.samfile = samfile
- *         # TODO
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_14IteratorColumn_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_14IteratorColumn_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  PyObject *__pyx_v_kwargs = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__cinit__ (wrapper)", 0);
-  __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1;
-  __Pyx_GOTREF(__pyx_v_kwargs);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,0};
-    PyObject* values[1] = {0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1658; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 1) {
-      goto __pyx_L5_argtuple_error;
-    } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1658; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), __pyx_v_samfile, __pyx_v_kwargs);
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_kwargs);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_14IteratorColumn___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, PyObject *__pyx_v_kwargs) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_t_2;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__cinit__", 0);
-  __Pyx_TraceCall("__cinit__", __pyx_f[0], 1658);
-
-  /* "pysam/csamfile.pyx":1659
- * 
- *     def __cinit__( self, Samfile samfile, **kwargs ):
- *         self.samfile = samfile             # <<<<<<<<<<<<<<
- *         # TODO
- *         # self.mask = kwargs.get("mask", BAM_DEF_MASK )
- */
-  __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __Pyx_GOTREF(__pyx_v_self->samfile);
-  __Pyx_DECREF(((PyObject *)__pyx_v_self->samfile));
-  __pyx_v_self->samfile = __pyx_v_samfile;
-
-  /* "pysam/csamfile.pyx":1662
- *         # TODO
- *         # self.mask = kwargs.get("mask", BAM_DEF_MASK )
- *         self.fastafile = kwargs.get("fastafile", None)             # <<<<<<<<<<<<<<
- *         self.stepper = kwargs.get("stepper", None)
- *         self.max_depth = kwargs.get("max_depth", 8000)
- */
-  __pyx_t_1 = __Pyx_PyDict_GetItemDefault(__pyx_v_kwargs, __pyx_n_s_fastafile, Py_None); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1662; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5pysam_6cfaidx_Fastafile))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1662; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GIVEREF(__pyx_t_1);
-  __Pyx_GOTREF(__pyx_v_self->fastafile);
-  __Pyx_DECREF(((PyObject *)__pyx_v_self->fastafile));
-  __pyx_v_self->fastafile = ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1663
- *         # self.mask = kwargs.get("mask", BAM_DEF_MASK )
- *         self.fastafile = kwargs.get("fastafile", None)
- *         self.stepper = kwargs.get("stepper", None)             # <<<<<<<<<<<<<<
- *         self.max_depth = kwargs.get("max_depth", 8000)
- *         self.iterdata.seq = NULL
- */
-  __pyx_t_1 = __Pyx_PyDict_GetItemDefault(__pyx_v_kwargs, __pyx_n_s_stepper, Py_None); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  __Pyx_GOTREF(__pyx_v_self->stepper);
-  __Pyx_DECREF(__pyx_v_self->stepper);
-  __pyx_v_self->stepper = __pyx_t_1;
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1664
- *         self.fastafile = kwargs.get("fastafile", None)
- *         self.stepper = kwargs.get("stepper", None)
- *         self.max_depth = kwargs.get("max_depth", 8000)             # <<<<<<<<<<<<<<
- *         self.iterdata.seq = NULL
- *         self.tid = 0
- */
-  __pyx_t_1 = __Pyx_PyDict_GetItemDefault(__pyx_v_kwargs, __pyx_n_s_max_depth, __pyx_int_8000); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_2 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_v_self->max_depth = __pyx_t_2;
-
-  /* "pysam/csamfile.pyx":1665
- *         self.stepper = kwargs.get("stepper", None)
- *         self.max_depth = kwargs.get("max_depth", 8000)
- *         self.iterdata.seq = NULL             # <<<<<<<<<<<<<<
- *         self.tid = 0
- *         self.pos = 0
- */
-  __pyx_v_self->iterdata.seq = NULL;
-
-  /* "pysam/csamfile.pyx":1666
- *         self.max_depth = kwargs.get("max_depth", 8000)
- *         self.iterdata.seq = NULL
- *         self.tid = 0             # <<<<<<<<<<<<<<
- *         self.pos = 0
- *         self.n_plp = 0
- */
-  __pyx_v_self->tid = 0;
-
-  /* "pysam/csamfile.pyx":1667
- *         self.iterdata.seq = NULL
- *         self.tid = 0
- *         self.pos = 0             # <<<<<<<<<<<<<<
- *         self.n_plp = 0
- *         self.plp = NULL
- */
-  __pyx_v_self->pos = 0;
-
-  /* "pysam/csamfile.pyx":1668
- *         self.tid = 0
- *         self.pos = 0
- *         self.n_plp = 0             # <<<<<<<<<<<<<<
- *         self.plp = NULL
- *         self.pileup_iter = <bam_plp_t>NULL
- */
-  __pyx_v_self->n_plp = 0;
-
-  /* "pysam/csamfile.pyx":1669
- *         self.pos = 0
- *         self.n_plp = 0
- *         self.plp = NULL             # <<<<<<<<<<<<<<
- *         self.pileup_iter = <bam_plp_t>NULL
- * 
- */
-  __pyx_v_self->plp = NULL;
-
-  /* "pysam/csamfile.pyx":1670
- *         self.n_plp = 0
- *         self.plp = NULL
- *         self.pileup_iter = <bam_plp_t>NULL             # <<<<<<<<<<<<<<
- * 
- *     def __iter__(self):
- */
-  __pyx_v_self->pileup_iter = ((bam_plp_t)NULL);
-
-  /* "pysam/csamfile.pyx":1658
- *     '''
- * 
- *     def __cinit__( self, Samfile samfile, **kwargs ):             # <<<<<<<<<<<<<<
- *         self.samfile = samfile
- *         # TODO
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1672
- *         self.pileup_iter = <bam_plp_t>NULL
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn_2__iter__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_2__iter__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__iter__", 0);
-  __Pyx_TraceCall("__iter__", __pyx_f[0], 1672);
-
-  /* "pysam/csamfile.pyx":1673
- * 
- *     def __iter__(self):
- *         return self             # <<<<<<<<<<<<<<
- * 
- *     cdef int cnext(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  __pyx_r = ((PyObject *)__pyx_v_self);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1672
- *         self.pileup_iter = <bam_plp_t>NULL
- * 
- *     def __iter__(self):             # <<<<<<<<<<<<<<
- *         return self
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1675
- *         return self
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''perform next iteration.
- *         '''
- */
-
-static int __pyx_f_5pysam_8csamfile_14IteratorColumn_cnext(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("cnext", 0);
-  __Pyx_TraceCall("cnext", __pyx_f[0], 1675);
-
-  /* "pysam/csamfile.pyx":1678
- *         '''perform next iteration.
- *         '''
- *         self.plp = bam_plp_auto( self.pileup_iter,             # <<<<<<<<<<<<<<
- *                                  &self.tid,
- *                                  &self.pos,
- */
-  __pyx_v_self->plp = bam_plp_auto(__pyx_v_self->pileup_iter, (&__pyx_v_self->tid), (&__pyx_v_self->pos), (&__pyx_v_self->n_plp));
-
-  /* "pysam/csamfile.pyx":1675
- *         return self
- * 
- *     cdef int cnext(self):             # <<<<<<<<<<<<<<
- *         '''perform next iteration.
- *         '''
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1683
- *                                  &self.n_plp )
- * 
- *     cdef char * getSequence( self ):             # <<<<<<<<<<<<<<
- *         '''return current reference sequence underlying the iterator.
- *         '''
- */
-
-static char *__pyx_f_5pysam_8csamfile_14IteratorColumn_getSequence(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
-  char *__pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("getSequence", 0);
-  __Pyx_TraceCall("getSequence", __pyx_f[0], 1683);
-
-  /* "pysam/csamfile.pyx":1686
- *         '''return current reference sequence underlying the iterator.
- *         '''
- *         return self.iterdata.seq             # <<<<<<<<<<<<<<
- * 
- *     property seq_len:
- */
-  __pyx_r = __pyx_v_self->iterdata.seq;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1683
- *                                  &self.n_plp )
- * 
- *     cdef char * getSequence( self ):             # <<<<<<<<<<<<<<
- *         '''return current reference sequence underlying the iterator.
- *         '''
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1690
- *     property seq_len:
- *         '''current sequence length.'''
- *         def __get__(self): return self.iterdata.seq_len             # <<<<<<<<<<<<<<
- * 
- *     def addReference(self, Fastafile fastafile):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_7seq_len_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_7seq_len_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_7seq_len___get__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 1690);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->iterdata.seq_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.seq_len.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1692
- *         def __get__(self): return self.iterdata.seq_len
- * 
- *     def addReference(self, Fastafile fastafile):             # <<<<<<<<<<<<<<
- *        '''
- *        add reference sequences in *fastafile* to iterator.'''
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_5addReference(PyObject *__pyx_v_self, PyObject *__pyx_v_fastafile); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_14IteratorColumn_4addReference[] = "IteratorColumn.addReference(self, Fastafile fastafile)\n\n       add reference sequences in *fastafile* to iterator.";
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_5addReference(PyObject *__pyx_v_self, PyObject *__pyx_v_fastafile) {
-  CYTHON_UNUSED int __pyx_lineno = 0;
-  CYTHON_UNUSED const char *__pyx_filename = NULL;
-  CYTHON_UNUSED int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("addReference (wrapper)", 0);
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_fastafile), __pyx_ptype_5pysam_6cfaidx_Fastafile, 1, "fastafile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)__pyx_v_fastafile));
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_4addReference(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, struct __pyx_obj_5pysam_6cfaidx_Fastafile *__pyx_v_fastafile) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  faidx_t *__pyx_t_2;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("addReference", 0);
-  __Pyx_TraceCall("addReference", __pyx_f[0], 1692);
-
-  /* "pysam/csamfile.pyx":1695
- *        '''
- *        add reference sequences in *fastafile* to iterator.'''
- *        self.fastafile = fastafile             # <<<<<<<<<<<<<<
- *        if self.iterdata.seq != NULL: free(self.iterdata.seq)
- *        self.iterdata.tid = -1
- */
-  __Pyx_INCREF(((PyObject *)__pyx_v_fastafile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_fastafile));
-  __Pyx_GOTREF(__pyx_v_self->fastafile);
-  __Pyx_DECREF(((PyObject *)__pyx_v_self->fastafile));
-  __pyx_v_self->fastafile = __pyx_v_fastafile;
-
-  /* "pysam/csamfile.pyx":1696
- *        add reference sequences in *fastafile* to iterator.'''
- *        self.fastafile = fastafile
- *        if self.iterdata.seq != NULL: free(self.iterdata.seq)             # <<<<<<<<<<<<<<
- *        self.iterdata.tid = -1
- *        self.iterdata.fastafile = self.fastafile.fastafile
- */
-  __pyx_t_1 = ((__pyx_v_self->iterdata.seq != NULL) != 0);
-  if (__pyx_t_1) {
-    free(__pyx_v_self->iterdata.seq);
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1697
- *        self.fastafile = fastafile
- *        if self.iterdata.seq != NULL: free(self.iterdata.seq)
- *        self.iterdata.tid = -1             # <<<<<<<<<<<<<<
- *        self.iterdata.fastafile = self.fastafile.fastafile
- * 
- */
-  __pyx_v_self->iterdata.tid = -1;
-
-  /* "pysam/csamfile.pyx":1698
- *        if self.iterdata.seq != NULL: free(self.iterdata.seq)
- *        self.iterdata.tid = -1
- *        self.iterdata.fastafile = self.fastafile.fastafile             # <<<<<<<<<<<<<<
- * 
- *     def hasReference(self):
- */
-  __pyx_t_2 = __pyx_v_self->fastafile->fastafile;
-  __pyx_v_self->iterdata.fastafile = __pyx_t_2;
-
-  /* "pysam/csamfile.pyx":1692
- *         def __get__(self): return self.iterdata.seq_len
- * 
- *     def addReference(self, Fastafile fastafile):             # <<<<<<<<<<<<<<
- *        '''
- *        add reference sequences in *fastafile* to iterator.'''
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1700
- *        self.iterdata.fastafile = self.fastafile.fastafile
- * 
- *     def hasReference(self):             # <<<<<<<<<<<<<<
- *         '''
- *         return true if iterator is associated with a reference'''
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_7hasReference(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_14IteratorColumn_6hasReference[] = "IteratorColumn.hasReference(self)\n\n        return true if iterator is associated with a reference";
-static PyObject *__pyx_pw_5pysam_8csamfile_14IteratorColumn_7hasReference(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("hasReference (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_14IteratorColumn_6hasReference(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_14IteratorColumn_6hasReference(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("hasReference", 0);
-  __Pyx_TraceCall("hasReference", __pyx_f[0], 1700);
-
-  /* "pysam/csamfile.pyx":1703
- *         '''
- *         return true if iterator is associated with a reference'''
- *         return self.fastafile             # <<<<<<<<<<<<<<
- * 
- *     cdef setMask(self, mask):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self->fastafile));
-  __pyx_r = ((PyObject *)__pyx_v_self->fastafile);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1700
- *        self.iterdata.fastafile = self.fastafile.fastafile
- * 
- *     def hasReference(self):             # <<<<<<<<<<<<<<
- *         '''
- *         return true if iterator is associated with a reference'''
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1705
- *         return self.fastafile
- * 
- *     cdef setMask(self, mask):             # <<<<<<<<<<<<<<
- *         '''set masking flag in iterator.
- * 
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setMask(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_mask) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("setMask", 0);
-  __Pyx_TraceCall("setMask", __pyx_f[0], 1705);
-
-  /* "pysam/csamfile.pyx":1710
- *         reads with bits set in *mask* will be skipped.
- *         '''
- *         raise NotImplementedError()             # <<<<<<<<<<<<<<
- *         # self.mask = mask
- *         # bam_plp_set_mask( self.pileup_iter, self.mask )
- */
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":1705
- *         return self.fastafile
- * 
- *     cdef setMask(self, mask):             # <<<<<<<<<<<<<<
- *         '''set masking flag in iterator.
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.setMask", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1714
- *         # bam_plp_set_mask( self.pileup_iter, self.mask )
- * 
- *     cdef setupIteratorData( self,             # <<<<<<<<<<<<<<
- *                             int tid,
- *                             int start,
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData *__pyx_optional_args) {
-  int __pyx_v_reopen = ((int)0);
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  htsFile *__pyx_t_6;
-  hts_itr_t *__pyx_t_7;
-  bam_hdr_t *__pyx_t_8;
-  int __pyx_t_9;
-  faidx_t *__pyx_t_10;
-  int __pyx_t_11;
-  int __pyx_t_12;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("setupIteratorData", 0);
-  __Pyx_TraceCall("setupIteratorData", __pyx_f[0], 1714);
-  if (__pyx_optional_args) {
-    if (__pyx_optional_args->__pyx_n > 0) {
-      __pyx_v_reopen = __pyx_optional_args->reopen;
-    }
-  }
-
-  /* "pysam/csamfile.pyx":1721
- *         '''setup the iterator structure'''
- * 
- *         self.iter = IteratorRowRegion(self.samfile, tid, start, end, reopen)             # <<<<<<<<<<<<<<
- *         self.iterdata.htsfile = self.samfile.htsfile
- *         self.iterdata.iter = self.iter.iter
- */
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_start); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_end); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_reopen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = PyTuple_New(5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self->samfile));
-  PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_v_self->samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self->samfile));
-  PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_5, 2, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_5, 3, __pyx_t_3);
-  __Pyx_GIVEREF(__pyx_t_3);
-  PyTuple_SET_ITEM(__pyx_t_5, 4, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  __pyx_t_1 = 0;
-  __pyx_t_2 = 0;
-  __pyx_t_3 = 0;
-  __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowRegion)), __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  __Pyx_GIVEREF(__pyx_t_4);
-  __Pyx_GOTREF(__pyx_v_self->iter);
-  __Pyx_DECREF(((PyObject *)__pyx_v_self->iter));
-  __pyx_v_self->iter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_t_4);
-  __pyx_t_4 = 0;
-
-  /* "pysam/csamfile.pyx":1722
- * 
- *         self.iter = IteratorRowRegion(self.samfile, tid, start, end, reopen)
- *         self.iterdata.htsfile = self.samfile.htsfile             # <<<<<<<<<<<<<<
- *         self.iterdata.iter = self.iter.iter
- *         self.iterdata.seq = NULL
- */
-  __pyx_t_6 = __pyx_v_self->samfile->htsfile;
-  __pyx_v_self->iterdata.htsfile = __pyx_t_6;
-
-  /* "pysam/csamfile.pyx":1723
- *         self.iter = IteratorRowRegion(self.samfile, tid, start, end, reopen)
- *         self.iterdata.htsfile = self.samfile.htsfile
- *         self.iterdata.iter = self.iter.iter             # <<<<<<<<<<<<<<
- *         self.iterdata.seq = NULL
- *         self.iterdata.tid = -1
- */
-  __pyx_t_7 = __pyx_v_self->iter->iter;
-  __pyx_v_self->iterdata.iter = __pyx_t_7;
-
-  /* "pysam/csamfile.pyx":1724
- *         self.iterdata.htsfile = self.samfile.htsfile
- *         self.iterdata.iter = self.iter.iter
- *         self.iterdata.seq = NULL             # <<<<<<<<<<<<<<
- *         self.iterdata.tid = -1
- *         self.iterdata.header = self.samfile.header
- */
-  __pyx_v_self->iterdata.seq = NULL;
-
-  /* "pysam/csamfile.pyx":1725
- *         self.iterdata.iter = self.iter.iter
- *         self.iterdata.seq = NULL
- *         self.iterdata.tid = -1             # <<<<<<<<<<<<<<
- *         self.iterdata.header = self.samfile.header
- * 
- */
-  __pyx_v_self->iterdata.tid = -1;
-
-  /* "pysam/csamfile.pyx":1726
- *         self.iterdata.seq = NULL
- *         self.iterdata.tid = -1
- *         self.iterdata.header = self.samfile.header             # <<<<<<<<<<<<<<
- * 
- *         if self.fastafile != None:
- */
-  __pyx_t_8 = __pyx_v_self->samfile->header;
-  __pyx_v_self->iterdata.header = __pyx_t_8;
-
-  /* "pysam/csamfile.pyx":1728
- *         self.iterdata.header = self.samfile.header
- * 
- *         if self.fastafile != None:             # <<<<<<<<<<<<<<
- *             self.iterdata.fastafile = self.fastafile.fastafile
- *         else:
- */
-  __pyx_t_4 = PyObject_RichCompare(((PyObject *)__pyx_v_self->fastafile), Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_9 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (__pyx_t_9) {
-
-    /* "pysam/csamfile.pyx":1729
- * 
- *         if self.fastafile != None:
- *             self.iterdata.fastafile = self.fastafile.fastafile             # <<<<<<<<<<<<<<
- *         else:
- *             self.iterdata.fastafile = NULL
- */
-    __pyx_t_10 = __pyx_v_self->fastafile->fastafile;
-    __pyx_v_self->iterdata.fastafile = __pyx_t_10;
-    goto __pyx_L3;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":1731
- *             self.iterdata.fastafile = self.fastafile.fastafile
- *         else:
- *             self.iterdata.fastafile = NULL             # <<<<<<<<<<<<<<
- * 
- *         if self.stepper == None or self.stepper == "all":
- */
-    __pyx_v_self->iterdata.fastafile = NULL;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1733
- *             self.iterdata.fastafile = NULL
- * 
- *         if self.stepper == None or self.stepper == "all":             # <<<<<<<<<<<<<<
- *             self.pileup_iter = bam_plp_init(
- *                 <bam_plp_auto_f>&__advance_all,
- */
-  __pyx_t_4 = PyObject_RichCompare(__pyx_v_self->stepper, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_9 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (!__pyx_t_9) {
-    __pyx_t_11 = (__Pyx_PyString_Equals(__pyx_v_self->stepper, __pyx_n_s_all, Py_EQ)); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_12 = __pyx_t_11;
-  } else {
-    __pyx_t_12 = __pyx_t_9;
-  }
-  if (__pyx_t_12) {
-
-    /* "pysam/csamfile.pyx":1734
- * 
- *         if self.stepper == None or self.stepper == "all":
- *             self.pileup_iter = bam_plp_init(             # <<<<<<<<<<<<<<
- *                 <bam_plp_auto_f>&__advance_all,
- *                 &self.iterdata)
- */
-    __pyx_v_self->pileup_iter = bam_plp_init(((bam_plp_auto_f)(&__pyx_f_5pysam_8csamfile___advance_all)), (&__pyx_v_self->iterdata));
-    goto __pyx_L4;
-  }
-
-  /* "pysam/csamfile.pyx":1737
- *                 <bam_plp_auto_f>&__advance_all,
- *                 &self.iterdata)
- *         elif self.stepper == "samtools":             # <<<<<<<<<<<<<<
- *             self.pileup_iter = bam_plp_init(
- *                 <bam_plp_auto_f>&__advance_snpcalls,
- */
-  __pyx_t_12 = (__Pyx_PyString_Equals(__pyx_v_self->stepper, __pyx_n_s_samtools, Py_EQ)); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_12) {
-
-    /* "pysam/csamfile.pyx":1738
- *                 &self.iterdata)
- *         elif self.stepper == "samtools":
- *             self.pileup_iter = bam_plp_init(             # <<<<<<<<<<<<<<
- *                 <bam_plp_auto_f>&__advance_snpcalls,
- *                 &self.iterdata)
- */
-    __pyx_v_self->pileup_iter = bam_plp_init(((bam_plp_auto_f)(&__pyx_f_5pysam_8csamfile___advance_snpcalls)), (&__pyx_v_self->iterdata));
-    goto __pyx_L4;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":1743
- *         else:
- *             raise ValueError(
- *                 "unknown stepper option `%s` in IteratorColumn" % self.stepper)             # <<<<<<<<<<<<<<
- * 
- *         if self.max_depth:
- */
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_unknown_stepper_option_s_in_Iter, __pyx_v_self->stepper); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-
-    /* "pysam/csamfile.pyx":1742
- *                 &self.iterdata)
- *         else:
- *             raise ValueError(             # <<<<<<<<<<<<<<
- *                 "unknown stepper option `%s` in IteratorColumn" % self.stepper)
- * 
- */
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __Pyx_Raise(__pyx_t_4, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-  __pyx_L4:;
-
-  /* "pysam/csamfile.pyx":1745
- *                 "unknown stepper option `%s` in IteratorColumn" % self.stepper)
- * 
- *         if self.max_depth:             # <<<<<<<<<<<<<<
- *             bam_plp_set_maxcnt(self.pileup_iter, self.max_depth)
- * 
- */
-  __pyx_t_12 = (__pyx_v_self->max_depth != 0);
-  if (__pyx_t_12) {
-
-    /* "pysam/csamfile.pyx":1746
- * 
- *         if self.max_depth:
- *             bam_plp_set_maxcnt(self.pileup_iter, self.max_depth)             # <<<<<<<<<<<<<<
- * 
- *         # bam_plp_set_mask( self.pileup_iter, self.mask )
- */
-    bam_plp_set_maxcnt(__pyx_v_self->pileup_iter, __pyx_v_self->max_depth);
-    goto __pyx_L5;
-  }
-  __pyx_L5:;
-
-  /* "pysam/csamfile.pyx":1714
- *         # bam_plp_set_mask( self.pileup_iter, self.mask )
- * 
- *     cdef setupIteratorData( self,             # <<<<<<<<<<<<<<
- *                             int tid,
- *                             int start,
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.setupIteratorData", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1750
- *         # bam_plp_set_mask( self.pileup_iter, self.mask )
- * 
- *     cdef reset( self, tid, start, end ):             # <<<<<<<<<<<<<<
- *         '''reset iterator position.
- * 
- */
-
-static PyObject *__pyx_f_5pysam_8csamfile_14IteratorColumn_reset(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self, PyObject *__pyx_v_tid, PyObject *__pyx_v_start, PyObject *__pyx_v_end) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  hts_itr_t *__pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("reset", 0);
-  __Pyx_TraceCall("reset", __pyx_f[0], 1750);
-
-  /* "pysam/csamfile.pyx":1756
- *         having to incur the full set-up costs.
- *         '''
- *         self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen = 0 )             # <<<<<<<<<<<<<<
- *         self.iterdata.iter = self.iter.iter
- * 
- */
-  __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1756; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self->samfile));
-  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_self->samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self->samfile));
-  __Pyx_INCREF(__pyx_v_tid);
-  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_tid);
-  __Pyx_GIVEREF(__pyx_v_tid);
-  __Pyx_INCREF(__pyx_v_start);
-  PyTuple_SET_ITEM(__pyx_t_1, 2, __pyx_v_start);
-  __Pyx_GIVEREF(__pyx_v_start);
-  __Pyx_INCREF(__pyx_v_end);
-  PyTuple_SET_ITEM(__pyx_t_1, 3, __pyx_v_end);
-  __Pyx_GIVEREF(__pyx_v_end);
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1756; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_reopen, __pyx_int_0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1756; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowRegion)), __pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1756; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_GIVEREF(__pyx_t_3);
-  __Pyx_GOTREF(__pyx_v_self->iter);
-  __Pyx_DECREF(((PyObject *)__pyx_v_self->iter));
-  __pyx_v_self->iter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)__pyx_t_3);
-  __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":1757
- *         '''
- *         self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen = 0 )
- *         self.iterdata.iter = self.iter.iter             # <<<<<<<<<<<<<<
- * 
- *         # invalidate sequence if different tid
- */
-  __pyx_t_4 = __pyx_v_self->iter->iter;
-  __pyx_v_self->iterdata.iter = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":1760
- * 
- *         # invalidate sequence if different tid
- *         if self.tid != tid:             # <<<<<<<<<<<<<<
- *             if self.iterdata.seq != NULL: free( self.iterdata.seq )
- *             self.iterdata.seq = NULL
- */
-  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_v_tid, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":1761
- *         # invalidate sequence if different tid
- *         if self.tid != tid:
- *             if self.iterdata.seq != NULL: free( self.iterdata.seq )             # <<<<<<<<<<<<<<
- *             self.iterdata.seq = NULL
- *             self.iterdata.tid = -1
- */
-    __pyx_t_5 = ((__pyx_v_self->iterdata.seq != NULL) != 0);
-    if (__pyx_t_5) {
-      free(__pyx_v_self->iterdata.seq);
-      goto __pyx_L4;
-    }
-    __pyx_L4:;
-
-    /* "pysam/csamfile.pyx":1762
- *         if self.tid != tid:
- *             if self.iterdata.seq != NULL: free( self.iterdata.seq )
- *             self.iterdata.seq = NULL             # <<<<<<<<<<<<<<
- *             self.iterdata.tid = -1
- * 
- */
-    __pyx_v_self->iterdata.seq = NULL;
-
-    /* "pysam/csamfile.pyx":1763
- *             if self.iterdata.seq != NULL: free( self.iterdata.seq )
- *             self.iterdata.seq = NULL
- *             self.iterdata.tid = -1             # <<<<<<<<<<<<<<
- * 
- *         # self.pileup_iter = bam_plp_init( &__advancepileup, &self.iterdata )
- */
-    __pyx_v_self->iterdata.tid = -1;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1766
- * 
- *         # self.pileup_iter = bam_plp_init( &__advancepileup, &self.iterdata )
- *         bam_plp_reset(self.pileup_iter)             # <<<<<<<<<<<<<<
- * 
- *     def __dealloc__(self):
- */
-  bam_plp_reset(__pyx_v_self->pileup_iter);
-
-  /* "pysam/csamfile.pyx":1750
- *         # bam_plp_set_mask( self.pileup_iter, self.mask )
- * 
- *     cdef reset( self, tid, start, end ):             # <<<<<<<<<<<<<<
- *         '''reset iterator position.
- * 
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumn.reset", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1768
- *         bam_plp_reset(self.pileup_iter)
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         # reset in order to avoid memory leak messages for iterators
- *         # that have not been fully consumed
- */
-
-/* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_14IteratorColumn_9__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_14IteratorColumn_9__dealloc__(PyObject *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-}
-
-static void __pyx_pf_5pysam_8csamfile_14IteratorColumn_8__dealloc__(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 1768);
-
-  /* "pysam/csamfile.pyx":1771
- *         # reset in order to avoid memory leak messages for iterators
- *         # that have not been fully consumed
- *         if self.pileup_iter != <bam_plp_t>NULL:             # <<<<<<<<<<<<<<
- *             bam_plp_reset(self.pileup_iter)
- *             bam_plp_destroy(self.pileup_iter)
- */
-  __pyx_t_1 = ((__pyx_v_self->pileup_iter != ((bam_plp_t)NULL)) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1772
- *         # that have not been fully consumed
- *         if self.pileup_iter != <bam_plp_t>NULL:
- *             bam_plp_reset(self.pileup_iter)             # <<<<<<<<<<<<<<
- *             bam_plp_destroy(self.pileup_iter)
- *             self.pileup_iter = <bam_plp_t>NULL
- */
-    bam_plp_reset(__pyx_v_self->pileup_iter);
-
-    /* "pysam/csamfile.pyx":1773
- *         if self.pileup_iter != <bam_plp_t>NULL:
- *             bam_plp_reset(self.pileup_iter)
- *             bam_plp_destroy(self.pileup_iter)             # <<<<<<<<<<<<<<
- *             self.pileup_iter = <bam_plp_t>NULL
- *             self.plp = <bam_pileup1_t*>NULL
- */
-    bam_plp_destroy(__pyx_v_self->pileup_iter);
-
-    /* "pysam/csamfile.pyx":1774
- *             bam_plp_reset(self.pileup_iter)
- *             bam_plp_destroy(self.pileup_iter)
- *             self.pileup_iter = <bam_plp_t>NULL             # <<<<<<<<<<<<<<
- *             self.plp = <bam_pileup1_t*>NULL
- * 
- */
-    __pyx_v_self->pileup_iter = ((bam_plp_t)NULL);
-
-    /* "pysam/csamfile.pyx":1775
- *             bam_plp_destroy(self.pileup_iter)
- *             self.pileup_iter = <bam_plp_t>NULL
- *             self.plp = <bam_pileup1_t*>NULL             # <<<<<<<<<<<<<<
- * 
- *         if self.iterdata.seq != NULL:
- */
-    __pyx_v_self->plp = ((bam_pileup1_t *)NULL);
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1777
- *             self.plp = <bam_pileup1_t*>NULL
- * 
- *         if self.iterdata.seq != NULL:             # <<<<<<<<<<<<<<
- *             free(self.iterdata.seq)
- *             self.iterdata.seq = NULL
- */
-  __pyx_t_1 = ((__pyx_v_self->iterdata.seq != NULL) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1778
- * 
- *         if self.iterdata.seq != NULL:
- *             free(self.iterdata.seq)             # <<<<<<<<<<<<<<
- *             self.iterdata.seq = NULL
- * 
- */
-    free(__pyx_v_self->iterdata.seq);
-
-    /* "pysam/csamfile.pyx":1779
- *         if self.iterdata.seq != NULL:
- *             free(self.iterdata.seq)
- *             self.iterdata.seq = NULL             # <<<<<<<<<<<<<<
- * 
- * 
- */
-    __pyx_v_self->iterdata.seq = NULL;
-    goto __pyx_L4;
-  }
-  __pyx_L4:;
-
-  /* "pysam/csamfile.pyx":1768
- *         bam_plp_reset(self.pileup_iter)
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         # reset in order to avoid memory leak messages for iterators
- *         # that have not been fully consumed
- */
-
-  /* function exit code */
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-}
-
-/* "pysam/csamfile.pyx":1785
- *     '''iterates over a region only.
- *     '''
- *     def __cinit__(self, Samfile samfile,             # <<<<<<<<<<<<<<
- *                   int tid = 0,
- *                   int start = 0,
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  int __pyx_v_tid;
-  int __pyx_v_start;
-  int __pyx_v_end;
-  int __pyx_v_truncate;
-  CYTHON_UNUSED PyObject *__pyx_v_kwargs = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__cinit__ (wrapper)", 0);
-  __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1;
-  __Pyx_GOTREF(__pyx_v_kwargs);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_tid,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_truncate,0};
-    PyObject* values[5] = {0,0,0,0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_tid);
-          if (value) { values[1] = value; kw_args--; }
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_start);
-          if (value) { values[2] = value; kw_args--; }
-        }
-        case  3:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end);
-          if (value) { values[3] = value; kw_args--; }
-        }
-        case  4:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_truncate);
-          if (value) { values[4] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1785; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    if (values[1]) {
-      __pyx_v_tid = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_tid == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-      __pyx_v_tid = ((int)0);
-    }
-    if (values[2]) {
-      __pyx_v_start = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_start == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1787; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-      __pyx_v_start = ((int)0);
-    }
-    if (values[3]) {
-      __pyx_v_end = __Pyx_PyInt_As_int(values[3]); if (unlikely((__pyx_v_end == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1788; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-      __pyx_v_end = __pyx_k__57;
-    }
-    if (values[4]) {
-      __pyx_v_truncate = __Pyx_PyInt_As_int(values[4]); if (unlikely((__pyx_v_truncate == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1789; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-
-      /* "pysam/csamfile.pyx":1789
- *                   int start = 0,
- *                   int end = max_pos,
- *                   int truncate = False,             # <<<<<<<<<<<<<<
- *                   **kwargs ):
- * 
- */
-      __pyx_v_truncate = ((int)0);
-    }
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 1, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1785; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnRegion.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *)__pyx_v_self), __pyx_v_samfile, __pyx_v_tid, __pyx_v_start, __pyx_v_end, __pyx_v_truncate, __pyx_v_kwargs);
-
-  /* "pysam/csamfile.pyx":1785
- *     '''iterates over a region only.
- *     '''
- *     def __cinit__(self, Samfile samfile,             # <<<<<<<<<<<<<<
- *                   int tid = 0,
- *                   int start = 0,
- */
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_kwargs);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *__pyx_v_self, CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_tid, int __pyx_v_start, int __pyx_v_end, int __pyx_v_truncate, CYTHON_UNUSED PyObject *__pyx_v_kwargs) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData __pyx_t_2;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__cinit__", 0);
-  __Pyx_TraceCall("__cinit__", __pyx_f[0], 1785);
-
-  /* "pysam/csamfile.pyx":1793
- * 
- *         # initialize iterator
- *         self.setupIteratorData( tid, start, end, 1 )             # <<<<<<<<<<<<<<
- *         self.start = start
- *         self.end = end
- */
-  __pyx_t_2.__pyx_n = 1;
-  __pyx_t_2.reopen = 1;
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), __pyx_v_tid, __pyx_v_start, __pyx_v_end, &__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1794
- *         # initialize iterator
- *         self.setupIteratorData( tid, start, end, 1 )
- *         self.start = start             # <<<<<<<<<<<<<<
- *         self.end = end
- *         self.truncate = truncate
- */
-  __pyx_v_self->start = __pyx_v_start;
-
-  /* "pysam/csamfile.pyx":1795
- *         self.setupIteratorData( tid, start, end, 1 )
- *         self.start = start
- *         self.end = end             # <<<<<<<<<<<<<<
- *         self.truncate = truncate
- * 
- */
-  __pyx_v_self->end = __pyx_v_end;
-
-  /* "pysam/csamfile.pyx":1796
- *         self.start = start
- *         self.end = end
- *         self.truncate = truncate             # <<<<<<<<<<<<<<
- * 
- *     def __next__(self):
- */
-  __pyx_v_self->truncate = __pyx_v_truncate;
-
-  /* "pysam/csamfile.pyx":1785
- *     '''iterates over a region only.
- *     '''
- *     def __cinit__(self, Samfile samfile,             # <<<<<<<<<<<<<<
- *                   int tid = 0,
- *                   int start = 0,
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnRegion.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1798
- *         self.truncate = truncate
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- *         """
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_3__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_20IteratorColumnRegion_2__next__[] = "python version of next().\n        ";
-#if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_20IteratorColumnRegion_2__next__;
-#endif
-static PyObject *__pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_3__next__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_20IteratorColumnRegion_2__next__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1798);
-
-  /* "pysam/csamfile.pyx":1802
- *         """
- * 
- *         while 1:             # <<<<<<<<<<<<<<
- *             self.cnext()
- *             if self.n_plp < 0:
- */
-  while (1) {
-
-    /* "pysam/csamfile.pyx":1803
- * 
- *         while 1:
- *             self.cnext()             # <<<<<<<<<<<<<<
- *             if self.n_plp < 0:
- *                 raise ValueError("error during iteration" )
- */
-    ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.cnext(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
-
-    /* "pysam/csamfile.pyx":1804
- *         while 1:
- *             self.cnext()
- *             if self.n_plp < 0:             # <<<<<<<<<<<<<<
- *                 raise ValueError("error during iteration" )
- * 
- */
-    __pyx_t_1 = ((__pyx_v_self->__pyx_base.n_plp < 0) != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1805
- *             self.cnext()
- *             if self.n_plp < 0:
- *                 raise ValueError("error during iteration" )             # <<<<<<<<<<<<<<
- * 
- *             if self.plp == NULL:
- */
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__58, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":1807
- *                 raise ValueError("error during iteration" )
- * 
- *             if self.plp == NULL:             # <<<<<<<<<<<<<<
- *                 raise StopIteration
- * 
- */
-    __pyx_t_1 = ((__pyx_v_self->__pyx_base.plp == NULL) != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1808
- * 
- *             if self.plp == NULL:
- *                 raise StopIteration             # <<<<<<<<<<<<<<
- * 
- *             if self.truncate:
- */
-      __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1808; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":1810
- *                 raise StopIteration
- * 
- *             if self.truncate:             # <<<<<<<<<<<<<<
- *                 if self.start > self.pos: continue
- *                 if self.pos >= self.end: raise StopIteration
- */
-    __pyx_t_1 = (__pyx_v_self->truncate != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1811
- * 
- *             if self.truncate:
- *                 if self.start > self.pos: continue             # <<<<<<<<<<<<<<
- *                 if self.pos >= self.end: raise StopIteration
- * 
- */
-      __pyx_t_1 = ((__pyx_v_self->start > __pyx_v_self->__pyx_base.pos) != 0);
-      if (__pyx_t_1) {
-        goto __pyx_L3_continue;
-      }
-
-      /* "pysam/csamfile.pyx":1812
- *             if self.truncate:
- *                 if self.start > self.pos: continue
- *                 if self.pos >= self.end: raise StopIteration             # <<<<<<<<<<<<<<
- * 
- *             return makePileupProxy(&self.plp,
- */
-      __pyx_t_1 = ((__pyx_v_self->__pyx_base.pos >= __pyx_v_self->end) != 0);
-      if (__pyx_t_1) {
-        __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      goto __pyx_L7;
-    }
-    __pyx_L7:;
-
-    /* "pysam/csamfile.pyx":1814
- *                 if self.pos >= self.end: raise StopIteration
- * 
- *             return makePileupProxy(&self.plp,             # <<<<<<<<<<<<<<
- *                                    self.tid,
- *                                    self.pos,
- */
-    __Pyx_XDECREF(__pyx_r);
-
-    /* "pysam/csamfile.pyx":1817
- *                                    self.tid,
- *                                    self.pos,
- *                                    self.n_plp)             # <<<<<<<<<<<<<<
- * 
- * 
- */
-    __pyx_t_2 = __pyx_f_5pysam_8csamfile_makePileupProxy((&__pyx_v_self->__pyx_base.plp), __pyx_v_self->__pyx_base.tid, __pyx_v_self->__pyx_base.pos, __pyx_v_self->__pyx_base.n_plp); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L0;
-    __pyx_L3_continue:;
-  }
-
-  /* "pysam/csamfile.pyx":1798
- *         self.truncate = truncate
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- *         """
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnRegion.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1824
- *     """
- * 
- *     def __cinit__(self,             # <<<<<<<<<<<<<<
- *                   Samfile samfile,
- *                   **kwargs):
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  CYTHON_UNUSED PyObject *__pyx_v_kwargs = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__cinit__ (wrapper)", 0);
-  __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1;
-  __Pyx_GOTREF(__pyx_v_kwargs);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,0};
-    PyObject* values[1] = {0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1824; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 1) {
-      goto __pyx_L5_argtuple_error;
-    } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1824; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnAllRefs.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1825; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self), __pyx_v_samfile, __pyx_v_kwargs);
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_kwargs);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs___cinit__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, CYTHON_UNUSED PyObject *__pyx_v_kwargs) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_t_2;
-  int __pyx_t_3;
-  struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__cinit__", 0);
-  __Pyx_TraceCall("__cinit__", __pyx_f[0], 1824);
-
-  /* "pysam/csamfile.pyx":1829
- * 
- *         # no iteration over empty files
- *         if not samfile.nreferences:             # <<<<<<<<<<<<<<
- *             raise StopIteration
- * 
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = ((!__pyx_t_2) != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":1830
- *         # no iteration over empty files
- *         if not samfile.nreferences:
- *             raise StopIteration             # <<<<<<<<<<<<<<
- * 
- *         # initialize iterator
- */
-    __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":1833
- * 
- *         # initialize iterator
- *         self.setupIteratorData(self.tid, 0, max_pos, 1)             # <<<<<<<<<<<<<<
- * 
- *     def __next__(self):
- */
-  __pyx_t_4.__pyx_n = 1;
-  __pyx_t_4.reopen = 1;
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), __pyx_v_self->__pyx_base.tid, 0, __pyx_v_5pysam_8csamfile_max_pos, &__pyx_t_4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1824
- *     """
- * 
- *     def __cinit__(self,             # <<<<<<<<<<<<<<
- *                   Samfile samfile,
- *                   **kwargs):
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnAllRefs.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1835
- *         self.setupIteratorData(self.tid, 0, max_pos, 1)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- *         """
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_3__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__[] = "python version of next().\n        ";
-#if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__;
-#endif
-static PyObject *__pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_3__next__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(((struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData __pyx_t_5;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__next__", 0);
-  __Pyx_TraceCall("__next__", __pyx_f[0], 1835);
-
-  /* "pysam/csamfile.pyx":1839
- *         """
- * 
- *         while 1:             # <<<<<<<<<<<<<<
- *             self.cnext()
- * 
- */
-  while (1) {
-
-    /* "pysam/csamfile.pyx":1840
- * 
- *         while 1:
- *             self.cnext()             # <<<<<<<<<<<<<<
- * 
- *             if self.n_plp < 0:
- */
-    ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.cnext(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self));
-
-    /* "pysam/csamfile.pyx":1842
- *             self.cnext()
- * 
- *             if self.n_plp < 0:             # <<<<<<<<<<<<<<
- *                 raise ValueError("error during iteration" )
- * 
- */
-    __pyx_t_1 = ((__pyx_v_self->__pyx_base.n_plp < 0) != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1843
- * 
- *             if self.n_plp < 0:
- *                 raise ValueError("error during iteration" )             # <<<<<<<<<<<<<<
- * 
- *             # return result, if within same reference
- */
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__59, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_Raise(__pyx_t_2, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-
-    /* "pysam/csamfile.pyx":1846
- * 
- *             # return result, if within same reference
- *             if self.plp != NULL:             # <<<<<<<<<<<<<<
- *                 return makePileupProxy(&self.plp,
- *                                        self.tid,
- */
-    __pyx_t_1 = ((__pyx_v_self->__pyx_base.plp != NULL) != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1847
- *             # return result, if within same reference
- *             if self.plp != NULL:
- *                 return makePileupProxy(&self.plp,             # <<<<<<<<<<<<<<
- *                                        self.tid,
- *                                        self.pos,
- */
-      __Pyx_XDECREF(__pyx_r);
-
-      /* "pysam/csamfile.pyx":1850
- *                                        self.tid,
- *                                        self.pos,
- *                                        self.n_plp)             # <<<<<<<<<<<<<<
- * 
- *             # otherwise, proceed to next reference or stop
- */
-      __pyx_t_2 = __pyx_f_5pysam_8csamfile_makePileupProxy((&__pyx_v_self->__pyx_base.plp), __pyx_v_self->__pyx_base.tid, __pyx_v_self->__pyx_base.pos, __pyx_v_self->__pyx_base.n_plp); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1847; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __pyx_r = __pyx_t_2;
-      __pyx_t_2 = 0;
-      goto __pyx_L0;
-    }
-
-    /* "pysam/csamfile.pyx":1853
- * 
- *             # otherwise, proceed to next reference or stop
- *             self.tid += 1             # <<<<<<<<<<<<<<
- *             if self.tid < self.samfile.nreferences:
- *                 self.setupIteratorData(self.tid, 0, max_pos, 0)
- */
-    __pyx_v_self->__pyx_base.tid = (__pyx_v_self->__pyx_base.tid + 1);
-
-    /* "pysam/csamfile.pyx":1854
- *             # otherwise, proceed to next reference or stop
- *             self.tid += 1
- *             if self.tid < self.samfile.nreferences:             # <<<<<<<<<<<<<<
- *                 self.setupIteratorData(self.tid, 0, max_pos, 0)
- *             else:
- */
-    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_self->__pyx_base.tid); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self->__pyx_base.samfile), __pyx_n_s_nreferences); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = PyObject_RichCompare(__pyx_t_2, __pyx_t_3, Py_LT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1855
- *             self.tid += 1
- *             if self.tid < self.samfile.nreferences:
- *                 self.setupIteratorData(self.tid, 0, max_pos, 0)             # <<<<<<<<<<<<<<
- *             else:
- *                 raise StopIteration
- */
-      __pyx_t_5.__pyx_n = 1;
-      __pyx_t_5.reopen = 0;
-      __pyx_t_4 = ((struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.setupIteratorData(((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)__pyx_v_self), __pyx_v_self->__pyx_base.tid, 0, __pyx_v_5pysam_8csamfile_max_pos, &__pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      goto __pyx_L7;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":1857
- *                 self.setupIteratorData(self.tid, 0, max_pos, 0)
- *             else:
- *                 raise StopIteration             # <<<<<<<<<<<<<<
- * 
- * ##-------------------------------------------------------------------
- */
-      __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-    __pyx_L7:;
-  }
-
-  /* "pysam/csamfile.pyx":1835
- *         self.setupIteratorData(self.tid, 0, max_pos, 1)
- * 
- *     def __next__(self):             # <<<<<<<<<<<<<<
- *         """python version of next().
- *         """
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.IteratorColumnAllRefs.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1862
- * ##-------------------------------------------------------------------
- * ##-------------------------------------------------------------------
- * cdef inline int32_t query_start(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
- *     cdef uint32_t * cigar_p
- *     cdef uint32_t k, op
- */
-
-static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_start(bam1_t *__pyx_v_src) {
-  uint32_t *__pyx_v_cigar_p;
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_op;
-  uint32_t __pyx_v_start_offset;
-  int32_t __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  uint16_t __pyx_t_2;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("query_start", 0);
-  __Pyx_TraceCall("query_start", __pyx_f[0], 1862);
-
-  /* "pysam/csamfile.pyx":1865
- *     cdef uint32_t * cigar_p
- *     cdef uint32_t k, op
- *     cdef uint32_t start_offset = 0             # <<<<<<<<<<<<<<
- * 
- *     if pysam_get_n_cigar(src):
- */
-  __pyx_v_start_offset = 0;
-
-  /* "pysam/csamfile.pyx":1867
- *     cdef uint32_t start_offset = 0
- * 
- *     if pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *         cigar_p = pysam_bam_get_cigar(src);
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- */
-  __pyx_t_1 = (pysam_get_n_cigar(__pyx_v_src) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1868
- * 
- *     if pysam_get_n_cigar(src):
- *         cigar_p = pysam_bam_get_cigar(src);             # <<<<<<<<<<<<<<
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- */
-    __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-    /* "pysam/csamfile.pyx":1869
- *     if pysam_get_n_cigar(src):
- *         cigar_p = pysam_bam_get_cigar(src);
- *         for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             if op == BAM_CHARD_CLIP:
- */
-    __pyx_t_2 = pysam_get_n_cigar(__pyx_v_src);
-    for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_2; __pyx_v_k++) {
-
-      /* "pysam/csamfile.pyx":1870
- *         cigar_p = pysam_bam_get_cigar(src);
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *             if op == BAM_CHARD_CLIP:
- *                 if start_offset != 0 and start_offset != src.core.l_qseq:
- */
-      __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
-
-      /* "pysam/csamfile.pyx":1871
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             if op == BAM_CHARD_CLIP:             # <<<<<<<<<<<<<<
- *                 if start_offset != 0 and start_offset != src.core.l_qseq:
- *                     PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')
- */
-      __pyx_t_1 = ((__pyx_v_op == BAM_CHARD_CLIP) != 0);
-      if (__pyx_t_1) {
-
-        /* "pysam/csamfile.pyx":1872
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             if op == BAM_CHARD_CLIP:
- *                 if start_offset != 0 and start_offset != src.core.l_qseq:             # <<<<<<<<<<<<<<
- *                     PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')
- *                     return -1
- */
-        __pyx_t_1 = ((__pyx_v_start_offset != 0) != 0);
-        if (__pyx_t_1) {
-          __pyx_t_3 = ((__pyx_v_start_offset != __pyx_v_src->core.l_qseq) != 0);
-          __pyx_t_4 = __pyx_t_3;
-        } else {
-          __pyx_t_4 = __pyx_t_1;
-        }
-        if (__pyx_t_4) {
-
-          /* "pysam/csamfile.pyx":1873
- *             if op == BAM_CHARD_CLIP:
- *                 if start_offset != 0 and start_offset != src.core.l_qseq:
- *                     PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')             # <<<<<<<<<<<<<<
- *                     return -1
- *             elif op == BAM_CSOFT_CLIP:
- */
-          PyErr_SetString(__pyx_builtin_ValueError, __pyx_k_Invalid_clipping_in_CIGAR_string);
-
-          /* "pysam/csamfile.pyx":1874
- *                 if start_offset != 0 and start_offset != src.core.l_qseq:
- *                     PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')
- *                     return -1             # <<<<<<<<<<<<<<
- *             elif op == BAM_CSOFT_CLIP:
- *                 start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-          __pyx_r = -1;
-          goto __pyx_L0;
-        }
-        goto __pyx_L6;
-      }
-
-      /* "pysam/csamfile.pyx":1875
- *                     PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')
- *                     return -1
- *             elif op == BAM_CSOFT_CLIP:             # <<<<<<<<<<<<<<
- *                 start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT
- *             else:
- */
-      __pyx_t_4 = ((__pyx_v_op == BAM_CSOFT_CLIP) != 0);
-      if (__pyx_t_4) {
-
-        /* "pysam/csamfile.pyx":1876
- *                     return -1
- *             elif op == BAM_CSOFT_CLIP:
- *                 start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- *             else:
- *                 break
- */
-        __pyx_v_start_offset = (__pyx_v_start_offset + ((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT));
-        goto __pyx_L6;
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":1878
- *                 start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT
- *             else:
- *                 break             # <<<<<<<<<<<<<<
- * 
- *     return start_offset
- */
-        goto __pyx_L5_break;
-      }
-      __pyx_L6:;
-    }
-    __pyx_L5_break:;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1880
- *                 break
- * 
- *     return start_offset             # <<<<<<<<<<<<<<
- * 
- * ##-------------------------------------------------------------------
- */
-  __pyx_r = __pyx_v_start_offset;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1862
- * ##-------------------------------------------------------------------
- * ##-------------------------------------------------------------------
- * cdef inline int32_t query_start(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
- *     cdef uint32_t * cigar_p
- *     cdef uint32_t k, op
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1885
- * ##-------------------------------------------------------------------
- * ##-------------------------------------------------------------------
- * cdef inline int32_t query_end(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
- *     cdef uint32_t * cigar_p
- *     cdef uint32_t k, op
- */
-
-static CYTHON_INLINE int32_t __pyx_f_5pysam_8csamfile_query_end(bam1_t *__pyx_v_src) {
-  uint32_t *__pyx_v_cigar_p;
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_op;
-  uint32_t __pyx_v_end_offset;
-  int32_t __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_t_2;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("query_end", 0);
-  __Pyx_TraceCall("query_end", __pyx_f[0], 1885);
-
-  /* "pysam/csamfile.pyx":1888
- *     cdef uint32_t * cigar_p
- *     cdef uint32_t k, op
- *     cdef uint32_t end_offset = src.core.l_qseq             # <<<<<<<<<<<<<<
- * 
- *     if pysam_get_n_cigar(src) > 1:
- */
-  __pyx_t_1 = __pyx_v_src->core.l_qseq;
-  __pyx_v_end_offset = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":1890
- *     cdef uint32_t end_offset = src.core.l_qseq
- * 
- *     if pysam_get_n_cigar(src) > 1:             # <<<<<<<<<<<<<<
- *         cigar_p = pysam_bam_get_cigar(src);
- *         for k from pysam_get_n_cigar(src) > k >= 1:
- */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) > 1) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":1891
- * 
- *     if pysam_get_n_cigar(src) > 1:
- *         cigar_p = pysam_bam_get_cigar(src);             # <<<<<<<<<<<<<<
- *         for k from pysam_get_n_cigar(src) > k >= 1:
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- */
-    __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-    /* "pysam/csamfile.pyx":1892
- *     if pysam_get_n_cigar(src) > 1:
- *         cigar_p = pysam_bam_get_cigar(src);
- *         for k from pysam_get_n_cigar(src) > k >= 1:             # <<<<<<<<<<<<<<
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             if op == BAM_CHARD_CLIP:
- */
-    for (__pyx_v_k = pysam_get_n_cigar(__pyx_v_src)-1; __pyx_v_k >= 1; __pyx_v_k--) {
-
-      /* "pysam/csamfile.pyx":1893
- *         cigar_p = pysam_bam_get_cigar(src);
- *         for k from pysam_get_n_cigar(src) > k >= 1:
- *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *             if op == BAM_CHARD_CLIP:
- *                 if end_offset != 0 and end_offset != src.core.l_qseq:
- */
-      __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
-
-      /* "pysam/csamfile.pyx":1894
- *         for k from pysam_get_n_cigar(src) > k >= 1:
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             if op == BAM_CHARD_CLIP:             # <<<<<<<<<<<<<<
- *                 if end_offset != 0 and end_offset != src.core.l_qseq:
- *                     PyErr_SetString(ValueError,
- */
-      __pyx_t_2 = ((__pyx_v_op == BAM_CHARD_CLIP) != 0);
-      if (__pyx_t_2) {
-
-        /* "pysam/csamfile.pyx":1895
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             if op == BAM_CHARD_CLIP:
- *                 if end_offset != 0 and end_offset != src.core.l_qseq:             # <<<<<<<<<<<<<<
- *                     PyErr_SetString(ValueError,
- *                                     'Invalid clipping in CIGAR string')
- */
-        __pyx_t_2 = ((__pyx_v_end_offset != 0) != 0);
-        if (__pyx_t_2) {
-          __pyx_t_3 = ((__pyx_v_end_offset != __pyx_v_src->core.l_qseq) != 0);
-          __pyx_t_4 = __pyx_t_3;
-        } else {
-          __pyx_t_4 = __pyx_t_2;
-        }
-        if (__pyx_t_4) {
-
-          /* "pysam/csamfile.pyx":1896
- *             if op == BAM_CHARD_CLIP:
- *                 if end_offset != 0 and end_offset != src.core.l_qseq:
- *                     PyErr_SetString(ValueError,             # <<<<<<<<<<<<<<
- *                                     'Invalid clipping in CIGAR string')
- *                     return -1
- */
-          PyErr_SetString(__pyx_builtin_ValueError, __pyx_k_Invalid_clipping_in_CIGAR_string);
-
-          /* "pysam/csamfile.pyx":1898
- *                     PyErr_SetString(ValueError,
- *                                     'Invalid clipping in CIGAR string')
- *                     return -1             # <<<<<<<<<<<<<<
- *             elif op == BAM_CSOFT_CLIP:
- *                 end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-          __pyx_r = -1;
-          goto __pyx_L0;
-        }
-        goto __pyx_L6;
-      }
-
-      /* "pysam/csamfile.pyx":1899
- *                                     'Invalid clipping in CIGAR string')
- *                     return -1
- *             elif op == BAM_CSOFT_CLIP:             # <<<<<<<<<<<<<<
- *                 end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT
- *             else:
- */
-      __pyx_t_4 = ((__pyx_v_op == BAM_CSOFT_CLIP) != 0);
-      if (__pyx_t_4) {
-
-        /* "pysam/csamfile.pyx":1900
- *                     return -1
- *             elif op == BAM_CSOFT_CLIP:
- *                 end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- *             else:
- *                 break
- */
-        __pyx_v_end_offset = (__pyx_v_end_offset - ((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT));
-        goto __pyx_L6;
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":1902
- *                 end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT
- *             else:
- *                 break             # <<<<<<<<<<<<<<
- * 
- *     if end_offset == 0:
- */
-        goto __pyx_L5_break;
-      }
-      __pyx_L6:;
-    }
-    __pyx_L5_break:;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1904
- *                 break
- * 
- *     if end_offset == 0:             # <<<<<<<<<<<<<<
- *         end_offset = src.core.l_qseq
- * 
- */
-  __pyx_t_4 = ((__pyx_v_end_offset == 0) != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":1905
- * 
- *     if end_offset == 0:
- *         end_offset = src.core.l_qseq             # <<<<<<<<<<<<<<
- * 
- *     return end_offset
- */
-    __pyx_t_1 = __pyx_v_src->core.l_qseq;
-    __pyx_v_end_offset = __pyx_t_1;
-    goto __pyx_L8;
-  }
-  __pyx_L8:;
-
-  /* "pysam/csamfile.pyx":1907
- *         end_offset = src.core.l_qseq
- * 
- *     return end_offset             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __pyx_r = __pyx_v_end_offset;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1885
- * ##-------------------------------------------------------------------
- * ##-------------------------------------------------------------------
- * cdef inline int32_t query_end(bam1_t *src) except -1:             # <<<<<<<<<<<<<<
- *     cdef uint32_t * cigar_p
- *     cdef uint32_t k, op
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1910
- * 
- * 
- * cdef inline object get_seq_range(bam1_t *src, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
- *     cdef uint8_t * p
- *     cdef uint32_t k
- */
-
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_seq_range(bam1_t *__pyx_v_src, uint32_t __pyx_v_start, uint32_t __pyx_v_end) {
-  uint8_t *__pyx_v_p;
-  uint32_t __pyx_v_k;
-  char *__pyx_v_s;
-  PyObject *__pyx_v_seq = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  char *__pyx_t_3;
-  uint32_t __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("get_seq_range", 0);
-  __Pyx_TraceCall("get_seq_range", __pyx_f[0], 1910);
-
-  /* "pysam/csamfile.pyx":1915
- *     cdef char * s
- * 
- *     if not src.core.l_qseq:             # <<<<<<<<<<<<<<
- *         return None
- * 
- */
-  __pyx_t_1 = ((!(__pyx_v_src->core.l_qseq != 0)) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1916
- * 
- *     if not src.core.l_qseq:
- *         return None             # <<<<<<<<<<<<<<
- * 
- *     seq = PyBytes_FromStringAndSize(NULL, end - start)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":1918
- *         return None
- * 
- *     seq = PyBytes_FromStringAndSize(NULL, end - start)             # <<<<<<<<<<<<<<
- *     s   = <char*>seq
- *     p   = pysam_bam_get_seq(src)
- */
-  __pyx_t_2 = PyBytes_FromStringAndSize(NULL, (__pyx_v_end - __pyx_v_start)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_v_seq = ((PyObject*)__pyx_t_2);
-  __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":1919
- * 
- *     seq = PyBytes_FromStringAndSize(NULL, end - start)
- *     s   = <char*>seq             # <<<<<<<<<<<<<<
- *     p   = pysam_bam_get_seq(src)
- * 
- */
-  __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_v_seq); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1919; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_s = ((char *)__pyx_t_3);
-
-  /* "pysam/csamfile.pyx":1920
- *     seq = PyBytes_FromStringAndSize(NULL, end - start)
- *     s   = <char*>seq
- *     p   = pysam_bam_get_seq(src)             # <<<<<<<<<<<<<<
- * 
- *     for k from start <= k < end:
- */
-  __pyx_v_p = pysam_bam_get_seq(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":1922
- *     p   = pysam_bam_get_seq(src)
- * 
- *     for k from start <= k < end:             # <<<<<<<<<<<<<<
- *         # equivalent to seq_nt16_str[bam1_seqi(s, i)] (see bam.c)
- *         # note: do not use string literal as it will be a python string
- */
-  __pyx_t_4 = __pyx_v_end;
-  for (__pyx_v_k = __pyx_v_start; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":1925
- *         # equivalent to seq_nt16_str[bam1_seqi(s, i)] (see bam.c)
- *         # note: do not use string literal as it will be a python string
- *         s[k-start] = seq_nt16_str[p[k/2] >> 4 * (1 - k%2) & 0xf]             # <<<<<<<<<<<<<<
- * 
- *     return seq
- */
-    (__pyx_v_s[(__pyx_v_k - __pyx_v_start)]) = (seq_nt16_str[(((__pyx_v_p[__Pyx_div_long(__pyx_v_k, 2)]) >> (4 * (1 - __Pyx_mod_long(__pyx_v_k, 2)))) & 0xf)]);
-  }
-
-  /* "pysam/csamfile.pyx":1927
- *         s[k-start] = seq_nt16_str[p[k/2] >> 4 * (1 - k%2) & 0xf]
- * 
- *     return seq             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_seq);
-  __pyx_r = __pyx_v_seq;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1910
- * 
- * 
- * cdef inline object get_seq_range(bam1_t *src, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
- *     cdef uint8_t * p
- *     cdef uint32_t k
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.get_seq_range", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_seq);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1930
- * 
- * 
- * cdef inline object get_qual_range(bam1_t *src, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
- *     cdef uint8_t * p
- *     cdef uint32_t k
- */
-
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_get_qual_range(bam1_t *__pyx_v_src, uint32_t __pyx_v_start, uint32_t __pyx_v_end) {
-  uint8_t *__pyx_v_p;
-  uint32_t __pyx_v_k;
-  char *__pyx_v_q;
-  PyObject *__pyx_v_qual = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  char *__pyx_t_3;
-  uint32_t __pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("get_qual_range", 0);
-  __Pyx_TraceCall("get_qual_range", __pyx_f[0], 1930);
-
-  /* "pysam/csamfile.pyx":1935
- *     cdef char * q
- * 
- *     p = pysam_bam_get_qual(src)             # <<<<<<<<<<<<<<
- *     if p[0] == 0xff:
- *         return None
- */
-  __pyx_v_p = pysam_bam_get_qual(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":1936
- * 
- *     p = pysam_bam_get_qual(src)
- *     if p[0] == 0xff:             # <<<<<<<<<<<<<<
- *         return None
- * 
- */
-  __pyx_t_1 = (((__pyx_v_p[0]) == 0xff) != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":1937
- *     p = pysam_bam_get_qual(src)
- *     if p[0] == 0xff:
- *         return None             # <<<<<<<<<<<<<<
- * 
- *     qual = PyBytes_FromStringAndSize(NULL, end - start)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":1939
- *         return None
- * 
- *     qual = PyBytes_FromStringAndSize(NULL, end - start)             # <<<<<<<<<<<<<<
- *     q    = <char*>qual
- * 
- */
-  __pyx_t_2 = PyBytes_FromStringAndSize(NULL, (__pyx_v_end - __pyx_v_start)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_v_qual = ((PyObject*)__pyx_t_2);
-  __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":1940
- * 
- *     qual = PyBytes_FromStringAndSize(NULL, end - start)
- *     q    = <char*>qual             # <<<<<<<<<<<<<<
- * 
- *     for k from start <= k < end:
- */
-  __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_v_qual); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_q = ((char *)__pyx_t_3);
-
-  /* "pysam/csamfile.pyx":1942
- *     q    = <char*>qual
- * 
- *     for k from start <= k < end:             # <<<<<<<<<<<<<<
- *         ## equivalent to t[i] + 33 (see bam.c)
- *         q[k-start] = p[k] + 33
- */
-  __pyx_t_4 = __pyx_v_end;
-  for (__pyx_v_k = __pyx_v_start; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":1944
- *     for k from start <= k < end:
- *         ## equivalent to t[i] + 33 (see bam.c)
- *         q[k-start] = p[k] + 33             # <<<<<<<<<<<<<<
- * 
- *     return qual
- */
-    (__pyx_v_q[(__pyx_v_k - __pyx_v_start)]) = ((__pyx_v_p[__pyx_v_k]) + 33);
-  }
-
-  /* "pysam/csamfile.pyx":1946
- *         q[k-start] = p[k] + 33
- * 
- *     return qual             # <<<<<<<<<<<<<<
- * 
- * cdef inline uint8_t get_type_code(value, value_type = None):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_qual);
-  __pyx_r = __pyx_v_qual;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1930
- * 
- * 
- * cdef inline object get_qual_range(bam1_t *src, uint32_t start, uint32_t end):             # <<<<<<<<<<<<<<
- *     cdef uint8_t * p
- *     cdef uint32_t k
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.get_qual_range", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_qual);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1948
- *     return qual
- * 
- * cdef inline uint8_t get_type_code(value, value_type = None):             # <<<<<<<<<<<<<<
- *     '''guess type code for a *value*. If *value_type* is None,
- *     the type code will be inferred based on the Python type of
- */
-
-static CYTHON_INLINE uint8_t __pyx_f_5pysam_8csamfile_get_type_code(PyObject *__pyx_v_value, struct __pyx_opt_args_5pysam_8csamfile_get_type_code *__pyx_optional_args) {
-  PyObject *__pyx_v_value_type = ((PyObject *)Py_None);
-  uint8_t __pyx_v_type_code;
-  char *__pyx_v__char_type;
-  uint8_t __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  char *__pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("get_type_code", 0);
-  __Pyx_TraceCall("get_type_code", __pyx_f[0], 1948);
-  if (__pyx_optional_args) {
-    if (__pyx_optional_args->__pyx_n > 0) {
-      __pyx_v_value_type = __pyx_optional_args->value_type;
-    }
-  }
-  __Pyx_INCREF(__pyx_v_value_type);
-
-  /* "pysam/csamfile.pyx":1955
- *     cdef char * _char_type
- * 
- *     if value_type is None:             # <<<<<<<<<<<<<<
- *         if isinstance(value, int):
- *             type_code = 'i'
- */
-  __pyx_t_1 = (__pyx_v_value_type == Py_None);
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":1956
- * 
- *     if value_type is None:
- *         if isinstance(value, int):             # <<<<<<<<<<<<<<
- *             type_code = 'i'
- *         elif isinstance(value, float):
- */
-    __pyx_t_2 = PyInt_Check(__pyx_v_value); 
-    __pyx_t_1 = (__pyx_t_2 != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1957
- *     if value_type is None:
- *         if isinstance(value, int):
- *             type_code = 'i'             # <<<<<<<<<<<<<<
- *         elif isinstance(value, float):
- *             type_code = 'd'
- */
-      __pyx_v_type_code = 'i';
-      goto __pyx_L4;
-    }
-
-    /* "pysam/csamfile.pyx":1958
- *         if isinstance(value, int):
- *             type_code = 'i'
- *         elif isinstance(value, float):             # <<<<<<<<<<<<<<
- *             type_code = 'd'
- *         elif isinstance(value, str):
- */
-    __pyx_t_1 = PyFloat_Check(__pyx_v_value); 
-    __pyx_t_2 = (__pyx_t_1 != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":1959
- *             type_code = 'i'
- *         elif isinstance(value, float):
- *             type_code = 'd'             # <<<<<<<<<<<<<<
- *         elif isinstance(value, str):
- *             type_code = 'Z'
- */
-      __pyx_v_type_code = 'd';
-      goto __pyx_L4;
-    }
-
-    /* "pysam/csamfile.pyx":1960
- *         elif isinstance(value, float):
- *             type_code = 'd'
- *         elif isinstance(value, str):             # <<<<<<<<<<<<<<
- *             type_code = 'Z'
- *         elif isinstance(value, bytes):
- */
-    __pyx_t_2 = PyString_Check(__pyx_v_value); 
-    __pyx_t_1 = (__pyx_t_2 != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1961
- *             type_code = 'd'
- *         elif isinstance(value, str):
- *             type_code = 'Z'             # <<<<<<<<<<<<<<
- *         elif isinstance(value, bytes):
- *             type_code = 'Z'
- */
-      __pyx_v_type_code = 'Z';
-      goto __pyx_L4;
-    }
-
-    /* "pysam/csamfile.pyx":1962
- *         elif isinstance(value, str):
- *             type_code = 'Z'
- *         elif isinstance(value, bytes):             # <<<<<<<<<<<<<<
- *             type_code = 'Z'
- *         else:
- */
-    __pyx_t_1 = PyBytes_Check(__pyx_v_value); 
-    __pyx_t_2 = (__pyx_t_1 != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":1963
- *             type_code = 'Z'
- *         elif isinstance(value, bytes):
- *             type_code = 'Z'             # <<<<<<<<<<<<<<
- *         else:
- *             return 0
- */
-      __pyx_v_type_code = 'Z';
-      goto __pyx_L4;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":1965
- *             type_code = 'Z'
- *         else:
- *             return 0             # <<<<<<<<<<<<<<
- *     else:
- *         if value_type not in 'Zidf':
- */
-      __pyx_r = 0;
-      goto __pyx_L0;
-    }
-    __pyx_L4:;
-    goto __pyx_L3;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":1967
- *             return 0
- *     else:
- *         if value_type not in 'Zidf':             # <<<<<<<<<<<<<<
- *             return 0
- *         value_type = _forceBytes( value_type )
- */
-    __pyx_t_2 = (__Pyx_PySequence_Contains(__pyx_v_value_type, __pyx_n_s_Zidf, Py_NE)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_1 = (__pyx_t_2 != 0);
-    if (__pyx_t_1) {
-
-      /* "pysam/csamfile.pyx":1968
- *     else:
- *         if value_type not in 'Zidf':
- *             return 0             # <<<<<<<<<<<<<<
- *         value_type = _forceBytes( value_type )
- *         _char_type = value_type
- */
-      __pyx_r = 0;
-      goto __pyx_L0;
-    }
-
-    /* "pysam/csamfile.pyx":1969
- *         if value_type not in 'Zidf':
- *             return 0
- *         value_type = _forceBytes( value_type )             # <<<<<<<<<<<<<<
- *         _char_type = value_type
- *         type_code = (<uint8_t*>_char_type)[0]
- */
-    __pyx_t_3 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_value_type); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1969; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF_SET(__pyx_v_value_type, __pyx_t_3);
-    __pyx_t_3 = 0;
-
-    /* "pysam/csamfile.pyx":1970
- *             return 0
- *         value_type = _forceBytes( value_type )
- *         _char_type = value_type             # <<<<<<<<<<<<<<
- *         type_code = (<uint8_t*>_char_type)[0]
- * 
- */
-    __pyx_t_4 = __Pyx_PyObject_AsString(__pyx_v_value_type); if (unlikely((!__pyx_t_4) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1970; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v__char_type = __pyx_t_4;
-
-    /* "pysam/csamfile.pyx":1971
- *         value_type = _forceBytes( value_type )
- *         _char_type = value_type
- *         type_code = (<uint8_t*>_char_type)[0]             # <<<<<<<<<<<<<<
- * 
- *     return type_code
- */
-    __pyx_v_type_code = (((uint8_t *)__pyx_v__char_type)[0]);
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1973
- *         type_code = (<uint8_t*>_char_type)[0]
- * 
- *     return type_code             # <<<<<<<<<<<<<<
- * 
- * cdef inline convert_python_tag(pytag, value, fmts, args):
- */
-  __pyx_r = __pyx_v_type_code;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":1948
- *     return qual
- * 
- * cdef inline uint8_t get_type_code(value, value_type = None):             # <<<<<<<<<<<<<<
- *     '''guess type code for a *value*. If *value_type* is None,
- *     the type code will be inferred based on the Python type of
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_WriteUnraisable("pysam.csamfile.get_type_code", __pyx_clineno, __pyx_lineno, __pyx_filename, 0);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_value_type);
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":1975
- *     return type_code
- * 
- * cdef inline convert_python_tag(pytag, value, fmts, args):             # <<<<<<<<<<<<<<
- * 
- *     if not type(pytag) is bytes:
- */
-
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_8csamfile_convert_python_tag(PyObject *__pyx_v_pytag, PyObject *__pyx_v_value, PyObject *__pyx_v_fmts, PyObject *__pyx_v_args) {
-  PyObject *__pyx_v_t = NULL;
-  PyObject *__pyx_v_pytype = NULL;
-  PyObject *__pyx_v_datafmt = NULL;
-  PyObject *__pyx_v_datatype = NULL;
-  PyObject *__pyx_v_mi = NULL;
-  PyObject *__pyx_v_ma = NULL;
-  PyObject *__pyx_v_absmax = NULL;
-  PyObject *__pyx_v_fmt = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  Py_ssize_t __pyx_t_8;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  int __pyx_t_11;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("convert_python_tag", 0);
-  __Pyx_TraceCall("convert_python_tag", __pyx_f[0], 1975);
-  __Pyx_INCREF(__pyx_v_pytag);
-  __Pyx_INCREF(__pyx_v_value);
-
-  /* "pysam/csamfile.pyx":1977
- * cdef inline convert_python_tag(pytag, value, fmts, args):
- * 
- *     if not type(pytag) is bytes:             # <<<<<<<<<<<<<<
- *         pytag = pytag.encode('ascii')
- *     t = type(value)
- */
-  __pyx_t_1 = (((PyObject *)Py_TYPE(__pyx_v_pytag)) != ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":1978
- * 
- *     if not type(pytag) is bytes:
- *         pytag = pytag.encode('ascii')             # <<<<<<<<<<<<<<
- *     t = type(value)
- * 
- */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytag, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1978; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__60, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1978; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_DECREF_SET(__pyx_v_pytag, __pyx_t_4);
-    __pyx_t_4 = 0;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":1979
- *     if not type(pytag) is bytes:
- *         pytag = pytag.encode('ascii')
- *     t = type(value)             # <<<<<<<<<<<<<<
- * 
- *     if t is tuple or t is list:
- */
-  __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_value)));
-  __pyx_v_t = ((PyObject*)((PyObject *)Py_TYPE(__pyx_v_value)));
-
-  /* "pysam/csamfile.pyx":1981
- *     t = type(value)
- * 
- *     if t is tuple or t is list:             # <<<<<<<<<<<<<<
- *         # binary tags - treat separately
- *         pytype = 'B'
- */
-  __pyx_t_2 = (__pyx_v_t == ((PyObject*)(&PyTuple_Type)));
-  if (!(__pyx_t_2 != 0)) {
-    __pyx_t_1 = (__pyx_v_t == ((PyObject*)(&PyList_Type)));
-    __pyx_t_5 = (__pyx_t_1 != 0);
-  } else {
-    __pyx_t_5 = (__pyx_t_2 != 0);
-  }
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":1983
- *     if t is tuple or t is list:
- *         # binary tags - treat separately
- *         pytype = 'B'             # <<<<<<<<<<<<<<
- *         # get data type - first value determines type
- *         if type(value[0]) is float:
- */
-    __Pyx_INCREF(__pyx_n_s_B);
-    __pyx_v_pytype = __pyx_n_s_B;
-
-    /* "pysam/csamfile.pyx":1985
- *         pytype = 'B'
- *         # get data type - first value determines type
- *         if type(value[0]) is float:             # <<<<<<<<<<<<<<
- *             datafmt, datatype = "f", "f"
- *         else:
- */
-    __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_value, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1985; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = (((PyObject *)Py_TYPE(__pyx_t_4)) == ((PyObject *)((PyObject*)(&PyFloat_Type))));
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_2 = (__pyx_t_5 != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":1986
- *         # get data type - first value determines type
- *         if type(value[0]) is float:
- *             datafmt, datatype = "f", "f"             # <<<<<<<<<<<<<<
- *         else:
- *             mi, ma = min(value), max(value)
- */
-      __pyx_t_4 = __pyx_n_s_f;
-      __Pyx_INCREF(__pyx_t_4);
-      __pyx_t_3 = __pyx_n_s_f;
-      __Pyx_INCREF(__pyx_t_3);
-      __pyx_v_datafmt = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_datatype = __pyx_t_3;
-      __pyx_t_3 = 0;
-      goto __pyx_L5;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":1988
- *             datafmt, datatype = "f", "f"
- *         else:
- *             mi, ma = min(value), max(value)             # <<<<<<<<<<<<<<
- *             absmax = max( abs(mi), abs(ma) )
- *             # signed ints
- */
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_INCREF(__pyx_v_value);
-      PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
-      __Pyx_GIVEREF(__pyx_v_value);
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_min, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_INCREF(__pyx_v_value);
-      PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
-      __Pyx_GIVEREF(__pyx_v_value);
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_max, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_mi = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_ma = __pyx_t_6;
-      __pyx_t_6 = 0;
-
-      /* "pysam/csamfile.pyx":1989
- *         else:
- *             mi, ma = min(value), max(value)
- *             absmax = max( abs(mi), abs(ma) )             # <<<<<<<<<<<<<<
- *             # signed ints
- *             if mi < 0:
- */
-      __pyx_t_6 = PyNumber_Absolute(__pyx_v_ma); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_4 = PyNumber_Absolute(__pyx_v_mi); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_6, __pyx_t_4, Py_GT); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      if (__pyx_t_2) {
-        __Pyx_INCREF(__pyx_t_6);
-        __pyx_t_3 = __pyx_t_6;
-      } else {
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_3 = __pyx_t_4;
-      }
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __pyx_t_3;
-      __Pyx_INCREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_absmax = __pyx_t_6;
-      __pyx_t_6 = 0;
-
-      /* "pysam/csamfile.pyx":1991
- *             absmax = max( abs(mi), abs(ma) )
- *             # signed ints
- *             if mi < 0:             # <<<<<<<<<<<<<<
- *                 if mi >= -127: datafmt, datatype = "b", 'c'
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'
- */
-      __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      if (__pyx_t_2) {
-
-        /* "pysam/csamfile.pyx":1992
- *             # signed ints
- *             if mi < 0:
- *                 if mi >= -127: datafmt, datatype = "b", 'c'             # <<<<<<<<<<<<<<
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'
- *                 elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_127, Py_GE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1992; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1992; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_6 = __pyx_n_s_b;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_c;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_v_datafmt = __pyx_t_6;
-          __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
-          goto __pyx_L7;
-        }
-
-        /* "pysam/csamfile.pyx":1993
- *             if mi < 0:
- *                 if mi >= -127: datafmt, datatype = "b", 'c'
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'             # <<<<<<<<<<<<<<
- *                 elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: datafmt, datatype = "i", 'i'
- */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_32767, Py_GE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __pyx_n_s_h;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_t_6 = __pyx_n_s_s_2;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
-          __pyx_v_datatype = __pyx_t_6;
-          __pyx_t_6 = 0;
-          goto __pyx_L7;
-        }
-
-        /* "pysam/csamfile.pyx":1994
- *                 if mi >= -127: datafmt, datatype = "b", 'c'
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'
- *                 elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                 else: datafmt, datatype = "i", 'i'
- * 
- */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_6);
-          __Pyx_GIVEREF(__pyx_t_6);
-          __pyx_t_6 = 0;
-          __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-          __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        /*else*/ {
-
-          /* "pysam/csamfile.pyx":1995
- *                 elif mi >= -32767: datafmt, datatype = "h", 's'
- *                 elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: datafmt, datatype = "i", 'i'             # <<<<<<<<<<<<<<
- * 
- *             # unsigned ints
- */
-          __pyx_t_6 = __pyx_n_s_i;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_i;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_v_datafmt = __pyx_t_6;
-          __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
-        }
-        __pyx_L7:;
-        goto __pyx_L6;
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":1999
- *             # unsigned ints
- *             else:
- *                 if absmax <= 255: datafmt, datatype = "B", 'C'             # <<<<<<<<<<<<<<
- *                 elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                 elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_255, Py_LE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1999; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1999; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __pyx_n_s_B;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_t_6 = __pyx_n_s_C;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
-          __pyx_v_datatype = __pyx_t_6;
-          __pyx_t_6 = 0;
-          goto __pyx_L8;
-        }
-
-        /* "pysam/csamfile.pyx":2000
- *             else:
- *                 if absmax <= 255: datafmt, datatype = "B", 'C'
- *                 elif absmax <= 65535: datafmt, datatype = "H", 'S'             # <<<<<<<<<<<<<<
- *                 elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: datafmt, datatype = "I", 'I'
- */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_65535, Py_LE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_6 = __pyx_n_s_H;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_S;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_v_datafmt = __pyx_t_6;
-          __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
-          goto __pyx_L8;
-        }
-
-        /* "pysam/csamfile.pyx":2001
- *                 if absmax <= 255: datafmt, datatype = "B", 'C'
- *                 elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                 elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                 else: datafmt, datatype = "I", 'I'
- * 
- */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_4294967295, Py_GT); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_3);
-          __Pyx_GIVEREF(__pyx_t_3);
-          __pyx_t_3 = 0;
-          __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          __Pyx_Raise(__pyx_t_3, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        /*else*/ {
-
-          /* "pysam/csamfile.pyx":2002
- *                 elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                 elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: datafmt, datatype = "I", 'I'             # <<<<<<<<<<<<<<
- * 
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- */
-          __pyx_t_3 = __pyx_n_s_I;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_t_6 = __pyx_n_s_I;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
-          __pyx_v_datatype = __pyx_t_6;
-          __pyx_t_6 = 0;
-        }
-        __pyx_L8:;
-      }
-      __pyx_L6:;
-    }
-    __pyx_L5:;
-
-    /* "pysam/csamfile.pyx":2004
- *                 else: datafmt, datatype = "I", 'I'
- * 
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)             # <<<<<<<<<<<<<<
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),
- */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = PyInt_FromSsize_t(__pyx_t_8); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_6);
-    __Pyx_GIVEREF(__pyx_t_6);
-    __Pyx_INCREF(__pyx_v_datafmt);
-    PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_v_datafmt);
-    __Pyx_GIVEREF(__pyx_v_datafmt);
-    __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_2sccI_i_s, __pyx_t_3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_DECREF_SET(__pyx_v_datafmt, __pyx_t_6);
-    __pyx_t_6 = 0;
-
-    /* "pysam/csamfile.pyx":2005
- * 
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
- */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__61, 0, 1, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-
-    /* "pysam/csamfile.pyx":2006
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )
- */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_tuple__62, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-    /* "pysam/csamfile.pyx":2007
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       len(value)] + list(value) )
- *         fmts.append( datafmt )
- */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_datatype, __pyx_n_s_encode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_tuple__63, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-    /* "pysam/csamfile.pyx":2008
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )             # <<<<<<<<<<<<<<
- *         fmts.append( datafmt )
- *         return
- */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = PyInt_FromSsize_t(__pyx_t_8); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-
-    /* "pysam/csamfile.pyx":2005
- * 
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
- */
-    __pyx_t_10 = PyList_New(4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    PyList_SET_ITEM(__pyx_t_10, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    PyList_SET_ITEM(__pyx_t_10, 1, __pyx_t_7);
-    __Pyx_GIVEREF(__pyx_t_7);
-    PyList_SET_ITEM(__pyx_t_10, 2, __pyx_t_9);
-    __Pyx_GIVEREF(__pyx_t_9);
-    PyList_SET_ITEM(__pyx_t_10, 3, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_3 = 0;
-    __pyx_t_7 = 0;
-    __pyx_t_9 = 0;
-    __pyx_t_4 = 0;
-
-    /* "pysam/csamfile.pyx":2008
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )             # <<<<<<<<<<<<<<
- *         fmts.append( datafmt )
- *         return
- */
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_INCREF(__pyx_v_value);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_value);
-    __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_9 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_4, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = PyNumber_Add(__pyx_t_10, __pyx_t_9); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2008; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-
-    /* "pysam/csamfile.pyx":2005
- * 
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
- */
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-    /* "pysam/csamfile.pyx":2009
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )
- *         fmts.append( datafmt )             # <<<<<<<<<<<<<<
- *         return
- * 
- */
-    __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_datafmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":2010
- *                       len(value)] + list(value) )
- *         fmts.append( datafmt )
- *         return             # <<<<<<<<<<<<<<
- * 
- *     if t is float:
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2012
- *         return
- * 
- *     if t is float:             # <<<<<<<<<<<<<<
- *         fmt, pytype = "2scf", 'f'
- *     elif t is int:
- */
-  __pyx_t_2 = (__pyx_v_t == ((PyObject*)(&PyFloat_Type)));
-  __pyx_t_5 = (__pyx_t_2 != 0);
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":2013
- * 
- *     if t is float:
- *         fmt, pytype = "2scf", 'f'             # <<<<<<<<<<<<<<
- *     elif t is int:
- *         # negative values
- */
-    __pyx_t_4 = __pyx_kp_s_2scf;
-    __Pyx_INCREF(__pyx_t_4);
-    __pyx_t_9 = __pyx_n_s_f;
-    __Pyx_INCREF(__pyx_t_9);
-    __pyx_v_fmt = __pyx_t_4;
-    __pyx_t_4 = 0;
-    __pyx_v_pytype = __pyx_t_9;
-    __pyx_t_9 = 0;
-    goto __pyx_L9;
-  }
-
-  /* "pysam/csamfile.pyx":2014
- *     if t is float:
- *         fmt, pytype = "2scf", 'f'
- *     elif t is int:             # <<<<<<<<<<<<<<
- *         # negative values
- *         if value < 0:
- */
-  __pyx_t_5 = (__pyx_v_t == ((PyObject*)(&PyInt_Type)));
-  __pyx_t_2 = (__pyx_t_5 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2016
- *     elif t is int:
- *         # negative values
- *         if value < 0:             # <<<<<<<<<<<<<<
- *             if value >= -127: fmt, pytype = "2scb", 'c'
- *             elif value >= -32767: fmt, pytype = "2sch", 's'
- */
-    __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2016; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2016; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":2017
- *         # negative values
- *         if value < 0:
- *             if value >= -127: fmt, pytype = "2scb", 'c'             # <<<<<<<<<<<<<<
- *             elif value >= -32767: fmt, pytype = "2sch", 's'
- *             elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_127, Py_GE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __pyx_kp_s_2scb;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_c;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_v_fmt = __pyx_t_9;
-        __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
-        goto __pyx_L11;
-      }
-
-      /* "pysam/csamfile.pyx":2018
- *         if value < 0:
- *             if value >= -127: fmt, pytype = "2scb", 'c'
- *             elif value >= -32767: fmt, pytype = "2sch", 's'             # <<<<<<<<<<<<<<
- *             elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *             else: fmt, pytype = "2sci", 'i'
- */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_32767, Py_GE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_4 = __pyx_kp_s_2sch;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_9 = __pyx_n_s_s_2;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
-        __pyx_v_pytype = __pyx_t_9;
-        __pyx_t_9 = 0;
-        goto __pyx_L11;
-      }
-
-      /* "pysam/csamfile.pyx":2019
- *             if value >= -127: fmt, pytype = "2scb", 'c'
- *             elif value >= -32767: fmt, pytype = "2sch", 's'
- *             elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *             else: fmt, pytype = "2sci", 'i'
- *         # positive values
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_9);
-        __Pyx_GIVEREF(__pyx_t_9);
-        __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-        __Pyx_Raise(__pyx_t_9, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":2020
- *             elif value >= -32767: fmt, pytype = "2sch", 's'
- *             elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *             else: fmt, pytype = "2sci", 'i'             # <<<<<<<<<<<<<<
- *         # positive values
- *         else:
- */
-        __pyx_t_9 = __pyx_kp_s_2sci;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_i;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_v_fmt = __pyx_t_9;
-        __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
-      }
-      __pyx_L11:;
-      goto __pyx_L10;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":2023
- *         # positive values
- *         else:
- *             if value <= 255: fmt, pytype = "2scB", 'C'             # <<<<<<<<<<<<<<
- *             elif value <= 65535: fmt, pytype = "2scH", 'S'
- *             elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_255, Py_LE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2023; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2023; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_4 = __pyx_kp_s_2scB;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_9 = __pyx_n_s_C;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
-        __pyx_v_pytype = __pyx_t_9;
-        __pyx_t_9 = 0;
-        goto __pyx_L12;
-      }
-
-      /* "pysam/csamfile.pyx":2024
- *         else:
- *             if value <= 255: fmt, pytype = "2scB", 'C'
- *             elif value <= 65535: fmt, pytype = "2scH", 'S'             # <<<<<<<<<<<<<<
- *             elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *             else: fmt, pytype = "2scI", 'I'
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_65535, Py_LE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2024; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2024; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __pyx_kp_s_2scH;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_S;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_v_fmt = __pyx_t_9;
-        __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
-        goto __pyx_L12;
-      }
-
-      /* "pysam/csamfile.pyx":2025
- *             if value <= 255: fmt, pytype = "2scB", 'C'
- *             elif value <= 65535: fmt, pytype = "2scH", 'S'
- *             elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *             else: fmt, pytype = "2scI", 'I'
- *     else:
- */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_4294967295, Py_GT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_4);
-        __Pyx_GIVEREF(__pyx_t_4);
-        __pyx_t_4 = 0;
-        __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __Pyx_Raise(__pyx_t_4, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":2026
- *             elif value <= 65535: fmt, pytype = "2scH", 'S'
- *             elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *             else: fmt, pytype = "2scI", 'I'             # <<<<<<<<<<<<<<
- *     else:
- *         # Note: hex strings (H) are not supported yet
- */
-        __pyx_t_4 = __pyx_kp_s_2scI;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_9 = __pyx_n_s_I;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
-        __pyx_v_pytype = __pyx_t_9;
-        __pyx_t_9 = 0;
-      }
-      __pyx_L12:;
-    }
-    __pyx_L10:;
-    goto __pyx_L9;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":2029
- *     else:
- *         # Note: hex strings (H) are not supported yet
- *         if t is not bytes:             # <<<<<<<<<<<<<<
- *             value = value.encode('ascii')
- *         if len(value) == 1:
- */
-    __pyx_t_2 = (__pyx_v_t != ((PyObject*)(&PyBytes_Type)));
-    __pyx_t_5 = (__pyx_t_2 != 0);
-    if (__pyx_t_5) {
-
-      /* "pysam/csamfile.pyx":2030
- *         # Note: hex strings (H) are not supported yet
- *         if t is not bytes:
- *             value = value.encode('ascii')             # <<<<<<<<<<<<<<
- *         if len(value) == 1:
- *             fmt, pytype = "2scc", 'A'
- */
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_value, __pyx_n_s_encode); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__64, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_4);
-      __pyx_t_4 = 0;
-      goto __pyx_L13;
-    }
-    __pyx_L13:;
-
-    /* "pysam/csamfile.pyx":2031
- *         if t is not bytes:
- *             value = value.encode('ascii')
- *         if len(value) == 1:             # <<<<<<<<<<<<<<
- *             fmt, pytype = "2scc", 'A'
- *         else:
- */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2031; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = ((__pyx_t_8 == 1) != 0);
-    if (__pyx_t_5) {
-
-      /* "pysam/csamfile.pyx":2032
- *             value = value.encode('ascii')
- *         if len(value) == 1:
- *             fmt, pytype = "2scc", 'A'             # <<<<<<<<<<<<<<
- *         else:
- *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- */
-      __pyx_t_4 = __pyx_kp_s_2scc;
-      __Pyx_INCREF(__pyx_t_4);
-      __pyx_t_9 = __pyx_n_s_A;
-      __Pyx_INCREF(__pyx_t_9);
-      __pyx_v_fmt = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_pytype = __pyx_t_9;
-      __pyx_t_9 = 0;
-      goto __pyx_L14;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":2034
- *             fmt, pytype = "2scc", 'A'
- *         else:
- *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'             # <<<<<<<<<<<<<<
- * 
- *     args.extend( [pytag[:2],
- */
-      __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_9 = PyInt_FromSsize_t((__pyx_t_8 + 1)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_2sc_is, __pyx_t_9); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_9 = __pyx_n_s_Z;
-      __Pyx_INCREF(__pyx_t_9);
-      __pyx_v_fmt = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_pytype = __pyx_t_9;
-      __pyx_t_9 = 0;
-    }
-    __pyx_L14:;
-  }
-  __pyx_L9:;
-
-  /* "pysam/csamfile.pyx":2036
- *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- * 
- *     args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                   pytype.encode('ascii'),
- *                   value ] )
- */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_4 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__65, 0, 1, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-
-  /* "pysam/csamfile.pyx":2037
- * 
- *     args.extend( [pytag[:2],
- *                   pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                   value ] )
- * 
- */
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__66, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_10);
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-  /* "pysam/csamfile.pyx":2036
- *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- * 
- *     args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                   pytype.encode('ascii'),
- *                   value ] )
- */
-  __pyx_t_6 = PyList_New(3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  PyList_SET_ITEM(__pyx_t_6, 0, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  PyList_SET_ITEM(__pyx_t_6, 1, __pyx_t_10);
-  __Pyx_GIVEREF(__pyx_t_10);
-  __Pyx_INCREF(__pyx_v_value);
-  PyList_SET_ITEM(__pyx_t_6, 2, __pyx_v_value);
-  __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_4 = 0;
-  __pyx_t_10 = 0;
-  __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_10);
-  PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_6);
-  __Pyx_GIVEREF(__pyx_t_6);
-  __pyx_t_6 = 0;
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_10, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-  /* "pysam/csamfile.pyx":2040
- *                   value ] )
- * 
- *     fmts.append( fmt )             # <<<<<<<<<<<<<<
- * 
- * ###########################################################
- */
-  __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_fmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2040; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":1975
- *     return type_code
- * 
- * cdef inline convert_python_tag(pytag, value, fmts, args):             # <<<<<<<<<<<<<<
- * 
- *     if not type(pytag) is bytes:
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_AddTraceback("pysam.csamfile.convert_python_tag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_t);
-  __Pyx_XDECREF(__pyx_v_pytype);
-  __Pyx_XDECREF(__pyx_v_datafmt);
-  __Pyx_XDECREF(__pyx_v_datatype);
-  __Pyx_XDECREF(__pyx_v_mi);
-  __Pyx_XDECREF(__pyx_v_ma);
-  __Pyx_XDECREF(__pyx_v_absmax);
-  __Pyx_XDECREF(__pyx_v_fmt);
-  __Pyx_XDECREF(__pyx_v_pytag);
-  __Pyx_XDECREF(__pyx_v_value);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2074
- * 
- *     # Now only called when instances are created from Python
- *     def __init__(self):             # <<<<<<<<<<<<<<
- *         # see bam_init1
- *         self._delegate = <bam1_t*>calloc( 1, sizeof( bam1_t) )
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  if (unlikely(PyTuple_GET_SIZE(__pyx_args) > 0)) {
-    __Pyx_RaiseArgtupleInvalid("__init__", 1, 0, 0, PyTuple_GET_SIZE(__pyx_args)); return -1;}
-  if (unlikely(__pyx_kwds) && unlikely(PyDict_Size(__pyx_kwds) > 0) && unlikely(!__Pyx_CheckKeywordStrings(__pyx_kwds, "__init__", 0))) return -1;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead___init__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 2074);
-
-  /* "pysam/csamfile.pyx":2076
- *     def __init__(self):
- *         # see bam_init1
- *         self._delegate = <bam1_t*>calloc( 1, sizeof( bam1_t) )             # <<<<<<<<<<<<<<
- *         # allocate some memory
- *         # If size is 0, calloc does not return a pointer that can be passed to free()
- */
-  __pyx_v_self->_delegate = ((bam1_t *)calloc(1, (sizeof(bam1_t))));
-
-  /* "pysam/csamfile.pyx":2080
- *         # If size is 0, calloc does not return a pointer that can be passed to free()
- *         # so allocate 40 bytes for a new read
- *         self._delegate.m_data = 40             # <<<<<<<<<<<<<<
- *         self._delegate.data = <uint8_t *>calloc(
- *             self._delegate.m_data, 1)
- */
-  __pyx_v_self->_delegate->m_data = 40;
-
-  /* "pysam/csamfile.pyx":2081
- *         # so allocate 40 bytes for a new read
- *         self._delegate.m_data = 40
- *         self._delegate.data = <uint8_t *>calloc(             # <<<<<<<<<<<<<<
- *             self._delegate.m_data, 1)
- *         self._delegate.l_data = 0
- */
-  __pyx_v_self->_delegate->data = ((uint8_t *)calloc(__pyx_v_self->_delegate->m_data, 1));
-
-  /* "pysam/csamfile.pyx":2083
- *         self._delegate.data = <uint8_t *>calloc(
- *             self._delegate.m_data, 1)
- *         self._delegate.l_data = 0             # <<<<<<<<<<<<<<
- * 
- *     def __dealloc__(self):
- */
-  __pyx_v_self->_delegate->l_data = 0;
-
-  /* "pysam/csamfile.pyx":2074
- * 
- *     # Now only called when instances are created from Python
- *     def __init__(self):             # <<<<<<<<<<<<<<
- *         # see bam_init1
- *         self._delegate = <bam1_t*>calloc( 1, sizeof( bam1_t) )
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2085
- *         self._delegate.l_data = 0
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         bam_destroy1(self._delegate)
- * 
- */
-
-/* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_11AlignedRead_3__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_11AlignedRead_3__dealloc__(PyObject *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_11AlignedRead_2__dealloc__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-}
-
-static void __pyx_pf_5pysam_8csamfile_11AlignedRead_2__dealloc__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 2085);
-
-  /* "pysam/csamfile.pyx":2086
- * 
- *     def __dealloc__(self):
- *         bam_destroy1(self._delegate)             # <<<<<<<<<<<<<<
- * 
- *     def __str__(self):
- */
-  bam_destroy1(__pyx_v_self->_delegate);
-
-  /* "pysam/csamfile.pyx":2085
- *         self._delegate.l_data = 0
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         bam_destroy1(self._delegate)
- * 
- */
-
-  /* function exit code */
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-}
-
-/* "pysam/csamfile.pyx":2088
- *         bam_destroy1(self._delegate)
- * 
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         """return string representation of alignment.
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5__str__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_4__str__[] = "return string representation of alignment.\n\n        The representation is an approximate :term:`sam` format.\n\n        An aligned read might not be associated with a :term:`Samfile`.\n        As a result :term:`tid` is shown instead of the reference name.\n\n        Similarly, the tags field is returned in its parsed state.\n        ";
-#if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_8csamfile_11AlignedRead_4__str__;
-#endif
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5__str__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4__str__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_v_seq = NULL;
-  PyObject *__pyx_v_qual = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  PyObject *__pyx_t_11 = NULL;
-  PyObject *__pyx_t_12 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 2088);
-
-  /* "pysam/csamfile.pyx":2100
- *         # sam-parsing is done in sam.c/bam_format1_core which
- *         # requires a valid header.
- *         if sys.version_info[0] < 3:             # <<<<<<<<<<<<<<
- *             seq = self.seq
- *             qual = self.qual
- */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_version_info); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_GetItemInt(__pyx_t_2, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, __pyx_int_3, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2100; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":2101
- *         # requires a valid header.
- *         if sys.version_info[0] < 3:
- *             seq = self.seq             # <<<<<<<<<<<<<<
- *             qual = self.qual
- *         else:
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_seq); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2101; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_v_seq = __pyx_t_2;
-    __pyx_t_2 = 0;
-
-    /* "pysam/csamfile.pyx":2102
- *         if sys.version_info[0] < 3:
- *             seq = self.seq
- *             qual = self.qual             # <<<<<<<<<<<<<<
- *         else:
- *             seq = self.seq.decode('ascii')
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_qual); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2102; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_v_qual = __pyx_t_2;
-    __pyx_t_2 = 0;
-    goto __pyx_L3;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":2104
- *             qual = self.qual
- *         else:
- *             seq = self.seq.decode('ascii')             # <<<<<<<<<<<<<<
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_seq); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_decode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__67, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_v_seq = __pyx_t_2;
-    __pyx_t_2 = 0;
-
-    /* "pysam/csamfile.pyx":2105
- *         else:
- *             seq = self.seq.decode('ascii')
- *             qual = self.qual.decode('ascii')             # <<<<<<<<<<<<<<
- *         return "\t".join(map(str, (self.qname,
- *                                    self.flag,
- */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_qual); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_decode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__68, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_v_qual = __pyx_t_2;
-    __pyx_t_2 = 0;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":2106
- *             seq = self.seq.decode('ascii')
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,             # <<<<<<<<<<<<<<
- *                                    self.flag,
- *                                    self.rname,
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_qname); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-
-  /* "pysam/csamfile.pyx":2107
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,
- *                                    self.flag,             # <<<<<<<<<<<<<<
- *                                    self.rname,
- *                                    self.pos,
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-
-  /* "pysam/csamfile.pyx":2108
- *         return "\t".join(map(str, (self.qname,
- *                                    self.flag,
- *                                    self.rname,             # <<<<<<<<<<<<<<
- *                                    self.pos,
- *                                    self.mapq,
- */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_rname); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-
-  /* "pysam/csamfile.pyx":2109
- *                                    self.flag,
- *                                    self.rname,
- *                                    self.pos,             # <<<<<<<<<<<<<<
- *                                    self.mapq,
- *                                    self.cigar,
- */
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_pos); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2109; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-
-  /* "pysam/csamfile.pyx":2110
- *                                    self.rname,
- *                                    self.pos,
- *                                    self.mapq,             # <<<<<<<<<<<<<<
- *                                    self.cigar,
- *                                    self.mrnm,
- */
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mapq); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-
-  /* "pysam/csamfile.pyx":2111
- *                                    self.pos,
- *                                    self.mapq,
- *                                    self.cigar,             # <<<<<<<<<<<<<<
- *                                    self.mrnm,
- *                                    self.mpos,
- */
-  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigar); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2111; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-
-  /* "pysam/csamfile.pyx":2112
- *                                    self.mapq,
- *                                    self.cigar,
- *                                    self.mrnm,             # <<<<<<<<<<<<<<
- *                                    self.mpos,
- *                                    self.rlen,
- */
-  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mrnm); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2112; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_8);
-
-  /* "pysam/csamfile.pyx":2113
- *                                    self.cigar,
- *                                    self.mrnm,
- *                                    self.mpos,             # <<<<<<<<<<<<<<
- *                                    self.rlen,
- *                                    seq,
- */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mpos); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-
-  /* "pysam/csamfile.pyx":2114
- *                                    self.mrnm,
- *                                    self.mpos,
- *                                    self.rlen,             # <<<<<<<<<<<<<<
- *                                    seq,
- *                                    qual,
- */
-  __pyx_t_10 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_rlen); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2114; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_10);
-
-  /* "pysam/csamfile.pyx":2117
- *                                    seq,
- *                                    qual,
- *                                    self.tags )))             # <<<<<<<<<<<<<<
- * 
- *     def compare(self, AlignedRead other):
- */
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_tags); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2117; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_11);
-
-  /* "pysam/csamfile.pyx":2106
- *             seq = self.seq.decode('ascii')
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,             # <<<<<<<<<<<<<<
- *                                    self.flag,
- *                                    self.rname,
- */
-  __pyx_t_12 = PyTuple_New(12); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_12);
-  PyTuple_SET_ITEM(__pyx_t_12, 0, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_12, 1, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_12, 2, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_12, 3, __pyx_t_5);
-  __Pyx_GIVEREF(__pyx_t_5);
-  PyTuple_SET_ITEM(__pyx_t_12, 4, __pyx_t_6);
-  __Pyx_GIVEREF(__pyx_t_6);
-  PyTuple_SET_ITEM(__pyx_t_12, 5, __pyx_t_7);
-  __Pyx_GIVEREF(__pyx_t_7);
-  PyTuple_SET_ITEM(__pyx_t_12, 6, __pyx_t_8);
-  __Pyx_GIVEREF(__pyx_t_8);
-  PyTuple_SET_ITEM(__pyx_t_12, 7, __pyx_t_9);
-  __Pyx_GIVEREF(__pyx_t_9);
-  PyTuple_SET_ITEM(__pyx_t_12, 8, __pyx_t_10);
-  __Pyx_GIVEREF(__pyx_t_10);
-  __Pyx_INCREF(__pyx_v_seq);
-  PyTuple_SET_ITEM(__pyx_t_12, 9, __pyx_v_seq);
-  __Pyx_GIVEREF(__pyx_v_seq);
-  __Pyx_INCREF(__pyx_v_qual);
-  PyTuple_SET_ITEM(__pyx_t_12, 10, __pyx_v_qual);
-  __Pyx_GIVEREF(__pyx_v_qual);
-  PyTuple_SET_ITEM(__pyx_t_12, 11, __pyx_t_11);
-  __Pyx_GIVEREF(__pyx_t_11);
-  __pyx_t_2 = 0;
-  __pyx_t_1 = 0;
-  __pyx_t_4 = 0;
-  __pyx_t_5 = 0;
-  __pyx_t_6 = 0;
-  __pyx_t_7 = 0;
-  __pyx_t_8 = 0;
-  __pyx_t_9 = 0;
-  __pyx_t_10 = 0;
-  __pyx_t_11 = 0;
-  __pyx_t_11 = PyTuple_New(2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_11);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_11, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_11, 1, __pyx_t_12);
-  __Pyx_GIVEREF(__pyx_t_12);
-  __pyx_t_12 = 0;
-  __pyx_t_12 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_11, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_12);
-  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-  __pyx_t_11 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_12); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2106; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_11);
-  __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-  __pyx_r = __pyx_t_11;
-  __pyx_t_11 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2088
- *         bam_destroy1(self._delegate)
- * 
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         """return string representation of alignment.
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_XDECREF(__pyx_t_11);
-  __Pyx_XDECREF(__pyx_t_12);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_seq);
-  __Pyx_XDECREF(__pyx_v_qual);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2119
- *                                    self.tags )))
- * 
- *     def compare(self, AlignedRead other):             # <<<<<<<<<<<<<<
- *         '''return -1,0,1, if contents in this are binary <,=,> to *other*'''
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_7compare(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_6compare[] = "AlignedRead.compare(self, AlignedRead other)\nreturn -1,0,1, if contents in this are binary <,=,> to *other*";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_7compare(PyObject *__pyx_v_self, PyObject *__pyx_v_other) {
-  CYTHON_UNUSED int __pyx_lineno = 0;
-  CYTHON_UNUSED const char *__pyx_filename = NULL;
-  CYTHON_UNUSED int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("compare (wrapper)", 0);
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_5pysam_8csamfile_AlignedRead, 1, "other", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2119; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_other));
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6compare(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_other) {
-  int __pyx_v_retval;
-  bam1_t *__pyx_v_t;
-  bam1_t *__pyx_v_o;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("compare", 0);
-  __Pyx_TraceCall("compare", __pyx_f[0], 2119);
-
-  /* "pysam/csamfile.pyx":2126
- *         cdef bam1_t *o
- * 
- *         t = self._delegate             # <<<<<<<<<<<<<<
- *         o = other._delegate
- * 
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_t = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2127
- * 
- *         t = self._delegate
- *         o = other._delegate             # <<<<<<<<<<<<<<
- * 
- *         # uncomment for debugging purposes
- */
-  __pyx_t_1 = __pyx_v_other->_delegate;
-  __pyx_v_o = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2139
- * 
- *         # Fast-path test for object identity
- *         if t == o:             # <<<<<<<<<<<<<<
- *             return 0
- * 
- */
-  __pyx_t_2 = ((__pyx_v_t == __pyx_v_o) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2140
- *         # Fast-path test for object identity
- *         if t == o:
- *             return 0             # <<<<<<<<<<<<<<
- * 
- *         retval = memcmp(&t.core, &o.core, sizeof(bam1_core_t))
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_int_0);
-    __pyx_r = __pyx_int_0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2142
- *             return 0
- * 
- *         retval = memcmp(&t.core, &o.core, sizeof(bam1_core_t))             # <<<<<<<<<<<<<<
- * 
- *         if retval: return retval
- */
-  __pyx_v_retval = memcmp((&__pyx_v_t->core), (&__pyx_v_o->core), (sizeof(bam1_core_t)));
-
-  /* "pysam/csamfile.pyx":2144
- *         retval = memcmp(&t.core, &o.core, sizeof(bam1_core_t))
- * 
- *         if retval: return retval             # <<<<<<<<<<<<<<
- *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)
- *         if retval: return retval
- */
-  __pyx_t_2 = (__pyx_v_retval != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_retval); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2145
- * 
- *         if retval: return retval
- *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)             # <<<<<<<<<<<<<<
- *         if retval: return retval
- *         return memcmp(t.data, o.data, t.l_data)
- */
-  __pyx_v_retval = ((__pyx_v_t->l_data > __pyx_v_o->l_data) - (__pyx_v_t->l_data < __pyx_v_o->l_data));
-
-  /* "pysam/csamfile.pyx":2146
- *         if retval: return retval
- *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)
- *         if retval: return retval             # <<<<<<<<<<<<<<
- *         return memcmp(t.data, o.data, t.l_data)
- * 
- */
-  __pyx_t_2 = (__pyx_v_retval != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_retval); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2146; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2147
- *         retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)
- *         if retval: return retval
- *         return memcmp(t.data, o.data, t.l_data)             # <<<<<<<<<<<<<<
- * 
- *     # Disabled so long as __cmp__ is a special method
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __Pyx_PyInt_From_int(memcmp(__pyx_v_t->data, __pyx_v_o->data, __pyx_v_t->l_data)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2147; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2119
- *                                    self.tags )))
- * 
- *     def compare(self, AlignedRead other):             # <<<<<<<<<<<<<<
- *         '''return -1,0,1, if contents in this are binary <,=,> to *other*'''
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.compare", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2150
- * 
- *     # Disabled so long as __cmp__ is a special method
- *     def __hash__(self):             # <<<<<<<<<<<<<<
- *         return _Py_HashPointer(<void *>self)
- * 
- */
-
-/* Python wrapper */
-static Py_hash_t __pyx_pw_5pysam_8csamfile_11AlignedRead_9__hash__(PyObject *__pyx_v_self); /*proto*/
-static Py_hash_t __pyx_pw_5pysam_8csamfile_11AlignedRead_9__hash__(PyObject *__pyx_v_self) {
-  Py_hash_t __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__hash__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8__hash__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static Py_hash_t __pyx_pf_5pysam_8csamfile_11AlignedRead_8__hash__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  Py_hash_t __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__hash__", 0);
-  __Pyx_TraceCall("__hash__", __pyx_f[0], 2150);
-
-  /* "pysam/csamfile.pyx":2151
- *     # Disabled so long as __cmp__ is a special method
- *     def __hash__(self):
- *         return _Py_HashPointer(<void *>self)             # <<<<<<<<<<<<<<
- * 
- *     def _convert_python_tag(self, pytag, value, fmts, args):
- */
-  __pyx_r = _Py_HashPointer(((void *)__pyx_v_self));
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2150
- * 
- *     # Disabled so long as __cmp__ is a special method
- *     def __hash__(self):             # <<<<<<<<<<<<<<
- *         return _Py_HashPointer(<void *>self)
- * 
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  if (unlikely(__pyx_r == -1) && !PyErr_Occurred()) __pyx_r = -2;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2153
- *         return _Py_HashPointer(<void *>self)
- * 
- *     def _convert_python_tag(self, pytag, value, fmts, args):             # <<<<<<<<<<<<<<
- * 
- *         if not type(pytag) is bytes:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11_convert_python_tag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_10_convert_python_tag[] = "AlignedRead._convert_python_tag(self, pytag, value, fmts, args)";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11_convert_python_tag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_pytag = 0;
-  PyObject *__pyx_v_value = 0;
-  PyObject *__pyx_v_fmts = 0;
-  PyObject *__pyx_v_args = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("_convert_python_tag (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_pytag,&__pyx_n_s_value,&__pyx_n_s_fmts,&__pyx_n_s_args,0};
-    PyObject* values[4] = {0,0,0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_pytag)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("_convert_python_tag", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  2:
-        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_fmts)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("_convert_python_tag", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  3:
-        if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_args)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("_convert_python_tag", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_convert_python_tag") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
-      goto __pyx_L5_argtuple_error;
-    } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-      values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-    }
-    __pyx_v_pytag = values[0];
-    __pyx_v_value = values[1];
-    __pyx_v_fmts = values[2];
-    __pyx_v_args = values[3];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_convert_python_tag", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2153; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead._convert_python_tag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_10_convert_python_tag(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), __pyx_v_pytag, __pyx_v_value, __pyx_v_fmts, __pyx_v_args);
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_10_convert_python_tag(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_pytag, PyObject *__pyx_v_value, PyObject *__pyx_v_fmts, PyObject *__pyx_v_args) {
-  PyObject *__pyx_v_t = NULL;
-  PyObject *__pyx_v_pytype = NULL;
-  PyObject *__pyx_v_datafmt = NULL;
-  PyObject *__pyx_v_datatype = NULL;
-  PyObject *__pyx_v_mi = NULL;
-  PyObject *__pyx_v_ma = NULL;
-  PyObject *__pyx_v_absmax = NULL;
-  PyObject *__pyx_v_fmt = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  Py_ssize_t __pyx_t_8;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  int __pyx_t_11;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("_convert_python_tag", 0);
-  __Pyx_TraceCall("_convert_python_tag", __pyx_f[0], 2153);
-  __Pyx_INCREF(__pyx_v_pytag);
-  __Pyx_INCREF(__pyx_v_value);
-
-  /* "pysam/csamfile.pyx":2155
- *     def _convert_python_tag(self, pytag, value, fmts, args):
- * 
- *         if not type(pytag) is bytes:             # <<<<<<<<<<<<<<
- *             pytag = pytag.encode('ascii')
- *         t = type(value)
- */
-  __pyx_t_1 = (((PyObject *)Py_TYPE(__pyx_v_pytag)) != ((PyObject *)((PyObject*)(&PyBytes_Type))));
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2156
- * 
- *         if not type(pytag) is bytes:
- *             pytag = pytag.encode('ascii')             # <<<<<<<<<<<<<<
- *         t = type(value)
- * 
- */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytag, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__69, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_DECREF_SET(__pyx_v_pytag, __pyx_t_4);
-    __pyx_t_4 = 0;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":2157
- *         if not type(pytag) is bytes:
- *             pytag = pytag.encode('ascii')
- *         t = type(value)             # <<<<<<<<<<<<<<
- * 
- *         if t is tuple or t is list:
- */
-  __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_value)));
-  __pyx_v_t = ((PyObject*)((PyObject *)Py_TYPE(__pyx_v_value)));
-
-  /* "pysam/csamfile.pyx":2159
- *         t = type(value)
- * 
- *         if t is tuple or t is list:             # <<<<<<<<<<<<<<
- *             # binary tags - treat separately
- *             pytype = 'B'
- */
-  __pyx_t_2 = (__pyx_v_t == ((PyObject*)(&PyTuple_Type)));
-  if (!(__pyx_t_2 != 0)) {
-    __pyx_t_1 = (__pyx_v_t == ((PyObject*)(&PyList_Type)));
-    __pyx_t_5 = (__pyx_t_1 != 0);
-  } else {
-    __pyx_t_5 = (__pyx_t_2 != 0);
-  }
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":2161
- *         if t is tuple or t is list:
- *             # binary tags - treat separately
- *             pytype = 'B'             # <<<<<<<<<<<<<<
- *             # get data type - first value determines type
- *             if type(value[0]) is float:
- */
-    __Pyx_INCREF(__pyx_n_s_B);
-    __pyx_v_pytype = __pyx_n_s_B;
-
-    /* "pysam/csamfile.pyx":2163
- *             pytype = 'B'
- *             # get data type - first value determines type
- *             if type(value[0]) is float:             # <<<<<<<<<<<<<<
- *                 datafmt, datatype = "f", "f"
- *             else:
- */
-    __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_value, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2163; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = (((PyObject *)Py_TYPE(__pyx_t_4)) == ((PyObject *)((PyObject*)(&PyFloat_Type))));
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_2 = (__pyx_t_5 != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":2164
- *             # get data type - first value determines type
- *             if type(value[0]) is float:
- *                 datafmt, datatype = "f", "f"             # <<<<<<<<<<<<<<
- *             else:
- *                 mi, ma = min(value), max(value)
- */
-      __pyx_t_4 = __pyx_n_s_f;
-      __Pyx_INCREF(__pyx_t_4);
-      __pyx_t_3 = __pyx_n_s_f;
-      __Pyx_INCREF(__pyx_t_3);
-      __pyx_v_datafmt = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_datatype = __pyx_t_3;
-      __pyx_t_3 = 0;
-      goto __pyx_L5;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":2166
- *                 datafmt, datatype = "f", "f"
- *             else:
- *                 mi, ma = min(value), max(value)             # <<<<<<<<<<<<<<
- *                 absmax = max( abs(mi), abs(ma) )
- *                 # signed ints
- */
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_INCREF(__pyx_v_value);
-      PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
-      __Pyx_GIVEREF(__pyx_v_value);
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_min, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_INCREF(__pyx_v_value);
-      PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_value);
-      __Pyx_GIVEREF(__pyx_v_value);
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_max, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_mi = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_ma = __pyx_t_6;
-      __pyx_t_6 = 0;
-
-      /* "pysam/csamfile.pyx":2167
- *             else:
- *                 mi, ma = min(value), max(value)
- *                 absmax = max( abs(mi), abs(ma) )             # <<<<<<<<<<<<<<
- *                 # signed ints
- *                 if mi < 0:
- */
-      __pyx_t_6 = PyNumber_Absolute(__pyx_v_ma); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_4 = PyNumber_Absolute(__pyx_v_mi); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_6, __pyx_t_4, Py_GT); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      if (__pyx_t_2) {
-        __Pyx_INCREF(__pyx_t_6);
-        __pyx_t_3 = __pyx_t_6;
-      } else {
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_3 = __pyx_t_4;
-      }
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __pyx_t_3;
-      __Pyx_INCREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_absmax = __pyx_t_6;
-      __pyx_t_6 = 0;
-
-      /* "pysam/csamfile.pyx":2169
- *                 absmax = max( abs(mi), abs(ma) )
- *                 # signed ints
- *                 if mi < 0:             # <<<<<<<<<<<<<<
- *                     if mi >= -127: datafmt, datatype = "b", 'c'
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'
- */
-      __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      if (__pyx_t_2) {
-
-        /* "pysam/csamfile.pyx":2170
- *                 # signed ints
- *                 if mi < 0:
- *                     if mi >= -127: datafmt, datatype = "b", 'c'             # <<<<<<<<<<<<<<
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'
- *                     elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_127, Py_GE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2170; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2170; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_6 = __pyx_n_s_b;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_c;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_v_datafmt = __pyx_t_6;
-          __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
-          goto __pyx_L7;
-        }
-
-        /* "pysam/csamfile.pyx":2171
- *                 if mi < 0:
- *                     if mi >= -127: datafmt, datatype = "b", 'c'
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'             # <<<<<<<<<<<<<<
- *                     elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                     else: datafmt, datatype = "i", 'i'
- */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_mi, __pyx_int_neg_32767, Py_GE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __pyx_n_s_h;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_t_6 = __pyx_n_s_s_2;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
-          __pyx_v_datatype = __pyx_t_6;
-          __pyx_t_6 = 0;
-          goto __pyx_L7;
-        }
-
-        /* "pysam/csamfile.pyx":2172
- *                     if mi >= -127: datafmt, datatype = "b", 'c'
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'
- *                     elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                     else: datafmt, datatype = "i", 'i'
- * 
- */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_6);
-          __Pyx_GIVEREF(__pyx_t_6);
-          __pyx_t_6 = 0;
-          __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-          __Pyx_Raise(__pyx_t_6, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        /*else*/ {
-
-          /* "pysam/csamfile.pyx":2173
- *                     elif mi >= -32767: datafmt, datatype = "h", 's'
- *                     elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                     else: datafmt, datatype = "i", 'i'             # <<<<<<<<<<<<<<
- * 
- *                 # unsigned ints
- */
-          __pyx_t_6 = __pyx_n_s_i;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_i;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_v_datafmt = __pyx_t_6;
-          __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
-        }
-        __pyx_L7:;
-        goto __pyx_L6;
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":2177
- *                 # unsigned ints
- *                 else:
- *                     if absmax <= 255: datafmt, datatype = "B", 'C'             # <<<<<<<<<<<<<<
- *                     elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                     elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_255, Py_LE); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __pyx_n_s_B;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_t_6 = __pyx_n_s_C;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
-          __pyx_v_datatype = __pyx_t_6;
-          __pyx_t_6 = 0;
-          goto __pyx_L8;
-        }
-
-        /* "pysam/csamfile.pyx":2178
- *                 else:
- *                     if absmax <= 255: datafmt, datatype = "B", 'C'
- *                     elif absmax <= 65535: datafmt, datatype = "H", 'S'             # <<<<<<<<<<<<<<
- *                     elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                     else: datafmt, datatype = "I", 'I'
- */
-        __pyx_t_6 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_65535, Py_LE); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_6 = __pyx_n_s_H;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_t_3 = __pyx_n_s_S;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_v_datafmt = __pyx_t_6;
-          __pyx_t_6 = 0;
-          __pyx_v_datatype = __pyx_t_3;
-          __pyx_t_3 = 0;
-          goto __pyx_L8;
-        }
-
-        /* "pysam/csamfile.pyx":2179
- *                     if absmax <= 255: datafmt, datatype = "B", 'C'
- *                     elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                     elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                     else: datafmt, datatype = "I", 'I'
- * 
- */
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_absmax, __pyx_int_4294967295, Py_GT); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        if (__pyx_t_2) {
-          __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_6);
-          PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_3);
-          __Pyx_GIVEREF(__pyx_t_3);
-          __pyx_t_3 = 0;
-          __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __Pyx_GOTREF(__pyx_t_3);
-          __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-          __Pyx_Raise(__pyx_t_3, 0, 0, 0);
-          __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        /*else*/ {
-
-          /* "pysam/csamfile.pyx":2180
- *                     elif absmax <= 65535: datafmt, datatype = "H", 'S'
- *                     elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                     else: datafmt, datatype = "I", 'I'             # <<<<<<<<<<<<<<
- * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- */
-          __pyx_t_3 = __pyx_n_s_I;
-          __Pyx_INCREF(__pyx_t_3);
-          __pyx_t_6 = __pyx_n_s_I;
-          __Pyx_INCREF(__pyx_t_6);
-          __pyx_v_datafmt = __pyx_t_3;
-          __pyx_t_3 = 0;
-          __pyx_v_datatype = __pyx_t_6;
-          __pyx_t_6 = 0;
-        }
-        __pyx_L8:;
-      }
-      __pyx_L6:;
-    }
-    __pyx_L5:;
-
-    /* "pysam/csamfile.pyx":2182
- *                     else: datafmt, datatype = "I", 'I'
- * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)             # <<<<<<<<<<<<<<
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),
- */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = PyInt_FromSsize_t(__pyx_t_8); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_6);
-    __Pyx_GIVEREF(__pyx_t_6);
-    __Pyx_INCREF(__pyx_v_datafmt);
-    PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_v_datafmt);
-    __Pyx_GIVEREF(__pyx_v_datafmt);
-    __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_2sccI_i_s, __pyx_t_3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_DECREF_SET(__pyx_v_datafmt, __pyx_t_6);
-    __pyx_t_6 = 0;
-
-    /* "pysam/csamfile.pyx":2183
- * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
- */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_3 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__70, 0, 1, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-
-    /* "pysam/csamfile.pyx":2184
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )
- */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_tuple__71, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-    /* "pysam/csamfile.pyx":2185
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                           len(value)] + list(value) )
- *             fmts.append( datafmt )
- */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_datatype, __pyx_n_s_encode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_tuple__72, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-    /* "pysam/csamfile.pyx":2186
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )             # <<<<<<<<<<<<<<
- *             fmts.append( datafmt )
- *             return
- */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = PyInt_FromSsize_t(__pyx_t_8); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-
-    /* "pysam/csamfile.pyx":2183
- * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
- */
-    __pyx_t_10 = PyList_New(4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    PyList_SET_ITEM(__pyx_t_10, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    PyList_SET_ITEM(__pyx_t_10, 1, __pyx_t_7);
-    __Pyx_GIVEREF(__pyx_t_7);
-    PyList_SET_ITEM(__pyx_t_10, 2, __pyx_t_9);
-    __Pyx_GIVEREF(__pyx_t_9);
-    PyList_SET_ITEM(__pyx_t_10, 3, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_3 = 0;
-    __pyx_t_7 = 0;
-    __pyx_t_9 = 0;
-    __pyx_t_4 = 0;
-
-    /* "pysam/csamfile.pyx":2186
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )             # <<<<<<<<<<<<<<
- *             fmts.append( datafmt )
- *             return
- */
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_INCREF(__pyx_v_value);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_value);
-    __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_9 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_4, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = PyNumber_Add(__pyx_t_10, __pyx_t_9); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-
-    /* "pysam/csamfile.pyx":2183
- * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
- */
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-    /* "pysam/csamfile.pyx":2187
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )
- *             fmts.append( datafmt )             # <<<<<<<<<<<<<<
- *             return
- * 
- */
-    __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_datafmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":2188
- *                           len(value)] + list(value) )
- *             fmts.append( datafmt )
- *             return             # <<<<<<<<<<<<<<
- * 
- *         if t is float:
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2190
- *             return
- * 
- *         if t is float:             # <<<<<<<<<<<<<<
- *             fmt, pytype = "2scf", 'f'
- *         elif t is int:
- */
-  __pyx_t_2 = (__pyx_v_t == ((PyObject*)(&PyFloat_Type)));
-  __pyx_t_5 = (__pyx_t_2 != 0);
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":2191
- * 
- *         if t is float:
- *             fmt, pytype = "2scf", 'f'             # <<<<<<<<<<<<<<
- *         elif t is int:
- *             # negative values
- */
-    __pyx_t_4 = __pyx_kp_s_2scf;
-    __Pyx_INCREF(__pyx_t_4);
-    __pyx_t_9 = __pyx_n_s_f;
-    __Pyx_INCREF(__pyx_t_9);
-    __pyx_v_fmt = __pyx_t_4;
-    __pyx_t_4 = 0;
-    __pyx_v_pytype = __pyx_t_9;
-    __pyx_t_9 = 0;
-    goto __pyx_L9;
-  }
-
-  /* "pysam/csamfile.pyx":2192
- *         if t is float:
- *             fmt, pytype = "2scf", 'f'
- *         elif t is int:             # <<<<<<<<<<<<<<
- *             # negative values
- *             if value < 0:
- */
-  __pyx_t_5 = (__pyx_v_t == ((PyObject*)(&PyInt_Type)));
-  __pyx_t_2 = (__pyx_t_5 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2194
- *         elif t is int:
- *             # negative values
- *             if value < 0:             # <<<<<<<<<<<<<<
- *                 if value >= -127: fmt, pytype = "2scb", 'c'
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'
- */
-    __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":2195
- *             # negative values
- *             if value < 0:
- *                 if value >= -127: fmt, pytype = "2scb", 'c'             # <<<<<<<<<<<<<<
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'
- *                 elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_127, Py_GE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2195; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2195; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __pyx_kp_s_2scb;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_c;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_v_fmt = __pyx_t_9;
-        __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
-        goto __pyx_L11;
-      }
-
-      /* "pysam/csamfile.pyx":2196
- *             if value < 0:
- *                 if value >= -127: fmt, pytype = "2scb", 'c'
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'             # <<<<<<<<<<<<<<
- *                 elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: fmt, pytype = "2sci", 'i'
- */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_32767, Py_GE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_4 = __pyx_kp_s_2sch;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_9 = __pyx_n_s_s_2;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
-        __pyx_v_pytype = __pyx_t_9;
-        __pyx_t_9 = 0;
-        goto __pyx_L11;
-      }
-
-      /* "pysam/csamfile.pyx":2197
- *                 if value >= -127: fmt, pytype = "2scb", 'c'
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'
- *                 elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                 else: fmt, pytype = "2sci", 'i'
- *             # positive values
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_neg_2147483648, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_9);
-        __Pyx_GIVEREF(__pyx_t_9);
-        __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-        __Pyx_Raise(__pyx_t_9, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":2198
- *                 elif value >= -32767: fmt, pytype = "2sch", 's'
- *                 elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: fmt, pytype = "2sci", 'i'             # <<<<<<<<<<<<<<
- *             # positive values
- *             else:
- */
-        __pyx_t_9 = __pyx_kp_s_2sci;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_i;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_v_fmt = __pyx_t_9;
-        __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
-      }
-      __pyx_L11:;
-      goto __pyx_L10;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":2201
- *             # positive values
- *             else:
- *                 if value <= 255: fmt, pytype = "2scB", 'C'             # <<<<<<<<<<<<<<
- *                 elif value <= 65535: fmt, pytype = "2scH", 'S'
- *                 elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_255, Py_LE); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2201; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_4 = __pyx_kp_s_2scB;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_9 = __pyx_n_s_C;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
-        __pyx_v_pytype = __pyx_t_9;
-        __pyx_t_9 = 0;
-        goto __pyx_L12;
-      }
-
-      /* "pysam/csamfile.pyx":2202
- *             else:
- *                 if value <= 255: fmt, pytype = "2scB", 'C'
- *                 elif value <= 65535: fmt, pytype = "2scH", 'S'             # <<<<<<<<<<<<<<
- *                 elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: fmt, pytype = "2scI", 'I'
- */
-      __pyx_t_9 = PyObject_RichCompare(__pyx_v_value, __pyx_int_65535, Py_LE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __pyx_kp_s_2scH;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_t_4 = __pyx_n_s_S;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_v_fmt = __pyx_t_9;
-        __pyx_t_9 = 0;
-        __pyx_v_pytype = __pyx_t_4;
-        __pyx_t_4 = 0;
-        goto __pyx_L12;
-      }
-
-      /* "pysam/csamfile.pyx":2203
- *                 if value <= 255: fmt, pytype = "2scB", 'C'
- *                 elif value <= 65535: fmt, pytype = "2scH", 'S'
- *                 elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )             # <<<<<<<<<<<<<<
- *                 else: fmt, pytype = "2scI", 'I'
- *         else:
- */
-      __pyx_t_4 = PyObject_RichCompare(__pyx_v_value, __pyx_int_4294967295, Py_GT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_v_value); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_4);
-        __Pyx_GIVEREF(__pyx_t_4);
-        __pyx_t_4 = 0;
-        __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_4);
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __Pyx_Raise(__pyx_t_4, 0, 0, 0);
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      /*else*/ {
-
-        /* "pysam/csamfile.pyx":2204
- *                 elif value <= 65535: fmt, pytype = "2scH", 'S'
- *                 elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
- *                 else: fmt, pytype = "2scI", 'I'             # <<<<<<<<<<<<<<
- *         else:
- *             # Note: hex strings (H) are not supported yet
- */
-        __pyx_t_4 = __pyx_kp_s_2scI;
-        __Pyx_INCREF(__pyx_t_4);
-        __pyx_t_9 = __pyx_n_s_I;
-        __Pyx_INCREF(__pyx_t_9);
-        __pyx_v_fmt = __pyx_t_4;
-        __pyx_t_4 = 0;
-        __pyx_v_pytype = __pyx_t_9;
-        __pyx_t_9 = 0;
-      }
-      __pyx_L12:;
-    }
-    __pyx_L10:;
-    goto __pyx_L9;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":2207
- *         else:
- *             # Note: hex strings (H) are not supported yet
- *             if t is not bytes:             # <<<<<<<<<<<<<<
- *                 value = value.encode('ascii')
- *             if len(value) == 1:
- */
-    __pyx_t_2 = (__pyx_v_t != ((PyObject*)(&PyBytes_Type)));
-    __pyx_t_5 = (__pyx_t_2 != 0);
-    if (__pyx_t_5) {
-
-      /* "pysam/csamfile.pyx":2208
- *             # Note: hex strings (H) are not supported yet
- *             if t is not bytes:
- *                 value = value.encode('ascii')             # <<<<<<<<<<<<<<
- *             if len(value) == 1:
- *                 fmt, pytype = "2scc", 'A'
- */
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_value, __pyx_n_s_encode); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__73, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_4);
-      __pyx_t_4 = 0;
-      goto __pyx_L13;
-    }
-    __pyx_L13:;
-
-    /* "pysam/csamfile.pyx":2209
- *             if t is not bytes:
- *                 value = value.encode('ascii')
- *             if len(value) == 1:             # <<<<<<<<<<<<<<
- *                 fmt, pytype = "2scc", 'A'
- *             else:
- */
-    __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = ((__pyx_t_8 == 1) != 0);
-    if (__pyx_t_5) {
-
-      /* "pysam/csamfile.pyx":2210
- *                 value = value.encode('ascii')
- *             if len(value) == 1:
- *                 fmt, pytype = "2scc", 'A'             # <<<<<<<<<<<<<<
- *             else:
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- */
-      __pyx_t_4 = __pyx_kp_s_2scc;
-      __Pyx_INCREF(__pyx_t_4);
-      __pyx_t_9 = __pyx_n_s_A;
-      __Pyx_INCREF(__pyx_t_9);
-      __pyx_v_fmt = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_pytype = __pyx_t_9;
-      __pyx_t_9 = 0;
-      goto __pyx_L14;
-    }
-    /*else*/ {
-
-      /* "pysam/csamfile.pyx":2212
- *                 fmt, pytype = "2scc", 'A'
- *             else:
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'             # <<<<<<<<<<<<<<
- * 
- *         args.extend( [pytag[:2],
- */
-      __pyx_t_8 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_9 = PyInt_FromSsize_t((__pyx_t_8 + 1)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_2sc_is, __pyx_t_9); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_9 = __pyx_n_s_Z;
-      __Pyx_INCREF(__pyx_t_9);
-      __pyx_v_fmt = __pyx_t_4;
-      __pyx_t_4 = 0;
-      __pyx_v_pytype = __pyx_t_9;
-      __pyx_t_9 = 0;
-    }
-    __pyx_L14:;
-  }
-  __pyx_L9:;
-
-  /* "pysam/csamfile.pyx":2214
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- * 
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       value ] )
- */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_args, __pyx_n_s_extend); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_4 = __Pyx_PyObject_GetSlice(__pyx_v_pytag, 0, 2, NULL, NULL, &__pyx_slice__74, 0, 1, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-
-  /* "pysam/csamfile.pyx":2215
- * 
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       value ] )
- * 
- */
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_pytype, __pyx_n_s_encode); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__75, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_10);
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-  /* "pysam/csamfile.pyx":2214
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- * 
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       value ] )
- */
-  __pyx_t_6 = PyList_New(3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  PyList_SET_ITEM(__pyx_t_6, 0, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  PyList_SET_ITEM(__pyx_t_6, 1, __pyx_t_10);
-  __Pyx_GIVEREF(__pyx_t_10);
-  __Pyx_INCREF(__pyx_v_value);
-  PyList_SET_ITEM(__pyx_t_6, 2, __pyx_v_value);
-  __Pyx_GIVEREF(__pyx_v_value);
-  __pyx_t_4 = 0;
-  __pyx_t_10 = 0;
-  __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_10);
-  PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_6);
-  __Pyx_GIVEREF(__pyx_t_6);
-  __pyx_t_6 = 0;
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_10, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-  /* "pysam/csamfile.pyx":2218
- *                       value ] )
- * 
- *         fmts.append( fmt )             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_fmts, __pyx_v_fmt); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2218; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":2153
- *         return _Py_HashPointer(<void *>self)
- * 
- *     def _convert_python_tag(self, pytag, value, fmts, args):             # <<<<<<<<<<<<<<
- * 
- *         if not type(pytag) is bytes:
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead._convert_python_tag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_t);
-  __Pyx_XDECREF(__pyx_v_pytype);
-  __Pyx_XDECREF(__pyx_v_datafmt);
-  __Pyx_XDECREF(__pyx_v_datatype);
-  __Pyx_XDECREF(__pyx_v_mi);
-  __Pyx_XDECREF(__pyx_v_ma);
-  __Pyx_XDECREF(__pyx_v_absmax);
-  __Pyx_XDECREF(__pyx_v_fmt);
-  __Pyx_XDECREF(__pyx_v_pytag);
-  __Pyx_XDECREF(__pyx_v_value);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2227
- *     property qname:
- *         """the query name (None if not present)"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5qname___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5qname___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2227);
-
-  /* "pysam/csamfile.pyx":2229
- *         def __get__(self):
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_l_qname(src) == 0:
- *                 return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2230
- *             cdef bam1_t * src
- *             src = self._delegate
- *             if pysam_get_l_qname(src) == 0:             # <<<<<<<<<<<<<<
- *                 return None
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
- */
-  __pyx_t_2 = ((pysam_get_l_qname(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2231
- *             src = self._delegate
- *             if pysam_get_l_qname(src) == 0:
- *                 return None             # <<<<<<<<<<<<<<
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
- * 
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2232
- *             if pysam_get_l_qname(src) == 0:
- *                 return None
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))             # <<<<<<<<<<<<<<
- * 
- *         def __set__(self, qname ):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile__charptr_to_str(((char *)pysam_bam_get_qname(__pyx_v_src))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2232; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2227
- *     property qname:
- *         """the query name (None if not present)"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qname.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2234
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
- * 
- *         def __set__(self, qname ):             # <<<<<<<<<<<<<<
- *             if qname == None or len(qname) == 0: return
- *             qname = _forceBytes(qname)
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qname); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qname) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5qname_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_qname));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5qname_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qname) {
-  bam1_t *__pyx_v_src;
-  int __pyx_v_l;
-  char *__pyx_v_p;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_t_2;
-  Py_ssize_t __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  bam1_t *__pyx_t_6;
-  char const *__pyx_t_7;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2234);
-  __Pyx_INCREF(__pyx_v_qname);
-
-  /* "pysam/csamfile.pyx":2235
- * 
- *         def __set__(self, qname ):
- *             if qname == None or len(qname) == 0: return             # <<<<<<<<<<<<<<
- *             qname = _forceBytes(qname)
- *             cdef bam1_t * src
- */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_qname, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (!__pyx_t_2) {
-    __pyx_t_3 = PyObject_Length(__pyx_v_qname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = (__pyx_t_3 == 0);
-    __pyx_t_5 = __pyx_t_4;
-  } else {
-    __pyx_t_5 = __pyx_t_2;
-  }
-  if (__pyx_t_5) {
-    __pyx_r = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2236
- *         def __set__(self, qname ):
- *             if qname == None or len(qname) == 0: return
- *             qname = _forceBytes(qname)             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef int l
- */
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_qname); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF_SET(__pyx_v_qname, __pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":2241
- *             cdef char * p
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             p = pysam_bam_get_qname( src )
- * 
- */
-  __pyx_t_6 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_6;
-
-  /* "pysam/csamfile.pyx":2242
- * 
- *             src = self._delegate
- *             p = pysam_bam_get_qname( src )             # <<<<<<<<<<<<<<
- * 
- *             # the qname is \0 terminated
- */
-  __pyx_v_p = pysam_bam_get_qname(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2245
- * 
- *             # the qname is \0 terminated
- *             l = len(qname) + 1             # <<<<<<<<<<<<<<
- *             pysam_bam_update(src,
- *                              pysam_get_l_qname(src),
- */
-  __pyx_t_3 = PyObject_Length(__pyx_v_qname); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_l = (__pyx_t_3 + 1);
-
-  /* "pysam/csamfile.pyx":2246
- *             # the qname is \0 terminated
- *             l = len(qname) + 1
- *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
- *                              pysam_get_l_qname(src),
- *                              l,
- */
-  pysam_bam_update(__pyx_v_src, pysam_get_l_qname(__pyx_v_src), __pyx_v_l, ((uint8_t *)__pyx_v_p));
-
-  /* "pysam/csamfile.pyx":2252
- * 
- * 
- *             pysam_set_l_qname(src, l)             # <<<<<<<<<<<<<<
- * 
- *             # re-acquire pointer to location in memory
- */
-  pysam_set_l_qname(__pyx_v_src, __pyx_v_l);
-
-  /* "pysam/csamfile.pyx":2256
- *             # re-acquire pointer to location in memory
- *             # as it might have moved
- *             p = pysam_bam_get_qname(src)             # <<<<<<<<<<<<<<
- * 
- *             strncpy(p, qname, l)
- */
-  __pyx_v_p = pysam_bam_get_qname(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2258
- *             p = pysam_bam_get_qname(src)
- * 
- *             strncpy(p, qname, l)             # <<<<<<<<<<<<<<
- * 
- *     property cigar:
- */
-  __pyx_t_7 = __Pyx_PyObject_AsString(__pyx_v_qname); if (unlikely((!__pyx_t_7) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  strncpy(__pyx_v_p, __pyx_t_7, __pyx_v_l);
-
-  /* "pysam/csamfile.pyx":2234
- *             return _charptr_to_str(<char *>pysam_bam_get_qname(src))
- * 
- *         def __set__(self, qname ):             # <<<<<<<<<<<<<<
- *             if qname == None or len(qname) == 0: return
- *             qname = _forceBytes(qname)
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qname.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_qname);
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2299
- *         or None.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t * cigar_p
- *             cdef bam1_t * src
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_op = 0;
-  PyObject *__pyx_v_l = 0;
-  PyObject *__pyx_v_cigar = 0;
-  int __pyx_v_k;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  bam1_t *__pyx_t_2;
-  int __pyx_t_3;
-  uint16_t __pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2299);
-
-  /* "pysam/csamfile.pyx":2304
- *             cdef op, l, cigar
- *             cdef int k
- *             cigar = []             # <<<<<<<<<<<<<<
- * 
- *             src = self._delegate
- */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_cigar = __pyx_t_1;
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":2306
- *             cigar = []
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src) == 0:
- *                 return cigar
- */
-  __pyx_t_2 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_2;
-
-  /* "pysam/csamfile.pyx":2307
- * 
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return cigar
- * 
- */
-  __pyx_t_3 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":2308
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:
- *                 return cigar             # <<<<<<<<<<<<<<
- * 
- *             cigar_p = pysam_bam_get_cigar(src);
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_v_cigar);
-    __pyx_r = __pyx_v_cigar;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2310
- *                 return cigar
- * 
- *             cigar_p = pysam_bam_get_cigar(src);             # <<<<<<<<<<<<<<
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2311
- * 
- *             cigar_p = pysam_bam_get_cigar(src);
- *             for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-  __pyx_t_4 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":2312
- *             cigar_p = pysam_bam_get_cigar(src);
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 cigar.append((op, l))
- */
-    __pyx_t_1 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_XDECREF_SET(__pyx_v_op, __pyx_t_1);
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":2313
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- *                 cigar.append((op, l))
- *             return cigar
- */
-    __pyx_t_1 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2313; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_1);
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":2314
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 cigar.append((op, l))             # <<<<<<<<<<<<<<
- *             return cigar
- * 
- */
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_INCREF(__pyx_v_op);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_op);
-    __Pyx_GIVEREF(__pyx_v_op);
-    __Pyx_INCREF(__pyx_v_l);
-    PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_l);
-    __Pyx_GIVEREF(__pyx_v_l);
-    __pyx_t_5 = __Pyx_PyObject_Append(__pyx_v_cigar, __pyx_t_1); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  }
-
-  /* "pysam/csamfile.pyx":2315
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 cigar.append((op, l))
- *             return cigar             # <<<<<<<<<<<<<<
- * 
- *         def __set__(self, values):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_cigar);
-  __pyx_r = __pyx_v_cigar;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2299
- *         or None.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t * cigar_p
- *             cdef bam1_t * src
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.cigar.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_op);
-  __Pyx_XDECREF(__pyx_v_l);
-  __Pyx_XDECREF(__pyx_v_cigar);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2317
- *             return cigar
- * 
- *         def __set__(self, values):             # <<<<<<<<<<<<<<
- *             cdef uint32_t * p
- *             cdef bam1_t * src
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_values); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_values) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_values));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5cigar_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_values) {
-  uint32_t *__pyx_v_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_op = 0;
-  PyObject *__pyx_v_l = 0;
-  int __pyx_v_k;
-  int __pyx_v_ncigar;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  int __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  Py_ssize_t __pyx_t_5;
-  PyObject *(*__pyx_t_6)(PyObject *);
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  PyObject *(*__pyx_t_11)(PyObject *);
-  uint32_t __pyx_t_12;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2317);
-  __Pyx_INCREF(__pyx_v_values);
-
-  /* "pysam/csamfile.pyx":2323
- *             cdef int k, ncigar
- * 
- *             k = 0             # <<<<<<<<<<<<<<
- * 
- *             src = self._delegate
- */
-  __pyx_v_k = 0;
-
-  /* "pysam/csamfile.pyx":2325
- *             k = 0
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             # get location of cigar string
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2328
- * 
- *             # get location of cigar string
- *             p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *             # empty values for cigar string
- */
-  __pyx_v_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2331
- * 
- *             # empty values for cigar string
- *             if values is None:             # <<<<<<<<<<<<<<
- *                 values = []
- * 
- */
-  __pyx_t_2 = (__pyx_v_values == Py_None);
-  __pyx_t_3 = (__pyx_t_2 != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":2332
- *             # empty values for cigar string
- *             if values is None:
- *                 values = []             # <<<<<<<<<<<<<<
- * 
- *             ncigar = len(values)
- */
-    __pyx_t_4 = PyList_New(0); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF_SET(__pyx_v_values, __pyx_t_4);
-    __pyx_t_4 = 0;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":2334
- *                 values = []
- * 
- *             ncigar = len(values)             # <<<<<<<<<<<<<<
- *             # create space for cigar data within src.data
- *             pysam_bam_update(src,
- */
-  __pyx_t_5 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_ncigar = __pyx_t_5;
-
-  /* "pysam/csamfile.pyx":2336
- *             ncigar = len(values)
- *             # create space for cigar data within src.data
- *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
- *                              pysam_get_n_cigar(src) * 4,
- *                              ncigar * 4,
- */
-  pysam_bam_update(__pyx_v_src, (pysam_get_n_cigar(__pyx_v_src) * 4), (__pyx_v_ncigar * 4), ((uint8_t *)__pyx_v_p));
-
-  /* "pysam/csamfile.pyx":2342
- * 
- *             # length is number of cigar operations, not bytes
- *             pysam_set_n_cigar(src, ncigar)             # <<<<<<<<<<<<<<
- * 
- *             # re-acquire pointer to location in memory
- */
-  pysam_set_n_cigar(__pyx_v_src, __pyx_v_ncigar);
-
-  /* "pysam/csamfile.pyx":2346
- *             # re-acquire pointer to location in memory
- *             # as it might have moved
- *             p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *             # insert cigar operations
- */
-  __pyx_v_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2349
- * 
- *             # insert cigar operations
- *             for op, l in values:             # <<<<<<<<<<<<<<
- *                 p[k] = l << BAM_CIGAR_SHIFT | op
- *                 k += 1
- */
-  if (PyList_CheckExact(__pyx_v_values) || PyTuple_CheckExact(__pyx_v_values)) {
-    __pyx_t_4 = __pyx_v_values; __Pyx_INCREF(__pyx_t_4); __pyx_t_5 = 0;
-    __pyx_t_6 = NULL;
-  } else {
-    __pyx_t_5 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_6 = Py_TYPE(__pyx_t_4)->tp_iternext;
-  }
-  for (;;) {
-    if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_4)) {
-      if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_4)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_4)) {
-      if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_4)) break;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_7); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      #endif
-    } else {
-      __pyx_t_7 = __pyx_t_6(__pyx_t_4);
-      if (unlikely(!__pyx_t_7)) {
-        PyObject* exc_type = PyErr_Occurred();
-        if (exc_type) {
-          if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        break;
-      }
-      __Pyx_GOTREF(__pyx_t_7);
-    }
-    if ((likely(PyTuple_CheckExact(__pyx_t_7))) || (PyList_CheckExact(__pyx_t_7))) {
-      PyObject* sequence = __pyx_t_7;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      Py_ssize_t size = Py_SIZE(sequence);
-      #else
-      Py_ssize_t size = PySequence_Size(sequence);
-      #endif
-      if (unlikely(size != 2)) {
-        if (size > 2) __Pyx_RaiseTooManyValuesError(2);
-        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      #if CYTHON_COMPILING_IN_CPYTHON
-      if (likely(PyTuple_CheckExact(sequence))) {
-        __pyx_t_8 = PyTuple_GET_ITEM(sequence, 0); 
-        __pyx_t_9 = PyTuple_GET_ITEM(sequence, 1); 
-      } else {
-        __pyx_t_8 = PyList_GET_ITEM(sequence, 0); 
-        __pyx_t_9 = PyList_GET_ITEM(sequence, 1); 
-      }
-      __Pyx_INCREF(__pyx_t_8);
-      __Pyx_INCREF(__pyx_t_9);
-      #else
-      __pyx_t_8 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_9 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      #endif
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    } else {
-      Py_ssize_t index = -1;
-      __pyx_t_10 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_10);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_11 = Py_TYPE(__pyx_t_10)->tp_iternext;
-      index = 0; __pyx_t_8 = __pyx_t_11(__pyx_t_10); if (unlikely(!__pyx_t_8)) goto __pyx_L6_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_8);
-      index = 1; __pyx_t_9 = __pyx_t_11(__pyx_t_10); if (unlikely(!__pyx_t_9)) goto __pyx_L6_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_9);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_11(__pyx_t_10), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_11 = NULL;
-      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      goto __pyx_L7_unpacking_done;
-      __pyx_L6_unpacking_failed:;
-      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      __pyx_t_11 = NULL;
-      if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_L7_unpacking_done:;
-    }
-    __Pyx_XDECREF_SET(__pyx_v_op, __pyx_t_8);
-    __pyx_t_8 = 0;
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_9);
-    __pyx_t_9 = 0;
-
-    /* "pysam/csamfile.pyx":2350
- *             # insert cigar operations
- *             for op, l in values:
- *                 p[k] = l << BAM_CIGAR_SHIFT | op             # <<<<<<<<<<<<<<
- *                 k += 1
- * 
- */
-    __pyx_t_7 = __Pyx_PyInt_From_int(BAM_CIGAR_SHIFT); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_9 = PyNumber_Lshift(__pyx_v_l, __pyx_t_7); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    __pyx_t_7 = PyNumber_Or(__pyx_t_9, __pyx_v_op); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_12 = __Pyx_PyInt_As_uint32_t(__pyx_t_7); if (unlikely((__pyx_t_12 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    (__pyx_v_p[__pyx_v_k]) = __pyx_t_12;
-
-    /* "pysam/csamfile.pyx":2351
- *             for op, l in values:
- *                 p[k] = l << BAM_CIGAR_SHIFT | op
- *                 k += 1             # <<<<<<<<<<<<<<
- * 
- *             ## setting the cigar string requires updating the bin
- */
-    __pyx_v_k = (__pyx_v_k + 1);
-  }
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-
-  /* "pysam/csamfile.pyx":2354
- * 
- *             ## setting the cigar string requires updating the bin
- *             pysam_set_bin(src,             # <<<<<<<<<<<<<<
- *                           hts_reg2bin(
- *                               src.core.pos,
- */
-  pysam_set_bin(__pyx_v_src, hts_reg2bin(__pyx_v_src->core.pos, bam_endpos(__pyx_v_src), 14, 5));
-
-  /* "pysam/csamfile.pyx":2317
- *             return cigar
- * 
- *         def __set__(self, values):             # <<<<<<<<<<<<<<
- *             cdef uint32_t * p
- *             cdef bam1_t * src
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.cigar.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_op);
-  __Pyx_XDECREF(__pyx_v_l);
-  __Pyx_XDECREF(__pyx_v_values);
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2378
- *         empty string.
- *         '''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             c = self.cigar
- *             if c == None: return ""
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_v_c = NULL;
-  PyObject *__pyx_v_x = NULL;
-  PyObject *__pyx_v_y = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  Py_ssize_t __pyx_t_4;
-  PyObject *(*__pyx_t_5)(PyObject *);
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *(*__pyx_t_10)(PyObject *);
-  Py_ssize_t __pyx_t_11;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2378);
-
-  /* "pysam/csamfile.pyx":2379
- *         '''
- *         def __get__(self):
- *             c = self.cigar             # <<<<<<<<<<<<<<
- *             if c == None: return ""
- *             # reverse order
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigar); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_c = __pyx_t_1;
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":2380
- *         def __get__(self):
- *             c = self.cigar
- *             if c == None: return ""             # <<<<<<<<<<<<<<
- *             # reverse order
- *             else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
- */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_c, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_kp_s__9);
-    __pyx_r = __pyx_kp_s__9;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":2382
- *             if c == None: return ""
- *             # reverse order
- *             else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])             # <<<<<<<<<<<<<<
- * 
- *         def __set__(self, cigar):
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    if (PyList_CheckExact(__pyx_v_c) || PyTuple_CheckExact(__pyx_v_c)) {
-      __pyx_t_3 = __pyx_v_c; __Pyx_INCREF(__pyx_t_3); __pyx_t_4 = 0;
-      __pyx_t_5 = NULL;
-    } else {
-      __pyx_t_4 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_v_c); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_5 = Py_TYPE(__pyx_t_3)->tp_iternext;
-    }
-    for (;;) {
-      if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_3)) {
-        if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_3)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_6 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_6); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_6 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_3)) {
-        if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_3)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_6); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_6 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else {
-        __pyx_t_6 = __pyx_t_5(__pyx_t_3);
-        if (unlikely(!__pyx_t_6)) {
-          PyObject* exc_type = PyErr_Occurred();
-          if (exc_type) {
-            if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          break;
-        }
-        __Pyx_GOTREF(__pyx_t_6);
-      }
-      if ((likely(PyTuple_CheckExact(__pyx_t_6))) || (PyList_CheckExact(__pyx_t_6))) {
-        PyObject* sequence = __pyx_t_6;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        Py_ssize_t size = Py_SIZE(sequence);
-        #else
-        Py_ssize_t size = PySequence_Size(sequence);
-        #endif
-        if (unlikely(size != 2)) {
-          if (size > 2) __Pyx_RaiseTooManyValuesError(2);
-          else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        #if CYTHON_COMPILING_IN_CPYTHON
-        if (likely(PyTuple_CheckExact(sequence))) {
-          __pyx_t_7 = PyTuple_GET_ITEM(sequence, 0); 
-          __pyx_t_8 = PyTuple_GET_ITEM(sequence, 1); 
-        } else {
-          __pyx_t_7 = PyList_GET_ITEM(sequence, 0); 
-          __pyx_t_8 = PyList_GET_ITEM(sequence, 1); 
-        }
-        __Pyx_INCREF(__pyx_t_7);
-        __Pyx_INCREF(__pyx_t_8);
-        #else
-        __pyx_t_7 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_8 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        #endif
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      } else {
-        Py_ssize_t index = -1;
-        __pyx_t_9 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_10 = Py_TYPE(__pyx_t_9)->tp_iternext;
-        index = 0; __pyx_t_7 = __pyx_t_10(__pyx_t_9); if (unlikely(!__pyx_t_7)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_7);
-        index = 1; __pyx_t_8 = __pyx_t_10(__pyx_t_9); if (unlikely(!__pyx_t_8)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_8);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_10(__pyx_t_9), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_10 = NULL;
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        goto __pyx_L7_unpacking_done;
-        __pyx_L6_unpacking_failed:;
-        __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_10 = NULL;
-        if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_L7_unpacking_done:;
-      }
-      __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_7);
-      __pyx_t_7 = 0;
-      __Pyx_XDECREF_SET(__pyx_v_y, __pyx_t_8);
-      __pyx_t_8 = 0;
-      __pyx_t_11 = __Pyx_PyIndex_AsSsize_t(__pyx_v_x); if (unlikely((__pyx_t_11 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_6 = __Pyx_PyInt_From_char((__pyx_v_5pysam_8csamfile_CODE2CIGAR[__pyx_t_11])); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_INCREF(__pyx_v_y);
-      PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_y);
-      __Pyx_GIVEREF(__pyx_v_y);
-      PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_6);
-      __Pyx_GIVEREF(__pyx_t_6);
-      __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_i_c, __pyx_t_8); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    }
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__9, __pyx_t_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2378
- *         empty string.
- *         '''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             c = self.cigar
- *             if c == None: return ""
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.cigarstring.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_c);
-  __Pyx_XDECREF(__pyx_v_x);
-  __Pyx_XDECREF(__pyx_v_y);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2384
- *             else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
- * 
- *         def __set__(self, cigar):             # <<<<<<<<<<<<<<
- *             if cigar is None or len(cigar) == 0:
- *                 self.cigar = []
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_cigar); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_cigar) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_cigar));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11cigarstring_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_cigar) {
-  PyObject *__pyx_v_parts = NULL;
-  PyObject *__pyx_v_x = NULL;
-  PyObject *__pyx_v_y = NULL;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  Py_ssize_t __pyx_t_2;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *(*__pyx_t_8)(PyObject *);
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  PyObject *__pyx_t_11 = NULL;
-  PyObject *(*__pyx_t_12)(PyObject *);
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2384);
-
-  /* "pysam/csamfile.pyx":2385
- * 
- *         def __set__(self, cigar):
- *             if cigar is None or len(cigar) == 0:             # <<<<<<<<<<<<<<
- *                 self.cigar = []
- *             else:
- */
-  __pyx_t_1 = (__pyx_v_cigar == Py_None);
-  if (!(__pyx_t_1 != 0)) {
-    __pyx_t_2 = PyObject_Length(__pyx_v_cigar); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = ((__pyx_t_2 == 0) != 0);
-    __pyx_t_4 = __pyx_t_3;
-  } else {
-    __pyx_t_4 = (__pyx_t_1 != 0);
-  }
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":2386
- *         def __set__(self, cigar):
- *             if cigar is None or len(cigar) == 0:
- *                 self.cigar = []             # <<<<<<<<<<<<<<
- *             else:
- *                 parts = CIGAR_REGEX.findall(cigar)
- */
-    __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigar, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    goto __pyx_L3;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":2388
- *                 self.cigar = []
- *             else:
- *                 parts = CIGAR_REGEX.findall(cigar)             # <<<<<<<<<<<<<<
- *                 # reverse order
- *                 self.cigar = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]
- */
-    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_CIGAR_REGEX); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_findall); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_INCREF(__pyx_v_cigar);
-    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_cigar);
-    __Pyx_GIVEREF(__pyx_v_cigar);
-    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_5, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_v_parts = __pyx_t_7;
-    __pyx_t_7 = 0;
-
-    /* "pysam/csamfile.pyx":2390
- *                 parts = CIGAR_REGEX.findall(cigar)
- *                 # reverse order
- *                 self.cigar = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]             # <<<<<<<<<<<<<<
- * 
- *     property seq:
- */
-    __pyx_t_7 = PyList_New(0); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    if (PyList_CheckExact(__pyx_v_parts) || PyTuple_CheckExact(__pyx_v_parts)) {
-      __pyx_t_5 = __pyx_v_parts; __Pyx_INCREF(__pyx_t_5); __pyx_t_2 = 0;
-      __pyx_t_8 = NULL;
-    } else {
-      __pyx_t_2 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_v_parts); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_8 = Py_TYPE(__pyx_t_5)->tp_iternext;
-    }
-    for (;;) {
-      if (!__pyx_t_8 && PyList_CheckExact(__pyx_t_5)) {
-        if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_5)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_6 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_2); __Pyx_INCREF(__pyx_t_6); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_6 = PySequence_ITEM(__pyx_t_5, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else if (!__pyx_t_8 && PyTuple_CheckExact(__pyx_t_5)) {
-        if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_5)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_2); __Pyx_INCREF(__pyx_t_6); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_6 = PySequence_ITEM(__pyx_t_5, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else {
-        __pyx_t_6 = __pyx_t_8(__pyx_t_5);
-        if (unlikely(!__pyx_t_6)) {
-          PyObject* exc_type = PyErr_Occurred();
-          if (exc_type) {
-            if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          break;
-        }
-        __Pyx_GOTREF(__pyx_t_6);
-      }
-      if ((likely(PyTuple_CheckExact(__pyx_t_6))) || (PyList_CheckExact(__pyx_t_6))) {
-        PyObject* sequence = __pyx_t_6;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        Py_ssize_t size = Py_SIZE(sequence);
-        #else
-        Py_ssize_t size = PySequence_Size(sequence);
-        #endif
-        if (unlikely(size != 2)) {
-          if (size > 2) __Pyx_RaiseTooManyValuesError(2);
-          else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        #if CYTHON_COMPILING_IN_CPYTHON
-        if (likely(PyTuple_CheckExact(sequence))) {
-          __pyx_t_9 = PyTuple_GET_ITEM(sequence, 0); 
-          __pyx_t_10 = PyTuple_GET_ITEM(sequence, 1); 
-        } else {
-          __pyx_t_9 = PyList_GET_ITEM(sequence, 0); 
-          __pyx_t_10 = PyList_GET_ITEM(sequence, 1); 
-        }
-        __Pyx_INCREF(__pyx_t_9);
-        __Pyx_INCREF(__pyx_t_10);
-        #else
-        __pyx_t_9 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_10 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_10);
-        #endif
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      } else {
-        Py_ssize_t index = -1;
-        __pyx_t_11 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_11);
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext;
-        index = 0; __pyx_t_9 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_9)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_9);
-        index = 1; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_10);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_12 = NULL;
-        __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-        goto __pyx_L7_unpacking_done;
-        __pyx_L6_unpacking_failed:;
-        __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-        __pyx_t_12 = NULL;
-        if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_L7_unpacking_done:;
-      }
-      __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_9);
-      __pyx_t_9 = 0;
-      __Pyx_XDECREF_SET(__pyx_v_y, __pyx_t_10);
-      __pyx_t_10 = 0;
-      __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_CIGAR2CODE); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_10);
-      __Pyx_INCREF(__pyx_v_y);
-      PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_v_y);
-      __Pyx_GIVEREF(__pyx_v_y);
-      __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_10, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      __pyx_t_10 = PyObject_GetItem(__pyx_t_6, __pyx_t_9); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_10);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_9 = PyNumber_Int(__pyx_v_x); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_10);
-      __Pyx_GIVEREF(__pyx_t_10);
-      PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_t_9);
-      __Pyx_GIVEREF(__pyx_t_9);
-      __pyx_t_10 = 0;
-      __pyx_t_9 = 0;
-      if (unlikely(__Pyx_ListComp_Append(__pyx_t_7, (PyObject*)__pyx_t_6))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    }
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    if (__Pyx_PyObject_SetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_cigar, __pyx_t_7) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":2384
- *             else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
- * 
- *         def __set__(self, cigar):             # <<<<<<<<<<<<<<
- *             if cigar is None or len(cigar) == 0:
- *                 self.cigar = []
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_XDECREF(__pyx_t_11);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.cigarstring.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_parts);
-  __Pyx_XDECREF(__pyx_v_x);
-  __Pyx_XDECREF(__pyx_v_y);
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2412
- *         sequence.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef char * s
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3seq___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2412);
-
-  /* "pysam/csamfile.pyx":2415
- *             cdef bam1_t * src
- *             cdef char * s
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             if src.core.l_qseq == 0: return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2417
- *             src = self._delegate
- * 
- *             if src.core.l_qseq == 0: return None             # <<<<<<<<<<<<<<
- * 
- *             return get_seq_range(src, 0, src.core.l_qseq)
- */
-  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2419
- *             if src.core.l_qseq == 0: return None
- * 
- *             return get_seq_range(src, 0, src.core.l_qseq)             # <<<<<<<<<<<<<<
- * 
- *         def __set__(self, seq):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_get_seq_range(__pyx_v_src, 0, __pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2419; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2412
- *         sequence.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef char * s
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.seq.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2421
- *             return get_seq_range(src, 0, src.core.l_qseq)
- * 
- *         def __set__(self, seq):             # <<<<<<<<<<<<<<
- *             # samtools manages sequence and quality length memory together
- *             # if no quality information is present, the first byte says 0xff.
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_seq); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_seq) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_seq));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3seq_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_seq) {
-  bam1_t *__pyx_v_src;
-  uint8_t *__pyx_v_p;
-  char *__pyx_v_s;
-  int __pyx_v_l;
-  int __pyx_v_k;
-  int __pyx_v_nbytes_new;
-  int __pyx_v_nbytes_old;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_t_2;
-  Py_ssize_t __pyx_t_3;
-  bam1_t *__pyx_t_4;
-  int __pyx_t_5;
-  char *__pyx_t_6;
-  long __pyx_t_7;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2421);
-  __Pyx_INCREF(__pyx_v_seq);
-
-  /* "pysam/csamfile.pyx":2429
- *             cdef int l, k, nbytes_new, nbytes_old
- * 
- *             if seq == None:             # <<<<<<<<<<<<<<
- *                 l = 0
- *             else:
- */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_seq, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2430
- * 
- *             if seq == None:
- *                 l = 0             # <<<<<<<<<<<<<<
- *             else:
- *                 l = len(seq)
- */
-    __pyx_v_l = 0;
-    goto __pyx_L3;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":2432
- *                 l = 0
- *             else:
- *                 l = len(seq)             # <<<<<<<<<<<<<<
- *                 seq = _forceBytes(seq)
- * 
- */
-    __pyx_t_3 = PyObject_Length(__pyx_v_seq); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_l = __pyx_t_3;
-
-    /* "pysam/csamfile.pyx":2433
- *             else:
- *                 l = len(seq)
- *                 seq = _forceBytes(seq)             # <<<<<<<<<<<<<<
- * 
- *             src = self._delegate
- */
-    __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_seq); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF_SET(__pyx_v_seq, __pyx_t_1);
-    __pyx_t_1 = 0;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":2435
- *                 seq = _forceBytes(seq)
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             # as the sequence is stored in half-bytes, the total length (sequence
- */
-  __pyx_t_4 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":2439
- *             # as the sequence is stored in half-bytes, the total length (sequence
- *             # plus quality scores) is (l+1)/2 + l
- *             nbytes_new = (l + 1) / 2 + l             # <<<<<<<<<<<<<<
- *             nbytes_old = (src.core.l_qseq + 1) / 2 + src.core.l_qseq
- * 
- */
-  __pyx_v_nbytes_new = (__Pyx_div_long((__pyx_v_l + 1), 2) + __pyx_v_l);
-
-  /* "pysam/csamfile.pyx":2440
- *             # plus quality scores) is (l+1)/2 + l
- *             nbytes_new = (l + 1) / 2 + l
- *             nbytes_old = (src.core.l_qseq + 1) / 2 + src.core.l_qseq             # <<<<<<<<<<<<<<
- * 
- *             # acquire pointer to location in memory
- */
-  __pyx_v_nbytes_old = (__Pyx_div_long((__pyx_v_src->core.l_qseq + 1), 2) + __pyx_v_src->core.l_qseq);
-
-  /* "pysam/csamfile.pyx":2443
- * 
- *             # acquire pointer to location in memory
- *             p = pysam_bam_get_seq(src)             # <<<<<<<<<<<<<<
- *             src.core.l_qseq = l
- * 
- */
-  __pyx_v_p = pysam_bam_get_seq(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2444
- *             # acquire pointer to location in memory
- *             p = pysam_bam_get_seq(src)
- *             src.core.l_qseq = l             # <<<<<<<<<<<<<<
- * 
- *             # change length of data field
- */
-  __pyx_v_src->core.l_qseq = __pyx_v_l;
-
-  /* "pysam/csamfile.pyx":2447
- * 
- *             # change length of data field
- *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
- *                              nbytes_old,
- *                              nbytes_new,
- */
-  pysam_bam_update(__pyx_v_src, __pyx_v_nbytes_old, __pyx_v_nbytes_new, __pyx_v_p);
-
-  /* "pysam/csamfile.pyx":2452
- *                              p)
- * 
- *             if l > 0:             # <<<<<<<<<<<<<<
- *                 # re-acquire pointer to location in memory
- *                 # as it might have moved
- */
-  __pyx_t_2 = ((__pyx_v_l > 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2455
- *                 # re-acquire pointer to location in memory
- *                 # as it might have moved
- *                 p = pysam_bam_get_seq(src)             # <<<<<<<<<<<<<<
- *                 for k from 0 <= k < nbytes_new:
- *                     p[k] = 0
- */
-    __pyx_v_p = pysam_bam_get_seq(__pyx_v_src);
-
-    /* "pysam/csamfile.pyx":2456
- *                 # as it might have moved
- *                 p = pysam_bam_get_seq(src)
- *                 for k from 0 <= k < nbytes_new:             # <<<<<<<<<<<<<<
- *                     p[k] = 0
- *                 # convert to C string
- */
-    __pyx_t_5 = __pyx_v_nbytes_new;
-    for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
-
-      /* "pysam/csamfile.pyx":2457
- *                 p = pysam_bam_get_seq(src)
- *                 for k from 0 <= k < nbytes_new:
- *                     p[k] = 0             # <<<<<<<<<<<<<<
- *                 # convert to C string
- *                 s = seq
- */
-      (__pyx_v_p[__pyx_v_k]) = 0;
-    }
-
-    /* "pysam/csamfile.pyx":2459
- *                     p[k] = 0
- *                 # convert to C string
- *                 s = seq             # <<<<<<<<<<<<<<
- *                 for k from 0 <= k < l:
- *                     p[k/2] |= seq_nt16_table[<unsigned char>s[k]] << 4 * (1 - k % 2)
- */
-    __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_seq); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_s = __pyx_t_6;
-
-    /* "pysam/csamfile.pyx":2460
- *                 # convert to C string
- *                 s = seq
- *                 for k from 0 <= k < l:             # <<<<<<<<<<<<<<
- *                     p[k/2] |= seq_nt16_table[<unsigned char>s[k]] << 4 * (1 - k % 2)
- * 
- */
-    __pyx_t_5 = __pyx_v_l;
-    for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
-
-      /* "pysam/csamfile.pyx":2461
- *                 s = seq
- *                 for k from 0 <= k < l:
- *                     p[k/2] |= seq_nt16_table[<unsigned char>s[k]] << 4 * (1 - k % 2)             # <<<<<<<<<<<<<<
- * 
- *                 # erase qualities
- */
-      __pyx_t_7 = __Pyx_div_long(__pyx_v_k, 2);
-      (__pyx_v_p[__pyx_t_7]) = ((__pyx_v_p[__pyx_t_7]) | ((seq_nt16_table[((unsigned char)(__pyx_v_s[__pyx_v_k]))]) << (4 * (1 - __Pyx_mod_long(__pyx_v_k, 2)))));
-    }
-
-    /* "pysam/csamfile.pyx":2464
- * 
- *                 # erase qualities
- *                 p = pysam_bam_get_qual(src)             # <<<<<<<<<<<<<<
- *                 p[0] = 0xff
- * 
- */
-    __pyx_v_p = pysam_bam_get_qual(__pyx_v_src);
-
-    /* "pysam/csamfile.pyx":2465
- *                 # erase qualities
- *                 p = pysam_bam_get_qual(src)
- *                 p[0] = 0xff             # <<<<<<<<<<<<<<
- * 
- *     property qual:
- */
-    (__pyx_v_p[0]) = 0xff;
-    goto __pyx_L4;
-  }
-  __pyx_L4:;
-
-  /* "pysam/csamfile.pyx":2421
- *             return get_seq_range(src, 0, src.core.l_qseq)
- * 
- *         def __set__(self, seq):             # <<<<<<<<<<<<<<
- *             # samtools manages sequence and quality length memory together
- *             # if no quality information is present, the first byte says 0xff.
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.seq.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_seq);
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2482
- *         quality scores and the sequence are not the same.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- * 
- *             cdef bam1_t * src
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4qual___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qual___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2482);
-
-  /* "pysam/csamfile.pyx":2487
- *             cdef char * q
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             if src.core.l_qseq == 0: return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2489
- *             src = self._delegate
- * 
- *             if src.core.l_qseq == 0: return None             # <<<<<<<<<<<<<<
- * 
- *             return get_qual_range(src, 0, src.core.l_qseq)
- */
-  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2491
- *             if src.core.l_qseq == 0: return None
- * 
- *             return get_qual_range(src, 0, src.core.l_qseq)             # <<<<<<<<<<<<<<
- * 
- *         def __set__(self,qual):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_get_qual_range(__pyx_v_src, 0, __pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2482
- *         quality scores and the sequence are not the same.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- * 
- *             cdef bam1_t * src
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qual.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2493
- *             return get_qual_range(src, 0, src.core.l_qseq)
- * 
- *         def __set__(self,qual):             # <<<<<<<<<<<<<<
- *             # note that space is already allocated via the sequences
- *             cdef bam1_t * src
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4qual_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_qual));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4qual_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qual) {
-  bam1_t *__pyx_v_src;
-  uint8_t *__pyx_v_p;
-  char *__pyx_v_q;
-  int __pyx_v_k;
-  int __pyx_v_l;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  Py_ssize_t __pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  char *__pyx_t_7;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  int __pyx_t_10;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2493);
-  __Pyx_INCREF(__pyx_v_qual);
-
-  /* "pysam/csamfile.pyx":2500
- *             cdef int k
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             p = pysam_bam_get_qual(src)
- *             if qual == None or len(qual) == 0:
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2501
- * 
- *             src = self._delegate
- *             p = pysam_bam_get_qual(src)             # <<<<<<<<<<<<<<
- *             if qual == None or len(qual) == 0:
- *                 # if absent - set to 0xff
- */
-  __pyx_v_p = pysam_bam_get_qual(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2502
- *             src = self._delegate
- *             p = pysam_bam_get_qual(src)
- *             if qual == None or len(qual) == 0:             # <<<<<<<<<<<<<<
- *                 # if absent - set to 0xff
- *                 p[0] = 0xff
- */
-  __pyx_t_2 = PyObject_RichCompare(__pyx_v_qual, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (!__pyx_t_3) {
-    __pyx_t_4 = PyObject_Length(__pyx_v_qual); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = (__pyx_t_4 == 0);
-    __pyx_t_6 = __pyx_t_5;
-  } else {
-    __pyx_t_6 = __pyx_t_3;
-  }
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":2504
- *             if qual == None or len(qual) == 0:
- *                 # if absent - set to 0xff
- *                 p[0] = 0xff             # <<<<<<<<<<<<<<
- *                 return
- *             qual = _forceBytes(qual)
- */
-    (__pyx_v_p[0]) = 0xff;
-
-    /* "pysam/csamfile.pyx":2505
- *                 # if absent - set to 0xff
- *                 p[0] = 0xff
- *                 return             # <<<<<<<<<<<<<<
- *             qual = _forceBytes(qual)
- *             cdef int l
- */
-    __pyx_r = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2506
- *                 p[0] = 0xff
- *                 return
- *             qual = _forceBytes(qual)             # <<<<<<<<<<<<<<
- *             cdef int l
- *             # convert to C string
- */
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_qual); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF_SET(__pyx_v_qual, __pyx_t_2);
-  __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":2509
- *             cdef int l
- *             # convert to C string
- *             q = qual             # <<<<<<<<<<<<<<
- *             l = len(qual)
- *             if src.core.l_qseq != l:
- */
-  __pyx_t_7 = __Pyx_PyObject_AsString(__pyx_v_qual); if (unlikely((!__pyx_t_7) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_q = __pyx_t_7;
-
-  /* "pysam/csamfile.pyx":2510
- *             # convert to C string
- *             q = qual
- *             l = len(qual)             # <<<<<<<<<<<<<<
- *             if src.core.l_qseq != l:
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
- */
-  __pyx_t_4 = PyObject_Length(__pyx_v_qual); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_l = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":2511
- *             q = qual
- *             l = len(qual)
- *             if src.core.l_qseq != l:             # <<<<<<<<<<<<<<
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
- *             assert src.core.l_qseq == l
- */
-  __pyx_t_6 = ((__pyx_v_src->core.l_qseq != __pyx_v_l) != 0);
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":2512
- *             l = len(qual)
- *             if src.core.l_qseq != l:
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))             # <<<<<<<<<<<<<<
- *             assert src.core.l_qseq == l
- *             for k from 0 <= k < l:
- */
-    __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_l); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_8 = __Pyx_PyInt_From_int32_t(__pyx_v_src->core.l_qseq); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_2);
-    __Pyx_GIVEREF(__pyx_t_2);
-    PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_t_8);
-    __Pyx_GIVEREF(__pyx_t_8);
-    __pyx_t_2 = 0;
-    __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_quality_and_sequence_mismatch_i, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
-    __Pyx_GIVEREF(__pyx_t_8);
-    __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_Raise(__pyx_t_8, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":2513
- *             if src.core.l_qseq != l:
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
- *             assert src.core.l_qseq == l             # <<<<<<<<<<<<<<
- *             for k from 0 <= k < l:
- *                 p[k] = <uint8_t>q[k] - 33
- */
-  #ifndef CYTHON_WITHOUT_ASSERTIONS
-  if (unlikely(!Py_OptimizeFlag)) {
-    if (unlikely(!((__pyx_v_src->core.l_qseq == __pyx_v_l) != 0))) {
-      PyErr_SetNone(PyExc_AssertionError);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-  }
-  #endif
-
-  /* "pysam/csamfile.pyx":2514
- *                 raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
- *             assert src.core.l_qseq == l
- *             for k from 0 <= k < l:             # <<<<<<<<<<<<<<
- *                 p[k] = <uint8_t>q[k] - 33
- * 
- */
-  __pyx_t_10 = __pyx_v_l;
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_10; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":2515
- *             assert src.core.l_qseq == l
- *             for k from 0 <= k < l:
- *                 p[k] = <uint8_t>q[k] - 33             # <<<<<<<<<<<<<<
- * 
- *     property query:
- */
-    (__pyx_v_p[__pyx_v_k]) = (((uint8_t)(__pyx_v_q[__pyx_v_k])) - 33);
-  }
-
-  /* "pysam/csamfile.pyx":2493
- *             return get_qual_range(src, 0, src.core.l_qseq)
- * 
- *         def __set__(self,qual):             # <<<<<<<<<<<<<<
- *             # note that space is already allocated via the sequences
- *             cdef bam1_t * src
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qual.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_qual);
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2534
- *         were not considered for alignment may have been retained."""
- * 
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint32_t start, end
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5query_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5query_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5query___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5query___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  uint32_t __pyx_v_start;
-  uint32_t __pyx_v_end;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  int32_t __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2534);
-
-  /* "pysam/csamfile.pyx":2539
- *             cdef char * s
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             if src.core.l_qseq == 0: return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2541
- *             src = self._delegate
- * 
- *             if src.core.l_qseq == 0: return None             # <<<<<<<<<<<<<<
- * 
- *             start = query_start(src)
- */
-  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2543
- *             if src.core.l_qseq == 0: return None
- * 
- *             start = query_start(src)             # <<<<<<<<<<<<<<
- *             end   = query_end(src)
- * 
- */
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_start(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_start = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":2544
- * 
- *             start = query_start(src)
- *             end   = query_end(src)             # <<<<<<<<<<<<<<
- * 
- *             return get_seq_range(src, start, end)
- */
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_end(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_end = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":2546
- *             end   = query_end(src)
- * 
- *             return get_seq_range(src, start, end)             # <<<<<<<<<<<<<<
- * 
- *     property qqual:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_4 = __pyx_f_5pysam_8csamfile_get_seq_range(__pyx_v_src, __pyx_v_start, __pyx_v_end); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2534
- *         were not considered for alignment may have been retained."""
- * 
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint32_t start, end
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.query.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2557
- * 
- *         In Python 3, this property is of type bytes."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint32_t start, end
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5qqual_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5qqual_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5qqual___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5qqual___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  uint32_t __pyx_v_start;
-  uint32_t __pyx_v_end;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  int32_t __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2557);
-
-  /* "pysam/csamfile.pyx":2561
- *             cdef uint32_t start, end
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- * 
- *             if src.core.l_qseq == 0: return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2563
- *             src = self._delegate
- * 
- *             if src.core.l_qseq == 0: return None             # <<<<<<<<<<<<<<
- * 
- *             start = query_start(src)
- */
-  __pyx_t_2 = ((__pyx_v_src->core.l_qseq == 0) != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2565
- *             if src.core.l_qseq == 0: return None
- * 
- *             start = query_start(src)             # <<<<<<<<<<<<<<
- *             end   = query_end(src)
- * 
- */
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_start(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_start = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":2566
- * 
- *             start = query_start(src)
- *             end   = query_end(src)             # <<<<<<<<<<<<<<
- * 
- *             return get_qual_range(src, start, end)
- */
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_end(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_end = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":2568
- *             end   = query_end(src)
- * 
- *             return get_qual_range(src, start, end)             # <<<<<<<<<<<<<<
- * 
- *     property qstart:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_4 = __pyx_f_5pysam_8csamfile_get_qual_range(__pyx_v_src, __pyx_v_start, __pyx_v_end); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2557
- * 
- *         In Python 3, this property is of type bytes."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint32_t start, end
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qqual.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2575
- *         This the index of the first base in :attr:`seq` that is not soft-clipped.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return query_start(self._delegate)
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_6qstart_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_6qstart_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_6qstart___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6qstart___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2575);
-
-  /* "pysam/csamfile.pyx":2576
- *         """
- *         def __get__(self):
- *             return query_start(self._delegate)             # <<<<<<<<<<<<<<
- * 
- *     property qend:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_query_start(__pyx_v_self->_delegate); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2575
- *         This the index of the first base in :attr:`seq` that is not soft-clipped.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return query_start(self._delegate)
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qstart.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2580
- *     property qend:
- *         """end index of the aligned query portion of the sequence (0-based, exclusive)"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return query_end(self._delegate)
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qend_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qend_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4qend___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qend___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2580);
-
-  /* "pysam/csamfile.pyx":2581
- *         """end index of the aligned query portion of the sequence (0-based, exclusive)"""
- *         def __get__(self):
- *             return query_end(self._delegate)             # <<<<<<<<<<<<<<
- * 
- *     property qlen:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_query_end(__pyx_v_self->_delegate); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyInt_From_int32_t(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2580
- *     property qend:
- *         """end index of the aligned query portion of the sequence (0-based, exclusive)"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return query_end(self._delegate)
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qend.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2587
- * 
- *         This is equal to :attr:`qend` - :attr:`qstart`"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qlen_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4qlen_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4qlen___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4qlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int32_t __pyx_t_2;
-  int32_t __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2587);
-
-  /* "pysam/csamfile.pyx":2589
- *         def __get__(self):
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             return query_end(src)-query_start(src)
- * 
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2590
- *             cdef bam1_t * src
- *             src = self._delegate
- *             return query_end(src)-query_start(src)             # <<<<<<<<<<<<<<
- * 
- *     property tags:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __pyx_f_5pysam_8csamfile_query_end(__pyx_v_src); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_3 = __pyx_f_5pysam_8csamfile_query_start(__pyx_v_src); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_4 = __Pyx_PyInt_From_int((__pyx_t_2 - __pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2587
- * 
- *         This is equal to :attr:`qend` - :attr:`qstart`"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.qlen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2606
- *         multiple times.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef char * ctag
- *             cdef bam1_t * src
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tags___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  uint8_t *__pyx_v_s;
-  char __pyx_v_auxtag[3];
-  char __pyx_v_auxtype;
-  uint8_t __pyx_v_byte_size;
-  int32_t __pyx_v_nvalues;
-  PyObject *__pyx_v_result = NULL;
-  PyObject *__pyx_v_value = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  Py_ssize_t __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *(*__pyx_t_9)(PyObject *);
-  uint8_t __pyx_t_10;
-  int32_t __pyx_t_11;
-  int __pyx_t_12;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2606);
-
-  /* "pysam/csamfile.pyx":2615
- *             cdef int32_t nvalues
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if src.l_data == 0:
- *                 return []
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2616
- * 
- *             src = self._delegate
- *             if src.l_data == 0:             # <<<<<<<<<<<<<<
- *                 return []
- *             s = pysam_bam_get_aux(src)
- */
-  __pyx_t_2 = ((__pyx_v_src->l_data == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":2617
- *             src = self._delegate
- *             if src.l_data == 0:
- *                 return []             # <<<<<<<<<<<<<<
- *             s = pysam_bam_get_aux(src)
- *             result = []
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2618
- *             if src.l_data == 0:
- *                 return []
- *             s = pysam_bam_get_aux(src)             # <<<<<<<<<<<<<<
- *             result = []
- *             auxtag[2] = 0
- */
-  __pyx_v_s = pysam_bam_get_aux(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2619
- *                 return []
- *             s = pysam_bam_get_aux(src)
- *             result = []             # <<<<<<<<<<<<<<
- *             auxtag[2] = 0
- *             while s < (src.data + src.l_data):
- */
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_v_result = ((PyObject*)__pyx_t_3);
-  __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":2620
- *             s = pysam_bam_get_aux(src)
- *             result = []
- *             auxtag[2] = 0             # <<<<<<<<<<<<<<
- *             while s < (src.data + src.l_data):
- *                 # get tag
- */
-  (__pyx_v_auxtag[2]) = 0;
-
-  /* "pysam/csamfile.pyx":2621
- *             result = []
- *             auxtag[2] = 0
- *             while s < (src.data + src.l_data):             # <<<<<<<<<<<<<<
- *                 # get tag
- *                 auxtag[0] = s[0]
- */
-  while (1) {
-    __pyx_t_2 = ((__pyx_v_s < (__pyx_v_src->data + __pyx_v_src->l_data)) != 0);
-    if (!__pyx_t_2) break;
-
-    /* "pysam/csamfile.pyx":2623
- *             while s < (src.data + src.l_data):
- *                 # get tag
- *                 auxtag[0] = s[0]             # <<<<<<<<<<<<<<
- *                 auxtag[1] = s[1]
- *                 s += 2
- */
-    (__pyx_v_auxtag[0]) = (__pyx_v_s[0]);
-
-    /* "pysam/csamfile.pyx":2624
- *                 # get tag
- *                 auxtag[0] = s[0]
- *                 auxtag[1] = s[1]             # <<<<<<<<<<<<<<
- *                 s += 2
- *                 auxtype = s[0]
- */
-    (__pyx_v_auxtag[1]) = (__pyx_v_s[1]);
-
-    /* "pysam/csamfile.pyx":2625
- *                 auxtag[0] = s[0]
- *                 auxtag[1] = s[1]
- *                 s += 2             # <<<<<<<<<<<<<<
- *                 auxtype = s[0]
- *                 if auxtype in ('c', 'C'):
- */
-    __pyx_v_s = (__pyx_v_s + 2);
-
-    /* "pysam/csamfile.pyx":2626
- *                 auxtag[1] = s[1]
- *                 s += 2
- *                 auxtype = s[0]             # <<<<<<<<<<<<<<
- *                 if auxtype in ('c', 'C'):
- *                     value = <int>bam_aux2i(s)
- */
-    __pyx_v_auxtype = (__pyx_v_s[0]);
-
-    /* "pysam/csamfile.pyx":2649
- *                     # +1 for NULL terminated string
- *                     s += len(value) + 1
- *                 elif auxtype == 'B':             # <<<<<<<<<<<<<<
- *                     s += 1
- *                     byte_size, nvalues, value = convertBinaryTagToList( s )
- */
-    switch (__pyx_v_auxtype) {
-
-      /* "pysam/csamfile.pyx":2627
- *                 s += 2
- *                 auxtype = s[0]
- *                 if auxtype in ('c', 'C'):             # <<<<<<<<<<<<<<
- *                     value = <int>bam_aux2i(s)
- *                     s += 1
- */
-      case 'c':
-      case 'C':
-
-      /* "pysam/csamfile.pyx":2628
- *                 auxtype = s[0]
- *                 if auxtype in ('c', 'C'):
- *                     value = <int>bam_aux2i(s)             # <<<<<<<<<<<<<<
- *                     s += 1
- *                 elif auxtype in ('s', 'S'):
- */
-      __pyx_t_3 = __Pyx_PyInt_From_int(((int)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
-      __pyx_t_3 = 0;
-
-      /* "pysam/csamfile.pyx":2629
- *                 if auxtype in ('c', 'C'):
- *                     value = <int>bam_aux2i(s)
- *                     s += 1             # <<<<<<<<<<<<<<
- *                 elif auxtype in ('s', 'S'):
- *                     value = <int>bam_aux2i(s)
- */
-      __pyx_v_s = (__pyx_v_s + 1);
-      break;
-
-      /* "pysam/csamfile.pyx":2630
- *                     value = <int>bam_aux2i(s)
- *                     s += 1
- *                 elif auxtype in ('s', 'S'):             # <<<<<<<<<<<<<<
- *                     value = <int>bam_aux2i(s)
- *                     s += 2
- */
-      case 's':
-      case 'S':
-
-      /* "pysam/csamfile.pyx":2631
- *                     s += 1
- *                 elif auxtype in ('s', 'S'):
- *                     value = <int>bam_aux2i(s)             # <<<<<<<<<<<<<<
- *                     s += 2
- *                 elif auxtype in ('i', 'I'):
- */
-      __pyx_t_3 = __Pyx_PyInt_From_int(((int)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
-      __pyx_t_3 = 0;
-
-      /* "pysam/csamfile.pyx":2632
- *                 elif auxtype in ('s', 'S'):
- *                     value = <int>bam_aux2i(s)
- *                     s += 2             # <<<<<<<<<<<<<<
- *                 elif auxtype in ('i', 'I'):
- *                     value = <int32_t>bam_aux2i(s)
- */
-      __pyx_v_s = (__pyx_v_s + 2);
-      break;
-
-      /* "pysam/csamfile.pyx":2633
- *                     value = <int>bam_aux2i(s)
- *                     s += 2
- *                 elif auxtype in ('i', 'I'):             # <<<<<<<<<<<<<<
- *                     value = <int32_t>bam_aux2i(s)
- *                     s += 4
- */
-      case 'i':
-      case 'I':
-
-      /* "pysam/csamfile.pyx":2634
- *                     s += 2
- *                 elif auxtype in ('i', 'I'):
- *                     value = <int32_t>bam_aux2i(s)             # <<<<<<<<<<<<<<
- *                     s += 4
- *                 elif auxtype == 'f':
- */
-      __pyx_t_3 = __Pyx_PyInt_From_int32_t(((int32_t)bam_aux2i(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
-      __pyx_t_3 = 0;
-
-      /* "pysam/csamfile.pyx":2635
- *                 elif auxtype in ('i', 'I'):
- *                     value = <int32_t>bam_aux2i(s)
- *                     s += 4             # <<<<<<<<<<<<<<
- *                 elif auxtype == 'f':
- *                     value = <float>bam_aux2f(s)
- */
-      __pyx_v_s = (__pyx_v_s + 4);
-      break;
-
-      /* "pysam/csamfile.pyx":2636
- *                     value = <int32_t>bam_aux2i(s)
- *                     s += 4
- *                 elif auxtype == 'f':             # <<<<<<<<<<<<<<
- *                     value = <float>bam_aux2f(s)
- *                     s += 4
- */
-      case 'f':
-
-      /* "pysam/csamfile.pyx":2637
- *                     s += 4
- *                 elif auxtype == 'f':
- *                     value = <float>bam_aux2f(s)             # <<<<<<<<<<<<<<
- *                     s += 4
- *                 elif auxtype == 'd':
- */
-      __pyx_t_3 = PyFloat_FromDouble(((float)bam_aux2f(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
-      __pyx_t_3 = 0;
-
-      /* "pysam/csamfile.pyx":2638
- *                 elif auxtype == 'f':
- *                     value = <float>bam_aux2f(s)
- *                     s += 4             # <<<<<<<<<<<<<<
- *                 elif auxtype == 'd':
- *                     value = <double>bam_aux2f(s)
- */
-      __pyx_v_s = (__pyx_v_s + 4);
-      break;
-
-      /* "pysam/csamfile.pyx":2639
- *                     value = <float>bam_aux2f(s)
- *                     s += 4
- *                 elif auxtype == 'd':             # <<<<<<<<<<<<<<
- *                     value = <double>bam_aux2f(s)
- *                     s += 8
- */
-      case 'd':
-
-      /* "pysam/csamfile.pyx":2640
- *                     s += 4
- *                 elif auxtype == 'd':
- *                     value = <double>bam_aux2f(s)             # <<<<<<<<<<<<<<
- *                     s += 8
- *                 elif auxtype == 'A':
- */
-      __pyx_t_3 = PyFloat_FromDouble(((double)bam_aux2f(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
-      __pyx_t_3 = 0;
-
-      /* "pysam/csamfile.pyx":2641
- *                 elif auxtype == 'd':
- *                     value = <double>bam_aux2f(s)
- *                     s += 8             # <<<<<<<<<<<<<<
- *                 elif auxtype == 'A':
- *                     value = "%c" % <char>bam_aux2A(s)
- */
-      __pyx_v_s = (__pyx_v_s + 8);
-      break;
-
-      /* "pysam/csamfile.pyx":2642
- *                     value = <double>bam_aux2f(s)
- *                     s += 8
- *                 elif auxtype == 'A':             # <<<<<<<<<<<<<<
- *                     value = "%c" % <char>bam_aux2A(s)
- *                     s += 1
- */
-      case 'A':
-
-      /* "pysam/csamfile.pyx":2643
- *                     s += 8
- *                 elif auxtype == 'A':
- *                     value = "%c" % <char>bam_aux2A(s)             # <<<<<<<<<<<<<<
- *                     s += 1
- *                 elif auxtype in ('Z', 'H'):
- */
-      __pyx_t_3 = __Pyx_PyInt_From_char(((char)bam_aux2A(__pyx_v_s))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2643; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_c_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2643; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_4);
-      __pyx_t_4 = 0;
-
-      /* "pysam/csamfile.pyx":2644
- *                 elif auxtype == 'A':
- *                     value = "%c" % <char>bam_aux2A(s)
- *                     s += 1             # <<<<<<<<<<<<<<
- *                 elif auxtype in ('Z', 'H'):
- *                     value = _charptr_to_str(<char*>bam_aux2Z(s))
- */
-      __pyx_v_s = (__pyx_v_s + 1);
-      break;
-
-      /* "pysam/csamfile.pyx":2645
- *                     value = "%c" % <char>bam_aux2A(s)
- *                     s += 1
- *                 elif auxtype in ('Z', 'H'):             # <<<<<<<<<<<<<<
- *                     value = _charptr_to_str(<char*>bam_aux2Z(s))
- *                     # +1 for NULL terminated string
- */
-      case 'Z':
-      case 'H':
-
-      /* "pysam/csamfile.pyx":2646
- *                     s += 1
- *                 elif auxtype in ('Z', 'H'):
- *                     value = _charptr_to_str(<char*>bam_aux2Z(s))             # <<<<<<<<<<<<<<
- *                     # +1 for NULL terminated string
- *                     s += len(value) + 1
- */
-      __pyx_t_4 = __pyx_f_5pysam_8csamfile__charptr_to_str(((char *)bam_aux2Z(__pyx_v_s))); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_4);
-      __pyx_t_4 = 0;
-
-      /* "pysam/csamfile.pyx":2648
- *                     value = _charptr_to_str(<char*>bam_aux2Z(s))
- *                     # +1 for NULL terminated string
- *                     s += len(value) + 1             # <<<<<<<<<<<<<<
- *                 elif auxtype == 'B':
- *                     s += 1
- */
-      __pyx_t_5 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_v_s = (__pyx_v_s + (__pyx_t_5 + 1));
-      break;
-
-      /* "pysam/csamfile.pyx":2649
- *                     # +1 for NULL terminated string
- *                     s += len(value) + 1
- *                 elif auxtype == 'B':             # <<<<<<<<<<<<<<
- *                     s += 1
- *                     byte_size, nvalues, value = convertBinaryTagToList( s )
- */
-      case 'B':
-
-      /* "pysam/csamfile.pyx":2650
- *                     s += len(value) + 1
- *                 elif auxtype == 'B':
- *                     s += 1             # <<<<<<<<<<<<<<
- *                     byte_size, nvalues, value = convertBinaryTagToList( s )
- *                     # 5 for 1 char and 1 int
- */
-      __pyx_v_s = (__pyx_v_s + 1);
-
-      /* "pysam/csamfile.pyx":2651
- *                 elif auxtype == 'B':
- *                     s += 1
- *                     byte_size, nvalues, value = convertBinaryTagToList( s )             # <<<<<<<<<<<<<<
- *                     # 5 for 1 char and 1 int
- *                     s += 5 + ( nvalues * byte_size) - 1
- */
-      __pyx_t_4 = __pyx_f_5pysam_8csamfile_convertBinaryTagToList(__pyx_v_s); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      if ((likely(PyTuple_CheckExact(__pyx_t_4))) || (PyList_CheckExact(__pyx_t_4))) {
-        PyObject* sequence = __pyx_t_4;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        Py_ssize_t size = Py_SIZE(sequence);
-        #else
-        Py_ssize_t size = PySequence_Size(sequence);
-        #endif
-        if (unlikely(size != 3)) {
-          if (size > 3) __Pyx_RaiseTooManyValuesError(3);
-          else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        #if CYTHON_COMPILING_IN_CPYTHON
-        if (likely(PyTuple_CheckExact(sequence))) {
-          __pyx_t_3 = PyTuple_GET_ITEM(sequence, 0); 
-          __pyx_t_6 = PyTuple_GET_ITEM(sequence, 1); 
-          __pyx_t_7 = PyTuple_GET_ITEM(sequence, 2); 
-        } else {
-          __pyx_t_3 = PyList_GET_ITEM(sequence, 0); 
-          __pyx_t_6 = PyList_GET_ITEM(sequence, 1); 
-          __pyx_t_7 = PyList_GET_ITEM(sequence, 2); 
-        }
-        __Pyx_INCREF(__pyx_t_3);
-        __Pyx_INCREF(__pyx_t_6);
-        __Pyx_INCREF(__pyx_t_7);
-        #else
-        __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_3);
-        __pyx_t_6 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_7 = PySequence_ITEM(sequence, 2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_7);
-        #endif
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      } else {
-        Py_ssize_t index = -1;
-        __pyx_t_8 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-        __pyx_t_9 = Py_TYPE(__pyx_t_8)->tp_iternext;
-        index = 0; __pyx_t_3 = __pyx_t_9(__pyx_t_8); if (unlikely(!__pyx_t_3)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_3);
-        index = 1; __pyx_t_6 = __pyx_t_9(__pyx_t_8); if (unlikely(!__pyx_t_6)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_6);
-        index = 2; __pyx_t_7 = __pyx_t_9(__pyx_t_8); if (unlikely(!__pyx_t_7)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_7);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_9(__pyx_t_8), 3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_9 = NULL;
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        goto __pyx_L7_unpacking_done;
-        __pyx_L6_unpacking_failed:;
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_9 = NULL;
-        if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_L7_unpacking_done:;
-      }
-      __pyx_t_10 = __Pyx_PyInt_As_uint8_t(__pyx_t_3); if (unlikely((__pyx_t_10 == (uint8_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_11 = __Pyx_PyInt_As_int32_t(__pyx_t_6); if (unlikely((__pyx_t_11 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_v_byte_size = __pyx_t_10;
-      __pyx_v_nvalues = __pyx_t_11;
-      __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_7);
-      __pyx_t_7 = 0;
-
-      /* "pysam/csamfile.pyx":2653
- *                     byte_size, nvalues, value = convertBinaryTagToList( s )
- *                     # 5 for 1 char and 1 int
- *                     s += 5 + ( nvalues * byte_size) - 1             # <<<<<<<<<<<<<<
- *                 else:
- *                     raise KeyError("unknown type '%s'" % auxtype)
- */
-      __pyx_v_s = (__pyx_v_s + ((5 + (__pyx_v_nvalues * __pyx_v_byte_size)) - 1));
-      break;
-      default:
-
-      /* "pysam/csamfile.pyx":2655
- *                     s += 5 + ( nvalues * byte_size) - 1
- *                 else:
- *                     raise KeyError("unknown type '%s'" % auxtype)             # <<<<<<<<<<<<<<
- * 
- *                 s += 1
- */
-      __pyx_t_4 = __Pyx_PyInt_From_char(__pyx_v_auxtype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_unknown_type_s, __pyx_t_4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_7);
-      __Pyx_GIVEREF(__pyx_t_7);
-      __pyx_t_7 = 0;
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __Pyx_Raise(__pyx_t_7, 0, 0, 0);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      break;
-    }
-
-    /* "pysam/csamfile.pyx":2657
- *                     raise KeyError("unknown type '%s'" % auxtype)
- * 
- *                 s += 1             # <<<<<<<<<<<<<<
- * 
- *                 result.append((_charptr_to_str(auxtag), value))
- */
-    __pyx_v_s = (__pyx_v_s + 1);
-
-    /* "pysam/csamfile.pyx":2659
- *                 s += 1
- * 
- *                 result.append((_charptr_to_str(auxtag), value))             # <<<<<<<<<<<<<<
- * 
- *             return result
- */
-    __pyx_t_7 = __pyx_f_5pysam_8csamfile__charptr_to_str(__pyx_v_auxtag); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_7);
-    __Pyx_GIVEREF(__pyx_t_7);
-    __Pyx_INCREF(__pyx_v_value);
-    PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_v_value);
-    __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_7 = 0;
-    __pyx_t_12 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_4); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  }
-
-  /* "pysam/csamfile.pyx":2661
- *                 result.append((_charptr_to_str(auxtag), value))
- * 
- *             return result             # <<<<<<<<<<<<<<
- * 
- *         def __set__(self, tags):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_result);
-  __pyx_r = __pyx_v_result;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2606
- *         multiple times.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef char * ctag
- *             cdef bam1_t * src
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tags.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_result);
-  __Pyx_XDECREF(__pyx_v_value);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2663
- *             return result
- * 
- *         def __set__(self, tags):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint8_t * s
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tags); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tags) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_tags));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tags_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tags) {
-  bam1_t *__pyx_v_src;
-  uint8_t *__pyx_v_s;
-  char *__pyx_v_temp;
-  int __pyx_v_new_size;
-  int __pyx_v_old_size;
-  PyObject *__pyx_v_fmts = NULL;
-  PyObject *__pyx_v_args = NULL;
-  PyObject *__pyx_v_pytag = NULL;
-  PyObject *__pyx_v_value = NULL;
-  PyObject *__pyx_v_fmt = NULL;
-  PyObject *__pyx_v_buffer = NULL;
-  PyObject *__pyx_v_p = NULL;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_t_4;
-  Py_ssize_t __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_t_7;
-  PyObject *(*__pyx_t_8)(PyObject *);
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  PyObject *__pyx_t_11 = NULL;
-  PyObject *(*__pyx_t_12)(PyObject *);
-  int __pyx_t_13;
-  char *__pyx_t_14;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2663);
-
-  /* "pysam/csamfile.pyx":2667
- *             cdef uint8_t * s
- *             cdef char * temp
- *             cdef int new_size = 0             # <<<<<<<<<<<<<<
- *             cdef int old_size
- *             src = self._delegate
- */
-  __pyx_v_new_size = 0;
-
-  /* "pysam/csamfile.pyx":2669
- *             cdef int new_size = 0
- *             cdef int old_size
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             fmts, args = ["<"], []
- * 
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2670
- *             cdef int old_size
- *             src = self._delegate
- *             fmts, args = ["<"], []             # <<<<<<<<<<<<<<
- * 
- *             if tags is not None and len(tags) > 0:
- */
-  __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2670; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(__pyx_kp_s__76);
-  PyList_SET_ITEM(__pyx_t_2, 0, __pyx_kp_s__76);
-  __Pyx_GIVEREF(__pyx_kp_s__76);
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2670; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_v_fmts = ((PyObject*)__pyx_t_2);
-  __pyx_t_2 = 0;
-  __pyx_v_args = ((PyObject*)__pyx_t_3);
-  __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":2672
- *             fmts, args = ["<"], []
- * 
- *             if tags is not None and len(tags) > 0:             # <<<<<<<<<<<<<<
- *                 for pytag, value in tags:
- *                     convert_python_tag(pytag, value, fmts, args)
- */
-  __pyx_t_4 = (__pyx_v_tags != Py_None);
-  if ((__pyx_t_4 != 0)) {
-    __pyx_t_5 = PyObject_Length(__pyx_v_tags); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2672; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = ((__pyx_t_5 > 0) != 0);
-    __pyx_t_7 = __pyx_t_6;
-  } else {
-    __pyx_t_7 = (__pyx_t_4 != 0);
-  }
-  if (__pyx_t_7) {
-
-    /* "pysam/csamfile.pyx":2673
- * 
- *             if tags is not None and len(tags) > 0:
- *                 for pytag, value in tags:             # <<<<<<<<<<<<<<
- *                     convert_python_tag(pytag, value, fmts, args)
- *                 fmt = "".join(fmts)
- */
-    if (PyList_CheckExact(__pyx_v_tags) || PyTuple_CheckExact(__pyx_v_tags)) {
-      __pyx_t_3 = __pyx_v_tags; __Pyx_INCREF(__pyx_t_3); __pyx_t_5 = 0;
-      __pyx_t_8 = NULL;
-    } else {
-      __pyx_t_5 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_v_tags); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_8 = Py_TYPE(__pyx_t_3)->tp_iternext;
-    }
-    for (;;) {
-      if (!__pyx_t_8 && PyList_CheckExact(__pyx_t_3)) {
-        if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_3)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else if (!__pyx_t_8 && PyTuple_CheckExact(__pyx_t_3)) {
-        if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_3)) break;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        #endif
-      } else {
-        __pyx_t_2 = __pyx_t_8(__pyx_t_3);
-        if (unlikely(!__pyx_t_2)) {
-          PyObject* exc_type = PyErr_Occurred();
-          if (exc_type) {
-            if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          }
-          break;
-        }
-        __Pyx_GOTREF(__pyx_t_2);
-      }
-      if ((likely(PyTuple_CheckExact(__pyx_t_2))) || (PyList_CheckExact(__pyx_t_2))) {
-        PyObject* sequence = __pyx_t_2;
-        #if CYTHON_COMPILING_IN_CPYTHON
-        Py_ssize_t size = Py_SIZE(sequence);
-        #else
-        Py_ssize_t size = PySequence_Size(sequence);
-        #endif
-        if (unlikely(size != 2)) {
-          if (size > 2) __Pyx_RaiseTooManyValuesError(2);
-          else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        }
-        #if CYTHON_COMPILING_IN_CPYTHON
-        if (likely(PyTuple_CheckExact(sequence))) {
-          __pyx_t_9 = PyTuple_GET_ITEM(sequence, 0); 
-          __pyx_t_10 = PyTuple_GET_ITEM(sequence, 1); 
-        } else {
-          __pyx_t_9 = PyList_GET_ITEM(sequence, 0); 
-          __pyx_t_10 = PyList_GET_ITEM(sequence, 1); 
-        }
-        __Pyx_INCREF(__pyx_t_9);
-        __Pyx_INCREF(__pyx_t_10);
-        #else
-        __pyx_t_9 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_10 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_10);
-        #endif
-        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      } else {
-        Py_ssize_t index = -1;
-        __pyx_t_11 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_11);
-        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext;
-        index = 0; __pyx_t_9 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_9)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_9);
-        index = 1; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L6_unpacking_failed;
-        __Pyx_GOTREF(__pyx_t_10);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_12 = NULL;
-        __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-        goto __pyx_L7_unpacking_done;
-        __pyx_L6_unpacking_failed:;
-        __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-        __pyx_t_12 = NULL;
-        if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_L7_unpacking_done:;
-      }
-      __Pyx_XDECREF_SET(__pyx_v_pytag, __pyx_t_9);
-      __pyx_t_9 = 0;
-      __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_10);
-      __pyx_t_10 = 0;
-
-      /* "pysam/csamfile.pyx":2674
- *             if tags is not None and len(tags) > 0:
- *                 for pytag, value in tags:
- *                     convert_python_tag(pytag, value, fmts, args)             # <<<<<<<<<<<<<<
- *                 fmt = "".join(fmts)
- *                 new_size = struct.calcsize(fmt)
- */
-      __pyx_t_2 = __pyx_f_5pysam_8csamfile_convert_python_tag(__pyx_v_pytag, __pyx_v_value, __pyx_v_fmts, __pyx_v_args); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2674; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    }
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-
-    /* "pysam/csamfile.pyx":2675
- *                 for pytag, value in tags:
- *                     convert_python_tag(pytag, value, fmts, args)
- *                 fmt = "".join(fmts)             # <<<<<<<<<<<<<<
- *                 new_size = struct.calcsize(fmt)
- *                 buffer = ctypes.create_string_buffer(new_size)
- */
-    __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__9, __pyx_v_fmts); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_v_fmt = ((PyObject*)__pyx_t_3);
-    __pyx_t_3 = 0;
-
-    /* "pysam/csamfile.pyx":2676
- *                     convert_python_tag(pytag, value, fmts, args)
- *                 fmt = "".join(fmts)
- *                 new_size = struct.calcsize(fmt)             # <<<<<<<<<<<<<<
- *                 buffer = ctypes.create_string_buffer(new_size)
- *                 struct.pack_into(fmt,
- */
-    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_struct); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_calcsize); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_INCREF(__pyx_v_fmt);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_fmt);
-    __Pyx_GIVEREF(__pyx_v_fmt);
-    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_10); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __pyx_v_new_size = __pyx_t_13;
-
-    /* "pysam/csamfile.pyx":2677
- *                 fmt = "".join(fmts)
- *                 new_size = struct.calcsize(fmt)
- *                 buffer = ctypes.create_string_buffer(new_size)             # <<<<<<<<<<<<<<
- *                 struct.pack_into(fmt,
- *                                  buffer,
- */
-    __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_ctypes); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_create_string_buffer); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __pyx_t_10 = __Pyx_PyInt_From_int(__pyx_v_new_size); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_10);
-    __Pyx_GIVEREF(__pyx_t_10);
-    __pyx_t_10 = 0;
-    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2677; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_v_buffer = __pyx_t_10;
-    __pyx_t_10 = 0;
-
-    /* "pysam/csamfile.pyx":2678
- *                 new_size = struct.calcsize(fmt)
- *                 buffer = ctypes.create_string_buffer(new_size)
- *                 struct.pack_into(fmt,             # <<<<<<<<<<<<<<
- *                                  buffer,
- *                                  0,
- */
-    __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_struct); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_pack_into); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-
-    /* "pysam/csamfile.pyx":2679
- *                 buffer = ctypes.create_string_buffer(new_size)
- *                 struct.pack_into(fmt,
- *                                  buffer,             # <<<<<<<<<<<<<<
- *                                  0,
- *                                  *args)
- */
-    __pyx_t_10 = PyTuple_New(3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    __Pyx_INCREF(__pyx_v_fmt);
-    PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_v_fmt);
-    __Pyx_GIVEREF(__pyx_v_fmt);
-    __Pyx_INCREF(__pyx_v_buffer);
-    PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_v_buffer);
-    __Pyx_GIVEREF(__pyx_v_buffer);
-    __Pyx_INCREF(__pyx_int_0);
-    PyTuple_SET_ITEM(__pyx_t_10, 2, __pyx_int_0);
-    __Pyx_GIVEREF(__pyx_int_0);
-
-    /* "pysam/csamfile.pyx":2678
- *                 new_size = struct.calcsize(fmt)
- *                 buffer = ctypes.create_string_buffer(new_size)
- *                 struct.pack_into(fmt,             # <<<<<<<<<<<<<<
- *                                  buffer,
- *                                  0,
- */
-    __pyx_t_3 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_9 = PyNumber_Add(__pyx_t_10, __pyx_t_3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":2686
- *             # If total_size == 0, the aux field will be
- *             # empty
- *             old_size = pysam_bam_get_l_aux(src)             # <<<<<<<<<<<<<<
- *             pysam_bam_update(src,
- *                              old_size,
- */
-  __pyx_v_old_size = pysam_bam_get_l_aux(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":2687
- *             # empty
- *             old_size = pysam_bam_get_l_aux(src)
- *             pysam_bam_update(src,             # <<<<<<<<<<<<<<
- *                              old_size,
- *                              new_size,
- */
-  pysam_bam_update(__pyx_v_src, __pyx_v_old_size, __pyx_v_new_size, pysam_bam_get_aux(__pyx_v_src));
-
-  /* "pysam/csamfile.pyx":2693
- * 
- *             # copy data only if there is any
- *             if new_size > 0:             # <<<<<<<<<<<<<<
- * 
- *                 # get location of new data
- */
-  __pyx_t_7 = ((__pyx_v_new_size > 0) != 0);
-  if (__pyx_t_7) {
-
-    /* "pysam/csamfile.pyx":2696
- * 
- *                 # get location of new data
- *                 s = pysam_bam_get_aux(src)             # <<<<<<<<<<<<<<
- * 
- *                 # check if there is direct path from buffer.raw to tmp
- */
-    __pyx_v_s = pysam_bam_get_aux(__pyx_v_src);
-
-    /* "pysam/csamfile.pyx":2699
- * 
- *                 # check if there is direct path from buffer.raw to tmp
- *                 p = buffer.raw             # <<<<<<<<<<<<<<
- *                 # create handle to make sure buffer stays alive long
- *                 # enough for memcpy, see issue 129
- */
-    if (unlikely(!__pyx_v_buffer)) { __Pyx_RaiseUnboundLocalError("buffer"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2699; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_buffer, __pyx_n_s_raw); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_v_p = __pyx_t_3;
-    __pyx_t_3 = 0;
-
-    /* "pysam/csamfile.pyx":2702
- *                 # create handle to make sure buffer stays alive long
- *                 # enough for memcpy, see issue 129
- *                 temp = p             # <<<<<<<<<<<<<<
- *                 memcpy(s, temp, new_size)
- * 
- */
-    __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_p); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_temp = __pyx_t_14;
-
-    /* "pysam/csamfile.pyx":2703
- *                 # enough for memcpy, see issue 129
- *                 temp = p
- *                 memcpy(s, temp, new_size)             # <<<<<<<<<<<<<<
- * 
- *     cpdef setTag(self, tag, value,
- */
-    memcpy(__pyx_v_s, __pyx_v_temp, __pyx_v_new_size);
-    goto __pyx_L8;
-  }
-  __pyx_L8:;
-
-  /* "pysam/csamfile.pyx":2663
- *             return result
- * 
- *         def __set__(self, tags):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             cdef uint8_t * s
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_XDECREF(__pyx_t_11);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tags.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_fmts);
-  __Pyx_XDECREF(__pyx_v_args);
-  __Pyx_XDECREF(__pyx_v_pytag);
-  __Pyx_XDECREF(__pyx_v_value);
-  __Pyx_XDECREF(__pyx_v_fmt);
-  __Pyx_XDECREF(__pyx_v_buffer);
-  __Pyx_XDECREF(__pyx_v_p);
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2705
- *                 memcpy(s, temp, new_size)
- * 
- *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
- *                  value_type = None,
- *                  replace = True):
- */
-
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static PyObject *__pyx_f_5pysam_8csamfile_11AlignedRead_setTag(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag, PyObject *__pyx_v_value, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag *__pyx_optional_args) {
-
-  /* "pysam/csamfile.pyx":2706
- * 
- *     cpdef setTag(self, tag, value,
- *                  value_type = None,             # <<<<<<<<<<<<<<
- *                  replace = True):
- *         '''
- */
-  PyObject *__pyx_v_value_type = ((PyObject *)Py_None);
-
-  /* "pysam/csamfile.pyx":2707
- *     cpdef setTag(self, tag, value,
- *                  value_type = None,
- *                  replace = True):             # <<<<<<<<<<<<<<
- *         '''
- *         Set optional field of alignment *tag* to *value*.  *value_type* may be specified,
- */
-  PyObject *__pyx_v_replace = ((PyObject *)Py_True);
-  int __pyx_v_value_size;
-  uint8_t *__pyx_v_value_ptr;
-  uint8_t *__pyx_v_existing_ptr;
-  uint8_t __pyx_v_type_code;
-  float __pyx_v_float_value;
-  double __pyx_v_double_value;
-  int32_t __pyx_v_int_value;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  bam1_t *__pyx_t_4;
-  Py_ssize_t __pyx_t_5;
-  int __pyx_t_6;
-  uint8_t __pyx_t_7;
-  struct __pyx_opt_args_5pysam_8csamfile_get_type_code __pyx_t_8;
-  char *__pyx_t_9;
-  int32_t __pyx_t_10;
-  double __pyx_t_11;
-  float __pyx_t_12;
-  char const *__pyx_t_13;
-  char const *__pyx_t_14;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("setTag", 0);
-  __Pyx_TraceCall("setTag", __pyx_f[0], 2705);
-  if (__pyx_optional_args) {
-    if (__pyx_optional_args->__pyx_n > 0) {
-      __pyx_v_value_type = __pyx_optional_args->value_type;
-      if (__pyx_optional_args->__pyx_n > 1) {
-        __pyx_v_replace = __pyx_optional_args->replace;
-      }
-    }
-  }
-  __Pyx_INCREF(__pyx_v_tag);
-  __Pyx_INCREF(__pyx_v_value);
-
-  /* "pysam/csamfile.pyx":2705
- *                 memcpy(s, temp, new_size)
- * 
- *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
- *                  value_type = None,
- *                  replace = True):
- */
-  /* Check if called by wrapper */
-  if (unlikely(__pyx_skip_dispatch)) ;
-  /* Check if overridden in Python */
-  else if (unlikely(Py_TYPE(((PyObject *)__pyx_v_self))->tp_dictoffset != 0)) {
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_setTag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    if (!PyCFunction_Check(__pyx_t_1) || (PyCFunction_GET_FUNCTION(__pyx_t_1) != (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag)) {
-      __Pyx_XDECREF(__pyx_r);
-      __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_2);
-      __Pyx_INCREF(__pyx_v_tag);
-      PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_tag);
-      __Pyx_GIVEREF(__pyx_v_tag);
-      __Pyx_INCREF(__pyx_v_value);
-      PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_value);
-      __Pyx_GIVEREF(__pyx_v_value);
-      __Pyx_INCREF(__pyx_v_value_type);
-      PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_value_type);
-      __Pyx_GIVEREF(__pyx_v_value_type);
-      __Pyx_INCREF(__pyx_v_replace);
-      PyTuple_SET_ITEM(__pyx_t_2, 3, __pyx_v_replace);
-      __Pyx_GIVEREF(__pyx_v_replace);
-      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_r = __pyx_t_3;
-      __pyx_t_3 = 0;
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      goto __pyx_L0;
-    }
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  }
-
-  /* "pysam/csamfile.pyx":2723
- *         cdef double   double_value
- *         cdef int32_t  int_value
- *         cdef bam1_t * src = self._delegate             # <<<<<<<<<<<<<<
- *         cdef char * _value_type
- * 
- */
-  __pyx_t_4 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":2726
- *         cdef char * _value_type
- * 
- *         if len(tag) != 2:             # <<<<<<<<<<<<<<
- *             raise ValueError('Invalid tag: %s' % tag)
- * 
- */
-  __pyx_t_5 = PyObject_Length(__pyx_v_tag); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2726; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_6 = ((__pyx_t_5 != 2) != 0);
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":2727
- * 
- *         if len(tag) != 2:
- *             raise ValueError('Invalid tag: %s' % tag)             # <<<<<<<<<<<<<<
- * 
- *         type_code = get_type_code(value, value_type)
- */
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_Invalid_tag_s, __pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":2729
- *             raise ValueError('Invalid tag: %s' % tag)
- * 
- *         type_code = get_type_code(value, value_type)             # <<<<<<<<<<<<<<
- *         if type_code == 0:
- *             raise ValueError("can't guess type or invalid type code specified")
- */
-  __pyx_t_8.__pyx_n = 1;
-  __pyx_t_8.value_type = __pyx_v_value_type;
-  __pyx_t_7 = __pyx_f_5pysam_8csamfile_get_type_code(__pyx_v_value, &__pyx_t_8); 
-  __pyx_v_type_code = __pyx_t_7;
-
-  /* "pysam/csamfile.pyx":2730
- * 
- *         type_code = get_type_code(value, value_type)
- *         if type_code == 0:             # <<<<<<<<<<<<<<
- *             raise ValueError("can't guess type or invalid type code specified")
- * 
- */
-  __pyx_t_6 = ((__pyx_v_type_code == 0) != 0);
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":2731
- *         type_code = get_type_code(value, value_type)
- *         if type_code == 0:
- *             raise ValueError("can't guess type or invalid type code specified")             # <<<<<<<<<<<<<<
- * 
- *         # Not Endian-safe, but then again neither is samtools!
- */
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__77, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2731; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2731; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":2746
- *             value_ptr    = <uint8_t*>&double_value
- *             value_size   = sizeof(double)
- *         elif type_code == 'f':             # <<<<<<<<<<<<<<
- *             float_value  = value
- *             value_ptr    = <uint8_t*>&float_value
- */
-  switch (__pyx_v_type_code) {
-
-    /* "pysam/csamfile.pyx":2734
- * 
- *         # Not Endian-safe, but then again neither is samtools!
- *         if type_code == 'Z':             # <<<<<<<<<<<<<<
- *             value = _forceBytes( value )
- *             value_ptr    = <uint8_t*><char*>value
- */
-    case 'Z':
-
-    /* "pysam/csamfile.pyx":2735
- *         # Not Endian-safe, but then again neither is samtools!
- *         if type_code == 'Z':
- *             value = _forceBytes( value )             # <<<<<<<<<<<<<<
- *             value_ptr    = <uint8_t*><char*>value
- *             value_size   = len(value)+1
- */
-    __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_value); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF_SET(__pyx_v_value, __pyx_t_1);
-    __pyx_t_1 = 0;
-
-    /* "pysam/csamfile.pyx":2736
- *         if type_code == 'Z':
- *             value = _forceBytes( value )
- *             value_ptr    = <uint8_t*><char*>value             # <<<<<<<<<<<<<<
- *             value_size   = len(value)+1
- *         elif type_code == 'i':
- */
-    __pyx_t_9 = __Pyx_PyObject_AsString(__pyx_v_value); if (unlikely((!__pyx_t_9) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_value_ptr = ((uint8_t *)((char *)__pyx_t_9));
-
-    /* "pysam/csamfile.pyx":2737
- *             value = _forceBytes( value )
- *             value_ptr    = <uint8_t*><char*>value
- *             value_size   = len(value)+1             # <<<<<<<<<<<<<<
- *         elif type_code == 'i':
- *             int_value    = value
- */
-    __pyx_t_5 = PyObject_Length(__pyx_v_value); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_value_size = (__pyx_t_5 + 1);
-    break;
-
-    /* "pysam/csamfile.pyx":2738
- *             value_ptr    = <uint8_t*><char*>value
- *             value_size   = len(value)+1
- *         elif type_code == 'i':             # <<<<<<<<<<<<<<
- *             int_value    = value
- *             value_ptr    = <uint8_t*>&int_value
- */
-    case 'i':
-
-    /* "pysam/csamfile.pyx":2739
- *             value_size   = len(value)+1
- *         elif type_code == 'i':
- *             int_value    = value             # <<<<<<<<<<<<<<
- *             value_ptr    = <uint8_t*>&int_value
- *             value_size   = sizeof(int32_t)
- */
-    __pyx_t_10 = __Pyx_PyInt_As_int32_t(__pyx_v_value); if (unlikely((__pyx_t_10 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2739; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_int_value = __pyx_t_10;
-
-    /* "pysam/csamfile.pyx":2740
- *         elif type_code == 'i':
- *             int_value    = value
- *             value_ptr    = <uint8_t*>&int_value             # <<<<<<<<<<<<<<
- *             value_size   = sizeof(int32_t)
- *         elif type_code == 'd':
- */
-    __pyx_v_value_ptr = ((uint8_t *)(&__pyx_v_int_value));
-
-    /* "pysam/csamfile.pyx":2741
- *             int_value    = value
- *             value_ptr    = <uint8_t*>&int_value
- *             value_size   = sizeof(int32_t)             # <<<<<<<<<<<<<<
- *         elif type_code == 'd':
- *             double_value = value
- */
-    __pyx_v_value_size = (sizeof(int32_t));
-    break;
-
-    /* "pysam/csamfile.pyx":2742
- *             value_ptr    = <uint8_t*>&int_value
- *             value_size   = sizeof(int32_t)
- *         elif type_code == 'd':             # <<<<<<<<<<<<<<
- *             double_value = value
- *             value_ptr    = <uint8_t*>&double_value
- */
-    case 'd':
-
-    /* "pysam/csamfile.pyx":2743
- *             value_size   = sizeof(int32_t)
- *         elif type_code == 'd':
- *             double_value = value             # <<<<<<<<<<<<<<
- *             value_ptr    = <uint8_t*>&double_value
- *             value_size   = sizeof(double)
- */
-    __pyx_t_11 = __pyx_PyFloat_AsDouble(__pyx_v_value); if (unlikely((__pyx_t_11 == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_double_value = __pyx_t_11;
-
-    /* "pysam/csamfile.pyx":2744
- *         elif type_code == 'd':
- *             double_value = value
- *             value_ptr    = <uint8_t*>&double_value             # <<<<<<<<<<<<<<
- *             value_size   = sizeof(double)
- *         elif type_code == 'f':
- */
-    __pyx_v_value_ptr = ((uint8_t *)(&__pyx_v_double_value));
-
-    /* "pysam/csamfile.pyx":2745
- *             double_value = value
- *             value_ptr    = <uint8_t*>&double_value
- *             value_size   = sizeof(double)             # <<<<<<<<<<<<<<
- *         elif type_code == 'f':
- *             float_value  = value
- */
-    __pyx_v_value_size = (sizeof(double));
-    break;
-
-    /* "pysam/csamfile.pyx":2746
- *             value_ptr    = <uint8_t*>&double_value
- *             value_size   = sizeof(double)
- *         elif type_code == 'f':             # <<<<<<<<<<<<<<
- *             float_value  = value
- *             value_ptr    = <uint8_t*>&float_value
- */
-    case 'f':
-
-    /* "pysam/csamfile.pyx":2747
- *             value_size   = sizeof(double)
- *         elif type_code == 'f':
- *             float_value  = value             # <<<<<<<<<<<<<<
- *             value_ptr    = <uint8_t*>&float_value
- *             value_size   = sizeof(float)
- */
-    __pyx_t_12 = __pyx_PyFloat_AsFloat(__pyx_v_value); if (unlikely((__pyx_t_12 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_float_value = __pyx_t_12;
-
-    /* "pysam/csamfile.pyx":2748
- *         elif type_code == 'f':
- *             float_value  = value
- *             value_ptr    = <uint8_t*>&float_value             # <<<<<<<<<<<<<<
- *             value_size   = sizeof(float)
- *         else:
- */
-    __pyx_v_value_ptr = ((uint8_t *)(&__pyx_v_float_value));
-
-    /* "pysam/csamfile.pyx":2749
- *             float_value  = value
- *             value_ptr    = <uint8_t*>&float_value
- *             value_size   = sizeof(float)             # <<<<<<<<<<<<<<
- *         else:
- *             raise ValueError('Unsupported value_type in set_option')
- */
-    __pyx_v_value_size = (sizeof(float));
-    break;
-    default:
-
-    /* "pysam/csamfile.pyx":2751
- *             value_size   = sizeof(float)
- *         else:
- *             raise ValueError('Unsupported value_type in set_option')             # <<<<<<<<<<<<<<
- * 
- *         tag = _forceBytes( tag )
- */
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__78, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    break;
-  }
-
-  /* "pysam/csamfile.pyx":2753
- *             raise ValueError('Unsupported value_type in set_option')
- * 
- *         tag = _forceBytes( tag )             # <<<<<<<<<<<<<<
- *         if replace:
- *             existing_ptr = bam_aux_get(src, tag)
- */
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2753; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF_SET(__pyx_v_tag, __pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":2754
- * 
- *         tag = _forceBytes( tag )
- *         if replace:             # <<<<<<<<<<<<<<
- *             existing_ptr = bam_aux_get(src, tag)
- *             if existing_ptr:
- */
-  __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_v_replace); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2754; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":2755
- *         tag = _forceBytes( tag )
- *         if replace:
- *             existing_ptr = bam_aux_get(src, tag)             # <<<<<<<<<<<<<<
- *             if existing_ptr:
- *                 bam_aux_del(src, existing_ptr)
- */
-    __pyx_t_13 = __Pyx_PyObject_AsString(__pyx_v_tag); if (unlikely((!__pyx_t_13) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2755; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_existing_ptr = bam_aux_get(__pyx_v_src, __pyx_t_13);
-
-    /* "pysam/csamfile.pyx":2756
- *         if replace:
- *             existing_ptr = bam_aux_get(src, tag)
- *             if existing_ptr:             # <<<<<<<<<<<<<<
- *                 bam_aux_del(src, existing_ptr)
- * 
- */
-    __pyx_t_6 = (__pyx_v_existing_ptr != 0);
-    if (__pyx_t_6) {
-
-      /* "pysam/csamfile.pyx":2757
- *             existing_ptr = bam_aux_get(src, tag)
- *             if existing_ptr:
- *                 bam_aux_del(src, existing_ptr)             # <<<<<<<<<<<<<<
- * 
- *         bam_aux_append(src,
- */
-      bam_aux_del(__pyx_v_src, __pyx_v_existing_ptr);
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-    goto __pyx_L5;
-  }
-  __pyx_L5:;
-
-  /* "pysam/csamfile.pyx":2760
- * 
- *         bam_aux_append(src,
- *                        tag,             # <<<<<<<<<<<<<<
- *                        type_code,
- *                        value_size,
- */
-  __pyx_t_14 = __Pyx_PyObject_AsString(__pyx_v_tag); if (unlikely((!__pyx_t_14) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2760; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":2759
- *                 bam_aux_del(src, existing_ptr)
- * 
- *         bam_aux_append(src,             # <<<<<<<<<<<<<<
- *                        tag,
- *                        type_code,
- */
-  bam_aux_append(__pyx_v_src, __pyx_t_14, __pyx_v_type_code, __pyx_v_value_size, __pyx_v_value_ptr);
-
-  /* "pysam/csamfile.pyx":2705
- *                 memcpy(s, temp, new_size)
- * 
- *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
- *                  value_type = None,
- *                  replace = True):
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.setTag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_tag);
-  __Pyx_XDECREF(__pyx_v_value);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_12setTag[] = "AlignedRead.setTag(self, tag, value, value_type=None, replace=True)\n\n        Set optional field of alignment *tag* to *value*.  *value_type* may be specified,\n        but if not the type will be inferred based on the Python type of *value*\n\n        An existing value of the same tag will be overwritten unless\n        *replace* is set to False.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  PyObject *__pyx_v_tag = 0;
-  PyObject *__pyx_v_value = 0;
-  PyObject *__pyx_v_value_type = 0;
-  PyObject *__pyx_v_replace = 0;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("setTag (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_tag,&__pyx_n_s_value,&__pyx_n_s_value_type,&__pyx_n_s_replace,0};
-    PyObject* values[4] = {0,0,0,0};
-
-    /* "pysam/csamfile.pyx":2706
- * 
- *     cpdef setTag(self, tag, value,
- *                  value_type = None,             # <<<<<<<<<<<<<<
- *                  replace = True):
- *         '''
- */
-    values[2] = ((PyObject *)Py_None);
-
-    /* "pysam/csamfile.pyx":2707
- *     cpdef setTag(self, tag, value,
- *                  value_type = None,
- *                  replace = True):             # <<<<<<<<<<<<<<
- *         '''
- *         Set optional field of alignment *tag* to *value*.  *value_type* may be specified,
- */
-    values[3] = ((PyObject *)Py_True);
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_tag)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("setTag", 0, 2, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-        case  2:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value_type);
-          if (value) { values[2] = value; kw_args--; }
-        }
-        case  3:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_replace);
-          if (value) { values[3] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setTag") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
-        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_tag = values[0];
-    __pyx_v_value = values[1];
-    __pyx_v_value_type = values[2];
-    __pyx_v_replace = values[3];
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setTag", 0, 2, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.setTag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12setTag(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), __pyx_v_tag, __pyx_v_value, __pyx_v_value_type, __pyx_v_replace);
-
-  /* "pysam/csamfile.pyx":2705
- *                 memcpy(s, temp, new_size)
- * 
- *     cpdef setTag(self, tag, value,             # <<<<<<<<<<<<<<
- *                  value_type = None,
- *                  replace = True):
- */
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12setTag(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag, PyObject *__pyx_v_value, PyObject *__pyx_v_value_type, PyObject *__pyx_v_replace) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag __pyx_t_2;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("setTag", 0);
-  __Pyx_TraceCall("setTag", __pyx_f[0], 2705);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2.__pyx_n = 2;
-  __pyx_t_2.value_type = __pyx_v_value_type;
-  __pyx_t_2.replace = __pyx_v_replace;
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead *)__pyx_v_self->__pyx_vtab)->setTag(__pyx_v_self, __pyx_v_tag, __pyx_v_value, 1, &__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.setTag", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2767
- *     property flag:
- *         """properties flag"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4flag___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4flag___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2767);
-
-  /* "pysam/csamfile.pyx":2768
- *         """properties flag"""
- *         def __get__(self):
- *             return pysam_get_flag(self._delegate)             # <<<<<<<<<<<<<<
- *         def __set__(self, flag):
- *             pysam_set_flag(self._delegate, flag)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint16_t(pysam_get_flag(__pyx_v_self->_delegate)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2767
- *     property flag:
- *         """properties flag"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.flag.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2769
- *         def __get__(self):
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):             # <<<<<<<<<<<<<<
- *             pysam_set_flag(self._delegate, flag)
- * 
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_flag); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_flag) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4flag_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_flag));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4flag_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_flag) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint8_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2769);
-
-  /* "pysam/csamfile.pyx":2770
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):
- *             pysam_set_flag(self._delegate, flag)             # <<<<<<<<<<<<<<
- * 
- *     property rname:
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint8_t(__pyx_v_flag); if (unlikely((__pyx_t_1 == (uint8_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_set_flag(__pyx_v_self->_delegate, __pyx_t_1);
-
-  /* "pysam/csamfile.pyx":2769
- *         def __get__(self):
- *             return pysam_get_flag(self._delegate)
- *         def __set__(self, flag):             # <<<<<<<<<<<<<<
- *             pysam_set_flag(self._delegate, flag)
- * 
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.flag.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2788
- * 
- *         """
- *         def __get__(self): return self._delegate.core.tid             # <<<<<<<<<<<<<<
- *         def __set__(self, tid): self._delegate.core.tid = tid
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5rname___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rname___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2788);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rname.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2789
- *         """
- *         def __get__(self): return self._delegate.core.tid
- *         def __set__(self, tid): self._delegate.core.tid = tid             # <<<<<<<<<<<<<<
- * 
- *     property tid:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5rname_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_tid));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rname_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tid) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2789);
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_tid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2789; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.tid = __pyx_t_1;
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rname.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2802
- * 
- *         """
- *         def __get__(self): return self._delegate.core.tid             # <<<<<<<<<<<<<<
- *         def __set__(self, tid): self._delegate.core.tid = tid
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3tid___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3tid___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2802);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tid.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2803
- *         """
- *         def __get__(self): return self._delegate.core.tid
- *         def __set__(self, tid): self._delegate.core.tid = tid             # <<<<<<<<<<<<<<
- * 
- *     property pos:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_tid) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3tid_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_tid));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3tid_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tid) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2803);
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_tid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.tid = __pyx_t_1;
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tid.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2807
- *     property pos:
- *         """0-based leftmost coordinate"""
- *         def __get__(self): return self._delegate.core.pos             # <<<<<<<<<<<<<<
- *         def __set__(self, pos):
- *             ## setting the position requires updating the "bin" attribute
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3pos___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3pos___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2807);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2808
- *         """0-based leftmost coordinate"""
- *         def __get__(self): return self._delegate.core.pos
- *         def __set__(self, pos):             # <<<<<<<<<<<<<<
- *             ## setting the position requires updating the "bin" attribute
- *             cdef bam1_t * src
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_pos); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_pos) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3pos_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_pos));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3pos_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_pos) {
-  bam1_t *__pyx_v_src;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int32_t __pyx_t_2;
-  int __pyx_t_3;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2808);
-
-  /* "pysam/csamfile.pyx":2811
- *             ## setting the position requires updating the "bin" attribute
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             src.core.pos = pos
- *             if pysam_get_n_cigar(src):
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2812
- *             cdef bam1_t * src
- *             src = self._delegate
- *             src.core.pos = pos             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src):
- *                 pysam_set_bin(src,
- */
-  __pyx_t_2 = __Pyx_PyInt_As_int32_t(__pyx_v_pos); if (unlikely((__pyx_t_2 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_src->core.pos = __pyx_t_2;
-
-  /* "pysam/csamfile.pyx":2813
- *             src = self._delegate
- *             src.core.pos = pos
- *             if pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 pysam_set_bin(src,
- *                               hts_reg2bin(
- */
-  __pyx_t_3 = (pysam_get_n_cigar(__pyx_v_src) != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":2814
- *             src.core.pos = pos
- *             if pysam_get_n_cigar(src):
- *                 pysam_set_bin(src,             # <<<<<<<<<<<<<<
- *                               hts_reg2bin(
- *                                   src.core.pos,
- */
-    pysam_set_bin(__pyx_v_src, hts_reg2bin(__pyx_v_src->core.pos, bam_endpos(__pyx_v_src), 14, 5));
-    goto __pyx_L3;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":2821
- *                                   5))
- *             else:
- *                 pysam_set_bin(src,             # <<<<<<<<<<<<<<
- *                               hts_reg2bin(
- *                                   src.core.pos,
- */
-    pysam_set_bin(__pyx_v_src, hts_reg2bin(__pyx_v_src->core.pos, (__pyx_v_src->core.pos + 1), 14, 5));
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":2808
- *         """0-based leftmost coordinate"""
- *         def __get__(self): return self._delegate.core.pos
- *         def __set__(self, pos):             # <<<<<<<<<<<<<<
- *             ## setting the position requires updating the "bin" attribute
- *             cdef bam1_t * src
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.pos.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2830
- *     property bin:
- *         """properties bin"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_3bin___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2830);
-
-  /* "pysam/csamfile.pyx":2831
- *         """properties bin"""
- *         def __get__(self):
- *             return pysam_get_bin(self._delegate)             # <<<<<<<<<<<<<<
- *         def __set__(self, bin):
- *             pysam_set_bin(self._delegate, bin)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint16_t(pysam_get_bin(__pyx_v_self->_delegate)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2830
- *     property bin:
- *         """properties bin"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.bin.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2832
- *         def __get__(self):
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):             # <<<<<<<<<<<<<<
- *             pysam_set_bin(self._delegate, bin)
- * 
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_bin); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_bin) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_bin));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_3bin_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_bin) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2832);
-
-  /* "pysam/csamfile.pyx":2833
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):
- *             pysam_set_bin(self._delegate, bin)             # <<<<<<<<<<<<<<
- * 
- *     property rlen:
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_bin); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_set_bin(__pyx_v_self->_delegate, __pyx_t_1);
-
-  /* "pysam/csamfile.pyx":2832
- *         def __get__(self):
- *             return pysam_get_bin(self._delegate)
- *         def __set__(self, bin):             # <<<<<<<<<<<<<<
- *             pysam_set_bin(self._delegate, bin)
- * 
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.bin.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2842
- * 
- *         Returns 0 if not available."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.l_qseq
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4rlen_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4rlen_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4rlen___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4rlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2842);
-
-  /* "pysam/csamfile.pyx":2843
- *         Returns 0 if not available."""
- *         def __get__(self):
- *             return self._delegate.core.l_qseq             # <<<<<<<<<<<<<<
- * 
- *     property aend:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.l_qseq); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2842
- * 
- *         Returns 0 if not available."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.l_qseq
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rlen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2850
- *         aend points to one past the last aligned residue.
- *         Returns None if not available.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4aend_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4aend_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4aend___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4aend___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_t_7;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2850);
-
-  /* "pysam/csamfile.pyx":2852
- *         def __get__(self):
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2853
- *             cdef bam1_t * src
- *             src = self._delegate
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return None
- *             return bam_endpos(src)
- */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyInt_From_int(BAM_FUNMAP); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyNumber_And(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (!__pyx_t_5) {
-    __pyx_t_6 = (pysam_get_n_cigar(__pyx_v_src) == 0);
-    __pyx_t_7 = __pyx_t_6;
-  } else {
-    __pyx_t_7 = __pyx_t_5;
-  }
-  if (__pyx_t_7) {
-
-    /* "pysam/csamfile.pyx":2854
- *             src = self._delegate
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None             # <<<<<<<<<<<<<<
- *             return bam_endpos(src)
- * 
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2855
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None
- *             return bam_endpos(src)             # <<<<<<<<<<<<<<
- * 
- *     property alen:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_4 = __Pyx_PyInt_From_int32_t(bam_endpos(__pyx_v_src)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2850
- *         aend points to one past the last aligned residue.
- *         Returns None if not available.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.aend.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2861
- * 
- *         This is equal to `aend - pos`. Returns None if not available.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4alen_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4alen_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4alen___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4alen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_t_7;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2861);
-
-  /* "pysam/csamfile.pyx":2863
- *         def __get__(self):
- *             cdef bam1_t * src
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2864
- *             cdef bam1_t * src
- *             src = self._delegate
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return None
- *             return bam_endpos(src) - \
- */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyInt_From_int(BAM_FUNMAP); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyNumber_And(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (!__pyx_t_5) {
-    __pyx_t_6 = (pysam_get_n_cigar(__pyx_v_src) == 0);
-    __pyx_t_7 = __pyx_t_6;
-  } else {
-    __pyx_t_7 = __pyx_t_5;
-  }
-  if (__pyx_t_7) {
-
-    /* "pysam/csamfile.pyx":2865
- *             src = self._delegate
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None             # <<<<<<<<<<<<<<
- *             return bam_endpos(src) - \
- *                 self._delegate.core.pos
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(Py_None);
-    __pyx_r = Py_None;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":2866
- *             if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
- *                 return None
- *             return bam_endpos(src) - \             # <<<<<<<<<<<<<<
- *                 self._delegate.core.pos
- * 
- */
-  __Pyx_XDECREF(__pyx_r);
-
-  /* "pysam/csamfile.pyx":2867
- *                 return None
- *             return bam_endpos(src) - \
- *                 self._delegate.core.pos             # <<<<<<<<<<<<<<
- * 
- *     property mapq:
- */
-  __pyx_t_4 = __Pyx_PyInt_From_int((bam_endpos(__pyx_v_src) - __pyx_v_self->_delegate->core.pos)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_r = __pyx_t_4;
-  __pyx_t_4 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2861
- * 
- *         This is equal to `aend - pos`. Returns None if not available.'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef bam1_t * src
- *             src = self._delegate
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.alen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2871
- *     property mapq:
- *         """mapping quality"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2871);
-
-  /* "pysam/csamfile.pyx":2872
- *         """mapping quality"""
- *         def __get__(self):
- *             return pysam_get_qual(self._delegate)             # <<<<<<<<<<<<<<
- *         def __set__(self, qual):
- *             pysam_set_qual(self._delegate, qual)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint8_t(pysam_get_qual(__pyx_v_self->_delegate)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2871
- *     property mapq:
- *         """mapping quality"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mapq.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2873
- *         def __get__(self):
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):             # <<<<<<<<<<<<<<
- *             pysam_set_qual(self._delegate, qual)
- * 
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_qual) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_qual));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mapq_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_qual) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint8_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2873);
-
-  /* "pysam/csamfile.pyx":2874
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):
- *             pysam_set_qual(self._delegate, qual)             # <<<<<<<<<<<<<<
- * 
- *     property mrnm:
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint8_t(__pyx_v_qual); if (unlikely((__pyx_t_1 == (uint8_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_set_qual(__pyx_v_self->_delegate, __pyx_t_1);
-
-  /* "pysam/csamfile.pyx":2873
- *         def __get__(self):
- *             return pysam_get_qual(self._delegate)
- *         def __set__(self, qual):             # <<<<<<<<<<<<<<
- *             pysam_set_qual(self._delegate, qual)
- * 
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mapq.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2880
- *         deprecated, use RNEXT instead.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2880);
-
-  /* "pysam/csamfile.pyx":2881
- *         """
- *         def __get__(self):
- *             return self._delegate.core.mtid             # <<<<<<<<<<<<<<
- *         def __set__(self, mtid):
- *             self._delegate.core.mtid = mtid
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mtid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2881; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2880
- *         deprecated, use RNEXT instead.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mrnm.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2882
- *         def __get__(self):
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mtid = mtid
- * 
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_mtid));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mrnm_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mtid) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2882);
-
-  /* "pysam/csamfile.pyx":2883
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):
- *             self._delegate.core.mtid = mtid             # <<<<<<<<<<<<<<
- * 
- *     property rnext:
- */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mtid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2883; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.mtid = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2882
- *         def __get__(self):
- *             return self._delegate.core.mtid
- *         def __set__(self, mtid):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mtid = mtid
- * 
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mrnm.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2887
- *     property rnext:
- *         """the :term:`reference` id of the mate """
- *         def __get__(self): return self._delegate.core.mtid             # <<<<<<<<<<<<<<
- *         def __set__(self, mtid): self._delegate.core.mtid = mtid
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2887);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mtid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rnext.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2888
- *         """the :term:`reference` id of the mate """
- *         def __get__(self): return self._delegate.core.mtid
- *         def __set__(self, mtid): self._delegate.core.mtid = mtid             # <<<<<<<<<<<<<<
- * 
- *     property mpos:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mtid) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_mtid));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5rnext_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mtid) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2888);
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mtid); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.mtid = __pyx_t_1;
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.rnext.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2893
- *         """the position of the mate
- *         deprecated, use PNEXT instead."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2893);
-
-  /* "pysam/csamfile.pyx":2894
- *         deprecated, use PNEXT instead."""
- *         def __get__(self):
- *             return self._delegate.core.mpos             # <<<<<<<<<<<<<<
- *         def __set__(self, mpos):
- *             self._delegate.core.mpos = mpos
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2893
- *         """the position of the mate
- *         deprecated, use PNEXT instead."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mpos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2895
- *         def __get__(self):
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mpos = mpos
- * 
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_mpos));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4mpos_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mpos) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2895);
-
-  /* "pysam/csamfile.pyx":2896
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
- *             self._delegate.core.mpos = mpos             # <<<<<<<<<<<<<<
- * 
- *     property pnext:
- */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mpos); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2896; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.mpos = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2895
- *         def __get__(self):
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mpos = mpos
- * 
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mpos.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2900
- *     property pnext:
- *         """the position of the mate"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2900);
-
-  /* "pysam/csamfile.pyx":2901
- *         """the position of the mate"""
- *         def __get__(self):
- *             return self._delegate.core.mpos             # <<<<<<<<<<<<<<
- *         def __set__(self, mpos):
- *             self._delegate.core.mpos = mpos
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.mpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2901; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2900
- *     property pnext:
- *         """the position of the mate"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.pnext.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2902
- *         def __get__(self):
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mpos = mpos
- * 
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_mpos) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_mpos));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5pnext_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_mpos) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2902);
-
-  /* "pysam/csamfile.pyx":2903
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):
- *             self._delegate.core.mpos = mpos             # <<<<<<<<<<<<<<
- * 
- *     #######################################################################
- */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_mpos); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.mpos = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2902
- *         def __get__(self):
- *             return self._delegate.core.mpos
- *         def __set__(self, mpos):             # <<<<<<<<<<<<<<
- *             self._delegate.core.mpos = mpos
- * 
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.pnext.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2912
- *         """the insert size
- *         deprecated: use tlen instead"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5isize___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_5isize___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2912);
-
-  /* "pysam/csamfile.pyx":2913
- *         deprecated: use tlen instead"""
- *         def __get__(self):
- *             return self._delegate.core.isize             # <<<<<<<<<<<<<<
- *         def __set__(self, isize):
- *             self._delegate.core.isize = isize
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.isize); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2913; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2912
- *         """the insert size
- *         deprecated: use tlen instead"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.isize.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2914
- *         def __get__(self):
- *             return self._delegate.core.isize
- *         def __set__(self, isize):             # <<<<<<<<<<<<<<
- *             self._delegate.core.isize = isize
- *     property tlen:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_5isize_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_isize));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_5isize_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_isize) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2914);
-
-  /* "pysam/csamfile.pyx":2915
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
- *             self._delegate.core.isize = isize             # <<<<<<<<<<<<<<
- *     property tlen:
- *         """the template length"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_isize); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.isize = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2914
- *         def __get__(self):
- *             return self._delegate.core.isize
- *         def __set__(self, isize):             # <<<<<<<<<<<<<<
- *             self._delegate.core.isize = isize
- *     property tlen:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.isize.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2918
- *     property tlen:
- *         """the template length"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2918);
-
-  /* "pysam/csamfile.pyx":2919
- *         """the template length"""
- *         def __get__(self):
- *             return self._delegate.core.isize             # <<<<<<<<<<<<<<
- *         def __set__(self, isize):
- *             self._delegate.core.isize = isize
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_delegate->core.isize); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2919; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2918
- *     property tlen:
- *         """the template length"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tlen.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2920
- *         def __get__(self):
- *             return self._delegate.core.isize
- *         def __set__(self, isize):             # <<<<<<<<<<<<<<
- *             self._delegate.core.isize = isize
- *     property is_paired:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_isize) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_isize));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_4tlen_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_isize) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int32_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2920);
-
-  /* "pysam/csamfile.pyx":2921
- *             return self._delegate.core.isize
- *         def __set__(self, isize):
- *             self._delegate.core.isize = isize             # <<<<<<<<<<<<<<
- *     property is_paired:
- *         """true if read is paired in sequencing"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_int32_t(__pyx_v_isize); if (unlikely((__pyx_t_1 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_delegate->core.isize = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":2920
- *         def __get__(self):
- *             return self._delegate.core.isize
- *         def __set__(self, isize):             # <<<<<<<<<<<<<<
- *             self._delegate.core.isize = isize
- *     property is_paired:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.tlen.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2924
- *     property is_paired:
- *         """true if read is paired in sequencing"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2924);
-
-  /* "pysam/csamfile.pyx":2925
- *         """true if read is paired in sequencing"""
- *         def __get__(self):
- *             return (self.flag & BAM_FPAIRED) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FPAIRED); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2924
- *     property is_paired:
- *         """true if read is paired in sequencing"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_paired.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2926
- *         def __get__(self):
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
- *     property is_proper_pair:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_paired_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2926);
-
-  /* "pysam/csamfile.pyx":2927
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)             # <<<<<<<<<<<<<<
- *     property is_proper_pair:
- *         """true if read is mapped in a proper pair"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FPAIRED);
-
-  /* "pysam/csamfile.pyx":2926
- *         def __get__(self):
- *             return (self.flag & BAM_FPAIRED) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FPAIRED)
- *     property is_proper_pair:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_paired.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2930
- *     property is_proper_pair:
- *         """true if read is mapped in a proper pair"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2930);
-
-  /* "pysam/csamfile.pyx":2931
- *         """true if read is mapped in a proper pair"""
- *         def __get__(self):
- *             return (self.flag & BAM_FPROPER_PAIR) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2931; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FPROPER_PAIR); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2931; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2931; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2931; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2930
- *     property is_proper_pair:
- *         """true if read is mapped in a proper pair"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_proper_pair.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2932
- *         def __get__(self):
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
- *     property is_unmapped:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_14is_proper_pair_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2932);
-
-  /* "pysam/csamfile.pyx":2933
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)             # <<<<<<<<<<<<<<
- *     property is_unmapped:
- *         """true if read itself is unmapped"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2933; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FPROPER_PAIR);
-
-  /* "pysam/csamfile.pyx":2932
- *         def __get__(self):
- *             return (self.flag & BAM_FPROPER_PAIR) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
- *     property is_unmapped:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_proper_pair.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2936
- *     property is_unmapped:
- *         """true if read itself is unmapped"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2936);
-
-  /* "pysam/csamfile.pyx":2937
- *         """true if read itself is unmapped"""
- *         def __get__(self):
- *             return (self.flag & BAM_FUNMAP) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FUNMAP); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2936
- *     property is_unmapped:
- *         """true if read itself is unmapped"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_unmapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2938
- *         def __get__(self):
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)
- *     property mate_is_unmapped:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_11is_unmapped_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2938);
-
-  /* "pysam/csamfile.pyx":2939
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)             # <<<<<<<<<<<<<<
- *     property mate_is_unmapped:
- *         """true if the mate is unmapped"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FUNMAP);
-
-  /* "pysam/csamfile.pyx":2938
- *         def __get__(self):
- *             return (self.flag & BAM_FUNMAP) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FUNMAP)
- *     property mate_is_unmapped:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_unmapped.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2942
- *     property mate_is_unmapped:
- *         """true if the mate is unmapped"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2942);
-
-  /* "pysam/csamfile.pyx":2943
- *         """true if the mate is unmapped"""
- *         def __get__(self):
- *             return (self.flag & BAM_FMUNMAP) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2943; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FMUNMAP); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2943; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2943; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2943; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2942
- *     property mate_is_unmapped:
- *         """true if the mate is unmapped"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mate_is_unmapped.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2944
- *         def __get__(self):
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
- *     property is_reverse:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2944);
-
-  /* "pysam/csamfile.pyx":2945
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)             # <<<<<<<<<<<<<<
- *     property is_reverse:
- *         """true if read is mapped to reverse strand"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FMUNMAP);
-
-  /* "pysam/csamfile.pyx":2944
- *         def __get__(self):
- *             return (self.flag & BAM_FMUNMAP) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
- *     property is_reverse:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mate_is_unmapped.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2948
- *     property is_reverse:
- *         """true if read is mapped to reverse strand"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2948);
-
-  /* "pysam/csamfile.pyx":2949
- *         """true if read is mapped to reverse strand"""
- *         def __get__(self):
- *             return (self.flag & BAM_FREVERSE) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FREVERSE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2949; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2948
- *     property is_reverse:
- *         """true if read is mapped to reverse strand"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_reverse.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2950
- *         def __get__(self):
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)
- *     property mate_is_reverse:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_10is_reverse_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2950);
-
-  /* "pysam/csamfile.pyx":2951
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)             # <<<<<<<<<<<<<<
- *     property mate_is_reverse:
- *         """true is read is mapped to reverse strand"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2951; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FREVERSE);
-
-  /* "pysam/csamfile.pyx":2950
- *         def __get__(self):
- *             return (self.flag & BAM_FREVERSE) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREVERSE)
- *     property mate_is_reverse:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_reverse.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2954
- *     property mate_is_reverse:
- *         """true is read is mapped to reverse strand"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2954);
-
-  /* "pysam/csamfile.pyx":2955
- *         """true is read is mapped to reverse strand"""
- *         def __get__(self):
- *             return (self.flag & BAM_FMREVERSE) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2955; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FMREVERSE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2955; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2955; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2955; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2954
- *     property mate_is_reverse:
- *         """true is read is mapped to reverse strand"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mate_is_reverse.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2956
- *         def __get__(self):
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
- *     property is_read1:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2956);
-
-  /* "pysam/csamfile.pyx":2957
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)             # <<<<<<<<<<<<<<
- *     property is_read1:
- *         """true if this is read1"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FMREVERSE);
-
-  /* "pysam/csamfile.pyx":2956
- *         def __get__(self):
- *             return (self.flag & BAM_FMREVERSE) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
- *     property is_read1:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.mate_is_reverse.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2960
- *     property is_read1:
- *         """true if this is read1"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2960);
-
-  /* "pysam/csamfile.pyx":2961
- *         """true if this is read1"""
- *         def __get__(self):
- *             return (self.flag & BAM_FREAD1) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FREAD1)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FREAD1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2961; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2960
- *     property is_read1:
- *         """true if this is read1"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_read1.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2962
- *         def __get__(self):
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREAD1)
- *     property is_read2:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read1_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2962);
-
-  /* "pysam/csamfile.pyx":2963
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):
- *             pysam_update_flag(self._delegate, val, BAM_FREAD1)             # <<<<<<<<<<<<<<
- *     property is_read2:
- *         """true if this is read2"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2963; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FREAD1);
-
-  /* "pysam/csamfile.pyx":2962
- *         def __get__(self):
- *             return (self.flag & BAM_FREAD1) != 0
- *         def __set__(self,val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREAD1)
- *     property is_read2:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_read1.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2966
- *     property is_read2:
- *         """true if this is read2"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2966);
-
-  /* "pysam/csamfile.pyx":2967
- *         """true if this is read2"""
- *         def __get__(self):
- *             return (self.flag & BAM_FREAD2) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FREAD2)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FREAD2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2966
- *     property is_read2:
- *         """true if this is read2"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_read2.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2968
- *         def __get__(self):
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREAD2)
- *     property is_secondary:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_8is_read2_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2968);
-
-  /* "pysam/csamfile.pyx":2969
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FREAD2)             # <<<<<<<<<<<<<<
- *     property is_secondary:
- *         """true if not primary alignment"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2969; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FREAD2);
-
-  /* "pysam/csamfile.pyx":2968
- *         def __get__(self):
- *             return (self.flag & BAM_FREAD2) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FREAD2)
- *     property is_secondary:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_read2.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2972
- *     property is_secondary:
- *         """true if not primary alignment"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2972);
-
-  /* "pysam/csamfile.pyx":2973
- *         """true if not primary alignment"""
- *         def __get__(self):
- *             return (self.flag & BAM_FSECONDARY) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FSECONDARY); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2972
- *     property is_secondary:
- *         """true if not primary alignment"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_secondary.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2974
- *         def __get__(self):
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
- *     property is_qcfail:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_secondary_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2974);
-
-  /* "pysam/csamfile.pyx":2975
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)             # <<<<<<<<<<<<<<
- *     property is_qcfail:
- *         """true if QC failure"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2975; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FSECONDARY);
-
-  /* "pysam/csamfile.pyx":2974
- *         def __get__(self):
- *             return (self.flag & BAM_FSECONDARY) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
- *     property is_qcfail:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_secondary.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2978
- *     property is_qcfail:
- *         """true if QC failure"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2978);
-
-  /* "pysam/csamfile.pyx":2979
- *         """true if QC failure"""
- *         def __get__(self):
- *             return (self.flag & BAM_FQCFAIL) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FQCFAIL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2978
- *     property is_qcfail:
- *         """true if QC failure"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_qcfail.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2980
- *         def __get__(self):
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
- *     property is_duplicate:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_9is_qcfail_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2980);
-
-  /* "pysam/csamfile.pyx":2981
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)             # <<<<<<<<<<<<<<
- *     property is_duplicate:
- *         """true if optical or PCR duplicate"""
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FQCFAIL);
-
-  /* "pysam/csamfile.pyx":2980
- *         def __get__(self):
- *             return (self.flag & BAM_FQCFAIL) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
- *     property is_duplicate:
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_qcfail.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2984
- *     property is_duplicate:
- *         """true if optical or PCR duplicate"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2984);
-
-  /* "pysam/csamfile.pyx":2985
- *         """true if optical or PCR duplicate"""
- *         def __get__(self):
- *             return (self.flag & BAM_FDUP) != 0             # <<<<<<<<<<<<<<
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FDUP)
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_flag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(BAM_FDUP); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_And(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_0, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2984
- *     property is_duplicate:
- *         """true if optical or PCR duplicate"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_duplicate.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2986
- *         def __get__(self):
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FDUP)
- * 
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate_2__set__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_val));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11AlignedRead_12is_duplicate_2__set__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_val) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  uint16_t __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 2986);
-
-  /* "pysam/csamfile.pyx":2987
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):
- *             pysam_update_flag(self._delegate, val, BAM_FDUP)             # <<<<<<<<<<<<<<
- * 
- *     #######################################################################
- */
-  __pyx_t_1 = __Pyx_PyInt_As_uint16_t(__pyx_v_val); if (unlikely((__pyx_t_1 == (uint16_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  pysam_update_flag(__pyx_v_self->_delegate, __pyx_t_1, BAM_FDUP);
-
-  /* "pysam/csamfile.pyx":2986
- *         def __get__(self):
- *             return (self.flag & BAM_FDUP) != 0
- *         def __set__(self, val):             # <<<<<<<<<<<<<<
- *             pysam_update_flag(self._delegate, val, BAM_FDUP)
- * 
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.is_duplicate.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":2995
- *     property positions:
- *         """a list of reference positions that this read aligns to."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, i, pos
- *             cdef int op
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9positions_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_9positions_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_9positions___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_9positions___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_i;
-  uint32_t __pyx_v_pos;
-  int __pyx_v_op;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_result = NULL;
-  PyObject *__pyx_v_l = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int32_t __pyx_t_4;
-  uint16_t __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  uint32_t __pyx_t_7;
-  int __pyx_t_8;
-  int __pyx_t_9;
-  int __pyx_t_10;
-  int __pyx_t_11;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 2995);
-
-  /* "pysam/csamfile.pyx":3001
- *             cdef bam1_t * src
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":3002
- * 
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return []
- * 
- */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":3003
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []             # <<<<<<<<<<<<<<
- * 
- *             result = []
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3003; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3005
- *                 return []
- * 
- *             result = []             # <<<<<<<<<<<<<<
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)
- */
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_v_result = ((PyObject*)__pyx_t_3);
-  __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":3006
- * 
- *             result = []
- *             pos = src.core.pos             # <<<<<<<<<<<<<<
- *             cigar_p = pysam_bam_get_cigar(src)
- * 
- */
-  __pyx_t_4 = __pyx_v_src->core.pos;
-  __pyx_v_pos = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":3007
- *             result = []
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":3009
- *             cigar_p = pysam_bam_get_cigar(src)
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":3010
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:
- */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
-
-    /* "pysam/csamfile.pyx":3011
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:
- */
-    __pyx_t_3 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_3);
-    __pyx_t_3 = 0;
-
-    /* "pysam/csamfile.pyx":3012
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
- *                     for i from pos <= i < pos + l:
- *                         result.append( i )
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":3013
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
- *                         result.append( i )
- * 
- */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_6 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_6); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
-
-        /* "pysam/csamfile.pyx":3014
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:
- *                         result.append( i )             # <<<<<<<<<<<<<<
- * 
- *                 if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
- */
-        __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_i); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_8 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_6); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      }
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-
-    /* "pysam/csamfile.pyx":3016
- *                         result.append( i )
- * 
- *                 if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
- *                     pos += l
- * 
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_9 = ((__pyx_v_op == BAM_CDEL) != 0);
-      if (!__pyx_t_9) {
-        __pyx_t_10 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
-        __pyx_t_11 = __pyx_t_10;
-      } else {
-        __pyx_t_11 = __pyx_t_9;
-      }
-      __pyx_t_9 = __pyx_t_11;
-    } else {
-      __pyx_t_9 = __pyx_t_2;
-    }
-    if (__pyx_t_9) {
-
-      /* "pysam/csamfile.pyx":3017
- * 
- *                 if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     pos += l             # <<<<<<<<<<<<<<
- * 
- *             return result
- */
-      __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_t_6, __pyx_v_l); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_3); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_pos = __pyx_t_7;
-      goto __pyx_L9;
-    }
-    __pyx_L9:;
-  }
-
-  /* "pysam/csamfile.pyx":3019
- *                     pos += l
- * 
- *             return result             # <<<<<<<<<<<<<<
- * 
- *     property inferred_length:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_result);
-  __pyx_r = __pyx_v_result;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":2995
- *     property positions:
- *         """a list of reference positions that this read aligns to."""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, i, pos
- *             cdef int op
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.positions.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_result);
-  __Pyx_XDECREF(__pyx_v_l);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3026
- *         Returns 0 if CIGAR string is not present.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *            cdef uint32_t k, qpos
- *            cdef int op
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15inferred_length_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15inferred_length_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_15inferred_length___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_15inferred_length___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_qpos;
-  int __pyx_v_op;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  uint16_t __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_t_7;
-  int __pyx_t_8;
-  PyObject *__pyx_t_9 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3026);
-
-  /* "pysam/csamfile.pyx":3032
- *            cdef bam1_t * src
- * 
- *            src = self._delegate             # <<<<<<<<<<<<<<
- *            if pysam_get_n_cigar(src) == 0: return 0
- * 
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":3033
- * 
- *            src = self._delegate
- *            if pysam_get_n_cigar(src) == 0: return 0             # <<<<<<<<<<<<<<
- * 
- *            qpos = 0
- */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_int_0);
-    __pyx_r = __pyx_int_0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3035
- *            if pysam_get_n_cigar(src) == 0: return 0
- * 
- *            qpos = 0             # <<<<<<<<<<<<<<
- *            cigar_p = pysam_bam_get_cigar(src)
- * 
- */
-  __pyx_v_qpos = 0;
-
-  /* "pysam/csamfile.pyx":3036
- * 
- *            qpos = 0
- *            cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *            for k from 0 <= k < pysam_get_n_cigar(src):
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":3038
- *            cigar_p = pysam_bam_get_cigar(src)
- * 
- *            for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                op = cigar_p[k] & BAM_CIGAR_MASK
- * 
- */
-  __pyx_t_3 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_3; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":3039
- * 
- *            for k from 0 <= k < pysam_get_n_cigar(src):
- *                op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- * 
- *                if op == BAM_CMATCH or op == BAM_CINS or \
- */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
-
-    /* "pysam/csamfile.pyx":3041
- *                op = cigar_p[k] & BAM_CIGAR_MASK
- * 
- *                if op == BAM_CMATCH or op == BAM_CINS or \             # <<<<<<<<<<<<<<
- *                   op == BAM_CSOFT_CLIP or \
- *                   op == BAM_CEQUAL or op == BAM_CDIFF:
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_4 = ((__pyx_v_op == BAM_CINS) != 0);
-      if (!__pyx_t_4) {
-
-        /* "pysam/csamfile.pyx":3042
- * 
- *                if op == BAM_CMATCH or op == BAM_CINS or \
- *                   op == BAM_CSOFT_CLIP or \             # <<<<<<<<<<<<<<
- *                   op == BAM_CEQUAL or op == BAM_CDIFF:
- *                    qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-        __pyx_t_5 = ((__pyx_v_op == BAM_CSOFT_CLIP) != 0);
-        if (!__pyx_t_5) {
-
-          /* "pysam/csamfile.pyx":3043
- *                if op == BAM_CMATCH or op == BAM_CINS or \
- *                   op == BAM_CSOFT_CLIP or \
- *                   op == BAM_CEQUAL or op == BAM_CDIFF:             # <<<<<<<<<<<<<<
- *                    qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
- */
-          __pyx_t_6 = ((__pyx_v_op == BAM_CEQUAL) != 0);
-          if (!__pyx_t_6) {
-            __pyx_t_7 = ((__pyx_v_op == BAM_CDIFF) != 0);
-            __pyx_t_8 = __pyx_t_7;
-          } else {
-            __pyx_t_8 = __pyx_t_6;
-          }
-          __pyx_t_6 = __pyx_t_8;
-        } else {
-          __pyx_t_6 = __pyx_t_5;
-        }
-        __pyx_t_5 = __pyx_t_6;
-      } else {
-        __pyx_t_5 = __pyx_t_4;
-      }
-      __pyx_t_4 = __pyx_t_5;
-    } else {
-      __pyx_t_4 = __pyx_t_2;
-    }
-    if (__pyx_t_4) {
-
-      /* "pysam/csamfile.pyx":3044
- *                   op == BAM_CSOFT_CLIP or \
- *                   op == BAM_CEQUAL or op == BAM_CDIFF:
- *                    qpos += cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- * 
- *            return qpos
- */
-      __pyx_v_qpos = (__pyx_v_qpos + ((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT));
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-  }
-
-  /* "pysam/csamfile.pyx":3046
- *                    qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
- *            return qpos             # <<<<<<<<<<<<<<
- * 
- *     property aligned_pairs:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_9 = __Pyx_PyInt_From_uint32_t(__pyx_v_qpos); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3046; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_r = __pyx_t_9;
-  __pyx_t_9 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3026
- *         Returns 0 if CIGAR string is not present.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *            cdef uint32_t k, qpos
- *            cdef int op
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.inferred_length.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3053
- *         Unaligned position are marked by None.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, i, pos, qpos
- *             cdef int op
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13aligned_pairs_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_13aligned_pairs_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_13aligned_pairs___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_13aligned_pairs___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_i;
-  uint32_t __pyx_v_pos;
-  uint32_t __pyx_v_qpos;
-  int __pyx_v_op;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_result = NULL;
-  PyObject *__pyx_v_l = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int32_t __pyx_t_4;
-  uint16_t __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  uint32_t __pyx_t_7;
-  PyObject *__pyx_t_8 = NULL;
-  int __pyx_t_9;
-  int __pyx_t_10;
-  int __pyx_t_11;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3053);
-
-  /* "pysam/csamfile.pyx":3059
- *             cdef bam1_t * src
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":3060
- * 
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return []
- * 
- */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":3061
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []             # <<<<<<<<<<<<<<
- * 
- *             result = []
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3061; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3063
- *                 return []
- * 
- *             result = []             # <<<<<<<<<<<<<<
- *             pos = src.core.pos
- *             qpos = 0
- */
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3063; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_v_result = ((PyObject*)__pyx_t_3);
-  __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":3064
- * 
- *             result = []
- *             pos = src.core.pos             # <<<<<<<<<<<<<<
- *             qpos = 0
- *             cigar_p = pysam_bam_get_cigar(src)
- */
-  __pyx_t_4 = __pyx_v_src->core.pos;
-  __pyx_v_pos = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":3065
- *             result = []
- *             pos = src.core.pos
- *             qpos = 0             # <<<<<<<<<<<<<<
- *             cigar_p = pysam_bam_get_cigar(src)
- * 
- */
-  __pyx_v_qpos = 0;
-
-  /* "pysam/csamfile.pyx":3066
- *             pos = src.core.pos
- *             qpos = 0
- *             cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":3068
- *             cigar_p = pysam_bam_get_cigar(src)
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":3069
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
- */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
-
-    /* "pysam/csamfile.pyx":3070
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- * 
- *                 if op == BAM_CMATCH:
- */
-    __pyx_t_3 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_3);
-    __pyx_t_3 = 0;
-
-    /* "pysam/csamfile.pyx":3072
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
- *                 if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, i) )
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":3073
- * 
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
- *                         result.append( (qpos, i) )
- *                         qpos += 1
- */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3073; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_6 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3073; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_6); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3073; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
-
-        /* "pysam/csamfile.pyx":3074
- *                 if op == BAM_CMATCH:
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, i) )             # <<<<<<<<<<<<<<
- *                         qpos += 1
- *                     pos += l
- */
-        __pyx_t_6 = __Pyx_PyInt_From_uint32_t(__pyx_v_qpos); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3074; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_i); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3074; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_3);
-        __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3074; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_6);
-        __Pyx_GIVEREF(__pyx_t_6);
-        PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_3);
-        __Pyx_GIVEREF(__pyx_t_3);
-        __pyx_t_6 = 0;
-        __pyx_t_3 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_8); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3074; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-
-        /* "pysam/csamfile.pyx":3075
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, i) )
- *                         qpos += 1             # <<<<<<<<<<<<<<
- *                     pos += l
- * 
- */
-        __pyx_v_qpos = (__pyx_v_qpos + 1);
-      }
-
-      /* "pysam/csamfile.pyx":3076
- *                         result.append( (qpos, i) )
- *                         qpos += 1
- *                     pos += l             # <<<<<<<<<<<<<<
- * 
- *                 elif op == BAM_CINS:
- */
-      __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3076; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_t_8, __pyx_v_l); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3076; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_3); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3076; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_v_pos = __pyx_t_7;
-      goto __pyx_L6;
-    }
-
-    /* "pysam/csamfile.pyx":3078
- *                     pos += l
- * 
- *                 elif op == BAM_CINS:             # <<<<<<<<<<<<<<
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, None) )
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CINS) != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":3079
- * 
- *                 elif op == BAM_CINS:
- *                     for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
- *                         result.append( (qpos, None) )
- *                         qpos += 1
- */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_8 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
-
-        /* "pysam/csamfile.pyx":3080
- *                 elif op == BAM_CINS:
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, None) )             # <<<<<<<<<<<<<<
- *                         qpos += 1
- * 
- */
-        __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_v_qpos); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_3);
-        PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_8);
-        __Pyx_GIVEREF(__pyx_t_8);
-        __Pyx_INCREF(Py_None);
-        PyTuple_SET_ITEM(__pyx_t_3, 1, Py_None);
-        __Pyx_GIVEREF(Py_None);
-        __pyx_t_8 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_3); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-
-        /* "pysam/csamfile.pyx":3081
- *                     for i from pos <= i < pos + l:
- *                         result.append( (qpos, None) )
- *                         qpos += 1             # <<<<<<<<<<<<<<
- * 
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- */
-        __pyx_v_qpos = (__pyx_v_qpos + 1);
-      }
-      goto __pyx_L6;
-    }
-
-    /* "pysam/csamfile.pyx":3083
- *                         qpos += 1
- * 
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
- *                     for i from pos <= i < pos + l:
- *                         result.append( (None, i) )
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CDEL) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_10 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
-      __pyx_t_11 = __pyx_t_10;
-    } else {
-      __pyx_t_11 = __pyx_t_2;
-    }
-    if (__pyx_t_11) {
-
-      /* "pysam/csamfile.pyx":3084
- * 
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     for i from pos <= i < pos + l:             # <<<<<<<<<<<<<<
- *                         result.append( (None, i) )
- *                     pos += l
- */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3084; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_8 = PyNumber_Add(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3084; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3084; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      for (__pyx_v_i = __pyx_v_pos; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
-
-        /* "pysam/csamfile.pyx":3085
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     for i from pos <= i < pos + l:
- *                         result.append( (None, i) )             # <<<<<<<<<<<<<<
- *                     pos += l
- * 
- */
-        __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_v_i); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_3);
-        __Pyx_INCREF(Py_None);
-        PyTuple_SET_ITEM(__pyx_t_3, 0, Py_None);
-        __Pyx_GIVEREF(Py_None);
-        PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_8);
-        __Pyx_GIVEREF(__pyx_t_8);
-        __pyx_t_8 = 0;
-        __pyx_t_9 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_3); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3085; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      }
-
-      /* "pysam/csamfile.pyx":3086
- *                     for i from pos <= i < pos + l:
- *                         result.append( (None, i) )
- *                     pos += l             # <<<<<<<<<<<<<<
- * 
- *             return result
- */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_8 = PyNumber_InPlaceAdd(__pyx_t_3, __pyx_v_l); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_7 = __Pyx_PyInt_As_uint32_t(__pyx_t_8); if (unlikely((__pyx_t_7 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_v_pos = __pyx_t_7;
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-  }
-
-  /* "pysam/csamfile.pyx":3088
- *                     pos += l
- * 
- *             return result             # <<<<<<<<<<<<<<
- * 
- *     property blocks:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_result);
-  __pyx_r = __pyx_v_result;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3053
- *         Unaligned position are marked by None.
- *         """
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, i, pos, qpos
- *             cdef int op
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.aligned_pairs.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_result);
-  __Pyx_XDECREF(__pyx_v_l);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3103
- *         """
- * 
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, pos, l
- *             cdef int op
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_6blocks_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_6blocks_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_6blocks___get__(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_6blocks___get__(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_pos;
-  uint32_t __pyx_v_l;
-  int __pyx_v_op;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_result = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int32_t __pyx_t_4;
-  uint16_t __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  int __pyx_t_8;
-  int __pyx_t_9;
-  int __pyx_t_10;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3103);
-
-  /* "pysam/csamfile.pyx":3109
- *             cdef bam1_t * src
- * 
- *             src = self._delegate             # <<<<<<<<<<<<<<
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":3110
- * 
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *                 return []
- * 
- */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":3111
- *             src = self._delegate
- *             if pysam_get_n_cigar(src) == 0:
- *                 return []             # <<<<<<<<<<<<<<
- * 
- *             result = []
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3111; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_r = __pyx_t_3;
-    __pyx_t_3 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3113
- *                 return []
- * 
- *             result = []             # <<<<<<<<<<<<<<
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)
- */
-  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3113; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_v_result = ((PyObject*)__pyx_t_3);
-  __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":3114
- * 
- *             result = []
- *             pos = src.core.pos             # <<<<<<<<<<<<<<
- *             cigar_p = pysam_bam_get_cigar(src)
- *             l = 0
- */
-  __pyx_t_4 = __pyx_v_src->core.pos;
-  __pyx_v_pos = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":3115
- *             result = []
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- *             l = 0
- * 
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":3116
- *             pos = src.core.pos
- *             cigar_p = pysam_bam_get_cigar(src)
- *             l = 0             # <<<<<<<<<<<<<<
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- */
-  __pyx_v_l = 0;
-
-  /* "pysam/csamfile.pyx":3118
- *             l = 0
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-  __pyx_t_5 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_5; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":3119
- * 
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:
- */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
-
-    /* "pysam/csamfile.pyx":3120
- *             for k from 0 <= k < pysam_get_n_cigar(src):
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- *                 if op == BAM_CMATCH:
- *                     result.append((pos, pos + l))
- */
-    __pyx_v_l = ((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT);
-
-    /* "pysam/csamfile.pyx":3121
- *                 op = cigar_p[k] & BAM_CIGAR_MASK
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
- *                     result.append((pos, pos + l))
- *                     pos += l
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":3122
- *                 l = cigar_p[k] >> BAM_CIGAR_SHIFT
- *                 if op == BAM_CMATCH:
- *                     result.append((pos, pos + l))             # <<<<<<<<<<<<<<
- *                     pos += l
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- */
-      __pyx_t_3 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_6 = __Pyx_PyInt_From_uint32_t((__pyx_v_pos + __pyx_v_l)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_3);
-      __Pyx_GIVEREF(__pyx_t_3);
-      PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_6);
-      __Pyx_GIVEREF(__pyx_t_6);
-      __pyx_t_3 = 0;
-      __pyx_t_6 = 0;
-      __pyx_t_8 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_7); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-
-      /* "pysam/csamfile.pyx":3123
- *                 if op == BAM_CMATCH:
- *                     result.append((pos, pos + l))
- *                     pos += l             # <<<<<<<<<<<<<<
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     pos += l
- */
-      __pyx_v_pos = (__pyx_v_pos + __pyx_v_l);
-      goto __pyx_L6;
-    }
-
-    /* "pysam/csamfile.pyx":3124
- *                     result.append((pos, pos + l))
- *                     pos += l
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
- *                     pos += l
- * 
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CDEL) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_9 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
-      __pyx_t_10 = __pyx_t_9;
-    } else {
-      __pyx_t_10 = __pyx_t_2;
-    }
-    if (__pyx_t_10) {
-
-      /* "pysam/csamfile.pyx":3125
- *                     pos += l
- *                 elif op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                     pos += l             # <<<<<<<<<<<<<<
- * 
- *             return result
- */
-      __pyx_v_pos = (__pyx_v_pos + __pyx_v_l);
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-  }
-
-  /* "pysam/csamfile.pyx":3127
- *                     pos += l
- * 
- *             return result             # <<<<<<<<<<<<<<
- * 
- *     #######################################################################
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_result);
-  __pyx_r = __pyx_v_result;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3103
- *         """
- * 
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef uint32_t k, pos, l
- *             cdef int op
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.blocks.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_result);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3133
- *     ##
- *     #######################################################################
- *     def overlap( self, uint32_t start, uint32_t end ):             # <<<<<<<<<<<<<<
- *         """return number of aligned bases of read overlapping the interval *start* and *end*
- *         on the reference sequence.
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15overlap(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_14overlap[] = "AlignedRead.overlap(self, uint32_t start, uint32_t end)\nreturn number of aligned bases of read overlapping the interval *start* and *end*\n        on the reference sequence.\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_15overlap(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  uint32_t __pyx_v_start;
-  uint32_t __pyx_v_end;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("overlap (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_start,&__pyx_n_s_end,0};
-    PyObject* values[2] = {0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_start)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("overlap", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "overlap") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
-      goto __pyx_L5_argtuple_error;
-    } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-    }
-    __pyx_v_start = __Pyx_PyInt_As_uint32_t(values[0]); if (unlikely((__pyx_v_start == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    __pyx_v_end = __Pyx_PyInt_As_uint32_t(values[1]); if (unlikely((__pyx_v_end == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("overlap", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3133; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.overlap", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return NULL;
-  __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_14overlap(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), __pyx_v_start, __pyx_v_end);
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_14overlap(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, uint32_t __pyx_v_start, uint32_t __pyx_v_end) {
-  uint32_t __pyx_v_k;
-  uint32_t __pyx_v_pos;
-  uint32_t __pyx_v_overlap;
-  int __pyx_v_op;
-  int __pyx_v_o;
-  uint32_t *__pyx_v_cigar_p;
-  bam1_t *__pyx_v_src;
-  PyObject *__pyx_v_l = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  bam1_t *__pyx_t_1;
-  int __pyx_t_2;
-  int32_t __pyx_t_3;
-  uint16_t __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  uint32_t __pyx_t_6;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  uint32_t __pyx_t_10;
-  uint32_t __pyx_t_11;
-  int __pyx_t_12;
-  int __pyx_t_13;
-  int __pyx_t_14;
-  int __pyx_t_15;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("overlap", 0);
-  __Pyx_TraceCall("overlap", __pyx_f[0], 3133);
-
-  /* "pysam/csamfile.pyx":3142
- *         cdef bam1_t * src
- * 
- *         overlap = 0             # <<<<<<<<<<<<<<
- * 
- *         src = self._delegate
- */
-  __pyx_v_overlap = 0;
-
-  /* "pysam/csamfile.pyx":3144
- *         overlap = 0
- * 
- *         src = self._delegate             # <<<<<<<<<<<<<<
- *         if pysam_get_n_cigar(src) == 0:
- *             return 0
- */
-  __pyx_t_1 = __pyx_v_self->_delegate;
-  __pyx_v_src = __pyx_t_1;
-
-  /* "pysam/csamfile.pyx":3145
- * 
- *         src = self._delegate
- *         if pysam_get_n_cigar(src) == 0:             # <<<<<<<<<<<<<<
- *             return 0
- *         pos = src.core.pos
- */
-  __pyx_t_2 = ((pysam_get_n_cigar(__pyx_v_src) == 0) != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":3146
- *         src = self._delegate
- *         if pysam_get_n_cigar(src) == 0:
- *             return 0             # <<<<<<<<<<<<<<
- *         pos = src.core.pos
- *         o = 0
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_int_0);
-    __pyx_r = __pyx_int_0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3147
- *         if pysam_get_n_cigar(src) == 0:
- *             return 0
- *         pos = src.core.pos             # <<<<<<<<<<<<<<
- *         o = 0
- * 
- */
-  __pyx_t_3 = __pyx_v_src->core.pos;
-  __pyx_v_pos = __pyx_t_3;
-
-  /* "pysam/csamfile.pyx":3148
- *             return 0
- *         pos = src.core.pos
- *         o = 0             # <<<<<<<<<<<<<<
- * 
- *         cigar_p = pysam_bam_get_cigar(src)
- */
-  __pyx_v_o = 0;
-
-  /* "pysam/csamfile.pyx":3150
- *         o = 0
- * 
- *         cigar_p = pysam_bam_get_cigar(src)             # <<<<<<<<<<<<<<
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- */
-  __pyx_v_cigar_p = pysam_bam_get_cigar(__pyx_v_src);
-
-  /* "pysam/csamfile.pyx":3151
- * 
- *         cigar_p = pysam_bam_get_cigar(src)
- *         for k from 0 <= k < pysam_get_n_cigar(src):             # <<<<<<<<<<<<<<
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
- */
-  __pyx_t_4 = pysam_get_n_cigar(__pyx_v_src);
-  for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_4; __pyx_v_k++) {
-
-    /* "pysam/csamfile.pyx":3152
- *         cigar_p = pysam_bam_get_cigar(src)
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- *             op = cigar_p[k] & BAM_CIGAR_MASK             # <<<<<<<<<<<<<<
- *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
- */
-    __pyx_v_op = ((__pyx_v_cigar_p[__pyx_v_k]) & BAM_CIGAR_MASK);
-
-    /* "pysam/csamfile.pyx":3153
- *         for k from 0 <= k < pysam_get_n_cigar(src):
- *             op = cigar_p[k] & BAM_CIGAR_MASK
- *             l = cigar_p[k] >> BAM_CIGAR_SHIFT             # <<<<<<<<<<<<<<
- * 
- *             if op == BAM_CMATCH:
- */
-    __pyx_t_5 = __Pyx_PyInt_From_uint32_t(((__pyx_v_cigar_p[__pyx_v_k]) >> BAM_CIGAR_SHIFT)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_5);
-    __pyx_t_5 = 0;
-
-    /* "pysam/csamfile.pyx":3155
- *             l = cigar_p[k] >> BAM_CIGAR_SHIFT
- * 
- *             if op == BAM_CMATCH:             # <<<<<<<<<<<<<<
- *                 o = min( pos + l, end) - max( pos, start )
- *                 if o > 0: overlap += o
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (__pyx_t_2) {
-
-      /* "pysam/csamfile.pyx":3156
- * 
- *             if op == BAM_CMATCH:
- *                 o = min( pos + l, end) - max( pos, start )             # <<<<<<<<<<<<<<
- *                 if o > 0: overlap += o
- * 
- */
-      __pyx_t_6 = __pyx_v_end;
-      __pyx_t_5 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_7 = PyNumber_Add(__pyx_t_5, __pyx_v_l); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __pyx_t_8 = __Pyx_PyInt_From_uint32_t(__pyx_t_6); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_9 = PyObject_RichCompare(__pyx_t_8, __pyx_t_7, Py_LT); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      if (__pyx_t_2) {
-        __pyx_t_9 = __Pyx_PyInt_From_uint32_t(__pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_5 = __pyx_t_9;
-        __pyx_t_9 = 0;
-      } else {
-        __Pyx_INCREF(__pyx_t_7);
-        __pyx_t_5 = __pyx_t_7;
-      }
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_6 = __pyx_v_start;
-      __pyx_t_10 = __pyx_v_pos;
-      if (((__pyx_t_6 > __pyx_t_10) != 0)) {
-        __pyx_t_11 = __pyx_t_6;
-      } else {
-        __pyx_t_11 = __pyx_t_10;
-      }
-      __pyx_t_7 = __Pyx_PyInt_From_uint32_t(__pyx_t_11); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_9 = PyNumber_Subtract(__pyx_t_5, __pyx_t_7); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_12 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_12 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_v_o = __pyx_t_12;
-
-      /* "pysam/csamfile.pyx":3157
- *             if op == BAM_CMATCH:
- *                 o = min( pos + l, end) - max( pos, start )
- *                 if o > 0: overlap += o             # <<<<<<<<<<<<<<
- * 
- *             if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
- */
-      __pyx_t_2 = ((__pyx_v_o > 0) != 0);
-      if (__pyx_t_2) {
-        __pyx_v_overlap = (__pyx_v_overlap + __pyx_v_o);
-        goto __pyx_L7;
-      }
-      __pyx_L7:;
-      goto __pyx_L6;
-    }
-    __pyx_L6:;
-
-    /* "pysam/csamfile.pyx":3159
- *                 if o > 0: overlap += o
- * 
- *             if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:             # <<<<<<<<<<<<<<
- *                 pos += l
- * 
- */
-    __pyx_t_2 = ((__pyx_v_op == BAM_CMATCH) != 0);
-    if (!__pyx_t_2) {
-      __pyx_t_13 = ((__pyx_v_op == BAM_CDEL) != 0);
-      if (!__pyx_t_13) {
-        __pyx_t_14 = ((__pyx_v_op == BAM_CREF_SKIP) != 0);
-        __pyx_t_15 = __pyx_t_14;
-      } else {
-        __pyx_t_15 = __pyx_t_13;
-      }
-      __pyx_t_13 = __pyx_t_15;
-    } else {
-      __pyx_t_13 = __pyx_t_2;
-    }
-    if (__pyx_t_13) {
-
-      /* "pysam/csamfile.pyx":3160
- * 
- *             if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
- *                 pos += l             # <<<<<<<<<<<<<<
- * 
- *         return overlap
- */
-      __pyx_t_9 = __Pyx_PyInt_From_uint32_t(__pyx_v_pos); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_7 = PyNumber_InPlaceAdd(__pyx_t_9, __pyx_v_l); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_11 = __Pyx_PyInt_As_uint32_t(__pyx_t_7); if (unlikely((__pyx_t_11 == (uint32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_v_pos = __pyx_t_11;
-      goto __pyx_L8;
-    }
-    __pyx_L8:;
-  }
-
-  /* "pysam/csamfile.pyx":3162
- *                 pos += l
- * 
- *         return overlap             # <<<<<<<<<<<<<<
- * 
- *     def opt(self, tag):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_7 = __Pyx_PyInt_From_uint32_t(__pyx_v_overlap); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3162; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-  __pyx_r = __pyx_t_7;
-  __pyx_t_7 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3133
- *     ##
- *     #######################################################################
- *     def overlap( self, uint32_t start, uint32_t end ):             # <<<<<<<<<<<<<<
- *         """return number of aligned bases of read overlapping the interval *start* and *end*
- *         on the reference sequence.
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.overlap", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_l);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3164
- *         return overlap
- * 
- *     def opt(self, tag):             # <<<<<<<<<<<<<<
- *         """retrieves optional data given a two-letter *tag*"""
- *         #see bam_aux.c: bam_aux_get() and bam_aux2i() etc
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_17opt(PyObject *__pyx_v_self, PyObject *__pyx_v_tag); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_16opt[] = "AlignedRead.opt(self, tag)\nretrieves optional data given a two-letter *tag*";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_17opt(PyObject *__pyx_v_self, PyObject *__pyx_v_tag) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("opt (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_16opt(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self), ((PyObject *)__pyx_v_tag));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_16opt(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self, PyObject *__pyx_v_tag) {
-  uint8_t *__pyx_v_v;
-  CYTHON_UNUSED int __pyx_v_nvalues;
-  PyObject *__pyx_v_btag = NULL;
-  PyObject *__pyx_v_auxtype = NULL;
-  CYTHON_UNUSED PyObject *__pyx_v_bytesize = NULL;
-  PyObject *__pyx_v_values = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  char const *__pyx_t_2;
-  int __pyx_t_3;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_t_7;
-  int __pyx_t_8;
-  PyObject *__pyx_t_9 = NULL;
-  PyObject *__pyx_t_10 = NULL;
-  PyObject *__pyx_t_11 = NULL;
-  PyObject *(*__pyx_t_12)(PyObject *);
-  int __pyx_t_13;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("opt", 0);
-  __Pyx_TraceCall("opt", __pyx_f[0], 3164);
-
-  /* "pysam/csamfile.pyx":3169
- *         cdef uint8_t * v
- *         cdef int nvalues
- *         btag = _forceBytes(tag)             # <<<<<<<<<<<<<<
- *         v = bam_aux_get(self._delegate, btag)
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
- */
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile__forceBytes(__pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_btag = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3170
- *         cdef int nvalues
- *         btag = _forceBytes(tag)
- *         v = bam_aux_get(self._delegate, btag)             # <<<<<<<<<<<<<<
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
- *         auxtype = chr(v[0])
- */
-  __pyx_t_2 = __Pyx_PyObject_AsString(__pyx_v_btag); if (unlikely((!__pyx_t_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3170; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_v = bam_aux_get(__pyx_v_self->_delegate, __pyx_t_2);
-
-  /* "pysam/csamfile.pyx":3171
- *         btag = _forceBytes(tag)
- *         v = bam_aux_get(self._delegate, btag)
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )             # <<<<<<<<<<<<<<
- *         auxtype = chr(v[0])
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
- */
-  __pyx_t_3 = ((__pyx_v_v == NULL) != 0);
-  if (__pyx_t_3) {
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_tag_s_not_present, __pyx_v_tag); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3171; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":3172
- *         v = bam_aux_get(self._delegate, btag)
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
- *         auxtype = chr(v[0])             # <<<<<<<<<<<<<<
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
- *             return <int>bam_aux2i(v)
- */
-  __pyx_t_1 = __Pyx_PyInt_From_uint8_t((__pyx_v_v[0])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_chr, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_v_auxtype = __pyx_t_1;
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3173
- *         if v == NULL: raise KeyError( "tag '%s' not present" % tag )
- *         auxtype = chr(v[0])
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':             # <<<<<<<<<<<<<<
- *             return <int>bam_aux2i(v)
- *         elif auxtype == 'i' or auxtype == 'I':
- */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_c, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_3) {
-    __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_C, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (!__pyx_t_5) {
-      __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_s_2, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      if (!__pyx_t_6) {
-        __pyx_t_7 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_S, Py_EQ)); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3173; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_8 = __pyx_t_7;
-      } else {
-        __pyx_t_8 = __pyx_t_6;
-      }
-      __pyx_t_6 = __pyx_t_8;
-    } else {
-      __pyx_t_6 = __pyx_t_5;
-    }
-    __pyx_t_5 = __pyx_t_6;
-  } else {
-    __pyx_t_5 = __pyx_t_3;
-  }
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":3174
- *         auxtype = chr(v[0])
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
- *             return <int>bam_aux2i(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'i' or auxtype == 'I':
- *             return <int32_t>bam_aux2i(v)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyInt_From_int(((int)bam_aux2i(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_r = __pyx_t_1;
-    __pyx_t_1 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3175
- *         if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
- *             return <int>bam_aux2i(v)
- *         elif auxtype == 'i' or auxtype == 'I':             # <<<<<<<<<<<<<<
- *             return <int32_t>bam_aux2i(v)
- *         elif auxtype == 'f' or auxtype == 'F':
- */
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_i, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_5) {
-    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_I, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = __pyx_t_3;
-  } else {
-    __pyx_t_6 = __pyx_t_5;
-  }
-  if (__pyx_t_6) {
-
-    /* "pysam/csamfile.pyx":3176
- *             return <int>bam_aux2i(v)
- *         elif auxtype == 'i' or auxtype == 'I':
- *             return <int32_t>bam_aux2i(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'f' or auxtype == 'F':
- *             return <float>bam_aux2f(v)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyInt_From_int32_t(((int32_t)bam_aux2i(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_r = __pyx_t_1;
-    __pyx_t_1 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3177
- *         elif auxtype == 'i' or auxtype == 'I':
- *             return <int32_t>bam_aux2i(v)
- *         elif auxtype == 'f' or auxtype == 'F':             # <<<<<<<<<<<<<<
- *             return <float>bam_aux2f(v)
- *         elif auxtype == 'd' or auxtype == 'D':
- */
-  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_f, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_6) {
-    __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_F, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = __pyx_t_5;
-  } else {
-    __pyx_t_3 = __pyx_t_6;
-  }
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":3178
- *             return <int32_t>bam_aux2i(v)
- *         elif auxtype == 'f' or auxtype == 'F':
- *             return <float>bam_aux2f(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'd' or auxtype == 'D':
- *             return <double>bam_aux2f(v)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = PyFloat_FromDouble(((float)bam_aux2f(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_r = __pyx_t_1;
-    __pyx_t_1 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3179
- *         elif auxtype == 'f' or auxtype == 'F':
- *             return <float>bam_aux2f(v)
- *         elif auxtype == 'd' or auxtype == 'D':             # <<<<<<<<<<<<<<
- *             return <double>bam_aux2f(v)
- *         elif auxtype == 'A':
- */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_d, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (!__pyx_t_3) {
-    __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_D, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = __pyx_t_6;
-  } else {
-    __pyx_t_5 = __pyx_t_3;
-  }
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":3180
- *             return <float>bam_aux2f(v)
- *         elif auxtype == 'd' or auxtype == 'D':
- *             return <double>bam_aux2f(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'A':
- *             # there might a more efficient way
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = PyFloat_FromDouble(((double)bam_aux2f(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_r = __pyx_t_1;
-    __pyx_t_1 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3181
- *         elif auxtype == 'd' or auxtype == 'D':
- *             return <double>bam_aux2f(v)
- *         elif auxtype == 'A':             # <<<<<<<<<<<<<<
- *             # there might a more efficient way
- *             # to convert a char into a string
- */
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_A, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":3184
- *             # there might a more efficient way
- *             # to convert a char into a string
- *             return '%c' % <char>bam_aux2A(v)             # <<<<<<<<<<<<<<
- *         elif auxtype == 'Z':
- *             return _charptr_to_str(<char*>bam_aux2Z(v))
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyInt_From_char(((char)bam_aux2A(__pyx_v_v))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_c_2, __pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_r = __pyx_t_4;
-    __pyx_t_4 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3185
- *             # to convert a char into a string
- *             return '%c' % <char>bam_aux2A(v)
- *         elif auxtype == 'Z':             # <<<<<<<<<<<<<<
- *             return _charptr_to_str(<char*>bam_aux2Z(v))
- *         elif auxtype == 'B':
- */
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_Z, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":3186
- *             return '%c' % <char>bam_aux2A(v)
- *         elif auxtype == 'Z':
- *             return _charptr_to_str(<char*>bam_aux2Z(v))             # <<<<<<<<<<<<<<
- *         elif auxtype == 'B':
- *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
- */
-    __Pyx_XDECREF(__pyx_r);
-    __pyx_t_4 = __pyx_f_5pysam_8csamfile__charptr_to_str(((char *)bam_aux2Z(__pyx_v_v))); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_r = __pyx_t_4;
-    __pyx_t_4 = 0;
-    goto __pyx_L0;
-  }
-
-  /* "pysam/csamfile.pyx":3187
- *         elif auxtype == 'Z':
- *             return _charptr_to_str(<char*>bam_aux2Z(v))
- *         elif auxtype == 'B':             # <<<<<<<<<<<<<<
- *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
- *             return values
- */
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_v_auxtype, __pyx_n_s_B, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__pyx_t_5) {
-
-    /* "pysam/csamfile.pyx":3188
- *             return _charptr_to_str(<char*>bam_aux2Z(v))
- *         elif auxtype == 'B':
- *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )             # <<<<<<<<<<<<<<
- *             return values
- *         else:
- */
-    __pyx_t_4 = __pyx_f_5pysam_8csamfile_convertBinaryTagToList((__pyx_v_v + 1)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    if ((likely(PyTuple_CheckExact(__pyx_t_4))) || (PyList_CheckExact(__pyx_t_4))) {
-      PyObject* sequence = __pyx_t_4;
-      #if CYTHON_COMPILING_IN_CPYTHON
-      Py_ssize_t size = Py_SIZE(sequence);
-      #else
-      Py_ssize_t size = PySequence_Size(sequence);
-      #endif
-      if (unlikely(size != 3)) {
-        if (size > 3) __Pyx_RaiseTooManyValuesError(3);
-        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-      #if CYTHON_COMPILING_IN_CPYTHON
-      if (likely(PyTuple_CheckExact(sequence))) {
-        __pyx_t_1 = PyTuple_GET_ITEM(sequence, 0); 
-        __pyx_t_9 = PyTuple_GET_ITEM(sequence, 1); 
-        __pyx_t_10 = PyTuple_GET_ITEM(sequence, 2); 
-      } else {
-        __pyx_t_1 = PyList_GET_ITEM(sequence, 0); 
-        __pyx_t_9 = PyList_GET_ITEM(sequence, 1); 
-        __pyx_t_10 = PyList_GET_ITEM(sequence, 2); 
-      }
-      __Pyx_INCREF(__pyx_t_1);
-      __Pyx_INCREF(__pyx_t_9);
-      __Pyx_INCREF(__pyx_t_10);
-      #else
-      __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_9 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_10 = PySequence_ITEM(sequence, 2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_10);
-      #endif
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    } else {
-      Py_ssize_t index = -1;
-      __pyx_t_11 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_11);
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext;
-      index = 0; __pyx_t_1 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_1)) goto __pyx_L5_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_1);
-      index = 1; __pyx_t_9 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_9)) goto __pyx_L5_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_9);
-      index = 2; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L5_unpacking_failed;
-      __Pyx_GOTREF(__pyx_t_10);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_12 = NULL;
-      __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-      goto __pyx_L6_unpacking_done;
-      __pyx_L5_unpacking_failed:;
-      __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-      __pyx_t_12 = NULL;
-      if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_L6_unpacking_done:;
-    }
-    __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_t_9); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_v_bytesize = __pyx_t_1;
-    __pyx_t_1 = 0;
-    __pyx_v_nvalues = __pyx_t_13;
-    __pyx_v_values = __pyx_t_10;
-    __pyx_t_10 = 0;
-
-    /* "pysam/csamfile.pyx":3189
- *         elif auxtype == 'B':
- *             bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
- *             return values             # <<<<<<<<<<<<<<
- *         else:
- *             raise ValueError("unknown auxilliary type '%s'" % auxtype)
- */
-    __Pyx_XDECREF(__pyx_r);
-    __Pyx_INCREF(__pyx_v_values);
-    __pyx_r = __pyx_v_values;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":3191
- *             return values
- *         else:
- *             raise ValueError("unknown auxilliary type '%s'" % auxtype)             # <<<<<<<<<<<<<<
- * 
- * 
- */
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_unknown_auxilliary_type_s, __pyx_v_auxtype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_10);
-    PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_4);
-    __Pyx_GIVEREF(__pyx_t_4);
-    __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_10, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __Pyx_Raise(__pyx_t_4, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":3164
- *         return overlap
- * 
- *     def opt(self, tag):             # <<<<<<<<<<<<<<
- *         """retrieves optional data given a two-letter *tag*"""
- *         #see bam_aux.c: bam_aux_get() and bam_aux2i() etc
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_XDECREF(__pyx_t_10);
-  __Pyx_XDECREF(__pyx_t_11);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.opt", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_btag);
-  __Pyx_XDECREF(__pyx_v_auxtype);
-  __Pyx_XDECREF(__pyx_v_bytesize);
-  __Pyx_XDECREF(__pyx_v_values);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3194
- * 
- * 
- *     def fancy_str (self):             # <<<<<<<<<<<<<<
- *         """returns list of fieldnames/values in pretty format for debugging.
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_19fancy_str(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_11AlignedRead_18fancy_str[] = "AlignedRead.fancy_str(self)\nreturns list of fieldnames/values in pretty format for debugging.\n\n        ";
-static PyObject *__pyx_pw_5pysam_8csamfile_11AlignedRead_19fancy_str(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("fancy_str (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11AlignedRead_18fancy_str(((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11AlignedRead_18fancy_str(struct __pyx_obj_5pysam_8csamfile_AlignedRead *__pyx_v_self) {
-  PyObject *__pyx_v_ret_string = NULL;
-  PyObject *__pyx_v_field_names = NULL;
-  PyObject *__pyx_v_fields_names_in_order = NULL;
-  PyObject *__pyx_v_f = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  Py_ssize_t __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  int __pyx_t_10;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("fancy_str", 0);
-  __Pyx_TraceCall("fancy_str", __pyx_f[0], 3194);
-
-  /* "pysam/csamfile.pyx":3198
- * 
- *         """
- *         ret_string = []             # <<<<<<<<<<<<<<
- * 
- *         # Originally written by Leo. Note that not all of these fields
- */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_ret_string = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3202
- *         # Originally written by Leo. Note that not all of these fields
- *         # exist. Deprecate?
- *         field_names = {             # <<<<<<<<<<<<<<
- *            "tid":           "Contig index",
- *            "pos":           "Mapped position on contig",
- */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_tid, __pyx_kp_s_Contig_index) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_pos, __pyx_kp_s_Mapped_position_on_contig) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_mtid, __pyx_kp_s_Contig_index_for_mate_pair) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_mpos, __pyx_kp_s_Position_of_mate_pair) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_isize, __pyx_kp_s_Insert_size) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_flag, __pyx_kp_s_Binary_flag) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_n_cigar, __pyx_kp_s_Count_of_cigar_entries) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_cigar, __pyx_kp_s_Cigar_entries) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_qual, __pyx_kp_s_Mapping_quality) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_bin, __pyx_kp_s_Bam_index_bin_number) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_l_qname, __pyx_kp_s_Length_of_query_name) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_qname, __pyx_kp_s_Query_name) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_l_qseq, __pyx_kp_s_Length_of_query_sequence) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_qseq, __pyx_kp_s_Query_sequence) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_bqual, __pyx_kp_s_Quality_scores) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_l_data, __pyx_kp_s_Length_of_auxilary_data) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_m_data, __pyx_kp_s_Maximum_data_length) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3202; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_field_names = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3221
- *            "m_data":        "Maximum data length",
- *            }
- *         fields_names_in_order = ["tid", "pos", "mtid", "mpos", "isize", "flag",             # <<<<<<<<<<<<<<
- *                                  "n_cigar", "cigar", "qual", "bin", "l_qname", "qname",
- *                                  "l_qseq", "qseq", "bqual", "l_data", "m_data"]
- */
-  __pyx_t_1 = PyList_New(17); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(__pyx_n_s_tid);
-  PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_tid);
-  __Pyx_GIVEREF(__pyx_n_s_tid);
-  __Pyx_INCREF(__pyx_n_s_pos);
-  PyList_SET_ITEM(__pyx_t_1, 1, __pyx_n_s_pos);
-  __Pyx_GIVEREF(__pyx_n_s_pos);
-  __Pyx_INCREF(__pyx_n_s_mtid);
-  PyList_SET_ITEM(__pyx_t_1, 2, __pyx_n_s_mtid);
-  __Pyx_GIVEREF(__pyx_n_s_mtid);
-  __Pyx_INCREF(__pyx_n_s_mpos);
-  PyList_SET_ITEM(__pyx_t_1, 3, __pyx_n_s_mpos);
-  __Pyx_GIVEREF(__pyx_n_s_mpos);
-  __Pyx_INCREF(__pyx_n_s_isize);
-  PyList_SET_ITEM(__pyx_t_1, 4, __pyx_n_s_isize);
-  __Pyx_GIVEREF(__pyx_n_s_isize);
-  __Pyx_INCREF(__pyx_n_s_flag);
-  PyList_SET_ITEM(__pyx_t_1, 5, __pyx_n_s_flag);
-  __Pyx_GIVEREF(__pyx_n_s_flag);
-  __Pyx_INCREF(__pyx_n_s_n_cigar);
-  PyList_SET_ITEM(__pyx_t_1, 6, __pyx_n_s_n_cigar);
-  __Pyx_GIVEREF(__pyx_n_s_n_cigar);
-  __Pyx_INCREF(__pyx_n_s_cigar);
-  PyList_SET_ITEM(__pyx_t_1, 7, __pyx_n_s_cigar);
-  __Pyx_GIVEREF(__pyx_n_s_cigar);
-  __Pyx_INCREF(__pyx_n_s_qual);
-  PyList_SET_ITEM(__pyx_t_1, 8, __pyx_n_s_qual);
-  __Pyx_GIVEREF(__pyx_n_s_qual);
-  __Pyx_INCREF(__pyx_n_s_bin);
-  PyList_SET_ITEM(__pyx_t_1, 9, __pyx_n_s_bin);
-  __Pyx_GIVEREF(__pyx_n_s_bin);
-  __Pyx_INCREF(__pyx_n_s_l_qname);
-  PyList_SET_ITEM(__pyx_t_1, 10, __pyx_n_s_l_qname);
-  __Pyx_GIVEREF(__pyx_n_s_l_qname);
-  __Pyx_INCREF(__pyx_n_s_qname);
-  PyList_SET_ITEM(__pyx_t_1, 11, __pyx_n_s_qname);
-  __Pyx_GIVEREF(__pyx_n_s_qname);
-  __Pyx_INCREF(__pyx_n_s_l_qseq);
-  PyList_SET_ITEM(__pyx_t_1, 12, __pyx_n_s_l_qseq);
-  __Pyx_GIVEREF(__pyx_n_s_l_qseq);
-  __Pyx_INCREF(__pyx_n_s_qseq);
-  PyList_SET_ITEM(__pyx_t_1, 13, __pyx_n_s_qseq);
-  __Pyx_GIVEREF(__pyx_n_s_qseq);
-  __Pyx_INCREF(__pyx_n_s_bqual);
-  PyList_SET_ITEM(__pyx_t_1, 14, __pyx_n_s_bqual);
-  __Pyx_GIVEREF(__pyx_n_s_bqual);
-  __Pyx_INCREF(__pyx_n_s_l_data);
-  PyList_SET_ITEM(__pyx_t_1, 15, __pyx_n_s_l_data);
-  __Pyx_GIVEREF(__pyx_n_s_l_data);
-  __Pyx_INCREF(__pyx_n_s_m_data);
-  PyList_SET_ITEM(__pyx_t_1, 16, __pyx_n_s_m_data);
-  __Pyx_GIVEREF(__pyx_n_s_m_data);
-  __pyx_v_fields_names_in_order = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3225
- *                                  "l_qseq", "qseq", "bqual", "l_data", "m_data"]
- * 
- *         for f in fields_names_in_order:             # <<<<<<<<<<<<<<
- *             if f not in dir(self):
- *                 continue
- */
-  __pyx_t_1 = __pyx_v_fields_names_in_order; __Pyx_INCREF(__pyx_t_1); __pyx_t_2 = 0;
-  for (;;) {
-    if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_1)) break;
-    #if CYTHON_COMPILING_IN_CPYTHON
-    __pyx_t_3 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_3); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    #else
-    __pyx_t_3 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    #endif
-    __Pyx_XDECREF_SET(__pyx_v_f, __pyx_t_3);
-    __pyx_t_3 = 0;
-
-    /* "pysam/csamfile.pyx":3226
- * 
- *         for f in fields_names_in_order:
- *             if f not in dir(self):             # <<<<<<<<<<<<<<
- *                 continue
- *             ret_string.append("%-30s %-10s= %s" %
- */
-    __pyx_t_3 = PyObject_Dir(((PyObject *)__pyx_v_self)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = (__Pyx_PySequence_Contains(__pyx_v_f, __pyx_t_3, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_5 = (__pyx_t_4 != 0);
-    if (__pyx_t_5) {
-
-      /* "pysam/csamfile.pyx":3227
- *         for f in fields_names_in_order:
- *             if f not in dir(self):
- *                 continue             # <<<<<<<<<<<<<<
- *             ret_string.append("%-30s %-10s= %s" %
- *                               (field_names[f],
- */
-      goto __pyx_L3_continue;
-    }
-
-    /* "pysam/csamfile.pyx":3229
- *                 continue
- *             ret_string.append("%-30s %-10s= %s" %
- *                               (field_names[f],             # <<<<<<<<<<<<<<
- *                                "(" + f + ")",
- *                                self.__getattribute__(f)))
- */
-    __pyx_t_3 = __Pyx_PyDict_GetItem(__pyx_v_field_names, __pyx_v_f); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3229; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_3);
-
-    /* "pysam/csamfile.pyx":3230
- *             ret_string.append("%-30s %-10s= %s" %
- *                               (field_names[f],
- *                                "(" + f + ")",             # <<<<<<<<<<<<<<
- *                                self.__getattribute__(f)))
- * 
- */
-    __pyx_t_6 = PyNumber_Add(__pyx_kp_s__79, __pyx_v_f); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3230; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_7 = PyNumber_Add(__pyx_t_6, __pyx_kp_s__80); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3230; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-    /* "pysam/csamfile.pyx":3231
- *                               (field_names[f],
- *                                "(" + f + ")",
- *                                self.__getattribute__(f)))             # <<<<<<<<<<<<<<
- * 
- *         return ret_string
- */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_getattribute); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3231; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3231; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    __Pyx_INCREF(__pyx_v_f);
-    PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_f);
-    __Pyx_GIVEREF(__pyx_v_f);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3231; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-
-    /* "pysam/csamfile.pyx":3229
- *                 continue
- *             ret_string.append("%-30s %-10s= %s" %
- *                               (field_names[f],             # <<<<<<<<<<<<<<
- *                                "(" + f + ")",
- *                                self.__getattribute__(f)))
- */
-    __pyx_t_8 = PyTuple_New(3); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_8);
-    PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_7);
-    __Pyx_GIVEREF(__pyx_t_7);
-    PyTuple_SET_ITEM(__pyx_t_8, 2, __pyx_t_9);
-    __Pyx_GIVEREF(__pyx_t_9);
-    __pyx_t_3 = 0;
-    __pyx_t_7 = 0;
-    __pyx_t_9 = 0;
-
-    /* "pysam/csamfile.pyx":3228
- *             if f not in dir(self):
- *                 continue
- *             ret_string.append("%-30s %-10s= %s" %             # <<<<<<<<<<<<<<
- *                               (field_names[f],
- *                                "(" + f + ")",
- */
-    __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_30s_10s_s, __pyx_t_8); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3228; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    __pyx_t_10 = __Pyx_PyList_Append(__pyx_v_ret_string, __pyx_t_9); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3228; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_L3_continue:;
-  }
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3233
- *                                self.__getattribute__(f)))
- * 
- *         return ret_string             # <<<<<<<<<<<<<<
- * 
- * cdef class PileupProxy:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_ret_string);
-  __pyx_r = __pyx_v_ret_string;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3194
- * 
- * 
- *     def fancy_str (self):             # <<<<<<<<<<<<<<
- *         """returns list of fieldnames/values in pretty format for debugging.
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.AlignedRead.fancy_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_ret_string);
-  __Pyx_XDECREF(__pyx_v_field_names);
-  __Pyx_XDECREF(__pyx_v_fields_names_in_order);
-  __Pyx_XDECREF(__pyx_v_f);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3252
- *     will change.
- *     '''
- *     def __init__(self):             # <<<<<<<<<<<<<<
- *         raise TypeError("This class cannot be instantiated from Python")
- * 
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11PileupProxy_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11PileupProxy_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  if (unlikely(PyTuple_GET_SIZE(__pyx_args) > 0)) {
-    __Pyx_RaiseArgtupleInvalid("__init__", 1, 0, 0, PyTuple_GET_SIZE(__pyx_args)); return -1;}
-  if (unlikely(__pyx_kwds) && unlikely(PyDict_Size(__pyx_kwds) > 0) && unlikely(!__Pyx_CheckKeywordStrings(__pyx_kwds, "__init__", 0))) return -1;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy___init__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11PileupProxy___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 3252);
-
-  /* "pysam/csamfile.pyx":3253
- *     '''
- *     def __init__(self):
- *         raise TypeError("This class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
- * 
- *     def __str__(self):
- */
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__81, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":3252
- *     will change.
- *     '''
- *     def __init__(self):             # <<<<<<<<<<<<<<
- *         raise TypeError("This class cannot be instantiated from Python")
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3255
- *         raise TypeError("This class cannot be instantiated from Python")
- * 
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" +\
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3__str__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3__str__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_2__str__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 3255);
-
-  /* "pysam/csamfile.pyx":3256
- * 
- *     def __str__(self):
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\             # <<<<<<<<<<<<<<
- *             "\n" +\
- *             "\n".join( map(str, self.pileups) )
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_self->pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_n); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyTuple_New(3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_4, 2, __pyx_t_3);
-  __Pyx_GIVEREF(__pyx_t_3);
-  __pyx_t_1 = 0;
-  __pyx_t_2 = 0;
-  __pyx_t_3 = 0;
-  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = PyNumber_Add(__pyx_t_3, __pyx_kp_s__4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3256; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":3258
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" +\
- *             "\n".join( map(str, self.pileups) )             # <<<<<<<<<<<<<<
- * 
- *     property tid:
- */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_pileups); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_3);
-  __Pyx_GIVEREF(__pyx_t_3);
-  __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__4, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":3257
- *     def __str__(self):
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" +\             # <<<<<<<<<<<<<<
- *             "\n".join( map(str, self.pileups) )
- * 
- */
-  __pyx_t_3 = PyNumber_Add(__pyx_t_4, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_r = __pyx_t_3;
-  __pyx_t_3 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3255
- *         raise TypeError("This class cannot be instantiated from Python")
- * 
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" +\
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3262
- *     property tid:
- *         '''the chromosome ID as is defined in the header'''
- *         def __get__(self): return self.tid             # <<<<<<<<<<<<<<
- * 
- *     property n:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3tid_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3tid_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_3tid___get__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3tid___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3262);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3262; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.tid.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3266
- *     property n:
- *         '''number of reads mapping to this column.'''
- *         def __get__(self): return self.n_pu             # <<<<<<<<<<<<<<
- *         def __set__(self, n): self.n_pu = n
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_1n_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_1n_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_1n___get__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_1n___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3266);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->n_pu); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.n.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3267
- *         '''number of reads mapping to this column.'''
- *         def __get__(self): return self.n_pu
- *         def __set__(self, n): self.n_pu = n             # <<<<<<<<<<<<<<
- * 
- *     property pos:
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_11PileupProxy_1n_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_n); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_11PileupProxy_1n_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_n) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__set__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_1n_2__set__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self), ((PyObject *)__pyx_v_n));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_11PileupProxy_1n_2__set__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self, PyObject *__pyx_v_n) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__set__", 0);
-  __Pyx_TraceCall("__set__", __pyx_f[0], 3267);
-  __pyx_t_1 = __Pyx_PyInt_As_int(__pyx_v_n); if (unlikely((__pyx_t_1 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->n_pu = __pyx_t_1;
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.n.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3270
- * 
- *     property pos:
- *         def __get__(self): return self.pos             # <<<<<<<<<<<<<<
- * 
- *     property pileups:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3pos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_3pos_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_3pos___get__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_3pos___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3270);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3274
- *     property pileups:
- *         '''list of reads (:class:`pysam.PileupRead`) aligned to this column'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef int x
- *             pileups = []
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_7pileups_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_11PileupProxy_7pileups_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(((struct __pyx_obj_5pysam_8csamfile_PileupProxy *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_11PileupProxy_7pileups___get__(struct __pyx_obj_5pysam_8csamfile_PileupProxy *__pyx_v_self) {
-  int __pyx_v_x;
-  PyObject *__pyx_v_pileups = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_t_2;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_t_6;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3274);
-
-  /* "pysam/csamfile.pyx":3276
- *         def __get__(self):
- *             cdef int x
- *             pileups = []             # <<<<<<<<<<<<<<
- * 
- *             if self.plp == NULL or self.plp[0] == NULL:
- */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_pileups = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":3278
- *             pileups = []
- * 
- *             if self.plp == NULL or self.plp[0] == NULL:             # <<<<<<<<<<<<<<
- *                 raise ValueError("PileupProxy accessed after iterator finished")
- * 
- */
-  __pyx_t_2 = ((__pyx_v_self->plp == NULL) != 0);
-  if (!__pyx_t_2) {
-    __pyx_t_3 = (((__pyx_v_self->plp[0]) == NULL) != 0);
-    __pyx_t_4 = __pyx_t_3;
-  } else {
-    __pyx_t_4 = __pyx_t_2;
-  }
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":3279
- * 
- *             if self.plp == NULL or self.plp[0] == NULL:
- *                 raise ValueError("PileupProxy accessed after iterator finished")             # <<<<<<<<<<<<<<
- * 
- *             # warning: there could be problems if self.n and self.buf are
- */
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__82, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":3283
- *             # warning: there could be problems if self.n and self.buf are
- *             # out of sync.
- *             for x from 0 <= x < self.n_pu:             # <<<<<<<<<<<<<<
- *                 pileups.append(makePileupRead(&(self.plp[0][x])))
- *             return pileups
- */
-  __pyx_t_5 = __pyx_v_self->n_pu;
-  for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_5; __pyx_v_x++) {
-
-    /* "pysam/csamfile.pyx":3284
- *             # out of sync.
- *             for x from 0 <= x < self.n_pu:
- *                 pileups.append(makePileupRead(&(self.plp[0][x])))             # <<<<<<<<<<<<<<
- *             return pileups
- * 
- */
-    __pyx_t_1 = __pyx_f_5pysam_8csamfile_makePileupRead((&((__pyx_v_self->plp[0])[__pyx_v_x]))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3284; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_pileups, __pyx_t_1); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3284; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  }
-
-  /* "pysam/csamfile.pyx":3285
- *             for x from 0 <= x < self.n_pu:
- *                 pileups.append(makePileupRead(&(self.plp[0][x])))
- *             return pileups             # <<<<<<<<<<<<<<
- * 
- * cdef class PileupRead:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(__pyx_v_pileups);
-  __pyx_r = __pyx_v_pileups;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3274
- *     property pileups:
- *         '''list of reads (:class:`pysam.PileupRead`) aligned to this column'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             cdef int x
- *             pileups = []
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupProxy.pileups.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_pileups);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3291
- *     '''
- * 
- *     def __init__(self):             # <<<<<<<<<<<<<<
- *         raise TypeError("This class cannot be instantiated from Python")
- * 
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_10PileupRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_10PileupRead_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  if (unlikely(PyTuple_GET_SIZE(__pyx_args) > 0)) {
-    __Pyx_RaiseArgtupleInvalid("__init__", 1, 0, 0, PyTuple_GET_SIZE(__pyx_args)); return -1;}
-  if (unlikely(__pyx_kwds) && unlikely(PyDict_Size(__pyx_kwds) > 0) && unlikely(!__Pyx_CheckKeywordStrings(__pyx_kwds, "__init__", 0))) return -1;
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead___init__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_10PileupRead___init__(CYTHON_UNUSED struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 3291);
-
-  /* "pysam/csamfile.pyx":3292
- * 
- *     def __init__(self):
- *         raise TypeError("This class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
- * 
- *     def __str__(self):
- */
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__83, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":3291
- *     '''
- * 
- *     def __init__(self):             # <<<<<<<<<<<<<<
- *         raise TypeError("This class cannot be instantiated from Python")
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3294
- *         raise TypeError("This class cannot be instantiated from Python")
- * 
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.alignment, self.qpos, self.indel, self.level, self.is_del, self.is_head, self.is_tail ) ) )
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_3__str__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_3__str__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_2__str__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 3294);
-
-  /* "pysam/csamfile.pyx":3295
- * 
- *     def __str__(self):
- *         return "\t".join( map(str, (self.alignment, self.qpos, self.indel, self.level, self.is_del, self.is_head, self.is_tail ) ) )             # <<<<<<<<<<<<<<
- * 
- *     property alignment:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_alignment); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_qpos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_indel); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_level); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_del); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_head); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_is_tail); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-  __pyx_t_8 = PyTuple_New(7); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_8);
-  PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_8, 2, __pyx_t_3);
-  __Pyx_GIVEREF(__pyx_t_3);
-  PyTuple_SET_ITEM(__pyx_t_8, 3, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_8, 4, __pyx_t_5);
-  __Pyx_GIVEREF(__pyx_t_5);
-  PyTuple_SET_ITEM(__pyx_t_8, 5, __pyx_t_6);
-  __Pyx_GIVEREF(__pyx_t_6);
-  PyTuple_SET_ITEM(__pyx_t_8, 6, __pyx_t_7);
-  __Pyx_GIVEREF(__pyx_t_7);
-  __pyx_t_1 = 0;
-  __pyx_t_2 = 0;
-  __pyx_t_3 = 0;
-  __pyx_t_4 = 0;
-  __pyx_t_5 = 0;
-  __pyx_t_6 = 0;
-  __pyx_t_7 = 0;
-  __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_7, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_8);
-  __Pyx_GIVEREF(__pyx_t_8);
-  __pyx_t_8 = 0;
-  __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_7, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_8);
-  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-  __pyx_t_7 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_8); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-  __pyx_r = __pyx_t_7;
-  __pyx_t_7 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3294
- *         raise TypeError("This class cannot be instantiated from Python")
- * 
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.alignment, self.qpos, self.indel, self.level, self.is_del, self.is_head, self.is_tail ) ) )
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3299
- *     property alignment:
- *         """a :class:`pysam.AlignedRead` object of the aligned read"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._alignment
- *     property qpos:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_9alignment_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_9alignment_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_9alignment___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_9alignment___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3299);
-
-  /* "pysam/csamfile.pyx":3300
- *         """a :class:`pysam.AlignedRead` object of the aligned read"""
- *         def __get__(self):
- *             return self._alignment             # <<<<<<<<<<<<<<
- *     property qpos:
- *         """position of the read base at the pileup site, 0-based"""
- */
-  __Pyx_XDECREF(__pyx_r);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self->_alignment));
-  __pyx_r = ((PyObject *)__pyx_v_self->_alignment);
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3299
- *     property alignment:
- *         """a :class:`pysam.AlignedRead` object of the aligned read"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._alignment
- *     property qpos:
- */
-
-  /* function exit code */
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3303
- *     property qpos:
- *         """position of the read base at the pileup site, 0-based"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._qpos
- *     property indel:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_4qpos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_4qpos_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_4qpos___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_4qpos___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3303);
-
-  /* "pysam/csamfile.pyx":3304
- *         """position of the read base at the pileup site, 0-based"""
- *         def __get__(self):
- *             return self._qpos             # <<<<<<<<<<<<<<
- *     property indel:
- *         """indel length; 0 for no indel, positive for ins and negative for del"""
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int32_t(__pyx_v_self->_qpos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3303
- *     property qpos:
- *         """position of the read base at the pileup site, 0-based"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._qpos
- *     property indel:
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.qpos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3307
- *     property indel:
- *         """indel length; 0 for no indel, positive for ins and negative for del"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._indel
- *     property is_del:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_5indel_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_5indel_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_5indel___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5indel___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3307);
-
-  /* "pysam/csamfile.pyx":3308
- *         """indel length; 0 for no indel, positive for ins and negative for del"""
- *         def __get__(self):
- *             return self._indel             # <<<<<<<<<<<<<<
- *     property is_del:
- *         """1 iff the base on the padded read is a deletion"""
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_indel); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3308; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3307
- *     property indel:
- *         """indel length; 0 for no indel, positive for ins and negative for del"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._indel
- *     property is_del:
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.indel.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3311
- *     property is_del:
- *         """1 iff the base on the padded read is a deletion"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._is_del
- *     property is_head:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_6is_del_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_6is_del_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_6is_del___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3311);
-
-  /* "pysam/csamfile.pyx":3312
- *         """1 iff the base on the padded read is a deletion"""
- *         def __get__(self):
- *             return self._is_del             # <<<<<<<<<<<<<<
- *     property is_head:
- *         def __get__(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->_is_del); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3311
- *     property is_del:
- *         """1 iff the base on the padded read is a deletion"""
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._is_del
- *     property is_head:
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.is_del.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3314
- *             return self._is_del
- *     property is_head:
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._is_head
- *     property is_tail:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_7is_head_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_7is_head_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_head___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3314);
-
-  /* "pysam/csamfile.pyx":3315
- *     property is_head:
- *         def __get__(self):
- *             return self._is_head             # <<<<<<<<<<<<<<
- *     property is_tail:
- *         def __get__(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->_is_head); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3315; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3314
- *             return self._is_del
- *     property is_head:
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._is_head
- *     property is_tail:
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.is_head.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3317
- *             return self._is_head
- *     property is_tail:
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._is_tail
- *     property level:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_7is_tail_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_7is_tail_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_7is_tail___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_7is_tail___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3317);
-
-  /* "pysam/csamfile.pyx":3318
- *     property is_tail:
- *         def __get__(self):
- *             return self._is_tail             # <<<<<<<<<<<<<<
- *     property level:
- *         def __get__(self):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->_is_tail); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3317
- *             return self._is_head
- *     property is_tail:
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._is_tail
- *     property level:
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.is_tail.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3320
- *             return self._is_tail
- *     property level:
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._level
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_5level_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_10PileupRead_5level_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_10PileupRead_5level___get__(((struct __pyx_obj_5pysam_8csamfile_PileupRead *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_10PileupRead_5level___get__(struct __pyx_obj_5pysam_8csamfile_PileupRead *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3320);
-
-  /* "pysam/csamfile.pyx":3321
- *     property level:
- *         def __get__(self):
- *             return self._level             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_level); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3320
- *             return self._is_tail
- *     property level:
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._level
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.PileupRead.level.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3337
- *     property tid:
- *         '''the chromosome ID as is defined in the header'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._tid
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_3tid_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_3tid_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3tid___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3337);
-
-  /* "pysam/csamfile.pyx":3338
- *         '''the chromosome ID as is defined in the header'''
- *         def __get__(self):
- *             return self._tid             # <<<<<<<<<<<<<<
- * 
- *     property pos:
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3337
- *     property tid:
- *         '''the chromosome ID as is defined in the header'''
- *         def __get__(self):             # <<<<<<<<<<<<<<
- *             return self._tid
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.tid.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3342
- *     property pos:
- *        '''nucleotide position of SNP.'''
- *        def __get__(self): return self._pos             # <<<<<<<<<<<<<<
- * 
- *     property reference_base:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_3pos_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_3pos_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_3pos___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3342);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3342; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.pos.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3346
- *     property reference_base:
- *        '''reference base at pos. ``N`` if no reference sequence supplied.'''
- *        def __get__(self): return from_string_and_size( &self._reference_base, 1 )             # <<<<<<<<<<<<<<
- * 
- *     property genotype:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_14reference_base_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_14reference_base_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_14reference_base___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3346);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_from_string_and_size((&__pyx_v_self->_reference_base), 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.reference_base.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3350
- *     property genotype:
- *        '''the genotype called.'''
- *        def __get__(self): return from_string_and_size( &self._genotype, 1 )             # <<<<<<<<<<<<<<
- * 
- *     property consensus_quality:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_8genotype_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_8genotype_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8genotype___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3350);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_8csamfile_from_string_and_size((&__pyx_v_self->_genotype), 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.genotype.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3354
- *     property consensus_quality:
- *        '''the genotype quality (Phred-scaled).'''
- *        def __get__(self): return self._consensus_quality             # <<<<<<<<<<<<<<
- * 
- *     property snp_quality:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_17consensus_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_17consensus_quality_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_17consensus_quality___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_17consensus_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3354);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_consensus_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.consensus_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3358
- *     property snp_quality:
- *        '''the snp quality (Phred scaled) - probability of consensus being identical to reference sequence.'''
- *        def __get__(self): return self._snp_quality             # <<<<<<<<<<<<<<
- * 
- *     property mapping_quality:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_11snp_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_11snp_quality_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_11snp_quality___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_11snp_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3358);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_snp_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.snp_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3362
- *     property mapping_quality:
- *        '''the root mean square (rms) of the mapping quality of all reads involved in the call.'''
- *        def __get__(self): return self._rms_mapping_quality             # <<<<<<<<<<<<<<
- * 
- *     property coverage:
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_15mapping_quality_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_15mapping_quality_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_15mapping_quality___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3362);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_rms_mapping_quality); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.mapping_quality.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3366
- *     property coverage:
- *        '''coverage or read depth - the number of reads involved in the call.'''
- *        def __get__(self): return self._coverage             # <<<<<<<<<<<<<<
- * 
- *     def __str__(self):
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_8coverage_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_8coverage_1__get__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall_8coverage___get__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__get__", 0);
-  __Pyx_TraceCall("__get__", __pyx_f[0], 3366);
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->_coverage); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = __pyx_t_1;
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.coverage.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3368
- *        def __get__(self): return self._coverage
- * 
- *     def __str__(self):             # <<<<<<<<<<<<<<
- * 
- *         return "\t".join( map(str, (
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_1__str__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_8csamfile_7SNPCall_1__str__(PyObject *__pyx_v_self) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__str__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_7SNPCall___str__(((struct __pyx_obj_5pysam_8csamfile_SNPCall *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_7SNPCall___str__(struct __pyx_obj_5pysam_8csamfile_SNPCall *__pyx_v_self) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  PyObject *__pyx_t_7 = NULL;
-  PyObject *__pyx_t_8 = NULL;
-  PyObject *__pyx_t_9 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__str__", 0);
-  __Pyx_TraceCall("__str__", __pyx_f[0], 3368);
-
-  /* "pysam/csamfile.pyx":3370
- *     def __str__(self):
- * 
- *         return "\t".join( map(str, (             # <<<<<<<<<<<<<<
- *                     self.tid,
- *                     self.pos,
- */
-  __Pyx_XDECREF(__pyx_r);
-
-  /* "pysam/csamfile.pyx":3371
- * 
- *         return "\t".join( map(str, (
- *                     self.tid,             # <<<<<<<<<<<<<<
- *                     self.pos,
- *                     self.reference_base,
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_tid); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-
-  /* "pysam/csamfile.pyx":3372
- *         return "\t".join( map(str, (
- *                     self.tid,
- *                     self.pos,             # <<<<<<<<<<<<<<
- *                     self.reference_base,
- *                     self.genotype,
- */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_pos); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-
-  /* "pysam/csamfile.pyx":3373
- *                     self.tid,
- *                     self.pos,
- *                     self.reference_base,             # <<<<<<<<<<<<<<
- *                     self.genotype,
- *                     self.consensus_quality,
- */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_reference_base); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-
-  /* "pysam/csamfile.pyx":3374
- *                     self.pos,
- *                     self.reference_base,
- *                     self.genotype,             # <<<<<<<<<<<<<<
- *                     self.consensus_quality,
- *                     self.snp_quality,
- */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_genotype); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_4);
-
-  /* "pysam/csamfile.pyx":3375
- *                     self.reference_base,
- *                     self.genotype,
- *                     self.consensus_quality,             # <<<<<<<<<<<<<<
- *                     self.snp_quality,
- *                     self.mapping_quality,
- */
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_consensus_quality); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-
-  /* "pysam/csamfile.pyx":3376
- *                     self.genotype,
- *                     self.consensus_quality,
- *                     self.snp_quality,             # <<<<<<<<<<<<<<
- *                     self.mapping_quality,
- *                     self.coverage ) ) )
- */
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_snp_quality); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-
-  /* "pysam/csamfile.pyx":3377
- *                     self.consensus_quality,
- *                     self.snp_quality,
- *                     self.mapping_quality,             # <<<<<<<<<<<<<<
- *                     self.coverage ) ) )
- * 
- */
-  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_mapping_quality); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_7);
-
-  /* "pysam/csamfile.pyx":3378
- *                     self.snp_quality,
- *                     self.mapping_quality,
- *                     self.coverage ) ) )             # <<<<<<<<<<<<<<
- * 
- * 
- */
-  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_coverage); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_8);
-
-  /* "pysam/csamfile.pyx":3371
- * 
- *         return "\t".join( map(str, (
- *                     self.tid,             # <<<<<<<<<<<<<<
- *                     self.pos,
- *                     self.reference_base,
- */
-  __pyx_t_9 = PyTuple_New(8); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_9, 2, __pyx_t_3);
-  __Pyx_GIVEREF(__pyx_t_3);
-  PyTuple_SET_ITEM(__pyx_t_9, 3, __pyx_t_4);
-  __Pyx_GIVEREF(__pyx_t_4);
-  PyTuple_SET_ITEM(__pyx_t_9, 4, __pyx_t_5);
-  __Pyx_GIVEREF(__pyx_t_5);
-  PyTuple_SET_ITEM(__pyx_t_9, 5, __pyx_t_6);
-  __Pyx_GIVEREF(__pyx_t_6);
-  PyTuple_SET_ITEM(__pyx_t_9, 6, __pyx_t_7);
-  __Pyx_GIVEREF(__pyx_t_7);
-  PyTuple_SET_ITEM(__pyx_t_9, 7, __pyx_t_8);
-  __Pyx_GIVEREF(__pyx_t_8);
-  __pyx_t_1 = 0;
-  __pyx_t_2 = 0;
-  __pyx_t_3 = 0;
-  __pyx_t_4 = 0;
-  __pyx_t_5 = 0;
-  __pyx_t_6 = 0;
-  __pyx_t_7 = 0;
-  __pyx_t_8 = 0;
-
-  /* "pysam/csamfile.pyx":3370
- *     def __str__(self):
- * 
- *         return "\t".join( map(str, (             # <<<<<<<<<<<<<<
- *                     self.tid,
- *                     self.pos,
- */
-  __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_8);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_8, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyString_Type))));
-  PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_9);
-  __Pyx_GIVEREF(__pyx_t_9);
-  __pyx_t_9 = 0;
-  __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-  __pyx_t_8 = __Pyx_PyString_Join(__pyx_kp_s__3, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_8);
-  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  __pyx_r = __pyx_t_8;
-  __pyx_t_8 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/csamfile.pyx":3368
- *        def __get__(self): return self._coverage
- * 
- *     def __str__(self):             # <<<<<<<<<<<<<<
- * 
- *         return "\t".join( map(str, (
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
-  __Pyx_XDECREF(__pyx_t_7);
-  __Pyx_XDECREF(__pyx_t_8);
-  __Pyx_XDECREF(__pyx_t_9);
-  __Pyx_AddTraceback("pysam.csamfile.SNPCall.__str__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3391
- *     """
- * 
- *     def __init__(self, Samfile samfile, int reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         # makes sure that samfile stays alive as long as this
- */
-
-/* Python wrapper */
-static int __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile = 0;
-  int __pyx_v_reopen;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
-  {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_samfile,&__pyx_n_s_reopen,0};
-    PyObject* values[2] = {0,0};
-    if (unlikely(__pyx_kwds)) {
-      Py_ssize_t kw_args;
-      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
-      switch (pos_args) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        case  0: break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-      kw_args = PyDict_Size(__pyx_kwds);
-      switch (pos_args) {
-        case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samfile)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (kw_args > 0) {
-          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_reopen);
-          if (value) { values[1] = value; kw_args--; }
-        }
-      }
-      if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-      }
-    } else {
-      switch (PyTuple_GET_SIZE(__pyx_args)) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
-        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-        break;
-        default: goto __pyx_L5_argtuple_error;
-      }
-    }
-    __pyx_v_samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)values[0]);
-    if (values[1]) {
-      __pyx_v_reopen = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_reopen == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    } else {
-      __pyx_v_reopen = ((int)1);
-    }
-  }
-  goto __pyx_L4_argument_unpacking_done;
-  __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-  __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IndexedReads.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __Pyx_RefNannyFinishContext();
-  return -1;
-  __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_samfile), __pyx_ptype_5pysam_8csamfile_Samfile, 1, "samfile", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_r = __pyx_pf_5pysam_8csamfile_12IndexedReads___init__(((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)__pyx_v_self), __pyx_v_samfile, __pyx_v_reopen);
-
-  /* function exit code */
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static int __pyx_pf_5pysam_8csamfile_12IndexedReads___init__(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self, struct __pyx_obj_5pysam_8csamfile_Samfile *__pyx_v_samfile, int __pyx_v_reopen) {
-  int __pyx_r;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  char const *__pyx_t_2;
-  htsFile *__pyx_t_3;
-  BGZF *__pyx_t_4;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__init__", 0);
-  __Pyx_TraceCall("__init__", __pyx_f[0], 3391);
-
-  /* "pysam/csamfile.pyx":3395
- *         # makes sure that samfile stays alive as long as this
- *         # object is alive.
- *         self.samfile = samfile             # <<<<<<<<<<<<<<
- * 
- *         assert samfile.isbam, "can only IndexReads on bam files"
- */
-  __Pyx_INCREF(((PyObject *)__pyx_v_samfile));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_samfile));
-  __Pyx_GOTREF(__pyx_v_self->samfile);
-  __Pyx_DECREF(((PyObject *)__pyx_v_self->samfile));
-  __pyx_v_self->samfile = __pyx_v_samfile;
-
-  /* "pysam/csamfile.pyx":3397
- *         self.samfile = samfile
- * 
- *         assert samfile.isbam, "can only IndexReads on bam files"             # <<<<<<<<<<<<<<
- * 
- *         # reopen the file - note that this makes the iterator
- */
-  #ifndef CYTHON_WITHOUT_ASSERTIONS
-  if (unlikely(!Py_OptimizeFlag)) {
-    if (unlikely(!(__pyx_v_samfile->isbam != 0))) {
-      PyErr_SetObject(PyExc_AssertionError, __pyx_kp_s_can_only_IndexReads_on_bam_files);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-  }
-  #endif
-
-  /* "pysam/csamfile.pyx":3401
- *         # reopen the file - note that this makes the iterator
- *         # slow and causes pileup to slow down significantly.
- *         if reopen:             # <<<<<<<<<<<<<<
- *             self.htsfile = hts_open(samfile._filename, 'r')
- *             assert self.htsfile != NULL
- */
-  __pyx_t_1 = (__pyx_v_reopen != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":3402
- *         # slow and causes pileup to slow down significantly.
- *         if reopen:
- *             self.htsfile = hts_open(samfile._filename, 'r')             # <<<<<<<<<<<<<<
- *             assert self.htsfile != NULL
- *             # read header - required for accurate positioning
- */
-    __pyx_t_2 = __Pyx_PyObject_AsString(__pyx_v_samfile->_filename); if (unlikely((!__pyx_t_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3402; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_self->htsfile = hts_open(__pyx_t_2, __pyx_k_r);
-
-    /* "pysam/csamfile.pyx":3403
- *         if reopen:
- *             self.htsfile = hts_open(samfile._filename, 'r')
- *             assert self.htsfile != NULL             # <<<<<<<<<<<<<<
- *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)
- */
-    #ifndef CYTHON_WITHOUT_ASSERTIONS
-    if (unlikely(!Py_OptimizeFlag)) {
-      if (unlikely(!((__pyx_v_self->htsfile != NULL) != 0))) {
-        PyErr_SetNone(PyExc_AssertionError);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3403; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      }
-    }
-    #endif
-
-    /* "pysam/csamfile.pyx":3405
- *             assert self.htsfile != NULL
- *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)             # <<<<<<<<<<<<<<
- *             self.owns_samfile = True
- *         else:
- */
-    sam_hdr_read(__pyx_v_self->htsfile);
-
-    /* "pysam/csamfile.pyx":3406
- *             # read header - required for accurate positioning
- *             sam_hdr_read(self.htsfile)
- *             self.owns_samfile = True             # <<<<<<<<<<<<<<
- *         else:
- *             self.htsfile = self.samfile.htsfile
- */
-    __pyx_v_self->owns_samfile = 1;
-    goto __pyx_L3;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":3408
- *             self.owns_samfile = True
- *         else:
- *             self.htsfile = self.samfile.htsfile             # <<<<<<<<<<<<<<
- *             self.owns_samfile = False
- * 
- */
-    __pyx_t_3 = __pyx_v_self->samfile->htsfile;
-    __pyx_v_self->htsfile = __pyx_t_3;
-
-    /* "pysam/csamfile.pyx":3409
- *         else:
- *             self.htsfile = self.samfile.htsfile
- *             self.owns_samfile = False             # <<<<<<<<<<<<<<
- * 
- *         # TODO: BAM file specific
- */
-    __pyx_v_self->owns_samfile = 0;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":3412
- * 
- *         # TODO: BAM file specific
- *         self.fp = self.htsfile.fp.bgzf             # <<<<<<<<<<<<<<
- * 
- *     def build(self):
- */
-  __pyx_t_4 = __pyx_v_self->htsfile->fp.bgzf;
-  __pyx_v_self->fp = __pyx_t_4;
-
-  /* "pysam/csamfile.pyx":3391
- *     """
- * 
- *     def __init__(self, Samfile samfile, int reopen=True):             # <<<<<<<<<<<<<<
- * 
- *         # makes sure that samfile stays alive as long as this
- */
-
-  /* function exit code */
-  __pyx_r = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_AddTraceback("pysam.csamfile.IndexedReads.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = -1;
-  __pyx_L0:;
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3414
- *         self.fp = self.htsfile.fp.bgzf
- * 
- *     def build(self):             # <<<<<<<<<<<<<<
- *         '''build index.'''
- * 
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_12IndexedReads_3build(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_12IndexedReads_2build[] = "IndexedReads.build(self)\nbuild index.";
-static PyObject *__pyx_pw_5pysam_8csamfile_12IndexedReads_3build(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("build (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_12IndexedReads_2build(((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_12IndexedReads_2build(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self) {
-  int __pyx_v_ret;
-  bam1_t *__pyx_v_b;
-  uint64_t __pyx_v_pos;
-  PyObject *__pyx_v_qname = NULL;
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  PyObject *__pyx_t_3 = NULL;
-  int __pyx_t_4;
-  int __pyx_t_5;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("build", 0);
-  __Pyx_TraceCall("build", __pyx_f[0], 3414);
-
-  /* "pysam/csamfile.pyx":3417
- *         '''build index.'''
- * 
- *         self.index = collections.defaultdict(list)             # <<<<<<<<<<<<<<
- * 
- *         # this method will start indexing from the current file position
- */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_collections); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_defaultdict); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(((PyObject *)((PyObject*)(&PyList_Type))));
-  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)((PyObject*)(&PyList_Type))));
-  __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyList_Type))));
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_GIVEREF(__pyx_t_3);
-  __Pyx_GOTREF(__pyx_v_self->index);
-  __Pyx_DECREF(__pyx_v_self->index);
-  __pyx_v_self->index = __pyx_t_3;
-  __pyx_t_3 = 0;
-
-  /* "pysam/csamfile.pyx":3421
- *         # this method will start indexing from the current file position
- *         # if you decide
- *         cdef int ret = 1             # <<<<<<<<<<<<<<
- *         cdef bam1_t * b = <bam1_t*>calloc(1, sizeof( bam1_t))
- * 
- */
-  __pyx_v_ret = 1;
-
-  /* "pysam/csamfile.pyx":3422
- *         # if you decide
- *         cdef int ret = 1
- *         cdef bam1_t * b = <bam1_t*>calloc(1, sizeof( bam1_t))             # <<<<<<<<<<<<<<
- * 
- *         cdef uint64_t pos
- */
-  __pyx_v_b = ((bam1_t *)calloc(1, (sizeof(bam1_t))));
-
-  /* "pysam/csamfile.pyx":3426
- *         cdef uint64_t pos
- * 
- *         while ret > 0:             # <<<<<<<<<<<<<<
- *             pos = bgzf_tell(self.fp)
- *             ret = sam_read1(self.htsfile,
- */
-  while (1) {
-    __pyx_t_4 = ((__pyx_v_ret > 0) != 0);
-    if (!__pyx_t_4) break;
-
-    /* "pysam/csamfile.pyx":3427
- * 
- *         while ret > 0:
- *             pos = bgzf_tell(self.fp)             # <<<<<<<<<<<<<<
- *             ret = sam_read1(self.htsfile,
- *                             self.samfile.header,
- */
-    __pyx_v_pos = bgzf_tell(__pyx_v_self->fp);
-
-    /* "pysam/csamfile.pyx":3428
- *         while ret > 0:
- *             pos = bgzf_tell(self.fp)
- *             ret = sam_read1(self.htsfile,             # <<<<<<<<<<<<<<
- *                             self.samfile.header,
- *                             b)
- */
-    __pyx_v_ret = sam_read1(__pyx_v_self->htsfile, __pyx_v_self->samfile->header, __pyx_v_b);
-
-    /* "pysam/csamfile.pyx":3431
- *                             self.samfile.header,
- *                             b)
- *             if ret > 0:             # <<<<<<<<<<<<<<
- *                 qname = _charptr_to_str(pysam_bam_get_qname(b))
- *                 self.index[qname].append(pos)
- */
-    __pyx_t_4 = ((__pyx_v_ret > 0) != 0);
-    if (__pyx_t_4) {
-
-      /* "pysam/csamfile.pyx":3432
- *                             b)
- *             if ret > 0:
- *                 qname = _charptr_to_str(pysam_bam_get_qname(b))             # <<<<<<<<<<<<<<
- *                 self.index[qname].append(pos)
- * 
- */
-      __pyx_t_3 = __pyx_f_5pysam_8csamfile__charptr_to_str(pysam_bam_get_qname(__pyx_v_b)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_XDECREF_SET(__pyx_v_qname, __pyx_t_3);
-      __pyx_t_3 = 0;
-
-      /* "pysam/csamfile.pyx":3433
- *             if ret > 0:
- *                 qname = _charptr_to_str(pysam_bam_get_qname(b))
- *                 self.index[qname].append(pos)             # <<<<<<<<<<<<<<
- * 
- *         bam_destroy1(b)
- */
-      __pyx_t_3 = PyObject_GetItem(__pyx_v_self->index, __pyx_v_qname); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3433; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_1 = __Pyx_PyInt_From_uint64_t(__pyx_v_pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_5 = __Pyx_PyObject_Append(__pyx_t_3, __pyx_t_1); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      goto __pyx_L5;
-    }
-    __pyx_L5:;
-  }
-
-  /* "pysam/csamfile.pyx":3435
- *                 self.index[qname].append(pos)
- * 
- *         bam_destroy1(b)             # <<<<<<<<<<<<<<
- * 
- *     def find(self, qname):
- */
-  bam_destroy1(__pyx_v_b);
-
-  /* "pysam/csamfile.pyx":3414
- *         self.fp = self.htsfile.fp.bgzf
- * 
- *     def build(self):             # <<<<<<<<<<<<<<
- *         '''build index.'''
- * 
- */
-
-  /* function exit code */
-  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_AddTraceback("pysam.csamfile.IndexedReads.build", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XDECREF(__pyx_v_qname);
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3437
- *         bam_destroy1(b)
- * 
- *     def find(self, qname):             # <<<<<<<<<<<<<<
- *         if qname in self.index:
- *             return IteratorRowSelection(
- */
-
-/* Python wrapper */
-static PyObject *__pyx_pw_5pysam_8csamfile_12IndexedReads_5find(PyObject *__pyx_v_self, PyObject *__pyx_v_qname); /*proto*/
-static char __pyx_doc_5pysam_8csamfile_12IndexedReads_4find[] = "IndexedReads.find(self, qname)";
-static PyObject *__pyx_pw_5pysam_8csamfile_12IndexedReads_5find(PyObject *__pyx_v_self, PyObject *__pyx_v_qname) {
-  PyObject *__pyx_r = 0;
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("find (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_8csamfile_12IndexedReads_4find(((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)__pyx_v_self), ((PyObject *)__pyx_v_qname));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-static PyObject *__pyx_pf_5pysam_8csamfile_12IndexedReads_4find(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self, PyObject *__pyx_v_qname) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  int __pyx_t_2;
-  PyObject *__pyx_t_3 = NULL;
-  PyObject *__pyx_t_4 = NULL;
-  PyObject *__pyx_t_5 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("find", 0);
-  __Pyx_TraceCall("find", __pyx_f[0], 3437);
-
-  /* "pysam/csamfile.pyx":3438
- * 
- *     def find(self, qname):
- *         if qname in self.index:             # <<<<<<<<<<<<<<
- *             return IteratorRowSelection(
- *                 self.samfile,
- */
-  __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_v_qname, __pyx_v_self->index, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3438; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = (__pyx_t_1 != 0);
-  if (__pyx_t_2) {
-
-    /* "pysam/csamfile.pyx":3439
- *     def find(self, qname):
- *         if qname in self.index:
- *             return IteratorRowSelection(             # <<<<<<<<<<<<<<
- *                 self.samfile,
- *                 self.index[qname],
- */
-    __Pyx_XDECREF(__pyx_r);
-
-    /* "pysam/csamfile.pyx":3441
- *             return IteratorRowSelection(
- *                 self.samfile,
- *                 self.index[qname],             # <<<<<<<<<<<<<<
- *                 reopen = False)
- *         else:
- */
-    __pyx_t_3 = PyObject_GetItem(__pyx_v_self->index, __pyx_v_qname); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3441; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-    __Pyx_GOTREF(__pyx_t_3);
-
-    /* "pysam/csamfile.pyx":3439
- *     def find(self, qname):
- *         if qname in self.index:
- *             return IteratorRowSelection(             # <<<<<<<<<<<<<<
- *                 self.samfile,
- *                 self.index[qname],
- */
-    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_INCREF(((PyObject *)__pyx_v_self->samfile));
-    PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_v_self->samfile));
-    __Pyx_GIVEREF(((PyObject *)__pyx_v_self->samfile));
-    PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    __pyx_t_3 = 0;
-    __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-
-    /* "pysam/csamfile.pyx":3442
- *                 self.samfile,
- *                 self.index[qname],
- *                 reopen = False)             # <<<<<<<<<<<<<<
- *         else:
- *             raise KeyError("read %s not found" % qname)
- */
-    if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_reopen, Py_False) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-    /* "pysam/csamfile.pyx":3439
- *     def find(self, qname):
- *         if qname in self.index:
- *             return IteratorRowSelection(             # <<<<<<<<<<<<<<
- *                 self.samfile,
- *                 self.index[qname],
- */
-    __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_8csamfile_IteratorRowSelection)), __pyx_t_4, __pyx_t_3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_r = __pyx_t_5;
-    __pyx_t_5 = 0;
-    goto __pyx_L0;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":3444
- *                 reopen = False)
- *         else:
- *             raise KeyError("read %s not found" % qname)             # <<<<<<<<<<<<<<
- * 
- *     def __dealloc__(self):
- */
-    __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_read_s_not_found, __pyx_v_qname); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_5);
-    __Pyx_GIVEREF(__pyx_t_5);
-    __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __Pyx_Raise(__pyx_t_5, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/csamfile.pyx":3437
- *         bam_destroy1(b)
- * 
- *     def find(self, qname):             # <<<<<<<<<<<<<<
- *         if qname in self.index:
- *             return IteratorRowSelection(
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_3);
-  __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_AddTraceback("pysam.csamfile.IndexedReads.find", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = NULL;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_TraceReturn(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/csamfile.pyx":3446
- *             raise KeyError("read %s not found" % qname)
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         if self.owns_samfile:
- *             hts_close(self.htsfile)
- */
-
-/* Python wrapper */
-static void __pyx_pw_5pysam_8csamfile_12IndexedReads_7__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_8csamfile_12IndexedReads_7__dealloc__(PyObject *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_8csamfile_12IndexedReads_6__dealloc__(((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)__pyx_v_self));
-
-  /* function exit code */
-  __Pyx_RefNannyFinishContext();
-}
-
-static void __pyx_pf_5pysam_8csamfile_12IndexedReads_6__dealloc__(struct __pyx_obj_5pysam_8csamfile_IndexedReads *__pyx_v_self) {
-  __Pyx_RefNannyDeclarations
-  int __pyx_t_1;
-  __Pyx_TraceDeclarations
-  __Pyx_RefNannySetupContext("__dealloc__", 0);
-  __Pyx_TraceCall("__dealloc__", __pyx_f[0], 3446);
-
-  /* "pysam/csamfile.pyx":3447
- * 
- *     def __dealloc__(self):
- *         if self.owns_samfile:             # <<<<<<<<<<<<<<
- *             hts_close(self.htsfile)
- * 
- */
-  __pyx_t_1 = (__pyx_v_self->owns_samfile != 0);
-  if (__pyx_t_1) {
-
-    /* "pysam/csamfile.pyx":3448
- *     def __dealloc__(self):
- *         if self.owns_samfile:
- *             hts_close(self.htsfile)             # <<<<<<<<<<<<<<
- * 
- * __all__ = ["Samfile",
- */
-    hts_close(__pyx_v_self->htsfile);
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":3446
- *             raise KeyError("read %s not found" % qname)
- * 
- *     def __dealloc__(self):             # <<<<<<<<<<<<<<
- *         if self.owns_samfile:
- *             hts_close(self.htsfile)
- */
-
-  /* function exit code */
-  __Pyx_TraceReturn(Py_None);
-  __Pyx_RefNannyFinishContext();
-}
-static struct __pyx_vtabstruct_5pysam_8csamfile_AlignedRead __pyx_vtable_5pysam_8csamfile_AlignedRead;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_AlignedRead(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_AlignedRead *p;
-  PyObject *o;
-  if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
-    o = (*t->tp_alloc)(t, 0);
-  } else {
-    o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
-  }
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_AlignedRead;
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_AlignedRead(PyObject *o) {
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  {
-    PyObject *etype, *eval, *etb;
-    PyErr_Fetch(&etype, &eval, &etb);
-    ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_11AlignedRead_3__dealloc__(o);
-    --Py_REFCNT(o);
-    PyErr_Restore(etype, eval, etb);
-  }
-  (*Py_TYPE(o)->tp_free)(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qname(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_qname(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5qname_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_cigar(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigar(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5cigar_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_cigarstring(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigarstring(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_11cigarstring_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_seq(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_seq(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_3seq_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qual(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_qual(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4qual_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_query(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5query_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qqual(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5qqual_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qstart(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_6qstart_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qend(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4qend_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_qlen(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4qlen_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_tags(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_tags(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4tags_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_flag(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_flag(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4flag_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_rname(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_rname(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5rname_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_tid(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_tid(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_3tid_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_pos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_pos(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_3pos_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_bin(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_bin(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_3bin_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_rlen(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4rlen_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_aend(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4aend_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_alen(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4alen_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mapq(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mapq(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mapq_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mrnm(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mrnm(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mrnm_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_rnext(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_rnext(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5rnext_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mpos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mpos(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4mpos_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_pnext(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_pnext(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5pnext_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_isize(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_isize(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_5isize_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_tlen(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_tlen(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_4tlen_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_paired(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_paired(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_paired_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_proper_pair(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_proper_pair(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_14is_proper_pair_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_unmapped(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_unmapped(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_11is_unmapped_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mate_is_unmapped(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_unmapped(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_16mate_is_unmapped_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_reverse(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_reverse(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_10is_reverse_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_mate_is_reverse(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_reverse(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_15mate_is_reverse_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_read1(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read1(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read1_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_read2(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read2(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_8is_read2_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_secondary(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_secondary(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_secondary_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_qcfail(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_qcfail(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_9is_qcfail_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_is_duplicate(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_duplicate(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11AlignedRead_12is_duplicate_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_positions(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_9positions_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_inferred_length(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_15inferred_length_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_aligned_pairs(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_13aligned_pairs_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11AlignedRead_blocks(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11AlignedRead_6blocks_1__get__(o);
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_AlignedRead[] = {
-  {__Pyx_NAMESTR("compare"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_7compare, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_6compare)},
-  {__Pyx_NAMESTR("_convert_python_tag"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_11_convert_python_tag, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_10_convert_python_tag)},
-  {__Pyx_NAMESTR("setTag"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_13setTag, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_12setTag)},
-  {__Pyx_NAMESTR("overlap"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_15overlap, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_14overlap)},
-  {__Pyx_NAMESTR("opt"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_17opt, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_16opt)},
-  {__Pyx_NAMESTR("fancy_str"), (PyCFunction)__pyx_pw_5pysam_8csamfile_11AlignedRead_19fancy_str, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_11AlignedRead_18fancy_str)},
-  {0, 0, 0, 0}
-};
-
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_AlignedRead[] = {
-  {(char *)"qname", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qname, __pyx_setprop_5pysam_8csamfile_11AlignedRead_qname, __Pyx_DOCSTR(__pyx_k_the_query_name_None_if_not_prese), 0},
-  {(char *)"cigar", __pyx_getprop_5pysam_8csamfile_11AlignedRead_cigar, __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigar, __Pyx_DOCSTR(__pyx_k_the_term_cigar_alignment_The_ali), 0},
-  {(char *)"cigarstring", __pyx_getprop_5pysam_8csamfile_11AlignedRead_cigarstring, __pyx_setprop_5pysam_8csamfile_11AlignedRead_cigarstring, __Pyx_DOCSTR(__pyx_k_the_term_cigar_alignment_as_a_st), 0},
-  {(char *)"seq", __pyx_getprop_5pysam_8csamfile_11AlignedRead_seq, __pyx_setprop_5pysam_8csamfile_11AlignedRead_seq, __Pyx_DOCSTR(__pyx_k_read_sequence_bases_including_te), 0},
-  {(char *)"qual", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qual, __pyx_setprop_5pysam_8csamfile_11AlignedRead_qual, __Pyx_DOCSTR(__pyx_k_read_sequence_base_qualities_inc), 0},
-  {(char *)"query", __pyx_getprop_5pysam_8csamfile_11AlignedRead_query, 0, __Pyx_DOCSTR(__pyx_k_aligned_portion_of_the_read_This), 0},
-  {(char *)"qqual", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qqual, 0, __Pyx_DOCSTR(__pyx_k_aligned_query_sequence_quality_v), 0},
-  {(char *)"qstart", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qstart, 0, __Pyx_DOCSTR(__pyx_k_start_index_of_the_aligned_query), 0},
-  {(char *)"qend", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qend, 0, __Pyx_DOCSTR(__pyx_k_end_index_of_the_aligned_query_p), 0},
-  {(char *)"qlen", __pyx_getprop_5pysam_8csamfile_11AlignedRead_qlen, 0, __Pyx_DOCSTR(__pyx_k_length_of_the_aligned_query_sequ), 0},
-  {(char *)"tags", __pyx_getprop_5pysam_8csamfile_11AlignedRead_tags, __pyx_setprop_5pysam_8csamfile_11AlignedRead_tags, __Pyx_DOCSTR(__pyx_k_the_tags_in_the_AUX_field_This_p), 0},
-  {(char *)"flag", __pyx_getprop_5pysam_8csamfile_11AlignedRead_flag, __pyx_setprop_5pysam_8csamfile_11AlignedRead_flag, __Pyx_DOCSTR(__pyx_k_properties_flag), 0},
-  {(char *)"rname", __pyx_getprop_5pysam_8csamfile_11AlignedRead_rname, __pyx_setprop_5pysam_8csamfile_11AlignedRead_rname, __Pyx_DOCSTR(__pyx_k_term_target_ID_DEPRECATED_from), 0},
-  {(char *)"tid", __pyx_getprop_5pysam_8csamfile_11AlignedRead_tid, __pyx_setprop_5pysam_8csamfile_11AlignedRead_tid, __Pyx_DOCSTR(__pyx_k_term_target_ID_note_This_field), 0},
-  {(char *)"pos", __pyx_getprop_5pysam_8csamfile_11AlignedRead_pos, __pyx_setprop_5pysam_8csamfile_11AlignedRead_pos, __Pyx_DOCSTR(__pyx_k_0_based_leftmost_coordinate), 0},
-  {(char *)"bin", __pyx_getprop_5pysam_8csamfile_11AlignedRead_bin, __pyx_setprop_5pysam_8csamfile_11AlignedRead_bin, __Pyx_DOCSTR(__pyx_k_properties_bin), 0},
-  {(char *)"rlen", __pyx_getprop_5pysam_8csamfile_11AlignedRead_rlen, 0, __Pyx_DOCSTR(__pyx_k_length_of_the_read_This_includes), 0},
-  {(char *)"aend", __pyx_getprop_5pysam_8csamfile_11AlignedRead_aend, 0, __Pyx_DOCSTR(__pyx_k_aligned_reference_position_of_th), 0},
-  {(char *)"alen", __pyx_getprop_5pysam_8csamfile_11AlignedRead_alen, 0, __Pyx_DOCSTR(__pyx_k_aligned_length_of_the_read_on_th), 0},
-  {(char *)"mapq", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mapq, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mapq, __Pyx_DOCSTR(__pyx_k_mapping_quality_2), 0},
-  {(char *)"mrnm", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mrnm, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mrnm, __Pyx_DOCSTR(__pyx_k_the_term_reference_id_of_the_mat), 0},
-  {(char *)"rnext", __pyx_getprop_5pysam_8csamfile_11AlignedRead_rnext, __pyx_setprop_5pysam_8csamfile_11AlignedRead_rnext, __Pyx_DOCSTR(__pyx_k_the_term_reference_id_of_the_mat_2), 0},
-  {(char *)"mpos", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mpos, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mpos, __Pyx_DOCSTR(__pyx_k_the_position_of_the_mate_depreca), 0},
-  {(char *)"pnext", __pyx_getprop_5pysam_8csamfile_11AlignedRead_pnext, __pyx_setprop_5pysam_8csamfile_11AlignedRead_pnext, __Pyx_DOCSTR(__pyx_k_the_position_of_the_mate), 0},
-  {(char *)"isize", __pyx_getprop_5pysam_8csamfile_11AlignedRead_isize, __pyx_setprop_5pysam_8csamfile_11AlignedRead_isize, __Pyx_DOCSTR(__pyx_k_the_insert_size_deprecated_use_t), 0},
-  {(char *)"tlen", __pyx_getprop_5pysam_8csamfile_11AlignedRead_tlen, __pyx_setprop_5pysam_8csamfile_11AlignedRead_tlen, __Pyx_DOCSTR(__pyx_k_the_template_length), 0},
-  {(char *)"is_paired", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_paired, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_paired, __Pyx_DOCSTR(__pyx_k_true_if_read_is_paired_in_sequen), 0},
-  {(char *)"is_proper_pair", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_proper_pair, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_proper_pair, __Pyx_DOCSTR(__pyx_k_true_if_read_is_mapped_in_a_prop), 0},
-  {(char *)"is_unmapped", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_unmapped, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_unmapped, __Pyx_DOCSTR(__pyx_k_true_if_read_itself_is_unmapped), 0},
-  {(char *)"mate_is_unmapped", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mate_is_unmapped, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_unmapped, __Pyx_DOCSTR(__pyx_k_true_if_the_mate_is_unmapped), 0},
-  {(char *)"is_reverse", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_reverse, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_reverse, __Pyx_DOCSTR(__pyx_k_true_if_read_is_mapped_to_revers), 0},
-  {(char *)"mate_is_reverse", __pyx_getprop_5pysam_8csamfile_11AlignedRead_mate_is_reverse, __pyx_setprop_5pysam_8csamfile_11AlignedRead_mate_is_reverse, __Pyx_DOCSTR(__pyx_k_true_is_read_is_mapped_to_revers), 0},
-  {(char *)"is_read1", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_read1, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read1, __Pyx_DOCSTR(__pyx_k_true_if_this_is_read1), 0},
-  {(char *)"is_read2", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_read2, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_read2, __Pyx_DOCSTR(__pyx_k_true_if_this_is_read2), 0},
-  {(char *)"is_secondary", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_secondary, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_secondary, __Pyx_DOCSTR(__pyx_k_true_if_not_primary_alignment), 0},
-  {(char *)"is_qcfail", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_qcfail, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_qcfail, __Pyx_DOCSTR(__pyx_k_true_if_QC_failure), 0},
-  {(char *)"is_duplicate", __pyx_getprop_5pysam_8csamfile_11AlignedRead_is_duplicate, __pyx_setprop_5pysam_8csamfile_11AlignedRead_is_duplicate, __Pyx_DOCSTR(__pyx_k_true_if_optical_or_PCR_duplicate), 0},
-  {(char *)"positions", __pyx_getprop_5pysam_8csamfile_11AlignedRead_positions, 0, __Pyx_DOCSTR(__pyx_k_a_list_of_reference_positions_th), 0},
-  {(char *)"inferred_length", __pyx_getprop_5pysam_8csamfile_11AlignedRead_inferred_length, 0, __Pyx_DOCSTR(__pyx_k_inferred_read_length_from_CIGAR), 0},
-  {(char *)"aligned_pairs", __pyx_getprop_5pysam_8csamfile_11AlignedRead_aligned_pairs, 0, __Pyx_DOCSTR(__pyx_k_a_list_of_aligned_read_and_refer), 0},
-  {(char *)"blocks", __pyx_getprop_5pysam_8csamfile_11AlignedRead_blocks, 0, __Pyx_DOCSTR(__pyx_k_a_list_of_start_and_end_positio), 0},
-  {0, 0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_AlignedRead = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.AlignedRead"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_AlignedRead), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_AlignedRead, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  __pyx_pw_5pysam_8csamfile_11AlignedRead_9__hash__, /*tp_hash*/
-  0, /*tp_call*/
-  __pyx_pw_5pysam_8csamfile_11AlignedRead_5__str__, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("AlignedRead()\n\n    Class representing an aligned read. See the SAM format specification for\n    the meaning of fields (http://samtools.sourceforge.net/).\n\n    This class stores a handle to the samtools C-structure representing\n    an aligned read. Member read access is forwarded to the C-structure\n    and converted into python objects. This implementation should be fast,\n    as only the data needed is converted.\n\n    For write access, the C-structure is updated  [...]
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  0, /*tp_iter*/
-  0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_AlignedRead, /*tp_methods*/
-  0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_AlignedRead, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_11AlignedRead_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_AlignedRead, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-static struct __pyx_vtabstruct_5pysam_8csamfile_Samfile __pyx_vtable_5pysam_8csamfile_Samfile;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_Samfile(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *p;
-  PyObject *o;
-  if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
-    o = (*t->tp_alloc)(t, 0);
-  } else {
-    o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
-  }
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_Samfile;
-  p->_filename = Py_None; Py_INCREF(Py_None);
-  if (unlikely(__pyx_pw_5pysam_8csamfile_7Samfile_1__cinit__(o, a, k) < 0)) {
-    Py_DECREF(o); o = 0;
-  }
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_Samfile(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_Samfile *p = (struct __pyx_obj_5pysam_8csamfile_Samfile *)o;
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  PyObject_GC_UnTrack(o);
-  {
-    PyObject *etype, *eval, *etb;
-    PyErr_Fetch(&etype, &eval, &etb);
-    ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_7Samfile_33__dealloc__(o);
-    --Py_REFCNT(o);
-    PyErr_Restore(etype, eval, etb);
-  }
-  Py_CLEAR(p->_filename);
-  (*Py_TYPE(o)->tp_free)(o);
-}
-
-static int __pyx_tp_traverse_5pysam_8csamfile_Samfile(PyObject *o, visitproc v, void *a) {
-  int e;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *p = (struct __pyx_obj_5pysam_8csamfile_Samfile *)o;
-  if (p->_filename) {
-    e = (*v)(p->_filename, a); if (e) return e;
-  }
-  return 0;
-}
-
-static int __pyx_tp_clear_5pysam_8csamfile_Samfile(PyObject *o) {
-  PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_Samfile *p = (struct __pyx_obj_5pysam_8csamfile_Samfile *)o;
-  tmp = ((PyObject*)p->_filename);
-  p->_filename = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  return 0;
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_filename(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_8filename_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_nreferences(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_11nreferences_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_references(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_10references_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_lengths(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_7lengths_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_mapped(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_6mapped_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_unmapped(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_8unmapped_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_nocoordinate(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_12nocoordinate_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_text(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_4text_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7Samfile_header(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7Samfile_6header_1__get__(o);
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_Samfile[] = {
-  {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_3_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_2_isOpen)},
-  {__Pyx_NAMESTR("_hasIndex"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_5_hasIndex, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_4_hasIndex)},
-  {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_7_open, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_6_open)},
-  {__Pyx_NAMESTR("gettid"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_9gettid, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_8gettid)},
-  {__Pyx_NAMESTR("getrname"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_11getrname, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_10getrname)},
-  {__Pyx_NAMESTR("_parseRegion"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_13_parseRegion, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_12_parseRegion)},
-  {__Pyx_NAMESTR("reset"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_15reset, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_14reset)},
-  {__Pyx_NAMESTR("seek"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_17seek, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_16seek)},
-  {__Pyx_NAMESTR("tell"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_19tell, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_18tell)},
-  {__Pyx_NAMESTR("fetch"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_21fetch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_20fetch)},
-  {__Pyx_NAMESTR("head"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_23head, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_22head)},
-  {__Pyx_NAMESTR("mate"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_25mate, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_24mate)},
-  {__Pyx_NAMESTR("count"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_27count, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_26count)},
-  {__Pyx_NAMESTR("pileup"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_29pileup, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_28pileup)},
-  {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_31close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_30close)},
-  {__Pyx_NAMESTR("write"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_35write, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_34write)},
-  {__Pyx_NAMESTR("__enter__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_37__enter__, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_36__enter__)},
-  {__Pyx_NAMESTR("__exit__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_39__exit__, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_38__exit__)},
-  {__Pyx_NAMESTR("_checkIndex"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_41_checkIndex, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_40_checkIndex)},
-  {__Pyx_NAMESTR("_buildLine"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_43_buildLine, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_42_buildLine)},
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_7Samfile_47__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_7Samfile_46__next__)},
-  {0, 0, 0, 0}
-};
-
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_Samfile[] = {
-  {(char *)"filename", __pyx_getprop_5pysam_8csamfile_7Samfile_filename, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_filename_associat), 0},
-  {(char *)"nreferences", __pyx_getprop_5pysam_8csamfile_7Samfile_nreferences, 0, __Pyx_DOCSTR(__pyx_k_number_of_term_reference_sequenc), 0},
-  {(char *)"references", __pyx_getprop_5pysam_8csamfile_7Samfile_references, 0, __Pyx_DOCSTR(__pyx_k_tuple_with_the_names_of_term_ref), 0},
-  {(char *)"lengths", __pyx_getprop_5pysam_8csamfile_7Samfile_lengths, 0, __Pyx_DOCSTR(__pyx_k_tuple_of_the_lengths_of_the_term), 0},
-  {(char *)"mapped", __pyx_getprop_5pysam_8csamfile_7Samfile_mapped, 0, __Pyx_DOCSTR(__pyx_k_total_number_of_mapped_alignment), 0},
-  {(char *)"unmapped", __pyx_getprop_5pysam_8csamfile_7Samfile_unmapped, 0, __Pyx_DOCSTR(__pyx_k_total_number_of_unmapped_reads_i), 0},
-  {(char *)"nocoordinate", __pyx_getprop_5pysam_8csamfile_7Samfile_nocoordinate, 0, __Pyx_DOCSTR(__pyx_k_total_number_of_reads_without_co), 0},
-  {(char *)"text", __pyx_getprop_5pysam_8csamfile_7Samfile_text, 0, __Pyx_DOCSTR(__pyx_k_full_contents_of_the_term_sam_fi), 0},
-  {(char *)"header", __pyx_getprop_5pysam_8csamfile_7Samfile_header, 0, __Pyx_DOCSTR(__pyx_k_header_information_within_the_te), 0},
-  {0, 0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_Samfile = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.Samfile"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_Samfile), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_Samfile, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("*(filename, mode=None, template = None,\n         referencenames = None, referencelengths = None,\n         text = NULL, header = None,\n         add_sq_text = False, check_header = True,\n         check_sq = True )*\n\n    A :term:`SAM`/:term:`BAM` formatted file. The file is\n    automatically opened.\n\n    *mode* should be ``r`` for reading or ``w`` for writing. The\n    default is text mode (:term:`SAM`). For binary (:term:`BAM`) I/O\n    you should append ``b`` for  [...]
-  __pyx_tp_traverse_5pysam_8csamfile_Samfile, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_Samfile, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_7Samfile_45__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_7Samfile_47__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_Samfile, /*tp_methods*/
-  0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_Samfile, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  0, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_Samfile, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupProxy(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  PyObject *o;
-  if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
-    o = (*t->tp_alloc)(t, 0);
-  } else {
-    o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
-  }
-  if (unlikely(!o)) return 0;
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_PileupProxy(PyObject *o) {
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  (*Py_TYPE(o)->tp_free)(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11PileupProxy_tid(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11PileupProxy_3tid_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11PileupProxy_n(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11PileupProxy_1n_1__get__(o);
-}
-
-static int __pyx_setprop_5pysam_8csamfile_11PileupProxy_n(PyObject *o, PyObject *v, CYTHON_UNUSED void *x) {
-  if (v) {
-    return __pyx_pw_5pysam_8csamfile_11PileupProxy_1n_3__set__(o, v);
-  }
-  else {
-    PyErr_SetString(PyExc_NotImplementedError, "__del__");
-    return -1;
-  }
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11PileupProxy_pos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11PileupProxy_3pos_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_11PileupProxy_pileups(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_11PileupProxy_7pileups_1__get__(o);
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_PileupProxy[] = {
-  {0, 0, 0, 0}
-};
-
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_PileupProxy[] = {
-  {(char *)"tid", __pyx_getprop_5pysam_8csamfile_11PileupProxy_tid, 0, __Pyx_DOCSTR(__pyx_k_the_chromosome_ID_as_is_defined), 0},
-  {(char *)"n", __pyx_getprop_5pysam_8csamfile_11PileupProxy_n, __pyx_setprop_5pysam_8csamfile_11PileupProxy_n, __Pyx_DOCSTR(__pyx_k_number_of_reads_mapping_to_this), 0},
-  {(char *)"pos", __pyx_getprop_5pysam_8csamfile_11PileupProxy_pos, 0, 0, 0},
-  {(char *)"pileups", __pyx_getprop_5pysam_8csamfile_11PileupProxy_pileups, 0, __Pyx_DOCSTR(__pyx_k_list_of_reads_class_pysam_Pileup), 0},
-  {0, 0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_PileupProxy = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.PileupProxy"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_PileupProxy), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_PileupProxy, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  __pyx_pw_5pysam_8csamfile_11PileupProxy_3__str__, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("PileupProxy()\nA pileup column. A pileup column contains\n    all the reads that map to a certain target base.\n\n    tid\n        chromosome ID as is defined in the header\n    pos\n        the target base coordinate (0-based)\n    n\n        number of reads mapping to this column\n    pileups\n        list of reads (:class:`pysam.PileupRead`) aligned to this column\n\n    This class is a proxy for results returned by the samtools pileup engine.\n    If the underlying en [...]
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  0, /*tp_iter*/
-  0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_PileupProxy, /*tp_methods*/
-  0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_PileupProxy, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_11PileupProxy_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_PileupProxy, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_PileupRead(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *p;
-  PyObject *o;
-  if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
-    o = (*t->tp_alloc)(t, 0);
-  } else {
-    o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
-  }
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_PileupRead *)o);
-  p->_alignment = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)Py_None); Py_INCREF(Py_None);
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_PileupRead(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *p = (struct __pyx_obj_5pysam_8csamfile_PileupRead *)o;
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  PyObject_GC_UnTrack(o);
-  Py_CLEAR(p->_alignment);
-  (*Py_TYPE(o)->tp_free)(o);
-}
-
-static int __pyx_tp_traverse_5pysam_8csamfile_PileupRead(PyObject *o, visitproc v, void *a) {
-  int e;
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *p = (struct __pyx_obj_5pysam_8csamfile_PileupRead *)o;
-  if (p->_alignment) {
-    e = (*v)(((PyObject*)p->_alignment), a); if (e) return e;
-  }
-  return 0;
-}
-
-static int __pyx_tp_clear_5pysam_8csamfile_PileupRead(PyObject *o) {
-  PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_PileupRead *p = (struct __pyx_obj_5pysam_8csamfile_PileupRead *)o;
-  tmp = ((PyObject*)p->_alignment);
-  p->_alignment = ((struct __pyx_obj_5pysam_8csamfile_AlignedRead *)Py_None); Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  return 0;
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_alignment(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_9alignment_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_qpos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_4qpos_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_indel(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_5indel_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_is_del(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_6is_del_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_is_head(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_7is_head_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_is_tail(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_7is_tail_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_10PileupRead_level(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_10PileupRead_5level_1__get__(o);
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_PileupRead[] = {
-  {0, 0, 0, 0}
-};
-
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_PileupRead[] = {
-  {(char *)"alignment", __pyx_getprop_5pysam_8csamfile_10PileupRead_alignment, 0, __Pyx_DOCSTR(__pyx_k_a_class_pysam_AlignedRead_object), 0},
-  {(char *)"qpos", __pyx_getprop_5pysam_8csamfile_10PileupRead_qpos, 0, __Pyx_DOCSTR(__pyx_k_position_of_the_read_base_at_the), 0},
-  {(char *)"indel", __pyx_getprop_5pysam_8csamfile_10PileupRead_indel, 0, __Pyx_DOCSTR(__pyx_k_indel_length_0_for_no_indel_posi), 0},
-  {(char *)"is_del", __pyx_getprop_5pysam_8csamfile_10PileupRead_is_del, 0, __Pyx_DOCSTR(__pyx_k_1_iff_the_base_on_the_padded_rea), 0},
-  {(char *)"is_head", __pyx_getprop_5pysam_8csamfile_10PileupRead_is_head, 0, 0, 0},
-  {(char *)"is_tail", __pyx_getprop_5pysam_8csamfile_10PileupRead_is_tail, 0, 0, 0},
-  {(char *)"level", __pyx_getprop_5pysam_8csamfile_10PileupRead_level, 0, 0, 0},
-  {0, 0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_PileupRead = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.PileupRead"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_PileupRead), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_PileupRead, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  __pyx_pw_5pysam_8csamfile_10PileupRead_3__str__, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("PileupRead()\nA read aligned to a column.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_PileupRead, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_PileupRead, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  0, /*tp_iter*/
-  0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_PileupRead, /*tp_methods*/
-  0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_PileupRead, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_10PileupRead_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_PileupRead, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRow(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow *p;
-  PyObject *o;
-  if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
-    o = (*t->tp_alloc)(t, 0);
-  } else {
-    o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
-  }
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRow *)o);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRow *)o;
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  PyObject_GC_UnTrack(o);
-  {
-    PyObject *etype, *eval, *etb;
-    PyErr_Fetch(&etype, &eval, &etb);
-    ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_11IteratorRow_3__dealloc__(o);
-    --Py_REFCNT(o);
-    PyErr_Restore(etype, eval, etb);
-  }
-  Py_CLEAR(p->samfile);
-  (*Py_TYPE(o)->tp_free)(o);
-}
-
-static int __pyx_tp_traverse_5pysam_8csamfile_IteratorRow(PyObject *o, visitproc v, void *a) {
-  int e;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRow *)o;
-  if (p->samfile) {
-    e = (*v)(((PyObject*)p->samfile), a); if (e) return e;
-  }
-  return 0;
-}
-
-static int __pyx_tp_clear_5pysam_8csamfile_IteratorRow(PyObject *o) {
-  PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRow *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRow *)o;
-  tmp = ((PyObject*)p->samfile);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  return 0;
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRow[] = {
-  {0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRow = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRow"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRow), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRow(Samfile samfile, int reopen=True)\nabstract base class for iterators over mapped reads.\n\n    Various iterators implement different behaviours for wrapping around\n    contig boundaries. Examples include:\n\n    :class:`pysam.IteratorRowRegion`\n        iterate within a single contig and a defined region.\n\n    :class:`pysam.IteratorRowAll`\n        iterate until EOF. This iterator will also include unmapped reads.\n\n    :class:`pysam.IteratorRowAllRefs`\n  [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRow, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  0, /*tp_iter*/
-  0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRow, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_11IteratorRow_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRow, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowRegion __pyx_vtable_5pysam_8csamfile_IteratorRowRegion;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowRegion(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorRowRegion;
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowRegion(PyObject *o) {
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  PyObject_GC_UnTrack(o);
-  {
-    PyObject *etype, *eval, *etb;
-    PyErr_Fetch(&etype, &eval, &etb);
-    ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_7__dealloc__(o);
-    --Py_REFCNT(o);
-    PyErr_Restore(etype, eval, etb);
-  }
-  PyObject_GC_Track(o);
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(o);
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowRegion[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_17IteratorRowRegion_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_17IteratorRowRegion_4__next__)},
-  {0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowRegion = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowRegion"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowRegion, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowRegion(Samfile samfile, int tid, int beg, int end, int reopen=True)\n*(Samfile samfile, int tid, int beg, int end, int reopen = True )*\n\n    iterate over mapped reads in a region.\n\n    By default, the file is re-openend to avoid conflicts between\n    multiple iterators working on the same file. Set *reopen* = False\n    to not re-open *samfile*.\n\n    The samtools iterators assume that the file\n    position between iterations do not change.\n    As a con [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRow, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_3__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_5__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowRegion, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_17IteratorRowRegion_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowRegion, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowHead __pyx_vtable_5pysam_8csamfile_IteratorRowHead;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowHead(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorRowHead;
-  return o;
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowHead[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_15IteratorRowHead_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_15IteratorRowHead_4__next__)},
-  {0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowHead = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowHead"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowHead(Samfile samfile, int n, int reopen=True)\n*(Samfile samfile, n, int reopen = True)*\n\n    iterate over first n reads in *samfile*\n\n    By default, the file is re-openend to avoid conflicts between\n    multiple iterators working on the same file. Set *reopen* = False\n    to not re-open *samfile*.\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`S [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRow, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_15IteratorRowHead_3__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_15IteratorRowHead_5__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowHead, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_15IteratorRowHead_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowHead, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowAll __pyx_vtable_5pysam_8csamfile_IteratorRowAll;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowAll(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorRowAll;
-  return o;
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowAll[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_14IteratorRowAll_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_14IteratorRowAll_4__next__)},
-  {0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAll = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowAll"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowAll(Samfile samfile, int reopen=True)\n*(Samfile samfile, int reopen = True)*\n\n    iterate over all reads in *samfile*\n\n    By default, the file is re-openend to avoid conflicts between\n    multiple iterators working on the same file. Set *reopen* = False\n    to not re-open *samfile*.\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.\ [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRow, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_14IteratorRowAll_3__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_14IteratorRowAll_5__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowAll, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_14IteratorRowAll_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowAll, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowAllRefs(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)o);
-  p->rowiter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowAllRefs(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)o;
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  PyObject_GC_UnTrack(o);
-  Py_CLEAR(p->rowiter);
-  PyObject_GC_Track(o);
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(o);
-}
-
-static int __pyx_tp_traverse_5pysam_8csamfile_IteratorRowAllRefs(PyObject *o, visitproc v, void *a) {
-  int e;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)o;
-  e = __pyx_tp_traverse_5pysam_8csamfile_IteratorRow(o, v, a); if (e) return e;
-  if (p->rowiter) {
-    e = (*v)(((PyObject*)p->rowiter), a); if (e) return e;
-  }
-  return 0;
-}
-
-static int __pyx_tp_clear_5pysam_8csamfile_IteratorRowAllRefs(PyObject *o) {
-  PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs *)o;
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow(o);
-  tmp = ((PyObject*)p->rowiter);
-  p->rowiter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  return 0;
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowAllRefs[] = {
-  {__Pyx_NAMESTR("nextiter"), (PyCFunction)__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_3nextiter, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_2nextiter)},
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_7__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_6__next__)},
-  {0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowAllRefs = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowAllRefs"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowAllRefs), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowAllRefs, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowAllRefs(Samfile samfile, reopen=True)\niterates over all mapped reads by chaining iterators over each reference\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRowAllRefs, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRowAllRefs, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_5__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_7__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowAllRefs, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_18IteratorRowAllRefs_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowAllRefs, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorRowSelection __pyx_vtable_5pysam_8csamfile_IteratorRowSelection;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorRowSelection(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorRow(t, a, k);
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorRowSelection;
-  p->positions = Py_None; Py_INCREF(Py_None);
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowSelection(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)o;
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  PyObject_GC_UnTrack(o);
-  Py_CLEAR(p->positions);
-  PyObject_GC_Track(o);
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRow(o);
-}
-
-static int __pyx_tp_traverse_5pysam_8csamfile_IteratorRowSelection(PyObject *o, visitproc v, void *a) {
-  int e;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)o;
-  e = __pyx_tp_traverse_5pysam_8csamfile_IteratorRow(o, v, a); if (e) return e;
-  if (p->positions) {
-    e = (*v)(p->positions, a); if (e) return e;
-  }
-  return 0;
-}
-
-static int __pyx_tp_clear_5pysam_8csamfile_IteratorRowSelection(PyObject *o) {
-  PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *p = (struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *)o;
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRow(o);
-  tmp = ((PyObject*)p->positions);
-  p->positions = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  return 0;
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorRowSelection[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_20IteratorRowSelection_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_20IteratorRowSelection_4__next__)},
-  {0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorRowSelection = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorRowSelection"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorRowSelection, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IteratorRowSelection(Samfile samfile, positions, int reopen=True)\n*(Samfile samfile)*\n\n    iterate over reads in *samfile* at a given list of file positions.\n\n    .. note::\n        It is usually not necessary to create an object of this class\n        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorRowSelection, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorRowSelection, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_3__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_5__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorRowSelection, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_20IteratorRowSelection_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorRowSelection, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn __pyx_vtable_5pysam_8csamfile_IteratorColumn;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumn(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn *p;
-  PyObject *o;
-  if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
-    o = (*t->tp_alloc)(t, 0);
-  } else {
-    o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
-  }
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)o);
-  p->__pyx_vtab = __pyx_vtabptr_5pysam_8csamfile_IteratorColumn;
-  p->iter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
-  p->fastafile = ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)Py_None); Py_INCREF(Py_None);
-  p->stepper = Py_None; Py_INCREF(Py_None);
-  if (unlikely(__pyx_pw_5pysam_8csamfile_14IteratorColumn_1__cinit__(o, a, k) < 0)) {
-    Py_DECREF(o); o = 0;
-  }
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn *p = (struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)o;
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  PyObject_GC_UnTrack(o);
-  {
-    PyObject *etype, *eval, *etb;
-    PyErr_Fetch(&etype, &eval, &etb);
-    ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_14IteratorColumn_9__dealloc__(o);
-    --Py_REFCNT(o);
-    PyErr_Restore(etype, eval, etb);
-  }
-  Py_CLEAR(p->iter);
-  Py_CLEAR(p->samfile);
-  Py_CLEAR(p->fastafile);
-  Py_CLEAR(p->stepper);
-  (*Py_TYPE(o)->tp_free)(o);
-}
-
-static int __pyx_tp_traverse_5pysam_8csamfile_IteratorColumn(PyObject *o, visitproc v, void *a) {
-  int e;
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn *p = (struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)o;
-  if (p->iter) {
-    e = (*v)(((PyObject*)p->iter), a); if (e) return e;
-  }
-  if (p->samfile) {
-    e = (*v)(((PyObject*)p->samfile), a); if (e) return e;
-  }
-  if (p->fastafile) {
-    e = (*v)(((PyObject*)p->fastafile), a); if (e) return e;
-  }
-  if (p->stepper) {
-    e = (*v)(p->stepper, a); if (e) return e;
-  }
-  return 0;
-}
-
-static int __pyx_tp_clear_5pysam_8csamfile_IteratorColumn(PyObject *o) {
-  PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumn *p = (struct __pyx_obj_5pysam_8csamfile_IteratorColumn *)o;
-  tmp = ((PyObject*)p->iter);
-  p->iter = ((struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *)Py_None); Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->samfile);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->fastafile);
-  p->fastafile = ((struct __pyx_obj_5pysam_6cfaidx_Fastafile *)Py_None); Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->stepper);
-  p->stepper = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  return 0;
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_14IteratorColumn_seq_len(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_14IteratorColumn_7seq_len_1__get__(o);
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorColumn[] = {
-  {__Pyx_NAMESTR("addReference"), (PyCFunction)__pyx_pw_5pysam_8csamfile_14IteratorColumn_5addReference, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_14IteratorColumn_4addReference)},
-  {__Pyx_NAMESTR("hasReference"), (PyCFunction)__pyx_pw_5pysam_8csamfile_14IteratorColumn_7hasReference, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_14IteratorColumn_6hasReference)},
-  {0, 0, 0, 0}
-};
-
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_IteratorColumn[] = {
-  {(char *)"seq_len", __pyx_getprop_5pysam_8csamfile_14IteratorColumn_seq_len, 0, __Pyx_DOCSTR(__pyx_k_current_sequence_length), 0},
-  {0, 0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumn = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorColumn"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorColumn), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("abstract base class for iterators over columns.\n\n    IteratorColumn objects wrap the pileup functionality of samtools.\n\n    For reasons of efficiency, the iterator points to the current\n    pileup buffer. The pileup buffer is updated at every iteration.\n    This might cause some unexpected behavious. For example,\n    consider the conversion to a list::\n\n       f = Samfile(\"file.bam\", \"rb\")\n       result = list( f.pileup() )\n\n    Here, ``result`` will conta [...]
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorColumn, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorColumn, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__, /*tp_iter*/
-  0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorColumn, /*tp_methods*/
-  0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_IteratorColumn, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  0, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorColumn, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnRegion __pyx_vtable_5pysam_8csamfile_IteratorColumnRegion;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumnRegion(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorColumn(t, a, k);
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion *)o);
-  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn*)__pyx_vtabptr_5pysam_8csamfile_IteratorColumnRegion;
-  if (unlikely(__pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_1__cinit__(o, a, k) < 0)) {
-    Py_DECREF(o); o = 0;
-  }
-  return o;
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorColumnRegion[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_3__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_20IteratorColumnRegion_2__next__)},
-  {0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnRegion = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorColumnRegion"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorColumnRegion), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("iterates over a region only.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorColumn, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorColumn, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  #if CYTHON_COMPILING_IN_PYPY
-  __pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__, /*tp_iter*/
-  #else
-  0, /*tp_iter*/
-  #endif
-  __pyx_pw_5pysam_8csamfile_20IteratorColumnRegion_3__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorColumnRegion, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  0, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorColumnRegion, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-static struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumnAllRefs __pyx_vtable_5pysam_8csamfile_IteratorColumnAllRefs;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IteratorColumnAllRefs(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *p;
-  PyObject *o = __pyx_tp_new_5pysam_8csamfile_IteratorColumn(t, a, k);
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs *)o);
-  p->__pyx_base.__pyx_vtab = (struct __pyx_vtabstruct_5pysam_8csamfile_IteratorColumn*)__pyx_vtabptr_5pysam_8csamfile_IteratorColumnAllRefs;
-  if (unlikely(__pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_1__cinit__(o, a, k) < 0)) {
-    Py_DECREF(o); o = 0;
-  }
-  return o;
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IteratorColumnAllRefs[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_3__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__)},
-  {0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IteratorColumnAllRefs = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IteratorColumnAllRefs"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IteratorColumnAllRefs), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IteratorColumn, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("iterates over all columns by chaining iterators over each reference\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IteratorColumn, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IteratorColumn, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  #if CYTHON_COMPILING_IN_PYPY
-  __pyx_pw_5pysam_8csamfile_14IteratorColumn_3__iter__, /*tp_iter*/
-  #else
-  0, /*tp_iter*/
-  #endif
-  __pyx_pw_5pysam_8csamfile_21IteratorColumnAllRefs_3__next__, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IteratorColumnAllRefs, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  0, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IteratorColumnAllRefs, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_IndexedReads(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  struct __pyx_obj_5pysam_8csamfile_IndexedReads *p;
-  PyObject *o;
-  if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
-    o = (*t->tp_alloc)(t, 0);
-  } else {
-    o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
-  }
-  if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_8csamfile_IndexedReads *)o);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
-  p->index = Py_None; Py_INCREF(Py_None);
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_IndexedReads(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile_IndexedReads *p = (struct __pyx_obj_5pysam_8csamfile_IndexedReads *)o;
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  PyObject_GC_UnTrack(o);
-  {
-    PyObject *etype, *eval, *etb;
-    PyErr_Fetch(&etype, &eval, &etb);
-    ++Py_REFCNT(o);
-    __pyx_pw_5pysam_8csamfile_12IndexedReads_7__dealloc__(o);
-    --Py_REFCNT(o);
-    PyErr_Restore(etype, eval, etb);
-  }
-  Py_CLEAR(p->samfile);
-  Py_CLEAR(p->index);
-  (*Py_TYPE(o)->tp_free)(o);
-}
-
-static int __pyx_tp_traverse_5pysam_8csamfile_IndexedReads(PyObject *o, visitproc v, void *a) {
-  int e;
-  struct __pyx_obj_5pysam_8csamfile_IndexedReads *p = (struct __pyx_obj_5pysam_8csamfile_IndexedReads *)o;
-  if (p->samfile) {
-    e = (*v)(((PyObject*)p->samfile), a); if (e) return e;
-  }
-  if (p->index) {
-    e = (*v)(p->index, a); if (e) return e;
-  }
-  return 0;
-}
-
-static int __pyx_tp_clear_5pysam_8csamfile_IndexedReads(PyObject *o) {
-  PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile_IndexedReads *p = (struct __pyx_obj_5pysam_8csamfile_IndexedReads *)o;
-  tmp = ((PyObject*)p->samfile);
-  p->samfile = ((struct __pyx_obj_5pysam_8csamfile_Samfile *)Py_None); Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->index);
-  p->index = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  return 0;
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_IndexedReads[] = {
-  {__Pyx_NAMESTR("build"), (PyCFunction)__pyx_pw_5pysam_8csamfile_12IndexedReads_3build, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_12IndexedReads_2build)},
-  {__Pyx_NAMESTR("find"), (PyCFunction)__pyx_pw_5pysam_8csamfile_12IndexedReads_5find, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_8csamfile_12IndexedReads_4find)},
-  {0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_IndexedReads = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.IndexedReads"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_IndexedReads), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_IndexedReads, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("IndexedReads(Samfile samfile, int reopen=True)\nindex a bamfile by read.\n\n    The index is kept in memory.\n\n    By default, the file is re-openend to avoid conflicts if\n    multiple operators work on the same file. Set *reopen* = False\n    to not re-open *samfile*.\n    "), /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile_IndexedReads, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile_IndexedReads, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  0, /*tp_iter*/
-  0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_IndexedReads, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  __pyx_pw_5pysam_8csamfile_12IndexedReads_1__init__, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_IndexedReads, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile_SNPCall(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  PyObject *o;
-  if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
-    o = (*t->tp_alloc)(t, 0);
-  } else {
-    o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
-  }
-  if (unlikely(!o)) return 0;
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile_SNPCall(PyObject *o) {
-  #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
-    if (PyObject_CallFinalizerFromDealloc(o)) return;
-  }
-  #endif
-  (*Py_TYPE(o)->tp_free)(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_tid(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_3tid_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_pos(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_3pos_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_reference_base(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_14reference_base_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_genotype(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_8genotype_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_consensus_quality(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_17consensus_quality_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_snp_quality(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_11snp_quality_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_mapping_quality(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_15mapping_quality_1__get__(o);
-}
-
-static PyObject *__pyx_getprop_5pysam_8csamfile_7SNPCall_coverage(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_8csamfile_7SNPCall_8coverage_1__get__(o);
-}
-
-static PyMethodDef __pyx_methods_5pysam_8csamfile_SNPCall[] = {
-  {0, 0, 0, 0}
-};
-
-static struct PyGetSetDef __pyx_getsets_5pysam_8csamfile_SNPCall[] = {
-  {(char *)"tid", __pyx_getprop_5pysam_8csamfile_7SNPCall_tid, 0, __Pyx_DOCSTR(__pyx_k_the_chromosome_ID_as_is_defined), 0},
-  {(char *)"pos", __pyx_getprop_5pysam_8csamfile_7SNPCall_pos, 0, __Pyx_DOCSTR(__pyx_k_nucleotide_position_of_SNP), 0},
-  {(char *)"reference_base", __pyx_getprop_5pysam_8csamfile_7SNPCall_reference_base, 0, __Pyx_DOCSTR(__pyx_k_reference_base_at_pos_N_if_no_re), 0},
-  {(char *)"genotype", __pyx_getprop_5pysam_8csamfile_7SNPCall_genotype, 0, __Pyx_DOCSTR(__pyx_k_the_genotype_called), 0},
-  {(char *)"consensus_quality", __pyx_getprop_5pysam_8csamfile_7SNPCall_consensus_quality, 0, __Pyx_DOCSTR(__pyx_k_the_genotype_quality_Phred_scale), 0},
-  {(char *)"snp_quality", __pyx_getprop_5pysam_8csamfile_7SNPCall_snp_quality, 0, __Pyx_DOCSTR(__pyx_k_the_snp_quality_Phred_scaled_pro), 0},
-  {(char *)"mapping_quality", __pyx_getprop_5pysam_8csamfile_7SNPCall_mapping_quality, 0, __Pyx_DOCSTR(__pyx_k_the_root_mean_square_rms_of_the), 0},
-  {(char *)"coverage", __pyx_getprop_5pysam_8csamfile_7SNPCall_coverage, 0, __Pyx_DOCSTR(__pyx_k_coverage_or_read_depth_the_numbe), 0},
-  {0, 0, 0, 0, 0}
-};
-
-static PyTypeObject __pyx_type_5pysam_8csamfile_SNPCall = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.SNPCall"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile_SNPCall), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile_SNPCall, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  __pyx_pw_5pysam_8csamfile_7SNPCall_1__str__, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("the results of a SNP call."), /*tp_doc*/
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  0, /*tp_iter*/
-  0, /*tp_iternext*/
-  __pyx_methods_5pysam_8csamfile_SNPCall, /*tp_methods*/
-  0, /*tp_members*/
-  __pyx_getsets_5pysam_8csamfile_SNPCall, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  0, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile_SNPCall, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-
-static struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *__pyx_freelist_5pysam_8csamfile___pyx_scope_struct__genexpr[8];
-static int __pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr = 0;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct__genexpr(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  PyObject *o;
-  if (likely((__pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr > 0) & (t->tp_basicsize == sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr)))) {
-    o = (PyObject*)__pyx_freelist_5pysam_8csamfile___pyx_scope_struct__genexpr[--__pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr];
-    memset(o, 0, sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr));
-    (void) PyObject_INIT(o, t);
-    PyObject_GC_Track(o);
-  } else {
-    o = (*t->tp_alloc)(t, 0);
-    if (unlikely(!o)) return 0;
-  }
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile___pyx_scope_struct__genexpr(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)o;
-  PyObject_GC_UnTrack(o);
-  Py_CLEAR(p->__pyx_v_x);
-  Py_CLEAR(p->__pyx_v_y);
-  Py_CLEAR(p->__pyx_t_0);
-  Py_CLEAR(p->__pyx_t_1);
-  if ((__pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr < 8) & (Py_TYPE(o)->tp_basicsize == sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr))) {
-    __pyx_freelist_5pysam_8csamfile___pyx_scope_struct__genexpr[__pyx_freecount_5pysam_8csamfile___pyx_scope_struct__genexpr++] = ((struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)o);
-  } else {
-    (*Py_TYPE(o)->tp_free)(o);
-  }
-}
-
-static int __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct__genexpr(PyObject *o, visitproc v, void *a) {
-  int e;
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)o;
-  if (p->__pyx_v_x) {
-    e = (*v)(p->__pyx_v_x, a); if (e) return e;
-  }
-  if (p->__pyx_v_y) {
-    e = (*v)(p->__pyx_v_y, a); if (e) return e;
-  }
-  if (p->__pyx_t_0) {
-    e = (*v)(p->__pyx_t_0, a); if (e) return e;
-  }
-  if (p->__pyx_t_1) {
-    e = (*v)(p->__pyx_t_1, a); if (e) return e;
-  }
-  return 0;
-}
-
-static int __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct__genexpr(PyObject *o) {
-  PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr *)o;
-  tmp = ((PyObject*)p->__pyx_v_x);
-  p->__pyx_v_x = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->__pyx_v_y);
-  p->__pyx_v_y = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->__pyx_t_0);
-  p->__pyx_t_0 = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->__pyx_t_1);
-  p->__pyx_t_1 = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  return 0;
-}
-
-static PyTypeObject __pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.__pyx_scope_struct__genexpr"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct__genexpr), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile___pyx_scope_struct__genexpr, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  0, /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct__genexpr, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct__genexpr, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  0, /*tp_iter*/
-  0, /*tp_iternext*/
-  0, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  0, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile___pyx_scope_struct__genexpr, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-
-static struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *__pyx_freelist_5pysam_8csamfile___pyx_scope_struct_1_genexpr[8];
-static int __pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr = 0;
-
-static PyObject *__pyx_tp_new_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyTypeObject *t, CYTHON_UNUSED PyObject *a, CYTHON_UNUSED PyObject *k) {
-  PyObject *o;
-  if (likely((__pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr > 0) & (t->tp_basicsize == sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr)))) {
-    o = (PyObject*)__pyx_freelist_5pysam_8csamfile___pyx_scope_struct_1_genexpr[--__pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr];
-    memset(o, 0, sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr));
-    (void) PyObject_INIT(o, t);
-    PyObject_GC_Track(o);
-  } else {
-    o = (*t->tp_alloc)(t, 0);
-    if (unlikely(!o)) return 0;
-  }
-  return o;
-}
-
-static void __pyx_tp_dealloc_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyObject *o) {
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)o;
-  PyObject_GC_UnTrack(o);
-  Py_CLEAR(p->__pyx_v_x);
-  Py_CLEAR(p->__pyx_v_y);
-  Py_CLEAR(p->__pyx_t_0);
-  Py_CLEAR(p->__pyx_t_1);
-  if ((__pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr < 8) & (Py_TYPE(o)->tp_basicsize == sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr))) {
-    __pyx_freelist_5pysam_8csamfile___pyx_scope_struct_1_genexpr[__pyx_freecount_5pysam_8csamfile___pyx_scope_struct_1_genexpr++] = ((struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)o);
-  } else {
-    (*Py_TYPE(o)->tp_free)(o);
-  }
-}
-
-static int __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyObject *o, visitproc v, void *a) {
-  int e;
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)o;
-  if (p->__pyx_v_x) {
-    e = (*v)(p->__pyx_v_x, a); if (e) return e;
-  }
-  if (p->__pyx_v_y) {
-    e = (*v)(p->__pyx_v_y, a); if (e) return e;
-  }
-  if (p->__pyx_t_0) {
-    e = (*v)(p->__pyx_t_0, a); if (e) return e;
-  }
-  if (p->__pyx_t_1) {
-    e = (*v)(p->__pyx_t_1, a); if (e) return e;
-  }
-  return 0;
-}
-
-static int __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct_1_genexpr(PyObject *o) {
-  PyObject* tmp;
-  struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *p = (struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr *)o;
-  tmp = ((PyObject*)p->__pyx_v_x);
-  p->__pyx_v_x = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->__pyx_v_y);
-  p->__pyx_v_y = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->__pyx_t_0);
-  p->__pyx_t_0 = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  tmp = ((PyObject*)p->__pyx_t_1);
-  p->__pyx_t_1 = Py_None; Py_INCREF(Py_None);
-  Py_XDECREF(tmp);
-  return 0;
-}
-
-static PyTypeObject __pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr = {
-  PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.csamfile.__pyx_scope_struct_1_genexpr"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_8csamfile___pyx_scope_struct_1_genexpr), /*tp_basicsize*/
-  0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_8csamfile___pyx_scope_struct_1_genexpr, /*tp_dealloc*/
-  0, /*tp_print*/
-  0, /*tp_getattr*/
-  0, /*tp_setattr*/
-  #if PY_MAJOR_VERSION < 3
-  0, /*tp_compare*/
-  #else
-  0, /*reserved*/
-  #endif
-  0, /*tp_repr*/
-  0, /*tp_as_number*/
-  0, /*tp_as_sequence*/
-  0, /*tp_as_mapping*/
-  0, /*tp_hash*/
-  0, /*tp_call*/
-  0, /*tp_str*/
-  0, /*tp_getattro*/
-  0, /*tp_setattro*/
-  0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  0, /*tp_doc*/
-  __pyx_tp_traverse_5pysam_8csamfile___pyx_scope_struct_1_genexpr, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_8csamfile___pyx_scope_struct_1_genexpr, /*tp_clear*/
-  0, /*tp_richcompare*/
-  0, /*tp_weaklistoffset*/
-  0, /*tp_iter*/
-  0, /*tp_iternext*/
-  0, /*tp_methods*/
-  0, /*tp_members*/
-  0, /*tp_getset*/
-  0, /*tp_base*/
-  0, /*tp_dict*/
-  0, /*tp_descr_get*/
-  0, /*tp_descr_set*/
-  0, /*tp_dictoffset*/
-  0, /*tp_init*/
-  0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_8csamfile___pyx_scope_struct_1_genexpr, /*tp_new*/
-  0, /*tp_free*/
-  0, /*tp_is_gc*/
-  0, /*tp_bases*/
-  0, /*tp_mro*/
-  0, /*tp_cache*/
-  0, /*tp_subclasses*/
-  0, /*tp_weaklist*/
-  0, /*tp_del*/
-  #if PY_VERSION_HEX >= 0x02060000
-  0, /*tp_version_tag*/
-  #endif
-  #if PY_VERSION_HEX >= 0x030400a1
-  0, /*tp_finalize*/
-  #endif
-};
-
-static PyMethodDef __pyx_methods[] = {
-  {0, 0, 0, 0}
-};
-
-#if PY_MAJOR_VERSION >= 3
-static struct PyModuleDef __pyx_moduledef = {
-  #if PY_VERSION_HEX < 0x03020000
-    { PyObject_HEAD_INIT(NULL) NULL, 0, NULL },
-  #else
-    PyModuleDef_HEAD_INIT,
-  #endif
-    __Pyx_NAMESTR("csamfile"),
-    0, /* m_doc */
-    -1, /* m_size */
-    __pyx_methods /* m_methods */,
-    NULL, /* m_reload */
-    NULL, /* m_traverse */
-    NULL, /* m_clear */
-    NULL /* m_free */
-};
-#endif
-
-static __Pyx_StringTabEntry __pyx_string_tab[] = {
-  {&__pyx_kp_s_2scB, __pyx_k_2scB, sizeof(__pyx_k_2scB), 0, 0, 1, 0},
-  {&__pyx_kp_s_2scH, __pyx_k_2scH, sizeof(__pyx_k_2scH), 0, 0, 1, 0},
-  {&__pyx_kp_s_2scI, __pyx_k_2scI, sizeof(__pyx_k_2scI), 0, 0, 1, 0},
-  {&__pyx_kp_s_2sc_is, __pyx_k_2sc_is, sizeof(__pyx_k_2sc_is), 0, 0, 1, 0},
-  {&__pyx_kp_s_2scb, __pyx_k_2scb, sizeof(__pyx_k_2scb), 0, 0, 1, 0},
-  {&__pyx_kp_s_2scc, __pyx_k_2scc, sizeof(__pyx_k_2scc), 0, 0, 1, 0},
-  {&__pyx_kp_s_2sccI_i_s, __pyx_k_2sccI_i_s, sizeof(__pyx_k_2sccI_i_s), 0, 0, 1, 0},
-  {&__pyx_kp_s_2scf, __pyx_k_2scf, sizeof(__pyx_k_2scf), 0, 0, 1, 0},
-  {&__pyx_kp_s_2sch, __pyx_k_2sch, sizeof(__pyx_k_2sch), 0, 0, 1, 0},
-  {&__pyx_kp_s_2sci, __pyx_k_2sci, sizeof(__pyx_k_2sci), 0, 0, 1, 0},
-  {&__pyx_kp_s_30s_10s_s, __pyx_k_30s_10s_s, sizeof(__pyx_k_30s_10s_s), 0, 0, 1, 0},
-  {&__pyx_n_s_A, __pyx_k_A, sizeof(__pyx_k_A), 0, 0, 1, 1},
-  {&__pyx_n_s_AS, __pyx_k_AS, sizeof(__pyx_k_AS), 0, 0, 1, 1},
-  {&__pyx_kp_s_A_pileup_column_A_pileup_column, __pyx_k_A_pileup_column_A_pileup_column, sizeof(__pyx_k_A_pileup_column_A_pileup_column), 0, 0, 1, 0},
-  {&__pyx_n_s_AlignedRead, __pyx_k_AlignedRead, sizeof(__pyx_k_AlignedRead), 0, 0, 1, 1},
-  {&__pyx_kp_u_Argument_must_be_string_bytes_or, __pyx_k_Argument_must_be_string_bytes_or, sizeof(__pyx_k_Argument_must_be_string_bytes_or), 0, 1, 0, 0},
-  {&__pyx_kp_u_Argument_must_be_string_or_unico, __pyx_k_Argument_must_be_string_or_unico, sizeof(__pyx_k_Argument_must_be_string_or_unico), 0, 1, 0, 0},
-  {&__pyx_n_s_AttributeError, __pyx_k_AttributeError, sizeof(__pyx_k_AttributeError), 0, 0, 1, 1},
-  {&__pyx_n_s_B, __pyx_k_B, sizeof(__pyx_k_B), 0, 0, 1, 1},
-  {&__pyx_kp_s_Bam_index_bin_number, __pyx_k_Bam_index_bin_number, sizeof(__pyx_k_Bam_index_bin_number), 0, 0, 1, 0},
-  {&__pyx_kp_s_Binary_flag, __pyx_k_Binary_flag, sizeof(__pyx_k_Binary_flag), 0, 0, 1, 0},
-  {&__pyx_n_s_C, __pyx_k_C, sizeof(__pyx_k_C), 0, 0, 1, 1},
-  {&__pyx_n_s_CIGAR2CODE, __pyx_k_CIGAR2CODE, sizeof(__pyx_k_CIGAR2CODE), 0, 0, 1, 1},
-  {&__pyx_n_s_CIGAR_REGEX, __pyx_k_CIGAR_REGEX, sizeof(__pyx_k_CIGAR_REGEX), 0, 0, 1, 1},
-  {&__pyx_n_s_CL, __pyx_k_CL, sizeof(__pyx_k_CL), 0, 0, 1, 1},
-  {&__pyx_n_s_CN, __pyx_k_CN, sizeof(__pyx_k_CN), 0, 0, 1, 1},
-  {&__pyx_n_s_CO, __pyx_k_CO, sizeof(__pyx_k_CO), 0, 0, 1, 1},
-  {&__pyx_kp_s_Cigar_entries, __pyx_k_Cigar_entries, sizeof(__pyx_k_Cigar_entries), 0, 0, 1, 0},
-  {&__pyx_kp_s_Contig_index, __pyx_k_Contig_index, sizeof(__pyx_k_Contig_index), 0, 0, 1, 0},
-  {&__pyx_kp_s_Contig_index_for_mate_pair, __pyx_k_Contig_index_for_mate_pair, sizeof(__pyx_k_Contig_index_for_mate_pair), 0, 0, 1, 0},
-  {&__pyx_kp_s_Count_of_cigar_entries, __pyx_k_Count_of_cigar_entries, sizeof(__pyx_k_Count_of_cigar_entries), 0, 0, 1, 0},
-  {&__pyx_n_s_D, __pyx_k_D, sizeof(__pyx_k_D), 0, 0, 1, 1},
-  {&__pyx_n_s_DS, __pyx_k_DS, sizeof(__pyx_k_DS), 0, 0, 1, 1},
-  {&__pyx_n_s_DT, __pyx_k_DT, sizeof(__pyx_k_DT), 0, 0, 1, 1},
-  {&__pyx_n_s_F, __pyx_k_F, sizeof(__pyx_k_F), 0, 0, 1, 1},
-  {&__pyx_n_s_FO, __pyx_k_FO, sizeof(__pyx_k_FO), 0, 0, 1, 1},
-  {&__pyx_n_s_GO, __pyx_k_GO, sizeof(__pyx_k_GO), 0, 0, 1, 1},
-  {&__pyx_n_s_H, __pyx_k_H, sizeof(__pyx_k_H), 0, 0, 1, 1},
-  {&__pyx_n_s_HD, __pyx_k_HD, sizeof(__pyx_k_HD), 0, 0, 1, 1},
-  {&__pyx_n_s_I, __pyx_k_I, sizeof(__pyx_k_I), 0, 0, 1, 1},
-  {&__pyx_n_s_ID, __pyx_k_ID, sizeof(__pyx_k_ID), 0, 0, 1, 1},
-  {&__pyx_n_s_IOError, __pyx_k_IOError, sizeof(__pyx_k_IOError), 0, 0, 1, 1},
-  {&__pyx_n_s_IS_PYTHON3, __pyx_k_IS_PYTHON3, sizeof(__pyx_k_IS_PYTHON3), 0, 0, 1, 1},
-  {&__pyx_kp_s_I_O_operation_on_closed_file, __pyx_k_I_O_operation_on_closed_file, sizeof(__pyx_k_I_O_operation_on_closed_file), 0, 0, 1, 0},
-  {&__pyx_n_s_IndexedReads, __pyx_k_IndexedReads, sizeof(__pyx_k_IndexedReads), 0, 0, 1, 1},
-  {&__pyx_kp_s_Insert_size, __pyx_k_Insert_size, sizeof(__pyx_k_Insert_size), 0, 0, 1, 0},
-  {&__pyx_kp_s_Invalid_tag_s, __pyx_k_Invalid_tag_s, sizeof(__pyx_k_Invalid_tag_s), 0, 0, 1, 0},
-  {&__pyx_n_s_IteratorColumn, __pyx_k_IteratorColumn, sizeof(__pyx_k_IteratorColumn), 0, 0, 1, 1},
-  {&__pyx_n_s_IteratorRow, __pyx_k_IteratorRow, sizeof(__pyx_k_IteratorRow), 0, 0, 1, 1},
-  {&__pyx_n_s_KS, __pyx_k_KS, sizeof(__pyx_k_KS), 0, 0, 1, 1},
-  {&__pyx_n_s_KeyError, __pyx_k_KeyError, sizeof(__pyx_k_KeyError), 0, 0, 1, 1},
-  {&__pyx_n_s_LB, __pyx_k_LB, sizeof(__pyx_k_LB), 0, 0, 1, 1},
-  {&__pyx_n_s_LN, __pyx_k_LN, sizeof(__pyx_k_LN), 0, 0, 1, 1},
-  {&__pyx_kp_s_Length_of_auxilary_data, __pyx_k_Length_of_auxilary_data, sizeof(__pyx_k_Length_of_auxilary_data), 0, 0, 1, 0},
-  {&__pyx_kp_s_Length_of_query_name, __pyx_k_Length_of_query_name, sizeof(__pyx_k_Length_of_query_name), 0, 0, 1, 0},
-  {&__pyx_kp_s_Length_of_query_sequence, __pyx_k_Length_of_query_sequence, sizeof(__pyx_k_Length_of_query_sequence), 0, 0, 1, 0},
-  {&__pyx_n_s_M5, __pyx_k_M5, sizeof(__pyx_k_M5), 0, 0, 1, 1},
-  {&__pyx_kp_s_Mapped_position_on_contig, __pyx_k_Mapped_position_on_contig, sizeof(__pyx_k_Mapped_position_on_contig), 0, 0, 1, 0},
-  {&__pyx_kp_s_Mapping_quality, __pyx_k_Mapping_quality, sizeof(__pyx_k_Mapping_quality), 0, 0, 1, 0},
-  {&__pyx_kp_s_Maximum_data_length, __pyx_k_Maximum_data_length, sizeof(__pyx_k_Maximum_data_length), 0, 0, 1, 0},
-  {&__pyx_n_s_NotImplementedError, __pyx_k_NotImplementedError, sizeof(__pyx_k_NotImplementedError), 0, 0, 1, 1},
-  {&__pyx_n_s_OSError, __pyx_k_OSError, sizeof(__pyx_k_OSError), 0, 0, 1, 1},
-  {&__pyx_n_s_OverflowError, __pyx_k_OverflowError, sizeof(__pyx_k_OverflowError), 0, 0, 1, 1},
-  {&__pyx_n_s_PG, __pyx_k_PG, sizeof(__pyx_k_PG), 0, 0, 1, 1},
-  {&__pyx_n_s_PI, __pyx_k_PI, sizeof(__pyx_k_PI), 0, 0, 1, 1},
-  {&__pyx_n_s_PL, __pyx_k_PL, sizeof(__pyx_k_PL), 0, 0, 1, 1},
-  {&__pyx_n_s_PN, __pyx_k_PN, sizeof(__pyx_k_PN), 0, 0, 1, 1},
-  {&__pyx_n_s_PP, __pyx_k_PP, sizeof(__pyx_k_PP), 0, 0, 1, 1},
-  {&__pyx_n_s_PU, __pyx_k_PU, sizeof(__pyx_k_PU), 0, 0, 1, 1},
-  {&__pyx_n_s_PileupColumn, __pyx_k_PileupColumn, sizeof(__pyx_k_PileupColumn), 0, 0, 1, 1},
-  {&__pyx_n_s_PileupColumn___str, __pyx_k_PileupColumn___str, sizeof(__pyx_k_PileupColumn___str), 0, 0, 1, 1},
-  {&__pyx_n_s_PileupProxy, __pyx_k_PileupProxy, sizeof(__pyx_k_PileupProxy), 0, 0, 1, 1},
-  {&__pyx_kp_s_PileupProxy_accessed_after_itera, __pyx_k_PileupProxy_accessed_after_itera, sizeof(__pyx_k_PileupProxy_accessed_after_itera), 0, 0, 1, 0},
-  {&__pyx_n_s_PileupRead, __pyx_k_PileupRead, sizeof(__pyx_k_PileupRead), 0, 0, 1, 1},
-  {&__pyx_kp_s_Position_of_mate_pair, __pyx_k_Position_of_mate_pair, sizeof(__pyx_k_Position_of_mate_pair), 0, 0, 1, 0},
-  {&__pyx_kp_s_Quality_scores, __pyx_k_Quality_scores, sizeof(__pyx_k_Quality_scores), 0, 0, 1, 0},
-  {&__pyx_kp_s_Query_name, __pyx_k_Query_name, sizeof(__pyx_k_Query_name), 0, 0, 1, 0},
-  {&__pyx_kp_s_Query_sequence, __pyx_k_Query_sequence, sizeof(__pyx_k_Query_sequence), 0, 0, 1, 0},
-  {&__pyx_n_s_RG, __pyx_k_RG, sizeof(__pyx_k_RG), 0, 0, 1, 1},
-  {&__pyx_n_s_S, __pyx_k_S, sizeof(__pyx_k_S), 0, 0, 1, 1},
-  {&__pyx_n_s_SM, __pyx_k_SM, sizeof(__pyx_k_SM), 0, 0, 1, 1},
-  {&__pyx_n_s_SN, __pyx_k_SN, sizeof(__pyx_k_SN), 0, 0, 1, 1},
-  {&__pyx_n_s_SO, __pyx_k_SO, sizeof(__pyx_k_SO), 0, 0, 1, 1},
-  {&__pyx_n_s_SP, __pyx_k_SP, sizeof(__pyx_k_SP), 0, 0, 1, 1},
-  {&__pyx_n_s_SQ, __pyx_k_SQ, sizeof(__pyx_k_SQ), 0, 0, 1, 1},
-  {&__pyx_kp_s_SQ_SN_s_LN_s, __pyx_k_SQ_SN_s_LN_s, sizeof(__pyx_k_SQ_SN_s_LN_s), 0, 0, 1, 0},
-  {&__pyx_n_s_Samfile, __pyx_k_Samfile, sizeof(__pyx_k_Samfile), 0, 0, 1, 1},
-  {&__pyx_kp_s_Samfile_mapped_only_available_in, __pyx_k_Samfile_mapped_only_available_in, sizeof(__pyx_k_Samfile_mapped_only_available_in), 0, 0, 1, 0},
-  {&__pyx_n_s_StopIteration, __pyx_k_StopIteration, sizeof(__pyx_k_StopIteration), 0, 0, 1, 1},
-  {&__pyx_kp_s_This_class_cannot_be_instantiate, __pyx_k_This_class_cannot_be_instantiate, sizeof(__pyx_k_This_class_cannot_be_instantiate), 0, 0, 1, 0},
-  {&__pyx_n_s_TypeError, __pyx_k_TypeError, sizeof(__pyx_k_TypeError), 0, 0, 1, 1},
-  {&__pyx_n_s_UR, __pyx_k_UR, sizeof(__pyx_k_UR), 0, 0, 1, 1},
-  {&__pyx_kp_s_Unsupported_value_type_in_set_op, __pyx_k_Unsupported_value_type_in_set_op, sizeof(__pyx_k_Unsupported_value_type_in_set_op), 0, 0, 1, 0},
-  {&__pyx_n_s_VALID_HEADERS, __pyx_k_VALID_HEADERS, sizeof(__pyx_k_VALID_HEADERS), 0, 0, 1, 1},
-  {&__pyx_n_s_VALID_HEADER_FIELDS, __pyx_k_VALID_HEADER_FIELDS, sizeof(__pyx_k_VALID_HEADER_FIELDS), 0, 0, 1, 1},
-  {&__pyx_n_s_VALID_HEADER_ORDER, __pyx_k_VALID_HEADER_ORDER, sizeof(__pyx_k_VALID_HEADER_ORDER), 0, 0, 1, 1},
-  {&__pyx_n_s_VALID_HEADER_TYPES, __pyx_k_VALID_HEADER_TYPES, sizeof(__pyx_k_VALID_HEADER_TYPES), 0, 0, 1, 1},
-  {&__pyx_n_s_VN, __pyx_k_VN, sizeof(__pyx_k_VN), 0, 0, 1, 1},
-  {&__pyx_n_s_ValueError, __pyx_k_ValueError, sizeof(__pyx_k_ValueError), 0, 0, 1, 1},
-  {&__pyx_n_s_Z, __pyx_k_Z, sizeof(__pyx_k_Z), 0, 0, 1, 1},
-  {&__pyx_n_s_Zidf, __pyx_k_Zidf, sizeof(__pyx_k_Zidf), 0, 0, 1, 1},
-  {&__pyx_kp_s__14, __pyx_k__14, sizeof(__pyx_k__14), 0, 0, 1, 0},
-  {&__pyx_kp_s__3, __pyx_k__3, sizeof(__pyx_k__3), 0, 0, 1, 0},
-  {&__pyx_kp_s__4, __pyx_k__4, sizeof(__pyx_k__4), 0, 0, 1, 0},
-  {&__pyx_kp_s__41, __pyx_k__41, sizeof(__pyx_k__41), 0, 0, 1, 0},
-  {&__pyx_kp_s__47, __pyx_k__47, sizeof(__pyx_k__47), 0, 0, 1, 0},
-  {&__pyx_kp_b__6, __pyx_k__6, sizeof(__pyx_k__6), 0, 0, 0, 0},
-  {&__pyx_kp_s__76, __pyx_k__76, sizeof(__pyx_k__76), 0, 0, 1, 0},
-  {&__pyx_kp_s__79, __pyx_k__79, sizeof(__pyx_k__79), 0, 0, 1, 0},
-  {&__pyx_kp_s__80, __pyx_k__80, sizeof(__pyx_k__80), 0, 0, 1, 0},
-  {&__pyx_kp_s__9, __pyx_k__9, sizeof(__pyx_k__9), 0, 0, 1, 0},
-  {&__pyx_n_s_add_sq_text, __pyx_k_add_sq_text, sizeof(__pyx_k_add_sq_text), 0, 0, 1, 1},
-  {&__pyx_n_s_alignment, __pyx_k_alignment, sizeof(__pyx_k_alignment), 0, 0, 1, 1},
-  {&__pyx_n_s_all, __pyx_k_all, sizeof(__pyx_k_all), 0, 0, 1, 1},
-  {&__pyx_n_s_all_2, __pyx_k_all_2, sizeof(__pyx_k_all_2), 0, 0, 1, 1},
-  {&__pyx_n_s_append, __pyx_k_append, sizeof(__pyx_k_append), 0, 0, 1, 1},
-  {&__pyx_n_s_args, __pyx_k_args, sizeof(__pyx_k_args), 0, 0, 1, 1},
-  {&__pyx_n_s_ascii, __pyx_k_ascii, sizeof(__pyx_k_ascii), 0, 0, 1, 1},
-  {&__pyx_n_s_b, __pyx_k_b, sizeof(__pyx_k_b), 0, 0, 1, 1},
-  {&__pyx_kp_b_bai, __pyx_k_bai, sizeof(__pyx_k_bai), 0, 0, 0, 0},
-  {&__pyx_n_s_beg, __pyx_k_beg, sizeof(__pyx_k_beg), 0, 0, 1, 1},
-  {&__pyx_n_s_bin, __pyx_k_bin, sizeof(__pyx_k_bin), 0, 0, 1, 1},
-  {&__pyx_n_s_bqual, __pyx_k_bqual, sizeof(__pyx_k_bqual), 0, 0, 1, 1},
-  {&__pyx_n_s_buildLine, __pyx_k_buildLine, sizeof(__pyx_k_buildLine), 0, 0, 1, 1},
-  {&__pyx_n_s_c, __pyx_k_c, sizeof(__pyx_k_c), 0, 0, 1, 1},
-  {&__pyx_kp_s_c_2, __pyx_k_c_2, sizeof(__pyx_k_c_2), 0, 0, 1, 0},
-  {&__pyx_n_s_calcsize, __pyx_k_calcsize, sizeof(__pyx_k_calcsize), 0, 0, 1, 1},
-  {&__pyx_n_s_callback, __pyx_k_callback, sizeof(__pyx_k_callback), 0, 0, 1, 1},
-  {&__pyx_kp_s_callback_not_implemented_yet, __pyx_k_callback_not_implemented_yet, sizeof(__pyx_k_callback_not_implemented_yet), 0, 0, 1, 0},
-  {&__pyx_kp_s_can_not_iterate_over_samfile_wit, __pyx_k_can_not_iterate_over_samfile_wit, sizeof(__pyx_k_can_not_iterate_over_samfile_wit), 0, 0, 1, 0},
-  {&__pyx_kp_s_can_only_IndexReads_on_bam_files, __pyx_k_can_only_IndexReads_on_bam_files, sizeof(__pyx_k_can_only_IndexReads_on_bam_files), 0, 0, 1, 0},
-  {&__pyx_kp_s_can_t_guess_type_or_invalid_type, __pyx_k_can_t_guess_type_or_invalid_type, sizeof(__pyx_k_can_t_guess_type_or_invalid_type), 0, 0, 1, 0},
-  {&__pyx_n_s_checkIndex, __pyx_k_checkIndex, sizeof(__pyx_k_checkIndex), 0, 0, 1, 1},
-  {&__pyx_n_s_check_header, __pyx_k_check_header, sizeof(__pyx_k_check_header), 0, 0, 1, 1},
-  {&__pyx_n_s_check_sq, __pyx_k_check_sq, sizeof(__pyx_k_check_sq), 0, 0, 1, 1},
-  {&__pyx_n_s_chr, __pyx_k_chr, sizeof(__pyx_k_chr), 0, 0, 1, 1},
-  {&__pyx_n_s_cigar, __pyx_k_cigar, sizeof(__pyx_k_cigar), 0, 0, 1, 1},
-  {&__pyx_n_s_close, __pyx_k_close, sizeof(__pyx_k_close), 0, 0, 1, 1},
-  {&__pyx_n_s_collections, __pyx_k_collections, sizeof(__pyx_k_collections), 0, 0, 1, 1},
-  {&__pyx_n_s_compile, __pyx_k_compile, sizeof(__pyx_k_compile), 0, 0, 1, 1},
-  {&__pyx_n_s_consensus_quality, __pyx_k_consensus_quality, sizeof(__pyx_k_consensus_quality), 0, 0, 1, 1},
-  {&__pyx_kp_s_could_not_open_file_mode_s_is_it, __pyx_k_could_not_open_file_mode_s_is_it, sizeof(__pyx_k_could_not_open_file_mode_s_is_it), 0, 0, 1, 0},
-  {&__pyx_kp_s_could_not_open_file_s, __pyx_k_could_not_open_file_s, sizeof(__pyx_k_could_not_open_file_s), 0, 0, 1, 0},
-  {&__pyx_n_s_coverage, __pyx_k_coverage, sizeof(__pyx_k_coverage), 0, 0, 1, 1},
-  {&__pyx_n_s_create_string_buffer, __pyx_k_create_string_buffer, sizeof(__pyx_k_create_string_buffer), 0, 0, 1, 1},
-  {&__pyx_n_s_ctypes, __pyx_k_ctypes, sizeof(__pyx_k_ctypes), 0, 0, 1, 1},
-  {&__pyx_n_s_d, __pyx_k_d, sizeof(__pyx_k_d), 0, 0, 1, 1},
-  {&__pyx_kp_s_d_MIDNSHP_X, __pyx_k_d_MIDNSHP_X, sizeof(__pyx_k_d_MIDNSHP_X), 0, 0, 1, 0},
-  {&__pyx_n_s_decode, __pyx_k_decode, sizeof(__pyx_k_decode), 0, 0, 1, 1},
-  {&__pyx_n_s_defaultdict, __pyx_k_defaultdict, sizeof(__pyx_k_defaultdict), 0, 0, 1, 1},
-  {&__pyx_n_s_doc, __pyx_k_doc, sizeof(__pyx_k_doc), 0, 0, 1, 1},
-  {&__pyx_kp_s_either_supply_options_template_h, __pyx_k_either_supply_options_template_h, sizeof(__pyx_k_either_supply_options_template_h), 0, 0, 1, 0},
-  {&__pyx_n_s_encode, __pyx_k_encode, sizeof(__pyx_k_encode), 0, 0, 1, 1},
-  {&__pyx_n_s_end, __pyx_k_end, sizeof(__pyx_k_end), 0, 0, 1, 1},
-  {&__pyx_kp_s_end_out_of_range_i, __pyx_k_end_out_of_range_i, sizeof(__pyx_k_end_out_of_range_i), 0, 0, 1, 0},
-  {&__pyx_n_s_enumerate, __pyx_k_enumerate, sizeof(__pyx_k_enumerate), 0, 0, 1, 1},
-  {&__pyx_kp_s_error_during_iteration, __pyx_k_error_during_iteration, sizeof(__pyx_k_error_during_iteration), 0, 0, 1, 0},
-  {&__pyx_kp_s_error_while_opening_index_s, __pyx_k_error_while_opening_index_s, sizeof(__pyx_k_error_while_opening_index_s), 0, 0, 1, 0},
-  {&__pyx_n_s_exc_type, __pyx_k_exc_type, sizeof(__pyx_k_exc_type), 0, 0, 1, 1},
-  {&__pyx_n_s_exc_value, __pyx_k_exc_value, sizeof(__pyx_k_exc_value), 0, 0, 1, 1},
-  {&__pyx_n_s_exists, __pyx_k_exists, sizeof(__pyx_k_exists), 0, 0, 1, 1},
-  {&__pyx_n_s_extend, __pyx_k_extend, sizeof(__pyx_k_extend), 0, 0, 1, 1},
-  {&__pyx_n_s_f, __pyx_k_f, sizeof(__pyx_k_f), 0, 0, 1, 1},
-  {&__pyx_n_s_fastafile, __pyx_k_fastafile, sizeof(__pyx_k_fastafile), 0, 0, 1, 1},
-  {&__pyx_n_s_fetch, __pyx_k_fetch, sizeof(__pyx_k_fetch), 0, 0, 1, 1},
-  {&__pyx_kp_s_fetch_called_for_htsfile_without, __pyx_k_fetch_called_for_htsfile_without, sizeof(__pyx_k_fetch_called_for_htsfile_without), 0, 0, 1, 0},
-  {&__pyx_kp_s_fetch_called_on_bamfile_without, __pyx_k_fetch_called_on_bamfile_without, sizeof(__pyx_k_fetch_called_on_bamfile_without), 0, 0, 1, 0},
-  {&__pyx_kp_s_fetching_by_region_is_not_availa, __pyx_k_fetching_by_region_is_not_availa, sizeof(__pyx_k_fetching_by_region_is_not_availa), 0, 0, 1, 0},
-  {&__pyx_n_s_fields, __pyx_k_fields, sizeof(__pyx_k_fields), 0, 0, 1, 1},
-  {&__pyx_kp_s_file_does_not_have_valid_header, __pyx_k_file_does_not_have_valid_header, sizeof(__pyx_k_file_does_not_have_valid_header), 0, 0, 1, 0},
-  {&__pyx_kp_s_file_does_not_have_valid_header_2, __pyx_k_file_does_not_have_valid_header_2, sizeof(__pyx_k_file_does_not_have_valid_header_2), 0, 0, 1, 0},
-  {&__pyx_kp_s_file_header_is_empty_mode_s_is_i, __pyx_k_file_header_is_empty_mode_s_is_i, sizeof(__pyx_k_file_header_is_empty_mode_s_is_i), 0, 0, 1, 0},
-  {&__pyx_kp_s_file_s_not_found, __pyx_k_file_s_not_found, sizeof(__pyx_k_file_s_not_found), 0, 0, 1, 0},
-  {&__pyx_n_s_filename, __pyx_k_filename, sizeof(__pyx_k_filename), 0, 0, 1, 1},
-  {&__pyx_n_s_findall, __pyx_k_findall, sizeof(__pyx_k_findall), 0, 0, 1, 1},
-  {&__pyx_n_s_flag, __pyx_k_flag, sizeof(__pyx_k_flag), 0, 0, 1, 1},
-  {&__pyx_n_s_fmts, __pyx_k_fmts, sizeof(__pyx_k_fmts), 0, 0, 1, 1},
-  {&__pyx_kp_b_ftp, __pyx_k_ftp, sizeof(__pyx_k_ftp), 0, 0, 0, 0},
-  {&__pyx_n_s_genotype, __pyx_k_genotype, sizeof(__pyx_k_genotype), 0, 0, 1, 1},
-  {&__pyx_n_s_get, __pyx_k_get, sizeof(__pyx_k_get), 0, 0, 1, 1},
-  {&__pyx_n_s_getattribute, __pyx_k_getattribute, sizeof(__pyx_k_getattribute), 0, 0, 1, 1},
-  {&__pyx_n_s_getdefaultencoding, __pyx_k_getdefaultencoding, sizeof(__pyx_k_getdefaultencoding), 0, 0, 1, 1},
-  {&__pyx_n_s_getfilesystemencoding, __pyx_k_getfilesystemencoding, sizeof(__pyx_k_getfilesystemencoding), 0, 0, 1, 1},
-  {&__pyx_n_s_gettid, __pyx_k_gettid, sizeof(__pyx_k_gettid), 0, 0, 1, 1},
-  {&__pyx_n_s_h, __pyx_k_h, sizeof(__pyx_k_h), 0, 0, 1, 1},
-  {&__pyx_n_s_hasIndex, __pyx_k_hasIndex, sizeof(__pyx_k_hasIndex), 0, 0, 1, 1},
-  {&__pyx_n_s_header, __pyx_k_header, sizeof(__pyx_k_header), 0, 0, 1, 1},
-  {&__pyx_kp_s_header_line_with_invalid_type_s, __pyx_k_header_line_with_invalid_type_s, sizeof(__pyx_k_header_line_with_invalid_type_s), 0, 0, 1, 0},
-  {&__pyx_kp_s_header_line_without_s, __pyx_k_header_line_without_s, sizeof(__pyx_k_header_line_without_s), 0, 0, 1, 0},
-  {&__pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_k_home_andreas_devel_pysam_pysam, sizeof(__pyx_k_home_andreas_devel_pysam_pysam), 0, 0, 1, 0},
-  {&__pyx_kp_b_http, __pyx_k_http, sizeof(__pyx_k_http), 0, 0, 0, 0},
-  {&__pyx_n_s_i, __pyx_k_i, sizeof(__pyx_k_i), 0, 0, 1, 1},
-  {&__pyx_kp_s_i_c, __pyx_k_i_c, sizeof(__pyx_k_i_c), 0, 0, 1, 0},
-  {&__pyx_n_s_import, __pyx_k_import, sizeof(__pyx_k_import), 0, 0, 1, 1},
-  {&__pyx_kp_s_incomplete_sequence_information, __pyx_k_incomplete_sequence_information, sizeof(__pyx_k_incomplete_sequence_information), 0, 0, 1, 0},
-  {&__pyx_n_s_indel, __pyx_k_indel, sizeof(__pyx_k_indel), 0, 0, 1, 1},
-  {&__pyx_n_s_init, __pyx_k_init, sizeof(__pyx_k_init), 0, 0, 1, 1},
-  {&__pyx_kp_s_integer_i_out_of_range_of_BAM_SA, __pyx_k_integer_i_out_of_range_of_BAM_SA, sizeof(__pyx_k_integer_i_out_of_range_of_BAM_SA), 0, 0, 1, 0},
-  {&__pyx_kp_s_invalid_coordinates_start_i_end, __pyx_k_invalid_coordinates_start_i_end, sizeof(__pyx_k_invalid_coordinates_start_i_end), 0, 0, 1, 0},
-  {&__pyx_kp_s_invalid_file_opening_mode_s, __pyx_k_invalid_file_opening_mode_s, sizeof(__pyx_k_invalid_file_opening_mode_s), 0, 0, 1, 0},
-  {&__pyx_kp_s_invalid_reference_s, __pyx_k_invalid_reference_s, sizeof(__pyx_k_invalid_reference_s), 0, 0, 1, 0},
-  {&__pyx_kp_s_invalid_type_for_record_s_s_expe, __pyx_k_invalid_type_for_record_s_s_expe, sizeof(__pyx_k_invalid_type_for_record_s_s_expe), 0, 0, 1, 0},
-  {&__pyx_n_s_isOpen, __pyx_k_isOpen, sizeof(__pyx_k_isOpen), 0, 0, 1, 1},
-  {&__pyx_n_s_is_del, __pyx_k_is_del, sizeof(__pyx_k_is_del), 0, 0, 1, 1},
-  {&__pyx_n_s_is_head, __pyx_k_is_head, sizeof(__pyx_k_is_head), 0, 0, 1, 1},
-  {&__pyx_n_s_is_tail, __pyx_k_is_tail, sizeof(__pyx_k_is_tail), 0, 0, 1, 1},
-  {&__pyx_n_s_isize, __pyx_k_isize, sizeof(__pyx_k_isize), 0, 0, 1, 1},
-  {&__pyx_n_s_islower, __pyx_k_islower, sizeof(__pyx_k_islower), 0, 0, 1, 1},
-  {&__pyx_n_s_isupper, __pyx_k_isupper, sizeof(__pyx_k_isupper), 0, 0, 1, 1},
-  {&__pyx_n_s_items, __pyx_k_items, sizeof(__pyx_k_items), 0, 0, 1, 1},
-  {&__pyx_n_s_itertools, __pyx_k_itertools, sizeof(__pyx_k_itertools), 0, 0, 1, 1},
-  {&__pyx_n_s_join, __pyx_k_join, sizeof(__pyx_k_join), 0, 0, 1, 1},
-  {&__pyx_n_s_l_data, __pyx_k_l_data, sizeof(__pyx_k_l_data), 0, 0, 1, 1},
-  {&__pyx_n_s_l_qname, __pyx_k_l_qname, sizeof(__pyx_k_l_qname), 0, 0, 1, 1},
-  {&__pyx_n_s_l_qseq, __pyx_k_l_qseq, sizeof(__pyx_k_l_qseq), 0, 0, 1, 1},
-  {&__pyx_n_s_lengths, __pyx_k_lengths, sizeof(__pyx_k_lengths), 0, 0, 1, 1},
-  {&__pyx_n_s_level, __pyx_k_level, sizeof(__pyx_k_level), 0, 0, 1, 1},
-  {&__pyx_n_s_m_data, __pyx_k_m_data, sizeof(__pyx_k_m_data), 0, 0, 1, 1},
-  {&__pyx_n_s_main, __pyx_k_main, sizeof(__pyx_k_main), 0, 0, 1, 1},
-  {&__pyx_kp_s_malformatted_header_no_in_field, __pyx_k_malformatted_header_no_in_field, sizeof(__pyx_k_malformatted_header_no_in_field), 0, 0, 1, 0},
-  {&__pyx_n_s_map, __pyx_k_map, sizeof(__pyx_k_map), 0, 0, 1, 1},
-  {&__pyx_kp_s_mapping_information_not_recorded, __pyx_k_mapping_information_not_recorded, sizeof(__pyx_k_mapping_information_not_recorded), 0, 0, 1, 0},
-  {&__pyx_n_s_mapping_quality, __pyx_k_mapping_quality, sizeof(__pyx_k_mapping_quality), 0, 0, 1, 1},
-  {&__pyx_n_s_mapq, __pyx_k_mapq, sizeof(__pyx_k_mapq), 0, 0, 1, 1},
-  {&__pyx_kp_s_mate_not_found, __pyx_k_mate_not_found, sizeof(__pyx_k_mate_not_found), 0, 0, 1, 0},
-  {&__pyx_kp_s_mate_s_is_unmapped, __pyx_k_mate_s_is_unmapped, sizeof(__pyx_k_mate_s_is_unmapped), 0, 0, 1, 0},
-  {&__pyx_n_s_max, __pyx_k_max, sizeof(__pyx_k_max), 0, 0, 1, 1},
-  {&__pyx_n_s_max_depth, __pyx_k_max_depth, sizeof(__pyx_k_max_depth), 0, 0, 1, 1},
-  {&__pyx_n_s_metaclass, __pyx_k_metaclass, sizeof(__pyx_k_metaclass), 0, 0, 1, 1},
-  {&__pyx_n_s_min, __pyx_k_min, sizeof(__pyx_k_min), 0, 0, 1, 1},
-  {&__pyx_n_s_mode, __pyx_k_mode, sizeof(__pyx_k_mode), 0, 0, 1, 1},
-  {&__pyx_n_s_module, __pyx_k_module, sizeof(__pyx_k_module), 0, 0, 1, 1},
-  {&__pyx_n_s_mpos, __pyx_k_mpos, sizeof(__pyx_k_mpos), 0, 0, 1, 1},
-  {&__pyx_n_s_mrnm, __pyx_k_mrnm, sizeof(__pyx_k_mrnm), 0, 0, 1, 1},
-  {&__pyx_n_s_mtid, __pyx_k_mtid, sizeof(__pyx_k_mtid), 0, 0, 1, 1},
-  {&__pyx_kp_s_multiple_s_lines_are_not_permitt, __pyx_k_multiple_s_lines_are_not_permitt, sizeof(__pyx_k_multiple_s_lines_are_not_permitt), 0, 0, 1, 0},
-  {&__pyx_n_s_n, __pyx_k_n, sizeof(__pyx_k_n), 0, 0, 1, 1},
-  {&__pyx_n_s_n_cigar, __pyx_k_n_cigar, sizeof(__pyx_k_n_cigar), 0, 0, 1, 1},
-  {&__pyx_n_s_nextiter, __pyx_k_nextiter, sizeof(__pyx_k_nextiter), 0, 0, 1, 1},
-  {&__pyx_kp_s_no_index_available_for_fetch, __pyx_k_no_index_available_for_fetch, sizeof(__pyx_k_no_index_available_for_fetch), 0, 0, 1, 0},
-  {&__pyx_kp_s_no_index_available_for_iteration, __pyx_k_no_index_available_for_iteration, sizeof(__pyx_k_no_index_available_for_iteration), 0, 0, 1, 0},
-  {&__pyx_kp_s_no_index_available_for_pileup, __pyx_k_no_index_available_for_pileup, sizeof(__pyx_k_no_index_available_for_pileup), 0, 0, 1, 0},
-  {&__pyx_n_s_nreferences, __pyx_k_nreferences, sizeof(__pyx_k_nreferences), 0, 0, 1, 1},
-  {&__pyx_n_s_object, __pyx_k_object, sizeof(__pyx_k_object), 0, 0, 1, 1},
-  {&__pyx_n_s_offset, __pyx_k_offset, sizeof(__pyx_k_offset), 0, 0, 1, 1},
-  {&__pyx_n_s_open, __pyx_k_open, sizeof(__pyx_k_open), 0, 0, 1, 1},
-  {&__pyx_n_s_ord, __pyx_k_ord, sizeof(__pyx_k_ord), 0, 0, 1, 1},
-  {&__pyx_n_s_os, __pyx_k_os, sizeof(__pyx_k_os), 0, 0, 1, 1},
-  {&__pyx_n_s_pack_into, __pyx_k_pack_into, sizeof(__pyx_k_pack_into), 0, 0, 1, 1},
-  {&__pyx_n_s_parseRegion, __pyx_k_parseRegion, sizeof(__pyx_k_parseRegion), 0, 0, 1, 1},
-  {&__pyx_n_s_path, __pyx_k_path, sizeof(__pyx_k_path), 0, 0, 1, 1},
-  {&__pyx_kp_s_pileup_of_samfiles_not_implement, __pyx_k_pileup_of_samfiles_not_implement, sizeof(__pyx_k_pileup_of_samfiles_not_implement), 0, 0, 1, 0},
-  {&__pyx_n_s_pileups, __pyx_k_pileups, sizeof(__pyx_k_pileups), 0, 0, 1, 1},
-  {&__pyx_n_s_platform, __pyx_k_platform, sizeof(__pyx_k_platform), 0, 0, 1, 1},
-  {&__pyx_n_s_port, __pyx_k_port, sizeof(__pyx_k_port), 0, 0, 1, 1},
-  {&__pyx_n_s_pos, __pyx_k_pos, sizeof(__pyx_k_pos), 0, 0, 1, 1},
-  {&__pyx_n_s_positions, __pyx_k_positions, sizeof(__pyx_k_positions), 0, 0, 1, 1},
-  {&__pyx_n_s_prepare, __pyx_k_prepare, sizeof(__pyx_k_prepare), 0, 0, 1, 1},
-  {&__pyx_n_s_pysam_csamfile, __pyx_k_pysam_csamfile, sizeof(__pyx_k_pysam_csamfile), 0, 0, 1, 1},
-  {&__pyx_n_s_pytag, __pyx_k_pytag, sizeof(__pyx_k_pytag), 0, 0, 1, 1},
-  {&__pyx_n_s_pyx_vtable, __pyx_k_pyx_vtable, sizeof(__pyx_k_pyx_vtable), 0, 0, 1, 1},
-  {&__pyx_n_s_qname, __pyx_k_qname, sizeof(__pyx_k_qname), 0, 0, 1, 1},
-  {&__pyx_n_s_qpos, __pyx_k_qpos, sizeof(__pyx_k_qpos), 0, 0, 1, 1},
-  {&__pyx_n_s_qseq, __pyx_k_qseq, sizeof(__pyx_k_qseq), 0, 0, 1, 1},
-  {&__pyx_n_s_qual, __pyx_k_qual, sizeof(__pyx_k_qual), 0, 0, 1, 1},
-  {&__pyx_kp_s_quality_and_sequence_mismatch_i, __pyx_k_quality_and_sequence_mismatch_i, sizeof(__pyx_k_quality_and_sequence_mismatch_i), 0, 0, 1, 0},
-  {&__pyx_n_s_qualname, __pyx_k_qualname, sizeof(__pyx_k_qualname), 0, 0, 1, 1},
-  {&__pyx_n_s_r, __pyx_k_r, sizeof(__pyx_k_r), 0, 0, 1, 1},
-  {&__pyx_n_s_rU, __pyx_k_rU, sizeof(__pyx_k_rU), 0, 0, 1, 1},
-  {&__pyx_n_s_raw, __pyx_k_raw, sizeof(__pyx_k_raw), 0, 0, 1, 1},
-  {&__pyx_n_s_rb, __pyx_k_rb, sizeof(__pyx_k_rb), 0, 0, 1, 1},
-  {&__pyx_n_s_re, __pyx_k_re, sizeof(__pyx_k_re), 0, 0, 1, 1},
-  {&__pyx_kp_s_read_s_is_unpaired, __pyx_k_read_s_is_unpaired, sizeof(__pyx_k_read_s_is_unpaired), 0, 0, 1, 0},
-  {&__pyx_kp_s_read_s_not_found, __pyx_k_read_s_not_found, sizeof(__pyx_k_read_s_not_found), 0, 0, 1, 0},
-  {&__pyx_n_s_record, __pyx_k_record, sizeof(__pyx_k_record), 0, 0, 1, 1},
-  {&__pyx_n_s_reference, __pyx_k_reference, sizeof(__pyx_k_reference), 0, 0, 1, 1},
-  {&__pyx_n_s_reference_base, __pyx_k_reference_base, sizeof(__pyx_k_reference_base), 0, 0, 1, 1},
-  {&__pyx_kp_s_reference_sequence_for_s_tid_i_n, __pyx_k_reference_sequence_for_s_tid_i_n, sizeof(__pyx_k_reference_sequence_for_s_tid_i_n), 0, 0, 1, 0},
-  {&__pyx_n_s_referencelengths, __pyx_k_referencelengths, sizeof(__pyx_k_referencelengths), 0, 0, 1, 1},
-  {&__pyx_n_s_referencenames, __pyx_k_referencenames, sizeof(__pyx_k_referencenames), 0, 0, 1, 1},
-  {&__pyx_n_s_references, __pyx_k_references, sizeof(__pyx_k_references), 0, 0, 1, 1},
-  {&__pyx_n_s_region, __pyx_k_region, sizeof(__pyx_k_region), 0, 0, 1, 1},
-  {&__pyx_n_s_reopen, __pyx_k_reopen, sizeof(__pyx_k_reopen), 0, 0, 1, 1},
-  {&__pyx_n_s_replace, __pyx_k_replace, sizeof(__pyx_k_replace), 0, 0, 1, 1},
-  {&__pyx_n_s_rlen, __pyx_k_rlen, sizeof(__pyx_k_rlen), 0, 0, 1, 1},
-  {&__pyx_n_s_rname, __pyx_k_rname, sizeof(__pyx_k_rname), 0, 0, 1, 1},
-  {&__pyx_kp_s_s, __pyx_k_s, sizeof(__pyx_k_s), 0, 0, 1, 0},
-  {&__pyx_n_s_s_2, __pyx_k_s_2, sizeof(__pyx_k_s_2), 0, 0, 1, 1},
-  {&__pyx_kp_s_s_s, __pyx_k_s_s, sizeof(__pyx_k_s_s), 0, 0, 1, 0},
-  {&__pyx_n_s_samfile, __pyx_k_samfile, sizeof(__pyx_k_samfile), 0, 0, 1, 1},
-  {&__pyx_n_s_samtools, __pyx_k_samtools, sizeof(__pyx_k_samtools), 0, 0, 1, 1},
-  {&__pyx_n_s_seek, __pyx_k_seek, sizeof(__pyx_k_seek), 0, 0, 1, 1},
-  {&__pyx_kp_s_seek_no_available_in_streams, __pyx_k_seek_no_available_in_streams, sizeof(__pyx_k_seek_no_available_in_streams), 0, 0, 1, 0},
-  {&__pyx_kp_s_seek_only_available_in_bam_files, __pyx_k_seek_only_available_in_bam_files, sizeof(__pyx_k_seek_only_available_in_bam_files), 0, 0, 1, 0},
-  {&__pyx_n_s_self, __pyx_k_self, sizeof(__pyx_k_self), 0, 0, 1, 1},
-  {&__pyx_n_s_send, __pyx_k_send, sizeof(__pyx_k_send), 0, 0, 1, 1},
-  {&__pyx_n_s_seq, __pyx_k_seq, sizeof(__pyx_k_seq), 0, 0, 1, 1},
-  {&__pyx_n_s_setTag, __pyx_k_setTag, sizeof(__pyx_k_setTag), 0, 0, 1, 1},
-  {&__pyx_n_s_snp_quality, __pyx_k_snp_quality, sizeof(__pyx_k_snp_quality), 0, 0, 1, 1},
-  {&__pyx_n_s_sorted, __pyx_k_sorted, sizeof(__pyx_k_sorted), 0, 0, 1, 1},
-  {&__pyx_n_s_split, __pyx_k_split, sizeof(__pyx_k_split), 0, 0, 1, 1},
-  {&__pyx_n_s_start, __pyx_k_start, sizeof(__pyx_k_start), 0, 0, 1, 1},
-  {&__pyx_kp_s_start_out_of_range_i, __pyx_k_start_out_of_range_i, sizeof(__pyx_k_start_out_of_range_i), 0, 0, 1, 0},
-  {&__pyx_n_s_startswith, __pyx_k_startswith, sizeof(__pyx_k_startswith), 0, 0, 1, 1},
-  {&__pyx_n_s_stepper, __pyx_k_stepper, sizeof(__pyx_k_stepper), 0, 0, 1, 1},
-  {&__pyx_n_s_str, __pyx_k_str, sizeof(__pyx_k_str), 0, 0, 1, 1},
-  {&__pyx_n_s_strip, __pyx_k_strip, sizeof(__pyx_k_strip), 0, 0, 1, 1},
-  {&__pyx_n_s_struct, __pyx_k_struct, sizeof(__pyx_k_struct), 0, 0, 1, 1},
-  {&__pyx_n_s_sys, __pyx_k_sys, sizeof(__pyx_k_sys), 0, 0, 1, 1},
-  {&__pyx_n_s_tag, __pyx_k_tag, sizeof(__pyx_k_tag), 0, 0, 1, 1},
-  {&__pyx_kp_s_tag_s_not_present, __pyx_k_tag_s_not_present, sizeof(__pyx_k_tag_s_not_present), 0, 0, 1, 0},
-  {&__pyx_n_s_tags, __pyx_k_tags, sizeof(__pyx_k_tags), 0, 0, 1, 1},
-  {&__pyx_n_s_tempfile, __pyx_k_tempfile, sizeof(__pyx_k_tempfile), 0, 0, 1, 1},
-  {&__pyx_n_s_template, __pyx_k_template, sizeof(__pyx_k_template), 0, 0, 1, 1},
-  {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1},
-  {&__pyx_n_s_text, __pyx_k_text, sizeof(__pyx_k_text), 0, 0, 1, 1},
-  {&__pyx_n_s_throw, __pyx_k_throw, sizeof(__pyx_k_throw), 0, 0, 1, 1},
-  {&__pyx_n_s_tid, __pyx_k_tid, sizeof(__pyx_k_tid), 0, 0, 1, 1},
-  {&__pyx_kp_s_tid_i_out_of_range_0_tid_i, __pyx_k_tid_i_out_of_range_0_tid_i, sizeof(__pyx_k_tid_i_out_of_range_0_tid_i), 0, 0, 1, 0},
-  {&__pyx_n_s_traceback, __pyx_k_traceback, sizeof(__pyx_k_traceback), 0, 0, 1, 1},
-  {&__pyx_n_s_truncate, __pyx_k_truncate, sizeof(__pyx_k_truncate), 0, 0, 1, 1},
-  {&__pyx_n_s_types, __pyx_k_types, sizeof(__pyx_k_types), 0, 0, 1, 1},
-  {&__pyx_kp_s_unable_to_open_index_for_s, __pyx_k_unable_to_open_index_for_s, sizeof(__pyx_k_unable_to_open_index_for_s), 0, 0, 1, 0},
-  {&__pyx_kp_s_unequal_names_and_lengths_of_ref, __pyx_k_unequal_names_and_lengths_of_ref, sizeof(__pyx_k_unequal_names_and_lengths_of_ref), 0, 0, 1, 0},
-  {&__pyx_kp_s_unknown_auxilliary_type_s, __pyx_k_unknown_auxilliary_type_s, sizeof(__pyx_k_unknown_auxilliary_type_s), 0, 0, 1, 0},
-  {&__pyx_kp_s_unknown_field_code_s_in_record_s, __pyx_k_unknown_field_code_s_in_record_s, sizeof(__pyx_k_unknown_field_code_s_in_record_s), 0, 0, 1, 0},
-  {&__pyx_kp_s_unknown_stepper_option_s_in_Iter, __pyx_k_unknown_stepper_option_s_in_Iter, sizeof(__pyx_k_unknown_stepper_option_s_in_Iter), 0, 0, 1, 0},
-  {&__pyx_kp_s_unknown_type_s, __pyx_k_unknown_type_s, sizeof(__pyx_k_unknown_type_s), 0, 0, 1, 0},
-  {&__pyx_n_s_until_eof, __pyx_k_until_eof, sizeof(__pyx_k_until_eof), 0, 0, 1, 1},
-  {&__pyx_n_s_value, __pyx_k_value, sizeof(__pyx_k_value), 0, 0, 1, 1},
-  {&__pyx_n_s_value_type, __pyx_k_value_type, sizeof(__pyx_k_value_type), 0, 0, 1, 1},
-  {&__pyx_n_s_version_info, __pyx_k_version_info, sizeof(__pyx_k_version_info), 0, 0, 1, 1},
-  {&__pyx_n_s_w, __pyx_k_w, sizeof(__pyx_k_w), 0, 0, 1, 1},
-  {&__pyx_n_s_warn, __pyx_k_warn, sizeof(__pyx_k_warn), 0, 0, 1, 1},
-  {&__pyx_n_s_warnings, __pyx_k_warnings, sizeof(__pyx_k_warnings), 0, 0, 1, 1},
-  {&__pyx_n_s_wb, __pyx_k_wb, sizeof(__pyx_k_wb), 0, 0, 1, 1},
-  {&__pyx_n_s_wbu, __pyx_k_wbu, sizeof(__pyx_k_wbu), 0, 0, 1, 1},
-  {&__pyx_n_s_wh, __pyx_k_wh, sizeof(__pyx_k_wh), 0, 0, 1, 1},
-  {&__pyx_n_s_where, __pyx_k_where, sizeof(__pyx_k_where), 0, 0, 1, 1},
-  {&__pyx_n_s_write, __pyx_k_write, sizeof(__pyx_k_write), 0, 0, 1, 1},
-  {&__pyx_n_s_zip, __pyx_k_zip, sizeof(__pyx_k_zip), 0, 0, 1, 1},
-  {0, 0, 0, 0, 0, 0, 0}
-};
-static int __Pyx_InitCachedBuiltins(void) {
-  __pyx_builtin_object = __Pyx_GetBuiltinName(__pyx_n_s_object); if (!__pyx_builtin_object) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_TypeError = __Pyx_GetBuiltinName(__pyx_n_s_TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_enumerate = __Pyx_GetBuiltinName(__pyx_n_s_enumerate); if (!__pyx_builtin_enumerate) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_ord = __Pyx_GetBuiltinName(__pyx_n_s_ord); if (!__pyx_builtin_ord) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_map = __Pyx_GetBuiltinName(__pyx_n_s_map); if (!__pyx_builtin_map) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_IOError = __Pyx_GetBuiltinName(__pyx_n_s_IOError); if (!__pyx_builtin_IOError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_OverflowError = __Pyx_GetBuiltinName(__pyx_n_s_OverflowError); if (!__pyx_builtin_OverflowError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_NotImplementedError = __Pyx_GetBuiltinName(__pyx_n_s_NotImplementedError); if (!__pyx_builtin_NotImplementedError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_OSError = __Pyx_GetBuiltinName(__pyx_n_s_OSError); if (!__pyx_builtin_OSError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_AttributeError = __Pyx_GetBuiltinName(__pyx_n_s_AttributeError); if (!__pyx_builtin_AttributeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_zip = __Pyx_GetBuiltinName(__pyx_n_s_zip); if (!__pyx_builtin_zip) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1070; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_sorted = __Pyx_GetBuiltinName(__pyx_n_s_sorted); if (!__pyx_builtin_sorted) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1086; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_KeyError = __Pyx_GetBuiltinName(__pyx_n_s_KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_min = __Pyx_GetBuiltinName(__pyx_n_s_min); if (!__pyx_builtin_min) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_max = __Pyx_GetBuiltinName(__pyx_n_s_max); if (!__pyx_builtin_max) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_chr = __Pyx_GetBuiltinName(__pyx_n_s_chr); if (!__pyx_builtin_chr) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  return 0;
-  __pyx_L1_error:;
-  return -1;
-}
-
-static int __Pyx_InitCachedConstants(void) {
-  __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0);
-
-  /* "pysam/csamfile.pyx":66
- *         return s
- *     elif PyUnicode_Check(s):
- *         return s.encode('ascii')             # <<<<<<<<<<<<<<
- *     else:
- *         raise TypeError, u"Argument must be string, bytes or unicode."
- */
-  __pyx_tuple_ = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple_);
-  __Pyx_GIVEREF(__pyx_tuple_);
-
-  /* "pysam/csamfile.pyx":87
- *         return s
- *     elif PyBytes_Check(s):
- *         return s.decode('ascii')             # <<<<<<<<<<<<<<
- *     else:
- *         # assume unicode
- */
-  __pyx_tuple__2 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__2);
-  __Pyx_GIVEREF(__pyx_tuple__2);
-
-  /* "pysam/csamfile.pyx":379
- *             self.close()
- * 
- *         cdef bytes bmode = mode.encode('ascii')             # <<<<<<<<<<<<<<
- *         self._filename = filename = _encodeFilename(filename)
- *         self.isstream = filename == b"-"
- */
-  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__5);
-  __Pyx_GIVEREF(__pyx_tuple__5);
-
-  /* "pysam/csamfile.pyx":385
- *         self.isbam = len(mode) > 1 and mode[1] == 'b'
- * 
- *         self.isremote = filename.startswith(b"http:") or \             # <<<<<<<<<<<<<<
- *                         filename.startswith(b"ftp:")
- * 
- */
-  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_b_http); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__7);
-  __Pyx_GIVEREF(__pyx_tuple__7);
-
-  /* "pysam/csamfile.pyx":386
- * 
- *         self.isremote = filename.startswith(b"http:") or \
- *                         filename.startswith(b"ftp:")             # <<<<<<<<<<<<<<
- * 
- *         cdef char * ctext
- */
-  __pyx_tuple__8 = PyTuple_Pack(1, __pyx_kp_b_ftp); if (unlikely(!__pyx_tuple__8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__8);
-  __Pyx_GIVEREF(__pyx_tuple__8);
-
-  /* "pysam/csamfile.pyx":503
- *             if not self.isremote:
- *                 if not os.path.exists(filename + b".bai") \
- *                         and not os.path.exists( filename[:-4] + b".bai"):             # <<<<<<<<<<<<<<
- *                     self.index = NULL
- *                 else:
- */
-  __pyx_slice__10 = PySlice_New(Py_None, __pyx_int_neg_4, Py_None); if (unlikely(!__pyx_slice__10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__10);
-  __Pyx_GIVEREF(__pyx_slice__10);
-
-  /* "pysam/csamfile.pyx":525
- *         '''
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *         reference = _forceBytes(reference)
- *         return bam_name2id(self.header, reference)
- */
-  __pyx_tuple__11 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__11);
-  __Pyx_GIVEREF(__pyx_tuple__11);
-
-  /* "pysam/csamfile.pyx":533
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *         if not 0 <= tid < self.header.n_targets:
- *             raise ValueError("tid %i out of range 0<=tid<%i" %
- */
-  __pyx_tuple__12 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__12);
-  __Pyx_GIVEREF(__pyx_tuple__12);
-
-  /* "pysam/csamfile.pyx":543
- *         convert numerical :term:`tid` into :term:`reference` name.'''
- *         if not self._isOpen():
- *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
- * 
- *         if not 0 <= tid < self.header.n_targets:
- */
-  __pyx_tuple__13 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__13);
-  __Pyx_GIVEREF(__pyx_tuple__13);
-
-  /* "pysam/csamfile.pyx":595
- * 
- *         if not reference:
- *             return 0, 0, 0, 0             # <<<<<<<<<<<<<<
- * 
- *         if tid is not None:
- */
-  __pyx_tuple__15 = PyTuple_Pack(4, __pyx_int_0, __pyx_int_0, __pyx_int_0, __pyx_int_0); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__15);
-  __Pyx_GIVEREF(__pyx_tuple__15);
-
-  /* "pysam/csamfile.pyx":625
- * 
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *         if not self.isbam:
- *             raise NotImplementedError("seek only available in bam files")
- */
-  __pyx_tuple__16 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__16);
-  __Pyx_GIVEREF(__pyx_tuple__16);
-
-  /* "pysam/csamfile.pyx":627
- *             raise ValueError( "I/O operation on closed file" )
- *         if not self.isbam:
- *             raise NotImplementedError("seek only available in bam files")             # <<<<<<<<<<<<<<
- *         if self.isstream:
- *             raise OSError("seek no available in streams")
- */
-  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_kp_s_seek_only_available_in_bam_files); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__17);
-  __Pyx_GIVEREF(__pyx_tuple__17);
-
-  /* "pysam/csamfile.pyx":629
- *             raise NotImplementedError("seek only available in bam files")
- *         if self.isstream:
- *             raise OSError("seek no available in streams")             # <<<<<<<<<<<<<<
- * 
- *         return bgzf_seek(self.fp, offset, where)
- */
-  __pyx_tuple__18 = PyTuple_Pack(1, __pyx_kp_s_seek_no_available_in_streams); if (unlikely(!__pyx_tuple__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__18);
-  __Pyx_GIVEREF(__pyx_tuple__18);
-
-  /* "pysam/csamfile.pyx":638
- *         '''
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *         if not self.isbam:
- *             raise NotImplementedError("seek only available in bam files")
- */
-  __pyx_tuple__19 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__19);
-  __Pyx_GIVEREF(__pyx_tuple__19);
-
-  /* "pysam/csamfile.pyx":640
- *             raise ValueError( "I/O operation on closed file" )
- *         if not self.isbam:
- *             raise NotImplementedError("seek only available in bam files")             # <<<<<<<<<<<<<<
- * 
- *         return bgzf_tell(self.fp)
- */
-  __pyx_tuple__20 = PyTuple_Pack(1, __pyx_kp_s_seek_only_available_in_bam_files); if (unlikely(!__pyx_tuple__20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__20);
-  __Pyx_GIVEREF(__pyx_tuple__20);
-
-  /* "pysam/csamfile.pyx":686
- * 
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         has_coord, rtid, rstart, rend = self._parseRegion(reference,
- */
-  __pyx_tuple__21 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__21)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__21);
-  __Pyx_GIVEREF(__pyx_tuple__21);
-
-  /* "pysam/csamfile.pyx":700
- *         if self.isbam:
- *             if not until_eof and not self._hasIndex() and not self.isremote:
- *                 raise ValueError( "fetch called on bamfile without index" )             # <<<<<<<<<<<<<<
- * 
- *             if has_coord:
- */
-  __pyx_tuple__22 = PyTuple_Pack(1, __pyx_kp_s_fetch_called_on_bamfile_without); if (unlikely(!__pyx_tuple__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__22);
-  __Pyx_GIVEREF(__pyx_tuple__22);
-
-  /* "pysam/csamfile.pyx":713
- *         else:
- *             if has_coord:
- *                 raise ValueError ("fetching by region is not available for sam files")             # <<<<<<<<<<<<<<
- * 
- *             if callback:
- */
-  __pyx_tuple__23 = PyTuple_Pack(1, __pyx_kp_s_fetching_by_region_is_not_availa); if (unlikely(!__pyx_tuple__23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__23);
-  __Pyx_GIVEREF(__pyx_tuple__23);
-
-  /* "pysam/csamfile.pyx":716
- * 
- *             if callback:
- *                 raise NotImplementedError("callback not implemented yet")             # <<<<<<<<<<<<<<
- * 
- *             if self.header == NULL:
- */
-  __pyx_tuple__24 = PyTuple_Pack(1, __pyx_kp_s_callback_not_implemented_yet); if (unlikely(!__pyx_tuple__24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__24);
-  __Pyx_GIVEREF(__pyx_tuple__24);
-
-  /* "pysam/csamfile.pyx":719
- * 
- *             if self.header == NULL:
- *                 raise ValueError("fetch called for htsfile without header")             # <<<<<<<<<<<<<<
- * 
- *             # check if targets are defined
- */
-  __pyx_tuple__25 = PyTuple_Pack(1, __pyx_kp_s_fetch_called_for_htsfile_without); if (unlikely(!__pyx_tuple__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__25);
-  __Pyx_GIVEREF(__pyx_tuple__25);
-
-  /* "pysam/csamfile.pyx":724
- *             # give warning, sam_read1 segfaults
- *             if self.header.n_targets == 0:
- *                 warnings.warn("fetch called for htsfile without header")             # <<<<<<<<<<<<<<
- * 
- *             return IteratorRowAll(self, reopen=reopen)
- */
-  __pyx_tuple__26 = PyTuple_Pack(1, __pyx_kp_s_fetch_called_for_htsfile_without); if (unlikely(!__pyx_tuple__26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__26);
-  __Pyx_GIVEREF(__pyx_tuple__26);
-
-  /* "pysam/csamfile.pyx":777
- *                 break
- *         else:
- *             raise ValueError("mate not found")             # <<<<<<<<<<<<<<
- * 
- *         return mate
- */
-  __pyx_tuple__27 = PyTuple_Pack(1, __pyx_kp_s_mate_not_found); if (unlikely(!__pyx_tuple__27)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__27);
-  __Pyx_GIVEREF(__pyx_tuple__27);
-
-  /* "pysam/csamfile.pyx":802
- * 
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         for read in self.fetch(reference=reference,
- */
-  __pyx_tuple__28 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__28)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__28);
-  __Pyx_GIVEREF(__pyx_tuple__28);
-
-  /* "pysam/csamfile.pyx":868
- * 
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         has_coord, rtid, rstart, rend = self._parseRegion(
- */
-  __pyx_tuple__29 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__29)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__29);
-  __Pyx_GIVEREF(__pyx_tuple__29);
-
-  /* "pysam/csamfile.pyx":875
- *         if self.isbam:
- *             if not self._hasIndex():
- *                 raise ValueError("no index available for pileup")             # <<<<<<<<<<<<<<
- * 
- *             if has_coord:
- */
-  __pyx_tuple__30 = PyTuple_Pack(1, __pyx_kp_s_no_index_available_for_pileup); if (unlikely(!__pyx_tuple__30)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__30);
-  __Pyx_GIVEREF(__pyx_tuple__30);
-
-  /* "pysam/csamfile.pyx":887
- * 
- *         else:
- *             raise NotImplementedError( "pileup of samfiles not implemented yet" )             # <<<<<<<<<<<<<<
- * 
- *     def close( self ):
- */
-  __pyx_tuple__31 = PyTuple_Pack(1, __pyx_kp_s_pileup_of_samfiles_not_implement); if (unlikely(!__pyx_tuple__31)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__31);
-  __Pyx_GIVEREF(__pyx_tuple__31);
-
-  /* "pysam/csamfile.pyx":941
- *         '''number of :term:`reference` sequences in the file.'''
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *             return self.header.n_targets
- * 
- */
-  __pyx_tuple__32 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__32);
-  __Pyx_GIVEREF(__pyx_tuple__32);
-
-  /* "pysam/csamfile.pyx":947
- *         """tuple with the names of :term:`reference` sequences."""
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *             t = []
- *             for x from 0 <= x < self.header.n_targets:
- */
-  __pyx_tuple__33 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__33);
-  __Pyx_GIVEREF(__pyx_tuple__33);
-
-  /* "pysam/csamfile.pyx":958
- *         """
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *             t = []
- *             for x from 0 <= x < self.header.n_targets:
- */
-  __pyx_tuple__34 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__34)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__34);
-  __Pyx_GIVEREF(__pyx_tuple__34);
-
-  /* "pysam/csamfile.pyx":981
- *         an error.'''
- *         if not self._isOpen():
- *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
- *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")
- */
-  __pyx_tuple__35 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__35)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__35);
-  __Pyx_GIVEREF(__pyx_tuple__35);
-
-  /* "pysam/csamfile.pyx":983
- *             raise ValueError("I/O operation on closed file")
- *         if not self.isbam:
- *             raise AttributeError("Samfile.mapped only available in bam files")             # <<<<<<<<<<<<<<
- *         if self.index == NULL:
- *             raise ValueError("mapping information not recorded in index "
- */
-  __pyx_tuple__36 = PyTuple_Pack(1, __pyx_kp_s_Samfile_mapped_only_available_in); if (unlikely(!__pyx_tuple__36)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__36);
-  __Pyx_GIVEREF(__pyx_tuple__36);
-
-  /* "pysam/csamfile.pyx":985
- *             raise AttributeError("Samfile.mapped only available in bam files")
- *         if self.index == NULL:
- *             raise ValueError("mapping information not recorded in index "             # <<<<<<<<<<<<<<
- *                                  "or index not available")
- * 
- */
-  __pyx_tuple__37 = PyTuple_Pack(1, __pyx_kp_s_mapping_information_not_recorded); if (unlikely(!__pyx_tuple__37)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__37);
-  __Pyx_GIVEREF(__pyx_tuple__37);
-
-  /* "pysam/csamfile.pyx":1012
- *         '''full contents of the :term:`sam file` header as a string.'''
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- *             return from_string_and_size(self.header.text, self.header.l_text)
- * 
- */
-  __pyx_tuple__38 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__38)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__38);
-  __Pyx_GIVEREF(__pyx_tuple__38);
-
-  /* "pysam/csamfile.pyx":1020
- *         '''
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *             result = {}
- */
-  __pyx_tuple__39 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__39)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__39);
-  __Pyx_GIVEREF(__pyx_tuple__39);
-
-  /* "pysam/csamfile.pyx":1027
- *                 # convert to python string (note: call self.text to create 0-terminated string)
- *                 t = self.text
- *                 for line in t.split("\n"):             # <<<<<<<<<<<<<<
- *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
- */
-  __pyx_tuple__40 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__40)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1027; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__40);
-  __Pyx_GIVEREF(__pyx_tuple__40);
-
-  /* "pysam/csamfile.pyx":1029
- *                 for line in t.split("\n"):
- *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line             # <<<<<<<<<<<<<<
- *                     fields = line[1:].split("\t")
- *                     record = fields[0]
- */
-  __pyx_tuple__42 = PyTuple_Pack(1, __pyx_kp_s__41); if (unlikely(!__pyx_tuple__42)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1029; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__42);
-  __Pyx_GIVEREF(__pyx_tuple__42);
-
-  /* "pysam/csamfile.pyx":1030
- *                     if not line.strip(): continue
- *                     assert line.startswith("@"), "header line without '@': '%s'" % line
- *                     fields = line[1:].split("\t")             # <<<<<<<<<<<<<<
- *                     record = fields[0]
- *                     assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)
- */
-  __pyx_slice__43 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__43)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__43);
-  __Pyx_GIVEREF(__pyx_slice__43);
-  __pyx_tuple__44 = PyTuple_Pack(1, __pyx_kp_s__3); if (unlikely(!__pyx_tuple__44)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__44);
-  __Pyx_GIVEREF(__pyx_tuple__44);
-
-  /* "pysam/csamfile.pyx":1037
- *                     if record == "CO":
- *                         if record not in result: result[record] = []
- *                         result[record].append( "\t".join( fields[1:] ) )             # <<<<<<<<<<<<<<
- *                         continue
- *                     # the following is clumsy as generators do not work?
- */
-  __pyx_slice__45 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__45)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__45);
-  __Pyx_GIVEREF(__pyx_slice__45);
-
-  /* "pysam/csamfile.pyx":1041
- *                     # the following is clumsy as generators do not work?
- *                     x = {}
- *                     for field in fields[1:]:             # <<<<<<<<<<<<<<
- *                         if ":" not in field:
- *                             raise ValueError("malformatted header: no ':' in field" )
- */
-  __pyx_slice__46 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__46)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__46);
-  __Pyx_GIVEREF(__pyx_slice__46);
-
-  /* "pysam/csamfile.pyx":1043
- *                     for field in fields[1:]:
- *                         if ":" not in field:
- *                             raise ValueError("malformatted header: no ':' in field" )             # <<<<<<<<<<<<<<
- *                         key, value = field.split(":",1)
- *                         # uppercase keys must be valid
- */
-  __pyx_tuple__48 = PyTuple_Pack(1, __pyx_kp_s_malformatted_header_no_in_field); if (unlikely(!__pyx_tuple__48)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__48);
-  __Pyx_GIVEREF(__pyx_tuple__48);
-
-  /* "pysam/csamfile.pyx":1044
- *                         if ":" not in field:
- *                             raise ValueError("malformatted header: no ':' in field" )
- *                         key, value = field.split(":",1)             # <<<<<<<<<<<<<<
- *                         # uppercase keys must be valid
- *                         # lowercase are permitted for user fields
- */
-  __pyx_tuple__49 = PyTuple_Pack(2, __pyx_kp_s__47, __pyx_int_1); if (unlikely(!__pyx_tuple__49)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__49);
-  __Pyx_GIVEREF(__pyx_tuple__49);
-
-  /* "pysam/csamfile.pyx":1144
- *         dest.text = <char*>calloc( len(text), sizeof(char))
- *         dest.l_text = len(text)
- *         cdef bytes btext = text.encode('ascii')             # <<<<<<<<<<<<<<
- *         strncpy( dest.text, btext, dest.l_text )
- * 
- */
-  __pyx_tuple__50 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__50)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1144; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__50);
-  __Pyx_GIVEREF(__pyx_tuple__50);
-
-  /* "pysam/csamfile.pyx":1164
- *                 seqname, seqlen = seqs[x]
- *                 dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
- *                 bseqname = seqname.encode('ascii')             # <<<<<<<<<<<<<<
- *                 strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
- *                 dest.target_len[x] = seqlen
- */
-  __pyx_tuple__51 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__51)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__51);
-  __Pyx_GIVEREF(__pyx_tuple__51);
-
-  /* "pysam/csamfile.pyx":1180
- *     def __iter__(self):
- *         if not self._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         if not self.isbam and self.header.n_targets == 0:
- */
-  __pyx_tuple__52 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__52)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__52);
-  __Pyx_GIVEREF(__pyx_tuple__52);
-
-  /* "pysam/csamfile.pyx":1183
- * 
- *         if not self.isbam and self.header.n_targets == 0:
- *             raise NotImplementedError(             # <<<<<<<<<<<<<<
- *                 "can not iterate over samfile without header")
- *         return self
- */
-  __pyx_tuple__53 = PyTuple_Pack(1, __pyx_kp_s_can_not_iterate_over_samfile_wit); if (unlikely(!__pyx_tuple__53)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__53);
-  __Pyx_GIVEREF(__pyx_tuple__53);
-
-  /* "pysam/csamfile.pyx":1239
- * 
- *         if not samfile._isOpen():
- *             raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
- * 
- *         # makes sure that samfile stays alive as long as the
- */
-  __pyx_tuple__54 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__54)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__54);
-  __Pyx_GIVEREF(__pyx_tuple__54);
-
-  /* "pysam/csamfile.pyx":1298
- * 
- *         if not samfile._hasIndex():
- *             raise ValueError( "no index available for iteration" )             # <<<<<<<<<<<<<<
- * 
- *         self.iter = sam_itr_queryi(
- */
-  __pyx_tuple__55 = PyTuple_Pack(1, __pyx_kp_s_no_index_available_for_iteration); if (unlikely(!__pyx_tuple__55)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1298; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__55);
-  __Pyx_GIVEREF(__pyx_tuple__55);
-
-  /* "pysam/csamfile.pyx":1443
- * 
- *         if not samfile._hasIndex():
- *             raise ValueError("no index available for fetch")             # <<<<<<<<<<<<<<
- * 
- *         self.tid = -1
- */
-  __pyx_tuple__56 = PyTuple_Pack(1, __pyx_kp_s_no_index_available_for_fetch); if (unlikely(!__pyx_tuple__56)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__56);
-  __Pyx_GIVEREF(__pyx_tuple__56);
-
-  /* "pysam/csamfile.pyx":1805
- *             self.cnext()
- *             if self.n_plp < 0:
- *                 raise ValueError("error during iteration" )             # <<<<<<<<<<<<<<
- * 
- *             if self.plp == NULL:
- */
-  __pyx_tuple__58 = PyTuple_Pack(1, __pyx_kp_s_error_during_iteration); if (unlikely(!__pyx_tuple__58)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__58);
-  __Pyx_GIVEREF(__pyx_tuple__58);
-
-  /* "pysam/csamfile.pyx":1843
- * 
- *             if self.n_plp < 0:
- *                 raise ValueError("error during iteration" )             # <<<<<<<<<<<<<<
- * 
- *             # return result, if within same reference
- */
-  __pyx_tuple__59 = PyTuple_Pack(1, __pyx_kp_s_error_during_iteration); if (unlikely(!__pyx_tuple__59)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__59);
-  __Pyx_GIVEREF(__pyx_tuple__59);
-
-  /* "pysam/csamfile.pyx":1978
- * 
- *     if not type(pytag) is bytes:
- *         pytag = pytag.encode('ascii')             # <<<<<<<<<<<<<<
- *     t = type(value)
- * 
- */
-  __pyx_tuple__60 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__60)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1978; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__60);
-  __Pyx_GIVEREF(__pyx_tuple__60);
-
-  /* "pysam/csamfile.pyx":2005
- * 
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),
- */
-  __pyx_slice__61 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__61)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2005; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__61);
-  __Pyx_GIVEREF(__pyx_slice__61);
-
-  /* "pysam/csamfile.pyx":2006
- *         datafmt = "2sccI%i%s" % (len(value), datafmt)
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       datatype.encode('ascii'),
- *                       len(value)] + list(value) )
- */
-  __pyx_tuple__62 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__62)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__62);
-  __Pyx_GIVEREF(__pyx_tuple__62);
-
-  /* "pysam/csamfile.pyx":2007
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),
- *                       datatype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       len(value)] + list(value) )
- *         fmts.append( datafmt )
- */
-  __pyx_tuple__63 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__63)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__63);
-  __Pyx_GIVEREF(__pyx_tuple__63);
-
-  /* "pysam/csamfile.pyx":2030
- *         # Note: hex strings (H) are not supported yet
- *         if t is not bytes:
- *             value = value.encode('ascii')             # <<<<<<<<<<<<<<
- *         if len(value) == 1:
- *             fmt, pytype = "2scc", 'A'
- */
-  __pyx_tuple__64 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__64)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2030; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__64);
-  __Pyx_GIVEREF(__pyx_tuple__64);
-
-  /* "pysam/csamfile.pyx":2036
- *             fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- * 
- *     args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                   pytype.encode('ascii'),
- *                   value ] )
- */
-  __pyx_slice__65 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__65)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2036; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__65);
-  __Pyx_GIVEREF(__pyx_slice__65);
-
-  /* "pysam/csamfile.pyx":2037
- * 
- *     args.extend( [pytag[:2],
- *                   pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                   value ] )
- * 
- */
-  __pyx_tuple__66 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__66)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__66);
-  __Pyx_GIVEREF(__pyx_tuple__66);
-
-  /* "pysam/csamfile.pyx":2104
- *             qual = self.qual
- *         else:
- *             seq = self.seq.decode('ascii')             # <<<<<<<<<<<<<<
- *             qual = self.qual.decode('ascii')
- *         return "\t".join(map(str, (self.qname,
- */
-  __pyx_tuple__67 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__67)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__67);
-  __Pyx_GIVEREF(__pyx_tuple__67);
-
-  /* "pysam/csamfile.pyx":2105
- *         else:
- *             seq = self.seq.decode('ascii')
- *             qual = self.qual.decode('ascii')             # <<<<<<<<<<<<<<
- *         return "\t".join(map(str, (self.qname,
- *                                    self.flag,
- */
-  __pyx_tuple__68 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__68)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__68);
-  __Pyx_GIVEREF(__pyx_tuple__68);
-
-  /* "pysam/csamfile.pyx":2156
- * 
- *         if not type(pytag) is bytes:
- *             pytag = pytag.encode('ascii')             # <<<<<<<<<<<<<<
- *         t = type(value)
- * 
- */
-  __pyx_tuple__69 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__69)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__69);
-  __Pyx_GIVEREF(__pyx_tuple__69);
-
-  /* "pysam/csamfile.pyx":2183
- * 
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),
- */
-  __pyx_slice__70 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__70)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__70);
-  __Pyx_GIVEREF(__pyx_slice__70);
-
-  /* "pysam/csamfile.pyx":2184
- *             datafmt = "2sccI%i%s" % (len(value), datafmt)
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                           datatype.encode('ascii'),
- *                           len(value)] + list(value) )
- */
-  __pyx_tuple__71 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__71)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__71);
-  __Pyx_GIVEREF(__pyx_tuple__71);
-
-  /* "pysam/csamfile.pyx":2185
- *             args.extend( [pytag[:2],
- *                           pytype.encode('ascii'),
- *                           datatype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                           len(value)] + list(value) )
- *             fmts.append( datafmt )
- */
-  __pyx_tuple__72 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__72)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__72);
-  __Pyx_GIVEREF(__pyx_tuple__72);
-
-  /* "pysam/csamfile.pyx":2208
- *             # Note: hex strings (H) are not supported yet
- *             if t is not bytes:
- *                 value = value.encode('ascii')             # <<<<<<<<<<<<<<
- *             if len(value) == 1:
- *                 fmt, pytype = "2scc", 'A'
- */
-  __pyx_tuple__73 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__73)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__73);
-  __Pyx_GIVEREF(__pyx_tuple__73);
-
-  /* "pysam/csamfile.pyx":2214
- *                 fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
- * 
- *         args.extend( [pytag[:2],             # <<<<<<<<<<<<<<
- *                       pytype.encode('ascii'),
- *                       value ] )
- */
-  __pyx_slice__74 = PySlice_New(Py_None, __pyx_int_2, Py_None); if (unlikely(!__pyx_slice__74)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_slice__74);
-  __Pyx_GIVEREF(__pyx_slice__74);
-
-  /* "pysam/csamfile.pyx":2215
- * 
- *         args.extend( [pytag[:2],
- *                       pytype.encode('ascii'),             # <<<<<<<<<<<<<<
- *                       value ] )
- * 
- */
-  __pyx_tuple__75 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__75)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__75);
-  __Pyx_GIVEREF(__pyx_tuple__75);
-
-  /* "pysam/csamfile.pyx":2731
- *         type_code = get_type_code(value, value_type)
- *         if type_code == 0:
- *             raise ValueError("can't guess type or invalid type code specified")             # <<<<<<<<<<<<<<
- * 
- *         # Not Endian-safe, but then again neither is samtools!
- */
-  __pyx_tuple__77 = PyTuple_Pack(1, __pyx_kp_s_can_t_guess_type_or_invalid_type); if (unlikely(!__pyx_tuple__77)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2731; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__77);
-  __Pyx_GIVEREF(__pyx_tuple__77);
-
-  /* "pysam/csamfile.pyx":2751
- *             value_size   = sizeof(float)
- *         else:
- *             raise ValueError('Unsupported value_type in set_option')             # <<<<<<<<<<<<<<
- * 
- *         tag = _forceBytes( tag )
- */
-  __pyx_tuple__78 = PyTuple_Pack(1, __pyx_kp_s_Unsupported_value_type_in_set_op); if (unlikely(!__pyx_tuple__78)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__78);
-  __Pyx_GIVEREF(__pyx_tuple__78);
-
-  /* "pysam/csamfile.pyx":3253
- *     '''
- *     def __init__(self):
- *         raise TypeError("This class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
- * 
- *     def __str__(self):
- */
-  __pyx_tuple__81 = PyTuple_Pack(1, __pyx_kp_s_This_class_cannot_be_instantiate); if (unlikely(!__pyx_tuple__81)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3253; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__81);
-  __Pyx_GIVEREF(__pyx_tuple__81);
-
-  /* "pysam/csamfile.pyx":3279
- * 
- *             if self.plp == NULL or self.plp[0] == NULL:
- *                 raise ValueError("PileupProxy accessed after iterator finished")             # <<<<<<<<<<<<<<
- * 
- *             # warning: there could be problems if self.n and self.buf are
- */
-  __pyx_tuple__82 = PyTuple_Pack(1, __pyx_kp_s_PileupProxy_accessed_after_itera); if (unlikely(!__pyx_tuple__82)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__82);
-  __Pyx_GIVEREF(__pyx_tuple__82);
-
-  /* "pysam/csamfile.pyx":3292
- * 
- *     def __init__(self):
- *         raise TypeError("This class cannot be instantiated from Python")             # <<<<<<<<<<<<<<
- * 
- *     def __str__(self):
- */
-  __pyx_tuple__83 = PyTuple_Pack(1, __pyx_kp_s_This_class_cannot_be_instantiate); if (unlikely(!__pyx_tuple__83)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__83);
-  __Pyx_GIVEREF(__pyx_tuple__83);
-
-  /* "pysam/csamfile.pyx":108
- * else:
- *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
- * CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )             # <<<<<<<<<<<<<<
- * 
- * #####################################################################
- */
-  __pyx_tuple__84 = PyTuple_Pack(1, __pyx_kp_s_d_MIDNSHP_X); if (unlikely(!__pyx_tuple__84)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__84);
-  __Pyx_GIVEREF(__pyx_tuple__84);
-
-  /* "pysam/csamfile.pyx":210
- *         list of reads (:class:`pysam.PileupRead`) aligned to this column
- *     '''
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )
- */
-  __pyx_tuple__85 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__85)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__85);
-  __Pyx_GIVEREF(__pyx_tuple__85);
-  __pyx_codeobj__86 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__85, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_str, 210, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__86)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":223
- * 
- * # order of records within sam headers
- * VALID_HEADERS = ("HD", "SQ", "RG", "PG", "CO")             # <<<<<<<<<<<<<<
- * 
- * # type conversions within sam header records
- */
-  __pyx_tuple__87 = PyTuple_Pack(5, __pyx_n_s_HD, __pyx_n_s_SQ, __pyx_n_s_RG, __pyx_n_s_PG, __pyx_n_s_CO); if (unlikely(!__pyx_tuple__87)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__87);
-  __Pyx_GIVEREF(__pyx_tuple__87);
-
-  /* "pysam/csamfile.pyx":237
- * 
- * # output order of fields within records
- * VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),             # <<<<<<<<<<<<<<
- *                       "SQ" : ("SN", "LN", "AS", "M5",
- *                                "UR", "SP"),
- */
-  __pyx_tuple__88 = PyTuple_Pack(3, __pyx_n_s_VN, __pyx_n_s_SO, __pyx_n_s_GO); if (unlikely(!__pyx_tuple__88)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__88);
-  __Pyx_GIVEREF(__pyx_tuple__88);
-
-  /* "pysam/csamfile.pyx":238
- * # output order of fields within records
- * VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),
- *                       "SQ" : ("SN", "LN", "AS", "M5",             # <<<<<<<<<<<<<<
- *                                "UR", "SP"),
- *                       "RG" : ("ID", "SM", "LB", "DS",
- */
-  __pyx_tuple__89 = PyTuple_Pack(6, __pyx_n_s_SN, __pyx_n_s_LN, __pyx_n_s_AS, __pyx_n_s_M5, __pyx_n_s_UR, __pyx_n_s_SP); if (unlikely(!__pyx_tuple__89)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__89);
-  __Pyx_GIVEREF(__pyx_tuple__89);
-
-  /* "pysam/csamfile.pyx":240
- *                       "SQ" : ("SN", "LN", "AS", "M5",
- *                                "UR", "SP"),
- *                       "RG" : ("ID", "SM", "LB", "DS",             # <<<<<<<<<<<<<<
- *                               "PU", "PI", "CN", "DT",
- *                               "PL", "FO", "KS", "PG"),
- */
-  __pyx_tuple__90 = PyTuple_Pack(12, __pyx_n_s_ID, __pyx_n_s_SM, __pyx_n_s_LB, __pyx_n_s_DS, __pyx_n_s_PU, __pyx_n_s_PI, __pyx_n_s_CN, __pyx_n_s_DT, __pyx_n_s_PL, __pyx_n_s_FO, __pyx_n_s_KS, __pyx_n_s_PG); if (unlikely(!__pyx_tuple__90)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 240; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__90);
-  __Pyx_GIVEREF(__pyx_tuple__90);
-
-  /* "pysam/csamfile.pyx":243
- *                               "PU", "PI", "CN", "DT",
- *                               "PL", "FO", "KS", "PG"),
- *                       "PG" : ("PN", "ID", "VN", "CL",             # <<<<<<<<<<<<<<
- *                               "PP"),}
- * 
- */
-  __pyx_tuple__91 = PyTuple_Pack(5, __pyx_n_s_PN, __pyx_n_s_ID, __pyx_n_s_VN, __pyx_n_s_CL, __pyx_n_s_PP); if (unlikely(!__pyx_tuple__91)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__91);
-  __Pyx_GIVEREF(__pyx_tuple__91);
-  __Pyx_RefNannyFinishContext();
-  return 0;
-  __pyx_L1_error:;
-  __Pyx_RefNannyFinishContext();
-  return -1;
-}
-
-static int __Pyx_InitGlobals(void) {
-  if (__Pyx_InitStrings(__pyx_string_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  __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_error;}
-  __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_error;}
-  __pyx_int_2 = PyInt_FromLong(2); if (unlikely(!__pyx_int_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_3 = PyInt_FromLong(3); if (unlikely(!__pyx_int_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_255 = PyInt_FromLong(255); if (unlikely(!__pyx_int_255)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_8000 = PyInt_FromLong(8000); if (unlikely(!__pyx_int_8000)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_65535 = PyInt_FromLong(65535L); if (unlikely(!__pyx_int_65535)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_536870912 = PyInt_FromLong(536870912L); if (unlikely(!__pyx_int_536870912)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_4294967295 = PyInt_FromString((char *)"4294967295", 0, 0); if (unlikely(!__pyx_int_4294967295)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_neg_4 = PyInt_FromLong(-4); if (unlikely(!__pyx_int_neg_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_neg_127 = PyInt_FromLong(-127); if (unlikely(!__pyx_int_neg_127)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_neg_32767 = PyInt_FromLong(-32767L); if (unlikely(!__pyx_int_neg_32767)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_int_neg_2147483648 = PyInt_FromLong(-2147483648L); if (unlikely(!__pyx_int_neg_2147483648)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  return 0;
-  __pyx_L1_error:;
-  return -1;
-}
-
-#if PY_MAJOR_VERSION < 3
-PyMODINIT_FUNC initcsamfile(void); /*proto*/
-PyMODINIT_FUNC initcsamfile(void)
-#else
-PyMODINIT_FUNC PyInit_csamfile(void); /*proto*/
-PyMODINIT_FUNC PyInit_csamfile(void)
-#endif
-{
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_t_3;
-  int __pyx_t_4;
-  PyObject *__pyx_t_5 = NULL;
-  PyObject *__pyx_t_6 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_RefNannyDeclarations
-  #if CYTHON_REFNANNY
-  __Pyx_RefNanny = __Pyx_RefNannyImportAPI("refnanny");
-  if (!__Pyx_RefNanny) {
-      PyErr_Clear();
-      __Pyx_RefNanny = __Pyx_RefNannyImportAPI("Cython.Runtime.refnanny");
-      if (!__Pyx_RefNanny)
-          Py_FatalError("failed to import 'refnanny' module");
-  }
-  #endif
-  __Pyx_RefNannySetupContext("PyMODINIT_FUNC PyInit_csamfile(void)", 0);
-  if ( __Pyx_check_binary_version() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __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_error;}
-  __pyx_empty_bytes = PyBytes_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_bytes)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  #ifdef __Pyx_CyFunction_USED
-  if (__Pyx_CyFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  #endif
-  #ifdef __Pyx_FusedFunction_USED
-  if (__pyx_FusedFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  #endif
-  #ifdef __Pyx_Generator_USED
-  if (__pyx_Generator_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  #endif
-  /*--- Library function declarations ---*/
-  /*--- Threads initialization code ---*/
-  #if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS
-  #ifdef WITH_THREAD /* Python build with threading support? */
-  PyEval_InitThreads();
-  #endif
-  #endif
-  /*--- Module creation code ---*/
-  #if PY_MAJOR_VERSION < 3
-  __pyx_m = Py_InitModule4(__Pyx_NAMESTR("csamfile"), __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m);
-  #else
-  __pyx_m = PyModule_Create(&__pyx_moduledef);
-  #endif
-  if (unlikely(!__pyx_m)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  Py_INCREF(__pyx_d);
-  __pyx_b = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME)); if (unlikely(!__pyx_b)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  #if CYTHON_COMPILING_IN_PYPY
-  Py_INCREF(__pyx_b);
-  #endif
-  if (__Pyx_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  /*--- Initialize various global constants etc. ---*/
-  if (unlikely(__Pyx_InitGlobals() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT)
-  if (__Pyx_init_sys_getdefaultencoding_params() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  #endif
-  if (__pyx_module_is_main_pysam__csamfile) {
-    if (__Pyx_SetAttrString(__pyx_m, "__name__", __pyx_n_s_main) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  }
-  #if PY_MAJOR_VERSION >= 3
-  {
-    PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (!PyDict_GetItemString(modules, "pysam.csamfile")) {
-      if (unlikely(PyDict_SetItemString(modules, "pysam.csamfile", __pyx_m) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
-  }
-  #endif
-  /*--- Builtin init code ---*/
-  if (unlikely(__Pyx_InitCachedBuiltins() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  /*--- Constants init code ---*/
-  if (unlikely(__Pyx_InitCachedConstants() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  /*--- Global init code ---*/
-  __pyx_v_5pysam_8csamfile__FILENAME_ENCODING = ((PyObject*)Py_None); Py_INCREF(Py_None);
-  /*--- Variable export code ---*/
-  /*--- Function export code ---*/
-  /*--- Type init code ---*/
-  __pyx_vtabptr_5pysam_8csamfile_AlignedRead = &__pyx_vtable_5pysam_8csamfile_AlignedRead;
-  __pyx_vtable_5pysam_8csamfile_AlignedRead.setTag = (PyObject *(*)(struct __pyx_obj_5pysam_8csamfile_AlignedRead *, PyObject *, PyObject *, int __pyx_skip_dispatch, struct __pyx_opt_args_5pysam_8csamfile_11AlignedRead_setTag *__pyx_optional_args))__pyx_f_5pysam_8csamfile_11AlignedRead_setTag;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_AlignedRead.tp_print = 0;
-  #if CYTHON_COMPILING_IN_CPYTHON
-  {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_AlignedRead, "__str__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_11AlignedRead_4__str__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_11AlignedRead_4__str__.doc = __pyx_doc_5pysam_8csamfile_11AlignedRead_4__str__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_11AlignedRead_4__str__;
-    }
-  }
-  #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_AlignedRead.tp_dict, __pyx_vtabptr_5pysam_8csamfile_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "AlignedRead", (PyObject *)&__pyx_type_5pysam_8csamfile_AlignedRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_AlignedRead = &__pyx_type_5pysam_8csamfile_AlignedRead;
-  __pyx_vtabptr_5pysam_8csamfile_Samfile = &__pyx_vtable_5pysam_8csamfile_Samfile;
-  __pyx_vtable_5pysam_8csamfile_Samfile._buildHeader = (bam_hdr_t *(*)(struct __pyx_obj_5pysam_8csamfile_Samfile *, PyObject *))__pyx_f_5pysam_8csamfile_7Samfile__buildHeader;
-  __pyx_vtable_5pysam_8csamfile_Samfile.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_Samfile *))__pyx_f_5pysam_8csamfile_7Samfile_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_Samfile.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_Samfile *))__pyx_f_5pysam_8csamfile_7Samfile_cnext;
-  __pyx_vtable_5pysam_8csamfile_Samfile.write = (int (*)(struct __pyx_obj_5pysam_8csamfile_Samfile *, struct __pyx_obj_5pysam_8csamfile_AlignedRead *, int __pyx_skip_dispatch))__pyx_f_5pysam_8csamfile_7Samfile_write;
-  __pyx_vtable_5pysam_8csamfile_Samfile._getrname = (char *(*)(struct __pyx_obj_5pysam_8csamfile_Samfile *, int))__pyx_f_5pysam_8csamfile_7Samfile__getrname;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_Samfile.tp_print = 0;
-  #if CYTHON_COMPILING_IN_CPYTHON
-  {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_Samfile, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_7Samfile_46__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_7Samfile_46__next__.doc = __pyx_doc_5pysam_8csamfile_7Samfile_46__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_7Samfile_46__next__;
-    }
-  }
-  #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_Samfile.tp_dict, __pyx_vtabptr_5pysam_8csamfile_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "Samfile", (PyObject *)&__pyx_type_5pysam_8csamfile_Samfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_Samfile = &__pyx_type_5pysam_8csamfile_Samfile;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_PileupProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_PileupProxy.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "PileupProxy", (PyObject *)&__pyx_type_5pysam_8csamfile_PileupProxy) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3235; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_PileupProxy = &__pyx_type_5pysam_8csamfile_PileupProxy;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_PileupRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3287; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_PileupRead.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "PileupRead", (PyObject *)&__pyx_type_5pysam_8csamfile_PileupRead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3287; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_PileupRead = &__pyx_type_5pysam_8csamfile_PileupRead;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRow) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRow.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRow", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRow) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRow = &__pyx_type_5pysam_8csamfile_IteratorRow;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorRowRegion = &__pyx_vtable_5pysam_8csamfile_IteratorRowRegion;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowRegion.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *))__pyx_f_5pysam_8csamfile_17IteratorRowRegion_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowRegion.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowRegion *))__pyx_f_5pysam_8csamfile_17IteratorRowRegion_cnext;
-  __pyx_type_5pysam_8csamfile_IteratorRowRegion.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowRegion.tp_print = 0;
-  #if CYTHON_COMPILING_IN_CPYTHON
-  {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowRegion, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_17IteratorRowRegion_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_17IteratorRowRegion_4__next__.doc = __pyx_doc_5pysam_8csamfile_17IteratorRowRegion_4__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_17IteratorRowRegion_4__next__;
-    }
-  }
-  #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorRowRegion.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowRegion", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1266; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowRegion = &__pyx_type_5pysam_8csamfile_IteratorRowRegion;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorRowHead = &__pyx_vtable_5pysam_8csamfile_IteratorRowHead;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowHead.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *))__pyx_f_5pysam_8csamfile_15IteratorRowHead_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowHead.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowHead *))__pyx_f_5pysam_8csamfile_15IteratorRowHead_cnext;
-  __pyx_type_5pysam_8csamfile_IteratorRowHead.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowHead.tp_print = 0;
-  #if CYTHON_COMPILING_IN_CPYTHON
-  {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowHead, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_15IteratorRowHead_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_15IteratorRowHead_4__next__.doc = __pyx_doc_5pysam_8csamfile_15IteratorRowHead_4__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_15IteratorRowHead_4__next__;
-    }
-  }
-  #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorRowHead.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorRowHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowHead", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowHead = &__pyx_type_5pysam_8csamfile_IteratorRowHead;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorRowAll = &__pyx_vtable_5pysam_8csamfile_IteratorRowAll;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowAll.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *))__pyx_f_5pysam_8csamfile_14IteratorRowAll_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowAll.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowAll *))__pyx_f_5pysam_8csamfile_14IteratorRowAll_cnext;
-  __pyx_type_5pysam_8csamfile_IteratorRowAll.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowAll.tp_print = 0;
-  #if CYTHON_COMPILING_IN_CPYTHON
-  {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowAll, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_14IteratorRowAll_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_14IteratorRowAll_4__next__.doc = __pyx_doc_5pysam_8csamfile_14IteratorRowAll_4__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_14IteratorRowAll_4__next__;
-    }
-  }
-  #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorRowAll.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowAll", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowAll) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowAll = &__pyx_type_5pysam_8csamfile_IteratorRowAll;
-  __pyx_type_5pysam_8csamfile_IteratorRowAllRefs.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowAllRefs.tp_print = 0;
-  #if CYTHON_COMPILING_IN_CPYTHON
-  {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowAllRefs, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_18IteratorRowAllRefs_6__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_18IteratorRowAllRefs_6__next__.doc = __pyx_doc_5pysam_8csamfile_18IteratorRowAllRefs_6__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_18IteratorRowAllRefs_6__next__;
-    }
-  }
-  #endif
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowAllRefs", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowAllRefs = &__pyx_type_5pysam_8csamfile_IteratorRowAllRefs;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorRowSelection = &__pyx_vtable_5pysam_8csamfile_IteratorRowSelection;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowSelection.getCurrent = (bam1_t *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *))__pyx_f_5pysam_8csamfile_20IteratorRowSelection_getCurrent;
-  __pyx_vtable_5pysam_8csamfile_IteratorRowSelection.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorRowSelection *))__pyx_f_5pysam_8csamfile_20IteratorRowSelection_cnext;
-  __pyx_type_5pysam_8csamfile_IteratorRowSelection.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorRow;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorRowSelection) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorRowSelection.tp_print = 0;
-  #if CYTHON_COMPILING_IN_CPYTHON
-  {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowSelection, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_20IteratorRowSelection_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_20IteratorRowSelection_4__next__.doc = __pyx_doc_5pysam_8csamfile_20IteratorRowSelection_4__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_20IteratorRowSelection_4__next__;
-    }
-  }
-  #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorRowSelection.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorRowSelection) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorRowSelection", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorRowSelection) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorRowSelection = &__pyx_type_5pysam_8csamfile_IteratorRowSelection;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorColumn = &__pyx_vtable_5pysam_8csamfile_IteratorColumn;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.cnext = (int (*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *))__pyx_f_5pysam_8csamfile_14IteratorColumn_cnext;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.getSequence = (char *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *))__pyx_f_5pysam_8csamfile_14IteratorColumn_getSequence;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.setMask = (PyObject *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, PyObject *))__pyx_f_5pysam_8csamfile_14IteratorColumn_setMask;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.setupIteratorData = (PyObject *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, int, int, int, struct __pyx_opt_args_5pysam_8csamfile_14IteratorColumn_setupIteratorData *__pyx_optional_args))__pyx_f_5pysam_8csamfile_14IteratorColumn_setupIteratorData;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumn.reset = (PyObject *(*)(struct __pyx_obj_5pysam_8csamfile_IteratorColumn *, PyObject *, PyObject *, PyObject *))__pyx_f_5pysam_8csamfile_14IteratorColumn_reset;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorColumn.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorColumn.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorColumn", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumn) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorColumn = &__pyx_type_5pysam_8csamfile_IteratorColumn;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorColumnRegion = &__pyx_vtable_5pysam_8csamfile_IteratorColumnRegion;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumnRegion.__pyx_base = *__pyx_vtabptr_5pysam_8csamfile_IteratorColumn;
-  __pyx_type_5pysam_8csamfile_IteratorColumnRegion.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorColumn;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorColumnRegion.tp_print = 0;
-  #if CYTHON_COMPILING_IN_CPYTHON
-  {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumnRegion, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_20IteratorColumnRegion_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_20IteratorColumnRegion_2__next__.doc = __pyx_doc_5pysam_8csamfile_20IteratorColumnRegion_2__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_20IteratorColumnRegion_2__next__;
-    }
-  }
-  #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorColumnRegion.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorColumnRegion", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumnRegion) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorColumnRegion = &__pyx_type_5pysam_8csamfile_IteratorColumnRegion;
-  __pyx_vtabptr_5pysam_8csamfile_IteratorColumnAllRefs = &__pyx_vtable_5pysam_8csamfile_IteratorColumnAllRefs;
-  __pyx_vtable_5pysam_8csamfile_IteratorColumnAllRefs.__pyx_base = *__pyx_vtabptr_5pysam_8csamfile_IteratorColumn;
-  __pyx_type_5pysam_8csamfile_IteratorColumnAllRefs.tp_base = __pyx_ptype_5pysam_8csamfile_IteratorColumn;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IteratorColumnAllRefs.tp_print = 0;
-  #if CYTHON_COMPILING_IN_CPYTHON
-  {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__.doc = __pyx_doc_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_8csamfile_21IteratorColumnAllRefs_2__next__;
-    }
-  }
-  #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs.tp_dict, __pyx_vtabptr_5pysam_8csamfile_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "IteratorColumnAllRefs", (PyObject *)&__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IteratorColumnAllRefs = &__pyx_type_5pysam_8csamfile_IteratorColumnAllRefs;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_IndexedReads) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_IndexedReads.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "IndexedReads", (PyObject *)&__pyx_type_5pysam_8csamfile_IndexedReads) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_IndexedReads = &__pyx_type_5pysam_8csamfile_IndexedReads;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile_SNPCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile_SNPCall.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "SNPCall", (PyObject *)&__pyx_type_5pysam_8csamfile_SNPCall) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_8csamfile_SNPCall = &__pyx_type_5pysam_8csamfile_SNPCall;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr.tp_print = 0;
-  __pyx_ptype_5pysam_8csamfile___pyx_scope_struct__genexpr = &__pyx_type_5pysam_8csamfile___pyx_scope_struct__genexpr;
-  if (PyType_Ready(&__pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr.tp_print = 0;
-  __pyx_ptype_5pysam_8csamfile___pyx_scope_struct_1_genexpr = &__pyx_type_5pysam_8csamfile___pyx_scope_struct_1_genexpr;
-  /*--- Type import code ---*/
-  __pyx_ptype_5pysam_6cfaidx_Fastafile = __Pyx_ImportType("pysam.cfaidx", "Fastafile", sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastafile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_Fastafile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6cfaidx_Fastafile = (struct __pyx_vtabstruct_5pysam_6cfaidx_Fastafile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_Fastafile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_Fastafile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6cfaidx_FastqProxy = __Pyx_ImportType("pysam.cfaidx", "FastqProxy", sizeof(struct __pyx_obj_5pysam_6cfaidx_FastqProxy), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_FastqProxy)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6cfaidx_Fastqfile = __Pyx_ImportType("pysam.cfaidx", "Fastqfile", sizeof(struct __pyx_obj_5pysam_6cfaidx_Fastqfile), 1); if (unlikely(!__pyx_ptype_5pysam_6cfaidx_Fastqfile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6cfaidx_Fastqfile = (struct __pyx_vtabstruct_5pysam_6cfaidx_Fastqfile*)__Pyx_GetVtable(__pyx_ptype_5pysam_6cfaidx_Fastqfile->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6cfaidx_Fastqfile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "type", 
-  #if CYTHON_COMPILING_IN_PYPY
-  sizeof(PyTypeObject),
-  #else
-  sizeof(PyHeapTypeObject),
-  #endif
-  0); if (unlikely(!__pyx_ptype_7cpython_4type_type)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_7cpython_4bool_bool = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "bool", sizeof(PyBoolObject), 0); if (unlikely(!__pyx_ptype_7cpython_4bool_bool)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_7cpython_7complex_complex = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "complex", sizeof(PyComplexObject), 0); if (unlikely(!__pyx_ptype_7cpython_7complex_complex)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  /*--- Variable import code ---*/
-  /*--- Function import code ---*/
-  /*--- Execution code ---*/
-
-  /* "pysam/csamfile.pyx":4
- * # cython: profile=True
- * # adds doc-strings for sphinx
- * import tempfile             # <<<<<<<<<<<<<<
- * import os
- * import sys
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_tempfile, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tempfile, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":5
- * # adds doc-strings for sphinx
- * import tempfile
- * import os             # <<<<<<<<<<<<<<
- * import sys
- * import types
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_os, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_os, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":6
- * import tempfile
- * import os
- * import sys             # <<<<<<<<<<<<<<
- * import types
- * import itertools
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_sys, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_sys, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":7
- * import os
- * import sys
- * import types             # <<<<<<<<<<<<<<
- * import itertools
- * import struct
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_types, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_types, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":8
- * import sys
- * import types
- * import itertools             # <<<<<<<<<<<<<<
- * import struct
- * import ctypes
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_itertools, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_itertools, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":9
- * import types
- * import itertools
- * import struct             # <<<<<<<<<<<<<<
- * import ctypes
- * import collections
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_struct, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_struct, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":10
- * import itertools
- * import struct
- * import ctypes             # <<<<<<<<<<<<<<
- * import collections
- * import re
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_ctypes, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_ctypes, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":11
- * import struct
- * import ctypes
- * import collections             # <<<<<<<<<<<<<<
- * import re
- * import platform
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_collections, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_collections, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":12
- * import ctypes
- * import collections
- * import re             # <<<<<<<<<<<<<<
- * import platform
- * import warnings
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_re, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_re, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":13
- * import collections
- * import re
- * import platform             # <<<<<<<<<<<<<<
- * import warnings
- * from cpython cimport PyErr_SetString, \
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_platform, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_platform, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":14
- * import re
- * import platform
- * import warnings             # <<<<<<<<<<<<<<
- * from cpython cimport PyErr_SetString, \
- *     PyBytes_Check, \
- */
-  __pyx_t_1 = __Pyx_Import(__pyx_n_s_warnings, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_warnings, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":27
- * ## Python 3 compatibility functions
- * ########################################################################
- * IS_PYTHON3 = PY_MAJOR_VERSION >= 3             # <<<<<<<<<<<<<<
- * cdef from_string_and_size(char* s, size_t length):
- *     if PY_MAJOR_VERSION < 3:
- */
-  __pyx_t_1 = __Pyx_PyBool_FromLong((PY_MAJOR_VERSION >= 3)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 27; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_IS_PYTHON3, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 27; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":36
- * # filename encoding (copied from lxml.etree.pyx)
- * cdef str _FILENAME_ENCODING
- * _FILENAME_ENCODING = sys.getfilesystemencoding()             # <<<<<<<<<<<<<<
- * if _FILENAME_ENCODING is None:
- *     _FILENAME_ENCODING = sys.getdefaultencoding()
- */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getfilesystemencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_XGOTREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-  __Pyx_DECREF_SET(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
-  __Pyx_GIVEREF(__pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":37
- * cdef str _FILENAME_ENCODING
- * _FILENAME_ENCODING = sys.getfilesystemencoding()
- * if _FILENAME_ENCODING is None:             # <<<<<<<<<<<<<<
- *     _FILENAME_ENCODING = sys.getdefaultencoding()
- * if _FILENAME_ENCODING is None:
- */
-  __pyx_t_3 = (__pyx_v_5pysam_8csamfile__FILENAME_ENCODING == ((PyObject*)Py_None));
-  __pyx_t_4 = (__pyx_t_3 != 0);
-  if (__pyx_t_4) {
-
-    /* "pysam/csamfile.pyx":38
- * _FILENAME_ENCODING = sys.getfilesystemencoding()
- * if _FILENAME_ENCODING is None:
- *     _FILENAME_ENCODING = sys.getdefaultencoding()             # <<<<<<<<<<<<<<
- * if _FILENAME_ENCODING is None:
- *     _FILENAME_ENCODING = 'ascii'
- */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getdefaultencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_XGOTREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    __Pyx_DECREF_SET(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_1 = 0;
-    goto __pyx_L2;
-  }
-  __pyx_L2:;
-
-  /* "pysam/csamfile.pyx":39
- * if _FILENAME_ENCODING is None:
- *     _FILENAME_ENCODING = sys.getdefaultencoding()
- * if _FILENAME_ENCODING is None:             # <<<<<<<<<<<<<<
- *     _FILENAME_ENCODING = 'ascii'
- * 
- */
-  __pyx_t_4 = (__pyx_v_5pysam_8csamfile__FILENAME_ENCODING == ((PyObject*)Py_None));
-  __pyx_t_3 = (__pyx_t_4 != 0);
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":40
- *     _FILENAME_ENCODING = sys.getdefaultencoding()
- * if _FILENAME_ENCODING is None:
- *     _FILENAME_ENCODING = 'ascii'             # <<<<<<<<<<<<<<
- * 
- * #cdef char* _C_FILENAME_ENCODING
- */
-    __Pyx_INCREF(__pyx_n_s_ascii);
-    __Pyx_XGOTREF(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING);
-    __Pyx_DECREF_SET(__pyx_v_5pysam_8csamfile__FILENAME_ENCODING, __pyx_n_s_ascii);
-    __Pyx_GIVEREF(__pyx_n_s_ascii);
-    goto __pyx_L3;
-  }
-  __pyx_L3:;
-
-  /* "pysam/csamfile.pyx":103
- * DEF SEEK_END = 2
- * 
- * cdef char* CODE2CIGAR= "MIDNSHP=X"             # <<<<<<<<<<<<<<
- * if IS_PYTHON3:
- *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
- */
-  __pyx_v_5pysam_8csamfile_CODE2CIGAR = __pyx_k_MIDNSHP_X;
-
-  /* "pysam/csamfile.pyx":104
- * 
- * cdef char* CODE2CIGAR= "MIDNSHP=X"
- * if IS_PYTHON3:             # <<<<<<<<<<<<<<
- *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
- * else:
- */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_IS_PYTHON3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (__pyx_t_3) {
-
-    /* "pysam/csamfile.pyx":105
- * cdef char* CODE2CIGAR= "MIDNSHP=X"
- * if IS_PYTHON3:
- *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
- * else:
- *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
- */
-    __pyx_t_1 = __pyx_pf_5pysam_8csamfile_genexpr(NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyDict_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (PyDict_SetItem(__pyx_d, __pyx_n_s_CIGAR2CODE, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    goto __pyx_L4;
-  }
-  /*else*/ {
-
-    /* "pysam/csamfile.pyx":107
- *     CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
- * else:
- *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )             # <<<<<<<<<<<<<<
- * CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )
- * 
- */
-    __pyx_t_1 = __pyx_pf_5pysam_8csamfile_3genexpr(NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyDict_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (PyDict_SetItem(__pyx_d, __pyx_n_s_CIGAR2CODE, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  }
-  __pyx_L4:;
-
-  /* "pysam/csamfile.pyx":108
- * else:
- *     CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
- * CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )             # <<<<<<<<<<<<<<
- * 
- * #####################################################################
- */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_compile); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__84, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_CIGAR_REGEX, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":112
- * #####################################################################
- * # hard-coded constants
- * cdef int max_pos = 2 << 29             # <<<<<<<<<<<<<<
- * 
- * #####################################################################
- */
-  __pyx_v_5pysam_8csamfile_max_pos = 1073741824;
-
-  /* "pysam/csamfile.pyx":197
- * 
- * 
- * class PileupColumn(object):             # <<<<<<<<<<<<<<
- *     '''A pileup column. A pileup column contains
- *     all the reads that map to a certain target base.
- */
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(__pyx_builtin_object);
-  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_builtin_object);
-  __Pyx_GIVEREF(__pyx_builtin_object);
-  __pyx_t_2 = __Pyx_CalculateMetaclass(NULL, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_5 = __Pyx_Py3MetaclassPrepare(__pyx_t_2, __pyx_t_1, __pyx_n_s_PileupColumn, __pyx_n_s_PileupColumn, (PyObject *) NULL, __pyx_n_s_pysam_csamfile, __pyx_kp_s_A_pileup_column_A_pileup_column); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_5);
-
-  /* "pysam/csamfile.pyx":210
- *         list of reads (:class:`pysam.PileupRead`) aligned to this column
- *     '''
- *     def __str__(self):             # <<<<<<<<<<<<<<
- *         return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
- *             "\n" + "\n".join( map(str, self.pileups) )
- */
-  __pyx_t_6 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_8csamfile_12PileupColumn_1__str__, 0, __pyx_n_s_PileupColumn___str, NULL, __pyx_n_s_pysam_csamfile, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__86)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  if (PyObject_SetItem(__pyx_t_5, __pyx_n_s_str, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-
-  /* "pysam/csamfile.pyx":197
- * 
- * 
- * class PileupColumn(object):             # <<<<<<<<<<<<<<
- *     '''A pileup column. A pileup column contains
- *     all the reads that map to a certain target base.
- */
-  __pyx_t_6 = __Pyx_Py3ClassCreate(__pyx_t_2, __pyx_n_s_PileupColumn, __pyx_t_1, __pyx_t_5, NULL, 0, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_6);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_PileupColumn, __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 197; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":216
- * 
- * # valid types for sam headers
- * VALID_HEADER_TYPES = {"HD" : dict,             # <<<<<<<<<<<<<<
- *                       "SQ" : list,
- *                       "RG" : list,
- */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_HD, ((PyObject *)((PyObject*)(&PyDict_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":217
- * # valid types for sam headers
- * VALID_HEADER_TYPES = {"HD" : dict,
- *                       "SQ" : list,             # <<<<<<<<<<<<<<
- *                       "RG" : list,
- *                       "PG" : list,
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_SQ, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":218
- * VALID_HEADER_TYPES = {"HD" : dict,
- *                       "SQ" : list,
- *                       "RG" : list,             # <<<<<<<<<<<<<<
- *                       "PG" : list,
- *                       "CO" : list}
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_RG, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":219
- *                       "SQ" : list,
- *                       "RG" : list,
- *                       "PG" : list,             # <<<<<<<<<<<<<<
- *                       "CO" : list}
- * 
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_PG, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":220
- *                       "RG" : list,
- *                       "PG" : list,
- *                       "CO" : list}             # <<<<<<<<<<<<<<
- * 
- * # order of records within sam headers
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_CO, ((PyObject *)((PyObject*)(&PyList_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADER_TYPES, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":223
- * 
- * # order of records within sam headers
- * VALID_HEADERS = ("HD", "SQ", "RG", "PG", "CO")             # <<<<<<<<<<<<<<
- * 
- * # type conversions within sam header records
- */
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADERS, __pyx_tuple__87) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":226
- * 
- * # type conversions within sam header records
- * VALID_HEADER_FIELDS = {"HD" : {"VN" : str, "SO" : str, "GO" : str},             # <<<<<<<<<<<<<<
- *                        "SQ" : {"SN" : str, "LN" : int, "AS" : str,
- *                                "M5" : str, "SP" : str, "UR" : str,},
- */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_VN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SO, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_GO, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_HD, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":227
- * # type conversions within sam header records
- * VALID_HEADER_FIELDS = {"HD" : {"VN" : str, "SO" : str, "GO" : str},
- *                        "SQ" : {"SN" : str, "LN" : int, "AS" : str,             # <<<<<<<<<<<<<<
- *                                "M5" : str, "SP" : str, "UR" : str,},
- *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,
- */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_LN, ((PyObject *)((PyObject*)(&PyInt_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_AS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":228
- * VALID_HEADER_FIELDS = {"HD" : {"VN" : str, "SO" : str, "GO" : str},
- *                        "SQ" : {"SN" : str, "LN" : int, "AS" : str,
- *                                "M5" : str, "SP" : str, "UR" : str,},             # <<<<<<<<<<<<<<
- *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,
- *                                "DT" : str, "FO" : str, "KS" : str,
- */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_M5, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SP, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_UR, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 227; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_SQ, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":229
- *                        "SQ" : {"SN" : str, "LN" : int, "AS" : str,
- *                                "M5" : str, "SP" : str, "UR" : str,},
- *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,             # <<<<<<<<<<<<<<
- *                                "DT" : str, "FO" : str, "KS" : str,
- *                                "LB" : str, "PG" : str, "PI" : str,
- */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_ID, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_CN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_DS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":230
- *                                "M5" : str, "SP" : str, "UR" : str,},
- *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,
- *                                "DT" : str, "FO" : str, "KS" : str,             # <<<<<<<<<<<<<<
- *                                "LB" : str, "PG" : str, "PI" : str,
- *                                "PL" : str, "PU" : str, "SM" : str,},
- */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_DT, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_FO, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_KS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":231
- *                        "RG" : {"ID" : str, "CN" : str, "DS" : str,
- *                                "DT" : str, "FO" : str, "KS" : str,
- *                                "LB" : str, "PG" : str, "PI" : str,             # <<<<<<<<<<<<<<
- *                                "PL" : str, "PU" : str, "SM" : str,},
- *                        "PG" : {"ID" : str, "PN" : str, "CL" : str,
- */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_LB, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PG, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PI, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":232
- *                                "DT" : str, "FO" : str, "KS" : str,
- *                                "LB" : str, "PG" : str, "PI" : str,
- *                                "PL" : str, "PU" : str, "SM" : str,},             # <<<<<<<<<<<<<<
- *                        "PG" : {"ID" : str, "PN" : str, "CL" : str,
- *                                "PP" : str, "DS" : str, "VN" : str,},}
- */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PL, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PU, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_SM, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_RG, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/csamfile.pyx":233
- *                                "LB" : str, "PG" : str, "PI" : str,
- *                                "PL" : str, "PU" : str, "SM" : str,},
- *                        "PG" : {"ID" : str, "PN" : str, "CL" : str,             # <<<<<<<<<<<<<<
- *                                "PP" : str, "DS" : str, "VN" : str,},}
- * 
- */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_ID, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_CL, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":234
- *                                "PL" : str, "PU" : str, "SM" : str,},
- *                        "PG" : {"ID" : str, "PN" : str, "CL" : str,
- *                                "PP" : str, "DS" : str, "VN" : str,},}             # <<<<<<<<<<<<<<
- * 
- * # output order of fields within records
- */
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_PP, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_DS, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_VN, ((PyObject *)((PyObject*)(&PyString_Type)))) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_PG, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADER_FIELDS, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 226; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":237
- * 
- * # output order of fields within records
- * VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),             # <<<<<<<<<<<<<<
- *                       "SQ" : ("SN", "LN", "AS", "M5",
- *                                "UR", "SP"),
- */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_HD, __pyx_tuple__88) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":238
- * # output order of fields within records
- * VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),
- *                       "SQ" : ("SN", "LN", "AS", "M5",             # <<<<<<<<<<<<<<
- *                                "UR", "SP"),
- *                       "RG" : ("ID", "SM", "LB", "DS",
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_SQ, __pyx_tuple__89) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":240
- *                       "SQ" : ("SN", "LN", "AS", "M5",
- *                                "UR", "SP"),
- *                       "RG" : ("ID", "SM", "LB", "DS",             # <<<<<<<<<<<<<<
- *                               "PU", "PI", "CN", "DT",
- *                               "PL", "FO", "KS", "PG"),
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_RG, __pyx_tuple__90) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/csamfile.pyx":243
- *                               "PU", "PI", "CN", "DT",
- *                               "PL", "FO", "KS", "PG"),
- *                       "PG" : ("PN", "ID", "VN", "CL",             # <<<<<<<<<<<<<<
- *                               "PP"),}
- * 
- */
-  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_PG, __pyx_tuple__91) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VALID_HEADER_ORDER, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1788
- *                   int tid = 0,
- *                   int start = 0,
- *                   int end = max_pos,             # <<<<<<<<<<<<<<
- *                   int truncate = False,
- *                   **kwargs ):
- */
-  __pyx_k__57 = __pyx_v_5pysam_8csamfile_max_pos;
-
-  /* "pysam/csamfile.pyx":3450
- *             hts_close(self.htsfile)
- * 
- * __all__ = ["Samfile",             # <<<<<<<<<<<<<<
- *            "IteratorRow",
- *            "IteratorColumn",
- */
-  __pyx_t_1 = PyList_New(8); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(__pyx_n_s_Samfile);
-  PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_Samfile);
-  __Pyx_GIVEREF(__pyx_n_s_Samfile);
-  __Pyx_INCREF(__pyx_n_s_IteratorRow);
-  PyList_SET_ITEM(__pyx_t_1, 1, __pyx_n_s_IteratorRow);
-  __Pyx_GIVEREF(__pyx_n_s_IteratorRow);
-  __Pyx_INCREF(__pyx_n_s_IteratorColumn);
-  PyList_SET_ITEM(__pyx_t_1, 2, __pyx_n_s_IteratorColumn);
-  __Pyx_GIVEREF(__pyx_n_s_IteratorColumn);
-  __Pyx_INCREF(__pyx_n_s_AlignedRead);
-  PyList_SET_ITEM(__pyx_t_1, 3, __pyx_n_s_AlignedRead);
-  __Pyx_GIVEREF(__pyx_n_s_AlignedRead);
-  __Pyx_INCREF(__pyx_n_s_PileupColumn);
-  PyList_SET_ITEM(__pyx_t_1, 4, __pyx_n_s_PileupColumn);
-  __Pyx_GIVEREF(__pyx_n_s_PileupColumn);
-  __Pyx_INCREF(__pyx_n_s_PileupProxy);
-  PyList_SET_ITEM(__pyx_t_1, 5, __pyx_n_s_PileupProxy);
-  __Pyx_GIVEREF(__pyx_n_s_PileupProxy);
-  __Pyx_INCREF(__pyx_n_s_PileupRead);
-  PyList_SET_ITEM(__pyx_t_1, 6, __pyx_n_s_PileupRead);
-  __Pyx_GIVEREF(__pyx_n_s_PileupRead);
-  __Pyx_INCREF(__pyx_n_s_IndexedReads);
-  PyList_SET_ITEM(__pyx_t_1, 7, __pyx_n_s_IndexedReads);
-  __Pyx_GIVEREF(__pyx_n_s_IndexedReads);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-
-  /* "pysam/csamfile.pyx":1
- * # cython: embedsignature=True             # <<<<<<<<<<<<<<
- * # cython: profile=True
- * # adds doc-strings for sphinx
- */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  goto __pyx_L0;
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_6);
-  if (__pyx_m) {
-    __Pyx_AddTraceback("init pysam.csamfile", __pyx_clineno, __pyx_lineno, __pyx_filename);
-    Py_DECREF(__pyx_m); __pyx_m = 0;
-  } else if (!PyErr_Occurred()) {
-    PyErr_SetString(PyExc_ImportError, "init pysam.csamfile");
-  }
-  __pyx_L0:;
-  __Pyx_RefNannyFinishContext();
-  #if PY_MAJOR_VERSION < 3
-  return;
-  #else
-  return __pyx_m;
-  #endif
-}
-
-/* Runtime support code */
-#if CYTHON_REFNANNY
-static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) {
-    PyObject *m = NULL, *p = NULL;
-    void *r = NULL;
-    m = PyImport_ImportModule((char *)modname);
-    if (!m) goto end;
-    p = PyObject_GetAttrString(m, (char *)"RefNannyAPI");
-    if (!p) goto end;
-    r = PyLong_AsVoidPtr(p);
-end:
-    Py_XDECREF(p);
-    Py_XDECREF(m);
-    return (__Pyx_RefNannyAPIStruct *)r;
-}
-#endif /* CYTHON_REFNANNY */
-
-static PyObject *__Pyx_GetBuiltinName(PyObject *name) {
-    PyObject* result = __Pyx_PyObject_GetAttrStr(__pyx_b, name);
-    if (unlikely(!result)) {
-        PyErr_Format(PyExc_NameError,
-#if PY_MAJOR_VERSION >= 3
-            "name '%U' is not defined", name);
-#else
-            "name '%.200s' is not defined", PyString_AS_STRING(name));
-#endif
-    }
-    return result;
-}
-
-#if CYTHON_PROFILE
-static int __Pyx_TraceSetupAndCall(PyCodeObject** code,
-                                   PyFrameObject** frame,
-                                   const char *funcname,
-                                   const char *srcfile,
-                                   int firstlineno) {
-    int retval;
-    PyThreadState* tstate = PyThreadState_GET();
-    if (*frame == NULL || !CYTHON_PROFILE_REUSE_FRAME) {
-        if (*code == NULL) {
-            *code = __Pyx_createFrameCodeObject(funcname, srcfile, firstlineno);
-            if (*code == NULL) return 0;
-        }
-        *frame = PyFrame_New(
-            tstate,                          /*PyThreadState *tstate*/
-            *code,                           /*PyCodeObject *code*/
-            __pyx_d,                  /*PyObject *globals*/
-            0                                /*PyObject *locals*/
-        );
-        if (*frame == NULL) return 0;
-        if (CYTHON_TRACE && (*frame)->f_trace == NULL) {
-            Py_INCREF(Py_None);
-            (*frame)->f_trace = Py_None;
-        }
-#if PY_VERSION_HEX < 0x030400B1
-    } else {
-        (*frame)->f_tstate = tstate;
-#endif
-    }
-    (*frame)->f_lineno = firstlineno;
-    tstate->use_tracing = 0;
-    #if CYTHON_TRACE
-    if (tstate->c_tracefunc)
-        tstate->c_tracefunc(tstate->c_traceobj, *frame, PyTrace_CALL, NULL);
-    if (!tstate->c_profilefunc)
-        retval = 1;
-    else
-    #endif
-        retval = tstate->c_profilefunc(tstate->c_profileobj, *frame, PyTrace_CALL, NULL) == 0;
-    tstate->use_tracing = (tstate->c_profilefunc ||
-                           (CYTHON_TRACE && tstate->c_tracefunc));
-    return tstate->use_tracing && retval;
-}
-static PyCodeObject *__Pyx_createFrameCodeObject(const char *funcname, const char *srcfile, int firstlineno) {
-    PyObject *py_srcfile = 0;
-    PyObject *py_funcname = 0;
-    PyCodeObject *py_code = 0;
-    #if PY_MAJOR_VERSION < 3
-    py_funcname = PyString_FromString(funcname);
-    py_srcfile = PyString_FromString(srcfile);
-    #else
-    py_funcname = PyUnicode_FromString(funcname);
-    py_srcfile = PyUnicode_FromString(srcfile);
-    #endif
-    if (!py_funcname | !py_srcfile) goto bad;
-    py_code = PyCode_New(
-        0,                /*int argcount,*/
-        #if PY_MAJOR_VERSION >= 3
-        0,                /*int kwonlyargcount,*/
-        #endif
-        0,                /*int nlocals,*/
-        0,                /*int stacksize,*/
-        0,                /*int flags,*/
-        __pyx_empty_bytes,     /*PyObject *code,*/
-        __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,*/
-        firstlineno,      /*int firstlineno,*/
-        __pyx_empty_bytes      /*PyObject *lnotab*/
-    );
-bad:
-    Py_XDECREF(py_srcfile);
-    Py_XDECREF(py_funcname);
-    return py_code;
-}
-#endif /* CYTHON_PROFILE */
-
-#if CYTHON_COMPILING_IN_CPYTHON
-static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) {
-    PyObject *result;
-    ternaryfunc call = func->ob_type->tp_call;
-    if (unlikely(!call))
-        return PyObject_Call(func, arg, kw);
-#if PY_VERSION_HEX >= 0x02060000
-    if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object")))
-        return NULL;
-#endif
-    result = (*call)(func, arg, kw);
-#if PY_VERSION_HEX >= 0x02060000
-    Py_LeaveRecursiveCall();
-#endif
-    if (unlikely(!result) && unlikely(!PyErr_Occurred())) {
-        PyErr_SetString(
-            PyExc_SystemError,
-            "NULL result without error in PyObject_Call");
-    }
-    return result;
-}
-#endif
-
-static CYTHON_INLINE PyObject* __Pyx_decode_c_string(
-         const char* cstring, Py_ssize_t start, Py_ssize_t stop,
-         const char* encoding, const char* errors,
-         PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) {
-    Py_ssize_t length;
-    if (unlikely((start < 0) | (stop < 0))) {
-        length = strlen(cstring);
-        if (start < 0) {
-            start += length;
-            if (start < 0)
-                start = 0;
-        }
-        if (stop < 0)
-            stop += length;
-    }
-    length = stop - start;
-    if (unlikely(length <= 0))
-        return PyUnicode_FromUnicode(NULL, 0);
-    cstring += start;
-    if (decode_func) {
-        return decode_func(cstring, length, errors);
-    } else {
-        return PyUnicode_Decode(cstring, length, encoding, errors);
-    }
-}
-
-static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyObject *tmp_type, *tmp_value, *tmp_tb;
-    PyThreadState *tstate = PyThreadState_GET();
-    tmp_type = tstate->curexc_type;
-    tmp_value = tstate->curexc_value;
-    tmp_tb = tstate->curexc_traceback;
-    tstate->curexc_type = type;
-    tstate->curexc_value = value;
-    tstate->curexc_traceback = tb;
-    Py_XDECREF(tmp_type);
-    Py_XDECREF(tmp_value);
-    Py_XDECREF(tmp_tb);
-#else
-    PyErr_Restore(type, value, tb);
-#endif
-}
-static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyThreadState *tstate = PyThreadState_GET();
-    *type = tstate->curexc_type;
-    *value = tstate->curexc_value;
-    *tb = tstate->curexc_traceback;
-    tstate->curexc_type = 0;
-    tstate->curexc_value = 0;
-    tstate->curexc_traceback = 0;
-#else
-    PyErr_Fetch(type, value, tb);
-#endif
-}
-
-#if PY_MAJOR_VERSION < 3
-static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb,
-                        CYTHON_UNUSED PyObject *cause) {
-    Py_XINCREF(type);
-    if (!value || value == Py_None)
-        value = NULL;
-    else
-        Py_INCREF(value);
-    if (!tb || tb == Py_None)
-        tb = NULL;
-    else {
-        Py_INCREF(tb);
-        if (!PyTraceBack_Check(tb)) {
-            PyErr_SetString(PyExc_TypeError,
-                "raise: arg 3 must be a traceback or None");
-            goto raise_error;
-        }
-    }
-    #if PY_VERSION_HEX < 0x02050000
-    if (PyClass_Check(type)) {
-    #else
-    if (PyType_Check(type)) {
-    #endif
-#if CYTHON_COMPILING_IN_PYPY
-        if (!value) {
-            Py_INCREF(Py_None);
-            value = Py_None;
-        }
-#endif
-        PyErr_NormalizeException(&type, &value, &tb);
-    } else {
-        if (value) {
-            PyErr_SetString(PyExc_TypeError,
-                "instance exception may not have a separate value");
-            goto raise_error;
-        }
-        value = type;
-        #if PY_VERSION_HEX < 0x02050000
-        if (PyInstance_Check(type)) {
-            type = (PyObject*) ((PyInstanceObject*)type)->in_class;
-            Py_INCREF(type);
-        } else {
-            type = 0;
-            PyErr_SetString(PyExc_TypeError,
-                "raise: exception must be an old-style class or instance");
-            goto raise_error;
-        }
-        #else
-        type = (PyObject*) Py_TYPE(type);
-        Py_INCREF(type);
-        if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) {
-            PyErr_SetString(PyExc_TypeError,
-                "raise: exception class must be a subclass of BaseException");
-            goto raise_error;
-        }
-        #endif
-    }
-    __Pyx_ErrRestore(type, value, tb);
-    return;
-raise_error:
-    Py_XDECREF(value);
-    Py_XDECREF(type);
-    Py_XDECREF(tb);
-    return;
-}
-#else /* Python 3+ */
-static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause) {
-    PyObject* owned_instance = NULL;
-    if (tb == Py_None) {
-        tb = 0;
-    } else if (tb && !PyTraceBack_Check(tb)) {
-        PyErr_SetString(PyExc_TypeError,
-            "raise: arg 3 must be a traceback or None");
-        goto bad;
-    }
-    if (value == Py_None)
-        value = 0;
-    if (PyExceptionInstance_Check(type)) {
-        if (value) {
-            PyErr_SetString(PyExc_TypeError,
-                "instance exception may not have a separate value");
-            goto bad;
-        }
-        value = type;
-        type = (PyObject*) Py_TYPE(value);
-    } else if (PyExceptionClass_Check(type)) {
-        PyObject *instance_class = NULL;
-        if (value && PyExceptionInstance_Check(value)) {
-            instance_class = (PyObject*) Py_TYPE(value);
-            if (instance_class != type) {
-                if (PyObject_IsSubclass(instance_class, type)) {
-                    type = instance_class;
-                } else {
-                    instance_class = NULL;
-                }
-            }
-        }
-        if (!instance_class) {
-            PyObject *args;
-            if (!value)
-                args = PyTuple_New(0);
-            else if (PyTuple_Check(value)) {
-                Py_INCREF(value);
-                args = value;
-            } else
-                args = PyTuple_Pack(1, value);
-            if (!args)
-                goto bad;
-            owned_instance = PyObject_Call(type, args, NULL);
-            Py_DECREF(args);
-            if (!owned_instance)
-                goto bad;
-            value = owned_instance;
-            if (!PyExceptionInstance_Check(value)) {
-                PyErr_Format(PyExc_TypeError,
-                             "calling %R should have returned an instance of "
-                             "BaseException, not %R",
-                             type, Py_TYPE(value));
-                goto bad;
-            }
-        }
-    } else {
-        PyErr_SetString(PyExc_TypeError,
-            "raise: exception class must be a subclass of BaseException");
-        goto bad;
-    }
-#if PY_VERSION_HEX >= 0x03030000
-    if (cause) {
-#else
-    if (cause && cause != Py_None) {
-#endif
-        PyObject *fixed_cause;
-        if (cause == Py_None) {
-            fixed_cause = NULL;
-        } else if (PyExceptionClass_Check(cause)) {
-            fixed_cause = PyObject_CallObject(cause, NULL);
-            if (fixed_cause == NULL)
-                goto bad;
-        } else if (PyExceptionInstance_Check(cause)) {
-            fixed_cause = cause;
-            Py_INCREF(fixed_cause);
-        } else {
-            PyErr_SetString(PyExc_TypeError,
-                            "exception causes must derive from "
-                            "BaseException");
-            goto bad;
-        }
-        PyException_SetCause(value, fixed_cause);
-    }
-    PyErr_SetObject(type, value);
-    if (tb) {
-        PyThreadState *tstate = PyThreadState_GET();
-        PyObject* tmp_tb = tstate->curexc_traceback;
-        if (tb != tmp_tb) {
-            Py_INCREF(tb);
-            tstate->curexc_traceback = tb;
-            Py_XDECREF(tmp_tb);
-        }
-    }
-bad:
-    Py_XDECREF(owned_instance);
-    return;
-}
-#endif
-
-static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
-    if (unlikely(!type)) {
-        PyErr_SetString(PyExc_SystemError, "Missing type object");
-        return 0;
-    }
-    if (likely(PyObject_TypeCheck(obj, type)))
-        return 1;
-    PyErr_Format(PyExc_TypeError, "Cannot convert %.200s to %.200s",
-                 Py_TYPE(obj)->tp_name, type->tp_name);
-    return 0;
-}
-
-#if !CYTHON_COMPILING_IN_CPYTHON
-static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* values) {
-    return PyObject_CallMethodObjArgs(sep, __pyx_n_s_join, values, NULL)
-}
-#endif
-
-static CYTHON_INLINE int __Pyx_CheckKeywordStrings(
-    PyObject *kwdict,
-    const char* function_name,
-    int kw_allowed)
-{
-    PyObject* key = 0;
-    Py_ssize_t pos = 0;
-#if CPYTHON_COMPILING_IN_PYPY
-    if (!kw_allowed && PyDict_Next(kwdict, &pos, &key, 0))
-        goto invalid_keyword;
-    return 1;
-#else
-    while (PyDict_Next(kwdict, &pos, &key, 0)) {
-        #if PY_MAJOR_VERSION < 3
-        if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key)))
-        #endif
-            if (unlikely(!PyUnicode_Check(key)))
-                goto invalid_keyword_type;
-    }
-    if ((!kw_allowed) && unlikely(key))
-        goto invalid_keyword;
-    return 1;
-invalid_keyword_type:
-    PyErr_Format(PyExc_TypeError,
-        "%.200s() keywords must be strings", function_name);
-    return 0;
-#endif
-invalid_keyword:
-    PyErr_Format(PyExc_TypeError,
-    #if PY_MAJOR_VERSION < 3
-        "%.200s() got an unexpected keyword argument '%.200s'",
-        function_name, PyString_AsString(key));
-    #else
-        "%s() got an unexpected keyword argument '%U'",
-        function_name, key);
-    #endif
-    return 0;
-}
-
-static void __Pyx_RaiseDoubleKeywordsError(
-    const char* func_name,
-    PyObject* kw_name)
-{
-    PyErr_Format(PyExc_TypeError,
-        #if PY_MAJOR_VERSION >= 3
-        "%s() got multiple values for keyword argument '%U'", func_name, kw_name);
-        #else
-        "%s() got multiple values for keyword argument '%s'", func_name,
-        PyString_AsString(kw_name));
-        #endif
-}
-
-static int __Pyx_ParseOptionalKeywords(
-    PyObject *kwds,
-    PyObject **argnames[],
-    PyObject *kwds2,
-    PyObject *values[],
-    Py_ssize_t num_pos_args,
-    const char* function_name)
-{
-    PyObject *key = 0, *value = 0;
-    Py_ssize_t pos = 0;
-    PyObject*** name;
-    PyObject*** first_kw_arg = argnames + num_pos_args;
-    while (PyDict_Next(kwds, &pos, &key, &value)) {
-        name = first_kw_arg;
-        while (*name && (**name != key)) name++;
-        if (*name) {
-            values[name-argnames] = value;
-            continue;
-        }
-        name = first_kw_arg;
-        #if PY_MAJOR_VERSION < 3
-        if (likely(PyString_CheckExact(key)) || likely(PyString_Check(key))) {
-            while (*name) {
-                if ((CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**name) == PyString_GET_SIZE(key))
-                        && _PyString_Eq(**name, key)) {
-                    values[name-argnames] = value;
-                    break;
-                }
-                name++;
-            }
-            if (*name) continue;
-            else {
-                PyObject*** argname = argnames;
-                while (argname != first_kw_arg) {
-                    if ((**argname == key) || (
-                            (CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**argname) == PyString_GET_SIZE(key))
-                             && _PyString_Eq(**argname, key))) {
-                        goto arg_passed_twice;
-                    }
-                    argname++;
-                }
-            }
-        } else
-        #endif
-        if (likely(PyUnicode_Check(key))) {
-            while (*name) {
-                int cmp = (**name == key) ? 0 :
-                #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3
-                    (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
-                #endif
-                    PyUnicode_Compare(**name, key);
-                if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;
-                if (cmp == 0) {
-                    values[name-argnames] = value;
-                    break;
-                }
-                name++;
-            }
-            if (*name) continue;
-            else {
-                PyObject*** argname = argnames;
-                while (argname != first_kw_arg) {
-                    int cmp = (**argname == key) ? 0 :
-                    #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3
-                        (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
-                    #endif
-                        PyUnicode_Compare(**argname, key);
-                    if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;
-                    if (cmp == 0) goto arg_passed_twice;
-                    argname++;
-                }
-            }
-        } else
-            goto invalid_keyword_type;
-        if (kwds2) {
-            if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
-        } else {
-            goto invalid_keyword;
-        }
-    }
-    return 0;
-arg_passed_twice:
-    __Pyx_RaiseDoubleKeywordsError(function_name, key);
-    goto bad;
-invalid_keyword_type:
-    PyErr_Format(PyExc_TypeError,
-        "%.200s() keywords must be strings", function_name);
-    goto bad;
-invalid_keyword:
-    PyErr_Format(PyExc_TypeError,
-    #if PY_MAJOR_VERSION < 3
-        "%.200s() got an unexpected keyword argument '%.200s'",
-        function_name, PyString_AsString(key));
-    #else
-        "%s() got an unexpected keyword argument '%U'",
-        function_name, key);
-    #endif
-bad:
-    return -1;
-}
-
-static void __Pyx_RaiseArgtupleInvalid(
-    const char* func_name,
-    int exact,
-    Py_ssize_t num_min,
-    Py_ssize_t num_max,
-    Py_ssize_t num_found)
-{
-    Py_ssize_t num_expected;
-    const char *more_or_less;
-    if (num_found < num_min) {
-        num_expected = num_min;
-        more_or_less = "at least";
-    } else {
-        num_expected = num_max;
-        more_or_less = "at most";
-    }
-    if (exact) {
-        more_or_less = "exactly";
-    }
-    PyErr_Format(PyExc_TypeError,
-                 "%.200s() takes %.8s %" CYTHON_FORMAT_SSIZE_T "d positional argument%.1s (%" CYTHON_FORMAT_SSIZE_T "d given)",
-                 func_name, more_or_less, num_expected,
-                 (num_expected == 1) ? "" : "s", num_found);
-}
-
-static void __Pyx_RaiseArgumentTypeInvalid(const char* name, PyObject *obj, PyTypeObject *type) {
-    PyErr_Format(PyExc_TypeError,
-        "Argument '%.200s' has incorrect type (expected %.200s, got %.200s)",
-        name, type->tp_name, Py_TYPE(obj)->tp_name);
-}
-static CYTHON_INLINE int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
-    const char *name, int exact)
-{
-    if (unlikely(!type)) {
-        PyErr_SetString(PyExc_SystemError, "Missing type object");
-        return 0;
-    }
-    if (none_allowed && obj == Py_None) return 1;
-    else if (exact) {
-        if (likely(Py_TYPE(obj) == type)) return 1;
-        #if PY_MAJOR_VERSION == 2
-        else if ((type == &PyBaseString_Type) && likely(__Pyx_PyBaseString_CheckExact(obj))) return 1;
-        #endif
-    }
-    else {
-        if (likely(PyObject_TypeCheck(obj, type))) return 1;
-    }
-    __Pyx_RaiseArgumentTypeInvalid(name, obj, type);
-    return 0;
-}
-
-static CYTHON_INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyThreadState *tstate = PyThreadState_GET();
-    *type = tstate->exc_type;
-    *value = tstate->exc_value;
-    *tb = tstate->exc_traceback;
-    Py_XINCREF(*type);
-    Py_XINCREF(*value);
-    Py_XINCREF(*tb);
-#else
-    PyErr_GetExcInfo(type, value, tb);
-#endif
-}
-static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyObject *tmp_type, *tmp_value, *tmp_tb;
-    PyThreadState *tstate = PyThreadState_GET();
-    tmp_type = tstate->exc_type;
-    tmp_value = tstate->exc_value;
-    tmp_tb = tstate->exc_traceback;
-    tstate->exc_type = type;
-    tstate->exc_value = value;
-    tstate->exc_traceback = tb;
-    Py_XDECREF(tmp_type);
-    Py_XDECREF(tmp_value);
-    Py_XDECREF(tmp_tb);
-#else
-    PyErr_SetExcInfo(type, value, tb);
-#endif
-}
-
-static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb) {
-    PyObject *local_type, *local_value, *local_tb;
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyObject *tmp_type, *tmp_value, *tmp_tb;
-    PyThreadState *tstate = PyThreadState_GET();
-    local_type = tstate->curexc_type;
-    local_value = tstate->curexc_value;
-    local_tb = tstate->curexc_traceback;
-    tstate->curexc_type = 0;
-    tstate->curexc_value = 0;
-    tstate->curexc_traceback = 0;
-#else
-    PyErr_Fetch(&local_type, &local_value, &local_tb);
-#endif
-    PyErr_NormalizeException(&local_type, &local_value, &local_tb);
-#if CYTHON_COMPILING_IN_CPYTHON
-    if (unlikely(tstate->curexc_type))
-#else
-    if (unlikely(PyErr_Occurred()))
-#endif
-        goto bad;
-    #if PY_MAJOR_VERSION >= 3
-    if (local_tb) {
-        if (unlikely(PyException_SetTraceback(local_value, local_tb) < 0))
-            goto bad;
-    }
-    #endif
-    Py_XINCREF(local_tb);
-    Py_XINCREF(local_type);
-    Py_XINCREF(local_value);
-    *type = local_type;
-    *value = local_value;
-    *tb = local_tb;
-#if CYTHON_COMPILING_IN_CPYTHON
-    tmp_type = tstate->exc_type;
-    tmp_value = tstate->exc_value;
-    tmp_tb = tstate->exc_traceback;
-    tstate->exc_type = local_type;
-    tstate->exc_value = local_value;
-    tstate->exc_traceback = local_tb;
-    Py_XDECREF(tmp_type);
-    Py_XDECREF(tmp_value);
-    Py_XDECREF(tmp_tb);
-#else
-    PyErr_SetExcInfo(local_type, local_value, local_tb);
-#endif
-    return 0;
-bad:
-    *type = 0;
-    *value = 0;
-    *tb = 0;
-    Py_XDECREF(local_type);
-    Py_XDECREF(local_value);
-    Py_XDECREF(local_tb);
-    return -1;
-}
-
-static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals) {
-#if CYTHON_COMPILING_IN_PYPY
-    return PyObject_RichCompareBool(s1, s2, equals);
-#else
-    if (s1 == s2) {
-        return (equals == Py_EQ);
-    } else if (PyBytes_CheckExact(s1) & PyBytes_CheckExact(s2)) {
-        const char *ps1, *ps2;
-        Py_ssize_t length = PyBytes_GET_SIZE(s1);
-        if (length != PyBytes_GET_SIZE(s2))
-            return (equals == Py_NE);
-        ps1 = PyBytes_AS_STRING(s1);
-        ps2 = PyBytes_AS_STRING(s2);
-        if (ps1[0] != ps2[0]) {
-            return (equals == Py_NE);
-        } else if (length == 1) {
-            return (equals == Py_EQ);
-        } else {
-            int result = memcmp(ps1, ps2, (size_t)length);
-            return (equals == Py_EQ) ? (result == 0) : (result != 0);
-        }
-    } else if ((s1 == Py_None) & PyBytes_CheckExact(s2)) {
-        return (equals == Py_NE);
-    } else if ((s2 == Py_None) & PyBytes_CheckExact(s1)) {
-        return (equals == Py_NE);
-    } else {
-        int result;
-        PyObject* py_result = PyObject_RichCompare(s1, s2, equals);
-        if (!py_result)
-            return -1;
-        result = __Pyx_PyObject_IsTrue(py_result);
-        Py_DECREF(py_result);
-        return result;
-    }
-#endif
-}
-
-static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals) {
-#if CYTHON_COMPILING_IN_PYPY
-    return PyObject_RichCompareBool(s1, s2, equals);
-#else
-#if PY_MAJOR_VERSION < 3
-    PyObject* owned_ref = NULL;
-#endif
-    int s1_is_unicode, s2_is_unicode;
-    if (s1 == s2) {
-        goto return_eq;
-    }
-    s1_is_unicode = PyUnicode_CheckExact(s1);
-    s2_is_unicode = PyUnicode_CheckExact(s2);
-#if PY_MAJOR_VERSION < 3
-    if ((s1_is_unicode & (!s2_is_unicode)) && PyString_CheckExact(s2)) {
-        owned_ref = PyUnicode_FromObject(s2);
-        if (unlikely(!owned_ref))
-            return -1;
-        s2 = owned_ref;
-        s2_is_unicode = 1;
-    } else if ((s2_is_unicode & (!s1_is_unicode)) && PyString_CheckExact(s1)) {
-        owned_ref = PyUnicode_FromObject(s1);
-        if (unlikely(!owned_ref))
-            return -1;
-        s1 = owned_ref;
-        s1_is_unicode = 1;
-    } else if (((!s2_is_unicode) & (!s1_is_unicode))) {
-        return __Pyx_PyBytes_Equals(s1, s2, equals);
-    }
-#endif
-    if (s1_is_unicode & s2_is_unicode) {
-        Py_ssize_t length;
-        int kind;
-        void *data1, *data2;
-        #if CYTHON_PEP393_ENABLED
-        if (unlikely(PyUnicode_READY(s1) < 0) || unlikely(PyUnicode_READY(s2) < 0))
-            return -1;
-        #endif
-        length = __Pyx_PyUnicode_GET_LENGTH(s1);
-        if (length != __Pyx_PyUnicode_GET_LENGTH(s2)) {
-            goto return_ne;
-        }
-        kind = __Pyx_PyUnicode_KIND(s1);
-        if (kind != __Pyx_PyUnicode_KIND(s2)) {
-            goto return_ne;
-        }
-        data1 = __Pyx_PyUnicode_DATA(s1);
-        data2 = __Pyx_PyUnicode_DATA(s2);
-        if (__Pyx_PyUnicode_READ(kind, data1, 0) != __Pyx_PyUnicode_READ(kind, data2, 0)) {
-            goto return_ne;
-        } else if (length == 1) {
-            goto return_eq;
-        } else {
-            int result = memcmp(data1, data2, length * kind);
-            #if PY_MAJOR_VERSION < 3
-            Py_XDECREF(owned_ref);
-            #endif
-            return (equals == Py_EQ) ? (result == 0) : (result != 0);
-        }
-    } else if ((s1 == Py_None) & s2_is_unicode) {
-        goto return_ne;
-    } else if ((s2 == Py_None) & s1_is_unicode) {
-        goto return_ne;
-    } else {
-        int result;
-        PyObject* py_result = PyObject_RichCompare(s1, s2, equals);
-        if (!py_result)
-            return -1;
-        result = __Pyx_PyObject_IsTrue(py_result);
-        Py_DECREF(py_result);
-        return result;
-    }
-return_eq:
-    #if PY_MAJOR_VERSION < 3
-    Py_XDECREF(owned_ref);
-    #endif
-    return (equals == Py_EQ);
-return_ne:
-    #if PY_MAJOR_VERSION < 3
-    Py_XDECREF(owned_ref);
-    #endif
-    return (equals == Py_NE);
-#endif
-}
-
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
-    PyObject *r;
-    if (!j) return NULL;
-    r = PyObject_GetItem(o, j);
-    Py_DECREF(j);
-    return r;
-}
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i,
-                                                              int wraparound, int boundscheck) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    if (wraparound & unlikely(i < 0)) i += PyList_GET_SIZE(o);
-    if ((!boundscheck) || likely((0 <= i) & (i < PyList_GET_SIZE(o)))) {
-        PyObject *r = PyList_GET_ITEM(o, i);
-        Py_INCREF(r);
-        return r;
-    }
-    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
-#else
-    return PySequence_GetItem(o, i);
-#endif
-}
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i,
-                                                              int wraparound, int boundscheck) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    if (wraparound & unlikely(i < 0)) i += PyTuple_GET_SIZE(o);
-    if ((!boundscheck) || likely((0 <= i) & (i < PyTuple_GET_SIZE(o)))) {
-        PyObject *r = PyTuple_GET_ITEM(o, i);
-        Py_INCREF(r);
-        return r;
-    }
-    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
-#else
-    return PySequence_GetItem(o, i);
-#endif
-}
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
-                                                     int is_list, int wraparound, int boundscheck) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    if (is_list || PyList_CheckExact(o)) {
-        Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyList_GET_SIZE(o);
-        if ((!boundscheck) || (likely((n >= 0) & (n < PyList_GET_SIZE(o))))) {
-            PyObject *r = PyList_GET_ITEM(o, n);
-            Py_INCREF(r);
-            return r;
-        }
-    }
-    else if (PyTuple_CheckExact(o)) {
-        Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyTuple_GET_SIZE(o);
-        if ((!boundscheck) || likely((n >= 0) & (n < PyTuple_GET_SIZE(o)))) {
-            PyObject *r = PyTuple_GET_ITEM(o, n);
-            Py_INCREF(r);
-            return r;
-        }
-    } else {
-        PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
-        if (likely(m && m->sq_item)) {
-            if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
-                Py_ssize_t l = m->sq_length(o);
-                if (likely(l >= 0)) {
-                    i += l;
-                } else {
-                    if (PyErr_ExceptionMatches(PyExc_OverflowError))
-                        PyErr_Clear();
-                    else
-                        return NULL;
-                }
-            }
-            return m->sq_item(o, i);
-        }
-    }
-#else
-    if (is_list || PySequence_Check(o)) {
-        return PySequence_GetItem(o, i);
-    }
-#endif
-    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
-}
-
-static CYTHON_INLINE int __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
-    if (likely(PyList_CheckExact(L))) {
-        if (unlikely(__Pyx_PyList_Append(L, x) < 0)) return -1;
-    } else {
-        PyObject* retval = __Pyx_PyObject_CallMethod1(L, __pyx_n_s_append, x);
-        if (unlikely(!retval))
-            return -1;
-        Py_DECREF(retval);
-    }
-    return 0;
-}
-
-static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {
-    PyObject *result;
-#if CYTHON_COMPILING_IN_CPYTHON
-    result = PyDict_GetItem(__pyx_d, name);
-    if (result) {
-        Py_INCREF(result);
-    } else {
-#else
-    result = PyObject_GetItem(__pyx_d, name);
-    if (!result) {
-        PyErr_Clear();
-#endif
-        result = __Pyx_GetBuiltinName(name);
-    }
-    return result;
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(
-        PyObject* obj, Py_ssize_t cstart, Py_ssize_t cstop,
-        PyObject** _py_start, PyObject** _py_stop, PyObject** _py_slice,
-        int has_cstart, int has_cstop, CYTHON_UNUSED int wraparound) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyMappingMethods* mp;
-#if PY_MAJOR_VERSION < 3
-    PySequenceMethods* ms = Py_TYPE(obj)->tp_as_sequence;
-    if (likely(ms && ms->sq_slice)) {
-        if (!has_cstart) {
-            if (_py_start && (*_py_start != Py_None)) {
-                cstart = __Pyx_PyIndex_AsSsize_t(*_py_start);
-                if ((cstart == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;
-            } else
-                cstart = 0;
-        }
-        if (!has_cstop) {
-            if (_py_stop && (*_py_stop != Py_None)) {
-                cstop = __Pyx_PyIndex_AsSsize_t(*_py_stop);
-                if ((cstop == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;
-            } else
-                cstop = PY_SSIZE_T_MAX;
-        }
-        if (wraparound && unlikely((cstart < 0) | (cstop < 0)) && likely(ms->sq_length)) {
-            Py_ssize_t l = ms->sq_length(obj);
-            if (likely(l >= 0)) {
-                if (cstop < 0) {
-                    cstop += l;
-                    if (cstop < 0) cstop = 0;
-                }
-                if (cstart < 0) {
-                    cstart += l;
-                    if (cstart < 0) cstart = 0;
-                }
-            } else {
-                if (PyErr_ExceptionMatches(PyExc_OverflowError))
-                    PyErr_Clear();
-                else
-                    goto bad;
-            }
-        }
-        return ms->sq_slice(obj, cstart, cstop);
-    }
-#endif
-    mp = Py_TYPE(obj)->tp_as_mapping;
-    if (likely(mp && mp->mp_subscript))
-#endif
-    {
-        PyObject* result;
-        PyObject *py_slice, *py_start, *py_stop;
-        if (_py_slice) {
-            py_slice = *_py_slice;
-        } else {
-            PyObject* owned_start = NULL;
-            PyObject* owned_stop = NULL;
-            if (_py_start) {
-                py_start = *_py_start;
-            } else {
-                if (has_cstart) {
-                    owned_start = py_start = PyInt_FromSsize_t(cstart);
-                    if (unlikely(!py_start)) goto bad;
-                } else
-                    py_start = Py_None;
-            }
-            if (_py_stop) {
-                py_stop = *_py_stop;
-            } else {
-                if (has_cstop) {
-                    owned_stop = py_stop = PyInt_FromSsize_t(cstop);
-                    if (unlikely(!py_stop)) {
-                        Py_XDECREF(owned_start);
-                        goto bad;
-                    }
-                } else
-                    py_stop = Py_None;
-            }
-            py_slice = PySlice_New(py_start, py_stop, Py_None);
-            Py_XDECREF(owned_start);
-            Py_XDECREF(owned_stop);
-            if (unlikely(!py_slice)) goto bad;
-        }
-#if CYTHON_COMPILING_IN_CPYTHON
-        result = mp->mp_subscript(obj, py_slice);
-#else
-        result = PyObject_GetItem(obj, py_slice);
-#endif
-        if (!_py_slice) {
-            Py_DECREF(py_slice);
-        }
-        return result;
-    }
-    PyErr_Format(PyExc_TypeError,
-        "'%.200s' object is unsliceable", Py_TYPE(obj)->tp_name);
-bad:
-    return NULL;
-}
-
-static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno,
-                                  CYTHON_UNUSED int lineno, CYTHON_UNUSED const char *filename,
-                                  int full_traceback) {
-    PyObject *old_exc, *old_val, *old_tb;
-    PyObject *ctx;
-    __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
-    if (full_traceback) {
-        Py_XINCREF(old_exc);
-        Py_XINCREF(old_val);
-        Py_XINCREF(old_tb);
-        __Pyx_ErrRestore(old_exc, old_val, old_tb);
-        PyErr_PrintEx(1);
-    }
-    #if PY_MAJOR_VERSION < 3
-    ctx = PyString_FromString(name);
-    #else
-    ctx = PyUnicode_FromString(name);
-    #endif
-    __Pyx_ErrRestore(old_exc, old_val, old_tb);
-    if (!ctx) {
-        PyErr_WriteUnraisable(Py_None);
-    } else {
-        PyErr_WriteUnraisable(ctx);
-        Py_DECREF(ctx);
-    }
-}
-
-static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
-    PyErr_Format(PyExc_ValueError,
-                 "too many values to unpack (expected %" CYTHON_FORMAT_SSIZE_T "d)", expected);
-}
-
-static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
-    PyErr_Format(PyExc_ValueError,
-                 "need more than %" CYTHON_FORMAT_SSIZE_T "d value%.1s to unpack",
-                 index, (index == 1) ? "" : "s");
-}
-
-static CYTHON_INLINE int __Pyx_IterFinish(void) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyThreadState *tstate = PyThreadState_GET();
-    PyObject* exc_type = tstate->curexc_type;
-    if (unlikely(exc_type)) {
-        if (likely(exc_type == PyExc_StopIteration) || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration)) {
-            PyObject *exc_value, *exc_tb;
-            exc_value = tstate->curexc_value;
-            exc_tb = tstate->curexc_traceback;
-            tstate->curexc_type = 0;
-            tstate->curexc_value = 0;
-            tstate->curexc_traceback = 0;
-            Py_DECREF(exc_type);
-            Py_XDECREF(exc_value);
-            Py_XDECREF(exc_tb);
-            return 0;
-        } else {
-            return -1;
-        }
-    }
-    return 0;
-#else
-    if (unlikely(PyErr_Occurred())) {
-        if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) {
-            PyErr_Clear();
-            return 0;
-        } else {
-            return -1;
-        }
-    }
-    return 0;
-#endif
-}
-
-static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected) {
-    if (unlikely(retval)) {
-        Py_DECREF(retval);
-        __Pyx_RaiseTooManyValuesError(expected);
-        return -1;
-    } else {
-        return __Pyx_IterFinish();
-    }
-    return 0;
-}
-
-static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObject* default_value) {
-    PyObject* value;
-#if PY_MAJOR_VERSION >= 3
-    value = PyDict_GetItemWithError(d, key);
-    if (unlikely(!value)) {
-        if (unlikely(PyErr_Occurred()))
-            return NULL;
-        value = default_value;
-    }
-    Py_INCREF(value);
-#else
-    if (PyString_CheckExact(key) || PyUnicode_CheckExact(key) || PyInt_CheckExact(key)) {
-        value = PyDict_GetItem(d, key);
-        if (unlikely(!value)) {
-            value = default_value;
-        }
-        Py_INCREF(value);
-    } else {
-        if (default_value == Py_None)
-            default_value = NULL;
-        value = PyObject_CallMethodObjArgs(
-            d, __pyx_n_s_get, key, default_value, NULL);
-    }
-#endif
-    return value;
-}
-
-static CYTHON_INLINE long __Pyx_div_long(long a, long b) {
-    long q = a / b;
-    long r = a - q*b;
-    q -= ((r != 0) & ((r ^ b) < 0));
-    return q;
-}
-
-static CYTHON_INLINE long __Pyx_mod_long(long a, long b) {
-    long r = a % b;
-    r += ((r != 0) & ((r ^ b) < 0)) * b;
-    return r;
-}
-
-static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname) {
-    PyErr_Format(PyExc_UnboundLocalError, "local variable '%s' referenced before assignment", varname);
-}
-
-static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
-#if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
-    PyObject *ob = PyCapsule_New(vtable, 0, 0);
-#else
-    PyObject *ob = PyCObject_FromVoidPtr(vtable, 0);
-#endif
-    if (!ob)
-        goto bad;
-    if (PyDict_SetItem(dict, __pyx_n_s_pyx_vtable, ob) < 0)
-        goto bad;
-    Py_DECREF(ob);
-    return 0;
-bad:
-    Py_XDECREF(ob);
-    return -1;
-}
-
-static void* __Pyx_GetVtable(PyObject *dict) {
-    void* ptr;
-    PyObject *ob = PyObject_GetItem(dict, __pyx_n_s_pyx_vtable);
-    if (!ob)
-        goto bad;
-#if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
-    ptr = PyCapsule_GetPointer(ob, 0);
-#else
-    ptr = PyCObject_AsVoidPtr(ob);
-#endif
-    if (!ptr && !PyErr_Occurred())
-        PyErr_SetString(PyExc_RuntimeError, "invalid vtable found for imported type");
-    Py_DECREF(ob);
-    return ptr;
-bad:
-    Py_XDECREF(ob);
-    return NULL;
-}
-
-static PyObject *__Pyx_CalculateMetaclass(PyTypeObject *metaclass, PyObject *bases) {
-    Py_ssize_t i, nbases = PyTuple_GET_SIZE(bases);
-    for (i=0; i < nbases; i++) {
-        PyTypeObject *tmptype;
-        PyObject *tmp = PyTuple_GET_ITEM(bases, i);
-        tmptype = Py_TYPE(tmp);
-#if PY_MAJOR_VERSION < 3
-        if (tmptype == &PyClass_Type)
-            continue;
-#endif
-        if (!metaclass) {
-            metaclass = tmptype;
-            continue;
-        }
-        if (PyType_IsSubtype(metaclass, tmptype))
-            continue;
-        if (PyType_IsSubtype(tmptype, metaclass)) {
-            metaclass = tmptype;
-            continue;
-        }
-        PyErr_SetString(PyExc_TypeError,
-                        "metaclass conflict: "
-                        "the metaclass of a derived class "
-                        "must be a (non-strict) subclass "
-                        "of the metaclasses of all its bases");
-        return NULL;
-    }
-    if (!metaclass) {
-#if PY_MAJOR_VERSION < 3
-        metaclass = &PyClass_Type;
-#else
-        metaclass = &PyType_Type;
-#endif
-    }
-    Py_INCREF((PyObject*) metaclass);
-    return (PyObject*) metaclass;
-}
-
-static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type) {
-    PyObject* fake_module;
-    PyTypeObject* cached_type = NULL;
-    fake_module = PyImport_AddModule((char*) "_cython_" CYTHON_ABI);
-    if (!fake_module) return NULL;
-    Py_INCREF(fake_module);
-    cached_type = (PyTypeObject*) PyObject_GetAttrString(fake_module, type->tp_name);
-    if (cached_type) {
-        if (!PyType_Check((PyObject*)cached_type)) {
-            PyErr_Format(PyExc_TypeError,
-                "Shared Cython type %.200s is not a type object",
-                type->tp_name);
-            goto bad;
-        }
-        if (cached_type->tp_basicsize != type->tp_basicsize) {
-            PyErr_Format(PyExc_TypeError,
-                "Shared Cython type %.200s has the wrong size, try recompiling",
-                type->tp_name);
-            goto bad;
-        }
-    } else {
-        if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad;
-        PyErr_Clear();
-        if (PyType_Ready(type) < 0) goto bad;
-        if (PyObject_SetAttrString(fake_module, type->tp_name, (PyObject*) type) < 0)
-            goto bad;
-        Py_INCREF(type);
-        cached_type = type;
-    }
-done:
-    Py_DECREF(fake_module);
-    return cached_type;
-bad:
-    Py_XDECREF(cached_type);
-    cached_type = NULL;
-    goto done;
-}
-
-static PyObject *
-__Pyx_CyFunction_get_doc(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *closure)
-{
-    if (unlikely(op->func_doc == NULL)) {
-        if (op->func.m_ml->ml_doc) {
-#if PY_MAJOR_VERSION >= 3
-            op->func_doc = PyUnicode_FromString(op->func.m_ml->ml_doc);
-#else
-            op->func_doc = PyString_FromString(op->func.m_ml->ml_doc);
-#endif
-            if (unlikely(op->func_doc == NULL))
-                return NULL;
-        } else {
-            Py_INCREF(Py_None);
-            return Py_None;
-        }
-    }
-    Py_INCREF(op->func_doc);
-    return op->func_doc;
-}
-static int
-__Pyx_CyFunction_set_doc(__pyx_CyFunctionObject *op, PyObject *value)
-{
-    PyObject *tmp = op->func_doc;
-    if (value == NULL)
-        value = Py_None; /* Mark as deleted */
-    Py_INCREF(value);
-    op->func_doc = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_name(__pyx_CyFunctionObject *op)
-{
-    if (unlikely(op->func_name == NULL)) {
-#if PY_MAJOR_VERSION >= 3
-        op->func_name = PyUnicode_InternFromString(op->func.m_ml->ml_name);
-#else
-        op->func_name = PyString_InternFromString(op->func.m_ml->ml_name);
-#endif
-        if (unlikely(op->func_name == NULL))
-            return NULL;
-    }
-    Py_INCREF(op->func_name);
-    return op->func_name;
-}
-static int
-__Pyx_CyFunction_set_name(__pyx_CyFunctionObject *op, PyObject *value)
-{
-    PyObject *tmp;
-#if PY_MAJOR_VERSION >= 3
-    if (unlikely(value == NULL || !PyUnicode_Check(value))) {
-#else
-    if (unlikely(value == NULL || !PyString_Check(value))) {
-#endif
-        PyErr_SetString(PyExc_TypeError,
-                        "__name__ must be set to a string object");
-        return -1;
-    }
-    tmp = op->func_name;
-    Py_INCREF(value);
-    op->func_name = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_qualname(__pyx_CyFunctionObject *op)
-{
-    Py_INCREF(op->func_qualname);
-    return op->func_qualname;
-}
-static int
-__Pyx_CyFunction_set_qualname(__pyx_CyFunctionObject *op, PyObject *value)
-{
-    PyObject *tmp;
-#if PY_MAJOR_VERSION >= 3
-    if (unlikely(value == NULL || !PyUnicode_Check(value))) {
-#else
-    if (unlikely(value == NULL || !PyString_Check(value))) {
-#endif
-        PyErr_SetString(PyExc_TypeError,
-                        "__qualname__ must be set to a string object");
-        return -1;
-    }
-    tmp = op->func_qualname;
-    Py_INCREF(value);
-    op->func_qualname = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_self(__pyx_CyFunctionObject *m, CYTHON_UNUSED void *closure)
-{
-    PyObject *self;
-    self = m->func_closure;
-    if (self == NULL)
-        self = Py_None;
-    Py_INCREF(self);
-    return self;
-}
-static PyObject *
-__Pyx_CyFunction_get_dict(__pyx_CyFunctionObject *op)
-{
-    if (unlikely(op->func_dict == NULL)) {
-        op->func_dict = PyDict_New();
-        if (unlikely(op->func_dict == NULL))
-            return NULL;
-    }
-    Py_INCREF(op->func_dict);
-    return op->func_dict;
-}
-static int
-__Pyx_CyFunction_set_dict(__pyx_CyFunctionObject *op, PyObject *value)
-{
-    PyObject *tmp;
-    if (unlikely(value == NULL)) {
-        PyErr_SetString(PyExc_TypeError,
-               "function's dictionary may not be deleted");
-        return -1;
-    }
-    if (unlikely(!PyDict_Check(value))) {
-        PyErr_SetString(PyExc_TypeError,
-               "setting function's dictionary to a non-dict");
-        return -1;
-    }
-    tmp = op->func_dict;
-    Py_INCREF(value);
-    op->func_dict = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_globals(__pyx_CyFunctionObject *op)
-{
-    Py_INCREF(op->func_globals);
-    return op->func_globals;
-}
-static PyObject *
-__Pyx_CyFunction_get_closure(CYTHON_UNUSED __pyx_CyFunctionObject *op)
-{
-    Py_INCREF(Py_None);
-    return Py_None;
-}
-static PyObject *
-__Pyx_CyFunction_get_code(__pyx_CyFunctionObject *op)
-{
-    PyObject* result = (op->func_code) ? op->func_code : Py_None;
-    Py_INCREF(result);
-    return result;
-}
-static int
-__Pyx_CyFunction_init_defaults(__pyx_CyFunctionObject *op) {
-    PyObject *res = op->defaults_getter((PyObject *) op);
-    if (unlikely(!res))
-        return -1;
-    op->defaults_tuple = PyTuple_GET_ITEM(res, 0);
-    Py_INCREF(op->defaults_tuple);
-    op->defaults_kwdict = PyTuple_GET_ITEM(res, 1);
-    Py_INCREF(op->defaults_kwdict);
-    Py_DECREF(res);
-    return 0;
-}
-static int
-__Pyx_CyFunction_set_defaults(__pyx_CyFunctionObject *op, PyObject* value) {
-    PyObject* tmp;
-    if (!value) {
-        value = Py_None;
-    } else if (value != Py_None && !PyTuple_Check(value)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "__defaults__ must be set to a tuple object");
-        return -1;
-    }
-    Py_INCREF(value);
-    tmp = op->defaults_tuple;
-    op->defaults_tuple = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_defaults(__pyx_CyFunctionObject *op) {
-    PyObject* result = op->defaults_tuple;
-    if (unlikely(!result)) {
-        if (op->defaults_getter) {
-            if (__Pyx_CyFunction_init_defaults(op) < 0) return NULL;
-            result = op->defaults_tuple;
-        } else {
-            result = Py_None;
-        }
-    }
-    Py_INCREF(result);
-    return result;
-}
-static int
-__Pyx_CyFunction_set_kwdefaults(__pyx_CyFunctionObject *op, PyObject* value) {
-    PyObject* tmp;
-    if (!value) {
-        value = Py_None;
-    } else if (value != Py_None && !PyDict_Check(value)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "__kwdefaults__ must be set to a dict object");
-        return -1;
-    }
-    Py_INCREF(value);
-    tmp = op->defaults_kwdict;
-    op->defaults_kwdict = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_kwdefaults(__pyx_CyFunctionObject *op) {
-    PyObject* result = op->defaults_kwdict;
-    if (unlikely(!result)) {
-        if (op->defaults_getter) {
-            if (__Pyx_CyFunction_init_defaults(op) < 0) return NULL;
-            result = op->defaults_kwdict;
-        } else {
-            result = Py_None;
-        }
-    }
-    Py_INCREF(result);
-    return result;
-}
-static int
-__Pyx_CyFunction_set_annotations(__pyx_CyFunctionObject *op, PyObject* value) {
-    PyObject* tmp;
-    if (!value || value == Py_None) {
-        value = NULL;
-    } else if (!PyDict_Check(value)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "__annotations__ must be set to a dict object");
-        return -1;
-    }
-    Py_XINCREF(value);
-    tmp = op->func_annotations;
-    op->func_annotations = value;
-    Py_XDECREF(tmp);
-    return 0;
-}
-static PyObject *
-__Pyx_CyFunction_get_annotations(__pyx_CyFunctionObject *op) {
-    PyObject* result = op->func_annotations;
-    if (unlikely(!result)) {
-        result = PyDict_New();
-        if (unlikely(!result)) return NULL;
-        op->func_annotations = result;
-    }
-    Py_INCREF(result);
-    return result;
-}
-static PyGetSetDef __pyx_CyFunction_getsets[] = {
-    {(char *) "func_doc", (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
-    {(char *) "__doc__",  (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
-    {(char *) "func_name", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0},
-    {(char *) "__name__", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0},
-    {(char *) "__qualname__", (getter)__Pyx_CyFunction_get_qualname, (setter)__Pyx_CyFunction_set_qualname, 0, 0},
-    {(char *) "__self__", (getter)__Pyx_CyFunction_get_self, 0, 0, 0},
-    {(char *) "func_dict", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0},
-    {(char *) "__dict__", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0},
-    {(char *) "func_globals", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0},
-    {(char *) "__globals__", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0},
-    {(char *) "func_closure", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0},
-    {(char *) "__closure__", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0},
-    {(char *) "func_code", (getter)__Pyx_CyFunction_get_code, 0, 0, 0},
-    {(char *) "__code__", (getter)__Pyx_CyFunction_get_code, 0, 0, 0},
-    {(char *) "func_defaults", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0},
-    {(char *) "__defaults__", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0},
-    {(char *) "__kwdefaults__", (getter)__Pyx_CyFunction_get_kwdefaults, (setter)__Pyx_CyFunction_set_kwdefaults, 0, 0},
-    {(char *) "__annotations__", (getter)__Pyx_CyFunction_get_annotations, (setter)__Pyx_CyFunction_set_annotations, 0, 0},
-    {0, 0, 0, 0, 0}
-};
-#ifndef PY_WRITE_RESTRICTED /* < Py2.5 */
-#define PY_WRITE_RESTRICTED WRITE_RESTRICTED
-#endif
-static PyMemberDef __pyx_CyFunction_members[] = {
-    {(char *) "__module__", T_OBJECT, offsetof(__pyx_CyFunctionObject, func.m_module), PY_WRITE_RESTRICTED, 0},
-    {0, 0, 0,  0, 0}
-};
-static PyObject *
-__Pyx_CyFunction_reduce(__pyx_CyFunctionObject *m, CYTHON_UNUSED PyObject *args)
-{
-#if PY_MAJOR_VERSION >= 3
-    return PyUnicode_FromString(m->func.m_ml->ml_name);
-#else
-    return PyString_FromString(m->func.m_ml->ml_name);
-#endif
-}
-static PyMethodDef __pyx_CyFunction_methods[] = {
-    {__Pyx_NAMESTR("__reduce__"), (PyCFunction)__Pyx_CyFunction_reduce, METH_VARARGS, 0},
-    {0, 0, 0, 0}
-};
-static PyObject *__Pyx_CyFunction_New(PyTypeObject *type, PyMethodDef *ml, int flags, PyObject* qualname,
-                                      PyObject *closure, PyObject *module, PyObject* globals, PyObject* code) {
-    __pyx_CyFunctionObject *op = PyObject_GC_New(__pyx_CyFunctionObject, type);
-    if (op == NULL)
-        return NULL;
-    op->flags = flags;
-    op->func_weakreflist = NULL;
-    op->func.m_ml = ml;
-    op->func.m_self = (PyObject *) op;
-    Py_XINCREF(closure);
-    op->func_closure = closure;
-    Py_XINCREF(module);
-    op->func.m_module = module;
-    op->func_dict = NULL;
-    op->func_name = NULL;
-    Py_INCREF(qualname);
-    op->func_qualname = qualname;
-    op->func_doc = NULL;
-    op->func_classobj = NULL;
-    op->func_globals = globals;
-    Py_INCREF(op->func_globals);
-    Py_XINCREF(code);
-    op->func_code = code;
-    op->defaults_pyobjects = 0;
-    op->defaults = NULL;
-    op->defaults_tuple = NULL;
-    op->defaults_kwdict = NULL;
-    op->defaults_getter = NULL;
-    op->func_annotations = NULL;
-    PyObject_GC_Track(op);
-    return (PyObject *) op;
-}
-static int
-__Pyx_CyFunction_clear(__pyx_CyFunctionObject *m)
-{
-    Py_CLEAR(m->func_closure);
-    Py_CLEAR(m->func.m_module);
-    Py_CLEAR(m->func_dict);
-    Py_CLEAR(m->func_name);
-    Py_CLEAR(m->func_qualname);
-    Py_CLEAR(m->func_doc);
-    Py_CLEAR(m->func_globals);
-    Py_CLEAR(m->func_code);
-    Py_CLEAR(m->func_classobj);
-    Py_CLEAR(m->defaults_tuple);
-    Py_CLEAR(m->defaults_kwdict);
-    Py_CLEAR(m->func_annotations);
-    if (m->defaults) {
-        PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
-        int i;
-        for (i = 0; i < m->defaults_pyobjects; i++)
-            Py_XDECREF(pydefaults[i]);
-        PyMem_Free(m->defaults);
-        m->defaults = NULL;
-    }
-    return 0;
-}
-static void __Pyx_CyFunction_dealloc(__pyx_CyFunctionObject *m)
-{
-    PyObject_GC_UnTrack(m);
-    if (m->func_weakreflist != NULL)
-        PyObject_ClearWeakRefs((PyObject *) m);
-    __Pyx_CyFunction_clear(m);
-    PyObject_GC_Del(m);
-}
-static int __Pyx_CyFunction_traverse(__pyx_CyFunctionObject *m, visitproc visit, void *arg)
-{
-    Py_VISIT(m->func_closure);
-    Py_VISIT(m->func.m_module);
-    Py_VISIT(m->func_dict);
-    Py_VISIT(m->func_name);
-    Py_VISIT(m->func_qualname);
-    Py_VISIT(m->func_doc);
-    Py_VISIT(m->func_globals);
-    Py_VISIT(m->func_code);
-    Py_VISIT(m->func_classobj);
-    Py_VISIT(m->defaults_tuple);
-    Py_VISIT(m->defaults_kwdict);
-    if (m->defaults) {
-        PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
-        int i;
-        for (i = 0; i < m->defaults_pyobjects; i++)
-            Py_VISIT(pydefaults[i]);
-    }
-    return 0;
-}
-static PyObject *__Pyx_CyFunction_descr_get(PyObject *func, PyObject *obj, PyObject *type)
-{
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    if (m->flags & __Pyx_CYFUNCTION_STATICMETHOD) {
-        Py_INCREF(func);
-        return func;
-    }
-    if (m->flags & __Pyx_CYFUNCTION_CLASSMETHOD) {
-        if (type == NULL)
-            type = (PyObject *)(Py_TYPE(obj));
-        return PyMethod_New(func,
-                            type, (PyObject *)(Py_TYPE(type)));
-    }
-    if (obj == Py_None)
-        obj = NULL;
-    return PyMethod_New(func, obj, type);
-}
-static PyObject*
-__Pyx_CyFunction_repr(__pyx_CyFunctionObject *op)
-{
-#if PY_MAJOR_VERSION >= 3
-    return PyUnicode_FromFormat("<cyfunction %U at %p>",
-                                op->func_qualname, (void *)op);
-#else
-    return PyString_FromFormat("<cyfunction %s at %p>",
-                               PyString_AsString(op->func_qualname), (void *)op);
-#endif
-}
-#if CYTHON_COMPILING_IN_PYPY
-static PyObject * __Pyx_CyFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) {
-    PyCFunctionObject* f = (PyCFunctionObject*)func;
-    PyCFunction meth = PyCFunction_GET_FUNCTION(func);
-    PyObject *self = PyCFunction_GET_SELF(func);
-    Py_ssize_t size;
-    switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
-    case METH_VARARGS:
-        if (likely(kw == NULL) || PyDict_Size(kw) == 0)
-            return (*meth)(self, arg);
-        break;
-    case METH_VARARGS | METH_KEYWORDS:
-        return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
-    case METH_NOARGS:
-        if (likely(kw == NULL) || PyDict_Size(kw) == 0) {
-            size = PyTuple_GET_SIZE(arg);
-            if (size == 0)
-                return (*meth)(self, NULL);
-            PyErr_Format(PyExc_TypeError,
-                "%.200s() takes no arguments (%zd given)",
-                f->m_ml->ml_name, size);
-            return NULL;
-        }
-        break;
-    case METH_O:
-        if (likely(kw == NULL) || PyDict_Size(kw) == 0) {
-            size = PyTuple_GET_SIZE(arg);
-            if (size == 1)
-                return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
-            PyErr_Format(PyExc_TypeError,
-                "%.200s() takes exactly one argument (%zd given)",
-                f->m_ml->ml_name, size);
-            return NULL;
-        }
-        break;
-    default:
-        PyErr_SetString(PyExc_SystemError, "Bad call flags in "
-                        "__Pyx_CyFunction_Call. METH_OLDARGS is no "
-                        "longer supported!");
-        return NULL;
-    }
-    PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
-                 f->m_ml->ml_name);
-    return NULL;
-}
-#else
-static PyObject * __Pyx_CyFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) {
-	return PyCFunction_Call(func, arg, kw);
-}
-#endif
-static PyTypeObject __pyx_CyFunctionType_type = {
-    PyVarObject_HEAD_INIT(0, 0)
-    __Pyx_NAMESTR("cython_function_or_method"), /*tp_name*/
-    sizeof(__pyx_CyFunctionObject),   /*tp_basicsize*/
-    0,                                  /*tp_itemsize*/
-    (destructor) __Pyx_CyFunction_dealloc, /*tp_dealloc*/
-    0,                                  /*tp_print*/
-    0,                                  /*tp_getattr*/
-    0,                                  /*tp_setattr*/
-#if PY_MAJOR_VERSION < 3
-    0,                                  /*tp_compare*/
-#else
-    0,                                  /*reserved*/
-#endif
-    (reprfunc) __Pyx_CyFunction_repr,   /*tp_repr*/
-    0,                                  /*tp_as_number*/
-    0,                                  /*tp_as_sequence*/
-    0,                                  /*tp_as_mapping*/
-    0,                                  /*tp_hash*/
-    __Pyx_CyFunction_Call,              /*tp_call*/
-    0,                                  /*tp_str*/
-    0,                                  /*tp_getattro*/
-    0,                                  /*tp_setattro*/
-    0,                                  /*tp_as_buffer*/
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags*/
-    0,                                  /*tp_doc*/
-    (traverseproc) __Pyx_CyFunction_traverse,   /*tp_traverse*/
-    (inquiry) __Pyx_CyFunction_clear,   /*tp_clear*/
-    0,                                  /*tp_richcompare*/
-    offsetof(__pyx_CyFunctionObject, func_weakreflist), /* tp_weaklistoffse */
-    0,                                  /*tp_iter*/
-    0,                                  /*tp_iternext*/
-    __pyx_CyFunction_methods,           /*tp_methods*/
-    __pyx_CyFunction_members,           /*tp_members*/
-    __pyx_CyFunction_getsets,           /*tp_getset*/
-    0,                                  /*tp_base*/
-    0,                                  /*tp_dict*/
-    __Pyx_CyFunction_descr_get,         /*tp_descr_get*/
-    0,                                  /*tp_descr_set*/
-    offsetof(__pyx_CyFunctionObject, func_dict),/*tp_dictoffset*/
-    0,                                  /*tp_init*/
-    0,                                  /*tp_alloc*/
-    0,                                  /*tp_new*/
-    0,                                  /*tp_free*/
-    0,                                  /*tp_is_gc*/
-    0,                                  /*tp_bases*/
-    0,                                  /*tp_mro*/
-    0,                                  /*tp_cache*/
-    0,                                  /*tp_subclasses*/
-    0,                                  /*tp_weaklist*/
-    0,                                  /*tp_del*/
-#if PY_VERSION_HEX >= 0x02060000
-    0,                                  /*tp_version_tag*/
-#endif
-#if PY_VERSION_HEX >= 0x030400a1
-    0,                                  /*tp_finalize*/
-#endif
-};
-static int __Pyx_CyFunction_init(void) {
-#if !CYTHON_COMPILING_IN_PYPY
-    __pyx_CyFunctionType_type.tp_call = PyCFunction_Call;
-#endif
-    __pyx_CyFunctionType = __Pyx_FetchCommonType(&__pyx_CyFunctionType_type);
-    if (__pyx_CyFunctionType == NULL) {
-        return -1;
-    }
-    return 0;
-}
-static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *func, size_t size, int pyobjects) {
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    m->defaults = PyMem_Malloc(size);
-    if (!m->defaults)
-        return PyErr_NoMemory();
-    memset(m->defaults, 0, size);
-    m->defaults_pyobjects = pyobjects;
-    return m->defaults;
-}
-static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *func, PyObject *tuple) {
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    m->defaults_tuple = tuple;
-    Py_INCREF(tuple);
-}
-static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *func, PyObject *dict) {
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    m->defaults_kwdict = dict;
-    Py_INCREF(dict);
-}
-static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *func, PyObject *dict) {
-    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
-    m->func_annotations = dict;
-    Py_INCREF(dict);
-}
-
-static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name,
-                                           PyObject *qualname, PyObject *mkw, PyObject *modname, PyObject *doc) {
-    PyObject *ns;
-    if (metaclass) {
-        PyObject *prep = __Pyx_PyObject_GetAttrStr(metaclass, __pyx_n_s_prepare);
-        if (prep) {
-            PyObject *pargs = PyTuple_Pack(2, name, bases);
-            if (unlikely(!pargs)) {
-                Py_DECREF(prep);
-                return NULL;
-            }
-            ns = PyObject_Call(prep, pargs, mkw);
-            Py_DECREF(prep);
-            Py_DECREF(pargs);
-        } else {
-            if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError)))
-                return NULL;
-            PyErr_Clear();
-            ns = PyDict_New();
-        }
-    } else {
-        ns = PyDict_New();
-    }
-    if (unlikely(!ns))
-        return NULL;
-    if (unlikely(PyObject_SetItem(ns, __pyx_n_s_module, modname) < 0)) goto bad;
-    if (unlikely(PyObject_SetItem(ns, __pyx_n_s_qualname, qualname) < 0)) goto bad;
-    if (unlikely(doc && PyObject_SetItem(ns, __pyx_n_s_doc, doc) < 0)) goto bad;
-    return ns;
-bad:
-    Py_DECREF(ns);
-    return NULL;
-}
-static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases,
-                                      PyObject *dict, PyObject *mkw,
-                                      int calculate_metaclass, int allow_py2_metaclass) {
-    PyObject *result, *margs;
-    PyObject *owned_metaclass = NULL;
-    if (allow_py2_metaclass) {
-        owned_metaclass = PyObject_GetItem(dict, __pyx_n_s_metaclass);
-        if (owned_metaclass) {
-            metaclass = owned_metaclass;
-        } else if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) {
-            PyErr_Clear();
-        } else {
-            return NULL;
-        }
-    }
-    if (calculate_metaclass && (!metaclass || PyType_Check(metaclass))) {
-        metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases);
-        Py_XDECREF(owned_metaclass);
-        if (unlikely(!metaclass))
-            return NULL;
-        owned_metaclass = metaclass;
-    }
-    margs = PyTuple_Pack(3, name, bases, dict);
-    if (unlikely(!margs)) {
-        result = NULL;
-    } else {
-        result = PyObject_Call(metaclass, margs, mkw);
-        Py_DECREF(margs);
-    }
-    Py_XDECREF(owned_metaclass);
-    return result;
-}
-
-static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) {
-    PyObject *empty_list = 0;
-    PyObject *module = 0;
-    PyObject *global_dict = 0;
-    PyObject *empty_dict = 0;
-    PyObject *list;
-    #if PY_VERSION_HEX < 0x03030000
-    PyObject *py_import;
-    py_import = __Pyx_PyObject_GetAttrStr(__pyx_b, __pyx_n_s_import);
-    if (!py_import)
-        goto bad;
-    #endif
-    if (from_list)
-        list = from_list;
-    else {
-        empty_list = PyList_New(0);
-        if (!empty_list)
-            goto bad;
-        list = empty_list;
-    }
-    global_dict = PyModule_GetDict(__pyx_m);
-    if (!global_dict)
-        goto bad;
-    empty_dict = PyDict_New();
-    if (!empty_dict)
-        goto bad;
-    #if PY_VERSION_HEX >= 0x02050000
-    {
-        #if PY_MAJOR_VERSION >= 3
-        if (level == -1) {
-            if (strchr(__Pyx_MODULE_NAME, '.')) {
-                #if PY_VERSION_HEX < 0x03030000
-                PyObject *py_level = PyInt_FromLong(1);
-                if (!py_level)
-                    goto bad;
-                module = PyObject_CallFunctionObjArgs(py_import,
-                    name, global_dict, empty_dict, list, py_level, NULL);
-                Py_DECREF(py_level);
-                #else
-                module = PyImport_ImportModuleLevelObject(
-                    name, global_dict, empty_dict, list, 1);
-                #endif
-                if (!module) {
-                    if (!PyErr_ExceptionMatches(PyExc_ImportError))
-                        goto bad;
-                    PyErr_Clear();
-                }
-            }
-            level = 0; /* try absolute import on failure */
-        }
-        #endif
-        if (!module) {
-            #if PY_VERSION_HEX < 0x03030000
-            PyObject *py_level = PyInt_FromLong(level);
-            if (!py_level)
-                goto bad;
-            module = PyObject_CallFunctionObjArgs(py_import,
-                name, global_dict, empty_dict, list, py_level, NULL);
-            Py_DECREF(py_level);
-            #else
-            module = PyImport_ImportModuleLevelObject(
-                name, global_dict, empty_dict, list, level);
-            #endif
-        }
-    }
-    #else
-    if (level>0) {
-        PyErr_SetString(PyExc_RuntimeError, "Relative import is not supported for Python <=2.4.");
-        goto bad;
-    }
-    module = PyObject_CallFunctionObjArgs(py_import,
-        name, global_dict, empty_dict, list, NULL);
-    #endif
-bad:
-    #if PY_VERSION_HEX < 0x03030000
-    Py_XDECREF(py_import);
-    #endif
-    Py_XDECREF(empty_list);
-    Py_XDECREF(empty_dict);
-    return module;
-}
-
-#define __PYX_VERIFY_RETURN_INT(target_type, func_type, func)             \
-    {                                                                     \
-        func_type value = func(x);                                        \
-        if (sizeof(target_type) < sizeof(func_type)) {                    \
-            if (unlikely(value != (func_type) (target_type) value)) {     \
-                func_type zero = 0;                                       \
-                PyErr_SetString(PyExc_OverflowError,                      \
-                    (is_unsigned && unlikely(value < zero)) ?             \
-                    "can't convert negative value to " #target_type :     \
-                    "value too large to convert to " #target_type);       \
-                return (target_type) -1;                                  \
-            }                                                             \
-        }                                                                 \
-        return (target_type) value;                                       \
-    }
-
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-  #include "longintrepr.h"
- #endif
-#endif
-static CYTHON_INLINE uint64_t __Pyx_PyInt_As_uint64_t(PyObject *x) {
-    const uint64_t neg_one = (uint64_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-#if PY_MAJOR_VERSION < 3
-    if (likely(PyInt_Check(x))) {
-        if (sizeof(uint64_t) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(uint64_t, long, PyInt_AS_LONG)
-        } else {
-            long val = PyInt_AS_LONG(x);
-            if (is_unsigned && unlikely(val < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint64_t");
-                return (uint64_t) -1;
-            }
-            return (uint64_t) val;
-        }
-    } else
-#endif
-    if (likely(PyLong_Check(x))) {
-        if (is_unsigned) {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint64_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return (uint64_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (unlikely(Py_SIZE(x) < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint64_t");
-                return (uint64_t) -1;
-            }
-            if (sizeof(uint64_t) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(uint64_t, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(uint64_t) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(uint64_t, unsigned long long, PyLong_AsUnsignedLongLong)
-            }
-        } else {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint64_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return +(uint64_t) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(uint64_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (sizeof(uint64_t) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(uint64_t, long, PyLong_AsLong)
-            } else if (sizeof(uint64_t) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(uint64_t, long long, PyLong_AsLongLong)
-            }
-        }
-        {
-#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
-            PyErr_SetString(PyExc_RuntimeError,
-                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
-#else
-            uint64_t val;
-            PyObject *v = __Pyx_PyNumber_Int(x);
- #if PY_MAJOR_VERSION < 3
-            if (likely(v) && !PyLong_Check(v)) {
-                PyObject *tmp = v;
-                v = PyNumber_Long(tmp);
-                Py_DECREF(tmp);
-            }
- #endif
-            if (likely(v)) {
-                int one = 1; int is_little = (int)*(unsigned char *)&one;
-                unsigned char *bytes = (unsigned char *)&val;
-                int ret = _PyLong_AsByteArray((PyLongObject *)v,
-                                              bytes, sizeof(val),
-                                              is_little, !is_unsigned);
-                Py_DECREF(v);
-                if (likely(!ret))
-                    return val;
-            }
-#endif
-            return (uint64_t) -1;
-        }
-    } else {
-        uint64_t val;
-        PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (uint64_t) -1;
-        val = __Pyx_PyInt_As_uint64_t(tmp);
-        Py_DECREF(tmp);
-        return val;
-    }
-}
-
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-  #include "longintrepr.h"
- #endif
-#endif
-static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {
-    const int neg_one = (int) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-#if PY_MAJOR_VERSION < 3
-    if (likely(PyInt_Check(x))) {
-        if (sizeof(int) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG)
-        } else {
-            long val = PyInt_AS_LONG(x);
-            if (is_unsigned && unlikely(val < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to int");
-                return (int) -1;
-            }
-            return (int) val;
-        }
-    } else
-#endif
-    if (likely(PyLong_Check(x))) {
-        if (is_unsigned) {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(int)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return (int) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (unlikely(Py_SIZE(x) < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to int");
-                return (int) -1;
-            }
-            if (sizeof(int) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(int, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(int) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(int, unsigned long long, PyLong_AsUnsignedLongLong)
-            }
-        } else {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(int)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return +(int) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(int) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (sizeof(int) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(int, long, PyLong_AsLong)
-            } else if (sizeof(int) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(int, long long, PyLong_AsLongLong)
-            }
-        }
-        {
-#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
-            PyErr_SetString(PyExc_RuntimeError,
-                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
-#else
-            int val;
-            PyObject *v = __Pyx_PyNumber_Int(x);
- #if PY_MAJOR_VERSION < 3
-            if (likely(v) && !PyLong_Check(v)) {
-                PyObject *tmp = v;
-                v = PyNumber_Long(tmp);
-                Py_DECREF(tmp);
-            }
- #endif
-            if (likely(v)) {
-                int one = 1; int is_little = (int)*(unsigned char *)&one;
-                unsigned char *bytes = (unsigned char *)&val;
-                int ret = _PyLong_AsByteArray((PyLongObject *)v,
-                                              bytes, sizeof(val),
-                                              is_little, !is_unsigned);
-                Py_DECREF(v);
-                if (likely(!ret))
-                    return val;
-            }
-#endif
-            return (int) -1;
-        }
-    } else {
-        int val;
-        PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (int) -1;
-        val = __Pyx_PyInt_As_int(tmp);
-        Py_DECREF(tmp);
-        return val;
-    }
-}
-
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-  #include "longintrepr.h"
- #endif
-#endif
-static CYTHON_INLINE uint32_t __Pyx_PyInt_As_uint32_t(PyObject *x) {
-    const uint32_t neg_one = (uint32_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-#if PY_MAJOR_VERSION < 3
-    if (likely(PyInt_Check(x))) {
-        if (sizeof(uint32_t) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(uint32_t, long, PyInt_AS_LONG)
-        } else {
-            long val = PyInt_AS_LONG(x);
-            if (is_unsigned && unlikely(val < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint32_t");
-                return (uint32_t) -1;
-            }
-            return (uint32_t) val;
-        }
-    } else
-#endif
-    if (likely(PyLong_Check(x))) {
-        if (is_unsigned) {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint32_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return (uint32_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (unlikely(Py_SIZE(x) < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint32_t");
-                return (uint32_t) -1;
-            }
-            if (sizeof(uint32_t) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(uint32_t, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(uint32_t) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(uint32_t, unsigned long long, PyLong_AsUnsignedLongLong)
-            }
-        } else {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint32_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return +(uint32_t) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(uint32_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (sizeof(uint32_t) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(uint32_t, long, PyLong_AsLong)
-            } else if (sizeof(uint32_t) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(uint32_t, long long, PyLong_AsLongLong)
-            }
-        }
-        {
-#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
-            PyErr_SetString(PyExc_RuntimeError,
-                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
-#else
-            uint32_t val;
-            PyObject *v = __Pyx_PyNumber_Int(x);
- #if PY_MAJOR_VERSION < 3
-            if (likely(v) && !PyLong_Check(v)) {
-                PyObject *tmp = v;
-                v = PyNumber_Long(tmp);
-                Py_DECREF(tmp);
-            }
- #endif
-            if (likely(v)) {
-                int one = 1; int is_little = (int)*(unsigned char *)&one;
-                unsigned char *bytes = (unsigned char *)&val;
-                int ret = _PyLong_AsByteArray((PyLongObject *)v,
-                                              bytes, sizeof(val),
-                                              is_little, !is_unsigned);
-                Py_DECREF(v);
-                if (likely(!ret))
-                    return val;
-            }
-#endif
-            return (uint32_t) -1;
-        }
-    } else {
-        uint32_t val;
-        PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (uint32_t) -1;
-        val = __Pyx_PyInt_As_uint32_t(tmp);
-        Py_DECREF(tmp);
-        return val;
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value) {
-    const int neg_one = (int) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(int) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(int) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(int) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(int),
-                                     little, !is_unsigned);
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int8_t(int8_t value) {
-    const int8_t neg_one = (int8_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(int8_t) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int8_t) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(int8_t) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(int8_t) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int8_t) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(int8_t),
-                                     little, !is_unsigned);
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint8_t(uint8_t value) {
-    const uint8_t neg_one = (uint8_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(uint8_t) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(uint8_t) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(uint8_t) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(uint8_t) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(uint8_t) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(uint8_t),
-                                     little, !is_unsigned);
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int16_t(int16_t value) {
-    const int16_t neg_one = (int16_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(int16_t) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int16_t) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(int16_t) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(int16_t) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int16_t) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(int16_t),
-                                     little, !is_unsigned);
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint16_t(uint16_t value) {
-    const uint16_t neg_one = (uint16_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(uint16_t) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(uint16_t) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(uint16_t) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(uint16_t) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(uint16_t) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(uint16_t),
-                                     little, !is_unsigned);
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int32_t(int32_t value) {
-    const int32_t neg_one = (int32_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(int32_t) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int32_t) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(int32_t) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(int32_t) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int32_t) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(int32_t),
-                                     little, !is_unsigned);
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint32_t(uint32_t value) {
-    const uint32_t neg_one = (uint32_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(uint32_t) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(uint32_t) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(uint32_t) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(uint32_t) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(uint32_t) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(uint32_t),
-                                     little, !is_unsigned);
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {
-    const long neg_one = (long) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(long) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(long) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(long) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(long) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(long) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(long),
-                                     little, !is_unsigned);
-    }
-}
-
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-  #include "longintrepr.h"
- #endif
-#endif
-static CYTHON_INLINE size_t __Pyx_PyInt_As_size_t(PyObject *x) {
-    const size_t neg_one = (size_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-#if PY_MAJOR_VERSION < 3
-    if (likely(PyInt_Check(x))) {
-        if (sizeof(size_t) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(size_t, long, PyInt_AS_LONG)
-        } else {
-            long val = PyInt_AS_LONG(x);
-            if (is_unsigned && unlikely(val < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to size_t");
-                return (size_t) -1;
-            }
-            return (size_t) val;
-        }
-    } else
-#endif
-    if (likely(PyLong_Check(x))) {
-        if (is_unsigned) {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(size_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return (size_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (unlikely(Py_SIZE(x) < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to size_t");
-                return (size_t) -1;
-            }
-            if (sizeof(size_t) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(size_t, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(size_t) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(size_t, unsigned long long, PyLong_AsUnsignedLongLong)
-            }
-        } else {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(size_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return +(size_t) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(size_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (sizeof(size_t) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(size_t, long, PyLong_AsLong)
-            } else if (sizeof(size_t) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(size_t, long long, PyLong_AsLongLong)
-            }
-        }
-        {
-#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
-            PyErr_SetString(PyExc_RuntimeError,
-                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
-#else
-            size_t val;
-            PyObject *v = __Pyx_PyNumber_Int(x);
- #if PY_MAJOR_VERSION < 3
-            if (likely(v) && !PyLong_Check(v)) {
-                PyObject *tmp = v;
-                v = PyNumber_Long(tmp);
-                Py_DECREF(tmp);
-            }
- #endif
-            if (likely(v)) {
-                int one = 1; int is_little = (int)*(unsigned char *)&one;
-                unsigned char *bytes = (unsigned char *)&val;
-                int ret = _PyLong_AsByteArray((PyLongObject *)v,
-                                              bytes, sizeof(val),
-                                              is_little, !is_unsigned);
-                Py_DECREF(v);
-                if (likely(!ret))
-                    return val;
-            }
-#endif
-            return (size_t) -1;
-        }
-    } else {
-        size_t val;
-        PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (size_t) -1;
-        val = __Pyx_PyInt_As_size_t(tmp);
-        Py_DECREF(tmp);
-        return val;
-    }
-}
-
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-  #include "longintrepr.h"
- #endif
-#endif
-static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_As_PY_LONG_LONG(PyObject *x) {
-    const PY_LONG_LONG neg_one = (PY_LONG_LONG) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-#if PY_MAJOR_VERSION < 3
-    if (likely(PyInt_Check(x))) {
-        if (sizeof(PY_LONG_LONG) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(PY_LONG_LONG, long, PyInt_AS_LONG)
-        } else {
-            long val = PyInt_AS_LONG(x);
-            if (is_unsigned && unlikely(val < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to PY_LONG_LONG");
-                return (PY_LONG_LONG) -1;
-            }
-            return (PY_LONG_LONG) val;
-        }
-    } else
-#endif
-    if (likely(PyLong_Check(x))) {
-        if (is_unsigned) {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(PY_LONG_LONG)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return (PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (unlikely(Py_SIZE(x) < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to PY_LONG_LONG");
-                return (PY_LONG_LONG) -1;
-            }
-            if (sizeof(PY_LONG_LONG) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(PY_LONG_LONG, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(PY_LONG_LONG) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(PY_LONG_LONG, unsigned long long, PyLong_AsUnsignedLongLong)
-            }
-        } else {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(PY_LONG_LONG)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return +(PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (sizeof(PY_LONG_LONG) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(PY_LONG_LONG, long, PyLong_AsLong)
-            } else if (sizeof(PY_LONG_LONG) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(PY_LONG_LONG, long long, PyLong_AsLongLong)
-            }
-        }
-        {
-#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
-            PyErr_SetString(PyExc_RuntimeError,
-                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
-#else
-            PY_LONG_LONG val;
-            PyObject *v = __Pyx_PyNumber_Int(x);
- #if PY_MAJOR_VERSION < 3
-            if (likely(v) && !PyLong_Check(v)) {
-                PyObject *tmp = v;
-                v = PyNumber_Long(tmp);
-                Py_DECREF(tmp);
-            }
- #endif
-            if (likely(v)) {
-                int one = 1; int is_little = (int)*(unsigned char *)&one;
-                unsigned char *bytes = (unsigned char *)&val;
-                int ret = _PyLong_AsByteArray((PyLongObject *)v,
-                                              bytes, sizeof(val),
-                                              is_little, !is_unsigned);
-                Py_DECREF(v);
-                if (likely(!ret))
-                    return val;
-            }
-#endif
-            return (PY_LONG_LONG) -1;
-        }
-    } else {
-        PY_LONG_LONG val;
-        PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (PY_LONG_LONG) -1;
-        val = __Pyx_PyInt_As_PY_LONG_LONG(tmp);
-        Py_DECREF(tmp);
-        return val;
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_PY_LONG_LONG(PY_LONG_LONG value) {
-    const PY_LONG_LONG neg_one = (PY_LONG_LONG) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(PY_LONG_LONG) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(PY_LONG_LONG) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(PY_LONG_LONG) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(PY_LONG_LONG) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(PY_LONG_LONG) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(PY_LONG_LONG),
-                                     little, !is_unsigned);
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int64_t(int64_t value) {
-    const int64_t neg_one = (int64_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(int64_t) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int64_t) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(int64_t) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(int64_t) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(int64_t) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(int64_t),
-                                     little, !is_unsigned);
-    }
-}
-
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_uint64_t(uint64_t value) {
-    const uint64_t neg_one = (uint64_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(uint64_t) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(uint64_t) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(uint64_t) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(uint64_t) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(uint64_t) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(uint64_t),
-                                     little, !is_unsigned);
-    }
-}
-
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-  #include "longintrepr.h"
- #endif
-#endif
-static CYTHON_INLINE int64_t __Pyx_PyInt_As_int64_t(PyObject *x) {
-    const int64_t neg_one = (int64_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-#if PY_MAJOR_VERSION < 3
-    if (likely(PyInt_Check(x))) {
-        if (sizeof(int64_t) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(int64_t, long, PyInt_AS_LONG)
-        } else {
-            long val = PyInt_AS_LONG(x);
-            if (is_unsigned && unlikely(val < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to int64_t");
-                return (int64_t) -1;
-            }
-            return (int64_t) val;
-        }
-    } else
-#endif
-    if (likely(PyLong_Check(x))) {
-        if (is_unsigned) {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(int64_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return (int64_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (unlikely(Py_SIZE(x) < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to int64_t");
-                return (int64_t) -1;
-            }
-            if (sizeof(int64_t) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(int64_t, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(int64_t) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(int64_t, unsigned long long, PyLong_AsUnsignedLongLong)
-            }
-        } else {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(int64_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return +(int64_t) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(int64_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (sizeof(int64_t) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(int64_t, long, PyLong_AsLong)
-            } else if (sizeof(int64_t) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(int64_t, long long, PyLong_AsLongLong)
-            }
-        }
-        {
-#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
-            PyErr_SetString(PyExc_RuntimeError,
-                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
+static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
+#if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
+    PyObject *ob = PyCapsule_New(vtable, 0, 0);
 #else
-            int64_t val;
-            PyObject *v = __Pyx_PyNumber_Int(x);
- #if PY_MAJOR_VERSION < 3
-            if (likely(v) && !PyLong_Check(v)) {
-                PyObject *tmp = v;
-                v = PyNumber_Long(tmp);
-                Py_DECREF(tmp);
-            }
- #endif
-            if (likely(v)) {
-                int one = 1; int is_little = (int)*(unsigned char *)&one;
-                unsigned char *bytes = (unsigned char *)&val;
-                int ret = _PyLong_AsByteArray((PyLongObject *)v,
-                                              bytes, sizeof(val),
-                                              is_little, !is_unsigned);
-                Py_DECREF(v);
-                if (likely(!ret))
-                    return val;
-            }
+    PyObject *ob = PyCObject_FromVoidPtr(vtable, 0);
 #endif
-            return (int64_t) -1;
-        }
-    } else {
-        int64_t val;
-        PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (int64_t) -1;
-        val = __Pyx_PyInt_As_int64_t(tmp);
-        Py_DECREF(tmp);
-        return val;
-    }
+    if (!ob)
+        goto bad;
+    if (PyDict_SetItem(dict, __pyx_n_s_pyx_vtable, ob) < 0)
+        goto bad;
+    Py_DECREF(ob);
+    return 0;
+bad:
+    Py_XDECREF(ob);
+    return -1;
 }
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_char(char value) {
-    const char neg_one = (char) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(char) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(char) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(char) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(char) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(char) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
+static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) {
+    PyObject *empty_list = 0;
+    PyObject *module = 0;
+    PyObject *global_dict = 0;
+    PyObject *empty_dict = 0;
+    PyObject *list;
+    #if PY_VERSION_HEX < 0x03030000
+    PyObject *py_import;
+    py_import = __Pyx_PyObject_GetAttrStr(__pyx_b, __pyx_n_s_import);
+    if (!py_import)
+        goto bad;
+    #endif
+    if (from_list)
+        list = from_list;
+    else {
+        empty_list = PyList_New(0);
+        if (!empty_list)
+            goto bad;
+        list = empty_list;
     }
+    global_dict = PyModule_GetDict(__pyx_m);
+    if (!global_dict)
+        goto bad;
+    empty_dict = PyDict_New();
+    if (!empty_dict)
+        goto bad;
+    #if PY_VERSION_HEX >= 0x02050000
     {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(char),
-                                     little, !is_unsigned);
-    }
-}
-
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-  #include "longintrepr.h"
- #endif
-#endif
-static CYTHON_INLINE uint8_t __Pyx_PyInt_As_uint8_t(PyObject *x) {
-    const uint8_t neg_one = (uint8_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-#if PY_MAJOR_VERSION < 3
-    if (likely(PyInt_Check(x))) {
-        if (sizeof(uint8_t) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(uint8_t, long, PyInt_AS_LONG)
-        } else {
-            long val = PyInt_AS_LONG(x);
-            if (is_unsigned && unlikely(val < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint8_t");
-                return (uint8_t) -1;
-            }
-            return (uint8_t) val;
-        }
-    } else
-#endif
-    if (likely(PyLong_Check(x))) {
-        if (is_unsigned) {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint8_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return (uint8_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (unlikely(Py_SIZE(x) < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint8_t");
-                return (uint8_t) -1;
-            }
-            if (sizeof(uint8_t) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(uint8_t, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(uint8_t) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(uint8_t, unsigned long long, PyLong_AsUnsignedLongLong)
-            }
-        } else {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint8_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return +(uint8_t) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(uint8_t) ((PyLongObject*)x)->ob_digit[0];
+        #if PY_MAJOR_VERSION >= 3
+        if (level == -1) {
+            if (strchr(__Pyx_MODULE_NAME, '.')) {
+                #if PY_VERSION_HEX < 0x03030000
+                PyObject *py_level = PyInt_FromLong(1);
+                if (!py_level)
+                    goto bad;
+                module = PyObject_CallFunctionObjArgs(py_import,
+                    name, global_dict, empty_dict, list, py_level, NULL);
+                Py_DECREF(py_level);
+                #else
+                module = PyImport_ImportModuleLevelObject(
+                    name, global_dict, empty_dict, list, 1);
+                #endif
+                if (!module) {
+                    if (!PyErr_ExceptionMatches(PyExc_ImportError))
+                        goto bad;
+                    PyErr_Clear();
                 }
             }
- #endif
-#endif
-            if (sizeof(uint8_t) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(uint8_t, long, PyLong_AsLong)
-            } else if (sizeof(uint8_t) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(uint8_t, long long, PyLong_AsLongLong)
-            }
+            level = 0; /* try absolute import on failure */
         }
-        {
-#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
-            PyErr_SetString(PyExc_RuntimeError,
-                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
-#else
-            uint8_t val;
-            PyObject *v = __Pyx_PyNumber_Int(x);
- #if PY_MAJOR_VERSION < 3
-            if (likely(v) && !PyLong_Check(v)) {
-                PyObject *tmp = v;
-                v = PyNumber_Long(tmp);
-                Py_DECREF(tmp);
-            }
- #endif
-            if (likely(v)) {
-                int one = 1; int is_little = (int)*(unsigned char *)&one;
-                unsigned char *bytes = (unsigned char *)&val;
-                int ret = _PyLong_AsByteArray((PyLongObject *)v,
-                                              bytes, sizeof(val),
-                                              is_little, !is_unsigned);
-                Py_DECREF(v);
-                if (likely(!ret))
-                    return val;
-            }
-#endif
-            return (uint8_t) -1;
+        #endif
+        if (!module) {
+            #if PY_VERSION_HEX < 0x03030000
+            PyObject *py_level = PyInt_FromLong(level);
+            if (!py_level)
+                goto bad;
+            module = PyObject_CallFunctionObjArgs(py_import,
+                name, global_dict, empty_dict, list, py_level, NULL);
+            Py_DECREF(py_level);
+            #else
+            module = PyImport_ImportModuleLevelObject(
+                name, global_dict, empty_dict, list, level);
+            #endif
         }
-    } else {
-        uint8_t val;
-        PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (uint8_t) -1;
-        val = __Pyx_PyInt_As_uint8_t(tmp);
-        Py_DECREF(tmp);
-        return val;
     }
-}
-
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-  #include "longintrepr.h"
- #endif
-#endif
-static CYTHON_INLINE int32_t __Pyx_PyInt_As_int32_t(PyObject *x) {
-    const int32_t neg_one = (int32_t) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-#if PY_MAJOR_VERSION < 3
-    if (likely(PyInt_Check(x))) {
-        if (sizeof(int32_t) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(int32_t, long, PyInt_AS_LONG)
-        } else {
-            long val = PyInt_AS_LONG(x);
-            if (is_unsigned && unlikely(val < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to int32_t");
-                return (int32_t) -1;
-            }
-            return (int32_t) val;
-        }
-    } else
-#endif
-    if (likely(PyLong_Check(x))) {
-        if (is_unsigned) {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(int32_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return (int32_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (unlikely(Py_SIZE(x) < 0)) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to int32_t");
-                return (int32_t) -1;
-            }
-            if (sizeof(int32_t) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(int32_t, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(int32_t) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(int32_t, unsigned long long, PyLong_AsUnsignedLongLong)
-            }
-        } else {
-#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
- #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(int32_t)) {
-                switch (Py_SIZE(x)) {
-                    case  0: return 0;
-                    case  1: return +(int32_t) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(int32_t) ((PyLongObject*)x)->ob_digit[0];
-                }
-            }
- #endif
-#endif
-            if (sizeof(int32_t) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(int32_t, long, PyLong_AsLong)
-            } else if (sizeof(int32_t) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(int32_t, long long, PyLong_AsLongLong)
-            }
-        }
-        {
-#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
-            PyErr_SetString(PyExc_RuntimeError,
-                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
-#else
-            int32_t val;
-            PyObject *v = __Pyx_PyNumber_Int(x);
- #if PY_MAJOR_VERSION < 3
-            if (likely(v) && !PyLong_Check(v)) {
-                PyObject *tmp = v;
-                v = PyNumber_Long(tmp);
-                Py_DECREF(tmp);
-            }
- #endif
-            if (likely(v)) {
-                int one = 1; int is_little = (int)*(unsigned char *)&one;
-                unsigned char *bytes = (unsigned char *)&val;
-                int ret = _PyLong_AsByteArray((PyLongObject *)v,
-                                              bytes, sizeof(val),
-                                              is_little, !is_unsigned);
-                Py_DECREF(v);
-                if (likely(!ret))
-                    return val;
-            }
-#endif
-            return (int32_t) -1;
+    #else
+    if (level>0) {
+        PyErr_SetString(PyExc_RuntimeError, "Relative import is not supported for Python <=2.4.");
+        goto bad;
+    }
+    module = PyObject_CallFunctionObjArgs(py_import,
+        name, global_dict, empty_dict, list, NULL);
+    #endif
+bad:
+    #if PY_VERSION_HEX < 0x03030000
+    Py_XDECREF(py_import);
+    #endif
+    Py_XDECREF(empty_list);
+    Py_XDECREF(empty_dict);
+    return module;
+}
+
+static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {
+    const long neg_one = (long) -1, const_zero = 0;
+    const int is_unsigned = neg_one > const_zero;
+    if (is_unsigned) {
+        if (sizeof(long) < sizeof(long)) {
+            return PyInt_FromLong((long) value);
+        } else if (sizeof(long) <= sizeof(unsigned long)) {
+            return PyLong_FromUnsignedLong((unsigned long) value);
+        } else if (sizeof(long) <= sizeof(unsigned long long)) {
+            return PyLong_FromUnsignedLongLong((unsigned long long) value);
         }
     } else {
-        int32_t val;
-        PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (int32_t) -1;
-        val = __Pyx_PyInt_As_int32_t(tmp);
-        Py_DECREF(tmp);
-        return val;
+        if (sizeof(long) <= sizeof(long)) {
+            return PyInt_FromLong((long) value);
+        } else if (sizeof(long) <= sizeof(long long)) {
+            return PyLong_FromLongLong((long long) value);
+        }
+    }
+    {
+        int one = 1; int little = (int)*(unsigned char *)&one;
+        unsigned char *bytes = (unsigned char *)&value;
+        return _PyLong_FromByteArray(bytes, sizeof(long),
+                                     little, !is_unsigned);
     }
 }
 
+#define __PYX_VERIFY_RETURN_INT(target_type, func_type, func)             \
+    {                                                                     \
+        func_type value = func(x);                                        \
+        if (sizeof(target_type) < sizeof(func_type)) {                    \
+            if (unlikely(value != (func_type) (target_type) value)) {     \
+                func_type zero = 0;                                       \
+                PyErr_SetString(PyExc_OverflowError,                      \
+                    (is_unsigned && unlikely(value < zero)) ?             \
+                    "can't convert negative value to " #target_type :     \
+                    "value too large to convert to " #target_type);       \
+                return (target_type) -1;                                  \
+            }                                                             \
+        }                                                                 \
+        return (target_type) value;                                       \
+    }
+
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
   #include "longintrepr.h"
  #endif
 #endif
-static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
-    const uint16_t neg_one = (uint16_t) -1, const_zero = 0;
+static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {
+    const long neg_one = (long) -1, const_zero = 0;
     const int is_unsigned = neg_one > const_zero;
 #if PY_MAJOR_VERSION < 3
     if (likely(PyInt_Check(x))) {
-        if (sizeof(uint16_t) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(uint16_t, long, PyInt_AS_LONG)
+        if (sizeof(long) < sizeof(long)) {
+            __PYX_VERIFY_RETURN_INT(long, long, PyInt_AS_LONG)
         } else {
             long val = PyInt_AS_LONG(x);
             if (is_unsigned && unlikely(val < 0)) {
                 PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint16_t");
-                return (uint16_t) -1;
+                                "can't convert negative value to long");
+                return (long) -1;
             }
-            return (uint16_t) val;
+            return (long) val;
         }
     } else
 #endif
@@ -43619,40 +2238,40 @@ static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
         if (is_unsigned) {
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint16_t)) {
+            if (sizeof(digit) <= sizeof(long)) {
                 switch (Py_SIZE(x)) {
                     case  0: return 0;
-                    case  1: return (uint16_t) ((PyLongObject*)x)->ob_digit[0];
+                    case  1: return (long) ((PyLongObject*)x)->ob_digit[0];
                 }
             }
  #endif
 #endif
             if (unlikely(Py_SIZE(x) < 0)) {
                 PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to uint16_t");
-                return (uint16_t) -1;
+                                "can't convert negative value to long");
+                return (long) -1;
             }
-            if (sizeof(uint16_t) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(uint16_t, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(uint16_t) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(uint16_t, unsigned long long, PyLong_AsUnsignedLongLong)
+            if (sizeof(long) <= sizeof(unsigned long)) {
+                __PYX_VERIFY_RETURN_INT(long, unsigned long, PyLong_AsUnsignedLong)
+            } else if (sizeof(long) <= sizeof(unsigned long long)) {
+                __PYX_VERIFY_RETURN_INT(long, unsigned long long, PyLong_AsUnsignedLongLong)
             }
         } else {
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(uint16_t)) {
+            if (sizeof(digit) <= sizeof(long)) {
                 switch (Py_SIZE(x)) {
                     case  0: return 0;
-                    case  1: return +(uint16_t) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(uint16_t) ((PyLongObject*)x)->ob_digit[0];
+                    case  1: return +(long) ((PyLongObject*)x)->ob_digit[0];
+                    case -1: return -(long) ((PyLongObject*)x)->ob_digit[0];
                 }
             }
  #endif
 #endif
-            if (sizeof(uint16_t) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(uint16_t, long, PyLong_AsLong)
-            } else if (sizeof(uint16_t) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(uint16_t, long long, PyLong_AsLongLong)
+            if (sizeof(long) <= sizeof(long)) {
+                __PYX_VERIFY_RETURN_INT(long, long, PyLong_AsLong)
+            } else if (sizeof(long) <= sizeof(long long)) {
+                __PYX_VERIFY_RETURN_INT(long, long long, PyLong_AsLongLong)
             }
         }
         {
@@ -43660,7 +2279,7 @@ static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
             PyErr_SetString(PyExc_RuntimeError,
                             "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
 #else
-            uint16_t val;
+            long val;
             PyObject *v = __Pyx_PyNumber_Int(x);
  #if PY_MAJOR_VERSION < 3
             if (likely(v) && !PyLong_Check(v)) {
@@ -43680,13 +2299,13 @@ static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
                     return val;
             }
 #endif
-            return (uint16_t) -1;
+            return (long) -1;
         }
     } else {
-        uint16_t val;
+        long val;
         PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (uint16_t) -1;
-        val = __Pyx_PyInt_As_uint16_t(tmp);
+        if (!tmp) return (long) -1;
+        val = __Pyx_PyInt_As_long(tmp);
         Py_DECREF(tmp);
         return val;
     }
@@ -43697,21 +2316,21 @@ static CYTHON_INLINE uint16_t __Pyx_PyInt_As_uint16_t(PyObject *x) {
   #include "longintrepr.h"
  #endif
 #endif
-static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {
-    const long neg_one = (long) -1, const_zero = 0;
+static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {
+    const int neg_one = (int) -1, const_zero = 0;
     const int is_unsigned = neg_one > const_zero;
 #if PY_MAJOR_VERSION < 3
     if (likely(PyInt_Check(x))) {
-        if (sizeof(long) < sizeof(long)) {
-            __PYX_VERIFY_RETURN_INT(long, long, PyInt_AS_LONG)
+        if (sizeof(int) < sizeof(long)) {
+            __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG)
         } else {
             long val = PyInt_AS_LONG(x);
             if (is_unsigned && unlikely(val < 0)) {
                 PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to long");
-                return (long) -1;
+                                "can't convert negative value to int");
+                return (int) -1;
             }
-            return (long) val;
+            return (int) val;
         }
     } else
 #endif
@@ -43719,40 +2338,40 @@ static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {
         if (is_unsigned) {
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(long)) {
+            if (sizeof(digit) <= sizeof(int)) {
                 switch (Py_SIZE(x)) {
                     case  0: return 0;
-                    case  1: return (long) ((PyLongObject*)x)->ob_digit[0];
+                    case  1: return (int) ((PyLongObject*)x)->ob_digit[0];
                 }
             }
  #endif
 #endif
             if (unlikely(Py_SIZE(x) < 0)) {
                 PyErr_SetString(PyExc_OverflowError,
-                                "can't convert negative value to long");
-                return (long) -1;
+                                "can't convert negative value to int");
+                return (int) -1;
             }
-            if (sizeof(long) <= sizeof(unsigned long)) {
-                __PYX_VERIFY_RETURN_INT(long, unsigned long, PyLong_AsUnsignedLong)
-            } else if (sizeof(long) <= sizeof(unsigned long long)) {
-                __PYX_VERIFY_RETURN_INT(long, unsigned long long, PyLong_AsUnsignedLongLong)
+            if (sizeof(int) <= sizeof(unsigned long)) {
+                __PYX_VERIFY_RETURN_INT(int, unsigned long, PyLong_AsUnsignedLong)
+            } else if (sizeof(int) <= sizeof(unsigned long long)) {
+                __PYX_VERIFY_RETURN_INT(int, unsigned long long, PyLong_AsUnsignedLongLong)
             }
         } else {
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
-            if (sizeof(digit) <= sizeof(long)) {
+            if (sizeof(digit) <= sizeof(int)) {
                 switch (Py_SIZE(x)) {
                     case  0: return 0;
-                    case  1: return +(long) ((PyLongObject*)x)->ob_digit[0];
-                    case -1: return -(long) ((PyLongObject*)x)->ob_digit[0];
+                    case  1: return +(int) ((PyLongObject*)x)->ob_digit[0];
+                    case -1: return -(int) ((PyLongObject*)x)->ob_digit[0];
                 }
             }
  #endif
 #endif
-            if (sizeof(long) <= sizeof(long)) {
-                __PYX_VERIFY_RETURN_INT(long, long, PyLong_AsLong)
-            } else if (sizeof(long) <= sizeof(long long)) {
-                __PYX_VERIFY_RETURN_INT(long, long long, PyLong_AsLongLong)
+            if (sizeof(int) <= sizeof(long)) {
+                __PYX_VERIFY_RETURN_INT(int, long, PyLong_AsLong)
+            } else if (sizeof(int) <= sizeof(long long)) {
+                __PYX_VERIFY_RETURN_INT(int, long long, PyLong_AsLongLong)
             }
         }
         {
@@ -43760,7 +2379,7 @@ static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {
             PyErr_SetString(PyExc_RuntimeError,
                             "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
 #else
-            long val;
+            int val;
             PyObject *v = __Pyx_PyNumber_Int(x);
  #if PY_MAJOR_VERSION < 3
             if (likely(v) && !PyLong_Check(v)) {
@@ -43780,553 +2399,18 @@ static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {
                     return val;
             }
 #endif
-            return (long) -1;
+            return (int) -1;
         }
     } else {
-        long val;
+        int val;
         PyObject *tmp = __Pyx_PyNumber_Int(x);
-        if (!tmp) return (long) -1;
-        val = __Pyx_PyInt_As_long(tmp);
+        if (!tmp) return (int) -1;
+        val = __Pyx_PyInt_As_int(tmp);
         Py_DECREF(tmp);
         return val;
     }
 }
 
-static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, PyObject **tb) {
-    PyObject *tmp_type, *tmp_value, *tmp_tb;
-#if CYTHON_COMPILING_IN_CPYTHON
-    PyThreadState *tstate = PyThreadState_GET();
-    tmp_type = tstate->exc_type;
-    tmp_value = tstate->exc_value;
-    tmp_tb = tstate->exc_traceback;
-    tstate->exc_type = *type;
-    tstate->exc_value = *value;
-    tstate->exc_traceback = *tb;
-#else
-    PyErr_GetExcInfo(&tmp_type, &tmp_value, &tmp_tb);
-    PyErr_SetExcInfo(*type, *value, *tb);
-#endif
-    *type = tmp_type;
-    *value = tmp_value;
-    *tb = tmp_tb;
-}
-
-static PyObject *__Pyx_Generator_Next(PyObject *self);
-static PyObject *__Pyx_Generator_Send(PyObject *self, PyObject *value);
-static PyObject *__Pyx_Generator_Close(PyObject *self);
-static PyObject *__Pyx_Generator_Throw(PyObject *gen, PyObject *args);
-static PyTypeObject *__pyx_GeneratorType = 0;
-#define __Pyx_Generator_CheckExact(obj) (Py_TYPE(obj) == __pyx_GeneratorType)
-#define __Pyx_Generator_Undelegate(gen) Py_CLEAR((gen)->yieldfrom)
-#if 1 || PY_VERSION_HEX < 0x030300B0
-static int __Pyx_PyGen_FetchStopIterationValue(PyObject **pvalue) {
-    PyObject *et, *ev, *tb;
-    PyObject *value = NULL;
-    __Pyx_ErrFetch(&et, &ev, &tb);
-    if (!et) {
-        Py_XDECREF(tb);
-        Py_XDECREF(ev);
-        Py_INCREF(Py_None);
-        *pvalue = Py_None;
-        return 0;
-    }
-    if (unlikely(et != PyExc_StopIteration) &&
-            unlikely(!PyErr_GivenExceptionMatches(et, PyExc_StopIteration))) {
-        __Pyx_ErrRestore(et, ev, tb);
-        return -1;
-    }
-    if (likely(et == PyExc_StopIteration)) {
-        if (likely(!ev) || !PyObject_IsInstance(ev, PyExc_StopIteration)) {
-            if (!ev) {
-                Py_INCREF(Py_None);
-                ev = Py_None;
-            }
-            Py_XDECREF(tb);
-            Py_DECREF(et);
-            *pvalue = ev;
-            return 0;
-        }
-    }
-    PyErr_NormalizeException(&et, &ev, &tb);
-    if (unlikely(!PyObject_IsInstance(ev, PyExc_StopIteration))) {
-        __Pyx_ErrRestore(et, ev, tb);
-        return -1;
-    }
-    Py_XDECREF(tb);
-    Py_DECREF(et);
-#if PY_VERSION_HEX >= 0x030300A0
-    value = ((PyStopIterationObject *)ev)->value;
-    Py_INCREF(value);
-    Py_DECREF(ev);
-#else
-    {
-        PyObject* args = PyObject_GetAttr(ev, __pyx_n_s_args);
-        Py_DECREF(ev);
-        if (likely(args)) {
-            value = PyObject_GetItem(args, 0);
-            Py_DECREF(args);
-        }
-        if (unlikely(!value)) {
-            __Pyx_ErrRestore(NULL, NULL, NULL);
-            Py_INCREF(Py_None);
-            value = Py_None;
-        }
-    }
-#endif
-    *pvalue = value;
-    return 0;
-}
-#endif
-static CYTHON_INLINE
-void __Pyx_Generator_ExceptionClear(__pyx_GeneratorObject *self) {
-    PyObject *exc_type = self->exc_type;
-    PyObject *exc_value = self->exc_value;
-    PyObject *exc_traceback = self->exc_traceback;
-    self->exc_type = NULL;
-    self->exc_value = NULL;
-    self->exc_traceback = NULL;
-    Py_XDECREF(exc_type);
-    Py_XDECREF(exc_value);
-    Py_XDECREF(exc_traceback);
-}
-static CYTHON_INLINE
-int __Pyx_Generator_CheckRunning(__pyx_GeneratorObject *gen) {
-    if (unlikely(gen->is_running)) {
-        PyErr_SetString(PyExc_ValueError,
-                        "generator already executing");
-        return 1;
-    }
-    return 0;
-}
-static CYTHON_INLINE
-PyObject *__Pyx_Generator_SendEx(__pyx_GeneratorObject *self, PyObject *value) {
-    PyObject *retval;
-    assert(!self->is_running);
-    if (unlikely(self->resume_label == 0)) {
-        if (unlikely(value && value != Py_None)) {
-            PyErr_SetString(PyExc_TypeError,
-                            "can't send non-None value to a "
-                            "just-started generator");
-            return NULL;
-        }
-    }
-    if (unlikely(self->resume_label == -1)) {
-        PyErr_SetNone(PyExc_StopIteration);
-        return NULL;
-    }
-    if (value) {
-#if CYTHON_COMPILING_IN_PYPY
-#else
-        /* Generators always return to their most recent caller, not
-         * necessarily their creator. */
-        if (self->exc_traceback) {
-            PyThreadState *tstate = PyThreadState_GET();
-            PyTracebackObject *tb = (PyTracebackObject *) self->exc_traceback;
-            PyFrameObject *f = tb->tb_frame;
-            Py_XINCREF(tstate->frame);
-            assert(f->f_back == NULL);
-            f->f_back = tstate->frame;
-        }
-#endif
-        __Pyx_ExceptionSwap(&self->exc_type, &self->exc_value,
-                            &self->exc_traceback);
-    } else {
-        __Pyx_Generator_ExceptionClear(self);
-    }
-    self->is_running = 1;
-    retval = self->body((PyObject *) self, value);
-    self->is_running = 0;
-    if (retval) {
-        __Pyx_ExceptionSwap(&self->exc_type, &self->exc_value,
-                            &self->exc_traceback);
-#if CYTHON_COMPILING_IN_PYPY
-#else
-        /* Don't keep the reference to f_back any longer than necessary.  It
-         * may keep a chain of frames alive or it could create a reference
-         * cycle. */
-        if (self->exc_traceback) {
-            PyTracebackObject *tb = (PyTracebackObject *) self->exc_traceback;
-            PyFrameObject *f = tb->tb_frame;
-            Py_CLEAR(f->f_back);
-        }
-#endif
-    } else {
-        __Pyx_Generator_ExceptionClear(self);
-    }
-    return retval;
-}
-static CYTHON_INLINE
-PyObject *__Pyx_Generator_FinishDelegation(__pyx_GeneratorObject *gen) {
-    PyObject *ret;
-    PyObject *val = NULL;
-    __Pyx_Generator_Undelegate(gen);
-    __Pyx_PyGen_FetchStopIterationValue(&val);
-    ret = __Pyx_Generator_SendEx(gen, val);
-    Py_XDECREF(val);
-    return ret;
-}
-static PyObject *__Pyx_Generator_Next(PyObject *self) {
-    __pyx_GeneratorObject *gen = (__pyx_GeneratorObject*) self;
-    PyObject *yf = gen->yieldfrom;
-    if (unlikely(__Pyx_Generator_CheckRunning(gen)))
-        return NULL;
-    if (yf) {
-        PyObject *ret;
-        gen->is_running = 1;
-        ret = Py_TYPE(yf)->tp_iternext(yf);
-        gen->is_running = 0;
-        if (likely(ret)) {
-            return ret;
-        }
-        return __Pyx_Generator_FinishDelegation(gen);
-    }
-    return __Pyx_Generator_SendEx(gen, Py_None);
-}
-static PyObject *__Pyx_Generator_Send(PyObject *self, PyObject *value) {
-    __pyx_GeneratorObject *gen = (__pyx_GeneratorObject*) self;
-    PyObject *yf = gen->yieldfrom;
-    if (unlikely(__Pyx_Generator_CheckRunning(gen)))
-        return NULL;
-    if (yf) {
-        PyObject *ret;
-        gen->is_running = 1;
-        if (__Pyx_Generator_CheckExact(yf)) {
-            ret = __Pyx_Generator_Send(yf, value);
-        } else {
-            if (value == Py_None)
-                ret = PyIter_Next(yf);
-            else
-                ret = __Pyx_PyObject_CallMethod1(yf, __pyx_n_s_send, value);
-        }
-        gen->is_running = 0;
-        if (likely(ret)) {
-            return ret;
-        }
-        return __Pyx_Generator_FinishDelegation(gen);
-    }
-    return __Pyx_Generator_SendEx(gen, value);
-}
-static int __Pyx_Generator_CloseIter(__pyx_GeneratorObject *gen, PyObject *yf) {
-    PyObject *retval = NULL;
-    int err = 0;
-    if (__Pyx_Generator_CheckExact(yf)) {
-        retval = __Pyx_Generator_Close(yf);
-        if (!retval)
-            return -1;
-    } else {
-        PyObject *meth;
-        gen->is_running = 1;
-        meth = PyObject_GetAttr(yf, __pyx_n_s_close);
-        if (unlikely(!meth)) {
-            if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
-                PyErr_WriteUnraisable(yf);
-            }
-            PyErr_Clear();
-        } else {
-            retval = PyObject_CallFunction(meth, NULL);
-            Py_DECREF(meth);
-            if (!retval)
-                err = -1;
-        }
-        gen->is_running = 0;
-    }
-    Py_XDECREF(retval);
-    return err;
-}
-static PyObject *__Pyx_Generator_Close(PyObject *self) {
-    __pyx_GeneratorObject *gen = (__pyx_GeneratorObject *) self;
-    PyObject *retval, *raised_exception;
-    PyObject *yf = gen->yieldfrom;
-    int err = 0;
-    if (unlikely(__Pyx_Generator_CheckRunning(gen)))
-        return NULL;
-    if (yf) {
-        Py_INCREF(yf);
-        err = __Pyx_Generator_CloseIter(gen, yf);
-        __Pyx_Generator_Undelegate(gen);
-        Py_DECREF(yf);
-    }
-    if (err == 0)
-#if PY_VERSION_HEX < 0x02050000
-        PyErr_SetNone(PyExc_StopIteration);
-#else
-        PyErr_SetNone(PyExc_GeneratorExit);
-#endif
-    retval = __Pyx_Generator_SendEx(gen, NULL);
-    if (retval) {
-        Py_DECREF(retval);
-        PyErr_SetString(PyExc_RuntimeError,
-                        "generator ignored GeneratorExit");
-        return NULL;
-    }
-    raised_exception = PyErr_Occurred();
-    if (!raised_exception
-        || raised_exception == PyExc_StopIteration
-#if PY_VERSION_HEX >= 0x02050000
-        || raised_exception == PyExc_GeneratorExit
-        || PyErr_GivenExceptionMatches(raised_exception, PyExc_GeneratorExit)
-#endif
-        || PyErr_GivenExceptionMatches(raised_exception, PyExc_StopIteration))
-    {
-        if (raised_exception) PyErr_Clear();      /* ignore these errors */
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-    return NULL;
-}
-static PyObject *__Pyx_Generator_Throw(PyObject *self, PyObject *args) {
-    __pyx_GeneratorObject *gen = (__pyx_GeneratorObject *) self;
-    PyObject *typ;
-    PyObject *tb = NULL;
-    PyObject *val = NULL;
-    PyObject *yf = gen->yieldfrom;
-    if (!PyArg_UnpackTuple(args, (char *)"throw", 1, 3, &typ, &val, &tb))
-        return NULL;
-    if (unlikely(__Pyx_Generator_CheckRunning(gen)))
-        return NULL;
-    if (yf) {
-        PyObject *ret;
-        Py_INCREF(yf);
-#if PY_VERSION_HEX >= 0x02050000
-        if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) {
-            int err = __Pyx_Generator_CloseIter(gen, yf);
-            Py_DECREF(yf);
-            __Pyx_Generator_Undelegate(gen);
-            if (err < 0)
-                return __Pyx_Generator_SendEx(gen, NULL);
-            goto throw_here;
-        }
-#endif
-        gen->is_running = 1;
-        if (__Pyx_Generator_CheckExact(yf)) {
-            ret = __Pyx_Generator_Throw(yf, args);
-        } else {
-            PyObject *meth = PyObject_GetAttr(yf, __pyx_n_s_throw);
-            if (unlikely(!meth)) {
-                Py_DECREF(yf);
-                if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
-                    gen->is_running = 0;
-                    return NULL;
-                }
-                PyErr_Clear();
-                __Pyx_Generator_Undelegate(gen);
-                gen->is_running = 0;
-                goto throw_here;
-            }
-            ret = PyObject_CallObject(meth, args);
-            Py_DECREF(meth);
-        }
-        gen->is_running = 0;
-        Py_DECREF(yf);
-        if (!ret) {
-            ret = __Pyx_Generator_FinishDelegation(gen);
-        }
-        return ret;
-    }
-throw_here:
-    __Pyx_Raise(typ, val, tb, NULL);
-    return __Pyx_Generator_SendEx(gen, NULL);
-}
-static int __Pyx_Generator_traverse(PyObject *self, visitproc visit, void *arg) {
-    __pyx_GeneratorObject *gen = (__pyx_GeneratorObject *) self;
-    Py_VISIT(gen->closure);
-    Py_VISIT(gen->classobj);
-    Py_VISIT(gen->yieldfrom);
-    Py_VISIT(gen->exc_type);
-    Py_VISIT(gen->exc_value);
-    Py_VISIT(gen->exc_traceback);
-    return 0;
-}
-static int __Pyx_Generator_clear(PyObject *self) {
-    __pyx_GeneratorObject *gen = (__pyx_GeneratorObject *) self;
-    Py_CLEAR(gen->closure);
-    Py_CLEAR(gen->classobj);
-    Py_CLEAR(gen->yieldfrom);
-    Py_CLEAR(gen->exc_type);
-    Py_CLEAR(gen->exc_value);
-    Py_CLEAR(gen->exc_traceback);
-    return 0;
-}
-static void __Pyx_Generator_dealloc(PyObject *self) {
-    __pyx_GeneratorObject *gen = (__pyx_GeneratorObject *) self;
-    PyObject_GC_UnTrack(gen);
-    if (gen->gi_weakreflist != NULL)
-        PyObject_ClearWeakRefs(self);
-    if (gen->resume_label > 0) {
-        PyObject_GC_Track(self);
-#if PY_VERSION_HEX >= 0x030400a1
-        if (PyObject_CallFinalizerFromDealloc(self))
-#else
-        Py_TYPE(gen)->tp_del(self);
-        if (self->ob_refcnt > 0)
-#endif
-            return;                     /* resurrected.  :( */
-        PyObject_GC_UnTrack(self);
-    }
-    __Pyx_Generator_clear(self);
-    PyObject_GC_Del(gen);
-}
-static void __Pyx_Generator_del(PyObject *self) {
-    PyObject *res;
-    PyObject *error_type, *error_value, *error_traceback;
-    __pyx_GeneratorObject *gen = (__pyx_GeneratorObject *) self;
-    if (gen->resume_label <= 0)
-        return ;
-#if PY_VERSION_HEX < 0x030400a1
-    assert(self->ob_refcnt == 0);
-    self->ob_refcnt = 1;
-#endif
-    __Pyx_ErrFetch(&error_type, &error_value, &error_traceback);
-    res = __Pyx_Generator_Close(self);
-    if (res == NULL)
-        PyErr_WriteUnraisable(self);
-    else
-        Py_DECREF(res);
-    __Pyx_ErrRestore(error_type, error_value, error_traceback);
-#if PY_VERSION_HEX < 0x030400a1
-    /* Undo the temporary resurrection; can't use DECREF here, it would
-     * cause a recursive call.
-     */
-    assert(self->ob_refcnt > 0);
-    if (--self->ob_refcnt == 0)
-        return; /* this is the normal path out */
-    /* close() resurrected it!  Make it look like the original Py_DECREF
-     * never happened.
-     */
-    {
-        Py_ssize_t refcnt = self->ob_refcnt;
-        _Py_NewReference(self);
-        self->ob_refcnt = refcnt;
-    }
-#if CYTHON_COMPILING_IN_CPYTHON
-    assert(PyType_IS_GC(self->ob_type) &&
-           _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
-    /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
-     * we need to undo that. */
-    _Py_DEC_REFTOTAL;
-#endif
-    /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
-     * chain, so no more to do there.
-     * If COUNT_ALLOCS, the original decref bumped tp_frees, and
-     * _Py_NewReference bumped tp_allocs:  both of those need to be
-     * undone.
-     */
-#ifdef COUNT_ALLOCS
-    --Py_TYPE(self)->tp_frees;
-    --Py_TYPE(self)->tp_allocs;
-#endif
-#endif
-}
-static PyMemberDef __pyx_Generator_memberlist[] = {
-    {(char *) "gi_running",
-#if PY_VERSION_HEX >= 0x02060000
-     T_BOOL,
-#else
-     T_BYTE,
-#endif
-     offsetof(__pyx_GeneratorObject, is_running),
-     READONLY,
-     NULL},
-    {0, 0, 0, 0, 0}
-};
-static PyMethodDef __pyx_Generator_methods[] = {
-    {__Pyx_NAMESTR("send"), (PyCFunction) __Pyx_Generator_Send, METH_O, 0},
-    {__Pyx_NAMESTR("throw"), (PyCFunction) __Pyx_Generator_Throw, METH_VARARGS, 0},
-    {__Pyx_NAMESTR("close"), (PyCFunction) __Pyx_Generator_Close, METH_NOARGS, 0},
-    {0, 0, 0, 0}
-};
-static PyTypeObject __pyx_GeneratorType_type = {
-    PyVarObject_HEAD_INIT(0, 0)
-    __Pyx_NAMESTR("generator"),         /*tp_name*/
-    sizeof(__pyx_GeneratorObject),      /*tp_basicsize*/
-    0,                                  /*tp_itemsize*/
-    (destructor) __Pyx_Generator_dealloc,/*tp_dealloc*/
-    0,                                  /*tp_print*/
-    0,                                  /*tp_getattr*/
-    0,                                  /*tp_setattr*/
-#if PY_MAJOR_VERSION < 3
-    0,                                  /*tp_compare*/
-#else
-    0,                                  /*reserved*/
-#endif
-    0,                                   /*tp_repr*/
-    0,                                  /*tp_as_number*/
-    0,                                  /*tp_as_sequence*/
-    0,                                  /*tp_as_mapping*/
-    0,                                  /*tp_hash*/
-    0,                                  /*tp_call*/
-    0,                                  /*tp_str*/
-    0,                                  /*tp_getattro*/
-    0,                                  /*tp_setattro*/
-    0,                                  /*tp_as_buffer*/
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags*/
-    0,                                  /*tp_doc*/
-    (traverseproc) __Pyx_Generator_traverse,   /*tp_traverse*/
-    0,                                  /*tp_clear*/
-    0,                                  /*tp_richcompare*/
-    offsetof(__pyx_GeneratorObject, gi_weakreflist), /* tp_weaklistoffse */
-    0,                                  /*tp_iter*/
-    (iternextfunc) __Pyx_Generator_Next, /*tp_iternext*/
-    __pyx_Generator_methods,            /*tp_methods*/
-    __pyx_Generator_memberlist,         /*tp_members*/
-    0,                                  /*tp_getset*/
-    0,                                  /*tp_base*/
-    0,                                  /*tp_dict*/
-    0,                                  /*tp_descr_get*/
-    0,                                  /*tp_descr_set*/
-    0,                                  /*tp_dictoffset*/
-    0,                                  /*tp_init*/
-    0,                                  /*tp_alloc*/
-    0,                                  /*tp_new*/
-    0,                                  /*tp_free*/
-    0,                                  /*tp_is_gc*/
-    0,                                  /*tp_bases*/
-    0,                                  /*tp_mro*/
-    0,                                  /*tp_cache*/
-    0,                                  /*tp_subclasses*/
-    0,                                  /*tp_weaklist*/
-#if PY_VERSION_HEX >= 0x030400a1
-    0,                                  /*tp_del*/
-#else
-    __Pyx_Generator_del,                /*tp_del*/
-#endif
-#if PY_VERSION_HEX >= 0x02060000
-    0,                                  /*tp_version_tag*/
-#endif
-#if PY_VERSION_HEX >= 0x030400a1
-    __Pyx_Generator_del,                /*tp_finalize*/
-#endif
-};
-static __pyx_GeneratorObject *__Pyx_Generator_New(__pyx_generator_body_t body,
-                                                  PyObject *closure) {
-    __pyx_GeneratorObject *gen =
-        PyObject_GC_New(__pyx_GeneratorObject, &__pyx_GeneratorType_type);
-    if (gen == NULL)
-        return NULL;
-    gen->body = body;
-    gen->closure = closure;
-    Py_XINCREF(closure);
-    gen->is_running = 0;
-    gen->resume_label = 0;
-    gen->classobj = NULL;
-    gen->yieldfrom = NULL;
-    gen->exc_type = NULL;
-    gen->exc_value = NULL;
-    gen->exc_traceback = NULL;
-    gen->gi_weakreflist = NULL;
-    PyObject_GC_Track(gen);
-    return gen;
-}
-static int __pyx_Generator_init(void) {
-    __pyx_GeneratorType_type.tp_getattro = PyObject_GenericGetAttr;
-    __pyx_GeneratorType_type.tp_iter = PyObject_SelfIter;
-    __pyx_GeneratorType = __Pyx_FetchCommonType(&__pyx_GeneratorType_type);
-    if (__pyx_GeneratorType == NULL) {
-        return -1;
-    }
-    return 0;
-}
-
 static int __Pyx_check_binary_version(void) {
     char ctversion[4], rtversion[4];
     PyOS_snprintf(ctversion, 4, "%d.%d", PY_MAJOR_VERSION, PY_MINOR_VERSION);
diff --git a/pysam/csamfile.pxd b/pysam/csamfile.pxd
index 28cabd1..f027b29 100644
--- a/pysam/csamfile.pxd
+++ b/pysam/csamfile.pxd
@@ -1,22 +1,25 @@
-from libc.stdint cimport int8_t, int16_t, int32_t, int64_t
-from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
-from libc.stdlib cimport malloc, calloc, realloc, free
-from libc.string cimport memcpy, memcmp, strncpy, strlen, strdup
-from libc.stdio cimport FILE, printf
+from calignmentfile cimport AlignedSegment, AlignmentFile
 
-from cfaidx cimport faidx_t, Fastafile
+#################################################
+# Compatibility Layer for pysam < 0.8
+
+# import all declarations from htslib
 from chtslib cimport *
 
-cdef extern from *:
-    ctypedef char* const_char_ptr "const char*"
+cdef class AlignedRead(AlignedSegment):
+    pass
 
+cdef class Samfile(AlignmentFile):
+    pass
+
+# import the conversion functions
 cdef extern from "htslib_util.h":
 
     # add *nbytes* into the variable length data of *src* at *pos*
-    bam1_t * pysam_bam_update( bam1_t * b,
-                               size_t nbytes_old,
-                               size_t nbytes_new,
-                               uint8_t * pos )
+    bam1_t * pysam_bam_update(bam1_t * b,
+                              size_t nbytes_old,
+                              size_t nbytes_new,
+                              uint8_t * pos)
 
     # now: static
     int aux_type2size(int)
@@ -37,164 +40,6 @@ cdef extern from "htslib_util.h":
     void pysam_set_bin(bam1_t * b, uint16_t v)
     void pysam_set_qual(bam1_t * b, uint8_t v)
     void pysam_set_l_qname(bam1_t * b, uint8_t v)
-    void pysam_set_flag(bam1_t * b, uint8_t v)
+    void pysam_set_flag(bam1_t * b, uint16_t v)
     void pysam_set_n_cigar(bam1_t * b, uint16_t v)
     void pysam_update_flag(bam1_t * b, uint16_t v, uint16_t flag)
-
-
-cdef extern from "samfile_util.h":
-
-    int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
-    int bam_prob_realn(bam1_t *b, const char *ref)
-
-
-####################################################################
-# Utility types
-
-ctypedef struct __iterdata:
-    htsFile * htsfile
-    bam_hdr_t * header
-    hts_itr_t * iter
-    faidx_t * fastafile
-    int tid
-    char * seq
-    int seq_len
-
-# Exposing pysam extension classes
-#
-# Note: need to declare all C fields and methods here
-cdef class AlignedRead:
-
-    # object that this AlignedRead represents
-    cdef bam1_t * _delegate
-
-    # add an alignment tag with value to the AlignedRead
-    # an existing tag of the same name will be replaced.
-    cpdef setTag( self, tag, value, value_type = ?, replace = ? )
-
-cdef class Samfile:
-
-    cdef object _filename
-
-    # pointer to htsFile structure
-    cdef htsFile * htsfile
-
-    # pointer to compressed file
-    cdef BGZF * fp
-
-    # pointer to index
-    cdef hts_idx_t *index
-    # header structure
-    cdef bam_hdr_t * header
-    # true if file is a bam file
-    cdef int isbam
-    # true if not a file but a stream
-    cdef int isstream
-    # true if file is not on the local filesystem
-    cdef int isremote
-    # current read within iteration
-    cdef bam1_t * b
-    # file opening mode
-    cdef char * mode
-
-    # beginning of read section
-    cdef int64_t start_offset
-
-    cdef bam_hdr_t * _buildHeader(self, new_header)
-    cdef bam1_t * getCurrent(self)
-    cdef int cnext(self)
-
-    # write an aligned read
-    cpdef int write(self, AlignedRead read)
-
-    cdef char * _getrname(self, int tid)
-
-cdef class PileupProxy:
-    cdef bam_pileup1_t ** plp
-    cdef int tid
-    cdef int pos
-    cdef int n_pu
-
-cdef class PileupRead:
-    cdef AlignedRead _alignment
-    cdef int32_t  _qpos
-    cdef int _indel
-    cdef int _level
-    cdef uint32_t _is_del
-    cdef uint32_t _is_head
-    cdef uint32_t _is_tail
-
-cdef class IteratorRow:
-    cdef int retval
-    cdef bam1_t * b
-    cdef Samfile samfile
-    cdef htsFile * htsfile
-    cdef int owns_samfile
-
-cdef class IteratorRowRegion(IteratorRow):
-    cdef hts_itr_t * iter
-    cdef bam1_t * getCurrent( self )
-    cdef int cnext(self)
-
-cdef class IteratorRowHead(IteratorRow):
-    cdef int max_rows
-    cdef int current_row
-    cdef bam1_t * getCurrent(self)
-    cdef int cnext(self)
-
-cdef class IteratorRowAll(IteratorRow):
-    cdef bam1_t * getCurrent( self )
-    cdef int cnext(self)
-
-cdef class IteratorRowAllRefs(IteratorRow):
-    cdef int         tid
-    cdef IteratorRowRegion rowiter
-
-cdef class IteratorRowSelection(IteratorRow):
-    cdef int current_pos
-    cdef positions
-    cdef bam1_t * getCurrent( self )
-    cdef int cnext(self)
-    cdef BGZF * fp
-
-cdef class IteratorColumn:
-
-    # result of the last plbuf_push
-    cdef IteratorRowRegion iter
-    cdef int tid
-    cdef int pos
-    cdef int n_plp
-    cdef int mask
-    cdef bam_pileup1_t * plp
-    cdef bam_plp_t pileup_iter
-    cdef __iterdata iterdata
-    cdef Samfile samfile
-    cdef Fastafile fastafile
-    cdef stepper
-    cdef int max_depth
-
-    cdef int cnext(self)
-    cdef char * getSequence( self )
-    cdef setMask(self, mask)
-    cdef setupIteratorData(self,
-                           int tid,
-                           int start,
-                           int end,
-                           int reopen = ?)
-
-    cdef reset(self, tid, start, end)
-
-cdef class IteratorColumnRegion(IteratorColumn):
-    cdef int start
-    cdef int end
-    cdef int truncate
-
-cdef class IteratorColumnAllRefs(IteratorColumn):
-    pass
-
-cdef class IndexedReads:
-    cdef Samfile samfile
-    cdef htsFile * htsfile
-    cdef index
-    cdef int owns_samfile
-    cdef BGZF * fp
diff --git a/pysam/csamfile.pyx b/pysam/csamfile.pyx
index e91f7b8..b76ce89 100644
--- a/pysam/csamfile.pyx
+++ b/pysam/csamfile.pyx
@@ -19,3446 +19,25 @@ from cpython cimport PyErr_SetString, \
 
 from cpython.version cimport PY_MAJOR_VERSION
 
-########################################################################
-########################################################################
-########################################################################
-## Python 3 compatibility functions
-########################################################################
-IS_PYTHON3 = PY_MAJOR_VERSION >= 3
-cdef from_string_and_size(char* s, size_t length):
-    if PY_MAJOR_VERSION < 3:
-        return s[:length]
-    else:
-        return s[:length].decode("ascii")
+from calignmentfile cimport AlignmentFile, AlignedSegment
 
-# filename encoding (copied from lxml.etree.pyx)
-cdef str _FILENAME_ENCODING
-_FILENAME_ENCODING = sys.getfilesystemencoding()
-if _FILENAME_ENCODING is None:
-    _FILENAME_ENCODING = sys.getdefaultencoding()
-if _FILENAME_ENCODING is None:
-    _FILENAME_ENCODING = 'ascii'
 
-#cdef char* _C_FILENAME_ENCODING
-#_C_FILENAME_ENCODING = <char*>_FILENAME_ENCODING
+cdef class Samfile(AlignmentFile):
+    '''Deprecated alternative for :class:`~pysam.AlignmentFile`
 
-cdef bytes _encodeFilename(object filename):
-    """Make sure a filename is 8-bit encoded (or None)."""
-    if filename is None:
-        return None
-    elif PyBytes_Check(filename):
-        return filename
-    elif PyUnicode_Check(filename):
-        return filename.encode(_FILENAME_ENCODING)
-    else:
-        raise TypeError, u"Argument must be string or unicode."
-
-cdef bytes _forceBytes(object s):
-    u"""convert string or unicode object to bytes, assuming ascii encoding.
-    """
-    if PY_MAJOR_VERSION < 3:
-        return s
-    elif s is None:
-        return None
-    elif PyBytes_Check(s):
-        return s
-    elif PyUnicode_Check(s):
-        return s.encode('ascii')
-    else:
-        raise TypeError, u"Argument must be string, bytes or unicode."
-
-cdef inline bytes _forceCmdlineBytes(object s):
-    return _forceBytes(s)
-
-cdef _charptr_to_str(char* s):
-    if PY_MAJOR_VERSION < 3:
-        return s
-    else:
-        return s.decode("ascii")
-
-cdef _forceStr(object s):
-    """Return s converted to str type of current Python
-    (bytes in Py2, unicode in Py3)"""
-    if s is None:
-        return None
-    if PY_MAJOR_VERSION < 3:
-        return s
-    elif PyBytes_Check(s):
-        return s.decode('ascii')
-    else:
-        # assume unicode
-        return s
-
-########################################################################
-########################################################################
-########################################################################
-## Constants and global variables
-########################################################################
-
-# defines imported from samtools
-DEF SEEK_SET = 0
-DEF SEEK_CUR = 1
-DEF SEEK_END = 2
-
-cdef char* CODE2CIGAR= "MIDNSHP=X"
-if IS_PYTHON3:
-    CIGAR2CODE = dict( [y,x] for x,y in enumerate( CODE2CIGAR) )
-else:
-    CIGAR2CODE = dict( [ord(y),x] for x,y in enumerate( CODE2CIGAR) )
-CIGAR_REGEX = re.compile( "(\d+)([MIDNSHP=X])" )
-
-#####################################################################
-# hard-coded constants
-cdef int max_pos = 2 << 29
-
-#####################################################################
-#####################################################################
-#####################################################################
-## private factory methods
-#####################################################################
-cdef class AlignedRead
-cdef makeAlignedRead(bam1_t * src):
-    '''enter src into AlignedRead.'''
-    # note that the following does not call __init__
-    cdef AlignedRead dest = AlignedRead.__new__(AlignedRead)
-    dest._delegate = bam_dup1(src)
-    return dest
-
-cdef class PileupProxy
-cdef makePileupProxy(bam_pileup1_t ** plp, int tid, int pos, int n):
-    # note that the following does not call __init__
-     cdef PileupProxy dest = PileupProxy.__new__(PileupProxy)
-     dest.plp = plp
-     dest.tid = tid
-     dest.pos = pos
-     dest.n = n
-     return dest
-
-cdef class PileupRead
-cdef makePileupRead( bam_pileup1_t * src ):
-    '''fill a  PileupRead object from a bam_pileup1_t * object.'''
-    cdef PileupRead dest = PileupRead.__new__(PileupRead)
-    dest._alignment = makeAlignedRead( src.b )
-    dest._qpos = src.qpos
-    dest._indel = src.indel
-    dest._level = src.level
-    dest._is_del = src.is_del
-    dest._is_head = src.is_head
-    dest._is_tail = src.is_tail
-    return dest
-
-cdef convertBinaryTagToList( uint8_t * s ):
-    """return bytesize, number of values list of values in s."""
-    cdef char auxtype
-    cdef uint8_t byte_size
-    cdef int32_t nvalues
-
-    # get byte size
-    auxtype = s[0]
-    byte_size = aux_type2size( auxtype )
-    s += 1
-    # get number of values in array
-    nvalues = (<int32_t*>s)[0]
-    s += 4
-    # get values
-    values = []
-    if auxtype == 'c':
-        for x from 0 <= x < nvalues:
-            values.append((<int8_t*>s)[0])
-            s += 1
-    elif auxtype == 'C':
-        for x from 0 <= x < nvalues:
-            values.append((<uint8_t*>s)[0])
-            s += 1
-    elif auxtype == 's':
-        for x from 0 <= x < nvalues:
-            values.append((<int16_t*>s)[0])
-            s += 2
-    elif auxtype == 'S':
-        for x from 0 <= x < nvalues:
-            values.append((<uint16_t*>s)[0])
-            s += 2
-    elif auxtype == 'i':
-        for x from 0 <= x < nvalues:
-            values.append((<int32_t*>s)[0])
-            s += 4
-    elif auxtype == 'I':
-        for x from 0 <= x < nvalues:
-            values.append((<uint32_t*>s)[0])
-            s += 4
-    elif auxtype == 'f':
-        for x from 0 <= x < nvalues:
-            values.append((<float*>s)[0])
-            s += 4
-
-    return byte_size, nvalues, values
-
-
-class PileupColumn(object):
-    '''A pileup column. A pileup column contains
-    all the reads that map to a certain target base.
-
-    tid
-        chromosome ID as is defined in the header
-    pos
-        the target base coordinate (0-based)
-    n
-        number of reads mapping to this column
-    pileups
-        list of reads (:class:`pysam.PileupRead`) aligned to this column
-    '''
-    def __str__(self):
-        return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
-            "\n" + "\n".join( map(str, self.pileups) )
-
-
-# valid types for sam headers
-VALID_HEADER_TYPES = {"HD" : dict,
-                      "SQ" : list,
-                      "RG" : list,
-                      "PG" : list,
-                      "CO" : list}
-
-# order of records within sam headers
-VALID_HEADERS = ("HD", "SQ", "RG", "PG", "CO")
-
-# type conversions within sam header records
-VALID_HEADER_FIELDS = {"HD" : {"VN" : str, "SO" : str, "GO" : str},
-                       "SQ" : {"SN" : str, "LN" : int, "AS" : str, 
-                               "M5" : str, "SP" : str, "UR" : str,},
-                       "RG" : {"ID" : str, "CN" : str, "DS" : str,
-                               "DT" : str, "FO" : str, "KS" : str,
-                               "LB" : str, "PG" : str, "PI" : str,
-                               "PL" : str, "PU" : str, "SM" : str,},
-                       "PG" : {"ID" : str, "PN" : str, "CL" : str, 
-                               "PP" : str, "DS" : str, "VN" : str,},}
-
-# output order of fields within records
-VALID_HEADER_ORDER = {"HD" : ("VN", "SO", "GO"),
-                      "SQ" : ("SN", "LN", "AS", "M5",
-                               "UR", "SP"),
-                      "RG" : ("ID", "SM", "LB", "DS", 
-                              "PU", "PI", "CN", "DT",
-                              "PL", "FO", "KS", "PG"),
-                      "PG" : ("PN", "ID", "VN", "CL", 
-                              "PP"),}
-
-
-cdef class Samfile:
-    '''*(filename, mode=None, template = None,
-         referencenames = None, referencelengths = None,
-         text = NULL, header = None,
-         add_sq_text = False, check_header = True,
-         check_sq = True )*
-
-    A :term:`SAM`/:term:`BAM` formatted file. The file is
-    automatically opened.
-
-    *mode* should be ``r`` for reading or ``w`` for writing. The
-    default is text mode (:term:`SAM`). For binary (:term:`BAM`) I/O
-    you should append ``b`` for compressed or ``u`` for uncompressed
-    :term:`BAM` output.  Use ``h`` to output header information in
-    text (:term:`TAM`) mode.
-
-    If ``b`` is present, it must immediately follow ``r`` or ``w``.
-    Valid modes are ``r``, ``w``, ``wh``, ``rb``, ``wb`` and
-    ``wbu``. For instance, to open a :term:`BAM` formatted file for
-    reading, type::
-
-        f = pysam.Samfile('ex1.bam','rb')
-
-    If mode is not specified, we will try to auto-detect in the order
-    'rb', 'r', thus both the following should work::
-
-        f1 = pysam.Samfile('ex1.bam' )
-        f2 = pysam.Samfile('ex1.sam' )
-
-    If an index for a BAM file exists (.bai), it will be opened
-    automatically. Without an index random access to reads via
-    :meth:`fetch` and :meth:`pileup` is disabled.
-
-    For writing, the header of a :term:`SAM` file/:term:`BAM` file can
-    be constituted from several sources (see also the samtools format
-    specification):
-
-        1. If *template* is given, the header is copied from a another
-           *Samfile* (*template* must be of type *Samfile*).
-
-        2. If *header* is given, the header is built from a
-           multi-level dictionary. The first level are the four types
-           ('HD', 'SQ', ...). The second level are a list of lines,
-           with each line being a list of tag-value pairs. The header
-           is constructed first from all the defined fields, followed
-           by user tags in alphabetical order.
-
-        3. If *text* is given, new header text is copied from raw
-           text.
-
-        4. The names (*referencenames*) and lengths
-           (*referencelengths*) are supplied directly as lists.  By
-           default, 'SQ' and 'LN' tags will be added to the header
-           text. This option can be changed by unsetting the flag
-           *add_sq_text*.
-
-    By default, if a file is opened in mode 'r', it is checked
-    for a valid header (*check_header* = True) and a definition of
-    chromosome names (*check_sq* = True).
-
-    '''
-
-    def __cinit__(self, *args, **kwargs ):
-        self.htsfile = NULL
-        self._filename = None
-        self.isbam = False
-        self.isstream = False
-        self._open(*args, **kwargs)
-
-        # allocate memory for iterator
-        self.b = <bam1_t*>calloc(1, sizeof(bam1_t))
-
-    def _isOpen( self ):
-        '''return true if htsfile has been opened.'''
-        return self.htsfile != NULL
-
-    def _hasIndex( self ):
-        '''return true if htsfile has an existing (and opened) index.'''
-        return self.index != NULL
-
-    def _open(self,
-              filename,
-              mode=None,
-              Samfile template=None,
-              referencenames=None,
-              referencelengths=None,
-              text=None,
-              header=None,
-              port=None,
-              add_sq_text=True,
-              check_header=True,
-              check_sq=True):
-        '''open a sam/bam file.
-
-        If _open is called on an existing bamfile, the current file will be
-        closed and a new file will be opened.
-        '''
-
-        # read mode autodetection
-        if mode is None:
-            try:
-                self._open(filename, 'rb',
-                           template=template,
-                           referencenames=referencenames,
-                           referencelengths=referencelengths,
-                           text=text,
-                           header=header,
-                           port=port,
-                           check_header=check_header,
-                           check_sq=check_sq)
-                return
-            except ValueError, msg:
-                pass
-
-            self._open(filename, 'r',
-                       template=template,
-                       referencenames=referencenames,
-                       referencelengths=referencelengths,
-                       text=text,
-                       header=header,
-                       port=port,
-                       check_header=check_header,
-                       check_sq=check_sq)
-            return
-
-        assert mode in ( "r","w","rb","wb", "wh", "wbu", "rU" ), \
-            "invalid file opening mode `%s`" % mode
-
-        # close a previously opened file
-        if self.htsfile != NULL:
-            self.close()
-
-        cdef bytes bmode = mode.encode('ascii')
-        self._filename = filename = _encodeFilename(filename)
-        self.isstream = filename == b"-"
-
-        self.isbam = len(mode) > 1 and mode[1] == 'b'
-
-        self.isremote = filename.startswith(b"http:") or \
-                        filename.startswith(b"ftp:")
-
-        cdef char * ctext
-        ctext = NULL
-
-        if mode[0] == 'w':
-            # open file for writing
-
-            # header structure (used for writing)
-            if template:
-                self.header = bam_hdr_dup(template.header)
-            elif header:
-                self.header = self._buildHeader(header)
-            else:
-                # build header from a target names and lengths
-                assert referencenames and referencelengths, \
-                    ("either supply options `template`, `header` "
-                     "or  both `referencenames` and `referencelengths` "
-                     "for writing")
-                assert len(referencenames) == len(referencelengths), \
-                    "unequal names and lengths of reference sequences"
-
-                # allocate and fill header
-                referencenames = [_forceBytes(ref) for ref in referencenames]
-                self.header = bam_hdr_init()
-                self.header.n_targets = len(referencenames)
-                n = 0
-                for x in referencenames:
-                    n += len(x) + 1
-                self.header.target_name = <char**>calloc(n, sizeof(char*))
-                self.header.target_len = <uint32_t*>calloc(n, sizeof(uint32_t))
-                for x from 0 <= x < self.header.n_targets:
-                    self.header.target_len[x] = referencelengths[x]
-                    name = referencenames[x]
-                    self.header.target_name[x] = <char*>calloc(
-                        len(name) + 1, sizeof(char))
-                    strncpy(self.header.target_name[x], name, len(name))
-
-                # Optionally, if there is no text, add a SAM
-                # compatible header to output file.
-                if text is None and add_sq_text:
-                    text = []
-                    for x from 0 <= x < self.header.n_targets:
-                        text.append("@SQ\tSN:%s\tLN:%s\n" % \
-                                    (_forceStr(referencenames[x]), 
-                                     referencelengths[x]))
-                    text = ''.join(text)
-
-                if text != None:
-                    # copy without \0
-                    text = _forceBytes(text)
-                    ctext = text
-                    self.header.l_text = strlen(ctext)
-                    self.header.text = <char*>calloc(
-                        strlen(ctext), sizeof(char))
-                    memcpy(self.header.text, ctext, strlen(ctext))
-
-            # open file. Header gets written to file at the same time for bam files
-            # and sam files (in the latter case, the mode needs to be wh)
-            self.htsfile = hts_open(filename, bmode)
-            
-            # for compatibility - "w" writes sam file without header
-            if self.isbam or "h" in mode:
-                # write header to htsfile
-                sam_hdr_write(self.htsfile, self.header)
-                
-        elif mode[0] == "r":
-            # open file for reading
-            if (filename != b"-"
-                and not self.isremote
-                and not os.path.exists(filename)):
-                raise IOError("file `%s` not found" % filename)
-
-            # try to detect errors
-            self.htsfile = hts_open(filename, bmode)
-            if self.htsfile == NULL:
-                raise ValueError(
-                    "could not open file (mode='%s') - "
-                    "is it SAM/BAM format?" % mode)
-
-            # get file pointer
-            # TODO: this is specific to BAM files
-            #       refactor to make generalizable
-            self.fp = self.htsfile.fp.bgzf
-
-            # bam files require a valid header
-            if self.isbam:
-                self.header = sam_hdr_read(self.htsfile)
-                if self.header == NULL:
-                    raise ValueError(
-                        "file does not have valid header (mode='%s') "
-                        "- is it BAM format?" % mode )
-            else:
-                # in sam files it is optional (htsfile full of unmapped reads)
-                if check_header:
-                    self.header = sam_hdr_read(self.htsfile)
-                    if self.header == NULL:
-                        raise ValueError(
-                            "file does not have valid header (mode='%s') "
-                            "- is it SAM format?" % mode )
-                    # self.header.ignore_sam_err = True
-
-            # disabled for autodetection to work needs to be disabled
-            # so that reading from sam-files without headers works
-            if check_sq and self.header.n_targets == 0:
-                raise ValueError(
-                    ("file header is empty (mode='%s') - "
-                     "is it SAM/BAM format?") % mode)
-
-        if self.htsfile == NULL:
-            raise IOError("could not open file `%s`" % filename )
-
-        # check for index and open if present
-        if mode[0] == "r" and self.isbam:
-
-            if not self.isremote:
-                if not os.path.exists(filename + b".bai") \
-                        and not os.path.exists( filename[:-4] + b".bai"):
-                    self.index = NULL
-                else:
-                    # returns NULL if there is no index or index could not be opened
-                    self.index = hts_idx_load(filename, HTS_FMT_BAI)
-                    if self.index == NULL:
-                        raise IOError("error while opening index `%s` " % filename )
-            else:
-                self.index = hts_idx_load(filename, HTS_FMT_BAI)
-                if self.index == NULL:
-                    warnings.warn("unable to open index for `%s` " % filename)
-
-            if not self.isstream:
-                self.start_offset = bgzf_tell(self.fp)
-
-    def gettid( self, reference ):
-        '''
-        convert :term:`reference` name into numerical :term:`tid`
-
-        returns -1 if reference is not known.
-        '''
-        if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
-        reference = _forceBytes(reference)
-        return bam_name2id(self.header, reference)
-
-    def getrname( self, tid ):
-        '''
-        convert numerical :term:`tid` into :term:`reference` name.'''
-        if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
-        if not 0 <= tid < self.header.n_targets:
-            raise ValueError("tid %i out of range 0<=tid<%i" % 
-                             (tid, self.header.n_targets ) )
-        return _charptr_to_str(self.header.target_name[tid])
-
-    cdef char * _getrname( self, int tid ): # TODO unused
-        '''
-        convert numerical :term:`tid` into :term:`reference` name.'''
-        if not self._isOpen():
-            raise ValueError("I/O operation on closed file")
-
-        if not 0 <= tid < self.header.n_targets:
-            raise ValueError("tid %i out of range 0<=tid<%i" %
-                             (tid, self.header.n_targets ))
-        return self.header.target_name[tid]
-
-    def _parseRegion(self,
-                     reference=None,
-                     start=None,
-                     end=None,
-                     region=None,
-                     tid=None):
-        '''
-        parse region information.
-
-        raise ValueError for for invalid regions.
-
-        returns a tuple of flag, tid, start and end. Flag indicates
-        whether some coordinates were supplied.
-
-        Note that regions are 1-based, while start,end are python coordinates.
-        '''
-        cdef int rtid
-        cdef long long rstart
-        cdef long long rend
-
-        rtid = -1
-        rstart = 0
-        rend = max_pos
-        if start != None:
-            try:
-                rstart = start
-            except OverflowError:
-                raise ValueError( 'start out of range (%i)' % start )
-
-        if end != None:
-            try:
-                rend = end
-            except OverflowError:
-                raise ValueError( 'end out of range (%i)' % end )
-
-        if region:
-            region = _forceStr(region)
-            parts = re.split("[:-]", region)
-            reference = parts[0]
-            if len(parts) >= 2:
-                rstart = int(parts[1]) - 1
-            if len(parts) >= 3:
-                rend = int(parts[2])
-
-        if not reference:
-            return 0, 0, 0, 0
-
-        if tid is not None:
-            rtid = tid
-        else:
-            rtid = self.gettid(reference)
-
-        if rtid < 0:
-            raise ValueError(
-                "invalid reference `%s`" % reference)
-        if rstart > rend:
-            raise ValueError(
-                'invalid coordinates: start (%i) > end (%i)' % (rstart, rend))
-        if not 0 <= rstart < max_pos:
-            raise ValueError('start out of range (%i)' % rstart)
-        if not 0 <= rend <= max_pos:
-            raise ValueError('end out of range (%i)' % rend)
-
-        return 1, rtid, rstart, rend
-
-    def reset(self):
-        '''reset file position to beginning of read section.'''
-        return self.seek(self.start_offset, 0)
-
-    def seek(self, uint64_t offset, int where = 0):
-        '''
-        move file pointer to position *offset*, see :meth:`pysam.Samfile.tell`.
-        '''
-
-        if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
-        if not self.isbam:
-            raise NotImplementedError("seek only available in bam files")
-        if self.isstream:
-            raise OSError("seek no available in streams")
-
-        return bgzf_seek(self.fp, offset, where)
-
-    def tell(self):
-        '''
-        return current file position
-        '''
-        if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
-        if not self.isbam:
-            raise NotImplementedError("seek only available in bam files")
-
-        return bgzf_tell(self.fp)
-
-    def fetch(self,
-              reference=None,
-              start=None,
-              end=None,
-              region=None,
-              tid=None,
-              callback=None,
-              until_eof=False,
-              reopen=True):
-        '''fetch aligned reads in a :term:`region` using 0-based indexing. The
-        region is specified by :term:`reference`, *start* and
-        *end*. Alternatively, a samtools :term:`region` string can be
-        supplied.
-
-        Without *reference* or *region* all mapped reads will be
-        fetched. The reads will be returned ordered by reference
-        sequence, which will not necessarily be the order within the
-        file.
-
-        If *until_eof* is given, all reads from the current file
-        position will be returned in order as they are within the
-        file. Using this option will also fetch unmapped reads.
-
-        If *reopen* is set to true, the iterator returned will receive
-        its own filehandle to the htsfile effectively opening its own
-        copy of the file. The default behaviour is to re-open in order
-        to safely work with multiple concurrent iterators on the same
-        file. Re-opening a htsfile creates some overhead, so when
-        using many calls to fetch() *reopen* can be set to False to
-        gain some speed. Also, the tell() method will only work if
-        *reopen* is set to False.
-
-        If only *reference* is set, all reads aligned to *reference*
-        will be fetched.
-
-        Note that a :term:`SAM` file does not allow random access. If
-        *region* or *reference* are given, an exception is raised.
-
-        '''
-        cdef int rtid, rstart, rend, has_coord
-
-        if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
-
-        has_coord, rtid, rstart, rend = self._parseRegion(reference,
-                                                          start,
-                                                          end,
-                                                          region,
-                                                          tid)
-
-        # Turn of re-opening if htsfile is a stream
-        if self.isstream:
-            reopen = False
-
-        if self.isbam:
-            if not until_eof and not self._hasIndex() and not self.isremote:
-                raise ValueError( "fetch called on bamfile without index" )
-
-            if has_coord:
-                return IteratorRowRegion(self, rtid, rstart, rend, 
-                                         reopen=reopen)
-            else:
-                if until_eof:
-                    return IteratorRowAll(self, reopen=reopen)
-                else:
-                    # AH: check - reason why no reopen for AllRefs?
-                    return IteratorRowAllRefs(self) # , reopen=reopen )
-        else:
-            if has_coord:
-                raise ValueError ("fetching by region is not available for sam files")
-
-            if callback:
-                raise NotImplementedError("callback not implemented yet")
-
-            if self.header == NULL:
-                raise ValueError("fetch called for htsfile without header")
-
-            # check if targets are defined
-            # give warning, sam_read1 segfaults
-            if self.header.n_targets == 0:
-                warnings.warn("fetch called for htsfile without header")
-                
-            return IteratorRowAll(self, reopen=reopen)
-
-    def head(self, n):
-        '''
-        return iterator over the first n alignments. 
-
-        This is useful for inspecting the bam-file.
-        '''
-        return IteratorRowHead(self, n)
-
-    def mate(self,
-             AlignedRead read):
-        '''return the mate of :class:`AlignedRead` *read*.
-
-        Throws a ValueError if read is unpaired or the mate
-        is unmapped.
-
-        .. note::
-
-            Calling this method will change the file position.
-            This might interfere with any iterators that have
-            not re-opened the file.
-
-        .. note::
-  
-           This method is too slow for high-throughput processing.
-           If a read needs to be processed with its mate, work
-           from a read name sorted file or, better, cache reads.
-
-        '''
-        cdef uint32_t flag = read._delegate.core.flag
-
-        if flag & BAM_FPAIRED == 0:
-            raise ValueError("read %s: is unpaired" % (read.qname))
-        if flag & BAM_FMUNMAP != 0:
-            raise ValueError("mate %s: is unmapped" % (read.qname))
-
-        # xor flags to get the other mate
-        cdef int x = BAM_FREAD1 + BAM_FREAD2
-        flag = (flag ^ x) & x
-
-        # the following code is not using the C API and
-        # could thus be made much quicker
-        for mate in self.fetch(
-                read._delegate.core.mpos,
-                read._delegate.core.mpos + 1,
-                tid=read._delegate.core.mtid):
-            if mate.flag & flag != 0 and \
-               mate.qname == read.qname:
-                break
-        else:
-            raise ValueError("mate not found")
-        
-        return mate
-
-    def count(self,
-              reference=None,
-              start=None,
-              end=None,
-              region=None,
-              until_eof=False):
-        '''*(reference = None, start = None, end = None,
-        region = None, callback = None, until_eof = False)*
-
-        count reads :term:`region` using 0-based indexing. The region
-        is specified by :term:`reference`, *start* and
-        *end*. Alternatively, a samtools :term:`region` string can be
-        supplied.
-
-        Note that a :term:`TAM` file does not allow random access. If
-        *region* or *reference* are given, an exception is raised.
-        '''
-        cdef AlignedRead read
-        cdef long counter = 0
-
-        if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
-            
-        for read in self.fetch(reference=reference,
-                               start=start,
-                               end=end,
-                               region=region,
-                               until_eof=until_eof):
-            counter += 1
-
-        return counter
-
-    def pileup( self,
-                reference = None,
-                start = None,
-                end = None,
-                region = None,
-                **kwargs ):
-        '''
-        perform a :term:`pileup` within a :term:`region`. The region is specified by
-        :term:`reference`, *start* and *end* (using 0-based indexing).
-        Alternatively, a samtools *region* string can be supplied.
-
-        Without *reference* or *region* all reads will be used for the pileup. The reads will be returned
-        ordered by :term:`reference` sequence, which will not necessarily be the order within the file.
-
-        The method returns an iterator of type :class:`pysam.IteratorColumn` unless
-        a *callback is provided. If a *callback* is given, the callback will be executed
-        for each column within the :term:`region`.
-
-        Note that :term:`SAM` formatted files do not allow random access.
-        In these files, if a *region* or *reference* are given an exception is raised.
-
-        Optional *kwargs* to the iterator:
-
-        stepper
-           The stepper controlls how the iterator advances.
-           Possible options for the stepper are
-
-           ``all``
-              use all reads for pileup.
-           ``samtools``
-              same filter and read processing as in :term:`csamtools` pileup
-
-        fastafile
-           A :class:`FastaFile` object
-
-         mask
-           Skip all reads with bits set in mask if mask=True.
-
-         max_depth
-           Maximum read depth permitted. The default limit is *8000*.
-
-         truncate
-           By default, the samtools pileup engine outputs all reads overlapping a region (see note below).
-           If truncate is True and a region is given, only output columns in the exact region
-           specificied.
-
-        .. note::
-
-            *all* reads which overlap the region are returned. The first base returned will be the
-            first base of the first read *not* necessarily the first base of the region used in the query.
-
-        '''
-        cdef int rtid, rstart, rend, has_coord
-
-        if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
-
-        has_coord, rtid, rstart, rend = self._parseRegion(
-            reference, start, end, region )
-
-        if self.isbam:
-            if not self._hasIndex():
-                raise ValueError("no index available for pileup")
-
-            if has_coord:
-                return IteratorColumnRegion(self,
-                                            tid = rtid,
-                                            start = rstart,
-                                            end = rend,
-                                            **kwargs )
-            else:
-                return IteratorColumnAllRefs(self, **kwargs )
-
-        else:
-            raise NotImplementedError( "pileup of samfiles not implemented yet" )
-
-    def close( self ):
-        '''
-        closes the :class:`pysam.Samfile`.'''
-        if self.htsfile != NULL:
-            hts_close(self.htsfile)
-            hts_idx_destroy(self.index);
-            self.htsfile = NULL
-
-    def __dealloc__( self ):
-        # remember: dealloc cannot call other methods
-        # note: no doc string
-        # note: __del__ is not called.
-        self.close()
-        bam_destroy1(self.b)
-        if self.header != NULL:
-            bam_hdr_destroy(self.header)
-            
-    cpdef int write( self, AlignedRead read ) except -1:
-        '''
-        write a single :class:`pysam.AlignedRead` to disk.
-
-        returns the number of bytes written.
-        '''
-        if not self._isOpen():
-            return 0
-
-        x = sam_write1(self.htsfile,
-                       self.header,
-                       read._delegate)
-
-        return x
-
-    def __enter__(self):
-        return self
-
-    def __exit__(self, exc_type, exc_value, traceback):
-        self.close()
-        return False
-
-    ###############################################################
-    ###############################################################
-    ###############################################################
-    ## properties
-    ###############################################################
-    property filename:
-        '''number of :term:`filename` associated with this object.'''
-        def __get__(self):
-            return self._filename
-
-    property nreferences:
-        '''number of :term:`reference` sequences in the file.'''
-        def __get__(self):
-            if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
-            return self.header.n_targets
-
-    property references:
-        """tuple with the names of :term:`reference` sequences."""
-        def __get__(self):
-            if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
-            t = []
-            for x from 0 <= x < self.header.n_targets:
-                t.append(_charptr_to_str(self.header.target_name[x]))
-            return tuple(t)
-
-    property lengths:
-        """tuple of the lengths of the :term:`reference` sequences. The lengths are in the same order as
-        :attr:`pysam.Samfile.references`
-        """
-        def __get__(self):
-            if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
-            t = []
-            for x from 0 <= x < self.header.n_targets:
-                t.append( self.header.target_len[x] )
-            return tuple(t)
-
-    property mapped:
-        """total number of mapped alignments in file.
-        """
-        def __get__(self):
-            self._checkIndex()
-            cdef int tid
-            cdef uint64_t total = 0
-            cdef uint64_t mapped, unmapped
-            for tid from 0 <= tid < self.header.n_targets:
-                hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
-                total += mapped
-            return total
-
-    def _checkIndex(self):
-        '''check if index is present. Otherwise raise
-        an error.'''
-        if not self._isOpen():
-            raise ValueError("I/O operation on closed file")
-        if not self.isbam:
-            raise AttributeError("Samfile.mapped only available in bam files")
-        if self.index == NULL:
-            raise ValueError("mapping information not recorded in index "
-                                 "or index not available")
-
-
-    property unmapped:
-        """total number of unmapped reads in file.
-        """
-        def __get__(self):
-            self._checkIndex()
-            cdef int tid
-            cdef uint64_t total = 0
-            cdef uint64_t mapped, unmapped
-            for tid from 0 <= tid < self.header.n_targets:
-                hts_idx_get_stat(self.index, tid, &mapped, &unmapped)
-                total += unmapped
-            return total
-
-    property nocoordinate:
-        """total number of reads without coordinates
-        """
-        def __get__(self):
-            self._checkIndex()
-            return hts_idx_get_n_no_coor(self.index)
-
-    property text:
-        '''full contents of the :term:`sam file` header as a string.'''
-        def __get__(self):
-            if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
-            return from_string_and_size(self.header.text, self.header.l_text)
-
-    property header:
-        '''header information within the :term:`sam file`. The records and fields are returned as
-        a two-level dictionary.
-        '''
-        def __get__(self):
-            if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
-
-            result = {}
-            
-            if self.header.text != NULL:
-                # convert to python string (note: call self.text to create 0-terminated string)
-                t = self.text
-                for line in t.split("\n"):
-                    if not line.strip(): continue
-                    assert line.startswith("@"), "header line without '@': '%s'" % line
-                    fields = line[1:].split("\t")
-                    record = fields[0]
-                    assert record in VALID_HEADER_TYPES, "header line with invalid type '%s': '%s'" % (record, line)
-
-                    # treat comments
-                    if record == "CO":
-                        if record not in result: result[record] = []
-                        result[record].append( "\t".join( fields[1:] ) )
-                        continue
-                    # the following is clumsy as generators do not work?
-                    x = {}
-                    for field in fields[1:]:
-                        if ":" not in field: 
-                            raise ValueError("malformatted header: no ':' in field" )
-                        key, value = field.split(":",1)
-                        # uppercase keys must be valid
-                        # lowercase are permitted for user fields
-                        if key in VALID_HEADER_FIELDS[record]:
-                            x[key] = VALID_HEADER_FIELDS[record][key](value)
-                        elif not key.isupper():
-                            x[key] = value
-                        else:
-                            raise ValueError( "unknown field code '%s' in record '%s'" % (key, record) )
-
-                    if VALID_HEADER_TYPES[record] == dict:
-                        if record in result:
-                            raise ValueError( "multiple '%s' lines are not permitted" % record )
-                        result[record] = x
-                    elif VALID_HEADER_TYPES[record] == list:
-                        if record not in result: result[record] = []
-                        result[record].append( x )
-
-                # if there are no SQ lines in the header, add the reference names
-                # from the information in the bam file.
-                # Background: c-samtools keeps the textual part of the header separate from
-                # the list of reference names and lengths. Thus, if a header contains only 
-                # SQ lines, the SQ information is not part of the textual header and thus
-                # are missing from the output. See issue 84.
-                if "SQ" not in result:
-                    sq = []
-                    for ref, length in zip( self.references, self.lengths ):
-                        sq.append( {'LN': length, 'SN': ref } )
-                    result["SQ"] = sq
-
-            return result
-
-    def _buildLine( self, fields, record ):
-        '''build a header line from *fields* dictionary for *record*'''
-
-        # TODO: add checking for field and sort order
-        line = ["@%s" % record ]
-        # comment
-        if record == "CO":
-            line.append( fields )
-        # user tags
-        elif record.islower():
-            for key in sorted(fields):
-                line.append( "%s:%s" % (key, str(fields[key])))
-        # defined tags
-        else:
-            # write fields of the specification
-            for key in VALID_HEADER_ORDER[record]:
-                if key in fields:
-                    line.append( "%s:%s" % (key, str(fields[key])))
-            # write user fields
-            for key in fields:
-                if not key.isupper():
-                    line.append( "%s:%s" % (key, str(fields[key])))
-
-        return "\t".join( line )
-
-    cdef bam_hdr_t * _buildHeader( self, new_header ):
-        '''return a new header built from a dictionary in *new_header*.
-
-        This method inserts the text field, target_name and target_len.
-        '''
-
-        lines = []
-
-        # check if hash exists
-
-        # create new header and copy old data
-        cdef bam_hdr_t * dest
-
-        dest = bam_hdr_init()
-
-        # first: defined tags
-        for record in VALID_HEADERS:
-            if record in new_header:
-                ttype = VALID_HEADER_TYPES[record]
-                data = new_header[record]
-                if type(data) != type(ttype()):
-                    raise ValueError(
-                        "invalid type for record %s: %s, expected %s" %
-                        (record, type(data), type(ttype())))
-                if type(data) is dict:
-                    lines.append(self._buildLine(data, record))
-                else:
-                    for fields in new_header[record]:
-                        lines.append(self._buildLine(fields, record))
-
-        # then: user tags (lower case), sorted alphabetically
-        for record, data in sorted(new_header.items()):
-            if record in VALID_HEADERS: continue
-            if type( data ) is dict:
-                lines.append( self._buildLine( data, record ) )
-            else:
-                for fields in new_header[record]:
-                    lines.append( self._buildLine( fields, record ) )
-
-        text = "\n".join(lines) + "\n"
-        if dest.text != NULL: free( dest.text )
-        dest.text = <char*>calloc( len(text), sizeof(char))
-        dest.l_text = len(text)
-        cdef bytes btext = text.encode('ascii')
-        strncpy( dest.text, btext, dest.l_text )
-
-        cdef bytes bseqname
-        # collect targets
-        if "SQ" in new_header:
-            seqs = []
-            for fields in new_header["SQ"]:
-                try:
-                    seqs.append( (fields["SN"], fields["LN"] ) )
-                except KeyError:
-                    raise KeyError( "incomplete sequence information in '%s'" % str(fields))
-
-            dest.n_targets = len(seqs)
-            dest.target_name = <char**>calloc( dest.n_targets, sizeof(char*) )
-            dest.target_len = <uint32_t*>calloc( dest.n_targets, sizeof(uint32_t) )
-
-            for x from 0 <= x < dest.n_targets:
-                seqname, seqlen = seqs[x]
-                dest.target_name[x] = <char*>calloc( len( seqname ) + 1, sizeof(char) )
-                bseqname = seqname.encode('ascii')
-                strncpy( dest.target_name[x], bseqname, len(seqname) + 1 )
-                dest.target_len[x] = seqlen
-
-        return dest
-
-    ###############################################################
-    ###############################################################
-    ###############################################################
-    ## file-object like iterator access
-    ## note: concurrent access will cause errors (see IteratorRow
-    ## and reopen)
-    ## Possible solutions: deprecate or open new file handle
-    ###############################################################
-    def __iter__(self):
-        if not self._isOpen():
-            raise ValueError( "I/O operation on closed file" )
-
-        if not self.isbam and self.header.n_targets == 0:
-            raise NotImplementedError(
-                "can not iterate over samfile without header")
-        return self
-
-    cdef bam1_t * getCurrent( self ):
-        return self.b
-
-    cdef int cnext(self):
-        '''
-        cversion of iterator. Used by :class:`pysam.Samfile.IteratorColumn`.
-        '''
-        cdef int ret
-        return sam_read1(self.htsfile,
-                         self.header,
-                         self.b)
-
-    def __next__(self):
-        """
-        python version of next().
-        """
-        cdef int ret
-        ret = sam_read1(self.htsfile, self.header, self.b)
-        if (ret >= 0):
-            return makeAlignedRead(self.b)
-        else:
-            raise StopIteration
-
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-cdef class IteratorRow:
-    '''abstract base class for iterators over mapped reads.
-
-    Various iterators implement different behaviours for wrapping around
-    contig boundaries. Examples include:
-
-    :class:`pysam.IteratorRowRegion`
-        iterate within a single contig and a defined region.
-
-    :class:`pysam.IteratorRowAll`
-        iterate until EOF. This iterator will also include unmapped reads.
-
-    :class:`pysam.IteratorRowAllRefs`
-        iterate over all reads in all reference sequences.
-
-    The method :meth:`Samfile.fetch` returns an IteratorRow.
-
-    .. note::
-        It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
-
-    '''
-
-    def __init__(self, Samfile samfile, int reopen=True):
-        
-        if not samfile._isOpen():
-            raise ValueError( "I/O operation on closed file" )
-
-        # makes sure that samfile stays alive as long as the
-        # iterator is alive
-        self.samfile = samfile
-
-        # reopen the file - note that this makes the iterator
-        # slow and causes pileup to slow down significantly.
-        if reopen:
-            self.htsfile = hts_open(samfile._filename, 'r')
-            assert self.htsfile != NULL
-            # read header - required for accurate positioning
-            sam_hdr_read(self.htsfile)
-            self.owns_samfile = True
-        else:
-            self.htsfile = self.samfile.htsfile
-            self.owns_samfile = False
-
-        self.retval = 0
-
-        self.b = bam_init1()
-
-    def __dealloc__(self):
-        bam_destroy1(self.b)
-        if self.owns_samfile:
-            hts_close(self.htsfile)
-
-cdef class IteratorRowRegion(IteratorRow):
-    """*(Samfile samfile, int tid, int beg, int end, int reopen = True )*
-
-    iterate over mapped reads in a region.
-
-    By default, the file is re-openend to avoid conflicts between
-    multiple iterators working on the same file. Set *reopen* = False
-    to not re-open *samfile*.
-
-    The samtools iterators assume that the file
-    position between iterations do not change.
-    As a consequence, no two iterators can work
-    on the same file. To permit this, each iterator
-    creates its own file handle by re-opening the
-    file.
-
-    Note that the index will be shared between
-    samfile and the iterator.
-
-    .. note::
-        It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
-
-    """
-
-    def __init__(self, Samfile samfile,
-                 int tid, int beg, int end,
-                 int reopen=True):
-
-        IteratorRow.__init__(self, samfile, reopen=reopen)
-
-        if not samfile._hasIndex():
-            raise ValueError( "no index available for iteration" )
-
-        self.iter = sam_itr_queryi(
-            self.samfile.index,
-            tid,
-            beg,
-            end)
-    
-    def __iter__(self):
-        return self
-
-    cdef bam1_t * getCurrent( self ):
-        return self.b
-
-    cdef int cnext(self):
-        '''cversion of iterator. Used by IteratorColumn'''
-        self.retval = hts_itr_next(self.htsfile.fp.bgzf,
-                                   self.iter,
-                                   self.b,
-                                   NULL)
-
-    def __next__(self):
-        """python version of next().
-        """
-        self.cnext()
-        if self.retval < 0:
-            raise StopIteration
-        return makeAlignedRead(self.b)
-
-    def __dealloc__(self):
-        hts_itr_destroy(self.iter)
-
-cdef class IteratorRowHead(IteratorRow):
-    """*(Samfile samfile, n, int reopen = True)*
-
-    iterate over first n reads in *samfile*
-
-    By default, the file is re-openend to avoid conflicts between
-    multiple iterators working on the same file. Set *reopen* = False
-    to not re-open *samfile*.
-
-    .. note::
-        It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.head`.
-        
-
-    """
-
-    def __init__(self, Samfile samfile, int n, int reopen=True):
-
-        IteratorRow.__init__(self, samfile, reopen=reopen)
-
-        self.max_rows = n
-        self.current_row = 0
-
-    def __iter__(self):
-        return self
-
-    cdef bam1_t * getCurrent( self ):
-        return self.b
-
-    cdef int cnext(self):
-        '''cversion of iterator. Used by IteratorColumn'''
-        return sam_read1(self.htsfile,
-                         self.samfile.header,
-                         self.b)
-
-    def __next__(self):
-        """python version of next().
-
-        pyrex uses this non-standard name instead of next()
-        """
-        if self.current_row >= self.max_rows:
-            raise StopIteration
-
-        cdef int ret
-        ret = sam_read1(self.htsfile,
-                        self.samfile.header, self.b)
-        if (ret >= 0):
-            self.current_row += 1
-            return makeAlignedRead( self.b )
-        else:
-            raise StopIteration
-
-
-cdef class IteratorRowAll(IteratorRow):
-    """*(Samfile samfile, int reopen = True)*
-
-    iterate over all reads in *samfile*
-
-    By default, the file is re-openend to avoid conflicts between
-    multiple iterators working on the same file. Set *reopen* = False
-    to not re-open *samfile*.
-
-    .. note::
-        It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
-        
-
-    """
-
-    def __init__(self, Samfile samfile, int reopen = True):
-
-        IteratorRow.__init__(self, samfile, reopen=reopen)
-
-    def __iter__(self):
-        return self
-
-    cdef bam1_t * getCurrent( self ):
-        return self.b
-
-    cdef int cnext(self):
-        '''cversion of iterator. Used by IteratorColumn'''
-        return sam_read1(self.htsfile,
-                         self.samfile.header,
-                         self.b)
-
-    def __next__(self):
-        """python version of next().
-
-        pyrex uses this non-standard name instead of next()
-        """
-        cdef int ret
-        ret = sam_read1(self.htsfile,
-                        self.samfile.header,
-                        self.b)
-        if (ret >= 0):
-            return makeAlignedRead(self.b)
-        else:
-            raise StopIteration
-
-
-cdef class IteratorRowAllRefs(IteratorRow):
-    """iterates over all mapped reads by chaining iterators over each reference
-
-    .. note::
-        It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
-    """
-
-    def __init__(self, Samfile samfile, reopen=True):
-
-        IteratorRow.__init__(self, samfile, reopen=reopen)
-
-        if not samfile._hasIndex():
-            raise ValueError("no index available for fetch")
-
-        self.tid = -1
-
-    def nextiter(self):
-        self.rowiter = IteratorRowRegion(self.samfile,
-                                         self.tid,
-                                         0,
-                                         1<<29)
-
-    def __iter__(self):
-        return self
-
-    def __next__(self):
-        """python version of next().
-
-        pyrex uses this non-standard name instead of next()
-        """
-        # Create an initial iterator
-        if self.tid == -1:
-            if not self.samfile.nreferences:
-                raise StopIteration
-            self.tid = 0
-            self.nextiter()
-
-        while 1:
-            self.rowiter.cnext()
-
-            # If current iterator is not exhausted, return aligned read
-            if self.rowiter.retval>0:
-                return makeAlignedRead(self.rowiter.b)
-
-            self.tid += 1
-
-            # Otherwise, proceed to next reference or stop
-            if self.tid < self.samfile.nreferences:
-                self.nextiter()
-            else:
-                raise StopIteration
-
-
-cdef class IteratorRowSelection(IteratorRow):
-    """*(Samfile samfile)*
-
-    iterate over reads in *samfile* at a given list of file positions.
-
-    .. note::
-        It is usually not necessary to create an object of this class
-        explicitely. It is returned as a result of call to a :meth:`Samfile.fetch`.
-    """
-
-    def __init__(self, Samfile samfile, positions, int reopen=True):
-
-        IteratorRow.__init__(self, samfile, reopen=reopen)
-
-        self.positions = positions
-        self.current_pos = 0
-
-        self.fp = self.htsfile.fp.bgzf
-
-    def __iter__(self):
-        return self
-
-    cdef bam1_t * getCurrent(self):
-        return self.b
-
-    cdef int cnext(self):
-        '''cversion of iterator'''
-
-        # end iteration if out of positions
-        if self.current_pos >= len(self.positions): return -1
-
-        bgzf_seek(self.fp,
-                  self.positions[self.current_pos],
-                  0)
-        self.current_pos += 1
-        return sam_read1(self.htsfile,
-                         self.samfile.header,
-                         self.b)
-
-    def __next__(self):
-        """python version of next().
-
-        pyrex uses this non-standard name instead of next()
-        """
-
-        cdef int ret = self.cnext()
-        if (ret >= 0):
-            return makeAlignedRead(self.b)
-        else:
-            raise StopIteration
-
-
-cdef int __advance_all(void *data, bam1_t *b):
-    '''advance without any read filtering.
-    '''
-    cdef __iterdata * d
-    d = <__iterdata*>data
-    return sam_itr_next(d.htsfile, d.iter, b)
-
-
-cdef int __advance_snpcalls(void * data, bam1_t * b):
-    '''advance using same filter and read processing as in
-    the samtools pileup.
-    '''
-
-    # Note that this method requries acces to some 
-    # functions in the samtools code base and is thus
-    # not htslib only.
-    # The functions accessed in samtools are:
-    # 1. bam_prob_realn
-    # 2. bam_cap_mapQ
-    cdef __iterdata * d
-    d = <__iterdata*>data
-
-    cdef int ret = sam_itr_next(d.htsfile, d.iter, b)
-    cdef int skip = 0
-    cdef int q
-    cdef int is_cns = 1
-    cdef int is_nobaq = 0
-    cdef int capQ_thres = 0
-
-    # reload sequence
-    if d.fastafile != NULL and b.core.tid != d.tid:
-        if d.seq != NULL:
-            free(d.seq)
-        d.tid = b.core.tid
-        d.seq = faidx_fetch_seq(
-            d.fastafile,
-            d.header.target_name[d.tid],
-            0, max_pos,
-            &d.seq_len)
-
-        if d.seq == NULL:
-            raise ValueError(
-                "reference sequence for '%s' (tid=%i) not found" % \
-                (d.header.target_name[d.tid],
-                 d.tid))
-
-
-    while ret >= 0:
-        skip = 0
-
-        # realign read - changes base qualities
-        if d.seq != NULL and is_cns and not is_nobaq: 
-            bam_prob_realn(b, d.seq)
-
-        if d.seq != NULL and capQ_thres > 10:
-            q = bam_cap_mapQ(b, d.seq, capQ_thres)
-            if q < 0:
-                skip = 1
-            elif b.core.qual > q:
-                b.core.qual = q
-        if b.core.flag & BAM_FUNMAP:
-            skip = 1
-        elif b.core.flag & 1 and not b.core.flag & 2:
-            skip = 1
-
-        if not skip: break
-        # additional filters
-
-        ret = sam_itr_next(d.htsfile, d.iter, b)
-
-    return ret
-
-cdef class IteratorColumn:
-    '''abstract base class for iterators over columns.
-
-    IteratorColumn objects wrap the pileup functionality of samtools.
-
-    For reasons of efficiency, the iterator points to the current
-    pileup buffer. The pileup buffer is updated at every iteration.
-    This might cause some unexpected behavious. For example,
-    consider the conversion to a list::
-
-       f = Samfile("file.bam", "rb")
-       result = list( f.pileup() )
-
-    Here, ``result`` will contain ``n`` objects of type
-    :class:`PileupProxy` for ``n`` columns, but each object in
-    ``result`` will contain the same information.
-
-    The desired behaviour can be achieved by list comprehension::
-
-       result = [ x.pileups() for x in f.pileup() ]
-
-    ``result`` will be a list of ``n`` lists of objects of type :class:`PileupRead`.
-
-    If the iterator is associated with a :class:`Fastafile` using the
-    :meth:`addReference` method, then the iterator will export the
-    current sequence via the methods :meth:`getSequence` and
-    :meth:`seq_len`.
-
-    Optional kwargs to the iterator:
-
-    stepper
-       The stepper controls how the iterator advances.
-
-       Valid values are None, "all" or "samtools".
-
-       The default stepper "all" uses all reads for
-       computing the pileup. This corresponds to the
-       mpileup options "-B" and "-A".
-
-       The stepper "samtools" uses the mpileup default
-       parameterization to advance.
-
-    fastafile
-       A :class:`FastaFile` object
-
-    max_depth
-       maximum read depth. The default is 8000.
-
-    '''
-
-    def __cinit__( self, Samfile samfile, **kwargs ):
-        self.samfile = samfile
-        # TODO
-        # self.mask = kwargs.get("mask", BAM_DEF_MASK )
-        self.fastafile = kwargs.get("fastafile", None)
-        self.stepper = kwargs.get("stepper", None)
-        self.max_depth = kwargs.get("max_depth", 8000)
-        self.iterdata.seq = NULL
-        self.tid = 0
-        self.pos = 0
-        self.n_plp = 0
-        self.plp = NULL
-        self.pileup_iter = <bam_plp_t>NULL
-
-    def __iter__(self):
-        return self
-
-    cdef int cnext(self):
-        '''perform next iteration.
-        '''
-        self.plp = bam_plp_auto( self.pileup_iter,
-                                 &self.tid,
-                                 &self.pos,
-                                 &self.n_plp )
-
-    cdef char * getSequence( self ):
-        '''return current reference sequence underlying the iterator.
-        '''
-        return self.iterdata.seq
-
-    property seq_len:
-        '''current sequence length.'''
-        def __get__(self): return self.iterdata.seq_len
-
-    def addReference(self, Fastafile fastafile):
-       '''
-       add reference sequences in *fastafile* to iterator.'''
-       self.fastafile = fastafile
-       if self.iterdata.seq != NULL: free(self.iterdata.seq)
-       self.iterdata.tid = -1
-       self.iterdata.fastafile = self.fastafile.fastafile
-
-    def hasReference(self):
-        '''
-        return true if iterator is associated with a reference'''
-        return self.fastafile
-
-    cdef setMask(self, mask):
-        '''set masking flag in iterator.
-
-        reads with bits set in *mask* will be skipped.
-        '''
-        raise NotImplementedError()
-        # self.mask = mask
-        # bam_plp_set_mask( self.pileup_iter, self.mask )
-
-    cdef setupIteratorData( self,
-                            int tid,
-                            int start,
-                            int end,
-                            int reopen = 0 ):
-        '''setup the iterator structure'''
-
-        self.iter = IteratorRowRegion(self.samfile, tid, start, end, reopen)
-        self.iterdata.htsfile = self.samfile.htsfile
-        self.iterdata.iter = self.iter.iter
-        self.iterdata.seq = NULL
-        self.iterdata.tid = -1
-        self.iterdata.header = self.samfile.header
-
-        if self.fastafile != None:
-            self.iterdata.fastafile = self.fastafile.fastafile
-        else:
-            self.iterdata.fastafile = NULL
-
-        if self.stepper == None or self.stepper == "all":
-            self.pileup_iter = bam_plp_init(
-                <bam_plp_auto_f>&__advance_all,
-                &self.iterdata)
-        elif self.stepper == "samtools":
-            self.pileup_iter = bam_plp_init(
-                <bam_plp_auto_f>&__advance_snpcalls,
-                &self.iterdata)
-        else:
-            raise ValueError(
-                "unknown stepper option `%s` in IteratorColumn" % self.stepper)
-
-        if self.max_depth:
-            bam_plp_set_maxcnt(self.pileup_iter, self.max_depth)
-
-        # bam_plp_set_mask( self.pileup_iter, self.mask )
-
-    cdef reset( self, tid, start, end ):
-        '''reset iterator position.
-
-        This permits using the iterator multiple times without
-        having to incur the full set-up costs.
-        '''
-        self.iter = IteratorRowRegion( self.samfile, tid, start, end, reopen = 0 )
-        self.iterdata.iter = self.iter.iter
-
-        # invalidate sequence if different tid
-        if self.tid != tid:
-            if self.iterdata.seq != NULL: free( self.iterdata.seq )
-            self.iterdata.seq = NULL
-            self.iterdata.tid = -1
-
-        # self.pileup_iter = bam_plp_init( &__advancepileup, &self.iterdata )
-        bam_plp_reset(self.pileup_iter)
-
-    def __dealloc__(self):
-        # reset in order to avoid memory leak messages for iterators 
-        # that have not been fully consumed
-        if self.pileup_iter != <bam_plp_t>NULL:
-            bam_plp_reset(self.pileup_iter)
-            bam_plp_destroy(self.pileup_iter)
-            self.pileup_iter = <bam_plp_t>NULL
-            self.plp = <bam_pileup1_t*>NULL
-
-        if self.iterdata.seq != NULL:
-            free(self.iterdata.seq)
-            self.iterdata.seq = NULL
-
-
-cdef class IteratorColumnRegion(IteratorColumn):
-    '''iterates over a region only.
-    '''
-    def __cinit__(self, Samfile samfile,
-                  int tid = 0,
-                  int start = 0,
-                  int end = max_pos,
-                  int truncate = False,
-                  **kwargs ):
-
-        # initialize iterator
-        self.setupIteratorData( tid, start, end, 1 )
-        self.start = start
-        self.end = end
-        self.truncate = truncate
-
-    def __next__(self):
-        """python version of next().
-        """
-
-        while 1:
-            self.cnext()
-            if self.n_plp < 0:
-                raise ValueError("error during iteration" )
-
-            if self.plp == NULL:
-                raise StopIteration
-            
-            if self.truncate:
-                if self.start > self.pos: continue
-                if self.pos >= self.end: raise StopIteration
-
-            return makePileupProxy(&self.plp,
-                                   self.tid,
-                                   self.pos,
-                                   self.n_plp)
-
-
-cdef class IteratorColumnAllRefs(IteratorColumn):
-    """iterates over all columns by chaining iterators over each reference
-    """
-
-    def __cinit__(self,
-                  Samfile samfile,
-                  **kwargs):
-
-        # no iteration over empty files
-        if not samfile.nreferences:
-            raise StopIteration
-
-        # initialize iterator
-        self.setupIteratorData(self.tid, 0, max_pos, 1)
-
-    def __next__(self):
-        """python version of next().
-        """
-
-        while 1:
-            self.cnext()
-
-            if self.n_plp < 0:
-                raise ValueError("error during iteration" )
-
-            # return result, if within same reference
-            if self.plp != NULL:
-                return makePileupProxy(&self.plp,
-                                       self.tid,
-                                       self.pos,
-                                       self.n_plp)
-                
-            # otherwise, proceed to next reference or stop
-            self.tid += 1
-            if self.tid < self.samfile.nreferences:
-                self.setupIteratorData(self.tid, 0, max_pos, 0)
-            else:
-                raise StopIteration
-
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-cdef inline int32_t query_start(bam1_t *src) except -1:
-    cdef uint32_t * cigar_p
-    cdef uint32_t k, op
-    cdef uint32_t start_offset = 0
-
-    if pysam_get_n_cigar(src):
-        cigar_p = pysam_bam_get_cigar(src);
-        for k from 0 <= k < pysam_get_n_cigar(src):
-            op = cigar_p[k] & BAM_CIGAR_MASK
-            if op == BAM_CHARD_CLIP:
-                if start_offset != 0 and start_offset != src.core.l_qseq:
-                    PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')
-                    return -1
-            elif op == BAM_CSOFT_CLIP:
-                start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT
-            else:
-                break
-
-    return start_offset
-
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-##-------------------------------------------------------------------
-cdef inline int32_t query_end(bam1_t *src) except -1:
-    cdef uint32_t * cigar_p
-    cdef uint32_t k, op
-    cdef uint32_t end_offset = src.core.l_qseq
-
-    if pysam_get_n_cigar(src) > 1:
-        cigar_p = pysam_bam_get_cigar(src);
-        for k from pysam_get_n_cigar(src) > k >= 1:
-            op = cigar_p[k] & BAM_CIGAR_MASK
-            if op == BAM_CHARD_CLIP:
-                if end_offset != 0 and end_offset != src.core.l_qseq:
-                    PyErr_SetString(ValueError,
-                                    'Invalid clipping in CIGAR string')
-                    return -1
-            elif op == BAM_CSOFT_CLIP:
-                end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT
-            else:
-                break
-
-    if end_offset == 0:
-        end_offset = src.core.l_qseq
-
-    return end_offset
-
-
-cdef inline object get_seq_range(bam1_t *src, uint32_t start, uint32_t end):
-    cdef uint8_t * p
-    cdef uint32_t k
-    cdef char * s
-
-    if not src.core.l_qseq:
-        return None
-
-    seq = PyBytes_FromStringAndSize(NULL, end - start)
-    s   = <char*>seq
-    p   = pysam_bam_get_seq(src)
-
-    for k from start <= k < end:
-        # equivalent to seq_nt16_str[bam1_seqi(s, i)] (see bam.c)
-        # note: do not use string literal as it will be a python string
-        s[k-start] = seq_nt16_str[p[k/2] >> 4 * (1 - k%2) & 0xf]
-
-    return seq
-
-
-cdef inline object get_qual_range(bam1_t *src, uint32_t start, uint32_t end):
-    cdef uint8_t * p
-    cdef uint32_t k
-    cdef char * q
-
-    p = pysam_bam_get_qual(src)
-    if p[0] == 0xff:
-        return None
-
-    qual = PyBytes_FromStringAndSize(NULL, end - start)
-    q    = <char*>qual
-
-    for k from start <= k < end:
-        ## equivalent to t[i] + 33 (see bam.c)
-        q[k-start] = p[k] + 33
-
-    return qual
-
-cdef inline uint8_t get_type_code(value, value_type = None):
-    '''guess type code for a *value*. If *value_type* is None,
-    the type code will be inferred based on the Python type of
-    *value*'''
-    cdef uint8_t  type_code    
-    cdef char * _char_type
-
-    if value_type is None:
-        if isinstance(value, int):
-            type_code = 'i'
-        elif isinstance(value, float):
-            type_code = 'd'
-        elif isinstance(value, str):
-            type_code = 'Z'
-        elif isinstance(value, bytes):
-            type_code = 'Z'
-        else:
-            return 0
-    else:
-        if value_type not in 'Zidf':
-            return 0
-        value_type = _forceBytes( value_type )
-        _char_type = value_type
-        type_code = (<uint8_t*>_char_type)[0]
-
-    return type_code
-
-cdef inline convert_python_tag(pytag, value, fmts, args):
-    
-    if not type(pytag) is bytes:
-        pytag = pytag.encode('ascii')
-    t = type(value)
-
-    if t is tuple or t is list:
-        # binary tags - treat separately
-        pytype = 'B'
-        # get data type - first value determines type
-        if type(value[0]) is float:
-            datafmt, datatype = "f", "f"
-        else:
-            mi, ma = min(value), max(value)
-            absmax = max( abs(mi), abs(ma) )
-            # signed ints
-            if mi < 0: 
-                if mi >= -127: datafmt, datatype = "b", 'c'
-                elif mi >= -32767: datafmt, datatype = "h", 's'
-                elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                else: datafmt, datatype = "i", 'i'
-
-            # unsigned ints
-            else:
-                if absmax <= 255: datafmt, datatype = "B", 'C'
-                elif absmax <= 65535: datafmt, datatype = "H", 'S'
-                elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                else: datafmt, datatype = "I", 'I'
-
-        datafmt = "2sccI%i%s" % (len(value), datafmt)
-        args.extend( [pytag[:2], 
-                      pytype.encode('ascii'),
-                      datatype.encode('ascii'),
-                      len(value)] + list(value) )
-        fmts.append( datafmt )
-        return
-
-    if t is float:
-        fmt, pytype = "2scf", 'f'
-    elif t is int:
-        # negative values
-        if value < 0:
-            if value >= -127: fmt, pytype = "2scb", 'c'
-            elif value >= -32767: fmt, pytype = "2sch", 's'
-            elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-            else: fmt, pytype = "2sci", 'i'
-        # positive values
-        else:
-            if value <= 255: fmt, pytype = "2scB", 'C'
-            elif value <= 65535: fmt, pytype = "2scH", 'S'
-            elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-            else: fmt, pytype = "2scI", 'I'
-    else:
-        # Note: hex strings (H) are not supported yet
-        if t is not bytes:
-            value = value.encode('ascii')
-        if len(value) == 1:
-            fmt, pytype = "2scc", 'A'
-        else:
-            fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
-
-    args.extend( [pytag[:2],
-                  pytype.encode('ascii'),
-                  value ] )
-
-    fmts.append( fmt )
-    
-###########################################################
-###########################################################
-###########################################################
-cdef class AlignedRead:
-    '''
-    Class representing an aligned read. See the SAM format specification for
-    the meaning of fields (http://samtools.sourceforge.net/).
-
-    This class stores a handle to the samtools C-structure representing
-    an aligned read. Member read access is forwarded to the C-structure
-    and converted into python objects. This implementation should be fast,
-    as only the data needed is converted.
-
-    For write access, the C-structure is updated in-place. This is
-    not the most efficient way to build BAM entries, as the variable
-    length data is concatenated and thus needs to be resized if
-    a field is updated. Furthermore, the BAM entry might be
-    in an inconsistent state.
-
-    One issue to look out for is that the sequence should always
-    be set *before* the quality scores. Setting the sequence will
-    also erase any quality scores that were set previously.
-
-    In Python 3, the fields containing sequence and quality
-    (seq, query, qual and qqual) data are of type bytes. Other
-    string data, such as the qname field and strings in the
-    tags tuple, is represented as unicode strings. On assignment,
-    both bytes and unicode objects are allowed, but unicode strings
-    must contain only ASCII characters.
-    '''
-
-    # Now only called when instances are created from Python
-    def __init__(self):
-        # see bam_init1
-        self._delegate = <bam1_t*>calloc( 1, sizeof( bam1_t) )
-        # allocate some memory
-        # If size is 0, calloc does not return a pointer that can be passed to free()
-        # so allocate 40 bytes for a new read
-        self._delegate.m_data = 40
-        self._delegate.data = <uint8_t *>calloc(
-            self._delegate.m_data, 1)
-        self._delegate.l_data = 0
-
-    def __dealloc__(self):
-        bam_destroy1(self._delegate)
-
-    def __str__(self):
-        """return string representation of alignment.
-
-        The representation is an approximate :term:`sam` format.
-
-        An aligned read might not be associated with a :term:`Samfile`.
-        As a result :term:`tid` is shown instead of the reference name.
-
-        Similarly, the tags field is returned in its parsed state.
-        """
-        # sam-parsing is done in sam.c/bam_format1_core which
-        # requires a valid header.
-        if sys.version_info[0] < 3:
-            seq = self.seq
-            qual = self.qual
-        else:
-            seq = self.seq.decode('ascii')
-            qual = self.qual.decode('ascii')
-        return "\t".join(map(str, (self.qname,
-                                   self.flag,
-                                   self.rname,
-                                   self.pos,
-                                   self.mapq,
-                                   self.cigar,
-                                   self.mrnm,
-                                   self.mpos,
-                                   self.rlen,
-                                   seq,
-                                   qual,
-                                   self.tags )))
-
-    def compare(self, AlignedRead other):
-        '''return -1,0,1, if contents in this are binary <,=,> to *other*'''
-
-        cdef int retval, x
-        cdef bam1_t *t
-        cdef bam1_t *o
-
-        t = self._delegate
-        o = other._delegate
-
-        # uncomment for debugging purposes
-        # cdef unsigned char * oo, * tt
-        # tt = <unsigned char*>(&t.core)
-        # oo = <unsigned char*>(&o.core)
-        # for x from 0 <= x < sizeof( bam1_core_t): print x, tt[x], oo[x]
-        # tt = <unsigned char*>(t.data)
-        # oo = <unsigned char*>(o.data)
-        # for x from 0 <= x < max(t.l_data, o.l_data): print x, tt[x], oo[x], chr(tt[x]), chr(oo[x])
-
-        # Fast-path test for object identity
-        if t == o:
-            return 0
-
-        retval = memcmp(&t.core, &o.core, sizeof(bam1_core_t))
-
-        if retval: return retval
-        retval = (t.l_data > o.l_data) - (t.l_data < o.l_data) # cmp(t.l_data, o.l_data)
-        if retval: return retval
-        return memcmp(t.data, o.data, t.l_data)
-
-    # Disabled so long as __cmp__ is a special method
-    def __hash__(self):
-        return _Py_HashPointer(<void *>self)
-
-    def _convert_python_tag(self, pytag, value, fmts, args):
-
-        if not type(pytag) is bytes:
-            pytag = pytag.encode('ascii')
-        t = type(value)
-
-        if t is tuple or t is list:
-            # binary tags - treat separately
-            pytype = 'B'
-            # get data type - first value determines type
-            if type(value[0]) is float:
-                datafmt, datatype = "f", "f"
-            else:
-                mi, ma = min(value), max(value)
-                absmax = max( abs(mi), abs(ma) )
-                # signed ints
-                if mi < 0: 
-                    if mi >= -127: datafmt, datatype = "b", 'c'
-                    elif mi >= -32767: datafmt, datatype = "h", 's'
-                    elif absmax < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                    else: datafmt, datatype = "i", 'i'
-
-                # unsigned ints
-                else:
-                    if absmax <= 255: datafmt, datatype = "B", 'C'
-                    elif absmax <= 65535: datafmt, datatype = "H", 'S'
-                    elif absmax > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                    else: datafmt, datatype = "I", 'I'
-                    
-            datafmt = "2sccI%i%s" % (len(value), datafmt)
-            args.extend( [pytag[:2], 
-                          pytype.encode('ascii'),
-                          datatype.encode('ascii'),
-                          len(value)] + list(value) )
-            fmts.append( datafmt )
-            return
-
-        if t is float:
-            fmt, pytype = "2scf", 'f'
-        elif t is int:
-            # negative values
-            if value < 0:
-                if value >= -127: fmt, pytype = "2scb", 'c'
-                elif value >= -32767: fmt, pytype = "2sch", 's'
-                elif value < -2147483648: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                else: fmt, pytype = "2sci", 'i'
-            # positive values
-            else:
-                if value <= 255: fmt, pytype = "2scB", 'C'
-                elif value <= 65535: fmt, pytype = "2scH", 'S'
-                elif value > 4294967295: raise ValueError( "integer %i out of range of BAM/SAM specification" % value )
-                else: fmt, pytype = "2scI", 'I'
-        else:
-            # Note: hex strings (H) are not supported yet
-            if t is not bytes:
-                value = value.encode('ascii')
-            if len(value) == 1:
-                fmt, pytype = "2scc", 'A'
-            else:
-                fmt, pytype = "2sc%is" % (len(value)+1), 'Z'
-
-        args.extend( [pytag[:2],
-                      pytype.encode('ascii'),
-                      value ] )
-        
-        fmts.append( fmt )
-
-
-    #######################################################################
-    #######################################################################
-    ## Basic properties
-    #######################################################################
-    property qname:
-        """the query name (None if not present)"""
-        def __get__(self):
-            cdef bam1_t * src
-            src = self._delegate
-            if pysam_get_l_qname(src) == 0:
-                return None
-            return _charptr_to_str(<char *>pysam_bam_get_qname(src))
-
-        def __set__(self, qname ):
-            if qname == None or len(qname) == 0: return
-            qname = _forceBytes(qname)
-            cdef bam1_t * src
-            cdef int l
-            cdef char * p
-
-            src = self._delegate
-            p = pysam_bam_get_qname( src )
-
-            # the qname is \0 terminated
-            l = len(qname) + 1
-            pysam_bam_update(src,
-                             pysam_get_l_qname(src),
-                             l,
-                             <uint8_t*>p)
-
-            
-            pysam_set_l_qname(src, l)
-
-            # re-acquire pointer to location in memory
-            # as it might have moved
-            p = pysam_bam_get_qname(src)
-
-            strncpy(p, qname, l)
-
-    property cigar:
-        """the :term:`cigar` alignment. The alignment
-        is returned as a list of tuples of (operation, length). 
-
-        If the alignment is not present, an empty list is
-        returned.
-
-        The operations are:
-
-        +-----+--------------+-----+
-        |M    |BAM_CMATCH    |0    |
-        +-----+--------------+-----+
-        |I    |BAM_CINS      |1    |
-        +-----+--------------+-----+
-        |D    |BAM_CDEL      |2    |
-        +-----+--------------+-----+
-        |N    |BAM_CREF_SKIP |3    |
-        +-----+--------------+-----+
-        |S    |BAM_CSOFT_CLIP|4    |
-        +-----+--------------+-----+
-        |H    |BAM_CHARD_CLIP|5    |
-        +-----+--------------+-----+
-        |P    |BAM_CPAD      |6    |
-        +-----+--------------+-----+
-        |=    |BAM_CEQUAL    |7    |
-        +-----+--------------+-----+
-        |X    |BAM_CDIFF     |8    |
-        +-----+--------------+-----+
-
-        .. note::
-            The output is a list of (operation, length) tuples, such as
-            ``[(0, 30)]``.
-            This is different from the SAM specification and
-            the :attr:`cigarstring` property, which uses a
-            (length, operation) order, for example: ``30M``.
-
-        To unset the cigar property, assign an empty list
-        or None.
-        """
-        def __get__(self):
-            cdef uint32_t * cigar_p
-            cdef bam1_t * src
-            cdef op, l, cigar
-            cdef int k
-            cigar = []
-
-            src = self._delegate
-            if pysam_get_n_cigar(src) == 0:
-                return cigar
-
-            cigar_p = pysam_bam_get_cigar(src);
-            for k from 0 <= k < pysam_get_n_cigar(src):
-                op = cigar_p[k] & BAM_CIGAR_MASK
-                l = cigar_p[k] >> BAM_CIGAR_SHIFT
-                cigar.append((op, l))
-            return cigar
-
-        def __set__(self, values):
-            cdef uint32_t * p
-            cdef bam1_t * src
-            cdef op, l
-            cdef int k, ncigar
-
-            k = 0
-
-            src = self._delegate
-
-            # get location of cigar string
-            p = pysam_bam_get_cigar(src)
-
-            # empty values for cigar string
-            if values is None:
-                values = []
-
-            ncigar = len(values)
-            # create space for cigar data within src.data
-            pysam_bam_update(src,
-                             pysam_get_n_cigar(src) * 4,
-                             ncigar * 4,
-                             <uint8_t*>p)
-
-            # length is number of cigar operations, not bytes
-            pysam_set_n_cigar(src, ncigar)
-
-            # re-acquire pointer to location in memory
-            # as it might have moved
-            p = pysam_bam_get_cigar(src)
-
-            # insert cigar operations
-            for op, l in values:
-                p[k] = l << BAM_CIGAR_SHIFT | op
-                k += 1
-
-            ## setting the cigar string requires updating the bin
-            pysam_set_bin(src,
-                          hts_reg2bin(
-                              src.core.pos,
-                              bam_endpos(src),
-                              14,
-                              5))
-
-    property cigarstring:
-        '''the :term:`cigar` alignment as a string.
-        
-        The cigar string is a string of alternating integers
-        and characters denoting the length and the type of
-        an operation.
-
-        .. note::
-            The order length,operation is specified in the
-            SAM format. It is different from the order of
-            the :attr:`cigar` property.
-
-        Returns the empty string if not present.
-
-        To unset the cigarstring, assign None or the
-        empty string.
-        '''
-        def __get__(self):
-            c = self.cigar
-            if c == None: return ""
-            # reverse order
-            else: return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
-            
-        def __set__(self, cigar):
-            if cigar is None or len(cigar) == 0:
-                self.cigar = []
-            else:
-                parts = CIGAR_REGEX.findall(cigar)
-                # reverse order
-                self.cigar = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]
-
-    property seq:
-        """read sequence bases, including :term:`soft clipped` bases 
-        (None if not present).
-
-        In Python 3, this property is of type bytes and assigning a
-        unicode string to it consisting of ASCII characters only will
-        work, but is inefficient.
-
-        Note that assigning to seq will invalidate any quality scores.
-        Thus, to in-place edit the sequence and quality scores, copies of
-        the quality scores need to be taken. Consider trimming for example::
-
-           q = read.qual
-           read.seq = read.seq[5:10]
-           read.qual = q[5:10]
-
-        The sequence is returned as it is stored in the BAM file. Some mappers
-        might have stored a reverse complement of the original read 
-        sequence.
-        """
-        def __get__(self):
-            cdef bam1_t * src
-            cdef char * s
-            src = self._delegate
-
-            if src.core.l_qseq == 0: return None
-
-            return get_seq_range(src, 0, src.core.l_qseq)
-
-        def __set__(self, seq):
-            # samtools manages sequence and quality length memory together
-            # if no quality information is present, the first byte says 0xff.
-            cdef bam1_t * src
-            cdef uint8_t * p
-            cdef char * s
-            cdef int l, k, nbytes_new, nbytes_old
-
-            if seq == None:
-                l = 0
-            else:
-                l = len(seq)                
-                seq = _forceBytes(seq)
-
-            src = self._delegate
-
-            # as the sequence is stored in half-bytes, the total length (sequence
-            # plus quality scores) is (l+1)/2 + l
-            nbytes_new = (l + 1) / 2 + l
-            nbytes_old = (src.core.l_qseq + 1) / 2 + src.core.l_qseq
-
-            # acquire pointer to location in memory
-            p = pysam_bam_get_seq(src)
-            src.core.l_qseq = l
-
-            # change length of data field
-            pysam_bam_update(src,
-                             nbytes_old,
-                             nbytes_new,
-                             p)
-
-            if l > 0:
-                # re-acquire pointer to location in memory
-                # as it might have moved
-                p = pysam_bam_get_seq(src)
-                for k from 0 <= k < nbytes_new:
-                    p[k] = 0
-                # convert to C string
-                s = seq
-                for k from 0 <= k < l:
-                    p[k/2] |= seq_nt16_table[<unsigned char>s[k]] << 4 * (1 - k % 2)
-
-                # erase qualities
-                p = pysam_bam_get_qual(src)
-                p[0] = 0xff
-
-    property qual:
-        """read sequence base qualities, including :term:`soft
-        clipped` bases (None if not present).
-
-        In Python 3, this property is of type bytes and assigning a
-        unicode string to it consisting of ASCII characters only will
-        work, but is inefficient.
-
-        Note that to set quality scores the sequence has to be set
-        previously as this will determine the permitted length of
-        the quality score array.
-
-        This method raises a ValueError if the length of the 
-        quality scores and the sequence are not the same.
-        """
-        def __get__(self):
-
-            cdef bam1_t * src
-            cdef char * q
-
-            src = self._delegate
-
-            if src.core.l_qseq == 0: return None
-
-            return get_qual_range(src, 0, src.core.l_qseq)
-
-        def __set__(self,qual):
-            # note that space is already allocated via the sequences
-            cdef bam1_t * src
-            cdef uint8_t * p
-            cdef char * q
-            cdef int k
-
-            src = self._delegate
-            p = pysam_bam_get_qual(src)
-            if qual == None or len(qual) == 0:
-                # if absent - set to 0xff
-                p[0] = 0xff
-                return
-            qual = _forceBytes(qual)
-            cdef int l
-            # convert to C string
-            q = qual
-            l = len(qual)
-            if src.core.l_qseq != l:
-                raise ValueError("quality and sequence mismatch: %i != %i" % (l, src.core.l_qseq))
-            assert src.core.l_qseq == l
-            for k from 0 <= k < l:
-                p[k] = <uint8_t>q[k] - 33
-
-    property query:
-        """aligned portion of the read.
-
-        This is a substring of :attr:`seq` that excludes flanking bases that were
-        :term:`soft clipped` (None if not present). It is equal to ``seq[qstart:qend]``.
-
-        In Python 3, this property is of type bytes. Assigning a
-        unicode string to it consisting of ASCII characters only will
-        work, but is inefficient.
-
-        SAM/BAM files may include extra flanking bases that are
-        not part of the alignment.  These bases may be the result of the
-        Smith-Waterman or other algorithms, which may not require alignments
-        that begin at the first residue or end at the last.  In addition,
-        extra sequencing adapters, multiplex identifiers, and low-quality bases that
-        were not considered for alignment may have been retained."""
-
-        def __get__(self):
-            cdef bam1_t * src
-            cdef uint32_t start, end
-            cdef char * s
-
-            src = self._delegate
-
-            if src.core.l_qseq == 0: return None
-
-            start = query_start(src)
-            end   = query_end(src)
-
-            return get_seq_range(src, start, end)
-
-    property qqual:
-        """aligned query sequence quality values (None if not
-        present). These are the quality values that correspond to :attr:`query`, that is,
-        they exclude qualities of :term:`soft clipped` bases. This is equal to
-        ``qual[qstart:qend]``.
-
-        This property is read-only.
-
-        In Python 3, this property is of type bytes."""
-        def __get__(self):
-            cdef bam1_t * src
-            cdef uint32_t start, end
-
-            src = self._delegate
-
-            if src.core.l_qseq == 0: return None
-
-            start = query_start(src)
-            end   = query_end(src)
-
-            return get_qual_range(src, start, end)
-
-    property qstart:
-        """start index of the aligned query portion of the sequence (0-based, inclusive).
-
-        This the index of the first base in :attr:`seq` that is not soft-clipped.
-        """
-        def __get__(self):
-            return query_start(self._delegate)
-
-    property qend:
-        """end index of the aligned query portion of the sequence (0-based, exclusive)"""
-        def __get__(self):
-            return query_end(self._delegate)
-
-    property qlen:
-        """length of the aligned query sequence.
-
-        This is equal to :attr:`qend` - :attr:`qstart`"""
-        def __get__(self):
-            cdef bam1_t * src
-            src = self._delegate
-            return query_end(src)-query_start(src)
-
-    property tags:
-        """the tags in the AUX field.
-
-        This property permits convenience access to
-        the tags. Changes it the returned list will
-        not update the tags automatically. Instead,
-        the following is required for adding a
-        new tag::
-
-            read.tags = read.tags + [("RG",0)]
-
-        This method will happily write the same tag
-        multiple times.
-        """
-        def __get__(self):
-            cdef char * ctag
-            cdef bam1_t * src
-            cdef uint8_t * s
-            cdef char auxtag[3]
-            cdef char auxtype
-            cdef uint8_t byte_size
-            cdef int32_t nvalues
-
-            src = self._delegate
-            if src.l_data == 0:
-                return []
-            s = pysam_bam_get_aux(src)
-            result = []
-            auxtag[2] = 0
-            while s < (src.data + src.l_data):
-                # get tag
-                auxtag[0] = s[0]
-                auxtag[1] = s[1]
-                s += 2
-                auxtype = s[0]
-                if auxtype in ('c', 'C'):
-                    value = <int>bam_aux2i(s)
-                    s += 1
-                elif auxtype in ('s', 'S'):
-                    value = <int>bam_aux2i(s)
-                    s += 2
-                elif auxtype in ('i', 'I'):
-                    value = <int32_t>bam_aux2i(s)
-                    s += 4
-                elif auxtype == 'f':
-                    value = <float>bam_aux2f(s)
-                    s += 4
-                elif auxtype == 'd':
-                    value = <double>bam_aux2f(s)
-                    s += 8
-                elif auxtype == 'A':
-                    value = "%c" % <char>bam_aux2A(s)
-                    s += 1
-                elif auxtype in ('Z', 'H'):
-                    value = _charptr_to_str(<char*>bam_aux2Z(s))
-                    # +1 for NULL terminated string
-                    s += len(value) + 1
-                elif auxtype == 'B':
-                    s += 1
-                    byte_size, nvalues, value = convertBinaryTagToList( s )
-                    # 5 for 1 char and 1 int
-                    s += 5 + ( nvalues * byte_size) - 1
-                else:
-                    raise KeyError("unknown type '%s'" % auxtype)
-
-                s += 1
-
-                result.append((_charptr_to_str(auxtag), value))
-
-            return result
-
-        def __set__(self, tags):
-            cdef bam1_t * src
-            cdef uint8_t * s
-            cdef char * temp
-            cdef int new_size = 0
-            cdef int old_size
-            src = self._delegate
-            fmts, args = ["<"], []
-            
-            if tags is not None and len(tags) > 0:
-                for pytag, value in tags:
-                    convert_python_tag(pytag, value, fmts, args)
-                fmt = "".join(fmts)
-                new_size = struct.calcsize(fmt)
-                buffer = ctypes.create_string_buffer(new_size)
-                struct.pack_into(fmt,
-                                 buffer,
-                                 0, 
-                                 *args)
-
-            # delete the old data and allocate new space.
-            # If total_size == 0, the aux field will be
-            # empty
-            old_size = pysam_bam_get_l_aux(src)
-            pysam_bam_update(src,
-                             old_size,
-                             new_size,
-                             pysam_bam_get_aux(src))
-
-            # copy data only if there is any
-            if new_size > 0:
-                
-                # get location of new data
-                s = pysam_bam_get_aux(src)
-
-                # check if there is direct path from buffer.raw to tmp
-                p = buffer.raw
-                # create handle to make sure buffer stays alive long 
-                # enough for memcpy, see issue 129
-                temp = p
-                memcpy(s, temp, new_size)
-
-    cpdef setTag(self, tag, value, 
-                 value_type = None, 
-                 replace = True):
-        '''
-        Set optional field of alignment *tag* to *value*.  *value_type* may be specified,
-        but if not the type will be inferred based on the Python type of *value*
-
-        An existing value of the same tag will be overwritten unless
-        *replace* is set to False.
-        '''
-
-        cdef int      value_size
-        cdef uint8_t * value_ptr
-        cdef uint8_t *existing_ptr
-        cdef uint8_t  type_code
-        cdef float    float_value
-        cdef double   double_value
-        cdef int32_t  int_value
-        cdef bam1_t * src = self._delegate
-        cdef char * _value_type
-        
-        if len(tag) != 2:
-            raise ValueError('Invalid tag: %s' % tag)
-        
-        type_code = get_type_code(value, value_type)
-        if type_code == 0:
-            raise ValueError("can't guess type or invalid type code specified")
-
-        # Not Endian-safe, but then again neither is samtools!
-        if type_code == 'Z':
-            value = _forceBytes( value )
-            value_ptr    = <uint8_t*><char*>value
-            value_size   = len(value)+1
-        elif type_code == 'i':
-            int_value    = value
-            value_ptr    = <uint8_t*>&int_value
-            value_size   = sizeof(int32_t)
-        elif type_code == 'd':
-            double_value = value
-            value_ptr    = <uint8_t*>&double_value
-            value_size   = sizeof(double)
-        elif type_code == 'f':
-            float_value  = value
-            value_ptr    = <uint8_t*>&float_value
-            value_size   = sizeof(float)
-        else:
-            raise ValueError('Unsupported value_type in set_option')
-
-        tag = _forceBytes( tag )
-        if replace:
-            existing_ptr = bam_aux_get(src, tag)
-            if existing_ptr:
-                bam_aux_del(src, existing_ptr)
-
-        bam_aux_append(src,
-                       tag,
-                       type_code, 
-                       value_size,
-                       value_ptr)
-
-    property flag:
-        """properties flag"""
-        def __get__(self):
-            return pysam_get_flag(self._delegate)
-        def __set__(self, flag):
-            pysam_set_flag(self._delegate, flag)
-
-    property rname:
-        """
-        :term:`target` ID
-
-        DEPRECATED from pysam-0.4 - use tid in the future.
-        The rname field caused a lot of confusion as it returns
-        the :term:`target` ID instead of the reference sequence
-        name.
-
-        .. note::
-
-            This field contains the index of the reference sequence
-            in the sequence dictionary. To obtain the name
-            of the reference sequence, use :meth:`pysam.Samfile.getrname()`
-
-        """
-        def __get__(self): return self._delegate.core.tid
-        def __set__(self, tid): self._delegate.core.tid = tid
-
-    property tid:
-        """
-        :term:`target` ID
-
-        .. note::
-
-            This field contains the index of the reference sequence
-            in the sequence dictionary. To obtain the name
-            of the reference sequence, use :meth:`pysam.Samfile.getrname()`
-
-        """
-        def __get__(self): return self._delegate.core.tid
-        def __set__(self, tid): self._delegate.core.tid = tid
-
-    property pos:
-        """0-based leftmost coordinate"""
-        def __get__(self): return self._delegate.core.pos
-        def __set__(self, pos):
-            ## setting the position requires updating the "bin" attribute
-            cdef bam1_t * src
-            src = self._delegate
-            src.core.pos = pos
-            if pysam_get_n_cigar(src):
-                pysam_set_bin(src, 
-                              hts_reg2bin(
-                                  src.core.pos,
-                                  bam_endpos(src),
-                                  14,
-                                  5))
-            else:
-                pysam_set_bin(src,
-                              hts_reg2bin(
-                                  src.core.pos,
-                                  src.core.pos + 1,
-                                  14,
-                                  5))
-
-    property bin:
-        """properties bin"""
-        def __get__(self):
-            return pysam_get_bin(self._delegate)
-        def __set__(self, bin):
-            pysam_set_bin(self._delegate, bin)
-
-    property rlen:
-        """length of the read. This includes soft-clipped bases
-        and is equal to ``len(seq)``.
-
-        This property is read-only.
-
-        Returns 0 if not available."""
-        def __get__(self):
-            return self._delegate.core.l_qseq
-
-    property aend:
-        '''aligned reference position of the read on the reference genome.  
-        
-        aend points to one past the last aligned residue.
-        Returns None if not available.'''
-        def __get__(self):
-            cdef bam1_t * src
-            src = self._delegate
-            if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
-                return None
-            return bam_endpos(src)
-
-    property alen:
-        '''aligned length of the read on the reference genome.
-
-        This is equal to `aend - pos`. Returns None if not available.'''
-        def __get__(self):
-            cdef bam1_t * src
-            src = self._delegate
-            if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
-                return None
-            return bam_endpos(src) - \
-                self._delegate.core.pos
-
-    property mapq:
-        """mapping quality"""
-        def __get__(self):
-            return pysam_get_qual(self._delegate)
-        def __set__(self, qual):
-            pysam_set_qual(self._delegate, qual)
-
-    property mrnm:
-        """the :term:`reference` id of the mate
-        deprecated, use RNEXT instead.
-        """
-        def __get__(self):
-            return self._delegate.core.mtid
-        def __set__(self, mtid):
-            self._delegate.core.mtid = mtid
-
-    property rnext:
-        """the :term:`reference` id of the mate """
-        def __get__(self): return self._delegate.core.mtid
-        def __set__(self, mtid): self._delegate.core.mtid = mtid
-
-    property mpos:
-        """the position of the mate
-        deprecated, use PNEXT instead."""
-        def __get__(self):
-            return self._delegate.core.mpos
-        def __set__(self, mpos):
-            self._delegate.core.mpos = mpos
-
-    property pnext:
-        """the position of the mate"""
-        def __get__(self):
-            return self._delegate.core.mpos
-        def __set__(self, mpos):
-            self._delegate.core.mpos = mpos
-
-    #######################################################################
-    #######################################################################
-    ## Flags
-    #######################################################################
-    property isize:
-        """the insert size
-        deprecated: use tlen instead"""
-        def __get__(self):
-            return self._delegate.core.isize
-        def __set__(self, isize):
-            self._delegate.core.isize = isize
-    property tlen:
-        """the template length"""
-        def __get__(self):
-            return self._delegate.core.isize
-        def __set__(self, isize):
-            self._delegate.core.isize = isize
-    property is_paired:
-        """true if read is paired in sequencing"""
-        def __get__(self):
-            return (self.flag & BAM_FPAIRED) != 0
-        def __set__(self,val):
-            pysam_update_flag(self._delegate, val, BAM_FPAIRED)
-    property is_proper_pair:
-        """true if read is mapped in a proper pair"""
-        def __get__(self):
-            return (self.flag & BAM_FPROPER_PAIR) != 0
-        def __set__(self,val):
-            pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
-    property is_unmapped:
-        """true if read itself is unmapped"""
-        def __get__(self):
-            return (self.flag & BAM_FUNMAP) != 0
-        def __set__(self, val):
-            pysam_update_flag(self._delegate, val, BAM_FUNMAP)
-    property mate_is_unmapped:
-        """true if the mate is unmapped"""
-        def __get__(self):
-            return (self.flag & BAM_FMUNMAP) != 0
-        def __set__(self,val):
-            pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
-    property is_reverse:
-        """true if read is mapped to reverse strand"""
-        def __get__(self):
-            return (self.flag & BAM_FREVERSE) != 0
-        def __set__(self,val):
-            pysam_update_flag(self._delegate, val, BAM_FREVERSE)
-    property mate_is_reverse:
-        """true is read is mapped to reverse strand"""
-        def __get__(self):
-            return (self.flag & BAM_FMREVERSE) != 0
-        def __set__(self,val):
-            pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
-    property is_read1:
-        """true if this is read1"""
-        def __get__(self):
-            return (self.flag & BAM_FREAD1) != 0
-        def __set__(self,val):
-            pysam_update_flag(self._delegate, val, BAM_FREAD1)
-    property is_read2:
-        """true if this is read2"""
-        def __get__(self):
-            return (self.flag & BAM_FREAD2) != 0
-        def __set__(self, val):
-            pysam_update_flag(self._delegate, val, BAM_FREAD2)
-    property is_secondary:
-        """true if not primary alignment"""
-        def __get__(self):
-            return (self.flag & BAM_FSECONDARY) != 0
-        def __set__(self, val):
-            pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
-    property is_qcfail:
-        """true if QC failure"""
-        def __get__(self):
-            return (self.flag & BAM_FQCFAIL) != 0
-        def __set__(self, val):
-            pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
-    property is_duplicate:
-        """true if optical or PCR duplicate"""
-        def __get__(self):
-            return (self.flag & BAM_FDUP) != 0
-        def __set__(self, val):
-            pysam_update_flag(self._delegate, val, BAM_FDUP)
-
-    #######################################################################
-    #######################################################################
-    ## Derived properties
-    #######################################################################
-    property positions:
-        """a list of reference positions that this read aligns to."""
-        def __get__(self):
-            cdef uint32_t k, i, pos
-            cdef int op
-            cdef uint32_t * cigar_p
-            cdef bam1_t * src
-
-            src = self._delegate
-            if pysam_get_n_cigar(src) == 0:
-                return []
-
-            result = []
-            pos = src.core.pos
-            cigar_p = pysam_bam_get_cigar(src)
-
-            for k from 0 <= k < pysam_get_n_cigar(src):
-                op = cigar_p[k] & BAM_CIGAR_MASK
-                l = cigar_p[k] >> BAM_CIGAR_SHIFT
-                if op == BAM_CMATCH:
-                    for i from pos <= i < pos + l:
-                        result.append( i )
-
-                if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
-                    pos += l
-
-            return result
-
-    property inferred_length:
-        """inferred read length from CIGAR string.
-
-        Returns 0 if CIGAR string is not present.
-        """
-        def __get__(self):
-           cdef uint32_t k, qpos
-           cdef int op
-           cdef uint32_t * cigar_p
-           cdef bam1_t * src 
-
-           src = self._delegate
-           if pysam_get_n_cigar(src) == 0: return 0
-
-           qpos = 0
-           cigar_p = pysam_bam_get_cigar(src)
-
-           for k from 0 <= k < pysam_get_n_cigar(src):
-               op = cigar_p[k] & BAM_CIGAR_MASK
-
-               if op == BAM_CMATCH or op == BAM_CINS or \
-                  op == BAM_CSOFT_CLIP or \
-                  op == BAM_CEQUAL or op == BAM_CDIFF:
-                   qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
-
-           return qpos
-            
-    property aligned_pairs:
-        """a list of aligned read and reference positions.
-
-        Unaligned position are marked by None.
-        """
-        def __get__(self):
-            cdef uint32_t k, i, pos, qpos
-            cdef int op
-            cdef uint32_t * cigar_p
-            cdef bam1_t * src 
-
-            src = self._delegate
-            if pysam_get_n_cigar(src) == 0:
-                return []
-
-            result = []
-            pos = src.core.pos
-            qpos = 0
-            cigar_p = pysam_bam_get_cigar(src)
-
-            for k from 0 <= k < pysam_get_n_cigar(src):
-                op = cigar_p[k] & BAM_CIGAR_MASK
-                l = cigar_p[k] >> BAM_CIGAR_SHIFT
-
-                if op == BAM_CMATCH:
-                    for i from pos <= i < pos + l:
-                        result.append( (qpos, i) )
-                        qpos += 1
-                    pos += l
-
-                elif op == BAM_CINS:
-                    for i from pos <= i < pos + l:
-                        result.append( (qpos, None) )
-                        qpos += 1
-
-                elif op == BAM_CDEL or op == BAM_CREF_SKIP:
-                    for i from pos <= i < pos + l:
-                        result.append( (None, i) )
-                    pos += l
-                       
-            return result
-
-    property blocks:
-        """ a list of start and end positions of
-        aligned gapless blocks.
-
-        The start and end positions are in genomic 
-        coordinates. 
-      
-        Blocks are not normalized, i.e. two blocks 
-        might be directly adjacent. This happens if
-        the two blocks are separated by an insertion 
-        in the read.
-        """
-
-        def __get__(self):
-            cdef uint32_t k, pos, l
-            cdef int op
-            cdef uint32_t * cigar_p
-            cdef bam1_t * src
-
-            src = self._delegate
-            if pysam_get_n_cigar(src) == 0:
-                return []
-            
-            result = []
-            pos = src.core.pos
-            cigar_p = pysam_bam_get_cigar(src)
-            l = 0
-
-            for k from 0 <= k < pysam_get_n_cigar(src):
-                op = cigar_p[k] & BAM_CIGAR_MASK
-                l = cigar_p[k] >> BAM_CIGAR_SHIFT
-                if op == BAM_CMATCH:
-                    result.append((pos, pos + l))
-                    pos += l
-                elif op == BAM_CDEL or op == BAM_CREF_SKIP:
-                    pos += l
-
-            return result
-
-    #######################################################################
-    #######################################################################
-    ## 
-    #######################################################################
-    def overlap( self, uint32_t start, uint32_t end ):
-        """return number of aligned bases of read overlapping the interval *start* and *end*
-        on the reference sequence.
-        """
-        cdef uint32_t k, i, pos, overlap
-        cdef int op, o
-        cdef uint32_t * cigar_p
-        cdef bam1_t * src
-
-        overlap = 0
-
-        src = self._delegate
-        if pysam_get_n_cigar(src) == 0:
-            return 0
-        pos = src.core.pos
-        o = 0
-
-        cigar_p = pysam_bam_get_cigar(src)
-        for k from 0 <= k < pysam_get_n_cigar(src):
-            op = cigar_p[k] & BAM_CIGAR_MASK
-            l = cigar_p[k] >> BAM_CIGAR_SHIFT
-
-            if op == BAM_CMATCH:
-                o = min( pos + l, end) - max( pos, start )
-                if o > 0: overlap += o
-
-            if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
-                pos += l
-
-        return overlap
-
-    def opt(self, tag):
-        """retrieves optional data given a two-letter *tag*"""
-        #see bam_aux.c: bam_aux_get() and bam_aux2i() etc
-        cdef uint8_t * v
-        cdef int nvalues
-        btag = _forceBytes(tag)
-        v = bam_aux_get(self._delegate, btag)
-        if v == NULL: raise KeyError( "tag '%s' not present" % tag )
-        auxtype = chr(v[0])
-        if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
-            return <int>bam_aux2i(v)
-        elif auxtype == 'i' or auxtype == 'I':
-            return <int32_t>bam_aux2i(v)
-        elif auxtype == 'f' or auxtype == 'F':
-            return <float>bam_aux2f(v)
-        elif auxtype == 'd' or auxtype == 'D':
-            return <double>bam_aux2f(v)
-        elif auxtype == 'A':
-            # there might a more efficient way
-            # to convert a char into a string
-            return '%c' % <char>bam_aux2A(v)
-        elif auxtype == 'Z':
-            return _charptr_to_str(<char*>bam_aux2Z(v))
-        elif auxtype == 'B':
-            bytesize, nvalues, values = convertBinaryTagToList( v + 1 )
-            return values
-        else:
-            raise ValueError("unknown auxilliary type '%s'" % auxtype)
-
-
-    def fancy_str (self):
-        """returns list of fieldnames/values in pretty format for debugging.
-
-        """
-        ret_string = []
-
-        # Originally written by Leo. Note that not all of these fields
-        # exist. Deprecate?
-        field_names = {
-           "tid":           "Contig index",
-           "pos":           "Mapped position on contig",
-           "mtid":          "Contig index for mate pair",
-           "mpos":          "Position of mate pair",
-           "isize":         "Insert size",
-           "flag":          "Binary flag",
-           "n_cigar":       "Count of cigar entries",
-           "cigar":         "Cigar entries",
-           "qual":          "Mapping quality",
-           "bin":           "Bam index bin number",
-           "l_qname":       "Length of query name",
-           "qname":         "Query name",
-           "l_qseq":        "Length of query sequence",
-           "qseq":          "Query sequence",
-           "bqual":         "Quality scores",
-           "l_data":         "Length of auxilary data",
-           "m_data":        "Maximum data length",
-           }
-        fields_names_in_order = ["tid", "pos", "mtid", "mpos", "isize", "flag",
-                                 "n_cigar", "cigar", "qual", "bin", "l_qname", "qname",
-                                 "l_qseq", "qseq", "bqual", "l_data", "m_data"]
-
-        for f in fields_names_in_order:
-            if f not in dir(self):
-                continue
-            ret_string.append("%-30s %-10s= %s" %
-                              (field_names[f],
-                               "(" + f + ")",
-                               self.__getattribute__(f)))
-
-        return ret_string
-
-cdef class PileupProxy:
-    '''A pileup column. A pileup column contains
-    all the reads that map to a certain target base.
-
-    tid
-        chromosome ID as is defined in the header
-    pos
-        the target base coordinate (0-based)
-    n
-        number of reads mapping to this column
-    pileups
-        list of reads (:class:`pysam.PileupRead`) aligned to this column
-
-    This class is a proxy for results returned by the samtools pileup engine.
-    If the underlying engine iterator advances, the results of this column
-    will change.
+    Added for backwards compatibility with pysam <= 0.8.0
     '''
-    def __init__(self):
-        raise TypeError("This class cannot be instantiated from Python")
-
-    def __str__(self):
-        return "\t".join( map(str, (self.tid, self.pos, self.n))) +\
-            "\n" +\
-            "\n".join( map(str, self.pileups) )
-
-    property tid:
-        '''the chromosome ID as is defined in the header'''
-        def __get__(self): return self.tid
-
-    property n:
-        '''number of reads mapping to this column.'''
-        def __get__(self): return self.n_pu
-        def __set__(self, n): self.n_pu = n
-
-    property pos:
-        def __get__(self): return self.pos
-
-    property pileups:
-        '''list of reads (:class:`pysam.PileupRead`) aligned to this column'''
-        def __get__(self):
-            cdef int x
-            pileups = []
+    pass
 
-            if self.plp == NULL or self.plp[0] == NULL:
-                raise ValueError("PileupProxy accessed after iterator finished")
 
-            # warning: there could be problems if self.n and self.buf are
-            # out of sync.
-            for x from 0 <= x < self.n_pu:
-                pileups.append(makePileupRead(&(self.plp[0][x])))
-            return pileups
+cdef class AlignedRead(AlignedSegment):
+    '''Deprecated alternative for :class:`~pysam.AlignedSegment`
 
-cdef class PileupRead:
-    '''A read aligned to a column.
+    Added for backwards compatibility with pysam <= 0.8.0
     '''
+    pass
 
-    def __init__(self):
-        raise TypeError("This class cannot be instantiated from Python")
-
-    def __str__(self):
-        return "\t".join( map(str, (self.alignment, self.qpos, self.indel, self.level, self.is_del, self.is_head, self.is_tail ) ) )
-
-    property alignment:
-        """a :class:`pysam.AlignedRead` object of the aligned read"""
-        def __get__(self):
-            return self._alignment
-    property qpos:
-        """position of the read base at the pileup site, 0-based"""
-        def __get__(self):
-            return self._qpos
-    property indel:
-        """indel length; 0 for no indel, positive for ins and negative for del"""
-        def __get__(self):
-            return self._indel
-    property is_del:
-        """1 iff the base on the padded read is a deletion"""
-        def __get__(self):
-            return self._is_del
-    property is_head:
-        def __get__(self):
-            return self._is_head
-    property is_tail:
-        def __get__(self):
-            return self._is_tail
-    property level:
-        def __get__(self):
-            return self._level
-
-
-cdef class SNPCall:
-    '''the results of a SNP call.'''
-    cdef int _tid
-    cdef int _pos
-    cdef char _reference_base
-    cdef char _genotype
-    cdef int _consensus_quality
-    cdef int _snp_quality
-    cdef int _rms_mapping_quality
-    cdef int _coverage
-
-    property tid:
-        '''the chromosome ID as is defined in the header'''
-        def __get__(self):
-            return self._tid
-
-    property pos:
-       '''nucleotide position of SNP.'''
-       def __get__(self): return self._pos
-
-    property reference_base:
-       '''reference base at pos. ``N`` if no reference sequence supplied.'''
-       def __get__(self): return from_string_and_size( &self._reference_base, 1 )
-
-    property genotype:
-       '''the genotype called.'''
-       def __get__(self): return from_string_and_size( &self._genotype, 1 )
-
-    property consensus_quality:
-       '''the genotype quality (Phred-scaled).'''
-       def __get__(self): return self._consensus_quality
-
-    property snp_quality:
-       '''the snp quality (Phred scaled) - probability of consensus being identical to reference sequence.'''
-       def __get__(self): return self._snp_quality
-
-    property mapping_quality:
-       '''the root mean square (rms) of the mapping quality of all reads involved in the call.'''
-       def __get__(self): return self._rms_mapping_quality
-
-    property coverage:
-       '''coverage or read depth - the number of reads involved in the call.'''
-       def __get__(self): return self._coverage
-
-    def __str__(self):
-
-        return "\t".join( map(str, (
-                    self.tid,
-                    self.pos,
-                    self.reference_base,
-                    self.genotype,
-                    self.consensus_quality,
-                    self.snp_quality,
-                    self.mapping_quality,
-                    self.coverage ) ) )
-
-
-cdef class IndexedReads:
-    """index a bamfile by read.
-
-    The index is kept in memory.
-
-    By default, the file is re-openend to avoid conflicts if
-    multiple operators work on the same file. Set *reopen* = False
-    to not re-open *samfile*.
-    """
-
-    def __init__(self, Samfile samfile, int reopen=True):
-
-        # makes sure that samfile stays alive as long as this
-        # object is alive.
-        self.samfile = samfile
-
-        assert samfile.isbam, "can only IndexReads on bam files"
-
-        # reopen the file - note that this makes the iterator
-        # slow and causes pileup to slow down significantly.
-        if reopen:
-            self.htsfile = hts_open(samfile._filename, 'r')
-            assert self.htsfile != NULL
-            # read header - required for accurate positioning
-            sam_hdr_read(self.htsfile)
-            self.owns_samfile = True
-        else:
-            self.htsfile = self.samfile.htsfile
-            self.owns_samfile = False
-
-        # TODO: BAM file specific
-        self.fp = self.htsfile.fp.bgzf
-
-    def build(self):
-        '''build index.'''
-
-        self.index = collections.defaultdict(list)
-
-        # this method will start indexing from the current file position
-        # if you decide
-        cdef int ret = 1
-        cdef bam1_t * b = <bam1_t*>calloc(1, sizeof( bam1_t))
-
-        cdef uint64_t pos
-
-        while ret > 0:
-            pos = bgzf_tell(self.fp)
-            ret = sam_read1(self.htsfile,
-                            self.samfile.header,
-                            b)
-            if ret > 0:
-                qname = _charptr_to_str(pysam_bam_get_qname(b))
-                self.index[qname].append(pos)
-
-        bam_destroy1(b)
-
-    def find(self, qname):
-        if qname in self.index:
-            return IteratorRowSelection(
-                self.samfile,
-                self.index[qname],
-                reopen = False)
-        else:
-            raise KeyError("read %s not found" % qname)
-
-    def __dealloc__(self):
-        if self.owns_samfile:
-            hts_close(self.htsfile)
-
-__all__ = ["Samfile",
-           "IteratorRow",
-           "IteratorColumn",
-           "AlignedRead",
-           "PileupColumn",
-           "PileupProxy",
-           "PileupRead",
-           "IndexedReads" ]
-           # "IteratorSNPCalls",
-           # "SNPCaller",
-           # "IndelCaller",
-           # "IteratorIndelCalls",
 
+__all__ = ['Samfile', 'AlignedRead']
 
 
diff --git a/pysam/csamtools.c b/pysam/csamtools.c
index 9118d0b..1c49dc0 100644
--- a/pysam/csamtools.c
+++ b/pysam/csamtools.c
@@ -1,4 +1,4 @@
-/* Generated by Cython 0.20.1 on Tue Jul 15 21:20:07 2014 */
+/* Generated by Cython 0.20.1 on Fri Nov 21 19:49:04 2014 */
 
 #define PY_SSIZE_T_CLEAN
 #ifndef CYTHON_USE_PYLONG_INTERNALS
diff --git a/pysam/ctabix.c b/pysam/ctabix.c
index 9018781..266f5cc 100644
--- a/pysam/ctabix.c
+++ b/pysam/ctabix.c
@@ -1,4 +1,4 @@
-/* Generated by Cython 0.20.1 on Tue Jul 15 21:20:07 2014 */
+/* Generated by Cython 0.20.1 on Fri Nov 21 19:49:04 2014 */
 
 #define PY_SSIZE_T_CLEAN
 #ifndef CYTHON_USE_PYLONG_INTERNALS
@@ -556,7 +556,7 @@ struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy;
 struct __pyx_obj_5pysam_10TabProxies_BedProxy;
 struct __pyx_obj_5pysam_10TabProxies_VCFProxy;
 struct __pyx_obj_5pysam_6ctabix_tabix_file_iterator;
-struct __pyx_obj_5pysam_6ctabix_Tabixfile;
+struct __pyx_obj_5pysam_6ctabix_TabixFile;
 struct __pyx_obj_5pysam_6ctabix_Parser;
 struct __pyx_obj_5pysam_6ctabix_asTuple;
 struct __pyx_obj_5pysam_6ctabix_asGTF;
@@ -567,6 +567,46 @@ struct __pyx_obj_5pysam_6ctabix_TabixIteratorParsed;
 struct __pyx_obj_5pysam_6ctabix_GZIterator;
 struct __pyx_obj_5pysam_6ctabix_GZIteratorHead;
 struct __pyx_obj_5pysam_6ctabix_GZIteratorParsed;
+struct __pyx_obj_5pysam_6ctabix_Tabixfile;
+struct __pyx_opt_args_5pysam_6ctabix__force_str;
+struct __pyx_opt_args_5pysam_6ctabix__force_bytes;
+struct __pyx_opt_args_5pysam_6ctabix__charptr_to_str;
+
+/* "pysam/ctabix.pxd":50
+ * ###########################################
+ * # used by cvcf.pyx
+ * cdef _force_str(object s, encoding=?)             # <<<<<<<<<<<<<<
+ * 
+ * ###########################################
+ */
+struct __pyx_opt_args_5pysam_6ctabix__force_str {
+  int __pyx_n;
+  PyObject *encoding;
+};
+
+/* "pysam/ctabix.pyx":51
+ *         raise TypeError, u"Argument must be string or unicode."
+ * 
+ * cdef inline bytes _force_bytes(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *     u"""convert string or unicode object to bytes, assuming ascii encoding.
+ *     """
+ */
+struct __pyx_opt_args_5pysam_6ctabix__force_bytes {
+  int __pyx_n;
+  PyObject *encoding;
+};
+
+/* "pysam/ctabix.pyx":65
+ *         raise TypeError, u"Argument must be string, bytes or unicode."
+ * 
+ * cdef inline _charptr_to_str(char* s, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *     if PY_MAJOR_VERSION < 3:
+ *         return s
+ */
+struct __pyx_opt_args_5pysam_6ctabix__charptr_to_str {
+  int __pyx_n;
+  PyObject *encoding;
+};
 
 /* "TabProxies.pxd":41
  *   ctypedef int uint64_t
@@ -585,13 +625,14 @@ struct __pyx_obj_5pysam_10TabProxies_TupleProxy {
   int nbytes;
   int offset;
   int is_modified;
+  PyObject *encoding;
 };
 
 
-/* "TabProxies.pxd":60
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "TabProxies.pxd":63
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class GTFProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class GTFProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -602,10 +643,10 @@ struct __pyx_obj_5pysam_10TabProxies_GTFProxy {
 };
 
 
-/* "TabProxies.pxd":69
+/* "TabProxies.pxd":73
  *     cdef char * getAttributes( self )
  * 
- * cdef class NamedTupleProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class NamedTupleProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  *     pass
  * 
  */
@@ -614,10 +655,10 @@ struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy {
 };
 
 
-/* "TabProxies.pxd":72
+/* "TabProxies.pxd":76
  *     pass
  * 
- * cdef class BedProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class BedProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -630,10 +671,10 @@ struct __pyx_obj_5pysam_10TabProxies_BedProxy {
 };
 
 
-/* "TabProxies.pxd":83
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "TabProxies.pxd":88
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class VCFProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class VCFProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -660,41 +701,45 @@ struct __pyx_obj_5pysam_6ctabix_tabix_file_iterator {
   size_t size;
   struct __pyx_obj_5pysam_6ctabix_Parser *parser;
   int fd;
+  int duplicated_fd;
   PyObject *infile;
 };
 
 
-/* "pysam/ctabix.pxd":30
+/* "pysam/ctabix.pxd":31
  *     cdef __cnext__(self)
  * 
- * cdef class Tabixfile:             # <<<<<<<<<<<<<<
+ * cdef class TabixFile:             # <<<<<<<<<<<<<<
  * 
  *     # pointer to tabixfile
  */
-struct __pyx_obj_5pysam_6ctabix_Tabixfile {
+struct __pyx_obj_5pysam_6ctabix_TabixFile {
   PyObject_HEAD
   htsFile *tabixfile;
   tbx_t *index;
   int isremote;
-  char *_filename;
+  PyObject *_filename;
+  PyObject *_filename_index;
   struct __pyx_obj_5pysam_6ctabix_Parser *parser;
+  PyObject *encoding;
 };
 
 
-/* "pysam/ctabix.pxd":44
- *     cdef Parser parser
+/* "pysam/ctabix.pxd":53
  * 
+ * ###########################################
  * cdef class Parser:             # <<<<<<<<<<<<<<
- *     cdef parse(self, char * buffer, int len)
+ *     cdef encoding
  * 
  */
 struct __pyx_obj_5pysam_6ctabix_Parser {
   PyObject_HEAD
   struct __pyx_vtabstruct_5pysam_6ctabix_Parser *__pyx_vtab;
+  PyObject *encoding;
 };
 
 
-/* "pysam/ctabix.pxd":47
+/* "pysam/ctabix.pxd":58
  *     cdef parse(self, char * buffer, int len)
  * 
  * cdef class asTuple(Parser):             # <<<<<<<<<<<<<<
@@ -706,7 +751,7 @@ struct __pyx_obj_5pysam_6ctabix_asTuple {
 };
 
 
-/* "pysam/ctabix.pxd":50
+/* "pysam/ctabix.pxd":61
  *     cdef parse(self, char * buffer, int len)
  * 
  * cdef class asGTF(Parser):             # <<<<<<<<<<<<<<
@@ -718,7 +763,7 @@ struct __pyx_obj_5pysam_6ctabix_asGTF {
 };
 
 
-/* "pysam/ctabix.pxd":53
+/* "pysam/ctabix.pxd":64
  *     pass
  * 
  * cdef class asBed(Parser):             # <<<<<<<<<<<<<<
@@ -730,7 +775,7 @@ struct __pyx_obj_5pysam_6ctabix_asBed {
 };
 
 
-/* "pysam/ctabix.pxd":56
+/* "pysam/ctabix.pxd":67
  *     pass
  * 
  * cdef class asVCF(Parser):             # <<<<<<<<<<<<<<
@@ -742,23 +787,24 @@ struct __pyx_obj_5pysam_6ctabix_asVCF {
 };
 
 
-/* "pysam/ctabix.pxd":59
+/* "pysam/ctabix.pxd":70
  *     pass
  * 
  * cdef class TabixIterator:             # <<<<<<<<<<<<<<
  *     cdef hts_itr_t * iterator
- *     cdef Tabixfile tabixfile
+ *     cdef TabixFile tabixfile
  */
 struct __pyx_obj_5pysam_6ctabix_TabixIterator {
   PyObject_HEAD
   struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator *__pyx_vtab;
   hts_itr_t *iterator;
-  struct __pyx_obj_5pysam_6ctabix_Tabixfile *tabixfile;
+  struct __pyx_obj_5pysam_6ctabix_TabixFile *tabixfile;
   kstring_t buffer;
+  PyObject *encoding;
 };
 
 
-/* "pysam/ctabix.pxd":65
+/* "pysam/ctabix.pxd":77
  *     cdef int __cnext__(self)
  * 
  * cdef class TabixIteratorParsed(TabixIterator):             # <<<<<<<<<<<<<<
@@ -771,7 +817,7 @@ struct __pyx_obj_5pysam_6ctabix_TabixIteratorParsed {
 };
 
 
-/* "pysam/ctabix.pxd":68
+/* "pysam/ctabix.pxd":80
  *     cdef Parser parser
  * 
  * cdef class GZIterator:             # <<<<<<<<<<<<<<
@@ -785,11 +831,12 @@ struct __pyx_obj_5pysam_6ctabix_GZIterator {
   gzFile gzipfile;
   kstream_t *kstream;
   kstring_t buffer;
+  PyObject *encoding;
 };
 
 
-/* "pysam/ctabix.pxd":75
- *     cdef int __cnext__(self)
+/* "pysam/ctabix.pxd":88
+ *     cdef encoding
  * 
  * cdef class GZIteratorHead(GZIterator):             # <<<<<<<<<<<<<<
  *     pass
@@ -800,7 +847,7 @@ struct __pyx_obj_5pysam_6ctabix_GZIteratorHead {
 };
 
 
-/* "pysam/ctabix.pxd":78
+/* "pysam/ctabix.pxd":91
  *     pass
  * 
  * cdef class GZIteratorParsed(GZIterator):             # <<<<<<<<<<<<<<
@@ -813,6 +860,18 @@ struct __pyx_obj_5pysam_6ctabix_GZIteratorParsed {
 };
 
 
+/* "pysam/ctabix.pxd":95
+ * 
+ * # Compatibility Layer for pysam < 0.8
+ * cdef class Tabixfile(TabixFile):             # <<<<<<<<<<<<<<
+ *     pass
+ * 
+ */
+struct __pyx_obj_5pysam_6ctabix_Tabixfile {
+  struct __pyx_obj_5pysam_6ctabix_TabixFile __pyx_base;
+};
+
+
 
 /* "TabProxies.pxd":41
  *   ctypedef int uint64_t
@@ -823,7 +882,8 @@ struct __pyx_obj_5pysam_6ctabix_GZIteratorParsed {
  */
 
 struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy {
-  int (*getMaxFields)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, size_t);
+  int (*getMaxFields)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *);
+  int (*getMinFields)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *);
   PyObject *(*take)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t);
   PyObject *(*present)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t);
   PyObject *(*copy)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t);
@@ -832,10 +892,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *__pyx_vtabptr_5pysam_10TabProxies_TupleProxy;
 
 
-/* "TabProxies.pxd":60
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "TabProxies.pxd":63
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class GTFProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class GTFProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -847,10 +907,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy *__pyx_vtabptr_5pysam_10TabProxies_GTFProxy;
 
 
-/* "TabProxies.pxd":69
+/* "TabProxies.pxd":73
  *     cdef char * getAttributes( self )
  * 
- * cdef class NamedTupleProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class NamedTupleProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  *     pass
  * 
  */
@@ -861,10 +921,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy *__pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy;
 
 
-/* "TabProxies.pxd":72
+/* "TabProxies.pxd":76
  *     pass
  * 
- * cdef class BedProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class BedProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -875,10 +935,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy *__pyx_vtabptr_5pysam_10TabProxies_BedProxy;
 
 
-/* "TabProxies.pxd":83
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "TabProxies.pxd":88
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class VCFProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class VCFProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -889,7 +949,7 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy *__pyx_vtabptr_5pysam_10TabProxies_VCFProxy;
 
 
-/* "pysam/ctabix.pyx":829
+/* "pysam/ctabix.pyx":923
  * 
  * 
  * cdef class tabix_file_iterator:             # <<<<<<<<<<<<<<
@@ -903,12 +963,12 @@ struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator {
 static struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator *__pyx_vtabptr_5pysam_6ctabix_tabix_file_iterator;
 
 
-/* "pysam/ctabix.pyx":86
+/* "pysam/ctabix.pyx":85
  * 
  * 
  * cdef class Parser:             # <<<<<<<<<<<<<<
  * 
- *     cdef parse(self, char * buffer, int length):
+ *     def __init__(self, encoding="ascii"):
  */
 
 struct __pyx_vtabstruct_5pysam_6ctabix_Parser {
@@ -917,8 +977,8 @@ struct __pyx_vtabstruct_5pysam_6ctabix_Parser {
 static struct __pyx_vtabstruct_5pysam_6ctabix_Parser *__pyx_vtabptr_5pysam_6ctabix_Parser;
 
 
-/* "pysam/ctabix.pyx":95
- *         return self.parse(buffer, length)
+/* "pysam/ctabix.pyx":104
+ * 
  * 
  * cdef class asTuple(Parser):             # <<<<<<<<<<<<<<
  *     '''converts a :term:`tabix row` into a python tuple.
@@ -931,8 +991,8 @@ struct __pyx_vtabstruct_5pysam_6ctabix_asTuple {
 static struct __pyx_vtabstruct_5pysam_6ctabix_asTuple *__pyx_vtabptr_5pysam_6ctabix_asTuple;
 
 
-/* "pysam/ctabix.pyx":108
- *         return r
+/* "pysam/ctabix.pyx":118
+ * 
  * 
  * cdef class asGTF(Parser):             # <<<<<<<<<<<<<<
  *     '''converts a :term:`tabix row` into a GTF record with the following
@@ -945,10 +1005,10 @@ struct __pyx_vtabstruct_5pysam_6ctabix_asGTF {
 static struct __pyx_vtabstruct_5pysam_6ctabix_asGTF *__pyx_vtabptr_5pysam_6ctabix_asGTF;
 
 
-/* "pysam/ctabix.pyx":145
- *         return r
+/* "pysam/ctabix.pyx":165
+ * 
  * 
- * cdef class asBed( Parser ):             # <<<<<<<<<<<<<<
+ * cdef class asBed(Parser):             # <<<<<<<<<<<<<<
  *     '''converts a :term:`tabix row` into a bed record
  *     with the following fields:
  */
@@ -959,10 +1019,10 @@ struct __pyx_vtabstruct_5pysam_6ctabix_asBed {
 static struct __pyx_vtabstruct_5pysam_6ctabix_asBed *__pyx_vtabptr_5pysam_6ctabix_asBed;
 
 
-/* "pysam/ctabix.pyx":185
- *         return r
+/* "pysam/ctabix.pyx":213
  * 
- * cdef class asVCF( Parser ):             # <<<<<<<<<<<<<<
+ * 
+ * cdef class asVCF(Parser):             # <<<<<<<<<<<<<<
  *     '''converts a :term:`tabix row` into a VCF record with
  *     the following fields:
  */
@@ -973,8 +1033,8 @@ struct __pyx_vtabstruct_5pysam_6ctabix_asVCF {
 static struct __pyx_vtabstruct_5pysam_6ctabix_asVCF *__pyx_vtabptr_5pysam_6ctabix_asVCF;
 
 
-/* "pysam/ctabix.pyx":428
- *             free(self._filename)
+/* "pysam/ctabix.pyx":490
+ * 
  * 
  * cdef class TabixIterator:             # <<<<<<<<<<<<<<
  *     """iterates over rows in *tabixfile* in region
@@ -987,7 +1047,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator {
 static struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator *__pyx_vtabptr_5pysam_6ctabix_TabixIterator;
 
 
-/* "pysam/ctabix.pyx":486
+/* "pysam/ctabix.pyx":568
  * 
  * 
  * cdef class TabixIteratorParsed(TabixIterator):             # <<<<<<<<<<<<<<
@@ -1001,11 +1061,11 @@ struct __pyx_vtabstruct_5pysam_6ctabix_TabixIteratorParsed {
 static struct __pyx_vtabstruct_5pysam_6ctabix_TabixIteratorParsed *__pyx_vtabptr_5pysam_6ctabix_TabixIteratorParsed;
 
 
-/* "pysam/ctabix.pyx":510
+/* "pysam/ctabix.pyx":598
  * 
  * 
  * cdef class GZIterator:             # <<<<<<<<<<<<<<
- *     def __init__(self, filename, int buffer_size=65536):
+ *     def __init__(self, filename, int buffer_size=65536, encoding="ascii"):
  *         '''iterate line-by-line through gzip (or bgzip)
  */
 
@@ -1015,7 +1075,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_GZIterator {
 static struct __pyx_vtabstruct_5pysam_6ctabix_GZIterator *__pyx_vtabptr_5pysam_6ctabix_GZIterator;
 
 
-/* "pysam/ctabix.pyx":558
+/* "pysam/ctabix.pyx":649
  * 
  * 
  * cdef class GZIteratorHead(GZIterator):             # <<<<<<<<<<<<<<
@@ -1029,7 +1089,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorHead {
 static struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorHead *__pyx_vtabptr_5pysam_6ctabix_GZIteratorHead;
 
 
-/* "pysam/ctabix.pyx":575
+/* "pysam/ctabix.pyx":666
  * 
  * 
  * cdef class GZIteratorParsed(GZIterator):             # <<<<<<<<<<<<<<
@@ -1131,24 +1191,19 @@ static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyOb
 
 static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/
 
-#include <string.h>
-
-static CYTHON_INLINE PyObject* __Pyx_decode_c_string(
-         const char* cstring, Py_ssize_t start, Py_ssize_t stop,
-         const char* encoding, const char* errors,
-         PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors));
-
-static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
-    Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/
-
 static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/
 
 static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \
     PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \
     const char* function_name); /*proto*/
 
+static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
+    Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/
+
 static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
 
+#include <string.h>
+
 static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
 
 static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
@@ -1159,27 +1214,6 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int
 #define __Pyx_PyString_Equals __Pyx_PyBytes_Equals
 #endif
 
-#define __Pyx_GetItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
-    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
-    __Pyx_GetItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound, boundscheck) : \
-    (is_list ? (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL) : \
-               __Pyx_GetItemInt_Generic(o, to_py_func(i))))
-#define __Pyx_GetItemInt_List(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
-    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
-    __Pyx_GetItemInt_List_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) : \
-    (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL))
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i,
-                                                              int wraparound, int boundscheck);
-#define __Pyx_GetItemInt_Tuple(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
-    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
-    __Pyx_GetItemInt_Tuple_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) : \
-    (PyErr_SetString(PyExc_IndexError, "tuple index out of range"), (PyObject*)NULL))
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i,
-                                                              int wraparound, int boundscheck);
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j);
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
-                                                     int is_list, int wraparound, int boundscheck);
-
 static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name); /*proto*/
 
 #if CYTHON_COMPILING_IN_CPYTHON
@@ -1358,12 +1392,12 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); /
 
 static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *);
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value);
-
 static CYTHON_INLINE int32_t __Pyx_PyInt_As_int32_t(PyObject *);
 
 static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value);
 
+static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value);
+
 static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *);
 
 static int __Pyx_check_binary_version(void);
@@ -1414,6 +1448,8 @@ static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
 
 /* Module declarations from 'libc.errno' */
 
+/* Module declarations from 'posix.unistd' */
+
 /* Module declarations from 'cpython.version' */
 
 /* Module declarations from 'cpython.ref' */
@@ -1502,7 +1538,7 @@ static PyTypeObject *__pyx_ptype_5pysam_10TabProxies_VCFProxy = 0;
 
 /* Module declarations from 'pysam.ctabix' */
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_tabix_file_iterator = 0;
-static PyTypeObject *__pyx_ptype_5pysam_6ctabix_Tabixfile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6ctabix_TabixFile = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_Parser = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_asTuple = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_asGTF = 0;
@@ -1513,41 +1549,49 @@ static PyTypeObject *__pyx_ptype_5pysam_6ctabix_TabixIteratorParsed = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_GZIterator = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_GZIteratorHead = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_GZIteratorParsed = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6ctabix_Tabixfile = 0;
 static PyObject *__pyx_v_5pysam_6ctabix__FILENAME_ENCODING = 0;
-static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *); /*proto*/
-static PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *); /*proto*/
-static PyObject *__pyx_f_5pysam_6ctabix__charptr_to_str(char *); /*proto*/
+static PyObject *__pyx_f_5pysam_6ctabix__force_str(PyObject *, struct __pyx_opt_args_5pysam_6ctabix__force_str *__pyx_optional_args); /*proto*/
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *); /*proto*/
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *, struct __pyx_opt_args_5pysam_6ctabix__force_bytes *__pyx_optional_args); /*proto*/
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_6ctabix__charptr_to_str(char *, struct __pyx_opt_args_5pysam_6ctabix__charptr_to_str *__pyx_optional_args); /*proto*/
 #define __Pyx_MODULE_NAME "pysam.ctabix"
 int __pyx_module_is_main_pysam__ctabix = 0;
 
 /* Implementation of 'pysam.ctabix' */
 static PyObject *__pyx_builtin_TypeError;
 static PyObject *__pyx_builtin_NotImplementedError;
-static PyObject *__pyx_builtin_ValueError;
 static PyObject *__pyx_builtin_IOError;
+static PyObject *__pyx_builtin_ValueError;
 static PyObject *__pyx_builtin_StopIteration;
 static PyObject *__pyx_builtin_OSError;
 static PyObject *__pyx_builtin_ord;
 static PyObject *__pyx_builtin_KeyError;
-static PyObject *__pyx_builtin_MemoryError;
-static PyObject *__pyx_pf_5pysam_6ctabix_6Parser___call__(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_length); /* proto */
-static int __pyx_pf_5pysam_6ctabix_9Tabixfile___cinit__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, PyObject *__pyx_v_parser, PyObject *__pyx_v_index, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs); /* proto */
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, PyObject *__pyx_v_index); /* proto */
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_4_isOpen(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_parser); /* proto */
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8filename___get__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6header___get__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8close(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_6ctabix_9Tabixfile_10__dealloc__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator___iter__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self); /* proto */
-static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_2__next__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self); /* proto */
-static void __pyx_pf_5pysam_6ctabix_13TabixIterator_4__dealloc__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_6ctabix_6Parser___init__(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, PyObject *__pyx_v_encoding); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_6Parser_2set_encoding(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, PyObject *__pyx_v_encoding); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_6Parser_4get_encoding(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_6Parser_6__call__(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_length); /* proto */
+static int __pyx_pf_5pysam_6ctabix_9TabixFile___cinit__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, PyObject *__pyx_v_parser, PyObject *__pyx_v_index, PyObject *__pyx_v_encoding, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_2_open(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, PyObject *__pyx_v_index); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_4_dup(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_6_isOpen(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_8fetch(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_parser, PyObject *__pyx_v_multiple_iterators); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_8filename___get__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_6header___get__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_7contigs___get__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_10close(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self); /* proto */
+static void __pyx_pf_5pysam_6ctabix_9TabixFile_12__dealloc__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self); /* proto */
+static int __pyx_pf_5pysam_6ctabix_13TabixIterator___init__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self, PyObject *__pyx_v_encoding); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_2__iter__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_4__next__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_6next(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self); /* proto */
+static void __pyx_pf_5pysam_6ctabix_13TabixIterator_8__dealloc__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_6ctabix_13EmptyIterator___iter__(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_6ctabix_13EmptyIterator_2next(CYTHON_UNUSED PyObject *__pyx_self, CYTHON_UNUSED PyObject *__pyx_v_self); /* proto */
+static PyObject *__pyx_pf_5pysam_6ctabix_13EmptyIterator_4__next__(CYTHON_UNUSED PyObject *__pyx_self, CYTHON_UNUSED PyObject *__pyx_v_self); /* proto */
 static int __pyx_pf_5pysam_6ctabix_19TabixIteratorParsed___init__(struct __pyx_obj_5pysam_6ctabix_TabixIteratorParsed *__pyx_v_self, struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_parser); /* proto */
 static PyObject *__pyx_pf_5pysam_6ctabix_19TabixIteratorParsed_2__next__(struct __pyx_obj_5pysam_6ctabix_TabixIteratorParsed *__pyx_v_self); /* proto */
-static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam_6ctabix_GZIterator *__pyx_v_self, PyObject *__pyx_v_filename, int __pyx_v_buffer_size); /* proto */
+static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam_6ctabix_GZIterator *__pyx_v_self, PyObject *__pyx_v_filename, int __pyx_v_buffer_size, PyObject *__pyx_v_encoding); /* proto */
 static void __pyx_pf_5pysam_6ctabix_10GZIterator_2__dealloc__(struct __pyx_obj_5pysam_6ctabix_GZIterator *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_6ctabix_10GZIterator_4__iter__(struct __pyx_obj_5pysam_6ctabix_GZIterator *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_6ctabix_10GZIterator_6__next__(struct __pyx_obj_5pysam_6ctabix_GZIterator *__pyx_v_self); /* proto */
@@ -1567,7 +1611,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_4__next__(CYTH
 static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_6next(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self); /* proto */
 static PyObject *__pyx_pf_5pysam_6ctabix_4tabix_iterator(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_infile, PyObject *__pyx_v_parser); /* proto */
 static PyObject *__pyx_tp_new_5pysam_6ctabix_tabix_file_iterator(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
-static PyObject *__pyx_tp_new_5pysam_6ctabix_Tabixfile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_6ctabix_TabixFile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_6ctabix_Parser(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_6ctabix_asTuple(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_6ctabix_asGTF(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
@@ -1578,6 +1622,7 @@ static PyObject *__pyx_tp_new_5pysam_6ctabix_TabixIteratorParsed(PyTypeObject *t
 static PyObject *__pyx_tp_new_5pysam_6ctabix_GZIterator(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_6ctabix_GZIteratorHead(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static PyObject *__pyx_tp_new_5pysam_6ctabix_GZIteratorParsed(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
+static PyObject *__pyx_tp_new_5pysam_6ctabix_Tabixfile(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
 static char __pyx_k_b[] = "b";
 static char __pyx_k_c[] = "c";
 static char __pyx_k_r[] = "r";
@@ -1587,12 +1632,13 @@ static char __pyx_k_fn[] = "fn";
 static char __pyx_k_fp[] = "fp";
 static char __pyx_k_gz[] = ".gz";
 static char __pyx_k_os[] = "os";
-static char __pyx_k__12[] = "#";
-static char __pyx_k__16[] = ",";
+static char __pyx_k__10[] = "#";
+static char __pyx_k__14[] = ",";
 static char __pyx_k_all[] = "__all__";
 static char __pyx_k_bed[] = "bed";
 static char __pyx_k_cpy[] = "cpy";
 static char __pyx_k_doc[] = "__doc__";
+static char __pyx_k_dup[] = "_dup";
 static char __pyx_k_end[] = "end";
 static char __pyx_k_ftp[] = "ftp:";
 static char __pyx_k_gff[] = "gff";
@@ -1654,12 +1700,15 @@ static char __pyx_k_prepare[] = "__prepare__";
 static char __pyx_k_seq_col[] = "seq_col";
 static char __pyx_k_KeyError[] = "KeyError";
 static char __pyx_k_O_RDONLY[] = "O_RDONLY";
+static char __pyx_k_encoding[] = "encoding";
 static char __pyx_k_endswith[] = "endswith";
 static char __pyx_k_filename[] = "filename";
 static char __pyx_k_qualname[] = "__qualname__";
 static char __pyx_k_readline[] = "readline";
+static char __pyx_k_TabixFile[] = "TabixFile";
 static char __pyx_k_Tabixfile[] = "Tabixfile";
 static char __pyx_k_TypeError[] = "TypeError";
+static char __pyx_k_bytes_cpy[] = "bytes_cpy";
 static char __pyx_k_conf_data[] = "conf_data";
 static char __pyx_k_meta_char[] = "meta_char";
 static char __pyx_k_metaclass[] = "__metaclass__";
@@ -1671,14 +1720,15 @@ static char __pyx_k_GZIterator[] = "GZIterator";
 static char __pyx_k_ValueError[] = "ValueError";
 static char __pyx_k_pyx_vtable[] = "__pyx_vtable__";
 static char __pyx_k_startswith[] = "startswith";
-static char __pyx_k_MemoryError[] = "MemoryError";
 static char __pyx_k_WINDOW_SIZE[] = "WINDOW_SIZE";
 static char __pyx_k_buffer_size[] = "buffer_size";
 static char __pyx_k_filename_in[] = "filename_in";
 static char __pyx_k_preset2conf[] = "preset2conf";
 static char __pyx_k_tabix_index[] = "tabix_index";
 static char __pyx_k_filename_out[] = "filename_out";
+static char __pyx_k_get_encoding[] = "get_encoding";
 static char __pyx_k_pysam_ctabix[] = "pysam.ctabix";
+static char __pyx_k_set_encoding[] = "set_encoding";
 static char __pyx_k_EmptyIterator[] = "EmptyIterator";
 static char __pyx_k_StopIteration[] = "StopIteration";
 static char __pyx_k_error_d_s_d_s[] = "error (%d): %s (%d: %s)";
@@ -1686,22 +1736,24 @@ static char __pyx_k_start_i_end_i[] = "start (%i) > end (%i)";
 static char __pyx_k_GZIteratorHead[] = "GZIteratorHead";
 static char __pyx_k_No_such_file_s[] = "No such file '%s'";
 static char __pyx_k_empty_iterator[] = "empty iterator";
-static char __pyx_k_invalid_mode_s[] = "invalid mode '%s'";
 static char __pyx_k_tabix_compress[] = "tabix_compress";
 static char __pyx_k_tabix_iterator[] = "tabix_iterator";
 static char __pyx_k_writing_failed[] = "writing failed";
-static char __pyx_k_chromosome_names[] = "chromosome names";
 static char __pyx_k_file_s_not_found[] = "file `%s` not found";
 static char __pyx_k_index_s_not_found[] = "index `%s` not found";
 static char __pyx_k_EmptyIterator_next[] = "EmptyIterator.next";
 static char __pyx_k_getdefaultencoding[] = "getdefaultencoding";
+static char __pyx_k_multiple_iterators[] = "multiple_iterators";
 static char __pyx_k_NotImplementedError[] = "NotImplementedError";
 static char __pyx_k_tabix_file_iterator[] = "tabix_file_iterator";
 static char __pyx_k_EmptyIterator___iter[] = "EmptyIterator.__iter__";
+static char __pyx_k_EmptyIterator___next[] = "EmptyIterator.__next__";
 static char __pyx_k_incomplete_line_at_s[] = "incomplete line at %s";
 static char __pyx_k_could_not_open_file_s[] = "could not open file `%s`";
 static char __pyx_k_getfilesystemencoding[] = "getfilesystemencoding";
 static char __pyx_k_tabix_generic_iterator[] = "tabix_generic_iterator";
+static char __pyx_k_iteration_on_closed_file[] = "iteration on closed file";
+static char __pyx_k_list_of_chromosome_names[] = "list of chromosome names";
 static char __pyx_k_writing_to_file_s_failed[] = "writing to file %s failed";
 static char __pyx_k_error_when_closing_file_s[] = "error when closing file %s";
 static char __pyx_k_could_not_open_index_for_s[] = "could not open index for `%s`";
@@ -1717,7 +1769,7 @@ static char __pyx_k_tabix_generic_iterator___next[] = "tabix_generic_iterator.__
 static char __pyx_k_I_O_operation_on_closed_file_2[] = "I/O operation on closed file.";
 static char __pyx_k_home_andreas_devel_pysam_pysam[] = "/home/andreas/devel/pysam/pysam/ctabix.pyx";
 static char __pyx_k_iterate_over_infile_Permits_the[] = "iterate over ``infile``.\n    \n    Permits the use of file-like objects for example from the gzip module.\n    ";
-static char __pyx_k_the_file_header_note_The_header[] = "the file header.\n          \n        .. note::\n            The header is returned as an iterator over lines without the\n            newline character.\n        ";
+static char __pyx_k_the_file_header_note_The_header[] = "the file header.\n          \n        .. note::\n            The header is returned as an iterator presenting lines without the\n            newline character.\n        ";
 static char __pyx_k_Argument_must_be_string_bytes_or[] = "Argument must be string, bytes or unicode.";
 static char __pyx_k_Argument_must_be_string_or_unico[] = "Argument must be string or unicode.";
 static char __pyx_k_Filename_s_already_exists_use_fo[] = "Filename '%s' already exists, use *force* to overwrite";
@@ -1727,11 +1779,11 @@ static char __pyx_k_filename_associated_with_this_ob[] = "filename associated wi
 static char __pyx_k_neither_preset_nor_seq_col_start[] = "neither preset nor seq_col,start_col and end_col given";
 static char __pyx_k_parse_method_of_s_not_implemente[] = "parse method of %s not implemented";
 static char __pyx_k_unknown_preset_s_valid_presets_a[] = "unknown preset '%s', valid presets are '%s'";
-static char __pyx_k_writing_to_tabix_files_not_imple[] = "writing to tabix files not implemented";
 static PyObject *__pyx_kp_u_Argument_must_be_string_bytes_or;
 static PyObject *__pyx_kp_u_Argument_must_be_string_or_unico;
 static PyObject *__pyx_n_s_EmptyIterator;
 static PyObject *__pyx_n_s_EmptyIterator___iter;
+static PyObject *__pyx_n_s_EmptyIterator___next;
 static PyObject *__pyx_n_s_EmptyIterator_next;
 static PyObject *__pyx_kp_s_Filename_s_already_exists_use_fo;
 static PyObject *__pyx_kp_s_Filename_s_tbi_already_exists_us;
@@ -1741,7 +1793,6 @@ static PyObject *__pyx_n_s_IOError;
 static PyObject *__pyx_kp_s_I_O_operation_on_closed_file;
 static PyObject *__pyx_kp_s_I_O_operation_on_closed_file_2;
 static PyObject *__pyx_n_s_KeyError;
-static PyObject *__pyx_n_s_MemoryError;
 static PyObject *__pyx_kp_s_No_such_file_or_directory_s;
 static PyObject *__pyx_kp_s_No_such_file_s;
 static PyObject *__pyx_n_s_NotImplementedError;
@@ -1749,12 +1800,13 @@ static PyObject *__pyx_n_s_OSError;
 static PyObject *__pyx_n_s_O_RDONLY;
 static PyObject *__pyx_n_s_PYTHON3;
 static PyObject *__pyx_n_s_StopIteration;
+static PyObject *__pyx_n_s_TabixFile;
 static PyObject *__pyx_n_s_Tabixfile;
 static PyObject *__pyx_n_s_TypeError;
 static PyObject *__pyx_n_s_ValueError;
 static PyObject *__pyx_n_s_WINDOW_SIZE;
-static PyObject *__pyx_kp_s__12;
-static PyObject *__pyx_kp_s__16;
+static PyObject *__pyx_kp_s__10;
+static PyObject *__pyx_kp_s__14;
 static PyObject *__pyx_n_s_all;
 static PyObject *__pyx_n_s_asBed;
 static PyObject *__pyx_n_s_asGTF;
@@ -1765,6 +1817,7 @@ static PyObject *__pyx_n_s_b;
 static PyObject *__pyx_n_s_bed;
 static PyObject *__pyx_n_s_buffer;
 static PyObject *__pyx_n_s_buffer_size;
+static PyObject *__pyx_n_s_bytes_cpy;
 static PyObject *__pyx_n_s_c;
 static PyObject *__pyx_n_s_close;
 static PyObject *__pyx_n_s_closed;
@@ -1778,8 +1831,10 @@ static PyObject *__pyx_kp_s_could_not_open_s_for_writing;
 static PyObject *__pyx_n_s_cpy;
 static PyObject *__pyx_n_s_decode;
 static PyObject *__pyx_n_s_doc;
+static PyObject *__pyx_n_s_dup;
 static PyObject *__pyx_kp_s_empty_iterator;
 static PyObject *__pyx_n_s_encode;
+static PyObject *__pyx_n_s_encoding;
 static PyObject *__pyx_n_s_end;
 static PyObject *__pyx_n_s_end_col;
 static PyObject *__pyx_n_s_endswith;
@@ -1795,6 +1850,7 @@ static PyObject *__pyx_n_s_fn;
 static PyObject *__pyx_n_s_force;
 static PyObject *__pyx_n_s_fp;
 static PyObject *__pyx_kp_s_ftp;
+static PyObject *__pyx_n_s_get_encoding;
 static PyObject *__pyx_n_s_getdefaultencoding;
 static PyObject *__pyx_n_s_getfilesystemencoding;
 static PyObject *__pyx_n_s_gff;
@@ -1808,10 +1864,10 @@ static PyObject *__pyx_kp_s_index_s_not_found;
 static PyObject *__pyx_n_s_infile;
 static PyObject *__pyx_n_s_init;
 static PyObject *__pyx_kp_s_invalid_file_opening_mode_s;
-static PyObject *__pyx_kp_s_invalid_mode_s;
 static PyObject *__pyx_n_s_isOpen;
 static PyObject *__pyx_n_s_iter;
 static PyObject *__pyx_kp_s_iterate_over_infile_Permits_the;
+static PyObject *__pyx_kp_s_iteration_on_closed_file;
 static PyObject *__pyx_n_s_join;
 static PyObject *__pyx_n_s_keys;
 static PyObject *__pyx_n_s_length;
@@ -1822,6 +1878,7 @@ static PyObject *__pyx_n_s_metaclass;
 static PyObject *__pyx_n_s_min_shift;
 static PyObject *__pyx_n_s_mode;
 static PyObject *__pyx_n_s_module;
+static PyObject *__pyx_n_s_multiple_iterators;
 static PyObject *__pyx_n_s_nbytes;
 static PyObject *__pyx_kp_s_neither_preset_nor_seq_col_start;
 static PyObject *__pyx_n_s_next;
@@ -1851,6 +1908,7 @@ static PyObject *__pyx_kp_s_s_i_i;
 static PyObject *__pyx_n_s_sam;
 static PyObject *__pyx_n_s_self;
 static PyObject *__pyx_n_s_seq_col;
+static PyObject *__pyx_n_s_set_encoding;
 static PyObject *__pyx_n_s_start;
 static PyObject *__pyx_n_s_start_col;
 static PyObject *__pyx_kp_s_start_i_end_i;
@@ -1870,10 +1928,8 @@ static PyObject *__pyx_n_s_test;
 static PyObject *__pyx_kp_s_unknown_preset_s_valid_presets_a;
 static PyObject *__pyx_n_s_unlink;
 static PyObject *__pyx_n_s_vcf;
-static PyObject *__pyx_n_s_w;
 static PyObject *__pyx_kp_s_writing_failed;
 static PyObject *__pyx_kp_s_writing_to_file_s_failed;
-static PyObject *__pyx_kp_s_writing_to_tabix_files_not_imple;
 static PyObject *__pyx_n_s_zerobased;
 static PyObject *__pyx_int_0;
 static PyObject *__pyx_int_1;
@@ -1897,15 +1953,14 @@ static PyObject *__pyx_tuple__6;
 static PyObject *__pyx_tuple__7;
 static PyObject *__pyx_tuple__8;
 static PyObject *__pyx_tuple__9;
-static PyObject *__pyx_tuple__10;
 static PyObject *__pyx_tuple__11;
+static PyObject *__pyx_tuple__12;
 static PyObject *__pyx_tuple__13;
-static PyObject *__pyx_tuple__14;
 static PyObject *__pyx_tuple__15;
+static PyObject *__pyx_tuple__16;
 static PyObject *__pyx_tuple__17;
 static PyObject *__pyx_tuple__18;
 static PyObject *__pyx_tuple__19;
-static PyObject *__pyx_tuple__20;
 static PyObject *__pyx_tuple__21;
 static PyObject *__pyx_tuple__23;
 static PyObject *__pyx_tuple__25;
@@ -1915,6 +1970,7 @@ static PyObject *__pyx_tuple__31;
 static PyObject *__pyx_tuple__33;
 static PyObject *__pyx_tuple__35;
 static PyObject *__pyx_tuple__37;
+static PyObject *__pyx_codeobj__20;
 static PyObject *__pyx_codeobj__22;
 static PyObject *__pyx_codeobj__24;
 static PyObject *__pyx_codeobj__26;
@@ -1925,15 +1981,15 @@ static PyObject *__pyx_codeobj__34;
 static PyObject *__pyx_codeobj__36;
 static PyObject *__pyx_codeobj__38;
 
-/* "pysam/ctabix.pyx":38
+/* "pysam/ctabix.pyx":39
  * #_C_FILENAME_ENCODING = <char*>_FILENAME_ENCODING
  * 
- * cdef bytes _encodeFilename(object filename):             # <<<<<<<<<<<<<<
+ * cdef inline bytes _encodeFilename(object filename):             # <<<<<<<<<<<<<<
  *     u"""Make sure a filename is 8-bit encoded (or None).
  *     """
  */
 
-static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filename) {
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filename) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
@@ -1946,7 +2002,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filena
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_encodeFilename", 0);
 
-  /* "pysam/ctabix.pyx":41
+  /* "pysam/ctabix.pyx":42
  *     u"""Make sure a filename is 8-bit encoded (or None).
  *     """
  *     if filename is None:             # <<<<<<<<<<<<<<
@@ -1957,7 +2013,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filena
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":42
+    /* "pysam/ctabix.pyx":43
  *     """
  *     if filename is None:
  *         return None             # <<<<<<<<<<<<<<
@@ -1970,7 +2026,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filena
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":43
+  /* "pysam/ctabix.pyx":44
  *     if filename is None:
  *         return None
  *     elif PyBytes_Check(filename):             # <<<<<<<<<<<<<<
@@ -1980,7 +2036,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filena
   __pyx_t_2 = (PyBytes_Check(__pyx_v_filename) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":44
+    /* "pysam/ctabix.pyx":45
  *         return None
  *     elif PyBytes_Check(filename):
  *         return filename             # <<<<<<<<<<<<<<
@@ -1988,13 +2044,13 @@ static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filena
  *         return filename.encode(_FILENAME_ENCODING)
  */
     __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_filename))||((__pyx_v_filename) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_filename)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_v_filename))||((__pyx_v_filename) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_filename)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_INCREF(__pyx_v_filename);
     __pyx_r = ((PyObject*)__pyx_v_filename);
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":45
+  /* "pysam/ctabix.pyx":46
  *     elif PyBytes_Check(filename):
  *         return filename
  *     elif PyUnicode_Check(filename):             # <<<<<<<<<<<<<<
@@ -2004,7 +2060,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filena
   __pyx_t_2 = (PyUnicode_Check(__pyx_v_filename) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":46
+    /* "pysam/ctabix.pyx":47
  *         return filename
  *     elif PyUnicode_Check(filename):
  *         return filename.encode(_FILENAME_ENCODING)             # <<<<<<<<<<<<<<
@@ -2012,39 +2068,39 @@ static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filena
  *         raise TypeError, u"Argument must be string or unicode."
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_INCREF(__pyx_v_5pysam_6ctabix__FILENAME_ENCODING);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_5pysam_6ctabix__FILENAME_ENCODING);
     __Pyx_GIVEREF(__pyx_v_5pysam_6ctabix__FILENAME_ENCODING);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    if (!(likely(PyBytes_CheckExact(__pyx_t_5))||((__pyx_t_5) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_5)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_t_5))||((__pyx_t_5) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_5)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_r = ((PyObject*)__pyx_t_5);
     __pyx_t_5 = 0;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/ctabix.pyx":48
+    /* "pysam/ctabix.pyx":49
  *         return filename.encode(_FILENAME_ENCODING)
  *     else:
  *         raise TypeError, u"Argument must be string or unicode."             # <<<<<<<<<<<<<<
  * 
- * cdef bytes _force_bytes(object s):
+ * cdef inline bytes _force_bytes(object s, encoding="ascii"):
  */
     __Pyx_Raise(__pyx_builtin_TypeError, __pyx_kp_u_Argument_must_be_string_or_unico, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":38
+  /* "pysam/ctabix.pyx":39
  * #_C_FILENAME_ENCODING = <char*>_FILENAME_ENCODING
  * 
- * cdef bytes _encodeFilename(object filename):             # <<<<<<<<<<<<<<
+ * cdef inline bytes _encodeFilename(object filename):             # <<<<<<<<<<<<<<
  *     u"""Make sure a filename is 8-bit encoded (or None).
  *     """
  */
@@ -2062,27 +2118,34 @@ static PyObject *__pyx_f_5pysam_6ctabix__encodeFilename(PyObject *__pyx_v_filena
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":50
+/* "pysam/ctabix.pyx":51
  *         raise TypeError, u"Argument must be string or unicode."
  * 
- * cdef bytes _force_bytes(object s):             # <<<<<<<<<<<<<<
+ * cdef inline bytes _force_bytes(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
  *     u"""convert string or unicode object to bytes, assuming ascii encoding.
  *     """
  */
 
-static PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *__pyx_v_s) {
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *__pyx_v_s, struct __pyx_opt_args_5pysam_6ctabix__force_bytes *__pyx_optional_args) {
+  PyObject *__pyx_v_encoding = ((PyObject *)__pyx_n_s_ascii);
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   int __pyx_t_2;
   PyObject *__pyx_t_3 = NULL;
   PyObject *__pyx_t_4 = NULL;
+  PyObject *__pyx_t_5 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_force_bytes", 0);
+  if (__pyx_optional_args) {
+    if (__pyx_optional_args->__pyx_n > 0) {
+      __pyx_v_encoding = __pyx_optional_args->encoding;
+    }
+  }
 
-  /* "pysam/ctabix.pyx":53
+  /* "pysam/ctabix.pyx":54
  *     u"""convert string or unicode object to bytes, assuming ascii encoding.
  *     """
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
@@ -2092,7 +2155,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *__pyx_v_s) {
   __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":54
+    /* "pysam/ctabix.pyx":55
  *     """
  *     if PY_MAJOR_VERSION < 3:
  *         return s             # <<<<<<<<<<<<<<
@@ -2100,13 +2163,13 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *__pyx_v_s) {
  *         return None
  */
     __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_INCREF(__pyx_v_s);
     __pyx_r = ((PyObject*)__pyx_v_s);
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":55
+  /* "pysam/ctabix.pyx":56
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  *     elif s is None:             # <<<<<<<<<<<<<<
@@ -2117,7 +2180,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *__pyx_v_s) {
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":56
+    /* "pysam/ctabix.pyx":57
  *         return s
  *     elif s is None:
  *         return None             # <<<<<<<<<<<<<<
@@ -2130,7 +2193,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":57
+  /* "pysam/ctabix.pyx":58
  *     elif s is None:
  *         return None
  *     elif PyBytes_Check(s):             # <<<<<<<<<<<<<<
@@ -2140,65 +2203,71 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *__pyx_v_s) {
   __pyx_t_2 = (PyBytes_Check(__pyx_v_s) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":58
+    /* "pysam/ctabix.pyx":59
  *         return None
  *     elif PyBytes_Check(s):
  *         return s             # <<<<<<<<<<<<<<
  *     elif PyUnicode_Check(s):
- *         return s.encode('ascii')
+ *         return s.encode(encoding)
  */
     __Pyx_XDECREF(__pyx_r);
-    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyBytes_CheckExact(__pyx_v_s))||((__pyx_v_s) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_v_s)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_INCREF(__pyx_v_s);
     __pyx_r = ((PyObject*)__pyx_v_s);
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":59
+  /* "pysam/ctabix.pyx":60
  *     elif PyBytes_Check(s):
  *         return s
  *     elif PyUnicode_Check(s):             # <<<<<<<<<<<<<<
- *         return s.encode('ascii')
+ *         return s.encode(encoding)
  *     else:
  */
   __pyx_t_2 = (PyUnicode_Check(__pyx_v_s) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":60
+    /* "pysam/ctabix.pyx":61
  *         return s
  *     elif PyUnicode_Check(s):
- *         return s.encode('ascii')             # <<<<<<<<<<<<<<
+ *         return s.encode(encoding)             # <<<<<<<<<<<<<<
  *     else:
  *         raise TypeError, u"Argument must be string, bytes or unicode."
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple_, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_INCREF(__pyx_v_encoding);
+    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_encoding);
+    __Pyx_GIVEREF(__pyx_v_encoding);
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    if (!(likely(PyBytes_CheckExact(__pyx_t_4))||((__pyx_t_4) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_4)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_r = ((PyObject*)__pyx_t_4);
-    __pyx_t_4 = 0;
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    if (!(likely(PyBytes_CheckExact(__pyx_t_5))||((__pyx_t_5) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_5)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_r = ((PyObject*)__pyx_t_5);
+    __pyx_t_5 = 0;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/ctabix.pyx":62
- *         return s.encode('ascii')
+    /* "pysam/ctabix.pyx":63
+ *         return s.encode(encoding)
  *     else:
  *         raise TypeError, u"Argument must be string, bytes or unicode."             # <<<<<<<<<<<<<<
  * 
- * cdef inline bytes _force_cmdline_bytes(object s):
+ * cdef inline _charptr_to_str(char* s, encoding="ascii"):
  */
     __Pyx_Raise(__pyx_builtin_TypeError, __pyx_kp_u_Argument_must_be_string_bytes_or, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":50
+  /* "pysam/ctabix.pyx":51
  *         raise TypeError, u"Argument must be string or unicode."
  * 
- * cdef bytes _force_bytes(object s):             # <<<<<<<<<<<<<<
+ * cdef inline bytes _force_bytes(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
  *     u"""convert string or unicode object to bytes, assuming ascii encoding.
  *     """
  */
@@ -2207,6 +2276,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *__pyx_v_s) {
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_5);
   __Pyx_AddTraceback("pysam.ctabix._force_bytes", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -2215,77 +2285,35 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_bytes(PyObject *__pyx_v_s) {
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":64
- *         raise TypeError, u"Argument must be string, bytes or unicode."
- * 
- * cdef inline bytes _force_cmdline_bytes(object s):             # <<<<<<<<<<<<<<
- *     return _force_bytes(s)
- * 
- */
-
-static CYTHON_INLINE PyObject *__pyx_f_5pysam_6ctabix__force_cmdline_bytes(PyObject *__pyx_v_s) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_RefNannySetupContext("_force_cmdline_bytes", 0);
-
-  /* "pysam/ctabix.pyx":65
- * 
- * cdef inline bytes _force_cmdline_bytes(object s):
- *     return _force_bytes(s)             # <<<<<<<<<<<<<<
- * 
- * cdef _charptr_to_str(char* s):
- */
-  __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __pyx_f_5pysam_6ctabix__force_bytes(__pyx_v_s); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_r = ((PyObject*)__pyx_t_1);
-  __pyx_t_1 = 0;
-  goto __pyx_L0;
-
-  /* "pysam/ctabix.pyx":64
+/* "pysam/ctabix.pyx":65
  *         raise TypeError, u"Argument must be string, bytes or unicode."
  * 
- * cdef inline bytes _force_cmdline_bytes(object s):             # <<<<<<<<<<<<<<
- *     return _force_bytes(s)
- * 
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.ctabix._force_cmdline_bytes", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __pyx_L0:;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/ctabix.pyx":67
- *     return _force_bytes(s)
- * 
- * cdef _charptr_to_str(char* s):             # <<<<<<<<<<<<<<
+ * cdef inline _charptr_to_str(char* s, encoding="ascii"):             # <<<<<<<<<<<<<<
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  */
 
-static PyObject *__pyx_f_5pysam_6ctabix__charptr_to_str(char *__pyx_v_s) {
+static CYTHON_INLINE PyObject *__pyx_f_5pysam_6ctabix__charptr_to_str(char *__pyx_v_s, struct __pyx_opt_args_5pysam_6ctabix__charptr_to_str *__pyx_optional_args) {
+  PyObject *__pyx_v_encoding = ((PyObject *)__pyx_n_s_ascii);
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_charptr_to_str", 0);
+  if (__pyx_optional_args) {
+    if (__pyx_optional_args->__pyx_n > 0) {
+      __pyx_v_encoding = __pyx_optional_args->encoding;
+    }
+  }
 
-  /* "pysam/ctabix.pyx":68
+  /* "pysam/ctabix.pyx":66
  * 
- * cdef _charptr_to_str(char* s):
+ * cdef inline _charptr_to_str(char* s, encoding="ascii"):
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
  *         return s
  *     else:
@@ -2293,15 +2321,15 @@ static PyObject *__pyx_f_5pysam_6ctabix__charptr_to_str(char *__pyx_v_s) {
   __pyx_t_1 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":69
- * cdef _charptr_to_str(char* s):
+    /* "pysam/ctabix.pyx":67
+ * cdef inline _charptr_to_str(char* s, encoding="ascii"):
  *     if PY_MAJOR_VERSION < 3:
  *         return s             # <<<<<<<<<<<<<<
  *     else:
- *         return s.decode("ascii")
+ *         return s.decode(encoding)
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 67; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -2309,25 +2337,37 @@ static PyObject *__pyx_f_5pysam_6ctabix__charptr_to_str(char *__pyx_v_s) {
   }
   /*else*/ {
 
-    /* "pysam/ctabix.pyx":71
+    /* "pysam/ctabix.pyx":69
  *         return s
  *     else:
- *         return s.decode("ascii")             # <<<<<<<<<<<<<<
+ *         return s.decode(encoding)             # <<<<<<<<<<<<<<
  * 
- * cdef _force_str(object s):
+ * cdef _force_str(object s, encoding="ascii"):
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_s, 0, strlen(__pyx_v_s), NULL, NULL, PyUnicode_DecodeASCII); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_r = __pyx_t_2;
-    __pyx_t_2 = 0;
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_decode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_INCREF(__pyx_v_encoding);
+    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_encoding);
+    __Pyx_GIVEREF(__pyx_v_encoding);
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __pyx_r = __pyx_t_4;
+    __pyx_t_4 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":67
- *     return _force_bytes(s)
+  /* "pysam/ctabix.pyx":65
+ *         raise TypeError, u"Argument must be string, bytes or unicode."
  * 
- * cdef _charptr_to_str(char* s):             # <<<<<<<<<<<<<<
+ * cdef inline _charptr_to_str(char* s, encoding="ascii"):             # <<<<<<<<<<<<<<
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  */
@@ -2335,6 +2375,8 @@ static PyObject *__pyx_f_5pysam_6ctabix__charptr_to_str(char *__pyx_v_s) {
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
   __Pyx_AddTraceback("pysam.ctabix._charptr_to_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -2343,29 +2385,36 @@ static PyObject *__pyx_f_5pysam_6ctabix__charptr_to_str(char *__pyx_v_s) {
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":73
- *         return s.decode("ascii")
+/* "pysam/ctabix.pyx":71
+ *         return s.decode(encoding)
  * 
- * cdef _force_str(object s):             # <<<<<<<<<<<<<<
- *     """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
- *     if s is None:
+ * cdef _force_str(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *     """Return s converted to str type of current Python
+ *     (bytes in Py2, unicode in Py3)"""
  */
 
-static PyObject *__pyx_f_5pysam_6ctabix__force_str(PyObject *__pyx_v_s) {
+static PyObject *__pyx_f_5pysam_6ctabix__force_str(PyObject *__pyx_v_s, struct __pyx_opt_args_5pysam_6ctabix__force_str *__pyx_optional_args) {
+  PyObject *__pyx_v_encoding = ((PyObject *)__pyx_n_s_ascii);
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   int __pyx_t_2;
   PyObject *__pyx_t_3 = NULL;
   PyObject *__pyx_t_4 = NULL;
+  PyObject *__pyx_t_5 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_force_str", 0);
+  if (__pyx_optional_args) {
+    if (__pyx_optional_args->__pyx_n > 0) {
+      __pyx_v_encoding = __pyx_optional_args->encoding;
+    }
+  }
 
-  /* "pysam/ctabix.pyx":75
- * cdef _force_str(object s):
- *     """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
+  /* "pysam/ctabix.pyx":74
+ *     """Return s converted to str type of current Python
+ *     (bytes in Py2, unicode in Py3)"""
  *     if s is None:             # <<<<<<<<<<<<<<
  *         return None
  *     if PY_MAJOR_VERSION < 3:
@@ -2374,8 +2423,8 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_str(PyObject *__pyx_v_s) {
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":76
- *     """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
+    /* "pysam/ctabix.pyx":75
+ *     (bytes in Py2, unicode in Py3)"""
  *     if s is None:
  *         return None             # <<<<<<<<<<<<<<
  *     if PY_MAJOR_VERSION < 3:
@@ -2387,7 +2436,7 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_str(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":77
+  /* "pysam/ctabix.pyx":76
  *     if s is None:
  *         return None
  *     if PY_MAJOR_VERSION < 3:             # <<<<<<<<<<<<<<
@@ -2397,12 +2446,12 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_str(PyObject *__pyx_v_s) {
   __pyx_t_2 = ((PY_MAJOR_VERSION < 3) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":78
+    /* "pysam/ctabix.pyx":77
  *         return None
  *     if PY_MAJOR_VERSION < 3:
  *         return s             # <<<<<<<<<<<<<<
  *     elif PyBytes_Check(s):
- *         return s.decode('ascii')
+ *         return s.decode(encoding)
  */
     __Pyx_XDECREF(__pyx_r);
     __Pyx_INCREF(__pyx_v_s);
@@ -2410,36 +2459,42 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_str(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":79
+  /* "pysam/ctabix.pyx":78
  *     if PY_MAJOR_VERSION < 3:
  *         return s
  *     elif PyBytes_Check(s):             # <<<<<<<<<<<<<<
- *         return s.decode('ascii')
+ *         return s.decode(encoding)
  *     else:
  */
   __pyx_t_2 = (PyBytes_Check(__pyx_v_s) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":80
+    /* "pysam/ctabix.pyx":79
  *         return s
  *     elif PyBytes_Check(s):
- *         return s.decode('ascii')             # <<<<<<<<<<<<<<
+ *         return s.decode(encoding)             # <<<<<<<<<<<<<<
  *     else:
  *         # assume unicode
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_decode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_decode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_INCREF(__pyx_v_encoding);
+    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_encoding);
+    __Pyx_GIVEREF(__pyx_v_encoding);
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_r = __pyx_t_4;
-    __pyx_t_4 = 0;
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    __pyx_r = __pyx_t_5;
+    __pyx_t_5 = 0;
     goto __pyx_L0;
   }
   /*else*/ {
 
-    /* "pysam/ctabix.pyx":83
+    /* "pysam/ctabix.pyx":82
  *     else:
  *         # assume unicode
  *         return s             # <<<<<<<<<<<<<<
@@ -2452,18 +2507,19 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_str(PyObject *__pyx_v_s) {
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":73
- *         return s.decode("ascii")
+  /* "pysam/ctabix.pyx":71
+ *         return s.decode(encoding)
  * 
- * cdef _force_str(object s):             # <<<<<<<<<<<<<<
- *     """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
- *     if s is None:
+ * cdef _force_str(object s, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *     """Return s converted to str type of current Python
+ *     (bytes in Py2, unicode in Py3)"""
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_5);
   __Pyx_AddTraceback("pysam.ctabix._force_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -2472,108 +2528,32 @@ static PyObject *__pyx_f_5pysam_6ctabix__force_str(PyObject *__pyx_v_s) {
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":88
- * cdef class Parser:
- * 
- *     cdef parse(self, char * buffer, int length):             # <<<<<<<<<<<<<<
- *         raise NotImplementedError(
- *             'parse method of %s not implemented' % str(self))
- */
-
-static PyObject *__pyx_f_5pysam_6ctabix_6Parser_parse(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, CYTHON_UNUSED char *__pyx_v_buffer, CYTHON_UNUSED int __pyx_v_length) {
-  PyObject *__pyx_r = NULL;
-  __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  PyObject *__pyx_t_2 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_RefNannySetupContext("parse", 0);
-
-  /* "pysam/ctabix.pyx":90
- *     cdef parse(self, char * buffer, int length):
- *         raise NotImplementedError(
- *             'parse method of %s not implemented' % str(self))             # <<<<<<<<<<<<<<
- * 
- *     def __call__(self, char * buffer, int length):
- */
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_self));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_parse_method_of_s_not_implemente, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-
-  /* "pysam/ctabix.pyx":89
- * 
- *     cdef parse(self, char * buffer, int length):
- *         raise NotImplementedError(             # <<<<<<<<<<<<<<
- *             'parse method of %s not implemented' % str(self))
- * 
- */
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 89; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
-  __Pyx_GIVEREF(__pyx_t_1);
-  __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 89; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __Pyx_Raise(__pyx_t_1, 0, 0, 0);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 89; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-
-  /* "pysam/ctabix.pyx":88
+/* "pysam/ctabix.pyx":87
  * cdef class Parser:
  * 
- *     cdef parse(self, char * buffer, int length):             # <<<<<<<<<<<<<<
- *         raise NotImplementedError(
- *             'parse method of %s not implemented' % str(self))
- */
-
-  /* function exit code */
-  __pyx_L1_error:;
-  __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.ctabix.Parser.parse", __pyx_clineno, __pyx_lineno, __pyx_filename);
-  __pyx_r = 0;
-  __Pyx_XGIVEREF(__pyx_r);
-  __Pyx_RefNannyFinishContext();
-  return __pyx_r;
-}
-
-/* "pysam/ctabix.pyx":92
- *             'parse method of %s not implemented' % str(self))
- * 
- *     def __call__(self, char * buffer, int length):             # <<<<<<<<<<<<<<
- *         return self.parse(buffer, length)
+ *     def __init__(self, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *         self.encoding = encoding
  * 
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_6Parser_1__call__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static PyObject *__pyx_pw_5pysam_6ctabix_6Parser_1__call__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
-  char *__pyx_v_buffer;
-  int __pyx_v_length;
+static int __pyx_pw_5pysam_6ctabix_6Parser_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_6ctabix_6Parser_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  PyObject *__pyx_v_encoding = 0;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
-  PyObject *__pyx_r = 0;
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
-  __Pyx_RefNannySetupContext("__call__ (wrapper)", 0);
+  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_buffer,&__pyx_n_s_length,0};
-    PyObject* values[2] = {0,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_encoding,0};
+    PyObject* values[1] = {0};
+    values[0] = ((PyObject *)__pyx_n_s_ascii);
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
       const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
       switch (pos_args) {
-        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
         case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
         case  0: break;
         default: goto __pyx_L5_argtuple_error;
@@ -2581,65 +2561,347 @@ static PyObject *__pyx_pw_5pysam_6ctabix_6Parser_1__call__(PyObject *__pyx_v_sel
       kw_args = PyDict_Size(__pyx_kwds);
       switch (pos_args) {
         case  0:
-        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_buffer)) != 0)) kw_args--;
-        else goto __pyx_L5_argtuple_error;
-        case  1:
-        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_length)) != 0)) kw_args--;
-        else {
-          __Pyx_RaiseArgtupleInvalid("__call__", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_encoding);
+          if (value) { values[0] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__call__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
-      goto __pyx_L5_argtuple_error;
     } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+      switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
     }
-    __pyx_v_buffer = __Pyx_PyObject_AsString(values[0]); if (unlikely((!__pyx_v_buffer) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
-    __pyx_v_length = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_length == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_encoding = values[0];
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__call__", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 0, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.ctabix.Parser.__call__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.Parser.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
-  return NULL;
+  return -1;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_6ctabix_6Parser___call__(((struct __pyx_obj_5pysam_6ctabix_Parser *)__pyx_v_self), __pyx_v_buffer, __pyx_v_length);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_6Parser___init__(((struct __pyx_obj_5pysam_6ctabix_Parser *)__pyx_v_self), __pyx_v_encoding);
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_6Parser___call__(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_length) {
-  PyObject *__pyx_r = NULL;
+static int __pyx_pf_5pysam_6ctabix_6Parser___init__(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, PyObject *__pyx_v_encoding) {
+  int __pyx_r;
   __Pyx_RefNannyDeclarations
-  PyObject *__pyx_t_1 = NULL;
-  int __pyx_lineno = 0;
-  const char *__pyx_filename = NULL;
-  int __pyx_clineno = 0;
-  __Pyx_RefNannySetupContext("__call__", 0);
+  __Pyx_RefNannySetupContext("__init__", 0);
+
+  /* "pysam/ctabix.pyx":88
+ * 
+ *     def __init__(self, encoding="ascii"):
+ *         self.encoding = encoding             # <<<<<<<<<<<<<<
+ * 
+ *     def set_encoding(self, encoding):
+ */
+  __Pyx_INCREF(__pyx_v_encoding);
+  __Pyx_GIVEREF(__pyx_v_encoding);
+  __Pyx_GOTREF(__pyx_v_self->encoding);
+  __Pyx_DECREF(__pyx_v_self->encoding);
+  __pyx_v_self->encoding = __pyx_v_encoding;
+
+  /* "pysam/ctabix.pyx":87
+ * cdef class Parser:
+ * 
+ *     def __init__(self, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *         self.encoding = encoding
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/ctabix.pyx":90
+ *         self.encoding = encoding
+ * 
+ *     def set_encoding(self, encoding):             # <<<<<<<<<<<<<<
+ *         self.encoding = encoding
+ * 
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_6ctabix_6Parser_3set_encoding(PyObject *__pyx_v_self, PyObject *__pyx_v_encoding); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_6Parser_2set_encoding[] = "Parser.set_encoding(self, encoding)";
+static PyObject *__pyx_pw_5pysam_6ctabix_6Parser_3set_encoding(PyObject *__pyx_v_self, PyObject *__pyx_v_encoding) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("set_encoding (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_6Parser_2set_encoding(((struct __pyx_obj_5pysam_6ctabix_Parser *)__pyx_v_self), ((PyObject *)__pyx_v_encoding));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_6ctabix_6Parser_2set_encoding(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, PyObject *__pyx_v_encoding) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("set_encoding", 0);
+
+  /* "pysam/ctabix.pyx":91
+ * 
+ *     def set_encoding(self, encoding):
+ *         self.encoding = encoding             # <<<<<<<<<<<<<<
+ * 
+ *     def get_encoding(self):
+ */
+  __Pyx_INCREF(__pyx_v_encoding);
+  __Pyx_GIVEREF(__pyx_v_encoding);
+  __Pyx_GOTREF(__pyx_v_self->encoding);
+  __Pyx_DECREF(__pyx_v_self->encoding);
+  __pyx_v_self->encoding = __pyx_v_encoding;
+
+  /* "pysam/ctabix.pyx":90
+ *         self.encoding = encoding
+ * 
+ *     def set_encoding(self, encoding):             # <<<<<<<<<<<<<<
+ *         self.encoding = encoding
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/ctabix.pyx":93
+ *         self.encoding = encoding
+ * 
+ *     def get_encoding(self):             # <<<<<<<<<<<<<<
+ *         return self.encoding
+ * 
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_6ctabix_6Parser_5get_encoding(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_6Parser_4get_encoding[] = "Parser.get_encoding(self)";
+static PyObject *__pyx_pw_5pysam_6ctabix_6Parser_5get_encoding(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("get_encoding (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_6Parser_4get_encoding(((struct __pyx_obj_5pysam_6ctabix_Parser *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_6ctabix_6Parser_4get_encoding(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("get_encoding", 0);
+
+  /* "pysam/ctabix.pyx":94
+ * 
+ *     def get_encoding(self):
+ *         return self.encoding             # <<<<<<<<<<<<<<
+ * 
+ *     cdef parse(self, char * buffer, int length):
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __Pyx_INCREF(__pyx_v_self->encoding);
+  __pyx_r = __pyx_v_self->encoding;
+  goto __pyx_L0;
 
   /* "pysam/ctabix.pyx":93
+ *         self.encoding = encoding
+ * 
+ *     def get_encoding(self):             # <<<<<<<<<<<<<<
+ *         return self.encoding
+ * 
+ */
+
+  /* function exit code */
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/ctabix.pyx":96
+ *         return self.encoding
+ * 
+ *     cdef parse(self, char * buffer, int length):             # <<<<<<<<<<<<<<
+ *         raise NotImplementedError(
+ *             'parse method of %s not implemented' % str(self))
+ */
+
+static PyObject *__pyx_f_5pysam_6ctabix_6Parser_parse(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, CYTHON_UNUSED char *__pyx_v_buffer, CYTHON_UNUSED int __pyx_v_length) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_RefNannySetupContext("parse", 0);
+
+  /* "pysam/ctabix.pyx":98
+ *     cdef parse(self, char * buffer, int length):
+ *         raise NotImplementedError(
+ *             'parse method of %s not implemented' % str(self))             # <<<<<<<<<<<<<<
+ * 
+ *     def __call__(self, char * buffer, int length):
+ */
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 98; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_INCREF(((PyObject *)__pyx_v_self));
+  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_self));
+  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
+  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 98; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_parse_method_of_s_not_implemente, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 98; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+
+  /* "pysam/ctabix.pyx":97
+ * 
+ *     cdef parse(self, char * buffer, int length):
+ *         raise NotImplementedError(             # <<<<<<<<<<<<<<
+ *             'parse method of %s not implemented' % str(self))
+ * 
+ */
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 97; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
+  __Pyx_GIVEREF(__pyx_t_1);
+  __pyx_t_1 = 0;
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 97; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 97; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/ctabix.pyx":96
+ *         return self.encoding
+ * 
+ *     cdef parse(self, char * buffer, int length):             # <<<<<<<<<<<<<<
+ *         raise NotImplementedError(
+ *             'parse method of %s not implemented' % str(self))
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("pysam.ctabix.Parser.parse", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = 0;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/ctabix.pyx":100
+ *             'parse method of %s not implemented' % str(self))
+ * 
+ *     def __call__(self, char * buffer, int length):             # <<<<<<<<<<<<<<
+ *         return self.parse(buffer, length)
+ * 
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_6ctabix_6Parser_7__call__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static PyObject *__pyx_pw_5pysam_6ctabix_6Parser_7__call__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  char *__pyx_v_buffer;
+  int __pyx_v_length;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__call__ (wrapper)", 0);
+  {
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_buffer,&__pyx_n_s_length,0};
+    PyObject* values[2] = {0,0};
+    if (unlikely(__pyx_kwds)) {
+      Py_ssize_t kw_args;
+      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
+      switch (pos_args) {
+        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+      kw_args = PyDict_Size(__pyx_kwds);
+      switch (pos_args) {
+        case  0:
+        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_buffer)) != 0)) kw_args--;
+        else goto __pyx_L5_argtuple_error;
+        case  1:
+        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_length)) != 0)) kw_args--;
+        else {
+          __Pyx_RaiseArgtupleInvalid("__call__", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 100; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        }
+      }
+      if (unlikely(kw_args > 0)) {
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__call__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 100; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
+      goto __pyx_L5_argtuple_error;
+    } else {
+      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+    }
+    __pyx_v_buffer = __Pyx_PyObject_AsString(values[0]); if (unlikely((!__pyx_v_buffer) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 100; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_length = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_length == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 100; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("__call__", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 100; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("pysam.ctabix.Parser.__call__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_RefNannyFinishContext();
+  return NULL;
+  __pyx_L4_argument_unpacking_done:;
+  __pyx_r = __pyx_pf_5pysam_6ctabix_6Parser_6__call__(((struct __pyx_obj_5pysam_6ctabix_Parser *)__pyx_v_self), __pyx_v_buffer, __pyx_v_length);
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_6ctabix_6Parser_6__call__(struct __pyx_obj_5pysam_6ctabix_Parser *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_length) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_RefNannySetupContext("__call__", 0);
+
+  /* "pysam/ctabix.pyx":101
  * 
  *     def __call__(self, char * buffer, int length):
  *         return self.parse(buffer, length)             # <<<<<<<<<<<<<<
  * 
- * cdef class asTuple(Parser):
+ * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_6ctabix_Parser *)__pyx_v_self->__pyx_vtab)->parse(__pyx_v_self, __pyx_v_buffer, __pyx_v_length); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 93; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_6ctabix_Parser *)__pyx_v_self->__pyx_vtab)->parse(__pyx_v_self, __pyx_v_buffer, __pyx_v_length); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 101; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":92
+  /* "pysam/ctabix.pyx":100
  *             'parse method of %s not implemented' % str(self))
  * 
  *     def __call__(self, char * buffer, int length):             # <<<<<<<<<<<<<<
@@ -2658,70 +2920,78 @@ static PyObject *__pyx_pf_5pysam_6ctabix_6Parser___call__(struct __pyx_obj_5pysa
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":100
- *     Access is by numeric index.
+/* "pysam/ctabix.pyx":109
+ *     A field in a row is accessed by numeric index.
  *     '''
  *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
  *         cdef TabProxies.TupleProxy r
- *         r = TabProxies.TupleProxy()
+ *         r = TabProxies.TupleProxy(self.encoding)
  */
 
-static PyObject *__pyx_f_5pysam_6ctabix_7asTuple_parse(CYTHON_UNUSED struct __pyx_obj_5pysam_6ctabix_asTuple *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_len) {
+static PyObject *__pyx_f_5pysam_6ctabix_7asTuple_parse(struct __pyx_obj_5pysam_6ctabix_asTuple *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_len) {
   struct __pyx_obj_5pysam_10TabProxies_TupleProxy *__pyx_v_r = 0;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse", 0);
 
-  /* "pysam/ctabix.pyx":102
+  /* "pysam/ctabix.pyx":111
  *     cdef parse(self, char * buffer, int len):
  *         cdef TabProxies.TupleProxy r
- *         r = TabProxies.TupleProxy()             # <<<<<<<<<<<<<<
+ *         r = TabProxies.TupleProxy(self.encoding)             # <<<<<<<<<<<<<<
  *         # need to copy - there were some
  *         # persistence issues with "present"
  */
-  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 102; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_r = ((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_t_1);
-  __pyx_t_1 = 0;
+  __Pyx_INCREF(__pyx_v_self->__pyx_base.encoding);
+  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_self->__pyx_base.encoding);
+  __Pyx_GIVEREF(__pyx_v_self->__pyx_base.encoding);
+  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_TupleProxy)), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_v_r = ((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_t_2);
+  __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":105
+  /* "pysam/ctabix.pyx":114
  *         # need to copy - there were some
  *         # persistence issues with "present"
  *         r.copy(buffer, len)             # <<<<<<<<<<<<<<
  *         return r
  * 
  */
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_r->__pyx_vtab)->copy(__pyx_v_r, __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *)__pyx_v_r->__pyx_vtab)->copy(__pyx_v_r, __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 114; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":106
+  /* "pysam/ctabix.pyx":115
  *         # persistence issues with "present"
  *         r.copy(buffer, len)
  *         return r             # <<<<<<<<<<<<<<
  * 
- * cdef class asGTF(Parser):
+ * 
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(((PyObject *)__pyx_v_r));
   __pyx_r = ((PyObject *)__pyx_v_r);
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":100
- *     Access is by numeric index.
+  /* "pysam/ctabix.pyx":109
+ *     A field in a row is accessed by numeric index.
  *     '''
  *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
  *         cdef TabProxies.TupleProxy r
- *         r = TabProxies.TupleProxy()
+ *         r = TabProxies.TupleProxy(self.encoding)
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
   __Pyx_AddTraceback("pysam.ctabix.asTuple.parse", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -2731,70 +3001,78 @@ static PyObject *__pyx_f_5pysam_6ctabix_7asTuple_parse(CYTHON_UNUSED struct __py
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":139
+/* "pysam/ctabix.pyx":158
  * 
  *     '''
  *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
  *         cdef TabProxies.GTFProxy r
- *         r = TabProxies.GTFProxy()
+ *         r = TabProxies.GTFProxy(self.encoding)
  */
 
-static PyObject *__pyx_f_5pysam_6ctabix_5asGTF_parse(CYTHON_UNUSED struct __pyx_obj_5pysam_6ctabix_asGTF *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_len) {
+static PyObject *__pyx_f_5pysam_6ctabix_5asGTF_parse(struct __pyx_obj_5pysam_6ctabix_asGTF *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_len) {
   struct __pyx_obj_5pysam_10TabProxies_GTFProxy *__pyx_v_r = 0;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse", 0);
 
-  /* "pysam/ctabix.pyx":141
+  /* "pysam/ctabix.pyx":160
  *     cdef parse(self, char * buffer, int len):
  *         cdef TabProxies.GTFProxy r
- *         r = TabProxies.GTFProxy()             # <<<<<<<<<<<<<<
- *         r.copy( buffer, len )
+ *         r = TabProxies.GTFProxy(self.encoding)             # <<<<<<<<<<<<<<
+ *         r.copy(buffer, len)
  *         return r
  */
-  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_GTFProxy)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_r = ((struct __pyx_obj_5pysam_10TabProxies_GTFProxy *)__pyx_t_1);
-  __pyx_t_1 = 0;
+  __Pyx_INCREF(__pyx_v_self->__pyx_base.encoding);
+  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_self->__pyx_base.encoding);
+  __Pyx_GIVEREF(__pyx_v_self->__pyx_base.encoding);
+  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_GTFProxy)), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_v_r = ((struct __pyx_obj_5pysam_10TabProxies_GTFProxy *)__pyx_t_2);
+  __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":142
+  /* "pysam/ctabix.pyx":161
  *         cdef TabProxies.GTFProxy r
- *         r = TabProxies.GTFProxy()
- *         r.copy( buffer, len )             # <<<<<<<<<<<<<<
+ *         r = TabProxies.GTFProxy(self.encoding)
+ *         r.copy(buffer, len)             # <<<<<<<<<<<<<<
  *         return r
  * 
  */
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy *)__pyx_v_r->__pyx_base.__pyx_vtab)->__pyx_base.copy(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_r), __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy *)__pyx_v_r->__pyx_base.__pyx_vtab)->__pyx_base.copy(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_r), __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 161; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":143
- *         r = TabProxies.GTFProxy()
- *         r.copy( buffer, len )
+  /* "pysam/ctabix.pyx":162
+ *         r = TabProxies.GTFProxy(self.encoding)
+ *         r.copy(buffer, len)
  *         return r             # <<<<<<<<<<<<<<
  * 
- * cdef class asBed( Parser ):
+ * 
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(((PyObject *)__pyx_v_r));
   __pyx_r = ((PyObject *)__pyx_v_r);
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":139
+  /* "pysam/ctabix.pyx":158
  * 
  *     '''
  *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
  *         cdef TabProxies.GTFProxy r
- *         r = TabProxies.GTFProxy()
+ *         r = TabProxies.GTFProxy(self.encoding)
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
   __Pyx_AddTraceback("pysam.ctabix.asGTF.parse", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -2804,70 +3082,78 @@ static PyObject *__pyx_f_5pysam_6ctabix_5asGTF_parse(CYTHON_UNUSED struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":179
+/* "pysam/ctabix.pyx":206
  * 
  *     '''
  *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
  *         cdef TabProxies.BedProxy r
- *         r = TabProxies.BedProxy()
+ *         r = TabProxies.BedProxy(self.encoding)
  */
 
-static PyObject *__pyx_f_5pysam_6ctabix_5asBed_parse(CYTHON_UNUSED struct __pyx_obj_5pysam_6ctabix_asBed *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_len) {
+static PyObject *__pyx_f_5pysam_6ctabix_5asBed_parse(struct __pyx_obj_5pysam_6ctabix_asBed *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_len) {
   struct __pyx_obj_5pysam_10TabProxies_BedProxy *__pyx_v_r = 0;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse", 0);
 
-  /* "pysam/ctabix.pyx":181
+  /* "pysam/ctabix.pyx":208
  *     cdef parse(self, char * buffer, int len):
  *         cdef TabProxies.BedProxy r
- *         r = TabProxies.BedProxy()             # <<<<<<<<<<<<<<
- *         r.copy( buffer, len )
+ *         r = TabProxies.BedProxy(self.encoding)             # <<<<<<<<<<<<<<
+ *         r.copy(buffer, len)
  *         return r
  */
-  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_BedProxy)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 181; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_r = ((struct __pyx_obj_5pysam_10TabProxies_BedProxy *)__pyx_t_1);
-  __pyx_t_1 = 0;
+  __Pyx_INCREF(__pyx_v_self->__pyx_base.encoding);
+  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_self->__pyx_base.encoding);
+  __Pyx_GIVEREF(__pyx_v_self->__pyx_base.encoding);
+  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_BedProxy)), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_v_r = ((struct __pyx_obj_5pysam_10TabProxies_BedProxy *)__pyx_t_2);
+  __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":182
+  /* "pysam/ctabix.pyx":209
  *         cdef TabProxies.BedProxy r
- *         r = TabProxies.BedProxy()
- *         r.copy( buffer, len )             # <<<<<<<<<<<<<<
+ *         r = TabProxies.BedProxy(self.encoding)
+ *         r.copy(buffer, len)             # <<<<<<<<<<<<<<
  *         return r
  * 
  */
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy *)__pyx_v_r->__pyx_base.__pyx_base.__pyx_vtab)->__pyx_base.__pyx_base.copy(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_r), __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy *)__pyx_v_r->__pyx_base.__pyx_base.__pyx_vtab)->__pyx_base.__pyx_base.copy(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_r), __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":183
- *         r = TabProxies.BedProxy()
- *         r.copy( buffer, len )
+  /* "pysam/ctabix.pyx":210
+ *         r = TabProxies.BedProxy(self.encoding)
+ *         r.copy(buffer, len)
  *         return r             # <<<<<<<<<<<<<<
  * 
- * cdef class asVCF( Parser ):
+ * 
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(((PyObject *)__pyx_v_r));
   __pyx_r = ((PyObject *)__pyx_v_r);
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":179
+  /* "pysam/ctabix.pyx":206
  * 
  *     '''
  *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
  *         cdef TabProxies.BedProxy r
- *         r = TabProxies.BedProxy()
+ *         r = TabProxies.BedProxy(self.encoding)
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
   __Pyx_AddTraceback("pysam.ctabix.asBed.parse", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -2877,49 +3163,56 @@ static PyObject *__pyx_f_5pysam_6ctabix_5asBed_parse(CYTHON_UNUSED struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":215
+/* "pysam/ctabix.pyx":247
  * 
  *     '''
- *     cdef parse(self, char * buffer, int len ):             # <<<<<<<<<<<<<<
+ *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
  *         cdef TabProxies.VCFProxy r
- *         r = TabProxies.VCFProxy()
+ *         r = TabProxies.VCFProxy(self.encoding)
  */
 
-static PyObject *__pyx_f_5pysam_6ctabix_5asVCF_parse(CYTHON_UNUSED struct __pyx_obj_5pysam_6ctabix_asVCF *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_len) {
+static PyObject *__pyx_f_5pysam_6ctabix_5asVCF_parse(struct __pyx_obj_5pysam_6ctabix_asVCF *__pyx_v_self, char *__pyx_v_buffer, int __pyx_v_len) {
   struct __pyx_obj_5pysam_10TabProxies_VCFProxy *__pyx_v_r = 0;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse", 0);
 
-  /* "pysam/ctabix.pyx":217
- *     cdef parse(self, char * buffer, int len ):
+  /* "pysam/ctabix.pyx":249
+ *     cdef parse(self, char * buffer, int len):
  *         cdef TabProxies.VCFProxy r
- *         r = TabProxies.VCFProxy()             # <<<<<<<<<<<<<<
+ *         r = TabProxies.VCFProxy(self.encoding)             # <<<<<<<<<<<<<<
  *         r.copy(buffer, len)
  *         return r
  */
-  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_VCFProxy)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_v_r = ((struct __pyx_obj_5pysam_10TabProxies_VCFProxy *)__pyx_t_1);
-  __pyx_t_1 = 0;
+  __Pyx_INCREF(__pyx_v_self->__pyx_base.encoding);
+  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_self->__pyx_base.encoding);
+  __Pyx_GIVEREF(__pyx_v_self->__pyx_base.encoding);
+  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_10TabProxies_VCFProxy)), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_v_r = ((struct __pyx_obj_5pysam_10TabProxies_VCFProxy *)__pyx_t_2);
+  __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":218
+  /* "pysam/ctabix.pyx":250
  *         cdef TabProxies.VCFProxy r
- *         r = TabProxies.VCFProxy()
+ *         r = TabProxies.VCFProxy(self.encoding)
  *         r.copy(buffer, len)             # <<<<<<<<<<<<<<
  *         return r
  * 
  */
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy *)__pyx_v_r->__pyx_base.__pyx_base.__pyx_vtab)->__pyx_base.__pyx_base.copy(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_r), __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 218; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy *)__pyx_v_r->__pyx_base.__pyx_base.__pyx_vtab)->__pyx_base.__pyx_base.copy(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_r), __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 250; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":219
- *         r = TabProxies.VCFProxy()
+  /* "pysam/ctabix.pyx":251
+ *         r = TabProxies.VCFProxy(self.encoding)
  *         r.copy(buffer, len)
  *         return r             # <<<<<<<<<<<<<<
  * 
@@ -2930,17 +3223,18 @@ static PyObject *__pyx_f_5pysam_6ctabix_5asVCF_parse(CYTHON_UNUSED struct __pyx_
   __pyx_r = ((PyObject *)__pyx_v_r);
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":215
+  /* "pysam/ctabix.pyx":247
  * 
  *     '''
- *     cdef parse(self, char * buffer, int len ):             # <<<<<<<<<<<<<<
+ *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
  *         cdef TabProxies.VCFProxy r
- *         r = TabProxies.VCFProxy()
+ *         r = TabProxies.VCFProxy(self.encoding)
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
   __Pyx_AddTraceback("pysam.ctabix.asVCF.parse", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = 0;
   __pyx_L0:;
@@ -2950,21 +3244,22 @@ static PyObject *__pyx_f_5pysam_6ctabix_5asVCF_parse(CYTHON_UNUSED struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":234
- *     parsed data (see for example :meth:`asTuple` and :meth:`asGTF`).
+/* "pysam/ctabix.pyx":268
+ * 
  *     '''
- *     def __cinit__(self, filename, mode = 'r',             # <<<<<<<<<<<<<<
- *                   parser=None,
- *                   index=None,
+ *     def __cinit__(self,             # <<<<<<<<<<<<<<
+ *                   filename,
+ *                   mode = 'r',
  */
 
 /* Python wrapper */
-static int __pyx_pw_5pysam_6ctabix_9Tabixfile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static int __pyx_pw_5pysam_6ctabix_9Tabixfile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static int __pyx_pw_5pysam_6ctabix_9TabixFile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_6ctabix_9TabixFile_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_filename = 0;
   PyObject *__pyx_v_mode = 0;
   PyObject *__pyx_v_parser = 0;
   PyObject *__pyx_v_index = 0;
+  PyObject *__pyx_v_encoding = 0;
   PyObject *__pyx_v_args = 0;
   PyObject *__pyx_v_kwargs = 0;
   int __pyx_lineno = 0;
@@ -2975,8 +3270,8 @@ static int __pyx_pw_5pysam_6ctabix_9Tabixfile_1__cinit__(PyObject *__pyx_v_self,
   __Pyx_RefNannySetupContext("__cinit__ (wrapper)", 0);
   __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return -1;
   __Pyx_GOTREF(__pyx_v_kwargs);
-  if (PyTuple_GET_SIZE(__pyx_args) > 4) {
-    __pyx_v_args = PyTuple_GetSlice(__pyx_args, 4, PyTuple_GET_SIZE(__pyx_args));
+  if (PyTuple_GET_SIZE(__pyx_args) > 5) {
+    __pyx_v_args = PyTuple_GetSlice(__pyx_args, 5, PyTuple_GET_SIZE(__pyx_args));
     if (unlikely(!__pyx_v_args)) {
       __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
       __Pyx_RefNannyFinishContext();
@@ -2987,32 +3282,34 @@ static int __pyx_pw_5pysam_6ctabix_9Tabixfile_1__cinit__(PyObject *__pyx_v_self,
     __pyx_v_args = __pyx_empty_tuple; __Pyx_INCREF(__pyx_empty_tuple);
   }
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_filename,&__pyx_n_s_mode,&__pyx_n_s_parser,&__pyx_n_s_index,0};
-    PyObject* values[4] = {0,0,0,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_filename,&__pyx_n_s_mode,&__pyx_n_s_parser,&__pyx_n_s_index,&__pyx_n_s_encoding,0};
+    PyObject* values[5] = {0,0,0,0,0};
     values[1] = ((PyObject *)__pyx_n_s_r);
 
-    /* "pysam/ctabix.pyx":235
- *     '''
- *     def __cinit__(self, filename, mode = 'r',
+    /* "pysam/ctabix.pyx":271
+ *                   filename,
+ *                   mode = 'r',
  *                   parser=None,             # <<<<<<<<<<<<<<
  *                   index=None,
- *                   *args,
+ *                   encoding="ascii",
  */
     values[2] = ((PyObject *)Py_None);
 
-    /* "pysam/ctabix.pyx":236
- *     def __cinit__(self, filename, mode = 'r',
+    /* "pysam/ctabix.pyx":272
+ *                   mode = 'r',
  *                   parser=None,
  *                   index=None,             # <<<<<<<<<<<<<<
+ *                   encoding="ascii",
  *                   *args,
- *                   **kwargs ):
  */
     values[3] = ((PyObject *)Py_None);
+    values[4] = ((PyObject *)__pyx_n_s_ascii);
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
       const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
       switch (pos_args) {
         default:
+        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
         case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
         case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
         case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
@@ -3039,14 +3336,20 @@ static int __pyx_pw_5pysam_6ctabix_9Tabixfile_1__cinit__(PyObject *__pyx_v_self,
           PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_index);
           if (value) { values[3] = value; kw_args--; }
         }
+        case  4:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_encoding);
+          if (value) { values[4] = value; kw_args--; }
+        }
       }
       if (unlikely(kw_args > 0)) {
-        const Py_ssize_t used_pos_args = (pos_args < 4) ? pos_args : 4;
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, used_pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        const Py_ssize_t used_pos_args = (pos_args < 5) ? pos_args : 5;
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, used_pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 268; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
         default:
+        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
         case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
         case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
         case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
@@ -3060,25 +3363,26 @@ static int __pyx_pw_5pysam_6ctabix_9Tabixfile_1__cinit__(PyObject *__pyx_v_self,
     __pyx_v_mode = values[1];
     __pyx_v_parser = values[2];
     __pyx_v_index = values[3];
+    __pyx_v_encoding = values[4];
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 1, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 1, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 268; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_DECREF(__pyx_v_args); __pyx_v_args = 0;
   __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0;
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_6ctabix_9Tabixfile___cinit__(((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)__pyx_v_self), __pyx_v_filename, __pyx_v_mode, __pyx_v_parser, __pyx_v_index, __pyx_v_args, __pyx_v_kwargs);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_9TabixFile___cinit__(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self), __pyx_v_filename, __pyx_v_mode, __pyx_v_parser, __pyx_v_index, __pyx_v_encoding, __pyx_v_args, __pyx_v_kwargs);
 
-  /* "pysam/ctabix.pyx":234
- *     parsed data (see for example :meth:`asTuple` and :meth:`asGTF`).
+  /* "pysam/ctabix.pyx":268
+ * 
  *     '''
- *     def __cinit__(self, filename, mode = 'r',             # <<<<<<<<<<<<<<
- *                   parser=None,
- *                   index=None,
+ *     def __cinit__(self,             # <<<<<<<<<<<<<<
+ *                   filename,
+ *                   mode = 'r',
  */
 
   /* function exit code */
@@ -3088,7 +3392,7 @@ static int __pyx_pw_5pysam_6ctabix_9Tabixfile_1__cinit__(PyObject *__pyx_v_self,
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_6ctabix_9Tabixfile___cinit__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, PyObject *__pyx_v_parser, PyObject *__pyx_v_index, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs) {
+static int __pyx_pf_5pysam_6ctabix_9TabixFile___cinit__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, PyObject *__pyx_v_parser, PyObject *__pyx_v_index, PyObject *__pyx_v_encoding, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -3100,23 +3404,23 @@ static int __pyx_pf_5pysam_6ctabix_9Tabixfile___cinit__(struct __pyx_obj_5pysam_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__cinit__", 0);
 
-  /* "pysam/ctabix.pyx":240
+  /* "pysam/ctabix.pyx":277
  *                   **kwargs ):
  * 
  *         self.tabixfile = NULL             # <<<<<<<<<<<<<<
  *         self.parser = parser
- *         self._open( filename, mode, index, *args, **kwargs )
+ *         self._open(filename, mode, index, *args, **kwargs)
  */
   __pyx_v_self->tabixfile = NULL;
 
-  /* "pysam/ctabix.pyx":241
+  /* "pysam/ctabix.pyx":278
  * 
  *         self.tabixfile = NULL
  *         self.parser = parser             # <<<<<<<<<<<<<<
- *         self._open( filename, mode, index, *args, **kwargs )
- * 
+ *         self._open(filename, mode, index, *args, **kwargs)
+ *         self.encoding = encoding
  */
-  if (!(likely(((__pyx_v_parser) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_parser, __pyx_ptype_5pysam_6ctabix_Parser))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!(likely(((__pyx_v_parser) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_parser, __pyx_ptype_5pysam_6ctabix_Parser))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 278; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_1 = __pyx_v_parser;
   __Pyx_INCREF(__pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
@@ -3125,16 +3429,16 @@ static int __pyx_pf_5pysam_6ctabix_9Tabixfile___cinit__(struct __pyx_obj_5pysam_
   __pyx_v_self->parser = ((struct __pyx_obj_5pysam_6ctabix_Parser *)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":242
+  /* "pysam/ctabix.pyx":279
  *         self.tabixfile = NULL
  *         self.parser = parser
- *         self._open( filename, mode, index, *args, **kwargs )             # <<<<<<<<<<<<<<
+ *         self._open(filename, mode, index, *args, **kwargs)             # <<<<<<<<<<<<<<
+ *         self.encoding = encoding
  * 
- *     def _open( self,
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_open); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_filename);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_filename);
@@ -3145,27 +3449,40 @@ static int __pyx_pf_5pysam_6ctabix_9Tabixfile___cinit__(struct __pyx_obj_5pysam_
   __Pyx_INCREF(__pyx_v_index);
   PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_index);
   __Pyx_GIVEREF(__pyx_v_index);
-  __pyx_t_3 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PySequence_Tuple(__pyx_v_args); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyNumber_Add(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyNumber_Add(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_t_3 = __pyx_v_kwargs;
   __Pyx_INCREF(__pyx_t_3);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_4, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_4, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":234
- *     parsed data (see for example :meth:`asTuple` and :meth:`asGTF`).
+  /* "pysam/ctabix.pyx":280
+ *         self.parser = parser
+ *         self._open(filename, mode, index, *args, **kwargs)
+ *         self.encoding = encoding             # <<<<<<<<<<<<<<
+ * 
+ *     def _open( self,
+ */
+  __Pyx_INCREF(__pyx_v_encoding);
+  __Pyx_GIVEREF(__pyx_v_encoding);
+  __Pyx_GOTREF(__pyx_v_self->encoding);
+  __Pyx_DECREF(__pyx_v_self->encoding);
+  __pyx_v_self->encoding = __pyx_v_encoding;
+
+  /* "pysam/ctabix.pyx":268
+ * 
  *     '''
- *     def __cinit__(self, filename, mode = 'r',             # <<<<<<<<<<<<<<
- *                   parser=None,
- *                   index=None,
+ *     def __cinit__(self,             # <<<<<<<<<<<<<<
+ *                   filename,
+ *                   mode = 'r',
  */
 
   /* function exit code */
@@ -3176,25 +3493,25 @@ static int __pyx_pf_5pysam_6ctabix_9Tabixfile___cinit__(struct __pyx_obj_5pysam_
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = -1;
   __pyx_L0:;
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":244
- *         self._open( filename, mode, index, *args, **kwargs )
+/* "pysam/ctabix.pyx":282
+ *         self.encoding = encoding
  * 
  *     def _open( self,             # <<<<<<<<<<<<<<
  *                filename,
- *                mode ='r',
+ *                mode='r',
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_3_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_6ctabix_9Tabixfile_2_open[] = "Tabixfile._open(self, filename, mode='r', index=None)\nopen a :term:`tabix file` for reading.\n        ";
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_3_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_3_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_9TabixFile_2_open[] = "TabixFile._open(self, filename, mode='r', index=None)\nopen a :term:`tabix file` for reading.\n        ";
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_3_open(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_filename = 0;
   PyObject *__pyx_v_mode = 0;
   PyObject *__pyx_v_index = 0;
@@ -3209,10 +3526,10 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_3_open(PyObject *__pyx_v_sel
     PyObject* values[3] = {0,0,0};
     values[1] = ((PyObject *)__pyx_n_s_r);
 
-    /* "pysam/ctabix.pyx":247
+    /* "pysam/ctabix.pyx":285
  *                filename,
- *                mode ='r',
- *                index = None,             # <<<<<<<<<<<<<<
+ *                mode='r',
+ *                index=None,             # <<<<<<<<<<<<<<
  *               ):
  *         '''open a :term:`tabix file` for reading.
  */
@@ -3244,7 +3561,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_3_open(PyObject *__pyx_v_sel
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_open") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_open") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -3261,20 +3578,20 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_3_open(PyObject *__pyx_v_sel
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_open", 0, 1, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("_open", 0, 1, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)__pyx_v_self), __pyx_v_filename, __pyx_v_mode, __pyx_v_index);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_9TabixFile_2_open(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self), __pyx_v_filename, __pyx_v_mode, __pyx_v_index);
 
-  /* "pysam/ctabix.pyx":244
- *         self._open( filename, mode, index, *args, **kwargs )
+  /* "pysam/ctabix.pyx":282
+ *         self.encoding = encoding
  * 
  *     def _open( self,             # <<<<<<<<<<<<<<
  *                filename,
- *                mode ='r',
+ *                mode='r',
  */
 
   /* function exit code */
@@ -3282,9 +3599,10 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_3_open(PyObject *__pyx_v_sel
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, PyObject *__pyx_v_index) {
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_2_open(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_mode, PyObject *__pyx_v_index) {
   PyObject *__pyx_v_filename_index = NULL;
-  CYTHON_UNUSED PyObject *__pyx_v_bmode = 0;
+  PyObject *__pyx_v__encoded_filename = NULL;
+  PyObject *__pyx_v__encoded_index = NULL;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -3292,20 +3610,18 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
   PyObject *__pyx_t_3 = NULL;
   PyObject *__pyx_t_4 = NULL;
   int __pyx_t_5;
-  char const *__pyx_t_6;
-  int __pyx_t_7;
-  char const *__pyx_t_8;
-  char *__pyx_t_9;
+  int __pyx_t_6;
+  char const *__pyx_t_7;
+  char *__pyx_t_8;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_open", 0);
-  __Pyx_INCREF(__pyx_v_filename);
 
-  /* "pysam/ctabix.pyx":252
+  /* "pysam/ctabix.pyx":290
  *         '''
  * 
- *         assert mode in ( "r",), "invalid file opening mode `%s`" % mode             # <<<<<<<<<<<<<<
+ *         assert mode in ("r",), "invalid file opening mode `%s`" % mode             # <<<<<<<<<<<<<<
  * 
  *         if self.tabixfile != NULL:
  */
@@ -3313,20 +3629,20 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
   if (unlikely(!Py_OptimizeFlag)) {
     __Pyx_INCREF(__pyx_v_mode);
     __pyx_t_1 = __pyx_v_mode;
-    __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_1, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_1, __pyx_n_s_r, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 290; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (unlikely(!(__pyx_t_2 != 0))) {
-      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_invalid_file_opening_mode_s, __pyx_v_mode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_invalid_file_opening_mode_s, __pyx_v_mode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 290; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       PyErr_SetObject(PyExc_AssertionError, __pyx_t_1);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 290; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
   #endif
 
-  /* "pysam/ctabix.pyx":254
- *         assert mode in ( "r",), "invalid file opening mode `%s`" % mode
+  /* "pysam/ctabix.pyx":292
+ *         assert mode in ("r",), "invalid file opening mode `%s`" % mode
  * 
  *         if self.tabixfile != NULL:             # <<<<<<<<<<<<<<
  *             self.close()
@@ -3335,16 +3651,16 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
   __pyx_t_2 = ((__pyx_v_self->tabixfile != NULL) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":255
+    /* "pysam/ctabix.pyx":293
  * 
  *         if self.tabixfile != NULL:
  *             self.close()             # <<<<<<<<<<<<<<
  *         self.tabixfile = NULL
  * 
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 255; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_close); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 293; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 255; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 293; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -3352,7 +3668,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
   }
   __pyx_L3:;
 
-  /* "pysam/ctabix.pyx":256
+  /* "pysam/ctabix.pyx":294
  *         if self.tabixfile != NULL:
  *             self.close()
  *         self.tabixfile = NULL             # <<<<<<<<<<<<<<
@@ -3361,16 +3677,16 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
  */
   __pyx_v_self->tabixfile = NULL;
 
-  /* "pysam/ctabix.pyx":258
+  /* "pysam/ctabix.pyx":296
  *         self.tabixfile = NULL
  * 
  *         filename_index = index or (filename + ".tbi")             # <<<<<<<<<<<<<<
  *         self.isremote = filename.startswith("http:") or filename.startswith("ftp:")
  * 
  */
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_index); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_index); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (!__pyx_t_2) {
-    __pyx_t_3 = PyNumber_Add(__pyx_v_filename, __pyx_kp_s_tbi); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 258; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyNumber_Add(__pyx_v_filename, __pyx_kp_s_tbi); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_t_1 = __pyx_t_3;
     __pyx_t_3 = 0;
@@ -3381,24 +3697,24 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
   __pyx_v_filename_index = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":259
+  /* "pysam/ctabix.pyx":297
  * 
  *         filename_index = index or (filename + ".tbi")
  *         self.isremote = filename.startswith("http:") or filename.startswith("ftp:")             # <<<<<<<<<<<<<<
  * 
- *         # encode all the strings
+ *         if not self.isremote:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__3, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple_, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (!__pyx_t_2) {
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__4, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_t_1 = __pyx_t_4;
@@ -3407,138 +3723,12 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
     __pyx_t_1 = __pyx_t_3;
     __pyx_t_3 = 0;
   }
-  __pyx_t_5 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_v_self->isremote = __pyx_t_5;
-
-  /* "pysam/ctabix.pyx":262
- * 
- *         # encode all the strings
- *         filename = _encodeFilename(filename)             # <<<<<<<<<<<<<<
- *         filename_index = _encodeFilename(filename_index)
- *         cdef bytes bmode = mode.encode('ascii')
- */
-  __pyx_t_1 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 262; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF_SET(__pyx_v_filename, __pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/ctabix.pyx":263
- *         # encode all the strings
- *         filename = _encodeFilename(filename)
- *         filename_index = _encodeFilename(filename_index)             # <<<<<<<<<<<<<<
- *         cdef bytes bmode = mode.encode('ascii')
- * 
- */
-  __pyx_t_1 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename_index); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 263; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __Pyx_DECREF_SET(__pyx_v_filename_index, __pyx_t_1);
-  __pyx_t_1 = 0;
-
-  /* "pysam/ctabix.pyx":264
- *         filename = _encodeFilename(filename)
- *         filename_index = _encodeFilename(filename_index)
- *         cdef bytes bmode = mode.encode('ascii')             # <<<<<<<<<<<<<<
- * 
- *         if self._filename != NULL: free(self._filename )
- */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_mode, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 264; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 264; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_3);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (!(likely(PyBytes_CheckExact(__pyx_t_3))||((__pyx_t_3) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "bytes", Py_TYPE(__pyx_t_3)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 264; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_bmode = ((PyObject*)__pyx_t_3);
-  __pyx_t_3 = 0;
-
-  /* "pysam/ctabix.pyx":266
- *         cdef bytes bmode = mode.encode('ascii')
- * 
- *         if self._filename != NULL: free(self._filename )             # <<<<<<<<<<<<<<
- * 
- *         self._filename = strdup(filename)
- */
-  __pyx_t_2 = ((__pyx_v_self->_filename != NULL) != 0);
-  if (__pyx_t_2) {
-    free(__pyx_v_self->_filename);
-    goto __pyx_L4;
-  }
-  __pyx_L4:;
-
-  /* "pysam/ctabix.pyx":268
- *         if self._filename != NULL: free(self._filename )
- * 
- *         self._filename = strdup(filename)             # <<<<<<<<<<<<<<
- * 
- *         if mode[0] == 'w':
- */
-  __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 268; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->_filename = strdup(__pyx_t_6);
-
-  /* "pysam/ctabix.pyx":270
- *         self._filename = strdup(filename)
- * 
- *         if mode[0] == 'w':             # <<<<<<<<<<<<<<
- *             # open file for writing
- *             raise NotImplementedError("writing to tabix files not implemented" )
- */
-  __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 270; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_3, __pyx_n_s_w, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 270; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  if (__pyx_t_2) {
-
-    /* "pysam/ctabix.pyx":272
- *         if mode[0] == 'w':
- *             # open file for writing
- *             raise NotImplementedError("writing to tabix files not implemented" )             # <<<<<<<<<<<<<<
- * 
- *         if mode[0] != "r":
- */
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_Raise(__pyx_t_3, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
-
-  /* "pysam/ctabix.pyx":274
- *             raise NotImplementedError("writing to tabix files not implemented" )
- * 
- *         if mode[0] != "r":             # <<<<<<<<<<<<<<
- *             raise ValueError("invalid mode '%s'" % mode)
- *             # open file for reading
- */
-  __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_mode, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_t_3, __pyx_n_s_r, Py_NE)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  if (__pyx_t_2) {
-
-    /* "pysam/ctabix.pyx":275
- * 
- *         if mode[0] != "r":
- *             raise ValueError("invalid mode '%s'" % mode)             # <<<<<<<<<<<<<<
- *             # open file for reading
- * 
- */
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_invalid_mode_s, __pyx_v_mode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __Pyx_Raise(__pyx_t_3, 0, 0, 0);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  }
+  __pyx_t_5 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_v_self->isremote = __pyx_t_5;
 
-  /* "pysam/ctabix.pyx":278
- *             # open file for reading
+  /* "pysam/ctabix.pyx":299
+ *         self.isremote = filename.startswith("http:") or filename.startswith("ftp:")
  * 
  *         if not self.isremote:             # <<<<<<<<<<<<<<
  *             if not os.path.exists(filename):
@@ -3547,124 +3737,174 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
   __pyx_t_2 = ((!(__pyx_v_self->isremote != 0)) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":279
+    /* "pysam/ctabix.pyx":300
  * 
  *         if not self.isremote:
  *             if not os.path.exists(filename):             # <<<<<<<<<<<<<<
  *                 raise IOError("file `%s` not found" % filename)
  * 
  */
-    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_path); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_exists); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_path); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_exists); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_filename);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_filename);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_filename);
     __Pyx_GIVEREF(__pyx_v_filename);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_1, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 279; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_7 = ((!__pyx_t_2) != 0);
-    if (__pyx_t_7) {
+    __pyx_t_6 = ((!__pyx_t_2) != 0);
+    if (__pyx_t_6) {
 
-      /* "pysam/ctabix.pyx":280
+      /* "pysam/ctabix.pyx":301
  *         if not self.isremote:
  *             if not os.path.exists(filename):
  *                 raise IOError("file `%s` not found" % filename)             # <<<<<<<<<<<<<<
  * 
- *             if not os.path.exists( filename_index ):
+ *             if not os.path.exists(filename_index):
  */
-      __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_file_s_not_found, __pyx_v_filename); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 280; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_file_s_not_found, __pyx_v_filename); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 301; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 280; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_1);
-      PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_4);
+      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 301; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_3);
+      PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_4);
       __Pyx_GIVEREF(__pyx_t_4);
       __pyx_t_4 = 0;
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 280; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 301; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       __Pyx_Raise(__pyx_t_4, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 280; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 301; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/ctabix.pyx":282
+    /* "pysam/ctabix.pyx":303
  *                 raise IOError("file `%s` not found" % filename)
  * 
- *             if not os.path.exists( filename_index ):             # <<<<<<<<<<<<<<
+ *             if not os.path.exists(filename_index):             # <<<<<<<<<<<<<<
  *                 raise IOError("index `%s` not found" % filename_index)
  * 
  */
-    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_path); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_path); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_exists); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_exists); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_filename_index);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_filename_index);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_filename_index);
     __Pyx_GIVEREF(__pyx_v_filename_index);
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 282; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_2 = ((!__pyx_t_7) != 0);
+    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    __pyx_t_2 = ((!__pyx_t_6) != 0);
     if (__pyx_t_2) {
 
-      /* "pysam/ctabix.pyx":283
+      /* "pysam/ctabix.pyx":304
  * 
- *             if not os.path.exists( filename_index ):
+ *             if not os.path.exists(filename_index):
  *                 raise IOError("index `%s` not found" % filename_index)             # <<<<<<<<<<<<<<
  * 
- *         # open file
+ *         self._filename = filename
  */
-      __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_index_s_not_found, __pyx_v_filename_index); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 283; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 283; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_index_s_not_found, __pyx_v_filename_index); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_3);
-      __Pyx_GIVEREF(__pyx_t_3);
-      __pyx_t_3 = 0;
-      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 283; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
-      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __Pyx_Raise(__pyx_t_3, 0, 0, 0);
+      PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
+      __Pyx_GIVEREF(__pyx_t_1);
+      __pyx_t_1 = 0;
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 283; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
-    goto __pyx_L7;
+    goto __pyx_L4;
   }
-  __pyx_L7:;
+  __pyx_L4:;
+
+  /* "pysam/ctabix.pyx":306
+ *                 raise IOError("index `%s` not found" % filename_index)
+ * 
+ *         self._filename = filename             # <<<<<<<<<<<<<<
+ *         self._filename_index = filename_index
+ * 
+ */
+  __Pyx_INCREF(__pyx_v_filename);
+  __Pyx_GIVEREF(__pyx_v_filename);
+  __Pyx_GOTREF(__pyx_v_self->_filename);
+  __Pyx_DECREF(__pyx_v_self->_filename);
+  __pyx_v_self->_filename = __pyx_v_filename;
+
+  /* "pysam/ctabix.pyx":307
+ * 
+ *         self._filename = filename
+ *         self._filename_index = filename_index             # <<<<<<<<<<<<<<
+ * 
+ *         # encode all the strings to pass to tabix
+ */
+  __Pyx_INCREF(__pyx_v_filename_index);
+  __Pyx_GIVEREF(__pyx_v_filename_index);
+  __Pyx_GOTREF(__pyx_v_self->_filename_index);
+  __Pyx_DECREF(__pyx_v_self->_filename_index);
+  __pyx_v_self->_filename_index = __pyx_v_filename_index;
+
+  /* "pysam/ctabix.pyx":310
+ * 
+ *         # encode all the strings to pass to tabix
+ *         _encoded_filename = _encodeFilename(filename)             # <<<<<<<<<<<<<<
+ *         _encoded_index = _encodeFilename(filename_index)
+ * 
+ */
+  __pyx_t_1 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 310; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_v__encoded_filename = ((PyObject*)__pyx_t_1);
+  __pyx_t_1 = 0;
+
+  /* "pysam/ctabix.pyx":311
+ *         # encode all the strings to pass to tabix
+ *         _encoded_filename = _encodeFilename(filename)
+ *         _encoded_index = _encodeFilename(filename_index)             # <<<<<<<<<<<<<<
+ * 
+ *         # open file
+ */
+  __pyx_t_1 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename_index); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 311; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_v__encoded_index = ((PyObject*)__pyx_t_1);
+  __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":286
+  /* "pysam/ctabix.pyx":314
  * 
  *         # open file
- *         self.tabixfile = hts_open(filename, 'r')             # <<<<<<<<<<<<<<
+ *         self.tabixfile = hts_open(_encoded_filename, 'r')             # <<<<<<<<<<<<<<
  *         if self.tabixfile == NULL:
  *             raise IOError("could not open file `%s`" % filename)
  */
-  __pyx_t_8 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_8) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 286; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->tabixfile = hts_open(__pyx_t_8, __pyx_k_r);
+  __pyx_t_7 = __Pyx_PyObject_AsString(__pyx_v__encoded_filename); if (unlikely((!__pyx_t_7) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_self->tabixfile = hts_open(__pyx_t_7, __pyx_k_r);
 
-  /* "pysam/ctabix.pyx":287
+  /* "pysam/ctabix.pyx":315
  *         # open file
- *         self.tabixfile = hts_open(filename, 'r')
+ *         self.tabixfile = hts_open(_encoded_filename, 'r')
  *         if self.tabixfile == NULL:             # <<<<<<<<<<<<<<
  *             raise IOError("could not open file `%s`" % filename)
  * 
@@ -3672,41 +3912,41 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
   __pyx_t_2 = ((__pyx_v_self->tabixfile == NULL) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":288
- *         self.tabixfile = hts_open(filename, 'r')
+    /* "pysam/ctabix.pyx":316
+ *         self.tabixfile = hts_open(_encoded_filename, 'r')
  *         if self.tabixfile == NULL:
  *             raise IOError("could not open file `%s`" % filename)             # <<<<<<<<<<<<<<
  * 
- *         self.index = tbx_index_load(filename_index)
+ *         self.index = tbx_index_load(_encoded_index)
  */
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_file_s, __pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 288; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 288; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_file_s, __pyx_v_filename); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 288; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __Pyx_Raise(__pyx_t_3, 0, 0, 0);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
+    __Pyx_GIVEREF(__pyx_t_1);
+    __pyx_t_1 = 0;
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 288; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":290
+  /* "pysam/ctabix.pyx":318
  *             raise IOError("could not open file `%s`" % filename)
  * 
- *         self.index = tbx_index_load(filename_index)             # <<<<<<<<<<<<<<
+ *         self.index = tbx_index_load(_encoded_index)             # <<<<<<<<<<<<<<
  *         if self.index == NULL:
  *             raise IOError("could not open index for `%s`" % filename)
  */
-  __pyx_t_9 = __Pyx_PyObject_AsString(__pyx_v_filename_index); if (unlikely((!__pyx_t_9) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 290; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_v_self->index = tbx_index_load(__pyx_t_9);
+  __pyx_t_8 = __Pyx_PyObject_AsString(__pyx_v__encoded_index); if (unlikely((!__pyx_t_8) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_v_self->index = tbx_index_load(__pyx_t_8);
 
-  /* "pysam/ctabix.pyx":291
+  /* "pysam/ctabix.pyx":319
  * 
- *         self.index = tbx_index_load(filename_index)
+ *         self.index = tbx_index_load(_encoded_index)
  *         if self.index == NULL:             # <<<<<<<<<<<<<<
  *             raise IOError("could not open index for `%s`" % filename)
  * 
@@ -3714,34 +3954,34 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
   __pyx_t_2 = ((__pyx_v_self->index == NULL) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":292
- *         self.index = tbx_index_load(filename_index)
+    /* "pysam/ctabix.pyx":320
+ *         self.index = tbx_index_load(_encoded_index)
  *         if self.index == NULL:
  *             raise IOError("could not open index for `%s`" % filename)             # <<<<<<<<<<<<<<
  * 
- *     def _isOpen( self ):
+ *     def _dup(self):
  */
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_index_for_s, __pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_could_not_open_index_for_s, __pyx_v_filename); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_3);
-    __Pyx_GIVEREF(__pyx_t_3);
-    __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __Pyx_Raise(__pyx_t_3, 0, 0, 0);
+    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
+    __Pyx_GIVEREF(__pyx_t_1);
+    __pyx_t_1 = 0;
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":244
- *         self._open( filename, mode, index, *args, **kwargs )
+  /* "pysam/ctabix.pyx":282
+ *         self.encoding = encoding
  * 
  *     def _open( self,             # <<<<<<<<<<<<<<
  *                filename,
- *                mode ='r',
+ *                mode='r',
  */
 
   /* function exit code */
@@ -3751,40 +3991,153 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_2_open(struct __pyx_obj_5pys
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_3);
   __Pyx_XDECREF(__pyx_t_4);
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile._open", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_filename_index);
-  __Pyx_XDECREF(__pyx_v_bmode);
-  __Pyx_XDECREF(__pyx_v_filename);
+  __Pyx_XDECREF(__pyx_v__encoded_filename);
+  __Pyx_XDECREF(__pyx_v__encoded_index);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":294
+/* "pysam/ctabix.pyx":322
+ *             raise IOError("could not open index for `%s`" % filename)
+ * 
+ *     def _dup(self):             # <<<<<<<<<<<<<<
+ *         '''return a copy of this tabix file.
+ * 
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_5_dup(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_9TabixFile_4_dup[] = "TabixFile._dup(self)\nreturn a copy of this tabix file.\n        \n        The file is being re-opened.\n        ";
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_5_dup(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("_dup (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_9TabixFile_4_dup(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_4_dup(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_RefNannySetupContext("_dup", 0);
+
+  /* "pysam/ctabix.pyx":327
+ *         The file is being re-opened.
+ *         '''
+ *         return TabixFile(self._filename,             # <<<<<<<<<<<<<<
+ *                          mode="r",
+ *                          parser=self.parser,
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_INCREF(__pyx_v_self->_filename);
+  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_self->_filename);
+  __Pyx_GIVEREF(__pyx_v_self->_filename);
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_mode, __pyx_n_s_r) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/ctabix.pyx":329
+ *         return TabixFile(self._filename,
+ *                          mode="r",
+ *                          parser=self.parser,             # <<<<<<<<<<<<<<
+ *                          index=self._filename_index,
+ *                          encoding=self.encoding)
+ */
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_parser, ((PyObject *)__pyx_v_self->parser)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/ctabix.pyx":330
+ *                          mode="r",
+ *                          parser=self.parser,
+ *                          index=self._filename_index,             # <<<<<<<<<<<<<<
+ *                          encoding=self.encoding)
+ * 
+ */
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_index, __pyx_v_self->_filename_index) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/ctabix.pyx":331
+ *                          parser=self.parser,
+ *                          index=self._filename_index,
+ *                          encoding=self.encoding)             # <<<<<<<<<<<<<<
+ * 
+ *     def _isOpen(self):
+ */
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_encoding, __pyx_v_self->encoding) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/ctabix.pyx":327
+ *         The file is being re-opened.
+ *         '''
+ *         return TabixFile(self._filename,             # <<<<<<<<<<<<<<
+ *                          mode="r",
+ *                          parser=self.parser,
+ */
+  __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_TabixFile)), __pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_r = __pyx_t_3;
+  __pyx_t_3 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/ctabix.pyx":322
  *             raise IOError("could not open index for `%s`" % filename)
  * 
- *     def _isOpen( self ):             # <<<<<<<<<<<<<<
+ *     def _dup(self):             # <<<<<<<<<<<<<<
+ *         '''return a copy of this tabix file.
+ * 
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile._dup", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/ctabix.pyx":333
+ *                          encoding=self.encoding)
+ * 
+ *     def _isOpen(self):             # <<<<<<<<<<<<<<
  *         '''return true if samfile has been opened.'''
  *         return self.tabixfile != NULL
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_5_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_6ctabix_9Tabixfile_4_isOpen[] = "Tabixfile._isOpen(self)\nreturn true if samfile has been opened.";
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_5_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_7_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_9TabixFile_6_isOpen[] = "TabixFile._isOpen(self)\nreturn true if samfile has been opened.";
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_7_isOpen(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("_isOpen (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6ctabix_9Tabixfile_4_isOpen(((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6ctabix_9TabixFile_6_isOpen(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_4_isOpen(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_6_isOpen(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -3793,24 +4146,24 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_4_isOpen(struct __pyx_obj_5p
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_isOpen", 0);
 
-  /* "pysam/ctabix.pyx":296
- *     def _isOpen( self ):
+  /* "pysam/ctabix.pyx":335
+ *     def _isOpen(self):
  *         '''return true if samfile has been opened.'''
  *         return self.tabixfile != NULL             # <<<<<<<<<<<<<<
  * 
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->tabixfile != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBool_FromLong((__pyx_v_self->tabixfile != NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 335; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":294
- *             raise IOError("could not open index for `%s`" % filename)
+  /* "pysam/ctabix.pyx":333
+ *                          encoding=self.encoding)
  * 
- *     def _isOpen( self ):             # <<<<<<<<<<<<<<
+ *     def _isOpen(self):             # <<<<<<<<<<<<<<
  *         '''return true if samfile has been opened.'''
  *         return self.tabixfile != NULL
  */
@@ -3818,7 +4171,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_4_isOpen(struct __pyx_obj_5p
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile._isOpen", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile._isOpen", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -3826,7 +4179,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_4_isOpen(struct __pyx_obj_5p
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":299
+/* "pysam/ctabix.pyx":338
  * 
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
@@ -3835,14 +4188,15 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_4_isOpen(struct __pyx_obj_5p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_6ctabix_9Tabixfile_6fetch[] = "Tabixfile.fetch(self, reference=None, start=None, end=None, region=None, parser=None)\nfetch one or more rows in a :term:`region` using 0-based\n        indexing. The region is specified by :term:`reference`,\n        *start* and *end*. Alternatively, a samtools :term:`region`\n        string can be supplied.\n\n        Without *reference* or *region* all entries will be fetched. \n        \n        If only *reference* is set, a [...]
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_9fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_9TabixFile_8fetch[] = "TabixFile.fetch(self, reference=None, start=None, end=None, region=None, parser=None, multiple_iterators=False)\nfetch one or more rows in a :term:`region` using 0-based\n        indexing. The region is specified by :term:`reference`,\n        *start* and *end*. Alternatively, a samtools :term:`region`\n        string can be supplied.\n\n        Without *reference* or *region* all entries will be fetched. \n        \n        If  [...]
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_9fetch(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_reference = 0;
   PyObject *__pyx_v_start = 0;
   PyObject *__pyx_v_end = 0;
   PyObject *__pyx_v_region = 0;
   PyObject *__pyx_v_parser = 0;
+  PyObject *__pyx_v_multiple_iterators = 0;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -3850,10 +4204,10 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7fetch(PyObject *__pyx_v_sel
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("fetch (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,&__pyx_n_s_parser,0};
-    PyObject* values[5] = {0,0,0,0,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,&__pyx_n_s_parser,&__pyx_n_s_multiple_iterators,0};
+    PyObject* values[6] = {0,0,0,0,0,0};
 
-    /* "pysam/ctabix.pyx":300
+    /* "pysam/ctabix.pyx":339
  * 
  *     def fetch(self,
  *               reference=None,             # <<<<<<<<<<<<<<
@@ -3862,7 +4216,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7fetch(PyObject *__pyx_v_sel
  */
     values[0] = ((PyObject *)Py_None);
 
-    /* "pysam/ctabix.pyx":301
+    /* "pysam/ctabix.pyx":340
  *     def fetch(self,
  *               reference=None,
  *               start=None,             # <<<<<<<<<<<<<<
@@ -3871,36 +4225,46 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7fetch(PyObject *__pyx_v_sel
  */
     values[1] = ((PyObject *)Py_None);
 
-    /* "pysam/ctabix.pyx":302
+    /* "pysam/ctabix.pyx":341
  *               reference=None,
  *               start=None,
  *               end=None,             # <<<<<<<<<<<<<<
  *               region=None,
- *               parser=None):
+ *               parser=None,
  */
     values[2] = ((PyObject *)Py_None);
 
-    /* "pysam/ctabix.pyx":303
+    /* "pysam/ctabix.pyx":342
  *               start=None,
  *               end=None,
  *               region=None,             # <<<<<<<<<<<<<<
- *               parser=None):
- *         '''fetch one or more rows in a :term:`region` using 0-based
+ *               parser=None,
+ *               multiple_iterators=False):
  */
     values[3] = ((PyObject *)Py_None);
 
-    /* "pysam/ctabix.pyx":304
+    /* "pysam/ctabix.pyx":343
  *               end=None,
  *               region=None,
- *               parser=None):             # <<<<<<<<<<<<<<
+ *               parser=None,             # <<<<<<<<<<<<<<
+ *               multiple_iterators=False):
  *         '''fetch one or more rows in a :term:`region` using 0-based
- *         indexing. The region is specified by :term:`reference`,
  */
     values[4] = ((PyObject *)Py_None);
+
+    /* "pysam/ctabix.pyx":344
+ *               region=None,
+ *               parser=None,
+ *               multiple_iterators=False):             # <<<<<<<<<<<<<<
+ *         '''fetch one or more rows in a :term:`region` using 0-based
+ *         indexing. The region is specified by :term:`reference`,
+ */
+    values[5] = ((PyObject *)Py_False);
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
       const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
       switch (pos_args) {
+        case  6: values[5] = PyTuple_GET_ITEM(__pyx_args, 5);
         case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
         case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
         case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
@@ -3936,12 +4300,18 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7fetch(PyObject *__pyx_v_sel
           PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_parser);
           if (value) { values[4] = value; kw_args--; }
         }
+        case  5:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_multiple_iterators);
+          if (value) { values[5] = value; kw_args--; }
+        }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "fetch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 299; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "fetch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 338; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  6: values[5] = PyTuple_GET_ITEM(__pyx_args, 5);
         case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
         case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
         case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
@@ -3956,18 +4326,19 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7fetch(PyObject *__pyx_v_sel
     __pyx_v_end = values[2];
     __pyx_v_region = values[3];
     __pyx_v_parser = values[4];
+    __pyx_v_multiple_iterators = values[5];
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("fetch", 0, 0, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 299; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("fetch", 0, 0, 6, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 338; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_parser);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_9TabixFile_8fetch(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self), __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region, __pyx_v_parser, __pyx_v_multiple_iterators);
 
-  /* "pysam/ctabix.pyx":299
+  /* "pysam/ctabix.pyx":338
  * 
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
@@ -3980,8 +4351,10 @@ static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7fetch(PyObject *__pyx_v_sel
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_parser) {
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_8fetch(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region, PyObject *__pyx_v_parser, PyObject *__pyx_v_multiple_iterators) {
   hts_itr_t *__pyx_v_iter;
+  struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_fileobj = 0;
+  PyObject *__pyx_v_s = NULL;
   struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_a = 0;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
@@ -3989,7 +4362,9 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
   PyObject *__pyx_t_2 = NULL;
   int __pyx_t_3;
   int __pyx_t_4;
-  char *__pyx_t_5;
+  struct __pyx_opt_args_5pysam_6ctabix__force_bytes __pyx_t_5;
+  char *__pyx_t_6;
+  PyObject *__pyx_t_7 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -3998,48 +4373,48 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
   __Pyx_INCREF(__pyx_v_region);
   __Pyx_INCREF(__pyx_v_parser);
 
-  /* "pysam/ctabix.pyx":318
- *         parsing.
+  /* "pysam/ctabix.pyx":365
+ * 
  *         '''
  *         if not self._isOpen():             # <<<<<<<<<<<<<<
  *             raise ValueError("I/O operation on closed file")
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/ctabix.pyx":319
+    /* "pysam/ctabix.pyx":366
  *         '''
  *         if not self._isOpen():
  *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  * 
  *         # convert coordinates to region string
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":322
+  /* "pysam/ctabix.pyx":369
  * 
  *         # convert coordinates to region string
  *         if reference:             # <<<<<<<<<<<<<<
  *             if end is not None:
  *                 if start is None:
  */
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_reference); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_reference); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_4) {
 
-    /* "pysam/ctabix.pyx":323
+    /* "pysam/ctabix.pyx":370
  *         # convert coordinates to region string
  *         if reference:
  *             if end is not None:             # <<<<<<<<<<<<<<
@@ -4050,7 +4425,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
     __pyx_t_3 = (__pyx_t_4 != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/ctabix.pyx":324
+      /* "pysam/ctabix.pyx":371
  *         if reference:
  *             if end is not None:
  *                 if start is None:             # <<<<<<<<<<<<<<
@@ -4061,7 +4436,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
       __pyx_t_4 = (__pyx_t_3 != 0);
       if (__pyx_t_4) {
 
-        /* "pysam/ctabix.pyx":325
+        /* "pysam/ctabix.pyx":372
  *             if end is not None:
  *                 if start is None:
  *                     start = 0             # <<<<<<<<<<<<<<
@@ -4074,16 +4449,16 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
       }
       __pyx_L6:;
 
-      /* "pysam/ctabix.pyx":326
+      /* "pysam/ctabix.pyx":373
  *                 if start is None:
  *                     start = 0
  *                 region = '%s:%i-%i' % (reference, start + 1, end)             # <<<<<<<<<<<<<<
  *                 if start > end:
  *                     raise ValueError(
  */
-      __pyx_t_2 = PyNumber_Add(__pyx_v_start, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyNumber_Add(__pyx_v_start, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = PyTuple_New(3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_INCREF(__pyx_v_reference);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_reference);
@@ -4094,32 +4469,32 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
       PyTuple_SET_ITEM(__pyx_t_1, 2, __pyx_v_end);
       __Pyx_GIVEREF(__pyx_v_end);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s_i_i, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_s_i_i, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF_SET(__pyx_v_region, __pyx_t_2);
       __pyx_t_2 = 0;
 
-      /* "pysam/ctabix.pyx":327
+      /* "pysam/ctabix.pyx":374
  *                     start = 0
  *                 region = '%s:%i-%i' % (reference, start + 1, end)
  *                 if start > end:             # <<<<<<<<<<<<<<
  *                     raise ValueError(
  *                         'start (%i) > end (%i)' % (start, end))
  */
-      __pyx_t_2 = PyObject_RichCompare(__pyx_v_start, __pyx_v_end, Py_GT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyObject_RichCompare(__pyx_v_start, __pyx_v_end, Py_GT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       if (__pyx_t_4) {
 
-        /* "pysam/ctabix.pyx":329
+        /* "pysam/ctabix.pyx":376
  *                 if start > end:
  *                     raise ValueError(
  *                         'start (%i) > end (%i)' % (start, end))             # <<<<<<<<<<<<<<
  *             elif start is not None:
  *                 region = '%s:%i' % (reference, start + 1)
  */
-        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_INCREF(__pyx_v_start);
         PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_start);
@@ -4127,33 +4502,33 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
         __Pyx_INCREF(__pyx_v_end);
         PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_end);
         __Pyx_GIVEREF(__pyx_v_end);
-        __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_start_i_end_i, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_start_i_end_i, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-        /* "pysam/ctabix.pyx":328
+        /* "pysam/ctabix.pyx":375
  *                 region = '%s:%i-%i' % (reference, start + 1, end)
  *                 if start > end:
  *                     raise ValueError(             # <<<<<<<<<<<<<<
  *                         'start (%i) > end (%i)' % (start, end))
  *             elif start is not None:
  */
-        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
         __Pyx_GIVEREF(__pyx_t_1);
         __pyx_t_1 = 0;
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __Pyx_Raise(__pyx_t_1, 0, 0, 0);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       goto __pyx_L5;
     }
 
-    /* "pysam/ctabix.pyx":330
+    /* "pysam/ctabix.pyx":377
  *                     raise ValueError(
  *                         'start (%i) > end (%i)' % (start, end))
  *             elif start is not None:             # <<<<<<<<<<<<<<
@@ -4164,16 +4539,16 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
     __pyx_t_3 = (__pyx_t_4 != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/ctabix.pyx":331
+      /* "pysam/ctabix.pyx":378
  *                         'start (%i) > end (%i)' % (start, end))
  *             elif start is not None:
  *                 region = '%s:%i' % (reference, start + 1)             # <<<<<<<<<<<<<<
  *             else:
  *                 region = reference
  */
-      __pyx_t_1 = PyNumber_Add(__pyx_v_start, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyNumber_Add(__pyx_v_start, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_INCREF(__pyx_v_reference);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_reference);
@@ -4181,7 +4556,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
       PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_1);
       __Pyx_GIVEREF(__pyx_t_1);
       __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_s_i, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_s_i, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF_SET(__pyx_v_region, __pyx_t_1);
@@ -4190,7 +4565,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
     }
     /*else*/ {
 
-      /* "pysam/ctabix.pyx":333
+      /* "pysam/ctabix.pyx":380
  *                 region = '%s:%i' % (reference, start + 1)
  *             else:
  *                 region = reference             # <<<<<<<<<<<<<<
@@ -4205,51 +4580,101 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
   }
   __pyx_L4:;
 
-  /* "pysam/ctabix.pyx":337
- *         # get iterator
- *         cdef hts_itr_t * iter
+  /* "pysam/ctabix.pyx":387
+ * 
+ *         # reopen the same file if necessary
+ *         if multiple_iterators:             # <<<<<<<<<<<<<<
+ *             fileobj = self._dup()
+ *         else:
+ */
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_multiple_iterators); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__pyx_t_3) {
+
+    /* "pysam/ctabix.pyx":388
+ *         # reopen the same file if necessary
+ *         if multiple_iterators:
+ *             fileobj = self._dup()             # <<<<<<<<<<<<<<
+ *         else:
+ *             fileobj = self
+ */
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_dup); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5pysam_6ctabix_TabixFile))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 388; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_fileobj = ((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_t_2);
+    __pyx_t_2 = 0;
+    goto __pyx_L8;
+  }
+  /*else*/ {
+
+    /* "pysam/ctabix.pyx":390
+ *             fileobj = self._dup()
+ *         else:
+ *             fileobj = self             # <<<<<<<<<<<<<<
+ * 
+ *         if region is None:
+ */
+    __Pyx_INCREF(((PyObject *)__pyx_v_self));
+    __pyx_v_fileobj = __pyx_v_self;
+  }
+  __pyx_L8:;
+
+  /* "pysam/ctabix.pyx":392
+ *             fileobj = self
+ * 
  *         if region is None:             # <<<<<<<<<<<<<<
  *             # without region or reference - iterate from start
- *             iter = tbx_itr_queryi(self.index,
+ *             iter = tbx_itr_queryi(fileobj.index,
  */
   __pyx_t_3 = (__pyx_v_region == Py_None);
   __pyx_t_4 = (__pyx_t_3 != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/ctabix.pyx":339
+    /* "pysam/ctabix.pyx":394
  *         if region is None:
  *             # without region or reference - iterate from start
- *             iter = tbx_itr_queryi(self.index,             # <<<<<<<<<<<<<<
+ *             iter = tbx_itr_queryi(fileobj.index,             # <<<<<<<<<<<<<<
  *                                   HTS_IDX_START,
  *                                   0,
  */
-    __pyx_v_iter = tbx_itr_queryi(__pyx_v_self->index, HTS_IDX_START, 0, 0);
-    goto __pyx_L8;
+    __pyx_v_iter = tbx_itr_queryi(__pyx_v_fileobj->index, HTS_IDX_START, 0, 0);
+    goto __pyx_L9;
   }
   /*else*/ {
 
-    /* "pysam/ctabix.pyx":345
+    /* "pysam/ctabix.pyx":399
+ *                                   0)
  *         else:
- *             iter = tbx_itr_querys(self.index,
- *                                   region)             # <<<<<<<<<<<<<<
+ *             s = _force_bytes(region, encoding=fileobj.encoding)             # <<<<<<<<<<<<<<
+ *             iter = tbx_itr_querys(fileobj.index, s)
  * 
- *         if iter == NULL:
  */
-    __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_region); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __pyx_v_fileobj->encoding;
+    __Pyx_INCREF(__pyx_t_2);
+    __pyx_t_5.__pyx_n = 1;
+    __pyx_t_5.encoding = __pyx_t_2;
+    __pyx_t_1 = __pyx_f_5pysam_6ctabix__force_bytes(__pyx_v_region, &__pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 399; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __pyx_v_s = ((PyObject*)__pyx_t_1);
+    __pyx_t_1 = 0;
 
-    /* "pysam/ctabix.pyx":344
- *                                   0)
+    /* "pysam/ctabix.pyx":400
  *         else:
- *             iter = tbx_itr_querys(self.index,             # <<<<<<<<<<<<<<
- *                                   region)
+ *             s = _force_bytes(region, encoding=fileobj.encoding)
+ *             iter = tbx_itr_querys(fileobj.index, s)             # <<<<<<<<<<<<<<
  * 
+ *         if iter == NULL:
  */
-    __pyx_v_iter = tbx_itr_querys(__pyx_v_self->index, __pyx_t_5);
+    __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_s); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_iter = tbx_itr_querys(__pyx_v_fileobj->index, __pyx_t_6);
   }
-  __pyx_L8:;
+  __pyx_L9:;
 
-  /* "pysam/ctabix.pyx":347
- *                                   region)
+  /* "pysam/ctabix.pyx":402
+ *             iter = tbx_itr_querys(fileobj.index, s)
  * 
  *         if iter == NULL:             # <<<<<<<<<<<<<<
  *             if region is None:
@@ -4258,7 +4683,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
   __pyx_t_4 = ((__pyx_v_iter == NULL) != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/ctabix.pyx":348
+    /* "pysam/ctabix.pyx":403
  * 
  *         if iter == NULL:
  *             if region is None:             # <<<<<<<<<<<<<<
@@ -4269,7 +4694,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
     __pyx_t_3 = (__pyx_t_4 != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/ctabix.pyx":351
+      /* "pysam/ctabix.pyx":406
  *                 # possible reason is that the file is empty -
  *                 # return an empty iterator
  *                 return EmptyIterator()             # <<<<<<<<<<<<<<
@@ -4277,9 +4702,9 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
  *                 raise ValueError(
  */
       __Pyx_XDECREF(__pyx_r);
-      __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_EmptyIterator); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_EmptyIterator); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __pyx_r = __pyx_t_2;
@@ -4288,132 +4713,156 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
     }
     /*else*/ {
 
-      /* "pysam/ctabix.pyx":354
+      /* "pysam/ctabix.pyx":409
  *             else:
  *                 raise ValueError(
- *                     "could not create iterator for region '%s'" % region)             # <<<<<<<<<<<<<<
+ *                     "could not create iterator for region '%s'" %             # <<<<<<<<<<<<<<
+ *                     region)
  * 
- *         # use default parser if no parser is specified
  */
-      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_could_not_create_iterator_for_re, __pyx_v_region); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_could_not_create_iterator_for_re, __pyx_v_region); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
 
-      /* "pysam/ctabix.pyx":353
+      /* "pysam/ctabix.pyx":408
  *                 return EmptyIterator()
  *             else:
  *                 raise ValueError(             # <<<<<<<<<<<<<<
- *                     "could not create iterator for region '%s'" % region)
- * 
+ *                     "could not create iterator for region '%s'" %
+ *                     region)
  */
-      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_Raise(__pyx_t_2, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
 
-  /* "pysam/ctabix.pyx":357
+  /* "pysam/ctabix.pyx":413
  * 
  *         # use default parser if no parser is specified
  *         if parser is None:             # <<<<<<<<<<<<<<
- *             parser = self.parser
+ *             parser = fileobj.parser
  * 
  */
   __pyx_t_3 = (__pyx_v_parser == Py_None);
   __pyx_t_4 = (__pyx_t_3 != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/ctabix.pyx":358
+    /* "pysam/ctabix.pyx":414
  *         # use default parser if no parser is specified
  *         if parser is None:
- *             parser = self.parser             # <<<<<<<<<<<<<<
+ *             parser = fileobj.parser             # <<<<<<<<<<<<<<
  * 
  *         cdef TabixIterator a
  */
-    __pyx_t_2 = ((PyObject *)__pyx_v_self->parser);
+    __pyx_t_2 = ((PyObject *)__pyx_v_fileobj->parser);
     __Pyx_INCREF(__pyx_t_2);
     __Pyx_DECREF_SET(__pyx_v_parser, __pyx_t_2);
     __pyx_t_2 = 0;
-    goto __pyx_L11;
+    goto __pyx_L12;
   }
-  __pyx_L11:;
+  __pyx_L12:;
 
-  /* "pysam/ctabix.pyx":361
+  /* "pysam/ctabix.pyx":417
  * 
  *         cdef TabixIterator a
  *         if parser is None:             # <<<<<<<<<<<<<<
- *             a = TabixIterator()
+ *             a = TabixIterator(encoding=fileobj.encoding)
  *         else:
  */
   __pyx_t_4 = (__pyx_v_parser == Py_None);
   __pyx_t_3 = (__pyx_t_4 != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/ctabix.pyx":362
+    /* "pysam/ctabix.pyx":418
  *         cdef TabixIterator a
  *         if parser is None:
- *             a = TabixIterator()             # <<<<<<<<<<<<<<
+ *             a = TabixIterator(encoding=fileobj.encoding)             # <<<<<<<<<<<<<<
  *         else:
- *             a = TabixIteratorParsed(parser)
+ *             parser.set_encoding(fileobj.encoding)
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_TabixIterator)), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_v_a = ((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_t_2);
-    __pyx_t_2 = 0;
-    goto __pyx_L12;
+    if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_encoding, __pyx_v_fileobj->encoding) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_TabixIterator)), __pyx_empty_tuple, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __pyx_v_a = ((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_t_1);
+    __pyx_t_1 = 0;
+    goto __pyx_L13;
   }
   /*else*/ {
 
-    /* "pysam/ctabix.pyx":364
- *             a = TabixIterator()
+    /* "pysam/ctabix.pyx":420
+ *             a = TabixIterator(encoding=fileobj.encoding)
  *         else:
- *             a = TabixIteratorParsed(parser)             # <<<<<<<<<<<<<<
+ *             parser.set_encoding(fileobj.encoding)             # <<<<<<<<<<<<<<
+ *             a = TabixIteratorParsed(parser)
  * 
- *         a.tabixfile = self
  */
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_parser, __pyx_n_s_set_encoding); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_INCREF(__pyx_v_fileobj->encoding);
+    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_fileobj->encoding);
+    __Pyx_GIVEREF(__pyx_v_fileobj->encoding);
+    __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+
+    /* "pysam/ctabix.pyx":421
+ *         else:
+ *             parser.set_encoding(fileobj.encoding)
+ *             a = TabixIteratorParsed(parser)             # <<<<<<<<<<<<<<
+ * 
+ *         a.tabixfile = fileobj
+ */
+    __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_7);
     __Pyx_INCREF(__pyx_v_parser);
-    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_parser);
+    PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_parser);
     __Pyx_GIVEREF(__pyx_v_parser);
-    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_TabixIteratorParsed)), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_1);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_v_a = ((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_t_1);
-    __pyx_t_1 = 0;
+    __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_TabixIteratorParsed)), __pyx_t_7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+    __pyx_v_a = ((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_t_2);
+    __pyx_t_2 = 0;
   }
-  __pyx_L12:;
+  __pyx_L13:;
 
-  /* "pysam/ctabix.pyx":366
+  /* "pysam/ctabix.pyx":423
  *             a = TabixIteratorParsed(parser)
  * 
- *         a.tabixfile = self             # <<<<<<<<<<<<<<
+ *         a.tabixfile = fileobj             # <<<<<<<<<<<<<<
  *         a.iterator = iter
  * 
  */
-  __Pyx_INCREF(((PyObject *)__pyx_v_self));
-  __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
+  __Pyx_INCREF(((PyObject *)__pyx_v_fileobj));
+  __Pyx_GIVEREF(((PyObject *)__pyx_v_fileobj));
   __Pyx_GOTREF(__pyx_v_a->tabixfile);
   __Pyx_DECREF(((PyObject *)__pyx_v_a->tabixfile));
-  __pyx_v_a->tabixfile = __pyx_v_self;
+  __pyx_v_a->tabixfile = __pyx_v_fileobj;
 
-  /* "pysam/ctabix.pyx":367
+  /* "pysam/ctabix.pyx":424
  * 
- *         a.tabixfile = self
+ *         a.tabixfile = fileobj
  *         a.iterator = iter             # <<<<<<<<<<<<<<
  * 
  *         return a
  */
   __pyx_v_a->iterator = __pyx_v_iter;
 
-  /* "pysam/ctabix.pyx":369
+  /* "pysam/ctabix.pyx":426
  *         a.iterator = iter
  * 
  *         return a             # <<<<<<<<<<<<<<
@@ -4425,7 +4874,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
   __pyx_r = ((PyObject *)__pyx_v_a);
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":299
+  /* "pysam/ctabix.pyx":338
  * 
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
@@ -4437,9 +4886,12 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_XDECREF(__pyx_t_7);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
+  __Pyx_XDECREF((PyObject *)__pyx_v_fileobj);
+  __Pyx_XDECREF(__pyx_v_s);
   __Pyx_XDECREF((PyObject *)__pyx_v_a);
   __Pyx_XDECREF(__pyx_v_start);
   __Pyx_XDECREF(__pyx_v_region);
@@ -4449,28 +4901,28 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6fetch(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":378
+/* "pysam/ctabix.pyx":435
  *     property filename:
  *         '''filename associated with this object.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return self._filename
+ *             if not self._isOpen():
+ *                 raise ValueError("I/O operation on closed file")
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_8filename_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_8filename_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_8filename_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_8filename_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6ctabix_9Tabixfile_8filename___get__(((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6ctabix_9TabixFile_8filename___get__(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8filename___get__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_8filename___get__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4482,56 +4934,62 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8filename___get__(struct __p
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/ctabix.pyx":379
+  /* "pysam/ctabix.pyx":436
  *         '''filename associated with this object.'''
  *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             if not self._isOpen():             # <<<<<<<<<<<<<<
+ *                 raise ValueError("I/O operation on closed file")
  *             return self._filename
- * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_isOpen); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_3) != 0);
   if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__8, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/ctabix.pyx":437
+ *         def __get__(self):
+ *             if not self._isOpen():
+ *                 raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
+ *             return self._filename
+ * 
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__4, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":380
- *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
+  /* "pysam/ctabix.pyx":438
+ *             if not self._isOpen():
+ *                 raise ValueError("I/O operation on closed file")
  *             return self._filename             # <<<<<<<<<<<<<<
  * 
  *     property header:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->_filename); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __Pyx_INCREF(__pyx_v_self->_filename);
+  __pyx_r = __pyx_v_self->_filename;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":378
+  /* "pysam/ctabix.pyx":435
  *     property filename:
  *         '''filename associated with this object.'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
- *             return self._filename
+ *             if not self._isOpen():
+ *                 raise ValueError("I/O operation on closed file")
  */
 
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile.filename.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile.filename.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -4539,7 +4997,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8filename___get__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":390
+/* "pysam/ctabix.pyx":448
  *         '''
  * 
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4548,19 +5006,19 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8filename___get__(struct __p
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_6header_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_6header_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_6header_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_6header_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6ctabix_9Tabixfile_6header___get__(((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6ctabix_9TabixFile_6header___get__(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6header___get__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_6header___get__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -4570,7 +5028,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6header___get__(struct __pyx
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/ctabix.pyx":391
+  /* "pysam/ctabix.pyx":449
  * 
  *         def __get__(self):
  *             return GZIteratorHead(self.filename)             # <<<<<<<<<<<<<<
@@ -4578,21 +5036,21 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6header___get__(struct __pyx
  *     property contigs:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_filename); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_filename); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
   __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_GZIteratorHead)), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_GZIteratorHead)), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":390
+  /* "pysam/ctabix.pyx":448
  *         '''
  * 
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4604,7 +5062,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6header___get__(struct __pyx
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile.header.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile.header.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XGIVEREF(__pyx_r);
@@ -4612,28 +5070,28 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_6header___get__(struct __pyx
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":395
+/* "pysam/ctabix.pyx":453
  *     property contigs:
- *         '''chromosome names'''
+ *         '''list of chromosome names'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             cdef char ** sequences
  *             cdef int nsequences
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7contigs_1__get__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_7contigs_1__get__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_7contigs_1__get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_7contigs_1__get__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__get__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6ctabix_9TabixFile_7contigs___get__(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_7contigs___get__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self) {
   char **__pyx_v_sequences;
   int __pyx_v_nsequences;
   int __pyx_v_x;
@@ -4648,7 +5106,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(struct __py
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/ctabix.pyx":399
+  /* "pysam/ctabix.pyx":457
  *             cdef int nsequences
  * 
  *             sequences = tbx_seqnames(self.index, &nsequences)             # <<<<<<<<<<<<<<
@@ -4657,19 +5115,19 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(struct __py
  */
   __pyx_v_sequences = tbx_seqnames(__pyx_v_self->index, (&__pyx_v_nsequences));
 
-  /* "pysam/ctabix.pyx":401
+  /* "pysam/ctabix.pyx":459
  *             sequences = tbx_seqnames(self.index, &nsequences)
  *             cdef int x
  *             result = []             # <<<<<<<<<<<<<<
  *             for x from 0 <= x < nsequences:
  *                 result.append(sequences[x])
  */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_result = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":402
+  /* "pysam/ctabix.pyx":460
  *             cdef int x
  *             result = []
  *             for x from 0 <= x < nsequences:             # <<<<<<<<<<<<<<
@@ -4679,20 +5137,20 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(struct __py
   __pyx_t_2 = __pyx_v_nsequences;
   for (__pyx_v_x = 0; __pyx_v_x < __pyx_t_2; __pyx_v_x++) {
 
-    /* "pysam/ctabix.pyx":403
+    /* "pysam/ctabix.pyx":461
  *             result = []
  *             for x from 0 <= x < nsequences:
  *                 result.append(sequences[x])             # <<<<<<<<<<<<<<
  * 
  *             # htslib instructions:
  */
-    __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_sequences[__pyx_v_x])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 403; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_sequences[__pyx_v_x])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 403; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   }
 
-  /* "pysam/ctabix.pyx":407
+  /* "pysam/ctabix.pyx":465
  *             # htslib instructions:
  *             # only free container, not the sequences themselves
  *             free(sequences)             # <<<<<<<<<<<<<<
@@ -4701,7 +5159,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(struct __py
  */
   free(__pyx_v_sequences);
 
-  /* "pysam/ctabix.pyx":409
+  /* "pysam/ctabix.pyx":467
  *             free(sequences)
  * 
  *             return result             # <<<<<<<<<<<<<<
@@ -4713,9 +5171,9 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(struct __py
   __pyx_r = __pyx_v_result;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":395
+  /* "pysam/ctabix.pyx":453
  *     property contigs:
- *         '''chromosome names'''
+ *         '''list of chromosome names'''
  *         def __get__(self):             # <<<<<<<<<<<<<<
  *             cdef char ** sequences
  *             cdef int nsequences
@@ -4724,7 +5182,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(struct __py
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
-  __Pyx_AddTraceback("pysam.ctabix.Tabixfile.contigs.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_AddTraceback("pysam.ctabix.TabixFile.contigs.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
   __Pyx_XDECREF(__pyx_v_result);
@@ -4733,37 +5191,37 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_7contigs___get__(struct __py
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":411
+/* "pysam/ctabix.pyx":469
  *             return result
  * 
  *     def close(self):             # <<<<<<<<<<<<<<
  *         '''
- *         closes the :class:`pysam.Tabixfile`.'''
+ *         closes the :class:`pysam.TabixFile`.'''
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_9close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
-static char __pyx_doc_5pysam_6ctabix_9Tabixfile_8close[] = "Tabixfile.close(self)\n\n        closes the :class:`pysam.Tabixfile`.";
-static PyObject *__pyx_pw_5pysam_6ctabix_9Tabixfile_9close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_11close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_9TabixFile_10close[] = "TabixFile.close(self)\n\n        closes the :class:`pysam.TabixFile`.";
+static PyObject *__pyx_pw_5pysam_6ctabix_9TabixFile_11close(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("close (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6ctabix_9Tabixfile_8close(((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6ctabix_9TabixFile_10close(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8close(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6ctabix_9TabixFile_10close(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("close", 0);
 
-  /* "pysam/ctabix.pyx":414
+  /* "pysam/ctabix.pyx":472
  *         '''
- *         closes the :class:`pysam.Tabixfile`.'''
+ *         closes the :class:`pysam.TabixFile`.'''
  *         if self.tabixfile != NULL:             # <<<<<<<<<<<<<<
  *             hts_close(self.tabixfile)
  *             self.tabixfile = NULL
@@ -4771,33 +5229,64 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8close(struct __pyx_obj_5pys
   __pyx_t_1 = ((__pyx_v_self->tabixfile != NULL) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":415
- *         closes the :class:`pysam.Tabixfile`.'''
+    /* "pysam/ctabix.pyx":473
+ *         closes the :class:`pysam.TabixFile`.'''
  *         if self.tabixfile != NULL:
  *             hts_close(self.tabixfile)             # <<<<<<<<<<<<<<
  *             self.tabixfile = NULL
- * 
+ *         if self.index != NULL:
  */
     hts_close(__pyx_v_self->tabixfile);
 
-    /* "pysam/ctabix.pyx":416
+    /* "pysam/ctabix.pyx":474
  *         if self.tabixfile != NULL:
  *             hts_close(self.tabixfile)
  *             self.tabixfile = NULL             # <<<<<<<<<<<<<<
- * 
- *     def __dealloc__( self ):
+ *         if self.index != NULL:
+ *             tbx_destroy(self.index)
  */
     __pyx_v_self->tabixfile = NULL;
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/ctabix.pyx":411
+  /* "pysam/ctabix.pyx":475
+ *             hts_close(self.tabixfile)
+ *             self.tabixfile = NULL
+ *         if self.index != NULL:             # <<<<<<<<<<<<<<
+ *             tbx_destroy(self.index)
+ *             self.index = NULL
+ */
+  __pyx_t_1 = ((__pyx_v_self->index != NULL) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/ctabix.pyx":476
+ *             self.tabixfile = NULL
+ *         if self.index != NULL:
+ *             tbx_destroy(self.index)             # <<<<<<<<<<<<<<
+ *             self.index = NULL
+ * 
+ */
+    tbx_destroy(__pyx_v_self->index);
+
+    /* "pysam/ctabix.pyx":477
+ *         if self.index != NULL:
+ *             tbx_destroy(self.index)
+ *             self.index = NULL             # <<<<<<<<<<<<<<
+ * 
+ *     def __dealloc__( self ):
+ */
+    __pyx_v_self->index = NULL;
+    goto __pyx_L4;
+  }
+  __pyx_L4:;
+
+  /* "pysam/ctabix.pyx":469
  *             return result
  * 
  *     def close(self):             # <<<<<<<<<<<<<<
  *         '''
- *         closes the :class:`pysam.Tabixfile`.'''
+ *         closes the :class:`pysam.TabixFile`.'''
  */
 
   /* function exit code */
@@ -4807,8 +5296,8 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8close(struct __pyx_obj_5pys
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":418
- *             self.tabixfile = NULL
+/* "pysam/ctabix.pyx":479
+ *             self.index = NULL
  * 
  *     def __dealloc__( self ):             # <<<<<<<<<<<<<<
  *         # remember: dealloc cannot call other python methods
@@ -4816,22 +5305,22 @@ static PyObject *__pyx_pf_5pysam_6ctabix_9Tabixfile_8close(struct __pyx_obj_5pys
  */
 
 /* Python wrapper */
-static void __pyx_pw_5pysam_6ctabix_9Tabixfile_11__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_6ctabix_9Tabixfile_11__dealloc__(PyObject *__pyx_v_self) {
+static void __pyx_pw_5pysam_6ctabix_9TabixFile_13__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_6ctabix_9TabixFile_13__dealloc__(PyObject *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_6ctabix_9Tabixfile_10__dealloc__(((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)__pyx_v_self));
+  __pyx_pf_5pysam_6ctabix_9TabixFile_12__dealloc__(((struct __pyx_obj_5pysam_6ctabix_TabixFile *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
 }
 
-static void __pyx_pf_5pysam_6ctabix_9Tabixfile_10__dealloc__(struct __pyx_obj_5pysam_6ctabix_Tabixfile *__pyx_v_self) {
+static void __pyx_pf_5pysam_6ctabix_9TabixFile_12__dealloc__(struct __pyx_obj_5pysam_6ctabix_TabixFile *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("__dealloc__", 0);
 
-  /* "pysam/ctabix.pyx":422
+  /* "pysam/ctabix.pyx":483
  *         # note: no doc string
  *         # note: __del__ is not called.
  *         if self.tabixfile != NULL:             # <<<<<<<<<<<<<<
@@ -4841,51 +5330,51 @@ static void __pyx_pf_5pysam_6ctabix_9Tabixfile_10__dealloc__(struct __pyx_obj_5p
   __pyx_t_1 = ((__pyx_v_self->tabixfile != NULL) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":423
+    /* "pysam/ctabix.pyx":484
  *         # note: __del__ is not called.
  *         if self.tabixfile != NULL:
  *             hts_close(self.tabixfile)             # <<<<<<<<<<<<<<
  *             self.tabixfile = NULL
- *         if self._filename != NULL:
+ *         if self.index != NULL:
  */
     hts_close(__pyx_v_self->tabixfile);
 
-    /* "pysam/ctabix.pyx":424
+    /* "pysam/ctabix.pyx":485
  *         if self.tabixfile != NULL:
  *             hts_close(self.tabixfile)
  *             self.tabixfile = NULL             # <<<<<<<<<<<<<<
- *         if self._filename != NULL:
- *             free(self._filename)
+ *         if self.index != NULL:
+ *             tbx_destroy(self.index)
  */
     __pyx_v_self->tabixfile = NULL;
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/ctabix.pyx":425
+  /* "pysam/ctabix.pyx":486
  *             hts_close(self.tabixfile)
  *             self.tabixfile = NULL
- *         if self._filename != NULL:             # <<<<<<<<<<<<<<
- *             free(self._filename)
+ *         if self.index != NULL:             # <<<<<<<<<<<<<<
+ *             tbx_destroy(self.index)
  * 
  */
-  __pyx_t_1 = ((__pyx_v_self->_filename != NULL) != 0);
+  __pyx_t_1 = ((__pyx_v_self->index != NULL) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":426
+    /* "pysam/ctabix.pyx":487
  *             self.tabixfile = NULL
- *         if self._filename != NULL:
- *             free(self._filename)             # <<<<<<<<<<<<<<
+ *         if self.index != NULL:
+ *             tbx_destroy(self.index)             # <<<<<<<<<<<<<<
+ * 
  * 
- * cdef class TabixIterator:
  */
-    free(__pyx_v_self->_filename);
+    tbx_destroy(__pyx_v_self->index);
     goto __pyx_L4;
   }
   __pyx_L4:;
 
-  /* "pysam/ctabix.pyx":418
- *             self.tabixfile = NULL
+  /* "pysam/ctabix.pyx":479
+ *             self.index = NULL
  * 
  *     def __dealloc__( self ):             # <<<<<<<<<<<<<<
  *         # remember: dealloc cannot call other python methods
@@ -4896,33 +5385,130 @@ static void __pyx_pf_5pysam_6ctabix_9Tabixfile_10__dealloc__(struct __pyx_obj_5p
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/ctabix.pyx":433
+/* "pysam/ctabix.pyx":495
+ *     """
+ * 
+ *     def __init__(self, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *         self.encoding = encoding
+ * 
+ */
+
+/* Python wrapper */
+static int __pyx_pw_5pysam_6ctabix_13TabixIterator_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pw_5pysam_6ctabix_13TabixIterator_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  PyObject *__pyx_v_encoding = 0;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
+  {
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_encoding,0};
+    PyObject* values[1] = {0};
+    values[0] = ((PyObject *)__pyx_n_s_ascii);
+    if (unlikely(__pyx_kwds)) {
+      Py_ssize_t kw_args;
+      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
+      switch (pos_args) {
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+      kw_args = PyDict_Size(__pyx_kwds);
+      switch (pos_args) {
+        case  0:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_encoding);
+          if (value) { values[0] = value; kw_args--; }
+        }
+      }
+      if (unlikely(kw_args > 0)) {
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 495; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+    } else {
+      switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        case  0: break;
+        default: goto __pyx_L5_argtuple_error;
+      }
+    }
+    __pyx_v_encoding = values[0];
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 0, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 495; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("pysam.ctabix.TabixIterator.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __Pyx_RefNannyFinishContext();
+  return -1;
+  __pyx_L4_argument_unpacking_done:;
+  __pyx_r = __pyx_pf_5pysam_6ctabix_13TabixIterator___init__(((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_v_self), __pyx_v_encoding);
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static int __pyx_pf_5pysam_6ctabix_13TabixIterator___init__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self, PyObject *__pyx_v_encoding) {
+  int __pyx_r;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__init__", 0);
+
+  /* "pysam/ctabix.pyx":496
+ * 
+ *     def __init__(self, encoding="ascii"):
+ *         self.encoding = encoding             # <<<<<<<<<<<<<<
+ * 
+ *     def __iter__(self):
+ */
+  __Pyx_INCREF(__pyx_v_encoding);
+  __Pyx_GIVEREF(__pyx_v_encoding);
+  __Pyx_GOTREF(__pyx_v_self->encoding);
+  __Pyx_DECREF(__pyx_v_self->encoding);
+  __pyx_v_self->encoding = __pyx_v_encoding;
+
+  /* "pysam/ctabix.pyx":495
  *     """
  * 
+ *     def __init__(self, encoding="ascii"):             # <<<<<<<<<<<<<<
+ *         self.encoding = encoding
+ * 
+ */
+
+  /* function exit code */
+  __pyx_r = 0;
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/ctabix.pyx":498
+ *         self.encoding = encoding
+ * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         self.buffer.s = NULL
  *         self.buffer.l = 0
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_1__iter__(PyObject *__pyx_v_self); /*proto*/
-static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_1__iter__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_3__iter__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_3__iter__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6ctabix_13TabixIterator___iter__(((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6ctabix_13TabixIterator_2__iter__(((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator___iter__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_2__iter__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
 
-  /* "pysam/ctabix.pyx":434
+  /* "pysam/ctabix.pyx":499
  * 
  *     def __iter__(self):
  *         self.buffer.s = NULL             # <<<<<<<<<<<<<<
@@ -4931,7 +5517,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator___iter__(struct __pyx_o
  */
   __pyx_v_self->buffer.s = NULL;
 
-  /* "pysam/ctabix.pyx":435
+  /* "pysam/ctabix.pyx":500
  *     def __iter__(self):
  *         self.buffer.s = NULL
  *         self.buffer.l = 0             # <<<<<<<<<<<<<<
@@ -4940,7 +5526,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator___iter__(struct __pyx_o
  */
   __pyx_v_self->buffer.l = 0;
 
-  /* "pysam/ctabix.pyx":436
+  /* "pysam/ctabix.pyx":501
  *         self.buffer.s = NULL
  *         self.buffer.l = 0
  *         self.buffer.m = 0             # <<<<<<<<<<<<<<
@@ -4949,7 +5535,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator___iter__(struct __pyx_o
  */
   __pyx_v_self->buffer.m = 0;
 
-  /* "pysam/ctabix.pyx":438
+  /* "pysam/ctabix.pyx":503
  *         self.buffer.m = 0
  * 
  *         return self             # <<<<<<<<<<<<<<
@@ -4961,8 +5547,8 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator___iter__(struct __pyx_o
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":433
- *     """
+  /* "pysam/ctabix.pyx":498
+ *         self.encoding = encoding
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         self.buffer.s = NULL
@@ -4976,11 +5562,11 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator___iter__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":440
+/* "pysam/ctabix.pyx":505
  *         return self
  * 
  *     cdef int __cnext__(self):             # <<<<<<<<<<<<<<
- *         '''return value needs to be freed by caller'''
+ *         '''iterate to next element.
  * 
  */
 
@@ -4991,25 +5577,46 @@ static int __pyx_f_5pysam_6ctabix_13TabixIterator___cnext__(struct __pyx_obj_5py
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("__cnext__", 0);
 
-  /* "pysam/ctabix.pyx":444
+  /* "pysam/ctabix.pyx":511
+ *         was called.
+ *         '''
+ *         if self.tabixfile.tabixfile == NULL:             # <<<<<<<<<<<<<<
+ *             return -5
+ * 
+ */
+  __pyx_t_1 = ((__pyx_v_self->tabixfile->tabixfile == NULL) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/ctabix.pyx":512
+ *         '''
+ *         if self.tabixfile.tabixfile == NULL:
+ *             return -5             # <<<<<<<<<<<<<<
  * 
  *         cdef int retval
+ */
+    __pyx_r = -5;
+    goto __pyx_L0;
+  }
+
+  /* "pysam/ctabix.pyx":516
+ *         cdef int retval
+ * 
  *         while 1:             # <<<<<<<<<<<<<<
+ * 
  *             retval = tbx_itr_next(
- *                 self.tabixfile.tabixfile,
  */
   while (1) {
 
-    /* "pysam/ctabix.pyx":445
- *         cdef int retval
+    /* "pysam/ctabix.pyx":518
  *         while 1:
+ * 
  *             retval = tbx_itr_next(             # <<<<<<<<<<<<<<
  *                 self.tabixfile.tabixfile,
  *                 self.tabixfile.index,
  */
     __pyx_v_retval = tbx_itr_next(__pyx_v_self->tabixfile->tabixfile, __pyx_v_self->tabixfile->index, __pyx_v_self->iterator, (&__pyx_v_self->buffer));
 
-    /* "pysam/ctabix.pyx":450
+    /* "pysam/ctabix.pyx":523
  *                 self.iterator,
  *                 &self.buffer)
  *             if retval < 0:             # <<<<<<<<<<<<<<
@@ -5019,17 +5626,17 @@ static int __pyx_f_5pysam_6ctabix_13TabixIterator___cnext__(struct __pyx_obj_5py
     __pyx_t_1 = ((__pyx_v_retval < 0) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/ctabix.pyx":451
+      /* "pysam/ctabix.pyx":524
  *                 &self.buffer)
  *             if retval < 0:
  *                 break             # <<<<<<<<<<<<<<
  * 
  *             if self.buffer.s[0] != '#':
  */
-      goto __pyx_L4_break;
+      goto __pyx_L5_break;
     }
 
-    /* "pysam/ctabix.pyx":453
+    /* "pysam/ctabix.pyx":526
  *                 break
  * 
  *             if self.buffer.s[0] != '#':             # <<<<<<<<<<<<<<
@@ -5039,19 +5646,19 @@ static int __pyx_f_5pysam_6ctabix_13TabixIterator___cnext__(struct __pyx_obj_5py
     __pyx_t_1 = (((__pyx_v_self->buffer.s[0]) != '#') != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/ctabix.pyx":454
+      /* "pysam/ctabix.pyx":527
  * 
  *             if self.buffer.s[0] != '#':
  *                 break             # <<<<<<<<<<<<<<
  * 
  *         return retval
  */
-      goto __pyx_L4_break;
+      goto __pyx_L5_break;
     }
   }
-  __pyx_L4_break:;
+  __pyx_L5_break:;
 
-  /* "pysam/ctabix.pyx":456
+  /* "pysam/ctabix.pyx":529
  *                 break
  * 
  *         return retval             # <<<<<<<<<<<<<<
@@ -5061,11 +5668,11 @@ static int __pyx_f_5pysam_6ctabix_13TabixIterator___cnext__(struct __pyx_obj_5py
   __pyx_r = __pyx_v_retval;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":440
+  /* "pysam/ctabix.pyx":505
  *         return self
  * 
  *     cdef int __cnext__(self):             # <<<<<<<<<<<<<<
- *         '''return value needs to be freed by caller'''
+ *         '''iterate to next element.
  * 
  */
 
@@ -5075,7 +5682,7 @@ static int __pyx_f_5pysam_6ctabix_13TabixIterator___cnext__(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":458
+/* "pysam/ctabix.pyx":531
  *         return retval
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -5084,78 +5691,109 @@ static int __pyx_f_5pysam_6ctabix_13TabixIterator___cnext__(struct __pyx_obj_5py
  */
 
 /* Python wrapper */
-static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_3__next__(PyObject *__pyx_v_self); /*proto*/
-static char __pyx_doc_5pysam_6ctabix_13TabixIterator_2__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
+static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_5__next__(PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_13TabixIterator_4__next__[] = "python version of next().\n\n        pyrex uses this non-standard name instead of next()\n        ";
 #if CYTHON_COMPILING_IN_CPYTHON
-struct wrapperbase __pyx_wrapperbase_5pysam_6ctabix_13TabixIterator_2__next__;
+struct wrapperbase __pyx_wrapperbase_5pysam_6ctabix_13TabixIterator_4__next__;
 #endif
-static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_3__next__(PyObject *__pyx_v_self) {
+static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_5__next__(PyObject *__pyx_v_self) {
   PyObject *__pyx_r = 0;
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
-  __pyx_r = __pyx_pf_5pysam_6ctabix_13TabixIterator_2__next__(((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_v_self));
+  __pyx_r = __pyx_pf_5pysam_6ctabix_13TabixIterator_4__next__(((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_2__next__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self) {
+static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_4__next__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self) {
   int __pyx_v_retval;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  struct __pyx_opt_args_5pysam_6ctabix__charptr_to_str __pyx_t_4;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__next__", 0);
 
-  /* "pysam/ctabix.pyx":464
+  /* "pysam/ctabix.pyx":537
  *         """
  * 
  *         cdef int retval = self.__cnext__()             # <<<<<<<<<<<<<<
- *         if retval < 0:
- *             raise StopIteration
+ *         if retval == -5:
+ *             raise IOError("iteration on closed file")
  */
   __pyx_v_retval = ((struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator *)__pyx_v_self->__pyx_vtab)->__pyx___cnext__(__pyx_v_self);
 
-  /* "pysam/ctabix.pyx":465
+  /* "pysam/ctabix.pyx":538
  * 
  *         cdef int retval = self.__cnext__()
- *         if retval < 0:             # <<<<<<<<<<<<<<
+ *         if retval == -5:             # <<<<<<<<<<<<<<
+ *             raise IOError("iteration on closed file")
+ *         elif retval < 0:
+ */
+  __pyx_t_1 = ((__pyx_v_retval == -5) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/ctabix.pyx":539
+ *         cdef int retval = self.__cnext__()
+ *         if retval == -5:
+ *             raise IOError("iteration on closed file")             # <<<<<<<<<<<<<<
+ *         elif retval < 0:
+ *             raise StopIteration
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 539; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 539; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
+
+  /* "pysam/ctabix.pyx":540
+ *         if retval == -5:
+ *             raise IOError("iteration on closed file")
+ *         elif retval < 0:             # <<<<<<<<<<<<<<
  *             raise StopIteration
  * 
  */
   __pyx_t_1 = ((__pyx_v_retval < 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":466
- *         cdef int retval = self.__cnext__()
- *         if retval < 0:
+    /* "pysam/ctabix.pyx":541
+ *             raise IOError("iteration on closed file")
+ *         elif retval < 0:
  *             raise StopIteration             # <<<<<<<<<<<<<<
  * 
- *         return _charptr_to_str(self.buffer.s)
+ *         return _charptr_to_str(self.buffer.s, self.encoding)
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":468
+  /* "pysam/ctabix.pyx":543
  *             raise StopIteration
  * 
- *         return _charptr_to_str(self.buffer.s)             # <<<<<<<<<<<<<<
+ *         return _charptr_to_str(self.buffer.s, self.encoding)             # <<<<<<<<<<<<<<
  * 
- *     def __dealloc__(self):
+ *     def next(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __pyx_f_5pysam_6ctabix__charptr_to_str(__pyx_v_self->buffer.s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_t_2 = __pyx_v_self->encoding;
+  __Pyx_INCREF(__pyx_t_2);
+  __pyx_t_4.__pyx_n = 1;
+  __pyx_t_4.encoding = __pyx_t_2;
+  __pyx_t_3 = __pyx_f_5pysam_6ctabix__charptr_to_str(__pyx_v_self->buffer.s, &__pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_r = __pyx_t_3;
+  __pyx_t_3 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":458
+  /* "pysam/ctabix.pyx":531
  *         return retval
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -5166,6 +5804,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_2__next__(struct __pyx_
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
   __Pyx_AddTraceback("pysam.ctabix.TabixIterator.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
@@ -5174,8 +5813,77 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_2__next__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":470
- *         return _charptr_to_str(self.buffer.s)
+/* "pysam/ctabix.pyx":545
+ *         return _charptr_to_str(self.buffer.s, self.encoding)
+ * 
+ *     def next(self):             # <<<<<<<<<<<<<<
+ *         return self.__next__()
+ * 
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_7next(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_13TabixIterator_6next[] = "TabixIterator.next(self)";
+static PyObject *__pyx_pw_5pysam_6ctabix_13TabixIterator_7next(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("next (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_13TabixIterator_6next(((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_6next(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_RefNannySetupContext("next", 0);
+
+  /* "pysam/ctabix.pyx":546
+ * 
+ *     def next(self):
+ *         return self.__next__()             # <<<<<<<<<<<<<<
+ * 
+ *     def __dealloc__(self):
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_next); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_r = __pyx_t_2;
+  __pyx_t_2 = 0;
+  goto __pyx_L0;
+
+  /* "pysam/ctabix.pyx":545
+ *         return _charptr_to_str(self.buffer.s, self.encoding)
+ * 
+ *     def next(self):             # <<<<<<<<<<<<<<
+ *         return self.__next__()
+ * 
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("pysam.ctabix.TabixIterator.next", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/ctabix.pyx":548
+ *         return self.__next__()
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
  *         if <void*>self.iterator != NULL:
@@ -5183,45 +5891,67 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13TabixIterator_2__next__(struct __pyx_
  */
 
 /* Python wrapper */
-static void __pyx_pw_5pysam_6ctabix_13TabixIterator_5__dealloc__(PyObject *__pyx_v_self); /*proto*/
-static void __pyx_pw_5pysam_6ctabix_13TabixIterator_5__dealloc__(PyObject *__pyx_v_self) {
+static void __pyx_pw_5pysam_6ctabix_13TabixIterator_9__dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pw_5pysam_6ctabix_13TabixIterator_9__dealloc__(PyObject *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0);
-  __pyx_pf_5pysam_6ctabix_13TabixIterator_4__dealloc__(((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_v_self));
+  __pyx_pf_5pysam_6ctabix_13TabixIterator_8__dealloc__(((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_v_self));
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
 }
 
-static void __pyx_pf_5pysam_6ctabix_13TabixIterator_4__dealloc__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self) {
+static void __pyx_pf_5pysam_6ctabix_13TabixIterator_8__dealloc__(struct __pyx_obj_5pysam_6ctabix_TabixIterator *__pyx_v_self) {
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("__dealloc__", 0);
 
-  /* "pysam/ctabix.pyx":471
+  /* "pysam/ctabix.pyx":549
  * 
  *     def __dealloc__(self):
  *         if <void*>self.iterator != NULL:             # <<<<<<<<<<<<<<
  *             tbx_itr_destroy(self.iterator)
- * 
+ *         if self.buffer.s != NULL:
  */
   __pyx_t_1 = ((((void *)__pyx_v_self->iterator) != NULL) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":472
+    /* "pysam/ctabix.pyx":550
  *     def __dealloc__(self):
  *         if <void*>self.iterator != NULL:
  *             tbx_itr_destroy(self.iterator)             # <<<<<<<<<<<<<<
- * 
- * 
+ *         if self.buffer.s != NULL:
+ *             free(self.buffer.s)
  */
     tbx_itr_destroy(__pyx_v_self->iterator);
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/ctabix.pyx":470
- *         return _charptr_to_str(self.buffer.s)
+  /* "pysam/ctabix.pyx":551
+ *         if <void*>self.iterator != NULL:
+ *             tbx_itr_destroy(self.iterator)
+ *         if self.buffer.s != NULL:             # <<<<<<<<<<<<<<
+ *             free(self.buffer.s)
+ * 
+ */
+  __pyx_t_1 = ((__pyx_v_self->buffer.s != NULL) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/ctabix.pyx":552
+ *             tbx_itr_destroy(self.iterator)
+ *         if self.buffer.s != NULL:
+ *             free(self.buffer.s)             # <<<<<<<<<<<<<<
+ * 
+ * 
+ */
+    free(__pyx_v_self->buffer.s);
+    goto __pyx_L4;
+  }
+  __pyx_L4:;
+
+  /* "pysam/ctabix.pyx":548
+ *         return self.__next__()
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
  *         if <void*>self.iterator != NULL:
@@ -5232,7 +5962,7 @@ static void __pyx_pf_5pysam_6ctabix_13TabixIterator_4__dealloc__(struct __pyx_ob
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/ctabix.pyx":478
+/* "pysam/ctabix.pyx":558
  *     '''empty iterator'''
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -5260,19 +5990,19 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13EmptyIterator___iter__(CYTHON_UNUSED
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
 
-  /* "pysam/ctabix.pyx":479
+  /* "pysam/ctabix.pyx":559
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
  * 
- * 
+ *     def next(self):
  */
   __Pyx_XDECREF(__pyx_r);
   __Pyx_INCREF(__pyx_v_self);
   __pyx_r = __pyx_v_self;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":478
+  /* "pysam/ctabix.pyx":558
  *     '''empty iterator'''
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -5287,8 +6017,8 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13EmptyIterator___iter__(CYTHON_UNUSED
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":482
- * 
+/* "pysam/ctabix.pyx":561
+ *         return self
  * 
  *     def next(self):             # <<<<<<<<<<<<<<
  *         raise StopIteration()
@@ -5319,21 +6049,21 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13EmptyIterator_2next(CYTHON_UNUSED PyO
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("next", 0);
 
-  /* "pysam/ctabix.pyx":483
+  /* "pysam/ctabix.pyx":562
  * 
  *     def next(self):
  *         raise StopIteration()             # <<<<<<<<<<<<<<
  * 
- * 
+ *     def __next__(self):
  */
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_StopIteration, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_StopIteration, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_Raise(__pyx_t_1, 0, 0, 0);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":482
- * 
+  /* "pysam/ctabix.pyx":561
+ *         return self
  * 
  *     def next(self):             # <<<<<<<<<<<<<<
  *         raise StopIteration()
@@ -5350,7 +6080,70 @@ static PyObject *__pyx_pf_5pysam_6ctabix_13EmptyIterator_2next(CYTHON_UNUSED PyO
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":492
+/* "pysam/ctabix.pyx":564
+ *         raise StopIteration()
+ * 
+ *     def __next__(self):             # <<<<<<<<<<<<<<
+ *         raise StopIteration()
+ * 
+ */
+
+/* Python wrapper */
+static PyObject *__pyx_pw_5pysam_6ctabix_13EmptyIterator_5__next__(PyObject *__pyx_self, PyObject *__pyx_v_self); /*proto*/
+static char __pyx_doc_5pysam_6ctabix_13EmptyIterator_4__next__[] = "EmptyIterator.__next__(self)";
+static PyMethodDef __pyx_mdef_5pysam_6ctabix_13EmptyIterator_5__next__ = {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_6ctabix_13EmptyIterator_5__next__, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_13EmptyIterator_4__next__)};
+static PyObject *__pyx_pw_5pysam_6ctabix_13EmptyIterator_5__next__(PyObject *__pyx_self, PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = 0;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("__next__ (wrapper)", 0);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_13EmptyIterator_4__next__(__pyx_self, ((PyObject *)__pyx_v_self));
+
+  /* function exit code */
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+static PyObject *__pyx_pf_5pysam_6ctabix_13EmptyIterator_4__next__(CYTHON_UNUSED PyObject *__pyx_self, CYTHON_UNUSED PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  PyObject *__pyx_t_1 = NULL;
+  int __pyx_lineno = 0;
+  const char *__pyx_filename = NULL;
+  int __pyx_clineno = 0;
+  __Pyx_RefNannySetupContext("__next__", 0);
+
+  /* "pysam/ctabix.pyx":565
+ * 
+ *     def __next__(self):
+ *         raise StopIteration()             # <<<<<<<<<<<<<<
+ * 
+ * 
+ */
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_StopIteration, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_Raise(__pyx_t_1, 0, 0, 0);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/ctabix.pyx":564
+ *         raise StopIteration()
+ * 
+ *     def __next__(self):             # <<<<<<<<<<<<<<
+ *         raise StopIteration()
+ * 
+ */
+
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("pysam.ctabix.EmptyIterator.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = NULL;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+/* "pysam/ctabix.pyx":576
  *     """
  * 
  *     def __init__(self,             # <<<<<<<<<<<<<<
@@ -5386,7 +6179,7 @@ static int __pyx_pw_5pysam_6ctabix_19TabixIteratorParsed_1__init__(PyObject *__p
         else goto __pyx_L5_argtuple_error;
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 1) {
       goto __pyx_L5_argtuple_error;
@@ -5397,13 +6190,13 @@ static int __pyx_pw_5pysam_6ctabix_19TabixIteratorParsed_1__init__(PyObject *__p
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.ctabix.TabixIteratorParsed.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_parser), __pyx_ptype_5pysam_6ctabix_Parser, 1, "parser", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 493; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_parser), __pyx_ptype_5pysam_6ctabix_Parser, 1, "parser", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_r = __pyx_pf_5pysam_6ctabix_19TabixIteratorParsed___init__(((struct __pyx_obj_5pysam_6ctabix_TabixIteratorParsed *)__pyx_v_self), __pyx_v_parser);
 
   /* function exit code */
@@ -5426,27 +6219,27 @@ static int __pyx_pf_5pysam_6ctabix_19TabixIteratorParsed___init__(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__init__", 0);
 
-  /* "pysam/ctabix.pyx":495
+  /* "pysam/ctabix.pyx":579
  *                  Parser parser):
  * 
  *         TabixIterator.__init__(self)             # <<<<<<<<<<<<<<
  *         self.parser = parser
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_TabixIterator)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_TabixIterator)), __pyx_n_s_init); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(((PyObject *)__pyx_v_self));
   PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self));
   __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/ctabix.pyx":496
+  /* "pysam/ctabix.pyx":580
  * 
  *         TabixIterator.__init__(self)
  *         self.parser = parser             # <<<<<<<<<<<<<<
@@ -5459,7 +6252,7 @@ static int __pyx_pf_5pysam_6ctabix_19TabixIteratorParsed___init__(struct __pyx_o
   __Pyx_DECREF(((PyObject *)__pyx_v_self->parser));
   __pyx_v_self->parser = __pyx_v_parser;
 
-  /* "pysam/ctabix.pyx":492
+  /* "pysam/ctabix.pyx":576
  *     """
  * 
  *     def __init__(self,             # <<<<<<<<<<<<<<
@@ -5481,7 +6274,7 @@ static int __pyx_pf_5pysam_6ctabix_19TabixIteratorParsed___init__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":498
+/* "pysam/ctabix.pyx":582
  *         self.parser = parser
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -5517,51 +6310,83 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19TabixIteratorParsed_2__next__(struct
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__next__", 0);
 
-  /* "pysam/ctabix.pyx":504
+  /* "pysam/ctabix.pyx":588
  *         """
  * 
  *         cdef int retval = self.__cnext__()             # <<<<<<<<<<<<<<
- *         if retval < 0:
- *             raise StopIteration
+ *         if retval == -5:
+ *             raise IOError("iteration on closed file")
  */
   __pyx_v_retval = ((struct __pyx_vtabstruct_5pysam_6ctabix_TabixIteratorParsed *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.__pyx___cnext__(((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)__pyx_v_self));
 
-  /* "pysam/ctabix.pyx":505
+  /* "pysam/ctabix.pyx":589
  * 
  *         cdef int retval = self.__cnext__()
- *         if retval < 0:             # <<<<<<<<<<<<<<
+ *         if retval == -5:             # <<<<<<<<<<<<<<
+ *             raise IOError("iteration on closed file")
+ *         elif retval < 0:
+ */
+  __pyx_t_1 = ((__pyx_v_retval == -5) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/ctabix.pyx":590
+ *         cdef int retval = self.__cnext__()
+ *         if retval == -5:
+ *             raise IOError("iteration on closed file")             # <<<<<<<<<<<<<<
+ *         elif retval < 0:
+ *             raise StopIteration
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __Pyx_Raise(__pyx_t_2, 0, 0, 0);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  }
+
+  /* "pysam/ctabix.pyx":591
+ *         if retval == -5:
+ *             raise IOError("iteration on closed file")
+ *         elif retval < 0:             # <<<<<<<<<<<<<<
  *             raise StopIteration
- *         return self.parser.parse(self.buffer.s, self.buffer.l)
+ * 
  */
   __pyx_t_1 = ((__pyx_v_retval < 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":506
- *         cdef int retval = self.__cnext__()
- *         if retval < 0:
+    /* "pysam/ctabix.pyx":592
+ *             raise IOError("iteration on closed file")
+ *         elif retval < 0:
  *             raise StopIteration             # <<<<<<<<<<<<<<
- *         return self.parser.parse(self.buffer.s, self.buffer.l)
  * 
+ *         return self.parser.parse(self.buffer.s,
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":507
- *         if retval < 0:
+  /* "pysam/ctabix.pyx":594
  *             raise StopIteration
- *         return self.parser.parse(self.buffer.s, self.buffer.l)             # <<<<<<<<<<<<<<
  * 
+ *         return self.parser.parse(self.buffer.s,             # <<<<<<<<<<<<<<
+ *                                  self.buffer.l)
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_6ctabix_Parser *)__pyx_v_self->parser->__pyx_vtab)->parse(__pyx_v_self->parser, __pyx_v_self->__pyx_base.buffer.s, __pyx_v_self->__pyx_base.buffer.l); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/ctabix.pyx":595
+ * 
+ *         return self.parser.parse(self.buffer.s,
+ *                                  self.buffer.l)             # <<<<<<<<<<<<<<
+ * 
+ * 
+ */
+  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_6ctabix_Parser *)__pyx_v_self->parser->__pyx_vtab)->parse(__pyx_v_self->parser, __pyx_v_self->__pyx_base.buffer.s, __pyx_v_self->__pyx_base.buffer.l); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":498
+  /* "pysam/ctabix.pyx":582
  *         self.parser = parser
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -5580,10 +6405,10 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19TabixIteratorParsed_2__next__(struct
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":511
+/* "pysam/ctabix.pyx":599
  * 
  * cdef class GZIterator:
- *     def __init__(self, filename, int buffer_size=65536):             # <<<<<<<<<<<<<<
+ *     def __init__(self, filename, int buffer_size=65536, encoding="ascii"):             # <<<<<<<<<<<<<<
  *         '''iterate line-by-line through gzip (or bgzip)
  *         compressed file.
  */
@@ -5597,6 +6422,7 @@ struct wrapperbase __pyx_wrapperbase_5pysam_6ctabix_10GZIterator___init__;
 static int __pyx_pw_5pysam_6ctabix_10GZIterator_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_filename = 0;
   int __pyx_v_buffer_size;
+  PyObject *__pyx_v_encoding = 0;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -5604,12 +6430,14 @@ static int __pyx_pw_5pysam_6ctabix_10GZIterator_1__init__(PyObject *__pyx_v_self
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__ (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_filename,&__pyx_n_s_buffer_size,0};
-    PyObject* values[2] = {0,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_filename,&__pyx_n_s_buffer_size,&__pyx_n_s_encoding,0};
+    PyObject* values[3] = {0,0,0};
+    values[2] = ((PyObject *)__pyx_n_s_ascii);
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
       const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
       switch (pos_args) {
+        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
         case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
         case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
         case  0: break;
@@ -5625,12 +6453,18 @@ static int __pyx_pw_5pysam_6ctabix_10GZIterator_1__init__(PyObject *__pyx_v_self
           PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_buffer_size);
           if (value) { values[1] = value; kw_args--; }
         }
+        case  2:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_encoding);
+          if (value) { values[2] = value; kw_args--; }
+        }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
         case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
         case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
         break;
@@ -5639,27 +6473,28 @@ static int __pyx_pw_5pysam_6ctabix_10GZIterator_1__init__(PyObject *__pyx_v_self
     }
     __pyx_v_filename = values[0];
     if (values[1]) {
-      __pyx_v_buffer_size = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_buffer_size == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_buffer_size = __Pyx_PyInt_As_int(values[1]); if (unlikely((__pyx_v_buffer_size == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
       __pyx_v_buffer_size = ((int)65536);
     }
+    __pyx_v_encoding = values[2];
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.ctabix.GZIterator.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_6ctabix_10GZIterator___init__(((struct __pyx_obj_5pysam_6ctabix_GZIterator *)__pyx_v_self), __pyx_v_filename, __pyx_v_buffer_size);
+  __pyx_r = __pyx_pf_5pysam_6ctabix_10GZIterator___init__(((struct __pyx_obj_5pysam_6ctabix_GZIterator *)__pyx_v_self), __pyx_v_filename, __pyx_v_buffer_size, __pyx_v_encoding);
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam_6ctabix_GZIterator *__pyx_v_self, PyObject *__pyx_v_filename, int __pyx_v_buffer_size) {
+static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam_6ctabix_GZIterator *__pyx_v_self, PyObject *__pyx_v_filename, int __pyx_v_buffer_size, PyObject *__pyx_v_encoding) {
   int __pyx_r;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
@@ -5674,85 +6509,85 @@ static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam
   __Pyx_RefNannySetupContext("__init__", 0);
   __Pyx_INCREF(__pyx_v_filename);
 
-  /* "pysam/ctabix.pyx":515
+  /* "pysam/ctabix.pyx":603
  *         compressed file.
  *         '''
  *         if not os.path.exists(filename):             # <<<<<<<<<<<<<<
  *             raise IOError("No such file or directory: %s" % filename)
  * 
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_path); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_path); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_exists); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_exists); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_filename);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_filename);
   __Pyx_GIVEREF(__pyx_v_filename);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_t_5 = ((!__pyx_t_4) != 0);
   if (__pyx_t_5) {
 
-    /* "pysam/ctabix.pyx":516
+    /* "pysam/ctabix.pyx":604
  *         '''
  *         if not os.path.exists(filename):
  *             raise IOError("No such file or directory: %s" % filename)             # <<<<<<<<<<<<<<
  * 
  *         filename = _encodeFilename(filename)
  */
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_No_such_file_or_directory_s, __pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_No_such_file_or_directory_s, __pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_3);
     __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":518
+  /* "pysam/ctabix.pyx":606
  *             raise IOError("No such file or directory: %s" % filename)
  * 
  *         filename = _encodeFilename(filename)             # <<<<<<<<<<<<<<
  *         self.gzipfile = gzopen(filename, "r")
  *         self._filename = filename
  */
-  __pyx_t_3 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF_SET(__pyx_v_filename, __pyx_t_3);
   __pyx_t_3 = 0;
 
-  /* "pysam/ctabix.pyx":519
+  /* "pysam/ctabix.pyx":607
  * 
  *         filename = _encodeFilename(filename)
  *         self.gzipfile = gzopen(filename, "r")             # <<<<<<<<<<<<<<
  *         self._filename = filename
  *         self.kstream = ks_init(self.gzipfile)
  */
-  __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_filename); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_self->gzipfile = gzopen(__pyx_t_6, __pyx_k_r);
 
-  /* "pysam/ctabix.pyx":520
+  /* "pysam/ctabix.pyx":608
  *         filename = _encodeFilename(filename)
  *         self.gzipfile = gzopen(filename, "r")
  *         self._filename = filename             # <<<<<<<<<<<<<<
  *         self.kstream = ks_init(self.gzipfile)
- * 
+ *         self.encoding = encoding
  */
   __Pyx_INCREF(__pyx_v_filename);
   __Pyx_GIVEREF(__pyx_v_filename);
@@ -5760,17 +6595,30 @@ static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam
   __Pyx_DECREF(__pyx_v_self->_filename);
   __pyx_v_self->_filename = __pyx_v_filename;
 
-  /* "pysam/ctabix.pyx":521
+  /* "pysam/ctabix.pyx":609
  *         self.gzipfile = gzopen(filename, "r")
  *         self._filename = filename
  *         self.kstream = ks_init(self.gzipfile)             # <<<<<<<<<<<<<<
+ *         self.encoding = encoding
  * 
- *         self.buffer.l = 0
  */
   __pyx_v_self->kstream = ks_init(__pyx_v_self->gzipfile);
 
-  /* "pysam/ctabix.pyx":523
+  /* "pysam/ctabix.pyx":610
+ *         self._filename = filename
  *         self.kstream = ks_init(self.gzipfile)
+ *         self.encoding = encoding             # <<<<<<<<<<<<<<
+ * 
+ *         self.buffer.l = 0
+ */
+  __Pyx_INCREF(__pyx_v_encoding);
+  __Pyx_GIVEREF(__pyx_v_encoding);
+  __Pyx_GOTREF(__pyx_v_self->encoding);
+  __Pyx_DECREF(__pyx_v_self->encoding);
+  __pyx_v_self->encoding = __pyx_v_encoding;
+
+  /* "pysam/ctabix.pyx":612
+ *         self.encoding = encoding
  * 
  *         self.buffer.l = 0             # <<<<<<<<<<<<<<
  *         self.buffer.m = 0
@@ -5778,7 +6626,7 @@ static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam
  */
   __pyx_v_self->buffer.l = 0;
 
-  /* "pysam/ctabix.pyx":524
+  /* "pysam/ctabix.pyx":613
  * 
  *         self.buffer.l = 0
  *         self.buffer.m = 0             # <<<<<<<<<<<<<<
@@ -5787,7 +6635,7 @@ static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam
  */
   __pyx_v_self->buffer.m = 0;
 
-  /* "pysam/ctabix.pyx":525
+  /* "pysam/ctabix.pyx":614
  *         self.buffer.l = 0
  *         self.buffer.m = 0
  *         self.buffer.s = <char*>malloc(buffer_size)             # <<<<<<<<<<<<<<
@@ -5796,10 +6644,10 @@ static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam
  */
   __pyx_v_self->buffer.s = ((char *)malloc(__pyx_v_buffer_size));
 
-  /* "pysam/ctabix.pyx":511
+  /* "pysam/ctabix.pyx":599
  * 
  * cdef class GZIterator:
- *     def __init__(self, filename, int buffer_size=65536):             # <<<<<<<<<<<<<<
+ *     def __init__(self, filename, int buffer_size=65536, encoding="ascii"):             # <<<<<<<<<<<<<<
  *         '''iterate line-by-line through gzip (or bgzip)
  *         compressed file.
  */
@@ -5819,7 +6667,7 @@ static int __pyx_pf_5pysam_6ctabix_10GZIterator___init__(struct __pyx_obj_5pysam
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":527
+/* "pysam/ctabix.pyx":616
  *         self.buffer.s = <char*>malloc(buffer_size)
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -5843,7 +6691,7 @@ static void __pyx_pf_5pysam_6ctabix_10GZIterator_2__dealloc__(struct __pyx_obj_5
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("__dealloc__", 0);
 
-  /* "pysam/ctabix.pyx":529
+  /* "pysam/ctabix.pyx":618
  *     def __dealloc__(self):
  *         '''close file.'''
  *         if self.gzipfile != NULL:             # <<<<<<<<<<<<<<
@@ -5853,37 +6701,59 @@ static void __pyx_pf_5pysam_6ctabix_10GZIterator_2__dealloc__(struct __pyx_obj_5
   __pyx_t_1 = ((__pyx_v_self->gzipfile != NULL) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":530
+    /* "pysam/ctabix.pyx":619
  *         '''close file.'''
  *         if self.gzipfile != NULL:
  *             gzclose(self.gzipfile)             # <<<<<<<<<<<<<<
  *             self.gzipfile = NULL
- *         free(self.buffer.s)
+ *         if self.buffer.s != NULL:
  */
     gzclose(__pyx_v_self->gzipfile);
 
-    /* "pysam/ctabix.pyx":531
+    /* "pysam/ctabix.pyx":620
  *         if self.gzipfile != NULL:
  *             gzclose(self.gzipfile)
  *             self.gzipfile = NULL             # <<<<<<<<<<<<<<
- *         free(self.buffer.s)
- * 
+ *         if self.buffer.s != NULL:
+ *             free(self.buffer.s)
  */
     __pyx_v_self->gzipfile = NULL;
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/ctabix.pyx":532
+  /* "pysam/ctabix.pyx":621
  *             gzclose(self.gzipfile)
  *             self.gzipfile = NULL
- *         free(self.buffer.s)             # <<<<<<<<<<<<<<
+ *         if self.buffer.s != NULL:             # <<<<<<<<<<<<<<
+ *             free(self.buffer.s)
+ *         ks_destroy(self.kstream)
+ */
+  __pyx_t_1 = ((__pyx_v_self->buffer.s != NULL) != 0);
+  if (__pyx_t_1) {
+
+    /* "pysam/ctabix.pyx":622
+ *             self.gzipfile = NULL
+ *         if self.buffer.s != NULL:
+ *             free(self.buffer.s)             # <<<<<<<<<<<<<<
+ *         ks_destroy(self.kstream)
+ * 
+ */
+    free(__pyx_v_self->buffer.s);
+    goto __pyx_L4;
+  }
+  __pyx_L4:;
+
+  /* "pysam/ctabix.pyx":623
+ *         if self.buffer.s != NULL:
+ *             free(self.buffer.s)
+ *         ks_destroy(self.kstream)             # <<<<<<<<<<<<<<
  * 
  *     def __iter__(self):
  */
-  free(__pyx_v_self->buffer.s);
+  ks_destroy(__pyx_v_self->kstream);
 
-  /* "pysam/ctabix.pyx":527
+  /* "pysam/ctabix.pyx":616
  *         self.buffer.s = <char*>malloc(buffer_size)
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
@@ -5895,8 +6765,8 @@ static void __pyx_pf_5pysam_6ctabix_10GZIterator_2__dealloc__(struct __pyx_obj_5
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/ctabix.pyx":534
- *         free(self.buffer.s)
+/* "pysam/ctabix.pyx":625
+ *         ks_destroy(self.kstream)
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
@@ -5921,7 +6791,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_10GZIterator_4__iter__(struct __pyx_obj
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
 
-  /* "pysam/ctabix.pyx":535
+  /* "pysam/ctabix.pyx":626
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
@@ -5933,8 +6803,8 @@ static PyObject *__pyx_pf_5pysam_6ctabix_10GZIterator_4__iter__(struct __pyx_obj
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":534
- *         free(self.buffer.s)
+  /* "pysam/ctabix.pyx":625
+ *         ks_destroy(self.kstream)
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
@@ -5948,7 +6818,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_10GZIterator_4__iter__(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":537
+/* "pysam/ctabix.pyx":628
  *         return self
  * 
  *     cdef int __cnext__(self):             # <<<<<<<<<<<<<<
@@ -5964,7 +6834,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
   int __pyx_t_1;
   __Pyx_RefNannySetupContext("__cnext__", 0);
 
-  /* "pysam/ctabix.pyx":538
+  /* "pysam/ctabix.pyx":629
  * 
  *     cdef int __cnext__(self):
  *         cdef int dret = 0             # <<<<<<<<<<<<<<
@@ -5973,7 +6843,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
  */
   __pyx_v_dret = 0;
 
-  /* "pysam/ctabix.pyx":539
+  /* "pysam/ctabix.pyx":630
  *     cdef int __cnext__(self):
  *         cdef int dret = 0
  *         cdef int retval = 0             # <<<<<<<<<<<<<<
@@ -5982,7 +6852,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
  */
   __pyx_v_retval = 0;
 
-  /* "pysam/ctabix.pyx":540
+  /* "pysam/ctabix.pyx":631
  *         cdef int dret = 0
  *         cdef int retval = 0
  *         while 1:             # <<<<<<<<<<<<<<
@@ -5991,7 +6861,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
  */
   while (1) {
 
-    /* "pysam/ctabix.pyx":541
+    /* "pysam/ctabix.pyx":632
  *         cdef int retval = 0
  *         while 1:
  *             retval = ks_getuntil(self.kstream, '\n', &self.buffer, &dret)             # <<<<<<<<<<<<<<
@@ -6000,7 +6870,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
  */
     __pyx_v_retval = ks_getuntil(__pyx_v_self->kstream, '\n', (&__pyx_v_self->buffer), (&__pyx_v_dret));
 
-    /* "pysam/ctabix.pyx":543
+    /* "pysam/ctabix.pyx":634
  *             retval = ks_getuntil(self.kstream, '\n', &self.buffer, &dret)
  * 
  *             if retval < 0:             # <<<<<<<<<<<<<<
@@ -6010,7 +6880,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
     __pyx_t_1 = ((__pyx_v_retval < 0) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/ctabix.pyx":544
+      /* "pysam/ctabix.pyx":635
  * 
  *             if retval < 0:
  *                 break             # <<<<<<<<<<<<<<
@@ -6020,7 +6890,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
       goto __pyx_L4_break;
     }
 
-    /* "pysam/ctabix.pyx":546
+    /* "pysam/ctabix.pyx":637
  *                 break
  * 
  *             return dret             # <<<<<<<<<<<<<<
@@ -6032,7 +6902,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
   }
   __pyx_L4_break:;
 
-  /* "pysam/ctabix.pyx":547
+  /* "pysam/ctabix.pyx":638
  * 
  *             return dret
  *         return -1             # <<<<<<<<<<<<<<
@@ -6042,7 +6912,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
   __pyx_r = -1;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":537
+  /* "pysam/ctabix.pyx":628
  *         return self
  * 
  *     cdef int __cnext__(self):             # <<<<<<<<<<<<<<
@@ -6056,7 +6926,7 @@ static int __pyx_f_5pysam_6ctabix_10GZIterator___cnext__(struct __pyx_obj_5pysam
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":549
+/* "pysam/ctabix.pyx":640
  *         return -1
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -6087,12 +6957,15 @@ static PyObject *__pyx_pf_5pysam_6ctabix_10GZIterator_6__next__(struct __pyx_obj
   __Pyx_RefNannyDeclarations
   int __pyx_t_1;
   PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
+  struct __pyx_opt_args_5pysam_6ctabix__force_str __pyx_t_5;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__next__", 0);
 
-  /* "pysam/ctabix.pyx":552
+  /* "pysam/ctabix.pyx":643
  *         """python version of next().
  *         """
  *         cdef int retval = self.__cnext__()             # <<<<<<<<<<<<<<
@@ -6101,42 +6974,50 @@ static PyObject *__pyx_pf_5pysam_6ctabix_10GZIterator_6__next__(struct __pyx_obj
  */
   __pyx_v_retval = ((struct __pyx_vtabstruct_5pysam_6ctabix_GZIterator *)__pyx_v_self->__pyx_vtab)->__pyx___cnext__(__pyx_v_self);
 
-  /* "pysam/ctabix.pyx":553
+  /* "pysam/ctabix.pyx":644
  *         """
  *         cdef int retval = self.__cnext__()
  *         if retval < 0:             # <<<<<<<<<<<<<<
  *             raise StopIteration
- *         return self.buffer.s
+ *         return _force_str(self.buffer.s, self.encoding)
  */
   __pyx_t_1 = ((__pyx_v_retval < 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":554
+    /* "pysam/ctabix.pyx":645
  *         cdef int retval = self.__cnext__()
  *         if retval < 0:
  *             raise StopIteration             # <<<<<<<<<<<<<<
- *         return self.buffer.s
+ *         return _force_str(self.buffer.s, self.encoding)
  * 
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":555
+  /* "pysam/ctabix.pyx":646
  *         if retval < 0:
  *             raise StopIteration
- *         return self.buffer.s             # <<<<<<<<<<<<<<
+ *         return _force_str(self.buffer.s, self.encoding)             # <<<<<<<<<<<<<<
  * 
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->buffer.s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->buffer.s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_r = __pyx_t_2;
-  __pyx_t_2 = 0;
+  __pyx_t_3 = __pyx_v_self->encoding;
+  __Pyx_INCREF(__pyx_t_3);
+  __pyx_t_5.__pyx_n = 1;
+  __pyx_t_5.encoding = __pyx_t_3;
+  __pyx_t_4 = __pyx_f_5pysam_6ctabix__force_str(__pyx_t_2, &__pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_r = __pyx_t_4;
+  __pyx_t_4 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":549
+  /* "pysam/ctabix.pyx":640
  *         return -1
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -6147,6 +7028,8 @@ static PyObject *__pyx_pf_5pysam_6ctabix_10GZIterator_6__next__(struct __pyx_obj
   /* function exit code */
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
   __Pyx_AddTraceback("pysam.ctabix.GZIterator.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
@@ -6155,7 +7038,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_10GZIterator_6__next__(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":563
+/* "pysam/ctabix.pyx":654
  *     '''
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -6191,7 +7074,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_14GZIteratorHead___next__(struct __pyx_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__next__", 0);
 
-  /* "pysam/ctabix.pyx":566
+  /* "pysam/ctabix.pyx":657
  *         """python version of next().
  *         """
  *         cdef int retval = self.__cnext__()             # <<<<<<<<<<<<<<
@@ -6200,7 +7083,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_14GZIteratorHead___next__(struct __pyx_
  */
   __pyx_v_retval = ((struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorHead *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.__pyx___cnext__(((struct __pyx_obj_5pysam_6ctabix_GZIterator *)__pyx_v_self));
 
-  /* "pysam/ctabix.pyx":567
+  /* "pysam/ctabix.pyx":658
  *         """
  *         cdef int retval = self.__cnext__()
  *         if retval < 0:             # <<<<<<<<<<<<<<
@@ -6210,7 +7093,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_14GZIteratorHead___next__(struct __pyx_
   __pyx_t_1 = ((__pyx_v_retval < 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":568
+    /* "pysam/ctabix.pyx":659
  *         cdef int retval = self.__cnext__()
  *         if retval < 0:
  *             raise StopIteration             # <<<<<<<<<<<<<<
@@ -6218,10 +7101,10 @@ static PyObject *__pyx_pf_5pysam_6ctabix_14GZIteratorHead___next__(struct __pyx_
  *             return self.buffer.s
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":569
+  /* "pysam/ctabix.pyx":660
  *         if retval < 0:
  *             raise StopIteration
  *         if self.buffer.s[0] == '#':             # <<<<<<<<<<<<<<
@@ -6231,7 +7114,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_14GZIteratorHead___next__(struct __pyx_
   __pyx_t_1 = (((__pyx_v_self->__pyx_base.buffer.s[0]) == '#') != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":570
+    /* "pysam/ctabix.pyx":661
  *             raise StopIteration
  *         if self.buffer.s[0] == '#':
  *             return self.buffer.s             # <<<<<<<<<<<<<<
@@ -6239,7 +7122,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_14GZIteratorHead___next__(struct __pyx_
  *             raise StopIteration
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->__pyx_base.buffer.s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_self->__pyx_base.buffer.s); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -6247,7 +7130,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_14GZIteratorHead___next__(struct __pyx_
   }
   /*else*/ {
 
-    /* "pysam/ctabix.pyx":572
+    /* "pysam/ctabix.pyx":663
  *             return self.buffer.s
  *         else:
  *             raise StopIteration             # <<<<<<<<<<<<<<
@@ -6255,10 +7138,10 @@ static PyObject *__pyx_pf_5pysam_6ctabix_14GZIteratorHead___next__(struct __pyx_
  * 
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 572; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":563
+  /* "pysam/ctabix.pyx":654
  *     '''
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -6277,7 +7160,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_14GZIteratorHead___next__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":580
+/* "pysam/ctabix.pyx":671
  *     '''
  * 
  *     def __init__(self, parser):             # <<<<<<<<<<<<<<
@@ -6313,7 +7196,7 @@ static int __pyx_pw_5pysam_6ctabix_16GZIteratorParsed_1__init__(PyObject *__pyx_
         else goto __pyx_L5_argtuple_error;
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 1) {
       goto __pyx_L5_argtuple_error;
@@ -6324,7 +7207,7 @@ static int __pyx_pw_5pysam_6ctabix_16GZIteratorParsed_1__init__(PyObject *__pyx_
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.ctabix.GZIteratorParsed.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -6346,14 +7229,14 @@ static int __pyx_pf_5pysam_6ctabix_16GZIteratorParsed___init__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__init__", 0);
 
-  /* "pysam/ctabix.pyx":581
+  /* "pysam/ctabix.pyx":672
  * 
  *     def __init__(self, parser):
  *         self.parser = parser             # <<<<<<<<<<<<<<
  * 
  *     def __next__(self):
  */
-  if (!(likely(((__pyx_v_parser) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_parser, __pyx_ptype_5pysam_6ctabix_Parser))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!(likely(((__pyx_v_parser) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_parser, __pyx_ptype_5pysam_6ctabix_Parser))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 672; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_1 = __pyx_v_parser;
   __Pyx_INCREF(__pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
@@ -6362,7 +7245,7 @@ static int __pyx_pf_5pysam_6ctabix_16GZIteratorParsed___init__(struct __pyx_obj_
   __pyx_v_self->parser = ((struct __pyx_obj_5pysam_6ctabix_Parser *)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":580
+  /* "pysam/ctabix.pyx":671
  *     '''
  * 
  *     def __init__(self, parser):             # <<<<<<<<<<<<<<
@@ -6382,7 +7265,7 @@ static int __pyx_pf_5pysam_6ctabix_16GZIteratorParsed___init__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":583
+/* "pysam/ctabix.pyx":674
  *         self.parser = parser
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -6418,7 +7301,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_16GZIteratorParsed_2__next__(struct __p
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__next__", 0);
 
-  /* "pysam/ctabix.pyx":586
+  /* "pysam/ctabix.pyx":677
  *         """python version of next().
  *         """
  *         cdef int retval = self.__cnext__()             # <<<<<<<<<<<<<<
@@ -6427,7 +7310,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_16GZIteratorParsed_2__next__(struct __p
  */
   __pyx_v_retval = ((struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorParsed *)__pyx_v_self->__pyx_base.__pyx_vtab)->__pyx_base.__pyx___cnext__(((struct __pyx_obj_5pysam_6ctabix_GZIterator *)__pyx_v_self));
 
-  /* "pysam/ctabix.pyx":587
+  /* "pysam/ctabix.pyx":678
  *         """
  *         cdef int retval = self.__cnext__()
  *         if retval < 0:             # <<<<<<<<<<<<<<
@@ -6437,7 +7320,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_16GZIteratorParsed_2__next__(struct __p
   __pyx_t_1 = ((__pyx_v_retval < 0) != 0);
   if (__pyx_t_1) {
 
-    /* "pysam/ctabix.pyx":588
+    /* "pysam/ctabix.pyx":679
  *         cdef int retval = self.__cnext__()
  *         if retval < 0:
  *             raise StopIteration             # <<<<<<<<<<<<<<
@@ -6445,10 +7328,10 @@ static PyObject *__pyx_pf_5pysam_6ctabix_16GZIteratorParsed_2__next__(struct __p
  *         return self.parser.parse(self.buffer.s,
  */
     __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 679; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":590
+  /* "pysam/ctabix.pyx":681
  *             raise StopIteration
  * 
  *         return self.parser.parse(self.buffer.s,             # <<<<<<<<<<<<<<
@@ -6457,20 +7340,20 @@ static PyObject *__pyx_pf_5pysam_6ctabix_16GZIteratorParsed_2__next__(struct __p
  */
   __Pyx_XDECREF(__pyx_r);
 
-  /* "pysam/ctabix.pyx":591
+  /* "pysam/ctabix.pyx":682
  * 
  *         return self.parser.parse(self.buffer.s,
  *                                  self.buffer.l)             # <<<<<<<<<<<<<<
  * 
  * 
  */
-  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_6ctabix_Parser *)__pyx_v_self->parser->__pyx_vtab)->parse(__pyx_v_self->parser, __pyx_v_self->__pyx_base.buffer.s, __pyx_v_self->__pyx_base.buffer.l); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_6ctabix_Parser *)__pyx_v_self->parser->__pyx_vtab)->parse(__pyx_v_self->parser, __pyx_v_self->__pyx_base.buffer.s, __pyx_v_self->__pyx_base.buffer.l); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":583
+  /* "pysam/ctabix.pyx":674
  *         self.parser = parser
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -6489,7 +7372,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_16GZIteratorParsed_2__next__(struct __p
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":594
+/* "pysam/ctabix.pyx":685
  * 
  * 
  * def tabix_compress(filename_in,             # <<<<<<<<<<<<<<
@@ -6515,7 +7398,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_1tabix_compress(PyObject *__pyx_self, P
     static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_filename_in,&__pyx_n_s_filename_out,&__pyx_n_s_force,0};
     PyObject* values[3] = {0,0,0};
 
-    /* "pysam/ctabix.pyx":596
+    /* "pysam/ctabix.pyx":687
  * def tabix_compress(filename_in,
  *                    filename_out,
  *                    force=False):             # <<<<<<<<<<<<<<
@@ -6541,7 +7424,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_1tabix_compress(PyObject *__pyx_self, P
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_filename_out)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("tabix_compress", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("tabix_compress", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (kw_args > 0) {
@@ -6550,7 +7433,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_1tabix_compress(PyObject *__pyx_self, P
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "tabix_compress") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "tabix_compress") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -6567,7 +7450,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_1tabix_compress(PyObject *__pyx_self, P
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("tabix_compress", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("tabix_compress", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.ctabix.tabix_compress", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -6575,7 +7458,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_1tabix_compress(PyObject *__pyx_self, P
   __pyx_L4_argument_unpacking_done:;
   __pyx_r = __pyx_pf_5pysam_6ctabix_tabix_compress(__pyx_self, __pyx_v_filename_in, __pyx_v_filename_out, __pyx_v_force);
 
-  /* "pysam/ctabix.pyx":594
+  /* "pysam/ctabix.pyx":685
  * 
  * 
  * def tabix_compress(filename_in,             # <<<<<<<<<<<<<<
@@ -6613,34 +7496,34 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("tabix_compress", 0);
 
-  /* "pysam/ctabix.pyx":603
+  /* "pysam/ctabix.pyx":694
  *     '''
  * 
  *     if not force and os.path.exists(filename_out ):             # <<<<<<<<<<<<<<
  *         raise IOError(
  *             "Filename '%s' already exists, use *force* to overwrite" % filename_out)
  */
-  __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_force); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_force); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_2 = (!__pyx_t_1);
   if (__pyx_t_2) {
-    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_path); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_path); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_exists); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_exists); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_INCREF(__pyx_v_filename_out);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_filename_out);
     __Pyx_GIVEREF(__pyx_v_filename_out);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __pyx_t_6 = __pyx_t_1;
   } else {
@@ -6648,85 +7531,85 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
   }
   if (__pyx_t_6) {
 
-    /* "pysam/ctabix.pyx":605
+    /* "pysam/ctabix.pyx":696
  *     if not force and os.path.exists(filename_out ):
  *         raise IOError(
  *             "Filename '%s' already exists, use *force* to overwrite" % filename_out)             # <<<<<<<<<<<<<<
  * 
  *     cdef int WINDOW_SIZE
  */
-    __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_Filename_s_already_exists_use_fo, __pyx_v_filename_out); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_Filename_s_already_exists_use_fo, __pyx_v_filename_out); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
 
-    /* "pysam/ctabix.pyx":604
+    /* "pysam/ctabix.pyx":695
  * 
  *     if not force and os.path.exists(filename_out ):
  *         raise IOError(             # <<<<<<<<<<<<<<
  *             "Filename '%s' already exists, use *force* to overwrite" % filename_out)
  * 
  */
-    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_5);
     __Pyx_GIVEREF(__pyx_t_5);
     __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_Raise(__pyx_t_5, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 604; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":614
+  /* "pysam/ctabix.pyx":705
  * 
  *     cdef int O_RDONLY
  *     O_RDONLY = os.O_RDONLY             # <<<<<<<<<<<<<<
  * 
  *     WINDOW_SIZE = 64 * 1024
  */
-  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_O_RDONLY); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_O_RDONLY); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_t_4); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_t_4); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __pyx_v_O_RDONLY = __pyx_t_7;
 
-  /* "pysam/ctabix.pyx":616
+  /* "pysam/ctabix.pyx":707
  *     O_RDONLY = os.O_RDONLY
  * 
  *     WINDOW_SIZE = 64 * 1024             # <<<<<<<<<<<<<<
  * 
- *     fn = _force_bytes(filename_out)
+ *     fn = _encodeFilename(filename_out)
  */
   __pyx_v_WINDOW_SIZE = 65536;
 
-  /* "pysam/ctabix.pyx":618
+  /* "pysam/ctabix.pyx":709
  *     WINDOW_SIZE = 64 * 1024
  * 
- *     fn = _force_bytes(filename_out)             # <<<<<<<<<<<<<<
+ *     fn = _encodeFilename(filename_out)             # <<<<<<<<<<<<<<
  *     fp = bgzf_open( fn, "w")
  *     if fp == NULL:
  */
-  __pyx_t_4 = __pyx_f_5pysam_6ctabix__force_bytes(__pyx_v_filename_out); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename_out); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __pyx_v_fn = ((PyObject*)__pyx_t_4);
   __pyx_t_4 = 0;
 
-  /* "pysam/ctabix.pyx":619
+  /* "pysam/ctabix.pyx":710
  * 
- *     fn = _force_bytes(filename_out)
+ *     fn = _encodeFilename(filename_out)
  *     fp = bgzf_open( fn, "w")             # <<<<<<<<<<<<<<
  *     if fp == NULL:
  *         raise IOError("could not open '%s' for writing")
  */
-  __pyx_t_8 = __Pyx_PyObject_AsString(__pyx_v_fn); if (unlikely((!__pyx_t_8) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_AsString(__pyx_v_fn); if (unlikely((!__pyx_t_8) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_fp = bgzf_open(__pyx_t_8, __pyx_k_w);
 
-  /* "pysam/ctabix.pyx":620
- *     fn = _force_bytes(filename_out)
+  /* "pysam/ctabix.pyx":711
+ *     fn = _encodeFilename(filename_out)
  *     fp = bgzf_open( fn, "w")
  *     if fp == NULL:             # <<<<<<<<<<<<<<
  *         raise IOError("could not open '%s' for writing")
@@ -6735,44 +7618,44 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
   __pyx_t_6 = ((__pyx_v_fp == NULL) != 0);
   if (__pyx_t_6) {
 
-    /* "pysam/ctabix.pyx":621
+    /* "pysam/ctabix.pyx":712
  *     fp = bgzf_open( fn, "w")
  *     if fp == NULL:
  *         raise IOError("could not open '%s' for writing")             # <<<<<<<<<<<<<<
  * 
- *     fn = _force_bytes(filename_in)
+ *     fn = _encodeFilename(filename_in)
  */
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_Raise(__pyx_t_4, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":623
+  /* "pysam/ctabix.pyx":714
  *         raise IOError("could not open '%s' for writing")
  * 
- *     fn = _force_bytes(filename_in)             # <<<<<<<<<<<<<<
+ *     fn = _encodeFilename(filename_in)             # <<<<<<<<<<<<<<
  *     fd_src = open(fn, O_RDONLY)
  *     if fd_src == 0:
  */
-  __pyx_t_4 = __pyx_f_5pysam_6ctabix__force_bytes(__pyx_v_filename_in); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename_in); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF_SET(__pyx_v_fn, ((PyObject*)__pyx_t_4));
   __pyx_t_4 = 0;
 
-  /* "pysam/ctabix.pyx":624
+  /* "pysam/ctabix.pyx":715
  * 
- *     fn = _force_bytes(filename_in)
+ *     fn = _encodeFilename(filename_in)
  *     fd_src = open(fn, O_RDONLY)             # <<<<<<<<<<<<<<
  *     if fd_src == 0:
  *         raise IOError("could not open '%s' for reading")
  */
-  __pyx_t_9 = __Pyx_PyObject_AsString(__pyx_v_fn); if (unlikely((!__pyx_t_9) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 624; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_AsString(__pyx_v_fn); if (unlikely((!__pyx_t_9) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_fd_src = open(__pyx_t_9, __pyx_v_O_RDONLY);
 
-  /* "pysam/ctabix.pyx":625
- *     fn = _force_bytes(filename_in)
+  /* "pysam/ctabix.pyx":716
+ *     fn = _encodeFilename(filename_in)
  *     fd_src = open(fn, O_RDONLY)
  *     if fd_src == 0:             # <<<<<<<<<<<<<<
  *         raise IOError("could not open '%s' for reading")
@@ -6781,21 +7664,21 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
   __pyx_t_6 = ((__pyx_v_fd_src == 0) != 0);
   if (__pyx_t_6) {
 
-    /* "pysam/ctabix.pyx":626
+    /* "pysam/ctabix.pyx":717
  *     fd_src = open(fn, O_RDONLY)
  *     if fd_src == 0:
  *         raise IOError("could not open '%s' for reading")             # <<<<<<<<<<<<<<
  * 
  *     buffer = malloc(WINDOW_SIZE)
  */
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__10, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__8, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_Raise(__pyx_t_4, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":628
+  /* "pysam/ctabix.pyx":719
  *         raise IOError("could not open '%s' for reading")
  * 
  *     buffer = malloc(WINDOW_SIZE)             # <<<<<<<<<<<<<<
@@ -6804,7 +7687,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
  */
   __pyx_v_buffer = malloc(__pyx_v_WINDOW_SIZE);
 
-  /* "pysam/ctabix.pyx":629
+  /* "pysam/ctabix.pyx":720
  * 
  *     buffer = malloc(WINDOW_SIZE)
  *     c = 1             # <<<<<<<<<<<<<<
@@ -6813,7 +7696,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
  */
   __pyx_v_c = 1;
 
-  /* "pysam/ctabix.pyx":631
+  /* "pysam/ctabix.pyx":722
  *     c = 1
  * 
  *     while c > 0:             # <<<<<<<<<<<<<<
@@ -6824,7 +7707,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
     __pyx_t_6 = ((__pyx_v_c > 0) != 0);
     if (!__pyx_t_6) break;
 
-    /* "pysam/ctabix.pyx":632
+    /* "pysam/ctabix.pyx":723
  * 
  *     while c > 0:
  *         c = read(fd_src, buffer, WINDOW_SIZE)             # <<<<<<<<<<<<<<
@@ -6833,7 +7716,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
  */
     __pyx_v_c = read(__pyx_v_fd_src, __pyx_v_buffer, __pyx_v_WINDOW_SIZE);
 
-    /* "pysam/ctabix.pyx":633
+    /* "pysam/ctabix.pyx":724
  *     while c > 0:
  *         c = read(fd_src, buffer, WINDOW_SIZE)
  *         r = bgzf_write(fp, buffer, c)             # <<<<<<<<<<<<<<
@@ -6842,7 +7725,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
  */
     __pyx_v_r = bgzf_write(__pyx_v_fp, __pyx_v_buffer, __pyx_v_c);
 
-    /* "pysam/ctabix.pyx":634
+    /* "pysam/ctabix.pyx":725
  *         c = read(fd_src, buffer, WINDOW_SIZE)
  *         r = bgzf_write(fp, buffer, c)
  *         if r < 0:             # <<<<<<<<<<<<<<
@@ -6852,7 +7735,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
     __pyx_t_6 = ((__pyx_v_r < 0) != 0);
     if (__pyx_t_6) {
 
-      /* "pysam/ctabix.pyx":635
+      /* "pysam/ctabix.pyx":726
  *         r = bgzf_write(fp, buffer, c)
  *         if r < 0:
  *             free(buffer)             # <<<<<<<<<<<<<<
@@ -6861,22 +7744,22 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
  */
       free(__pyx_v_buffer);
 
-      /* "pysam/ctabix.pyx":636
+      /* "pysam/ctabix.pyx":727
  *         if r < 0:
  *             free(buffer)
  *             raise OSError("writing failed")             # <<<<<<<<<<<<<<
  * 
  *     free(buffer)
  */
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_OSError, __pyx_tuple__11, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_OSError, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
       __Pyx_Raise(__pyx_t_4, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
 
-  /* "pysam/ctabix.pyx":638
+  /* "pysam/ctabix.pyx":729
  *             raise OSError("writing failed")
  * 
  *     free(buffer)             # <<<<<<<<<<<<<<
@@ -6885,7 +7768,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
  */
   free(__pyx_v_buffer);
 
-  /* "pysam/ctabix.pyx":639
+  /* "pysam/ctabix.pyx":730
  * 
  *     free(buffer)
  *     r = bgzf_close(fp)             # <<<<<<<<<<<<<<
@@ -6894,7 +7777,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
  */
   __pyx_v_r = bgzf_close(__pyx_v_fp);
 
-  /* "pysam/ctabix.pyx":640
+  /* "pysam/ctabix.pyx":731
  *     free(buffer)
  *     r = bgzf_close(fp)
  *     if r < 0:             # <<<<<<<<<<<<<<
@@ -6904,29 +7787,29 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
   __pyx_t_6 = ((__pyx_v_r < 0) != 0);
   if (__pyx_t_6) {
 
-    /* "pysam/ctabix.pyx":641
+    /* "pysam/ctabix.pyx":732
  *     r = bgzf_close(fp)
  *     if r < 0:
  *         raise OSError("writing to file %s failed" % filename_out)             # <<<<<<<<<<<<<<
  * 
  *     r = close(fd_src)
  */
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_writing_to_file_s_failed, __pyx_v_filename_out); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_writing_to_file_s_failed, __pyx_v_filename_out); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4);
     __Pyx_GIVEREF(__pyx_t_4);
     __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_OSError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_OSError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_Raise(__pyx_t_4, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":643
+  /* "pysam/ctabix.pyx":734
  *         raise OSError("writing to file %s failed" % filename_out)
  * 
  *     r = close(fd_src)             # <<<<<<<<<<<<<<
@@ -6935,7 +7818,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
  */
   __pyx_v_r = close(__pyx_v_fd_src);
 
-  /* "pysam/ctabix.pyx":644
+  /* "pysam/ctabix.pyx":735
  * 
  *     r = close(fd_src)
  *     if r < 0:             # <<<<<<<<<<<<<<
@@ -6945,29 +7828,29 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
   __pyx_t_6 = ((__pyx_v_r < 0) != 0);
   if (__pyx_t_6) {
 
-    /* "pysam/ctabix.pyx":645
+    /* "pysam/ctabix.pyx":736
  *     r = close(fd_src)
  *     if r < 0:
  *         raise OSError("error when closing file %s" % filename_in)             # <<<<<<<<<<<<<<
  * 
  * def tabix_index( filename,
  */
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_error_when_closing_file_s, __pyx_v_filename_in); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_error_when_closing_file_s, __pyx_v_filename_in); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4);
     __Pyx_GIVEREF(__pyx_t_4);
     __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_OSError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_OSError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_Raise(__pyx_t_4, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":594
+  /* "pysam/ctabix.pyx":685
  * 
  * 
  * def tabix_compress(filename_in,             # <<<<<<<<<<<<<<
@@ -6991,7 +7874,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_tabix_compress(CYTHON_UNUSED PyObject *
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":647
+/* "pysam/ctabix.pyx":738
  *         raise OSError("error when closing file %s" % filename_in)
  * 
  * def tabix_index( filename,             # <<<<<<<<<<<<<<
@@ -7023,7 +7906,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_3tabix_index(PyObject *__pyx_self, PyOb
     static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_filename,&__pyx_n_s_force,&__pyx_n_s_seq_col,&__pyx_n_s_start_col,&__pyx_n_s_end_col,&__pyx_n_s_preset,&__pyx_n_s_meta_char,&__pyx_n_s_zerobased,&__pyx_n_s_min_shift,0};
     PyObject* values[9] = {0,0,0,0,0,0,0,0,0};
 
-    /* "pysam/ctabix.pyx":648
+    /* "pysam/ctabix.pyx":739
  * 
  * def tabix_index( filename,
  *                  force = False,             # <<<<<<<<<<<<<<
@@ -7032,7 +7915,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_3tabix_index(PyObject *__pyx_self, PyOb
  */
     values[1] = ((PyObject *)Py_False);
 
-    /* "pysam/ctabix.pyx":649
+    /* "pysam/ctabix.pyx":740
  * def tabix_index( filename,
  *                  force = False,
  *                  seq_col = None,             # <<<<<<<<<<<<<<
@@ -7041,7 +7924,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_3tabix_index(PyObject *__pyx_self, PyOb
  */
     values[2] = ((PyObject *)Py_None);
 
-    /* "pysam/ctabix.pyx":650
+    /* "pysam/ctabix.pyx":741
  *                  force = False,
  *                  seq_col = None,
  *                  start_col = None,             # <<<<<<<<<<<<<<
@@ -7050,7 +7933,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_3tabix_index(PyObject *__pyx_self, PyOb
  */
     values[3] = ((PyObject *)Py_None);
 
-    /* "pysam/ctabix.pyx":651
+    /* "pysam/ctabix.pyx":742
  *                  seq_col = None,
  *                  start_col = None,
  *                  end_col = None,             # <<<<<<<<<<<<<<
@@ -7059,7 +7942,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_3tabix_index(PyObject *__pyx_self, PyOb
  */
     values[4] = ((PyObject *)Py_None);
 
-    /* "pysam/ctabix.pyx":652
+    /* "pysam/ctabix.pyx":743
  *                  start_col = None,
  *                  end_col = None,
  *                  preset = None,             # <<<<<<<<<<<<<<
@@ -7067,9 +7950,9 @@ static PyObject *__pyx_pw_5pysam_6ctabix_3tabix_index(PyObject *__pyx_self, PyOb
  *                  zerobased = False,
  */
     values[5] = ((PyObject *)Py_None);
-    values[6] = ((PyObject *)__pyx_kp_s__12);
+    values[6] = ((PyObject *)__pyx_kp_s__10);
 
-    /* "pysam/ctabix.pyx":654
+    /* "pysam/ctabix.pyx":745
  *                  preset = None,
  *                  meta_char = "#",
  *                  zerobased = False,             # <<<<<<<<<<<<<<
@@ -7141,7 +8024,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_3tabix_index(PyObject *__pyx_self, PyOb
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "tabix_index") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "tabix_index") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -7170,7 +8053,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_3tabix_index(PyObject *__pyx_self, PyOb
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("tabix_index", 0, 1, 9, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("tabix_index", 0, 1, 9, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.ctabix.tabix_index", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -7178,7 +8061,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_3tabix_index(PyObject *__pyx_self, PyOb
   __pyx_L4_argument_unpacking_done:;
   __pyx_r = __pyx_pf_5pysam_6ctabix_2tabix_index(__pyx_self, __pyx_v_filename, __pyx_v_force, __pyx_v_seq_col, __pyx_v_start_col, __pyx_v_end_col, __pyx_v_preset, __pyx_v_meta_char, __pyx_v_zerobased, __pyx_v_min_shift);
 
-  /* "pysam/ctabix.pyx":647
+  /* "pysam/ctabix.pyx":738
  *         raise OSError("error when closing file %s" % filename_in)
  * 
  * def tabix_index( filename,             # <<<<<<<<<<<<<<
@@ -7230,136 +8113,136 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   __Pyx_INCREF(__pyx_v_end_col);
   __Pyx_INCREF(__pyx_v_preset);
 
-  /* "pysam/ctabix.pyx":694
+  /* "pysam/ctabix.pyx":785
  *     '''
  * 
  *     if not os.path.exists(filename):             # <<<<<<<<<<<<<<
  *         raise IOError("No such file '%s'" % filename)
  * 
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_path); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_path); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_exists); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_exists); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_filename);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_filename);
   __Pyx_GIVEREF(__pyx_v_filename);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_t_5 = ((!__pyx_t_4) != 0);
   if (__pyx_t_5) {
 
-    /* "pysam/ctabix.pyx":695
+    /* "pysam/ctabix.pyx":786
  * 
  *     if not os.path.exists(filename):
  *         raise IOError("No such file '%s'" % filename)             # <<<<<<<<<<<<<<
  * 
- *     if preset == None and (seq_col == None or start_col == None or end_col == None):
+ *     if preset is None and \
  */
-    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_No_such_file_s, __pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyString_Format(__pyx_kp_s_No_such_file_s, __pyx_v_filename); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_3);
     __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":697
+  /* "pysam/ctabix.pyx":788
  *         raise IOError("No such file '%s'" % filename)
  * 
- *     if preset == None and (seq_col == None or start_col == None or end_col == None):             # <<<<<<<<<<<<<<
- *         raise ValueError("neither preset nor seq_col,start_col and end_col given" )
+ *     if preset is None and \             # <<<<<<<<<<<<<<
+ *        (seq_col is None or start_col is None or end_col is None):
+ *         raise ValueError(
+ */
+  __pyx_t_5 = (__pyx_v_preset == Py_None);
+  if ((__pyx_t_5 != 0)) {
+
+    /* "pysam/ctabix.pyx":789
  * 
+ *     if preset is None and \
+ *        (seq_col is None or start_col is None or end_col is None):             # <<<<<<<<<<<<<<
+ *         raise ValueError(
+ *             "neither preset nor seq_col,start_col and end_col given")
  */
-  __pyx_t_3 = PyObject_RichCompare(__pyx_v_preset, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  if (__pyx_t_5) {
-    __pyx_t_3 = PyObject_RichCompare(__pyx_v_seq_col, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    if (!__pyx_t_4) {
-      __pyx_t_3 = PyObject_RichCompare(__pyx_v_start_col, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      if (!__pyx_t_6) {
-        __pyx_t_3 = PyObject_RichCompare(__pyx_v_end_col, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-        __pyx_t_8 = __pyx_t_7;
+    __pyx_t_4 = (__pyx_v_seq_col == Py_None);
+    if (!(__pyx_t_4 != 0)) {
+      __pyx_t_6 = (__pyx_v_start_col == Py_None);
+      if (!(__pyx_t_6 != 0)) {
+        __pyx_t_7 = (__pyx_v_end_col == Py_None);
+        __pyx_t_8 = (__pyx_t_7 != 0);
       } else {
-        __pyx_t_8 = __pyx_t_6;
+        __pyx_t_8 = (__pyx_t_6 != 0);
       }
       __pyx_t_6 = __pyx_t_8;
     } else {
-      __pyx_t_6 = __pyx_t_4;
+      __pyx_t_6 = (__pyx_t_4 != 0);
     }
     __pyx_t_4 = __pyx_t_6;
   } else {
-    __pyx_t_4 = __pyx_t_5;
+    __pyx_t_4 = (__pyx_t_5 != 0);
   }
   if (__pyx_t_4) {
 
-    /* "pysam/ctabix.pyx":698
+    /* "pysam/ctabix.pyx":790
+ *     if preset is None and \
+ *        (seq_col is None or start_col is None or end_col is None):
+ *         raise ValueError(             # <<<<<<<<<<<<<<
+ *             "neither preset nor seq_col,start_col and end_col given")
  * 
- *     if preset == None and (seq_col == None or start_col == None or end_col == None):
- *         raise ValueError("neither preset nor seq_col,start_col and end_col given" )             # <<<<<<<<<<<<<<
- * 
- *     if not filename.endswith(".gz"):
  */
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__13, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__11, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_Raise(__pyx_t_3, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":700
- *         raise ValueError("neither preset nor seq_col,start_col and end_col given" )
+  /* "pysam/ctabix.pyx":793
+ *             "neither preset nor seq_col,start_col and end_col given")
  * 
  *     if not filename.endswith(".gz"):             # <<<<<<<<<<<<<<
- *         tabix_compress( filename, filename + ".gz", force = force )
+ *         tabix_compress(filename, filename + ".gz", force=force)
  *         os.unlink( filename )
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_endswith); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_filename, __pyx_n_s_endswith); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__14, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__12, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_5 = ((!__pyx_t_4) != 0);
   if (__pyx_t_5) {
 
-    /* "pysam/ctabix.pyx":701
+    /* "pysam/ctabix.pyx":794
  * 
  *     if not filename.endswith(".gz"):
- *         tabix_compress( filename, filename + ".gz", force = force )             # <<<<<<<<<<<<<<
+ *         tabix_compress(filename, filename + ".gz", force=force)             # <<<<<<<<<<<<<<
  *         os.unlink( filename )
  *         filename += ".gz"
  */
-    __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_tabix_compress); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_tabix_compress); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = PyNumber_Add(__pyx_v_filename, __pyx_kp_s_gz); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyNumber_Add(__pyx_v_filename, __pyx_kp_s_gz); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_v_filename);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_filename);
@@ -7367,47 +8250,47 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
     PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_3);
     __Pyx_GIVEREF(__pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_force, __pyx_v_force) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, __pyx_t_3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 701; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_force, __pyx_v_force) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, __pyx_t_3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-    /* "pysam/ctabix.pyx":702
+    /* "pysam/ctabix.pyx":795
  *     if not filename.endswith(".gz"):
- *         tabix_compress( filename, filename + ".gz", force = force )
+ *         tabix_compress(filename, filename + ".gz", force=force)
  *         os.unlink( filename )             # <<<<<<<<<<<<<<
  *         filename += ".gz"
  * 
  */
-    __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_unlink); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_unlink); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_INCREF(__pyx_v_filename);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_filename);
     __Pyx_GIVEREF(__pyx_v_filename);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/ctabix.pyx":703
- *         tabix_compress( filename, filename + ".gz", force = force )
+    /* "pysam/ctabix.pyx":796
+ *         tabix_compress(filename, filename + ".gz", force=force)
  *         os.unlink( filename )
  *         filename += ".gz"             # <<<<<<<<<<<<<<
  * 
- *     if not force and os.path.exists(filename + ".tbi" ):
+ *     if not force and os.path.exists(filename + ".tbi"):
  */
-    __pyx_t_1 = PyNumber_InPlaceAdd(__pyx_v_filename, __pyx_kp_s_gz); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_InPlaceAdd(__pyx_v_filename, __pyx_kp_s_gz); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF_SET(__pyx_v_filename, __pyx_t_1);
     __pyx_t_1 = 0;
@@ -7415,36 +8298,36 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   }
   __pyx_L5:;
 
-  /* "pysam/ctabix.pyx":705
+  /* "pysam/ctabix.pyx":798
  *         filename += ".gz"
  * 
- *     if not force and os.path.exists(filename + ".tbi" ):             # <<<<<<<<<<<<<<
- *         raise IOError( "Filename '%s.tbi' already exists, use *force* to overwrite" )
- * 
+ *     if not force and os.path.exists(filename + ".tbi"):             # <<<<<<<<<<<<<<
+ *         raise IOError(
+ *             "Filename '%s.tbi' already exists, use *force* to overwrite")
  */
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_v_force); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_v_force); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_4 = (!__pyx_t_5);
   if (__pyx_t_4) {
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_os); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_path); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_path); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_exists); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_exists); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = PyNumber_Add(__pyx_v_filename, __pyx_kp_s_tbi); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyNumber_Add(__pyx_v_filename, __pyx_kp_s_tbi); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_9);
     __Pyx_GIVEREF(__pyx_t_9);
     __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __pyx_t_6 = __pyx_t_5;
   } else {
@@ -7452,38 +8335,38 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   }
   if (__pyx_t_6) {
 
-    /* "pysam/ctabix.pyx":706
+    /* "pysam/ctabix.pyx":799
  * 
- *     if not force and os.path.exists(filename + ".tbi" ):
- *         raise IOError( "Filename '%s.tbi' already exists, use *force* to overwrite" )             # <<<<<<<<<<<<<<
+ *     if not force and os.path.exists(filename + ".tbi"):
+ *         raise IOError(             # <<<<<<<<<<<<<<
+ *             "Filename '%s.tbi' already exists, use *force* to overwrite")
  * 
- *     # columns (1-based):
  */
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__15, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_tuple__13, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_Raise(__pyx_t_9, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":712
+  /* "pysam/ctabix.pyx":806
  *     #     comments, lines to ignore at beginning
  *     # 0 is a missing column
  *     preset2conf = {             # <<<<<<<<<<<<<<
- *         'gff' : ( 0, 1, 4, 5, ord('#'), 0 ),
- *         'bed' : ( 0x10000, 1, 2, 3, ord('#'), 0 ),
+ *         'gff' : (0, 1, 4, 5, ord('#'), 0),
+ *         'bed' : (0x10000, 1, 2, 3, ord('#'), 0),
  */
-  __pyx_t_9 = PyDict_New(); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = PyDict_New(); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
 
-  /* "pysam/ctabix.pyx":713
+  /* "pysam/ctabix.pyx":807
  *     # 0 is a missing column
  *     preset2conf = {
- *         'gff' : ( 0, 1, 4, 5, ord('#'), 0 ),             # <<<<<<<<<<<<<<
- *         'bed' : ( 0x10000, 1, 2, 3, ord('#'), 0 ),
- *         'psltbl' : ( 0x10000, 15, 17, 18, ord('#'), 0 ),
+ *         'gff' : (0, 1, 4, 5, ord('#'), 0),             # <<<<<<<<<<<<<<
+ *         'bed' : (0x10000, 1, 2, 3, ord('#'), 0),
+ *         'psltbl' : (0x10000, 15, 17, 18, ord('#'), 0),
  */
-  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_int_0);
@@ -7503,17 +8386,17 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_3, 5, __pyx_int_0);
   __Pyx_GIVEREF(__pyx_int_0);
-  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_gff, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_gff, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/ctabix.pyx":714
+  /* "pysam/ctabix.pyx":808
  *     preset2conf = {
- *         'gff' : ( 0, 1, 4, 5, ord('#'), 0 ),
- *         'bed' : ( 0x10000, 1, 2, 3, ord('#'), 0 ),             # <<<<<<<<<<<<<<
- *         'psltbl' : ( 0x10000, 15, 17, 18, ord('#'), 0 ),
- *         'sam' : ( 1, 3, 4, 0, ord('@'), 0 ),
+ *         'gff' : (0, 1, 4, 5, ord('#'), 0),
+ *         'bed' : (0x10000, 1, 2, 3, ord('#'), 0),             # <<<<<<<<<<<<<<
+ *         'psltbl' : (0x10000, 15, 17, 18, ord('#'), 0),
+ *         'sam' : (1, 3, 4, 0, ord('@'), 0),
  */
-  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 808; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_int_65536);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_int_65536);
@@ -7533,17 +8416,17 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_3, 5, __pyx_int_0);
   __Pyx_GIVEREF(__pyx_int_0);
-  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_bed, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_bed, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/ctabix.pyx":715
- *         'gff' : ( 0, 1, 4, 5, ord('#'), 0 ),
- *         'bed' : ( 0x10000, 1, 2, 3, ord('#'), 0 ),
- *         'psltbl' : ( 0x10000, 15, 17, 18, ord('#'), 0 ),             # <<<<<<<<<<<<<<
- *         'sam' : ( 1, 3, 4, 0, ord('@'), 0 ),
- *         'vcf' : ( 2, 1, 2, 0, ord('#'), 0 ),
+  /* "pysam/ctabix.pyx":809
+ *         'gff' : (0, 1, 4, 5, ord('#'), 0),
+ *         'bed' : (0x10000, 1, 2, 3, ord('#'), 0),
+ *         'psltbl' : (0x10000, 15, 17, 18, ord('#'), 0),             # <<<<<<<<<<<<<<
+ *         'sam' : (1, 3, 4, 0, ord('@'), 0),
+ *         'vcf' : (2, 1, 2, 0, ord('#'), 0),
  */
-  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_int_65536);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_int_65536);
@@ -7563,17 +8446,17 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_3, 5, __pyx_int_0);
   __Pyx_GIVEREF(__pyx_int_0);
-  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_psltbl, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_psltbl, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/ctabix.pyx":716
- *         'bed' : ( 0x10000, 1, 2, 3, ord('#'), 0 ),
- *         'psltbl' : ( 0x10000, 15, 17, 18, ord('#'), 0 ),
- *         'sam' : ( 1, 3, 4, 0, ord('@'), 0 ),             # <<<<<<<<<<<<<<
- *         'vcf' : ( 2, 1, 2, 0, ord('#'), 0 ),
- *         'pileup': (3, 1, 2, 0, ord('#'), 0 ),
+  /* "pysam/ctabix.pyx":810
+ *         'bed' : (0x10000, 1, 2, 3, ord('#'), 0),
+ *         'psltbl' : (0x10000, 15, 17, 18, ord('#'), 0),
+ *         'sam' : (1, 3, 4, 0, ord('@'), 0),             # <<<<<<<<<<<<<<
+ *         'vcf' : (2, 1, 2, 0, ord('#'), 0),
+ *         'pileup': (3, 1, 2, 0, ord('#'), 0),
  */
-  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_int_1);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_int_1);
@@ -7593,17 +8476,17 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_3, 5, __pyx_int_0);
   __Pyx_GIVEREF(__pyx_int_0);
-  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_sam, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_sam, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/ctabix.pyx":717
- *         'psltbl' : ( 0x10000, 15, 17, 18, ord('#'), 0 ),
- *         'sam' : ( 1, 3, 4, 0, ord('@'), 0 ),
- *         'vcf' : ( 2, 1, 2, 0, ord('#'), 0 ),             # <<<<<<<<<<<<<<
- *         'pileup': (3, 1, 2, 0, ord('#'), 0 ),
+  /* "pysam/ctabix.pyx":811
+ *         'psltbl' : (0x10000, 15, 17, 18, ord('#'), 0),
+ *         'sam' : (1, 3, 4, 0, ord('@'), 0),
+ *         'vcf' : (2, 1, 2, 0, ord('#'), 0),             # <<<<<<<<<<<<<<
+ *         'pileup': (3, 1, 2, 0, ord('#'), 0),
  *         }
  */
-  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_int_2);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_int_2);
@@ -7623,17 +8506,17 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_3, 5, __pyx_int_0);
   __Pyx_GIVEREF(__pyx_int_0);
-  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_vcf, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_vcf, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/ctabix.pyx":718
- *         'sam' : ( 1, 3, 4, 0, ord('@'), 0 ),
- *         'vcf' : ( 2, 1, 2, 0, ord('#'), 0 ),
- *         'pileup': (3, 1, 2, 0, ord('#'), 0 ),             # <<<<<<<<<<<<<<
+  /* "pysam/ctabix.pyx":812
+ *         'sam' : (1, 3, 4, 0, ord('@'), 0),
+ *         'vcf' : (2, 1, 2, 0, ord('#'), 0),
+ *         'pileup': (3, 1, 2, 0, ord('#'), 0),             # <<<<<<<<<<<<<<
  *         }
  * 
  */
-  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_int_3);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_int_3);
@@ -7653,22 +8536,22 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   __Pyx_INCREF(__pyx_int_0);
   PyTuple_SET_ITEM(__pyx_t_3, 5, __pyx_int_0);
   __Pyx_GIVEREF(__pyx_int_0);
-  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_pileup, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_pileup, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_v_preset2conf = ((PyObject*)__pyx_t_9);
   __pyx_t_9 = 0;
 
-  /* "pysam/ctabix.pyx":721
+  /* "pysam/ctabix.pyx":815
  *         }
  * 
  *     if preset:             # <<<<<<<<<<<<<<
  *         try:
  *             conf_data = preset2conf[preset]
  */
-  __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_v_preset); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_v_preset); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 815; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_6) {
 
-    /* "pysam/ctabix.pyx":722
+    /* "pysam/ctabix.pyx":816
  * 
  *     if preset:
  *         try:             # <<<<<<<<<<<<<<
@@ -7682,14 +8565,14 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
       __Pyx_XGOTREF(__pyx_t_12);
       /*try:*/ {
 
-        /* "pysam/ctabix.pyx":723
+        /* "pysam/ctabix.pyx":817
  *     if preset:
  *         try:
  *             conf_data = preset2conf[preset]             # <<<<<<<<<<<<<<
  *         except KeyError:
  *             raise KeyError(
  */
-        __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_preset2conf, __pyx_v_preset); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 723; __pyx_clineno = __LINE__; goto __pyx_L8_error;};
+        __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_preset2conf, __pyx_v_preset); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L8_error;};
         __Pyx_GOTREF(__pyx_t_9);
         __pyx_v_conf_data = __pyx_t_9;
         __pyx_t_9 = 0;
@@ -7704,7 +8587,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
       __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
       __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-      /* "pysam/ctabix.pyx":724
+      /* "pysam/ctabix.pyx":818
  *         try:
  *             conf_data = preset2conf[preset]
  *         except KeyError:             # <<<<<<<<<<<<<<
@@ -7714,24 +8597,24 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
       __pyx_t_13 = PyErr_ExceptionMatches(__pyx_builtin_KeyError);
       if (__pyx_t_13) {
         __Pyx_AddTraceback("pysam.ctabix.tabix_index", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_3, &__pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+        if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_3, &__pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_GOTREF(__pyx_t_3);
         __Pyx_GOTREF(__pyx_t_1);
 
-        /* "pysam/ctabix.pyx":727
+        /* "pysam/ctabix.pyx":821
  *             raise KeyError(
  *                 "unknown preset '%s', valid presets are '%s'" %
  *                 (preset, ",".join(preset2conf.keys())))             # <<<<<<<<<<<<<<
  *     else:
  *         if end_col == None:
  */
-        __pyx_t_2 = __Pyx_PyDict_Keys(__pyx_v_preset2conf); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+        __pyx_t_2 = __Pyx_PyDict_Keys(__pyx_v_preset2conf); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_14 = __Pyx_PyString_Join(__pyx_kp_s__16, __pyx_t_2); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+        __pyx_t_14 = __Pyx_PyString_Join(__pyx_kp_s__14, __pyx_t_2); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
         __Pyx_GOTREF(__pyx_t_14);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_INCREF(__pyx_v_preset);
         PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_preset);
@@ -7740,35 +8623,35 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
         __Pyx_GIVEREF(__pyx_t_14);
         __pyx_t_14 = 0;
 
-        /* "pysam/ctabix.pyx":726
+        /* "pysam/ctabix.pyx":820
  *         except KeyError:
  *             raise KeyError(
  *                 "unknown preset '%s', valid presets are '%s'" %             # <<<<<<<<<<<<<<
  *                 (preset, ",".join(preset2conf.keys())))
  *     else:
  */
-        __pyx_t_14 = __Pyx_PyString_Format(__pyx_kp_s_unknown_preset_s_valid_presets_a, __pyx_t_2); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 726; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+        __pyx_t_14 = __Pyx_PyString_Format(__pyx_kp_s_unknown_preset_s_valid_presets_a, __pyx_t_2); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 820; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
         __Pyx_GOTREF(__pyx_t_14);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-        /* "pysam/ctabix.pyx":725
+        /* "pysam/ctabix.pyx":819
  *             conf_data = preset2conf[preset]
  *         except KeyError:
  *             raise KeyError(             # <<<<<<<<<<<<<<
  *                 "unknown preset '%s', valid presets are '%s'" %
  *                 (preset, ",".join(preset2conf.keys())))
  */
-        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 725; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+        __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
         __Pyx_GOTREF(__pyx_t_2);
         PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_14);
         __Pyx_GIVEREF(__pyx_t_14);
         __pyx_t_14 = 0;
-        __pyx_t_14 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 725; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+        __pyx_t_14 = __Pyx_PyObject_Call(__pyx_builtin_KeyError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
         __Pyx_GOTREF(__pyx_t_14);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __Pyx_Raise(__pyx_t_14, 0, 0, 0);
         __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 725; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -7792,19 +8675,19 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   }
   /*else*/ {
 
-    /* "pysam/ctabix.pyx":729
+    /* "pysam/ctabix.pyx":823
  *                 (preset, ",".join(preset2conf.keys())))
  *     else:
  *         if end_col == None:             # <<<<<<<<<<<<<<
  *             end_col = -1
  *         preset = 0
  */
-    __pyx_t_1 = PyObject_RichCompare(__pyx_v_end_col, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 729; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 729; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyObject_RichCompare(__pyx_v_end_col, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (__pyx_t_6) {
 
-      /* "pysam/ctabix.pyx":730
+      /* "pysam/ctabix.pyx":824
  *     else:
  *         if end_col == None:
  *             end_col = -1             # <<<<<<<<<<<<<<
@@ -7817,7 +8700,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
     }
     __pyx_L18:;
 
-    /* "pysam/ctabix.pyx":731
+    /* "pysam/ctabix.pyx":825
  *         if end_col == None:
  *             end_col = -1
  *         preset = 0             # <<<<<<<<<<<<<<
@@ -7827,24 +8710,24 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
     __Pyx_INCREF(__pyx_int_0);
     __Pyx_DECREF_SET(__pyx_v_preset, __pyx_int_0);
 
-    /* "pysam/ctabix.pyx":739
+    /* "pysam/ctabix.pyx":833
  *         # subtracted from the start coordinate. To avoid doing this,
  *         # set the TI_FLAG_UCSC=0x10000 flag:
  *         if zerobased:             # <<<<<<<<<<<<<<
  *             preset = preset | 0x10000
  * 
  */
-    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_v_zerobased); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 739; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_v_zerobased); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_6) {
 
-      /* "pysam/ctabix.pyx":740
+      /* "pysam/ctabix.pyx":834
  *         # set the TI_FLAG_UCSC=0x10000 flag:
  *         if zerobased:
  *             preset = preset | 0x10000             # <<<<<<<<<<<<<<
  * 
  *         conf_data = (preset, seq_col+1, start_col+1, end_col+1, ord(meta_char), 0)
  */
-      __pyx_t_1 = PyNumber_Or(__pyx_v_preset, __pyx_int_65536); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyNumber_Or(__pyx_v_preset, __pyx_int_65536); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF_SET(__pyx_v_preset, __pyx_t_1);
       __pyx_t_1 = 0;
@@ -7852,28 +8735,28 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
     }
     __pyx_L19:;
 
-    /* "pysam/ctabix.pyx":742
+    /* "pysam/ctabix.pyx":836
  *             preset = preset | 0x10000
  * 
  *         conf_data = (preset, seq_col+1, start_col+1, end_col+1, ord(meta_char), 0)             # <<<<<<<<<<<<<<
  * 
  *     cdef tbx_conf_t conf
  */
-    __pyx_t_1 = PyNumber_Add(__pyx_v_seq_col, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_Add(__pyx_v_seq_col, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = PyNumber_Add(__pyx_v_start_col, __pyx_int_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyNumber_Add(__pyx_v_start_col, __pyx_int_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_9 = PyNumber_Add(__pyx_v_end_col, __pyx_int_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyNumber_Add(__pyx_v_end_col, __pyx_int_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_14 = PyTuple_New(1); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_14 = PyTuple_New(1); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_14);
     __Pyx_INCREF(__pyx_v_meta_char);
     PyTuple_SET_ITEM(__pyx_t_14, 0, __pyx_v_meta_char);
     __Pyx_GIVEREF(__pyx_v_meta_char);
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_14, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_14, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
-    __pyx_t_14 = PyTuple_New(6); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_14 = PyTuple_New(6); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_14);
     __Pyx_INCREF(__pyx_v_preset);
     PyTuple_SET_ITEM(__pyx_t_14, 0, __pyx_v_preset);
@@ -7898,7 +8781,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   }
   __pyx_L7:;
 
-  /* "pysam/ctabix.pyx":745
+  /* "pysam/ctabix.pyx":839
  * 
  *     cdef tbx_conf_t conf
  *     conf.preset, conf.sc, conf.bc, conf.ec, conf.meta_char, conf.line_skip = conf_data             # <<<<<<<<<<<<<<
@@ -7915,7 +8798,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
     if (unlikely(size != 6)) {
       if (size > 6) __Pyx_RaiseTooManyValuesError(6);
       else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     #if CYTHON_COMPILING_IN_CPYTHON
     if (likely(PyTuple_CheckExact(sequence))) {
@@ -7944,7 +8827,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
       Py_ssize_t i;
       PyObject** temps[6] = {&__pyx_t_14,&__pyx_t_2,&__pyx_t_9,&__pyx_t_3,&__pyx_t_1,&__pyx_t_15};
       for (i=0; i < 6; i++) {
-        PyObject* item = PySequence_ITEM(sequence, i); if (unlikely(!item)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        PyObject* item = PySequence_ITEM(sequence, i); if (unlikely(!item)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(item);
         *(temps[i]) = item;
       }
@@ -7953,7 +8836,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   } else {
     Py_ssize_t index = -1;
     PyObject** temps[6] = {&__pyx_t_14,&__pyx_t_2,&__pyx_t_9,&__pyx_t_3,&__pyx_t_1,&__pyx_t_15};
-    __pyx_t_16 = PyObject_GetIter(__pyx_v_conf_data); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_16 = PyObject_GetIter(__pyx_v_conf_data); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_16);
     __pyx_t_17 = Py_TYPE(__pyx_t_16)->tp_iternext;
     for (index=0; index < 6; index++) {
@@ -7961,7 +8844,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
       __Pyx_GOTREF(item);
       *(temps[index]) = item;
     }
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_17(__pyx_t_16), 6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_IternextUnpackEndCheck(__pyx_t_17(__pyx_t_16), 6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_17 = NULL;
     __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
     goto __pyx_L21_unpacking_done;
@@ -7969,20 +8852,20 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
     __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
     __pyx_t_17 = NULL;
     if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_L21_unpacking_done:;
   }
-  __pyx_t_18 = __Pyx_PyInt_As_int32_t(__pyx_t_14); if (unlikely((__pyx_t_18 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_18 = __Pyx_PyInt_As_int32_t(__pyx_t_14); if (unlikely((__pyx_t_18 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
-  __pyx_t_19 = __Pyx_PyInt_As_int32_t(__pyx_t_2); if (unlikely((__pyx_t_19 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_19 = __Pyx_PyInt_As_int32_t(__pyx_t_2); if (unlikely((__pyx_t_19 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_20 = __Pyx_PyInt_As_int32_t(__pyx_t_9); if (unlikely((__pyx_t_20 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_20 = __Pyx_PyInt_As_int32_t(__pyx_t_9); if (unlikely((__pyx_t_20 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-  __pyx_t_21 = __Pyx_PyInt_As_int32_t(__pyx_t_3); if (unlikely((__pyx_t_21 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_21 = __Pyx_PyInt_As_int32_t(__pyx_t_3); if (unlikely((__pyx_t_21 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_22 = __Pyx_PyInt_As_int32_t(__pyx_t_1); if (unlikely((__pyx_t_22 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_22 = __Pyx_PyInt_As_int32_t(__pyx_t_1); if (unlikely((__pyx_t_22 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_23 = __Pyx_PyInt_As_int32_t(__pyx_t_15); if (unlikely((__pyx_t_23 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_23 = __Pyx_PyInt_As_int32_t(__pyx_t_15); if (unlikely((__pyx_t_23 == (int32_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;
   __pyx_v_conf.preset = __pyx_t_18;
   __pyx_v_conf.sc = __pyx_t_19;
@@ -7991,30 +8874,30 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   __pyx_v_conf.meta_char = __pyx_t_22;
   __pyx_v_conf.line_skip = __pyx_t_23;
 
-  /* "pysam/ctabix.pyx":748
+  /* "pysam/ctabix.pyx":842
  * 
  * 
  *     fn = _encodeFilename(filename)             # <<<<<<<<<<<<<<
  *     tbx_index_build(fn, min_shift, &conf)
  * 
  */
-  __pyx_t_15 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = __pyx_f_5pysam_6ctabix__encodeFilename(__pyx_v_filename); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_15);
   __pyx_v_fn = ((PyObject*)__pyx_t_15);
   __pyx_t_15 = 0;
 
-  /* "pysam/ctabix.pyx":749
+  /* "pysam/ctabix.pyx":843
  * 
  *     fn = _encodeFilename(filename)
  *     tbx_index_build(fn, min_shift, &conf)             # <<<<<<<<<<<<<<
  * 
  *     return filename
  */
-  __pyx_t_24 = __Pyx_PyObject_AsString(__pyx_v_fn); if (unlikely((!__pyx_t_24) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 749; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_v_min_shift); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 749; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_24 = __Pyx_PyObject_AsString(__pyx_v_fn); if (unlikely((!__pyx_t_24) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_13 = __Pyx_PyInt_As_int(__pyx_v_min_shift); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   tbx_index_build(__pyx_t_24, __pyx_t_13, (&__pyx_v_conf));
 
-  /* "pysam/ctabix.pyx":751
+  /* "pysam/ctabix.pyx":845
  *     tbx_index_build(fn, min_shift, &conf)
  * 
  *     return filename             # <<<<<<<<<<<<<<
@@ -8026,7 +8909,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   __pyx_r = __pyx_v_filename;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":647
+  /* "pysam/ctabix.pyx":738
  *         raise OSError("error when closing file %s" % filename_in)
  * 
  * def tabix_index( filename,             # <<<<<<<<<<<<<<
@@ -8057,7 +8940,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_2tabix_index(CYTHON_UNUSED PyObject *__
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":823
+/* "pysam/ctabix.pyx":917
  * ## Iterators for parsing through unindexed files.
  * #########################################################
  * cdef buildGzipError(void *gzfp):             # <<<<<<<<<<<<<<
@@ -8080,7 +8963,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_buildGzipError(void *__pyx_v_gzfp) {
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("buildGzipError", 0);
 
-  /* "pysam/ctabix.pyx":824
+  /* "pysam/ctabix.pyx":918
  * #########################################################
  * cdef buildGzipError(void *gzfp):
  *     cdef int errnum = 0             # <<<<<<<<<<<<<<
@@ -8089,7 +8972,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_buildGzipError(void *__pyx_v_gzfp) {
  */
   __pyx_v_errnum = 0;
 
-  /* "pysam/ctabix.pyx":825
+  /* "pysam/ctabix.pyx":919
  * cdef buildGzipError(void *gzfp):
  *     cdef int errnum = 0
  *     cdef char *s = gzerror(gzfp, &errnum)             # <<<<<<<<<<<<<<
@@ -8098,7 +8981,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_buildGzipError(void *__pyx_v_gzfp) {
  */
   __pyx_v_s = gzerror(__pyx_v_gzfp, (&__pyx_v_errnum));
 
-  /* "pysam/ctabix.pyx":826
+  /* "pysam/ctabix.pyx":920
  *     cdef int errnum = 0
  *     cdef char *s = gzerror(gzfp, &errnum)
  *     return "error (%d): %s (%d: %s)" % (errno, strerror(errno), errnum, s)             # <<<<<<<<<<<<<<
@@ -8106,15 +8989,15 @@ static PyObject *__pyx_f_5pysam_6ctabix_buildGzipError(void *__pyx_v_gzfp) {
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_int(errno); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_int(errno); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyBytes_FromString(strerror(errno)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyBytes_FromString(strerror(errno)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_errnum); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyInt_From_int(__pyx_v_errnum); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyBytes_FromString(__pyx_v_s); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = PyTuple_New(4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyTuple_New(4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
@@ -8128,14 +9011,14 @@ static PyObject *__pyx_f_5pysam_6ctabix_buildGzipError(void *__pyx_v_gzfp) {
   __pyx_t_2 = 0;
   __pyx_t_3 = 0;
   __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_error_d_s_d_s, __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_error_d_s_d_s, __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   __pyx_r = __pyx_t_4;
   __pyx_t_4 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":823
+  /* "pysam/ctabix.pyx":917
  * ## Iterators for parsing through unindexed files.
  * #########################################################
  * cdef buildGzipError(void *gzfp):             # <<<<<<<<<<<<<<
@@ -8158,7 +9041,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_buildGzipError(void *__pyx_v_gzfp) {
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":833
+/* "pysam/ctabix.pyx":927
  *     '''
  * 
  *     def __cinit__(self,             # <<<<<<<<<<<<<<
@@ -8199,7 +9082,7 @@ static int __pyx_pw_5pysam_6ctabix_19tabix_file_iterator_1__cinit__(PyObject *__
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_parser)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 927; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (kw_args > 0) {
@@ -8208,7 +9091,7 @@ static int __pyx_pw_5pysam_6ctabix_19tabix_file_iterator_1__cinit__(PyObject *__
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 927; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -8222,20 +9105,20 @@ static int __pyx_pw_5pysam_6ctabix_19tabix_file_iterator_1__cinit__(PyObject *__
     __pyx_v_infile = values[0];
     __pyx_v_parser = ((struct __pyx_obj_5pysam_6ctabix_Parser *)values[1]);
     if (values[2]) {
-      __pyx_v_buffer_size = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_buffer_size == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_buffer_size = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_buffer_size == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
     } else {
       __pyx_v_buffer_size = ((int)65536);
     }
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__cinit__", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 927; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.ctabix.tabix_file_iterator.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return -1;
   __pyx_L4_argument_unpacking_done:;
-  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_parser), __pyx_ptype_5pysam_6ctabix_Parser, 1, "parser", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_parser), __pyx_ptype_5pysam_6ctabix_Parser, 1, "parser", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 929; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_r = __pyx_pf_5pysam_6ctabix_19tabix_file_iterator___cinit__(((struct __pyx_obj_5pysam_6ctabix_tabix_file_iterator *)__pyx_v_self), __pyx_v_infile, __pyx_v_parser, __pyx_v_buffer_size);
 
   /* function exit code */
@@ -8260,35 +9143,35 @@ static int __pyx_pf_5pysam_6ctabix_19tabix_file_iterator___cinit__(struct __pyx_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__cinit__", 0);
 
-  /* "pysam/ctabix.pyx":838
+  /* "pysam/ctabix.pyx":932
  *                   int buffer_size=65536):
  * 
  *         if infile.closed:             # <<<<<<<<<<<<<<
- *             raise ValueError( "I/O operation on closed file." )
+ *             raise ValueError("I/O operation on closed file.")
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_infile, __pyx_n_s_closed); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_infile, __pyx_n_s_closed); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":839
+    /* "pysam/ctabix.pyx":933
  * 
  *         if infile.closed:
- *             raise ValueError( "I/O operation on closed file." )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file.")             # <<<<<<<<<<<<<<
  * 
  *         self.infile = infile
  */
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__15, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 933; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_Raise(__pyx_t_1, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 933; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":841
- *             raise ValueError( "I/O operation on closed file." )
+  /* "pysam/ctabix.pyx":935
+ *             raise ValueError("I/O operation on closed file.")
  * 
  *         self.infile = infile             # <<<<<<<<<<<<<<
  * 
@@ -8300,17 +9183,17 @@ static int __pyx_pf_5pysam_6ctabix_19tabix_file_iterator___cinit__(struct __pyx_
   __Pyx_DECREF(__pyx_v_self->infile);
   __pyx_v_self->infile = __pyx_v_infile;
 
-  /* "pysam/ctabix.pyx":843
+  /* "pysam/ctabix.pyx":937
  *         self.infile = infile
  * 
  *         cdef int fd = PyObject_AsFileDescriptor(infile)             # <<<<<<<<<<<<<<
  *         if fd == -1:
  *             raise ValueError("I/O operation on closed file.")
  */
-  __pyx_t_3 = PyObject_AsFileDescriptor(__pyx_v_infile); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_AsFileDescriptor(__pyx_v_infile); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_fd = __pyx_t_3;
 
-  /* "pysam/ctabix.pyx":844
+  /* "pysam/ctabix.pyx":938
  * 
  *         cdef int fd = PyObject_AsFileDescriptor(infile)
  *         if fd == -1:             # <<<<<<<<<<<<<<
@@ -8320,31 +9203,40 @@ static int __pyx_pf_5pysam_6ctabix_19tabix_file_iterator___cinit__(struct __pyx_
   __pyx_t_2 = ((__pyx_v_fd == -1) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":845
+    /* "pysam/ctabix.pyx":939
  *         cdef int fd = PyObject_AsFileDescriptor(infile)
  *         if fd == -1:
  *             raise ValueError("I/O operation on closed file.")             # <<<<<<<<<<<<<<
  * 
- *         # From the manual:
+ *         self.duplicated_fd = dup(fd)
  */
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__18, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_Raise(__pyx_t_1, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":852
+  /* "pysam/ctabix.pyx":941
+ *             raise ValueError("I/O operation on closed file.")
+ * 
+ *         self.duplicated_fd = dup(fd)             # <<<<<<<<<<<<<<
+ * 
+ *         # From the manual:
+ */
+  __pyx_v_self->duplicated_fd = dup(__pyx_v_fd);
+
+  /* "pysam/ctabix.pyx":948
  *         # When reading, this will be detected automatically by looking
  *         # for the magic two-byte gzip header.
- *         self.fh = gzdopen(fd, 'r')             # <<<<<<<<<<<<<<
+ *         self.fh = gzdopen(self.duplicated_fd, 'r')             # <<<<<<<<<<<<<<
  * 
  *         if self.fh == NULL:
  */
-  __pyx_v_self->fh = gzdopen(__pyx_v_fd, __pyx_k_r);
+  __pyx_v_self->fh = gzdopen(__pyx_v_self->duplicated_fd, __pyx_k_r);
 
-  /* "pysam/ctabix.pyx":854
- *         self.fh = gzdopen(fd, 'r')
+  /* "pysam/ctabix.pyx":950
+ *         self.fh = gzdopen(self.duplicated_fd, 'r')
  * 
  *         if self.fh == NULL:             # <<<<<<<<<<<<<<
  *             raise IOError('%s' % strerror(errno))
@@ -8353,32 +9245,32 @@ static int __pyx_pf_5pysam_6ctabix_19tabix_file_iterator___cinit__(struct __pyx_
   __pyx_t_2 = ((__pyx_v_self->fh == NULL) != 0);
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":855
+    /* "pysam/ctabix.pyx":951
  * 
  *         if self.fh == NULL:
  *             raise IOError('%s' % strerror(errno))             # <<<<<<<<<<<<<<
  * 
  *         self.kstream = ks_init(self.fh)
  */
-    __pyx_t_1 = __Pyx_PyBytes_FromString(strerror(errno)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyBytes_FromString(strerror(errno)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 951; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_s, __pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_s, __pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 951; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 951; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_4);
     __Pyx_GIVEREF(__pyx_t_4);
     __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_IOError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 951; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_Raise(__pyx_t_4, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 951; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":857
+  /* "pysam/ctabix.pyx":953
  *             raise IOError('%s' % strerror(errno))
  * 
  *         self.kstream = ks_init(self.fh)             # <<<<<<<<<<<<<<
@@ -8387,7 +9279,7 @@ static int __pyx_pf_5pysam_6ctabix_19tabix_file_iterator___cinit__(struct __pyx_
  */
   __pyx_v_self->kstream = ks_init(__pyx_v_self->fh);
 
-  /* "pysam/ctabix.pyx":859
+  /* "pysam/ctabix.pyx":955
  *         self.kstream = ks_init(self.fh)
  * 
  *         self.buffer.s = <char*>malloc(buffer_size)             # <<<<<<<<<<<<<<
@@ -8396,7 +9288,7 @@ static int __pyx_pf_5pysam_6ctabix_19tabix_file_iterator___cinit__(struct __pyx_
  */
   __pyx_v_self->buffer.s = ((char *)malloc(__pyx_v_buffer_size));
 
-  /* "pysam/ctabix.pyx":863
+  /* "pysam/ctabix.pyx":959
  *         #    raise MemoryError( "tabix_file_iterator: could not allocate %i bytes" % buffer_size)
  *         #self.size = buffer_size
  *         self.parser = parser             # <<<<<<<<<<<<<<
@@ -8409,7 +9301,7 @@ static int __pyx_pf_5pysam_6ctabix_19tabix_file_iterator___cinit__(struct __pyx_
   __Pyx_DECREF(((PyObject *)__pyx_v_self->parser));
   __pyx_v_self->parser = __pyx_v_parser;
 
-  /* "pysam/ctabix.pyx":833
+  /* "pysam/ctabix.pyx":927
  *     '''
  * 
  *     def __cinit__(self,             # <<<<<<<<<<<<<<
@@ -8430,7 +9322,7 @@ static int __pyx_pf_5pysam_6ctabix_19tabix_file_iterator___cinit__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":865
+/* "pysam/ctabix.pyx":961
  *         self.parser = parser
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -8456,7 +9348,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19tabix_file_iterator_2__iter__(struct
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
 
-  /* "pysam/ctabix.pyx":866
+  /* "pysam/ctabix.pyx":962
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
@@ -8468,7 +9360,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19tabix_file_iterator_2__iter__(struct
   __pyx_r = ((PyObject *)__pyx_v_self);
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":865
+  /* "pysam/ctabix.pyx":961
  *         self.parser = parser
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -8483,7 +9375,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19tabix_file_iterator_2__iter__(struct
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":868
+/* "pysam/ctabix.pyx":964
  *         return self
  * 
  *     cdef __cnext__(self):             # <<<<<<<<<<<<<<
@@ -8508,7 +9400,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__cnext__", 0);
 
-  /* "pysam/ctabix.pyx":871
+  /* "pysam/ctabix.pyx":967
  * 
  *         cdef char * b
  *         cdef int dret = 0             # <<<<<<<<<<<<<<
@@ -8517,7 +9409,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
  */
   __pyx_v_dret = 0;
 
-  /* "pysam/ctabix.pyx":872
+  /* "pysam/ctabix.pyx":968
  *         cdef char * b
  *         cdef int dret = 0
  *         cdef int retval = 0             # <<<<<<<<<<<<<<
@@ -8526,7 +9418,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
  */
   __pyx_v_retval = 0;
 
-  /* "pysam/ctabix.pyx":873
+  /* "pysam/ctabix.pyx":969
  *         cdef int dret = 0
  *         cdef int retval = 0
  *         while 1:             # <<<<<<<<<<<<<<
@@ -8535,7 +9427,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
  */
   while (1) {
 
-    /* "pysam/ctabix.pyx":875
+    /* "pysam/ctabix.pyx":971
  *         while 1:
  * 
  *             retval = ks_getuntil(self.kstream, '\n', &self.buffer, &dret)             # <<<<<<<<<<<<<<
@@ -8544,7 +9436,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
  */
     __pyx_v_retval = ks_getuntil(__pyx_v_self->kstream, '\n', (&__pyx_v_self->buffer), (&__pyx_v_dret));
 
-    /* "pysam/ctabix.pyx":877
+    /* "pysam/ctabix.pyx":973
  *             retval = ks_getuntil(self.kstream, '\n', &self.buffer, &dret)
  * 
  *             if retval < 0:             # <<<<<<<<<<<<<<
@@ -8554,7 +9446,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
     __pyx_t_1 = ((__pyx_v_retval < 0) != 0);
     if (__pyx_t_1) {
 
-      /* "pysam/ctabix.pyx":878
+      /* "pysam/ctabix.pyx":974
  * 
  *             if retval < 0:
  *                 break             # <<<<<<<<<<<<<<
@@ -8564,7 +9456,7 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
       goto __pyx_L4_break;
     }
 
-    /* "pysam/ctabix.pyx":881
+    /* "pysam/ctabix.pyx":977
  *                 #raise IOError('gzip error: %s' % buildGzipError( self.fh ))
  * 
  *             b = self.buffer.s             # <<<<<<<<<<<<<<
@@ -8574,24 +9466,32 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
     __pyx_t_2 = __pyx_v_self->buffer.s;
     __pyx_v_b = __pyx_t_2;
 
-    /* "pysam/ctabix.pyx":884
+    /* "pysam/ctabix.pyx":980
  * 
  *             # skip comments
- *             if (b[0] == '#'): continue             # <<<<<<<<<<<<<<
+ *             if (b[0] == '#'):             # <<<<<<<<<<<<<<
+ *                 continue
  * 
- *             # skip empty lines
  */
     __pyx_t_1 = (((__pyx_v_b[0]) == '#') != 0);
     if (__pyx_t_1) {
+
+      /* "pysam/ctabix.pyx":981
+ *             # skip comments
+ *             if (b[0] == '#'):
+ *                 continue             # <<<<<<<<<<<<<<
+ * 
+ *             # skip empty lines
+ */
       goto __pyx_L3_continue;
     }
 
-    /* "pysam/ctabix.pyx":887
+    /* "pysam/ctabix.pyx":984
  * 
  *             # skip empty lines
- *             if b[0] == '\0' or b[0] == '\n' or b[0] == '\r': continue             # <<<<<<<<<<<<<<
+ *             if b[0] == '\0' or b[0] == '\n' or b[0] == '\r':             # <<<<<<<<<<<<<<
+ *                 continue
  * 
- *             # gzgets terminates at \n, no need to test
  */
     __pyx_t_1 = (((__pyx_v_b[0]) == '\x00') != 0);
     if (!__pyx_t_1) {
@@ -8607,18 +9507,26 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
       __pyx_t_3 = __pyx_t_1;
     }
     if (__pyx_t_3) {
+
+      /* "pysam/ctabix.pyx":985
+ *             # skip empty lines
+ *             if b[0] == '\0' or b[0] == '\n' or b[0] == '\r':
+ *                 continue             # <<<<<<<<<<<<<<
+ * 
+ *             # gzgets terminates at \n, no need to test
+ */
       goto __pyx_L3_continue;
     }
 
-    /* "pysam/ctabix.pyx":892
+    /* "pysam/ctabix.pyx":990
  * 
  *             # parser creates a copy
- *             return self.parser.parse( b, self.buffer.l )             # <<<<<<<<<<<<<<
+ *             return self.parser.parse( b, self.buffer.l)             # <<<<<<<<<<<<<<
  * 
  *         raise StopIteration
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_6 = ((struct __pyx_vtabstruct_5pysam_6ctabix_Parser *)__pyx_v_self->parser->__pyx_vtab)->parse(__pyx_v_self->parser, __pyx_v_b, __pyx_v_self->buffer.l); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = ((struct __pyx_vtabstruct_5pysam_6ctabix_Parser *)__pyx_v_self->parser->__pyx_vtab)->parse(__pyx_v_self->parser, __pyx_v_b, __pyx_v_self->buffer.l); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __pyx_r = __pyx_t_6;
     __pyx_t_6 = 0;
@@ -8627,17 +9535,17 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
   }
   __pyx_L4_break:;
 
-  /* "pysam/ctabix.pyx":894
- *             return self.parser.parse( b, self.buffer.l )
+  /* "pysam/ctabix.pyx":992
+ *             return self.parser.parse( b, self.buffer.l)
  * 
  *         raise StopIteration             # <<<<<<<<<<<<<<
  * 
  *     def __dealloc__(self):
  */
   __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 992; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":868
+  /* "pysam/ctabix.pyx":964
  *         return self
  * 
  *     cdef __cnext__(self):             # <<<<<<<<<<<<<<
@@ -8656,12 +9564,12 @@ static PyObject *__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__(struct _
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":896
+/* "pysam/ctabix.pyx":994
  *         raise StopIteration
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
  *         free(self.buffer.s)
- * 
+ *         ks_destroy(self.kstream)
  */
 
 /* Python wrapper */
@@ -8679,29 +9587,47 @@ static void __pyx_pf_5pysam_6ctabix_19tabix_file_iterator_4__dealloc__(struct __
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__dealloc__", 0);
 
-  /* "pysam/ctabix.pyx":897
+  /* "pysam/ctabix.pyx":995
  * 
  *     def __dealloc__(self):
  *         free(self.buffer.s)             # <<<<<<<<<<<<<<
+ *         ks_destroy(self.kstream)
+ *         gzclose(self.fh)
+ */
+  free(__pyx_v_self->buffer.s);
+
+  /* "pysam/ctabix.pyx":996
+ *     def __dealloc__(self):
+ *         free(self.buffer.s)
+ *         ks_destroy(self.kstream)             # <<<<<<<<<<<<<<
+ *         gzclose(self.fh)
+ * 
+ */
+  ks_destroy(__pyx_v_self->kstream);
+
+  /* "pysam/ctabix.pyx":997
+ *         free(self.buffer.s)
+ *         ks_destroy(self.kstream)
+ *         gzclose(self.fh)             # <<<<<<<<<<<<<<
  * 
  *     def __next__(self):
  */
-  free(__pyx_v_self->buffer.s);
+  gzclose(__pyx_v_self->fh);
 
-  /* "pysam/ctabix.pyx":896
+  /* "pysam/ctabix.pyx":994
  *         raise StopIteration
  * 
  *     def __dealloc__(self):             # <<<<<<<<<<<<<<
  *         free(self.buffer.s)
- * 
+ *         ks_destroy(self.kstream)
  */
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
 }
 
-/* "pysam/ctabix.pyx":899
- *         free(self.buffer.s)
+/* "pysam/ctabix.pyx":999
+ *         gzclose(self.fh)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
  *         return self.__cnext__()
@@ -8730,7 +9656,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19tabix_file_iterator_6__next__(struct
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__next__", 0);
 
-  /* "pysam/ctabix.pyx":900
+  /* "pysam/ctabix.pyx":1000
  * 
  *     def __next__(self):
  *         return self.__cnext__()             # <<<<<<<<<<<<<<
@@ -8738,14 +9664,14 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19tabix_file_iterator_6__next__(struct
  *     def next(self):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator *)__pyx_v_self->__pyx_vtab)->__pyx___cnext__(__pyx_v_self); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator *)__pyx_v_self->__pyx_vtab)->__pyx___cnext__(__pyx_v_self); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":899
- *         free(self.buffer.s)
+  /* "pysam/ctabix.pyx":999
+ *         gzclose(self.fh)
  * 
  *     def __next__(self):             # <<<<<<<<<<<<<<
  *         return self.__cnext__()
@@ -8763,7 +9689,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19tabix_file_iterator_6__next__(struct
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":902
+/* "pysam/ctabix.pyx":1002
  *         return self.__cnext__()
  * 
  *     def next(self):             # <<<<<<<<<<<<<<
@@ -8794,7 +9720,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19tabix_file_iterator_8next(struct __py
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("next", 0);
 
-  /* "pysam/ctabix.pyx":903
+  /* "pysam/ctabix.pyx":1003
  * 
  *     def next(self):
  *         return self.__cnext__()             # <<<<<<<<<<<<<<
@@ -8802,13 +9728,13 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19tabix_file_iterator_8next(struct __py
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator *)__pyx_v_self->__pyx_vtab)->__pyx___cnext__(__pyx_v_self); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = ((struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator *)__pyx_v_self->__pyx_vtab)->__pyx___cnext__(__pyx_v_self); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1003; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":902
+  /* "pysam/ctabix.pyx":1002
  *         return self.__cnext__()
  * 
  *     def next(self):             # <<<<<<<<<<<<<<
@@ -8827,10 +9753,10 @@ static PyObject *__pyx_pf_5pysam_6ctabix_19tabix_file_iterator_8next(struct __py
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":911
+/* "pysam/ctabix.pyx":1011
  *     Permits the use of file-like objects for example from the gzip module.
  *     '''
- *     def __init__(self, infile, parser ):             # <<<<<<<<<<<<<<
+ *     def __init__(self, infile, parser):             # <<<<<<<<<<<<<<
  * 
  *         self.infile = infile
  */
@@ -8870,16 +9796,16 @@ static PyObject *__pyx_pw_5pysam_6ctabix_22tabix_generic_iterator_1__init__(PyOb
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_infile)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__init__", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_parser)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("__init__", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("__init__", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
       goto __pyx_L5_argtuple_error;
@@ -8894,7 +9820,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_22tabix_generic_iterator_1__init__(PyOb
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.ctabix.tabix_generic_iterator.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -8918,58 +9844,58 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator___init__(CYTHO
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__init__", 0);
 
-  /* "pysam/ctabix.pyx":913
- *     def __init__(self, infile, parser ):
+  /* "pysam/ctabix.pyx":1013
+ *     def __init__(self, infile, parser):
  * 
  *         self.infile = infile             # <<<<<<<<<<<<<<
  *         if self.infile.closed:
  *             raise ValueError("I/O operation on closed file.")
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_infile, __pyx_v_infile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 913; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_infile, __pyx_v_infile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":914
+  /* "pysam/ctabix.pyx":1014
  * 
  *         self.infile = infile
  *         if self.infile.closed:             # <<<<<<<<<<<<<<
  *             raise ValueError("I/O operation on closed file.")
  *         self.parser = parser
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_infile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_infile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_closed); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_closed); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_3) {
 
-    /* "pysam/ctabix.pyx":915
+    /* "pysam/ctabix.pyx":1015
  *         self.infile = infile
  *         if self.infile.closed:
  *             raise ValueError("I/O operation on closed file.")             # <<<<<<<<<<<<<<
  *         self.parser = parser
  * 
  */
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__19, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1015; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1015; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":916
+  /* "pysam/ctabix.pyx":1016
  *         if self.infile.closed:
  *             raise ValueError("I/O operation on closed file.")
  *         self.parser = parser             # <<<<<<<<<<<<<<
  * 
  *     def __iter__(self):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_parser, __pyx_v_parser) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 916; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_parser, __pyx_v_parser) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1016; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":911
+  /* "pysam/ctabix.pyx":1011
  *     Permits the use of file-like objects for example from the gzip module.
  *     '''
- *     def __init__(self, infile, parser ):             # <<<<<<<<<<<<<<
+ *     def __init__(self, infile, parser):             # <<<<<<<<<<<<<<
  * 
  *         self.infile = infile
  */
@@ -8988,7 +9914,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator___init__(CYTHO
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":918
+/* "pysam/ctabix.pyx":1018
  *         self.parser = parser
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -9016,7 +9942,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_2__iter__(CYTH
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__iter__", 0);
 
-  /* "pysam/ctabix.pyx":919
+  /* "pysam/ctabix.pyx":1019
  * 
  *     def __iter__(self):
  *         return self             # <<<<<<<<<<<<<<
@@ -9028,7 +9954,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_2__iter__(CYTH
   __pyx_r = __pyx_v_self;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":918
+  /* "pysam/ctabix.pyx":1018
  *         self.parser = parser
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
@@ -9043,7 +9969,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_2__iter__(CYTH
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":922
+/* "pysam/ctabix.pyx":1022
  * 
  *     # cython version - required for python 3
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -9070,49 +9996,78 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_4__next__(CYTH
   char *__pyx_v_b;
   char *__pyx_v_cpy;
   size_t __pyx_v_nbytes;
+  PyObject *__pyx_v_encoding = NULL;
   PyObject *__pyx_v_line = NULL;
   PyObject *__pyx_v_s = NULL;
+  PyObject *__pyx_v_bytes_cpy = NULL;
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
   int __pyx_t_3;
   int __pyx_t_4;
-  char *__pyx_t_5;
-  Py_ssize_t __pyx_t_6;
-  int __pyx_t_7;
+  struct __pyx_opt_args_5pysam_6ctabix__force_bytes __pyx_t_5;
+  char *__pyx_t_6;
+  Py_ssize_t __pyx_t_7;
   int __pyx_t_8;
-  PyObject *__pyx_t_9 = NULL;
+  int __pyx_t_9;
   PyObject *__pyx_t_10 = NULL;
+  PyObject *__pyx_t_11 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__next__", 0);
 
-  /* "pysam/ctabix.pyx":930
+  /* "pysam/ctabix.pyx":1028
+ *         cdef size_t nbytes
+ * 
+ *         encoding = self.parser.get_encoding()             # <<<<<<<<<<<<<<
+ * 
+ *         # note that GzipFile.close() does not close the file
+ */
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parser); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_get_encoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __pyx_v_encoding = __pyx_t_1;
+  __pyx_t_1 = 0;
+
+  /* "pysam/ctabix.pyx":1032
  *         # note that GzipFile.close() does not close the file
  *         # reading is still possible.
- *         if self.infile.closed: raise ValueError( "I/O operation on closed file." )             # <<<<<<<<<<<<<<
+ *         if self.infile.closed:             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file.")
  * 
- *         while 1:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_infile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_infile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_closed); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_closed); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1032; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_3) {
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__20, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/ctabix.pyx":1033
+ *         # reading is still possible.
+ *         if self.infile.closed:
+ *             raise ValueError("I/O operation on closed file.")             # <<<<<<<<<<<<<<
+ * 
+ *         while 1:
+ */
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__18, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1033; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_Raise(__pyx_t_2, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1033; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/ctabix.pyx":932
- *         if self.infile.closed: raise ValueError( "I/O operation on closed file." )
+  /* "pysam/ctabix.pyx":1035
+ *             raise ValueError("I/O operation on closed file.")
  * 
  *         while 1:             # <<<<<<<<<<<<<<
  * 
@@ -9120,72 +10075,82 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_4__next__(CYTH
  */
   while (1) {
 
-    /* "pysam/ctabix.pyx":934
+    /* "pysam/ctabix.pyx":1037
  *         while 1:
  * 
  *             line = self.infile.readline()             # <<<<<<<<<<<<<<
- *             if not line: break
- * 
+ *             if not line:
+ *                 break
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_infile); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 934; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_infile); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_readline); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 934; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_readline); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 934; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_XDECREF_SET(__pyx_v_line, __pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/ctabix.pyx":935
+    /* "pysam/ctabix.pyx":1038
  * 
  *             line = self.infile.readline()
- *             if not line: break             # <<<<<<<<<<<<<<
+ *             if not line:             # <<<<<<<<<<<<<<
+ *                 break
  * 
- *             s = _force_bytes( line )
  */
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_line); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 935; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_line); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1038; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_4 = ((!__pyx_t_3) != 0);
     if (__pyx_t_4) {
+
+      /* "pysam/ctabix.pyx":1039
+ *             line = self.infile.readline()
+ *             if not line:
+ *                 break             # <<<<<<<<<<<<<<
+ * 
+ *             s = _force_bytes(line, encoding)
+ */
       goto __pyx_L5_break;
     }
 
-    /* "pysam/ctabix.pyx":937
- *             if not line: break
+    /* "pysam/ctabix.pyx":1041
+ *                 break
  * 
- *             s = _force_bytes( line )             # <<<<<<<<<<<<<<
+ *             s = _force_bytes(line, encoding)             # <<<<<<<<<<<<<<
  *             b = s
- *             nbytes = len( line )
+ *             nbytes = len(line)
  */
-    __pyx_t_2 = __pyx_f_5pysam_6ctabix__force_bytes(__pyx_v_line); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5.__pyx_n = 1;
+    __pyx_t_5.encoding = __pyx_v_encoding;
+    __pyx_t_2 = __pyx_f_5pysam_6ctabix__force_bytes(__pyx_v_line, &__pyx_t_5); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1041; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_XDECREF_SET(__pyx_v_s, ((PyObject*)__pyx_t_2));
     __pyx_t_2 = 0;
 
-    /* "pysam/ctabix.pyx":938
+    /* "pysam/ctabix.pyx":1042
  * 
- *             s = _force_bytes( line )
+ *             s = _force_bytes(line, encoding)
  *             b = s             # <<<<<<<<<<<<<<
- *             nbytes = len( line )
+ *             nbytes = len(line)
  *             assert b[nbytes] == '\0'
  */
-    __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_s); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 938; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_b = __pyx_t_5;
+    __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_s); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_b = __pyx_t_6;
 
-    /* "pysam/ctabix.pyx":939
- *             s = _force_bytes( line )
+    /* "pysam/ctabix.pyx":1043
+ *             s = _force_bytes(line, encoding)
  *             b = s
- *             nbytes = len( line )             # <<<<<<<<<<<<<<
+ *             nbytes = len(line)             # <<<<<<<<<<<<<<
  *             assert b[nbytes] == '\0'
  * 
  */
-    __pyx_t_6 = PyObject_Length(__pyx_v_line); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_v_nbytes = __pyx_t_6;
+    __pyx_t_7 = PyObject_Length(__pyx_v_line); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1043; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_nbytes = __pyx_t_7;
 
-    /* "pysam/ctabix.pyx":940
+    /* "pysam/ctabix.pyx":1044
  *             b = s
- *             nbytes = len( line )
+ *             nbytes = len(line)
  *             assert b[nbytes] == '\0'             # <<<<<<<<<<<<<<
  * 
  *             # skip comments
@@ -9194,149 +10159,160 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_4__next__(CYTH
     if (unlikely(!Py_OptimizeFlag)) {
       if (unlikely(!(((__pyx_v_b[__pyx_v_nbytes]) == '\x00') != 0))) {
         PyErr_SetNone(PyExc_AssertionError);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
     }
     #endif
 
-    /* "pysam/ctabix.pyx":943
+    /* "pysam/ctabix.pyx":1047
  * 
  *             # skip comments
- *             if (b[0] == '#'): continue             # <<<<<<<<<<<<<<
+ *             if b[0] == '#':             # <<<<<<<<<<<<<<
+ *                 continue
  * 
- *             # skip empty lines
  */
     __pyx_t_4 = (((__pyx_v_b[0]) == '#') != 0);
     if (__pyx_t_4) {
+
+      /* "pysam/ctabix.pyx":1048
+ *             # skip comments
+ *             if b[0] == '#':
+ *                 continue             # <<<<<<<<<<<<<<
+ * 
+ *             # skip empty lines
+ */
       goto __pyx_L4_continue;
     }
 
-    /* "pysam/ctabix.pyx":946
+    /* "pysam/ctabix.pyx":1051
  * 
  *             # skip empty lines
- *             if b[0] == '\0' or b[0] == '\n' or b[0] == '\r': continue             # <<<<<<<<<<<<<<
+ *             if b[0] == '\0' or b[0] == '\n' or b[0] == '\r':             # <<<<<<<<<<<<<<
+ *                 continue
  * 
- *             # make sure that entry is complete
  */
     __pyx_t_4 = (((__pyx_v_b[0]) == '\x00') != 0);
     if (!__pyx_t_4) {
       __pyx_t_3 = (((__pyx_v_b[0]) == '\n') != 0);
       if (!__pyx_t_3) {
-        __pyx_t_7 = (((__pyx_v_b[0]) == '\r') != 0);
-        __pyx_t_8 = __pyx_t_7;
+        __pyx_t_8 = (((__pyx_v_b[0]) == '\r') != 0);
+        __pyx_t_9 = __pyx_t_8;
       } else {
-        __pyx_t_8 = __pyx_t_3;
+        __pyx_t_9 = __pyx_t_3;
       }
-      __pyx_t_3 = __pyx_t_8;
+      __pyx_t_3 = __pyx_t_9;
     } else {
       __pyx_t_3 = __pyx_t_4;
     }
     if (__pyx_t_3) {
+
+      /* "pysam/ctabix.pyx":1052
+ *             # skip empty lines
+ *             if b[0] == '\0' or b[0] == '\n' or b[0] == '\r':
+ *                 continue             # <<<<<<<<<<<<<<
+ * 
+ *             # make sure that entry is complete
+ */
       goto __pyx_L4_continue;
     }
 
-    /* "pysam/ctabix.pyx":949
+    /* "pysam/ctabix.pyx":1055
  * 
  *             # make sure that entry is complete
  *             if b[nbytes-1] != '\n' and b[nbytes-1] != '\r':             # <<<<<<<<<<<<<<
- *                 raise ValueError( "incomplete line at %s" % line )
+ *                 raise ValueError("incomplete line at %s" % line)
  * 
  */
     __pyx_t_3 = (((__pyx_v_b[(__pyx_v_nbytes - 1)]) != '\n') != 0);
     if (__pyx_t_3) {
       __pyx_t_4 = (((__pyx_v_b[(__pyx_v_nbytes - 1)]) != '\r') != 0);
-      __pyx_t_8 = __pyx_t_4;
+      __pyx_t_9 = __pyx_t_4;
     } else {
-      __pyx_t_8 = __pyx_t_3;
+      __pyx_t_9 = __pyx_t_3;
     }
-    if (__pyx_t_8) {
+    if (__pyx_t_9) {
 
-      /* "pysam/ctabix.pyx":950
+      /* "pysam/ctabix.pyx":1056
  *             # make sure that entry is complete
  *             if b[nbytes-1] != '\n' and b[nbytes-1] != '\r':
- *                 raise ValueError( "incomplete line at %s" % line )             # <<<<<<<<<<<<<<
+ *                 raise ValueError("incomplete line at %s" % line)             # <<<<<<<<<<<<<<
  * 
- *             # create a copy
+ *             bytes_cpy = <bytes> b
  */
-      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_incomplete_line_at_s, __pyx_v_line); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_incomplete_line_at_s, __pyx_v_line); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_Raise(__pyx_t_2, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/ctabix.pyx":953
+    /* "pysam/ctabix.pyx":1058
+ *                 raise ValueError("incomplete line at %s" % line)
  * 
- *             # create a copy
- *             cpy = <char*>malloc(nbytes+1)             # <<<<<<<<<<<<<<
- *             if cpy == NULL: raise MemoryError()
- *             memcpy( cpy, b, nbytes+1)
- */
-    __pyx_v_cpy = ((char *)malloc((__pyx_v_nbytes + 1)));
-
-    /* "pysam/ctabix.pyx":954
- *             # create a copy
- *             cpy = <char*>malloc(nbytes+1)
- *             if cpy == NULL: raise MemoryError()             # <<<<<<<<<<<<<<
- *             memcpy( cpy, b, nbytes+1)
+ *             bytes_cpy = <bytes> b             # <<<<<<<<<<<<<<
+ *             cpy = <char *> bytes_cpy
  * 
  */
-    __pyx_t_8 = ((__pyx_v_cpy == NULL) != 0);
-    if (__pyx_t_8) {
-      PyErr_NoMemory(); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 954; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    }
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1058; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __pyx_t_1 = __pyx_t_2;
+    __Pyx_INCREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+    __pyx_v_bytes_cpy = ((PyObject*)__pyx_t_1);
+    __pyx_t_1 = 0;
 
-    /* "pysam/ctabix.pyx":955
- *             cpy = <char*>malloc(nbytes+1)
- *             if cpy == NULL: raise MemoryError()
- *             memcpy( cpy, b, nbytes+1)             # <<<<<<<<<<<<<<
+    /* "pysam/ctabix.pyx":1059
+ * 
+ *             bytes_cpy = <bytes> b
+ *             cpy = <char *> bytes_cpy             # <<<<<<<<<<<<<<
  * 
  *             return self.parser(cpy, nbytes)
  */
-    memcpy(__pyx_v_cpy, __pyx_v_b, (__pyx_v_nbytes + 1));
+    __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_bytes_cpy); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1059; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_cpy = ((char *)__pyx_t_6);
 
-    /* "pysam/ctabix.pyx":957
- *             memcpy( cpy, b, nbytes+1)
+    /* "pysam/ctabix.pyx":1061
+ *             cpy = <char *> bytes_cpy
  * 
  *             return self.parser(cpy, nbytes)             # <<<<<<<<<<<<<<
  * 
  *         raise StopIteration
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parser); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_cpy); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parser); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1061; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = __Pyx_PyInt_FromSize_t(__pyx_v_nbytes); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyBytes_FromString(__pyx_v_cpy); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1061; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_2);
+    __pyx_t_10 = __Pyx_PyInt_FromSize_t(__pyx_v_nbytes); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1061; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_1);
-    __Pyx_GIVEREF(__pyx_t_1);
-    PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_t_9);
-    __Pyx_GIVEREF(__pyx_t_9);
-    __pyx_t_1 = 0;
-    __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_10, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_9);
-    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __pyx_r = __pyx_t_9;
-    __pyx_t_9 = 0;
+    __pyx_t_11 = PyTuple_New(2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1061; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_11);
+    PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_2);
+    __Pyx_GIVEREF(__pyx_t_2);
+    PyTuple_SET_ITEM(__pyx_t_11, 1, __pyx_t_10);
+    __Pyx_GIVEREF(__pyx_t_10);
+    __pyx_t_2 = 0;
+    __pyx_t_10 = 0;
+    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1061; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_10);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
+    __pyx_r = __pyx_t_10;
+    __pyx_t_10 = 0;
     goto __pyx_L0;
     __pyx_L4_continue:;
   }
   __pyx_L5_break:;
 
-  /* "pysam/ctabix.pyx":959
+  /* "pysam/ctabix.pyx":1063
  *             return self.parser(cpy, nbytes)
  * 
  *         raise StopIteration             # <<<<<<<<<<<<<<
@@ -9344,9 +10320,9 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_4__next__(CYTH
  *     # python version - required for python 2.7
  */
   __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 959; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1063; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":922
+  /* "pysam/ctabix.pyx":1022
  * 
  *     # cython version - required for python 3
  *     def __next__(self):             # <<<<<<<<<<<<<<
@@ -9358,19 +10334,21 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_4__next__(CYTH
   __pyx_L1_error:;
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
-  __Pyx_XDECREF(__pyx_t_9);
   __Pyx_XDECREF(__pyx_t_10);
+  __Pyx_XDECREF(__pyx_t_11);
   __Pyx_AddTraceback("pysam.ctabix.tabix_generic_iterator.__next__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_encoding);
   __Pyx_XDECREF(__pyx_v_line);
   __Pyx_XDECREF(__pyx_v_s);
+  __Pyx_XDECREF(__pyx_v_bytes_cpy);
   __Pyx_XGIVEREF(__pyx_r);
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":962
+/* "pysam/ctabix.pyx":1066
  * 
  *     # python version - required for python 2.7
  *     def next(self):             # <<<<<<<<<<<<<<
@@ -9403,24 +10381,24 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_6next(CYTHON_U
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("next", 0);
 
-  /* "pysam/ctabix.pyx":963
+  /* "pysam/ctabix.pyx":1067
  *     # python version - required for python 2.7
  *     def next(self):
  *         return self.__next__()             # <<<<<<<<<<<<<<
  * 
- * def tabix_iterator( infile, parser ):
+ * def tabix_iterator(infile, parser):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_next); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 963; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_next); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1067; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 963; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1067; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_r = __pyx_t_2;
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/ctabix.pyx":962
+  /* "pysam/ctabix.pyx":1066
  * 
  *     # python version - required for python 2.7
  *     def next(self):             # <<<<<<<<<<<<<<
@@ -9440,17 +10418,17 @@ static PyObject *__pyx_pf_5pysam_6ctabix_22tabix_generic_iterator_6next(CYTHON_U
   return __pyx_r;
 }
 
-/* "pysam/ctabix.pyx":965
+/* "pysam/ctabix.pyx":1069
  *         return self.__next__()
  * 
- * def tabix_iterator( infile, parser ):             # <<<<<<<<<<<<<<
- *     """return an iterator over all entries in a file."""
- *     if PYTHON3:
+ * def tabix_iterator(infile, parser):             # <<<<<<<<<<<<<<
+ *     """return an iterator over all entries in a file.
+ * 
  */
 
 /* Python wrapper */
 static PyObject *__pyx_pw_5pysam_6ctabix_5tabix_iterator(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_5pysam_6ctabix_4tabix_iterator[] = "tabix_iterator(infile, parser)\nreturn an iterator over all entries in a file.";
+static char __pyx_doc_5pysam_6ctabix_4tabix_iterator[] = "tabix_iterator(infile, parser)\nreturn an iterator over all entries in a file.\n    \n    Results are returned parsed as specified by the *parser*. If\n    *parser* is None, the results are returned as an unparsed string.\n    Otherwise, *parser* is assumed to be a functor that will return\n    parsed data (see for example :class:`~pysam.asTuple` and\n    :class:`~pysam.asGTF`).\n\n    ";
 static PyMethodDef __pyx_mdef_5pysam_6ctabix_5tabix_iterator = {__Pyx_NAMESTR("tabix_iterator"), (PyCFunction)__pyx_pw_5pysam_6ctabix_5tabix_iterator, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_4tabix_iterator)};
 static PyObject *__pyx_pw_5pysam_6ctabix_5tabix_iterator(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_infile = 0;
@@ -9481,11 +10459,11 @@ static PyObject *__pyx_pw_5pysam_6ctabix_5tabix_iterator(PyObject *__pyx_self, P
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_parser)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("tabix_iterator", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("tabix_iterator", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "tabix_iterator") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "tabix_iterator") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -9498,7 +10476,7 @@ static PyObject *__pyx_pw_5pysam_6ctabix_5tabix_iterator(PyObject *__pyx_self, P
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("tabix_iterator", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("tabix_iterator", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.ctabix.tabix_iterator", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -9523,30 +10501,30 @@ static PyObject *__pyx_pf_5pysam_6ctabix_4tabix_iterator(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("tabix_iterator", 0);
 
-  /* "pysam/ctabix.pyx":967
- * def tabix_iterator( infile, parser ):
- *     """return an iterator over all entries in a file."""
+  /* "pysam/ctabix.pyx":1079
+ * 
+ *     """
  *     if PYTHON3:             # <<<<<<<<<<<<<<
- *         return tabix_generic_iterator( infile, parser )
+ *         return tabix_generic_iterator(infile, parser)
  *     else:
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_PYTHON3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_PYTHON3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 967; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1079; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_2) {
 
-    /* "pysam/ctabix.pyx":968
- *     """return an iterator over all entries in a file."""
+    /* "pysam/ctabix.pyx":1080
+ *     """
  *     if PYTHON3:
- *         return tabix_generic_iterator( infile, parser )             # <<<<<<<<<<<<<<
+ *         return tabix_generic_iterator(infile, parser)             # <<<<<<<<<<<<<<
  *     else:
- *         return tabix_file_iterator( infile, parser )
+ *         return tabix_file_iterator(infile, parser)
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_tabix_generic_iterator); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_tabix_generic_iterator); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_infile);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_infile);
@@ -9554,7 +10532,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_4tabix_iterator(CYTHON_UNUSED PyObject
     __Pyx_INCREF(__pyx_v_parser);
     PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_v_parser);
     __Pyx_GIVEREF(__pyx_v_parser);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1080; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -9564,15 +10542,15 @@ static PyObject *__pyx_pf_5pysam_6ctabix_4tabix_iterator(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/ctabix.pyx":970
- *         return tabix_generic_iterator( infile, parser )
+    /* "pysam/ctabix.pyx":1082
+ *         return tabix_generic_iterator(infile, parser)
  *     else:
- *         return tabix_file_iterator( infile, parser )             # <<<<<<<<<<<<<<
+ *         return tabix_file_iterator(infile, parser)             # <<<<<<<<<<<<<<
  * 
  *     # file objects can use C stdio
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 970; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_INCREF(__pyx_v_infile);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_infile);
@@ -9580,7 +10558,7 @@ static PyObject *__pyx_pf_5pysam_6ctabix_4tabix_iterator(CYTHON_UNUSED PyObject
     __Pyx_INCREF(__pyx_v_parser);
     PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_v_parser);
     __Pyx_GIVEREF(__pyx_v_parser);
-    __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_tabix_file_iterator)), __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 970; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_6ctabix_tabix_file_iterator)), __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __pyx_r = __pyx_t_3;
@@ -9588,12 +10566,12 @@ static PyObject *__pyx_pf_5pysam_6ctabix_4tabix_iterator(CYTHON_UNUSED PyObject
     goto __pyx_L0;
   }
 
-  /* "pysam/ctabix.pyx":965
+  /* "pysam/ctabix.pyx":1069
  *         return self.__next__()
  * 
- * def tabix_iterator( infile, parser ):             # <<<<<<<<<<<<<<
- *     """return an iterator over all entries in a file."""
- *     if PYTHON3:
+ * def tabix_iterator(infile, parser):             # <<<<<<<<<<<<<<
+ *     """return an iterator over all entries in a file.
+ * 
  */
 
   /* function exit code */
@@ -9739,8 +10717,8 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_tabix_file_iterator = {
   #endif
 };
 
-static PyObject *__pyx_tp_new_5pysam_6ctabix_Tabixfile(PyTypeObject *t, PyObject *a, PyObject *k) {
-  struct __pyx_obj_5pysam_6ctabix_Tabixfile *p;
+static PyObject *__pyx_tp_new_5pysam_6ctabix_TabixFile(PyTypeObject *t, PyObject *a, PyObject *k) {
+  struct __pyx_obj_5pysam_6ctabix_TabixFile *p;
   PyObject *o;
   if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) {
     o = (*t->tp_alloc)(t, 0);
@@ -9748,16 +10726,19 @@ static PyObject *__pyx_tp_new_5pysam_6ctabix_Tabixfile(PyTypeObject *t, PyObject
     o = (PyObject *) PyBaseObject_Type.tp_new(t, __pyx_empty_tuple, 0);
   }
   if (unlikely(!o)) return 0;
-  p = ((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)o);
+  p = ((struct __pyx_obj_5pysam_6ctabix_TabixFile *)o);
+  p->_filename = Py_None; Py_INCREF(Py_None);
+  p->_filename_index = Py_None; Py_INCREF(Py_None);
   p->parser = ((struct __pyx_obj_5pysam_6ctabix_Parser *)Py_None); Py_INCREF(Py_None);
-  if (unlikely(__pyx_pw_5pysam_6ctabix_9Tabixfile_1__cinit__(o, a, k) < 0)) {
+  p->encoding = Py_None; Py_INCREF(Py_None);
+  if (unlikely(__pyx_pw_5pysam_6ctabix_9TabixFile_1__cinit__(o, a, k) < 0)) {
     Py_DECREF(o); o = 0;
   }
   return o;
 }
 
-static void __pyx_tp_dealloc_5pysam_6ctabix_Tabixfile(PyObject *o) {
-  struct __pyx_obj_5pysam_6ctabix_Tabixfile *p = (struct __pyx_obj_5pysam_6ctabix_Tabixfile *)o;
+static void __pyx_tp_dealloc_5pysam_6ctabix_TabixFile(PyObject *o) {
+  struct __pyx_obj_5pysam_6ctabix_TabixFile *p = (struct __pyx_obj_5pysam_6ctabix_TabixFile *)o;
   #if PY_VERSION_HEX >= 0x030400a1
   if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
@@ -9768,65 +10749,87 @@ static void __pyx_tp_dealloc_5pysam_6ctabix_Tabixfile(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_6ctabix_9Tabixfile_11__dealloc__(o);
+    __pyx_pw_5pysam_6ctabix_9TabixFile_13__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
+  Py_CLEAR(p->_filename);
+  Py_CLEAR(p->_filename_index);
   Py_CLEAR(p->parser);
+  Py_CLEAR(p->encoding);
   (*Py_TYPE(o)->tp_free)(o);
 }
 
-static int __pyx_tp_traverse_5pysam_6ctabix_Tabixfile(PyObject *o, visitproc v, void *a) {
+static int __pyx_tp_traverse_5pysam_6ctabix_TabixFile(PyObject *o, visitproc v, void *a) {
   int e;
-  struct __pyx_obj_5pysam_6ctabix_Tabixfile *p = (struct __pyx_obj_5pysam_6ctabix_Tabixfile *)o;
+  struct __pyx_obj_5pysam_6ctabix_TabixFile *p = (struct __pyx_obj_5pysam_6ctabix_TabixFile *)o;
+  if (p->_filename) {
+    e = (*v)(p->_filename, a); if (e) return e;
+  }
+  if (p->_filename_index) {
+    e = (*v)(p->_filename_index, a); if (e) return e;
+  }
   if (p->parser) {
     e = (*v)(((PyObject*)p->parser), a); if (e) return e;
   }
+  if (p->encoding) {
+    e = (*v)(p->encoding, a); if (e) return e;
+  }
   return 0;
 }
 
-static int __pyx_tp_clear_5pysam_6ctabix_Tabixfile(PyObject *o) {
+static int __pyx_tp_clear_5pysam_6ctabix_TabixFile(PyObject *o) {
   PyObject* tmp;
-  struct __pyx_obj_5pysam_6ctabix_Tabixfile *p = (struct __pyx_obj_5pysam_6ctabix_Tabixfile *)o;
+  struct __pyx_obj_5pysam_6ctabix_TabixFile *p = (struct __pyx_obj_5pysam_6ctabix_TabixFile *)o;
+  tmp = ((PyObject*)p->_filename);
+  p->_filename = Py_None; Py_INCREF(Py_None);
+  Py_XDECREF(tmp);
+  tmp = ((PyObject*)p->_filename_index);
+  p->_filename_index = Py_None; Py_INCREF(Py_None);
+  Py_XDECREF(tmp);
   tmp = ((PyObject*)p->parser);
   p->parser = ((struct __pyx_obj_5pysam_6ctabix_Parser *)Py_None); Py_INCREF(Py_None);
   Py_XDECREF(tmp);
+  tmp = ((PyObject*)p->encoding);
+  p->encoding = Py_None; Py_INCREF(Py_None);
+  Py_XDECREF(tmp);
   return 0;
 }
 
-static PyObject *__pyx_getprop_5pysam_6ctabix_9Tabixfile_filename(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_6ctabix_9Tabixfile_8filename_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_6ctabix_9TabixFile_filename(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_6ctabix_9TabixFile_8filename_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_6ctabix_9Tabixfile_header(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_6ctabix_9Tabixfile_6header_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_6ctabix_9TabixFile_header(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_6ctabix_9TabixFile_6header_1__get__(o);
 }
 
-static PyObject *__pyx_getprop_5pysam_6ctabix_9Tabixfile_contigs(PyObject *o, CYTHON_UNUSED void *x) {
-  return __pyx_pw_5pysam_6ctabix_9Tabixfile_7contigs_1__get__(o);
+static PyObject *__pyx_getprop_5pysam_6ctabix_9TabixFile_contigs(PyObject *o, CYTHON_UNUSED void *x) {
+  return __pyx_pw_5pysam_6ctabix_9TabixFile_7contigs_1__get__(o);
 }
 
-static PyMethodDef __pyx_methods_5pysam_6ctabix_Tabixfile[] = {
-  {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pw_5pysam_6ctabix_9Tabixfile_3_open, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_9Tabixfile_2_open)},
-  {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pw_5pysam_6ctabix_9Tabixfile_5_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_9Tabixfile_4_isOpen)},
-  {__Pyx_NAMESTR("fetch"), (PyCFunction)__pyx_pw_5pysam_6ctabix_9Tabixfile_7fetch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_9Tabixfile_6fetch)},
-  {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pw_5pysam_6ctabix_9Tabixfile_9close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_9Tabixfile_8close)},
+static PyMethodDef __pyx_methods_5pysam_6ctabix_TabixFile[] = {
+  {__Pyx_NAMESTR("_open"), (PyCFunction)__pyx_pw_5pysam_6ctabix_9TabixFile_3_open, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_9TabixFile_2_open)},
+  {__Pyx_NAMESTR("_dup"), (PyCFunction)__pyx_pw_5pysam_6ctabix_9TabixFile_5_dup, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_9TabixFile_4_dup)},
+  {__Pyx_NAMESTR("_isOpen"), (PyCFunction)__pyx_pw_5pysam_6ctabix_9TabixFile_7_isOpen, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_9TabixFile_6_isOpen)},
+  {__Pyx_NAMESTR("fetch"), (PyCFunction)__pyx_pw_5pysam_6ctabix_9TabixFile_9fetch, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_9TabixFile_8fetch)},
+  {__Pyx_NAMESTR("close"), (PyCFunction)__pyx_pw_5pysam_6ctabix_9TabixFile_11close, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_9TabixFile_10close)},
   {0, 0, 0, 0}
 };
 
-static struct PyGetSetDef __pyx_getsets_5pysam_6ctabix_Tabixfile[] = {
-  {(char *)"filename", __pyx_getprop_5pysam_6ctabix_9Tabixfile_filename, 0, __Pyx_DOCSTR(__pyx_k_filename_associated_with_this_ob), 0},
-  {(char *)"header", __pyx_getprop_5pysam_6ctabix_9Tabixfile_header, 0, __Pyx_DOCSTR(__pyx_k_the_file_header_note_The_header), 0},
-  {(char *)"contigs", __pyx_getprop_5pysam_6ctabix_9Tabixfile_contigs, 0, __Pyx_DOCSTR(__pyx_k_chromosome_names), 0},
+static struct PyGetSetDef __pyx_getsets_5pysam_6ctabix_TabixFile[] = {
+  {(char *)"filename", __pyx_getprop_5pysam_6ctabix_9TabixFile_filename, 0, __Pyx_DOCSTR(__pyx_k_filename_associated_with_this_ob), 0},
+  {(char *)"header", __pyx_getprop_5pysam_6ctabix_9TabixFile_header, 0, __Pyx_DOCSTR(__pyx_k_the_file_header_note_The_header), 0},
+  {(char *)"contigs", __pyx_getprop_5pysam_6ctabix_9TabixFile_contigs, 0, __Pyx_DOCSTR(__pyx_k_list_of_chromosome_names), 0},
   {0, 0, 0, 0, 0}
 };
 
-static PyTypeObject __pyx_type_5pysam_6ctabix_Tabixfile = {
+static PyTypeObject __pyx_type_5pysam_6ctabix_TabixFile = {
   PyVarObject_HEAD_INIT(0, 0)
-  __Pyx_NAMESTR("pysam.ctabix.Tabixfile"), /*tp_name*/
-  sizeof(struct __pyx_obj_5pysam_6ctabix_Tabixfile), /*tp_basicsize*/
+  __Pyx_NAMESTR("pysam.ctabix.TabixFile"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_6ctabix_TabixFile), /*tp_basicsize*/
   0, /*tp_itemsize*/
-  __pyx_tp_dealloc_5pysam_6ctabix_Tabixfile, /*tp_dealloc*/
+  __pyx_tp_dealloc_5pysam_6ctabix_TabixFile, /*tp_dealloc*/
   0, /*tp_print*/
   0, /*tp_getattr*/
   0, /*tp_setattr*/
@@ -9846,16 +10849,16 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_Tabixfile = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("*(filename, mode='r', parser = None)*\n\n    opens a :term:`tabix file` for reading. A missing\n    index (*filename* + \".tbi\") will raise an exception. *index*\n    specifies an alternative name of the index.\n\n    *parser* sets the default parser for this tabix file. If *parser*\n    is None, the results are returned as an unparsed string.\n    Otherwise, *parser* is assumed to be a functor that will return\n    parsed data (see for example :meth:`asTuple` and :meth: [...]
-  __pyx_tp_traverse_5pysam_6ctabix_Tabixfile, /*tp_traverse*/
-  __pyx_tp_clear_5pysam_6ctabix_Tabixfile, /*tp_clear*/
+  __Pyx_DOCSTR("*(filename, mode='r', parser = None)*\n\n    opens a :term:`tabix file` for reading. A missing\n    index (*filename* + \".tbi\") will raise an exception. *index*\n    specifies an alternative name of the index.\n\n    *parser* sets the default parser for this tabix file. If *parser*\n    is None, the results are returned as an unparsed string.\n    Otherwise, *parser* is assumed to be a functor that will return\n    parsed data (see for example :class:`~pysam.asTuple` an [...]
+  __pyx_tp_traverse_5pysam_6ctabix_TabixFile, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6ctabix_TabixFile, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
   0, /*tp_iternext*/
-  __pyx_methods_5pysam_6ctabix_Tabixfile, /*tp_methods*/
+  __pyx_methods_5pysam_6ctabix_TabixFile, /*tp_methods*/
   0, /*tp_members*/
-  __pyx_getsets_5pysam_6ctabix_Tabixfile, /*tp_getset*/
+  __pyx_getsets_5pysam_6ctabix_TabixFile, /*tp_getset*/
   0, /*tp_base*/
   0, /*tp_dict*/
   0, /*tp_descr_get*/
@@ -9863,7 +10866,7 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_Tabixfile = {
   0, /*tp_dictoffset*/
   0, /*tp_init*/
   0, /*tp_alloc*/
-  __pyx_tp_new_5pysam_6ctabix_Tabixfile, /*tp_new*/
+  __pyx_tp_new_5pysam_6ctabix_TabixFile, /*tp_new*/
   0, /*tp_free*/
   0, /*tp_is_gc*/
   0, /*tp_bases*/
@@ -9892,19 +10895,43 @@ static PyObject *__pyx_tp_new_5pysam_6ctabix_Parser(PyTypeObject *t, CYTHON_UNUS
   if (unlikely(!o)) return 0;
   p = ((struct __pyx_obj_5pysam_6ctabix_Parser *)o);
   p->__pyx_vtab = __pyx_vtabptr_5pysam_6ctabix_Parser;
+  p->encoding = Py_None; Py_INCREF(Py_None);
   return o;
 }
 
 static void __pyx_tp_dealloc_5pysam_6ctabix_Parser(PyObject *o) {
+  struct __pyx_obj_5pysam_6ctabix_Parser *p = (struct __pyx_obj_5pysam_6ctabix_Parser *)o;
   #if PY_VERSION_HEX >= 0x030400a1
-  if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {
+  if (unlikely(Py_TYPE(o)->tp_finalize) && !_PyGC_FINALIZED(o)) {
     if (PyObject_CallFinalizerFromDealloc(o)) return;
   }
   #endif
+  PyObject_GC_UnTrack(o);
+  Py_CLEAR(p->encoding);
   (*Py_TYPE(o)->tp_free)(o);
 }
 
+static int __pyx_tp_traverse_5pysam_6ctabix_Parser(PyObject *o, visitproc v, void *a) {
+  int e;
+  struct __pyx_obj_5pysam_6ctabix_Parser *p = (struct __pyx_obj_5pysam_6ctabix_Parser *)o;
+  if (p->encoding) {
+    e = (*v)(p->encoding, a); if (e) return e;
+  }
+  return 0;
+}
+
+static int __pyx_tp_clear_5pysam_6ctabix_Parser(PyObject *o) {
+  PyObject* tmp;
+  struct __pyx_obj_5pysam_6ctabix_Parser *p = (struct __pyx_obj_5pysam_6ctabix_Parser *)o;
+  tmp = ((PyObject*)p->encoding);
+  p->encoding = Py_None; Py_INCREF(Py_None);
+  Py_XDECREF(tmp);
+  return 0;
+}
+
 static PyMethodDef __pyx_methods_5pysam_6ctabix_Parser[] = {
+  {__Pyx_NAMESTR("set_encoding"), (PyCFunction)__pyx_pw_5pysam_6ctabix_6Parser_3set_encoding, METH_O, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_6Parser_2set_encoding)},
+  {__Pyx_NAMESTR("get_encoding"), (PyCFunction)__pyx_pw_5pysam_6ctabix_6Parser_5get_encoding, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_6Parser_4get_encoding)},
   {0, 0, 0, 0}
 };
 
@@ -9927,15 +10954,15 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_Parser = {
   0, /*tp_as_sequence*/
   0, /*tp_as_mapping*/
   0, /*tp_hash*/
-  __pyx_pw_5pysam_6ctabix_6Parser_1__call__, /*tp_call*/
+  __pyx_pw_5pysam_6ctabix_6Parser_7__call__, /*tp_call*/
   0, /*tp_str*/
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  0, /*tp_doc*/
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  __Pyx_DOCSTR("Parser(encoding='ascii')"), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_6ctabix_Parser, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6ctabix_Parser, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
@@ -9948,7 +10975,7 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_Parser = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  0, /*tp_init*/
+  __pyx_pw_5pysam_6ctabix_6Parser_1__init__, /*tp_init*/
   0, /*tp_alloc*/
   __pyx_tp_new_5pysam_6ctabix_Parser, /*tp_new*/
   0, /*tp_free*/
@@ -9997,7 +11024,7 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asTuple = {
   0, /*tp_as_mapping*/
   0, /*tp_hash*/
   #if CYTHON_COMPILING_IN_PYPY
-  __pyx_pw_5pysam_6ctabix_6Parser_1__call__, /*tp_call*/
+  __pyx_pw_5pysam_6ctabix_6Parser_7__call__, /*tp_call*/
   #else
   0, /*tp_call*/
   #endif
@@ -10005,10 +11032,10 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asTuple = {
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("converts a :term:`tabix row` into a python tuple.\n\n    Access is by numeric index.\n    "), /*tp_doc*/
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  __Pyx_DOCSTR("converts a :term:`tabix row` into a python tuple.\n\n    A field in a row is accessed by numeric index.\n    "), /*tp_doc*/
+  __pyx_tp_traverse_5pysam_6ctabix_Parser, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6ctabix_Parser, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
@@ -10021,7 +11048,11 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asTuple = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
+  #if CYTHON_COMPILING_IN_PYPY
+  __pyx_pw_5pysam_6ctabix_6Parser_1__init__, /*tp_init*/
+  #else
   0, /*tp_init*/
+  #endif
   0, /*tp_alloc*/
   __pyx_tp_new_5pysam_6ctabix_asTuple, /*tp_new*/
   0, /*tp_free*/
@@ -10070,7 +11101,7 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asGTF = {
   0, /*tp_as_mapping*/
   0, /*tp_hash*/
   #if CYTHON_COMPILING_IN_PYPY
-  __pyx_pw_5pysam_6ctabix_6Parser_1__call__, /*tp_call*/
+  __pyx_pw_5pysam_6ctabix_6Parser_7__call__, /*tp_call*/
   #else
   0, /*tp_call*/
   #endif
@@ -10078,10 +11109,10 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asGTF = {
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("converts a :term:`tabix row` into a GTF record with the following \n    fields:\n\n    contig\n       contig\n    feature\n       feature\n    source\n       source\n    start\n       genomic start coordinate (0-based)\n    end\n       genomic end coordinate plus one (0-based)\n    score\n       feature score\n    strand\n       strand\n    frame\n       frame\n    attributes\n       attribute string.\n\n    GTF formatted entries also defined the attributes:\n\n    gene_i [...]
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  __Pyx_DOCSTR("converts a :term:`tabix row` into a GTF record with the following\n    fields:\n   \n    +----------+----------+-------------------------------+\n    |*Column*  |*Name*    |*Content*                      |\n    +----------+----------+-------------------------------+\n    |1         |contig    |the chromosome name            |\n    +----------+----------+-------------------------------+\n    |2         |feature   |The feature type               |\n    +----------+--------- [...]
+  __pyx_tp_traverse_5pysam_6ctabix_Parser, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6ctabix_Parser, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
@@ -10094,7 +11125,11 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asGTF = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
+  #if CYTHON_COMPILING_IN_PYPY
+  __pyx_pw_5pysam_6ctabix_6Parser_1__init__, /*tp_init*/
+  #else
   0, /*tp_init*/
+  #endif
   0, /*tp_alloc*/
   __pyx_tp_new_5pysam_6ctabix_asGTF, /*tp_new*/
   0, /*tp_free*/
@@ -10143,7 +11178,7 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asBed = {
   0, /*tp_as_mapping*/
   0, /*tp_hash*/
   #if CYTHON_COMPILING_IN_PYPY
-  __pyx_pw_5pysam_6ctabix_6Parser_1__call__, /*tp_call*/
+  __pyx_pw_5pysam_6ctabix_6Parser_7__call__, /*tp_call*/
   #else
   0, /*tp_call*/
   #endif
@@ -10151,10 +11186,10 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asBed = {
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("converts a :term:`tabix row` into a bed record\n    with the following fields:\n\n    contig\n       contig\n    start\n       genomic start coordinate (zero-based)\n    end\n       genomic end coordinate plus one (zero-based)\n    name\n       name of feature.\n    score\n       score of feature\n    strand\n       strand of feature\n    thickStart\n       thickStart\n    thickEnd\n       thickEnd\n    itemRGB\n       itemRGB\n    blockCount\n       number of bocks\n     [...]
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  __Pyx_DOCSTR("converts a :term:`tabix row` into a bed record\n    with the following fields:\n\n    +-----------+-----------+------------------------------------------+\n    |*Column*   |*Field*    |*Contents*                                |\n    |           |           |                                          |\n    +-----------+-----------+------------------------------------------+\n    |1          |contig     |contig                                    |\n    |           |        [...]
+  __pyx_tp_traverse_5pysam_6ctabix_Parser, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6ctabix_Parser, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
@@ -10167,7 +11202,11 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asBed = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
+  #if CYTHON_COMPILING_IN_PYPY
+  __pyx_pw_5pysam_6ctabix_6Parser_1__init__, /*tp_init*/
+  #else
   0, /*tp_init*/
+  #endif
   0, /*tp_alloc*/
   __pyx_tp_new_5pysam_6ctabix_asBed, /*tp_new*/
   0, /*tp_free*/
@@ -10216,7 +11255,7 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asVCF = {
   0, /*tp_as_mapping*/
   0, /*tp_hash*/
   #if CYTHON_COMPILING_IN_PYPY
-  __pyx_pw_5pysam_6ctabix_6Parser_1__call__, /*tp_call*/
+  __pyx_pw_5pysam_6ctabix_6Parser_7__call__, /*tp_call*/
   #else
   0, /*tp_call*/
   #endif
@@ -10224,10 +11263,10 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asVCF = {
   0, /*tp_getattro*/
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/
-  __Pyx_DOCSTR("converts a :term:`tabix row` into a VCF record with\n    the following fields:\n    \n    contig\n       contig\n    pos\n       chromosomal position, zero-based\n    id \n       id\n    ref\n       reference\n    alt\n       alt\n    qual\n       qual\n    filter\n       filter\n    info\n       info\n    format\n       format specifier.\n\n    Access to genotypes is via index::\n\n        contig = vcf.contig\n        first_sample_genotype = vcf[0]\n        second_sample [...]
-  0, /*tp_traverse*/
-  0, /*tp_clear*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  __Pyx_DOCSTR("converts a :term:`tabix row` into a VCF record with\n    the following fields:\n    \n    +----------+---------+------------------------------------+\n    |*Column*  |*Field*  |*Contents*                          |\n    |          |         |                                    |\n    +----------+---------+------------------------------------+\n    |1         |contig   |chromosome                          |\n    +----------+---------+------------------------------------+\n [...]
+  __pyx_tp_traverse_5pysam_6ctabix_Parser, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6ctabix_Parser, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   0, /*tp_iter*/
@@ -10240,7 +11279,11 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_asVCF = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
+  #if CYTHON_COMPILING_IN_PYPY
+  __pyx_pw_5pysam_6ctabix_6Parser_1__init__, /*tp_init*/
+  #else
   0, /*tp_init*/
+  #endif
   0, /*tp_alloc*/
   __pyx_tp_new_5pysam_6ctabix_asVCF, /*tp_new*/
   0, /*tp_free*/
@@ -10271,7 +11314,8 @@ static PyObject *__pyx_tp_new_5pysam_6ctabix_TabixIterator(PyTypeObject *t, CYTH
   if (unlikely(!o)) return 0;
   p = ((struct __pyx_obj_5pysam_6ctabix_TabixIterator *)o);
   p->__pyx_vtab = __pyx_vtabptr_5pysam_6ctabix_TabixIterator;
-  p->tabixfile = ((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)Py_None); Py_INCREF(Py_None);
+  p->tabixfile = ((struct __pyx_obj_5pysam_6ctabix_TabixFile *)Py_None); Py_INCREF(Py_None);
+  p->encoding = Py_None; Py_INCREF(Py_None);
   return o;
 }
 
@@ -10287,11 +11331,12 @@ static void __pyx_tp_dealloc_5pysam_6ctabix_TabixIterator(PyObject *o) {
     PyObject *etype, *eval, *etb;
     PyErr_Fetch(&etype, &eval, &etb);
     ++Py_REFCNT(o);
-    __pyx_pw_5pysam_6ctabix_13TabixIterator_5__dealloc__(o);
+    __pyx_pw_5pysam_6ctabix_13TabixIterator_9__dealloc__(o);
     --Py_REFCNT(o);
     PyErr_Restore(etype, eval, etb);
   }
   Py_CLEAR(p->tabixfile);
+  Py_CLEAR(p->encoding);
   (*Py_TYPE(o)->tp_free)(o);
 }
 
@@ -10301,6 +11346,9 @@ static int __pyx_tp_traverse_5pysam_6ctabix_TabixIterator(PyObject *o, visitproc
   if (p->tabixfile) {
     e = (*v)(((PyObject*)p->tabixfile), a); if (e) return e;
   }
+  if (p->encoding) {
+    e = (*v)(p->encoding, a); if (e) return e;
+  }
   return 0;
 }
 
@@ -10308,13 +11356,17 @@ static int __pyx_tp_clear_5pysam_6ctabix_TabixIterator(PyObject *o) {
   PyObject* tmp;
   struct __pyx_obj_5pysam_6ctabix_TabixIterator *p = (struct __pyx_obj_5pysam_6ctabix_TabixIterator *)o;
   tmp = ((PyObject*)p->tabixfile);
-  p->tabixfile = ((struct __pyx_obj_5pysam_6ctabix_Tabixfile *)Py_None); Py_INCREF(Py_None);
+  p->tabixfile = ((struct __pyx_obj_5pysam_6ctabix_TabixFile *)Py_None); Py_INCREF(Py_None);
+  Py_XDECREF(tmp);
+  tmp = ((PyObject*)p->encoding);
+  p->encoding = Py_None; Py_INCREF(Py_None);
   Py_XDECREF(tmp);
   return 0;
 }
 
 static PyMethodDef __pyx_methods_5pysam_6ctabix_TabixIterator[] = {
-  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_6ctabix_13TabixIterator_3__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_13TabixIterator_2__next__)},
+  {__Pyx_NAMESTR("__next__"), (PyCFunction)__pyx_pw_5pysam_6ctabix_13TabixIterator_5__next__, METH_NOARGS|METH_COEXIST, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_13TabixIterator_4__next__)},
+  {__Pyx_NAMESTR("next"), (PyCFunction)__pyx_pw_5pysam_6ctabix_13TabixIterator_7next, METH_NOARGS, __Pyx_DOCSTR(__pyx_doc_5pysam_6ctabix_13TabixIterator_6next)},
   {0, 0, 0, 0}
 };
 
@@ -10343,13 +11395,13 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_TabixIterator = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("iterates over rows in *tabixfile* in region\n    given by *tid*, *start* and *end*.\n    "), /*tp_doc*/
+  __Pyx_DOCSTR("TabixIterator(encoding='ascii')\niterates over rows in *tabixfile* in region\n    given by *tid*, *start* and *end*.\n    "), /*tp_doc*/
   __pyx_tp_traverse_5pysam_6ctabix_TabixIterator, /*tp_traverse*/
   __pyx_tp_clear_5pysam_6ctabix_TabixIterator, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
-  __pyx_pw_5pysam_6ctabix_13TabixIterator_1__iter__, /*tp_iter*/
-  __pyx_pw_5pysam_6ctabix_13TabixIterator_3__next__, /*tp_iternext*/
+  __pyx_pw_5pysam_6ctabix_13TabixIterator_3__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_6ctabix_13TabixIterator_5__next__, /*tp_iternext*/
   __pyx_methods_5pysam_6ctabix_TabixIterator, /*tp_methods*/
   0, /*tp_members*/
   0, /*tp_getset*/
@@ -10358,7 +11410,7 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_TabixIterator = {
   0, /*tp_descr_get*/
   0, /*tp_descr_set*/
   0, /*tp_dictoffset*/
-  0, /*tp_init*/
+  __pyx_pw_5pysam_6ctabix_13TabixIterator_1__init__, /*tp_init*/
   0, /*tp_alloc*/
   __pyx_tp_new_5pysam_6ctabix_TabixIterator, /*tp_new*/
   0, /*tp_free*/
@@ -10451,13 +11503,13 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_TabixIteratorParsed = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("TabixIteratorParsed(Parser parser)\niterates over mapped reads in a region.\n\n    Returns parsed data.\n    "), /*tp_doc*/
+  __Pyx_DOCSTR("TabixIteratorParsed(Parser parser)\niterates over mapped reads in a region.\n\n    The *parser* determines the encoding.\n\n    Returns parsed data.\n    "), /*tp_doc*/
   __pyx_tp_traverse_5pysam_6ctabix_TabixIteratorParsed, /*tp_traverse*/
   __pyx_tp_clear_5pysam_6ctabix_TabixIteratorParsed, /*tp_clear*/
   0, /*tp_richcompare*/
   0, /*tp_weaklistoffset*/
   #if CYTHON_COMPILING_IN_PYPY
-  __pyx_pw_5pysam_6ctabix_13TabixIterator_1__iter__, /*tp_iter*/
+  __pyx_pw_5pysam_6ctabix_13TabixIterator_3__iter__, /*tp_iter*/
   #else
   0, /*tp_iter*/
   #endif
@@ -10502,6 +11554,7 @@ static PyObject *__pyx_tp_new_5pysam_6ctabix_GZIterator(PyTypeObject *t, CYTHON_
   p = ((struct __pyx_obj_5pysam_6ctabix_GZIterator *)o);
   p->__pyx_vtab = __pyx_vtabptr_5pysam_6ctabix_GZIterator;
   p->_filename = Py_None; Py_INCREF(Py_None);
+  p->encoding = Py_None; Py_INCREF(Py_None);
   return o;
 }
 
@@ -10522,6 +11575,7 @@ static void __pyx_tp_dealloc_5pysam_6ctabix_GZIterator(PyObject *o) {
     PyErr_Restore(etype, eval, etb);
   }
   Py_CLEAR(p->_filename);
+  Py_CLEAR(p->encoding);
   (*Py_TYPE(o)->tp_free)(o);
 }
 
@@ -10531,6 +11585,9 @@ static int __pyx_tp_traverse_5pysam_6ctabix_GZIterator(PyObject *o, visitproc v,
   if (p->_filename) {
     e = (*v)(p->_filename, a); if (e) return e;
   }
+  if (p->encoding) {
+    e = (*v)(p->encoding, a); if (e) return e;
+  }
   return 0;
 }
 
@@ -10540,6 +11597,9 @@ static int __pyx_tp_clear_5pysam_6ctabix_GZIterator(PyObject *o) {
   tmp = ((PyObject*)p->_filename);
   p->_filename = Py_None; Py_INCREF(Py_None);
   Py_XDECREF(tmp);
+  tmp = ((PyObject*)p->encoding);
+  p->encoding = Py_None; Py_INCREF(Py_None);
+  Py_XDECREF(tmp);
   return 0;
 }
 
@@ -10573,7 +11633,7 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_GZIterator = {
   0, /*tp_setattro*/
   0, /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-  __Pyx_DOCSTR("GZIterator(filename, int buffer_size=65536)"), /*tp_doc*/
+  __Pyx_DOCSTR("GZIterator(filename, int buffer_size=65536, encoding='ascii')"), /*tp_doc*/
   __pyx_tp_traverse_5pysam_6ctabix_GZIterator, /*tp_traverse*/
   __pyx_tp_clear_5pysam_6ctabix_GZIterator, /*tp_clear*/
   0, /*tp_richcompare*/
@@ -10801,6 +11861,71 @@ static PyTypeObject __pyx_type_5pysam_6ctabix_GZIteratorParsed = {
   #endif
 };
 
+static PyObject *__pyx_tp_new_5pysam_6ctabix_Tabixfile(PyTypeObject *t, PyObject *a, PyObject *k) {
+  PyObject *o = __pyx_tp_new_5pysam_6ctabix_TabixFile(t, a, k);
+  if (unlikely(!o)) return 0;
+  return o;
+}
+
+static PyTypeObject __pyx_type_5pysam_6ctabix_Tabixfile = {
+  PyVarObject_HEAD_INIT(0, 0)
+  __Pyx_NAMESTR("pysam.ctabix.Tabixfile"), /*tp_name*/
+  sizeof(struct __pyx_obj_5pysam_6ctabix_Tabixfile), /*tp_basicsize*/
+  0, /*tp_itemsize*/
+  __pyx_tp_dealloc_5pysam_6ctabix_TabixFile, /*tp_dealloc*/
+  0, /*tp_print*/
+  0, /*tp_getattr*/
+  0, /*tp_setattr*/
+  #if PY_MAJOR_VERSION < 3
+  0, /*tp_compare*/
+  #else
+  0, /*reserved*/
+  #endif
+  0, /*tp_repr*/
+  0, /*tp_as_number*/
+  0, /*tp_as_sequence*/
+  0, /*tp_as_mapping*/
+  0, /*tp_hash*/
+  0, /*tp_call*/
+  0, /*tp_str*/
+  0, /*tp_getattro*/
+  0, /*tp_setattro*/
+  0, /*tp_as_buffer*/
+  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_VERSION_TAG|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+  0, /*tp_doc*/
+  __pyx_tp_traverse_5pysam_6ctabix_TabixFile, /*tp_traverse*/
+  __pyx_tp_clear_5pysam_6ctabix_TabixFile, /*tp_clear*/
+  0, /*tp_richcompare*/
+  0, /*tp_weaklistoffset*/
+  0, /*tp_iter*/
+  0, /*tp_iternext*/
+  0, /*tp_methods*/
+  0, /*tp_members*/
+  0, /*tp_getset*/
+  0, /*tp_base*/
+  0, /*tp_dict*/
+  0, /*tp_descr_get*/
+  0, /*tp_descr_set*/
+  0, /*tp_dictoffset*/
+  0, /*tp_init*/
+  0, /*tp_alloc*/
+  __pyx_tp_new_5pysam_6ctabix_Tabixfile, /*tp_new*/
+  0, /*tp_free*/
+  0, /*tp_is_gc*/
+  0, /*tp_bases*/
+  0, /*tp_mro*/
+  0, /*tp_cache*/
+  0, /*tp_subclasses*/
+  0, /*tp_weaklist*/
+  0, /*tp_del*/
+  #if PY_VERSION_HEX >= 0x02060000
+  0, /*tp_version_tag*/
+  #endif
+  #if PY_VERSION_HEX >= 0x030400a1
+  0, /*tp_finalize*/
+  #endif
+};
+
 static PyMethodDef __pyx_methods[] = {
   {0, 0, 0, 0}
 };
@@ -10828,6 +11953,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_kp_u_Argument_must_be_string_or_unico, __pyx_k_Argument_must_be_string_or_unico, sizeof(__pyx_k_Argument_must_be_string_or_unico), 0, 1, 0, 0},
   {&__pyx_n_s_EmptyIterator, __pyx_k_EmptyIterator, sizeof(__pyx_k_EmptyIterator), 0, 0, 1, 1},
   {&__pyx_n_s_EmptyIterator___iter, __pyx_k_EmptyIterator___iter, sizeof(__pyx_k_EmptyIterator___iter), 0, 0, 1, 1},
+  {&__pyx_n_s_EmptyIterator___next, __pyx_k_EmptyIterator___next, sizeof(__pyx_k_EmptyIterator___next), 0, 0, 1, 1},
   {&__pyx_n_s_EmptyIterator_next, __pyx_k_EmptyIterator_next, sizeof(__pyx_k_EmptyIterator_next), 0, 0, 1, 1},
   {&__pyx_kp_s_Filename_s_already_exists_use_fo, __pyx_k_Filename_s_already_exists_use_fo, sizeof(__pyx_k_Filename_s_already_exists_use_fo), 0, 0, 1, 0},
   {&__pyx_kp_s_Filename_s_tbi_already_exists_us, __pyx_k_Filename_s_tbi_already_exists_us, sizeof(__pyx_k_Filename_s_tbi_already_exists_us), 0, 0, 1, 0},
@@ -10837,7 +11963,6 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_kp_s_I_O_operation_on_closed_file, __pyx_k_I_O_operation_on_closed_file, sizeof(__pyx_k_I_O_operation_on_closed_file), 0, 0, 1, 0},
   {&__pyx_kp_s_I_O_operation_on_closed_file_2, __pyx_k_I_O_operation_on_closed_file_2, sizeof(__pyx_k_I_O_operation_on_closed_file_2), 0, 0, 1, 0},
   {&__pyx_n_s_KeyError, __pyx_k_KeyError, sizeof(__pyx_k_KeyError), 0, 0, 1, 1},
-  {&__pyx_n_s_MemoryError, __pyx_k_MemoryError, sizeof(__pyx_k_MemoryError), 0, 0, 1, 1},
   {&__pyx_kp_s_No_such_file_or_directory_s, __pyx_k_No_such_file_or_directory_s, sizeof(__pyx_k_No_such_file_or_directory_s), 0, 0, 1, 0},
   {&__pyx_kp_s_No_such_file_s, __pyx_k_No_such_file_s, sizeof(__pyx_k_No_such_file_s), 0, 0, 1, 0},
   {&__pyx_n_s_NotImplementedError, __pyx_k_NotImplementedError, sizeof(__pyx_k_NotImplementedError), 0, 0, 1, 1},
@@ -10845,12 +11970,13 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_O_RDONLY, __pyx_k_O_RDONLY, sizeof(__pyx_k_O_RDONLY), 0, 0, 1, 1},
   {&__pyx_n_s_PYTHON3, __pyx_k_PYTHON3, sizeof(__pyx_k_PYTHON3), 0, 0, 1, 1},
   {&__pyx_n_s_StopIteration, __pyx_k_StopIteration, sizeof(__pyx_k_StopIteration), 0, 0, 1, 1},
+  {&__pyx_n_s_TabixFile, __pyx_k_TabixFile, sizeof(__pyx_k_TabixFile), 0, 0, 1, 1},
   {&__pyx_n_s_Tabixfile, __pyx_k_Tabixfile, sizeof(__pyx_k_Tabixfile), 0, 0, 1, 1},
   {&__pyx_n_s_TypeError, __pyx_k_TypeError, sizeof(__pyx_k_TypeError), 0, 0, 1, 1},
   {&__pyx_n_s_ValueError, __pyx_k_ValueError, sizeof(__pyx_k_ValueError), 0, 0, 1, 1},
   {&__pyx_n_s_WINDOW_SIZE, __pyx_k_WINDOW_SIZE, sizeof(__pyx_k_WINDOW_SIZE), 0, 0, 1, 1},
-  {&__pyx_kp_s__12, __pyx_k__12, sizeof(__pyx_k__12), 0, 0, 1, 0},
-  {&__pyx_kp_s__16, __pyx_k__16, sizeof(__pyx_k__16), 0, 0, 1, 0},
+  {&__pyx_kp_s__10, __pyx_k__10, sizeof(__pyx_k__10), 0, 0, 1, 0},
+  {&__pyx_kp_s__14, __pyx_k__14, sizeof(__pyx_k__14), 0, 0, 1, 0},
   {&__pyx_n_s_all, __pyx_k_all, sizeof(__pyx_k_all), 0, 0, 1, 1},
   {&__pyx_n_s_asBed, __pyx_k_asBed, sizeof(__pyx_k_asBed), 0, 0, 1, 1},
   {&__pyx_n_s_asGTF, __pyx_k_asGTF, sizeof(__pyx_k_asGTF), 0, 0, 1, 1},
@@ -10861,6 +11987,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_bed, __pyx_k_bed, sizeof(__pyx_k_bed), 0, 0, 1, 1},
   {&__pyx_n_s_buffer, __pyx_k_buffer, sizeof(__pyx_k_buffer), 0, 0, 1, 1},
   {&__pyx_n_s_buffer_size, __pyx_k_buffer_size, sizeof(__pyx_k_buffer_size), 0, 0, 1, 1},
+  {&__pyx_n_s_bytes_cpy, __pyx_k_bytes_cpy, sizeof(__pyx_k_bytes_cpy), 0, 0, 1, 1},
   {&__pyx_n_s_c, __pyx_k_c, sizeof(__pyx_k_c), 0, 0, 1, 1},
   {&__pyx_n_s_close, __pyx_k_close, sizeof(__pyx_k_close), 0, 0, 1, 1},
   {&__pyx_n_s_closed, __pyx_k_closed, sizeof(__pyx_k_closed), 0, 0, 1, 1},
@@ -10874,8 +12001,10 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_cpy, __pyx_k_cpy, sizeof(__pyx_k_cpy), 0, 0, 1, 1},
   {&__pyx_n_s_decode, __pyx_k_decode, sizeof(__pyx_k_decode), 0, 0, 1, 1},
   {&__pyx_n_s_doc, __pyx_k_doc, sizeof(__pyx_k_doc), 0, 0, 1, 1},
+  {&__pyx_n_s_dup, __pyx_k_dup, sizeof(__pyx_k_dup), 0, 0, 1, 1},
   {&__pyx_kp_s_empty_iterator, __pyx_k_empty_iterator, sizeof(__pyx_k_empty_iterator), 0, 0, 1, 0},
   {&__pyx_n_s_encode, __pyx_k_encode, sizeof(__pyx_k_encode), 0, 0, 1, 1},
+  {&__pyx_n_s_encoding, __pyx_k_encoding, sizeof(__pyx_k_encoding), 0, 0, 1, 1},
   {&__pyx_n_s_end, __pyx_k_end, sizeof(__pyx_k_end), 0, 0, 1, 1},
   {&__pyx_n_s_end_col, __pyx_k_end_col, sizeof(__pyx_k_end_col), 0, 0, 1, 1},
   {&__pyx_n_s_endswith, __pyx_k_endswith, sizeof(__pyx_k_endswith), 0, 0, 1, 1},
@@ -10891,6 +12020,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_force, __pyx_k_force, sizeof(__pyx_k_force), 0, 0, 1, 1},
   {&__pyx_n_s_fp, __pyx_k_fp, sizeof(__pyx_k_fp), 0, 0, 1, 1},
   {&__pyx_kp_s_ftp, __pyx_k_ftp, sizeof(__pyx_k_ftp), 0, 0, 1, 0},
+  {&__pyx_n_s_get_encoding, __pyx_k_get_encoding, sizeof(__pyx_k_get_encoding), 0, 0, 1, 1},
   {&__pyx_n_s_getdefaultencoding, __pyx_k_getdefaultencoding, sizeof(__pyx_k_getdefaultencoding), 0, 0, 1, 1},
   {&__pyx_n_s_getfilesystemencoding, __pyx_k_getfilesystemencoding, sizeof(__pyx_k_getfilesystemencoding), 0, 0, 1, 1},
   {&__pyx_n_s_gff, __pyx_k_gff, sizeof(__pyx_k_gff), 0, 0, 1, 1},
@@ -10904,10 +12034,10 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_infile, __pyx_k_infile, sizeof(__pyx_k_infile), 0, 0, 1, 1},
   {&__pyx_n_s_init, __pyx_k_init, sizeof(__pyx_k_init), 0, 0, 1, 1},
   {&__pyx_kp_s_invalid_file_opening_mode_s, __pyx_k_invalid_file_opening_mode_s, sizeof(__pyx_k_invalid_file_opening_mode_s), 0, 0, 1, 0},
-  {&__pyx_kp_s_invalid_mode_s, __pyx_k_invalid_mode_s, sizeof(__pyx_k_invalid_mode_s), 0, 0, 1, 0},
   {&__pyx_n_s_isOpen, __pyx_k_isOpen, sizeof(__pyx_k_isOpen), 0, 0, 1, 1},
   {&__pyx_n_s_iter, __pyx_k_iter, sizeof(__pyx_k_iter), 0, 0, 1, 1},
   {&__pyx_kp_s_iterate_over_infile_Permits_the, __pyx_k_iterate_over_infile_Permits_the, sizeof(__pyx_k_iterate_over_infile_Permits_the), 0, 0, 1, 0},
+  {&__pyx_kp_s_iteration_on_closed_file, __pyx_k_iteration_on_closed_file, sizeof(__pyx_k_iteration_on_closed_file), 0, 0, 1, 0},
   {&__pyx_n_s_join, __pyx_k_join, sizeof(__pyx_k_join), 0, 0, 1, 1},
   {&__pyx_n_s_keys, __pyx_k_keys, sizeof(__pyx_k_keys), 0, 0, 1, 1},
   {&__pyx_n_s_length, __pyx_k_length, sizeof(__pyx_k_length), 0, 0, 1, 1},
@@ -10918,6 +12048,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_min_shift, __pyx_k_min_shift, sizeof(__pyx_k_min_shift), 0, 0, 1, 1},
   {&__pyx_n_s_mode, __pyx_k_mode, sizeof(__pyx_k_mode), 0, 0, 1, 1},
   {&__pyx_n_s_module, __pyx_k_module, sizeof(__pyx_k_module), 0, 0, 1, 1},
+  {&__pyx_n_s_multiple_iterators, __pyx_k_multiple_iterators, sizeof(__pyx_k_multiple_iterators), 0, 0, 1, 1},
   {&__pyx_n_s_nbytes, __pyx_k_nbytes, sizeof(__pyx_k_nbytes), 0, 0, 1, 1},
   {&__pyx_kp_s_neither_preset_nor_seq_col_start, __pyx_k_neither_preset_nor_seq_col_start, sizeof(__pyx_k_neither_preset_nor_seq_col_start), 0, 0, 1, 0},
   {&__pyx_n_s_next, __pyx_k_next, sizeof(__pyx_k_next), 0, 0, 1, 1},
@@ -10947,6 +12078,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_sam, __pyx_k_sam, sizeof(__pyx_k_sam), 0, 0, 1, 1},
   {&__pyx_n_s_self, __pyx_k_self, sizeof(__pyx_k_self), 0, 0, 1, 1},
   {&__pyx_n_s_seq_col, __pyx_k_seq_col, sizeof(__pyx_k_seq_col), 0, 0, 1, 1},
+  {&__pyx_n_s_set_encoding, __pyx_k_set_encoding, sizeof(__pyx_k_set_encoding), 0, 0, 1, 1},
   {&__pyx_n_s_start, __pyx_k_start, sizeof(__pyx_k_start), 0, 0, 1, 1},
   {&__pyx_n_s_start_col, __pyx_k_start_col, sizeof(__pyx_k_start_col), 0, 0, 1, 1},
   {&__pyx_kp_s_start_i_end_i, __pyx_k_start_i_end_i, sizeof(__pyx_k_start_i_end_i), 0, 0, 1, 0},
@@ -10966,23 +12098,20 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_kp_s_unknown_preset_s_valid_presets_a, __pyx_k_unknown_preset_s_valid_presets_a, sizeof(__pyx_k_unknown_preset_s_valid_presets_a), 0, 0, 1, 0},
   {&__pyx_n_s_unlink, __pyx_k_unlink, sizeof(__pyx_k_unlink), 0, 0, 1, 1},
   {&__pyx_n_s_vcf, __pyx_k_vcf, sizeof(__pyx_k_vcf), 0, 0, 1, 1},
-  {&__pyx_n_s_w, __pyx_k_w, sizeof(__pyx_k_w), 0, 0, 1, 1},
   {&__pyx_kp_s_writing_failed, __pyx_k_writing_failed, sizeof(__pyx_k_writing_failed), 0, 0, 1, 0},
   {&__pyx_kp_s_writing_to_file_s_failed, __pyx_k_writing_to_file_s_failed, sizeof(__pyx_k_writing_to_file_s_failed), 0, 0, 1, 0},
-  {&__pyx_kp_s_writing_to_tabix_files_not_imple, __pyx_k_writing_to_tabix_files_not_imple, sizeof(__pyx_k_writing_to_tabix_files_not_imple), 0, 0, 1, 0},
   {&__pyx_n_s_zerobased, __pyx_k_zerobased, sizeof(__pyx_k_zerobased), 0, 0, 1, 1},
   {0, 0, 0, 0, 0, 0, 0}
 };
 static int __Pyx_InitCachedBuiltins(void) {
-  __pyx_builtin_TypeError = __Pyx_GetBuiltinName(__pyx_n_s_TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_NotImplementedError = __Pyx_GetBuiltinName(__pyx_n_s_NotImplementedError); if (!__pyx_builtin_NotImplementedError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 89; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_IOError = __Pyx_GetBuiltinName(__pyx_n_s_IOError); if (!__pyx_builtin_IOError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 280; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_OSError = __Pyx_GetBuiltinName(__pyx_n_s_OSError); if (!__pyx_builtin_OSError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_ord = __Pyx_GetBuiltinName(__pyx_n_s_ord); if (!__pyx_builtin_ord) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_KeyError = __Pyx_GetBuiltinName(__pyx_n_s_KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_MemoryError = __Pyx_GetBuiltinName(__pyx_n_s_MemoryError); if (!__pyx_builtin_MemoryError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 954; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_TypeError = __Pyx_GetBuiltinName(__pyx_n_s_TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_NotImplementedError = __Pyx_GetBuiltinName(__pyx_n_s_NotImplementedError); if (!__pyx_builtin_NotImplementedError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 97; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_IOError = __Pyx_GetBuiltinName(__pyx_n_s_IOError); if (!__pyx_builtin_IOError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 301; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_OSError = __Pyx_GetBuiltinName(__pyx_n_s_OSError); if (!__pyx_builtin_OSError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_ord = __Pyx_GetBuiltinName(__pyx_n_s_ord); if (!__pyx_builtin_ord) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_KeyError = __Pyx_GetBuiltinName(__pyx_n_s_KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   return 0;
   __pyx_L1_error:;
   return -1;
@@ -10992,303 +12121,293 @@ static int __Pyx_InitCachedConstants(void) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0);
 
-  /* "pysam/ctabix.pyx":60
- *         return s
- *     elif PyUnicode_Check(s):
- *         return s.encode('ascii')             # <<<<<<<<<<<<<<
- *     else:
- *         raise TypeError, u"Argument must be string, bytes or unicode."
- */
-  __pyx_tuple_ = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple_);
-  __Pyx_GIVEREF(__pyx_tuple_);
-
-  /* "pysam/ctabix.pyx":80
- *         return s
- *     elif PyBytes_Check(s):
- *         return s.decode('ascii')             # <<<<<<<<<<<<<<
- *     else:
- *         # assume unicode
- */
-  __pyx_tuple__2 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__2);
-  __Pyx_GIVEREF(__pyx_tuple__2);
-
-  /* "pysam/ctabix.pyx":259
- * 
- *         filename_index = index or (filename + ".tbi")
- *         self.isremote = filename.startswith("http:") or filename.startswith("ftp:")             # <<<<<<<<<<<<<<
- * 
- *         # encode all the strings
- */
-  __pyx_tuple__3 = PyTuple_Pack(1, __pyx_kp_s_http); if (unlikely(!__pyx_tuple__3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__3);
-  __Pyx_GIVEREF(__pyx_tuple__3);
-  __pyx_tuple__4 = PyTuple_Pack(1, __pyx_kp_s_ftp); if (unlikely(!__pyx_tuple__4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__4);
-  __Pyx_GIVEREF(__pyx_tuple__4);
-
-  /* "pysam/ctabix.pyx":264
- *         filename = _encodeFilename(filename)
- *         filename_index = _encodeFilename(filename_index)
- *         cdef bytes bmode = mode.encode('ascii')             # <<<<<<<<<<<<<<
+  /* "pysam/ctabix.pyx":297
  * 
- *         if self._filename != NULL: free(self._filename )
- */
-  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_n_s_ascii); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 264; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__5);
-  __Pyx_GIVEREF(__pyx_tuple__5);
-
-  /* "pysam/ctabix.pyx":272
- *         if mode[0] == 'w':
- *             # open file for writing
- *             raise NotImplementedError("writing to tabix files not implemented" )             # <<<<<<<<<<<<<<
+ *         filename_index = index or (filename + ".tbi")
+ *         self.isremote = filename.startswith("http:") or filename.startswith("ftp:")             # <<<<<<<<<<<<<<
  * 
- *         if mode[0] != "r":
+ *         if not self.isremote:
  */
-  __pyx_tuple__6 = PyTuple_Pack(1, __pyx_kp_s_writing_to_tabix_files_not_imple); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 272; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__6);
-  __Pyx_GIVEREF(__pyx_tuple__6);
+  __pyx_tuple_ = PyTuple_Pack(1, __pyx_kp_s_http); if (unlikely(!__pyx_tuple_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple_);
+  __Pyx_GIVEREF(__pyx_tuple_);
+  __pyx_tuple__2 = PyTuple_Pack(1, __pyx_kp_s_ftp); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__2);
+  __Pyx_GIVEREF(__pyx_tuple__2);
 
-  /* "pysam/ctabix.pyx":319
+  /* "pysam/ctabix.pyx":366
  *         '''
  *         if not self._isOpen():
  *             raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  * 
  *         # convert coordinates to region string
  */
-  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__7);
-  __Pyx_GIVEREF(__pyx_tuple__7);
+  __pyx_tuple__3 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__3);
+  __Pyx_GIVEREF(__pyx_tuple__3);
 
-  /* "pysam/ctabix.pyx":379
- *         '''filename associated with this object.'''
+  /* "pysam/ctabix.pyx":437
  *         def __get__(self):
- *             if not self._isOpen(): raise ValueError( "I/O operation on closed file" )             # <<<<<<<<<<<<<<
+ *             if not self._isOpen():
+ *                 raise ValueError("I/O operation on closed file")             # <<<<<<<<<<<<<<
  *             return self._filename
  * 
  */
-  __pyx_tuple__8 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__8);
-  __Pyx_GIVEREF(__pyx_tuple__8);
+  __pyx_tuple__4 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file); if (unlikely(!__pyx_tuple__4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__4);
+  __Pyx_GIVEREF(__pyx_tuple__4);
 
-  /* "pysam/ctabix.pyx":621
+  /* "pysam/ctabix.pyx":539
+ *         cdef int retval = self.__cnext__()
+ *         if retval == -5:
+ *             raise IOError("iteration on closed file")             # <<<<<<<<<<<<<<
+ *         elif retval < 0:
+ *             raise StopIteration
+ */
+  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_s_iteration_on_closed_file); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 539; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__5);
+  __Pyx_GIVEREF(__pyx_tuple__5);
+
+  /* "pysam/ctabix.pyx":590
+ *         cdef int retval = self.__cnext__()
+ *         if retval == -5:
+ *             raise IOError("iteration on closed file")             # <<<<<<<<<<<<<<
+ *         elif retval < 0:
+ *             raise StopIteration
+ */
+  __pyx_tuple__6 = PyTuple_Pack(1, __pyx_kp_s_iteration_on_closed_file); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__6);
+  __Pyx_GIVEREF(__pyx_tuple__6);
+
+  /* "pysam/ctabix.pyx":712
  *     fp = bgzf_open( fn, "w")
  *     if fp == NULL:
  *         raise IOError("could not open '%s' for writing")             # <<<<<<<<<<<<<<
  * 
- *     fn = _force_bytes(filename_in)
+ *     fn = _encodeFilename(filename_in)
  */
-  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_kp_s_could_not_open_s_for_writing); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__9);
-  __Pyx_GIVEREF(__pyx_tuple__9);
+  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_s_could_not_open_s_for_writing); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__7);
+  __Pyx_GIVEREF(__pyx_tuple__7);
 
-  /* "pysam/ctabix.pyx":626
+  /* "pysam/ctabix.pyx":717
  *     fd_src = open(fn, O_RDONLY)
  *     if fd_src == 0:
  *         raise IOError("could not open '%s' for reading")             # <<<<<<<<<<<<<<
  * 
  *     buffer = malloc(WINDOW_SIZE)
  */
-  __pyx_tuple__10 = PyTuple_Pack(1, __pyx_kp_s_could_not_open_s_for_reading); if (unlikely(!__pyx_tuple__10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__10);
-  __Pyx_GIVEREF(__pyx_tuple__10);
+  __pyx_tuple__8 = PyTuple_Pack(1, __pyx_kp_s_could_not_open_s_for_reading); if (unlikely(!__pyx_tuple__8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__8);
+  __Pyx_GIVEREF(__pyx_tuple__8);
 
-  /* "pysam/ctabix.pyx":636
+  /* "pysam/ctabix.pyx":727
  *         if r < 0:
  *             free(buffer)
  *             raise OSError("writing failed")             # <<<<<<<<<<<<<<
  * 
  *     free(buffer)
  */
-  __pyx_tuple__11 = PyTuple_Pack(1, __pyx_kp_s_writing_failed); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__11);
-  __Pyx_GIVEREF(__pyx_tuple__11);
+  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_kp_s_writing_failed); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__9);
+  __Pyx_GIVEREF(__pyx_tuple__9);
 
-  /* "pysam/ctabix.pyx":698
+  /* "pysam/ctabix.pyx":790
+ *     if preset is None and \
+ *        (seq_col is None or start_col is None or end_col is None):
+ *         raise ValueError(             # <<<<<<<<<<<<<<
+ *             "neither preset nor seq_col,start_col and end_col given")
  * 
- *     if preset == None and (seq_col == None or start_col == None or end_col == None):
- *         raise ValueError("neither preset nor seq_col,start_col and end_col given" )             # <<<<<<<<<<<<<<
- * 
- *     if not filename.endswith(".gz"):
  */
-  __pyx_tuple__13 = PyTuple_Pack(1, __pyx_kp_s_neither_preset_nor_seq_col_start); if (unlikely(!__pyx_tuple__13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__13);
-  __Pyx_GIVEREF(__pyx_tuple__13);
+  __pyx_tuple__11 = PyTuple_Pack(1, __pyx_kp_s_neither_preset_nor_seq_col_start); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__11);
+  __Pyx_GIVEREF(__pyx_tuple__11);
 
-  /* "pysam/ctabix.pyx":700
- *         raise ValueError("neither preset nor seq_col,start_col and end_col given" )
+  /* "pysam/ctabix.pyx":793
+ *             "neither preset nor seq_col,start_col and end_col given")
  * 
  *     if not filename.endswith(".gz"):             # <<<<<<<<<<<<<<
- *         tabix_compress( filename, filename + ".gz", force = force )
+ *         tabix_compress(filename, filename + ".gz", force=force)
  *         os.unlink( filename )
  */
-  __pyx_tuple__14 = PyTuple_Pack(1, __pyx_kp_s_gz); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__14);
-  __Pyx_GIVEREF(__pyx_tuple__14);
+  __pyx_tuple__12 = PyTuple_Pack(1, __pyx_kp_s_gz); if (unlikely(!__pyx_tuple__12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__12);
+  __Pyx_GIVEREF(__pyx_tuple__12);
 
-  /* "pysam/ctabix.pyx":706
+  /* "pysam/ctabix.pyx":799
  * 
- *     if not force and os.path.exists(filename + ".tbi" ):
- *         raise IOError( "Filename '%s.tbi' already exists, use *force* to overwrite" )             # <<<<<<<<<<<<<<
+ *     if not force and os.path.exists(filename + ".tbi"):
+ *         raise IOError(             # <<<<<<<<<<<<<<
+ *             "Filename '%s.tbi' already exists, use *force* to overwrite")
  * 
- *     # columns (1-based):
  */
-  __pyx_tuple__15 = PyTuple_Pack(1, __pyx_kp_s_Filename_s_tbi_already_exists_us); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__15);
-  __Pyx_GIVEREF(__pyx_tuple__15);
+  __pyx_tuple__13 = PyTuple_Pack(1, __pyx_kp_s_Filename_s_tbi_already_exists_us); if (unlikely(!__pyx_tuple__13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__13);
+  __Pyx_GIVEREF(__pyx_tuple__13);
 
-  /* "pysam/ctabix.pyx":839
+  /* "pysam/ctabix.pyx":933
  * 
  *         if infile.closed:
- *             raise ValueError( "I/O operation on closed file." )             # <<<<<<<<<<<<<<
+ *             raise ValueError("I/O operation on closed file.")             # <<<<<<<<<<<<<<
  * 
  *         self.infile = infile
  */
-  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file_2); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__17);
-  __Pyx_GIVEREF(__pyx_tuple__17);
+  __pyx_tuple__15 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file_2); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 933; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__15);
+  __Pyx_GIVEREF(__pyx_tuple__15);
 
-  /* "pysam/ctabix.pyx":845
+  /* "pysam/ctabix.pyx":939
  *         cdef int fd = PyObject_AsFileDescriptor(infile)
  *         if fd == -1:
  *             raise ValueError("I/O operation on closed file.")             # <<<<<<<<<<<<<<
  * 
- *         # From the manual:
+ *         self.duplicated_fd = dup(fd)
  */
-  __pyx_tuple__18 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file_2); if (unlikely(!__pyx_tuple__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__18);
-  __Pyx_GIVEREF(__pyx_tuple__18);
+  __pyx_tuple__16 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file_2); if (unlikely(!__pyx_tuple__16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__16);
+  __Pyx_GIVEREF(__pyx_tuple__16);
 
-  /* "pysam/ctabix.pyx":915
+  /* "pysam/ctabix.pyx":1015
  *         self.infile = infile
  *         if self.infile.closed:
  *             raise ValueError("I/O operation on closed file.")             # <<<<<<<<<<<<<<
  *         self.parser = parser
  * 
  */
-  __pyx_tuple__19 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file_2); if (unlikely(!__pyx_tuple__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__19);
-  __Pyx_GIVEREF(__pyx_tuple__19);
+  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file_2); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1015; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__17);
+  __Pyx_GIVEREF(__pyx_tuple__17);
 
-  /* "pysam/ctabix.pyx":930
- *         # note that GzipFile.close() does not close the file
+  /* "pysam/ctabix.pyx":1033
  *         # reading is still possible.
- *         if self.infile.closed: raise ValueError( "I/O operation on closed file." )             # <<<<<<<<<<<<<<
+ *         if self.infile.closed:
+ *             raise ValueError("I/O operation on closed file.")             # <<<<<<<<<<<<<<
  * 
  *         while 1:
  */
-  __pyx_tuple__20 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file_2); if (unlikely(!__pyx_tuple__20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__20);
-  __Pyx_GIVEREF(__pyx_tuple__20);
+  __pyx_tuple__18 = PyTuple_Pack(1, __pyx_kp_s_I_O_operation_on_closed_file_2); if (unlikely(!__pyx_tuple__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1033; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__18);
+  __Pyx_GIVEREF(__pyx_tuple__18);
 
-  /* "pysam/ctabix.pyx":478
+  /* "pysam/ctabix.pyx":558
  *     '''empty iterator'''
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
  * 
  */
-  __pyx_tuple__21 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__21)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__19 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__19);
+  __Pyx_GIVEREF(__pyx_tuple__19);
+  __pyx_codeobj__20 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__19, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_iter, 558, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/ctabix.pyx":561
+ *         return self
+ * 
+ *     def next(self):             # <<<<<<<<<<<<<<
+ *         raise StopIteration()
+ * 
+ */
+  __pyx_tuple__21 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__21)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__21);
   __Pyx_GIVEREF(__pyx_tuple__21);
-  __pyx_codeobj__22 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__21, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_iter, 478, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__22 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__21, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_next_2, 561, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":482
- * 
+  /* "pysam/ctabix.pyx":564
+ *         raise StopIteration()
  * 
- *     def next(self):             # <<<<<<<<<<<<<<
+ *     def __next__(self):             # <<<<<<<<<<<<<<
  *         raise StopIteration()
  * 
  */
-  __pyx_tuple__23 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__23 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__23);
   __Pyx_GIVEREF(__pyx_tuple__23);
-  __pyx_codeobj__24 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__23, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_next_2, 482, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__24 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__23, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_next, 564, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":594
+  /* "pysam/ctabix.pyx":685
  * 
  * 
  * def tabix_compress(filename_in,             # <<<<<<<<<<<<<<
  *                    filename_out,
  *                    force=False):
  */
-  __pyx_tuple__25 = PyTuple_Pack(11, __pyx_n_s_filename_in, __pyx_n_s_filename_out, __pyx_n_s_force, __pyx_n_s_WINDOW_SIZE, __pyx_n_s_c, __pyx_n_s_r, __pyx_n_s_buffer, __pyx_n_s_fp, __pyx_n_s_fd_src, __pyx_n_s_O_RDONLY, __pyx_n_s_fn); if (unlikely(!__pyx_tuple__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__25 = PyTuple_Pack(11, __pyx_n_s_filename_in, __pyx_n_s_filename_out, __pyx_n_s_force, __pyx_n_s_WINDOW_SIZE, __pyx_n_s_c, __pyx_n_s_r, __pyx_n_s_buffer, __pyx_n_s_fp, __pyx_n_s_fd_src, __pyx_n_s_O_RDONLY, __pyx_n_s_fn); if (unlikely(!__pyx_tuple__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__25);
   __Pyx_GIVEREF(__pyx_tuple__25);
-  __pyx_codeobj__26 = (PyObject*)__Pyx_PyCode_New(3, 0, 11, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__25, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_tabix_compress, 594, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__26 = (PyObject*)__Pyx_PyCode_New(3, 0, 11, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__25, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_tabix_compress, 685, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":647
+  /* "pysam/ctabix.pyx":738
  *         raise OSError("error when closing file %s" % filename_in)
  * 
  * def tabix_index( filename,             # <<<<<<<<<<<<<<
  *                  force = False,
  *                  seq_col = None,
  */
-  __pyx_tuple__27 = PyTuple_Pack(13, __pyx_n_s_filename, __pyx_n_s_force, __pyx_n_s_seq_col, __pyx_n_s_start_col, __pyx_n_s_end_col, __pyx_n_s_preset, __pyx_n_s_meta_char, __pyx_n_s_zerobased, __pyx_n_s_min_shift, __pyx_n_s_preset2conf, __pyx_n_s_conf_data, __pyx_n_s_conf, __pyx_n_s_fn); if (unlikely(!__pyx_tuple__27)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__27 = PyTuple_Pack(13, __pyx_n_s_filename, __pyx_n_s_force, __pyx_n_s_seq_col, __pyx_n_s_start_col, __pyx_n_s_end_col, __pyx_n_s_preset, __pyx_n_s_meta_char, __pyx_n_s_zerobased, __pyx_n_s_min_shift, __pyx_n_s_preset2conf, __pyx_n_s_conf_data, __pyx_n_s_conf, __pyx_n_s_fn); if (unlikely(!__pyx_tuple__27)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__27);
   __Pyx_GIVEREF(__pyx_tuple__27);
-  __pyx_codeobj__28 = (PyObject*)__Pyx_PyCode_New(9, 0, 13, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__27, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_tabix_index, 647, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__28)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__28 = (PyObject*)__Pyx_PyCode_New(9, 0, 13, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__27, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_tabix_index, 738, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__28)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":911
+  /* "pysam/ctabix.pyx":1011
  *     Permits the use of file-like objects for example from the gzip module.
  *     '''
- *     def __init__(self, infile, parser ):             # <<<<<<<<<<<<<<
+ *     def __init__(self, infile, parser):             # <<<<<<<<<<<<<<
  * 
  *         self.infile = infile
  */
-  __pyx_tuple__29 = PyTuple_Pack(3, __pyx_n_s_self, __pyx_n_s_infile, __pyx_n_s_parser); if (unlikely(!__pyx_tuple__29)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__29 = PyTuple_Pack(3, __pyx_n_s_self, __pyx_n_s_infile, __pyx_n_s_parser); if (unlikely(!__pyx_tuple__29)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__29);
   __Pyx_GIVEREF(__pyx_tuple__29);
-  __pyx_codeobj__30 = (PyObject*)__Pyx_PyCode_New(3, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__29, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_init, 911, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__30)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__30 = (PyObject*)__Pyx_PyCode_New(3, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__29, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_init, 1011, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__30)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":918
+  /* "pysam/ctabix.pyx":1018
  *         self.parser = parser
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
  * 
  */
-  __pyx_tuple__31 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__31)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__31 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__31)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__31);
   __Pyx_GIVEREF(__pyx_tuple__31);
-  __pyx_codeobj__32 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__31, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_iter, 918, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__32 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__31, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_iter, 1018, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":922
+  /* "pysam/ctabix.pyx":1022
  * 
  *     # cython version - required for python 3
  *     def __next__(self):             # <<<<<<<<<<<<<<
  * 
  *         cdef char * b
  */
-  __pyx_tuple__33 = PyTuple_Pack(6, __pyx_n_s_self, __pyx_n_s_b, __pyx_n_s_cpy, __pyx_n_s_nbytes, __pyx_n_s_line, __pyx_n_s_s_2); if (unlikely(!__pyx_tuple__33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__33 = PyTuple_Pack(8, __pyx_n_s_self, __pyx_n_s_b, __pyx_n_s_cpy, __pyx_n_s_nbytes, __pyx_n_s_encoding, __pyx_n_s_line, __pyx_n_s_s_2, __pyx_n_s_bytes_cpy); if (unlikely(!__pyx_tuple__33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__33);
   __Pyx_GIVEREF(__pyx_tuple__33);
-  __pyx_codeobj__34 = (PyObject*)__Pyx_PyCode_New(1, 0, 6, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__33, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_next, 922, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__34)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__34 = (PyObject*)__Pyx_PyCode_New(1, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__33, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_next, 1022, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__34)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":962
+  /* "pysam/ctabix.pyx":1066
  * 
  *     # python version - required for python 2.7
  *     def next(self):             # <<<<<<<<<<<<<<
  *         return self.__next__()
  * 
  */
-  __pyx_tuple__35 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__35)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 962; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__35 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__35)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__35);
   __Pyx_GIVEREF(__pyx_tuple__35);
-  __pyx_codeobj__36 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__35, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_next_2, 962, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__36)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 962; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__36 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__35, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_next_2, 1066, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__36)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/ctabix.pyx":965
+  /* "pysam/ctabix.pyx":1069
  *         return self.__next__()
  * 
- * def tabix_iterator( infile, parser ):             # <<<<<<<<<<<<<<
- *     """return an iterator over all entries in a file."""
- *     if PYTHON3:
+ * def tabix_iterator(infile, parser):             # <<<<<<<<<<<<<<
+ *     """return an iterator over all entries in a file.
+ * 
  */
-  __pyx_tuple__37 = PyTuple_Pack(2, __pyx_n_s_infile, __pyx_n_s_parser); if (unlikely(!__pyx_tuple__37)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__37 = PyTuple_Pack(2, __pyx_n_s_infile, __pyx_n_s_parser); if (unlikely(!__pyx_tuple__37)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__37);
   __Pyx_GIVEREF(__pyx_tuple__37);
-  __pyx_codeobj__38 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__37, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_tabix_iterator, 965, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__38)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__38 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__37, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_tabix_iterator, 1069, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__38)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_RefNannyFinishContext();
   return 0;
   __pyx_L1_error:;
@@ -11399,87 +12518,87 @@ PyMODINIT_FUNC PyInit_ctabix(void)
   __pyx_v_5pysam_6ctabix__FILENAME_ENCODING = ((PyObject*)Py_None); Py_INCREF(Py_None);
   /*--- Variable export code ---*/
   /*--- Function export code ---*/
-  if (__Pyx_ExportFunction("_force_str", (void (*)(void))__pyx_f_5pysam_6ctabix__force_str, "PyObject *(PyObject *)") < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_ExportFunction("_force_str", (void (*)(void))__pyx_f_5pysam_6ctabix__force_str, "PyObject *(PyObject *, struct __pyx_opt_args_5pysam_6ctabix__force_str *__pyx_optional_args)") < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   /*--- Type init code ---*/
   __pyx_vtabptr_5pysam_6ctabix_tabix_file_iterator = &__pyx_vtable_5pysam_6ctabix_tabix_file_iterator;
   __pyx_vtable_5pysam_6ctabix_tabix_file_iterator.__pyx___cnext__ = (PyObject *(*)(struct __pyx_obj_5pysam_6ctabix_tabix_file_iterator *))__pyx_f_5pysam_6ctabix_19tabix_file_iterator___cnext__;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_tabix_file_iterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_tabix_file_iterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_tabix_file_iterator.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_tabix_file_iterator.tp_dict, __pyx_vtabptr_5pysam_6ctabix_tabix_file_iterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "tabix_file_iterator", (PyObject *)&__pyx_type_5pysam_6ctabix_tabix_file_iterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_tabix_file_iterator.tp_dict, __pyx_vtabptr_5pysam_6ctabix_tabix_file_iterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "tabix_file_iterator", (PyObject *)&__pyx_type_5pysam_6ctabix_tabix_file_iterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_tabix_file_iterator = &__pyx_type_5pysam_6ctabix_tabix_file_iterator;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_Tabixfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_type_5pysam_6ctabix_Tabixfile.tp_print = 0;
-  if (__Pyx_SetAttrString(__pyx_m, "Tabixfile", (PyObject *)&__pyx_type_5pysam_6ctabix_Tabixfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_Tabixfile = &__pyx_type_5pysam_6ctabix_Tabixfile;
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_TabixFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_6ctabix_TabixFile.tp_print = 0;
+  if (__Pyx_SetAttrString(__pyx_m, "TabixFile", (PyObject *)&__pyx_type_5pysam_6ctabix_TabixFile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 254; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_TabixFile = &__pyx_type_5pysam_6ctabix_TabixFile;
   __pyx_vtabptr_5pysam_6ctabix_Parser = &__pyx_vtable_5pysam_6ctabix_Parser;
   __pyx_vtable_5pysam_6ctabix_Parser.parse = (PyObject *(*)(struct __pyx_obj_5pysam_6ctabix_Parser *, char *, int))__pyx_f_5pysam_6ctabix_6Parser_parse;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_Parser) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_Parser) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 85; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_Parser.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_Parser.tp_dict, __pyx_vtabptr_5pysam_6ctabix_Parser) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "Parser", (PyObject *)&__pyx_type_5pysam_6ctabix_Parser) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 86; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_Parser.tp_dict, __pyx_vtabptr_5pysam_6ctabix_Parser) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 85; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "Parser", (PyObject *)&__pyx_type_5pysam_6ctabix_Parser) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 85; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_Parser = &__pyx_type_5pysam_6ctabix_Parser;
   __pyx_vtabptr_5pysam_6ctabix_asTuple = &__pyx_vtable_5pysam_6ctabix_asTuple;
   __pyx_vtable_5pysam_6ctabix_asTuple.__pyx_base = *__pyx_vtabptr_5pysam_6ctabix_Parser;
   __pyx_vtable_5pysam_6ctabix_asTuple.__pyx_base.parse = (PyObject *(*)(struct __pyx_obj_5pysam_6ctabix_Parser *, char *, int))__pyx_f_5pysam_6ctabix_7asTuple_parse;
   __pyx_type_5pysam_6ctabix_asTuple.tp_base = __pyx_ptype_5pysam_6ctabix_Parser;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_asTuple) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_asTuple) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_asTuple.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_asTuple.tp_dict, __pyx_vtabptr_5pysam_6ctabix_asTuple) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "asTuple", (PyObject *)&__pyx_type_5pysam_6ctabix_asTuple) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_asTuple.tp_dict, __pyx_vtabptr_5pysam_6ctabix_asTuple) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "asTuple", (PyObject *)&__pyx_type_5pysam_6ctabix_asTuple) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 104; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_asTuple = &__pyx_type_5pysam_6ctabix_asTuple;
   __pyx_vtabptr_5pysam_6ctabix_asGTF = &__pyx_vtable_5pysam_6ctabix_asGTF;
   __pyx_vtable_5pysam_6ctabix_asGTF.__pyx_base = *__pyx_vtabptr_5pysam_6ctabix_Parser;
   __pyx_vtable_5pysam_6ctabix_asGTF.__pyx_base.parse = (PyObject *(*)(struct __pyx_obj_5pysam_6ctabix_Parser *, char *, int))__pyx_f_5pysam_6ctabix_5asGTF_parse;
   __pyx_type_5pysam_6ctabix_asGTF.tp_base = __pyx_ptype_5pysam_6ctabix_Parser;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_asGTF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_asGTF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_asGTF.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_asGTF.tp_dict, __pyx_vtabptr_5pysam_6ctabix_asGTF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "asGTF", (PyObject *)&__pyx_type_5pysam_6ctabix_asGTF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_asGTF.tp_dict, __pyx_vtabptr_5pysam_6ctabix_asGTF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "asGTF", (PyObject *)&__pyx_type_5pysam_6ctabix_asGTF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_asGTF = &__pyx_type_5pysam_6ctabix_asGTF;
   __pyx_vtabptr_5pysam_6ctabix_asBed = &__pyx_vtable_5pysam_6ctabix_asBed;
   __pyx_vtable_5pysam_6ctabix_asBed.__pyx_base = *__pyx_vtabptr_5pysam_6ctabix_Parser;
   __pyx_vtable_5pysam_6ctabix_asBed.__pyx_base.parse = (PyObject *(*)(struct __pyx_obj_5pysam_6ctabix_Parser *, char *, int))__pyx_f_5pysam_6ctabix_5asBed_parse;
   __pyx_type_5pysam_6ctabix_asBed.tp_base = __pyx_ptype_5pysam_6ctabix_Parser;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_asBed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 145; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_asBed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_asBed.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_asBed.tp_dict, __pyx_vtabptr_5pysam_6ctabix_asBed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 145; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "asBed", (PyObject *)&__pyx_type_5pysam_6ctabix_asBed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 145; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_asBed.tp_dict, __pyx_vtabptr_5pysam_6ctabix_asBed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "asBed", (PyObject *)&__pyx_type_5pysam_6ctabix_asBed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_asBed = &__pyx_type_5pysam_6ctabix_asBed;
   __pyx_vtabptr_5pysam_6ctabix_asVCF = &__pyx_vtable_5pysam_6ctabix_asVCF;
   __pyx_vtable_5pysam_6ctabix_asVCF.__pyx_base = *__pyx_vtabptr_5pysam_6ctabix_Parser;
   __pyx_vtable_5pysam_6ctabix_asVCF.__pyx_base.parse = (PyObject *(*)(struct __pyx_obj_5pysam_6ctabix_Parser *, char *, int))__pyx_f_5pysam_6ctabix_5asVCF_parse;
   __pyx_type_5pysam_6ctabix_asVCF.tp_base = __pyx_ptype_5pysam_6ctabix_Parser;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_asVCF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_asVCF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_asVCF.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_asVCF.tp_dict, __pyx_vtabptr_5pysam_6ctabix_asVCF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "asVCF", (PyObject *)&__pyx_type_5pysam_6ctabix_asVCF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_asVCF.tp_dict, __pyx_vtabptr_5pysam_6ctabix_asVCF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "asVCF", (PyObject *)&__pyx_type_5pysam_6ctabix_asVCF) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_asVCF = &__pyx_type_5pysam_6ctabix_asVCF;
   __pyx_vtabptr_5pysam_6ctabix_TabixIterator = &__pyx_vtable_5pysam_6ctabix_TabixIterator;
   __pyx_vtable_5pysam_6ctabix_TabixIterator.__pyx___cnext__ = (int (*)(struct __pyx_obj_5pysam_6ctabix_TabixIterator *))__pyx_f_5pysam_6ctabix_13TabixIterator___cnext__;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_TabixIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_TabixIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_TabixIterator.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_TabixIterator, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_TabixIterator, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
-      __pyx_wrapperbase_5pysam_6ctabix_13TabixIterator_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
-      __pyx_wrapperbase_5pysam_6ctabix_13TabixIterator_2__next__.doc = __pyx_doc_5pysam_6ctabix_13TabixIterator_2__next__;
-      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_6ctabix_13TabixIterator_2__next__;
+      __pyx_wrapperbase_5pysam_6ctabix_13TabixIterator_4__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
+      __pyx_wrapperbase_5pysam_6ctabix_13TabixIterator_4__next__.doc = __pyx_doc_5pysam_6ctabix_13TabixIterator_4__next__;
+      ((PyWrapperDescrObject *)wrapper)->d_base = &__pyx_wrapperbase_5pysam_6ctabix_13TabixIterator_4__next__;
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_TabixIterator.tp_dict, __pyx_vtabptr_5pysam_6ctabix_TabixIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "TabixIterator", (PyObject *)&__pyx_type_5pysam_6ctabix_TabixIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_TabixIterator.tp_dict, __pyx_vtabptr_5pysam_6ctabix_TabixIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "TabixIterator", (PyObject *)&__pyx_type_5pysam_6ctabix_TabixIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_TabixIterator = &__pyx_type_5pysam_6ctabix_TabixIterator;
   __pyx_vtabptr_5pysam_6ctabix_TabixIteratorParsed = &__pyx_vtable_5pysam_6ctabix_TabixIteratorParsed;
   __pyx_vtable_5pysam_6ctabix_TabixIteratorParsed.__pyx_base = *__pyx_vtabptr_5pysam_6ctabix_TabixIterator;
   __pyx_type_5pysam_6ctabix_TabixIteratorParsed.tp_base = __pyx_ptype_5pysam_6ctabix_TabixIterator;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_TabixIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 486; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_TabixIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_TabixIteratorParsed.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_TabixIteratorParsed, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 486; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_TabixIteratorParsed, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_6ctabix_19TabixIteratorParsed_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_6ctabix_19TabixIteratorParsed_2__next__.doc = __pyx_doc_5pysam_6ctabix_19TabixIteratorParsed_2__next__;
@@ -11487,16 +12606,16 @@ PyMODINIT_FUNC PyInit_ctabix(void)
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_TabixIteratorParsed.tp_dict, __pyx_vtabptr_5pysam_6ctabix_TabixIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 486; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "TabixIteratorParsed", (PyObject *)&__pyx_type_5pysam_6ctabix_TabixIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 486; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_TabixIteratorParsed.tp_dict, __pyx_vtabptr_5pysam_6ctabix_TabixIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "TabixIteratorParsed", (PyObject *)&__pyx_type_5pysam_6ctabix_TabixIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_TabixIteratorParsed = &__pyx_type_5pysam_6ctabix_TabixIteratorParsed;
   __pyx_vtabptr_5pysam_6ctabix_GZIterator = &__pyx_vtable_5pysam_6ctabix_GZIterator;
   __pyx_vtable_5pysam_6ctabix_GZIterator.__pyx___cnext__ = (int (*)(struct __pyx_obj_5pysam_6ctabix_GZIterator *))__pyx_f_5pysam_6ctabix_10GZIterator___cnext__;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_GZIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_GZIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_GZIterator.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_GZIterator, "__init__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_GZIterator, "__init__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_6ctabix_10GZIterator___init__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_6ctabix_10GZIterator___init__.doc = __pyx_doc_5pysam_6ctabix_10GZIterator___init__;
@@ -11506,7 +12625,7 @@ PyMODINIT_FUNC PyInit_ctabix(void)
   #endif
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_GZIterator, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_GZIterator, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_6ctabix_10GZIterator_6__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_6ctabix_10GZIterator_6__next__.doc = __pyx_doc_5pysam_6ctabix_10GZIterator_6__next__;
@@ -11514,17 +12633,17 @@ PyMODINIT_FUNC PyInit_ctabix(void)
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_GZIterator.tp_dict, __pyx_vtabptr_5pysam_6ctabix_GZIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "GZIterator", (PyObject *)&__pyx_type_5pysam_6ctabix_GZIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_GZIterator.tp_dict, __pyx_vtabptr_5pysam_6ctabix_GZIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "GZIterator", (PyObject *)&__pyx_type_5pysam_6ctabix_GZIterator) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_GZIterator = &__pyx_type_5pysam_6ctabix_GZIterator;
   __pyx_vtabptr_5pysam_6ctabix_GZIteratorHead = &__pyx_vtable_5pysam_6ctabix_GZIteratorHead;
   __pyx_vtable_5pysam_6ctabix_GZIteratorHead.__pyx_base = *__pyx_vtabptr_5pysam_6ctabix_GZIterator;
   __pyx_type_5pysam_6ctabix_GZIteratorHead.tp_base = __pyx_ptype_5pysam_6ctabix_GZIterator;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_GZIteratorHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_GZIteratorHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 649; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_GZIteratorHead.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_GZIteratorHead, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_GZIteratorHead, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 649; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_6ctabix_14GZIteratorHead___next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_6ctabix_14GZIteratorHead___next__.doc = __pyx_doc_5pysam_6ctabix_14GZIteratorHead___next__;
@@ -11532,17 +12651,17 @@ PyMODINIT_FUNC PyInit_ctabix(void)
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_GZIteratorHead.tp_dict, __pyx_vtabptr_5pysam_6ctabix_GZIteratorHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "GZIteratorHead", (PyObject *)&__pyx_type_5pysam_6ctabix_GZIteratorHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_GZIteratorHead.tp_dict, __pyx_vtabptr_5pysam_6ctabix_GZIteratorHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 649; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "GZIteratorHead", (PyObject *)&__pyx_type_5pysam_6ctabix_GZIteratorHead) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 649; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_GZIteratorHead = &__pyx_type_5pysam_6ctabix_GZIteratorHead;
   __pyx_vtabptr_5pysam_6ctabix_GZIteratorParsed = &__pyx_vtable_5pysam_6ctabix_GZIteratorParsed;
   __pyx_vtable_5pysam_6ctabix_GZIteratorParsed.__pyx_base = *__pyx_vtabptr_5pysam_6ctabix_GZIterator;
   __pyx_type_5pysam_6ctabix_GZIteratorParsed.tp_base = __pyx_ptype_5pysam_6ctabix_GZIterator;
-  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_GZIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_GZIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_6ctabix_GZIteratorParsed.tp_print = 0;
   #if CYTHON_COMPILING_IN_CPYTHON
   {
-    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_GZIteratorParsed, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&__pyx_type_5pysam_6ctabix_GZIteratorParsed, "__next__"); if (unlikely(!wrapper)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {
       __pyx_wrapperbase_5pysam_6ctabix_16GZIteratorParsed_2__next__ = *((PyWrapperDescrObject *)wrapper)->d_base;
       __pyx_wrapperbase_5pysam_6ctabix_16GZIteratorParsed_2__next__.doc = __pyx_doc_5pysam_6ctabix_16GZIteratorParsed_2__next__;
@@ -11550,9 +12669,14 @@ PyMODINIT_FUNC PyInit_ctabix(void)
     }
   }
   #endif
-  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_GZIteratorParsed.tp_dict, __pyx_vtabptr_5pysam_6ctabix_GZIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "GZIteratorParsed", (PyObject *)&__pyx_type_5pysam_6ctabix_GZIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_6ctabix_GZIteratorParsed.tp_dict, __pyx_vtabptr_5pysam_6ctabix_GZIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "GZIteratorParsed", (PyObject *)&__pyx_type_5pysam_6ctabix_GZIteratorParsed) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_6ctabix_GZIteratorParsed = &__pyx_type_5pysam_6ctabix_GZIteratorParsed;
+  __pyx_type_5pysam_6ctabix_Tabixfile.tp_base = __pyx_ptype_5pysam_6ctabix_TabixFile;
+  if (PyType_Ready(&__pyx_type_5pysam_6ctabix_Tabixfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_type_5pysam_6ctabix_Tabixfile.tp_print = 0;
+  if (__Pyx_SetAttrString(__pyx_m, "Tabixfile", (PyObject *)&__pyx_type_5pysam_6ctabix_Tabixfile) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1097; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_Tabixfile = &__pyx_type_5pysam_6ctabix_Tabixfile;
   /*--- Type import code ---*/
   __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "type", 
   #if CYTHON_COMPILING_IN_PYPY
@@ -11565,14 +12689,14 @@ PyMODINIT_FUNC PyInit_ctabix(void)
   __pyx_ptype_7cpython_7complex_complex = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "complex", sizeof(PyComplexObject), 0); if (unlikely(!__pyx_ptype_7cpython_7complex_complex)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_10TabProxies_TupleProxy = __Pyx_ImportType("pysam.TabProxies", "TupleProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_TupleProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_TupleProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 41; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_vtabptr_5pysam_10TabProxies_TupleProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_TupleProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_TupleProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 41; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_10TabProxies_GTFProxy = __Pyx_ImportType("pysam.TabProxies", "GTFProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_GTFProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_GTFProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_10TabProxies_GTFProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_GTFProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_GTFProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_10TabProxies_NamedTupleProxy = __Pyx_ImportType("pysam.TabProxies", "NamedTupleProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_10TabProxies_BedProxy = __Pyx_ImportType("pysam.TabProxies", "BedProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_BedProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_BedProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_10TabProxies_BedProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_BedProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_BedProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_10TabProxies_VCFProxy = __Pyx_ImportType("pysam.TabProxies", "VCFProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_VCFProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_VCFProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_10TabProxies_VCFProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_VCFProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_VCFProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_10TabProxies_GTFProxy = __Pyx_ImportType("pysam.TabProxies", "GTFProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_GTFProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_GTFProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_10TabProxies_GTFProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_GTFProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_GTFProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_10TabProxies_NamedTupleProxy = __Pyx_ImportType("pysam.TabProxies", "NamedTupleProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_10TabProxies_BedProxy = __Pyx_ImportType("pysam.TabProxies", "BedProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_BedProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_BedProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 76; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_10TabProxies_BedProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_BedProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_BedProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 76; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_10TabProxies_VCFProxy = __Pyx_ImportType("pysam.TabProxies", "VCFProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_VCFProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_VCFProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_10TabProxies_VCFProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_VCFProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_VCFProxy)) {__pyx_filename = __pyx_f[4]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   /*--- Variable import code ---*/
   /*--- Function import code ---*/
   /*--- Execution code ---*/
@@ -11601,40 +12725,40 @@ PyMODINIT_FUNC PyInit_ctabix(void)
   if (PyDict_SetItem(__pyx_d, __pyx_n_s_sys, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":25
- *     gzopen, gzclose, gzerror, gzdopen
+  /* "pysam/ctabix.pyx":26
+ *     tbx_destroy, gzopen, gzclose, gzerror, gzdopen
  * 
  * PYTHON3 = PY_MAJOR_VERSION >= 3             # <<<<<<<<<<<<<<
  * 
  * # filename encoding (copied from lxml.etree.pyx)
  */
-  __pyx_t_1 = __Pyx_PyBool_FromLong((PY_MAJOR_VERSION >= 3)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBool_FromLong((PY_MAJOR_VERSION >= 3)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 26; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_PYTHON3, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_PYTHON3, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 26; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":29
+  /* "pysam/ctabix.pyx":30
  * # filename encoding (copied from lxml.etree.pyx)
  * cdef str _FILENAME_ENCODING
  * _FILENAME_ENCODING = sys.getfilesystemencoding()             # <<<<<<<<<<<<<<
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getfilesystemencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getfilesystemencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_XGOTREF(__pyx_v_5pysam_6ctabix__FILENAME_ENCODING);
   __Pyx_DECREF_SET(__pyx_v_5pysam_6ctabix__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
   __Pyx_GIVEREF(__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":30
+  /* "pysam/ctabix.pyx":31
  * cdef str _FILENAME_ENCODING
  * _FILENAME_ENCODING = sys.getfilesystemencoding()
  * if _FILENAME_ENCODING is None:             # <<<<<<<<<<<<<<
@@ -11645,22 +12769,22 @@ PyMODINIT_FUNC PyInit_ctabix(void)
   __pyx_t_4 = (__pyx_t_3 != 0);
   if (__pyx_t_4) {
 
-    /* "pysam/ctabix.pyx":31
+    /* "pysam/ctabix.pyx":32
  * _FILENAME_ENCODING = sys.getfilesystemencoding()
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = sys.getdefaultencoding()             # <<<<<<<<<<<<<<
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = 'ascii'
  */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_sys); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getdefaultencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_getdefaultencoding); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (!(likely(PyString_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_XGOTREF(__pyx_v_5pysam_6ctabix__FILENAME_ENCODING);
     __Pyx_DECREF_SET(__pyx_v_5pysam_6ctabix__FILENAME_ENCODING, ((PyObject*)__pyx_t_1));
     __Pyx_GIVEREF(__pyx_t_1);
@@ -11669,7 +12793,7 @@ PyMODINIT_FUNC PyInit_ctabix(void)
   }
   __pyx_L2:;
 
-  /* "pysam/ctabix.pyx":32
+  /* "pysam/ctabix.pyx":33
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  * if _FILENAME_ENCODING is None:             # <<<<<<<<<<<<<<
@@ -11680,7 +12804,7 @@ PyMODINIT_FUNC PyInit_ctabix(void)
   __pyx_t_3 = (__pyx_t_4 != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/ctabix.pyx":33
+    /* "pysam/ctabix.pyx":34
  *     _FILENAME_ENCODING = sys.getdefaultencoding()
  * if _FILENAME_ENCODING is None:
  *     _FILENAME_ENCODING = 'ascii'             # <<<<<<<<<<<<<<
@@ -11695,168 +12819,180 @@ PyMODINIT_FUNC PyInit_ctabix(void)
   }
   __pyx_L3:;
 
-  /* "pysam/ctabix.pyx":475
+  /* "pysam/ctabix.pyx":555
  * 
  * 
  * class EmptyIterator:             # <<<<<<<<<<<<<<
  *     '''empty iterator'''
  * 
  */
-  __pyx_t_1 = __Pyx_Py3MetaclassPrepare((PyObject *) NULL, __pyx_empty_tuple, __pyx_n_s_EmptyIterator, __pyx_n_s_EmptyIterator, (PyObject *) NULL, __pyx_n_s_pysam_ctabix, __pyx_kp_s_empty_iterator); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_Py3MetaclassPrepare((PyObject *) NULL, __pyx_empty_tuple, __pyx_n_s_EmptyIterator, __pyx_n_s_EmptyIterator, (PyObject *) NULL, __pyx_n_s_pysam_ctabix, __pyx_kp_s_empty_iterator); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
 
-  /* "pysam/ctabix.pyx":478
+  /* "pysam/ctabix.pyx":558
  *     '''empty iterator'''
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
  * 
  */
-  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_13EmptyIterator_1__iter__, 0, __pyx_n_s_EmptyIterator___iter, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__22)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_13EmptyIterator_1__iter__, 0, __pyx_n_s_EmptyIterator___iter, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__20)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_iter, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_iter, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":482
- * 
+  /* "pysam/ctabix.pyx":561
+ *         return self
  * 
  *     def next(self):             # <<<<<<<<<<<<<<
  *         raise StopIteration()
  * 
  */
-  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_13EmptyIterator_3next, 0, __pyx_n_s_EmptyIterator_next, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__24)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_13EmptyIterator_3next, 0, __pyx_n_s_EmptyIterator_next, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__22)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_next_2, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_next_2, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 561; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":475
+  /* "pysam/ctabix.pyx":564
+ *         raise StopIteration()
+ * 
+ *     def __next__(self):             # <<<<<<<<<<<<<<
+ *         raise StopIteration()
+ * 
+ */
+  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_13EmptyIterator_5__next__, 0, __pyx_n_s_EmptyIterator___next, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__24)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_next, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+
+  /* "pysam/ctabix.pyx":555
  * 
  * 
  * class EmptyIterator:             # <<<<<<<<<<<<<<
  *     '''empty iterator'''
  * 
  */
-  __pyx_t_2 = __Pyx_Py3ClassCreate(((PyObject*)&__Pyx_DefaultClassType), __pyx_n_s_EmptyIterator, __pyx_empty_tuple, __pyx_t_1, NULL, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_Py3ClassCreate(((PyObject*)&__Pyx_DefaultClassType), __pyx_n_s_EmptyIterator, __pyx_empty_tuple, __pyx_t_1, NULL, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_EmptyIterator, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_EmptyIterator, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":594
+  /* "pysam/ctabix.pyx":685
  * 
  * 
  * def tabix_compress(filename_in,             # <<<<<<<<<<<<<<
  *                    filename_out,
  *                    force=False):
  */
-  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_1tabix_compress, NULL, __pyx_n_s_pysam_ctabix); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_1tabix_compress, NULL, __pyx_n_s_pysam_ctabix); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tabix_compress, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tabix_compress, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":647
+  /* "pysam/ctabix.pyx":738
  *         raise OSError("error when closing file %s" % filename_in)
  * 
  * def tabix_index( filename,             # <<<<<<<<<<<<<<
  *                  force = False,
  *                  seq_col = None,
  */
-  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_3tabix_index, NULL, __pyx_n_s_pysam_ctabix); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_3tabix_index, NULL, __pyx_n_s_pysam_ctabix); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tabix_index, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tabix_index, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":906
+  /* "pysam/ctabix.pyx":1006
  * 
  * 
  * class tabix_generic_iterator:             # <<<<<<<<<<<<<<
  *     '''iterate over ``infile``.
  * 
  */
-  __pyx_t_1 = __Pyx_Py3MetaclassPrepare((PyObject *) NULL, __pyx_empty_tuple, __pyx_n_s_tabix_generic_iterator, __pyx_n_s_tabix_generic_iterator, (PyObject *) NULL, __pyx_n_s_pysam_ctabix, __pyx_kp_s_iterate_over_infile_Permits_the); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_Py3MetaclassPrepare((PyObject *) NULL, __pyx_empty_tuple, __pyx_n_s_tabix_generic_iterator, __pyx_n_s_tabix_generic_iterator, (PyObject *) NULL, __pyx_n_s_pysam_ctabix, __pyx_kp_s_iterate_over_infile_Permits_the); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
 
-  /* "pysam/ctabix.pyx":911
+  /* "pysam/ctabix.pyx":1011
  *     Permits the use of file-like objects for example from the gzip module.
  *     '''
- *     def __init__(self, infile, parser ):             # <<<<<<<<<<<<<<
+ *     def __init__(self, infile, parser):             # <<<<<<<<<<<<<<
  * 
  *         self.infile = infile
  */
-  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_22tabix_generic_iterator_1__init__, 0, __pyx_n_s_tabix_generic_iterator___init, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__30)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_22tabix_generic_iterator_1__init__, 0, __pyx_n_s_tabix_generic_iterator___init, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__30)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_init, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_init, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":918
+  /* "pysam/ctabix.pyx":1018
  *         self.parser = parser
  * 
  *     def __iter__(self):             # <<<<<<<<<<<<<<
  *         return self
  * 
  */
-  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_22tabix_generic_iterator_3__iter__, 0, __pyx_n_s_tabix_generic_iterator___iter, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__32)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_22tabix_generic_iterator_3__iter__, 0, __pyx_n_s_tabix_generic_iterator___iter, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__32)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_iter, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_iter, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":922
+  /* "pysam/ctabix.pyx":1022
  * 
  *     # cython version - required for python 3
  *     def __next__(self):             # <<<<<<<<<<<<<<
  * 
  *         cdef char * b
  */
-  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_22tabix_generic_iterator_5__next__, 0, __pyx_n_s_tabix_generic_iterator___next, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__34)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_22tabix_generic_iterator_5__next__, 0, __pyx_n_s_tabix_generic_iterator___next, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__34)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_next, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_next, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":962
+  /* "pysam/ctabix.pyx":1066
  * 
  *     # python version - required for python 2.7
  *     def next(self):             # <<<<<<<<<<<<<<
  *         return self.__next__()
  * 
  */
-  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_22tabix_generic_iterator_7next, 0, __pyx_n_s_tabix_generic_iterator_next, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__36)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 962; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_22tabix_generic_iterator_7next, 0, __pyx_n_s_tabix_generic_iterator_next, NULL, __pyx_n_s_pysam_ctabix, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__36)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_next_2, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 962; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_1, __pyx_n_s_next_2, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1066; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/ctabix.pyx":906
+  /* "pysam/ctabix.pyx":1006
  * 
  * 
  * class tabix_generic_iterator:             # <<<<<<<<<<<<<<
  *     '''iterate over ``infile``.
  * 
  */
-  __pyx_t_2 = __Pyx_Py3ClassCreate(((PyObject*)&__Pyx_DefaultClassType), __pyx_n_s_tabix_generic_iterator, __pyx_empty_tuple, __pyx_t_1, NULL, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_Py3ClassCreate(((PyObject*)&__Pyx_DefaultClassType), __pyx_n_s_tabix_generic_iterator, __pyx_empty_tuple, __pyx_t_1, NULL, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tabix_generic_iterator, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tabix_generic_iterator, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":965
+  /* "pysam/ctabix.pyx":1069
  *         return self.__next__()
  * 
- * def tabix_iterator( infile, parser ):             # <<<<<<<<<<<<<<
- *     """return an iterator over all entries in a file."""
- *     if PYTHON3:
+ * def tabix_iterator(infile, parser):             # <<<<<<<<<<<<<<
+ *     """return an iterator over all entries in a file.
+ * 
  */
-  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_5tabix_iterator, NULL, __pyx_n_s_pysam_ctabix); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5pysam_6ctabix_5tabix_iterator, NULL, __pyx_n_s_pysam_ctabix); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tabix_iterator, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_tabix_iterator, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/ctabix.pyx":985
- * #            return tabix_generic_iterator( infile, parser )
+  /* "pysam/ctabix.pyx":1101
+ * 
  * 
  * __all__ = [             # <<<<<<<<<<<<<<
  *     "tabix_index",
  *     "tabix_compress",
  */
-  __pyx_t_1 = PyList_New(12); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(13); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1101; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_n_s_tabix_index);
   PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_tabix_index);
@@ -11864,37 +13000,40 @@ PyMODINIT_FUNC PyInit_ctabix(void)
   __Pyx_INCREF(__pyx_n_s_tabix_compress);
   PyList_SET_ITEM(__pyx_t_1, 1, __pyx_n_s_tabix_compress);
   __Pyx_GIVEREF(__pyx_n_s_tabix_compress);
+  __Pyx_INCREF(__pyx_n_s_TabixFile);
+  PyList_SET_ITEM(__pyx_t_1, 2, __pyx_n_s_TabixFile);
+  __Pyx_GIVEREF(__pyx_n_s_TabixFile);
   __Pyx_INCREF(__pyx_n_s_Tabixfile);
-  PyList_SET_ITEM(__pyx_t_1, 2, __pyx_n_s_Tabixfile);
+  PyList_SET_ITEM(__pyx_t_1, 3, __pyx_n_s_Tabixfile);
   __Pyx_GIVEREF(__pyx_n_s_Tabixfile);
   __Pyx_INCREF(__pyx_n_s_asTuple);
-  PyList_SET_ITEM(__pyx_t_1, 3, __pyx_n_s_asTuple);
+  PyList_SET_ITEM(__pyx_t_1, 4, __pyx_n_s_asTuple);
   __Pyx_GIVEREF(__pyx_n_s_asTuple);
   __Pyx_INCREF(__pyx_n_s_asGTF);
-  PyList_SET_ITEM(__pyx_t_1, 4, __pyx_n_s_asGTF);
+  PyList_SET_ITEM(__pyx_t_1, 5, __pyx_n_s_asGTF);
   __Pyx_GIVEREF(__pyx_n_s_asGTF);
   __Pyx_INCREF(__pyx_n_s_asVCF);
-  PyList_SET_ITEM(__pyx_t_1, 5, __pyx_n_s_asVCF);
+  PyList_SET_ITEM(__pyx_t_1, 6, __pyx_n_s_asVCF);
   __Pyx_GIVEREF(__pyx_n_s_asVCF);
   __Pyx_INCREF(__pyx_n_s_asBed);
-  PyList_SET_ITEM(__pyx_t_1, 6, __pyx_n_s_asBed);
+  PyList_SET_ITEM(__pyx_t_1, 7, __pyx_n_s_asBed);
   __Pyx_GIVEREF(__pyx_n_s_asBed);
   __Pyx_INCREF(__pyx_n_s_GZIterator);
-  PyList_SET_ITEM(__pyx_t_1, 7, __pyx_n_s_GZIterator);
+  PyList_SET_ITEM(__pyx_t_1, 8, __pyx_n_s_GZIterator);
   __Pyx_GIVEREF(__pyx_n_s_GZIterator);
   __Pyx_INCREF(__pyx_n_s_GZIteratorHead);
-  PyList_SET_ITEM(__pyx_t_1, 8, __pyx_n_s_GZIteratorHead);
+  PyList_SET_ITEM(__pyx_t_1, 9, __pyx_n_s_GZIteratorHead);
   __Pyx_GIVEREF(__pyx_n_s_GZIteratorHead);
   __Pyx_INCREF(__pyx_n_s_tabix_iterator);
-  PyList_SET_ITEM(__pyx_t_1, 9, __pyx_n_s_tabix_iterator);
+  PyList_SET_ITEM(__pyx_t_1, 10, __pyx_n_s_tabix_iterator);
   __Pyx_GIVEREF(__pyx_n_s_tabix_iterator);
   __Pyx_INCREF(__pyx_n_s_tabix_generic_iterator);
-  PyList_SET_ITEM(__pyx_t_1, 10, __pyx_n_s_tabix_generic_iterator);
+  PyList_SET_ITEM(__pyx_t_1, 11, __pyx_n_s_tabix_generic_iterator);
   __Pyx_GIVEREF(__pyx_n_s_tabix_generic_iterator);
   __Pyx_INCREF(__pyx_n_s_tabix_file_iterator);
-  PyList_SET_ITEM(__pyx_t_1, 11, __pyx_n_s_tabix_file_iterator);
+  PyList_SET_ITEM(__pyx_t_1, 12, __pyx_n_s_tabix_file_iterator);
   __Pyx_GIVEREF(__pyx_n_s_tabix_file_iterator);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1101; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
   /* "pysam/ctabix.pyx":1
@@ -12174,57 +13313,6 @@ bad:
 }
 #endif
 
-static CYTHON_INLINE PyObject* __Pyx_decode_c_string(
-         const char* cstring, Py_ssize_t start, Py_ssize_t stop,
-         const char* encoding, const char* errors,
-         PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) {
-    Py_ssize_t length;
-    if (unlikely((start < 0) | (stop < 0))) {
-        length = strlen(cstring);
-        if (start < 0) {
-            start += length;
-            if (start < 0)
-                start = 0;
-        }
-        if (stop < 0)
-            stop += length;
-    }
-    length = stop - start;
-    if (unlikely(length <= 0))
-        return PyUnicode_FromUnicode(NULL, 0);
-    cstring += start;
-    if (decode_func) {
-        return decode_func(cstring, length, errors);
-    } else {
-        return PyUnicode_Decode(cstring, length, encoding, errors);
-    }
-}
-
-static void __Pyx_RaiseArgtupleInvalid(
-    const char* func_name,
-    int exact,
-    Py_ssize_t num_min,
-    Py_ssize_t num_max,
-    Py_ssize_t num_found)
-{
-    Py_ssize_t num_expected;
-    const char *more_or_less;
-    if (num_found < num_min) {
-        num_expected = num_min;
-        more_or_less = "at least";
-    } else {
-        num_expected = num_max;
-        more_or_less = "at most";
-    }
-    if (exact) {
-        more_or_less = "exactly";
-    }
-    PyErr_Format(PyExc_TypeError,
-                 "%.200s() takes %.8s %" CYTHON_FORMAT_SSIZE_T "d positional argument%.1s (%" CYTHON_FORMAT_SSIZE_T "d given)",
-                 func_name, more_or_less, num_expected,
-                 (num_expected == 1) ? "" : "s", num_found);
-}
-
 static void __Pyx_RaiseDoubleKeywordsError(
     const char* func_name,
     PyObject* kw_name)
@@ -12339,6 +13427,31 @@ bad:
     return -1;
 }
 
+static void __Pyx_RaiseArgtupleInvalid(
+    const char* func_name,
+    int exact,
+    Py_ssize_t num_min,
+    Py_ssize_t num_max,
+    Py_ssize_t num_found)
+{
+    Py_ssize_t num_expected;
+    const char *more_or_less;
+    if (num_found < num_min) {
+        num_expected = num_min;
+        more_or_less = "at least";
+    } else {
+        num_expected = num_max;
+        more_or_less = "at most";
+    }
+    if (exact) {
+        more_or_less = "exactly";
+    }
+    PyErr_Format(PyExc_TypeError,
+                 "%.200s() takes %.8s %" CYTHON_FORMAT_SSIZE_T "d positional argument%.1s (%" CYTHON_FORMAT_SSIZE_T "d given)",
+                 func_name, more_or_less, num_expected,
+                 (num_expected == 1) ? "" : "s", num_found);
+}
+
 static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
     if (unlikely(!type)) {
         PyErr_SetString(PyExc_SystemError, "Missing type object");
@@ -12473,84 +13586,6 @@ return_ne:
 #endif
 }
 
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
-    PyObject *r;
-    if (!j) return NULL;
-    r = PyObject_GetItem(o, j);
-    Py_DECREF(j);
-    return r;
-}
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i,
-                                                              int wraparound, int boundscheck) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    if (wraparound & unlikely(i < 0)) i += PyList_GET_SIZE(o);
-    if ((!boundscheck) || likely((0 <= i) & (i < PyList_GET_SIZE(o)))) {
-        PyObject *r = PyList_GET_ITEM(o, i);
-        Py_INCREF(r);
-        return r;
-    }
-    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
-#else
-    return PySequence_GetItem(o, i);
-#endif
-}
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i,
-                                                              int wraparound, int boundscheck) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    if (wraparound & unlikely(i < 0)) i += PyTuple_GET_SIZE(o);
-    if ((!boundscheck) || likely((0 <= i) & (i < PyTuple_GET_SIZE(o)))) {
-        PyObject *r = PyTuple_GET_ITEM(o, i);
-        Py_INCREF(r);
-        return r;
-    }
-    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
-#else
-    return PySequence_GetItem(o, i);
-#endif
-}
-static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
-                                                     int is_list, int wraparound, int boundscheck) {
-#if CYTHON_COMPILING_IN_CPYTHON
-    if (is_list || PyList_CheckExact(o)) {
-        Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyList_GET_SIZE(o);
-        if ((!boundscheck) || (likely((n >= 0) & (n < PyList_GET_SIZE(o))))) {
-            PyObject *r = PyList_GET_ITEM(o, n);
-            Py_INCREF(r);
-            return r;
-        }
-    }
-    else if (PyTuple_CheckExact(o)) {
-        Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyTuple_GET_SIZE(o);
-        if ((!boundscheck) || likely((n >= 0) & (n < PyTuple_GET_SIZE(o)))) {
-            PyObject *r = PyTuple_GET_ITEM(o, n);
-            Py_INCREF(r);
-            return r;
-        }
-    } else {
-        PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
-        if (likely(m && m->sq_item)) {
-            if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
-                Py_ssize_t l = m->sq_length(o);
-                if (likely(l >= 0)) {
-                    i += l;
-                } else {
-                    if (PyErr_ExceptionMatches(PyExc_OverflowError))
-                        PyErr_Clear();
-                    else
-                        return NULL;
-                }
-            }
-            return m->sq_item(o, i);
-        }
-    }
-#else
-    if (is_list || PySequence_Check(o)) {
-        return PySequence_GetItem(o, i);
-    }
-#endif
-    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
-}
-
 static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {
     PyObject *result;
 #if CYTHON_COMPILING_IN_CPYTHON
@@ -13674,32 +14709,6 @@ static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {
     }
 }
 
-static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {
-    const long neg_one = (long) -1, const_zero = 0;
-    const int is_unsigned = neg_one > const_zero;
-    if (is_unsigned) {
-        if (sizeof(long) < sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(long) <= sizeof(unsigned long)) {
-            return PyLong_FromUnsignedLong((unsigned long) value);
-        } else if (sizeof(long) <= sizeof(unsigned long long)) {
-            return PyLong_FromUnsignedLongLong((unsigned long long) value);
-        }
-    } else {
-        if (sizeof(long) <= sizeof(long)) {
-            return PyInt_FromLong((long) value);
-        } else if (sizeof(long) <= sizeof(long long)) {
-            return PyLong_FromLongLong((long long) value);
-        }
-    }
-    {
-        int one = 1; int little = (int)*(unsigned char *)&one;
-        unsigned char *bytes = (unsigned char *)&value;
-        return _PyLong_FromByteArray(bytes, sizeof(long),
-                                     little, !is_unsigned);
-    }
-}
-
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
   #include "longintrepr.h"
@@ -13826,6 +14835,32 @@ static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value) {
     }
 }
 
+static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {
+    const long neg_one = (long) -1, const_zero = 0;
+    const int is_unsigned = neg_one > const_zero;
+    if (is_unsigned) {
+        if (sizeof(long) < sizeof(long)) {
+            return PyInt_FromLong((long) value);
+        } else if (sizeof(long) <= sizeof(unsigned long)) {
+            return PyLong_FromUnsignedLong((unsigned long) value);
+        } else if (sizeof(long) <= sizeof(unsigned long long)) {
+            return PyLong_FromUnsignedLongLong((unsigned long long) value);
+        }
+    } else {
+        if (sizeof(long) <= sizeof(long)) {
+            return PyInt_FromLong((long) value);
+        } else if (sizeof(long) <= sizeof(long long)) {
+            return PyLong_FromLongLong((long long) value);
+        }
+    }
+    {
+        int one = 1; int little = (int)*(unsigned char *)&one;
+        unsigned char *bytes = (unsigned char *)&value;
+        return _PyLong_FromByteArray(bytes, sizeof(long),
+                                     little, !is_unsigned);
+    }
+}
+
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
  #if CYTHON_USE_PYLONG_INTERNALS
   #include "longintrepr.h"
diff --git a/pysam/ctabix.pxd b/pysam/ctabix.pxd
index 2b186d4..31bf7c1 100644
--- a/pysam/ctabix.pxd
+++ b/pysam/ctabix.pxd
@@ -23,11 +23,12 @@ cdef class tabix_file_iterator:
     cdef size_t size
     cdef Parser parser
     cdef int fd
+    cdef int duplicated_fd
     cdef infile
 
     cdef __cnext__(self)
 
-cdef class Tabixfile:
+cdef class TabixFile:
 
     # pointer to tabixfile
     cdef htsFile * tabixfile
@@ -37,11 +38,21 @@ cdef class Tabixfile:
     # flag indicating whether file is remote
     cdef int isremote
 
-    cdef char * _filename
+    cdef _filename
+    cdef _filename_index
 
     cdef Parser parser
-    
+
+    cdef encoding    
+
+###########################################
+# used by cvcf.pyx
+cdef _force_str(object s, encoding=?)
+
+###########################################
 cdef class Parser:
+    cdef encoding
+
     cdef parse(self, char * buffer, int len)
 
 cdef class asTuple(Parser):
@@ -58,8 +69,9 @@ cdef class asVCF(Parser):
 
 cdef class TabixIterator:
     cdef hts_itr_t * iterator
-    cdef Tabixfile tabixfile
+    cdef TabixFile tabixfile
     cdef kstring_t buffer
+    cdef encoding
     cdef int __cnext__(self)
 
 cdef class TabixIteratorParsed(TabixIterator):
@@ -71,6 +83,7 @@ cdef class GZIterator:
     cdef kstream_t * kstream
     cdef kstring_t buffer
     cdef int __cnext__(self)
+    cdef encoding
 
 cdef class GZIteratorHead(GZIterator):
     pass
@@ -78,4 +91,7 @@ cdef class GZIteratorHead(GZIterator):
 cdef class GZIteratorParsed(GZIterator):
     cdef Parser parser
 
-cdef _force_str(object s)
+# Compatibility Layer for pysam < 0.8
+cdef class Tabixfile(TabixFile):
+    pass
+
diff --git a/pysam/ctabix.pyx b/pysam/ctabix.pyx
index 37cca5e..f712233 100644
--- a/pysam/ctabix.pyx
+++ b/pysam/ctabix.pyx
@@ -6,6 +6,7 @@ import sys
 from libc.stdio cimport printf, fprintf, stderr
 from libc.string cimport strerror
 from libc.errno cimport errno
+from posix.unistd cimport dup
 
 from cpython cimport PyErr_SetString, PyBytes_Check, \
     PyUnicode_Check, PyBytes_FromStringAndSize, \
@@ -17,10 +18,10 @@ cimport TabProxies
 
 from chtslib cimport htsFile, hts_open, hts_close, HTS_IDX_START,\
     BGZF, bgzf_open, bgzf_close, bgzf_write, \
-    ks_init, gzFile, ks_getuntil, kstring_t, \
+    ks_init, ks_destroy, gzFile, ks_getuntil, kstring_t, \
     tbx_index_build, tbx_index_load, tbx_itr_queryi, tbx_itr_querys, \
     tbx_conf_t, tbx_seqnames, tbx_itr_next, tbx_itr_destroy, \
-    gzopen, gzclose, gzerror, gzdopen
+    tbx_destroy, gzopen, gzclose, gzerror, gzdopen
 
 PYTHON3 = PY_MAJOR_VERSION >= 3
 
@@ -35,7 +36,7 @@ if _FILENAME_ENCODING is None:
 #cdef char* _C_FILENAME_ENCODING
 #_C_FILENAME_ENCODING = <char*>_FILENAME_ENCODING
 
-cdef bytes _encodeFilename(object filename):
+cdef inline bytes _encodeFilename(object filename):
     u"""Make sure a filename is 8-bit encoded (or None).
     """
     if filename is None:
@@ -47,7 +48,7 @@ cdef bytes _encodeFilename(object filename):
     else:
         raise TypeError, u"Argument must be string or unicode."
 
-cdef bytes _force_bytes(object s):
+cdef inline bytes _force_bytes(object s, encoding="ascii"):
     u"""convert string or unicode object to bytes, assuming ascii encoding.
     """
     if PY_MAJOR_VERSION < 3:
@@ -57,27 +58,25 @@ cdef bytes _force_bytes(object s):
     elif PyBytes_Check(s):
         return s
     elif PyUnicode_Check(s):
-        return s.encode('ascii')
+        return s.encode(encoding)
     else:
         raise TypeError, u"Argument must be string, bytes or unicode."
 
-cdef inline bytes _force_cmdline_bytes(object s):
-    return _force_bytes(s)
-
-cdef _charptr_to_str(char* s):
+cdef inline _charptr_to_str(char* s, encoding="ascii"):
     if PY_MAJOR_VERSION < 3:
         return s
     else:
-        return s.decode("ascii")
+        return s.decode(encoding)
 
-cdef _force_str(object s):
-    """Return s converted to str type of current Python (bytes in Py2, unicode in Py3)"""
+cdef _force_str(object s, encoding="ascii"):
+    """Return s converted to str type of current Python
+    (bytes in Py2, unicode in Py3)"""
     if s is None:
         return None
     if PY_MAJOR_VERSION < 3:
         return s
     elif PyBytes_Check(s):
-        return s.decode('ascii')
+        return s.decode(encoding)
     else:
         # assume unicode
         return s
@@ -85,6 +84,15 @@ cdef _force_str(object s):
 
 cdef class Parser:
 
+    def __init__(self, encoding="ascii"):
+        self.encoding = encoding
+
+    def set_encoding(self, encoding):
+        self.encoding = encoding
+
+    def get_encoding(self):
+        return self.encoding
+
     cdef parse(self, char * buffer, int length):
         raise NotImplementedError(
             'parse method of %s not implemented' % str(self))
@@ -92,84 +100,103 @@ cdef class Parser:
     def __call__(self, char * buffer, int length):
         return self.parse(buffer, length)
 
+
 cdef class asTuple(Parser):
     '''converts a :term:`tabix row` into a python tuple.
 
-    Access is by numeric index.
+    A field in a row is accessed by numeric index.
     ''' 
     cdef parse(self, char * buffer, int len):
         cdef TabProxies.TupleProxy r
-        r = TabProxies.TupleProxy()
+        r = TabProxies.TupleProxy(self.encoding)
         # need to copy - there were some
         # persistence issues with "present"
         r.copy(buffer, len)
         return r
 
+
 cdef class asGTF(Parser):
-    '''converts a :term:`tabix row` into a GTF record with the following 
+    '''converts a :term:`tabix row` into a GTF record with the following
     fields:
+   
+    +----------+----------+-------------------------------+
+    |*Column*  |*Name*    |*Content*                      |
+    +----------+----------+-------------------------------+
+    |1         |contig    |the chromosome name            |
+    +----------+----------+-------------------------------+
+    |2         |feature   |The feature type               |
+    +----------+----------+-------------------------------+
+    |3         |source    |The feature source             |
+    +----------+----------+-------------------------------+
+    |4         |start     |genomic start coordinate       |
+    |          |          |(0-based)                      |
+    +----------+----------+-------------------------------+
+    |5         |end       |genomic end coordinate         |
+    |          |          |(0-based)                      |
+    +----------+----------+-------------------------------+
+    |6         |score     |feature score                  |
+    +----------+----------+-------------------------------+
+    |7         |strand    |strand                         |
+    +----------+----------+-------------------------------+
+    |8         |frame     |frame                          |
+    +----------+----------+-------------------------------+
+    |9         |attributes|the attribute field            |
+    +----------+----------+-------------------------------+
+
+    GTF formatted entries also define the following fields that
+    are derived from the attributes field:
+
+    +--------------------+------------------------------+
+    |*Name*              |*Content*                     |
+    +--------------------+------------------------------+
+    |gene_id             |the gene identifier           |
+    +--------------------+------------------------------+
+    |transcript_id       |the transcript identifier     |
+    +--------------------+------------------------------+
 
-    contig
-       contig
-    feature
-       feature
-    source
-       source
-    start
-       genomic start coordinate (0-based)
-    end
-       genomic end coordinate plus one (0-based)
-    score
-       feature score
-    strand
-       strand
-    frame
-       frame
-    attributes
-       attribute string.
-
-    GTF formatted entries also defined the attributes:
-
-    gene_id
-       the gene identifier
-    transcript_ind
-       the transcript identifier
-    
     ''' 
     cdef parse(self, char * buffer, int len):
         cdef TabProxies.GTFProxy r
-        r = TabProxies.GTFProxy()
-        r.copy( buffer, len )
+        r = TabProxies.GTFProxy(self.encoding)
+        r.copy(buffer, len)
         return r
 
-cdef class asBed( Parser ):
+
+cdef class asBed(Parser):
     '''converts a :term:`tabix row` into a bed record
     with the following fields:
 
-    contig
-       contig
-    start
-       genomic start coordinate (zero-based)
-    end
-       genomic end coordinate plus one (zero-based)
-    name
-       name of feature.
-    score
-       score of feature
-    strand
-       strand of feature
-    thickStart
-       thickStart
-    thickEnd
-       thickEnd
-    itemRGB
-       itemRGB
-    blockCount
-       number of bocks
-    blockSizes
-       ',' separated string of block sizes
-    blockStarts
-       ',' separated string of block genomic start positions
+    +-----------+-----------+------------------------------------------+
+    |*Column*   |*Field*    |*Contents*                                |
+    |           |           |                                          |
+    +-----------+-----------+------------------------------------------+
+    |1          |contig     |contig                                    |
+    |           |           |                                          |
+    +-----------+-----------+------------------------------------------+
+    |2          |start      |genomic start coordinate (zero-based)     |
+    +-----------+-----------+------------------------------------------+
+    |3          |end        |genomic end coordinate plus one           |
+    |           |           |(zero-based)                              |
+    +-----------+-----------+------------------------------------------+
+    |4          |name       |name of feature.                          |
+    +-----------+-----------+------------------------------------------+
+    |5          |score      |score of feature                          |
+    +-----------+-----------+------------------------------------------+
+    |6          |strand     |strand of feature                         |
+    +-----------+-----------+------------------------------------------+
+    |7          |thickStart |thickStart                                |
+    +-----------+-----------+------------------------------------------+
+    |8          |thickEnd   |thickEnd                                  |
+    +-----------+-----------+------------------------------------------+
+    |9          |itemRGB    |itemRGB                                   |
+    +-----------+-----------+------------------------------------------+
+    |10         |blockCount |number of bocks                           |
+    +-----------+-----------+------------------------------------------+
+    |11         |blockSizes |',' separated string of block sizes       |
+    +-----------+-----------+------------------------------------------+
+    |12         |blockStarts|',' separated string of block genomic     |
+    |           |           |start positions                           |
+    +-----------+-----------+------------------------------------------+
 
     Only the first three fields are required. Additional
     fields are optional, but if one is defined, all the preceeding
@@ -178,32 +205,37 @@ cdef class asBed( Parser ):
     ''' 
     cdef parse(self, char * buffer, int len):
         cdef TabProxies.BedProxy r
-        r = TabProxies.BedProxy()
-        r.copy( buffer, len )
+        r = TabProxies.BedProxy(self.encoding)
+        r.copy(buffer, len)
         return r
 
-cdef class asVCF( Parser ): 
+
+cdef class asVCF(Parser): 
     '''converts a :term:`tabix row` into a VCF record with
     the following fields:
     
-    contig
-       contig
-    pos
-       chromosomal position, zero-based
-    id 
-       id
-    ref
-       reference
-    alt
-       alt
-    qual
-       qual
-    filter
-       filter
-    info
-       info
-    format
-       format specifier.
+    +----------+---------+------------------------------------+
+    |*Column*  |*Field*  |*Contents*                          |
+    |          |         |                                    |
+    +----------+---------+------------------------------------+
+    |1         |contig   |chromosome                          |
+    +----------+---------+------------------------------------+
+    |2         |pos      |chromosomal position, zero-based    |
+    +----------+---------+------------------------------------+
+    |3         |id       |id                                  |
+    +----------+---------+------------------------------------+
+    |4         |ref      |reference allele                    |
+    +----------+---------+------------------------------------+
+    |5         |alt      |alternate alleles                   |
+    +----------+---------+------------------------------------+
+    |6         |qual     |quality                             |
+    +----------+---------+------------------------------------+
+    |7         |filter   |filter                              |
+    +----------+---------+------------------------------------+
+    |8         |info     |info                                |
+    +----------+---------+------------------------------------+
+    |9         |format   |format specifier.                   |
+    +----------+---------+------------------------------------+
 
     Access to genotypes is via index::
 
@@ -212,14 +244,14 @@ cdef class asVCF( Parser ):
         second_sample_genotype = vcf[1]
 
     '''
-    cdef parse(self, char * buffer, int len ):
+    cdef parse(self, char * buffer, int len):
         cdef TabProxies.VCFProxy r
-        r = TabProxies.VCFProxy()
+        r = TabProxies.VCFProxy(self.encoding)
         r.copy(buffer, len)
         return r
 
 
-cdef class Tabixfile:
+cdef class TabixFile:
     '''*(filename, mode='r', parser = None)*
 
     opens a :term:`tabix file` for reading. A missing
@@ -229,27 +261,33 @@ cdef class Tabixfile:
     *parser* sets the default parser for this tabix file. If *parser*
     is None, the results are returned as an unparsed string.
     Otherwise, *parser* is assumed to be a functor that will return
-    parsed data (see for example :meth:`asTuple` and :meth:`asGTF`).
+    parsed data (see for example :class:`~pysam.asTuple` and
+    :class:`~pysam.asGTF`).
+
     '''
-    def __cinit__(self, filename, mode = 'r',
+    def __cinit__(self,
+                  filename,
+                  mode = 'r',
                   parser=None,
                   index=None,
+                  encoding="ascii",
                   *args,
                   **kwargs ):
 
         self.tabixfile = NULL
         self.parser = parser
-        self._open( filename, mode, index, *args, **kwargs )
+        self._open(filename, mode, index, *args, **kwargs)
+        self.encoding = encoding
 
     def _open( self, 
                filename,
-               mode ='r',
-               index = None,
+               mode='r',
+               index=None,
               ):
         '''open a :term:`tabix file` for reading.
         '''
 
-        assert mode in ( "r",), "invalid file opening mode `%s`" % mode
+        assert mode in ("r",), "invalid file opening mode `%s`" % mode
 
         if self.tabixfile != NULL:
             self.close()
@@ -258,40 +296,41 @@ cdef class Tabixfile:
         filename_index = index or (filename + ".tbi")
         self.isremote = filename.startswith("http:") or filename.startswith("ftp:")
 
-        # encode all the strings
-        filename = _encodeFilename(filename)
-        filename_index = _encodeFilename(filename_index)
-        cdef bytes bmode = mode.encode('ascii')
-
-        if self._filename != NULL: free(self._filename )
-
-        self._filename = strdup(filename)
-
-        if mode[0] == 'w':
-            # open file for writing
-            raise NotImplementedError("writing to tabix files not implemented" )
-
-        if mode[0] != "r":
-            raise ValueError("invalid mode '%s'" % mode)
-            # open file for reading
-            
         if not self.isremote:
             if not os.path.exists(filename):
                 raise IOError("file `%s` not found" % filename)
 
-            if not os.path.exists( filename_index ):
+            if not os.path.exists(filename_index):
                 raise IOError("index `%s` not found" % filename_index)
 
+        self._filename = filename
+        self._filename_index = filename_index
+
+        # encode all the strings to pass to tabix
+        _encoded_filename = _encodeFilename(filename)
+        _encoded_index = _encodeFilename(filename_index)
+
         # open file
-        self.tabixfile = hts_open(filename, 'r')
+        self.tabixfile = hts_open(_encoded_filename, 'r')
         if self.tabixfile == NULL:
             raise IOError("could not open file `%s`" % filename)
         
-        self.index = tbx_index_load(filename_index)
+        self.index = tbx_index_load(_encoded_index)
         if self.index == NULL:
             raise IOError("could not open index for `%s`" % filename)
 
-    def _isOpen( self ):
+    def _dup(self):
+        '''return a copy of this tabix file.
+        
+        The file is being re-opened.
+        '''
+        return TabixFile(self._filename,
+                         mode="r", 
+                         parser=self.parser,
+                         index=self._filename_index,
+                         encoding=self.encoding)
+
+    def _isOpen(self):
         '''return true if samfile has been opened.'''
         return self.tabixfile != NULL
 
@@ -301,7 +340,8 @@ cdef class Tabixfile:
               start=None, 
               end=None, 
               region=None,
-              parser=None):
+              parser=None,
+              multiple_iterators=False):
         '''fetch one or more rows in a :term:`region` using 0-based
         indexing. The region is specified by :term:`reference`,
         *start* and *end*. Alternatively, a samtools :term:`region`
@@ -314,6 +354,13 @@ cdef class Tabixfile:
 
         If *parser* is None, the default parser will be used for
         parsing.
+        
+        Set *multiple_iterators* to true if you will be using multiple
+        iterators on the same file at the same time. The iterator
+        returned will receive its own copy of a filehandle to the file
+        effectively re-opening the file. Re-opening a file creates
+        some overhead, so beware.
+
         '''
         if not self._isOpen():
             raise ValueError("I/O operation on closed file")
@@ -334,15 +381,23 @@ cdef class Tabixfile:
 
         # get iterator
         cdef hts_itr_t * iter
+        cdef TabixFile fileobj
+
+        # reopen the same file if necessary
+        if multiple_iterators:
+            fileobj = self._dup()
+        else:
+            fileobj = self
+
         if region is None:
             # without region or reference - iterate from start
-            iter = tbx_itr_queryi(self.index,
+            iter = tbx_itr_queryi(fileobj.index,
                                   HTS_IDX_START,
                                   0,
                                   0)
         else:
-            iter = tbx_itr_querys(self.index,
-                                  region)
+            s = _force_bytes(region, encoding=fileobj.encoding)
+            iter = tbx_itr_querys(fileobj.index, s)
 
         if iter == NULL:
             if region is None:
@@ -351,21 +406,23 @@ cdef class Tabixfile:
                 return EmptyIterator()
             else:
                 raise ValueError(
-                    "could not create iterator for region '%s'" % region)
+                    "could not create iterator for region '%s'" %
+                    region)
             
         # use default parser if no parser is specified
         if parser is None:
-            parser = self.parser
+            parser = fileobj.parser
 
         cdef TabixIterator a
         if parser is None: 
-            a = TabixIterator()
+            a = TabixIterator(encoding=fileobj.encoding)
         else:
+            parser.set_encoding(fileobj.encoding)
             a = TabixIteratorParsed(parser)
 
-        a.tabixfile = self
+        a.tabixfile = fileobj
         a.iterator = iter
-        
+
         return a
 
     ###############################################################
@@ -376,14 +433,15 @@ cdef class Tabixfile:
     property filename:
         '''filename associated with this object.'''
         def __get__(self):
-            if not self._isOpen(): raise ValueError( "I/O operation on closed file" )
+            if not self._isOpen():
+                raise ValueError("I/O operation on closed file")
             return self._filename
 
     property header:
         '''the file header.
           
         .. note::
-            The header is returned as an iterator over lines without the
+            The header is returned as an iterator presenting lines without the
             newline character.
         '''
         
@@ -391,7 +449,7 @@ cdef class Tabixfile:
             return GZIteratorHead(self.filename)
 
     property contigs:
-        '''chromosome names'''
+        '''list of chromosome names'''
         def __get__(self):
             cdef char ** sequences
             cdef int nsequences
@@ -410,10 +468,13 @@ cdef class Tabixfile:
             
     def close(self):
         '''
-        closes the :class:`pysam.Tabixfile`.'''
+        closes the :class:`pysam.TabixFile`.'''
         if self.tabixfile != NULL:
             hts_close(self.tabixfile)
             self.tabixfile = NULL
+        if self.index != NULL:
+            tbx_destroy(self.index)
+            self.index = NULL
 
     def __dealloc__( self ):
         # remember: dealloc cannot call other python methods
@@ -422,13 +483,17 @@ cdef class Tabixfile:
         if self.tabixfile != NULL:
             hts_close(self.tabixfile)
             self.tabixfile = NULL
-        if self._filename != NULL:
-            free(self._filename)
+        if self.index != NULL:
+            tbx_destroy(self.index)
+
 
 cdef class TabixIterator:
     """iterates over rows in *tabixfile* in region
     given by *tid*, *start* and *end*.
     """
+
+    def __init__(self, encoding="ascii"):
+        self.encoding = encoding
     
     def __iter__(self):
         self.buffer.s = NULL
@@ -438,10 +503,18 @@ cdef class TabixIterator:
         return self 
 
     cdef int __cnext__(self):
-        '''return value needs to be freed by caller'''
+        '''iterate to next element.
+        
+        Return -5 if file has been closed when this function
+        was called.
+        '''
+        if self.tabixfile.tabixfile == NULL:
+            return -5
 
         cdef int retval
+
         while 1:
+                
             retval = tbx_itr_next(
                 self.tabixfile.tabixfile,
                 self.tabixfile.index,
@@ -462,14 +535,21 @@ cdef class TabixIterator:
         """
         
         cdef int retval = self.__cnext__()
-        if retval < 0:
+        if retval == -5:
+            raise IOError("iteration on closed file")
+        elif retval < 0:
             raise StopIteration
 
-        return _charptr_to_str(self.buffer.s)
+        return _charptr_to_str(self.buffer.s, self.encoding)
+
+    def next(self):
+        return self.__next__()
 
     def __dealloc__(self):
         if <void*>self.iterator != NULL:
             tbx_itr_destroy(self.iterator)
+        if self.buffer.s != NULL:
+            free(self.buffer.s)
 
 
 class EmptyIterator:
@@ -478,14 +558,18 @@ class EmptyIterator:
     def __iter__(self):
         return self
 
-
     def next(self):
         raise StopIteration()
 
+    def __next__(self):
+        raise StopIteration()
+
 
 cdef class TabixIteratorParsed(TabixIterator):
     """iterates over mapped reads in a region.
 
+    The *parser* determines the encoding.
+
     Returns parsed data.
     """
 
@@ -502,13 +586,17 @@ cdef class TabixIteratorParsed(TabixIterator):
         """
         
         cdef int retval = self.__cnext__()
-        if retval < 0:
+        if retval == -5:
+            raise IOError("iteration on closed file")
+        elif retval < 0:
             raise StopIteration
-        return self.parser.parse(self.buffer.s, self.buffer.l)
+
+        return self.parser.parse(self.buffer.s,
+                                 self.buffer.l)
 
 
 cdef class GZIterator:
-    def __init__(self, filename, int buffer_size=65536):
+    def __init__(self, filename, int buffer_size=65536, encoding="ascii"):
         '''iterate line-by-line through gzip (or bgzip)
         compressed file.
         '''
@@ -519,6 +607,7 @@ cdef class GZIterator:
         self.gzipfile = gzopen(filename, "r")
         self._filename = filename
         self.kstream = ks_init(self.gzipfile)
+        self.encoding = encoding
 
         self.buffer.l = 0
         self.buffer.m = 0
@@ -529,7 +618,9 @@ cdef class GZIterator:
         if self.gzipfile != NULL:
             gzclose(self.gzipfile)
             self.gzipfile = NULL
-        free(self.buffer.s)
+        if self.buffer.s != NULL:
+            free(self.buffer.s)
+        ks_destroy(self.kstream)
 
     def __iter__(self):
         return self
@@ -552,7 +643,7 @@ cdef class GZIterator:
         cdef int retval = self.__cnext__()
         if retval < 0:
             raise StopIteration
-        return self.buffer.s
+        return _force_str(self.buffer.s, self.encoding)
 
 
 cdef class GZIteratorHead(GZIterator):
@@ -615,12 +706,12 @@ def tabix_compress(filename_in,
 
     WINDOW_SIZE = 64 * 1024
 
-    fn = _force_bytes(filename_out)
+    fn = _encodeFilename(filename_out)
     fp = bgzf_open( fn, "w")
     if fp == NULL:
         raise IOError("could not open '%s' for writing")
 
-    fn = _force_bytes(filename_in)
+    fn = _encodeFilename(filename_in)
     fd_src = open(fn, O_RDONLY)
     if fd_src == 0:
         raise IOError("could not open '%s' for reading")
@@ -694,28 +785,31 @@ def tabix_index( filename,
     if not os.path.exists(filename):
         raise IOError("No such file '%s'" % filename)
 
-    if preset == None and (seq_col == None or start_col == None or end_col == None):
-        raise ValueError("neither preset nor seq_col,start_col and end_col given" )
+    if preset is None and \
+       (seq_col is None or start_col is None or end_col is None):
+        raise ValueError(
+            "neither preset nor seq_col,start_col and end_col given")
 
     if not filename.endswith(".gz"): 
-        tabix_compress( filename, filename + ".gz", force = force )
+        tabix_compress(filename, filename + ".gz", force=force)
         os.unlink( filename )
         filename += ".gz"
 
-    if not force and os.path.exists(filename + ".tbi" ):
-        raise IOError( "Filename '%s.tbi' already exists, use *force* to overwrite" )
+    if not force and os.path.exists(filename + ".tbi"):
+        raise IOError(
+            "Filename '%s.tbi' already exists, use *force* to overwrite")
 
     # columns (1-based):
     #   preset-code, contig, start, end, metachar for
     #     comments, lines to ignore at beginning
     # 0 is a missing column
     preset2conf = {
-        'gff' : ( 0, 1, 4, 5, ord('#'), 0 ),
-        'bed' : ( 0x10000, 1, 2, 3, ord('#'), 0 ),
-        'psltbl' : ( 0x10000, 15, 17, 18, ord('#'), 0 ),
-        'sam' : ( 1, 3, 4, 0, ord('@'), 0 ),
-        'vcf' : ( 2, 1, 2, 0, ord('#'), 0 ),
-        'pileup': (3, 1, 2, 0, ord('#'), 0 ),
+        'gff' : (0, 1, 4, 5, ord('#'), 0),
+        'bed' : (0x10000, 1, 2, 3, ord('#'), 0),
+        'psltbl' : (0x10000, 15, 17, 18, ord('#'), 0),
+        'sam' : (1, 3, 4, 0, ord('@'), 0),
+        'vcf' : (2, 1, 2, 0, ord('#'), 0),
+        'pileup': (3, 1, 2, 0, ord('#'), 0),
         }
 
     if preset:
@@ -836,7 +930,7 @@ cdef class tabix_file_iterator:
                   int buffer_size=65536):
 
         if infile.closed:
-            raise ValueError( "I/O operation on closed file." )
+            raise ValueError("I/O operation on closed file.")
 
         self.infile = infile
 
@@ -844,12 +938,14 @@ cdef class tabix_file_iterator:
         if fd == -1:
             raise ValueError("I/O operation on closed file.")
 
+        self.duplicated_fd = dup(fd)
+
         # From the manual:
         # gzopen can be used to read a file which is not in gzip format; 
         # in this case gzread will directly read from the file without decompression. 
         # When reading, this will be detected automatically by looking 
         # for the magic two-byte gzip header. 
-        self.fh = gzdopen(fd, 'r')
+        self.fh = gzdopen(self.duplicated_fd, 'r')
 
         if self.fh == NULL: 
             raise IOError('%s' % strerror(errno))
@@ -881,20 +977,24 @@ cdef class tabix_file_iterator:
             b = self.buffer.s
             
             # skip comments
-            if (b[0] == '#'): continue
+            if (b[0] == '#'):
+                continue
 
             # skip empty lines
-            if b[0] == '\0' or b[0] == '\n' or b[0] == '\r': continue
+            if b[0] == '\0' or b[0] == '\n' or b[0] == '\r':
+                continue
 
             # gzgets terminates at \n, no need to test
 
             # parser creates a copy
-            return self.parser.parse( b, self.buffer.l )
+            return self.parser.parse( b, self.buffer.l)
 
         raise StopIteration
 
     def __dealloc__(self):
         free(self.buffer.s)
+        ks_destroy(self.kstream)
+        gzclose(self.fh)
         
     def __next__(self):
         return self.__cnext__()
@@ -908,7 +1008,7 @@ class tabix_generic_iterator:
     
     Permits the use of file-like objects for example from the gzip module.
     '''
-    def __init__(self, infile, parser ):
+    def __init__(self, infile, parser):
 
         self.infile = infile
         if self.infile.closed:
@@ -925,34 +1025,38 @@ class tabix_generic_iterator:
         cdef char * cpy
         cdef size_t nbytes
 
+        encoding = self.parser.get_encoding()
+
         # note that GzipFile.close() does not close the file
         # reading is still possible.
-        if self.infile.closed: raise ValueError( "I/O operation on closed file." )
+        if self.infile.closed:
+            raise ValueError("I/O operation on closed file.")
 
         while 1:
 
             line = self.infile.readline()
-            if not line: break
+            if not line:
+                break
             
-            s = _force_bytes( line )
+            s = _force_bytes(line, encoding)
             b = s
-            nbytes = len( line )
+            nbytes = len(line)
             assert b[nbytes] == '\0'
 
             # skip comments
-            if (b[0] == '#'): continue
+            if b[0] == '#':
+                continue
 
             # skip empty lines
-            if b[0] == '\0' or b[0] == '\n' or b[0] == '\r': continue
+            if b[0] == '\0' or b[0] == '\n' or b[0] == '\r':
+                continue
             
             # make sure that entry is complete
             if b[nbytes-1] != '\n' and b[nbytes-1] != '\r':
-                raise ValueError( "incomplete line at %s" % line )
+                raise ValueError("incomplete line at %s" % line)
             
-            # create a copy
-            cpy = <char*>malloc(nbytes+1)        
-            if cpy == NULL: raise MemoryError()
-            memcpy( cpy, b, nbytes+1)
+            bytes_cpy = <bytes> b
+            cpy = <char *> bytes_cpy
 
             return self.parser(cpy, nbytes)            
 
@@ -962,12 +1066,20 @@ class tabix_generic_iterator:
     def next(self):
         return self.__next__()
 
-def tabix_iterator( infile, parser ):
-    """return an iterator over all entries in a file."""
+def tabix_iterator(infile, parser):
+    """return an iterator over all entries in a file.
+    
+    Results are returned parsed as specified by the *parser*. If
+    *parser* is None, the results are returned as an unparsed string.
+    Otherwise, *parser* is assumed to be a functor that will return
+    parsed data (see for example :class:`~pysam.asTuple` and
+    :class:`~pysam.asGTF`).
+
+    """
     if PYTHON3:
-        return tabix_generic_iterator( infile, parser )
+        return tabix_generic_iterator(infile, parser)
     else:
-        return tabix_file_iterator( infile, parser )
+        return tabix_file_iterator(infile, parser)
         
     # file objects can use C stdio
     # used to be: isinstance( infile, file):
@@ -982,9 +1094,14 @@ def tabix_iterator( infile, parser ):
 #        else:
 #            return tabix_generic_iterator( infile, parser )
     
+cdef class Tabixfile(TabixFile):
+    pass
+
+
 __all__ = [
     "tabix_index", 
     "tabix_compress",
+    "TabixFile",
     "Tabixfile",
     "asTuple",
     "asGTF",
diff --git a/pysam/cvcf.c b/pysam/cvcf.c
index 291da31..3e1e1da 100644
--- a/pysam/cvcf.c
+++ b/pysam/cvcf.c
@@ -1,4 +1,4 @@
-/* Generated by Cython 0.20.1 on Sun Jun 22 20:32:41 2014 */
+/* Generated by Cython 0.20.1 on Fri Nov 21 19:49:04 2014 */
 
 #define PY_SSIZE_T_CLEAN
 #ifndef CYTHON_USE_PYLONG_INTERNALS
@@ -547,7 +547,7 @@ static const char *__pyx_f[] = {
 
 /*--- Type declarations ---*/
 struct __pyx_obj_5pysam_6ctabix_tabix_file_iterator;
-struct __pyx_obj_5pysam_6ctabix_Tabixfile;
+struct __pyx_obj_5pysam_6ctabix_TabixFile;
 struct __pyx_obj_5pysam_6ctabix_Parser;
 struct __pyx_obj_5pysam_6ctabix_asTuple;
 struct __pyx_obj_5pysam_6ctabix_asGTF;
@@ -558,6 +558,7 @@ struct __pyx_obj_5pysam_6ctabix_TabixIteratorParsed;
 struct __pyx_obj_5pysam_6ctabix_GZIterator;
 struct __pyx_obj_5pysam_6ctabix_GZIteratorHead;
 struct __pyx_obj_5pysam_6ctabix_GZIteratorParsed;
+struct __pyx_obj_5pysam_6ctabix_Tabixfile;
 struct __pyx_obj_5pysam_10TabProxies_TupleProxy;
 struct __pyx_obj_5pysam_10TabProxies_GTFProxy;
 struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy;
@@ -566,8 +567,21 @@ struct __pyx_obj_5pysam_10TabProxies_VCFProxy;
 struct __pyx_obj_5pysam_4cvcf_VCFRecord;
 struct __pyx_obj_5pysam_4cvcf_asVCFRecord;
 struct __pyx_obj_5pysam_4cvcf___pyx_scope_struct___parse;
+struct __pyx_opt_args_5pysam_6ctabix__force_str;
+
+/* "ctabix.pxd":50
+ * ###########################################
+ * # used by cvcf.pyx
+ * cdef _force_str(object s, encoding=?)             # <<<<<<<<<<<<<<
+ * 
+ * ###########################################
+ */
+struct __pyx_opt_args_5pysam_6ctabix__force_str {
+  int __pyx_n;
+  PyObject *encoding;
+};
 
-/* "ctabix.pxd":18
+/* "ctabix.pxd":19
  *     kstream_t, kstring_t, gzFile, tbx_t
  * 
  * cdef class tabix_file_iterator:             # <<<<<<<<<<<<<<
@@ -583,41 +597,45 @@ struct __pyx_obj_5pysam_6ctabix_tabix_file_iterator {
   size_t size;
   struct __pyx_obj_5pysam_6ctabix_Parser *parser;
   int fd;
+  int duplicated_fd;
   PyObject *infile;
 };
 
 
-/* "ctabix.pxd":29
+/* "ctabix.pxd":31
  *     cdef __cnext__(self)
  * 
- * cdef class Tabixfile:             # <<<<<<<<<<<<<<
+ * cdef class TabixFile:             # <<<<<<<<<<<<<<
  * 
  *     # pointer to tabixfile
  */
-struct __pyx_obj_5pysam_6ctabix_Tabixfile {
+struct __pyx_obj_5pysam_6ctabix_TabixFile {
   PyObject_HEAD
   htsFile *tabixfile;
   tbx_t *index;
   int isremote;
-  char *_filename;
+  PyObject *_filename;
+  PyObject *_filename_index;
   struct __pyx_obj_5pysam_6ctabix_Parser *parser;
+  PyObject *encoding;
 };
 
 
-/* "ctabix.pxd":43
- *     cdef Parser parser
+/* "ctabix.pxd":53
  * 
+ * ###########################################
  * cdef class Parser:             # <<<<<<<<<<<<<<
- *     cdef parse(self, char * buffer, int len)
+ *     cdef encoding
  * 
  */
 struct __pyx_obj_5pysam_6ctabix_Parser {
   PyObject_HEAD
   struct __pyx_vtabstruct_5pysam_6ctabix_Parser *__pyx_vtab;
+  PyObject *encoding;
 };
 
 
-/* "ctabix.pxd":46
+/* "ctabix.pxd":58
  *     cdef parse(self, char * buffer, int len)
  * 
  * cdef class asTuple(Parser):             # <<<<<<<<<<<<<<
@@ -629,7 +647,7 @@ struct __pyx_obj_5pysam_6ctabix_asTuple {
 };
 
 
-/* "ctabix.pxd":49
+/* "ctabix.pxd":61
  *     cdef parse(self, char * buffer, int len)
  * 
  * cdef class asGTF(Parser):             # <<<<<<<<<<<<<<
@@ -641,7 +659,7 @@ struct __pyx_obj_5pysam_6ctabix_asGTF {
 };
 
 
-/* "ctabix.pxd":52
+/* "ctabix.pxd":64
  *     pass
  * 
  * cdef class asBed(Parser):             # <<<<<<<<<<<<<<
@@ -653,7 +671,7 @@ struct __pyx_obj_5pysam_6ctabix_asBed {
 };
 
 
-/* "ctabix.pxd":55
+/* "ctabix.pxd":67
  *     pass
  * 
  * cdef class asVCF(Parser):             # <<<<<<<<<<<<<<
@@ -665,23 +683,24 @@ struct __pyx_obj_5pysam_6ctabix_asVCF {
 };
 
 
-/* "ctabix.pxd":58
+/* "ctabix.pxd":70
  *     pass
  * 
  * cdef class TabixIterator:             # <<<<<<<<<<<<<<
  *     cdef hts_itr_t * iterator
- *     cdef Tabixfile tabixfile
+ *     cdef TabixFile tabixfile
  */
 struct __pyx_obj_5pysam_6ctabix_TabixIterator {
   PyObject_HEAD
   struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator *__pyx_vtab;
   hts_itr_t *iterator;
-  struct __pyx_obj_5pysam_6ctabix_Tabixfile *tabixfile;
+  struct __pyx_obj_5pysam_6ctabix_TabixFile *tabixfile;
   kstring_t buffer;
+  PyObject *encoding;
 };
 
 
-/* "ctabix.pxd":64
+/* "ctabix.pxd":77
  *     cdef int __cnext__(self)
  * 
  * cdef class TabixIteratorParsed(TabixIterator):             # <<<<<<<<<<<<<<
@@ -694,7 +713,7 @@ struct __pyx_obj_5pysam_6ctabix_TabixIteratorParsed {
 };
 
 
-/* "ctabix.pxd":67
+/* "ctabix.pxd":80
  *     cdef Parser parser
  * 
  * cdef class GZIterator:             # <<<<<<<<<<<<<<
@@ -708,11 +727,12 @@ struct __pyx_obj_5pysam_6ctabix_GZIterator {
   gzFile gzipfile;
   kstream_t *kstream;
   kstring_t buffer;
+  PyObject *encoding;
 };
 
 
-/* "ctabix.pxd":74
- *     cdef int __cnext__(self)
+/* "ctabix.pxd":88
+ *     cdef encoding
  * 
  * cdef class GZIteratorHead(GZIterator):             # <<<<<<<<<<<<<<
  *     pass
@@ -723,7 +743,7 @@ struct __pyx_obj_5pysam_6ctabix_GZIteratorHead {
 };
 
 
-/* "ctabix.pxd":77
+/* "ctabix.pxd":91
  *     pass
  * 
  * cdef class GZIteratorParsed(GZIterator):             # <<<<<<<<<<<<<<
@@ -736,6 +756,18 @@ struct __pyx_obj_5pysam_6ctabix_GZIteratorParsed {
 };
 
 
+/* "ctabix.pxd":95
+ * 
+ * # Compatibility Layer for pysam < 0.8
+ * cdef class Tabixfile(TabixFile):             # <<<<<<<<<<<<<<
+ *     pass
+ * 
+ */
+struct __pyx_obj_5pysam_6ctabix_Tabixfile {
+  struct __pyx_obj_5pysam_6ctabix_TabixFile __pyx_base;
+};
+
+
 /* "TabProxies.pxd":41
  *   ctypedef int uint64_t
  * 
@@ -753,13 +785,14 @@ struct __pyx_obj_5pysam_10TabProxies_TupleProxy {
   int nbytes;
   int offset;
   int is_modified;
+  PyObject *encoding;
 };
 
 
-/* "TabProxies.pxd":60
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "TabProxies.pxd":63
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class GTFProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class GTFProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -770,10 +803,10 @@ struct __pyx_obj_5pysam_10TabProxies_GTFProxy {
 };
 
 
-/* "TabProxies.pxd":69
+/* "TabProxies.pxd":73
  *     cdef char * getAttributes( self )
  * 
- * cdef class NamedTupleProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class NamedTupleProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  *     pass
  * 
  */
@@ -782,10 +815,10 @@ struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy {
 };
 
 
-/* "TabProxies.pxd":72
+/* "TabProxies.pxd":76
  *     pass
  * 
- * cdef class BedProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class BedProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -798,10 +831,10 @@ struct __pyx_obj_5pysam_10TabProxies_BedProxy {
 };
 
 
-/* "TabProxies.pxd":83
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "TabProxies.pxd":88
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class VCFProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class VCFProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -812,7 +845,7 @@ struct __pyx_obj_5pysam_10TabProxies_VCFProxy {
 };
 
 
-/* "pysam/cvcf.pyx":95
+/* "pysam/cvcf.pyx":96
  * ###########################################################################################################
  * 
  * cdef class VCFRecord( TabProxies.TupleProxy):             # <<<<<<<<<<<<<<
@@ -827,7 +860,7 @@ struct __pyx_obj_5pysam_4cvcf_VCFRecord {
 };
 
 
-/* "pysam/cvcf.pyx":229
+/* "pysam/cvcf.pyx":230
  * 
  * 
  * cdef class asVCFRecord(ctabix.Parser):             # <<<<<<<<<<<<<<
@@ -840,7 +873,7 @@ struct __pyx_obj_5pysam_4cvcf_asVCFRecord {
 };
 
 
-/* "pysam/cvcf.pyx":906
+/* "pysam/cvcf.pyx":914
  *         return line
  * 
  *     def _parse(self, line, stream):             # <<<<<<<<<<<<<<
@@ -860,7 +893,7 @@ struct __pyx_obj_5pysam_4cvcf___pyx_scope_struct___parse {
 
 
 
-/* "ctabix.pxd":18
+/* "ctabix.pxd":19
  *     kstream_t, kstring_t, gzFile, tbx_t
  * 
  * cdef class tabix_file_iterator:             # <<<<<<<<<<<<<<
@@ -874,11 +907,11 @@ struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator {
 static struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator *__pyx_vtabptr_5pysam_6ctabix_tabix_file_iterator;
 
 
-/* "ctabix.pxd":43
- *     cdef Parser parser
+/* "ctabix.pxd":53
  * 
+ * ###########################################
  * cdef class Parser:             # <<<<<<<<<<<<<<
- *     cdef parse(self, char * buffer, int len)
+ *     cdef encoding
  * 
  */
 
@@ -888,7 +921,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_Parser {
 static struct __pyx_vtabstruct_5pysam_6ctabix_Parser *__pyx_vtabptr_5pysam_6ctabix_Parser;
 
 
-/* "ctabix.pxd":46
+/* "ctabix.pxd":58
  *     cdef parse(self, char * buffer, int len)
  * 
  * cdef class asTuple(Parser):             # <<<<<<<<<<<<<<
@@ -902,7 +935,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_asTuple {
 static struct __pyx_vtabstruct_5pysam_6ctabix_asTuple *__pyx_vtabptr_5pysam_6ctabix_asTuple;
 
 
-/* "ctabix.pxd":49
+/* "ctabix.pxd":61
  *     cdef parse(self, char * buffer, int len)
  * 
  * cdef class asGTF(Parser):             # <<<<<<<<<<<<<<
@@ -916,7 +949,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_asGTF {
 static struct __pyx_vtabstruct_5pysam_6ctabix_asGTF *__pyx_vtabptr_5pysam_6ctabix_asGTF;
 
 
-/* "ctabix.pxd":52
+/* "ctabix.pxd":64
  *     pass
  * 
  * cdef class asBed(Parser):             # <<<<<<<<<<<<<<
@@ -930,7 +963,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_asBed {
 static struct __pyx_vtabstruct_5pysam_6ctabix_asBed *__pyx_vtabptr_5pysam_6ctabix_asBed;
 
 
-/* "ctabix.pxd":55
+/* "ctabix.pxd":67
  *     pass
  * 
  * cdef class asVCF(Parser):             # <<<<<<<<<<<<<<
@@ -944,12 +977,12 @@ struct __pyx_vtabstruct_5pysam_6ctabix_asVCF {
 static struct __pyx_vtabstruct_5pysam_6ctabix_asVCF *__pyx_vtabptr_5pysam_6ctabix_asVCF;
 
 
-/* "ctabix.pxd":58
+/* "ctabix.pxd":70
  *     pass
  * 
  * cdef class TabixIterator:             # <<<<<<<<<<<<<<
  *     cdef hts_itr_t * iterator
- *     cdef Tabixfile tabixfile
+ *     cdef TabixFile tabixfile
  */
 
 struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator {
@@ -958,7 +991,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator {
 static struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator *__pyx_vtabptr_5pysam_6ctabix_TabixIterator;
 
 
-/* "ctabix.pxd":64
+/* "ctabix.pxd":77
  *     cdef int __cnext__(self)
  * 
  * cdef class TabixIteratorParsed(TabixIterator):             # <<<<<<<<<<<<<<
@@ -972,7 +1005,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_TabixIteratorParsed {
 static struct __pyx_vtabstruct_5pysam_6ctabix_TabixIteratorParsed *__pyx_vtabptr_5pysam_6ctabix_TabixIteratorParsed;
 
 
-/* "ctabix.pxd":67
+/* "ctabix.pxd":80
  *     cdef Parser parser
  * 
  * cdef class GZIterator:             # <<<<<<<<<<<<<<
@@ -986,8 +1019,8 @@ struct __pyx_vtabstruct_5pysam_6ctabix_GZIterator {
 static struct __pyx_vtabstruct_5pysam_6ctabix_GZIterator *__pyx_vtabptr_5pysam_6ctabix_GZIterator;
 
 
-/* "ctabix.pxd":74
- *     cdef int __cnext__(self)
+/* "ctabix.pxd":88
+ *     cdef encoding
  * 
  * cdef class GZIteratorHead(GZIterator):             # <<<<<<<<<<<<<<
  *     pass
@@ -1000,7 +1033,7 @@ struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorHead {
 static struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorHead *__pyx_vtabptr_5pysam_6ctabix_GZIteratorHead;
 
 
-/* "ctabix.pxd":77
+/* "ctabix.pxd":91
  *     pass
  * 
  * cdef class GZIteratorParsed(GZIterator):             # <<<<<<<<<<<<<<
@@ -1023,7 +1056,8 @@ static struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorParsed *__pyx_vtabptr_5p
  */
 
 struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy {
-  int (*getMaxFields)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, size_t);
+  int (*getMaxFields)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *);
+  int (*getMinFields)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *);
   PyObject *(*take)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t);
   PyObject *(*present)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t);
   PyObject *(*copy)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t);
@@ -1032,10 +1066,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_TupleProxy *__pyx_vtabptr_5pysam_10TabProxies_TupleProxy;
 
 
-/* "TabProxies.pxd":60
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "TabProxies.pxd":63
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class GTFProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class GTFProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -1047,10 +1081,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy *__pyx_vtabptr_5pysam_10TabProxies_GTFProxy;
 
 
-/* "TabProxies.pxd":69
+/* "TabProxies.pxd":73
  *     cdef char * getAttributes( self )
  * 
- * cdef class NamedTupleProxy( TupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class NamedTupleProxy(TupleProxy) :             # <<<<<<<<<<<<<<
  *     pass
  * 
  */
@@ -1061,10 +1095,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy *__pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy;
 
 
-/* "TabProxies.pxd":72
+/* "TabProxies.pxd":76
  *     pass
  * 
- * cdef class BedProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class BedProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -1075,10 +1109,10 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy *__pyx_vtabptr_5pysam_10TabProxies_BedProxy;
 
 
-/* "TabProxies.pxd":83
- *     cdef update( self, char * buffer, size_t nbytes )
+/* "TabProxies.pxd":88
+ *     cdef update(self, char * buffer, size_t nbytes)
  * 
- * cdef class VCFProxy( NamedTupleProxy) :             # <<<<<<<<<<<<<<
+ * cdef class VCFProxy(NamedTupleProxy) :             # <<<<<<<<<<<<<<
  * 
  *     cdef:
  */
@@ -1089,7 +1123,7 @@ struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy {
 static struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy *__pyx_vtabptr_5pysam_10TabProxies_VCFProxy;
 
 
-/* "pysam/cvcf.pyx":95
+/* "pysam/cvcf.pyx":96
  * ###########################################################################################################
  * 
  * cdef class VCFRecord( TabProxies.TupleProxy):             # <<<<<<<<<<<<<<
@@ -1103,7 +1137,7 @@ struct __pyx_vtabstruct_5pysam_4cvcf_VCFRecord {
 static struct __pyx_vtabstruct_5pysam_4cvcf_VCFRecord *__pyx_vtabptr_5pysam_4cvcf_VCFRecord;
 
 
-/* "pysam/cvcf.pyx":229
+/* "pysam/cvcf.pyx":230
  * 
  * 
  * cdef class asVCFRecord(ctabix.Parser):             # <<<<<<<<<<<<<<
@@ -1622,7 +1656,7 @@ static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
 
 /* Module declarations from 'pysam.ctabix' */
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_tabix_file_iterator = 0;
-static PyTypeObject *__pyx_ptype_5pysam_6ctabix_Tabixfile = 0;
+static PyTypeObject *__pyx_ptype_5pysam_6ctabix_TabixFile = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_Parser = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_asTuple = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_asGTF = 0;
@@ -1633,7 +1667,8 @@ static PyTypeObject *__pyx_ptype_5pysam_6ctabix_TabixIteratorParsed = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_GZIterator = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_GZIteratorHead = 0;
 static PyTypeObject *__pyx_ptype_5pysam_6ctabix_GZIteratorParsed = 0;
-static PyObject *(*__pyx_f_5pysam_6ctabix__force_str)(PyObject *); /*proto*/
+static PyTypeObject *__pyx_ptype_5pysam_6ctabix_Tabixfile = 0;
+static PyObject *(*__pyx_f_5pysam_6ctabix__force_str)(PyObject *, struct __pyx_opt_args_5pysam_6ctabix__force_str *__pyx_optional_args); /*proto*/
 
 /* Module declarations from 'pysam.TabProxies' */
 static PyTypeObject *__pyx_ptype_5pysam_10TabProxies_TupleProxy = 0;
@@ -1717,7 +1752,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_71parse(CYTHON_UNUSED PyObject *__py
 static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_73write(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self, PyObject *__pyx_v_stream, PyObject *__pyx_v_datagenerator); /* proto */
 static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_75writeheader(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self, PyObject *__pyx_v_stream); /* proto */
 static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_77compare_calls(CYTHON_UNUSED PyObject *__pyx_self, CYTHON_UNUSED PyObject *__pyx_v_self, PyObject *__pyx_v_pos1, PyObject *__pyx_v_ref1, PyObject *__pyx_v_alt1, PyObject *__pyx_v_pos2, PyObject *__pyx_v_ref2, PyObject *__pyx_v_alt2); /* proto */
-static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_79connect(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self, PyObject *__pyx_v_filename); /* proto */
+static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_79connect(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_encoding); /* proto */
 static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_81fetch(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self, PyObject *__pyx_v_reference, PyObject *__pyx_v_start, PyObject *__pyx_v_end, PyObject *__pyx_v_region); /* proto */
 static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_83validate(CYTHON_UNUSED PyObject *__pyx_self, CYTHON_UNUSED PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *__pyx_v_record); /* proto */
 static PyObject *__pyx_tp_new_5pysam_4cvcf_VCFRecord(PyTypeObject *t, PyObject *a, PyObject *k); /*proto*/
@@ -1842,6 +1877,7 @@ static char __pyx_k_CHROM[] = "CHROM";
 static char __pyx_k_Error[] = "Error";
 static char __pyx_k_Float[] = "Float";
 static char __pyx_k_addns[] = "addns";
+static char __pyx_k_ascii[] = "ascii";
 static char __pyx_k_blurp[] = "blurp";
 static char __pyx_k_chrom[] = "chrom";
 static char __pyx_k_close[] = "close";
@@ -1926,6 +1962,7 @@ static char __pyx_k_Genotype[] = "Genotype";
 static char __pyx_k_KeyError[] = "KeyError";
 static char __pyx_k_Number_2[] = "Number";
 static char __pyx_k_deepcopy[] = "deepcopy";
+static char __pyx_k_encoding[] = "encoding";
 static char __pyx_k_endswith[] = "endswith";
 static char __pyx_k_expected[] = "expected";
 static char __pyx_k_filename[] = "filename";
@@ -2391,6 +2428,7 @@ static PyObject *__pyx_n_s_alt1;
 static PyObject *__pyx_n_s_alt2;
 static PyObject *__pyx_n_s_append;
 static PyObject *__pyx_n_s_args;
+static PyObject *__pyx_n_s_ascii;
 static PyObject *__pyx_n_s_bisect;
 static PyObject *__pyx_n_s_blurp;
 static PyObject *__pyx_n_s_c;
@@ -2417,6 +2455,7 @@ static PyObject *__pyx_n_s_dict;
 static PyObject *__pyx_n_s_dict_2;
 static PyObject *__pyx_n_s_doc;
 static PyObject *__pyx_n_s_elts;
+static PyObject *__pyx_n_s_encoding;
 static PyObject *__pyx_n_s_end;
 static PyObject *__pyx_n_s_endswith;
 static PyObject *__pyx_n_s_enter_default_format;
@@ -2780,8 +2819,9 @@ static PyObject *__pyx_tuple__179;
 static PyObject *__pyx_tuple__181;
 static PyObject *__pyx_tuple__183;
 static PyObject *__pyx_tuple__185;
-static PyObject *__pyx_tuple__187;
+static PyObject *__pyx_tuple__186;
 static PyObject *__pyx_tuple__188;
+static PyObject *__pyx_tuple__189;
 static PyObject *__pyx_codeobj__95;
 static PyObject *__pyx_codeobj__97;
 static PyObject *__pyx_codeobj__100;
@@ -2824,10 +2864,10 @@ static PyObject *__pyx_codeobj__178;
 static PyObject *__pyx_codeobj__180;
 static PyObject *__pyx_codeobj__182;
 static PyObject *__pyx_codeobj__184;
-static PyObject *__pyx_codeobj__186;
-static PyObject *__pyx_codeobj__189;
+static PyObject *__pyx_codeobj__187;
+static PyObject *__pyx_codeobj__190;
 
-/* "pysam/cvcf.pyx":59
+/* "pysam/cvcf.pyx":60
  * 
  * # Utility function.  Uses 0-based coordinates
  * def get_sequence(chrom, start, end, fa):             # <<<<<<<<<<<<<<
@@ -2871,21 +2911,21 @@ static PyObject *__pyx_pw_5pysam_4cvcf_1get_sequence(PyObject *__pyx_self, PyObj
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_start)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("get_sequence", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("get_sequence", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_end)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("get_sequence", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("get_sequence", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  3:
         if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_fa)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("get_sequence", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("get_sequence", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "get_sequence") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "get_sequence") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
       goto __pyx_L5_argtuple_error;
@@ -2902,7 +2942,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_1get_sequence(PyObject *__pyx_self, PyObj
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("get_sequence", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("get_sequence", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.get_sequence", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -2931,15 +2971,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_get_sequence(CYTHON_UNUSED PyObject *__py
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("get_sequence", 0);
 
-  /* "pysam/cvcf.pyx":61
+  /* "pysam/cvcf.pyx":62
  * def get_sequence(chrom, start, end, fa):
  *     # obtain sequence from .fa file, without truncation
  *     if end<=start: return ""             # <<<<<<<<<<<<<<
  *     if not fa: return "N"*(end-start)
  *     if start<0: return "N"*(-start) + get_sequence(chrom, 0, end, fa).upper()
  */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_end, __pyx_v_start, Py_LE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_v_end, __pyx_v_start, Py_LE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_2) {
     __Pyx_XDECREF(__pyx_r);
@@ -2948,20 +2988,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_get_sequence(CYTHON_UNUSED PyObject *__py
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":62
+  /* "pysam/cvcf.pyx":63
  *     # obtain sequence from .fa file, without truncation
  *     if end<=start: return ""
  *     if not fa: return "N"*(end-start)             # <<<<<<<<<<<<<<
  *     if start<0: return "N"*(-start) + get_sequence(chrom, 0, end, fa).upper()
  *     sequence = fa.fetch(chrom, start, end).upper()
  */
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_fa); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_fa); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_3 = ((!__pyx_t_2) != 0);
   if (__pyx_t_3) {
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = PyNumber_Subtract(__pyx_v_end, __pyx_v_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_Subtract(__pyx_v_end, __pyx_v_start); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_4 = PyNumber_Multiply(__pyx_n_s_N, __pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyNumber_Multiply(__pyx_n_s_N, __pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_r = __pyx_t_4;
@@ -2969,26 +3009,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_get_sequence(CYTHON_UNUSED PyObject *__py
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":63
+  /* "pysam/cvcf.pyx":64
  *     if end<=start: return ""
  *     if not fa: return "N"*(end-start)
  *     if start<0: return "N"*(-start) + get_sequence(chrom, 0, end, fa).upper()             # <<<<<<<<<<<<<<
  *     sequence = fa.fetch(chrom, start, end).upper()
  *     if len(sequence) < end-start: sequence += "N"*(end-start-len(sequence))
  */
-  __pyx_t_4 = PyObject_RichCompare(__pyx_v_start, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyObject_RichCompare(__pyx_v_start, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   if (__pyx_t_3) {
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_4 = PyNumber_Negative(__pyx_v_start); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyNumber_Negative(__pyx_v_start); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_1 = PyNumber_Multiply(__pyx_n_s_N, __pyx_t_4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_Multiply(__pyx_n_s_N, __pyx_t_4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = PyTuple_New(4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_INCREF(__pyx_v_chrom);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_chrom);
@@ -3002,17 +3042,17 @@ static PyObject *__pyx_pf_5pysam_4cvcf_get_sequence(CYTHON_UNUSED PyObject *__py
     __Pyx_INCREF(__pyx_v_fa);
     PyTuple_SET_ITEM(__pyx_t_5, 3, __pyx_v_fa);
     __Pyx_GIVEREF(__pyx_v_fa);
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_5, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_5, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_upper); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_upper); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = PyNumber_Add(__pyx_t_1, __pyx_t_6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyNumber_Add(__pyx_t_1, __pyx_t_6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
@@ -3021,16 +3061,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_get_sequence(CYTHON_UNUSED PyObject *__py
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":64
+  /* "pysam/cvcf.pyx":65
  *     if not fa: return "N"*(end-start)
  *     if start<0: return "N"*(-start) + get_sequence(chrom, 0, end, fa).upper()
  *     sequence = fa.fetch(chrom, start, end).upper()             # <<<<<<<<<<<<<<
  *     if len(sequence) < end-start: sequence += "N"*(end-start-len(sequence))
  *     return sequence
  */
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_fa, __pyx_n_s_fetch); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_fa, __pyx_n_s_fetch); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_6 = PyTuple_New(3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = PyTuple_New(3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_INCREF(__pyx_v_chrom);
   PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_chrom);
@@ -3041,50 +3081,50 @@ static PyObject *__pyx_pf_5pysam_4cvcf_get_sequence(CYTHON_UNUSED PyObject *__py
   __Pyx_INCREF(__pyx_v_end);
   PyTuple_SET_ITEM(__pyx_t_6, 2, __pyx_v_end);
   __Pyx_GIVEREF(__pyx_v_end);
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_6, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_6, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_upper); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_upper); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
   __pyx_v_sequence = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":65
+  /* "pysam/cvcf.pyx":66
  *     if start<0: return "N"*(-start) + get_sequence(chrom, 0, end, fa).upper()
  *     sequence = fa.fetch(chrom, start, end).upper()
  *     if len(sequence) < end-start: sequence += "N"*(end-start-len(sequence))             # <<<<<<<<<<<<<<
  *     return sequence
  * 
  */
-  __pyx_t_7 = PyObject_Length(__pyx_v_sequence); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_7); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = PyObject_Length(__pyx_v_sequence); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_7); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_6 = PyNumber_Subtract(__pyx_v_end, __pyx_v_start); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = PyNumber_Subtract(__pyx_v_end, __pyx_v_start); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_5 = PyObject_RichCompare(__pyx_t_1, __pyx_t_6, Py_LT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyObject_RichCompare(__pyx_t_1, __pyx_t_6, Py_LT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   if (__pyx_t_3) {
-    __pyx_t_5 = PyNumber_Subtract(__pyx_v_end, __pyx_v_start); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyNumber_Subtract(__pyx_v_end, __pyx_v_start); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_7 = PyObject_Length(__pyx_v_sequence); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = PyInt_FromSsize_t(__pyx_t_7); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyObject_Length(__pyx_v_sequence); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyInt_FromSsize_t(__pyx_t_7); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_1 = PyNumber_Subtract(__pyx_t_5, __pyx_t_6); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_Subtract(__pyx_t_5, __pyx_t_6); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = PyNumber_Multiply(__pyx_n_s_N, __pyx_t_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyNumber_Multiply(__pyx_n_s_N, __pyx_t_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyNumber_InPlaceAdd(__pyx_v_sequence, __pyx_t_6); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_InPlaceAdd(__pyx_v_sequence, __pyx_t_6); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_DECREF_SET(__pyx_v_sequence, __pyx_t_1);
@@ -3093,7 +3133,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_get_sequence(CYTHON_UNUSED PyObject *__py
   }
   __pyx_L6:;
 
-  /* "pysam/cvcf.pyx":66
+  /* "pysam/cvcf.pyx":67
  *     sequence = fa.fetch(chrom, start, end).upper()
  *     if len(sequence) < end-start: sequence += "N"*(end-start-len(sequence))
  *     return sequence             # <<<<<<<<<<<<<<
@@ -3105,7 +3145,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_get_sequence(CYTHON_UNUSED PyObject *__py
   __pyx_r = __pyx_v_sequence;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":59
+  /* "pysam/cvcf.pyx":60
  * 
  * # Utility function.  Uses 0-based coordinates
  * def get_sequence(chrom, start, end, fa):             # <<<<<<<<<<<<<<
@@ -3128,7 +3168,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_get_sequence(CYTHON_UNUSED PyObject *__py
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":69
+/* "pysam/cvcf.pyx":70
  * 
  * # Utility function.  Parses a region string
  * def parse_regions( string ):             # <<<<<<<<<<<<<<
@@ -3179,35 +3219,35 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse_regions", 0);
 
-  /* "pysam/cvcf.pyx":70
+  /* "pysam/cvcf.pyx":71
  * # Utility function.  Parses a region string
  * def parse_regions( string ):
  *     result = []             # <<<<<<<<<<<<<<
  *     for r in string.split(','):
  *         elts = r.split(':')
  */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 70; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_result = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":71
+  /* "pysam/cvcf.pyx":72
  * def parse_regions( string ):
  *     result = []
  *     for r in string.split(','):             # <<<<<<<<<<<<<<
  *         elts = r.split(':')
  *         chrom, start, end = elts[0], 0, 3000000000
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_string, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_string, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) {
     __pyx_t_1 = __pyx_t_2; __Pyx_INCREF(__pyx_t_1); __pyx_t_3 = 0;
     __pyx_t_4 = NULL;
   } else {
-    __pyx_t_3 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_t_4 = Py_TYPE(__pyx_t_1)->tp_iternext;
   }
@@ -3216,16 +3256,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
     if (!__pyx_t_4 && PyList_CheckExact(__pyx_t_1)) {
       if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_4 && PyTuple_CheckExact(__pyx_t_1)) {
       if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_2 = __pyx_t_4(__pyx_t_1);
@@ -3233,7 +3273,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -3242,29 +3282,29 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
     __Pyx_XDECREF_SET(__pyx_v_r, __pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":72
+    /* "pysam/cvcf.pyx":73
  *     result = []
  *     for r in string.split(','):
  *         elts = r.split(':')             # <<<<<<<<<<<<<<
  *         chrom, start, end = elts[0], 0, 3000000000
  *         if len(elts)==1: pass
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_r, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_r, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_XDECREF_SET(__pyx_v_elts, __pyx_t_5);
     __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":73
+    /* "pysam/cvcf.pyx":74
  *     for r in string.split(','):
  *         elts = r.split(':')
  *         chrom, start, end = elts[0], 0, 3000000000             # <<<<<<<<<<<<<<
  *         if len(elts)==1: pass
  *         elif len(elts)==2:
  */
-    __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 74; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_5);
     __pyx_t_2 = __pyx_int_0;
     __Pyx_INCREF(__pyx_t_2);
@@ -3277,80 +3317,80 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
     __Pyx_XDECREF_SET(__pyx_v_end, __pyx_t_6);
     __pyx_t_6 = 0;
 
-    /* "pysam/cvcf.pyx":74
+    /* "pysam/cvcf.pyx":75
  *         elts = r.split(':')
  *         chrom, start, end = elts[0], 0, 3000000000
  *         if len(elts)==1: pass             # <<<<<<<<<<<<<<
  *         elif len(elts)==2:
  *             if len(elts[1])>0:
  */
-    __pyx_t_7 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 74; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 75; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_8 = ((__pyx_t_7 == 1) != 0);
     if (__pyx_t_8) {
       goto __pyx_L5;
     }
 
-    /* "pysam/cvcf.pyx":75
+    /* "pysam/cvcf.pyx":76
  *         chrom, start, end = elts[0], 0, 3000000000
  *         if len(elts)==1: pass
  *         elif len(elts)==2:             # <<<<<<<<<<<<<<
  *             if len(elts[1])>0:
  *                 ielts = elts[1].split('-')
  */
-    __pyx_t_7 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 75; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 76; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_8 = ((__pyx_t_7 == 2) != 0);
     if (__pyx_t_8) {
 
-      /* "pysam/cvcf.pyx":76
+      /* "pysam/cvcf.pyx":77
  *         if len(elts)==1: pass
  *         elif len(elts)==2:
  *             if len(elts[1])>0:             # <<<<<<<<<<<<<<
  *                 ielts = elts[1].split('-')
  *                 if len(ielts) != 2: ValueError("Don't understand region string '%s'" % r)
  */
-      __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 76; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_7 = PyObject_Length(__pyx_t_6); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 76; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_Length(__pyx_t_6); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __pyx_t_8 = ((__pyx_t_7 > 0) != 0);
       if (__pyx_t_8) {
 
-        /* "pysam/cvcf.pyx":77
+        /* "pysam/cvcf.pyx":78
  *         elif len(elts)==2:
  *             if len(elts[1])>0:
  *                 ielts = elts[1].split('-')             # <<<<<<<<<<<<<<
  *                 if len(ielts) != 2: ValueError("Don't understand region string '%s'" % r)
  *                 try:    start, end = int(ielts[0])-1, int(ielts[1])
  */
-        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __Pyx_XDECREF_SET(__pyx_v_ielts, __pyx_t_6);
         __pyx_t_6 = 0;
 
-        /* "pysam/cvcf.pyx":78
+        /* "pysam/cvcf.pyx":79
  *             if len(elts[1])>0:
  *                 ielts = elts[1].split('-')
  *                 if len(ielts) != 2: ValueError("Don't understand region string '%s'" % r)             # <<<<<<<<<<<<<<
  *                 try:    start, end = int(ielts[0])-1, int(ielts[1])
  *                 except: raise ValueError("Don't understand region string '%s'" % r)
  */
-        __pyx_t_7 = PyObject_Length(__pyx_v_ielts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyObject_Length(__pyx_v_ielts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_8 = ((__pyx_t_7 != 2) != 0);
         if (__pyx_t_8) {
-          __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_Don_t_understand_region_string_s, __pyx_v_r); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_6 = __Pyx_PyString_Format(__pyx_kp_s_Don_t_understand_region_string_s, __pyx_v_r); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_6);
-          __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_2);
           PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_6);
           __Pyx_GIVEREF(__pyx_t_6);
           __pyx_t_6 = 0;
-          __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_6);
           __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
           __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
@@ -3358,7 +3398,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
         }
         __pyx_L7:;
 
-        /* "pysam/cvcf.pyx":79
+        /* "pysam/cvcf.pyx":80
  *                 ielts = elts[1].split('-')
  *                 if len(ielts) != 2: ValueError("Don't understand region string '%s'" % r)
  *                 try:    start, end = int(ielts[0])-1, int(ielts[1])             # <<<<<<<<<<<<<<
@@ -3371,17 +3411,17 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
           __Pyx_XGOTREF(__pyx_t_10);
           __Pyx_XGOTREF(__pyx_t_11);
           /*try:*/ {
-            __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_ielts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L8_error;};
+            __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_ielts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L8_error;};
             __Pyx_GOTREF(__pyx_t_6);
-            __pyx_t_2 = PyNumber_Int(__pyx_t_6); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L8_error;}
+            __pyx_t_2 = PyNumber_Int(__pyx_t_6); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L8_error;}
             __Pyx_GOTREF(__pyx_t_2);
             __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-            __pyx_t_6 = PyNumber_Subtract(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L8_error;}
+            __pyx_t_6 = PyNumber_Subtract(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L8_error;}
             __Pyx_GOTREF(__pyx_t_6);
             __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-            __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_ielts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L8_error;};
+            __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_ielts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L8_error;};
             __Pyx_GOTREF(__pyx_t_2);
-            __pyx_t_5 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L8_error;}
+            __pyx_t_5 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L8_error;}
             __Pyx_GOTREF(__pyx_t_5);
             __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
             __Pyx_DECREF_SET(__pyx_v_start, __pyx_t_6);
@@ -3398,7 +3438,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
           __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0;
           __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-          /* "pysam/cvcf.pyx":80
+          /* "pysam/cvcf.pyx":81
  *                 if len(ielts) != 2: ValueError("Don't understand region string '%s'" % r)
  *                 try:    start, end = int(ielts[0])-1, int(ielts[1])
  *                 except: raise ValueError("Don't understand region string '%s'" % r)             # <<<<<<<<<<<<<<
@@ -3407,23 +3447,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
  */
           /*except:*/ {
             __Pyx_AddTraceback("pysam.cvcf.parse_regions", __pyx_clineno, __pyx_lineno, __pyx_filename);
-            if (__Pyx_GetException(&__pyx_t_5, &__pyx_t_6, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+            if (__Pyx_GetException(&__pyx_t_5, &__pyx_t_6, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
             __Pyx_GOTREF(__pyx_t_5);
             __Pyx_GOTREF(__pyx_t_6);
             __Pyx_GOTREF(__pyx_t_2);
-            __pyx_t_12 = __Pyx_PyString_Format(__pyx_kp_s_Don_t_understand_region_string_s, __pyx_v_r); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+            __pyx_t_12 = __Pyx_PyString_Format(__pyx_kp_s_Don_t_understand_region_string_s, __pyx_v_r); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
             __Pyx_GOTREF(__pyx_t_12);
-            __pyx_t_13 = PyTuple_New(1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+            __pyx_t_13 = PyTuple_New(1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
             __Pyx_GOTREF(__pyx_t_13);
             PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_t_12);
             __Pyx_GIVEREF(__pyx_t_12);
             __pyx_t_12 = 0;
-            __pyx_t_12 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_13, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+            __pyx_t_12 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_13, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
             __Pyx_GOTREF(__pyx_t_12);
             __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
             __Pyx_Raise(__pyx_t_12, 0, 0, 0);
             __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-            {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
+            {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L10_except_error;}
             __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
             __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
             __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -3449,37 +3489,37 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
     }
     /*else*/ {
 
-      /* "pysam/cvcf.pyx":82
+      /* "pysam/cvcf.pyx":83
  *                 except: raise ValueError("Don't understand region string '%s'" % r)
  *         else:
  *             raise ValueError("Don't understand region string '%s'" % r)             # <<<<<<<<<<<<<<
  *         result.append( (chrom,start,end) )
  *     return result
  */
-      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_Don_t_understand_region_string_s, __pyx_v_r); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_Don_t_understand_region_string_s, __pyx_v_r); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_Raise(__pyx_t_2, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     __pyx_L5:;
 
-    /* "pysam/cvcf.pyx":83
+    /* "pysam/cvcf.pyx":84
  *         else:
  *             raise ValueError("Don't understand region string '%s'" % r)
  *         result.append( (chrom,start,end) )             # <<<<<<<<<<<<<<
  *     return result
  * 
  */
-    __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 84; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_v_chrom);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_chrom);
@@ -3490,12 +3530,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
     __Pyx_INCREF(__pyx_v_end);
     PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_end);
     __Pyx_GIVEREF(__pyx_v_end);
-    __pyx_t_14 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_2); if (unlikely(__pyx_t_14 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_14 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_2); if (unlikely(__pyx_t_14 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 84; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   }
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":84
+  /* "pysam/cvcf.pyx":85
  *             raise ValueError("Don't understand region string '%s'" % r)
  *         result.append( (chrom,start,end) )
  *     return result             # <<<<<<<<<<<<<<
@@ -3507,7 +3547,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
   __pyx_r = __pyx_v_result;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":69
+  /* "pysam/cvcf.pyx":70
  * 
  * # Utility function.  Parses a region string
  * def parse_regions( string ):             # <<<<<<<<<<<<<<
@@ -3538,7 +3578,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_2parse_regions(CYTHON_UNUSED PyObject *__
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":105
+/* "pysam/cvcf.pyx":106
  *     cdef uint32_t pos
  * 
  *     def __init__(self, vcf):             # <<<<<<<<<<<<<<
@@ -3574,7 +3614,7 @@ static int __pyx_pw_5pysam_4cvcf_9VCFRecord_1__init__(PyObject *__pyx_v_self, Py
         else goto __pyx_L5_argtuple_error;
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 1) {
       goto __pyx_L5_argtuple_error;
@@ -3585,7 +3625,7 @@ static int __pyx_pw_5pysam_4cvcf_9VCFRecord_1__init__(PyObject *__pyx_v_self, Py
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCFRecord.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -3603,7 +3643,7 @@ static int __pyx_pf_5pysam_4cvcf_9VCFRecord___init__(struct __pyx_obj_5pysam_4cv
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
 
-  /* "pysam/cvcf.pyx":106
+  /* "pysam/cvcf.pyx":107
  * 
  *     def __init__(self, vcf):
  *         self.vcf = vcf             # <<<<<<<<<<<<<<
@@ -3616,7 +3656,7 @@ static int __pyx_pf_5pysam_4cvcf_9VCFRecord___init__(struct __pyx_obj_5pysam_4cv
   __Pyx_DECREF(__pyx_v_self->vcf);
   __pyx_v_self->vcf = __pyx_v_vcf;
 
-  /* "pysam/cvcf.pyx":105
+  /* "pysam/cvcf.pyx":106
  *     cdef uint32_t pos
  * 
  *     def __init__(self, vcf):             # <<<<<<<<<<<<<<
@@ -3630,7 +3670,7 @@ static int __pyx_pf_5pysam_4cvcf_9VCFRecord___init__(struct __pyx_obj_5pysam_4cv
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":116
+/* "pysam/cvcf.pyx":117
  *         #                     len(data)))
  * 
  *     def __cinit__(self, vcf):             # <<<<<<<<<<<<<<
@@ -3666,7 +3706,7 @@ static int __pyx_pw_5pysam_4cvcf_9VCFRecord_3__cinit__(PyObject *__pyx_v_self, P
         else goto __pyx_L5_argtuple_error;
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 116; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 117; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 1) {
       goto __pyx_L5_argtuple_error;
@@ -3677,7 +3717,7 @@ static int __pyx_pw_5pysam_4cvcf_9VCFRecord_3__cinit__(PyObject *__pyx_v_self, P
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 116; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 117; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCFRecord.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -3695,7 +3735,7 @@ static int __pyx_pf_5pysam_4cvcf_9VCFRecord_2__cinit__(struct __pyx_obj_5pysam_4
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__cinit__", 0);
 
-  /* "pysam/cvcf.pyx":118
+  /* "pysam/cvcf.pyx":119
  *     def __cinit__(self, vcf):
  *         # start indexed access at genotypes
  *         self.offset = 9             # <<<<<<<<<<<<<<
@@ -3704,7 +3744,7 @@ static int __pyx_pf_5pysam_4cvcf_9VCFRecord_2__cinit__(struct __pyx_obj_5pysam_4
  */
   __pyx_v_self->__pyx_base.offset = 9;
 
-  /* "pysam/cvcf.pyx":120
+  /* "pysam/cvcf.pyx":121
  *         self.offset = 9
  * 
  *         self.vcf = vcf             # <<<<<<<<<<<<<<
@@ -3717,7 +3757,7 @@ static int __pyx_pf_5pysam_4cvcf_9VCFRecord_2__cinit__(struct __pyx_obj_5pysam_4
   __Pyx_DECREF(__pyx_v_self->vcf);
   __pyx_v_self->vcf = __pyx_v_vcf;
 
-  /* "pysam/cvcf.pyx":116
+  /* "pysam/cvcf.pyx":117
  *         #                     len(data)))
  * 
  *     def __cinit__(self, vcf):             # <<<<<<<<<<<<<<
@@ -3731,7 +3771,7 @@ static int __pyx_pf_5pysam_4cvcf_9VCFRecord_2__cinit__(struct __pyx_obj_5pysam_4
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":122
+/* "pysam/cvcf.pyx":123
  *         self.vcf = vcf
  * 
  *     def error(self, line, error, opt=None):             # <<<<<<<<<<<<<<
@@ -3774,7 +3814,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_9VCFRecord_5error(PyObject *__pyx_v_self,
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_error)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("error", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("error", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (kw_args > 0) {
@@ -3783,7 +3823,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_9VCFRecord_5error(PyObject *__pyx_v_self,
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "error") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "error") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -3800,7 +3840,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_9VCFRecord_5error(PyObject *__pyx_v_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("error", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("error", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCFRecord.error", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -3824,7 +3864,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4error(struct __pyx_obj_5pysam
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("error", 0);
 
-  /* "pysam/cvcf.pyx":125
+  /* "pysam/cvcf.pyx":126
  *         '''raise error.'''
  *         # pass to vcf file for error handling
  *         return self.vcf.error( line, error, opt )             # <<<<<<<<<<<<<<
@@ -3832,9 +3872,9 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4error(struct __pyx_obj_5pysam
  *     cdef update(self, char * buffer, size_t nbytes):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_line);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_line);
@@ -3845,7 +3885,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4error(struct __pyx_obj_5pysam
   __Pyx_INCREF(__pyx_v_opt);
   PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_opt);
   __Pyx_GIVEREF(__pyx_v_opt);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -3853,7 +3893,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4error(struct __pyx_obj_5pysam
   __pyx_t_3 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":122
+  /* "pysam/cvcf.pyx":123
  *         self.vcf = vcf
  * 
  *     def error(self, line, error, opt=None):             # <<<<<<<<<<<<<<
@@ -3874,7 +3914,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4error(struct __pyx_obj_5pysam
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":127
+/* "pysam/cvcf.pyx":128
  *         return self.vcf.error( line, error, opt )
  * 
  *     cdef update(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
@@ -3891,18 +3931,18 @@ static PyObject *__pyx_f_5pysam_4cvcf_9VCFRecord_update(struct __pyx_obj_5pysam_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("update", 0);
 
-  /* "pysam/cvcf.pyx":132
+  /* "pysam/cvcf.pyx":133
  *         nbytes does not include the terminal '\0'.
  *         '''
  *         TabProxies.TupleProxy.update(self, buffer, nbytes)             # <<<<<<<<<<<<<<
  * 
  *         self.contig = self.fields[0]
  */
-  __pyx_t_1 = __pyx_vtabptr_5pysam_10TabProxies_TupleProxy->update(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_self), __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 132; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __pyx_vtabptr_5pysam_10TabProxies_TupleProxy->update(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_self), __pyx_v_buffer, __pyx_v_nbytes); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 133; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":134
+  /* "pysam/cvcf.pyx":135
  *         TabProxies.TupleProxy.update(self, buffer, nbytes)
  * 
  *         self.contig = self.fields[0]             # <<<<<<<<<<<<<<
@@ -3911,7 +3951,7 @@ static PyObject *__pyx_f_5pysam_4cvcf_9VCFRecord_update(struct __pyx_obj_5pysam_
  */
   __pyx_v_self->contig = (__pyx_v_self->__pyx_base.fields[0]);
 
-  /* "pysam/cvcf.pyx":136
+  /* "pysam/cvcf.pyx":137
  *         self.contig = self.fields[0]
  *         # vcf counts from 1 - correct here
  *         self.pos = atoi(self.fields[1]) - 1             # <<<<<<<<<<<<<<
@@ -3920,7 +3960,7 @@ static PyObject *__pyx_f_5pysam_4cvcf_9VCFRecord_update(struct __pyx_obj_5pysam_
  */
   __pyx_v_self->pos = (atoi((__pyx_v_self->__pyx_base.fields[1])) - 1);
 
-  /* "pysam/cvcf.pyx":127
+  /* "pysam/cvcf.pyx":128
  *         return self.vcf.error( line, error, opt )
  * 
  *     cdef update(self, char * buffer, size_t nbytes):             # <<<<<<<<<<<<<<
@@ -3941,7 +3981,7 @@ static PyObject *__pyx_f_5pysam_4cvcf_9VCFRecord_update(struct __pyx_obj_5pysam_
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":138
+/* "pysam/cvcf.pyx":139
  *         self.pos = atoi(self.fields[1]) - 1
  * 
  *     def __len__(self):             # <<<<<<<<<<<<<<
@@ -3970,7 +4010,7 @@ static Py_ssize_t __pyx_pf_5pysam_4cvcf_9VCFRecord_6__len__(struct __pyx_obj_5py
   long __pyx_t_3;
   __Pyx_RefNannySetupContext("__len__", 0);
 
-  /* "pysam/cvcf.pyx":139
+  /* "pysam/cvcf.pyx":140
  * 
  *     def __len__(self):
  *         return max(0, self.nfields - 9)             # <<<<<<<<<<<<<<
@@ -3987,7 +4027,7 @@ static Py_ssize_t __pyx_pf_5pysam_4cvcf_9VCFRecord_6__len__(struct __pyx_obj_5py
   __pyx_r = __pyx_t_3;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":138
+  /* "pysam/cvcf.pyx":139
  *         self.pos = atoi(self.fields[1]) - 1
  * 
  *     def __len__(self):             # <<<<<<<<<<<<<<
@@ -4001,7 +4041,7 @@ static Py_ssize_t __pyx_pf_5pysam_4cvcf_9VCFRecord_6__len__(struct __pyx_obj_5py
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":142
+/* "pysam/cvcf.pyx":143
  * 
  *     property contig:
  *         def __get__(self): return self.contig             # <<<<<<<<<<<<<<
@@ -4031,7 +4071,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6contig___get__(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_self->contig); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 142; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_self->contig); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 143; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -4048,7 +4088,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6contig___get__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":145
+/* "pysam/cvcf.pyx":146
  * 
  *     property pos:
  *         def __get__(self): return self.pos             # <<<<<<<<<<<<<<
@@ -4078,7 +4118,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3pos___get__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 145; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyInt_From_uint32_t(__pyx_v_self->pos); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 146; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -4095,7 +4135,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3pos___get__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":148
+/* "pysam/cvcf.pyx":149
  * 
  *     property id:
  *         def __get__(self): return self.fields[2]             # <<<<<<<<<<<<<<
@@ -4125,7 +4165,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_2id___get__(struct __pyx_obj_5
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[2])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 148; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[2])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
@@ -4142,7 +4182,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_2id___get__(struct __pyx_obj_5
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":151
+/* "pysam/cvcf.pyx":152
  * 
  *     property ref:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4172,7 +4212,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3ref___get__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/cvcf.pyx":152
+  /* "pysam/cvcf.pyx":153
  *     property ref:
  *         def __get__(self):
  *             return self.fields[3]             # <<<<<<<<<<<<<<
@@ -4180,13 +4220,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3ref___get__(struct __pyx_obj_
  *     property alt:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[3])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 152; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[3])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":151
+  /* "pysam/cvcf.pyx":152
  * 
  *     property ref:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4205,7 +4245,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3ref___get__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":155
+/* "pysam/cvcf.pyx":156
  * 
  *     property alt:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4238,28 +4278,28 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3alt___get__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/cvcf.pyx":157
+  /* "pysam/cvcf.pyx":158
  *         def __get__(self):
  *             # convert v3.3 to v4.0 alleles below
  *             alt = self.fields[4]             # <<<<<<<<<<<<<<
  *             if alt == ".": alt = []
  *             else: alt = alt.upper().split(',')
  */
-  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[4])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 157; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[4])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_alt = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":158
+  /* "pysam/cvcf.pyx":159
  *             # convert v3.3 to v4.0 alleles below
  *             alt = self.fields[4]
  *             if alt == ".": alt = []             # <<<<<<<<<<<<<<
  *             else: alt = alt.upper().split(',')
  *             return alt
  */
-  __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_v_alt, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = (__Pyx_PyString_Equals(__pyx_v_alt, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_2) {
-    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF_SET(__pyx_v_alt, __pyx_t_1);
     __pyx_t_1 = 0;
@@ -4267,22 +4307,22 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3alt___get__(struct __pyx_obj_
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":159
+    /* "pysam/cvcf.pyx":160
  *             alt = self.fields[4]
  *             if alt == ".": alt = []
  *             else: alt = alt.upper().split(',')             # <<<<<<<<<<<<<<
  *             return alt
  * 
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_alt, __pyx_n_s_upper); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_alt, __pyx_n_s_upper); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF_SET(__pyx_v_alt, __pyx_t_3);
@@ -4290,7 +4330,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3alt___get__(struct __pyx_obj_
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":160
+  /* "pysam/cvcf.pyx":161
  *             if alt == ".": alt = []
  *             else: alt = alt.upper().split(',')
  *             return alt             # <<<<<<<<<<<<<<
@@ -4302,7 +4342,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3alt___get__(struct __pyx_obj_
   __pyx_r = __pyx_v_alt;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":155
+  /* "pysam/cvcf.pyx":156
  * 
  *     property alt:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4323,7 +4363,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_3alt___get__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":163
+/* "pysam/cvcf.pyx":164
  * 
  *     property qual:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4365,26 +4405,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4qual___get__(struct __pyx_obj
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/cvcf.pyx":164
+  /* "pysam/cvcf.pyx":165
  *     property qual:
  *         def __get__(self):
  *             qual = self.fields[5]             # <<<<<<<<<<<<<<
  *             if qual == b".": qual = -1
  *             else:
  */
-  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[5])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[5])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_qual = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":165
+  /* "pysam/cvcf.pyx":166
  *         def __get__(self):
  *             qual = self.fields[5]
  *             if qual == b".": qual = -1             # <<<<<<<<<<<<<<
  *             else:
  *                 try:    qual = float(qual)
  */
-  __pyx_t_2 = (__Pyx_PyBytes_Equals(__pyx_v_qual, __pyx_kp_b__8, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = (__Pyx_PyBytes_Equals(__pyx_v_qual, __pyx_kp_b__8, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_2) {
     __Pyx_INCREF(__pyx_int_neg_1);
     __Pyx_DECREF_SET(__pyx_v_qual, __pyx_int_neg_1);
@@ -4392,7 +4432,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4qual___get__(struct __pyx_obj
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":167
+    /* "pysam/cvcf.pyx":168
  *             if qual == b".": qual = -1
  *             else:
  *                 try:    qual = float(qual)             # <<<<<<<<<<<<<<
@@ -4405,8 +4445,8 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4qual___get__(struct __pyx_obj
       __Pyx_XGOTREF(__pyx_t_4);
       __Pyx_XGOTREF(__pyx_t_5);
       /*try:*/ {
-        __pyx_t_6 = __Pyx_PyObject_AsDouble(__pyx_v_qual); if (unlikely(__pyx_t_6 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
-        __pyx_t_1 = PyFloat_FromDouble(__pyx_t_6); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_6 = __Pyx_PyObject_AsDouble(__pyx_v_qual); if (unlikely(__pyx_t_6 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_1 = PyFloat_FromDouble(__pyx_t_6); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF_SET(__pyx_v_qual, __pyx_t_1);
         __pyx_t_1 = 0;
@@ -4418,7 +4458,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4qual___get__(struct __pyx_obj
       __pyx_L4_error:;
       __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/cvcf.pyx":168
+      /* "pysam/cvcf.pyx":169
  *             else:
  *                 try:    qual = float(qual)
  *                 except: self.vcf.error(str(self),self.QUAL_NOT_NUMERICAL)             # <<<<<<<<<<<<<<
@@ -4427,23 +4467,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4qual___get__(struct __pyx_obj
  */
       /*except:*/ {
         __Pyx_AddTraceback("pysam.cvcf.VCFRecord.qual.__get__", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_1, &__pyx_t_7, &__pyx_t_8) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        if (__Pyx_GetException(&__pyx_t_1, &__pyx_t_7, &__pyx_t_8) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_INCREF(((PyObject *)__pyx_v_self));
         PyTuple_SET_ITEM(__pyx_t_10, 0, ((PyObject *)__pyx_v_self));
         __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-        __pyx_t_11 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_10, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __pyx_t_11 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_10, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-        __pyx_t_10 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_QUAL_NOT_NUMERICAL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __pyx_t_10 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_QUAL_NOT_NUMERICAL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_12 = PyTuple_New(2); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __pyx_t_12 = PyTuple_New(2); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_12);
         PyTuple_SET_ITEM(__pyx_t_12, 0, __pyx_t_11);
         __Pyx_GIVEREF(__pyx_t_11);
@@ -4451,7 +4491,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4qual___get__(struct __pyx_obj
         __Pyx_GIVEREF(__pyx_t_10);
         __pyx_t_11 = 0;
         __pyx_t_10 = 0;
-        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_12, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_12, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
@@ -4477,7 +4517,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4qual___get__(struct __pyx_obj
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":169
+  /* "pysam/cvcf.pyx":170
  *                 try:    qual = float(qual)
  *                 except: self.vcf.error(str(self),self.QUAL_NOT_NUMERICAL)
  *             return qual             # <<<<<<<<<<<<<<
@@ -4489,7 +4529,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4qual___get__(struct __pyx_obj
   __pyx_r = __pyx_v_qual;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":163
+  /* "pysam/cvcf.pyx":164
  * 
  *     property qual:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4515,7 +4555,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4qual___get__(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":172
+/* "pysam/cvcf.pyx":173
  * 
  *     property filter:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4551,7 +4591,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6filter___get__(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/cvcf.pyx":173
+  /* "pysam/cvcf.pyx":174
  *     property filter:
  *         def __get__(self):
  *             f = self.fields[6]             # <<<<<<<<<<<<<<
@@ -4560,26 +4600,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6filter___get__(struct __pyx_o
  */
   __pyx_v_f = (__pyx_v_self->__pyx_base.fields[6]);
 
-  /* "pysam/cvcf.pyx":175
+  /* "pysam/cvcf.pyx":176
  *             f = self.fields[6]
  *             # postpone checking that filters exist.  Encode missing filter or no filtering as empty list
  *             if f == b"." or f == b"PASS" or f == b"0": return []             # <<<<<<<<<<<<<<
  *             else: return f.split(';')
  * 
  */
-  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_f); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_f); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = (__Pyx_PyBytes_Equals(__pyx_t_1, __pyx_kp_b__8, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = (__Pyx_PyBytes_Equals(__pyx_t_1, __pyx_kp_b__8, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (!__pyx_t_2) {
-    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_f); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_f); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = (__Pyx_PyBytes_Equals(__pyx_t_1, __pyx_n_b_PASS, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = (__Pyx_PyBytes_Equals(__pyx_t_1, __pyx_n_b_PASS, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (!__pyx_t_3) {
-      __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_f); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_f); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_4 = (__Pyx_PyBytes_Equals(__pyx_t_1, __pyx_kp_b_0, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = (__Pyx_PyBytes_Equals(__pyx_t_1, __pyx_kp_b_0, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __pyx_t_5 = __pyx_t_4;
     } else {
@@ -4591,7 +4631,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6filter___get__(struct __pyx_o
   }
   if (__pyx_t_3) {
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_r = __pyx_t_1;
     __pyx_t_1 = 0;
@@ -4599,7 +4639,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6filter___get__(struct __pyx_o
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":176
+    /* "pysam/cvcf.pyx":177
  *             # postpone checking that filters exist.  Encode missing filter or no filtering as empty list
  *             if f == b"." or f == b"PASS" or f == b"0": return []
  *             else: return f.split(';')             # <<<<<<<<<<<<<<
@@ -4607,12 +4647,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6filter___get__(struct __pyx_o
  *     property info:
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_f); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_f); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__11, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_tuple__11, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __pyx_r = __pyx_t_1;
@@ -4620,7 +4660,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6filter___get__(struct __pyx_o
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":172
+  /* "pysam/cvcf.pyx":173
  * 
  *     property filter:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4640,7 +4680,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6filter___get__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":179
+/* "pysam/cvcf.pyx":180
  * 
  *     property info:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4684,7 +4724,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/cvcf.pyx":180
+  /* "pysam/cvcf.pyx":181
  *     property info:
  *         def __get__(self):
  *             col = self.fields[7]             # <<<<<<<<<<<<<<
@@ -4693,51 +4733,51 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
  */
   __pyx_v_col = (__pyx_v_self->__pyx_base.fields[7]);
 
-  /* "pysam/cvcf.pyx":182
+  /* "pysam/cvcf.pyx":183
  *             col = self.fields[7]
  *             # dictionary of keys, and list of values
  *             info = {}             # <<<<<<<<<<<<<<
  *             if col != b".":
  *                 for blurp in col.split(';'):
  */
-  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 182; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_info = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":183
+  /* "pysam/cvcf.pyx":184
  *             # dictionary of keys, and list of values
  *             info = {}
  *             if col != b".":             # <<<<<<<<<<<<<<
  *                 for blurp in col.split(';'):
  *                     elts = blurp.split('=')
  */
-  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_col); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_col); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = (__Pyx_PyBytes_Equals(__pyx_t_1, __pyx_kp_b__8, Py_NE)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 183; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = (__Pyx_PyBytes_Equals(__pyx_t_1, __pyx_kp_b__8, Py_NE)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_2) {
 
-    /* "pysam/cvcf.pyx":184
+    /* "pysam/cvcf.pyx":185
  *             info = {}
  *             if col != b".":
  *                 for blurp in col.split(';'):             # <<<<<<<<<<<<<<
  *                     elts = blurp.split('=')
  *                     if len(elts) == 1: v = None
  */
-    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_col); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_col); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__12, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__12, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     if (PyList_CheckExact(__pyx_t_1) || PyTuple_CheckExact(__pyx_t_1)) {
       __pyx_t_3 = __pyx_t_1; __Pyx_INCREF(__pyx_t_3); __pyx_t_4 = 0;
       __pyx_t_5 = NULL;
     } else {
-      __pyx_t_4 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __pyx_t_5 = Py_TYPE(__pyx_t_3)->tp_iternext;
     }
@@ -4746,16 +4786,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
       if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_3)) {
         if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_3)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_1); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_1); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_3)) {
         if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_3)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_1); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_1); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_1 = __pyx_t_5(__pyx_t_3);
@@ -4763,7 +4803,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -4772,29 +4812,29 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
       __Pyx_XDECREF_SET(__pyx_v_blurp, __pyx_t_1);
       __pyx_t_1 = 0;
 
-      /* "pysam/cvcf.pyx":185
+      /* "pysam/cvcf.pyx":186
  *             if col != b".":
  *                 for blurp in col.split(';'):
  *                     elts = blurp.split('=')             # <<<<<<<<<<<<<<
  *                     if len(elts) == 1: v = None
  *                     elif len(elts) == 2: v = elts[1]
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_blurp, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_blurp, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__14, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__14, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_XDECREF_SET(__pyx_v_elts, __pyx_t_6);
       __pyx_t_6 = 0;
 
-      /* "pysam/cvcf.pyx":186
+      /* "pysam/cvcf.pyx":187
  *                 for blurp in col.split(';'):
  *                     elts = blurp.split('=')
  *                     if len(elts) == 1: v = None             # <<<<<<<<<<<<<<
  *                     elif len(elts) == 2: v = elts[1]
  *                     else: self.vcf.error(str(self),self.ERROR_INFO_STRING)
  */
-      __pyx_t_7 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_2 = ((__pyx_t_7 == 1) != 0);
       if (__pyx_t_2) {
         __Pyx_INCREF(Py_None);
@@ -4802,17 +4842,17 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
         goto __pyx_L6;
       }
 
-      /* "pysam/cvcf.pyx":187
+      /* "pysam/cvcf.pyx":188
  *                     elts = blurp.split('=')
  *                     if len(elts) == 1: v = None
  *                     elif len(elts) == 2: v = elts[1]             # <<<<<<<<<<<<<<
  *                     else: self.vcf.error(str(self),self.ERROR_INFO_STRING)
  *                     info[elts[0]] = self.vcf.parse_formatdata(elts[0], v, self.vcf._info, str(self))
  */
-      __pyx_t_7 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_2 = ((__pyx_t_7 == 2) != 0);
       if (__pyx_t_2) {
-        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 187; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_6);
         __Pyx_XDECREF_SET(__pyx_v_v, __pyx_t_6);
         __pyx_t_6 = 0;
@@ -4820,26 +4860,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":188
+        /* "pysam/cvcf.pyx":189
  *                     if len(elts) == 1: v = None
  *                     elif len(elts) == 2: v = elts[1]
  *                     else: self.vcf.error(str(self),self.ERROR_INFO_STRING)             # <<<<<<<<<<<<<<
  *                     info[elts[0]] = self.vcf.parse_formatdata(elts[0], v, self.vcf._info, str(self))
  *             return info
  */
-        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_INCREF(((PyObject *)__pyx_v_self));
         PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_self));
         __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-        __pyx_t_8 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_ERROR_INFO_STRING); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_ERROR_INFO_STRING); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
         __Pyx_GIVEREF(__pyx_t_8);
@@ -4847,7 +4887,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
         __Pyx_GIVEREF(__pyx_t_1);
         __pyx_t_8 = 0;
         __pyx_t_1 = 0;
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -4855,29 +4895,29 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
       }
       __pyx_L6:;
 
-      /* "pysam/cvcf.pyx":189
+      /* "pysam/cvcf.pyx":190
  *                     elif len(elts) == 2: v = elts[1]
  *                     else: self.vcf.error(str(self),self.ERROR_INFO_STRING)
  *                     info[elts[0]] = self.vcf.parse_formatdata(elts[0], v, self.vcf._info, str(self))             # <<<<<<<<<<<<<<
  *             return info
  * 
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_parse_formatdata); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_parse_formatdata); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_9);
-      if (unlikely(!__pyx_v_v)) { __Pyx_RaiseUnboundLocalError("v"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_info); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(!__pyx_v_v)) { __Pyx_RaiseUnboundLocalError("v"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_info); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_INCREF(((PyObject *)__pyx_v_self));
       PyTuple_SET_ITEM(__pyx_t_8, 0, ((PyObject *)__pyx_v_self));
       __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
-      __pyx_t_10 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_8, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_8, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_8 = PyTuple_New(4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = PyTuple_New(4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_9);
       __Pyx_GIVEREF(__pyx_t_9);
@@ -4891,13 +4931,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
       __pyx_t_9 = 0;
       __pyx_t_6 = 0;
       __pyx_t_10 = 0;
-      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_8, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_8, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_8);
-      if (unlikely(PyDict_SetItem(__pyx_v_info, __pyx_t_8, __pyx_t_10) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyDict_SetItem(__pyx_v_info, __pyx_t_8, __pyx_t_10) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     }
@@ -4906,7 +4946,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":190
+  /* "pysam/cvcf.pyx":191
  *                     else: self.vcf.error(str(self),self.ERROR_INFO_STRING)
  *                     info[elts[0]] = self.vcf.parse_formatdata(elts[0], v, self.vcf._info, str(self))
  *             return info             # <<<<<<<<<<<<<<
@@ -4918,7 +4958,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
   __pyx_r = __pyx_v_info;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":179
+  /* "pysam/cvcf.pyx":180
  * 
  *     property info:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -4946,7 +4986,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_4info___get__(struct __pyx_obj
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":193
+/* "pysam/cvcf.pyx":194
  * 
  *     property format:
  *          def __get__(self):             # <<<<<<<<<<<<<<
@@ -4977,7 +5017,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6format___get__(struct __pyx_o
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/cvcf.pyx":194
+  /* "pysam/cvcf.pyx":195
  *     property format:
  *          def __get__(self):
  *              return self.fields[8].split(':')             # <<<<<<<<<<<<<<
@@ -4985,19 +5025,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6format___get__(struct __pyx_o
  *     property samples:
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[8])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[8])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__15, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__15, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":193
+  /* "pysam/cvcf.pyx":194
  * 
  *     property format:
  *          def __get__(self):             # <<<<<<<<<<<<<<
@@ -5017,7 +5057,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_6format___get__(struct __pyx_o
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":197
+/* "pysam/cvcf.pyx":198
  * 
  *     property samples:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -5047,7 +5087,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_7samples___get__(struct __pyx_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__get__", 0);
 
-  /* "pysam/cvcf.pyx":198
+  /* "pysam/cvcf.pyx":199
  *     property samples:
  *         def __get__(self):
  *             return self.vcf._samples             # <<<<<<<<<<<<<<
@@ -5055,13 +5095,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_7samples___get__(struct __pyx_
  *     def __getitem__(self, key):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_samples); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 198; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_samples); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 199; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":197
+  /* "pysam/cvcf.pyx":198
  * 
  *     property samples:
  *         def __get__(self):             # <<<<<<<<<<<<<<
@@ -5080,7 +5120,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_7samples___get__(struct __pyx_
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":200
+/* "pysam/cvcf.pyx":201
  *             return self.vcf._samples
  * 
  *     def __getitem__(self, key):             # <<<<<<<<<<<<<<
@@ -5128,103 +5168,103 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__getitem__", 0);
 
-  /* "pysam/cvcf.pyx":203
+  /* "pysam/cvcf.pyx":204
  * 
  *         # parse sample columns
  *         values = self.fields[self.vcf._sample2column[key]].split(':')             # <<<<<<<<<<<<<<
  *         alt = self.alt
  *         format = self.format
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_sample2column); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_sample2column); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyObject_GetItem(__pyx_t_1, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 203; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = PyObject_GetItem(__pyx_t_1, __pyx_v_key); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyIndex_AsSsize_t(__pyx_t_2); if (unlikely((__pyx_t_3 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyIndex_AsSsize_t(__pyx_t_2); if (unlikely((__pyx_t_3 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[__pyx_t_3])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyBytes_FromString((__pyx_v_self->__pyx_base.fields[__pyx_t_3])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_values = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":204
+  /* "pysam/cvcf.pyx":205
  *         # parse sample columns
  *         values = self.fields[self.vcf._sample2column[key]].split(':')
  *         alt = self.alt             # <<<<<<<<<<<<<<
  *         format = self.format
  * 
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_alt); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_alt); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_alt = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":205
+  /* "pysam/cvcf.pyx":206
  *         values = self.fields[self.vcf._sample2column[key]].split(':')
  *         alt = self.alt
  *         format = self.format             # <<<<<<<<<<<<<<
  * 
  *         if len(values) > len(format):
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_format); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 205; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_format); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_format = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":207
+  /* "pysam/cvcf.pyx":208
  *         format = self.format
  * 
  *         if len(values) > len(format):             # <<<<<<<<<<<<<<
  *             self.vcf.error(str(self.line),self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" %\
  *                            (len(values),key,len(format)))
  */
-  __pyx_t_3 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_4 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 207; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_5 = ((__pyx_t_3 > __pyx_t_4) != 0);
   if (__pyx_t_5) {
 
-    /* "pysam/cvcf.pyx":208
+    /* "pysam/cvcf.pyx":209
  * 
  *         if len(values) > len(format):
  *             self.vcf.error(str(self.line),self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" %\             # <<<<<<<<<<<<<<
  *                            (len(values),key,len(format)))
  * 
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_line); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_line); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_6, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_6, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_BAD_NUMBER_OF_VALUES); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_BAD_NUMBER_OF_VALUES); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
 
-    /* "pysam/cvcf.pyx":209
+    /* "pysam/cvcf.pyx":210
  *         if len(values) > len(format):
  *             self.vcf.error(str(self.line),self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" %\
  *                            (len(values),key,len(format)))             # <<<<<<<<<<<<<<
  * 
  *         result = {}
  */
-    __pyx_t_4 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_7 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_4 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_8 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_7);
     __Pyx_GIVEREF(__pyx_t_7);
@@ -5236,17 +5276,17 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
     __pyx_t_7 = 0;
     __pyx_t_8 = 0;
 
-    /* "pysam/cvcf.pyx":208
+    /* "pysam/cvcf.pyx":209
  * 
  *         if len(values) > len(format):
  *             self.vcf.error(str(self.line),self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" %\             # <<<<<<<<<<<<<<
  *                            (len(values),key,len(format)))
  * 
  */
-    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_found_s_values_in_element_s_exp, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_found_s_values_in_element_s_exp, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
@@ -5257,7 +5297,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
     __pyx_t_1 = 0;
     __pyx_t_6 = 0;
     __pyx_t_8 = 0;
-    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 208; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 209; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -5266,43 +5306,43 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":211
+  /* "pysam/cvcf.pyx":212
  *                            (len(values),key,len(format)))
  * 
  *         result = {}             # <<<<<<<<<<<<<<
  *         for idx in range(len(format)):
  *             expected = self.vcf.get_expected(format[idx], self.vcf._format, alt)
  */
-  __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 211; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
   __pyx_v_result = ((PyObject*)__pyx_t_8);
   __pyx_t_8 = 0;
 
-  /* "pysam/cvcf.pyx":212
+  /* "pysam/cvcf.pyx":213
  * 
  *         result = {}
  *         for idx in range(len(format)):             # <<<<<<<<<<<<<<
  *             expected = self.vcf.get_expected(format[idx], self.vcf._format, alt)
  *             if idx < len(values): value = values[idx]
  */
-  __pyx_t_4 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_4; __pyx_t_3+=1) {
     __pyx_v_idx = __pyx_t_3;
 
-    /* "pysam/cvcf.pyx":213
+    /* "pysam/cvcf.pyx":214
  *         result = {}
  *         for idx in range(len(format)):
  *             expected = self.vcf.get_expected(format[idx], self.vcf._format, alt)             # <<<<<<<<<<<<<<
  *             if idx < len(values): value = values[idx]
  *             else:
  */
-    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_get_expected); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_get_expected); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_format_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_format_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_6 = PyTuple_New(3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyTuple_New(3); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_9);
     __Pyx_GIVEREF(__pyx_t_9);
@@ -5313,24 +5353,24 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
     __Pyx_GIVEREF(__pyx_v_alt);
     __pyx_t_9 = 0;
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_XDECREF_SET(__pyx_v_expected, __pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":214
+    /* "pysam/cvcf.pyx":215
  *         for idx in range(len(format)):
  *             expected = self.vcf.get_expected(format[idx], self.vcf._format, alt)
  *             if idx < len(values): value = values[idx]             # <<<<<<<<<<<<<<
  *             else:
  *                 if expected == -1: value = "."
  */
-    __pyx_t_10 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_5 = ((__pyx_v_idx < __pyx_t_10) != 0);
     if (__pyx_t_5) {
-      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_values, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 214; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_values, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_2);
       __pyx_t_2 = 0;
@@ -5338,15 +5378,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
     }
     /*else*/ {
 
-      /* "pysam/cvcf.pyx":216
+      /* "pysam/cvcf.pyx":217
  *             if idx < len(values): value = values[idx]
  *             else:
  *                 if expected == -1: value = "."             # <<<<<<<<<<<<<<
  *                 else: value = ",".join(["."]*expected)
  * 
  */
-      __pyx_t_2 = PyObject_RichCompare(__pyx_v_expected, __pyx_int_neg_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 216; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyObject_RichCompare(__pyx_v_expected, __pyx_int_neg_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       if (__pyx_t_5) {
         __Pyx_INCREF(__pyx_kp_s__8);
@@ -5355,24 +5395,24 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":217
+        /* "pysam/cvcf.pyx":218
  *             else:
  *                 if expected == -1: value = "."
  *                 else: value = ",".join(["."]*expected)             # <<<<<<<<<<<<<<
  * 
  *             result[format[idx]] = self.vcf.parse_formatdata(format[idx], value, self.vcf._format, str(self.data))
  */
-        __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 218; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_INCREF(__pyx_kp_s__8);
         PyList_SET_ITEM(__pyx_t_2, 0, __pyx_kp_s__8);
         __Pyx_GIVEREF(__pyx_kp_s__8);
-        { PyObject* __pyx_temp = PyNumber_InPlaceMultiply(__pyx_t_2, __pyx_v_expected); if (unlikely(!__pyx_temp)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        { PyObject* __pyx_temp = PyNumber_InPlaceMultiply(__pyx_t_2, __pyx_v_expected); if (unlikely(!__pyx_temp)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 218; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_temp);
           __Pyx_DECREF(__pyx_t_2);
           __pyx_t_2 = __pyx_temp;
         }
-        __pyx_t_6 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 217; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 218; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_6);
@@ -5382,30 +5422,30 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
     }
     __pyx_L6:;
 
-    /* "pysam/cvcf.pyx":219
+    /* "pysam/cvcf.pyx":220
  *                 else: value = ",".join(["."]*expected)
  * 
  *             result[format[idx]] = self.vcf.parse_formatdata(format[idx], value, self.vcf._format, str(self.data))             # <<<<<<<<<<<<<<
  *             if expected != -1 and len(result[format[idx]]) != expected:
  *                 self.vcf.error(str(self.data),self.BAD_NUMBER_OF_PARAMETERS,
  */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_parse_formatdata); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_parse_formatdata); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_format_2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_format_2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_9 = __Pyx_PyBytes_FromString(__pyx_v_self->__pyx_base.data); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyBytes_FromString(__pyx_v_self->__pyx_base.data); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_9);
     __Pyx_GIVEREF(__pyx_t_9);
     __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
@@ -5419,39 +5459,39 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
     __pyx_t_2 = 0;
     __pyx_t_8 = 0;
     __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_1);
-    if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_t_1, __pyx_t_9) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_t_1, __pyx_t_9) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-    /* "pysam/cvcf.pyx":220
+    /* "pysam/cvcf.pyx":221
  * 
  *             result[format[idx]] = self.vcf.parse_formatdata(format[idx], value, self.vcf._format, str(self.data))
  *             if expected != -1 and len(result[format[idx]]) != expected:             # <<<<<<<<<<<<<<
  *                 self.vcf.error(str(self.data),self.BAD_NUMBER_OF_PARAMETERS,
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,result[format[idx]]))
  */
-    __pyx_t_9 = PyObject_RichCompare(__pyx_v_expected, __pyx_int_neg_1, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyObject_RichCompare(__pyx_v_expected, __pyx_int_neg_1, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     if (__pyx_t_5) {
-      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_9); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_9); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_10 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_10); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_10); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_9 = PyObject_RichCompare(__pyx_t_1, __pyx_v_expected, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyObject_RichCompare(__pyx_t_1, __pyx_v_expected, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_11 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 220; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       __pyx_t_12 = __pyx_t_11;
     } else {
@@ -5459,43 +5499,43 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
     }
     if (__pyx_t_12) {
 
-      /* "pysam/cvcf.pyx":221
+      /* "pysam/cvcf.pyx":222
  *             result[format[idx]] = self.vcf.parse_formatdata(format[idx], value, self.vcf._format, str(self.data))
  *             if expected != -1 and len(result[format[idx]]) != expected:
  *                 self.vcf.error(str(self.data),self.BAD_NUMBER_OF_PARAMETERS,             # <<<<<<<<<<<<<<
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,result[format[idx]]))
  *                 if len(result[format[idx]] ) < expected: result[format[idx]] += [result[format[idx]][-1]]*(expected-len(result[format[idx]]))
  */
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self->vcf, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_self->__pyx_base.data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_self->__pyx_base.data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_1);
       __Pyx_GIVEREF(__pyx_t_1);
       __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_6, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_6, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_BAD_NUMBER_OF_PARAMETERS); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_self), __pyx_n_s_BAD_NUMBER_OF_PARAMETERS); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
 
-      /* "pysam/cvcf.pyx":222
+      /* "pysam/cvcf.pyx":223
  *             if expected != -1 and len(result[format[idx]]) != expected:
  *                 self.vcf.error(str(self.data),self.BAD_NUMBER_OF_PARAMETERS,
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,result[format[idx]]))             # <<<<<<<<<<<<<<
  *                 if len(result[format[idx]] ) < expected: result[format[idx]] += [result[format[idx]][-1]]*(expected-len(result[format[idx]]))
  *                 result[format[idx]] = result[format[idx]][:expected]
  */
-      __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_2); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_7 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_2); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_8);
       __Pyx_GIVEREF(__pyx_t_8);
@@ -5506,18 +5546,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
       __Pyx_GIVEREF(__pyx_t_7);
       __pyx_t_8 = 0;
       __pyx_t_7 = 0;
-      __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_id_s_expected_s_parameters_got_s, __pyx_t_2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_id_s_expected_s_parameters_got_s, __pyx_t_2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-      /* "pysam/cvcf.pyx":221
+      /* "pysam/cvcf.pyx":222
  *             result[format[idx]] = self.vcf.parse_formatdata(format[idx], value, self.vcf._format, str(self.data))
  *             if expected != -1 and len(result[format[idx]]) != expected:
  *                 self.vcf.error(str(self.data),self.BAD_NUMBER_OF_PARAMETERS,             # <<<<<<<<<<<<<<
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,result[format[idx]]))
  *                 if len(result[format[idx]] ) < expected: result[format[idx]] += [result[format[idx]][-1]]*(expected-len(result[format[idx]]))
  */
-      __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
       __Pyx_GIVEREF(__pyx_t_1);
@@ -5528,97 +5568,97 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
       __pyx_t_1 = 0;
       __pyx_t_6 = 0;
       __pyx_t_7 = 0;
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 221; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 222; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
 
-      /* "pysam/cvcf.pyx":223
+      /* "pysam/cvcf.pyx":224
  *                 self.vcf.error(str(self.data),self.BAD_NUMBER_OF_PARAMETERS,
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,result[format[idx]]))
  *                 if len(result[format[idx]] ) < expected: result[format[idx]] += [result[format[idx]][-1]]*(expected-len(result[format[idx]]))             # <<<<<<<<<<<<<<
  *                 result[format[idx]] = result[format[idx]][:expected]
  * 
  */
-      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_7); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_7); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_10 = PyObject_Length(__pyx_t_2); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyObject_Length(__pyx_t_2); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = PyInt_FromSsize_t(__pyx_t_10); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyInt_FromSsize_t(__pyx_t_10); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_2, __pyx_v_expected, Py_LT); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_RichCompare(__pyx_t_2, __pyx_v_expected, Py_LT); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_12 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_12 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       if (__pyx_t_12) {
-        __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_7); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_7); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_6 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_9); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_6 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_9); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_6);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_GetItemInt(__pyx_t_6, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_9 = __Pyx_GetItemInt(__pyx_t_6, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_6); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_6); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_10 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_10); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_10); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_6 = PyNumber_Subtract(__pyx_v_expected, __pyx_t_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyNumber_Subtract(__pyx_v_expected, __pyx_t_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         PyList_SET_ITEM(__pyx_t_1, 0, __pyx_t_9);
         __Pyx_GIVEREF(__pyx_t_9);
-        { PyObject* __pyx_temp = PyNumber_InPlaceMultiply(__pyx_t_1, __pyx_t_6); if (unlikely(!__pyx_temp)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        { PyObject* __pyx_temp = PyNumber_InPlaceMultiply(__pyx_t_1, __pyx_t_6); if (unlikely(!__pyx_temp)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_temp);
           __Pyx_DECREF(__pyx_t_1);
           __pyx_t_1 = __pyx_temp;
         }
         __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-        __pyx_t_6 = PyNumber_InPlaceAdd(__pyx_t_2, __pyx_t_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyNumber_InPlaceAdd(__pyx_t_2, __pyx_t_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_6);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_t_7, __pyx_t_6) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 223; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_t_7, __pyx_t_6) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         goto __pyx_L9;
       }
       __pyx_L9:;
 
-      /* "pysam/cvcf.pyx":224
+      /* "pysam/cvcf.pyx":225
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,result[format[idx]]))
  *                 if len(result[format[idx]] ) < expected: result[format[idx]] += [result[format[idx]][-1]]*(expected-len(result[format[idx]]))
  *                 result[format[idx]] = result[format[idx]][:expected]             # <<<<<<<<<<<<<<
  * 
  *         return result
  */
-      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_6 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_7); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_6 = __Pyx_PyDict_GetItem(__pyx_v_result, __pyx_t_7); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = __Pyx_PyObject_GetSlice(__pyx_t_6, 0, 0, NULL, &__pyx_v_expected, NULL, 0, 0, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_GetSlice(__pyx_t_6, 0, 0, NULL, &__pyx_v_expected, NULL, 0, 0, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_6);
-      if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_t_6, __pyx_t_7) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 224; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyDict_SetItem(__pyx_v_result, __pyx_t_6, __pyx_t_7) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 225; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       goto __pyx_L8;
@@ -5626,7 +5666,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
     __pyx_L8:;
   }
 
-  /* "pysam/cvcf.pyx":226
+  /* "pysam/cvcf.pyx":227
  *                 result[format[idx]] = result[format[idx]][:expected]
  * 
  *         return result             # <<<<<<<<<<<<<<
@@ -5638,7 +5678,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
   __pyx_r = __pyx_v_result;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":200
+  /* "pysam/cvcf.pyx":201
  *             return self.vcf._samples
  * 
  *     def __getitem__(self, key):             # <<<<<<<<<<<<<<
@@ -5668,7 +5708,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_9VCFRecord_8__getitem__(struct __pyx_obj_
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":232
+/* "pysam/cvcf.pyx":233
  *     '''converts a :term:`tabix row` into a VCF record.'''
  *     cdef vcffile
  *     def __init__(self, vcffile):             # <<<<<<<<<<<<<<
@@ -5704,7 +5744,7 @@ static int __pyx_pw_5pysam_4cvcf_11asVCFRecord_1__init__(PyObject *__pyx_v_self,
         else goto __pyx_L5_argtuple_error;
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 232; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 1) {
       goto __pyx_L5_argtuple_error;
@@ -5715,7 +5755,7 @@ static int __pyx_pw_5pysam_4cvcf_11asVCFRecord_1__init__(PyObject *__pyx_v_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 232; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 233; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.asVCFRecord.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -5733,7 +5773,7 @@ static int __pyx_pf_5pysam_4cvcf_11asVCFRecord___init__(struct __pyx_obj_5pysam_
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__init__", 0);
 
-  /* "pysam/cvcf.pyx":233
+  /* "pysam/cvcf.pyx":234
  *     cdef vcffile
  *     def __init__(self, vcffile):
  *         self.vcffile = vcffile             # <<<<<<<<<<<<<<
@@ -5746,7 +5786,7 @@ static int __pyx_pf_5pysam_4cvcf_11asVCFRecord___init__(struct __pyx_obj_5pysam_
   __Pyx_DECREF(__pyx_v_self->vcffile);
   __pyx_v_self->vcffile = __pyx_v_vcffile;
 
-  /* "pysam/cvcf.pyx":232
+  /* "pysam/cvcf.pyx":233
  *     '''converts a :term:`tabix row` into a VCF record.'''
  *     cdef vcffile
  *     def __init__(self, vcffile):             # <<<<<<<<<<<<<<
@@ -5760,7 +5800,7 @@ static int __pyx_pf_5pysam_4cvcf_11asVCFRecord___init__(struct __pyx_obj_5pysam_
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":235
+/* "pysam/cvcf.pyx":236
  *         self.vcffile = vcffile
  * 
  *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
@@ -5779,36 +5819,36 @@ static PyObject *__pyx_f_5pysam_4cvcf_11asVCFRecord_parse(struct __pyx_obj_5pysa
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse", 0);
 
-  /* "pysam/cvcf.pyx":237
+  /* "pysam/cvcf.pyx":238
  *     cdef parse(self, char * buffer, int len):
  *         cdef VCFRecord r
  *         r = VCFRecord(self.vcffile)             # <<<<<<<<<<<<<<
  *         r.copy(buffer, len)
  *         return r
  */
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_v_self->vcffile);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_self->vcffile);
   __Pyx_GIVEREF(__pyx_v_self->vcffile);
-  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_4cvcf_VCFRecord)), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_4cvcf_VCFRecord)), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_r = ((struct __pyx_obj_5pysam_4cvcf_VCFRecord *)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":238
+  /* "pysam/cvcf.pyx":239
  *         cdef VCFRecord r
  *         r = VCFRecord(self.vcffile)
  *         r.copy(buffer, len)             # <<<<<<<<<<<<<<
  *         return r
  * 
  */
-  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_4cvcf_VCFRecord *)__pyx_v_r->__pyx_base.__pyx_vtab)->__pyx_base.copy(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_r), __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = ((struct __pyx_vtabstruct_5pysam_4cvcf_VCFRecord *)__pyx_v_r->__pyx_base.__pyx_vtab)->__pyx_base.copy(((struct __pyx_obj_5pysam_10TabProxies_TupleProxy *)__pyx_v_r), __pyx_v_buffer, __pyx_v_len); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 239; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":239
+  /* "pysam/cvcf.pyx":240
  *         r = VCFRecord(self.vcffile)
  *         r.copy(buffer, len)
  *         return r             # <<<<<<<<<<<<<<
@@ -5820,7 +5860,7 @@ static PyObject *__pyx_f_5pysam_4cvcf_11asVCFRecord_parse(struct __pyx_obj_5pysa
   __pyx_r = ((PyObject *)__pyx_v_r);
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":235
+  /* "pysam/cvcf.pyx":236
  *         self.vcffile = vcffile
  * 
  *     cdef parse(self, char * buffer, int len):             # <<<<<<<<<<<<<<
@@ -5841,12 +5881,12 @@ static PyObject *__pyx_f_5pysam_4cvcf_11asVCFRecord_parse(struct __pyx_obj_5pysa
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":318
+/* "pysam/cvcf.pyx":319
  *     _lines = None
  * 
- *     def __init__(self, _copy=None, reference=None, regions=None, lines=None, leftalign=False):             # <<<<<<<<<<<<<<
+ *     def __init__(self, _copy=None, reference=None, regions=None,             # <<<<<<<<<<<<<<
+ *                  lines=None, leftalign=False):
  *         # make error identifiers accessible by name
- *         for id in self._errors.keys(): self.__dict__[self._errors[id].split(':')[0]] = id
  */
 
 /* Python wrapper */
@@ -5871,6 +5911,14 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_1__init__(PyObject *__pyx_self, PyOb
     values[1] = ((PyObject *)((PyObject *)Py_None));
     values[2] = ((PyObject *)((PyObject *)Py_None));
     values[3] = ((PyObject *)((PyObject *)Py_None));
+
+    /* "pysam/cvcf.pyx":320
+ * 
+ *     def __init__(self, _copy=None, reference=None, regions=None,
+ *                  lines=None, leftalign=False):             # <<<<<<<<<<<<<<
+ *         # make error identifiers accessible by name
+ *         for id in self._errors.keys():
+ */
     values[4] = ((PyObject *)((PyObject *)Py_None));
     values[5] = ((PyObject *)((PyObject *)Py_False));
     if (unlikely(__pyx_kwds)) {
@@ -5918,7 +5966,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_1__init__(PyObject *__pyx_self, PyOb
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -5941,7 +5989,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_1__init__(PyObject *__pyx_self, PyOb
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 6, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 1, 6, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -5949,6 +5997,14 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_1__init__(PyObject *__pyx_self, PyOb
   __pyx_L4_argument_unpacking_done:;
   __pyx_r = __pyx_pf_5pysam_4cvcf_3VCF___init__(__pyx_self, __pyx_v_self, __pyx_v__copy, __pyx_v_reference, __pyx_v_regions, __pyx_v_lines, __pyx_v_leftalign);
 
+  /* "pysam/cvcf.pyx":319
+ *     _lines = None
+ * 
+ *     def __init__(self, _copy=None, reference=None, regions=None,             # <<<<<<<<<<<<<<
+ *                  lines=None, leftalign=False):
+ *         # make error identifiers accessible by name
+ */
+
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
@@ -5970,26 +6026,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF___init__(CYTHON_UNUSED PyObject *__p
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("__init__", 0);
 
-  /* "pysam/cvcf.pyx":320
- *     def __init__(self, _copy=None, reference=None, regions=None, lines=None, leftalign=False):
+  /* "pysam/cvcf.pyx":322
+ *                  lines=None, leftalign=False):
  *         # make error identifiers accessible by name
- *         for id in self._errors.keys(): self.__dict__[self._errors[id].split(':')[0]] = id             # <<<<<<<<<<<<<<
+ *         for id in self._errors.keys():             # <<<<<<<<<<<<<<
+ *             self.__dict__[self._errors[id].split(':')[0]] = id
  *         if _copy != None:
- *             self._leftalign = _copy._leftalign
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_keys); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_keys); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (PyList_CheckExact(__pyx_t_1) || PyTuple_CheckExact(__pyx_t_1)) {
     __pyx_t_2 = __pyx_t_1; __Pyx_INCREF(__pyx_t_2); __pyx_t_3 = 0;
     __pyx_t_4 = NULL;
   } else {
-    __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_t_4 = Py_TYPE(__pyx_t_2)->tp_iternext;
   }
@@ -5998,16 +6054,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF___init__(CYTHON_UNUSED PyObject *__p
     if (!__pyx_t_4 && PyList_CheckExact(__pyx_t_2)) {
       if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_4 && PyTuple_CheckExact(__pyx_t_2)) {
       if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_1 = __pyx_t_4(__pyx_t_2);
@@ -6015,7 +6071,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF___init__(CYTHON_UNUSED PyObject *__p
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -6023,334 +6079,351 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF___init__(CYTHON_UNUSED PyObject *__p
     }
     __Pyx_XDECREF_SET(__pyx_v_id, __pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_dict); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/cvcf.pyx":323
+ *         # make error identifiers accessible by name
+ *         for id in self._errors.keys():
+ *             self.__dict__[self._errors[id].split(':')[0]] = id             # <<<<<<<<<<<<<<
+ *         if _copy != None:
+ *             self._leftalign = _copy._leftalign
+ */
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_dict); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_errors); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_errors); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_6 = PyObject_GetItem(__pyx_t_5, __pyx_v_id); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_6 = PyObject_GetItem(__pyx_t_5, __pyx_v_id); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_split); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_split); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_GetItemInt(__pyx_t_6, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_5 = __Pyx_GetItemInt(__pyx_t_6, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    if (unlikely(PyObject_SetItem(__pyx_t_1, __pyx_t_5, __pyx_v_id) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyObject_SetItem(__pyx_t_1, __pyx_t_5, __pyx_v_id) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   }
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":321
- *         # make error identifiers accessible by name
- *         for id in self._errors.keys(): self.__dict__[self._errors[id].split(':')[0]] = id
+  /* "pysam/cvcf.pyx":324
+ *         for id in self._errors.keys():
+ *             self.__dict__[self._errors[id].split(':')[0]] = id
  *         if _copy != None:             # <<<<<<<<<<<<<<
  *             self._leftalign = _copy._leftalign
  *             self._header = _copy._header[:]
  */
-  __pyx_t_2 = PyObject_RichCompare(__pyx_v__copy, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyObject_RichCompare(__pyx_v__copy, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_7) {
 
-    /* "pysam/cvcf.pyx":322
- *         for id in self._errors.keys(): self.__dict__[self._errors[id].split(':')[0]] = id
+    /* "pysam/cvcf.pyx":325
+ *             self.__dict__[self._errors[id].split(':')[0]] = id
  *         if _copy != None:
  *             self._leftalign = _copy._leftalign             # <<<<<<<<<<<<<<
  *             self._header = _copy._header[:]
  *             self._version = _copy._version
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_leftalign_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_leftalign_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_leftalign_2, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 322; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_leftalign_2, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":323
+    /* "pysam/cvcf.pyx":326
  *         if _copy != None:
  *             self._leftalign = _copy._leftalign
  *             self._header = _copy._header[:]             # <<<<<<<<<<<<<<
  *             self._version = _copy._version
  *             self._info = copy.deepcopy(_copy._info)
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_header); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_header); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = __Pyx_PyObject_GetSlice(__pyx_t_2, 0, 0, NULL, NULL, &__pyx_slice__18, 0, 0, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetSlice(__pyx_t_2, 0, 0, NULL, NULL, &__pyx_slice__18, 0, 0, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_header, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_header, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":324
+    /* "pysam/cvcf.pyx":327
  *             self._leftalign = _copy._leftalign
  *             self._header = _copy._header[:]
  *             self._version = _copy._version             # <<<<<<<<<<<<<<
  *             self._info = copy.deepcopy(_copy._info)
  *             self._filter = copy.deepcopy(_copy._filter)
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_version); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_version); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":325
+    /* "pysam/cvcf.pyx":328
  *             self._header = _copy._header[:]
  *             self._version = _copy._version
  *             self._info = copy.deepcopy(_copy._info)             # <<<<<<<<<<<<<<
  *             self._filter = copy.deepcopy(_copy._filter)
  *             self._format = copy.deepcopy(_copy._format)
  */
-    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_info); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_info); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_5);
     __Pyx_GIVEREF(__pyx_t_5);
     __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_info, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_info, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":326
+    /* "pysam/cvcf.pyx":329
  *             self._version = _copy._version
  *             self._info = copy.deepcopy(_copy._info)
  *             self._filter = copy.deepcopy(_copy._filter)             # <<<<<<<<<<<<<<
  *             self._format = copy.deepcopy(_copy._format)
  *             self._samples = _copy._samples[:]
  */
-    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_filter); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_filter); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_5);
     __Pyx_GIVEREF(__pyx_t_5);
     __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_filter, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_filter, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":327
+    /* "pysam/cvcf.pyx":330
  *             self._info = copy.deepcopy(_copy._info)
  *             self._filter = copy.deepcopy(_copy._filter)
  *             self._format = copy.deepcopy(_copy._format)             # <<<<<<<<<<<<<<
  *             self._samples = _copy._samples[:]
  *             self._sample2column = copy.deepcopy(_copy._sample2column)
  */
-    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_format_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_format_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_5);
     __Pyx_GIVEREF(__pyx_t_5);
     __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_format_2, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_format_2, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":328
+    /* "pysam/cvcf.pyx":331
  *             self._filter = copy.deepcopy(_copy._filter)
  *             self._format = copy.deepcopy(_copy._format)
  *             self._samples = _copy._samples[:]             # <<<<<<<<<<<<<<
  *             self._sample2column = copy.deepcopy(_copy._sample2column)
  *             self._ignored_errors = copy.deepcopy(_copy._ignored_errors)
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_samples); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_samples); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_t_5, 0, 0, NULL, NULL, &__pyx_slice__19, 0, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_t_5, 0, 0, NULL, NULL, &__pyx_slice__19, 0, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_samples, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_samples, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":329
+    /* "pysam/cvcf.pyx":332
  *             self._format = copy.deepcopy(_copy._format)
  *             self._samples = _copy._samples[:]
  *             self._sample2column = copy.deepcopy(_copy._sample2column)             # <<<<<<<<<<<<<<
  *             self._ignored_errors = copy.deepcopy(_copy._ignored_errors)
  *             self._warn_errors = copy.deepcopy(_copy._warn_errors)
  */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_sample2column); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_sample2column); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_sample2column, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_sample2column, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":330
+    /* "pysam/cvcf.pyx":333
  *             self._samples = _copy._samples[:]
  *             self._sample2column = copy.deepcopy(_copy._sample2column)
  *             self._ignored_errors = copy.deepcopy(_copy._ignored_errors)             # <<<<<<<<<<<<<<
  *             self._warn_errors = copy.deepcopy(_copy._warn_errors)
  *             self._reference = _copy._reference
  */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 333; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 333; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_ignored_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_ignored_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 333; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 333; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 333; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_ignored_errors, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 330; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_ignored_errors, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 333; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":331
+    /* "pysam/cvcf.pyx":334
  *             self._sample2column = copy.deepcopy(_copy._sample2column)
  *             self._ignored_errors = copy.deepcopy(_copy._ignored_errors)
  *             self._warn_errors = copy.deepcopy(_copy._warn_errors)             # <<<<<<<<<<<<<<
  *             self._reference = _copy._reference
  *             self._regions = _copy._regions
  */
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_copy_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_deepcopy); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_warn_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_warn_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_warn_errors, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_warn_errors, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":332
+    /* "pysam/cvcf.pyx":335
  *             self._ignored_errors = copy.deepcopy(_copy._ignored_errors)
  *             self._warn_errors = copy.deepcopy(_copy._warn_errors)
  *             self._reference = _copy._reference             # <<<<<<<<<<<<<<
  *             self._regions = _copy._regions
  *         if reference: self._reference = reference
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 335; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_reference_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 332; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_reference_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 335; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":333
+    /* "pysam/cvcf.pyx":336
  *             self._warn_errors = copy.deepcopy(_copy._warn_errors)
  *             self._reference = _copy._reference
  *             self._regions = _copy._regions             # <<<<<<<<<<<<<<
  *         if reference: self._reference = reference
  *         if regions: self._regions = regions
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_regions_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 333; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v__copy, __pyx_n_s_regions_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_regions_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 333; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_regions_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     goto __pyx_L5;
   }
   __pyx_L5:;
 
-  /* "pysam/cvcf.pyx":334
+  /* "pysam/cvcf.pyx":337
  *             self._reference = _copy._reference
  *             self._regions = _copy._regions
  *         if reference: self._reference = reference             # <<<<<<<<<<<<<<
  *         if regions: self._regions = regions
  *         if leftalign: self._leftalign = leftalign
  */
-  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_v_reference); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_v_reference); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 337; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_7) {
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_reference_2, __pyx_v_reference) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 334; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_reference_2, __pyx_v_reference) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 337; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     goto __pyx_L6;
   }
   __pyx_L6:;
 
-  /* "pysam/cvcf.pyx":335
+  /* "pysam/cvcf.pyx":338
  *             self._regions = _copy._regions
  *         if reference: self._reference = reference
  *         if regions: self._regions = regions             # <<<<<<<<<<<<<<
  *         if leftalign: self._leftalign = leftalign
  *         self._lines = lines
  */
-  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_v_regions); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 335; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_v_regions); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_7) {
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_regions_2, __pyx_v_regions) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 335; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_regions_2, __pyx_v_regions) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 338; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     goto __pyx_L7;
   }
   __pyx_L7:;
 
-  /* "pysam/cvcf.pyx":336
+  /* "pysam/cvcf.pyx":339
  *         if reference: self._reference = reference
  *         if regions: self._regions = regions
  *         if leftalign: self._leftalign = leftalign             # <<<<<<<<<<<<<<
  *         self._lines = lines
- * 
+ *         self.encoding = "ascii"
  */
-  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_v_leftalign); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_v_leftalign); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_7) {
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_leftalign_2, __pyx_v_leftalign) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 336; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_leftalign_2, __pyx_v_leftalign) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     goto __pyx_L8;
   }
   __pyx_L8:;
 
-  /* "pysam/cvcf.pyx":337
+  /* "pysam/cvcf.pyx":340
  *         if regions: self._regions = regions
  *         if leftalign: self._leftalign = leftalign
  *         self._lines = lines             # <<<<<<<<<<<<<<
+ *         self.encoding = "ascii"
+ * 
+ */
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_lines_2, __pyx_v_lines) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 340; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/cvcf.pyx":341
+ *         if leftalign: self._leftalign = leftalign
+ *         self._lines = lines
+ *         self.encoding = "ascii"             # <<<<<<<<<<<<<<
  * 
  *     def error(self,line,error,opt=None):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_lines_2, __pyx_v_lines) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 337; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_encoding, __pyx_n_s_ascii) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":318
+  /* "pysam/cvcf.pyx":319
  *     _lines = None
  * 
- *     def __init__(self, _copy=None, reference=None, regions=None, lines=None, leftalign=False):             # <<<<<<<<<<<<<<
+ *     def __init__(self, _copy=None, reference=None, regions=None,             # <<<<<<<<<<<<<<
+ *                  lines=None, leftalign=False):
  *         # make error identifiers accessible by name
- *         for id in self._errors.keys(): self.__dict__[self._errors[id].split(':')[0]] = id
  */
 
   /* function exit code */
@@ -6370,8 +6443,8 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF___init__(CYTHON_UNUSED PyObject *__p
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":339
- *         self._lines = lines
+/* "pysam/cvcf.pyx":343
+ *         self.encoding = "ascii"
  * 
  *     def error(self,line,error,opt=None):             # <<<<<<<<<<<<<<
  *         if error in self._ignored_errors: return
@@ -6415,12 +6488,12 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_3error(PyObject *__pyx_self, PyObjec
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_line)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("error", 0, 3, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("error", 0, 3, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_error)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("error", 0, 3, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("error", 0, 3, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  3:
         if (kw_args > 0) {
@@ -6429,7 +6502,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_3error(PyObject *__pyx_self, PyObjec
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "error") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "error") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -6448,7 +6521,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_3error(PyObject *__pyx_self, PyObjec
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("error", 0, 3, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("error", 0, 3, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.error", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -6479,16 +6552,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("error", 0);
 
-  /* "pysam/cvcf.pyx":340
+  /* "pysam/cvcf.pyx":344
  * 
  *     def error(self,line,error,opt=None):
  *         if error in self._ignored_errors: return             # <<<<<<<<<<<<<<
  *         errorlabel, errorstring = self._errors[error].split(':')
  *         if opt: errorstring = errorstring % opt
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ignored_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 340; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ignored_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = (__Pyx_PySequence_Contains(__pyx_v_error, __pyx_t_1, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 340; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = (__Pyx_PySequence_Contains(__pyx_v_error, __pyx_t_1, Py_EQ)); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_t_3 = (__pyx_t_2 != 0);
   if (__pyx_t_3) {
@@ -6497,22 +6570,22 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":341
+  /* "pysam/cvcf.pyx":345
  *     def error(self,line,error,opt=None):
  *         if error in self._ignored_errors: return
  *         errorlabel, errorstring = self._errors[error].split(':')             # <<<<<<<<<<<<<<
  *         if opt: errorstring = errorstring % opt
  *         errwarn = ["Error","Warning"][error in self._warn_errors]
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_errors); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_4 = PyObject_GetItem(__pyx_t_1, __pyx_v_error); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_4 = PyObject_GetItem(__pyx_t_1, __pyx_v_error); if (unlikely(__pyx_t_4 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__20, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__20, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if ((likely(PyTuple_CheckExact(__pyx_t_4))) || (PyList_CheckExact(__pyx_t_4))) {
@@ -6525,7 +6598,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
     if (unlikely(size != 2)) {
       if (size > 2) __Pyx_RaiseTooManyValuesError(2);
       else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     #if CYTHON_COMPILING_IN_CPYTHON
     if (likely(PyTuple_CheckExact(sequence))) {
@@ -6538,15 +6611,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
     __Pyx_INCREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_t_5);
     #else
-    __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     #endif
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   } else {
     Py_ssize_t index = -1;
-    __pyx_t_6 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __pyx_t_7 = Py_TYPE(__pyx_t_6)->tp_iternext;
@@ -6554,7 +6627,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
     __Pyx_GOTREF(__pyx_t_1);
     index = 1; __pyx_t_5 = __pyx_t_7(__pyx_t_6); if (unlikely(!__pyx_t_5)) goto __pyx_L4_unpacking_failed;
     __Pyx_GOTREF(__pyx_t_5);
-    if (__Pyx_IternextUnpackEndCheck(__pyx_t_7(__pyx_t_6), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_IternextUnpackEndCheck(__pyx_t_7(__pyx_t_6), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_7 = NULL;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     goto __pyx_L5_unpacking_done;
@@ -6562,7 +6635,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __pyx_t_7 = NULL;
     if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_L5_unpacking_done:;
   }
   __pyx_v_errorlabel = __pyx_t_1;
@@ -6570,16 +6643,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
   __pyx_v_errorstring = __pyx_t_5;
   __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":342
+  /* "pysam/cvcf.pyx":346
  *         if error in self._ignored_errors: return
  *         errorlabel, errorstring = self._errors[error].split(':')
  *         if opt: errorstring = errorstring % opt             # <<<<<<<<<<<<<<
  *         errwarn = ["Error","Warning"][error in self._warn_errors]
  *         errorstring += " in line %s: '%s'\n%s %s: %s\n" % (self._lineno,line,errwarn,errorlabel,errorstring)
  */
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_opt); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 342; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_opt); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_3) {
-    __pyx_t_4 = PyNumber_Remainder(__pyx_v_errorstring, __pyx_v_opt); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 342; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyNumber_Remainder(__pyx_v_errorstring, __pyx_v_opt); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF_SET(__pyx_v_errorstring, __pyx_t_4);
     __pyx_t_4 = 0;
@@ -6587,14 +6660,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
   }
   __pyx_L6:;
 
-  /* "pysam/cvcf.pyx":343
+  /* "pysam/cvcf.pyx":347
  *         errorlabel, errorstring = self._errors[error].split(':')
  *         if opt: errorstring = errorstring % opt
  *         errwarn = ["Error","Warning"][error in self._warn_errors]             # <<<<<<<<<<<<<<
  *         errorstring += " in line %s: '%s'\n%s %s: %s\n" % (self._lineno,line,errwarn,errorlabel,errorstring)
  *         if error in self._warn_errors: return
  */
-  __pyx_t_4 = PyList_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyList_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 347; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_INCREF(__pyx_n_s_Error);
   PyList_SET_ITEM(__pyx_t_4, 0, __pyx_n_s_Error);
@@ -6602,26 +6675,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
   __Pyx_INCREF(__pyx_n_s_Warning);
   PyList_SET_ITEM(__pyx_t_4, 1, __pyx_n_s_Warning);
   __Pyx_GIVEREF(__pyx_n_s_Warning);
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_warn_errors); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_warn_errors); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 347; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_error, __pyx_t_5, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_error, __pyx_t_5, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 347; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  __pyx_t_5 = __Pyx_GetItemInt_List(__pyx_t_4, __pyx_t_3, int, 1, __Pyx_PyBool_FromLong, 1, 1, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_5 = __Pyx_GetItemInt_List(__pyx_t_4, __pyx_t_3, int, 1, __Pyx_PyBool_FromLong, 1, 1, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 347; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __pyx_v_errwarn = __pyx_t_5;
   __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":344
+  /* "pysam/cvcf.pyx":348
  *         if opt: errorstring = errorstring % opt
  *         errwarn = ["Error","Warning"][error in self._warn_errors]
  *         errorstring += " in line %s: '%s'\n%s %s: %s\n" % (self._lineno,line,errwarn,errorlabel,errorstring)             # <<<<<<<<<<<<<<
  *         if error in self._warn_errors: return
  *         raise ValueError(errorstring)
  */
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_lineno); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_lineno); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_4 = PyTuple_New(5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyTuple_New(5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_5);
   __Pyx_GIVEREF(__pyx_t_5);
@@ -6638,25 +6711,25 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
   PyTuple_SET_ITEM(__pyx_t_4, 4, __pyx_v_errorstring);
   __Pyx_GIVEREF(__pyx_v_errorstring);
   __pyx_t_5 = 0;
-  __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_in_line_s_s_s_s_s, __pyx_t_4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_in_line_s_s_s_s_s, __pyx_t_4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = PyNumber_InPlaceAdd(__pyx_v_errorstring, __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyNumber_InPlaceAdd(__pyx_v_errorstring, __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   __Pyx_DECREF_SET(__pyx_v_errorstring, __pyx_t_4);
   __pyx_t_4 = 0;
 
-  /* "pysam/cvcf.pyx":345
+  /* "pysam/cvcf.pyx":349
  *         errwarn = ["Error","Warning"][error in self._warn_errors]
  *         errorstring += " in line %s: '%s'\n%s %s: %s\n" % (self._lineno,line,errwarn,errorlabel,errorstring)
  *         if error in self._warn_errors: return             # <<<<<<<<<<<<<<
  *         raise ValueError(errorstring)
  * 
  */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_warn_errors); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_warn_errors); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_error, __pyx_t_4, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_v_error, __pyx_t_4, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __pyx_t_2 = (__pyx_t_3 != 0);
   if (__pyx_t_2) {
@@ -6665,27 +6738,27 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":346
+  /* "pysam/cvcf.pyx":350
  *         errorstring += " in line %s: '%s'\n%s %s: %s\n" % (self._lineno,line,errwarn,errorlabel,errorstring)
  *         if error in self._warn_errors: return
  *         raise ValueError(errorstring)             # <<<<<<<<<<<<<<
  * 
  *     def parse_format(self,line,format,filter=False):
  */
-  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_INCREF(__pyx_v_errorstring);
   PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_errorstring);
   __Pyx_GIVEREF(__pyx_v_errorstring);
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_Raise(__pyx_t_5, 0, 0, 0);
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":339
- *         self._lines = lines
+  /* "pysam/cvcf.pyx":343
+ *         self.encoding = "ascii"
  * 
  *     def error(self,line,error,opt=None):             # <<<<<<<<<<<<<<
  *         if error in self._ignored_errors: return
@@ -6709,7 +6782,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_2error(CYTHON_UNUSED PyObject *__pyx
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":348
+/* "pysam/cvcf.pyx":352
  *         raise ValueError(errorstring)
  * 
  *     def parse_format(self,line,format,filter=False):             # <<<<<<<<<<<<<<
@@ -6754,12 +6827,12 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_5parse_format(PyObject *__pyx_self,
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_line)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse_format", 0, 3, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse_format", 0, 3, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_format)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse_format", 0, 3, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse_format", 0, 3, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  3:
         if (kw_args > 0) {
@@ -6768,7 +6841,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_5parse_format(PyObject *__pyx_self,
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_format") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_format") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -6787,7 +6860,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_5parse_format(PyObject *__pyx_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("parse_format", 0, 3, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("parse_format", 0, 3, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.parse_format", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -6831,50 +6904,50 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   __Pyx_RefNannySetupContext("parse_format", 0);
   __Pyx_INCREF(__pyx_v_format);
 
-  /* "pysam/cvcf.pyx":349
+  /* "pysam/cvcf.pyx":353
  * 
  *     def parse_format(self,line,format,filter=False):
  *         if self._version == 40:             # <<<<<<<<<<<<<<
  *             if not format.startswith('<'):
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, __pyx_int_40, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, __pyx_int_40, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 349; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_3) {
 
-    /* "pysam/cvcf.pyx":350
+    /* "pysam/cvcf.pyx":354
  *     def parse_format(self,line,format,filter=False):
  *         if self._version == 40:
  *             if not format.startswith('<'):             # <<<<<<<<<<<<<<
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format = "<"+format
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__22, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__22, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_t_4 = ((!__pyx_t_3) != 0);
     if (__pyx_t_4) {
 
-      /* "pysam/cvcf.pyx":351
+      /* "pysam/cvcf.pyx":355
  *         if self._version == 40:
  *             if not format.startswith('<'):
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)             # <<<<<<<<<<<<<<
  *                 format = "<"+format
  *             if not format.endswith('>'):
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V40_MISSING_ANGLE_BRACKETS); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V40_MISSING_ANGLE_BRACKETS); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_line);
@@ -6882,20 +6955,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
       PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_5, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 351; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_5, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-      /* "pysam/cvcf.pyx":352
+      /* "pysam/cvcf.pyx":356
  *             if not format.startswith('<'):
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format = "<"+format             # <<<<<<<<<<<<<<
  *             if not format.endswith('>'):
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  */
-      __pyx_t_2 = PyNumber_Add(__pyx_kp_s__21, __pyx_v_format); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyNumber_Add(__pyx_kp_s__21, __pyx_v_format); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF_SET(__pyx_v_format, __pyx_t_2);
       __pyx_t_2 = 0;
@@ -6903,35 +6976,35 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     }
     __pyx_L4:;
 
-    /* "pysam/cvcf.pyx":353
+    /* "pysam/cvcf.pyx":357
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format = "<"+format
  *             if not format.endswith('>'):             # <<<<<<<<<<<<<<
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format += ">"
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_endswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_endswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__24, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__24, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __pyx_t_3 = ((!__pyx_t_4) != 0);
     if (__pyx_t_3) {
 
-      /* "pysam/cvcf.pyx":354
+      /* "pysam/cvcf.pyx":358
  *                 format = "<"+format
  *             if not format.endswith('>'):
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)             # <<<<<<<<<<<<<<
  *                 format += ">"
  *             format = format[1:-1]
  */
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V40_MISSING_ANGLE_BRACKETS); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V40_MISSING_ANGLE_BRACKETS); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_line);
@@ -6939,20 +7012,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
       PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 358; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-      /* "pysam/cvcf.pyx":355
+      /* "pysam/cvcf.pyx":359
  *             if not format.endswith('>'):
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format += ">"             # <<<<<<<<<<<<<<
  *             format = format[1:-1]
  *         data = {'id':None,'number':None,'type':None,'descr':None}
  */
-      __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_v_format, __pyx_kp_s__23); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_v_format, __pyx_kp_s__23); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 359; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF_SET(__pyx_v_format, __pyx_t_2);
       __pyx_t_2 = 0;
@@ -6960,14 +7033,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     }
     __pyx_L5:;
 
-    /* "pysam/cvcf.pyx":356
+    /* "pysam/cvcf.pyx":360
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format += ">"
  *             format = format[1:-1]             # <<<<<<<<<<<<<<
  *         data = {'id':None,'number':None,'type':None,'descr':None}
  *         idx = 0
  */
-    __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_format, 1, -1, NULL, NULL, &__pyx_slice__25, 1, 1, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_format, 1, -1, NULL, NULL, &__pyx_slice__25, 1, 1, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF_SET(__pyx_v_format, __pyx_t_2);
     __pyx_t_2 = 0;
@@ -6975,23 +7048,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":357
+  /* "pysam/cvcf.pyx":361
  *                 format += ">"
  *             format = format[1:-1]
  *         data = {'id':None,'number':None,'type':None,'descr':None}             # <<<<<<<<<<<<<<
  *         idx = 0
  *         while len(format.strip())>0:
  */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_id, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_number, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_type, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_descr, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_id, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_number, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_type, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_descr, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_data = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":358
+  /* "pysam/cvcf.pyx":362
  *             format = format[1:-1]
  *         data = {'id':None,'number':None,'type':None,'descr':None}
  *         idx = 0             # <<<<<<<<<<<<<<
@@ -7001,7 +7074,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   __Pyx_INCREF(__pyx_int_0);
   __pyx_v_idx = __pyx_int_0;
 
-  /* "pysam/cvcf.pyx":359
+  /* "pysam/cvcf.pyx":363
  *         data = {'id':None,'number':None,'type':None,'descr':None}
  *         idx = 0
  *         while len(format.strip())>0:             # <<<<<<<<<<<<<<
@@ -7009,49 +7082,49 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
  *             first, rest = elts[0], ','.join(elts[1:])
  */
   while (1) {
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 359; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 359; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_6 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 359; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_t_3 = ((__pyx_t_6 > 0) != 0);
     if (!__pyx_t_3) break;
 
-    /* "pysam/cvcf.pyx":360
+    /* "pysam/cvcf.pyx":364
  *         idx = 0
  *         while len(format.strip())>0:
  *             elts = format.strip().split(',')             # <<<<<<<<<<<<<<
  *             first, rest = elts[0], ','.join(elts[1:])
  *             if first.find('=') == -1 or (first.find('"')>=0 and first.find('=') > first.find('"')):
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_strip); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_strip); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__26, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__26, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_XDECREF_SET(__pyx_v_elts, __pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":361
+    /* "pysam/cvcf.pyx":365
  *         while len(format.strip())>0:
  *             elts = format.strip().split(',')
  *             first, rest = elts[0], ','.join(elts[1:])             # <<<<<<<<<<<<<<
  *             if first.find('=') == -1 or (first.find('"')>=0 and first.find('=') > first.find('"')):
  *                 if self._version == 40: self.error(line,self.V40_FORMAT_MUST_HAVE_NAMED_FIELDS)
  */
-    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_elts, 1, 0, NULL, NULL, &__pyx_slice__27, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_elts, 1, 0, NULL, NULL, &__pyx_slice__27, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_XDECREF_SET(__pyx_v_first, __pyx_t_2);
@@ -7059,47 +7132,47 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     __Pyx_XDECREF_SET(__pyx_v_rest, ((PyObject*)__pyx_t_5));
     __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":362
+    /* "pysam/cvcf.pyx":366
  *             elts = format.strip().split(',')
  *             first, rest = elts[0], ','.join(elts[1:])
  *             if first.find('=') == -1 or (first.find('"')>=0 and first.find('=') > first.find('"')):             # <<<<<<<<<<<<<<
  *                 if self._version == 40: self.error(line,self.V40_FORMAT_MUST_HAVE_NAMED_FIELDS)
  *                 if idx == 4: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_find); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_find); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__28, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__28, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_int_neg_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_int_neg_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     if (!__pyx_t_3) {
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_find); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_find); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__30, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__30, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_GE); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_int_0, Py_GE); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_find); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_find); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_5);
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__31, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__31, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_find); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_find); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_5);
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__32, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__32, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-        __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_GT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_GT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
         __pyx_t_8 = __pyx_t_7;
       } else {
@@ -7111,25 +7184,25 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     }
     if (__pyx_t_4) {
 
-      /* "pysam/cvcf.pyx":363
+      /* "pysam/cvcf.pyx":367
  *             first, rest = elts[0], ','.join(elts[1:])
  *             if first.find('=') == -1 or (first.find('"')>=0 and first.find('=') > first.find('"')):
  *                 if self._version == 40: self.error(line,self.V40_FORMAT_MUST_HAVE_NAMED_FIELDS)             # <<<<<<<<<<<<<<
  *                 if idx == 4: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *                 first = ["ID=","Number=","Type=","Description="][idx] + first
  */
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_1 = PyObject_RichCompare(__pyx_t_5, __pyx_int_40, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyObject_RichCompare(__pyx_t_5, __pyx_int_40, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       if (__pyx_t_4) {
-        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V40_FORMAT_MUST_HAVE_NAMED_FIELD); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V40_FORMAT_MUST_HAVE_NAMED_FIELD); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_5);
-        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_line);
@@ -7137,7 +7210,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_5);
         __Pyx_GIVEREF(__pyx_t_5);
         __pyx_t_5 = 0;
-        __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 363; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -7146,22 +7219,22 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
       }
       __pyx_L9:;
 
-      /* "pysam/cvcf.pyx":364
+      /* "pysam/cvcf.pyx":368
  *             if first.find('=') == -1 or (first.find('"')>=0 and first.find('=') > first.find('"')):
  *                 if self._version == 40: self.error(line,self.V40_FORMAT_MUST_HAVE_NAMED_FIELDS)
  *                 if idx == 4: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)             # <<<<<<<<<<<<<<
  *                 first = ["ID=","Number=","Type=","Description="][idx] + first
  *             if first.startswith('ID='):            data['id'] = first.split('=')[1]
  */
-      __pyx_t_5 = PyObject_RichCompare(__pyx_v_idx, __pyx_int_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyObject_RichCompare(__pyx_v_idx, __pyx_int_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_5);
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_line);
@@ -7169,7 +7242,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_2);
         __Pyx_GIVEREF(__pyx_t_2);
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -7178,14 +7251,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
       }
       __pyx_L10:;
 
-      /* "pysam/cvcf.pyx":365
+      /* "pysam/cvcf.pyx":369
  *                 if self._version == 40: self.error(line,self.V40_FORMAT_MUST_HAVE_NAMED_FIELDS)
  *                 if idx == 4: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *                 first = ["ID=","Number=","Type=","Description="][idx] + first             # <<<<<<<<<<<<<<
  *             if first.startswith('ID='):            data['id'] = first.split('=')[1]
  *             elif first.startswith('Number='):      data['number'] = first.split('=')[1]
  */
-      __pyx_t_2 = PyList_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyList_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_INCREF(__pyx_kp_s_ID);
       PyList_SET_ITEM(__pyx_t_2, 0, __pyx_kp_s_ID);
@@ -7199,10 +7272,10 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
       __Pyx_INCREF(__pyx_kp_s_Description);
       PyList_SET_ITEM(__pyx_t_2, 3, __pyx_kp_s_Description);
       __Pyx_GIVEREF(__pyx_kp_s_Description);
-      __pyx_t_1 = PyObject_GetItem(__pyx_t_2, __pyx_v_idx); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_1 = PyObject_GetItem(__pyx_t_2, __pyx_v_idx); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_v_first); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_v_first); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF_SET(__pyx_v_first, __pyx_t_2);
@@ -7211,144 +7284,144 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     }
     __pyx_L8:;
 
-    /* "pysam/cvcf.pyx":366
+    /* "pysam/cvcf.pyx":370
  *                 if idx == 4: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *                 first = ["ID=","Number=","Type=","Description="][idx] + first
  *             if first.startswith('ID='):            data['id'] = first.split('=')[1]             # <<<<<<<<<<<<<<
  *             elif first.startswith('Number='):      data['number'] = first.split('=')[1]
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__33, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__33, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (__pyx_t_4) {
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__34, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__34, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_GetItemInt(__pyx_t_2, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_1 = __Pyx_GetItemInt(__pyx_t_2, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_id, __pyx_t_1) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_id, __pyx_t_1) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       goto __pyx_L11;
     }
 
-    /* "pysam/cvcf.pyx":367
+    /* "pysam/cvcf.pyx":371
  *                 first = ["ID=","Number=","Type=","Description="][idx] + first
  *             if first.startswith('ID='):            data['id'] = first.split('=')[1]
  *             elif first.startswith('Number='):      data['number'] = first.split('=')[1]             # <<<<<<<<<<<<<<
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]
  *             elif first.startswith('Description='):
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__35, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__35, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (__pyx_t_4) {
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__36, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__36, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_GetItemInt(__pyx_t_1, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_2 = __Pyx_GetItemInt(__pyx_t_1, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_number, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_number, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       goto __pyx_L11;
     }
 
-    /* "pysam/cvcf.pyx":368
+    /* "pysam/cvcf.pyx":372
  *             if first.startswith('ID='):            data['id'] = first.split('=')[1]
  *             elif first.startswith('Number='):      data['number'] = first.split('=')[1]
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]             # <<<<<<<<<<<<<<
  *             elif first.startswith('Description='):
  *                 elts = format.split('"')
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__37, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__37, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (__pyx_t_4) {
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__38, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__38, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_GetItemInt(__pyx_t_2, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_1 = __Pyx_GetItemInt(__pyx_t_2, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_type, __pyx_t_1) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_type, __pyx_t_1) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       goto __pyx_L11;
     }
 
-    /* "pysam/cvcf.pyx":369
+    /* "pysam/cvcf.pyx":373
  *             elif first.startswith('Number='):      data['number'] = first.split('=')[1]
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]
  *             elif first.startswith('Description='):             # <<<<<<<<<<<<<<
  *                 elts = format.split('"')
  *                 if len(elts)<3:
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__39, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__39, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (__pyx_t_4) {
 
-      /* "pysam/cvcf.pyx":370
+      /* "pysam/cvcf.pyx":374
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]
  *             elif first.startswith('Description='):
  *                 elts = format.split('"')             # <<<<<<<<<<<<<<
  *                 if len(elts)<3:
  *                     self.error(line,self.FORMAT_MISSING_QUOTES)
  */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_format, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__40, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__40, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF_SET(__pyx_v_elts, __pyx_t_1);
       __pyx_t_1 = 0;
 
-      /* "pysam/cvcf.pyx":371
+      /* "pysam/cvcf.pyx":375
  *             elif first.startswith('Description='):
  *                 elts = format.split('"')
  *                 if len(elts)<3:             # <<<<<<<<<<<<<<
  *                     self.error(line,self.FORMAT_MISSING_QUOTES)
  *                     elts = first.split('=') + [rest]
  */
-      __pyx_t_6 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_4 = ((__pyx_t_6 < 3) != 0);
       if (__pyx_t_4) {
 
-        /* "pysam/cvcf.pyx":372
+        /* "pysam/cvcf.pyx":376
  *                 elts = format.split('"')
  *                 if len(elts)<3:
  *                     self.error(line,self.FORMAT_MISSING_QUOTES)             # <<<<<<<<<<<<<<
  *                     elts = first.split('=') + [rest]
  *                 data['descr'] = elts[1]
  */
-        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_FORMAT_MISSING_QUOTES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_FORMAT_MISSING_QUOTES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_line);
@@ -7356,30 +7429,30 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_2);
         __Pyx_GIVEREF(__pyx_t_2);
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_5, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_5, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-        /* "pysam/cvcf.pyx":373
+        /* "pysam/cvcf.pyx":377
  *                 if len(elts)<3:
  *                     self.error(line,self.FORMAT_MISSING_QUOTES)
  *                     elts = first.split('=') + [rest]             # <<<<<<<<<<<<<<
  *                 data['descr'] = elts[1]
  *                 rest = '"'.join(elts[2:])
  */
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_first, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__41, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__41, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_INCREF(__pyx_v_rest);
         PyList_SET_ITEM(__pyx_t_2, 0, __pyx_v_rest);
         __Pyx_GIVEREF(__pyx_v_rest);
-        __pyx_t_1 = PyNumber_Add(__pyx_t_5, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyNumber_Add(__pyx_t_5, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -7389,53 +7462,53 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
       }
       __pyx_L12:;
 
-      /* "pysam/cvcf.pyx":374
+      /* "pysam/cvcf.pyx":378
  *                     self.error(line,self.FORMAT_MISSING_QUOTES)
  *                     elts = first.split('=') + [rest]
  *                 data['descr'] = elts[1]             # <<<<<<<<<<<<<<
  *                 rest = '"'.join(elts[2:])
  *                 if rest.startswith(','): rest = rest[1:]
  */
-      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_1);
-      if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_descr, __pyx_t_1) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_descr, __pyx_t_1) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-      /* "pysam/cvcf.pyx":375
+      /* "pysam/cvcf.pyx":379
  *                     elts = first.split('=') + [rest]
  *                 data['descr'] = elts[1]
  *                 rest = '"'.join(elts[2:])             # <<<<<<<<<<<<<<
  *                 if rest.startswith(','): rest = rest[1:]
  *             else:
  */
-      __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_elts, 2, 0, NULL, NULL, &__pyx_slice__42, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_elts, 2, 0, NULL, NULL, &__pyx_slice__42, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__29, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__29, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF_SET(__pyx_v_rest, ((PyObject*)__pyx_t_2));
       __pyx_t_2 = 0;
 
-      /* "pysam/cvcf.pyx":376
+      /* "pysam/cvcf.pyx":380
  *                 data['descr'] = elts[1]
  *                 rest = '"'.join(elts[2:])
  *                 if rest.startswith(','): rest = rest[1:]             # <<<<<<<<<<<<<<
  *             else:
  *                 self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_rest, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_rest, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__43, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__43, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       if (__pyx_t_4) {
         if (unlikely(__pyx_v_rest == Py_None)) {
           PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
-        __pyx_t_1 = __Pyx_PySequence_GetSlice(__pyx_v_rest, 1, PY_SSIZE_T_MAX); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PySequence_GetSlice(__pyx_v_rest, 1, PY_SSIZE_T_MAX); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF_SET(__pyx_v_rest, ((PyObject*)__pyx_t_1));
         __pyx_t_1 = 0;
@@ -7446,18 +7519,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     }
     /*else*/ {
 
-      /* "pysam/cvcf.pyx":378
+      /* "pysam/cvcf.pyx":382
  *                 if rest.startswith(','): rest = rest[1:]
  *             else:
  *                 self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)             # <<<<<<<<<<<<<<
  *             format = rest
  *             idx += 1
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_line);
@@ -7465,7 +7538,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
       PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_5, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 378; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_5, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
@@ -7473,7 +7546,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     }
     __pyx_L11:;
 
-    /* "pysam/cvcf.pyx":379
+    /* "pysam/cvcf.pyx":383
  *             else:
  *                 self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *             format = rest             # <<<<<<<<<<<<<<
@@ -7483,29 +7556,29 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     __Pyx_INCREF(__pyx_v_rest);
     __Pyx_DECREF_SET(__pyx_v_format, __pyx_v_rest);
 
-    /* "pysam/cvcf.pyx":380
+    /* "pysam/cvcf.pyx":384
  *                 self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *             format = rest
  *             idx += 1             # <<<<<<<<<<<<<<
  *             if filter and idx==1: idx=3  # skip number and type fields for FILTER format strings
  *         if not data['id']: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  */
-    __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_v_idx, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_v_idx, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 384; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF_SET(__pyx_v_idx, __pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":381
+    /* "pysam/cvcf.pyx":385
  *             format = rest
  *             idx += 1
  *             if filter and idx==1: idx=3  # skip number and type fields for FILTER format strings             # <<<<<<<<<<<<<<
  *         if not data['id']: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *         if 'descr' not in data:
  */
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_filter); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_filter); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_4) {
-      __pyx_t_2 = PyObject_RichCompare(__pyx_v_idx, __pyx_int_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 381; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyObject_RichCompare(__pyx_v_idx, __pyx_int_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __pyx_t_8 = __pyx_t_3;
     } else {
@@ -7519,24 +7592,24 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     __pyx_L14:;
   }
 
-  /* "pysam/cvcf.pyx":382
+  /* "pysam/cvcf.pyx":386
  *             idx += 1
  *             if filter and idx==1: idx=3  # skip number and type fields for FILTER format strings
  *         if not data['id']: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)             # <<<<<<<<<<<<<<
  *         if 'descr' not in data:
  *             # missing description
  */
-  __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_id); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_id); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_4 = ((!__pyx_t_8) != 0);
   if (__pyx_t_4) {
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_line);
@@ -7544,7 +7617,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_5);
     __Pyx_GIVEREF(__pyx_t_5);
     __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 382; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -7553,29 +7626,29 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   }
   __pyx_L15:;
 
-  /* "pysam/cvcf.pyx":383
+  /* "pysam/cvcf.pyx":387
  *             if filter and idx==1: idx=3  # skip number and type fields for FILTER format strings
  *         if not data['id']: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *         if 'descr' not in data:             # <<<<<<<<<<<<<<
  *             # missing description
  *             self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  */
-  __pyx_t_4 = (__Pyx_PyDict_Contains(__pyx_n_s_descr, __pyx_v_data, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 383; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = (__Pyx_PyDict_Contains(__pyx_n_s_descr, __pyx_v_data, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_8 = (__pyx_t_4 != 0);
   if (__pyx_t_8) {
 
-    /* "pysam/cvcf.pyx":385
+    /* "pysam/cvcf.pyx":389
  *         if 'descr' not in data:
  *             # missing description
  *             self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)             # <<<<<<<<<<<<<<
  *             data['descr'] = ""
  *         if not data['type'] and not data['number']:
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_line);
@@ -7583,40 +7656,40 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 385; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":386
+    /* "pysam/cvcf.pyx":390
  *             # missing description
  *             self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *             data['descr'] = ""             # <<<<<<<<<<<<<<
  *         if not data['type'] and not data['number']:
  *             # fine, ##filter format
  */
-    if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_descr, __pyx_kp_s_) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 386; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_descr, __pyx_kp_s_) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     goto __pyx_L16;
   }
   __pyx_L16:;
 
-  /* "pysam/cvcf.pyx":387
+  /* "pysam/cvcf.pyx":391
  *             self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *             data['descr'] = ""
  *         if not data['type'] and not data['number']:             # <<<<<<<<<<<<<<
  *             # fine, ##filter format
  *             return FORMAT(data['id'],self.NT_NUMBER,0,"Flag",data['descr'],'.')
  */
-  __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 387; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_t_4 = ((!__pyx_t_8) != 0);
   if (__pyx_t_4) {
-    __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 387; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 387; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_t_3 = ((!__pyx_t_8) != 0);
     __pyx_t_8 = __pyx_t_3;
@@ -7625,7 +7698,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   }
   if (__pyx_t_8) {
 
-    /* "pysam/cvcf.pyx":389
+    /* "pysam/cvcf.pyx":393
  *         if not data['type'] and not data['number']:
  *             # fine, ##filter format
  *             return FORMAT(data['id'],self.NT_NUMBER,0,"Flag",data['descr'],'.')             # <<<<<<<<<<<<<<
@@ -7633,15 +7706,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
  *             self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_id); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_id); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_descr); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_descr); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_10 = PyTuple_New(6); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyTuple_New(6); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
@@ -7661,7 +7734,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     __pyx_t_2 = 0;
     __pyx_t_5 = 0;
     __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_10, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 389; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_10, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
@@ -7670,36 +7743,36 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":390
+  /* "pysam/cvcf.pyx":394
  *             # fine, ##filter format
  *             return FORMAT(data['id'],self.NT_NUMBER,0,"Flag",data['descr'],'.')
  *         if not data['type'] in ["Integer","Float","Character","String","Flag"]:             # <<<<<<<<<<<<<<
  *             self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *         # I would like a missing-value field, but it isn't there
  */
-  __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 390; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 394; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_8 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Integer, Py_NE)); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Integer, Py_NE)); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 394; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_8) {
-    __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Float, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Float, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 394; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_3 = __pyx_t_4;
   } else {
     __pyx_t_3 = __pyx_t_8;
   }
   if (__pyx_t_3) {
-    __pyx_t_8 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Character, Py_NE)); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Character, Py_NE)); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 394; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_4 = __pyx_t_8;
   } else {
     __pyx_t_4 = __pyx_t_3;
   }
   if (__pyx_t_4) {
-    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_String, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_String, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 394; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_8 = __pyx_t_3;
   } else {
     __pyx_t_8 = __pyx_t_4;
   }
   if (__pyx_t_8) {
-    __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Flag, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 390; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Flag, Py_NE)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 394; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_3 = __pyx_t_4;
   } else {
     __pyx_t_3 = __pyx_t_8;
@@ -7708,18 +7781,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   __pyx_t_8 = (__pyx_t_3 != 0);
   if (__pyx_t_8) {
 
-    /* "pysam/cvcf.pyx":391
+    /* "pysam/cvcf.pyx":395
  *             return FORMAT(data['id'],self.NT_NUMBER,0,"Flag",data['descr'],'.')
  *         if not data['type'] in ["Integer","Float","Character","String","Flag"]:
  *             self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)             # <<<<<<<<<<<<<<
  *         # I would like a missing-value field, but it isn't there
  *         if data['type'] in ['Integer','Float']: data['missing'] = None    # Do NOT use arbitrary int/float as missing value
  */
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_line);
@@ -7727,7 +7800,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_10);
     __Pyx_GIVEREF(__pyx_t_10);
     __pyx_t_10 = 0;
-    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_1, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_1, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -7736,18 +7809,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   }
   __pyx_L18:;
 
-  /* "pysam/cvcf.pyx":393
+  /* "pysam/cvcf.pyx":397
  *             self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *         # I would like a missing-value field, but it isn't there
  *         if data['type'] in ['Integer','Float']: data['missing'] = None    # Do NOT use arbitrary int/float as missing value             # <<<<<<<<<<<<<<
  *         else:                                   data['missing'] = '.'
  *         if not data['number']: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  */
-  __pyx_t_10 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_10 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_10);
-  __pyx_t_8 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_n_s_Integer, Py_EQ)); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_n_s_Integer, Py_EQ)); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (!__pyx_t_8) {
-    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_n_s_Float, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_n_s_Float, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_4 = __pyx_t_3;
   } else {
     __pyx_t_4 = __pyx_t_8;
@@ -7755,40 +7828,40 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   __pyx_t_8 = (__pyx_t_4 != 0);
   if (__pyx_t_8) {
-    if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_missing, Py_None) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_missing, Py_None) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     goto __pyx_L19;
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":394
+    /* "pysam/cvcf.pyx":398
  *         # I would like a missing-value field, but it isn't there
  *         if data['type'] in ['Integer','Float']: data['missing'] = None    # Do NOT use arbitrary int/float as missing value
  *         else:                                   data['missing'] = '.'             # <<<<<<<<<<<<<<
  *         if not data['number']: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *         try:
  */
-    if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_missing, __pyx_kp_s__8) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 394; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_missing, __pyx_kp_s__8) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 398; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
   __pyx_L19:;
 
-  /* "pysam/cvcf.pyx":395
+  /* "pysam/cvcf.pyx":399
  *         if data['type'] in ['Integer','Float']: data['missing'] = None    # Do NOT use arbitrary int/float as missing value
  *         else:                                   data['missing'] = '.'
  *         if not data['number']: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)             # <<<<<<<<<<<<<<
  *         try:
  *             n = int(data['number'])
  */
-  __pyx_t_10 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_10 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 399; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_10);
-  __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 399; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   __pyx_t_4 = ((!__pyx_t_8) != 0);
   if (__pyx_t_4) {
-    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 399; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 399; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 399; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_line);
@@ -7796,7 +7869,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 399; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -7805,7 +7878,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   }
   __pyx_L20:;
 
-  /* "pysam/cvcf.pyx":396
+  /* "pysam/cvcf.pyx":400
  *         else:                                   data['missing'] = '.'
  *         if not data['number']: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *         try:             # <<<<<<<<<<<<<<
@@ -7819,29 +7892,29 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     __Pyx_XGOTREF(__pyx_t_13);
     /*try:*/ {
 
-      /* "pysam/cvcf.pyx":397
+      /* "pysam/cvcf.pyx":401
  *         if not data['number']: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *         try:
  *             n = int(data['number'])             # <<<<<<<<<<<<<<
  *             t = self.NT_NUMBER
  *         except ValueError:
  */
-      __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L21_error;};
+      __pyx_t_1 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L21_error;};
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_9 = PyNumber_Int(__pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L21_error;}
+      __pyx_t_9 = PyNumber_Int(__pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L21_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __pyx_v_n = __pyx_t_9;
       __pyx_t_9 = 0;
 
-      /* "pysam/cvcf.pyx":398
+      /* "pysam/cvcf.pyx":402
  *         try:
  *             n = int(data['number'])
  *             t = self.NT_NUMBER             # <<<<<<<<<<<<<<
  *         except ValueError:
  *             n = -1
  */
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 398; __pyx_clineno = __LINE__; goto __pyx_L21_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 402; __pyx_clineno = __LINE__; goto __pyx_L21_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __pyx_v_t = __pyx_t_9;
       __pyx_t_9 = 0;
@@ -7857,7 +7930,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-    /* "pysam/cvcf.pyx":399
+    /* "pysam/cvcf.pyx":403
  *             n = int(data['number'])
  *             t = self.NT_NUMBER
  *         except ValueError:             # <<<<<<<<<<<<<<
@@ -7867,12 +7940,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     __pyx_t_14 = PyErr_ExceptionMatches(__pyx_builtin_ValueError);
     if (__pyx_t_14) {
       __Pyx_AddTraceback("pysam.cvcf.VCF.parse_format", __pyx_clineno, __pyx_lineno, __pyx_filename);
-      if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_1, &__pyx_t_10) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 399; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+      if (__Pyx_GetException(&__pyx_t_9, &__pyx_t_1, &__pyx_t_10) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 403; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_GOTREF(__pyx_t_10);
 
-      /* "pysam/cvcf.pyx":400
+      /* "pysam/cvcf.pyx":404
  *             t = self.NT_NUMBER
  *         except ValueError:
  *             n = -1             # <<<<<<<<<<<<<<
@@ -7882,152 +7955,152 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
       __Pyx_INCREF(__pyx_int_neg_1);
       __Pyx_XDECREF_SET(__pyx_v_n, __pyx_int_neg_1);
 
-      /* "pysam/cvcf.pyx":401
+      /* "pysam/cvcf.pyx":405
  *         except ValueError:
  *             n = -1
  *             if data['number'] == '.':                   t = self.NT_UNKNOWN             # <<<<<<<<<<<<<<
  *             elif data['number'] == '#alleles':          t = self.NT_ALLELES
  *             elif data['number'] == '#nonref_alleles':   t = self.NT_NR_ALLELES
  */
-      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
+      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 401; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_5);
         __pyx_t_5 = 0;
         goto __pyx_L31;
       }
 
-      /* "pysam/cvcf.pyx":402
+      /* "pysam/cvcf.pyx":406
  *             n = -1
  *             if data['number'] == '.':                   t = self.NT_UNKNOWN
  *             elif data['number'] == '#alleles':          t = self.NT_ALLELES             # <<<<<<<<<<<<<<
  *             elif data['number'] == '#nonref_alleles':   t = self.NT_NR_ALLELES
  *             elif data['number'] == '#genotypes':        t = self.NT_GENOTYPES
  */
-      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 402; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
+      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_alleles, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 402; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_alleles, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 402; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_5);
         __pyx_t_5 = 0;
         goto __pyx_L31;
       }
 
-      /* "pysam/cvcf.pyx":403
+      /* "pysam/cvcf.pyx":407
  *             if data['number'] == '.':                   t = self.NT_UNKNOWN
  *             elif data['number'] == '#alleles':          t = self.NT_ALLELES
  *             elif data['number'] == '#nonref_alleles':   t = self.NT_NR_ALLELES             # <<<<<<<<<<<<<<
  *             elif data['number'] == '#genotypes':        t = self.NT_GENOTYPES
  *             elif data['number'] == '#phased_genotypes': t = self.NT_PHASED_GENOTYPES
  */
-      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 403; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
+      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_nonref_alleles, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 403; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_nonref_alleles, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NR_ALLELES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 403; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NR_ALLELES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_5);
         __pyx_t_5 = 0;
         goto __pyx_L31;
       }
 
-      /* "pysam/cvcf.pyx":404
+      /* "pysam/cvcf.pyx":408
  *             elif data['number'] == '#alleles':          t = self.NT_ALLELES
  *             elif data['number'] == '#nonref_alleles':   t = self.NT_NR_ALLELES
  *             elif data['number'] == '#genotypes':        t = self.NT_GENOTYPES             # <<<<<<<<<<<<<<
  *             elif data['number'] == '#phased_genotypes': t = self.NT_PHASED_GENOTYPES
  *             elif data['number'] == '#phased_genotypes': t = self.NT_PHASED_GENOTYPES
  */
-      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
+      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_genotypes, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_genotypes, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_5);
         __pyx_t_5 = 0;
         goto __pyx_L31;
       }
 
-      /* "pysam/cvcf.pyx":405
+      /* "pysam/cvcf.pyx":409
  *             elif data['number'] == '#nonref_alleles':   t = self.NT_NR_ALLELES
  *             elif data['number'] == '#genotypes':        t = self.NT_GENOTYPES
  *             elif data['number'] == '#phased_genotypes': t = self.NT_PHASED_GENOTYPES             # <<<<<<<<<<<<<<
  *             elif data['number'] == '#phased_genotypes': t = self.NT_PHASED_GENOTYPES
  *             # abbreviations added in VCF version v4.1
  */
-      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
+      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_phased_genotypes, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_phased_genotypes, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_PHASED_GENOTYPES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_PHASED_GENOTYPES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_5);
         __pyx_t_5 = 0;
         goto __pyx_L31;
       }
 
-      /* "pysam/cvcf.pyx":406
+      /* "pysam/cvcf.pyx":410
  *             elif data['number'] == '#genotypes':        t = self.NT_GENOTYPES
  *             elif data['number'] == '#phased_genotypes': t = self.NT_PHASED_GENOTYPES
  *             elif data['number'] == '#phased_genotypes': t = self.NT_PHASED_GENOTYPES             # <<<<<<<<<<<<<<
  *             # abbreviations added in VCF version v4.1
  *             elif data['number'] == 'A': t = self.NT_ALLELES
  */
-      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
+      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 410; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_phased_genotypes, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_kp_s_phased_genotypes, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 410; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_PHASED_GENOTYPES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_PHASED_GENOTYPES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 410; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_5);
         __pyx_t_5 = 0;
         goto __pyx_L31;
       }
 
-      /* "pysam/cvcf.pyx":408
+      /* "pysam/cvcf.pyx":412
  *             elif data['number'] == '#phased_genotypes': t = self.NT_PHASED_GENOTYPES
  *             # abbreviations added in VCF version v4.1
  *             elif data['number'] == 'A': t = self.NT_ALLELES             # <<<<<<<<<<<<<<
  *             elif data['number'] == 'G': t = self.NT_GENOTYPES
  *             else:
  */
-      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
+      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 412; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_n_s_A, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_n_s_A, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 412; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 412; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_5);
         __pyx_t_5 = 0;
         goto __pyx_L31;
       }
 
-      /* "pysam/cvcf.pyx":409
+      /* "pysam/cvcf.pyx":413
  *             # abbreviations added in VCF version v4.1
  *             elif data['number'] == 'A': t = self.NT_ALLELES
  *             elif data['number'] == 'G': t = self.NT_GENOTYPES             # <<<<<<<<<<<<<<
  *             else:
  *                 self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  */
-      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
+      __pyx_t_5 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_number); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;};
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_n_s_G, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+      __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_5, __pyx_n_s_G, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       if (__pyx_t_4) {
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_5);
         __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_5);
         __pyx_t_5 = 0;
@@ -8035,18 +8108,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":411
+        /* "pysam/cvcf.pyx":415
  *             elif data['number'] == 'G': t = self.NT_GENOTYPES
  *             else:
  *                 self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)             # <<<<<<<<<<<<<<
  *         # if number is 0 - type must be Flag
  *         if n == 0 and data['type'] != 'Flag':
  */
-        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_5);
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_FORMAT_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_15 = PyTuple_New(2); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_15 = PyTuple_New(2); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_15);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_15, 0, __pyx_v_line);
@@ -8054,7 +8127,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_15, 1, __pyx_t_2);
         __Pyx_GIVEREF(__pyx_t_2);
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_15, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 411; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_15, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 415; __pyx_clineno = __LINE__; goto __pyx_L23_except_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
         __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;
@@ -8081,20 +8154,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     __pyx_L28_try_end:;
   }
 
-  /* "pysam/cvcf.pyx":413
+  /* "pysam/cvcf.pyx":417
  *                 self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *         # if number is 0 - type must be Flag
  *         if n == 0 and data['type'] != 'Flag':             # <<<<<<<<<<<<<<
  *             self.error( line, self.ZERO_FOR_NON_FLAG_FIELD)
  *             # force type 'Flag' if no number
  */
-  __pyx_t_10 = PyObject_RichCompare(__pyx_v_n, __pyx_int_0, Py_EQ); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = PyObject_RichCompare(__pyx_v_n, __pyx_int_0, Py_EQ); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   if (__pyx_t_4) {
-    __pyx_t_10 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_10 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_8 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_n_s_Flag, Py_NE)); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 413; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_n_s_Flag, Py_NE)); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 417; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     __pyx_t_3 = __pyx_t_8;
   } else {
@@ -8102,18 +8175,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   }
   if (__pyx_t_3) {
 
-    /* "pysam/cvcf.pyx":414
+    /* "pysam/cvcf.pyx":418
  *         # if number is 0 - type must be Flag
  *         if n == 0 and data['type'] != 'Flag':
  *             self.error( line, self.ZERO_FOR_NON_FLAG_FIELD)             # <<<<<<<<<<<<<<
  *             # force type 'Flag' if no number
  *             data['type'] = 'Flag'
  */
-    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ZERO_FOR_NON_FLAG_FIELD); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ZERO_FOR_NON_FLAG_FIELD); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_line);
@@ -8121,25 +8194,25 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
     PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":416
+    /* "pysam/cvcf.pyx":420
  *             self.error( line, self.ZERO_FOR_NON_FLAG_FIELD)
  *             # force type 'Flag' if no number
  *             data['type'] = 'Flag'             # <<<<<<<<<<<<<<
  * 
  *         return FORMAT(data['id'],t,n,data['type'],data['descr'],data['missing'])
  */
-    if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_type, __pyx_n_s_Flag) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyDict_SetItem(__pyx_v_data, __pyx_n_s_type, __pyx_n_s_Flag) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     goto __pyx_L32;
   }
   __pyx_L32:;
 
-  /* "pysam/cvcf.pyx":418
+  /* "pysam/cvcf.pyx":422
  *             data['type'] = 'Flag'
  * 
  *         return FORMAT(data['id'],t,n,data['type'],data['descr'],data['missing'])             # <<<<<<<<<<<<<<
@@ -8147,18 +8220,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
  *     def format_format( self, fmt, filter=False ):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_id); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_id); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_9);
-  if (unlikely(!__pyx_v_t)) { __Pyx_RaiseUnboundLocalError("t"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-  __pyx_t_10 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  if (unlikely(!__pyx_v_t)) { __Pyx_RaiseUnboundLocalError("t"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+  __pyx_t_10 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_type); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_10);
-  __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_descr); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_descr); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_15 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_missing); if (unlikely(__pyx_t_15 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_15 = __Pyx_PyDict_GetItem(__pyx_v_data, __pyx_n_s_missing); if (unlikely(__pyx_t_15 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_15);
-  __pyx_t_5 = PyTuple_New(6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyTuple_New(6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_9);
   __Pyx_GIVEREF(__pyx_t_9);
@@ -8178,7 +8251,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   __pyx_t_10 = 0;
   __pyx_t_2 = 0;
   __pyx_t_15 = 0;
-  __pyx_t_15 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_5, NULL); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 418; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_5, NULL); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_15);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
@@ -8186,7 +8259,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   __pyx_t_15 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":348
+  /* "pysam/cvcf.pyx":352
  *         raise ValueError(errorstring)
  * 
  *     def parse_format(self,line,format,filter=False):             # <<<<<<<<<<<<<<
@@ -8218,7 +8291,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_4parse_format(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":420
+/* "pysam/cvcf.pyx":424
  *         return FORMAT(data['id'],t,n,data['type'],data['descr'],data['missing'])
  * 
  *     def format_format( self, fmt, filter=False ):             # <<<<<<<<<<<<<<
@@ -8261,7 +8334,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_7format_format(PyObject *__pyx_self,
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_fmt)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("format_format", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("format_format", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (kw_args > 0) {
@@ -8270,7 +8343,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_7format_format(PyObject *__pyx_self,
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "format_format") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "format_format") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -8287,7 +8360,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_7format_format(PyObject *__pyx_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("format_format", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("format_format", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.format_format", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -8325,16 +8398,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("format_format", 0);
 
-  /* "pysam/cvcf.pyx":421
+  /* "pysam/cvcf.pyx":425
  * 
  *     def format_format( self, fmt, filter=False ):
  *         values = [('ID',fmt.id)]             # <<<<<<<<<<<<<<
  *         if fmt.number != None and not filter:
  *             if fmt.numbertype == self.NT_UNKNOWN: nmb = "."
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_id); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_n_s_ID_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_n_s_ID_2);
@@ -8342,7 +8415,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
   __pyx_t_1 = 0;
-  __pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 421; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   PyList_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
@@ -8350,21 +8423,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
   __pyx_v_values = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":422
+  /* "pysam/cvcf.pyx":426
  *     def format_format( self, fmt, filter=False ):
  *         values = [('ID',fmt.id)]
  *         if fmt.number != None and not filter:             # <<<<<<<<<<<<<<
  *             if fmt.numbertype == self.NT_UNKNOWN: nmb = "."
  *             elif fmt.numbertype == self.NT_NUMBER: nmb = str(fmt.number)
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_number); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_number); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_3) {
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_filter); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_filter); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_5 = (!__pyx_t_4);
     __pyx_t_4 = __pyx_t_5;
   } else {
@@ -8372,21 +8445,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
   }
   if (__pyx_t_4) {
 
-    /* "pysam/cvcf.pyx":423
+    /* "pysam/cvcf.pyx":427
  *         values = [('ID',fmt.id)]
  *         if fmt.number != None and not filter:
  *             if fmt.numbertype == self.NT_UNKNOWN: nmb = "."             # <<<<<<<<<<<<<<
  *             elif fmt.numbertype == self.NT_NUMBER: nmb = str(fmt.number)
  *             elif fmt.numbertype == self.NT_ALLELES: nmb = "#alleles"
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_6 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 423; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     if (__pyx_t_4) {
       __Pyx_INCREF(__pyx_kp_s__8);
@@ -8394,31 +8467,31 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
       goto __pyx_L4;
     }
 
-    /* "pysam/cvcf.pyx":424
+    /* "pysam/cvcf.pyx":428
  *         if fmt.number != None and not filter:
  *             if fmt.numbertype == self.NT_UNKNOWN: nmb = "."
  *             elif fmt.numbertype == self.NT_NUMBER: nmb = str(fmt.number)             # <<<<<<<<<<<<<<
  *             elif fmt.numbertype == self.NT_ALLELES: nmb = "#alleles"
  *             elif fmt.numbertype == self.NT_NR_ALLELES: nmb = "#nonref_alleles"
  */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyObject_RichCompare(__pyx_t_6, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyObject_RichCompare(__pyx_t_6, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (__pyx_t_4) {
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_number); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_number); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __pyx_v_nmb = __pyx_t_2;
@@ -8426,21 +8499,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
       goto __pyx_L4;
     }
 
-    /* "pysam/cvcf.pyx":425
+    /* "pysam/cvcf.pyx":429
  *             if fmt.numbertype == self.NT_UNKNOWN: nmb = "."
  *             elif fmt.numbertype == self.NT_NUMBER: nmb = str(fmt.number)
  *             elif fmt.numbertype == self.NT_ALLELES: nmb = "#alleles"             # <<<<<<<<<<<<<<
  *             elif fmt.numbertype == self.NT_NR_ALLELES: nmb = "#nonref_alleles"
  *             elif fmt.numbertype == self.NT_GENOTYPES: nmb = "#genotypes"
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_6 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 425; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     if (__pyx_t_4) {
       __Pyx_INCREF(__pyx_kp_s_alleles);
@@ -8448,21 +8521,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
       goto __pyx_L4;
     }
 
-    /* "pysam/cvcf.pyx":426
+    /* "pysam/cvcf.pyx":430
  *             elif fmt.numbertype == self.NT_NUMBER: nmb = str(fmt.number)
  *             elif fmt.numbertype == self.NT_ALLELES: nmb = "#alleles"
  *             elif fmt.numbertype == self.NT_NR_ALLELES: nmb = "#nonref_alleles"             # <<<<<<<<<<<<<<
  *             elif fmt.numbertype == self.NT_GENOTYPES: nmb = "#genotypes"
  *             elif fmt.numbertype == self.NT_PHASED_GENOTYPES: nmb = "#phased_genotypes"
  */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NR_ALLELES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NR_ALLELES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyObject_RichCompare(__pyx_t_6, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyObject_RichCompare(__pyx_t_6, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 426; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (__pyx_t_4) {
       __Pyx_INCREF(__pyx_kp_s_nonref_alleles);
@@ -8470,21 +8543,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
       goto __pyx_L4;
     }
 
-    /* "pysam/cvcf.pyx":427
+    /* "pysam/cvcf.pyx":431
  *             elif fmt.numbertype == self.NT_ALLELES: nmb = "#alleles"
  *             elif fmt.numbertype == self.NT_NR_ALLELES: nmb = "#nonref_alleles"
  *             elif fmt.numbertype == self.NT_GENOTYPES: nmb = "#genotypes"             # <<<<<<<<<<<<<<
  *             elif fmt.numbertype == self.NT_PHASED_GENOTYPES: nmb = "#phased_genotypes"
  *             else:
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_6 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_RichCompare(__pyx_t_2, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 427; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     if (__pyx_t_4) {
       __Pyx_INCREF(__pyx_kp_s_genotypes);
@@ -8492,21 +8565,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
       goto __pyx_L4;
     }
 
-    /* "pysam/cvcf.pyx":428
+    /* "pysam/cvcf.pyx":432
  *             elif fmt.numbertype == self.NT_NR_ALLELES: nmb = "#nonref_alleles"
  *             elif fmt.numbertype == self.NT_GENOTYPES: nmb = "#genotypes"
  *             elif fmt.numbertype == self.NT_PHASED_GENOTYPES: nmb = "#phased_genotypes"             # <<<<<<<<<<<<<<
  *             else:
  *                 raise ValueError("Unknown number type encountered: %s" % fmt.numbertype)
  */
-    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_PHASED_GENOTYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_PHASED_GENOTYPES); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyObject_RichCompare(__pyx_t_6, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyObject_RichCompare(__pyx_t_6, __pyx_t_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 428; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (__pyx_t_4) {
       __Pyx_INCREF(__pyx_kp_s_phased_genotypes);
@@ -8515,40 +8588,40 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
     }
     /*else*/ {
 
-      /* "pysam/cvcf.pyx":430
+      /* "pysam/cvcf.pyx":434
  *             elif fmt.numbertype == self.NT_PHASED_GENOTYPES: nmb = "#phased_genotypes"
  *             else:
  *                 raise ValueError("Unknown number type encountered: %s" % fmt.numbertype)             # <<<<<<<<<<<<<<
  *             values.append( ('Number',nmb) )
  *             values.append( ('Type', fmt.type) )
  */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_Unknown_number_type_encountered, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_Unknown_number_type_encountered, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
       __Pyx_GIVEREF(__pyx_t_1);
       __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_Raise(__pyx_t_1, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 430; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     __pyx_L4:;
 
-    /* "pysam/cvcf.pyx":431
+    /* "pysam/cvcf.pyx":435
  *             else:
  *                 raise ValueError("Unknown number type encountered: %s" % fmt.numbertype)
  *             values.append( ('Number',nmb) )             # <<<<<<<<<<<<<<
  *             values.append( ('Type', fmt.type) )
  *         values.append( ('Description', '"' + fmt.description + '"') )
  */
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_n_s_Number_2);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_Number_2);
@@ -8556,19 +8629,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
     __Pyx_INCREF(__pyx_v_nmb);
     PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_nmb);
     __Pyx_GIVEREF(__pyx_v_nmb);
-    __pyx_t_7 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":432
+    /* "pysam/cvcf.pyx":436
  *                 raise ValueError("Unknown number type encountered: %s" % fmt.numbertype)
  *             values.append( ('Number',nmb) )
  *             values.append( ('Type', fmt.type) )             # <<<<<<<<<<<<<<
  *         values.append( ('Description', '"' + fmt.description + '"') )
  *         if self._version == 33:
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_type); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_type); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_n_s_Type_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_n_s_Type_2);
@@ -8576,28 +8649,28 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
     PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_7 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_2); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 432; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_2); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 436; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     goto __pyx_L3;
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":433
+  /* "pysam/cvcf.pyx":437
  *             values.append( ('Number',nmb) )
  *             values.append( ('Type', fmt.type) )
  *         values.append( ('Description', '"' + fmt.description + '"') )             # <<<<<<<<<<<<<<
  *         if self._version == 33:
  *             format = ",".join([v for k,v in values])
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_description); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_description); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyNumber_Add(__pyx_kp_s__29, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyNumber_Add(__pyx_kp_s__29, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_kp_s__29); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_kp_s__29); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_n_s_Description_2);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_Description_2);
@@ -8605,40 +8678,40 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
   PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
   __pyx_t_2 = 0;
-  __pyx_t_7 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyList_Append(__pyx_v_values, __pyx_t_1); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":434
+  /* "pysam/cvcf.pyx":438
  *             values.append( ('Type', fmt.type) )
  *         values.append( ('Description', '"' + fmt.description + '"') )
  *         if self._version == 33:             # <<<<<<<<<<<<<<
  *             format = ",".join([v for k,v in values])
  *         else:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 438; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, __pyx_int_33, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_1, __pyx_int_33, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 438; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 434; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 438; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_4) {
 
-    /* "pysam/cvcf.pyx":435
+    /* "pysam/cvcf.pyx":439
  *         values.append( ('Description', '"' + fmt.description + '"') )
  *         if self._version == 33:
  *             format = ",".join([v for k,v in values])             # <<<<<<<<<<<<<<
  *         else:
  *             format = "<" + (",".join( ["%s=%s" % (k,v) for (k,v) in values] )) + ">"
  */
-    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_t_1 = __pyx_v_values; __Pyx_INCREF(__pyx_t_1); __pyx_t_8 = 0;
     for (;;) {
       if (__pyx_t_8 >= PyList_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_6 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_8); __Pyx_INCREF(__pyx_t_6); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_8); __Pyx_INCREF(__pyx_t_6); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_6 = PySequence_ITEM(__pyx_t_1, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PySequence_ITEM(__pyx_t_1, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
       if ((likely(PyTuple_CheckExact(__pyx_t_6))) || (PyList_CheckExact(__pyx_t_6))) {
         PyObject* sequence = __pyx_t_6;
@@ -8650,7 +8723,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
         if (unlikely(size != 2)) {
           if (size > 2) __Pyx_RaiseTooManyValuesError(2);
           else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         #if CYTHON_COMPILING_IN_CPYTHON
         if (likely(PyTuple_CheckExact(sequence))) {
@@ -8663,15 +8736,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
         __Pyx_INCREF(__pyx_t_9);
         __Pyx_INCREF(__pyx_t_10);
         #else
-        __pyx_t_9 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_10 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
         #endif
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       } else {
         Py_ssize_t index = -1;
-        __pyx_t_11 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext;
@@ -8679,7 +8752,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
         __Pyx_GOTREF(__pyx_t_9);
         index = 1; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L8_unpacking_failed;
         __Pyx_GOTREF(__pyx_t_10);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_12 = NULL;
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         goto __pyx_L9_unpacking_done;
@@ -8687,17 +8760,17 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         __pyx_t_12 = NULL;
         if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_L9_unpacking_done:;
       }
       __Pyx_XDECREF_SET(__pyx_v_k, __pyx_t_9);
       __pyx_t_9 = 0;
       __Pyx_XDECREF_SET(__pyx_v_v, __pyx_t_10);
       __pyx_t_10 = 0;
-      if (unlikely(__Pyx_ListComp_Append(__pyx_t_2, (PyObject*)__pyx_v_v))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(__Pyx_ListComp_Append(__pyx_t_2, (PyObject*)__pyx_v_v))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 439; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_v_format = ((PyObject*)__pyx_t_1);
@@ -8706,22 +8779,22 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":437
+    /* "pysam/cvcf.pyx":441
  *             format = ",".join([v for k,v in values])
  *         else:
  *             format = "<" + (",".join( ["%s=%s" % (k,v) for (k,v) in values] )) + ">"             # <<<<<<<<<<<<<<
  *         return format
  * 
  */
-    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_t_2 = __pyx_v_values; __Pyx_INCREF(__pyx_t_2); __pyx_t_8 = 0;
     for (;;) {
       if (__pyx_t_8 >= PyList_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_6 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_8); __Pyx_INCREF(__pyx_t_6); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_8); __Pyx_INCREF(__pyx_t_6); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_6 = PySequence_ITEM(__pyx_t_2, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PySequence_ITEM(__pyx_t_2, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
       if ((likely(PyTuple_CheckExact(__pyx_t_6))) || (PyList_CheckExact(__pyx_t_6))) {
         PyObject* sequence = __pyx_t_6;
@@ -8733,7 +8806,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
         if (unlikely(size != 2)) {
           if (size > 2) __Pyx_RaiseTooManyValuesError(2);
           else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         #if CYTHON_COMPILING_IN_CPYTHON
         if (likely(PyTuple_CheckExact(sequence))) {
@@ -8746,15 +8819,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
         __Pyx_INCREF(__pyx_t_10);
         __Pyx_INCREF(__pyx_t_9);
         #else
-        __pyx_t_10 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_9 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         #endif
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       } else {
         Py_ssize_t index = -1;
-        __pyx_t_11 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = PyObject_GetIter(__pyx_t_6); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext;
@@ -8762,7 +8835,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
         __Pyx_GOTREF(__pyx_t_10);
         index = 1; __pyx_t_9 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_9)) goto __pyx_L12_unpacking_failed;
         __Pyx_GOTREF(__pyx_t_9);
-        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_12 = NULL;
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         goto __pyx_L13_unpacking_done;
@@ -8770,14 +8843,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         __pyx_t_12 = NULL;
         if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_L13_unpacking_done:;
       }
       __Pyx_XDECREF_SET(__pyx_v_k, __pyx_t_10);
       __pyx_t_10 = 0;
       __Pyx_XDECREF_SET(__pyx_v_v, __pyx_t_9);
       __pyx_t_9 = 0;
-      __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_INCREF(__pyx_v_k);
       PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_k);
@@ -8785,20 +8858,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
       __Pyx_INCREF(__pyx_v_v);
       PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_v_v);
       __Pyx_GIVEREF(__pyx_v_v);
-      __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-      if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_9))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_9))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     }
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = PyNumber_Add(__pyx_kp_s__21, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_Add(__pyx_kp_s__21, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_kp_s__23); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 437; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyNumber_Add(__pyx_t_1, __pyx_kp_s__23); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_v_format = ((PyObject*)__pyx_t_2);
@@ -8806,7 +8879,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
   }
   __pyx_L5:;
 
-  /* "pysam/cvcf.pyx":438
+  /* "pysam/cvcf.pyx":442
  *         else:
  *             format = "<" + (",".join( ["%s=%s" % (k,v) for (k,v) in values] )) + ">"
  *         return format             # <<<<<<<<<<<<<<
@@ -8818,7 +8891,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
   __pyx_r = __pyx_v_format;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":420
+  /* "pysam/cvcf.pyx":424
  *         return FORMAT(data['id'],t,n,data['type'],data['descr'],data['missing'])
  * 
  *     def format_format( self, fmt, filter=False ):             # <<<<<<<<<<<<<<
@@ -8847,7 +8920,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_6format_format(CYTHON_UNUSED PyObjec
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":440
+/* "pysam/cvcf.pyx":444
  *         return format
  * 
  *     def get_expected(self, format, formatdict, alt):             # <<<<<<<<<<<<<<
@@ -8891,21 +8964,21 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_9get_expected(PyObject *__pyx_self,
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_format)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("get_expected", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 440; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("get_expected", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_formatdict)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("get_expected", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 440; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("get_expected", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  3:
         if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_alt)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("get_expected", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 440; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("get_expected", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "get_expected") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 440; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "get_expected") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
       goto __pyx_L5_argtuple_error;
@@ -8922,7 +8995,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_9get_expected(PyObject *__pyx_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("get_expected", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 440; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("get_expected", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.get_expected", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -8950,33 +9023,33 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_8get_expected(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("get_expected", 0);
 
-  /* "pysam/cvcf.pyx":441
+  /* "pysam/cvcf.pyx":445
  * 
  *     def get_expected(self, format, formatdict, alt):
  *         fmt = formatdict[format]             # <<<<<<<<<<<<<<
  *         if fmt.numbertype == self.NT_UNKNOWN: return -1
  *         if fmt.numbertype == self.NT_NUMBER: return fmt.number
  */
-  __pyx_t_1 = PyObject_GetItem(__pyx_v_formatdict, __pyx_v_format); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 441; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_1 = PyObject_GetItem(__pyx_v_formatdict, __pyx_v_format); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_v_fmt = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":442
+  /* "pysam/cvcf.pyx":446
  *     def get_expected(self, format, formatdict, alt):
  *         fmt = formatdict[format]
  *         if fmt.numbertype == self.NT_UNKNOWN: return -1             # <<<<<<<<<<<<<<
  *         if fmt.numbertype == self.NT_NUMBER: return fmt.number
  *         if fmt.numbertype == self.NT_ALLELES: return len(alt)+1
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 442; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if (__pyx_t_4) {
     __Pyx_XDECREF(__pyx_r);
@@ -8985,138 +9058,138 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_8get_expected(CYTHON_UNUSED PyObject
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":443
+  /* "pysam/cvcf.pyx":447
  *         fmt = formatdict[format]
  *         if fmt.numbertype == self.NT_UNKNOWN: return -1
  *         if fmt.numbertype == self.NT_NUMBER: return fmt.number             # <<<<<<<<<<<<<<
  *         if fmt.numbertype == self.NT_ALLELES: return len(alt)+1
  *         if fmt.numbertype == self.NT_NR_ALLELES: return len(alt)
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyObject_RichCompare(__pyx_t_3, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_t_3, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_4) {
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_number); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 443; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_number); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_r = __pyx_t_1;
     __pyx_t_1 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":444
+  /* "pysam/cvcf.pyx":448
  *         if fmt.numbertype == self.NT_UNKNOWN: return -1
  *         if fmt.numbertype == self.NT_NUMBER: return fmt.number
  *         if fmt.numbertype == self.NT_ALLELES: return len(alt)+1             # <<<<<<<<<<<<<<
  *         if fmt.numbertype == self.NT_NR_ALLELES: return len(alt)
  *         if fmt.numbertype == self.NT_GENOTYPES: return ((len(alt)+1)*(len(alt)+2)) // 2
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if (__pyx_t_4) {
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_5 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = PyInt_FromSsize_t((__pyx_t_5 + 1)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyInt_FromSsize_t((__pyx_t_5 + 1)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_r = __pyx_t_3;
     __pyx_t_3 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":445
+  /* "pysam/cvcf.pyx":449
  *         if fmt.numbertype == self.NT_NUMBER: return fmt.number
  *         if fmt.numbertype == self.NT_ALLELES: return len(alt)+1
  *         if fmt.numbertype == self.NT_NR_ALLELES: return len(alt)             # <<<<<<<<<<<<<<
  *         if fmt.numbertype == self.NT_GENOTYPES: return ((len(alt)+1)*(len(alt)+2)) // 2
  *         if fmt.numbertype == self.NT_PHASED_GENOTYPES: return (len(alt)+1)*(len(alt)+1)
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NR_ALLELES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NR_ALLELES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyObject_RichCompare(__pyx_t_3, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_t_3, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_4) {
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_5 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 445; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 449; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_r = __pyx_t_1;
     __pyx_t_1 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":446
+  /* "pysam/cvcf.pyx":450
  *         if fmt.numbertype == self.NT_ALLELES: return len(alt)+1
  *         if fmt.numbertype == self.NT_NR_ALLELES: return len(alt)
  *         if fmt.numbertype == self.NT_GENOTYPES: return ((len(alt)+1)*(len(alt)+2)) // 2             # <<<<<<<<<<<<<<
  *         if fmt.numbertype == self.NT_PHASED_GENOTYPES: return (len(alt)+1)*(len(alt)+1)
  *         return 0
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if (__pyx_t_4) {
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_5 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_6 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = PyInt_FromSsize_t(__Pyx_div_Py_ssize_t(((__pyx_t_5 + 1) * (__pyx_t_6 + 2)), 2)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 446; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyInt_FromSsize_t(__Pyx_div_Py_ssize_t(((__pyx_t_5 + 1) * (__pyx_t_6 + 2)), 2)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_r = __pyx_t_3;
     __pyx_t_3 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":447
+  /* "pysam/cvcf.pyx":451
  *         if fmt.numbertype == self.NT_NR_ALLELES: return len(alt)
  *         if fmt.numbertype == self.NT_GENOTYPES: return ((len(alt)+1)*(len(alt)+2)) // 2
  *         if fmt.numbertype == self.NT_PHASED_GENOTYPES: return (len(alt)+1)*(len(alt)+1)             # <<<<<<<<<<<<<<
  *         return 0
  * 
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_fmt, __pyx_n_s_numbertype); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_PHASED_GENOTYPES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_PHASED_GENOTYPES); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyObject_RichCompare(__pyx_t_3, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_t_3, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_4) {
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_6 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_1 = PyInt_FromSsize_t(((__pyx_t_6 + 1) * (__pyx_t_5 + 1))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 447; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyObject_Length(__pyx_v_alt); if (unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyInt_FromSsize_t(((__pyx_t_6 + 1) * (__pyx_t_5 + 1))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_r = __pyx_t_1;
     __pyx_t_1 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":448
+  /* "pysam/cvcf.pyx":452
  *         if fmt.numbertype == self.NT_GENOTYPES: return ((len(alt)+1)*(len(alt)+2)) // 2
  *         if fmt.numbertype == self.NT_PHASED_GENOTYPES: return (len(alt)+1)*(len(alt)+1)
  *         return 0             # <<<<<<<<<<<<<<
@@ -9128,7 +9201,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_8get_expected(CYTHON_UNUSED PyObject
   __pyx_r = __pyx_int_0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":440
+  /* "pysam/cvcf.pyx":444
  *         return format
  * 
  *     def get_expected(self, format, formatdict, alt):             # <<<<<<<<<<<<<<
@@ -9150,7 +9223,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_8get_expected(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":451
+/* "pysam/cvcf.pyx":455
  * 
  * 
  *     def _add_definition(self, formatdict, key, data, line ):             # <<<<<<<<<<<<<<
@@ -9196,26 +9269,26 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_11_add_definition(PyObject *__pyx_se
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_formatdict)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_key)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  3:
         if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_data)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  4:
         if (likely((values[4] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_line)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, 4); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, 4); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_add_definition") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_add_definition") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 5) {
       goto __pyx_L5_argtuple_error;
@@ -9234,7 +9307,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_11_add_definition(PyObject *__pyx_se
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("_add_definition", 1, 5, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF._add_definition", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -9261,14 +9334,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
   __Pyx_RefNannySetupContext("_add_definition", 0);
   __Pyx_INCREF(__pyx_v_data);
 
-  /* "pysam/cvcf.pyx":452
+  /* "pysam/cvcf.pyx":456
  * 
  *     def _add_definition(self, formatdict, key, data, line ):
  *         if key in formatdict: return             # <<<<<<<<<<<<<<
  *         self.error(line,self.ERROR_UNKNOWN_KEY,key)
  *         if data == None:
  */
-  __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_v_key, __pyx_v_formatdict, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 452; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = (__Pyx_PySequence_Contains(__pyx_v_key, __pyx_v_formatdict, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_2 = (__pyx_t_1 != 0);
   if (__pyx_t_2) {
     __Pyx_XDECREF(__pyx_r);
@@ -9276,18 +9349,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":453
+  /* "pysam/cvcf.pyx":457
  *     def _add_definition(self, formatdict, key, data, line ):
  *         if key in formatdict: return
  *         self.error(line,self.ERROR_UNKNOWN_KEY,key)             # <<<<<<<<<<<<<<
  *         if data == None:
  *             formatdict[key] = FORMAT(key,self.NT_NUMBER,0,"Flag","(Undefined tag)",".")
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 453; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_UNKNOWN_KEY); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 453; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_UNKNOWN_KEY); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_5 = PyTuple_New(3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 453; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyTuple_New(3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_INCREF(__pyx_v_line);
   PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_line);
@@ -9298,36 +9371,36 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
   PyTuple_SET_ITEM(__pyx_t_5, 2, __pyx_v_key);
   __Pyx_GIVEREF(__pyx_v_key);
   __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 453; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/cvcf.pyx":454
+  /* "pysam/cvcf.pyx":458
  *         if key in formatdict: return
  *         self.error(line,self.ERROR_UNKNOWN_KEY,key)
  *         if data == None:             # <<<<<<<<<<<<<<
  *             formatdict[key] = FORMAT(key,self.NT_NUMBER,0,"Flag","(Undefined tag)",".")
  *             return
  */
-  __pyx_t_4 = PyObject_RichCompare(__pyx_v_data, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyObject_RichCompare(__pyx_v_data, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 458; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 458; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   if (__pyx_t_2) {
 
-    /* "pysam/cvcf.pyx":455
+    /* "pysam/cvcf.pyx":459
  *         self.error(line,self.ERROR_UNKNOWN_KEY,key)
  *         if data == None:
  *             formatdict[key] = FORMAT(key,self.NT_NUMBER,0,"Flag","(Undefined tag)",".")             # <<<<<<<<<<<<<<
  *             return
  *         if data == []: data = [""]             # unsure what type -- say string
  */
-    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_key);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_key);
@@ -9347,14 +9420,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
     PyTuple_SET_ITEM(__pyx_t_3, 5, __pyx_kp_s__8);
     __Pyx_GIVEREF(__pyx_kp_s__8);
     __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    if (unlikely(PyObject_SetItem(__pyx_v_formatdict, __pyx_v_key, __pyx_t_5) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyObject_SetItem(__pyx_v_formatdict, __pyx_v_key, __pyx_t_5) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":456
+    /* "pysam/cvcf.pyx":460
  *         if data == None:
  *             formatdict[key] = FORMAT(key,self.NT_NUMBER,0,"Flag","(Undefined tag)",".")
  *             return             # <<<<<<<<<<<<<<
@@ -9366,21 +9439,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":457
+  /* "pysam/cvcf.pyx":461
  *             formatdict[key] = FORMAT(key,self.NT_NUMBER,0,"Flag","(Undefined tag)",".")
  *             return
  *         if data == []: data = [""]             # unsure what type -- say string             # <<<<<<<<<<<<<<
  *         if type(data[0]) == type(0.0):
  *             formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"Float","(Undefined tag)",None)
  */
-  __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_3 = PyObject_RichCompare(__pyx_v_data, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_RichCompare(__pyx_v_data, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if (__pyx_t_2) {
-    __pyx_t_3 = PyList_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyList_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_kp_s_);
     PyList_SET_ITEM(__pyx_t_3, 0, __pyx_kp_s_);
@@ -9391,33 +9464,33 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
   }
   __pyx_L5:;
 
-  /* "pysam/cvcf.pyx":458
+  /* "pysam/cvcf.pyx":462
  *             return
  *         if data == []: data = [""]             # unsure what type -- say string
  *         if type(data[0]) == type(0.0):             # <<<<<<<<<<<<<<
  *             formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"Float","(Undefined tag)",None)
  *             return
  */
-  __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_data, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 458; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_data, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_5 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_t_3)), ((PyObject *)Py_TYPE(__pyx_float_0_0)), Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 458; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_t_3)), ((PyObject *)Py_TYPE(__pyx_float_0_0)), Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 458; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   if (__pyx_t_2) {
 
-    /* "pysam/cvcf.pyx":459
+    /* "pysam/cvcf.pyx":463
  *         if data == []: data = [""]             # unsure what type -- say string
  *         if type(data[0]) == type(0.0):
  *             formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"Float","(Undefined tag)",None)             # <<<<<<<<<<<<<<
  *             return
  *         if type(data[0]) == type(0):
  */
-    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_4 = PyTuple_New(6); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(6); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_INCREF(__pyx_v_key);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_key);
@@ -9437,14 +9510,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
     PyTuple_SET_ITEM(__pyx_t_4, 5, Py_None);
     __Pyx_GIVEREF(Py_None);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    if (unlikely(PyObject_SetItem(__pyx_v_formatdict, __pyx_v_key, __pyx_t_3) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyObject_SetItem(__pyx_v_formatdict, __pyx_v_key, __pyx_t_3) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 463; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-    /* "pysam/cvcf.pyx":460
+    /* "pysam/cvcf.pyx":464
  *         if type(data[0]) == type(0.0):
  *             formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"Float","(Undefined tag)",None)
  *             return             # <<<<<<<<<<<<<<
@@ -9456,33 +9529,33 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":461
+  /* "pysam/cvcf.pyx":465
  *             formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"Float","(Undefined tag)",None)
  *             return
  *         if type(data[0]) == type(0):             # <<<<<<<<<<<<<<
  *             formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"Integer","(Undefined tag)",None)
  *             return
  */
-  __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_data, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_data, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 465; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_t_3)), ((PyObject *)Py_TYPE(__pyx_int_0)), Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_t_3)), ((PyObject *)Py_TYPE(__pyx_int_0)), Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 465; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 461; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 465; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   if (__pyx_t_2) {
 
-    /* "pysam/cvcf.pyx":462
+    /* "pysam/cvcf.pyx":466
  *             return
  *         if type(data[0]) == type(0):
  *             formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"Integer","(Undefined tag)",None)             # <<<<<<<<<<<<<<
  *             return
  *         formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"String","(Undefined tag)",".")
  */
-    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_5 = PyTuple_New(6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_INCREF(__pyx_v_key);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_key);
@@ -9502,14 +9575,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
     PyTuple_SET_ITEM(__pyx_t_5, 5, Py_None);
     __Pyx_GIVEREF(Py_None);
     __pyx_t_3 = 0;
-    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_5, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_5, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    if (unlikely(PyObject_SetItem(__pyx_v_formatdict, __pyx_v_key, __pyx_t_3) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyObject_SetItem(__pyx_v_formatdict, __pyx_v_key, __pyx_t_3) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 466; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-    /* "pysam/cvcf.pyx":463
+    /* "pysam/cvcf.pyx":467
  *         if type(data[0]) == type(0):
  *             formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"Integer","(Undefined tag)",None)
  *             return             # <<<<<<<<<<<<<<
@@ -9521,18 +9594,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":464
+  /* "pysam/cvcf.pyx":468
  *             formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"Integer","(Undefined tag)",None)
  *             return
  *         formatdict[key] = FORMAT(key,self.NT_UNKNOWN,-1,"String","(Undefined tag)",".")             # <<<<<<<<<<<<<<
  * 
  * 
  */
-  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 464; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 464; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_4 = PyTuple_New(6); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 464; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyTuple_New(6); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_INCREF(__pyx_v_key);
   PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_key);
@@ -9552,14 +9625,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
   PyTuple_SET_ITEM(__pyx_t_4, 5, __pyx_kp_s__8);
   __Pyx_GIVEREF(__pyx_kp_s__8);
   __pyx_t_5 = 0;
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 464; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (unlikely(PyObject_SetItem(__pyx_v_formatdict, __pyx_v_key, __pyx_t_5) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 464; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(PyObject_SetItem(__pyx_v_formatdict, __pyx_v_key, __pyx_t_5) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":451
+  /* "pysam/cvcf.pyx":455
  * 
  * 
  *     def _add_definition(self, formatdict, key, data, line ):             # <<<<<<<<<<<<<<
@@ -9583,7 +9656,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_10_add_definition(CYTHON_UNUSED PyOb
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":468
+/* "pysam/cvcf.pyx":472
  * 
  *     # todo: trim trailing missing values
  *     def format_formatdata( self, data, format, key=True, value=True, separator=":" ):             # <<<<<<<<<<<<<<
@@ -9634,12 +9707,12 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_13format_formatdata(PyObject *__pyx_
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_data)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("format_formatdata", 0, 3, 6, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("format_formatdata", 0, 3, 6, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_format)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("format_formatdata", 0, 3, 6, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("format_formatdata", 0, 3, 6, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  3:
         if (kw_args > 0) {
@@ -9658,7 +9731,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_13format_formatdata(PyObject *__pyx_
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "format_formatdata") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "format_formatdata") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -9681,7 +9754,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_13format_formatdata(PyObject *__pyx_
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("format_formatdata", 0, 3, 6, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("format_formatdata", 0, 3, 6, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.format_formatdata", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -9724,50 +9797,50 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
   __Pyx_RefNannySetupContext("format_formatdata", 0);
   __Pyx_INCREF(__pyx_v_data);
 
-  /* "pysam/cvcf.pyx":469
+  /* "pysam/cvcf.pyx":473
  *     # todo: trim trailing missing values
  *     def format_formatdata( self, data, format, key=True, value=True, separator=":" ):
  *         output, sdata = [], []             # <<<<<<<<<<<<<<
  *         if type(data) == type([]): # for FORMAT field, make data with dummy values
  *             d = {}
  */
-  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 469; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 473; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 469; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 473; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_output = ((PyObject*)__pyx_t_1);
   __pyx_t_1 = 0;
   __pyx_v_sdata = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":470
+  /* "pysam/cvcf.pyx":474
  *     def format_formatdata( self, data, format, key=True, value=True, separator=":" ):
  *         output, sdata = [], []
  *         if type(data) == type([]): # for FORMAT field, make data with dummy values             # <<<<<<<<<<<<<<
  *             d = {}
  *             for k in data: d[k] = []
  */
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 470; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 474; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_v_data)), ((PyObject *)Py_TYPE(__pyx_t_2)), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 470; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(((PyObject *)Py_TYPE(__pyx_v_data)), ((PyObject *)Py_TYPE(__pyx_t_2)), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 474; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 470; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 474; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_3) {
 
-    /* "pysam/cvcf.pyx":471
+    /* "pysam/cvcf.pyx":475
  *         output, sdata = [], []
  *         if type(data) == type([]): # for FORMAT field, make data with dummy values
  *             d = {}             # <<<<<<<<<<<<<<
  *             for k in data: d[k] = []
  *             data = d
  */
-    __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_v_d = ((PyObject*)__pyx_t_1);
     __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":472
+    /* "pysam/cvcf.pyx":476
  *         if type(data) == type([]): # for FORMAT field, make data with dummy values
  *             d = {}
  *             for k in data: d[k] = []             # <<<<<<<<<<<<<<
@@ -9778,7 +9851,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       __pyx_t_1 = __pyx_v_data; __Pyx_INCREF(__pyx_t_1); __pyx_t_4 = 0;
       __pyx_t_5 = NULL;
     } else {
-      __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __pyx_t_5 = Py_TYPE(__pyx_t_1)->tp_iternext;
     }
@@ -9786,16 +9859,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_1)) {
         if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_1)) {
         if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_2 = __pyx_t_5(__pyx_t_1);
@@ -9803,7 +9876,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -9811,14 +9884,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       }
       __Pyx_XDECREF_SET(__pyx_v_k, __pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      if (unlikely(PyDict_SetItem(__pyx_v_d, __pyx_v_k, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyDict_SetItem(__pyx_v_d, __pyx_v_k, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     }
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":473
+    /* "pysam/cvcf.pyx":477
  *             d = {}
  *             for k in data: d[k] = []
  *             data = d             # <<<<<<<<<<<<<<
@@ -9831,7 +9904,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":475
+  /* "pysam/cvcf.pyx":479
  *             data = d
  *         # convert missing values; and silently add definitions if required
  *         for k in data:             # <<<<<<<<<<<<<<
@@ -9842,7 +9915,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     __pyx_t_1 = __pyx_v_data; __Pyx_INCREF(__pyx_t_1); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_t_5 = Py_TYPE(__pyx_t_1)->tp_iternext;
   }
@@ -9850,16 +9923,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_1)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_1)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_2 = __pyx_t_5(__pyx_t_1);
@@ -9867,7 +9940,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 475; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 479; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -9876,18 +9949,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     __Pyx_XDECREF_SET(__pyx_v_k, __pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":476
+    /* "pysam/cvcf.pyx":480
  *         # convert missing values; and silently add definitions if required
  *         for k in data:
  *             self._add_definition( format, k, data[k], "(output)" )             # <<<<<<<<<<<<<<
  *             for idx,v in enumerate(data[k]):
  *                 if v == format[k].missingvalue: data[k][idx] = "."
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_add_definition); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_add_definition); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_6 = PyObject_GetItem(__pyx_v_data, __pyx_v_k); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_6 = PyObject_GetItem(__pyx_v_data, __pyx_v_k); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_7 = PyTuple_New(4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyTuple_New(4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_INCREF(__pyx_v_format);
     PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_format);
@@ -9901,13 +9974,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     PyTuple_SET_ITEM(__pyx_t_7, 3, __pyx_kp_s_output);
     __Pyx_GIVEREF(__pyx_kp_s_output);
     __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_7, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 476; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_7, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-    /* "pysam/cvcf.pyx":477
+    /* "pysam/cvcf.pyx":481
  *         for k in data:
  *             self._add_definition( format, k, data[k], "(output)" )
  *             for idx,v in enumerate(data[k]):             # <<<<<<<<<<<<<<
@@ -9916,13 +9989,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
  */
     __Pyx_INCREF(__pyx_int_0);
     __pyx_t_6 = __pyx_int_0;
-    __pyx_t_7 = PyObject_GetItem(__pyx_v_data, __pyx_v_k); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_7 = PyObject_GetItem(__pyx_v_data, __pyx_v_k); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_7);
     if (PyList_CheckExact(__pyx_t_7) || PyTuple_CheckExact(__pyx_t_7)) {
       __pyx_t_2 = __pyx_t_7; __Pyx_INCREF(__pyx_t_2); __pyx_t_8 = 0;
       __pyx_t_9 = NULL;
     } else {
-      __pyx_t_8 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __pyx_t_9 = Py_TYPE(__pyx_t_2)->tp_iternext;
     }
@@ -9931,16 +10004,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       if (!__pyx_t_9 && PyList_CheckExact(__pyx_t_2)) {
         if (__pyx_t_8 >= PyList_GET_SIZE(__pyx_t_2)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_7 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_8); __Pyx_INCREF(__pyx_t_7); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_8); __Pyx_INCREF(__pyx_t_7); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_9 && PyTuple_CheckExact(__pyx_t_2)) {
         if (__pyx_t_8 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_8); __Pyx_INCREF(__pyx_t_7); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_8); __Pyx_INCREF(__pyx_t_7); __pyx_t_8++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PySequence_ITEM(__pyx_t_2, __pyx_t_8); __pyx_t_8++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_7 = __pyx_t_9(__pyx_t_2);
@@ -9948,7 +10021,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -9958,32 +10031,32 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       __pyx_t_7 = 0;
       __Pyx_INCREF(__pyx_t_6);
       __Pyx_XDECREF_SET(__pyx_v_idx, __pyx_t_6);
-      __pyx_t_7 = PyNumber_Add(__pyx_t_6, __pyx_int_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyNumber_Add(__pyx_t_6, __pyx_int_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_6);
       __pyx_t_6 = __pyx_t_7;
       __pyx_t_7 = 0;
 
-      /* "pysam/cvcf.pyx":478
+      /* "pysam/cvcf.pyx":482
  *             self._add_definition( format, k, data[k], "(output)" )
  *             for idx,v in enumerate(data[k]):
  *                 if v == format[k].missingvalue: data[k][idx] = "."             # <<<<<<<<<<<<<<
  *         # make sure GT comes first; and ensure fixed ordering; also convert GT data back to string
  *         for k in data:
  */
-      __pyx_t_7 = PyObject_GetItem(__pyx_v_format, __pyx_v_k); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_7 = PyObject_GetItem(__pyx_v_format, __pyx_v_k); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_t_7, __pyx_n_s_missingvalue); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_t_7, __pyx_n_s_missingvalue); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = PyObject_RichCompare(__pyx_v_v, __pyx_t_10, Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_RichCompare(__pyx_v_v, __pyx_t_10, Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       if (__pyx_t_3) {
-        __pyx_t_7 = PyObject_GetItem(__pyx_v_data, __pyx_v_k); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_7 = PyObject_GetItem(__pyx_v_data, __pyx_v_k); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_7);
-        if (unlikely(PyObject_SetItem(__pyx_t_7, __pyx_v_idx, __pyx_kp_s__8) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 478; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(PyObject_SetItem(__pyx_t_7, __pyx_v_idx, __pyx_kp_s__8) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         goto __pyx_L10;
       }
@@ -9994,7 +10067,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
   }
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":480
+  /* "pysam/cvcf.pyx":484
  *                 if v == format[k].missingvalue: data[k][idx] = "."
  *         # make sure GT comes first; and ensure fixed ordering; also convert GT data back to string
  *         for k in data:             # <<<<<<<<<<<<<<
@@ -10005,7 +10078,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     __pyx_t_1 = __pyx_v_data; __Pyx_INCREF(__pyx_t_1); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_t_5 = Py_TYPE(__pyx_t_1)->tp_iternext;
   }
@@ -10013,16 +10086,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_1)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_6 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_6); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_6); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_6 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_1)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_6); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_6); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_6 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_6 = __pyx_t_5(__pyx_t_1);
@@ -10030,7 +10103,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -10039,18 +10112,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     __Pyx_XDECREF_SET(__pyx_v_k, __pyx_t_6);
     __pyx_t_6 = 0;
 
-    /* "pysam/cvcf.pyx":481
+    /* "pysam/cvcf.pyx":485
  *         # make sure GT comes first; and ensure fixed ordering; also convert GT data back to string
  *         for k in data:
  *             if k != 'GT': sdata.append( (k,data[k]) )             # <<<<<<<<<<<<<<
  *         sdata.sort()
  *         if 'GT' in data:
  */
-    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_k, __pyx_n_s_GT, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_k, __pyx_n_s_GT, Py_NE)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_3) {
-      __pyx_t_6 = PyObject_GetItem(__pyx_v_data, __pyx_v_k); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_6 = PyObject_GetItem(__pyx_v_data, __pyx_v_k); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_INCREF(__pyx_v_k);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_k);
@@ -10058,7 +10131,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_t_6);
       __Pyx_GIVEREF(__pyx_t_6);
       __pyx_t_6 = 0;
-      __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_sdata, __pyx_t_2); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_sdata, __pyx_t_2); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       goto __pyx_L13;
     }
@@ -10066,38 +10139,38 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
   }
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":482
+  /* "pysam/cvcf.pyx":486
  *         for k in data:
  *             if k != 'GT': sdata.append( (k,data[k]) )
  *         sdata.sort()             # <<<<<<<<<<<<<<
  *         if 'GT' in data:
  *             sdata = [('GT',map(self.convertGTback,data['GT']))] + sdata
  */
-  __pyx_t_11 = PyList_Sort(__pyx_v_sdata); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = PyList_Sort(__pyx_v_sdata); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 486; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":483
+  /* "pysam/cvcf.pyx":487
  *             if k != 'GT': sdata.append( (k,data[k]) )
  *         sdata.sort()
  *         if 'GT' in data:             # <<<<<<<<<<<<<<
  *             sdata = [('GT',map(self.convertGTback,data['GT']))] + sdata
  *         for k,v in sdata:
  */
-  __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_n_s_GT, __pyx_v_data, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PySequence_Contains(__pyx_n_s_GT, __pyx_v_data, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_12 = (__pyx_t_3 != 0);
   if (__pyx_t_12) {
 
-    /* "pysam/cvcf.pyx":484
+    /* "pysam/cvcf.pyx":488
  *         sdata.sort()
  *         if 'GT' in data:
  *             sdata = [('GT',map(self.convertGTback,data['GT']))] + sdata             # <<<<<<<<<<<<<<
  *         for k,v in sdata:
  *             if v == []: v = None
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_convertGTback); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_convertGTback); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_GT); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_GT); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
@@ -10105,10 +10178,10 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_1 = 0;
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_6, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_INCREF(__pyx_n_s_GT);
     PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_n_s_GT);
@@ -10116,12 +10189,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_2 = 0;
-    __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyList_SET_ITEM(__pyx_t_2, 0, __pyx_t_6);
     __Pyx_GIVEREF(__pyx_t_6);
     __pyx_t_6 = 0;
-    __pyx_t_6 = PyNumber_Add(__pyx_t_2, __pyx_v_sdata); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = PyNumber_Add(__pyx_t_2, __pyx_v_sdata); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF_SET(__pyx_v_sdata, ((PyObject*)__pyx_t_6));
@@ -10130,7 +10203,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
   }
   __pyx_L14:;
 
-  /* "pysam/cvcf.pyx":485
+  /* "pysam/cvcf.pyx":489
  *         if 'GT' in data:
  *             sdata = [('GT',map(self.convertGTback,data['GT']))] + sdata
  *         for k,v in sdata:             # <<<<<<<<<<<<<<
@@ -10141,9 +10214,9 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
   for (;;) {
     if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_6)) break;
     #if CYTHON_COMPILING_IN_CPYTHON
-    __pyx_t_2 = PyList_GET_ITEM(__pyx_t_6, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_GET_ITEM(__pyx_t_6, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     #else
-    __pyx_t_2 = PySequence_ITEM(__pyx_t_6, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PySequence_ITEM(__pyx_t_6, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     #endif
     if ((likely(PyTuple_CheckExact(__pyx_t_2))) || (PyList_CheckExact(__pyx_t_2))) {
       PyObject* sequence = __pyx_t_2;
@@ -10155,7 +10228,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       if (unlikely(size != 2)) {
         if (size > 2) __Pyx_RaiseTooManyValuesError(2);
         else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       #if CYTHON_COMPILING_IN_CPYTHON
       if (likely(PyTuple_CheckExact(sequence))) {
@@ -10168,15 +10241,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       __Pyx_INCREF(__pyx_t_1);
       __Pyx_INCREF(__pyx_t_7);
       #else
-      __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_7 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       #endif
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     } else {
       Py_ssize_t index = -1;
-      __pyx_t_10 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __pyx_t_13 = Py_TYPE(__pyx_t_10)->tp_iternext;
@@ -10184,7 +10257,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       __Pyx_GOTREF(__pyx_t_1);
       index = 1; __pyx_t_7 = __pyx_t_13(__pyx_t_10); if (unlikely(!__pyx_t_7)) goto __pyx_L17_unpacking_failed;
       __Pyx_GOTREF(__pyx_t_7);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_13(__pyx_t_10), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (__Pyx_IternextUnpackEndCheck(__pyx_t_13(__pyx_t_10), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_13 = NULL;
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
       goto __pyx_L18_unpacking_done;
@@ -10192,7 +10265,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
       __pyx_t_13 = NULL;
       if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_L18_unpacking_done:;
     }
     __Pyx_XDECREF_SET(__pyx_v_k, __pyx_t_1);
@@ -10200,18 +10273,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     __Pyx_XDECREF_SET(__pyx_v_v, __pyx_t_7);
     __pyx_t_7 = 0;
 
-    /* "pysam/cvcf.pyx":486
+    /* "pysam/cvcf.pyx":490
  *             sdata = [('GT',map(self.convertGTback,data['GT']))] + sdata
  *         for k,v in sdata:
  *             if v == []: v = None             # <<<<<<<<<<<<<<
  *             if key and value:
  *                 if v != None: output.append( k+"="+','.join(map(str,v)) )
  */
-    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 486; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_7 = PyObject_RichCompare(__pyx_v_v, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 486; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyObject_RichCompare(__pyx_v_v, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_12 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 486; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_12 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     if (__pyx_t_12) {
       __Pyx_INCREF(Py_None);
@@ -10220,36 +10293,36 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
     }
     __pyx_L19:;
 
-    /* "pysam/cvcf.pyx":487
+    /* "pysam/cvcf.pyx":491
  *         for k,v in sdata:
  *             if v == []: v = None
  *             if key and value:             # <<<<<<<<<<<<<<
  *                 if v != None: output.append( k+"="+','.join(map(str,v)) )
  *                 else: output.append( k )
  */
-    __pyx_t_12 = __Pyx_PyObject_IsTrue(__pyx_v_key); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_12 = __Pyx_PyObject_IsTrue(__pyx_v_key); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_12) {
-      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_value); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_value); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_14 = __pyx_t_3;
     } else {
       __pyx_t_14 = __pyx_t_12;
     }
     if (__pyx_t_14) {
 
-      /* "pysam/cvcf.pyx":488
+      /* "pysam/cvcf.pyx":492
  *             if v == []: v = None
  *             if key and value:
  *                 if v != None: output.append( k+"="+','.join(map(str,v)) )             # <<<<<<<<<<<<<<
  *                 else: output.append( k )
  *             elif key: output.append(k)
  */
-      __pyx_t_7 = PyObject_RichCompare(__pyx_v_v, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_RichCompare(__pyx_v_v, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       if (__pyx_t_14) {
-        __pyx_t_7 = PyNumber_Add(__pyx_v_k, __pyx_kp_s__13); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = PyNumber_Add(__pyx_v_k, __pyx_kp_s__13); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
         PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
@@ -10257,70 +10330,70 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
         __Pyx_INCREF(__pyx_v_v);
         PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_v);
         __Pyx_GIVEREF(__pyx_v_v);
-        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_1 = PyNumber_Add(__pyx_t_7, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyNumber_Add(__pyx_t_7, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_t_1); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_t_1); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         goto __pyx_L21;
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":489
+        /* "pysam/cvcf.pyx":493
  *             if key and value:
  *                 if v != None: output.append( k+"="+','.join(map(str,v)) )
  *                 else: output.append( k )             # <<<<<<<<<<<<<<
  *             elif key: output.append(k)
  *             elif value:
  */
-        __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_v_k); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 489; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_v_k); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 493; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       __pyx_L21:;
       goto __pyx_L20;
     }
 
-    /* "pysam/cvcf.pyx":490
+    /* "pysam/cvcf.pyx":494
  *                 if v != None: output.append( k+"="+','.join(map(str,v)) )
  *                 else: output.append( k )
  *             elif key: output.append(k)             # <<<<<<<<<<<<<<
  *             elif value:
  *                 if v != None: output.append( ','.join(map(str,v)) )
  */
-    __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_key); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_key); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 494; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_14) {
-      __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_v_k); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_v_k); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 494; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       goto __pyx_L20;
     }
 
-    /* "pysam/cvcf.pyx":491
+    /* "pysam/cvcf.pyx":495
  *                 else: output.append( k )
  *             elif key: output.append(k)
  *             elif value:             # <<<<<<<<<<<<<<
  *                 if v != None: output.append( ','.join(map(str,v)) )
  *                 else: output.append( "." )                    # should not happen
  */
-    __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_value); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 491; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_v_value); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_14) {
 
-      /* "pysam/cvcf.pyx":492
+      /* "pysam/cvcf.pyx":496
  *             elif key: output.append(k)
  *             elif value:
  *                 if v != None: output.append( ','.join(map(str,v)) )             # <<<<<<<<<<<<<<
  *                 else: output.append( "." )                    # should not happen
  *         # snip off trailing missing data
  */
-      __pyx_t_1 = PyObject_RichCompare(__pyx_v_v, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyObject_RichCompare(__pyx_v_v, Py_None, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       if (__pyx_t_14) {
-        __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
         PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
@@ -10328,26 +10401,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
         __Pyx_INCREF(__pyx_v_v);
         PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_v);
         __Pyx_GIVEREF(__pyx_v_v);
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-        __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_t_1); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_t_1); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         goto __pyx_L22;
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":493
+        /* "pysam/cvcf.pyx":497
  *             elif value:
  *                 if v != None: output.append( ','.join(map(str,v)) )
  *                 else: output.append( "." )                    # should not happen             # <<<<<<<<<<<<<<
  *         # snip off trailing missing data
  *         while len(output) > 1:
  */
-        __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_kp_s__8); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 493; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_PyList_Append(__pyx_v_output, __pyx_kp_s__8); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       __pyx_L22:;
       goto __pyx_L20;
@@ -10356,7 +10429,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
   }
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-  /* "pysam/cvcf.pyx":495
+  /* "pysam/cvcf.pyx":499
  *                 else: output.append( "." )                    # should not happen
  *         # snip off trailing missing data
  *         while len(output) > 1:             # <<<<<<<<<<<<<<
@@ -10364,62 +10437,62 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
  *             if len(last)>0: break
  */
   while (1) {
-    __pyx_t_4 = PyList_GET_SIZE(__pyx_v_output); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyList_GET_SIZE(__pyx_v_output); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_14 = ((__pyx_t_4 > 1) != 0);
     if (!__pyx_t_14) break;
 
-    /* "pysam/cvcf.pyx":496
+    /* "pysam/cvcf.pyx":500
  *         # snip off trailing missing data
  *         while len(output) > 1:
  *             last = output[-1].replace(',','').replace('.','')             # <<<<<<<<<<<<<<
  *             if len(last)>0: break
  *             output = output[:-1]
  */
-    __pyx_t_6 = __Pyx_GetItemInt_List(__pyx_v_output, -1, long, 1, __Pyx_PyInt_From_long, 1, 1, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_6 = __Pyx_GetItemInt_List(__pyx_v_output, -1, long, 1, __Pyx_PyInt_From_long, 1, 1, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_6);
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_replace); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_replace); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__44, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__44, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_replace); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s_replace); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__45, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__45, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_XDECREF_SET(__pyx_v_last, __pyx_t_6);
     __pyx_t_6 = 0;
 
-    /* "pysam/cvcf.pyx":497
+    /* "pysam/cvcf.pyx":501
  *         while len(output) > 1:
  *             last = output[-1].replace(',','').replace('.','')
  *             if len(last)>0: break             # <<<<<<<<<<<<<<
  *             output = output[:-1]
  *         return separator.join(output)
  */
-    __pyx_t_4 = PyObject_Length(__pyx_v_last); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyObject_Length(__pyx_v_last); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_14 = ((__pyx_t_4 > 0) != 0);
     if (__pyx_t_14) {
       goto __pyx_L24_break;
     }
 
-    /* "pysam/cvcf.pyx":498
+    /* "pysam/cvcf.pyx":502
  *             last = output[-1].replace(',','').replace('.','')
  *             if len(last)>0: break
  *             output = output[:-1]             # <<<<<<<<<<<<<<
  *         return separator.join(output)
  * 
  */
-    __pyx_t_6 = __Pyx_PyList_GetSlice(__pyx_v_output, 0, -1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyList_GetSlice(__pyx_v_output, 0, -1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF_SET(__pyx_v_output, ((PyObject*)__pyx_t_6));
     __pyx_t_6 = 0;
   }
   __pyx_L24_break:;
 
-  /* "pysam/cvcf.pyx":499
+  /* "pysam/cvcf.pyx":503
  *             if len(last)>0: break
  *             output = output[:-1]
  *         return separator.join(output)             # <<<<<<<<<<<<<<
@@ -10427,14 +10500,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
  * 
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_separator, __pyx_n_s_join); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_separator, __pyx_n_s_join); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_v_output);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_output);
   __Pyx_GIVEREF(__pyx_v_output);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -10442,7 +10515,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
   __pyx_t_2 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":468
+  /* "pysam/cvcf.pyx":472
  * 
  *     # todo: trim trailing missing values
  *     def format_formatdata( self, data, format, key=True, value=True, separator=":" ):             # <<<<<<<<<<<<<<
@@ -10473,7 +10546,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_12format_formatdata(CYTHON_UNUSED Py
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":502
+/* "pysam/cvcf.pyx":506
  * 
  * 
  *     def enter_default_format(self):             # <<<<<<<<<<<<<<
@@ -10523,18 +10596,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("enter_default_format", 0);
 
-  /* "pysam/cvcf.pyx":503
+  /* "pysam/cvcf.pyx":507
  * 
  *     def enter_default_format(self):
  *         for f in [FORMAT('GT',self.NT_NUMBER,1,'String','Genotype','.'),             # <<<<<<<<<<<<<<
  *                   FORMAT('DP',self.NT_NUMBER,1,'Integer','Read depth at this position for this sample',-1),
  *                   FORMAT('FT',self.NT_NUMBER,1,'String','Sample Genotype Filter','.'),
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(6); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_n_s_GT);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_n_s_GT);
@@ -10554,23 +10627,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_3, 5, __pyx_kp_s__8);
   __Pyx_GIVEREF(__pyx_kp_s__8);
   __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":504
+  /* "pysam/cvcf.pyx":508
  *     def enter_default_format(self):
  *         for f in [FORMAT('GT',self.NT_NUMBER,1,'String','Genotype','.'),
  *                   FORMAT('DP',self.NT_NUMBER,1,'Integer','Read depth at this position for this sample',-1),             # <<<<<<<<<<<<<<
  *                   FORMAT('FT',self.NT_NUMBER,1,'String','Sample Genotype Filter','.'),
  *                   FORMAT('GL',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),
  */
-  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_4 = PyTuple_New(6); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyTuple_New(6); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_INCREF(__pyx_n_s_DP);
   PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_n_s_DP);
@@ -10590,23 +10663,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_4, 5, __pyx_int_neg_1);
   __Pyx_GIVEREF(__pyx_int_neg_1);
   __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/cvcf.pyx":505
+  /* "pysam/cvcf.pyx":509
  *         for f in [FORMAT('GT',self.NT_NUMBER,1,'String','Genotype','.'),
  *                   FORMAT('DP',self.NT_NUMBER,1,'Integer','Read depth at this position for this sample',-1),
  *                   FORMAT('FT',self.NT_NUMBER,1,'String','Sample Genotype Filter','.'),             # <<<<<<<<<<<<<<
  *                   FORMAT('GL',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),
  *                   FORMAT('GLE',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),
  */
-  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_5 = PyTuple_New(6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyTuple_New(6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_INCREF(__pyx_n_s_FT);
   PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_n_s_FT);
@@ -10626,23 +10699,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_5, 5, __pyx_kp_s__8);
   __Pyx_GIVEREF(__pyx_kp_s__8);
   __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_5, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_5, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":506
+  /* "pysam/cvcf.pyx":510
  *                   FORMAT('DP',self.NT_NUMBER,1,'Integer','Read depth at this position for this sample',-1),
  *                   FORMAT('FT',self.NT_NUMBER,1,'String','Sample Genotype Filter','.'),
  *                   FORMAT('GL',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),             # <<<<<<<<<<<<<<
  *                   FORMAT('GLE',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),
  *                   FORMAT('GQ',self.NT_NUMBER,1,'Integer','Genotype Quality',-1),
  */
-  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
-  __pyx_t_6 = PyTuple_New(6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = PyTuple_New(6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_INCREF(__pyx_n_s_GL);
   PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_n_s_GL);
@@ -10662,23 +10735,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_6, 5, __pyx_kp_s__8);
   __Pyx_GIVEREF(__pyx_kp_s__8);
   __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_6, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_6, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-  /* "pysam/cvcf.pyx":507
+  /* "pysam/cvcf.pyx":511
  *                   FORMAT('FT',self.NT_NUMBER,1,'String','Sample Genotype Filter','.'),
  *                   FORMAT('GL',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),
  *                   FORMAT('GLE',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),             # <<<<<<<<<<<<<<
  *                   FORMAT('GQ',self.NT_NUMBER,1,'Integer','Genotype Quality',-1),
  *                   FORMAT('PL',self.NT_GENOTYPES,-1,'Integer','Phred-scaled genotype likelihoods', '.'),
  */
-  __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __pyx_t_7 = PyTuple_New(6); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = PyTuple_New(6); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
   __Pyx_INCREF(__pyx_n_s_GLE);
   PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_n_s_GLE);
@@ -10698,23 +10771,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_7, 5, __pyx_kp_s__8);
   __Pyx_GIVEREF(__pyx_kp_s__8);
   __pyx_t_5 = 0;
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_7, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_7, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
   __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
 
-  /* "pysam/cvcf.pyx":508
+  /* "pysam/cvcf.pyx":512
  *                   FORMAT('GL',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),
  *                   FORMAT('GLE',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),
  *                   FORMAT('GQ',self.NT_NUMBER,1,'Integer','Genotype Quality',-1),             # <<<<<<<<<<<<<<
  *                   FORMAT('PL',self.NT_GENOTYPES,-1,'Integer','Phred-scaled genotype likelihoods', '.'),
  *                   FORMAT('GP',self.NT_GENOTYPES,-1,'Float','Genotype posterior probabilities','.'),
  */
-  __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
-  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
-  __pyx_t_8 = PyTuple_New(6); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = PyTuple_New(6); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
   __Pyx_INCREF(__pyx_n_s_GQ);
   PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_n_s_GQ);
@@ -10734,23 +10807,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_8, 5, __pyx_int_neg_1);
   __Pyx_GIVEREF(__pyx_int_neg_1);
   __pyx_t_6 = 0;
-  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_8, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_8, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_6);
   __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
   __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
 
-  /* "pysam/cvcf.pyx":509
+  /* "pysam/cvcf.pyx":513
  *                   FORMAT('GLE',self.NT_UNKNOWN,-1,'Float','Genotype likelihoods','.'),
  *                   FORMAT('GQ',self.NT_NUMBER,1,'Integer','Genotype Quality',-1),
  *                   FORMAT('PL',self.NT_GENOTYPES,-1,'Integer','Phred-scaled genotype likelihoods', '.'),             # <<<<<<<<<<<<<<
  *                   FORMAT('GP',self.NT_GENOTYPES,-1,'Float','Genotype posterior probabilities','.'),
  *                   FORMAT('GQ',self.NT_GENOTYPES,-1,'Integer','Conditional genotype quality','.'),
  */
-  __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
-  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
-  __pyx_t_9 = PyTuple_New(6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = PyTuple_New(6); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
   __Pyx_INCREF(__pyx_n_s_PL);
   PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_n_s_PL);
@@ -10770,23 +10843,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_9, 5, __pyx_kp_s__8);
   __Pyx_GIVEREF(__pyx_kp_s__8);
   __pyx_t_7 = 0;
-  __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_9, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_9, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
   __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-  /* "pysam/cvcf.pyx":510
+  /* "pysam/cvcf.pyx":514
  *                   FORMAT('GQ',self.NT_NUMBER,1,'Integer','Genotype Quality',-1),
  *                   FORMAT('PL',self.NT_GENOTYPES,-1,'Integer','Phred-scaled genotype likelihoods', '.'),
  *                   FORMAT('GP',self.NT_GENOTYPES,-1,'Float','Genotype posterior probabilities','.'),             # <<<<<<<<<<<<<<
  *                   FORMAT('GQ',self.NT_GENOTYPES,-1,'Integer','Conditional genotype quality','.'),
  *                   FORMAT('HQ',self.NT_UNKNOWN,-1,'Integer','Haplotype Quality',-1),    # unknown number, since may be haploid
  */
-  __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
-  __pyx_t_10 = PyTuple_New(6); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = PyTuple_New(6); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   __Pyx_INCREF(__pyx_n_s_GP);
   PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_n_s_GP);
@@ -10806,23 +10879,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_10, 5, __pyx_kp_s__8);
   __Pyx_GIVEREF(__pyx_kp_s__8);
   __pyx_t_8 = 0;
-  __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_10, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_10, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
 
-  /* "pysam/cvcf.pyx":511
+  /* "pysam/cvcf.pyx":515
  *                   FORMAT('PL',self.NT_GENOTYPES,-1,'Integer','Phred-scaled genotype likelihoods', '.'),
  *                   FORMAT('GP',self.NT_GENOTYPES,-1,'Float','Genotype posterior probabilities','.'),
  *                   FORMAT('GQ',self.NT_GENOTYPES,-1,'Integer','Conditional genotype quality','.'),             # <<<<<<<<<<<<<<
  *                   FORMAT('HQ',self.NT_UNKNOWN,-1,'Integer','Haplotype Quality',-1),    # unknown number, since may be haploid
  *                   FORMAT('PS',self.NT_UNKNOWN,-1,'Integer','Phase set','.'),
  */
-  __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_GENOTYPES); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_11 = PyTuple_New(6); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = PyTuple_New(6); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
   __Pyx_INCREF(__pyx_n_s_GQ);
   PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_n_s_GQ);
@@ -10842,23 +10915,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_11, 5, __pyx_kp_s__8);
   __Pyx_GIVEREF(__pyx_kp_s__8);
   __pyx_t_9 = 0;
-  __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_11, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_11, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
 
-  /* "pysam/cvcf.pyx":512
+  /* "pysam/cvcf.pyx":516
  *                   FORMAT('GP',self.NT_GENOTYPES,-1,'Float','Genotype posterior probabilities','.'),
  *                   FORMAT('GQ',self.NT_GENOTYPES,-1,'Integer','Conditional genotype quality','.'),
  *                   FORMAT('HQ',self.NT_UNKNOWN,-1,'Integer','Haplotype Quality',-1),    # unknown number, since may be haploid             # <<<<<<<<<<<<<<
  *                   FORMAT('PS',self.NT_UNKNOWN,-1,'Integer','Phase set','.'),
  *                   FORMAT('PQ',self.NT_NUMBER,1,'Integer','Phasing quality',-1),
  */
-  __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
-  __pyx_t_12 = PyTuple_New(6); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = PyTuple_New(6); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_12);
   __Pyx_INCREF(__pyx_n_s_HQ);
   PyTuple_SET_ITEM(__pyx_t_12, 0, __pyx_n_s_HQ);
@@ -10878,23 +10951,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_12, 5, __pyx_int_neg_1);
   __Pyx_GIVEREF(__pyx_int_neg_1);
   __pyx_t_10 = 0;
-  __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_12, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 512; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_12, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
   __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
 
-  /* "pysam/cvcf.pyx":513
+  /* "pysam/cvcf.pyx":517
  *                   FORMAT('GQ',self.NT_GENOTYPES,-1,'Integer','Conditional genotype quality','.'),
  *                   FORMAT('HQ',self.NT_UNKNOWN,-1,'Integer','Haplotype Quality',-1),    # unknown number, since may be haploid
  *                   FORMAT('PS',self.NT_UNKNOWN,-1,'Integer','Phase set','.'),             # <<<<<<<<<<<<<<
  *                   FORMAT('PQ',self.NT_NUMBER,1,'Integer','Phasing quality',-1),
  *                   FORMAT('EC',self.NT_ALLELES,1,'Integer','Expected alternate allel counts',-1),
  */
-  __pyx_t_12 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_12);
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_UNKNOWN); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  __pyx_t_13 = PyTuple_New(6); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_13 = PyTuple_New(6); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_13);
   __Pyx_INCREF(__pyx_n_s_PS);
   PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_n_s_PS);
@@ -10914,23 +10987,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_13, 5, __pyx_kp_s__8);
   __Pyx_GIVEREF(__pyx_kp_s__8);
   __pyx_t_11 = 0;
-  __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_13, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 513; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_13, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
   __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
   __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
 
-  /* "pysam/cvcf.pyx":514
+  /* "pysam/cvcf.pyx":518
  *                   FORMAT('HQ',self.NT_UNKNOWN,-1,'Integer','Haplotype Quality',-1),    # unknown number, since may be haploid
  *                   FORMAT('PS',self.NT_UNKNOWN,-1,'Integer','Phase set','.'),
  *                   FORMAT('PQ',self.NT_NUMBER,1,'Integer','Phasing quality',-1),             # <<<<<<<<<<<<<<
  *                   FORMAT('EC',self.NT_ALLELES,1,'Integer','Expected alternate allel counts',-1),
  *                   FORMAT('MQ',self.NT_NUMBER,1,'Integer','RMS mapping quality',-1),
  */
-  __pyx_t_13 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_13 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_13);
-  __pyx_t_12 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_12);
-  __pyx_t_14 = PyTuple_New(6); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_14 = PyTuple_New(6); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_14);
   __Pyx_INCREF(__pyx_n_s_PQ);
   PyTuple_SET_ITEM(__pyx_t_14, 0, __pyx_n_s_PQ);
@@ -10950,23 +11023,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_14, 5, __pyx_int_neg_1);
   __Pyx_GIVEREF(__pyx_int_neg_1);
   __pyx_t_12 = 0;
-  __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_13, __pyx_t_14, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 514; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_13, __pyx_t_14, NULL); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_12);
   __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
   __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
 
-  /* "pysam/cvcf.pyx":515
+  /* "pysam/cvcf.pyx":519
  *                   FORMAT('PS',self.NT_UNKNOWN,-1,'Integer','Phase set','.'),
  *                   FORMAT('PQ',self.NT_NUMBER,1,'Integer','Phasing quality',-1),
  *                   FORMAT('EC',self.NT_ALLELES,1,'Integer','Expected alternate allel counts',-1),             # <<<<<<<<<<<<<<
  *                   FORMAT('MQ',self.NT_NUMBER,1,'Integer','RMS mapping quality',-1),
  *                   ]:
  */
-  __pyx_t_14 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_14 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_14);
-  __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_ALLELES); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_13);
-  __pyx_t_15 = PyTuple_New(6); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = PyTuple_New(6); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_15);
   __Pyx_INCREF(__pyx_n_s_EC);
   PyTuple_SET_ITEM(__pyx_t_15, 0, __pyx_n_s_EC);
@@ -10986,23 +11059,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_15, 5, __pyx_int_neg_1);
   __Pyx_GIVEREF(__pyx_int_neg_1);
   __pyx_t_13 = 0;
-  __pyx_t_13 = __Pyx_PyObject_Call(__pyx_t_14, __pyx_t_15, NULL); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 515; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_13 = __Pyx_PyObject_Call(__pyx_t_14, __pyx_t_15, NULL); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_13);
   __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
   __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;
 
-  /* "pysam/cvcf.pyx":516
+  /* "pysam/cvcf.pyx":520
  *                   FORMAT('PQ',self.NT_NUMBER,1,'Integer','Phasing quality',-1),
  *                   FORMAT('EC',self.NT_ALLELES,1,'Integer','Expected alternate allel counts',-1),
  *                   FORMAT('MQ',self.NT_NUMBER,1,'Integer','RMS mapping quality',-1),             # <<<<<<<<<<<<<<
  *                   ]:
  *             if f.id not in self._format:
  */
-  __pyx_t_15 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = __Pyx_GetModuleGlobalName(__pyx_n_s_FORMAT); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_15);
-  __pyx_t_14 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_14 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_NT_NUMBER); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_14);
-  __pyx_t_16 = PyTuple_New(6); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_16 = PyTuple_New(6); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_16);
   __Pyx_INCREF(__pyx_n_s_MQ);
   PyTuple_SET_ITEM(__pyx_t_16, 0, __pyx_n_s_MQ);
@@ -11022,19 +11095,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   PyTuple_SET_ITEM(__pyx_t_16, 5, __pyx_int_neg_1);
   __Pyx_GIVEREF(__pyx_int_neg_1);
   __pyx_t_14 = 0;
-  __pyx_t_14 = __Pyx_PyObject_Call(__pyx_t_15, __pyx_t_16, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 516; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_14 = __Pyx_PyObject_Call(__pyx_t_15, __pyx_t_16, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_14);
   __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;
   __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
 
-  /* "pysam/cvcf.pyx":503
+  /* "pysam/cvcf.pyx":507
  * 
  *     def enter_default_format(self):
  *         for f in [FORMAT('GT',self.NT_NUMBER,1,'String','Genotype','.'),             # <<<<<<<<<<<<<<
  *                   FORMAT('DP',self.NT_NUMBER,1,'Integer','Read depth at this position for this sample',-1),
  *                   FORMAT('FT',self.NT_NUMBER,1,'String','Sample Genotype Filter','.'),
  */
-  __pyx_t_16 = PyTuple_New(14); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_16 = PyTuple_New(14); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_16);
   PyTuple_SET_ITEM(__pyx_t_16, 0, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
@@ -11083,42 +11156,42 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   for (;;) {
     if (__pyx_t_17 >= 14) break;
     #if CYTHON_COMPILING_IN_CPYTHON
-    __pyx_t_16 = PyTuple_GET_ITEM(__pyx_t_14, __pyx_t_17); __Pyx_INCREF(__pyx_t_16); __pyx_t_17++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_16 = PyTuple_GET_ITEM(__pyx_t_14, __pyx_t_17); __Pyx_INCREF(__pyx_t_16); __pyx_t_17++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     #else
-    __pyx_t_16 = PySequence_ITEM(__pyx_t_14, __pyx_t_17); __pyx_t_17++; if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_16 = PySequence_ITEM(__pyx_t_14, __pyx_t_17); __pyx_t_17++; if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     #endif
     __Pyx_XDECREF_SET(__pyx_v_f, __pyx_t_16);
     __pyx_t_16 = 0;
 
-    /* "pysam/cvcf.pyx":518
+    /* "pysam/cvcf.pyx":522
  *                   FORMAT('MQ',self.NT_NUMBER,1,'Integer','RMS mapping quality',-1),
  *                   ]:
  *             if f.id not in self._format:             # <<<<<<<<<<<<<<
  *                 self._format[f.id] = f
  * 
  */
-    __pyx_t_16 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_16 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_16);
-    __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_13);
-    __pyx_t_18 = (__Pyx_PySequence_Contains(__pyx_t_16, __pyx_t_13, Py_NE)); if (unlikely(__pyx_t_18 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 518; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_18 = (__Pyx_PySequence_Contains(__pyx_t_16, __pyx_t_13, Py_NE)); if (unlikely(__pyx_t_18 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
     __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
     __pyx_t_19 = (__pyx_t_18 != 0);
     if (__pyx_t_19) {
 
-      /* "pysam/cvcf.pyx":519
+      /* "pysam/cvcf.pyx":523
  *                   ]:
  *             if f.id not in self._format:
  *                 self._format[f.id] = f             # <<<<<<<<<<<<<<
  * 
- *     def parse_header( self, line ):
+ *     def parse_header(self, line):
  */
-      __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_13);
-      __pyx_t_16 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_16 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_16);
-      if (unlikely(PyObject_SetItem(__pyx_t_13, __pyx_t_16, __pyx_v_f) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 519; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyObject_SetItem(__pyx_t_13, __pyx_t_16, __pyx_v_f) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
       __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
       goto __pyx_L5;
@@ -11127,7 +11200,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   }
   __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
 
-  /* "pysam/cvcf.pyx":502
+  /* "pysam/cvcf.pyx":506
  * 
  * 
  *     def enter_default_format(self):             # <<<<<<<<<<<<<<
@@ -11164,12 +11237,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_14enter_default_format(CYTHON_UNUSED
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":521
+/* "pysam/cvcf.pyx":525
  *                 self._format[f.id] = f
  * 
- *     def parse_header( self, line ):             # <<<<<<<<<<<<<<
+ *     def parse_header(self, line):             # <<<<<<<<<<<<<<
+ * 
  *         assert line.startswith('##')
- *         elts = line[2:].split('=')
  */
 
 /* Python wrapper */
@@ -11204,11 +11277,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_17parse_header(PyObject *__pyx_self,
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_line)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse_header", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse_header", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_header") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_header") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -11221,7 +11294,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_17parse_header(PyObject *__pyx_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("parse_header", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("parse_header", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.parse_header", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -11252,172 +11325,172 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_16parse_header(CYTHON_UNUSED PyObjec
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse_header", 0);
 
-  /* "pysam/cvcf.pyx":522
+  /* "pysam/cvcf.pyx":527
+ *     def parse_header(self, line):
  * 
- *     def parse_header( self, line ):
  *         assert line.startswith('##')             # <<<<<<<<<<<<<<
  *         elts = line[2:].split('=')
  *         key = elts[0].strip()
  */
   #ifndef CYTHON_WITHOUT_ASSERTIONS
   if (unlikely(!Py_OptimizeFlag)) {
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__47, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__47, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (unlikely(!__pyx_t_3)) {
       PyErr_SetNone(PyExc_AssertionError);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
   #endif
 
-  /* "pysam/cvcf.pyx":523
- *     def parse_header( self, line ):
+  /* "pysam/cvcf.pyx":528
+ * 
  *         assert line.startswith('##')
  *         elts = line[2:].split('=')             # <<<<<<<<<<<<<<
  *         key = elts[0].strip()
  *         value = '='.join(elts[1:]).strip()
  */
-  __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_line, 2, 0, NULL, NULL, &__pyx_slice__48, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_line, 2, 0, NULL, NULL, &__pyx_slice__48, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__49, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__49, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_elts = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":524
+  /* "pysam/cvcf.pyx":529
  *         assert line.startswith('##')
  *         elts = line[2:].split('=')
  *         key = elts[0].strip()             # <<<<<<<<<<<<<<
  *         value = '='.join(elts[1:]).strip()
  *         if key == "fileformat":
  */
-  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 529; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_strip); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_strip); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 529; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 529; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_key = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":525
+  /* "pysam/cvcf.pyx":530
  *         elts = line[2:].split('=')
  *         key = elts[0].strip()
  *         value = '='.join(elts[1:]).strip()             # <<<<<<<<<<<<<<
  *         if key == "fileformat":
  *             if value == "VCFv3.3":
  */
-  __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_elts, 1, 0, NULL, NULL, &__pyx_slice__50, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_elts, 1, 0, NULL, NULL, &__pyx_slice__50, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s__13, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s__13, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_v_value = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":526
+  /* "pysam/cvcf.pyx":531
  *         key = elts[0].strip()
  *         value = '='.join(elts[1:]).strip()
  *         if key == "fileformat":             # <<<<<<<<<<<<<<
  *             if value == "VCFv3.3":
  *                 self._version = 33
  */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_fileformat, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 526; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_fileformat, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_3) {
 
-    /* "pysam/cvcf.pyx":527
+    /* "pysam/cvcf.pyx":532
  *         value = '='.join(elts[1:]).strip()
  *         if key == "fileformat":
  *             if value == "VCFv3.3":             # <<<<<<<<<<<<<<
  *                 self._version = 33
  *             elif value == "VCFv4.0":
  */
-    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_value, __pyx_kp_s_VCFv3_3, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_value, __pyx_kp_s_VCFv3_3, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 532; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_3) {
 
-      /* "pysam/cvcf.pyx":528
+      /* "pysam/cvcf.pyx":533
  *         if key == "fileformat":
  *             if value == "VCFv3.3":
  *                 self._version = 33             # <<<<<<<<<<<<<<
  *             elif value == "VCFv4.0":
  *                 self._version = 40
  */
-      if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_int_33) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_int_33) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       goto __pyx_L4;
     }
 
-    /* "pysam/cvcf.pyx":529
+    /* "pysam/cvcf.pyx":534
  *             if value == "VCFv3.3":
  *                 self._version = 33
  *             elif value == "VCFv4.0":             # <<<<<<<<<<<<<<
  *                 self._version = 40
  *             elif value == "VCFv4.1":
  */
-    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_value, __pyx_kp_s_VCFv4_0, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 529; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_value, __pyx_kp_s_VCFv4_0, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 534; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_3) {
 
-      /* "pysam/cvcf.pyx":530
+      /* "pysam/cvcf.pyx":535
  *                 self._version = 33
  *             elif value == "VCFv4.0":
  *                 self._version = 40             # <<<<<<<<<<<<<<
  *             elif value == "VCFv4.1":
  *                 # AH - for testing
  */
-      if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_int_40) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_int_40) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       goto __pyx_L4;
     }
 
-    /* "pysam/cvcf.pyx":531
+    /* "pysam/cvcf.pyx":536
  *             elif value == "VCFv4.0":
  *                 self._version = 40
  *             elif value == "VCFv4.1":             # <<<<<<<<<<<<<<
  *                 # AH - for testing
  *                 self._version = 40
  */
-    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_value, __pyx_kp_s_VCFv4_1, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_value, __pyx_kp_s_VCFv4_1, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_3) {
 
-      /* "pysam/cvcf.pyx":533
+      /* "pysam/cvcf.pyx":538
  *             elif value == "VCFv4.1":
  *                 # AH - for testing
  *                 self._version = 40             # <<<<<<<<<<<<<<
  *             else:
  *                 self.error(line,self.UNKNOWN_FORMAT_STRING)
  */
-      if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_int_40) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 533; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_int_40) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 538; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       goto __pyx_L4;
     }
     /*else*/ {
 
-      /* "pysam/cvcf.pyx":535
+      /* "pysam/cvcf.pyx":540
  *                 self._version = 40
  *             else:
  *                 self.error(line,self.UNKNOWN_FORMAT_STRING)             # <<<<<<<<<<<<<<
  *         elif key == "INFO":
  *             f = self.parse_format(line, value)
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_UNKNOWN_FORMAT_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_UNKNOWN_FORMAT_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_line);
@@ -11425,7 +11498,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_16parse_header(CYTHON_UNUSED PyObjec
       PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_4, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 535; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_4, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
@@ -11435,26 +11508,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_16parse_header(CYTHON_UNUSED PyObjec
     goto __pyx_L3;
   }
 
-  /* "pysam/cvcf.pyx":536
+  /* "pysam/cvcf.pyx":541
  *             else:
  *                 self.error(line,self.UNKNOWN_FORMAT_STRING)
  *         elif key == "INFO":             # <<<<<<<<<<<<<<
  *             f = self.parse_format(line, value)
  *             self._info[ f.id ] = f
  */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_INFO, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_INFO, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_3) {
 
-    /* "pysam/cvcf.pyx":537
+    /* "pysam/cvcf.pyx":542
  *                 self.error(line,self.UNKNOWN_FORMAT_STRING)
  *         elif key == "INFO":
  *             f = self.parse_format(line, value)             # <<<<<<<<<<<<<<
  *             self._info[ f.id ] = f
  *         elif key == "FILTER":
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_format); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_format); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_line);
@@ -11462,50 +11535,50 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_16parse_header(CYTHON_UNUSED PyObjec
     __Pyx_INCREF(__pyx_v_value);
     PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_v_value);
     __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 537; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __pyx_v_f = __pyx_t_1;
     __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":538
+    /* "pysam/cvcf.pyx":543
  *         elif key == "INFO":
  *             f = self.parse_format(line, value)
  *             self._info[ f.id ] = f             # <<<<<<<<<<<<<<
  *         elif key == "FILTER":
  *             f = self.parse_format(line, value, filter=True)
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 538; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 538; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    if (unlikely(PyObject_SetItem(__pyx_t_1, __pyx_t_4, __pyx_v_f) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 538; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyObject_SetItem(__pyx_t_1, __pyx_t_4, __pyx_v_f) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     goto __pyx_L3;
   }
 
-  /* "pysam/cvcf.pyx":539
+  /* "pysam/cvcf.pyx":544
  *             f = self.parse_format(line, value)
  *             self._info[ f.id ] = f
  *         elif key == "FILTER":             # <<<<<<<<<<<<<<
  *             f = self.parse_format(line, value, filter=True)
  *             self._filter[ f.id ] = f
  */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_FILTER, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 539; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_FILTER, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_3) {
 
-    /* "pysam/cvcf.pyx":540
+    /* "pysam/cvcf.pyx":545
  *             self._info[ f.id ] = f
  *         elif key == "FILTER":
  *             f = self.parse_format(line, value, filter=True)             # <<<<<<<<<<<<<<
  *             self._filter[ f.id ] = f
  *         elif key == "FORMAT":
  */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_format); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_format); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_line);
@@ -11513,10 +11586,10 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_16parse_header(CYTHON_UNUSED PyObjec
     __Pyx_INCREF(__pyx_v_value);
     PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_value);
     __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_filter_2, Py_True) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 540; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (PyDict_SetItem(__pyx_t_2, __pyx_n_s_filter_2, Py_True) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 545; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -11524,43 +11597,43 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_16parse_header(CYTHON_UNUSED PyObjec
     __pyx_v_f = __pyx_t_5;
     __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":541
+    /* "pysam/cvcf.pyx":546
  *         elif key == "FILTER":
  *             f = self.parse_format(line, value, filter=True)
  *             self._filter[ f.id ] = f             # <<<<<<<<<<<<<<
  *         elif key == "FORMAT":
  *             f = self.parse_format(line, value)
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_filter); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_filter); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    if (unlikely(PyObject_SetItem(__pyx_t_5, __pyx_t_2, __pyx_v_f) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyObject_SetItem(__pyx_t_5, __pyx_t_2, __pyx_v_f) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     goto __pyx_L3;
   }
 
-  /* "pysam/cvcf.pyx":542
+  /* "pysam/cvcf.pyx":547
  *             f = self.parse_format(line, value, filter=True)
  *             self._filter[ f.id ] = f
  *         elif key == "FORMAT":             # <<<<<<<<<<<<<<
  *             f = self.parse_format(line, value)
  *             self._format[ f.id ] = f
  */
-  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_FORMAT, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 542; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = (__Pyx_PyString_Equals(__pyx_v_key, __pyx_n_s_FORMAT, Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_3) {
 
-    /* "pysam/cvcf.pyx":543
+    /* "pysam/cvcf.pyx":548
  *             self._filter[ f.id ] = f
  *         elif key == "FORMAT":
  *             f = self.parse_format(line, value)             # <<<<<<<<<<<<<<
  *             self._format[ f.id ] = f
  *         else:
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_format); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_format); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 548; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 548; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_line);
@@ -11568,41 +11641,41 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_16parse_header(CYTHON_UNUSED PyObjec
     __Pyx_INCREF(__pyx_v_value);
     PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_v_value);
     __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 548; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __pyx_v_f = __pyx_t_1;
     __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":544
+    /* "pysam/cvcf.pyx":549
  *         elif key == "FORMAT":
  *             f = self.parse_format(line, value)
  *             self._format[ f.id ] = f             # <<<<<<<<<<<<<<
  *         else:
  *             # keep other keys in the header field
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    if (unlikely(PyObject_SetItem(__pyx_t_1, __pyx_t_5, __pyx_v_f) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 544; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyObject_SetItem(__pyx_t_1, __pyx_t_5, __pyx_v_f) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 549; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     goto __pyx_L3;
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":547
+    /* "pysam/cvcf.pyx":552
  *         else:
  *             # keep other keys in the header field
  *             self._header.append( (key,value) )             # <<<<<<<<<<<<<<
  * 
  * 
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_header); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_header); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_INCREF(__pyx_v_key);
     PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_key);
@@ -11610,18 +11683,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_16parse_header(CYTHON_UNUSED PyObjec
     __Pyx_INCREF(__pyx_v_value);
     PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_value);
     __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_6 = __Pyx_PyObject_Append(__pyx_t_5, __pyx_t_1); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 547; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Append(__pyx_t_5, __pyx_t_1); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":521
+  /* "pysam/cvcf.pyx":525
  *                 self._format[f.id] = f
  * 
- *     def parse_header( self, line ):             # <<<<<<<<<<<<<<
+ *     def parse_header(self, line):             # <<<<<<<<<<<<<<
+ * 
  *         assert line.startswith('##')
- *         elts = line[2:].split('=')
  */
 
   /* function exit code */
@@ -11644,7 +11717,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_16parse_header(CYTHON_UNUSED PyObjec
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":550
+/* "pysam/cvcf.pyx":555
  * 
  * 
  *     def write_header( self, stream ):             # <<<<<<<<<<<<<<
@@ -11684,11 +11757,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_19write_header(PyObject *__pyx_self,
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_stream)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("write_header", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("write_header", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "write_header") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "write_header") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -11701,7 +11774,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_19write_header(PyObject *__pyx_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("write_header", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("write_header", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.write_header", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -11741,26 +11814,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("write_header", 0);
 
-  /* "pysam/cvcf.pyx":551
+  /* "pysam/cvcf.pyx":556
  * 
  *     def write_header( self, stream ):
  *         stream.write("##fileformat=VCFv%s.%s\n" % (self._version // 10, self._version % 10))             # <<<<<<<<<<<<<<
  *         for key,value in self._header: stream.write("##%s=%s\n" % (key,value))
  *         for var,label in [(self._info,"INFO"),(self._filter,"FILTER"),(self._format,"FORMAT")]:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 551; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 551; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_FloorDivide(__pyx_t_2, __pyx_int_10); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 551; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyNumber_FloorDivide(__pyx_t_2, __pyx_int_10); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 551; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_4 = PyNumber_Remainder(__pyx_t_2, __pyx_int_10); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 551; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyNumber_Remainder(__pyx_t_2, __pyx_int_10); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 551; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_3);
   __Pyx_GIVEREF(__pyx_t_3);
@@ -11768,34 +11841,34 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
   __Pyx_GIVEREF(__pyx_t_4);
   __pyx_t_3 = 0;
   __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_fileformat_VCFv_s_s, __pyx_t_2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 551; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyString_Format(__pyx_kp_s_fileformat_VCFv_s_s, __pyx_t_2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 551; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_4);
   __Pyx_GIVEREF(__pyx_t_4);
   __pyx_t_4 = 0;
-  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 551; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/cvcf.pyx":552
+  /* "pysam/cvcf.pyx":557
  *     def write_header( self, stream ):
  *         stream.write("##fileformat=VCFv%s.%s\n" % (self._version // 10, self._version % 10))
  *         for key,value in self._header: stream.write("##%s=%s\n" % (key,value))             # <<<<<<<<<<<<<<
  *         for var,label in [(self._info,"INFO"),(self._filter,"FILTER"),(self._format,"FORMAT")]:
  *             for f in var.itervalues(): stream.write("##%s=%s\n" % (label,self.format_format(f,filter=(label=="FILTER"))))
  */
-  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_header); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_header); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   if (PyList_CheckExact(__pyx_t_4) || PyTuple_CheckExact(__pyx_t_4)) {
     __pyx_t_2 = __pyx_t_4; __Pyx_INCREF(__pyx_t_2); __pyx_t_5 = 0;
     __pyx_t_6 = NULL;
   } else {
-    __pyx_t_5 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_t_6 = Py_TYPE(__pyx_t_2)->tp_iternext;
   }
@@ -11804,16 +11877,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
     if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_2)) {
       if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_4 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_4); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_4); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_4 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_2)) {
       if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_4); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_5); __Pyx_INCREF(__pyx_t_4); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_4 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PySequence_ITEM(__pyx_t_2, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_4 = __pyx_t_6(__pyx_t_2);
@@ -11821,7 +11894,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -11837,7 +11910,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
       if (unlikely(size != 2)) {
         if (size > 2) __Pyx_RaiseTooManyValuesError(2);
         else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       #if CYTHON_COMPILING_IN_CPYTHON
       if (likely(PyTuple_CheckExact(sequence))) {
@@ -11850,15 +11923,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
       __Pyx_INCREF(__pyx_t_1);
       __Pyx_INCREF(__pyx_t_3);
       #else
-      __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       #endif
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     } else {
       Py_ssize_t index = -1;
-      __pyx_t_7 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
       __pyx_t_8 = Py_TYPE(__pyx_t_7)->tp_iternext;
@@ -11866,7 +11939,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
       __Pyx_GOTREF(__pyx_t_1);
       index = 1; __pyx_t_3 = __pyx_t_8(__pyx_t_7); if (unlikely(!__pyx_t_3)) goto __pyx_L5_unpacking_failed;
       __Pyx_GOTREF(__pyx_t_3);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_8(__pyx_t_7), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (__Pyx_IternextUnpackEndCheck(__pyx_t_8(__pyx_t_7), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_8 = NULL;
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       goto __pyx_L6_unpacking_done;
@@ -11874,16 +11947,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __pyx_t_8 = NULL;
       if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_L6_unpacking_done:;
     }
     __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_1);
     __pyx_t_1 = 0;
     __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_3);
     __pyx_t_3 = 0;
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_key);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_key);
@@ -11891,15 +11964,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
     __Pyx_INCREF(__pyx_v_value);
     PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_v_value);
     __Pyx_GIVEREF(__pyx_v_value);
-    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_s_s_2, __pyx_t_3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_s_s_2, __pyx_t_3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -11907,16 +11980,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
   }
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":553
+  /* "pysam/cvcf.pyx":558
  *         stream.write("##fileformat=VCFv%s.%s\n" % (self._version // 10, self._version % 10))
  *         for key,value in self._header: stream.write("##%s=%s\n" % (key,value))
  *         for var,label in [(self._info,"INFO"),(self._filter,"FILTER"),(self._format,"FORMAT")]:             # <<<<<<<<<<<<<<
  *             for f in var.itervalues(): stream.write("##%s=%s\n" % (label,self.format_format(f,filter=(label=="FILTER"))))
  * 
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
@@ -11924,9 +11997,9 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
   PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_n_s_INFO);
   __Pyx_GIVEREF(__pyx_n_s_INFO);
   __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_filter); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_filter); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
@@ -11934,9 +12007,9 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
   PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_n_s_FILTER);
   __Pyx_GIVEREF(__pyx_n_s_FILTER);
   __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
@@ -11944,7 +12017,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
   PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_n_s_FORMAT);
   __Pyx_GIVEREF(__pyx_n_s_FORMAT);
   __pyx_t_2 = 0;
-  __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
@@ -11960,9 +12033,9 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
   for (;;) {
     if (__pyx_t_5 >= 3) break;
     #if CYTHON_COMPILING_IN_CPYTHON
-    __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_2); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     #else
-    __pyx_t_2 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     #endif
     if (likely(__pyx_t_2 != Py_None)) {
       PyObject* sequence = __pyx_t_2;
@@ -11974,7 +12047,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
       if (unlikely(size != 2)) {
         if (size > 2) __Pyx_RaiseTooManyValuesError(2);
         else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       #if CYTHON_COMPILING_IN_CPYTHON
       __pyx_t_3 = PyTuple_GET_ITEM(sequence, 0); 
@@ -11982,21 +12055,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
       __Pyx_INCREF(__pyx_t_3);
       __Pyx_INCREF(__pyx_t_1);
       #else
-      __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_1 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       #endif
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     } else {
-      __Pyx_RaiseNoneNotIterableError(); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_RaiseNoneNotIterableError(); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     __Pyx_XDECREF_SET(__pyx_v_var, __pyx_t_3);
     __pyx_t_3 = 0;
     __Pyx_XDECREF_SET(__pyx_v_label, __pyx_t_1);
     __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":554
+    /* "pysam/cvcf.pyx":559
  *         for key,value in self._header: stream.write("##%s=%s\n" % (key,value))
  *         for var,label in [(self._info,"INFO"),(self._filter,"FILTER"),(self._format,"FORMAT")]:
  *             for f in var.itervalues(): stream.write("##%s=%s\n" % (label,self.format_format(f,filter=(label=="FILTER"))))             # <<<<<<<<<<<<<<
@@ -12006,9 +12079,9 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
     __pyx_t_9 = 0;
     if (unlikely(__pyx_v_var == Py_None)) {
       PyErr_Format(PyExc_AttributeError, "'NoneType' object has no attribute '%s'", "itervalues");
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
-    __pyx_t_1 = __Pyx_dict_iterator(__pyx_v_var, 0, __pyx_n_s_itervalues, (&__pyx_t_10), (&__pyx_t_11)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_dict_iterator(__pyx_v_var, 0, __pyx_n_s_itervalues, (&__pyx_t_10), (&__pyx_t_11)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_XDECREF(__pyx_t_2);
     __pyx_t_2 = __pyx_t_1;
@@ -12016,30 +12089,30 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
     while (1) {
       __pyx_t_12 = __Pyx_dict_iter_next(__pyx_t_2, __pyx_t_10, &__pyx_t_9, NULL, &__pyx_t_1, NULL, __pyx_t_11);
       if (unlikely(__pyx_t_12 == 0)) break;
-      if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_XDECREF_SET(__pyx_v_f, __pyx_t_1);
       __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_format); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_format); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_INCREF(__pyx_v_f);
       PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_f);
       __Pyx_GIVEREF(__pyx_v_f);
-      __pyx_t_13 = PyDict_New(); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_13 = PyDict_New(); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_13);
-      __pyx_t_14 = PyObject_RichCompare(__pyx_v_label, __pyx_n_s_FILTER, Py_EQ); __Pyx_XGOTREF(__pyx_t_14); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      if (PyDict_SetItem(__pyx_t_13, __pyx_n_s_filter_2, __pyx_t_14) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_14 = PyObject_RichCompare(__pyx_v_label, __pyx_n_s_FILTER, Py_EQ); __Pyx_XGOTREF(__pyx_t_14); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (PyDict_SetItem(__pyx_t_13, __pyx_n_s_filter_2, __pyx_t_14) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
-      __pyx_t_14 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_7, __pyx_t_13); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_14 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_7, __pyx_t_13); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_14);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
-      __pyx_t_13 = PyTuple_New(2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_13 = PyTuple_New(2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_13);
       __Pyx_INCREF(__pyx_v_label);
       PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_v_label);
@@ -12047,15 +12120,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
       PyTuple_SET_ITEM(__pyx_t_13, 1, __pyx_t_14);
       __Pyx_GIVEREF(__pyx_t_14);
       __pyx_t_14 = 0;
-      __pyx_t_14 = __Pyx_PyString_Format(__pyx_kp_s_s_s_2, __pyx_t_13); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_14 = __Pyx_PyString_Format(__pyx_kp_s_s_s_2, __pyx_t_13); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_14);
       __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
-      __pyx_t_13 = PyTuple_New(1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_13 = PyTuple_New(1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_13);
       PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_t_14);
       __Pyx_GIVEREF(__pyx_t_14);
       __pyx_t_14 = 0;
-      __pyx_t_14 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_13, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 554; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_14 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_13, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_14);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
@@ -12065,7 +12138,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
   }
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/cvcf.pyx":550
+  /* "pysam/cvcf.pyx":555
  * 
  * 
  *     def write_header( self, stream ):             # <<<<<<<<<<<<<<
@@ -12097,7 +12170,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_18write_header(CYTHON_UNUSED PyObjec
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":557
+/* "pysam/cvcf.pyx":562
  * 
  * 
  *     def parse_heading( self, line ):             # <<<<<<<<<<<<<<
@@ -12137,11 +12210,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_21parse_heading(PyObject *__pyx_self
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_line)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse_heading", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse_heading", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_heading") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_heading") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -12154,7 +12227,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_21parse_heading(PyObject *__pyx_self
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("parse_heading", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("parse_heading", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.parse_heading", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -12193,7 +12266,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse_heading", 0);
 
-  /* "pysam/cvcf.pyx":558
+  /* "pysam/cvcf.pyx":563
  * 
  *     def parse_heading( self, line ):
  *         assert line.startswith('#')             # <<<<<<<<<<<<<<
@@ -12202,21 +12275,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
  */
   #ifndef CYTHON_WITHOUT_ASSERTIONS
   if (unlikely(!Py_OptimizeFlag)) {
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__52, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__52, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (unlikely(!__pyx_t_3)) {
       PyErr_SetNone(PyExc_AssertionError);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
   #endif
 
-  /* "pysam/cvcf.pyx":559
+  /* "pysam/cvcf.pyx":564
  *     def parse_heading( self, line ):
  *         assert line.startswith('#')
  *         assert not line.startswith('##')             # <<<<<<<<<<<<<<
@@ -12225,57 +12298,57 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
  */
   #ifndef CYTHON_WITHOUT_ASSERTIONS
   if (unlikely(!Py_OptimizeFlag)) {
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__53, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__53, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (unlikely(!((!__pyx_t_3) != 0))) {
       PyErr_SetNone(PyExc_AssertionError);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
   #endif
 
-  /* "pysam/cvcf.pyx":560
+  /* "pysam/cvcf.pyx":565
  *         assert line.startswith('#')
  *         assert not line.startswith('##')
  *         headings = line[1:].split('\t')             # <<<<<<<<<<<<<<
  *         # test for 8, as FORMAT field might be missing
  *         if len(headings)==1 and len(line[1:].split()) >= 8:
  */
-  __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_line, 1, 0, NULL, NULL, &__pyx_slice__54, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_line, 1, 0, NULL, NULL, &__pyx_slice__54, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__56, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__56, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_v_headings = __pyx_t_1;
   __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":562
+  /* "pysam/cvcf.pyx":567
  *         headings = line[1:].split('\t')
  *         # test for 8, as FORMAT field might be missing
  *         if len(headings)==1 and len(line[1:].split()) >= 8:             # <<<<<<<<<<<<<<
  *             self.error(line,self.HEADING_NOT_SEPARATED_BY_TABS)
  *             headings = line[1:].split()
  */
-  __pyx_t_4 = PyObject_Length(__pyx_v_headings); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyObject_Length(__pyx_v_headings); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_t_3 = ((__pyx_t_4 == 1) != 0);
   if (__pyx_t_3) {
-    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_line, 1, 0, NULL, NULL, &__pyx_slice__57, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_line, 1, 0, NULL, NULL, &__pyx_slice__57, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_4 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_t_5 = ((__pyx_t_4 >= 8) != 0);
     __pyx_t_6 = __pyx_t_5;
@@ -12284,18 +12357,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
   }
   if (__pyx_t_6) {
 
-    /* "pysam/cvcf.pyx":563
+    /* "pysam/cvcf.pyx":568
  *         # test for 8, as FORMAT field might be missing
  *         if len(headings)==1 and len(line[1:].split()) >= 8:
  *             self.error(line,self.HEADING_NOT_SEPARATED_BY_TABS)             # <<<<<<<<<<<<<<
  *             headings = line[1:].split()
  * 
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_HEADING_NOT_SEPARATED_BY_TABS); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_HEADING_NOT_SEPARATED_BY_TABS); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_line);
@@ -12303,25 +12376,25 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
     PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_7, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":564
+    /* "pysam/cvcf.pyx":569
  *         if len(headings)==1 and len(line[1:].split()) >= 8:
  *             self.error(line,self.HEADING_NOT_SEPARATED_BY_TABS)
  *             headings = line[1:].split()             # <<<<<<<<<<<<<<
  * 
  *         for i,s in enumerate(self._required):
  */
-    __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_line, 1, 0, NULL, NULL, &__pyx_slice__58, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_line, 1, 0, NULL, NULL, &__pyx_slice__58, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 569; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 569; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 569; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __Pyx_DECREF_SET(__pyx_v_headings, __pyx_t_2);
@@ -12330,7 +12403,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":566
+  /* "pysam/cvcf.pyx":571
  *             headings = line[1:].split()
  * 
  *         for i,s in enumerate(self._required):             # <<<<<<<<<<<<<<
@@ -12339,13 +12412,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
  */
   __Pyx_INCREF(__pyx_int_0);
   __pyx_t_2 = __pyx_int_0;
-  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_required); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_required); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_7);
   if (PyList_CheckExact(__pyx_t_7) || PyTuple_CheckExact(__pyx_t_7)) {
     __pyx_t_1 = __pyx_t_7; __Pyx_INCREF(__pyx_t_1); __pyx_t_4 = 0;
     __pyx_t_8 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_7); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_t_8 = Py_TYPE(__pyx_t_1)->tp_iternext;
   }
@@ -12354,16 +12427,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
     if (!__pyx_t_8 && PyList_CheckExact(__pyx_t_1)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_7); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_7); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_8 && PyTuple_CheckExact(__pyx_t_1)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_7); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_7); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_7 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_7 = __pyx_t_8(__pyx_t_1);
@@ -12371,7 +12444,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -12381,32 +12454,32 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
     __pyx_t_7 = 0;
     __Pyx_INCREF(__pyx_t_2);
     __Pyx_XDECREF_SET(__pyx_v_i, __pyx_t_2);
-    __pyx_t_7 = PyNumber_Add(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 566; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyNumber_Add(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_DECREF(__pyx_t_2);
     __pyx_t_2 = __pyx_t_7;
     __pyx_t_7 = 0;
 
-    /* "pysam/cvcf.pyx":568
+    /* "pysam/cvcf.pyx":573
  *         for i,s in enumerate(self._required):
  * 
  *             if len(headings)<=i or headings[i] != s:             # <<<<<<<<<<<<<<
  * 
  *                 if len(headings) <= i:
  */
-    __pyx_t_9 = PyObject_Length(__pyx_v_headings); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_7 = PyInt_FromSsize_t(__pyx_t_9); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyObject_Length(__pyx_v_headings); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyInt_FromSsize_t(__pyx_t_9); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_10 = PyObject_RichCompare(__pyx_t_7, __pyx_v_i, Py_LE); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyObject_RichCompare(__pyx_t_7, __pyx_v_i, Py_LE); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     if (!__pyx_t_6) {
-      __pyx_t_10 = PyObject_GetItem(__pyx_v_headings, __pyx_v_i); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_10 = PyObject_GetItem(__pyx_v_headings, __pyx_v_i); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_10);
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_10, __pyx_v_s, Py_NE); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_RichCompare(__pyx_t_10, __pyx_v_s, Py_NE); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 568; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __pyx_t_5 = __pyx_t_3;
     } else {
@@ -12414,32 +12487,32 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
     }
     if (__pyx_t_5) {
 
-      /* "pysam/cvcf.pyx":570
+      /* "pysam/cvcf.pyx":575
  *             if len(headings)<=i or headings[i] != s:
  * 
  *                 if len(headings) <= i:             # <<<<<<<<<<<<<<
  *                     err = "(%sth entry not found)" % (i+1)
  *                 else:
  */
-      __pyx_t_9 = PyObject_Length(__pyx_v_headings); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_7 = PyInt_FromSsize_t(__pyx_t_9); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyObject_Length(__pyx_v_headings); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyInt_FromSsize_t(__pyx_t_9); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_10 = PyObject_RichCompare(__pyx_t_7, __pyx_v_i, Py_LE); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyObject_RichCompare(__pyx_t_7, __pyx_v_i, Py_LE); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 570; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
       if (__pyx_t_5) {
 
-        /* "pysam/cvcf.pyx":571
+        /* "pysam/cvcf.pyx":576
  * 
  *                 if len(headings) <= i:
  *                     err = "(%sth entry not found)" % (i+1)             # <<<<<<<<<<<<<<
  *                 else:
  *                     err = "(found %s, expected %s)" % (headings[i],s)
  */
-        __pyx_t_10 = PyNumber_Add(__pyx_v_i, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PyNumber_Add(__pyx_v_i, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_sth_entry_not_found, __pyx_t_10); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_sth_entry_not_found, __pyx_t_10); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
         __Pyx_XDECREF_SET(__pyx_v_err, ((PyObject*)__pyx_t_7));
@@ -12448,16 +12521,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":573
+        /* "pysam/cvcf.pyx":578
  *                     err = "(%sth entry not found)" % (i+1)
  *                 else:
  *                     err = "(found %s, expected %s)" % (headings[i],s)             # <<<<<<<<<<<<<<
  * 
  *                 #self.error(line,self.BADLY_FORMATTED_HEADING,err)
  */
-        __pyx_t_7 = PyObject_GetItem(__pyx_v_headings, __pyx_v_i); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_7 = PyObject_GetItem(__pyx_v_headings, __pyx_v_i); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
         PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_7);
         __Pyx_GIVEREF(__pyx_t_7);
@@ -12465,7 +12538,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
         PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_v_s);
         __Pyx_GIVEREF(__pyx_v_s);
         __pyx_t_7 = 0;
-        __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_found_s_expected_s, __pyx_t_10); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = __Pyx_PyString_Format(__pyx_kp_s_found_s_expected_s, __pyx_t_10); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
         __Pyx_XDECREF_SET(__pyx_v_err, ((PyObject*)__pyx_t_7));
@@ -12473,41 +12546,41 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
       }
       __pyx_L7:;
 
-      /* "pysam/cvcf.pyx":577
+      /* "pysam/cvcf.pyx":582
  *                 #self.error(line,self.BADLY_FORMATTED_HEADING,err)
  *                 # allow FORMAT column to be absent
  *                 if len(headings) == 8:             # <<<<<<<<<<<<<<
  *                     headings.append("FORMAT")
  *                 else:
  */
-      __pyx_t_9 = PyObject_Length(__pyx_v_headings); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 577; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyObject_Length(__pyx_v_headings); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_5 = ((__pyx_t_9 == 8) != 0);
       if (__pyx_t_5) {
 
-        /* "pysam/cvcf.pyx":578
+        /* "pysam/cvcf.pyx":583
  *                 # allow FORMAT column to be absent
  *                 if len(headings) == 8:
  *                     headings.append("FORMAT")             # <<<<<<<<<<<<<<
  *                 else:
  *                     self.error(line,self.BADLY_FORMATTED_HEADING,err)
  */
-        __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_headings, __pyx_n_s_FORMAT); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 578; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_headings, __pyx_n_s_FORMAT); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         goto __pyx_L8;
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":580
+        /* "pysam/cvcf.pyx":585
  *                     headings.append("FORMAT")
  *                 else:
  *                     self.error(line,self.BADLY_FORMATTED_HEADING,err)             # <<<<<<<<<<<<<<
  * 
  *         self._samples = headings[9:]
  */
-        __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_HEADING); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BADLY_FORMATTED_HEADING); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_12 = PyTuple_New(3); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_12 = PyTuple_New(3); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_12);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_12, 0, __pyx_v_line);
@@ -12518,7 +12591,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
         PyTuple_SET_ITEM(__pyx_t_12, 2, __pyx_v_err);
         __Pyx_GIVEREF(__pyx_v_err);
         __pyx_t_10 = 0;
-        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_12, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 580; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_12, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
@@ -12532,36 +12605,36 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":582
+  /* "pysam/cvcf.pyx":587
  *                     self.error(line,self.BADLY_FORMATTED_HEADING,err)
  * 
  *         self._samples = headings[9:]             # <<<<<<<<<<<<<<
  *         self._sample2column = dict( [(y,x+9) for x,y in enumerate( self._samples ) ] )
  * 
  */
-  __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_headings, 9, 0, NULL, NULL, &__pyx_slice__59, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_headings, 9, 0, NULL, NULL, &__pyx_slice__59, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_samples, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_samples, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":583
+  /* "pysam/cvcf.pyx":588
  * 
  *         self._samples = headings[9:]
  *         self._sample2column = dict( [(y,x+9) for x,y in enumerate( self._samples ) ] )             # <<<<<<<<<<<<<<
  * 
  *     def write_heading( self, stream ):
  */
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_int_0);
   __pyx_t_1 = __pyx_int_0;
-  __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   if (PyList_CheckExact(__pyx_t_10) || PyTuple_CheckExact(__pyx_t_10)) {
     __pyx_t_12 = __pyx_t_10; __Pyx_INCREF(__pyx_t_12); __pyx_t_4 = 0;
     __pyx_t_8 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_12 = PyObject_GetIter(__pyx_t_10); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_12 = PyObject_GetIter(__pyx_t_10); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_12);
     __pyx_t_8 = Py_TYPE(__pyx_t_12)->tp_iternext;
   }
@@ -12570,16 +12643,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
     if (!__pyx_t_8 && PyList_CheckExact(__pyx_t_12)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_12)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_10 = PyList_GET_ITEM(__pyx_t_12, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyList_GET_ITEM(__pyx_t_12, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_10 = PySequence_ITEM(__pyx_t_12, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PySequence_ITEM(__pyx_t_12, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_8 && PyTuple_CheckExact(__pyx_t_12)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_12)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_10 = PyTuple_GET_ITEM(__pyx_t_12, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyTuple_GET_ITEM(__pyx_t_12, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_10 = PySequence_ITEM(__pyx_t_12, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PySequence_ITEM(__pyx_t_12, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_10 = __pyx_t_8(__pyx_t_12);
@@ -12587,7 +12660,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -12597,14 +12670,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
     __pyx_t_10 = 0;
     __Pyx_INCREF(__pyx_t_1);
     __Pyx_XDECREF_SET(__pyx_v_x, __pyx_t_1);
-    __pyx_t_10 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_1);
     __pyx_t_1 = __pyx_t_10;
     __pyx_t_10 = 0;
-    __pyx_t_10 = PyNumber_Add(__pyx_v_x, __pyx_int_9); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyNumber_Add(__pyx_v_x, __pyx_int_9); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyTuple_New(2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
     __Pyx_INCREF(__pyx_v_y);
     PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_v_y);
@@ -12612,23 +12685,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
     PyTuple_SET_ITEM(__pyx_t_7, 1, __pyx_t_10);
     __Pyx_GIVEREF(__pyx_t_10);
     __pyx_t_10 = 0;
-    if (unlikely(__Pyx_ListComp_Append(__pyx_t_2, (PyObject*)__pyx_t_7))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(__Pyx_ListComp_Append(__pyx_t_2, (PyObject*)__pyx_t_7))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
   }
   __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
   __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyDict_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyDict_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_sample2column, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 583; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_sample2column, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":557
+  /* "pysam/cvcf.pyx":562
  * 
  * 
  *     def parse_heading( self, line ):             # <<<<<<<<<<<<<<
@@ -12659,7 +12732,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_20parse_heading(CYTHON_UNUSED PyObje
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":585
+/* "pysam/cvcf.pyx":590
  *         self._sample2column = dict( [(y,x+9) for x,y in enumerate( self._samples ) ] )
  * 
  *     def write_heading( self, stream ):             # <<<<<<<<<<<<<<
@@ -12699,11 +12772,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_23write_heading(PyObject *__pyx_self
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_stream)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("write_heading", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("write_heading", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "write_heading") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "write_heading") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -12716,7 +12789,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_23write_heading(PyObject *__pyx_self
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("write_heading", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("write_heading", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.write_heading", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -12741,44 +12814,44 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_22write_heading(CYTHON_UNUSED PyObje
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("write_heading", 0);
 
-  /* "pysam/cvcf.pyx":586
+  /* "pysam/cvcf.pyx":591
  * 
  *     def write_heading( self, stream ):
  *         stream.write("#" + "\t".join(self._required + self._samples) + "\n")             # <<<<<<<<<<<<<<
  * 
  *     def convertGT(self, GTstring):
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_required); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_required); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_4 = PyNumber_Add(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyNumber_Add(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__55, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__55, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = PyNumber_Add(__pyx_kp_s__51, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyNumber_Add(__pyx_kp_s__51, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = PyNumber_Add(__pyx_t_4, __pyx_kp_s__60); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyNumber_Add(__pyx_t_4, __pyx_kp_s__60); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_3);
   __Pyx_GIVEREF(__pyx_t_3);
   __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 586; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":585
+  /* "pysam/cvcf.pyx":590
  *         self._sample2column = dict( [(y,x+9) for x,y in enumerate( self._samples ) ] )
  * 
  *     def write_heading( self, stream ):             # <<<<<<<<<<<<<<
@@ -12802,7 +12875,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_22write_heading(CYTHON_UNUSED PyObje
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":588
+/* "pysam/cvcf.pyx":593
  *         stream.write("#" + "\t".join(self._required + self._samples) + "\n")
  * 
  *     def convertGT(self, GTstring):             # <<<<<<<<<<<<<<
@@ -12842,11 +12915,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_25convertGT(PyObject *__pyx_self, Py
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_GTstring)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("convertGT", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("convertGT", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "convertGT") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "convertGT") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -12859,7 +12932,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_25convertGT(PyObject *__pyx_self, Py
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("convertGT", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("convertGT", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.convertGT", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -12897,17 +12970,17 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("convertGT", 0);
 
-  /* "pysam/cvcf.pyx":589
+  /* "pysam/cvcf.pyx":594
  * 
  *     def convertGT(self, GTstring):
  *         if GTstring == ".": return ["."]             # <<<<<<<<<<<<<<
  *         try:
  *             gts = gtsRegEx.split(GTstring)
  */
-  __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_v_GTstring, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_v_GTstring, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_1) {
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 589; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_kp_s__8);
     PyList_SET_ITEM(__pyx_t_2, 0, __pyx_kp_s__8);
@@ -12917,7 +12990,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":590
+  /* "pysam/cvcf.pyx":595
  *     def convertGT(self, GTstring):
  *         if GTstring == ".": return ["."]
  *         try:             # <<<<<<<<<<<<<<
@@ -12931,47 +13004,47 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
     __Pyx_XGOTREF(__pyx_t_5);
     /*try:*/ {
 
-      /* "pysam/cvcf.pyx":591
+      /* "pysam/cvcf.pyx":596
  *         if GTstring == ".": return ["."]
  *         try:
  *             gts = gtsRegEx.split(GTstring)             # <<<<<<<<<<<<<<
  *             if len(gts) == 1: return [int(gts[0])]
  *             if len(gts) != 2: raise ValueError()
  */
-      __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_gtsRegEx); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_gtsRegEx); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_INCREF(__pyx_v_GTstring);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_GTstring);
       __Pyx_GIVEREF(__pyx_v_GTstring);
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 591; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_2, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __pyx_v_gts = __pyx_t_7;
       __pyx_t_7 = 0;
 
-      /* "pysam/cvcf.pyx":592
+      /* "pysam/cvcf.pyx":597
  *         try:
  *             gts = gtsRegEx.split(GTstring)
  *             if len(gts) == 1: return [int(gts[0])]             # <<<<<<<<<<<<<<
  *             if len(gts) != 2: raise ValueError()
  *             if gts[0] == "." and gts[1] == ".": return [gts[0],GTstring[len(gts[0]):-len(gts[1])],gts[1]]
  */
-      __pyx_t_8 = PyObject_Length(__pyx_v_gts); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_8 = PyObject_Length(__pyx_v_gts); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __pyx_t_1 = ((__pyx_t_8 == 1) != 0);
       if (__pyx_t_1) {
         __Pyx_XDECREF(__pyx_r);
-        __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+        __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_2 = PyNumber_Int(__pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_2 = PyNumber_Int(__pyx_t_7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        __pyx_t_7 = PyList_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 592; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_7 = PyList_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_7);
         PyList_SET_ITEM(__pyx_t_7, 0, __pyx_t_2);
         __Pyx_GIVEREF(__pyx_t_2);
@@ -12981,38 +13054,38 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
         goto __pyx_L8_try_return;
       }
 
-      /* "pysam/cvcf.pyx":593
+      /* "pysam/cvcf.pyx":598
  *             gts = gtsRegEx.split(GTstring)
  *             if len(gts) == 1: return [int(gts[0])]
  *             if len(gts) != 2: raise ValueError()             # <<<<<<<<<<<<<<
  *             if gts[0] == "." and gts[1] == ".": return [gts[0],GTstring[len(gts[0]):-len(gts[1])],gts[1]]
  *             return [int(gts[0]),GTstring[len(gts[0]):-len(gts[1])],int(gts[1])]
  */
-      __pyx_t_8 = PyObject_Length(__pyx_v_gts); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_8 = PyObject_Length(__pyx_v_gts); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __pyx_t_1 = ((__pyx_t_8 != 2) != 0);
       if (__pyx_t_1) {
-        __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_7 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_7);
         __Pyx_Raise(__pyx_t_7, 0, 0, 0);
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       }
 
-      /* "pysam/cvcf.pyx":594
+      /* "pysam/cvcf.pyx":599
  *             if len(gts) == 1: return [int(gts[0])]
  *             if len(gts) != 2: raise ValueError()
  *             if gts[0] == "." and gts[1] == ".": return [gts[0],GTstring[len(gts[0]):-len(gts[1])],gts[1]]             # <<<<<<<<<<<<<<
  *             return [int(gts[0]),GTstring[len(gts[0]):-len(gts[1])],int(gts[1])]
  *         except ValueError:
  */
-      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_t_7, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_1 = (__Pyx_PyString_Equals(__pyx_t_7, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       if (__pyx_t_1) {
-        __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+        __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_9 = (__Pyx_PyString_Equals(__pyx_t_7, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_9 = (__Pyx_PyString_Equals(__pyx_t_7, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
         __pyx_t_10 = __pyx_t_9;
       } else {
@@ -13020,21 +13093,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
       }
       if (__pyx_t_10) {
         __Pyx_XDECREF(__pyx_r);
-        __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+        __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
         __Pyx_GOTREF(__pyx_t_7);
-        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_8 = PyObject_Length(__pyx_t_2); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_8 = PyObject_Length(__pyx_t_2); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_11 = PyObject_Length(__pyx_t_2); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_11 = PyObject_Length(__pyx_t_2); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_GTstring, __pyx_t_8, (-__pyx_t_11), NULL, NULL, NULL, 1, 1, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_GTstring, __pyx_t_8, (-__pyx_t_11), NULL, NULL, NULL, 1, 1, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
         __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_12 = PyList_New(3); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 594; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+        __pyx_t_12 = PyList_New(3); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 599; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
         __Pyx_GOTREF(__pyx_t_12);
         PyList_SET_ITEM(__pyx_t_12, 0, __pyx_t_7);
         __Pyx_GIVEREF(__pyx_t_7);
@@ -13050,7 +13123,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
         goto __pyx_L8_try_return;
       }
 
-      /* "pysam/cvcf.pyx":595
+      /* "pysam/cvcf.pyx":600
  *             if len(gts) != 2: raise ValueError()
  *             if gts[0] == "." and gts[1] == ".": return [gts[0],GTstring[len(gts[0]):-len(gts[1])],gts[1]]
  *             return [int(gts[0]),GTstring[len(gts[0]):-len(gts[1])],int(gts[1])]             # <<<<<<<<<<<<<<
@@ -13058,27 +13131,27 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
  *             self.error(self._line,self.BAD_GENOTYPE,GTstring)
  */
       __Pyx_XDECREF(__pyx_r);
-      __pyx_t_12 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+      __pyx_t_12 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
       __Pyx_GOTREF(__pyx_t_12);
-      __pyx_t_6 = PyNumber_Int(__pyx_t_12); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_6 = PyNumber_Int(__pyx_t_12); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-      __pyx_t_12 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+      __pyx_t_12 = __Pyx_GetItemInt(__pyx_v_gts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
       __Pyx_GOTREF(__pyx_t_12);
-      __pyx_t_11 = PyObject_Length(__pyx_t_12); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_11 = PyObject_Length(__pyx_t_12); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-      __pyx_t_12 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+      __pyx_t_12 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
       __Pyx_GOTREF(__pyx_t_12);
-      __pyx_t_8 = PyObject_Length(__pyx_t_12); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_8 = PyObject_Length(__pyx_t_12); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
-      __pyx_t_12 = __Pyx_PyObject_GetSlice(__pyx_v_GTstring, __pyx_t_11, (-__pyx_t_8), NULL, NULL, NULL, 1, 1, 1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_12 = __Pyx_PyObject_GetSlice(__pyx_v_GTstring, __pyx_t_11, (-__pyx_t_8), NULL, NULL, NULL, 1, 1, 1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_GOTREF(__pyx_t_12);
-      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
+      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_gts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;};
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_7 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = PyList_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 595; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+      __pyx_t_2 = PyList_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
       __Pyx_GOTREF(__pyx_t_2);
       PyList_SET_ITEM(__pyx_t_2, 0, __pyx_t_6);
       __Pyx_GIVEREF(__pyx_t_6);
@@ -13103,7 +13176,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
     __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0;
     __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":596
+    /* "pysam/cvcf.pyx":601
  *             if gts[0] == "." and gts[1] == ".": return [gts[0],GTstring[len(gts[0]):-len(gts[1])],gts[1]]
  *             return [int(gts[0]),GTstring[len(gts[0]):-len(gts[1])],int(gts[1])]
  *         except ValueError:             # <<<<<<<<<<<<<<
@@ -13113,25 +13186,25 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
     __pyx_t_13 = PyErr_ExceptionMatches(__pyx_builtin_ValueError);
     if (__pyx_t_13) {
       __Pyx_AddTraceback("pysam.cvcf.VCF.convertGT", __pyx_clineno, __pyx_lineno, __pyx_filename);
-      if (__Pyx_GetException(&__pyx_t_2, &__pyx_t_7, &__pyx_t_12) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 596; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+      if (__Pyx_GetException(&__pyx_t_2, &__pyx_t_7, &__pyx_t_12) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 601; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_GOTREF(__pyx_t_7);
       __Pyx_GOTREF(__pyx_t_12);
 
-      /* "pysam/cvcf.pyx":597
+      /* "pysam/cvcf.pyx":602
  *             return [int(gts[0]),GTstring[len(gts[0]):-len(gts[1])],int(gts[1])]
  *         except ValueError:
  *             self.error(self._line,self.BAD_GENOTYPE,GTstring)             # <<<<<<<<<<<<<<
  *             return [".","|","."]
  * 
  */
-      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+      __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 602; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
       __Pyx_GOTREF(__pyx_t_6);
-      __pyx_t_14 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_line_2); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+      __pyx_t_14 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_line_2); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 602; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
       __Pyx_GOTREF(__pyx_t_14);
-      __pyx_t_15 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BAD_GENOTYPE); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+      __pyx_t_15 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BAD_GENOTYPE); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 602; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
       __Pyx_GOTREF(__pyx_t_15);
-      __pyx_t_16 = PyTuple_New(3); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+      __pyx_t_16 = PyTuple_New(3); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 602; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
       __Pyx_GOTREF(__pyx_t_16);
       PyTuple_SET_ITEM(__pyx_t_16, 0, __pyx_t_14);
       __Pyx_GIVEREF(__pyx_t_14);
@@ -13142,13 +13215,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
       __Pyx_GIVEREF(__pyx_v_GTstring);
       __pyx_t_14 = 0;
       __pyx_t_15 = 0;
-      __pyx_t_15 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_16, NULL); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+      __pyx_t_15 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_16, NULL); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 602; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
       __Pyx_GOTREF(__pyx_t_15);
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
       __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;
 
-      /* "pysam/cvcf.pyx":598
+      /* "pysam/cvcf.pyx":603
  *         except ValueError:
  *             self.error(self._line,self.BAD_GENOTYPE,GTstring)
  *             return [".","|","."]             # <<<<<<<<<<<<<<
@@ -13156,7 +13229,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
  *     def convertGTback(self, GTdata):
  */
       __Pyx_XDECREF(__pyx_r);
-      __pyx_t_15 = PyList_New(3); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 598; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
+      __pyx_t_15 = PyList_New(3); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
       __Pyx_GOTREF(__pyx_t_15);
       __Pyx_INCREF(__pyx_kp_s__8);
       PyList_SET_ITEM(__pyx_t_15, 0, __pyx_kp_s__8);
@@ -13205,7 +13278,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
     __pyx_L11_try_end:;
   }
 
-  /* "pysam/cvcf.pyx":588
+  /* "pysam/cvcf.pyx":593
  *         stream.write("#" + "\t".join(self._required + self._samples) + "\n")
  * 
  *     def convertGT(self, GTstring):             # <<<<<<<<<<<<<<
@@ -13231,7 +13304,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_24convertGT(CYTHON_UNUSED PyObject *
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":600
+/* "pysam/cvcf.pyx":605
  *             return [".","|","."]
  * 
  *     def convertGTback(self, GTdata):             # <<<<<<<<<<<<<<
@@ -13271,11 +13344,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_27convertGTback(PyObject *__pyx_self
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_GTdata)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("convertGTback", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("convertGTback", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "convertGTback") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "convertGTback") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -13288,7 +13361,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_27convertGTback(PyObject *__pyx_self
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("convertGTback", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("convertGTback", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.convertGTback", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -13311,7 +13384,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_26convertGTback(CYTHON_UNUSED PyObje
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("convertGTback", 0);
 
-  /* "pysam/cvcf.pyx":601
+  /* "pysam/cvcf.pyx":606
  * 
  *     def convertGTback(self, GTdata):
  *         return ''.join(map(str,GTdata))             # <<<<<<<<<<<<<<
@@ -13319,7 +13392,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_26convertGTback(CYTHON_UNUSED PyObje
  *     def parse_formatdata( self, key, value, formatdict, line ):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 601; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(((PyObject *)((PyObject*)(&PyString_Type))));
   PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)((PyObject*)(&PyString_Type))));
@@ -13327,17 +13400,17 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_26convertGTback(CYTHON_UNUSED PyObje
   __Pyx_INCREF(__pyx_v_GTdata);
   PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_GTdata);
   __Pyx_GIVEREF(__pyx_v_GTdata);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 601; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s_, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 601; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyString_Join(__pyx_kp_s_, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":600
+  /* "pysam/cvcf.pyx":605
  *             return [".","|","."]
  * 
  *     def convertGTback(self, GTdata):             # <<<<<<<<<<<<<<
@@ -13357,7 +13430,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_26convertGTback(CYTHON_UNUSED PyObje
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":603
+/* "pysam/cvcf.pyx":608
  *         return ''.join(map(str,GTdata))
  * 
  *     def parse_formatdata( self, key, value, formatdict, line ):             # <<<<<<<<<<<<<<
@@ -13403,26 +13476,26 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_29parse_formatdata(PyObject *__pyx_s
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_key)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  3:
         if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_formatdict)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  4:
         if (likely((values[4] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_line)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, 4); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, 4); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_formatdata") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_formatdata") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 5) {
       goto __pyx_L5_argtuple_error;
@@ -13441,7 +13514,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_29parse_formatdata(PyObject *__pyx_s
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("parse_formatdata", 1, 5, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.parse_formatdata", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -13485,16 +13558,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse_formatdata", 0);
 
-  /* "pysam/cvcf.pyx":605
+  /* "pysam/cvcf.pyx":610
  *     def parse_formatdata( self, key, value, formatdict, line ):
  *         # To do: check that the right number of values is present
  *         f = formatdict.get(key,None)             # <<<<<<<<<<<<<<
  *         if f == None:
  *             self._add_definition(formatdict, key, value, line )
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_formatdict, __pyx_n_s_get); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_formatdict, __pyx_n_s_get); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 610; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 610; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_key);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_key);
@@ -13502,35 +13575,35 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
   __Pyx_INCREF(Py_None);
   PyTuple_SET_ITEM(__pyx_t_2, 1, Py_None);
   __Pyx_GIVEREF(Py_None);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 610; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_v_f = __pyx_t_3;
   __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":606
+  /* "pysam/cvcf.pyx":611
  *         # To do: check that the right number of values is present
  *         f = formatdict.get(key,None)
  *         if f == None:             # <<<<<<<<<<<<<<
  *             self._add_definition(formatdict, key, value, line )
  *             f = formatdict[key]
  */
-  __pyx_t_3 = PyObject_RichCompare(__pyx_v_f, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 606; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_RichCompare(__pyx_v_f, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if (__pyx_t_4) {
 
-    /* "pysam/cvcf.pyx":607
+    /* "pysam/cvcf.pyx":612
  *         f = formatdict.get(key,None)
  *         if f == None:
  *             self._add_definition(formatdict, key, value, line )             # <<<<<<<<<<<<<<
  *             f = formatdict[key]
  *         if f.type == "Flag":
  */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_add_definition); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_add_definition); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 612; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 612; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_v_formatdict);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_formatdict);
@@ -13544,20 +13617,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_2, 3, __pyx_v_line);
     __Pyx_GIVEREF(__pyx_v_line);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 607; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 612; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":608
+    /* "pysam/cvcf.pyx":613
  *         if f == None:
  *             self._add_definition(formatdict, key, value, line )
  *             f = formatdict[key]             # <<<<<<<<<<<<<<
  *         if f.type == "Flag":
  *             if value is not None: self.error(line,self.ERROR_FLAG_HAS_VALUE)
  */
-    __pyx_t_1 = PyObject_GetItem(__pyx_v_formatdict, __pyx_v_key); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_1 = PyObject_GetItem(__pyx_v_formatdict, __pyx_v_key); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 613; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF_SET(__pyx_v_f, __pyx_t_1);
     __pyx_t_1 = 0;
@@ -13565,20 +13638,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":609
+  /* "pysam/cvcf.pyx":614
  *             self._add_definition(formatdict, key, value, line )
  *             f = formatdict[key]
  *         if f.type == "Flag":             # <<<<<<<<<<<<<<
  *             if value is not None: self.error(line,self.ERROR_FLAG_HAS_VALUE)
  *             return []
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_1, __pyx_n_s_Flag, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 609; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_1, __pyx_n_s_Flag, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_4) {
 
-    /* "pysam/cvcf.pyx":610
+    /* "pysam/cvcf.pyx":615
  *             f = formatdict[key]
  *         if f.type == "Flag":
  *             if value is not None: self.error(line,self.ERROR_FLAG_HAS_VALUE)             # <<<<<<<<<<<<<<
@@ -13588,11 +13661,11 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     __pyx_t_4 = (__pyx_v_value != Py_None);
     __pyx_t_5 = (__pyx_t_4 != 0);
     if (__pyx_t_5) {
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 610; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_FLAG_HAS_VALUE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 610; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_FLAG_HAS_VALUE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 610; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_line);
@@ -13600,7 +13673,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 610; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -13609,7 +13682,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     }
     __pyx_L5:;
 
-    /* "pysam/cvcf.pyx":611
+    /* "pysam/cvcf.pyx":616
  *         if f.type == "Flag":
  *             if value is not None: self.error(line,self.ERROR_FLAG_HAS_VALUE)
  *             return []             # <<<<<<<<<<<<<<
@@ -13617,40 +13690,40 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
  *         # deal with trailing data in some early VCF files
  */
     __Pyx_XDECREF(__pyx_r);
-    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 616; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_r = __pyx_t_2;
     __pyx_t_2 = 0;
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":612
+  /* "pysam/cvcf.pyx":617
  *             if value is not None: self.error(line,self.ERROR_FLAG_HAS_VALUE)
  *             return []
  *         values = value.split(',')             # <<<<<<<<<<<<<<
  *         # deal with trailing data in some early VCF files
  *         if f.type in ["Float","Integer"] and len(values)>0 and values[-1].find(';') > -1:
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_value, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 612; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_value, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__62, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 612; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__62, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_v_values = __pyx_t_3;
   __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":614
+  /* "pysam/cvcf.pyx":619
  *         values = value.split(',')
  *         # deal with trailing data in some early VCF files
  *         if f.type in ["Float","Integer"] and len(values)>0 and values[-1].find(';') > -1:             # <<<<<<<<<<<<<<
  *             self.error(line,self.ERROR_TRAILING_DATA,values[-1])
  *             values[-1] = values[-1].split(';')[0]
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_t_3, __pyx_n_s_Float, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_t_3, __pyx_n_s_Float, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (!__pyx_t_5) {
-    __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_3, __pyx_n_s_Integer, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = (__Pyx_PyString_Equals(__pyx_t_3, __pyx_n_s_Integer, Py_EQ)); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_6 = __pyx_t_4;
   } else {
     __pyx_t_6 = __pyx_t_5;
@@ -13658,20 +13731,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __pyx_t_5 = __pyx_t_6;
   if (__pyx_t_5) {
-    __pyx_t_7 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_6 = (__pyx_t_7 > 0);
     if (__pyx_t_6) {
-      __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_values, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_values, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_3);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_find); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_find); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__63, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__63, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_neg_1, Py_GT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_neg_1, Py_GT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __pyx_t_8 = __pyx_t_4;
     } else {
@@ -13683,20 +13756,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
   }
   if (__pyx_t_6) {
 
-    /* "pysam/cvcf.pyx":615
+    /* "pysam/cvcf.pyx":620
  *         # deal with trailing data in some early VCF files
  *         if f.type in ["Float","Integer"] and len(values)>0 and values[-1].find(';') > -1:
  *             self.error(line,self.ERROR_TRAILING_DATA,values[-1])             # <<<<<<<<<<<<<<
  *             values[-1] = values[-1].split(';')[0]
  *         if f.type == "Integer":
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_TRAILING_DATA); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_TRAILING_DATA); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_values, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_values, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_line);
@@ -13707,50 +13780,50 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_3 = 0;
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 615; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":616
+    /* "pysam/cvcf.pyx":621
  *         if f.type in ["Float","Integer"] and len(values)>0 and values[-1].find(';') > -1:
  *             self.error(line,self.ERROR_TRAILING_DATA,values[-1])
  *             values[-1] = values[-1].split(';')[0]             # <<<<<<<<<<<<<<
  *         if f.type == "Integer":
  *             for idx,v in enumerate(values):
  */
-    __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_values, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 616; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_values, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 616; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__64, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 616; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__64, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_GetItemInt(__pyx_t_1, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 616; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_9 = __Pyx_GetItemInt(__pyx_t_1, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    if (unlikely(__Pyx_SetItemInt(__pyx_v_values, -1, __pyx_t_9, long, 1, __Pyx_PyInt_From_long, 0, 1, 1) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 616; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(__Pyx_SetItemInt(__pyx_v_values, -1, __pyx_t_9, long, 1, __Pyx_PyInt_From_long, 0, 1, 1) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     goto __pyx_L6;
   }
   __pyx_L6:;
 
-  /* "pysam/cvcf.pyx":617
+  /* "pysam/cvcf.pyx":622
  *             self.error(line,self.ERROR_TRAILING_DATA,values[-1])
  *             values[-1] = values[-1].split(';')[0]
  *         if f.type == "Integer":             # <<<<<<<<<<<<<<
  *             for idx,v in enumerate(values):
  *                 try:
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 622; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Integer, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Integer, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 622; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   if (__pyx_t_6) {
 
-    /* "pysam/cvcf.pyx":618
+    /* "pysam/cvcf.pyx":623
  *             values[-1] = values[-1].split(';')[0]
  *         if f.type == "Integer":
  *             for idx,v in enumerate(values):             # <<<<<<<<<<<<<<
@@ -13763,7 +13836,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       __pyx_t_1 = __pyx_v_values; __Pyx_INCREF(__pyx_t_1); __pyx_t_7 = 0;
       __pyx_t_10 = NULL;
     } else {
-      __pyx_t_7 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __pyx_t_10 = Py_TYPE(__pyx_t_1)->tp_iternext;
     }
@@ -13771,16 +13844,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       if (!__pyx_t_10 && PyList_CheckExact(__pyx_t_1)) {
         if (__pyx_t_7 >= PyList_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_7); __Pyx_INCREF(__pyx_t_2); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_7); __Pyx_INCREF(__pyx_t_2); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_10 && PyTuple_CheckExact(__pyx_t_1)) {
         if (__pyx_t_7 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_7); __Pyx_INCREF(__pyx_t_2); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_7); __Pyx_INCREF(__pyx_t_2); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_2 = __pyx_t_10(__pyx_t_1);
@@ -13788,7 +13861,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -13798,13 +13871,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       __pyx_t_2 = 0;
       __Pyx_INCREF(__pyx_t_9);
       __Pyx_XDECREF_SET(__pyx_v_idx, __pyx_t_9);
-      __pyx_t_2 = PyNumber_Add(__pyx_t_9, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 618; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyNumber_Add(__pyx_t_9, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_9);
       __pyx_t_9 = __pyx_t_2;
       __pyx_t_2 = 0;
 
-      /* "pysam/cvcf.pyx":619
+      /* "pysam/cvcf.pyx":624
  *         if f.type == "Integer":
  *             for idx,v in enumerate(values):
  *                 try:             # <<<<<<<<<<<<<<
@@ -13818,33 +13891,33 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
         __Pyx_XGOTREF(__pyx_t_13);
         /*try:*/ {
 
-          /* "pysam/cvcf.pyx":620
+          /* "pysam/cvcf.pyx":625
  *             for idx,v in enumerate(values):
  *                 try:
  *                     if v == ".": values[idx] = f.missingvalue             # <<<<<<<<<<<<<<
  *                     else:        values[idx] = int(v)
  *                 except:
  */
-          __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_v, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
+          __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_v, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
           if (__pyx_t_6) {
-            __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_missingvalue); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
+            __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_missingvalue); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
             __Pyx_GOTREF(__pyx_t_2);
-            if (unlikely(PyObject_SetItem(__pyx_v_values, __pyx_v_idx, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 620; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
+            if (unlikely(PyObject_SetItem(__pyx_v_values, __pyx_v_idx, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 625; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
             __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
             goto __pyx_L18;
           }
           /*else*/ {
 
-            /* "pysam/cvcf.pyx":621
+            /* "pysam/cvcf.pyx":626
  *                 try:
  *                     if v == ".": values[idx] = f.missingvalue
  *                     else:        values[idx] = int(v)             # <<<<<<<<<<<<<<
  *                 except:
  *                     self.error(line,self.ERROR_FORMAT_NOT_INTEGER,"%s=%s" % (key, str(values)))
  */
-            __pyx_t_2 = PyNumber_Int(__pyx_v_v); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
+            __pyx_t_2 = PyNumber_Int(__pyx_v_v); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
             __Pyx_GOTREF(__pyx_t_2);
-            if (unlikely(PyObject_SetItem(__pyx_v_values, __pyx_v_idx, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
+            if (unlikely(PyObject_SetItem(__pyx_v_values, __pyx_v_idx, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __pyx_clineno = __LINE__; goto __pyx_L10_error;}
             __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
           }
           __pyx_L18:;
@@ -13857,7 +13930,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
         __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
         __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-        /* "pysam/cvcf.pyx":622
+        /* "pysam/cvcf.pyx":627
  *                     if v == ".": values[idx] = f.missingvalue
  *                     else:        values[idx] = int(v)
  *                 except:             # <<<<<<<<<<<<<<
@@ -13866,31 +13939,31 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
  */
         /*except:*/ {
           __Pyx_AddTraceback("pysam.cvcf.VCF.parse_formatdata", __pyx_clineno, __pyx_lineno, __pyx_filename);
-          if (__Pyx_GetException(&__pyx_t_2, &__pyx_t_3, &__pyx_t_14) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 622; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          if (__Pyx_GetException(&__pyx_t_2, &__pyx_t_3, &__pyx_t_14) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_2);
           __Pyx_GOTREF(__pyx_t_3);
           __Pyx_GOTREF(__pyx_t_14);
 
-          /* "pysam/cvcf.pyx":623
+          /* "pysam/cvcf.pyx":628
  *                     else:        values[idx] = int(v)
  *                 except:
  *                     self.error(line,self.ERROR_FORMAT_NOT_INTEGER,"%s=%s" % (key, str(values)))             # <<<<<<<<<<<<<<
  *                     return [0] * len(values)
  *             return values
  */
-          __pyx_t_15 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_15 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_15);
-          __pyx_t_16 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_FORMAT_NOT_INTEGER); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_16 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_FORMAT_NOT_INTEGER); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_16);
-          __pyx_t_17 = PyTuple_New(1); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_17 = PyTuple_New(1); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_17);
           __Pyx_INCREF(__pyx_v_values);
           PyTuple_SET_ITEM(__pyx_t_17, 0, __pyx_v_values);
           __Pyx_GIVEREF(__pyx_v_values);
-          __pyx_t_18 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_17, NULL); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_18 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_17, NULL); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_18);
           __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
-          __pyx_t_17 = PyTuple_New(2); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_17 = PyTuple_New(2); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_17);
           __Pyx_INCREF(__pyx_v_key);
           PyTuple_SET_ITEM(__pyx_t_17, 0, __pyx_v_key);
@@ -13898,10 +13971,10 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
           PyTuple_SET_ITEM(__pyx_t_17, 1, __pyx_t_18);
           __Pyx_GIVEREF(__pyx_t_18);
           __pyx_t_18 = 0;
-          __pyx_t_18 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_17); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_18 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_17); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_18);
           __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
-          __pyx_t_17 = PyTuple_New(3); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_17 = PyTuple_New(3); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_17);
           __Pyx_INCREF(__pyx_v_line);
           PyTuple_SET_ITEM(__pyx_t_17, 0, __pyx_v_line);
@@ -13912,13 +13985,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
           __Pyx_GIVEREF(__pyx_t_18);
           __pyx_t_16 = 0;
           __pyx_t_18 = 0;
-          __pyx_t_18 = __Pyx_PyObject_Call(__pyx_t_15, __pyx_t_17, NULL); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 623; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_18 = __Pyx_PyObject_Call(__pyx_t_15, __pyx_t_17, NULL); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_18);
           __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;
           __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
           __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
 
-          /* "pysam/cvcf.pyx":624
+          /* "pysam/cvcf.pyx":629
  *                 except:
  *                     self.error(line,self.ERROR_FORMAT_NOT_INTEGER,"%s=%s" % (key, str(values)))
  *                     return [0] * len(values)             # <<<<<<<<<<<<<<
@@ -13926,8 +13999,8 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
  *         elif f.type == "String":
  */
           __Pyx_XDECREF(__pyx_r);
-          __pyx_t_19 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_19 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 624; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
-          __pyx_t_18 = PyList_New(1 * ((__pyx_t_19<0) ? 0:__pyx_t_19)); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 624; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_19 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_19 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
+          __pyx_t_18 = PyList_New(1 * ((__pyx_t_19<0) ? 0:__pyx_t_19)); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 629; __pyx_clineno = __LINE__; goto __pyx_L12_except_error;}
           __Pyx_GOTREF(__pyx_t_18);
           { Py_ssize_t __pyx_temp;
             for (__pyx_temp=0; __pyx_temp < __pyx_t_19; __pyx_temp++) {
@@ -13972,7 +14045,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-    /* "pysam/cvcf.pyx":625
+    /* "pysam/cvcf.pyx":630
  *                     self.error(line,self.ERROR_FORMAT_NOT_INTEGER,"%s=%s" % (key, str(values)))
  *                     return [0] * len(values)
  *             return values             # <<<<<<<<<<<<<<
@@ -13985,43 +14058,43 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":626
+  /* "pysam/cvcf.pyx":631
  *                     return [0] * len(values)
  *             return values
  *         elif f.type == "String":             # <<<<<<<<<<<<<<
  *             self._line = line
  *             if f.id == "GT": values = list(map( self.convertGT, values ))
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_String, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_String, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   if (__pyx_t_6) {
 
-    /* "pysam/cvcf.pyx":627
+    /* "pysam/cvcf.pyx":632
  *             return values
  *         elif f.type == "String":
  *             self._line = line             # <<<<<<<<<<<<<<
  *             if f.id == "GT": values = list(map( self.convertGT, values ))
  *             return values
  */
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_line_2, __pyx_v_line) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 627; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_line_2, __pyx_v_line) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-    /* "pysam/cvcf.pyx":628
+    /* "pysam/cvcf.pyx":633
  *         elif f.type == "String":
  *             self._line = line
  *             if f.id == "GT": values = list(map( self.convertGT, values ))             # <<<<<<<<<<<<<<
  *             return values
  *         elif f.type == "Character":
  */
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_id); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_GT, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_GT, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     if (__pyx_t_6) {
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_convertGT); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_convertGT); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_9);
       __Pyx_GIVEREF(__pyx_t_9);
@@ -14029,15 +14102,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_v_values);
       __Pyx_GIVEREF(__pyx_v_values);
       __pyx_t_9 = 0;
-      __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_9);
       __Pyx_GIVEREF(__pyx_t_9);
       __pyx_t_9 = 0;
-      __pyx_t_9 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_1, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 633; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF_SET(__pyx_v_values, __pyx_t_9);
@@ -14046,7 +14119,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     }
     __pyx_L21:;
 
-    /* "pysam/cvcf.pyx":629
+    /* "pysam/cvcf.pyx":634
  *             self._line = line
  *             if f.id == "GT": values = list(map( self.convertGT, values ))
  *             return values             # <<<<<<<<<<<<<<
@@ -14059,20 +14132,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":630
+  /* "pysam/cvcf.pyx":635
  *             if f.id == "GT": values = list(map( self.convertGT, values ))
  *             return values
  *         elif f.type == "Character":             # <<<<<<<<<<<<<<
  *             for v in values:
  *                 if len(v) != 1: self.error(line,self.ERROR_FORMAT_NOT_CHAR)
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Character, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Character, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   if (__pyx_t_6) {
 
-    /* "pysam/cvcf.pyx":631
+    /* "pysam/cvcf.pyx":636
  *             return values
  *         elif f.type == "Character":
  *             for v in values:             # <<<<<<<<<<<<<<
@@ -14083,7 +14156,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       __pyx_t_9 = __pyx_v_values; __Pyx_INCREF(__pyx_t_9); __pyx_t_7 = 0;
       __pyx_t_10 = NULL;
     } else {
-      __pyx_t_7 = -1; __pyx_t_9 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = -1; __pyx_t_9 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __pyx_t_10 = Py_TYPE(__pyx_t_9)->tp_iternext;
     }
@@ -14091,16 +14164,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       if (!__pyx_t_10 && PyList_CheckExact(__pyx_t_9)) {
         if (__pyx_t_7 >= PyList_GET_SIZE(__pyx_t_9)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_7); __Pyx_INCREF(__pyx_t_1); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_7); __Pyx_INCREF(__pyx_t_1); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_9, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PySequence_ITEM(__pyx_t_9, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_10 && PyTuple_CheckExact(__pyx_t_9)) {
         if (__pyx_t_7 >= PyTuple_GET_SIZE(__pyx_t_9)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_9, __pyx_t_7); __Pyx_INCREF(__pyx_t_1); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_9, __pyx_t_7); __Pyx_INCREF(__pyx_t_1); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_9, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PySequence_ITEM(__pyx_t_9, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_1 = __pyx_t_10(__pyx_t_9);
@@ -14108,7 +14181,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -14117,21 +14190,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       __Pyx_XDECREF_SET(__pyx_v_v, __pyx_t_1);
       __pyx_t_1 = 0;
 
-      /* "pysam/cvcf.pyx":632
+      /* "pysam/cvcf.pyx":637
  *         elif f.type == "Character":
  *             for v in values:
  *                 if len(v) != 1: self.error(line,self.ERROR_FORMAT_NOT_CHAR)             # <<<<<<<<<<<<<<
  *             return values
  *         elif f.type == "Float":
  */
-      __pyx_t_19 = PyObject_Length(__pyx_v_v); if (unlikely(__pyx_t_19 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_19 = PyObject_Length(__pyx_v_v); if (unlikely(__pyx_t_19 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_6 = ((__pyx_t_19 != 1) != 0);
       if (__pyx_t_6) {
-        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_14 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_FORMAT_NOT_CHAR); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_14 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_FORMAT_NOT_CHAR); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_14);
-        __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_3);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_line);
@@ -14139,7 +14212,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
         PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_14);
         __Pyx_GIVEREF(__pyx_t_14);
         __pyx_t_14 = 0;
-        __pyx_t_14 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_14 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_3, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_14);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -14150,7 +14223,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     }
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-    /* "pysam/cvcf.pyx":633
+    /* "pysam/cvcf.pyx":638
  *             for v in values:
  *                 if len(v) != 1: self.error(line,self.ERROR_FORMAT_NOT_CHAR)
  *             return values             # <<<<<<<<<<<<<<
@@ -14163,20 +14236,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":634
+  /* "pysam/cvcf.pyx":639
  *                 if len(v) != 1: self.error(line,self.ERROR_FORMAT_NOT_CHAR)
  *             return values
  *         elif f.type == "Float":             # <<<<<<<<<<<<<<
  *             for idx,v in enumerate(values):
  *                 if v == ".": values[idx] = f.missingvalue
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_type); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Float, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_t_9, __pyx_n_s_Float, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   if (__pyx_t_6) {
 
-    /* "pysam/cvcf.pyx":635
+    /* "pysam/cvcf.pyx":640
  *             return values
  *         elif f.type == "Float":
  *             for idx,v in enumerate(values):             # <<<<<<<<<<<<<<
@@ -14189,7 +14262,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       __pyx_t_14 = __pyx_v_values; __Pyx_INCREF(__pyx_t_14); __pyx_t_7 = 0;
       __pyx_t_10 = NULL;
     } else {
-      __pyx_t_7 = -1; __pyx_t_14 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = -1; __pyx_t_14 = PyObject_GetIter(__pyx_v_values); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_14);
       __pyx_t_10 = Py_TYPE(__pyx_t_14)->tp_iternext;
     }
@@ -14197,16 +14270,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       if (!__pyx_t_10 && PyList_CheckExact(__pyx_t_14)) {
         if (__pyx_t_7 >= PyList_GET_SIZE(__pyx_t_14)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_3 = PyList_GET_ITEM(__pyx_t_14, __pyx_t_7); __Pyx_INCREF(__pyx_t_3); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = PyList_GET_ITEM(__pyx_t_14, __pyx_t_7); __Pyx_INCREF(__pyx_t_3); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_3 = PySequence_ITEM(__pyx_t_14, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = PySequence_ITEM(__pyx_t_14, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_10 && PyTuple_CheckExact(__pyx_t_14)) {
         if (__pyx_t_7 >= PyTuple_GET_SIZE(__pyx_t_14)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_14, __pyx_t_7); __Pyx_INCREF(__pyx_t_3); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_14, __pyx_t_7); __Pyx_INCREF(__pyx_t_3); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_3 = PySequence_ITEM(__pyx_t_14, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = PySequence_ITEM(__pyx_t_14, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_3 = __pyx_t_10(__pyx_t_14);
@@ -14214,7 +14287,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -14224,24 +14297,24 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       __pyx_t_3 = 0;
       __Pyx_INCREF(__pyx_t_9);
       __Pyx_XDECREF_SET(__pyx_v_idx, __pyx_t_9);
-      __pyx_t_3 = PyNumber_Add(__pyx_t_9, __pyx_int_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyNumber_Add(__pyx_t_9, __pyx_int_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_3);
       __Pyx_DECREF(__pyx_t_9);
       __pyx_t_9 = __pyx_t_3;
       __pyx_t_3 = 0;
 
-      /* "pysam/cvcf.pyx":636
+      /* "pysam/cvcf.pyx":641
  *         elif f.type == "Float":
  *             for idx,v in enumerate(values):
  *                 if v == ".": values[idx] = f.missingvalue             # <<<<<<<<<<<<<<
  *             try: return list(map(float,values))
  *             except:
  */
-      __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_v, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = (__Pyx_PyString_Equals(__pyx_v_v, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       if (__pyx_t_6) {
-        __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_missingvalue); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_f, __pyx_n_s_missingvalue); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_3);
-        if (unlikely(PyObject_SetItem(__pyx_v_values, __pyx_v_idx, __pyx_t_3) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(PyObject_SetItem(__pyx_v_values, __pyx_v_idx, __pyx_t_3) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
         goto __pyx_L27;
       }
@@ -14250,7 +14323,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-    /* "pysam/cvcf.pyx":637
+    /* "pysam/cvcf.pyx":642
  *             for idx,v in enumerate(values):
  *                 if v == ".": values[idx] = f.missingvalue
  *             try: return list(map(float,values))             # <<<<<<<<<<<<<<
@@ -14264,7 +14337,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       __Pyx_XGOTREF(__pyx_t_11);
       /*try:*/ {
         __Pyx_XDECREF(__pyx_r);
-        __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
+        __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_INCREF(((PyObject *)((PyObject*)(&PyFloat_Type))));
         PyTuple_SET_ITEM(__pyx_t_9, 0, ((PyObject *)((PyObject*)(&PyFloat_Type))));
@@ -14272,15 +14345,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
         __Pyx_INCREF(__pyx_v_values);
         PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_v_values);
         __Pyx_GIVEREF(__pyx_v_values);
-        __pyx_t_14 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_9, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
+        __pyx_t_14 = __Pyx_PyObject_Call(__pyx_builtin_map, __pyx_t_9, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
         __Pyx_GOTREF(__pyx_t_14);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
+        __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
         __Pyx_GOTREF(__pyx_t_9);
         PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_14);
         __Pyx_GIVEREF(__pyx_t_14);
         __pyx_t_14 = 0;
-        __pyx_t_14 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_9, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 637; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
+        __pyx_t_14 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_9, NULL); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 642; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
         __Pyx_GOTREF(__pyx_t_14);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __pyx_r = __pyx_t_14;
@@ -14302,7 +14375,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
       __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;
       __Pyx_XDECREF(__pyx_t_14); __pyx_t_14 = 0;
 
-      /* "pysam/cvcf.pyx":638
+      /* "pysam/cvcf.pyx":643
  *                 if v == ".": values[idx] = f.missingvalue
  *             try: return list(map(float,values))
  *             except:             # <<<<<<<<<<<<<<
@@ -14311,31 +14384,31 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
  */
       /*except:*/ {
         __Pyx_AddTraceback("pysam.cvcf.VCF.parse_formatdata", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_14, &__pyx_t_9, &__pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 638; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        if (__Pyx_GetException(&__pyx_t_14, &__pyx_t_9, &__pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 643; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_14);
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_GOTREF(__pyx_t_3);
 
-        /* "pysam/cvcf.pyx":639
+        /* "pysam/cvcf.pyx":644
  *             try: return list(map(float,values))
  *             except:
  *                 self.error(line,self.ERROR_FORMAT_NOT_NUMERICAL,"%s=%s" % (key, str(values)))             # <<<<<<<<<<<<<<
  *                 return [0.0] * len(values)
  *         else:
  */
-        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_FORMAT_NOT_NUMERICAL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_FORMAT_NOT_NUMERICAL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_18 = PyTuple_New(1); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_18 = PyTuple_New(1); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_18);
         __Pyx_INCREF(__pyx_v_values);
         PyTuple_SET_ITEM(__pyx_t_18, 0, __pyx_v_values);
         __Pyx_GIVEREF(__pyx_v_values);
-        __pyx_t_17 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_18, NULL); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_17 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_18, NULL); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_17);
         __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
-        __pyx_t_18 = PyTuple_New(2); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_18 = PyTuple_New(2); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_18);
         __Pyx_INCREF(__pyx_v_key);
         PyTuple_SET_ITEM(__pyx_t_18, 0, __pyx_v_key);
@@ -14343,10 +14416,10 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
         PyTuple_SET_ITEM(__pyx_t_18, 1, __pyx_t_17);
         __Pyx_GIVEREF(__pyx_t_17);
         __pyx_t_17 = 0;
-        __pyx_t_17 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_18); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_17 = __Pyx_PyString_Format(__pyx_kp_s_s_s, __pyx_t_18); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_17);
         __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
-        __pyx_t_18 = PyTuple_New(3); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_18 = PyTuple_New(3); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_18);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_18, 0, __pyx_v_line);
@@ -14357,13 +14430,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
         __Pyx_GIVEREF(__pyx_t_17);
         __pyx_t_2 = 0;
         __pyx_t_17 = 0;
-        __pyx_t_17 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_18, NULL); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 639; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_17 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_18, NULL); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 644; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_17);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
         __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
 
-        /* "pysam/cvcf.pyx":640
+        /* "pysam/cvcf.pyx":645
  *             except:
  *                 self.error(line,self.ERROR_FORMAT_NOT_NUMERICAL,"%s=%s" % (key, str(values)))
  *                 return [0.0] * len(values)             # <<<<<<<<<<<<<<
@@ -14371,8 +14444,8 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
  *             # can't happen
  */
         __Pyx_XDECREF(__pyx_r);
-        __pyx_t_7 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
-        __pyx_t_17 = PyList_New(1 * ((__pyx_t_7<0) ? 0:__pyx_t_7)); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 640; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_7 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_17 = PyList_New(1 * ((__pyx_t_7<0) ? 0:__pyx_t_7)); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_17);
         { Py_ssize_t __pyx_temp;
           for (__pyx_temp=0; __pyx_temp < __pyx_t_7; __pyx_temp++) {
@@ -14420,18 +14493,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":643
+    /* "pysam/cvcf.pyx":648
  *         else:
  *             # can't happen
  *             self.error(line,self.ERROR_INFO_STRING)             # <<<<<<<<<<<<<<
  * 
  *     def inregion(self, chrom, pos):
  */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 643; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_INFO_STRING); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 643; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_INFO_STRING); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_14 = PyTuple_New(2); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 643; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_14 = PyTuple_New(2); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_14);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_14, 0, __pyx_v_line);
@@ -14439,14 +14512,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
     PyTuple_SET_ITEM(__pyx_t_14, 1, __pyx_t_9);
     __Pyx_GIVEREF(__pyx_t_9);
     __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_14, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 643; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_14, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   }
 
-  /* "pysam/cvcf.pyx":603
+  /* "pysam/cvcf.pyx":608
  *         return ''.join(map(str,GTdata))
  * 
  *     def parse_formatdata( self, key, value, formatdict, line ):             # <<<<<<<<<<<<<<
@@ -14479,7 +14552,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_28parse_formatdata(CYTHON_UNUSED PyO
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":645
+/* "pysam/cvcf.pyx":650
  *             self.error(line,self.ERROR_INFO_STRING)
  * 
  *     def inregion(self, chrom, pos):             # <<<<<<<<<<<<<<
@@ -14521,16 +14594,16 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_31inregion(PyObject *__pyx_self, PyO
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_chrom)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("inregion", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("inregion", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_pos)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("inregion", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("inregion", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "inregion") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "inregion") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
       goto __pyx_L5_argtuple_error;
@@ -14545,7 +14618,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_31inregion(PyObject *__pyx_self, PyO
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("inregion", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("inregion", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.inregion", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -14576,16 +14649,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_30inregion(CYTHON_UNUSED PyObject *_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("inregion", 0);
 
-  /* "pysam/cvcf.pyx":646
+  /* "pysam/cvcf.pyx":651
  * 
  *     def inregion(self, chrom, pos):
  *         if not self._regions: return True             # <<<<<<<<<<<<<<
  *         for r in self._regions:
  *             if r[0] == chrom and r[1] <= pos < r[2]: return True
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_regions_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_regions_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_t_3 = ((!__pyx_t_2) != 0);
   if (__pyx_t_3) {
@@ -14595,20 +14668,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_30inregion(CYTHON_UNUSED PyObject *_
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":647
+  /* "pysam/cvcf.pyx":652
  *     def inregion(self, chrom, pos):
  *         if not self._regions: return True
  *         for r in self._regions:             # <<<<<<<<<<<<<<
  *             if r[0] == chrom and r[1] <= pos < r[2]: return True
  *         return False
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_regions_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_regions_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   if (PyList_CheckExact(__pyx_t_1) || PyTuple_CheckExact(__pyx_t_1)) {
     __pyx_t_4 = __pyx_t_1; __Pyx_INCREF(__pyx_t_4); __pyx_t_5 = 0;
     __pyx_t_6 = NULL;
   } else {
-    __pyx_t_5 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
     __pyx_t_6 = Py_TYPE(__pyx_t_4)->tp_iternext;
   }
@@ -14617,16 +14690,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_30inregion(CYTHON_UNUSED PyObject *_
     if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_4)) {
       if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_4)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_1 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_1 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_4)) {
       if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_4)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_5); __Pyx_INCREF(__pyx_t_1); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_1 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PySequence_ITEM(__pyx_t_4, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_1 = __pyx_t_6(__pyx_t_4);
@@ -14634,7 +14707,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_30inregion(CYTHON_UNUSED PyObject *_
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 647; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -14643,32 +14716,32 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_30inregion(CYTHON_UNUSED PyObject *_
     __Pyx_XDECREF_SET(__pyx_v_r, __pyx_t_1);
     __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":648
+    /* "pysam/cvcf.pyx":653
  *         if not self._regions: return True
  *         for r in self._regions:
  *             if r[0] == chrom and r[1] <= pos < r[2]: return True             # <<<<<<<<<<<<<<
  *         return False
  * 
  */
-    __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_r, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_r, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_7 = PyObject_RichCompare(__pyx_t_1, __pyx_v_chrom, Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = PyObject_RichCompare(__pyx_t_1, __pyx_v_chrom, Py_EQ); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     if (__pyx_t_3) {
-      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_r, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_r, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_7 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_1 = PyObject_RichCompare(__pyx_t_7, __pyx_v_pos, Py_LE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyObject_RichCompare(__pyx_t_7, __pyx_v_pos, Py_LE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       if (__Pyx_PyObject_IsTrue(__pyx_t_1)) {
         __Pyx_DECREF(__pyx_t_1);
-        __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_r, 2, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_r, 2, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_1 = PyObject_RichCompare(__pyx_v_pos, __pyx_t_8, Py_LT); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyObject_RichCompare(__pyx_v_pos, __pyx_t_8, Py_LT); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       }
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 648; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __pyx_t_9 = __pyx_t_2;
     } else {
@@ -14684,7 +14757,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_30inregion(CYTHON_UNUSED PyObject *_
   }
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/cvcf.pyx":649
+  /* "pysam/cvcf.pyx":654
  *         for r in self._regions:
  *             if r[0] == chrom and r[1] <= pos < r[2]: return True
  *         return False             # <<<<<<<<<<<<<<
@@ -14696,7 +14769,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_30inregion(CYTHON_UNUSED PyObject *_
   __pyx_r = Py_False;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":645
+  /* "pysam/cvcf.pyx":650
  *             self.error(line,self.ERROR_INFO_STRING)
  * 
  *     def inregion(self, chrom, pos):             # <<<<<<<<<<<<<<
@@ -14719,7 +14792,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_30inregion(CYTHON_UNUSED PyObject *_
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":651
+/* "pysam/cvcf.pyx":656
  *         return False
  * 
  *     def parse_data( self, line, lineparse=False ):             # <<<<<<<<<<<<<<
@@ -14762,7 +14835,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_33parse_data(PyObject *__pyx_self, P
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_line)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse_data", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 651; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse_data", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 656; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (kw_args > 0) {
@@ -14771,7 +14844,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_33parse_data(PyObject *__pyx_self, P
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_data") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 651; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse_data") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 656; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -14788,7 +14861,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_33parse_data(PyObject *__pyx_self, P
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("parse_data", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 651; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("parse_data", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 656; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.parse_data", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -14876,104 +14949,104 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse_data", 0);
 
-  /* "pysam/cvcf.pyx":652
+  /* "pysam/cvcf.pyx":657
  * 
  *     def parse_data( self, line, lineparse=False ):
  *         cols = line.split('\t')             # <<<<<<<<<<<<<<
  *         if len(cols) != len(self._samples)+9:
  *             # gracefully deal with absent FORMAT column
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_split); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 657; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__65, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__65, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 657; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_v_cols = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":653
+  /* "pysam/cvcf.pyx":658
  *     def parse_data( self, line, lineparse=False ):
  *         cols = line.split('\t')
  *         if len(cols) != len(self._samples)+9:             # <<<<<<<<<<<<<<
  *             # gracefully deal with absent FORMAT column
  *             # and those missing samples
  */
-  __pyx_t_3 = PyObject_Length(__pyx_v_cols); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_Length(__pyx_v_cols); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_4 = PyObject_Length(__pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyObject_Length(__pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_5 = ((__pyx_t_3 != (__pyx_t_4 + 9)) != 0);
   if (__pyx_t_5) {
 
-    /* "pysam/cvcf.pyx":656
+    /* "pysam/cvcf.pyx":661
  *             # gracefully deal with absent FORMAT column
  *             # and those missing samples
  *             if len(cols) == 8:             # <<<<<<<<<<<<<<
  *                 cols.append("")
  *             else:
  */
-    __pyx_t_4 = PyObject_Length(__pyx_v_cols); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyObject_Length(__pyx_v_cols); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_5 = ((__pyx_t_4 == 8) != 0);
     if (__pyx_t_5) {
 
-      /* "pysam/cvcf.pyx":657
+      /* "pysam/cvcf.pyx":662
  *             # and those missing samples
  *             if len(cols) == 8:
  *                 cols.append("")             # <<<<<<<<<<<<<<
  *             else:
  *                 self.error(line,
  */
-      __pyx_t_6 = __Pyx_PyObject_Append(__pyx_v_cols, __pyx_kp_s_); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 657; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyObject_Append(__pyx_v_cols, __pyx_kp_s_); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 662; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       goto __pyx_L4;
     }
     /*else*/ {
 
-      /* "pysam/cvcf.pyx":659
+      /* "pysam/cvcf.pyx":664
  *                 cols.append("")
  *             else:
  *                 self.error(line,             # <<<<<<<<<<<<<<
  *                            self.BAD_NUMBER_OF_COLUMNS,
  *                            "expected %s for %s samples (%s), got %s" % (len(self._samples)+9, len(self._samples), self._samples, len(cols)))
  */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
 
-      /* "pysam/cvcf.pyx":660
+      /* "pysam/cvcf.pyx":665
  *             else:
  *                 self.error(line,
  *                            self.BAD_NUMBER_OF_COLUMNS,             # <<<<<<<<<<<<<<
  *                            "expected %s for %s samples (%s), got %s" % (len(self._samples)+9, len(self._samples), self._samples, len(cols)))
  * 
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BAD_NUMBER_OF_COLUMNS); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 660; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BAD_NUMBER_OF_COLUMNS); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 665; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
 
-      /* "pysam/cvcf.pyx":661
+      /* "pysam/cvcf.pyx":666
  *                 self.error(line,
  *                            self.BAD_NUMBER_OF_COLUMNS,
  *                            "expected %s for %s samples (%s), got %s" % (len(self._samples)+9, len(self._samples), self._samples, len(cols)))             # <<<<<<<<<<<<<<
  * 
  *         chrom = cols[0]
  */
-      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_4 = PyObject_Length(__pyx_t_7); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyObject_Length(__pyx_t_7); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = PyInt_FromSsize_t((__pyx_t_4 + 9)); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyInt_FromSsize_t((__pyx_t_4 + 9)); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_4 = PyObject_Length(__pyx_t_8); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyObject_Length(__pyx_t_8); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      __pyx_t_8 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_4 = PyObject_Length(__pyx_v_cols); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_10 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyObject_Length(__pyx_v_cols); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
-      __pyx_t_11 = PyTuple_New(4); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyTuple_New(4); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_7);
       __Pyx_GIVEREF(__pyx_t_7);
@@ -14987,18 +15060,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __pyx_t_8 = 0;
       __pyx_t_9 = 0;
       __pyx_t_10 = 0;
-      __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_expected_s_for_s_samples_s_got_s, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 661; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_expected_s_for_s_samples_s_got_s, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
 
-      /* "pysam/cvcf.pyx":659
+      /* "pysam/cvcf.pyx":664
  *                 cols.append("")
  *             else:
  *                 self.error(line,             # <<<<<<<<<<<<<<
  *                            self.BAD_NUMBER_OF_COLUMNS,
  *                            "expected %s for %s samples (%s), got %s" % (len(self._samples)+9, len(self._samples), self._samples, len(cols)))
  */
-      __pyx_t_11 = PyTuple_New(3); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyTuple_New(3); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_v_line);
@@ -15009,7 +15082,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GIVEREF(__pyx_t_10);
       __pyx_t_1 = 0;
       __pyx_t_10 = 0;
-      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
@@ -15020,19 +15093,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L3:;
 
-  /* "pysam/cvcf.pyx":663
+  /* "pysam/cvcf.pyx":668
  *                            "expected %s for %s samples (%s), got %s" % (len(self._samples)+9, len(self._samples), self._samples, len(cols)))
  * 
  *         chrom = cols[0]             # <<<<<<<<<<<<<<
  * 
  *         # get 0-based position
  */
-  __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 663; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 668; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_10);
   __pyx_v_chrom = __pyx_t_10;
   __pyx_t_10 = 0;
 
-  /* "pysam/cvcf.pyx":666
+  /* "pysam/cvcf.pyx":671
  * 
  *         # get 0-based position
  *         try:    pos = int(cols[1])-1             # <<<<<<<<<<<<<<
@@ -15045,12 +15118,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __Pyx_XGOTREF(__pyx_t_13);
     __Pyx_XGOTREF(__pyx_t_14);
     /*try:*/ {
-      __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L5_error;};
+      __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L5_error;};
       __Pyx_GOTREF(__pyx_t_10);
-      __pyx_t_11 = PyNumber_Int(__pyx_t_10); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L5_error;}
+      __pyx_t_11 = PyNumber_Int(__pyx_t_10); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L5_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      __pyx_t_10 = PyNumber_Subtract(__pyx_t_11, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 666; __pyx_clineno = __LINE__; goto __pyx_L5_error;}
+      __pyx_t_10 = PyNumber_Subtract(__pyx_t_11, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L5_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
       __pyx_v_pos = __pyx_t_10;
@@ -15069,7 +15142,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __Pyx_XDECREF(__pyx_t_11); __pyx_t_11 = 0;
     __Pyx_XDECREF(__pyx_t_10); __pyx_t_10 = 0;
 
-    /* "pysam/cvcf.pyx":667
+    /* "pysam/cvcf.pyx":672
  *         # get 0-based position
  *         try:    pos = int(cols[1])-1
  *         except: self.error(line,self.POS_NOT_NUMERICAL)             # <<<<<<<<<<<<<<
@@ -15078,15 +15151,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
  */
     /*except:*/ {
       __Pyx_AddTraceback("pysam.cvcf.VCF.parse_data", __pyx_clineno, __pyx_lineno, __pyx_filename);
-      if (__Pyx_GetException(&__pyx_t_10, &__pyx_t_11, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
+      if (__Pyx_GetException(&__pyx_t_10, &__pyx_t_11, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 672; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_GOTREF(__pyx_t_11);
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 672; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_POS_NOT_NUMERICAL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_POS_NOT_NUMERICAL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 672; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
+      __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 672; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_line);
@@ -15094,7 +15167,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_9);
       __Pyx_GIVEREF(__pyx_t_9);
       __pyx_t_9 = 0;
-      __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
+      __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 672; __pyx_clineno = __LINE__; goto __pyx_L7_except_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
@@ -15118,23 +15191,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __pyx_L12_try_end:;
   }
 
-  /* "pysam/cvcf.pyx":668
+  /* "pysam/cvcf.pyx":673
  *         try:    pos = int(cols[1])-1
  *         except: self.error(line,self.POS_NOT_NUMERICAL)
  *         if pos < 0: self.error(line,self.POS_NOT_POSITIVE)             # <<<<<<<<<<<<<<
  * 
  *         # implement filtering
  */
-  if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 668; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-  __pyx_t_2 = PyObject_RichCompare(__pyx_v_pos, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 668; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 668; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 673; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+  __pyx_t_2 = PyObject_RichCompare(__pyx_v_pos, __pyx_int_0, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_5) {
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 668; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_POS_NOT_POSITIVE); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 668; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_POS_NOT_POSITIVE); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
-    __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 668; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_v_line);
@@ -15142,7 +15215,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_t_11);
     __Pyx_GIVEREF(__pyx_t_11);
     __pyx_t_11 = 0;
-    __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_10, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 668; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_10, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 673; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
@@ -15151,17 +15224,17 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L15:;
 
-  /* "pysam/cvcf.pyx":671
+  /* "pysam/cvcf.pyx":676
  * 
  *         # implement filtering
  *         if not self.inregion(chrom,pos): return None             # <<<<<<<<<<<<<<
  * 
  *         # end of first-pass parse for sortedVCF
  */
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_inregion); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_inregion); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-  __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 676; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+  __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   __Pyx_INCREF(__pyx_v_chrom);
   PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_v_chrom);
@@ -15169,11 +15242,11 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   __Pyx_INCREF(__pyx_v_pos);
   PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_v_pos);
   __Pyx_GIVEREF(__pyx_v_pos);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_10, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_10, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 676; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_t_15 = ((!__pyx_t_5) != 0);
   if (__pyx_t_15) {
@@ -15183,18 +15256,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":674
+  /* "pysam/cvcf.pyx":679
  * 
  *         # end of first-pass parse for sortedVCF
  *         if lineparse: return chrom, pos, line             # <<<<<<<<<<<<<<
  * 
  *         id = cols[2]
  */
-  __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_v_lineparse); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 674; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_v_lineparse); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 679; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_15) {
     __Pyx_XDECREF(__pyx_r);
-    if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 674; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-    __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 674; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 679; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+    __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 679; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_v_chrom);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_chrom);
@@ -15210,58 +15283,58 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":676
+  /* "pysam/cvcf.pyx":681
  *         if lineparse: return chrom, pos, line
  * 
  *         id = cols[2]             # <<<<<<<<<<<<<<
  * 
  *         ref = cols[3].upper()
  */
-  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 2, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 676; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 2, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_id = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":678
+  /* "pysam/cvcf.pyx":683
  *         id = cols[2]
  * 
  *         ref = cols[3].upper()             # <<<<<<<<<<<<<<
  *         if ref == ".":
  *             self.error(line,self.MISSING_REF)
  */
-  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 3, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 678; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 3, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 683; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_upper); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_upper); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 683; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 678; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 683; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   __pyx_v_ref = __pyx_t_2;
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":679
+  /* "pysam/cvcf.pyx":684
  * 
  *         ref = cols[3].upper()
  *         if ref == ".":             # <<<<<<<<<<<<<<
  *             self.error(line,self.MISSING_REF)
  *             if self._version == 33: ref = get_sequence(chrom,pos,pos+1,self._reference)
  */
-  __pyx_t_15 = (__Pyx_PyString_Equals(__pyx_v_ref, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 679; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = (__Pyx_PyString_Equals(__pyx_v_ref, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 684; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   if (__pyx_t_15) {
 
-    /* "pysam/cvcf.pyx":680
+    /* "pysam/cvcf.pyx":685
  *         ref = cols[3].upper()
  *         if ref == ".":
  *             self.error(line,self.MISSING_REF)             # <<<<<<<<<<<<<<
  *             if self._version == 33: ref = get_sequence(chrom,pos,pos+1,self._reference)
  *             else:                   ref = ""
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 680; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_MISSING_REF); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 680; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_MISSING_REF); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_11 = PyTuple_New(2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 680; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = PyTuple_New(2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __Pyx_INCREF(__pyx_v_line);
     PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_v_line);
@@ -15269,35 +15342,35 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     PyTuple_SET_ITEM(__pyx_t_11, 1, __pyx_t_10);
     __Pyx_GIVEREF(__pyx_t_10);
     __pyx_t_10 = 0;
-    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 680; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
 
-    /* "pysam/cvcf.pyx":681
+    /* "pysam/cvcf.pyx":686
  *         if ref == ".":
  *             self.error(line,self.MISSING_REF)
  *             if self._version == 33: ref = get_sequence(chrom,pos,pos+1,self._reference)             # <<<<<<<<<<<<<<
  *             else:                   ref = ""
  *         else:
  */
-    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_11 = PyObject_RichCompare(__pyx_t_10, __pyx_int_33, Py_EQ); __Pyx_XGOTREF(__pyx_t_11); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = PyObject_RichCompare(__pyx_t_10, __pyx_int_33, Py_EQ); __Pyx_XGOTREF(__pyx_t_11); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
     if (__pyx_t_15) {
-      __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
-      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-      __pyx_t_10 = PyNumber_Add(__pyx_v_pos, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+      __pyx_t_10 = PyNumber_Add(__pyx_v_pos, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_9 = PyTuple_New(4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyTuple_New(4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_INCREF(__pyx_v_chrom);
       PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_chrom);
@@ -15311,7 +15384,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_10 = 0;
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_9, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 681; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_9, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -15321,7 +15394,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     }
     /*else*/ {
 
-      /* "pysam/cvcf.pyx":682
+      /* "pysam/cvcf.pyx":687
  *             self.error(line,self.MISSING_REF)
  *             if self._version == 33: ref = get_sequence(chrom,pos,pos+1,self._reference)
  *             else:                   ref = ""             # <<<<<<<<<<<<<<
@@ -15336,7 +15409,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":684
+    /* "pysam/cvcf.pyx":689
  *             else:                   ref = ""
  *         else:
  *             for c in ref:             # <<<<<<<<<<<<<<
@@ -15347,7 +15420,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __pyx_t_2 = __pyx_v_ref; __Pyx_INCREF(__pyx_t_2); __pyx_t_4 = 0;
       __pyx_t_16 = NULL;
     } else {
-      __pyx_t_4 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_v_ref); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 684; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_v_ref); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 689; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __pyx_t_16 = Py_TYPE(__pyx_t_2)->tp_iternext;
     }
@@ -15355,16 +15428,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_2)) {
         if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_2)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_9 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 684; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 689; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_9 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 684; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 689; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_2)) {
         if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_9 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 684; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 689; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_9 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 684; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 689; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_9 = __pyx_t_16(__pyx_t_2);
@@ -15372,7 +15445,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 684; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 689; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -15381,21 +15454,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_XDECREF_SET(__pyx_v_c, __pyx_t_9);
       __pyx_t_9 = 0;
 
-      /* "pysam/cvcf.pyx":685
+      /* "pysam/cvcf.pyx":690
  *         else:
  *             for c in ref:
  *                 if c not in "ACGTN": self.error(line,self.UNKNOWN_CHAR_IN_REF)             # <<<<<<<<<<<<<<
  *             if "N" in ref: ref = get_sequence(chrom,pos,pos+len(ref),self._reference)
  * 
  */
-      __pyx_t_15 = (__Pyx_PySequence_Contains(__pyx_v_c, __pyx_n_s_ACGTN, Py_NE)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_15 = (__Pyx_PySequence_Contains(__pyx_v_c, __pyx_n_s_ACGTN, Py_NE)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_5 = (__pyx_t_15 != 0);
       if (__pyx_t_5) {
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_UNKNOWN_CHAR_IN_REF); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_UNKNOWN_CHAR_IN_REF); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
-        __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_v_line);
@@ -15403,7 +15476,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_t_11);
         __Pyx_GIVEREF(__pyx_t_11);
         __pyx_t_11 = 0;
-        __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_10, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 685; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_10, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
@@ -15414,29 +15487,29 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     }
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":686
+    /* "pysam/cvcf.pyx":691
  *             for c in ref:
  *                 if c not in "ACGTN": self.error(line,self.UNKNOWN_CHAR_IN_REF)
  *             if "N" in ref: ref = get_sequence(chrom,pos,pos+len(ref),self._reference)             # <<<<<<<<<<<<<<
  * 
  *         # make sure reference is sane
  */
-    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_n_s_N, __pyx_v_ref, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_n_s_N, __pyx_v_ref, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_15 = (__pyx_t_5 != 0);
     if (__pyx_t_15) {
-      __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-      __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_11 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+      __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
-      __pyx_t_10 = PyNumber_Add(__pyx_v_pos, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyNumber_Add(__pyx_v_pos, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-      __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
-      __pyx_t_9 = PyTuple_New(4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyTuple_New(4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_INCREF(__pyx_v_chrom);
       PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_chrom);
@@ -15450,7 +15523,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GIVEREF(__pyx_t_11);
       __pyx_t_10 = 0;
       __pyx_t_11 = 0;
-      __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 686; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -15462,41 +15535,41 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L18:;
 
-  /* "pysam/cvcf.pyx":689
+  /* "pysam/cvcf.pyx":694
  * 
  *         # make sure reference is sane
  *         if self._reference:             # <<<<<<<<<<<<<<
  *             left = max(0,pos-100)
  *             faref_leftflank = get_sequence(chrom,left,pos+len(ref),self._reference)
  */
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 689; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 689; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
   if (__pyx_t_15) {
 
-    /* "pysam/cvcf.pyx":690
+    /* "pysam/cvcf.pyx":695
  *         # make sure reference is sane
  *         if self._reference:
  *             left = max(0,pos-100)             # <<<<<<<<<<<<<<
  *             faref_leftflank = get_sequence(chrom,left,pos+len(ref),self._reference)
  *             faref = faref_leftflank[pos-left:]
  */
-    if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-    __pyx_t_11 = PyNumber_Subtract(__pyx_v_pos, __pyx_int_100); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+    __pyx_t_11 = PyNumber_Subtract(__pyx_v_pos, __pyx_int_100); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __pyx_t_17 = 0;
-    __pyx_t_2 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_10 = PyObject_RichCompare(__pyx_t_11, __pyx_t_2, Py_GT); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyObject_RichCompare(__pyx_t_11, __pyx_t_2, Py_GT); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     if (__pyx_t_15) {
       __Pyx_INCREF(__pyx_t_11);
       __pyx_t_9 = __pyx_t_11;
     } else {
-      __pyx_t_10 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyInt_From_long(__pyx_t_17); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __pyx_t_9 = __pyx_t_10;
       __pyx_t_10 = 0;
@@ -15508,25 +15581,25 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __pyx_v_left = __pyx_t_11;
     __pyx_t_11 = 0;
 
-    /* "pysam/cvcf.pyx":691
+    /* "pysam/cvcf.pyx":696
  *         if self._reference:
  *             left = max(0,pos-100)
  *             faref_leftflank = get_sequence(chrom,left,pos+len(ref),self._reference)             # <<<<<<<<<<<<<<
  *             faref = faref_leftflank[pos-left:]
  *             if faref != ref: self.error(line,self.WRONG_REF,"(reference is %s, VCF says %s)" % (faref,ref))
  */
-    __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
-    if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-    __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_9 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+    __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_10 = PyNumber_Add(__pyx_v_pos, __pyx_t_9); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyNumber_Add(__pyx_v_pos, __pyx_t_9); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_INCREF(__pyx_v_chrom);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_chrom);
@@ -15540,45 +15613,45 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __Pyx_GIVEREF(__pyx_t_9);
     __pyx_t_10 = 0;
     __pyx_t_9 = 0;
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_2, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_2, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 696; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_v_faref_leftflank = __pyx_t_9;
     __pyx_t_9 = 0;
 
-    /* "pysam/cvcf.pyx":692
+    /* "pysam/cvcf.pyx":697
  *             left = max(0,pos-100)
  *             faref_leftflank = get_sequence(chrom,left,pos+len(ref),self._reference)
  *             faref = faref_leftflank[pos-left:]             # <<<<<<<<<<<<<<
  *             if faref != ref: self.error(line,self.WRONG_REF,"(reference is %s, VCF says %s)" % (faref,ref))
  *             ref = faref
  */
-    if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 692; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-    __pyx_t_9 = PyNumber_Subtract(__pyx_v_pos, __pyx_v_left); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+    __pyx_t_9 = PyNumber_Subtract(__pyx_v_pos, __pyx_v_left); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_faref_leftflank, 0, 0, &__pyx_t_9, NULL, NULL, 0, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_faref_leftflank, 0, 0, &__pyx_t_9, NULL, NULL, 0, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __pyx_v_faref = __pyx_t_2;
     __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":693
+    /* "pysam/cvcf.pyx":698
  *             faref_leftflank = get_sequence(chrom,left,pos+len(ref),self._reference)
  *             faref = faref_leftflank[pos-left:]
  *             if faref != ref: self.error(line,self.WRONG_REF,"(reference is %s, VCF says %s)" % (faref,ref))             # <<<<<<<<<<<<<<
  *             ref = faref
  * 
  */
-    __pyx_t_2 = PyObject_RichCompare(__pyx_v_faref, __pyx_v_ref, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyObject_RichCompare(__pyx_v_faref, __pyx_v_ref, Py_NE); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (__pyx_t_15) {
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_WRONG_REF); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_WRONG_REF); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_11 = PyTuple_New(2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyTuple_New(2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __Pyx_INCREF(__pyx_v_faref);
       PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_v_faref);
@@ -15586,10 +15659,10 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_INCREF(__pyx_v_ref);
       PyTuple_SET_ITEM(__pyx_t_11, 1, __pyx_v_ref);
       __Pyx_GIVEREF(__pyx_v_ref);
-      __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_reference_is_s_VCF_says_s, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyString_Format(__pyx_kp_s_reference_is_s_VCF_says_s, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-      __pyx_t_11 = PyTuple_New(3); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyTuple_New(3); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_v_line);
@@ -15600,7 +15673,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GIVEREF(__pyx_t_10);
       __pyx_t_9 = 0;
       __pyx_t_10 = 0;
-      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_11, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
@@ -15609,7 +15682,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     }
     __pyx_L25:;
 
-    /* "pysam/cvcf.pyx":694
+    /* "pysam/cvcf.pyx":699
  *             faref = faref_leftflank[pos-left:]
  *             if faref != ref: self.error(line,self.WRONG_REF,"(reference is %s, VCF says %s)" % (faref,ref))
  *             ref = faref             # <<<<<<<<<<<<<<
@@ -15622,19 +15695,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L24:;
 
-  /* "pysam/cvcf.pyx":697
+  /* "pysam/cvcf.pyx":702
  * 
  *         # convert v3.3 to v4.0 alleles below
  *         if cols[4] == ".": alt = []             # <<<<<<<<<<<<<<
  *         else: alt = cols[4].upper().split(',')
  * 
  */
-  __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 4, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 4, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_10);
-  __pyx_t_15 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   if (__pyx_t_15) {
-    __pyx_t_10 = PyList_New(0); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = PyList_New(0); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __pyx_v_alt = __pyx_t_10;
     __pyx_t_10 = 0;
@@ -15642,25 +15715,25 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":698
+    /* "pysam/cvcf.pyx":703
  *         # convert v3.3 to v4.0 alleles below
  *         if cols[4] == ".": alt = []
  *         else: alt = cols[4].upper().split(',')             # <<<<<<<<<<<<<<
  * 
  *         if cols[5] == ".": qual = -1
  */
-    __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 4, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 4, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_upper); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_upper); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_split); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_split); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_tuple__66, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_tuple__66, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
     __pyx_v_alt = __pyx_t_10;
@@ -15668,16 +15741,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L26:;
 
-  /* "pysam/cvcf.pyx":700
+  /* "pysam/cvcf.pyx":705
  *         else: alt = cols[4].upper().split(',')
  * 
  *         if cols[5] == ".": qual = -1             # <<<<<<<<<<<<<<
  *         else:
  *             try:    qual = float(cols[5])
  */
-  __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 5, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 5, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_10);
-  __pyx_t_15 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = (__Pyx_PyString_Equals(__pyx_t_10, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   if (__pyx_t_15) {
     __pyx_v_qual = -1.0;
@@ -15685,7 +15758,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":702
+    /* "pysam/cvcf.pyx":707
  *         if cols[5] == ".": qual = -1
  *         else:
  *             try:    qual = float(cols[5])             # <<<<<<<<<<<<<<
@@ -15698,9 +15771,9 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_XGOTREF(__pyx_t_13);
       __Pyx_XGOTREF(__pyx_t_12);
       /*try:*/ {
-        __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 5, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L28_error;};
+        __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_cols, 5, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L28_error;};
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_18 = __Pyx_PyObject_AsDouble(__pyx_t_10); if (unlikely(__pyx_t_18 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
+        __pyx_t_18 = __Pyx_PyObject_AsDouble(__pyx_t_10); if (unlikely(__pyx_t_18 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L28_error;}
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
         __pyx_v_qual = __pyx_t_18;
       }
@@ -15717,7 +15790,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_XDECREF(__pyx_t_11); __pyx_t_11 = 0;
       __Pyx_XDECREF(__pyx_t_10); __pyx_t_10 = 0;
 
-      /* "pysam/cvcf.pyx":703
+      /* "pysam/cvcf.pyx":708
  *         else:
  *             try:    qual = float(cols[5])
  *             except: self.error(line,self.QUAL_NOT_NUMERICAL)             # <<<<<<<<<<<<<<
@@ -15726,15 +15799,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
  */
       /*except:*/ {
         __Pyx_AddTraceback("pysam.cvcf.VCF.parse_data", __pyx_clineno, __pyx_lineno, __pyx_filename);
-        if (__Pyx_GetException(&__pyx_t_10, &__pyx_t_11, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        if (__Pyx_GetException(&__pyx_t_10, &__pyx_t_11, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 708; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 708; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_QUAL_NOT_NUMERICAL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_QUAL_NOT_NUMERICAL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 708; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 708; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_line);
@@ -15742,7 +15815,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_8);
         __Pyx_GIVEREF(__pyx_t_8);
         __pyx_t_8 = 0;
-        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
+        __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_1, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 708; __pyx_clineno = __LINE__; goto __pyx_L30_except_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -15768,26 +15841,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L27:;
 
-  /* "pysam/cvcf.pyx":706
+  /* "pysam/cvcf.pyx":711
  * 
  *         # postpone checking that filters exist.  Encode missing filter or no filtering as empty list
  *         if cols[6] == "." or cols[6] == "PASS" or cols[6] == "0": filter = []             # <<<<<<<<<<<<<<
  *         else: filter = cols[6].split(';')
  * 
  */
-  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 6, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 6, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_15 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s__8, Py_EQ)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (!__pyx_t_15) {
-    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 6, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 6, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_n_s_PASS, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_n_s_PASS, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (!__pyx_t_5) {
-      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 6, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 6, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_19 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s_0, Py_EQ)); if (unlikely(__pyx_t_19 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_19 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s_0, Py_EQ)); if (unlikely(__pyx_t_19 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __pyx_t_20 = __pyx_t_19;
     } else {
@@ -15798,7 +15871,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __pyx_t_5 = __pyx_t_15;
   }
   if (__pyx_t_5) {
-    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 706; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_v_filter = __pyx_t_2;
     __pyx_t_2 = 0;
@@ -15806,19 +15879,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":707
+    /* "pysam/cvcf.pyx":712
  *         # postpone checking that filters exist.  Encode missing filter or no filtering as empty list
  *         if cols[6] == "." or cols[6] == "PASS" or cols[6] == "0": filter = []
  *         else: filter = cols[6].split(';')             # <<<<<<<<<<<<<<
  * 
  *         # dictionary of keys, and list of values
  */
-    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 6, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 6, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_tuple__67, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_tuple__67, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
     __pyx_v_filter = __pyx_t_2;
@@ -15826,51 +15899,51 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L38:;
 
-  /* "pysam/cvcf.pyx":710
+  /* "pysam/cvcf.pyx":715
  * 
  *         # dictionary of keys, and list of values
  *         info = {}             # <<<<<<<<<<<<<<
  *         if cols[7] != ".":
  *             for blurp in cols[7].split(';'):
  */
-  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 710; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __pyx_v_info = ((PyObject*)__pyx_t_2);
   __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":711
+  /* "pysam/cvcf.pyx":716
  *         # dictionary of keys, and list of values
  *         info = {}
  *         if cols[7] != ".":             # <<<<<<<<<<<<<<
  *             for blurp in cols[7].split(';'):
  *                 elts = blurp.split('=')
  */
-  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 7, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 7, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s__8, Py_NE)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 711; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_t_2, __pyx_kp_s__8, Py_NE)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_5) {
 
-    /* "pysam/cvcf.pyx":712
+    /* "pysam/cvcf.pyx":717
  *         info = {}
  *         if cols[7] != ".":
  *             for blurp in cols[7].split(';'):             # <<<<<<<<<<<<<<
  *                 elts = blurp.split('=')
  *                 if len(elts) == 1: v = None
  */
-    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 7, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_cols, 7, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_split); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_tuple__68, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_tuple__68, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
     if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) {
       __pyx_t_11 = __pyx_t_2; __Pyx_INCREF(__pyx_t_11); __pyx_t_4 = 0;
       __pyx_t_16 = NULL;
     } else {
-      __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __pyx_t_16 = Py_TYPE(__pyx_t_11)->tp_iternext;
     }
@@ -15879,16 +15952,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_11)) {
         if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_11)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_11)) {
         if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_11)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_2 = __pyx_t_16(__pyx_t_11);
@@ -15896,7 +15969,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -15905,29 +15978,29 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_XDECREF_SET(__pyx_v_blurp, __pyx_t_2);
       __pyx_t_2 = 0;
 
-      /* "pysam/cvcf.pyx":713
+      /* "pysam/cvcf.pyx":718
  *         if cols[7] != ".":
  *             for blurp in cols[7].split(';'):
  *                 elts = blurp.split('=')             # <<<<<<<<<<<<<<
  *                 if len(elts) == 1: v = None
  *                 elif len(elts) == 2: v = elts[1]
  */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_blurp, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_blurp, __pyx_n_s_split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__69, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__69, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_XDECREF_SET(__pyx_v_elts, __pyx_t_10);
       __pyx_t_10 = 0;
 
-      /* "pysam/cvcf.pyx":714
+      /* "pysam/cvcf.pyx":719
  *             for blurp in cols[7].split(';'):
  *                 elts = blurp.split('=')
  *                 if len(elts) == 1: v = None             # <<<<<<<<<<<<<<
  *                 elif len(elts) == 2: v = elts[1]
  *                 else: self.error(line,self.ERROR_INFO_STRING)
  */
-      __pyx_t_3 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_5 = ((__pyx_t_3 == 1) != 0);
       if (__pyx_t_5) {
         __Pyx_INCREF(Py_None);
@@ -15935,17 +16008,17 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         goto __pyx_L42;
       }
 
-      /* "pysam/cvcf.pyx":715
+      /* "pysam/cvcf.pyx":720
  *                 elts = blurp.split('=')
  *                 if len(elts) == 1: v = None
  *                 elif len(elts) == 2: v = elts[1]             # <<<<<<<<<<<<<<
  *                 else: self.error(line,self.ERROR_INFO_STRING)
  *                 info[elts[0]] = self.parse_formatdata(elts[0],
  */
-      __pyx_t_3 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyObject_Length(__pyx_v_elts); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_5 = ((__pyx_t_3 == 2) != 0);
       if (__pyx_t_5) {
-        __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 715; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_elts, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 720; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_XDECREF_SET(__pyx_v_v, __pyx_t_10);
         __pyx_t_10 = 0;
@@ -15953,18 +16026,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":716
+        /* "pysam/cvcf.pyx":721
  *                 if len(elts) == 1: v = None
  *                 elif len(elts) == 2: v = elts[1]
  *                 else: self.error(line,self.ERROR_INFO_STRING)             # <<<<<<<<<<<<<<
  *                 info[elts[0]] = self.parse_formatdata(elts[0],
  *                                                       v,
  */
-        __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_INFO_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ERROR_INFO_STRING); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_line);
@@ -15972,7 +16045,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_2);
         __Pyx_GIVEREF(__pyx_t_2);
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_8, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 716; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_8, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 721; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
@@ -15980,45 +16053,45 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       __pyx_L42:;
 
-      /* "pysam/cvcf.pyx":717
+      /* "pysam/cvcf.pyx":722
  *                 elif len(elts) == 2: v = elts[1]
  *                 else: self.error(line,self.ERROR_INFO_STRING)
  *                 info[elts[0]] = self.parse_formatdata(elts[0],             # <<<<<<<<<<<<<<
  *                                                       v,
  *                                                       self._info,
  */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_formatdata); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_formatdata); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_8);
 
-      /* "pysam/cvcf.pyx":718
+      /* "pysam/cvcf.pyx":723
  *                 else: self.error(line,self.ERROR_INFO_STRING)
  *                 info[elts[0]] = self.parse_formatdata(elts[0],
  *                                                       v,             # <<<<<<<<<<<<<<
  *                                                       self._info,
  *                                                       line)
  */
-      if (unlikely(!__pyx_v_v)) { __Pyx_RaiseUnboundLocalError("v"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 718; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+      if (unlikely(!__pyx_v_v)) { __Pyx_RaiseUnboundLocalError("v"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 723; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
 
-      /* "pysam/cvcf.pyx":719
+      /* "pysam/cvcf.pyx":724
  *                 info[elts[0]] = self.parse_formatdata(elts[0],
  *                                                       v,
  *                                                       self._info,             # <<<<<<<<<<<<<<
  *                                                       line)
  * 
  */
-      __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 719; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
 
-      /* "pysam/cvcf.pyx":717
+      /* "pysam/cvcf.pyx":722
  *                 elif len(elts) == 2: v = elts[1]
  *                 else: self.error(line,self.ERROR_INFO_STRING)
  *                 info[elts[0]] = self.parse_formatdata(elts[0],             # <<<<<<<<<<<<<<
  *                                                       v,
  *                                                       self._info,
  */
-      __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_8);
       __Pyx_GIVEREF(__pyx_t_8);
@@ -16032,13 +16105,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GIVEREF(__pyx_v_line);
       __pyx_t_8 = 0;
       __pyx_t_10 = 0;
-      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_elts, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_1);
-      if (unlikely(PyDict_SetItem(__pyx_v_info, __pyx_t_1, __pyx_t_10) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyDict_SetItem(__pyx_v_info, __pyx_t_1, __pyx_t_10) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 722; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     }
@@ -16047,19 +16120,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L39:;
 
-  /* "pysam/cvcf.pyx":723
+  /* "pysam/cvcf.pyx":728
  * 
  *         # Gracefully deal with absent FORMAT column
  *         if cols[8] == "": format = []             # <<<<<<<<<<<<<<
  *         else: format = cols[8].split(':')
  * 
  */
-  __pyx_t_11 = __Pyx_GetItemInt(__pyx_v_cols, 8, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 723; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_11 = __Pyx_GetItemInt(__pyx_v_cols, 8, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_11);
-  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_t_11, __pyx_kp_s_, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 723; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = (__Pyx_PyString_Equals(__pyx_t_11, __pyx_kp_s_, Py_EQ)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
   if (__pyx_t_5) {
-    __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 723; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __pyx_v_format = __pyx_t_11;
     __pyx_t_11 = 0;
@@ -16067,19 +16140,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":724
+    /* "pysam/cvcf.pyx":729
  *         # Gracefully deal with absent FORMAT column
  *         if cols[8] == "": format = []
  *         else: format = cols[8].split(':')             # <<<<<<<<<<<<<<
  * 
  *         # check: all filters are defined
  */
-    __pyx_t_11 = __Pyx_GetItemInt(__pyx_v_cols, 8, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_11 = __Pyx_GetItemInt(__pyx_v_cols, 8, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 729; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_11);
-    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_t_11, __pyx_n_s_split); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_t_11, __pyx_n_s_split); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 729; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-    __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_tuple__70, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_tuple__70, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 729; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     __pyx_v_format = __pyx_t_11;
@@ -16087,7 +16160,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L43:;
 
-  /* "pysam/cvcf.pyx":727
+  /* "pysam/cvcf.pyx":732
  * 
  *         # check: all filters are defined
  *         for f in filter:             # <<<<<<<<<<<<<<
@@ -16098,7 +16171,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __pyx_t_11 = __pyx_v_filter; __Pyx_INCREF(__pyx_t_11); __pyx_t_4 = 0;
     __pyx_t_16 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_filter); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_filter); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __pyx_t_16 = Py_TYPE(__pyx_t_11)->tp_iternext;
   }
@@ -16106,16 +16179,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_11)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_11)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_10 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_10 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_11)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_11)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_10 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_10 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_10 = __pyx_t_16(__pyx_t_11);
@@ -16123,7 +16196,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 727; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -16132,24 +16205,24 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __Pyx_XDECREF_SET(__pyx_v_f, __pyx_t_10);
     __pyx_t_10 = 0;
 
-    /* "pysam/cvcf.pyx":728
+    /* "pysam/cvcf.pyx":733
  *         # check: all filters are defined
  *         for f in filter:
  *             if f not in self._filter: self.error(line,self.FILTER_NOT_DEFINED, f)             # <<<<<<<<<<<<<<
  * 
  *         # check: format fields are defined
  */
-    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_filter); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_filter); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_v_f, __pyx_t_10, Py_NE)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = (__Pyx_PySequence_Contains(__pyx_v_f, __pyx_t_10, Py_NE)); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
     __pyx_t_15 = (__pyx_t_5 != 0);
     if (__pyx_t_15) {
-      __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_FILTER_NOT_DEFINED); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_FILTER_NOT_DEFINED); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_line);
@@ -16160,7 +16233,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_f);
       __Pyx_GIVEREF(__pyx_v_f);
       __pyx_t_1 = 0;
-      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 728; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -16171,20 +16244,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
 
-  /* "pysam/cvcf.pyx":731
+  /* "pysam/cvcf.pyx":736
  * 
  *         # check: format fields are defined
  *         if self._format:             # <<<<<<<<<<<<<<
  *             for f in format:
  *                 if f not in self._format: self.error(line,self.FORMAT_NOT_DEFINED, f)
  */
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 731; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 731; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
   if (__pyx_t_15) {
 
-    /* "pysam/cvcf.pyx":732
+    /* "pysam/cvcf.pyx":737
  *         # check: format fields are defined
  *         if self._format:
  *             for f in format:             # <<<<<<<<<<<<<<
@@ -16195,7 +16268,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __pyx_t_11 = __pyx_v_format; __Pyx_INCREF(__pyx_t_11); __pyx_t_4 = 0;
       __pyx_t_16 = NULL;
     } else {
-      __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_format); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_format); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __pyx_t_16 = Py_TYPE(__pyx_t_11)->tp_iternext;
     }
@@ -16203,16 +16276,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_11)) {
         if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_11)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_1); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_1); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_11)) {
         if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_11)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_1); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_1); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_1 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_1 = __pyx_t_16(__pyx_t_11);
@@ -16220,7 +16293,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -16229,24 +16302,24 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_XDECREF_SET(__pyx_v_f, __pyx_t_1);
       __pyx_t_1 = 0;
 
-      /* "pysam/cvcf.pyx":733
+      /* "pysam/cvcf.pyx":738
  *         if self._format:
  *             for f in format:
  *                 if f not in self._format: self.error(line,self.FORMAT_NOT_DEFINED, f)             # <<<<<<<<<<<<<<
  * 
  *         # convert v3.3 alleles
  */
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_15 = (__Pyx_PySequence_Contains(__pyx_v_f, __pyx_t_1, Py_NE)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_15 = (__Pyx_PySequence_Contains(__pyx_v_f, __pyx_t_1, Py_NE)); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __pyx_t_5 = (__pyx_t_15 != 0);
       if (__pyx_t_5) {
-        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_FORMAT_NOT_DEFINED); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_FORMAT_NOT_DEFINED); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_10 = PyTuple_New(3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PyTuple_New(3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_v_line);
@@ -16257,7 +16330,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_10, 2, __pyx_v_f);
         __Pyx_GIVEREF(__pyx_v_f);
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_10, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_10, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
@@ -16271,36 +16344,36 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L47:;
 
-  /* "pysam/cvcf.pyx":736
+  /* "pysam/cvcf.pyx":741
  * 
  *         # convert v3.3 alleles
  *         if self._version == 33:             # <<<<<<<<<<<<<<
  *             if len(ref) != 1: self.error(line,self.V33_BAD_REF)
  *             newalts = []
  */
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 741; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_11, __pyx_int_33, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_11, __pyx_int_33, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 741; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 741; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_5) {
 
-    /* "pysam/cvcf.pyx":737
+    /* "pysam/cvcf.pyx":742
  *         # convert v3.3 alleles
  *         if self._version == 33:
  *             if len(ref) != 1: self.error(line,self.V33_BAD_REF)             # <<<<<<<<<<<<<<
  *             newalts = []
  *             have_deletions = False
  */
-    __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_5 = ((__pyx_t_4 != 1) != 0);
     if (__pyx_t_5) {
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V33_BAD_REF); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V33_BAD_REF); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
-      __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = PyTuple_New(2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_v_line);
@@ -16308,7 +16381,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_t_11);
       __Pyx_GIVEREF(__pyx_t_11);
       __pyx_t_11 = 0;
-      __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_10, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_10, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
@@ -16317,19 +16390,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     }
     __pyx_L52:;
 
-    /* "pysam/cvcf.pyx":738
+    /* "pysam/cvcf.pyx":743
  *         if self._version == 33:
  *             if len(ref) != 1: self.error(line,self.V33_BAD_REF)
  *             newalts = []             # <<<<<<<<<<<<<<
  *             have_deletions = False
  *             for a in alt:
  */
-    __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __pyx_v_newalts = ((PyObject*)__pyx_t_11);
     __pyx_t_11 = 0;
 
-    /* "pysam/cvcf.pyx":739
+    /* "pysam/cvcf.pyx":744
  *             if len(ref) != 1: self.error(line,self.V33_BAD_REF)
  *             newalts = []
  *             have_deletions = False             # <<<<<<<<<<<<<<
@@ -16338,7 +16411,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
  */
     __pyx_v_have_deletions = 0;
 
-    /* "pysam/cvcf.pyx":740
+    /* "pysam/cvcf.pyx":745
  *             newalts = []
  *             have_deletions = False
  *             for a in alt:             # <<<<<<<<<<<<<<
@@ -16349,7 +16422,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __pyx_t_11 = __pyx_v_alt; __Pyx_INCREF(__pyx_t_11); __pyx_t_4 = 0;
       __pyx_t_16 = NULL;
     } else {
-      __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __pyx_t_16 = Py_TYPE(__pyx_t_11)->tp_iternext;
     }
@@ -16357,16 +16430,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_11)) {
         if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_11)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_10 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_10 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_11)) {
         if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_11)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_10 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_10); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_10 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_10 = __pyx_t_16(__pyx_t_11);
@@ -16374,7 +16447,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 740; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -16383,19 +16456,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_XDECREF_SET(__pyx_v_a, __pyx_t_10);
       __pyx_t_10 = 0;
 
-      /* "pysam/cvcf.pyx":741
+      /* "pysam/cvcf.pyx":746
  *             have_deletions = False
  *             for a in alt:
  *                 if len(a) == 1: a = a + ref[1:]                       # SNP; add trailing reference             # <<<<<<<<<<<<<<
  *                 elif a.startswith('I'): a = ref[0] + a[1:] + ref[1:]  # insertion just beyond pos; add first and trailing reference
  *                 elif a.startswith('D'): # allow D<seq> and D<num>
  */
-      __pyx_t_3 = PyObject_Length(__pyx_v_a); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 741; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyObject_Length(__pyx_v_a); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 746; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_5 = ((__pyx_t_3 == 1) != 0);
       if (__pyx_t_5) {
-        __pyx_t_10 = __Pyx_PyObject_GetSlice(__pyx_v_ref, 1, 0, NULL, NULL, &__pyx_slice__71, 1, 0, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 741; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = __Pyx_PyObject_GetSlice(__pyx_v_ref, 1, 0, NULL, NULL, &__pyx_slice__71, 1, 0, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 746; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_2 = PyNumber_Add(__pyx_v_a, __pyx_t_10); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 741; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyNumber_Add(__pyx_v_a, __pyx_t_10); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 746; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
         __Pyx_DECREF_SET(__pyx_v_a, __pyx_t_2);
@@ -16403,32 +16476,32 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         goto __pyx_L55;
       }
 
-      /* "pysam/cvcf.pyx":742
+      /* "pysam/cvcf.pyx":747
  *             for a in alt:
  *                 if len(a) == 1: a = a + ref[1:]                       # SNP; add trailing reference
  *                 elif a.startswith('I'): a = ref[0] + a[1:] + ref[1:]  # insertion just beyond pos; add first and trailing reference             # <<<<<<<<<<<<<<
  *                 elif a.startswith('D'): # allow D<seq> and D<num>
  *                     have_deletions = True
  */
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_a, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_a, __pyx_n_s_startswith); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__72, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__72, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
       if (__pyx_t_5) {
-        __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_ref, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_10 = __Pyx_GetItemInt(__pyx_v_ref, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_10);
-        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_a, 1, 0, NULL, NULL, &__pyx_slice__73, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_a, 1, 0, NULL, NULL, &__pyx_slice__73, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_1 = PyNumber_Add(__pyx_t_10, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyNumber_Add(__pyx_t_10, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_1);
         __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_ref, 1, 0, NULL, NULL, &__pyx_slice__74, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_ref, 1, 0, NULL, NULL, &__pyx_slice__74, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_10 = PyNumber_Add(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_10 = PyNumber_Add(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_10);
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -16437,23 +16510,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         goto __pyx_L55;
       }
 
-      /* "pysam/cvcf.pyx":743
+      /* "pysam/cvcf.pyx":748
  *                 if len(a) == 1: a = a + ref[1:]                       # SNP; add trailing reference
  *                 elif a.startswith('I'): a = ref[0] + a[1:] + ref[1:]  # insertion just beyond pos; add first and trailing reference
  *                 elif a.startswith('D'): # allow D<seq> and D<num>             # <<<<<<<<<<<<<<
  *                     have_deletions = True
  *                     try:
  */
-      __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_a, __pyx_n_s_startswith); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_a, __pyx_n_s_startswith); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_10);
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_tuple__75, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_tuple__75, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       if (__pyx_t_5) {
 
-        /* "pysam/cvcf.pyx":744
+        /* "pysam/cvcf.pyx":749
  *                 elif a.startswith('I'): a = ref[0] + a[1:] + ref[1:]  # insertion just beyond pos; add first and trailing reference
  *                 elif a.startswith('D'): # allow D<seq> and D<num>
  *                     have_deletions = True             # <<<<<<<<<<<<<<
@@ -16462,7 +16535,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
  */
         __pyx_v_have_deletions = 1;
 
-        /* "pysam/cvcf.pyx":745
+        /* "pysam/cvcf.pyx":750
  *                 elif a.startswith('D'): # allow D<seq> and D<num>
  *                     have_deletions = True
  *                     try:             # <<<<<<<<<<<<<<
@@ -16476,59 +16549,59 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
           __Pyx_XGOTREF(__pyx_t_14);
           /*try:*/ {
 
-            /* "pysam/cvcf.pyx":746
+            /* "pysam/cvcf.pyx":751
  *                     have_deletions = True
  *                     try:
  *                         l = int(a[1:])          # throws ValueError if sequence             # <<<<<<<<<<<<<<
  *                         if len(ref) < l:        # add to reference if necessary
  *                             addns = get_sequence(chrom,pos+len(ref),pos+l,self._reference)
  */
-            __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_a, 1, 0, NULL, NULL, &__pyx_slice__76, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 746; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+            __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_a, 1, 0, NULL, NULL, &__pyx_slice__76, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 751; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
             __Pyx_GOTREF(__pyx_t_2);
-            __pyx_t_10 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 746; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+            __pyx_t_10 = PyNumber_Int(__pyx_t_2); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 751; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
             __Pyx_GOTREF(__pyx_t_10);
             __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
             __Pyx_XDECREF_SET(__pyx_v_l, __pyx_t_10);
             __pyx_t_10 = 0;
 
-            /* "pysam/cvcf.pyx":747
+            /* "pysam/cvcf.pyx":752
  *                     try:
  *                         l = int(a[1:])          # throws ValueError if sequence
  *                         if len(ref) < l:        # add to reference if necessary             # <<<<<<<<<<<<<<
  *                             addns = get_sequence(chrom,pos+len(ref),pos+l,self._reference)
  *                             ref += addns
  */
-            __pyx_t_3 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
-            __pyx_t_10 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+            __pyx_t_3 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 752; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+            __pyx_t_10 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 752; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
             __Pyx_GOTREF(__pyx_t_10);
-            __pyx_t_2 = PyObject_RichCompare(__pyx_t_10, __pyx_v_l, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+            __pyx_t_2 = PyObject_RichCompare(__pyx_t_10, __pyx_v_l, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 752; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
             __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-            __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+            __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 752; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
             __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
             if (__pyx_t_5) {
 
-              /* "pysam/cvcf.pyx":748
+              /* "pysam/cvcf.pyx":753
  *                         l = int(a[1:])          # throws ValueError if sequence
  *                         if len(ref) < l:        # add to reference if necessary
  *                             addns = get_sequence(chrom,pos+len(ref),pos+l,self._reference)             # <<<<<<<<<<<<<<
  *                             ref += addns
  *                             for i,na in enumerate(newalts): newalts[i] = na+addns
  */
-              __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+              __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
               __Pyx_GOTREF(__pyx_t_2);
-              if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;} }
-              __pyx_t_3 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
-              __pyx_t_10 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+              if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;} }
+              __pyx_t_3 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+              __pyx_t_10 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
               __Pyx_GOTREF(__pyx_t_10);
-              __pyx_t_1 = PyNumber_Add(__pyx_v_pos, __pyx_t_10); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+              __pyx_t_1 = PyNumber_Add(__pyx_v_pos, __pyx_t_10); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
               __Pyx_GOTREF(__pyx_t_1);
               __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-              if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;} }
-              __pyx_t_10 = PyNumber_Add(__pyx_v_pos, __pyx_v_l); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+              if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;} }
+              __pyx_t_10 = PyNumber_Add(__pyx_v_pos, __pyx_v_l); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
               __Pyx_GOTREF(__pyx_t_10);
-              __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+              __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
               __Pyx_GOTREF(__pyx_t_8);
-              __pyx_t_9 = PyTuple_New(4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+              __pyx_t_9 = PyTuple_New(4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
               __Pyx_GOTREF(__pyx_t_9);
               __Pyx_INCREF(__pyx_v_chrom);
               PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_chrom);
@@ -16542,26 +16615,26 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
               __pyx_t_1 = 0;
               __pyx_t_10 = 0;
               __pyx_t_8 = 0;
-              __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+              __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
               __Pyx_GOTREF(__pyx_t_8);
               __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
               __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
               __Pyx_XDECREF_SET(__pyx_v_addns, __pyx_t_8);
               __pyx_t_8 = 0;
 
-              /* "pysam/cvcf.pyx":749
+              /* "pysam/cvcf.pyx":754
  *                         if len(ref) < l:        # add to reference if necessary
  *                             addns = get_sequence(chrom,pos+len(ref),pos+l,self._reference)
  *                             ref += addns             # <<<<<<<<<<<<<<
  *                             for i,na in enumerate(newalts): newalts[i] = na+addns
  *                         a = ref[l:]             # new deletion, deleting pos...pos+l
  */
-              __pyx_t_8 = PyNumber_InPlaceAdd(__pyx_v_ref, __pyx_v_addns); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 749; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+              __pyx_t_8 = PyNumber_InPlaceAdd(__pyx_v_ref, __pyx_v_addns); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 754; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
               __Pyx_GOTREF(__pyx_t_8);
               __Pyx_DECREF_SET(__pyx_v_ref, __pyx_t_8);
               __pyx_t_8 = 0;
 
-              /* "pysam/cvcf.pyx":750
+              /* "pysam/cvcf.pyx":755
  *                             addns = get_sequence(chrom,pos+len(ref),pos+l,self._reference)
  *                             ref += addns
  *                             for i,na in enumerate(newalts): newalts[i] = na+addns             # <<<<<<<<<<<<<<
@@ -16574,22 +16647,22 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
               for (;;) {
                 if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_9)) break;
                 #if CYTHON_COMPILING_IN_CPYTHON
-                __pyx_t_2 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+                __pyx_t_2 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
                 #else
-                __pyx_t_2 = PySequence_ITEM(__pyx_t_9, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+                __pyx_t_2 = PySequence_ITEM(__pyx_t_9, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
                 #endif
                 __Pyx_XDECREF_SET(__pyx_v_na, __pyx_t_2);
                 __pyx_t_2 = 0;
                 __Pyx_INCREF(__pyx_t_8);
                 __Pyx_XDECREF_SET(__pyx_v_i, __pyx_t_8);
-                __pyx_t_2 = PyNumber_Add(__pyx_t_8, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+                __pyx_t_2 = PyNumber_Add(__pyx_t_8, __pyx_int_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
                 __Pyx_GOTREF(__pyx_t_2);
                 __Pyx_DECREF(__pyx_t_8);
                 __pyx_t_8 = __pyx_t_2;
                 __pyx_t_2 = 0;
-                __pyx_t_2 = PyNumber_Add(__pyx_v_na, __pyx_v_addns); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+                __pyx_t_2 = PyNumber_Add(__pyx_v_na, __pyx_v_addns); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
                 __Pyx_GOTREF(__pyx_t_2);
-                if (unlikely(PyObject_SetItem(__pyx_v_newalts, __pyx_v_i, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+                if (unlikely(PyObject_SetItem(__pyx_v_newalts, __pyx_v_i, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
                 __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
               }
               __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -16598,14 +16671,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
             }
             __pyx_L64:;
 
-            /* "pysam/cvcf.pyx":751
+            /* "pysam/cvcf.pyx":756
  *                             ref += addns
  *                             for i,na in enumerate(newalts): newalts[i] = na+addns
  *                         a = ref[l:]             # new deletion, deleting pos...pos+l             # <<<<<<<<<<<<<<
  *                     except ValueError:
  *                         s = a[1:]
  */
-            __pyx_t_8 = __Pyx_PyObject_GetSlice(__pyx_v_ref, 0, 0, &__pyx_v_l, NULL, NULL, 0, 0, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 751; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
+            __pyx_t_8 = __Pyx_PyObject_GetSlice(__pyx_v_ref, 0, 0, &__pyx_v_l, NULL, NULL, 0, 0, 1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L56_error;}
             __Pyx_GOTREF(__pyx_t_8);
             __Pyx_DECREF_SET(__pyx_v_a, __pyx_t_8);
             __pyx_t_8 = 0;
@@ -16622,7 +16695,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
           __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;
           __Pyx_XDECREF(__pyx_t_8); __pyx_t_8 = 0;
 
-          /* "pysam/cvcf.pyx":752
+          /* "pysam/cvcf.pyx":757
  *                             for i,na in enumerate(newalts): newalts[i] = na+addns
  *                         a = ref[l:]             # new deletion, deleting pos...pos+l
  *                     except ValueError:             # <<<<<<<<<<<<<<
@@ -16632,61 +16705,61 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
           __pyx_t_21 = PyErr_ExceptionMatches(__pyx_builtin_ValueError);
           if (__pyx_t_21) {
             __Pyx_AddTraceback("pysam.cvcf.VCF.parse_data", __pyx_clineno, __pyx_lineno, __pyx_filename);
-            if (__Pyx_GetException(&__pyx_t_8, &__pyx_t_9, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 752; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+            if (__Pyx_GetException(&__pyx_t_8, &__pyx_t_9, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 757; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
             __Pyx_GOTREF(__pyx_t_8);
             __Pyx_GOTREF(__pyx_t_9);
             __Pyx_GOTREF(__pyx_t_2);
 
-            /* "pysam/cvcf.pyx":753
+            /* "pysam/cvcf.pyx":758
  *                         a = ref[l:]             # new deletion, deleting pos...pos+l
  *                     except ValueError:
  *                         s = a[1:]             # <<<<<<<<<<<<<<
  *                         if len(ref) < len(s):   # add Ns to reference if necessary
  *                             addns = get_sequence(chrom,pos+len(ref),pos+len(s),self._reference)
  */
-            __pyx_t_10 = __Pyx_PyObject_GetSlice(__pyx_v_a, 1, 0, NULL, NULL, &__pyx_slice__77, 1, 0, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+            __pyx_t_10 = __Pyx_PyObject_GetSlice(__pyx_v_a, 1, 0, NULL, NULL, &__pyx_slice__77, 1, 0, 1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
             __Pyx_GOTREF(__pyx_t_10);
             __Pyx_XDECREF_SET(__pyx_v_s, __pyx_t_10);
             __pyx_t_10 = 0;
 
-            /* "pysam/cvcf.pyx":754
+            /* "pysam/cvcf.pyx":759
  *                     except ValueError:
  *                         s = a[1:]
  *                         if len(ref) < len(s):   # add Ns to reference if necessary             # <<<<<<<<<<<<<<
  *                             addns = get_sequence(chrom,pos+len(ref),pos+len(s),self._reference)
  *                             if not s.endswith(addns) and addns != 'N'*len(addns):
  */
-            __pyx_t_3 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 754; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
-            __pyx_t_22 = PyObject_Length(__pyx_v_s); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 754; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+            __pyx_t_3 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+            __pyx_t_22 = PyObject_Length(__pyx_v_s); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
             __pyx_t_5 = ((__pyx_t_3 < __pyx_t_22) != 0);
             if (__pyx_t_5) {
 
-              /* "pysam/cvcf.pyx":755
+              /* "pysam/cvcf.pyx":760
  *                         s = a[1:]
  *                         if len(ref) < len(s):   # add Ns to reference if necessary
  *                             addns = get_sequence(chrom,pos+len(ref),pos+len(s),self._reference)             # <<<<<<<<<<<<<<
  *                             if not s.endswith(addns) and addns != 'N'*len(addns):
  *                                 self.error(line,self.V33_UNMATCHED_DELETION,
  */
-              __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_10);
-              if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;} }
-              __pyx_t_22 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
-              __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;} }
+              __pyx_t_22 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_1);
-              __pyx_t_7 = PyNumber_Add(__pyx_v_pos, __pyx_t_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_7 = PyNumber_Add(__pyx_v_pos, __pyx_t_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_7);
               __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-              if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;} }
-              __pyx_t_22 = PyObject_Length(__pyx_v_s); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
-              __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;} }
+              __pyx_t_22 = PyObject_Length(__pyx_v_s); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_1 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_1);
-              __pyx_t_23 = PyNumber_Add(__pyx_v_pos, __pyx_t_1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_23 = PyNumber_Add(__pyx_v_pos, __pyx_t_1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_23);
               __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-              __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_1);
-              __pyx_t_24 = PyTuple_New(4); if (unlikely(!__pyx_t_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_24 = PyTuple_New(4); if (unlikely(!__pyx_t_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_24);
               __Pyx_INCREF(__pyx_v_chrom);
               PyTuple_SET_ITEM(__pyx_t_24, 0, __pyx_v_chrom);
@@ -16700,44 +16773,44 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
               __pyx_t_7 = 0;
               __pyx_t_23 = 0;
               __pyx_t_1 = 0;
-              __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_24, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 755; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_24, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_1);
               __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
               __Pyx_DECREF(__pyx_t_24); __pyx_t_24 = 0;
               __Pyx_XDECREF_SET(__pyx_v_addns, __pyx_t_1);
               __pyx_t_1 = 0;
 
-              /* "pysam/cvcf.pyx":756
+              /* "pysam/cvcf.pyx":761
  *                         if len(ref) < len(s):   # add Ns to reference if necessary
  *                             addns = get_sequence(chrom,pos+len(ref),pos+len(s),self._reference)
  *                             if not s.endswith(addns) and addns != 'N'*len(addns):             # <<<<<<<<<<<<<<
  *                                 self.error(line,self.V33_UNMATCHED_DELETION,
  *                                            "(deletion is %s, reference is %s)" % (a,get_sequence(chrom,pos,pos+len(s),self._reference)))
  */
-              __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_endswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_s, __pyx_n_s_endswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_1);
-              __pyx_t_24 = PyTuple_New(1); if (unlikely(!__pyx_t_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_24 = PyTuple_New(1); if (unlikely(!__pyx_t_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_24);
               __Pyx_INCREF(__pyx_v_addns);
               PyTuple_SET_ITEM(__pyx_t_24, 0, __pyx_v_addns);
               __Pyx_GIVEREF(__pyx_v_addns);
-              __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_24, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_24, NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_10);
               __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
               __Pyx_DECREF(__pyx_t_24); __pyx_t_24 = 0;
-              __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
               __pyx_t_15 = (!__pyx_t_5);
               if (__pyx_t_15) {
-                __pyx_t_22 = PyObject_Length(__pyx_v_addns); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
-                __pyx_t_10 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_22 = PyObject_Length(__pyx_v_addns); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_10 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_10);
-                __pyx_t_24 = PyNumber_Multiply(__pyx_n_s_N, __pyx_t_10); if (unlikely(!__pyx_t_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_24 = PyNumber_Multiply(__pyx_n_s_N, __pyx_t_10); if (unlikely(!__pyx_t_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_24);
                 __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-                __pyx_t_10 = PyObject_RichCompare(__pyx_v_addns, __pyx_t_24, Py_NE); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_10 = PyObject_RichCompare(__pyx_v_addns, __pyx_t_24, Py_NE); __Pyx_XGOTREF(__pyx_t_10); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_DECREF(__pyx_t_24); __pyx_t_24 = 0;
-                __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 756; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_10); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
                 __pyx_t_20 = __pyx_t_5;
               } else {
@@ -16745,38 +16818,38 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
               }
               if (__pyx_t_20) {
 
-                /* "pysam/cvcf.pyx":757
+                /* "pysam/cvcf.pyx":762
  *                             addns = get_sequence(chrom,pos+len(ref),pos+len(s),self._reference)
  *                             if not s.endswith(addns) and addns != 'N'*len(addns):
  *                                 self.error(line,self.V33_UNMATCHED_DELETION,             # <<<<<<<<<<<<<<
  *                                            "(deletion is %s, reference is %s)" % (a,get_sequence(chrom,pos,pos+len(s),self._reference)))
  *                             ref += addns
  */
-                __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 757; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 762; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_10);
-                __pyx_t_24 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V33_UNMATCHED_DELETION); if (unlikely(!__pyx_t_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 757; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_24 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V33_UNMATCHED_DELETION); if (unlikely(!__pyx_t_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 762; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_24);
 
-                /* "pysam/cvcf.pyx":758
+                /* "pysam/cvcf.pyx":763
  *                             if not s.endswith(addns) and addns != 'N'*len(addns):
  *                                 self.error(line,self.V33_UNMATCHED_DELETION,
  *                                            "(deletion is %s, reference is %s)" % (a,get_sequence(chrom,pos,pos+len(s),self._reference)))             # <<<<<<<<<<<<<<
  *                             ref += addns
  *                             for i,na in enumerate(newalts): newalts[i] = na+addns
  */
-                __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_1);
-                if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;} }
-                if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;} }
-                __pyx_t_22 = PyObject_Length(__pyx_v_s); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
-                __pyx_t_23 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;} }
+                if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;} }
+                __pyx_t_22 = PyObject_Length(__pyx_v_s); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_23 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_23);
-                __pyx_t_7 = PyNumber_Add(__pyx_v_pos, __pyx_t_23); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_7 = PyNumber_Add(__pyx_v_pos, __pyx_t_23); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_7);
                 __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-                __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_23);
-                __pyx_t_25 = PyTuple_New(4); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_25 = PyTuple_New(4); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_25);
                 __Pyx_INCREF(__pyx_v_chrom);
                 PyTuple_SET_ITEM(__pyx_t_25, 0, __pyx_v_chrom);
@@ -16790,11 +16863,11 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
                 __Pyx_GIVEREF(__pyx_t_23);
                 __pyx_t_7 = 0;
                 __pyx_t_23 = 0;
-                __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_25, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_25, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_23);
                 __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
                 __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
-                __pyx_t_25 = PyTuple_New(2); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_25 = PyTuple_New(2); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_25);
                 __Pyx_INCREF(__pyx_v_a);
                 PyTuple_SET_ITEM(__pyx_t_25, 0, __pyx_v_a);
@@ -16802,18 +16875,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
                 PyTuple_SET_ITEM(__pyx_t_25, 1, __pyx_t_23);
                 __Pyx_GIVEREF(__pyx_t_23);
                 __pyx_t_23 = 0;
-                __pyx_t_23 = __Pyx_PyString_Format(__pyx_kp_s_deletion_is_s_reference_is_s, __pyx_t_25); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_23 = __Pyx_PyString_Format(__pyx_kp_s_deletion_is_s_reference_is_s, __pyx_t_25); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_23);
                 __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
 
-                /* "pysam/cvcf.pyx":757
+                /* "pysam/cvcf.pyx":762
  *                             addns = get_sequence(chrom,pos+len(ref),pos+len(s),self._reference)
  *                             if not s.endswith(addns) and addns != 'N'*len(addns):
  *                                 self.error(line,self.V33_UNMATCHED_DELETION,             # <<<<<<<<<<<<<<
  *                                            "(deletion is %s, reference is %s)" % (a,get_sequence(chrom,pos,pos+len(s),self._reference)))
  *                             ref += addns
  */
-                __pyx_t_25 = PyTuple_New(3); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 757; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_25 = PyTuple_New(3); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 762; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_25);
                 __Pyx_INCREF(__pyx_v_line);
                 PyTuple_SET_ITEM(__pyx_t_25, 0, __pyx_v_line);
@@ -16824,7 +16897,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
                 __Pyx_GIVEREF(__pyx_t_23);
                 __pyx_t_24 = 0;
                 __pyx_t_23 = 0;
-                __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_25, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 757; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_25, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 762; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_23);
                 __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
                 __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
@@ -16833,19 +16906,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
               }
               __pyx_L70:;
 
-              /* "pysam/cvcf.pyx":759
+              /* "pysam/cvcf.pyx":764
  *                                 self.error(line,self.V33_UNMATCHED_DELETION,
  *                                            "(deletion is %s, reference is %s)" % (a,get_sequence(chrom,pos,pos+len(s),self._reference)))
  *                             ref += addns             # <<<<<<<<<<<<<<
  *                             for i,na in enumerate(newalts): newalts[i] = na+addns
  *                         a = ref[len(s):]        # new deletion, deleting from pos
  */
-              __pyx_t_23 = PyNumber_InPlaceAdd(__pyx_v_ref, __pyx_v_addns); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 759; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+              __pyx_t_23 = PyNumber_InPlaceAdd(__pyx_v_ref, __pyx_v_addns); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 764; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
               __Pyx_GOTREF(__pyx_t_23);
               __Pyx_DECREF_SET(__pyx_v_ref, __pyx_t_23);
               __pyx_t_23 = 0;
 
-              /* "pysam/cvcf.pyx":760
+              /* "pysam/cvcf.pyx":765
  *                                            "(deletion is %s, reference is %s)" % (a,get_sequence(chrom,pos,pos+len(s),self._reference)))
  *                             ref += addns
  *                             for i,na in enumerate(newalts): newalts[i] = na+addns             # <<<<<<<<<<<<<<
@@ -16858,22 +16931,22 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
               for (;;) {
                 if (__pyx_t_22 >= PyList_GET_SIZE(__pyx_t_25)) break;
                 #if CYTHON_COMPILING_IN_CPYTHON
-                __pyx_t_10 = PyList_GET_ITEM(__pyx_t_25, __pyx_t_22); __Pyx_INCREF(__pyx_t_10); __pyx_t_22++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_10 = PyList_GET_ITEM(__pyx_t_25, __pyx_t_22); __Pyx_INCREF(__pyx_t_10); __pyx_t_22++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 765; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 #else
-                __pyx_t_10 = PySequence_ITEM(__pyx_t_25, __pyx_t_22); __pyx_t_22++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_10 = PySequence_ITEM(__pyx_t_25, __pyx_t_22); __pyx_t_22++; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 765; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 #endif
                 __Pyx_XDECREF_SET(__pyx_v_na, __pyx_t_10);
                 __pyx_t_10 = 0;
                 __Pyx_INCREF(__pyx_t_23);
                 __Pyx_XDECREF_SET(__pyx_v_i, __pyx_t_23);
-                __pyx_t_10 = PyNumber_Add(__pyx_t_23, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_10 = PyNumber_Add(__pyx_t_23, __pyx_int_1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 765; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_10);
                 __Pyx_DECREF(__pyx_t_23);
                 __pyx_t_23 = __pyx_t_10;
                 __pyx_t_10 = 0;
-                __pyx_t_10 = PyNumber_Add(__pyx_v_na, __pyx_v_addns); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                __pyx_t_10 = PyNumber_Add(__pyx_v_na, __pyx_v_addns); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 765; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_GOTREF(__pyx_t_10);
-                if (unlikely(PyObject_SetItem(__pyx_v_newalts, __pyx_v_i, __pyx_t_10) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 760; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+                if (unlikely(PyObject_SetItem(__pyx_v_newalts, __pyx_v_i, __pyx_t_10) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 765; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
                 __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
               }
               __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
@@ -16882,15 +16955,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
             }
             __pyx_L69:;
 
-            /* "pysam/cvcf.pyx":761
+            /* "pysam/cvcf.pyx":766
  *                             ref += addns
  *                             for i,na in enumerate(newalts): newalts[i] = na+addns
  *                         a = ref[len(s):]        # new deletion, deleting from pos             # <<<<<<<<<<<<<<
  *                 else:
  *                     self.error(line,self.V33_BAD_ALLELE)
  */
-            __pyx_t_22 = PyObject_Length(__pyx_v_s); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
-            __pyx_t_23 = __Pyx_PyObject_GetSlice(__pyx_v_ref, __pyx_t_22, 0, NULL, NULL, NULL, 1, 0, 1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 761; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+            __pyx_t_22 = PyObject_Length(__pyx_v_s); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 766; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
+            __pyx_t_23 = __Pyx_PyObject_GetSlice(__pyx_v_ref, __pyx_t_22, 0, NULL, NULL, NULL, 1, 0, 1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 766; __pyx_clineno = __LINE__; goto __pyx_L58_except_error;}
             __Pyx_GOTREF(__pyx_t_23);
             __Pyx_DECREF_SET(__pyx_v_a, __pyx_t_23);
             __pyx_t_23 = 0;
@@ -16917,18 +16990,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":763
+        /* "pysam/cvcf.pyx":768
  *                         a = ref[len(s):]        # new deletion, deleting from pos
  *                 else:
  *                     self.error(line,self.V33_BAD_ALLELE)             # <<<<<<<<<<<<<<
  *                 newalts.append(a)
  *             alt = newalts
  */
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V33_BAD_ALLELE); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V33_BAD_ALLELE); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PyTuple_New(2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_v_line);
@@ -16936,7 +17009,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_8, 1, __pyx_t_9);
         __Pyx_GIVEREF(__pyx_t_9);
         __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_8, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
@@ -16944,18 +17017,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       __pyx_L55:;
 
-      /* "pysam/cvcf.pyx":764
+      /* "pysam/cvcf.pyx":769
  *                 else:
  *                     self.error(line,self.V33_BAD_ALLELE)
  *                 newalts.append(a)             # <<<<<<<<<<<<<<
  *             alt = newalts
  *             # deletion alleles exist, add dummy 1st reference allele, and account for leading base
  */
-      __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_newalts, __pyx_v_a); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 764; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_newalts, __pyx_v_a); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
 
-    /* "pysam/cvcf.pyx":765
+    /* "pysam/cvcf.pyx":770
  *                     self.error(line,self.V33_BAD_ALLELE)
  *                 newalts.append(a)
  *             alt = newalts             # <<<<<<<<<<<<<<
@@ -16965,7 +17038,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __Pyx_INCREF(__pyx_v_newalts);
     __Pyx_DECREF_SET(__pyx_v_alt, __pyx_v_newalts);
 
-    /* "pysam/cvcf.pyx":767
+    /* "pysam/cvcf.pyx":772
  *             alt = newalts
  *             # deletion alleles exist, add dummy 1st reference allele, and account for leading base
  *             if have_deletions:             # <<<<<<<<<<<<<<
@@ -16975,48 +17048,48 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __pyx_t_20 = (__pyx_v_have_deletions != 0);
     if (__pyx_t_20) {
 
-      /* "pysam/cvcf.pyx":768
+      /* "pysam/cvcf.pyx":773
  *             # deletion alleles exist, add dummy 1st reference allele, and account for leading base
  *             if have_deletions:
  *                 if pos == 0:             # <<<<<<<<<<<<<<
  *                     # Petr Danacek's: we can't have a leading nucleotide at (1-based) position 1
  *                     addn = get_sequence(chrom,pos+len(ref),pos+len(ref)+1,self._reference)
  */
-      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-      __pyx_t_11 = PyObject_RichCompare(__pyx_v_pos, __pyx_int_0, Py_EQ); __Pyx_XGOTREF(__pyx_t_11); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_20 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+      __pyx_t_11 = PyObject_RichCompare(__pyx_v_pos, __pyx_int_0, Py_EQ); __Pyx_XGOTREF(__pyx_t_11); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_20 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
       if (__pyx_t_20) {
 
-        /* "pysam/cvcf.pyx":770
+        /* "pysam/cvcf.pyx":775
  *                 if pos == 0:
  *                     # Petr Danacek's: we can't have a leading nucleotide at (1-based) position 1
  *                     addn = get_sequence(chrom,pos+len(ref),pos+len(ref)+1,self._reference)             # <<<<<<<<<<<<<<
  *                     ref += addn
  *                     alt = [allele+addn for allele in alt]
  */
-        __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
-        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-        __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_9 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+        __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_8 = PyNumber_Add(__pyx_v_pos, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PyNumber_Add(__pyx_v_pos, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-        __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_9 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+        __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_2 = PyNumber_Add(__pyx_v_pos, __pyx_t_9); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyNumber_Add(__pyx_v_pos, __pyx_t_9); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = PyNumber_Add(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyNumber_Add(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_23 = PyTuple_New(4); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_23 = PyTuple_New(4); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
         __Pyx_INCREF(__pyx_v_chrom);
         PyTuple_SET_ITEM(__pyx_t_23, 0, __pyx_v_chrom);
@@ -17030,47 +17103,47 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         __pyx_t_8 = 0;
         __pyx_t_9 = 0;
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_23, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_23, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
         __pyx_v_addn = __pyx_t_2;
         __pyx_t_2 = 0;
 
-        /* "pysam/cvcf.pyx":771
+        /* "pysam/cvcf.pyx":776
  *                     # Petr Danacek's: we can't have a leading nucleotide at (1-based) position 1
  *                     addn = get_sequence(chrom,pos+len(ref),pos+len(ref)+1,self._reference)
  *                     ref += addn             # <<<<<<<<<<<<<<
  *                     alt = [allele+addn for allele in alt]
  *                 else:
  */
-        __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_v_ref, __pyx_v_addn); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_v_ref, __pyx_v_addn); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF_SET(__pyx_v_ref, __pyx_t_2);
         __pyx_t_2 = 0;
 
-        /* "pysam/cvcf.pyx":772
+        /* "pysam/cvcf.pyx":777
  *                     addn = get_sequence(chrom,pos+len(ref),pos+len(ref)+1,self._reference)
  *                     ref += addn
  *                     alt = [allele+addn for allele in alt]             # <<<<<<<<<<<<<<
  *                 else:
  *                     addn = get_sequence(chrom,pos-1,pos,self._reference)
  */
-        __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __pyx_t_23 = __pyx_v_alt; __Pyx_INCREF(__pyx_t_23); __pyx_t_4 = 0;
         for (;;) {
           if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_23)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_11 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_11); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_11 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_11); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_11 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_11 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
           __Pyx_XDECREF_SET(__pyx_v_allele, __pyx_t_11);
           __pyx_t_11 = 0;
-          __pyx_t_11 = PyNumber_Add(__pyx_v_allele, __pyx_v_addn); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_11 = PyNumber_Add(__pyx_v_allele, __pyx_v_addn); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_11);
-          if (unlikely(__Pyx_ListComp_Append(__pyx_t_2, (PyObject*)__pyx_t_11))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          if (unlikely(__Pyx_ListComp_Append(__pyx_t_2, (PyObject*)__pyx_t_11))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
         }
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
@@ -17080,22 +17153,22 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":774
+        /* "pysam/cvcf.pyx":779
  *                     alt = [allele+addn for allele in alt]
  *                 else:
  *                     addn = get_sequence(chrom,pos-1,pos,self._reference)             # <<<<<<<<<<<<<<
  *                     ref = addn + ref
  *                     alt = [addn + allele for allele in alt]
  */
-        __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_get_sequence); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 779; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-        __pyx_t_23 = PyNumber_Subtract(__pyx_v_pos, __pyx_int_1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 779; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+        __pyx_t_23 = PyNumber_Subtract(__pyx_v_pos, __pyx_int_1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 779; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
-        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-        __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 779; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+        __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 779; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
-        __pyx_t_9 = PyTuple_New(4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyTuple_New(4); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 779; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_INCREF(__pyx_v_chrom);
         PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_chrom);
@@ -17109,62 +17182,62 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         __Pyx_GIVEREF(__pyx_t_11);
         __pyx_t_23 = 0;
         __pyx_t_11 = 0;
-        __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 779; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __pyx_v_addn = __pyx_t_11;
         __pyx_t_11 = 0;
 
-        /* "pysam/cvcf.pyx":775
+        /* "pysam/cvcf.pyx":780
  *                 else:
  *                     addn = get_sequence(chrom,pos-1,pos,self._reference)
  *                     ref = addn + ref             # <<<<<<<<<<<<<<
  *                     alt = [addn + allele for allele in alt]
  *                     pos -= 1
  */
-        __pyx_t_11 = PyNumber_Add(__pyx_v_addn, __pyx_v_ref); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = PyNumber_Add(__pyx_v_addn, __pyx_v_ref); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF_SET(__pyx_v_ref, __pyx_t_11);
         __pyx_t_11 = 0;
 
-        /* "pysam/cvcf.pyx":776
+        /* "pysam/cvcf.pyx":781
  *                     addn = get_sequence(chrom,pos-1,pos,self._reference)
  *                     ref = addn + ref
  *                     alt = [addn + allele for allele in alt]             # <<<<<<<<<<<<<<
  *                     pos -= 1
  *         else:
  */
-        __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __pyx_t_9 = __pyx_v_alt; __Pyx_INCREF(__pyx_t_9); __pyx_t_4 = 0;
         for (;;) {
           if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_9)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_2 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_2 = PySequence_ITEM(__pyx_t_9, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PySequence_ITEM(__pyx_t_9, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
           __Pyx_XDECREF_SET(__pyx_v_allele, __pyx_t_2);
           __pyx_t_2 = 0;
-          __pyx_t_2 = PyNumber_Add(__pyx_v_addn, __pyx_v_allele); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PyNumber_Add(__pyx_v_addn, __pyx_v_allele); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_2);
-          if (unlikely(__Pyx_ListComp_Append(__pyx_t_11, (PyObject*)__pyx_t_2))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          if (unlikely(__Pyx_ListComp_Append(__pyx_t_11, (PyObject*)__pyx_t_2))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         }
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF_SET(__pyx_v_alt, __pyx_t_11);
         __pyx_t_11 = 0;
 
-        /* "pysam/cvcf.pyx":777
+        /* "pysam/cvcf.pyx":782
  *                     ref = addn + ref
  *                     alt = [addn + allele for allele in alt]
  *                     pos -= 1             # <<<<<<<<<<<<<<
  *         else:
  *             # format v4.0 -- just check for nucleotides
  */
-        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-        __pyx_t_11 = PyNumber_InPlaceSubtract(__pyx_v_pos, __pyx_int_1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+        __pyx_t_11 = PyNumber_InPlaceSubtract(__pyx_v_pos, __pyx_int_1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_XDECREF_SET(__pyx_v_pos, __pyx_t_11);
         __pyx_t_11 = 0;
@@ -17177,7 +17250,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":780
+    /* "pysam/cvcf.pyx":785
  *         else:
  *             # format v4.0 -- just check for nucleotides
  *             for allele in alt:             # <<<<<<<<<<<<<<
@@ -17188,7 +17261,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __pyx_t_11 = __pyx_v_alt; __Pyx_INCREF(__pyx_t_11); __pyx_t_4 = 0;
       __pyx_t_16 = NULL;
     } else {
-      __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __pyx_t_16 = Py_TYPE(__pyx_t_11)->tp_iternext;
     }
@@ -17196,16 +17269,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_11)) {
         if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_11)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_9 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_9 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_11)) {
         if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_11)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_9 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_9 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
       } else {
         __pyx_t_9 = __pyx_t_16(__pyx_t_11);
@@ -17213,7 +17286,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
           PyObject* exc_type = PyErr_Occurred();
           if (exc_type) {
             if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+            else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           }
           break;
         }
@@ -17222,44 +17295,44 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_XDECREF_SET(__pyx_v_allele, __pyx_t_9);
       __pyx_t_9 = 0;
 
-      /* "pysam/cvcf.pyx":781
+      /* "pysam/cvcf.pyx":786
  *             # format v4.0 -- just check for nucleotides
  *             for allele in alt:
  *                 if not alleleRegEx.match(allele):             # <<<<<<<<<<<<<<
  *                     self.error(line,self.V40_BAD_ALLELE,allele)
  * 
  */
-      __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s_alleleRegEx); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s_alleleRegEx); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_match); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_match); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_INCREF(__pyx_v_allele);
       PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_allele);
       __Pyx_GIVEREF(__pyx_v_allele);
-      __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_23);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_20 = __Pyx_PyObject_IsTrue(__pyx_t_23); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_20 = __Pyx_PyObject_IsTrue(__pyx_t_23); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
       __pyx_t_15 = ((!__pyx_t_20) != 0);
       if (__pyx_t_15) {
 
-        /* "pysam/cvcf.pyx":782
+        /* "pysam/cvcf.pyx":787
  *             for allele in alt:
  *                 if not alleleRegEx.match(allele):
  *                     self.error(line,self.V40_BAD_ALLELE,allele)             # <<<<<<<<<<<<<<
  * 
  *         # check for leading nucleotide in indel calls
  */
-        __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V40_BAD_ALLELE); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_V40_BAD_ALLELE); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyTuple_New(3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_line);
@@ -17270,7 +17343,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_2, 2, __pyx_v_allele);
         __Pyx_GIVEREF(__pyx_v_allele);
         __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_23, __pyx_t_2, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_23, __pyx_t_2, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -17283,7 +17356,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L51:;
 
-  /* "pysam/cvcf.pyx":785
+  /* "pysam/cvcf.pyx":790
  * 
  *         # check for leading nucleotide in indel calls
  *         for allele in alt:             # <<<<<<<<<<<<<<
@@ -17294,7 +17367,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __pyx_t_11 = __pyx_v_alt; __Pyx_INCREF(__pyx_t_11); __pyx_t_4 = 0;
     __pyx_t_16 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __pyx_t_16 = Py_TYPE(__pyx_t_11)->tp_iternext;
   }
@@ -17302,16 +17375,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_11)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_11)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_9 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_9 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_11)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_11)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_9 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_9 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_9 = __pyx_t_16(__pyx_t_11);
@@ -17319,7 +17392,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 785; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -17328,33 +17401,33 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __Pyx_XDECREF_SET(__pyx_v_allele, __pyx_t_9);
     __pyx_t_9 = 0;
 
-    /* "pysam/cvcf.pyx":786
+    /* "pysam/cvcf.pyx":791
  *         # check for leading nucleotide in indel calls
  *         for allele in alt:
  *             if len(allele) != len(ref):             # <<<<<<<<<<<<<<
  *                 if len(allele) == 0: self.error(line,self.ZERO_LENGTH_ALLELE)
  *                 if ref[0].upper() != allele[0].upper() and "N" not in (ref[0]+allele[0]).upper():
  */
-    __pyx_t_22 = PyObject_Length(__pyx_v_allele); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_22 = PyObject_Length(__pyx_v_allele); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 791; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 791; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_15 = ((__pyx_t_22 != __pyx_t_3) != 0);
     if (__pyx_t_15) {
 
-      /* "pysam/cvcf.pyx":787
+      /* "pysam/cvcf.pyx":792
  *         for allele in alt:
  *             if len(allele) != len(ref):
  *                 if len(allele) == 0: self.error(line,self.ZERO_LENGTH_ALLELE)             # <<<<<<<<<<<<<<
  *                 if ref[0].upper() != allele[0].upper() and "N" not in (ref[0]+allele[0]).upper():
  *                     self.error(line,self.MISSING_INDEL_ALLELE_REF_BASE)
  */
-      __pyx_t_3 = PyObject_Length(__pyx_v_allele); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyObject_Length(__pyx_v_allele); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 792; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_15 = ((__pyx_t_3 == 0) != 0);
       if (__pyx_t_15) {
-        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 792; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ZERO_LENGTH_ALLELE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ZERO_LENGTH_ALLELE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 792; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_23 = PyTuple_New(2); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_23 = PyTuple_New(2); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 792; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_23, 0, __pyx_v_line);
@@ -17362,7 +17435,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_23, 1, __pyx_t_2);
         __Pyx_GIVEREF(__pyx_t_2);
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_23, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_23, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 792; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
@@ -17371,50 +17444,50 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       __pyx_L85:;
 
-      /* "pysam/cvcf.pyx":788
+      /* "pysam/cvcf.pyx":793
  *             if len(allele) != len(ref):
  *                 if len(allele) == 0: self.error(line,self.ZERO_LENGTH_ALLELE)
  *                 if ref[0].upper() != allele[0].upper() and "N" not in (ref[0]+allele[0]).upper():             # <<<<<<<<<<<<<<
  *                     self.error(line,self.MISSING_INDEL_ALLELE_REF_BASE)
  * 
  */
-      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_ref, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_ref, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_upper); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_upper); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_23);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_23, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_23, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-      __pyx_t_23 = __Pyx_GetItemInt(__pyx_v_allele, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_23 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_23 = __Pyx_GetItemInt(__pyx_v_allele, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_23 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_23);
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_23, __pyx_n_s_upper); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_23, __pyx_n_s_upper); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-      __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_23);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-      __pyx_t_9 = PyObject_RichCompare(__pyx_t_2, __pyx_t_23, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyObject_RichCompare(__pyx_t_2, __pyx_t_23, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-      __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       if (__pyx_t_15) {
-        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_ref, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_ref, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_23 = __Pyx_GetItemInt(__pyx_v_allele, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_23 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_23 = __Pyx_GetItemInt(__pyx_v_allele, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_23 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_23);
-        __pyx_t_2 = PyNumber_Add(__pyx_t_9, __pyx_t_23); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyNumber_Add(__pyx_t_9, __pyx_t_23); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-        __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_upper); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_upper); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_23, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_23, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-        __pyx_t_20 = (__Pyx_PySequence_Contains(__pyx_n_s_N, __pyx_t_2, Py_NE)); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_20 = (__Pyx_PySequence_Contains(__pyx_n_s_N, __pyx_t_2, Py_NE)); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __pyx_t_5 = __pyx_t_20;
       } else {
@@ -17422,18 +17495,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       if (__pyx_t_5) {
 
-        /* "pysam/cvcf.pyx":789
+        /* "pysam/cvcf.pyx":794
  *                 if len(allele) == 0: self.error(line,self.ZERO_LENGTH_ALLELE)
  *                 if ref[0].upper() != allele[0].upper() and "N" not in (ref[0]+allele[0]).upper():
  *                     self.error(line,self.MISSING_INDEL_ALLELE_REF_BASE)             # <<<<<<<<<<<<<<
  * 
  *         # trim trailing bases in alleles
  */
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 789; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_MISSING_INDEL_ALLELE_REF_BASE); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 789; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_MISSING_INDEL_ALLELE_REF_BASE); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
-        __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 789; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_line);
@@ -17441,7 +17514,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_t_23);
         __Pyx_GIVEREF(__pyx_t_23);
         __pyx_t_23 = 0;
-        __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 789; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_9, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
@@ -17455,21 +17528,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
 
-  /* "pysam/cvcf.pyx":801
+  /* "pysam/cvcf.pyx":806
  * 
  *         # left-align alleles, if a reference is available
  *         if self._leftalign and self._reference:             # <<<<<<<<<<<<<<
  *             while left < pos:
  *                 movable = True
  */
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_leftalign_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_leftalign_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
   if (__pyx_t_5) {
-    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_reference_2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
-    __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
     __pyx_t_20 = __pyx_t_15;
   } else {
@@ -17477,7 +17550,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   if (__pyx_t_20) {
 
-    /* "pysam/cvcf.pyx":802
+    /* "pysam/cvcf.pyx":807
  *         # left-align alleles, if a reference is available
  *         if self._leftalign and self._reference:
  *             while left < pos:             # <<<<<<<<<<<<<<
@@ -17485,14 +17558,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
  *                 for allele in alt:
  */
     while (1) {
-      if (unlikely(!__pyx_v_left)) { __Pyx_RaiseUnboundLocalError("left"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-      __pyx_t_11 = PyObject_RichCompare(__pyx_v_left, __pyx_v_pos, Py_LT); __Pyx_XGOTREF(__pyx_t_11); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_20 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 802; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(!__pyx_v_left)) { __Pyx_RaiseUnboundLocalError("left"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 807; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+      if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 807; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+      __pyx_t_11 = PyObject_RichCompare(__pyx_v_left, __pyx_v_pos, Py_LT); __Pyx_XGOTREF(__pyx_t_11); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_20 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
       if (!__pyx_t_20) break;
 
-      /* "pysam/cvcf.pyx":803
+      /* "pysam/cvcf.pyx":808
  *         if self._leftalign and self._reference:
  *             while left < pos:
  *                 movable = True             # <<<<<<<<<<<<<<
@@ -17501,7 +17574,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
  */
       __pyx_v_movable = 1;
 
-      /* "pysam/cvcf.pyx":804
+      /* "pysam/cvcf.pyx":809
  *             while left < pos:
  *                 movable = True
  *                 for allele in alt:             # <<<<<<<<<<<<<<
@@ -17512,7 +17585,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         __pyx_t_11 = __pyx_v_alt; __Pyx_INCREF(__pyx_t_11); __pyx_t_4 = 0;
         __pyx_t_16 = NULL;
       } else {
-        __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_4 = -1; __pyx_t_11 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __pyx_t_16 = Py_TYPE(__pyx_t_11)->tp_iternext;
       }
@@ -17520,16 +17593,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_11)) {
           if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_11)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_23 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_23); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_23 = PyList_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_23); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_23 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_23 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_11)) {
           if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_11)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_23 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_23); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_23 = PyTuple_GET_ITEM(__pyx_t_11, __pyx_t_4); __Pyx_INCREF(__pyx_t_23); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_23 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_23 = PySequence_ITEM(__pyx_t_11, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else {
           __pyx_t_23 = __pyx_t_16(__pyx_t_11);
@@ -17537,7 +17610,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
             PyObject* exc_type = PyErr_Occurred();
             if (exc_type) {
               if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             }
             break;
           }
@@ -17546,19 +17619,19 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         __Pyx_XDECREF_SET(__pyx_v_allele, __pyx_t_23);
         __pyx_t_23 = 0;
 
-        /* "pysam/cvcf.pyx":805
+        /* "pysam/cvcf.pyx":810
  *                 movable = True
  *                 for allele in alt:
  *                     if len(allele) > len(ref):             # <<<<<<<<<<<<<<
  *                         longest, shortest = allele, ref
  *                     else:
  */
-        __pyx_t_3 = PyObject_Length(__pyx_v_allele); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_22 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = PyObject_Length(__pyx_v_allele); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_22 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_20 = ((__pyx_t_3 > __pyx_t_22) != 0);
         if (__pyx_t_20) {
 
-          /* "pysam/cvcf.pyx":806
+          /* "pysam/cvcf.pyx":811
  *                 for allele in alt:
  *                     if len(allele) > len(ref):
  *                         longest, shortest = allele, ref             # <<<<<<<<<<<<<<
@@ -17577,7 +17650,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         }
         /*else*/ {
 
-          /* "pysam/cvcf.pyx":808
+          /* "pysam/cvcf.pyx":813
  *                         longest, shortest = allele, ref
  *                     else:
  *                         longest, shortest = ref, allele             # <<<<<<<<<<<<<<
@@ -17595,35 +17668,35 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         }
         __pyx_L92:;
 
-        /* "pysam/cvcf.pyx":809
+        /* "pysam/cvcf.pyx":814
  *                     else:
  *                         longest, shortest = ref, allele
  *                     if len(longest) == len(shortest) or longest[:len(shortest)].upper() != shortest.upper():             # <<<<<<<<<<<<<<
  *                         movable = False
  *                     if longest[-1].upper() != longest[len(shortest)-1].upper():
  */
-        __pyx_t_22 = PyObject_Length(__pyx_v_longest); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_3 = PyObject_Length(__pyx_v_shortest); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_22 = PyObject_Length(__pyx_v_longest); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = PyObject_Length(__pyx_v_shortest); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_20 = (__pyx_t_22 == __pyx_t_3);
         if (!__pyx_t_20) {
-          __pyx_t_3 = PyObject_Length(__pyx_v_shortest); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-          __pyx_t_23 = __Pyx_PyObject_GetSlice(__pyx_v_longest, 0, __pyx_t_3, NULL, NULL, NULL, 0, 1, 1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_3 = PyObject_Length(__pyx_v_shortest); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_23 = __Pyx_PyObject_GetSlice(__pyx_v_longest, 0, __pyx_t_3, NULL, NULL, NULL, 0, 1, 1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_23);
-          __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_23, __pyx_n_s_upper); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_23, __pyx_n_s_upper); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_9);
           __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-          __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_23 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_23);
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-          __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_shortest, __pyx_n_s_upper); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_shortest, __pyx_n_s_upper); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_9);
-          __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_2);
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-          __pyx_t_9 = PyObject_RichCompare(__pyx_t_23, __pyx_t_2, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = PyObject_RichCompare(__pyx_t_23, __pyx_t_2, Py_NE); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
           __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-          __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
           __pyx_t_15 = __pyx_t_5;
         } else {
@@ -17631,7 +17704,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         }
         if (__pyx_t_15) {
 
-          /* "pysam/cvcf.pyx":810
+          /* "pysam/cvcf.pyx":815
  *                         longest, shortest = ref, allele
  *                     if len(longest) == len(shortest) or longest[:len(shortest)].upper() != shortest.upper():
  *                         movable = False             # <<<<<<<<<<<<<<
@@ -17643,39 +17716,39 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         }
         __pyx_L93:;
 
-        /* "pysam/cvcf.pyx":811
+        /* "pysam/cvcf.pyx":816
  *                     if len(longest) == len(shortest) or longest[:len(shortest)].upper() != shortest.upper():
  *                         movable = False
  *                     if longest[-1].upper() != longest[len(shortest)-1].upper():             # <<<<<<<<<<<<<<
  *                         movable = False
  *                 if not movable:
  */
-        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_longest, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_longest, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_upper); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_upper); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_3 = PyObject_Length(__pyx_v_shortest); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_3 = PyObject_Length(__pyx_v_shortest); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_22 = (__pyx_t_3 - 1);
-        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_longest, __pyx_t_22, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_longest, __pyx_t_22, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_upper); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_upper); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_23, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_23, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-        __pyx_t_23 = PyObject_RichCompare(__pyx_t_9, __pyx_t_2, Py_NE); __Pyx_XGOTREF(__pyx_t_23); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_23 = PyObject_RichCompare(__pyx_t_9, __pyx_t_2, Py_NE); __Pyx_XGOTREF(__pyx_t_23); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_23); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_23); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
         if (__pyx_t_15) {
 
-          /* "pysam/cvcf.pyx":812
+          /* "pysam/cvcf.pyx":817
  *                         movable = False
  *                     if longest[-1].upper() != longest[len(shortest)-1].upper():
  *                         movable = False             # <<<<<<<<<<<<<<
@@ -17689,7 +17762,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
 
-      /* "pysam/cvcf.pyx":813
+      /* "pysam/cvcf.pyx":818
  *                     if longest[-1].upper() != longest[len(shortest)-1].upper():
  *                         movable = False
  *                 if not movable:             # <<<<<<<<<<<<<<
@@ -17699,7 +17772,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __pyx_t_15 = ((!(__pyx_v_movable != 0)) != 0);
       if (__pyx_t_15) {
 
-        /* "pysam/cvcf.pyx":814
+        /* "pysam/cvcf.pyx":819
  *                         movable = False
  *                 if not movable:
  *                     break             # <<<<<<<<<<<<<<
@@ -17709,32 +17782,32 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         goto __pyx_L89_break;
       }
 
-      /* "pysam/cvcf.pyx":815
+      /* "pysam/cvcf.pyx":820
  *                 if not movable:
  *                     break
  *                 ref = ref[:-1]             # <<<<<<<<<<<<<<
  *                 alt = [allele[:-1] for allele in alt]
  *                 if min([len(allele) for allele in alt]) == 0 or len(ref) == 0:
  */
-      __pyx_t_11 = __Pyx_PyObject_GetSlice(__pyx_v_ref, 0, -1, NULL, NULL, &__pyx_slice__78, 0, 1, 1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 815; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyObject_GetSlice(__pyx_v_ref, 0, -1, NULL, NULL, &__pyx_slice__78, 0, 1, 1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __Pyx_DECREF_SET(__pyx_v_ref, __pyx_t_11);
       __pyx_t_11 = 0;
 
-      /* "pysam/cvcf.pyx":816
+      /* "pysam/cvcf.pyx":821
  *                     break
  *                 ref = ref[:-1]
  *                 alt = [allele[:-1] for allele in alt]             # <<<<<<<<<<<<<<
  *                 if min([len(allele) for allele in alt]) == 0 or len(ref) == 0:
  *                     ref = faref_leftflank[pos-left-1] + ref
  */
-      __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       if (PyList_CheckExact(__pyx_v_alt) || PyTuple_CheckExact(__pyx_v_alt)) {
         __pyx_t_23 = __pyx_v_alt; __Pyx_INCREF(__pyx_t_23); __pyx_t_4 = 0;
         __pyx_t_16 = NULL;
       } else {
-        __pyx_t_4 = -1; __pyx_t_23 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_4 = -1; __pyx_t_23 = PyObject_GetIter(__pyx_v_alt); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
         __pyx_t_16 = Py_TYPE(__pyx_t_23)->tp_iternext;
       }
@@ -17742,16 +17815,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_23)) {
           if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_23)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_2 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_2 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_23)) {
           if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_23)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_2 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
         } else {
           __pyx_t_2 = __pyx_t_16(__pyx_t_23);
@@ -17759,7 +17832,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
             PyObject* exc_type = PyErr_Occurred();
             if (exc_type) {
               if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+              else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             }
             break;
           }
@@ -17767,55 +17840,55 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         }
         __Pyx_XDECREF_SET(__pyx_v_allele, __pyx_t_2);
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_allele, 0, -1, NULL, NULL, &__pyx_slice__79, 0, 1, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_allele, 0, -1, NULL, NULL, &__pyx_slice__79, 0, 1, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        if (unlikely(__Pyx_ListComp_Append(__pyx_t_11, (PyObject*)__pyx_t_2))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(__Pyx_ListComp_Append(__pyx_t_11, (PyObject*)__pyx_t_2))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       }
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
       __Pyx_DECREF_SET(__pyx_v_alt, __pyx_t_11);
       __pyx_t_11 = 0;
 
-      /* "pysam/cvcf.pyx":817
+      /* "pysam/cvcf.pyx":822
  *                 ref = ref[:-1]
  *                 alt = [allele[:-1] for allele in alt]
  *                 if min([len(allele) for allele in alt]) == 0 or len(ref) == 0:             # <<<<<<<<<<<<<<
  *                     ref = faref_leftflank[pos-left-1] + ref
  *                     alt = [faref_leftflank[pos-left-1] + allele for allele in alt]
  */
-      __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __pyx_t_23 = __pyx_v_alt; __Pyx_INCREF(__pyx_t_23); __pyx_t_4 = 0;
       for (;;) {
         if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_23)) break;
         #if CYTHON_COMPILING_IN_CPYTHON
-        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #else
-        __pyx_t_2 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         #endif
         __Pyx_XDECREF_SET(__pyx_v_allele, __pyx_t_2);
         __pyx_t_2 = 0;
-        __pyx_t_22 = PyObject_Length(__pyx_v_allele); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_2 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_22 = PyObject_Length(__pyx_v_allele); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyInt_FromSsize_t(__pyx_t_22); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
-        if (unlikely(__Pyx_ListComp_Append(__pyx_t_11, (PyObject*)__pyx_t_2))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(__Pyx_ListComp_Append(__pyx_t_11, (PyObject*)__pyx_t_2))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
       }
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-      __pyx_t_23 = PyTuple_New(1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_23 = PyTuple_New(1); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_23);
       PyTuple_SET_ITEM(__pyx_t_23, 0, __pyx_t_11);
       __Pyx_GIVEREF(__pyx_t_11);
       __pyx_t_11 = 0;
-      __pyx_t_11 = __Pyx_PyObject_Call(__pyx_builtin_min, __pyx_t_23, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyObject_Call(__pyx_builtin_min, __pyx_t_23, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-      __pyx_t_23 = PyObject_RichCompare(__pyx_t_11, __pyx_int_0, Py_EQ); __Pyx_XGOTREF(__pyx_t_23); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_23 = PyObject_RichCompare(__pyx_t_11, __pyx_int_0, Py_EQ); __Pyx_XGOTREF(__pyx_t_23); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-      __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_23); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_23); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
       if (!__pyx_t_15) {
-        __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_4 = PyObject_Length(__pyx_v_ref); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __pyx_t_20 = (__pyx_t_4 == 0);
         __pyx_t_5 = __pyx_t_20;
       } else {
@@ -17823,79 +17896,79 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       if (__pyx_t_5) {
 
-        /* "pysam/cvcf.pyx":818
+        /* "pysam/cvcf.pyx":823
  *                 alt = [allele[:-1] for allele in alt]
  *                 if min([len(allele) for allele in alt]) == 0 or len(ref) == 0:
  *                     ref = faref_leftflank[pos-left-1] + ref             # <<<<<<<<<<<<<<
  *                     alt = [faref_leftflank[pos-left-1] + allele for allele in alt]
  *                     pos -= 1
  */
-        if (unlikely(!__pyx_v_faref_leftflank)) { __Pyx_RaiseUnboundLocalError("faref_leftflank"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-        if (unlikely(!__pyx_v_left)) { __Pyx_RaiseUnboundLocalError("left"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-        __pyx_t_23 = PyNumber_Subtract(__pyx_v_pos, __pyx_v_left); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(!__pyx_v_faref_leftflank)) { __Pyx_RaiseUnboundLocalError("faref_leftflank"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+        if (unlikely(!__pyx_v_left)) { __Pyx_RaiseUnboundLocalError("left"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+        __pyx_t_23 = PyNumber_Subtract(__pyx_v_pos, __pyx_v_left); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_23);
-        __pyx_t_11 = PyNumber_Subtract(__pyx_t_23, __pyx_int_1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = PyNumber_Subtract(__pyx_t_23, __pyx_int_1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
-        __pyx_t_23 = PyObject_GetItem(__pyx_v_faref_leftflank, __pyx_t_11); if (unlikely(__pyx_t_23 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_23 = PyObject_GetItem(__pyx_v_faref_leftflank, __pyx_t_11); if (unlikely(__pyx_t_23 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_23);
         __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-        __pyx_t_11 = PyNumber_Add(__pyx_t_23, __pyx_v_ref); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = PyNumber_Add(__pyx_t_23, __pyx_v_ref); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
         __Pyx_DECREF_SET(__pyx_v_ref, __pyx_t_11);
         __pyx_t_11 = 0;
 
-        /* "pysam/cvcf.pyx":819
+        /* "pysam/cvcf.pyx":824
  *                 if min([len(allele) for allele in alt]) == 0 or len(ref) == 0:
  *                     ref = faref_leftflank[pos-left-1] + ref
  *                     alt = [faref_leftflank[pos-left-1] + allele for allele in alt]             # <<<<<<<<<<<<<<
  *                     pos -= 1
  * 
  */
-        __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __pyx_t_23 = __pyx_v_alt; __Pyx_INCREF(__pyx_t_23); __pyx_t_4 = 0;
         for (;;) {
           if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_23)) break;
           #if CYTHON_COMPILING_IN_CPYTHON
-          __pyx_t_2 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #else
-          __pyx_t_2 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_2 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           #endif
           __Pyx_XDECREF_SET(__pyx_v_allele, __pyx_t_2);
           __pyx_t_2 = 0;
-          if (unlikely(!__pyx_v_faref_leftflank)) { __Pyx_RaiseUnboundLocalError("faref_leftflank"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-          if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-          if (unlikely(!__pyx_v_left)) { __Pyx_RaiseUnboundLocalError("left"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-          __pyx_t_2 = PyNumber_Subtract(__pyx_v_pos, __pyx_v_left); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          if (unlikely(!__pyx_v_faref_leftflank)) { __Pyx_RaiseUnboundLocalError("faref_leftflank"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+          if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+          if (unlikely(!__pyx_v_left)) { __Pyx_RaiseUnboundLocalError("left"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+          __pyx_t_2 = PyNumber_Subtract(__pyx_v_pos, __pyx_v_left); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_2);
-          __pyx_t_9 = PyNumber_Subtract(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = PyNumber_Subtract(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_9);
           __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-          __pyx_t_2 = PyObject_GetItem(__pyx_v_faref_leftflank, __pyx_t_9); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_2 = PyObject_GetItem(__pyx_v_faref_leftflank, __pyx_t_9); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_2);
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-          __pyx_t_9 = PyNumber_Add(__pyx_t_2, __pyx_v_allele); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = PyNumber_Add(__pyx_t_2, __pyx_v_allele); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_9);
           __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-          if (unlikely(__Pyx_ListComp_Append(__pyx_t_11, (PyObject*)__pyx_t_9))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          if (unlikely(__Pyx_ListComp_Append(__pyx_t_11, (PyObject*)__pyx_t_9))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         }
         __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
         __Pyx_DECREF_SET(__pyx_v_alt, __pyx_t_11);
         __pyx_t_11 = 0;
 
-        /* "pysam/cvcf.pyx":820
+        /* "pysam/cvcf.pyx":825
  *                     ref = faref_leftflank[pos-left-1] + ref
  *                     alt = [faref_leftflank[pos-left-1] + allele for allele in alt]
  *                     pos -= 1             # <<<<<<<<<<<<<<
  * 
  *         # parse sample columns
  */
-        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 820; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-        __pyx_t_11 = PyNumber_InPlaceSubtract(__pyx_v_pos, __pyx_int_1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 825; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+        __pyx_t_11 = PyNumber_InPlaceSubtract(__pyx_v_pos, __pyx_int_1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 825; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_11);
         __Pyx_XDECREF_SET(__pyx_v_pos, __pyx_t_11);
         __pyx_t_11 = 0;
@@ -17908,32 +17981,32 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   }
   __pyx_L87:;
 
-  /* "pysam/cvcf.pyx":823
+  /* "pysam/cvcf.pyx":828
  * 
  *         # parse sample columns
  *         samples = []             # <<<<<<<<<<<<<<
  *         for sample in cols[9:]:
  *             dict = {}
  */
-  __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
   __pyx_v_samples = ((PyObject*)__pyx_t_11);
   __pyx_t_11 = 0;
 
-  /* "pysam/cvcf.pyx":824
+  /* "pysam/cvcf.pyx":829
  *         # parse sample columns
  *         samples = []
  *         for sample in cols[9:]:             # <<<<<<<<<<<<<<
  *             dict = {}
  *             values = sample.split(':')
  */
-  __pyx_t_11 = __Pyx_PyObject_GetSlice(__pyx_v_cols, 9, 0, NULL, NULL, &__pyx_slice__80, 1, 0, 1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetSlice(__pyx_v_cols, 9, 0, NULL, NULL, &__pyx_slice__80, 1, 0, 1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
   if (PyList_CheckExact(__pyx_t_11) || PyTuple_CheckExact(__pyx_t_11)) {
     __pyx_t_23 = __pyx_t_11; __Pyx_INCREF(__pyx_t_23); __pyx_t_4 = 0;
     __pyx_t_16 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_23 = PyObject_GetIter(__pyx_t_11); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_23 = PyObject_GetIter(__pyx_t_11); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_23);
     __pyx_t_16 = Py_TYPE(__pyx_t_23)->tp_iternext;
   }
@@ -17942,16 +18015,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_23)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_23)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_11 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_11); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyList_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_11); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_11 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_23)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_23)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_11 = PyTuple_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_11); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyTuple_GET_ITEM(__pyx_t_23, __pyx_t_4); __Pyx_INCREF(__pyx_t_11); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_11 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PySequence_ITEM(__pyx_t_23, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_11 = __pyx_t_16(__pyx_t_23);
@@ -17959,7 +18032,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -17968,63 +18041,63 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __Pyx_XDECREF_SET(__pyx_v_sample, __pyx_t_11);
     __pyx_t_11 = 0;
 
-    /* "pysam/cvcf.pyx":825
+    /* "pysam/cvcf.pyx":830
  *         samples = []
  *         for sample in cols[9:]:
  *             dict = {}             # <<<<<<<<<<<<<<
  *             values = sample.split(':')
  *             if len(values) > len(format):
  */
-    __pyx_t_11 = PyDict_New(); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 825; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = PyDict_New(); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
     __Pyx_XDECREF_SET(__pyx_v_dict, ((PyObject*)__pyx_t_11));
     __pyx_t_11 = 0;
 
-    /* "pysam/cvcf.pyx":826
+    /* "pysam/cvcf.pyx":831
  *         for sample in cols[9:]:
  *             dict = {}
  *             values = sample.split(':')             # <<<<<<<<<<<<<<
  *             if len(values) > len(format):
  *                 self.error(line,self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" % (len(values),sample,len(format)))
  */
-    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_sample, __pyx_n_s_split); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_sample, __pyx_n_s_split); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_11);
-    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_tuple__81, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_tuple__81, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
     __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
     __Pyx_XDECREF_SET(__pyx_v_values, __pyx_t_9);
     __pyx_t_9 = 0;
 
-    /* "pysam/cvcf.pyx":827
+    /* "pysam/cvcf.pyx":832
  *             dict = {}
  *             values = sample.split(':')
  *             if len(values) > len(format):             # <<<<<<<<<<<<<<
  *                 self.error(line,self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" % (len(values),sample,len(format)))
  *             for idx in range(len(format)):
  */
-    __pyx_t_22 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_22 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_22 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_5 = ((__pyx_t_22 > __pyx_t_3) != 0);
     if (__pyx_t_5) {
 
-      /* "pysam/cvcf.pyx":828
+      /* "pysam/cvcf.pyx":833
  *             values = sample.split(':')
  *             if len(values) > len(format):
  *                 self.error(line,self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" % (len(values),sample,len(format)))             # <<<<<<<<<<<<<<
  *             for idx in range(len(format)):
  *                 expected = self.get_expected(format[idx], self._format, alt)
  */
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BAD_NUMBER_OF_VALUES); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BAD_NUMBER_OF_VALUES); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
-      __pyx_t_3 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_2 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_3 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_8 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_25 = PyTuple_New(3); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_25 = PyTuple_New(3); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_25);
       PyTuple_SET_ITEM(__pyx_t_25, 0, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
@@ -18035,10 +18108,10 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GIVEREF(__pyx_t_8);
       __pyx_t_2 = 0;
       __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_found_s_values_in_element_s_exp, __pyx_t_25); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_found_s_values_in_element_s_exp, __pyx_t_25); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
-      __pyx_t_25 = PyTuple_New(3); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_25 = PyTuple_New(3); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_25);
       __Pyx_INCREF(__pyx_v_line);
       PyTuple_SET_ITEM(__pyx_t_25, 0, __pyx_v_line);
@@ -18049,7 +18122,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GIVEREF(__pyx_t_8);
       __pyx_t_11 = 0;
       __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_25, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_25, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
@@ -18058,31 +18131,31 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     }
     __pyx_L105:;
 
-    /* "pysam/cvcf.pyx":829
+    /* "pysam/cvcf.pyx":834
  *             if len(values) > len(format):
  *                 self.error(line,self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" % (len(values),sample,len(format)))
  *             for idx in range(len(format)):             # <<<<<<<<<<<<<<
  *                 expected = self.get_expected(format[idx], self._format, alt)
  *                 if idx < len(values): value = values[idx]
  */
-    __pyx_t_3 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyObject_Length(__pyx_v_format); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     for (__pyx_t_22 = 0; __pyx_t_22 < __pyx_t_3; __pyx_t_22+=1) {
       __pyx_v_idx = __pyx_t_22;
 
-      /* "pysam/cvcf.pyx":830
+      /* "pysam/cvcf.pyx":835
  *                 self.error(line,self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" % (len(values),sample,len(format)))
  *             for idx in range(len(format)):
  *                 expected = self.get_expected(format[idx], self._format, alt)             # <<<<<<<<<<<<<<
  *                 if idx < len(values): value = values[idx]
  *                 else:
  */
-      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_get_expected); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_get_expected); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_25 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_25 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_25);
-      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
-      __pyx_t_11 = PyTuple_New(3); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PyTuple_New(3); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_25);
       __Pyx_GIVEREF(__pyx_t_25);
@@ -18093,24 +18166,24 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GIVEREF(__pyx_v_alt);
       __pyx_t_25 = 0;
       __pyx_t_9 = 0;
-      __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_11, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_11, NULL); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
       __Pyx_XDECREF_SET(__pyx_v_expected, __pyx_t_9);
       __pyx_t_9 = 0;
 
-      /* "pysam/cvcf.pyx":831
+      /* "pysam/cvcf.pyx":836
  *             for idx in range(len(format)):
  *                 expected = self.get_expected(format[idx], self._format, alt)
  *                 if idx < len(values): value = values[idx]             # <<<<<<<<<<<<<<
  *                 else:
  *                     if expected == -1: value = "."
  */
-      __pyx_t_26 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_26 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_26 = PyObject_Length(__pyx_v_values); if (unlikely(__pyx_t_26 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_5 = ((__pyx_v_idx < __pyx_t_26) != 0);
       if (__pyx_t_5) {
-        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_values, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_values, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_9);
         __pyx_t_9 = 0;
@@ -18118,15 +18191,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       /*else*/ {
 
-        /* "pysam/cvcf.pyx":833
+        /* "pysam/cvcf.pyx":838
  *                 if idx < len(values): value = values[idx]
  *                 else:
  *                     if expected == -1: value = "."             # <<<<<<<<<<<<<<
  *                     else: value = ",".join(["."]*expected)
  * 
  */
-        __pyx_t_9 = PyObject_RichCompare(__pyx_v_expected, __pyx_int_neg_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-        __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyObject_RichCompare(__pyx_v_expected, __pyx_int_neg_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_9); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         if (__pyx_t_5) {
           __Pyx_INCREF(__pyx_kp_s__8);
@@ -18135,24 +18208,24 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         }
         /*else*/ {
 
-          /* "pysam/cvcf.pyx":834
+          /* "pysam/cvcf.pyx":839
  *                 else:
  *                     if expected == -1: value = "."
  *                     else: value = ",".join(["."]*expected)             # <<<<<<<<<<<<<<
  * 
  *                 dict[format[idx]] = self.parse_formatdata(format[idx],
  */
-          __pyx_t_9 = PyList_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_9 = PyList_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_9);
           __Pyx_INCREF(__pyx_kp_s__8);
           PyList_SET_ITEM(__pyx_t_9, 0, __pyx_kp_s__8);
           __Pyx_GIVEREF(__pyx_kp_s__8);
-          { PyObject* __pyx_temp = PyNumber_InPlaceMultiply(__pyx_t_9, __pyx_v_expected); if (unlikely(!__pyx_temp)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          { PyObject* __pyx_temp = PyNumber_InPlaceMultiply(__pyx_t_9, __pyx_v_expected); if (unlikely(!__pyx_temp)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             __Pyx_GOTREF(__pyx_temp);
             __Pyx_DECREF(__pyx_t_9);
             __pyx_t_9 = __pyx_temp;
           }
-          __pyx_t_11 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_9); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_11 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_9); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_11);
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
           __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_11);
@@ -18162,36 +18235,36 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       __pyx_L108:;
 
-      /* "pysam/cvcf.pyx":836
+      /* "pysam/cvcf.pyx":841
  *                     else: value = ",".join(["."]*expected)
  * 
  *                 dict[format[idx]] = self.parse_formatdata(format[idx],             # <<<<<<<<<<<<<<
  *                                                           value,
  *                                                           self._format,
  */
-      __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_formatdata); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_formatdata); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
-      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_9);
 
-      /* "pysam/cvcf.pyx":838
+      /* "pysam/cvcf.pyx":843
  *                 dict[format[idx]] = self.parse_formatdata(format[idx],
  *                                                           value,
  *                                                           self._format,             # <<<<<<<<<<<<<<
  *                                                           line)
  *                 if expected != -1 and len(dict[format[idx]]) != expected:
  */
-      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
 
-      /* "pysam/cvcf.pyx":836
+      /* "pysam/cvcf.pyx":841
  *                     else: value = ",".join(["."]*expected)
  * 
  *                 dict[format[idx]] = self.parse_formatdata(format[idx],             # <<<<<<<<<<<<<<
  *                                                           value,
  *                                                           self._format,
  */
-      __pyx_t_25 = PyTuple_New(4); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_25 = PyTuple_New(4); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_25);
       PyTuple_SET_ITEM(__pyx_t_25, 0, __pyx_t_9);
       __Pyx_GIVEREF(__pyx_t_9);
@@ -18205,39 +18278,39 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GIVEREF(__pyx_v_line);
       __pyx_t_9 = 0;
       __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_25, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_25, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
       __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
-      __pyx_t_25 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+      __pyx_t_25 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
       __Pyx_GOTREF(__pyx_t_25);
-      if (unlikely(PyDict_SetItem(__pyx_v_dict, __pyx_t_25, __pyx_t_8) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(PyDict_SetItem(__pyx_v_dict, __pyx_t_25, __pyx_t_8) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
 
-      /* "pysam/cvcf.pyx":840
+      /* "pysam/cvcf.pyx":845
  *                                                           self._format,
  *                                                           line)
  *                 if expected != -1 and len(dict[format[idx]]) != expected:             # <<<<<<<<<<<<<<
  *                     self.error(line,self.BAD_NUMBER_OF_PARAMETERS,
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,dict[format[idx]]))
  */
-      __pyx_t_8 = PyObject_RichCompare(__pyx_v_expected, __pyx_int_neg_1, Py_NE); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = PyObject_RichCompare(__pyx_v_expected, __pyx_int_neg_1, Py_NE); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       if (__pyx_t_5) {
-        __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_25 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_8); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_25 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_8); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_25);
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-        __pyx_t_26 = PyObject_Length(__pyx_t_25); if (unlikely(__pyx_t_26 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_26 = PyObject_Length(__pyx_t_25); if (unlikely(__pyx_t_26 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
-        __pyx_t_25 = PyInt_FromSsize_t(__pyx_t_26); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_25 = PyInt_FromSsize_t(__pyx_t_26); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_25);
-        __pyx_t_8 = PyObject_RichCompare(__pyx_t_25, __pyx_v_expected, Py_NE); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = PyObject_RichCompare(__pyx_t_25, __pyx_v_expected, Py_NE); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
-        __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_15 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
         __pyx_t_20 = __pyx_t_15;
       } else {
@@ -18245,33 +18318,33 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       }
       if (__pyx_t_20) {
 
-        /* "pysam/cvcf.pyx":841
+        /* "pysam/cvcf.pyx":846
  *                                                           line)
  *                 if expected != -1 and len(dict[format[idx]]) != expected:
  *                     self.error(line,self.BAD_NUMBER_OF_PARAMETERS,             # <<<<<<<<<<<<<<
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,dict[format[idx]]))
  *                     if len(dict[format[idx]] ) < expected: dict[format[idx]] += [dict[format[idx]][-1]]*(expected-len(dict[format[idx]]))
  */
-        __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_error); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 846; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_8);
-        __pyx_t_25 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BAD_NUMBER_OF_PARAMETERS); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_25 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_BAD_NUMBER_OF_PARAMETERS); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 846; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_25);
 
-        /* "pysam/cvcf.pyx":842
+        /* "pysam/cvcf.pyx":847
  *                 if expected != -1 and len(dict[format[idx]]) != expected:
  *                     self.error(line,self.BAD_NUMBER_OF_PARAMETERS,
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,dict[format[idx]]))             # <<<<<<<<<<<<<<
  *                     if len(dict[format[idx]] ) < expected: dict[format[idx]] += [dict[format[idx]][-1]]*(expected-len(dict[format[idx]]))
  *                     dict[format[idx]] = dict[format[idx]][:expected]
  */
-        __pyx_t_11 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_11 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 847; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_11);
-        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_9 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 847; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_9); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_2 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_9); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 847; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 847; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_11);
         __Pyx_GIVEREF(__pyx_t_11);
@@ -18282,18 +18355,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         __Pyx_GIVEREF(__pyx_t_2);
         __pyx_t_11 = 0;
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_id_s_expected_s_parameters_got_s, __pyx_t_9); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_id_s_expected_s_parameters_got_s, __pyx_t_9); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 847; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-        /* "pysam/cvcf.pyx":841
+        /* "pysam/cvcf.pyx":846
  *                                                           line)
  *                 if expected != -1 and len(dict[format[idx]]) != expected:
  *                     self.error(line,self.BAD_NUMBER_OF_PARAMETERS,             # <<<<<<<<<<<<<<
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,dict[format[idx]]))
  *                     if len(dict[format[idx]] ) < expected: dict[format[idx]] += [dict[format[idx]][-1]]*(expected-len(dict[format[idx]]))
  */
-        __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 846; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_INCREF(__pyx_v_line);
         PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_line);
@@ -18304,97 +18377,97 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         __Pyx_GIVEREF(__pyx_t_2);
         __pyx_t_25 = 0;
         __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_9, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_9, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 846; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-        /* "pysam/cvcf.pyx":843
+        /* "pysam/cvcf.pyx":848
  *                     self.error(line,self.BAD_NUMBER_OF_PARAMETERS,
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,dict[format[idx]]))
  *                     if len(dict[format[idx]] ) < expected: dict[format[idx]] += [dict[format[idx]][-1]]*(expected-len(dict[format[idx]]))             # <<<<<<<<<<<<<<
  *                     dict[format[idx]] = dict[format[idx]][:expected]
  *             samples.append( dict )
  */
-        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_2); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_2); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_9);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_26 = PyObject_Length(__pyx_t_9); if (unlikely(__pyx_t_26 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_26 = PyObject_Length(__pyx_t_9); if (unlikely(__pyx_t_26 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_9 = PyInt_FromSsize_t(__pyx_t_26); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_9 = PyInt_FromSsize_t(__pyx_t_26); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_9);
-        __pyx_t_2 = PyObject_RichCompare(__pyx_t_9, __pyx_v_expected, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = PyObject_RichCompare(__pyx_t_9, __pyx_v_expected, Py_LT); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
-        __pyx_t_20 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_20 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_20 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         if (__pyx_t_20) {
-          __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_2);
-          __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_2); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_9 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_2); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_9);
-          __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_8);
-          __pyx_t_25 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_8); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_25 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_8); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_25);
           __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-          __pyx_t_8 = __Pyx_GetItemInt(__pyx_t_25, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_8 = __Pyx_GetItemInt(__pyx_t_25, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_8);
           __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
-          __pyx_t_25 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_25 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_25);
-          __pyx_t_11 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_25); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+          __pyx_t_11 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_25); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
           __Pyx_GOTREF(__pyx_t_11);
           __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
-          __pyx_t_26 = PyObject_Length(__pyx_t_11); if (unlikely(__pyx_t_26 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_26 = PyObject_Length(__pyx_t_11); if (unlikely(__pyx_t_26 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-          __pyx_t_11 = PyInt_FromSsize_t(__pyx_t_26); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_11 = PyInt_FromSsize_t(__pyx_t_26); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_11);
-          __pyx_t_25 = PyNumber_Subtract(__pyx_v_expected, __pyx_t_11); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_25 = PyNumber_Subtract(__pyx_v_expected, __pyx_t_11); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_25);
           __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-          __pyx_t_11 = PyList_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_11 = PyList_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_11);
           PyList_SET_ITEM(__pyx_t_11, 0, __pyx_t_8);
           __Pyx_GIVEREF(__pyx_t_8);
-          { PyObject* __pyx_temp = PyNumber_InPlaceMultiply(__pyx_t_11, __pyx_t_25); if (unlikely(!__pyx_temp)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          { PyObject* __pyx_temp = PyNumber_InPlaceMultiply(__pyx_t_11, __pyx_t_25); if (unlikely(!__pyx_temp)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
             __Pyx_GOTREF(__pyx_temp);
             __Pyx_DECREF(__pyx_t_11);
             __pyx_t_11 = __pyx_temp;
           }
           __pyx_t_8 = 0;
           __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
-          __pyx_t_25 = PyNumber_InPlaceAdd(__pyx_t_9, __pyx_t_11); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          __pyx_t_25 = PyNumber_InPlaceAdd(__pyx_t_9, __pyx_t_11); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_GOTREF(__pyx_t_25);
           __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
           __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
-          if (unlikely(PyDict_SetItem(__pyx_v_dict, __pyx_t_2, __pyx_t_25) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          if (unlikely(PyDict_SetItem(__pyx_v_dict, __pyx_t_2, __pyx_t_25) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
           __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
           __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
           goto __pyx_L111;
         }
         __pyx_L111:;
 
-        /* "pysam/cvcf.pyx":844
+        /* "pysam/cvcf.pyx":849
  *                                "id=%s, expected %s parameters, got %s" % (format[idx],expected,dict[format[idx]]))
  *                     if len(dict[format[idx]] ) < expected: dict[format[idx]] += [dict[format[idx]][-1]]*(expected-len(dict[format[idx]]))
  *                     dict[format[idx]] = dict[format[idx]][:expected]             # <<<<<<<<<<<<<<
  *             samples.append( dict )
  * 
  */
-        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_2);
-        __pyx_t_25 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_2); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_25 = __Pyx_PyDict_GetItem(__pyx_v_dict, __pyx_t_2); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_25);
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_t_25, 0, 0, NULL, &__pyx_v_expected, NULL, 0, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_t_25, 0, 0, NULL, &__pyx_v_expected, NULL, 0, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_GOTREF(__pyx_t_2);
         __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
-        __pyx_t_25 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_25 = __Pyx_GetItemInt(__pyx_v_format, __pyx_v_idx, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 1, 1); if (unlikely(__pyx_t_25 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_25);
-        if (unlikely(PyDict_SetItem(__pyx_v_dict, __pyx_t_25, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        if (unlikely(PyDict_SetItem(__pyx_v_dict, __pyx_t_25, __pyx_t_2) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_25); __pyx_t_25 = 0;
         __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
         goto __pyx_L110;
@@ -18402,116 +18475,116 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __pyx_L110:;
     }
 
-    /* "pysam/cvcf.pyx":845
+    /* "pysam/cvcf.pyx":850
  *                     if len(dict[format[idx]] ) < expected: dict[format[idx]] += [dict[format[idx]][-1]]*(expected-len(dict[format[idx]]))
  *                     dict[format[idx]] = dict[format[idx]][:expected]
  *             samples.append( dict )             # <<<<<<<<<<<<<<
  * 
  *         # done
  */
-    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_samples, __pyx_v_dict); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_samples, __pyx_v_dict); if (unlikely(__pyx_t_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 850; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
   __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
 
-  /* "pysam/cvcf.pyx":848
+  /* "pysam/cvcf.pyx":853
  * 
  *         # done
  *         d = {'chrom':chrom,             # <<<<<<<<<<<<<<
  *              'pos':pos,      # return 0-based position
  *              'id':id,
  */
-  __pyx_t_23 = PyDict_New(); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_23 = PyDict_New(); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_23);
-  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_chrom, __pyx_v_chrom) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_chrom, __pyx_v_chrom) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":849
+  /* "pysam/cvcf.pyx":854
  *         # done
  *         d = {'chrom':chrom,
  *              'pos':pos,      # return 0-based position             # <<<<<<<<<<<<<<
  *              'id':id,
  *              'ref':ref,
  */
-  if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
-  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_pos, __pyx_v_pos) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(!__pyx_v_pos)) { __Pyx_RaiseUnboundLocalError("pos"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 854; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_pos, __pyx_v_pos) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":850
+  /* "pysam/cvcf.pyx":855
  *         d = {'chrom':chrom,
  *              'pos':pos,      # return 0-based position
  *              'id':id,             # <<<<<<<<<<<<<<
  *              'ref':ref,
  *              'alt':alt,
  */
-  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_id, __pyx_v_id) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_id, __pyx_v_id) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":851
+  /* "pysam/cvcf.pyx":856
  *              'pos':pos,      # return 0-based position
  *              'id':id,
  *              'ref':ref,             # <<<<<<<<<<<<<<
  *              'alt':alt,
  *              'qual':qual,
  */
-  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_ref, __pyx_v_ref) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_ref, __pyx_v_ref) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":852
+  /* "pysam/cvcf.pyx":857
  *              'id':id,
  *              'ref':ref,
  *              'alt':alt,             # <<<<<<<<<<<<<<
  *              'qual':qual,
  *              'filter':filter,
  */
-  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_alt, __pyx_v_alt) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_alt, __pyx_v_alt) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":853
+  /* "pysam/cvcf.pyx":858
  *              'ref':ref,
  *              'alt':alt,
  *              'qual':qual,             # <<<<<<<<<<<<<<
  *              'filter':filter,
  *              'info':info,
  */
-  __pyx_t_2 = PyFloat_FromDouble(__pyx_v_qual); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyFloat_FromDouble(__pyx_v_qual); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 858; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_qual, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_qual, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":854
+  /* "pysam/cvcf.pyx":859
  *              'alt':alt,
  *              'qual':qual,
  *              'filter':filter,             # <<<<<<<<<<<<<<
  *              'info':info,
  *              'format':format}
  */
-  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_filter_2, __pyx_v_filter) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_filter_2, __pyx_v_filter) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":855
+  /* "pysam/cvcf.pyx":860
  *              'qual':qual,
  *              'filter':filter,
  *              'info':info,             # <<<<<<<<<<<<<<
  *              'format':format}
  *         for key,value in zip(self._samples,samples):
  */
-  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_info_2, __pyx_v_info) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_info_2, __pyx_v_info) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":856
+  /* "pysam/cvcf.pyx":861
  *              'filter':filter,
  *              'info':info,
  *              'format':format}             # <<<<<<<<<<<<<<
  *         for key,value in zip(self._samples,samples):
  *             d[key] = value
  */
-  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_format, __pyx_v_format) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_23, __pyx_n_s_format, __pyx_v_format) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_v_d = ((PyObject*)__pyx_t_23);
   __pyx_t_23 = 0;
 
-  /* "pysam/cvcf.pyx":857
+  /* "pysam/cvcf.pyx":862
  *              'info':info,
  *              'format':format}
  *         for key,value in zip(self._samples,samples):             # <<<<<<<<<<<<<<
  *             d[key] = value
  * 
  */
-  __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_23 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_23);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_23);
   __Pyx_GIVEREF(__pyx_t_23);
@@ -18519,14 +18592,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_samples);
   __Pyx_GIVEREF(__pyx_v_samples);
   __pyx_t_23 = 0;
-  __pyx_t_23 = __Pyx_PyObject_Call(__pyx_builtin_zip, __pyx_t_2, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_23 = __Pyx_PyObject_Call(__pyx_builtin_zip, __pyx_t_2, NULL); if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_23);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (PyList_CheckExact(__pyx_t_23) || PyTuple_CheckExact(__pyx_t_23)) {
     __pyx_t_2 = __pyx_t_23; __Pyx_INCREF(__pyx_t_2); __pyx_t_4 = 0;
     __pyx_t_16 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_23); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_23); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __pyx_t_16 = Py_TYPE(__pyx_t_2)->tp_iternext;
   }
@@ -18535,16 +18608,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     if (!__pyx_t_16 && PyList_CheckExact(__pyx_t_2)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_23 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_23); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_23 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_23); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_23 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_23 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_16 && PyTuple_CheckExact(__pyx_t_2)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_23 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_23); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_23 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_23); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_23 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_23 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_23 = __pyx_t_16(__pyx_t_2);
@@ -18552,7 +18625,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -18568,7 +18641,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       if (unlikely(size != 2)) {
         if (size > 2) __Pyx_RaiseTooManyValuesError(2);
         else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
-        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       }
       #if CYTHON_COMPILING_IN_CPYTHON
       if (likely(PyTuple_CheckExact(sequence))) {
@@ -18581,15 +18654,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_INCREF(__pyx_t_25);
       __Pyx_INCREF(__pyx_t_11);
       #else
-      __pyx_t_25 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_25 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_25);
-      __pyx_t_11 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_11 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_11);
       #endif
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
     } else {
       Py_ssize_t index = -1;
-      __pyx_t_9 = PyObject_GetIter(__pyx_t_23); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyObject_GetIter(__pyx_t_23); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_9);
       __Pyx_DECREF(__pyx_t_23); __pyx_t_23 = 0;
       __pyx_t_27 = Py_TYPE(__pyx_t_9)->tp_iternext;
@@ -18597,7 +18670,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_GOTREF(__pyx_t_25);
       index = 1; __pyx_t_11 = __pyx_t_27(__pyx_t_9); if (unlikely(!__pyx_t_11)) goto __pyx_L114_unpacking_failed;
       __Pyx_GOTREF(__pyx_t_11);
-      if (__Pyx_IternextUnpackEndCheck(__pyx_t_27(__pyx_t_9), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (__Pyx_IternextUnpackEndCheck(__pyx_t_27(__pyx_t_9), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_27 = NULL;
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       goto __pyx_L115_unpacking_done;
@@ -18605,7 +18678,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       __pyx_t_27 = NULL;
       if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_L115_unpacking_done:;
     }
     __Pyx_XDECREF_SET(__pyx_v_key, __pyx_t_25);
@@ -18613,18 +18686,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
     __Pyx_XDECREF_SET(__pyx_v_value, __pyx_t_11);
     __pyx_t_11 = 0;
 
-    /* "pysam/cvcf.pyx":858
+    /* "pysam/cvcf.pyx":863
  *              'format':format}
  *         for key,value in zip(self._samples,samples):
  *             d[key] = value             # <<<<<<<<<<<<<<
  * 
  *         return d
  */
-    if (unlikely(PyDict_SetItem(__pyx_v_d, __pyx_v_key, __pyx_v_value) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 858; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (unlikely(PyDict_SetItem(__pyx_v_d, __pyx_v_key, __pyx_v_value) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":860
+  /* "pysam/cvcf.pyx":865
  *             d[key] = value
  * 
  *         return d             # <<<<<<<<<<<<<<
@@ -18636,7 +18709,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   __pyx_r = __pyx_v_d;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":651
+  /* "pysam/cvcf.pyx":656
  *         return False
  * 
  *     def parse_data( self, line, lineparse=False ):             # <<<<<<<<<<<<<<
@@ -18700,7 +18773,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_32parse_data(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":863
+/* "pysam/cvcf.pyx":868
  * 
  * 
  *     def write_data(self, stream, data):             # <<<<<<<<<<<<<<
@@ -18742,16 +18815,16 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_35write_data(PyObject *__pyx_self, P
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_stream)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("write_data", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 863; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("write_data", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_data)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("write_data", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 863; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("write_data", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "write_data") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 863; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "write_data") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
       goto __pyx_L5_argtuple_error;
@@ -18766,7 +18839,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_35write_data(PyObject *__pyx_self, P
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("write_data", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 863; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("write_data", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.write_data", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -18808,14 +18881,14 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("write_data", 0);
 
-  /* "pysam/cvcf.pyx":864
+  /* "pysam/cvcf.pyx":869
  * 
  *     def write_data(self, stream, data):
  *         required = ['chrom','pos','id','ref','alt','qual','filter','info','format'] + self._samples             # <<<<<<<<<<<<<<
  *         for k in required:
  *             if k not in data: raise ValueError("Required key %s not found in data" % str(k))
  */
-  __pyx_t_1 = PyList_New(9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyList_New(9); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 869; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_n_s_chrom);
   PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_chrom);
@@ -18844,16 +18917,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   __Pyx_INCREF(__pyx_n_s_format);
   PyList_SET_ITEM(__pyx_t_1, 8, __pyx_n_s_format);
   __Pyx_GIVEREF(__pyx_n_s_format);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 869; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyNumber_Add(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyNumber_Add(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 869; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_v_required = __pyx_t_3;
   __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":865
+  /* "pysam/cvcf.pyx":870
  *     def write_data(self, stream, data):
  *         required = ['chrom','pos','id','ref','alt','qual','filter','info','format'] + self._samples
  *         for k in required:             # <<<<<<<<<<<<<<
@@ -18864,7 +18937,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
     __pyx_t_3 = __pyx_v_required; __Pyx_INCREF(__pyx_t_3); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_v_required); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 865; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_v_required); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __pyx_t_5 = Py_TYPE(__pyx_t_3)->tp_iternext;
   }
@@ -18872,16 +18945,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
     if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_3)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_3)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 865; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 865; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_3)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_3)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 865; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 865; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_3, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_2 = __pyx_t_5(__pyx_t_3);
@@ -18889,7 +18962,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 865; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -18898,57 +18971,57 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
     __Pyx_XDECREF_SET(__pyx_v_k, __pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":866
+    /* "pysam/cvcf.pyx":871
  *         required = ['chrom','pos','id','ref','alt','qual','filter','info','format'] + self._samples
  *         for k in required:
  *             if k not in data: raise ValueError("Required key %s not found in data" % str(k))             # <<<<<<<<<<<<<<
  *         if data['alt'] == []: alt = "."
  *         else: alt = ",".join(data['alt'])
  */
-    __pyx_t_6 = (__Pyx_PySequence_Contains(__pyx_v_k, __pyx_v_data, Py_NE)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = (__Pyx_PySequence_Contains(__pyx_v_k, __pyx_v_data, Py_NE)); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_7 = (__pyx_t_6 != 0);
     if (__pyx_t_7) {
-      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_INCREF(__pyx_v_k);
       PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_k);
       __Pyx_GIVEREF(__pyx_v_k);
-      __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_Required_key_s_not_found_in_data, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyString_Format(__pyx_kp_s_Required_key_s_not_found_in_data, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
       PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
       __Pyx_GIVEREF(__pyx_t_2);
       __pyx_t_2 = 0;
-      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_Raise(__pyx_t_2, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
   }
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":867
+  /* "pysam/cvcf.pyx":872
  *         for k in required:
  *             if k not in data: raise ValueError("Required key %s not found in data" % str(k))
  *         if data['alt'] == []: alt = "."             # <<<<<<<<<<<<<<
  *         else: alt = ",".join(data['alt'])
  *         if data['filter'] == None: filter = "."
  */
-  __pyx_t_3 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_alt); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_3 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_alt); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 872; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyObject_RichCompare(__pyx_t_3, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_t_3, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_7) {
     __Pyx_INCREF(__pyx_kp_s__8);
@@ -18957,16 +19030,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":868
+    /* "pysam/cvcf.pyx":873
  *             if k not in data: raise ValueError("Required key %s not found in data" % str(k))
  *         if data['alt'] == []: alt = "."
  *         else: alt = ",".join(data['alt'])             # <<<<<<<<<<<<<<
  *         if data['filter'] == None: filter = "."
  *         elif data['filter'] == []:
  */
-    __pyx_t_1 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_alt); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_1 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_alt); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyString_Join(__pyx_kp_s__2, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_v_alt = __pyx_t_2;
@@ -18974,18 +19047,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   }
   __pyx_L6:;
 
-  /* "pysam/cvcf.pyx":869
+  /* "pysam/cvcf.pyx":874
  *         if data['alt'] == []: alt = "."
  *         else: alt = ",".join(data['alt'])
  *         if data['filter'] == None: filter = "."             # <<<<<<<<<<<<<<
  *         elif data['filter'] == []:
  *             if self._version == 33: filter = "0"
  */
-  __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_filter_2); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 869; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_filter_2); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 869; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_t_2, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 869; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_7) {
     __Pyx_INCREF(__pyx_kp_s__8);
@@ -18993,36 +19066,36 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
     goto __pyx_L7;
   }
 
-  /* "pysam/cvcf.pyx":870
+  /* "pysam/cvcf.pyx":875
  *         else: alt = ",".join(data['alt'])
  *         if data['filter'] == None: filter = "."
  *         elif data['filter'] == []:             # <<<<<<<<<<<<<<
  *             if self._version == 33: filter = "0"
  *             else: filter = "PASS"
  */
-  __pyx_t_1 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_filter_2); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_1 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_filter_2); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   if (__pyx_t_7) {
 
-    /* "pysam/cvcf.pyx":871
+    /* "pysam/cvcf.pyx":876
  *         if data['filter'] == None: filter = "."
  *         elif data['filter'] == []:
  *             if self._version == 33: filter = "0"             # <<<<<<<<<<<<<<
  *             else: filter = "PASS"
  *         else: filter = ';'.join(data['filter'])
  */
-    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_version); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 876; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
-    __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_33, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_33, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 876; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-    __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 876; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     if (__pyx_t_7) {
       __Pyx_INCREF(__pyx_kp_s_0);
@@ -19031,7 +19104,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
     }
     /*else*/ {
 
-      /* "pysam/cvcf.pyx":872
+      /* "pysam/cvcf.pyx":877
  *         elif data['filter'] == []:
  *             if self._version == 33: filter = "0"
  *             else: filter = "PASS"             # <<<<<<<<<<<<<<
@@ -19046,16 +19119,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":873
+    /* "pysam/cvcf.pyx":878
  *             if self._version == 33: filter = "0"
  *             else: filter = "PASS"
  *         else: filter = ';'.join(data['filter'])             # <<<<<<<<<<<<<<
  *         if data['qual'] == -1: qual = "."
  *         else: qual = str(data['qual'])
  */
-    __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_filter_2); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_filter_2); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__10, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 873; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyString_Join(__pyx_kp_s__10, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __pyx_v_filter = __pyx_t_3;
@@ -19063,18 +19136,18 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   }
   __pyx_L7:;
 
-  /* "pysam/cvcf.pyx":874
+  /* "pysam/cvcf.pyx":879
  *             else: filter = "PASS"
  *         else: filter = ';'.join(data['filter'])
  *         if data['qual'] == -1: qual = "."             # <<<<<<<<<<<<<<
  *         else: qual = str(data['qual'])
  * 
  */
-  __pyx_t_3 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_qual); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_3 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_qual); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 879; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_neg_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyObject_RichCompare(__pyx_t_3, __pyx_int_neg_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 879; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 874; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 879; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_7) {
     __Pyx_INCREF(__pyx_kp_s__8);
@@ -19083,21 +19156,21 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   }
   /*else*/ {
 
-    /* "pysam/cvcf.pyx":875
+    /* "pysam/cvcf.pyx":880
  *         else: filter = ';'.join(data['filter'])
  *         if data['qual'] == -1: qual = "."
  *         else: qual = str(data['qual'])             # <<<<<<<<<<<<<<
  * 
  *         output = [data['chrom'],
  */
-    __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_qual); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+    __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_qual); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 880; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 880; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 880; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __pyx_v_qual = __pyx_t_2;
@@ -19105,71 +19178,87 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   }
   __pyx_L9:;
 
-  /* "pysam/cvcf.pyx":877
+  /* "pysam/cvcf.pyx":882
  *         else: qual = str(data['qual'])
  * 
  *         output = [data['chrom'],             # <<<<<<<<<<<<<<
  *                   str(data['pos']+1),   # change to 1-based position
  *                   data['id'],
  */
-  __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_chrom); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 877; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_2 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_chrom); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 882; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_2);
 
-  /* "pysam/cvcf.pyx":878
+  /* "pysam/cvcf.pyx":883
  * 
  *         output = [data['chrom'],
  *                   str(data['pos']+1),   # change to 1-based position             # <<<<<<<<<<<<<<
  *                   data['id'],
  *                   data['ref'],
  */
-  __pyx_t_3 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_pos); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_3 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_pos); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 883; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_1 = PyNumber_Add(__pyx_t_3, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyNumber_Add(__pyx_t_3, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 883; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 883; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
   __Pyx_GIVEREF(__pyx_t_1);
   __pyx_t_1 = 0;
-  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 878; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 883; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":879
+  /* "pysam/cvcf.pyx":884
  *         output = [data['chrom'],
  *                   str(data['pos']+1),   # change to 1-based position
  *                   data['id'],             # <<<<<<<<<<<<<<
  *                   data['ref'],
  *                   alt,
  */
-  __pyx_t_3 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_id); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 879; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_3 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_id); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_3);
 
-  /* "pysam/cvcf.pyx":880
+  /* "pysam/cvcf.pyx":885
  *                   str(data['pos']+1),   # change to 1-based position
  *                   data['id'],
  *                   data['ref'],             # <<<<<<<<<<<<<<
  *                   alt,
  *                   qual,
  */
-  __pyx_t_8 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_ref); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 880; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __pyx_t_8 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_ref); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 885; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_8);
 
-  /* "pysam/cvcf.pyx":884
+  /* "pysam/cvcf.pyx":889
  *                   qual,
  *                   filter,
- *                   self.format_formatdata( data['info'], self._info, separator=";" ),             # <<<<<<<<<<<<<<
- *                   self.format_formatdata( data['format'], self._format, value=False ) ]
- * 
+ *                   self.format_formatdata(             # <<<<<<<<<<<<<<
+ *                       data['info'], self._info, separator=";"),
+ *                   self.format_formatdata(
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_formatdata); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_formatdata); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_10 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_info_2); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+
+  /* "pysam/cvcf.pyx":890
+ *                   filter,
+ *                   self.format_formatdata(
+ *                       data['info'], self._info, separator=";"),             # <<<<<<<<<<<<<<
+ *                   self.format_formatdata(
+ *                       data['format'], self._format, value=False)]
+ */
+  __pyx_t_10 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_info_2); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 890; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
   __Pyx_GOTREF(__pyx_t_10);
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 890; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  __pyx_t_12 = PyTuple_New(2); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/cvcf.pyx":889
+ *                   qual,
+ *                   filter,
+ *                   self.format_formatdata(             # <<<<<<<<<<<<<<
+ *                       data['info'], self._info, separator=";"),
+ *                   self.format_formatdata(
+ */
+  __pyx_t_12 = PyTuple_New(2); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_12);
   PyTuple_SET_ITEM(__pyx_t_12, 0, __pyx_t_10);
   __Pyx_GIVEREF(__pyx_t_10);
@@ -19177,53 +19266,85 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   __Pyx_GIVEREF(__pyx_t_11);
   __pyx_t_10 = 0;
   __pyx_t_11 = 0;
-  __pyx_t_11 = PyDict_New(); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = PyDict_New(); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  if (PyDict_SetItem(__pyx_t_11, __pyx_n_s_separator, __pyx_kp_s__10) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_12, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 884; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_11, __pyx_n_s_separator, __pyx_kp_s__10) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_12, __pyx_t_11); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
   __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
 
-  /* "pysam/cvcf.pyx":885
- *                   filter,
- *                   self.format_formatdata( data['info'], self._info, separator=";" ),
- *                   self.format_formatdata( data['format'], self._format, value=False ) ]             # <<<<<<<<<<<<<<
+  /* "pysam/cvcf.pyx":891
+ *                   self.format_formatdata(
+ *                       data['info'], self._info, separator=";"),
+ *                   self.format_formatdata(             # <<<<<<<<<<<<<<
+ *                       data['format'], self._format, value=False)]
  * 
- *         for s in self._samples:
  */
-  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_formatdata); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_formatdata); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_11);
-  __pyx_t_12 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_format); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 885; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
-  __Pyx_GOTREF(__pyx_t_12);
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_9);
-  __pyx_t_13 = PyTuple_New(2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_13);
+
+  /* "pysam/cvcf.pyx":892
+ *                       data['info'], self._info, separator=";"),
+ *                   self.format_formatdata(
+ *                       data['format'], self._format, value=False)]             # <<<<<<<<<<<<<<
+ * 
+ *         for s in self._samples:
+ */
+  __pyx_t_12 = PyObject_GetItem(__pyx_v_data, __pyx_n_s_format); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+  __Pyx_GOTREF(__pyx_t_12);
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_9);
+
+  /* "pysam/cvcf.pyx":891
+ *                   self.format_formatdata(
+ *                       data['info'], self._info, separator=";"),
+ *                   self.format_formatdata(             # <<<<<<<<<<<<<<
+ *                       data['format'], self._format, value=False)]
+ * 
+ */
+  __pyx_t_13 = PyTuple_New(2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_13);
   PyTuple_SET_ITEM(__pyx_t_13, 0, __pyx_t_12);
   __Pyx_GIVEREF(__pyx_t_12);
   PyTuple_SET_ITEM(__pyx_t_13, 1, __pyx_t_9);
   __Pyx_GIVEREF(__pyx_t_9);
   __pyx_t_12 = 0;
   __pyx_t_9 = 0;
-  __pyx_t_9 = PyDict_New(); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = PyDict_New(); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
-  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_value, Py_False) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_13, __pyx_t_9); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 885; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/cvcf.pyx":892
+ *                       data['info'], self._info, separator=";"),
+ *                   self.format_formatdata(
+ *                       data['format'], self._format, value=False)]             # <<<<<<<<<<<<<<
+ * 
+ *         for s in self._samples:
+ */
+  if (PyDict_SetItem(__pyx_t_9, __pyx_n_s_value, Py_False) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/cvcf.pyx":891
+ *                   self.format_formatdata(
+ *                       data['info'], self._info, separator=";"),
+ *                   self.format_formatdata(             # <<<<<<<<<<<<<<
+ *                       data['format'], self._format, value=False)]
+ * 
+ */
+  __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_11, __pyx_t_13, __pyx_t_9); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 891; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_12);
   __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
   __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
   __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
 
-  /* "pysam/cvcf.pyx":877
+  /* "pysam/cvcf.pyx":882
  *         else: qual = str(data['qual'])
  * 
  *         output = [data['chrom'],             # <<<<<<<<<<<<<<
  *                   str(data['pos']+1),   # change to 1-based position
  *                   data['id'],
  */
-  __pyx_t_9 = PyList_New(9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 877; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = PyList_New(9); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 882; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
   PyList_SET_ITEM(__pyx_t_9, 0, __pyx_t_2);
   __Pyx_GIVEREF(__pyx_t_2);
@@ -19255,20 +19376,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   __pyx_v_output = ((PyObject*)__pyx_t_9);
   __pyx_t_9 = 0;
 
-  /* "pysam/cvcf.pyx":887
- *                   self.format_formatdata( data['format'], self._format, value=False ) ]
+  /* "pysam/cvcf.pyx":894
+ *                       data['format'], self._format, value=False)]
  * 
  *         for s in self._samples:             # <<<<<<<<<<<<<<
- *             output.append( self.format_formatdata( data[s], self._format, key=False ) )
- * 
+ *             output.append(self.format_formatdata(
+ *                 data[s], self._format, key=False))
  */
-  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_9);
   if (PyList_CheckExact(__pyx_t_9) || PyTuple_CheckExact(__pyx_t_9)) {
     __pyx_t_12 = __pyx_t_9; __Pyx_INCREF(__pyx_t_12); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_12 = PyObject_GetIter(__pyx_t_9); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_12 = PyObject_GetIter(__pyx_t_9); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_12);
     __pyx_t_5 = Py_TYPE(__pyx_t_12)->tp_iternext;
   }
@@ -19277,16 +19398,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
     if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_12)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_12)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_9 = PyList_GET_ITEM(__pyx_t_12, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyList_GET_ITEM(__pyx_t_12, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_9 = PySequence_ITEM(__pyx_t_12, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PySequence_ITEM(__pyx_t_12, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_12)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_12)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_9 = PyTuple_GET_ITEM(__pyx_t_12, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PyTuple_GET_ITEM(__pyx_t_12, __pyx_t_4); __Pyx_INCREF(__pyx_t_9); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_9 = PySequence_ITEM(__pyx_t_12, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_9 = PySequence_ITEM(__pyx_t_12, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_9 = __pyx_t_5(__pyx_t_12);
@@ -19294,7 +19415,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -19303,20 +19424,36 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
     __Pyx_XDECREF_SET(__pyx_v_s, __pyx_t_9);
     __pyx_t_9 = 0;
 
-    /* "pysam/cvcf.pyx":888
+    /* "pysam/cvcf.pyx":895
  * 
  *         for s in self._samples:
- *             output.append( self.format_formatdata( data[s], self._format, key=False ) )             # <<<<<<<<<<<<<<
+ *             output.append(self.format_formatdata(             # <<<<<<<<<<<<<<
+ *                 data[s], self._format, key=False))
  * 
- *         stream.write( "\t".join(output) + "\n" )
  */
-    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_formatdata); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_formatdata); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 895; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_9);
-    __pyx_t_10 = PyObject_GetItem(__pyx_v_data, __pyx_v_s); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+
+    /* "pysam/cvcf.pyx":896
+ *         for s in self._samples:
+ *             output.append(self.format_formatdata(
+ *                 data[s], self._format, key=False))             # <<<<<<<<<<<<<<
+ * 
+ *         stream.write( "\t".join(output) + "\n" )
+ */
+    __pyx_t_10 = PyObject_GetItem(__pyx_v_data, __pyx_v_s); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 896; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
     __Pyx_GOTREF(__pyx_t_10);
-    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 896; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
-    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/cvcf.pyx":895
+ * 
+ *         for s in self._samples:
+ *             output.append(self.format_formatdata(             # <<<<<<<<<<<<<<
+ *                 data[s], self._format, key=False))
+ * 
+ */
+    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 895; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_10);
     __Pyx_GIVEREF(__pyx_t_10);
@@ -19324,45 +19461,61 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
     __Pyx_GIVEREF(__pyx_t_8);
     __pyx_t_10 = 0;
     __pyx_t_8 = 0;
-    __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_8 = PyDict_New(); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 895; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_8);
-    if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_key, Py_False) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_3, __pyx_t_8); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/cvcf.pyx":896
+ *         for s in self._samples:
+ *             output.append(self.format_formatdata(
+ *                 data[s], self._format, key=False))             # <<<<<<<<<<<<<<
+ * 
+ *         stream.write( "\t".join(output) + "\n" )
+ */
+    if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_key, Py_False) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 895; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+    /* "pysam/cvcf.pyx":895
+ * 
+ *         for s in self._samples:
+ *             output.append(self.format_formatdata(             # <<<<<<<<<<<<<<
+ *                 data[s], self._format, key=False))
+ * 
+ */
+    __pyx_t_10 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_t_3, __pyx_t_8); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 895; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_10);
     __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
     __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-    __pyx_t_14 = __Pyx_PyList_Append(__pyx_v_output, __pyx_t_10); if (unlikely(__pyx_t_14 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_14 = __Pyx_PyList_Append(__pyx_v_output, __pyx_t_10); if (unlikely(__pyx_t_14 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 895; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   }
   __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
 
-  /* "pysam/cvcf.pyx":890
- *             output.append( self.format_formatdata( data[s], self._format, key=False ) )
+  /* "pysam/cvcf.pyx":898
+ *                 data[s], self._format, key=False))
  * 
  *         stream.write( "\t".join(output) + "\n" )             # <<<<<<<<<<<<<<
  * 
  *     def _parse_header(self, stream):
  */
-  __pyx_t_12 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 890; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_12 = __Pyx_PyObject_GetAttrStr(__pyx_v_stream, __pyx_n_s_write); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_12);
-  __pyx_t_10 = __Pyx_PyString_Join(__pyx_kp_s__55, __pyx_v_output); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 890; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = __Pyx_PyString_Join(__pyx_kp_s__55, __pyx_v_output); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
-  __pyx_t_8 = PyNumber_Add(__pyx_t_10, __pyx_kp_s__60); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 890; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = PyNumber_Add(__pyx_t_10, __pyx_kp_s__60); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
-  __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 890; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_10 = PyTuple_New(1); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_10);
   PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_8);
   __Pyx_GIVEREF(__pyx_t_8);
   __pyx_t_8 = 0;
-  __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_10, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 890; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_10, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_8);
   __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
   __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
   __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
 
-  /* "pysam/cvcf.pyx":863
+  /* "pysam/cvcf.pyx":868
  * 
  * 
  *     def write_data(self, stream, data):             # <<<<<<<<<<<<<<
@@ -19398,7 +19551,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_34write_data(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":892
+/* "pysam/cvcf.pyx":900
  *         stream.write( "\t".join(output) + "\n" )
  * 
  *     def _parse_header(self, stream):             # <<<<<<<<<<<<<<
@@ -19438,11 +19591,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_37_parse_header(PyObject *__pyx_self
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_stream)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("_parse_header", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("_parse_header", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_parse_header") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_parse_header") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -19455,7 +19608,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_37_parse_header(PyObject *__pyx_self
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_parse_header", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("_parse_header", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF._parse_header", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -19477,34 +19630,35 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_36_parse_header(CYTHON_UNUSED PyObje
   PyObject *(*__pyx_t_3)(PyObject *);
   PyObject *__pyx_t_4 = NULL;
   PyObject *__pyx_t_5 = NULL;
-  int __pyx_t_6;
-  PyObject *__pyx_t_7 = NULL;
+  struct __pyx_opt_args_5pysam_6ctabix__force_str __pyx_t_6;
+  int __pyx_t_7;
+  PyObject *__pyx_t_8 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("_parse_header", 0);
 
-  /* "pysam/cvcf.pyx":893
+  /* "pysam/cvcf.pyx":901
  * 
  *     def _parse_header(self, stream):
  *         self._lineno = 0             # <<<<<<<<<<<<<<
  *         for line in stream:
- *             line = ctabix._force_str(line)
+ *             line = ctabix._force_str(line, self.encoding)
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_lineno, __pyx_int_0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 893; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_lineno, __pyx_int_0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 901; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":894
+  /* "pysam/cvcf.pyx":902
  *     def _parse_header(self, stream):
  *         self._lineno = 0
  *         for line in stream:             # <<<<<<<<<<<<<<
- *             line = ctabix._force_str(line)
+ *             line = ctabix._force_str(line, self.encoding)
  *             self._lineno += 1
  */
   if (PyList_CheckExact(__pyx_v_stream) || PyTuple_CheckExact(__pyx_v_stream)) {
     __pyx_t_1 = __pyx_v_stream; __Pyx_INCREF(__pyx_t_1); __pyx_t_2 = 0;
     __pyx_t_3 = NULL;
   } else {
-    __pyx_t_2 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_stream); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_stream); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_t_3 = Py_TYPE(__pyx_t_1)->tp_iternext;
   }
@@ -19512,16 +19666,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_36_parse_header(CYTHON_UNUSED PyObje
     if (!__pyx_t_3 && PyList_CheckExact(__pyx_t_1)) {
       if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_4 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_4); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_4); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_4 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_3 && PyTuple_CheckExact(__pyx_t_1)) {
       if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_4); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_4); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_4 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_4 = __pyx_t_3(__pyx_t_1);
@@ -19529,7 +19683,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_36_parse_header(CYTHON_UNUSED PyObje
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 894; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 902; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -19538,135 +19692,140 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_36_parse_header(CYTHON_UNUSED PyObje
     __Pyx_XDECREF_SET(__pyx_v_line, __pyx_t_4);
     __pyx_t_4 = 0;
 
-    /* "pysam/cvcf.pyx":895
+    /* "pysam/cvcf.pyx":903
  *         self._lineno = 0
  *         for line in stream:
- *             line = ctabix._force_str(line)             # <<<<<<<<<<<<<<
+ *             line = ctabix._force_str(line, self.encoding)             # <<<<<<<<<<<<<<
  *             self._lineno += 1
  *             if line.startswith('##'):
  */
-    __pyx_t_4 = __pyx_f_5pysam_6ctabix__force_str(__pyx_v_line); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 895; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_encoding); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF_SET(__pyx_v_line, __pyx_t_4);
-    __pyx_t_4 = 0;
+    __pyx_t_6.__pyx_n = 1;
+    __pyx_t_6.encoding = __pyx_t_4;
+    __pyx_t_5 = __pyx_f_5pysam_6ctabix__force_str(__pyx_v_line, &__pyx_t_6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    __Pyx_DECREF_SET(__pyx_v_line, __pyx_t_5);
+    __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":896
+    /* "pysam/cvcf.pyx":904
  *         for line in stream:
- *             line = ctabix._force_str(line)
+ *             line = ctabix._force_str(line, self.encoding)
  *             self._lineno += 1             # <<<<<<<<<<<<<<
  *             if line.startswith('##'):
- *                 self.parse_header( line.strip() )
+ *                 self.parse_header(line.strip())
  */
-    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_lineno); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 896; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_4);
-    __pyx_t_5 = PyNumber_InPlaceAdd(__pyx_t_4, __pyx_int_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 896; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_lineno); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 904; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_lineno, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 896; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = PyNumber_InPlaceAdd(__pyx_t_5, __pyx_int_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 904; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
     __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+    if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_lineno, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 904; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-    /* "pysam/cvcf.pyx":897
- *             line = ctabix._force_str(line)
+    /* "pysam/cvcf.pyx":905
+ *             line = ctabix._force_str(line, self.encoding)
  *             self._lineno += 1
  *             if line.startswith('##'):             # <<<<<<<<<<<<<<
- *                 self.parse_header( line.strip() )
+ *                 self.parse_header(line.strip())
  *             elif line.startswith('#'):
  */
-    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 897; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_tuple__82, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 897; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 905; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_4);
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 897; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_tuple__82, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 905; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-    if (__pyx_t_6) {
+    __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 905; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+    if (__pyx_t_7) {
 
-      /* "pysam/cvcf.pyx":898
+      /* "pysam/cvcf.pyx":906
  *             self._lineno += 1
  *             if line.startswith('##'):
- *                 self.parse_header( line.strip() )             # <<<<<<<<<<<<<<
+ *                 self.parse_header(line.strip())             # <<<<<<<<<<<<<<
  *             elif line.startswith('#'):
- *                 self.parse_heading( line.strip() )
+ *                 self.parse_heading(line.strip())
  */
-      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_header); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_header); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_5);
-      PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_7);
-      __Pyx_GIVEREF(__pyx_t_7);
-      __pyx_t_7 = 0;
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_5, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 898; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
+      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_4);
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_4);
+      PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_8);
+      __Pyx_GIVEREF(__pyx_t_8);
+      __pyx_t_8 = 0;
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_4, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       goto __pyx_L5;
     }
 
-    /* "pysam/cvcf.pyx":899
+    /* "pysam/cvcf.pyx":907
  *             if line.startswith('##'):
- *                 self.parse_header( line.strip() )
+ *                 self.parse_header(line.strip())
  *             elif line.startswith('#'):             # <<<<<<<<<<<<<<
- *                 self.parse_heading( line.strip() )
+ *                 self.parse_heading(line.strip())
  *                 self.enter_default_format()
  */
-    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 899; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_tuple__83, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 899; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_GOTREF(__pyx_t_5);
-    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 899; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-    if (__pyx_t_6) {
+    __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 907; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_8);
+    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_tuple__83, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 907; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_GOTREF(__pyx_t_4);
+    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+    __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 907; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+    if (__pyx_t_7) {
 
-      /* "pysam/cvcf.pyx":900
- *                 self.parse_header( line.strip() )
+      /* "pysam/cvcf.pyx":908
+ *                 self.parse_header(line.strip())
  *             elif line.startswith('#'):
- *                 self.parse_heading( line.strip() )             # <<<<<<<<<<<<<<
+ *                 self.parse_heading(line.strip())             # <<<<<<<<<<<<<<
  *                 self.enter_default_format()
  *             else:
  */
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_heading); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_heading); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
-      __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_4);
-      __Pyx_GIVEREF(__pyx_t_4);
-      __pyx_t_4 = 0;
-      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_7, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+      __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      __pyx_t_8 = PyTuple_New(1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      PyTuple_SET_ITEM(__pyx_t_8, 0, __pyx_t_5);
+      __Pyx_GIVEREF(__pyx_t_5);
+      __pyx_t_5 = 0;
+      __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_8, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-      /* "pysam/cvcf.pyx":901
+      /* "pysam/cvcf.pyx":909
  *             elif line.startswith('#'):
- *                 self.parse_heading( line.strip() )
+ *                 self.parse_heading(line.strip())
  *                 self.enter_default_format()             # <<<<<<<<<<<<<<
  *             else:
  *                 break
  */
-      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_enter_default_format); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 901; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_7 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 901; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-      __Pyx_GOTREF(__pyx_t_7);
-      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_enter_default_format); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 909; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_5);
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 909; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __Pyx_GOTREF(__pyx_t_8);
+      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
       goto __pyx_L5;
     }
     /*else*/ {
 
-      /* "pysam/cvcf.pyx":903
+      /* "pysam/cvcf.pyx":911
  *                 self.enter_default_format()
  *             else:
  *                 break             # <<<<<<<<<<<<<<
@@ -19680,7 +19839,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_36_parse_header(CYTHON_UNUSED PyObje
   __pyx_L4_break:;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":904
+  /* "pysam/cvcf.pyx":912
  *             else:
  *                 break
  *         return line             # <<<<<<<<<<<<<<
@@ -19688,12 +19847,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_36_parse_header(CYTHON_UNUSED PyObje
  *     def _parse(self, line, stream):
  */
   __Pyx_XDECREF(__pyx_r);
-  if (unlikely(!__pyx_v_line)) { __Pyx_RaiseUnboundLocalError("line"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 904; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
+  if (unlikely(!__pyx_v_line)) { __Pyx_RaiseUnboundLocalError("line"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
   __Pyx_INCREF(__pyx_v_line);
   __pyx_r = __pyx_v_line;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":892
+  /* "pysam/cvcf.pyx":900
  *         stream.write( "\t".join(output) + "\n" )
  * 
  *     def _parse_header(self, stream):             # <<<<<<<<<<<<<<
@@ -19706,7 +19865,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_36_parse_header(CYTHON_UNUSED PyObje
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_4);
   __Pyx_XDECREF(__pyx_t_5);
-  __Pyx_XDECREF(__pyx_t_7);
+  __Pyx_XDECREF(__pyx_t_8);
   __Pyx_AddTraceback("pysam.cvcf.VCF._parse_header", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
@@ -19717,7 +19876,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_36_parse_header(CYTHON_UNUSED PyObje
 }
 static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */
 
-/* "pysam/cvcf.pyx":906
+/* "pysam/cvcf.pyx":914
  *         return line
  * 
  *     def _parse(self, line, stream):             # <<<<<<<<<<<<<<
@@ -19759,16 +19918,16 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_39_parse(PyObject *__pyx_self, PyObj
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_line)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("_parse", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("_parse", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_stream)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("_parse", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("_parse", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_parse") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "_parse") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
       goto __pyx_L5_argtuple_error;
@@ -19783,7 +19942,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_39_parse(PyObject *__pyx_self, PyObj
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("_parse", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("_parse", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF._parse", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -19820,7 +19979,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_38_parse(CYTHON_UNUSED PyObject *__p
   __Pyx_INCREF(__pyx_cur_scope->__pyx_v_stream);
   __Pyx_GIVEREF(__pyx_cur_scope->__pyx_v_stream);
   {
-    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_4cvcf_3VCF_40generator, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5pysam_4cvcf_3VCF_40generator, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_cur_scope);
     __Pyx_RefNannyFinishContext();
     return (PyObject *) gen;
@@ -19866,21 +20025,21 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
     return NULL;
   }
   __pyx_L3_first_run:;
-  if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":908
+  /* "pysam/cvcf.pyx":916
  *     def _parse(self, line, stream):
  *         # deal with files with header only
  *         if line.startswith("##"): return             # <<<<<<<<<<<<<<
  *         if len(line.strip()) > 0:
  *             d = self.parse_data( line.strip() )
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_line, __pyx_n_s_startswith); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 916; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__84, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__84, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 916; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 916; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   if (__pyx_t_3) {
     __Pyx_XDECREF(__pyx_r);
@@ -19888,43 +20047,43 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":909
+  /* "pysam/cvcf.pyx":917
  *         # deal with files with header only
  *         if line.startswith("##"): return
  *         if len(line.strip()) > 0:             # <<<<<<<<<<<<<<
  *             d = self.parse_data( line.strip() )
  *             if d: yield d
  */
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 909; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 917; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 909; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 917; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_4 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 909; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 917; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __pyx_t_3 = ((__pyx_t_4 > 0) != 0);
   if (__pyx_t_3) {
 
-    /* "pysam/cvcf.pyx":910
+    /* "pysam/cvcf.pyx":918
  *         if line.startswith("##"): return
  *         if len(line.strip()) > 0:
  *             d = self.parse_data( line.strip() )             # <<<<<<<<<<<<<<
  *             if d: yield d
  *         for line in stream:
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_parse_data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_parse_data); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_5);
     __Pyx_GIVEREF(__pyx_t_5);
     __pyx_t_5 = 0;
-    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -19932,14 +20091,14 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
     __pyx_cur_scope->__pyx_v_d = __pyx_t_5;
     __pyx_t_5 = 0;
 
-    /* "pysam/cvcf.pyx":911
+    /* "pysam/cvcf.pyx":919
  *         if len(line.strip()) > 0:
  *             d = self.parse_data( line.strip() )
  *             if d: yield d             # <<<<<<<<<<<<<<
  *         for line in stream:
  *             self._lineno += 1
  */
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_cur_scope->__pyx_v_d); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_cur_scope->__pyx_v_d); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 919; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_3) {
       __Pyx_INCREF(__pyx_cur_scope->__pyx_v_d);
       __pyx_r = __pyx_cur_scope->__pyx_v_d;
@@ -19949,7 +20108,7 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
       __pyx_generator->resume_label = 1;
       return __pyx_r;
       __pyx_L7_resume_from_yield:;
-      if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 919; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       goto __pyx_L6;
     }
     __pyx_L6:;
@@ -19957,7 +20116,7 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
   }
   __pyx_L5:;
 
-  /* "pysam/cvcf.pyx":912
+  /* "pysam/cvcf.pyx":920
  *             d = self.parse_data( line.strip() )
  *             if d: yield d
  *         for line in stream:             # <<<<<<<<<<<<<<
@@ -19968,7 +20127,7 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
     __pyx_t_5 = __pyx_cur_scope->__pyx_v_stream; __Pyx_INCREF(__pyx_t_5); __pyx_t_4 = 0;
     __pyx_t_6 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_cur_scope->__pyx_v_stream); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_cur_scope->__pyx_v_stream); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_5);
     __pyx_t_6 = Py_TYPE(__pyx_t_5)->tp_iternext;
   }
@@ -19976,16 +20135,16 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
     if (!__pyx_t_6 && PyList_CheckExact(__pyx_t_5)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_5)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_5, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_5, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_6 && PyTuple_CheckExact(__pyx_t_5)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_5)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_5, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_5, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_2 = __pyx_t_6(__pyx_t_5);
@@ -19993,7 +20152,7 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 912; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 920; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -20004,41 +20163,41 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
     __Pyx_GIVEREF(__pyx_t_2);
     __pyx_t_2 = 0;
 
-    /* "pysam/cvcf.pyx":913
+    /* "pysam/cvcf.pyx":921
  *             if d: yield d
  *         for line in stream:
  *             self._lineno += 1             # <<<<<<<<<<<<<<
  *             if self._lines and self._lineno > self._lines: raise StopIteration
  *             d = self.parse_data( line.strip() )
  */
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lineno); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 913; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lineno); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = PyNumber_InPlaceAdd(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 913; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyNumber_InPlaceAdd(__pyx_t_2, __pyx_int_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    if (__Pyx_PyObject_SetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lineno, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 913; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    if (__Pyx_PyObject_SetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lineno, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":914
+    /* "pysam/cvcf.pyx":922
  *         for line in stream:
  *             self._lineno += 1
  *             if self._lines and self._lineno > self._lines: raise StopIteration             # <<<<<<<<<<<<<<
  *             d = self.parse_data( line.strip() )
  *             if d: yield d
  */
-    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lines_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lines_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     if (__pyx_t_3) {
-      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lineno); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lineno); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_1);
-      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lines_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_lines_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_GOTREF(__pyx_t_2);
-      __pyx_t_7 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_GT); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_7 = PyObject_RichCompare(__pyx_t_1, __pyx_t_2, Py_GT); __Pyx_XGOTREF(__pyx_t_7); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
       __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-      __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_7); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
       __pyx_t_9 = __pyx_t_8;
     } else {
@@ -20046,29 +20205,29 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
     }
     if (__pyx_t_9) {
       __Pyx_Raise(__pyx_builtin_StopIteration, 0, 0, 0);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     }
 
-    /* "pysam/cvcf.pyx":915
+    /* "pysam/cvcf.pyx":923
  *             self._lineno += 1
  *             if self._lines and self._lineno > self._lines: raise StopIteration
  *             d = self.parse_data( line.strip() )             # <<<<<<<<<<<<<<
  *             if d: yield d
  * 
  */
-    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_parse_data); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_self, __pyx_n_s_parse_data); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_7);
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_cur_scope->__pyx_v_line, __pyx_n_s_strip); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
     PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_7, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -20077,14 +20236,14 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
     __Pyx_GIVEREF(__pyx_t_1);
     __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":916
+    /* "pysam/cvcf.pyx":924
  *             if self._lines and self._lineno > self._lines: raise StopIteration
  *             d = self.parse_data( line.strip() )
  *             if d: yield d             # <<<<<<<<<<<<<<
  * 
  *     ######################################################################################################
  */
-    __pyx_t_9 = __Pyx_PyObject_IsTrue(__pyx_cur_scope->__pyx_v_d); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 916; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_9 = __Pyx_PyObject_IsTrue(__pyx_cur_scope->__pyx_v_d); if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     if (__pyx_t_9) {
       __Pyx_INCREF(__pyx_cur_scope->__pyx_v_d);
       __pyx_r = __pyx_cur_scope->__pyx_v_d;
@@ -20103,14 +20262,14 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
       __pyx_cur_scope->__pyx_t_1 = 0;
       __Pyx_XGOTREF(__pyx_t_5);
       __pyx_t_6 = __pyx_cur_scope->__pyx_t_2;
-      if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 916; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       goto __pyx_L11;
     }
     __pyx_L11:;
   }
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":906
+  /* "pysam/cvcf.pyx":914
  *         return line
  * 
  *     def _parse(self, line, stream):             # <<<<<<<<<<<<<<
@@ -20135,7 +20294,7 @@ static PyObject *__pyx_gb_5pysam_4cvcf_3VCF_40generator(__pyx_GeneratorObject *_
   return NULL;
 }
 
-/* "pysam/cvcf.pyx":924
+/* "pysam/cvcf.pyx":932
  *     ######################################################################################################
  * 
  *     def getsamples(self):             # <<<<<<<<<<<<<<
@@ -20167,7 +20326,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_41getsamples(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("getsamples", 0);
 
-  /* "pysam/cvcf.pyx":926
+  /* "pysam/cvcf.pyx":934
  *     def getsamples(self):
  *         """ List of samples in VCF file """
  *         return self._samples             # <<<<<<<<<<<<<<
@@ -20175,13 +20334,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_41getsamples(CYTHON_UNUSED PyObject
  *     def setsamples(self,samples):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 926; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_samples); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 934; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":924
+  /* "pysam/cvcf.pyx":932
  *     ######################################################################################################
  * 
  *     def getsamples(self):             # <<<<<<<<<<<<<<
@@ -20200,7 +20359,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_41getsamples(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":928
+/* "pysam/cvcf.pyx":936
  *         return self._samples
  * 
  *     def setsamples(self,samples):             # <<<<<<<<<<<<<<
@@ -20241,11 +20400,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_44setsamples(PyObject *__pyx_self, P
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_samples_2)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("setsamples", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 928; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("setsamples", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setsamples") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 928; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setsamples") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -20258,7 +20417,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_44setsamples(PyObject *__pyx_self, P
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setsamples", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 928; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("setsamples", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.setsamples", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -20279,16 +20438,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_43setsamples(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("setsamples", 0);
 
-  /* "pysam/cvcf.pyx":930
+  /* "pysam/cvcf.pyx":938
  *     def setsamples(self,samples):
  *         """ List of samples in VCF file """
  *         self._samples = samples             # <<<<<<<<<<<<<<
  * 
  *     def getheader(self):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_samples, __pyx_v_samples) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_samples, __pyx_v_samples) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 938; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":928
+  /* "pysam/cvcf.pyx":936
  *         return self._samples
  * 
  *     def setsamples(self,samples):             # <<<<<<<<<<<<<<
@@ -20308,7 +20467,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_43setsamples(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":932
+/* "pysam/cvcf.pyx":940
  *         self._samples = samples
  * 
  *     def getheader(self):             # <<<<<<<<<<<<<<
@@ -20340,7 +20499,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_45getheader(CYTHON_UNUSED PyObject *
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("getheader", 0);
 
-  /* "pysam/cvcf.pyx":934
+  /* "pysam/cvcf.pyx":942
  *     def getheader(self):
  *         """ List of header key-value pairs (strings) """
  *         return self._header             # <<<<<<<<<<<<<<
@@ -20348,13 +20507,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_45getheader(CYTHON_UNUSED PyObject *
  *     def setheader(self,header):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_header); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 934; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_header); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":932
+  /* "pysam/cvcf.pyx":940
  *         self._samples = samples
  * 
  *     def getheader(self):             # <<<<<<<<<<<<<<
@@ -20373,7 +20532,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_45getheader(CYTHON_UNUSED PyObject *
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":936
+/* "pysam/cvcf.pyx":944
  *         return self._header
  * 
  *     def setheader(self,header):             # <<<<<<<<<<<<<<
@@ -20414,11 +20573,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_48setheader(PyObject *__pyx_self, Py
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_header_2)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("setheader", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("setheader", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setheader") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setheader") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -20431,7 +20590,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_48setheader(PyObject *__pyx_self, Py
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setheader", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("setheader", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.setheader", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -20452,16 +20611,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_47setheader(CYTHON_UNUSED PyObject *
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("setheader", 0);
 
-  /* "pysam/cvcf.pyx":938
+  /* "pysam/cvcf.pyx":946
  *     def setheader(self,header):
  *         """ List of header key-value pairs (strings) """
  *         self._header = header             # <<<<<<<<<<<<<<
  * 
  *     def getinfo(self):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_header, __pyx_v_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 938; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_header, __pyx_v_header) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":936
+  /* "pysam/cvcf.pyx":944
  *         return self._header
  * 
  *     def setheader(self,header):             # <<<<<<<<<<<<<<
@@ -20481,7 +20640,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_47setheader(CYTHON_UNUSED PyObject *
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":940
+/* "pysam/cvcf.pyx":948
  *         self._header = header
  * 
  *     def getinfo(self):             # <<<<<<<<<<<<<<
@@ -20513,7 +20672,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_49getinfo(CYTHON_UNUSED PyObject *__
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("getinfo", 0);
 
-  /* "pysam/cvcf.pyx":942
+  /* "pysam/cvcf.pyx":950
  *     def getinfo(self):
  *         """ Dictionary of ##INFO tags, as VCF.FORMAT values """
  *         return self._info             # <<<<<<<<<<<<<<
@@ -20521,13 +20680,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_49getinfo(CYTHON_UNUSED PyObject *__
  *     def setinfo(self,info):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_info); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":940
+  /* "pysam/cvcf.pyx":948
  *         self._header = header
  * 
  *     def getinfo(self):             # <<<<<<<<<<<<<<
@@ -20546,7 +20705,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_49getinfo(CYTHON_UNUSED PyObject *__
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":944
+/* "pysam/cvcf.pyx":952
  *         return self._info
  * 
  *     def setinfo(self,info):             # <<<<<<<<<<<<<<
@@ -20587,11 +20746,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_52setinfo(PyObject *__pyx_self, PyOb
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_info_2)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("setinfo", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("setinfo", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setinfo") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setinfo") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -20604,7 +20763,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_52setinfo(PyObject *__pyx_self, PyOb
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setinfo", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("setinfo", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.setinfo", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -20625,16 +20784,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_51setinfo(CYTHON_UNUSED PyObject *__
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("setinfo", 0);
 
-  /* "pysam/cvcf.pyx":946
+  /* "pysam/cvcf.pyx":954
  *     def setinfo(self,info):
  *         """ Dictionary of ##INFO tags, as VCF.FORMAT values """
  *         self._info = info             # <<<<<<<<<<<<<<
  * 
  *     def getformat(self):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_info, __pyx_v_info) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_info, __pyx_v_info) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 954; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":944
+  /* "pysam/cvcf.pyx":952
  *         return self._info
  * 
  *     def setinfo(self,info):             # <<<<<<<<<<<<<<
@@ -20654,7 +20813,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_51setinfo(CYTHON_UNUSED PyObject *__
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":948
+/* "pysam/cvcf.pyx":956
  *         self._info = info
  * 
  *     def getformat(self):             # <<<<<<<<<<<<<<
@@ -20686,7 +20845,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_53getformat(CYTHON_UNUSED PyObject *
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("getformat", 0);
 
-  /* "pysam/cvcf.pyx":950
+  /* "pysam/cvcf.pyx":958
  *     def getformat(self):
  *         """ Dictionary of ##FORMAT tags, as VCF.FORMAT values """
  *         return self._format             # <<<<<<<<<<<<<<
@@ -20694,13 +20853,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_53getformat(CYTHON_UNUSED PyObject *
  *     def setformat(self,format):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 950; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_format_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":948
+  /* "pysam/cvcf.pyx":956
  *         self._info = info
  * 
  *     def getformat(self):             # <<<<<<<<<<<<<<
@@ -20719,7 +20878,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_53getformat(CYTHON_UNUSED PyObject *
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":952
+/* "pysam/cvcf.pyx":960
  *         return self._format
  * 
  *     def setformat(self,format):             # <<<<<<<<<<<<<<
@@ -20760,11 +20919,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_56setformat(PyObject *__pyx_self, Py
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_format)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("setformat", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("setformat", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setformat") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setformat") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -20777,7 +20936,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_56setformat(PyObject *__pyx_self, Py
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setformat", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("setformat", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.setformat", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -20798,16 +20957,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_55setformat(CYTHON_UNUSED PyObject *
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("setformat", 0);
 
-  /* "pysam/cvcf.pyx":954
+  /* "pysam/cvcf.pyx":962
  *     def setformat(self,format):
  *         """ Dictionary of ##FORMAT tags, as VCF.FORMAT values """
  *         self._format = format             # <<<<<<<<<<<<<<
  * 
  *     def getfilter(self):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_format_2, __pyx_v_format) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 954; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_format_2, __pyx_v_format) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 962; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":952
+  /* "pysam/cvcf.pyx":960
  *         return self._format
  * 
  *     def setformat(self,format):             # <<<<<<<<<<<<<<
@@ -20827,7 +20986,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_55setformat(CYTHON_UNUSED PyObject *
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":956
+/* "pysam/cvcf.pyx":964
  *         self._format = format
  * 
  *     def getfilter(self):             # <<<<<<<<<<<<<<
@@ -20859,7 +21018,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_57getfilter(CYTHON_UNUSED PyObject *
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("getfilter", 0);
 
-  /* "pysam/cvcf.pyx":958
+  /* "pysam/cvcf.pyx":966
  *     def getfilter(self):
  *         """ Dictionary of ##FILTER tags, as VCF.FORMAT values """
  *         return self._filter             # <<<<<<<<<<<<<<
@@ -20867,13 +21026,13 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_57getfilter(CYTHON_UNUSED PyObject *
  *     def setfilter(self,filter):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_filter); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 958; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_filter); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 966; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":956
+  /* "pysam/cvcf.pyx":964
  *         self._format = format
  * 
  *     def getfilter(self):             # <<<<<<<<<<<<<<
@@ -20892,7 +21051,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_57getfilter(CYTHON_UNUSED PyObject *
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":960
+/* "pysam/cvcf.pyx":968
  *         return self._filter
  * 
  *     def setfilter(self,filter):             # <<<<<<<<<<<<<<
@@ -20933,11 +21092,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_60setfilter(PyObject *__pyx_self, Py
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_filter_2)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("setfilter", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("setfilter", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setfilter") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setfilter") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -20950,7 +21109,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_60setfilter(PyObject *__pyx_self, Py
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setfilter", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("setfilter", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.setfilter", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -20971,16 +21130,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_59setfilter(CYTHON_UNUSED PyObject *
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("setfilter", 0);
 
-  /* "pysam/cvcf.pyx":962
+  /* "pysam/cvcf.pyx":970
  *     def setfilter(self,filter):
  *         """ Dictionary of ##FILTER tags, as VCF.FORMAT values """
  *         self._filter = filter             # <<<<<<<<<<<<<<
  * 
  *     def setversion(self, version):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_filter, __pyx_v_filter) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 962; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_filter, __pyx_v_filter) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 970; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":960
+  /* "pysam/cvcf.pyx":968
  *         return self._filter
  * 
  *     def setfilter(self,filter):             # <<<<<<<<<<<<<<
@@ -21000,7 +21159,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_59setfilter(CYTHON_UNUSED PyObject *
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":964
+/* "pysam/cvcf.pyx":972
  *         self._filter = filter
  * 
  *     def setversion(self, version):             # <<<<<<<<<<<<<<
@@ -21040,11 +21199,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_62setversion(PyObject *__pyx_self, P
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_version_2)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("setversion", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("setversion", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 972; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setversion") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setversion") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 972; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -21057,7 +21216,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_62setversion(PyObject *__pyx_self, P
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setversion", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("setversion", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 972; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.setversion", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -21082,42 +21241,42 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_61setversion(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("setversion", 0);
 
-  /* "pysam/cvcf.pyx":965
+  /* "pysam/cvcf.pyx":973
  * 
  *     def setversion(self, version):
  *         if version != 33 and version != 40: raise ValueError("Can only handle v3.3 and v4.0 VCF files")             # <<<<<<<<<<<<<<
  *         self._version = version
  * 
  */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_version, __pyx_int_33, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_v_version, __pyx_int_33, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_2) {
-    __pyx_t_1 = PyObject_RichCompare(__pyx_v_version, __pyx_int_40, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = PyObject_RichCompare(__pyx_v_version, __pyx_int_40, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
     __pyx_t_4 = __pyx_t_3;
   } else {
     __pyx_t_4 = __pyx_t_2;
   }
   if (__pyx_t_4) {
-    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__85, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__85, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_Raise(__pyx_t_1, 0, 0, 0);
     __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   }
 
-  /* "pysam/cvcf.pyx":966
+  /* "pysam/cvcf.pyx":974
  *     def setversion(self, version):
  *         if version != 33 and version != 40: raise ValueError("Can only handle v3.3 and v4.0 VCF files")
  *         self._version = version             # <<<<<<<<<<<<<<
  * 
  *     def setregions(self, regions):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_v_version) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 966; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_version, __pyx_v_version) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 974; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":964
+  /* "pysam/cvcf.pyx":972
  *         self._filter = filter
  * 
  *     def setversion(self, version):             # <<<<<<<<<<<<<<
@@ -21138,7 +21297,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_61setversion(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":968
+/* "pysam/cvcf.pyx":976
  *         self._version = version
  * 
  *     def setregions(self, regions):             # <<<<<<<<<<<<<<
@@ -21178,11 +21337,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_64setregions(PyObject *__pyx_self, P
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_regions)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("setregions", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("setregions", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setregions") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setregions") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -21195,7 +21354,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_64setregions(PyObject *__pyx_self, P
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setregions", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("setregions", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.setregions", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -21216,16 +21375,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_63setregions(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("setregions", 0);
 
-  /* "pysam/cvcf.pyx":969
+  /* "pysam/cvcf.pyx":977
  * 
  *     def setregions(self, regions):
  *         self._regions = regions             # <<<<<<<<<<<<<<
  * 
  *     def setreference(self, ref):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_regions_2, __pyx_v_regions) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 969; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_regions_2, __pyx_v_regions) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":968
+  /* "pysam/cvcf.pyx":976
  *         self._version = version
  * 
  *     def setregions(self, regions):             # <<<<<<<<<<<<<<
@@ -21245,7 +21404,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_63setregions(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":971
+/* "pysam/cvcf.pyx":979
  *         self._regions = regions
  * 
  *     def setreference(self, ref):             # <<<<<<<<<<<<<<
@@ -21286,11 +21445,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_66setreference(PyObject *__pyx_self,
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_ref)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("setreference", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("setreference", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setreference") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "setreference") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -21303,7 +21462,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_66setreference(PyObject *__pyx_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("setreference", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("setreference", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.setreference", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -21324,16 +21483,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_65setreference(CYTHON_UNUSED PyObjec
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("setreference", 0);
 
-  /* "pysam/cvcf.pyx":973
+  /* "pysam/cvcf.pyx":981
  *     def setreference(self, ref):
  *         """ Provide a reference sequence; a Python class supporting a fetch(chromosome, start, end) method, e.g. PySam.FastaFile """
  *         self._reference = ref             # <<<<<<<<<<<<<<
  * 
  *     def ignoreerror(self, errorstring):
  */
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_reference_2, __pyx_v_ref) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_reference_2, __pyx_v_ref) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":971
+  /* "pysam/cvcf.pyx":979
  *         self._regions = regions
  * 
  *     def setreference(self, ref):             # <<<<<<<<<<<<<<
@@ -21353,7 +21512,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_65setreference(CYTHON_UNUSED PyObjec
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":975
+/* "pysam/cvcf.pyx":983
  *         self._reference = ref
  * 
  *     def ignoreerror(self, errorstring):             # <<<<<<<<<<<<<<
@@ -21393,11 +21552,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_68ignoreerror(PyObject *__pyx_self,
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_errorstring)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("ignoreerror", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("ignoreerror", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "ignoreerror") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "ignoreerror") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -21410,7 +21569,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_68ignoreerror(PyObject *__pyx_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("ignoreerror", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("ignoreerror", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.ignoreerror", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -21440,7 +21599,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_67ignoreerror(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("ignoreerror", 0);
 
-  /* "pysam/cvcf.pyx":976
+  /* "pysam/cvcf.pyx":984
  * 
  *     def ignoreerror(self, errorstring):
  *         try:             self._ignored_errors.add(self.__dict__[errorstring])             # <<<<<<<<<<<<<<
@@ -21453,22 +21612,22 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_67ignoreerror(CYTHON_UNUSED PyObject
     __Pyx_XGOTREF(__pyx_t_2);
     __Pyx_XGOTREF(__pyx_t_3);
     /*try:*/ {
-      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ignored_errors); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_ignored_errors); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_add); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_add); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_5);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_dict); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_dict); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_6 = PyObject_GetItem(__pyx_t_4, __pyx_v_errorstring); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L3_error;};
+      __pyx_t_6 = PyObject_GetItem(__pyx_t_4, __pyx_v_errorstring); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L3_error;};
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_4);
       PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_6);
       __Pyx_GIVEREF(__pyx_t_6);
       __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_4, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_4, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 984; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
@@ -21483,7 +21642,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_67ignoreerror(CYTHON_UNUSED PyObject
     __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-    /* "pysam/cvcf.pyx":977
+    /* "pysam/cvcf.pyx":985
  *     def ignoreerror(self, errorstring):
  *         try:             self._ignored_errors.add(self.__dict__[errorstring])
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)             # <<<<<<<<<<<<<<
@@ -21493,23 +21652,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_67ignoreerror(CYTHON_UNUSED PyObject
     __pyx_t_7 = PyErr_ExceptionMatches(__pyx_builtin_KeyError);
     if (__pyx_t_7) {
       __Pyx_AddTraceback("pysam.cvcf.VCF.ignoreerror", __pyx_clineno, __pyx_lineno, __pyx_filename);
-      if (__Pyx_GetException(&__pyx_t_6, &__pyx_t_4, &__pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      if (__Pyx_GetException(&__pyx_t_6, &__pyx_t_4, &__pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_GOTREF(__pyx_t_4);
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_Invalid_error_string_s, __pyx_v_errorstring); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_Invalid_error_string_s, __pyx_v_errorstring); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_GOTREF(__pyx_t_9);
       PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
       __Pyx_GIVEREF(__pyx_t_8);
       __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       __Pyx_Raise(__pyx_t_8, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
@@ -21530,7 +21689,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_67ignoreerror(CYTHON_UNUSED PyObject
     __pyx_L10_try_end:;
   }
 
-  /* "pysam/cvcf.pyx":975
+  /* "pysam/cvcf.pyx":983
  *         self._reference = ref
  * 
  *     def ignoreerror(self, errorstring):             # <<<<<<<<<<<<<<
@@ -21555,7 +21714,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_67ignoreerror(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":979
+/* "pysam/cvcf.pyx":987
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  * 
  *     def warnerror(self, errorstring):             # <<<<<<<<<<<<<<
@@ -21595,11 +21754,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_70warnerror(PyObject *__pyx_self, Py
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_errorstring)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("warnerror", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("warnerror", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 987; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "warnerror") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "warnerror") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 987; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -21612,7 +21771,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_70warnerror(PyObject *__pyx_self, Py
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("warnerror", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("warnerror", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 987; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.warnerror", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -21642,7 +21801,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_69warnerror(CYTHON_UNUSED PyObject *
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("warnerror", 0);
 
-  /* "pysam/cvcf.pyx":980
+  /* "pysam/cvcf.pyx":988
  * 
  *     def warnerror(self, errorstring):
  *         try:             self._warn_errors.add(self.__dict__[errorstring])             # <<<<<<<<<<<<<<
@@ -21655,22 +21814,22 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_69warnerror(CYTHON_UNUSED PyObject *
     __Pyx_XGOTREF(__pyx_t_2);
     __Pyx_XGOTREF(__pyx_t_3);
     /*try:*/ {
-      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_warn_errors); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_warn_errors); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_add); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_add); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_5);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_dict); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_dict); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_4);
-      __pyx_t_6 = PyObject_GetItem(__pyx_t_4, __pyx_v_errorstring); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L3_error;};
+      __pyx_t_6 = PyObject_GetItem(__pyx_t_4, __pyx_v_errorstring); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L3_error;};
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_4);
       PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_6);
       __Pyx_GIVEREF(__pyx_t_6);
       __pyx_t_6 = 0;
-      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_4, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 980; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_4, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
@@ -21685,7 +21844,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_69warnerror(CYTHON_UNUSED PyObject *
     __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
     __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0;
 
-    /* "pysam/cvcf.pyx":981
+    /* "pysam/cvcf.pyx":989
  *     def warnerror(self, errorstring):
  *         try:             self._warn_errors.add(self.__dict__[errorstring])
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)             # <<<<<<<<<<<<<<
@@ -21695,23 +21854,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_69warnerror(CYTHON_UNUSED PyObject *
     __pyx_t_7 = PyErr_ExceptionMatches(__pyx_builtin_KeyError);
     if (__pyx_t_7) {
       __Pyx_AddTraceback("pysam.cvcf.VCF.warnerror", __pyx_clineno, __pyx_lineno, __pyx_filename);
-      if (__Pyx_GetException(&__pyx_t_6, &__pyx_t_4, &__pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      if (__Pyx_GetException(&__pyx_t_6, &__pyx_t_4, &__pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 989; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_GOTREF(__pyx_t_6);
       __Pyx_GOTREF(__pyx_t_4);
       __Pyx_GOTREF(__pyx_t_5);
-      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_Invalid_error_string_s, __pyx_v_errorstring); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      __pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_Invalid_error_string_s, __pyx_v_errorstring); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 989; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_GOTREF(__pyx_t_8);
-      __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 989; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_GOTREF(__pyx_t_9);
       PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
       __Pyx_GIVEREF(__pyx_t_8);
       __pyx_t_8 = 0;
-      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 989; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_GOTREF(__pyx_t_8);
       __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
       __Pyx_Raise(__pyx_t_8, 0, 0, 0);
       __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 989; __pyx_clineno = __LINE__; goto __pyx_L5_except_error;}
       __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
       __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
       __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
@@ -21732,7 +21891,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_69warnerror(CYTHON_UNUSED PyObject *
     __pyx_L10_try_end:;
   }
 
-  /* "pysam/cvcf.pyx":979
+  /* "pysam/cvcf.pyx":987
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  * 
  *     def warnerror(self, errorstring):             # <<<<<<<<<<<<<<
@@ -21757,7 +21916,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_69warnerror(CYTHON_UNUSED PyObject *
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":983
+/* "pysam/cvcf.pyx":991
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  * 
  *     def parse(self, stream):             # <<<<<<<<<<<<<<
@@ -21798,11 +21957,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_72parse(PyObject *__pyx_self, PyObje
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_stream)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("parse", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("parse", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "parse") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -21815,7 +21974,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_72parse(PyObject *__pyx_self, PyObje
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("parse", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("parse", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.parse", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -21840,28 +21999,28 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_71parse(CYTHON_UNUSED PyObject *__py
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("parse", 0);
 
-  /* "pysam/cvcf.pyx":985
+  /* "pysam/cvcf.pyx":993
  *     def parse(self, stream):
  *         """ Parse a stream of VCF-formatted lines.  Initializes class instance and return generator """
  *         last_line = self._parse_header(stream)             # <<<<<<<<<<<<<<
  *         # now return a generator that does the actual work.  In this way the pre-processing is done
  *         # before the first piece of data is yielded
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_header_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_header_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_stream);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_stream);
   __Pyx_GIVEREF(__pyx_v_stream);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __pyx_v_last_line = __pyx_t_3;
   __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":988
+  /* "pysam/cvcf.pyx":996
  *         # now return a generator that does the actual work.  In this way the pre-processing is done
  *         # before the first piece of data is yielded
  *         return self._parse(last_line, stream)             # <<<<<<<<<<<<<<
@@ -21869,9 +22028,9 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_71parse(CYTHON_UNUSED PyObject *__py
  *     def write(self, stream, datagenerator):
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_last_line);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_last_line);
@@ -21879,7 +22038,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_71parse(CYTHON_UNUSED PyObject *__py
   __Pyx_INCREF(__pyx_v_stream);
   PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_stream);
   __Pyx_GIVEREF(__pyx_v_stream);
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
@@ -21887,7 +22046,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_71parse(CYTHON_UNUSED PyObject *__py
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":983
+  /* "pysam/cvcf.pyx":991
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  * 
  *     def parse(self, stream):             # <<<<<<<<<<<<<<
@@ -21909,7 +22068,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_71parse(CYTHON_UNUSED PyObject *__py
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":990
+/* "pysam/cvcf.pyx":998
  *         return self._parse(last_line, stream)
  * 
  *     def write(self, stream, datagenerator):             # <<<<<<<<<<<<<<
@@ -21952,16 +22111,16 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_74write(PyObject *__pyx_self, PyObje
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_stream)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("write", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("write", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_datagenerator)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("write", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("write", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "write") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "write") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
       goto __pyx_L5_argtuple_error;
@@ -21976,7 +22135,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_74write(PyObject *__pyx_self, PyObje
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("write", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("write", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.write", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -22004,47 +22163,47 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_73write(CYTHON_UNUSED PyObject *__py
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("write", 0);
 
-  /* "pysam/cvcf.pyx":992
+  /* "pysam/cvcf.pyx":1000
  *     def write(self, stream, datagenerator):
  *         """ Writes a VCF file to a stream, using a data generator (or list) """
  *         self.write_header(stream)             # <<<<<<<<<<<<<<
  *         self.write_heading(stream)
  *         for data in datagenerator: self.write_data(stream,data)
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_header); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 992; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_header); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 992; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_stream);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_stream);
   __Pyx_GIVEREF(__pyx_v_stream);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 992; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":993
+  /* "pysam/cvcf.pyx":1001
  *         """ Writes a VCF file to a stream, using a data generator (or list) """
  *         self.write_header(stream)
  *         self.write_heading(stream)             # <<<<<<<<<<<<<<
  *         for data in datagenerator: self.write_data(stream,data)
  * 
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_heading); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_heading); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_stream);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_stream);
   __Pyx_GIVEREF(__pyx_v_stream);
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 993; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":994
+  /* "pysam/cvcf.pyx":1002
  *         self.write_header(stream)
  *         self.write_heading(stream)
  *         for data in datagenerator: self.write_data(stream,data)             # <<<<<<<<<<<<<<
@@ -22055,7 +22214,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_73write(CYTHON_UNUSED PyObject *__py
     __pyx_t_1 = __pyx_v_datagenerator; __Pyx_INCREF(__pyx_t_1); __pyx_t_4 = 0;
     __pyx_t_5 = NULL;
   } else {
-    __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_datagenerator); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_4 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_datagenerator); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __pyx_t_5 = Py_TYPE(__pyx_t_1)->tp_iternext;
   }
@@ -22063,16 +22222,16 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_73write(CYTHON_UNUSED PyObject *__py
     if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_1)) {
       if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_1)) {
       if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
       #if CYTHON_COMPILING_IN_CPYTHON
-      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_4); __Pyx_INCREF(__pyx_t_2); __pyx_t_4++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #else
-      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       #endif
     } else {
       __pyx_t_2 = __pyx_t_5(__pyx_t_1);
@@ -22080,7 +22239,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_73write(CYTHON_UNUSED PyObject *__py
         PyObject* exc_type = PyErr_Occurred();
         if (exc_type) {
           if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
-          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         }
         break;
       }
@@ -22088,9 +22247,9 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_73write(CYTHON_UNUSED PyObject *__py
     }
     __Pyx_XDECREF_SET(__pyx_v_data, __pyx_t_2);
     __pyx_t_2 = 0;
-    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_data); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_data); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_2);
-    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_3);
     __Pyx_INCREF(__pyx_v_stream);
     PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_stream);
@@ -22098,7 +22257,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_73write(CYTHON_UNUSED PyObject *__py
     __Pyx_INCREF(__pyx_v_data);
     PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_v_data);
     __Pyx_GIVEREF(__pyx_v_data);
-    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 994; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
     __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
@@ -22106,7 +22265,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_73write(CYTHON_UNUSED PyObject *__py
   }
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":990
+  /* "pysam/cvcf.pyx":998
  *         return self._parse(last_line, stream)
  * 
  *     def write(self, stream, datagenerator):             # <<<<<<<<<<<<<<
@@ -22131,7 +22290,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_73write(CYTHON_UNUSED PyObject *__py
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":996
+/* "pysam/cvcf.pyx":1004
  *         for data in datagenerator: self.write_data(stream,data)
  * 
  *     def writeheader(self, stream):             # <<<<<<<<<<<<<<
@@ -22172,11 +22331,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_76writeheader(PyObject *__pyx_self,
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_stream)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("writeheader", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("writeheader", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1004; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "writeheader") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "writeheader") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1004; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -22189,7 +22348,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_76writeheader(PyObject *__pyx_self,
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("writeheader", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("writeheader", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1004; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.writeheader", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -22213,47 +22372,47 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_75writeheader(CYTHON_UNUSED PyObject
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("writeheader", 0);
 
-  /* "pysam/cvcf.pyx":998
+  /* "pysam/cvcf.pyx":1006
  *     def writeheader(self, stream):
  *         """ Writes a VCF header """
  *         self.write_header(stream)             # <<<<<<<<<<<<<<
  *         self.write_heading(stream)
  * 
  */
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_header); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_header); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_stream);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_stream);
   __Pyx_GIVEREF(__pyx_v_stream);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_2, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":999
+  /* "pysam/cvcf.pyx":1007
  *         """ Writes a VCF header """
  *         self.write_header(stream)
  *         self.write_heading(stream)             # <<<<<<<<<<<<<<
  * 
  *     def compare_calls(self, pos1, ref1, alt1, pos2, ref2, alt2):
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_heading); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 999; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_write_heading); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 999; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_v_stream);
   PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_stream);
   __Pyx_GIVEREF(__pyx_v_stream);
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 999; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":996
+  /* "pysam/cvcf.pyx":1004
  *         for data in datagenerator: self.write_data(stream,data)
  * 
  *     def writeheader(self, stream):             # <<<<<<<<<<<<<<
@@ -22276,7 +22435,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_75writeheader(CYTHON_UNUSED PyObject
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":1001
+/* "pysam/cvcf.pyx":1009
  *         self.write_heading(stream)
  * 
  *     def compare_calls(self, pos1, ref1, alt1, pos2, ref2, alt2):             # <<<<<<<<<<<<<<
@@ -22327,36 +22486,36 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_78compare_calls(PyObject *__pyx_self
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_pos1)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  2:
         if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_ref1)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  3:
         if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_alt1)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  4:
         if (likely((values[4] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_pos2)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 4); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 4); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  5:
         if (likely((values[5] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_ref2)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 5); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 5); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
         case  6:
         if (likely((values[6] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_alt2)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 6); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, 6); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "compare_calls") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "compare_calls") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 7) {
       goto __pyx_L5_argtuple_error;
@@ -22379,7 +22538,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_78compare_calls(PyObject *__pyx_self
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("compare_calls", 1, 7, 7, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.compare_calls", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -22412,15 +22571,15 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_77compare_calls(CYTHON_UNUSED PyObje
   __Pyx_INCREF(__pyx_v_ref2);
   __Pyx_INCREF(__pyx_v_alt2);
 
-  /* "pysam/cvcf.pyx":1006
+  /* "pysam/cvcf.pyx":1014
  *         # the leftmost position of the alignment gap.  If this rule is implemented
  *         # correctly, the two positions must be equal for the calls to be identical.
  *         if pos1 != pos2: return False             # <<<<<<<<<<<<<<
  *         # from both calls, trim rightmost bases when identical.  Do this safely, i.e.
  *         # only when the reference bases are not Ns
  */
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_pos1, __pyx_v_pos2, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1006; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_v_pos1, __pyx_v_pos2, Py_NE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   if (__pyx_t_2) {
     __Pyx_XDECREF(__pyx_r);
@@ -22429,7 +22588,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_77compare_calls(CYTHON_UNUSED PyObje
     goto __pyx_L0;
   }
 
-  /* "pysam/cvcf.pyx":1009
+  /* "pysam/cvcf.pyx":1017
  *         # from both calls, trim rightmost bases when identical.  Do this safely, i.e.
  *         # only when the reference bases are not Ns
  *         while len(ref1)>0 and len(alt1)>0 and ref1[-1] == alt1[-1]:             # <<<<<<<<<<<<<<
@@ -22437,20 +22596,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_77compare_calls(CYTHON_UNUSED PyObje
  *             alt1 = alt1[:-1]
  */
   while (1) {
-    __pyx_t_3 = PyObject_Length(__pyx_v_ref1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyObject_Length(__pyx_v_ref1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_2 = (__pyx_t_3 > 0);
     if (__pyx_t_2) {
-      __pyx_t_3 = PyObject_Length(__pyx_v_alt1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyObject_Length(__pyx_v_alt1); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_4 = (__pyx_t_3 > 0);
       if (__pyx_t_4) {
-        __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_ref1, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_ref1, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_1);
-        __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_alt1, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_alt1, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_5);
-        __pyx_t_6 = PyObject_RichCompare(__pyx_t_1, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_6 = PyObject_RichCompare(__pyx_t_1, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_6); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-        __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1017; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __pyx_t_8 = __pyx_t_7;
       } else {
@@ -22462,32 +22621,32 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_77compare_calls(CYTHON_UNUSED PyObje
     }
     if (!__pyx_t_4) break;
 
-    /* "pysam/cvcf.pyx":1010
+    /* "pysam/cvcf.pyx":1018
  *         # only when the reference bases are not Ns
  *         while len(ref1)>0 and len(alt1)>0 and ref1[-1] == alt1[-1]:
  *             ref1 = ref1[:-1]             # <<<<<<<<<<<<<<
  *             alt1 = alt1[:-1]
  *         while len(ref2)>0 and len(alt2)>0 and ref2[-1] == alt2[-1]:
  */
-    __pyx_t_6 = __Pyx_PyObject_GetSlice(__pyx_v_ref1, 0, -1, NULL, NULL, &__pyx_slice__86, 0, 1, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1010; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetSlice(__pyx_v_ref1, 0, -1, NULL, NULL, &__pyx_slice__86, 0, 1, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF_SET(__pyx_v_ref1, __pyx_t_6);
     __pyx_t_6 = 0;
 
-    /* "pysam/cvcf.pyx":1011
+    /* "pysam/cvcf.pyx":1019
  *         while len(ref1)>0 and len(alt1)>0 and ref1[-1] == alt1[-1]:
  *             ref1 = ref1[:-1]
  *             alt1 = alt1[:-1]             # <<<<<<<<<<<<<<
  *         while len(ref2)>0 and len(alt2)>0 and ref2[-1] == alt2[-1]:
  *             ref2 = ref2[:-1]
  */
-    __pyx_t_6 = __Pyx_PyObject_GetSlice(__pyx_v_alt1, 0, -1, NULL, NULL, &__pyx_slice__87, 0, 1, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_6 = __Pyx_PyObject_GetSlice(__pyx_v_alt1, 0, -1, NULL, NULL, &__pyx_slice__87, 0, 1, 1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_6);
     __Pyx_DECREF_SET(__pyx_v_alt1, __pyx_t_6);
     __pyx_t_6 = 0;
   }
 
-  /* "pysam/cvcf.pyx":1012
+  /* "pysam/cvcf.pyx":1020
  *             ref1 = ref1[:-1]
  *             alt1 = alt1[:-1]
  *         while len(ref2)>0 and len(alt2)>0 and ref2[-1] == alt2[-1]:             # <<<<<<<<<<<<<<
@@ -22495,20 +22654,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_77compare_calls(CYTHON_UNUSED PyObje
  *             alt2 = alt2[:-1]
  */
   while (1) {
-    __pyx_t_3 = PyObject_Length(__pyx_v_ref2); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_3 = PyObject_Length(__pyx_v_ref2); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __pyx_t_4 = (__pyx_t_3 > 0);
     if (__pyx_t_4) {
-      __pyx_t_3 = PyObject_Length(__pyx_v_alt2); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+      __pyx_t_3 = PyObject_Length(__pyx_v_alt2); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
       __pyx_t_2 = (__pyx_t_3 > 0);
       if (__pyx_t_2) {
-        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_ref2, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_ref2, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_6 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_6);
-        __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_alt2, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
+        __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_alt2, -1, long, 1, __Pyx_PyInt_From_long, 0, 1, 1); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
         __Pyx_GOTREF(__pyx_t_5);
-        __pyx_t_1 = PyObject_RichCompare(__pyx_t_6, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_1 = PyObject_RichCompare(__pyx_t_6, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
         __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
-        __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1012; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+        __pyx_t_8 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
         __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
         __pyx_t_7 = __pyx_t_8;
       } else {
@@ -22520,32 +22679,32 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_77compare_calls(CYTHON_UNUSED PyObje
     }
     if (!__pyx_t_2) break;
 
-    /* "pysam/cvcf.pyx":1013
+    /* "pysam/cvcf.pyx":1021
  *             alt1 = alt1[:-1]
  *         while len(ref2)>0 and len(alt2)>0 and ref2[-1] == alt2[-1]:
  *             ref2 = ref2[:-1]             # <<<<<<<<<<<<<<
  *             alt2 = alt2[:-1]
  *         # now, the alternative alleles must be identical
  */
-    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_ref2, 0, -1, NULL, NULL, &__pyx_slice__88, 0, 1, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_ref2, 0, -1, NULL, NULL, &__pyx_slice__88, 0, 1, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1021; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF_SET(__pyx_v_ref2, __pyx_t_1);
     __pyx_t_1 = 0;
 
-    /* "pysam/cvcf.pyx":1014
+    /* "pysam/cvcf.pyx":1022
  *         while len(ref2)>0 and len(alt2)>0 and ref2[-1] == alt2[-1]:
  *             ref2 = ref2[:-1]
  *             alt2 = alt2[:-1]             # <<<<<<<<<<<<<<
  *         # now, the alternative alleles must be identical
  *         return alt1 == alt2
  */
-    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_alt2, 0, -1, NULL, NULL, &__pyx_slice__89, 0, 1, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_alt2, 0, -1, NULL, NULL, &__pyx_slice__89, 0, 1, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
     __Pyx_GOTREF(__pyx_t_1);
     __Pyx_DECREF_SET(__pyx_v_alt2, __pyx_t_1);
     __pyx_t_1 = 0;
   }
 
-  /* "pysam/cvcf.pyx":1016
+  /* "pysam/cvcf.pyx":1024
  *             alt2 = alt2[:-1]
  *         # now, the alternative alleles must be identical
  *         return alt1 == alt2             # <<<<<<<<<<<<<<
@@ -22553,12 +22712,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_77compare_calls(CYTHON_UNUSED PyObje
  * ###########################################################################################################
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = PyObject_RichCompare(__pyx_v_alt1, __pyx_v_alt2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1016; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyObject_RichCompare(__pyx_v_alt1, __pyx_v_alt2, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1024; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_r = __pyx_t_1;
   __pyx_t_1 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":1001
+  /* "pysam/cvcf.pyx":1009
  *         self.write_heading(stream)
  * 
  *     def compare_calls(self, pos1, ref1, alt1, pos2, ref2, alt2):             # <<<<<<<<<<<<<<
@@ -22583,12 +22742,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_77compare_calls(CYTHON_UNUSED PyObje
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":1023
+/* "pysam/cvcf.pyx":1031
  * ###########################################################################################################
  * 
- *     def connect(self, filename):             # <<<<<<<<<<<<<<
+ *     def connect(self, filename, encoding="ascii"):             # <<<<<<<<<<<<<<
  *         '''connect to tabix file.'''
- *         self.tabixfile = pysam.Tabixfile( filename )
+ *         self.encoding=encoding
  */
 
 /* Python wrapper */
@@ -22598,6 +22757,7 @@ static PyMethodDef __pyx_mdef_5pysam_4cvcf_3VCF_80connect = {__Pyx_NAMESTR("conn
 static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_80connect(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_self = 0;
   PyObject *__pyx_v_filename = 0;
+  PyObject *__pyx_v_encoding = 0;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
@@ -22605,12 +22765,14 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_80connect(PyObject *__pyx_self, PyOb
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("connect (wrapper)", 0);
   {
-    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_self,&__pyx_n_s_filename,0};
-    PyObject* values[2] = {0,0};
+    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_self,&__pyx_n_s_filename,&__pyx_n_s_encoding,0};
+    PyObject* values[3] = {0,0,0};
+    values[2] = ((PyObject *)((PyObject*)__pyx_n_s_ascii));
     if (unlikely(__pyx_kwds)) {
       Py_ssize_t kw_args;
       const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
       switch (pos_args) {
+        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
         case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
         case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
         case  0: break;
@@ -22624,102 +22786,125 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_80connect(PyObject *__pyx_self, PyOb
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_filename)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("connect", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1023; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("connect", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        }
+        case  2:
+        if (kw_args > 0) {
+          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_encoding);
+          if (value) { values[2] = value; kw_args--; }
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "connect") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1023; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "connect") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
-    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
-      goto __pyx_L5_argtuple_error;
     } else {
-      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
-      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+      switch (PyTuple_GET_SIZE(__pyx_args)) {
+        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
+        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+        break;
+        default: goto __pyx_L5_argtuple_error;
+      }
     }
     __pyx_v_self = values[0];
     __pyx_v_filename = values[1];
+    __pyx_v_encoding = values[2];
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("connect", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1023; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("connect", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.connect", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
   return NULL;
   __pyx_L4_argument_unpacking_done:;
-  __pyx_r = __pyx_pf_5pysam_4cvcf_3VCF_79connect(__pyx_self, __pyx_v_self, __pyx_v_filename);
+  __pyx_r = __pyx_pf_5pysam_4cvcf_3VCF_79connect(__pyx_self, __pyx_v_self, __pyx_v_filename, __pyx_v_encoding);
 
   /* function exit code */
   __Pyx_RefNannyFinishContext();
   return __pyx_r;
 }
 
-static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_79connect(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self, PyObject *__pyx_v_filename) {
+static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_79connect(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_self, PyObject *__pyx_v_filename, PyObject *__pyx_v_encoding) {
   PyObject *__pyx_r = NULL;
   __Pyx_RefNannyDeclarations
   PyObject *__pyx_t_1 = NULL;
   PyObject *__pyx_t_2 = NULL;
   PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
   int __pyx_lineno = 0;
   const char *__pyx_filename = NULL;
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("connect", 0);
 
-  /* "pysam/cvcf.pyx":1025
- *     def connect(self, filename):
+  /* "pysam/cvcf.pyx":1033
+ *     def connect(self, filename, encoding="ascii"):
+ *         '''connect to tabix file.'''
+ *         self.encoding=encoding             # <<<<<<<<<<<<<<
+ *         self.tabixfile = pysam.Tabixfile(filename, encoding=encoding)
+ *         self._parse_header(self.tabixfile.header)
+ */
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_encoding, __pyx_v_encoding) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1033; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+
+  /* "pysam/cvcf.pyx":1034
  *         '''connect to tabix file.'''
- *         self.tabixfile = pysam.Tabixfile( filename )             # <<<<<<<<<<<<<<
+ *         self.encoding=encoding
+ *         self.tabixfile = pysam.Tabixfile(filename, encoding=encoding)             # <<<<<<<<<<<<<<
  *         self._parse_header(self.tabixfile.header)
  * 
  */
-  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_pysam); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_pysam); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_Tabixfile); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_Tabixfile); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_v_filename);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_filename);
   __Pyx_GIVEREF(__pyx_v_filename);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
+  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_encoding, __pyx_v_encoding) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_tabixfile, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1025; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  if (__Pyx_PyObject_SetAttrStr(__pyx_v_self, __pyx_n_s_tabixfile, __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1034; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
 
-  /* "pysam/cvcf.pyx":1026
- *         '''connect to tabix file.'''
- *         self.tabixfile = pysam.Tabixfile( filename )
+  /* "pysam/cvcf.pyx":1035
+ *         self.encoding=encoding
+ *         self.tabixfile = pysam.Tabixfile(filename, encoding=encoding)
  *         self._parse_header(self.tabixfile.header)             # <<<<<<<<<<<<<<
  * 
  *     def fetch(self,
  */
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_header_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_parse_header_2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_4);
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_tabixfile); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_tabixfile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_header_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_header_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
-  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_3);
+  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
+  __Pyx_GIVEREF(__pyx_t_1);
+  __pyx_t_1 = 0;
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_2);
-  __Pyx_GIVEREF(__pyx_t_2);
-  __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1026; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":1023
+  /* "pysam/cvcf.pyx":1031
  * ###########################################################################################################
  * 
- *     def connect(self, filename):             # <<<<<<<<<<<<<<
+ *     def connect(self, filename, encoding="ascii"):             # <<<<<<<<<<<<<<
  *         '''connect to tabix file.'''
- *         self.tabixfile = pysam.Tabixfile( filename )
+ *         self.encoding=encoding
  */
 
   /* function exit code */
@@ -22729,6 +22914,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_79connect(CYTHON_UNUSED PyObject *__
   __Pyx_XDECREF(__pyx_t_1);
   __Pyx_XDECREF(__pyx_t_2);
   __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
   __Pyx_AddTraceback("pysam.cvcf.VCF.connect", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __pyx_r = NULL;
   __pyx_L0:;
@@ -22737,12 +22923,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_79connect(CYTHON_UNUSED PyObject *__
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":1028
+/* "pysam/cvcf.pyx":1037
  *         self._parse_header(self.tabixfile.header)
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
- *               reference = None,
- *               start = None,
+ *               reference=None,
+ *               start=None,
  */
 
 /* Python wrapper */
@@ -22765,37 +22951,37 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_82fetch(PyObject *__pyx_self, PyObje
     static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_self,&__pyx_n_s_reference,&__pyx_n_s_start,&__pyx_n_s_end,&__pyx_n_s_region,0};
     PyObject* values[5] = {0,0,0,0,0};
 
-    /* "pysam/cvcf.pyx":1029
+    /* "pysam/cvcf.pyx":1038
  * 
  *     def fetch(self,
- *               reference = None,             # <<<<<<<<<<<<<<
- *               start = None,
- *               end = None,
+ *               reference=None,             # <<<<<<<<<<<<<<
+ *               start=None,
+ *               end=None,
  */
     values[1] = ((PyObject *)((PyObject *)Py_None));
 
-    /* "pysam/cvcf.pyx":1030
+    /* "pysam/cvcf.pyx":1039
  *     def fetch(self,
- *               reference = None,
- *               start = None,             # <<<<<<<<<<<<<<
- *               end = None,
- *               region = None ):
+ *               reference=None,
+ *               start=None,             # <<<<<<<<<<<<<<
+ *               end=None,
+ *               region=None ):
  */
     values[2] = ((PyObject *)((PyObject *)Py_None));
 
-    /* "pysam/cvcf.pyx":1031
- *               reference = None,
- *               start = None,
- *               end = None,             # <<<<<<<<<<<<<<
- *               region = None ):
+    /* "pysam/cvcf.pyx":1040
+ *               reference=None,
+ *               start=None,
+ *               end=None,             # <<<<<<<<<<<<<<
+ *               region=None ):
  *         """ Parse a stream of VCF-formatted lines.
  */
     values[3] = ((PyObject *)((PyObject *)Py_None));
 
-    /* "pysam/cvcf.pyx":1032
- *               start = None,
- *               end = None,
- *               region = None ):             # <<<<<<<<<<<<<<
+    /* "pysam/cvcf.pyx":1041
+ *               start=None,
+ *               end=None,
+ *               region=None ):             # <<<<<<<<<<<<<<
  *         """ Parse a stream of VCF-formatted lines.
  *         Initializes class instance and return generator """
  */
@@ -22839,7 +23025,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_82fetch(PyObject *__pyx_self, PyObje
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "fetch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "fetch") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else {
       switch (PyTuple_GET_SIZE(__pyx_args)) {
@@ -22860,7 +23046,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_82fetch(PyObject *__pyx_self, PyObje
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("fetch", 0, 1, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("fetch", 0, 1, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.fetch", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -22868,12 +23054,12 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_82fetch(PyObject *__pyx_self, PyObje
   __pyx_L4_argument_unpacking_done:;
   __pyx_r = __pyx_pf_5pysam_4cvcf_3VCF_81fetch(__pyx_self, __pyx_v_self, __pyx_v_reference, __pyx_v_start, __pyx_v_end, __pyx_v_region);
 
-  /* "pysam/cvcf.pyx":1028
+  /* "pysam/cvcf.pyx":1037
  *         self._parse_header(self.tabixfile.header)
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
- *               reference = None,
- *               start = None,
+ *               reference=None,
+ *               start=None,
  */
 
   /* function exit code */
@@ -22894,7 +23080,7 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_81fetch(CYTHON_UNUSED PyObject *__py
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("fetch", 0);
 
-  /* "pysam/cvcf.pyx":1035
+  /* "pysam/cvcf.pyx":1044
  *         """ Parse a stream of VCF-formatted lines.
  *         Initializes class instance and return generator """
  *         return self.tabixfile.fetch(             # <<<<<<<<<<<<<<
@@ -22902,20 +23088,20 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_81fetch(CYTHON_UNUSED PyObject *__py
  *             start,
  */
   __Pyx_XDECREF(__pyx_r);
-  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_tabixfile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_self, __pyx_n_s_tabixfile); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
-  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_fetch); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_fetch); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 
-  /* "pysam/cvcf.pyx":1039
+  /* "pysam/cvcf.pyx":1048
  *             start,
  *             end,
  *             region,             # <<<<<<<<<<<<<<
  *             parser = asVCFRecord(self))
  * 
  */
-  __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = PyTuple_New(4); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_INCREF(__pyx_v_reference);
   PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_reference);
@@ -22930,42 +23116,42 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_81fetch(CYTHON_UNUSED PyObject *__py
   PyTuple_SET_ITEM(__pyx_t_1, 3, __pyx_v_region);
   __Pyx_GIVEREF(__pyx_v_region);
 
-  /* "pysam/cvcf.pyx":1035
+  /* "pysam/cvcf.pyx":1044
  *         """ Parse a stream of VCF-formatted lines.
  *         Initializes class instance and return generator """
  *         return self.tabixfile.fetch(             # <<<<<<<<<<<<<<
  *             reference,
  *             start,
  */
-  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
 
-  /* "pysam/cvcf.pyx":1040
+  /* "pysam/cvcf.pyx":1049
  *             end,
  *             region,
  *             parser = asVCFRecord(self))             # <<<<<<<<<<<<<<
  * 
- *     def validate( self, record ):
+ *     def validate(self, record):
  */
-  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1040; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1049; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
   __Pyx_INCREF(__pyx_v_self);
   PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_self);
   __Pyx_GIVEREF(__pyx_v_self);
-  __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_4cvcf_asVCFRecord)), __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1040; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5pysam_4cvcf_asVCFRecord)), __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1049; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
-  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_parser, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_3, __pyx_n_s_parser, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":1035
+  /* "pysam/cvcf.pyx":1044
  *         """ Parse a stream of VCF-formatted lines.
  *         Initializes class instance and return generator """
  *         return self.tabixfile.fetch(             # <<<<<<<<<<<<<<
  *             reference,
  *             start,
  */
-  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, __pyx_t_3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_1, __pyx_t_3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1044; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
@@ -22974,12 +23160,12 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_81fetch(CYTHON_UNUSED PyObject *__py
   __pyx_t_5 = 0;
   goto __pyx_L0;
 
-  /* "pysam/cvcf.pyx":1028
+  /* "pysam/cvcf.pyx":1037
  *         self._parse_header(self.tabixfile.header)
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
- *               reference = None,
- *               start = None,
+ *               reference=None,
+ *               start=None,
  */
 
   /* function exit code */
@@ -22997,10 +23183,10 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_81fetch(CYTHON_UNUSED PyObject *__py
   return __pyx_r;
 }
 
-/* "pysam/cvcf.pyx":1042
+/* "pysam/cvcf.pyx":1051
  *             parser = asVCFRecord(self))
  * 
- *     def validate( self, record ):             # <<<<<<<<<<<<<<
+ *     def validate(self, record):             # <<<<<<<<<<<<<<
  *         '''validate vcf record.
  * 
  */
@@ -23038,11 +23224,11 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_84validate(PyObject *__pyx_self, PyO
         case  1:
         if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_record)) != 0)) kw_args--;
         else {
-          __Pyx_RaiseArgtupleInvalid("validate", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+          __Pyx_RaiseArgtupleInvalid("validate", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1051; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
         }
       }
       if (unlikely(kw_args > 0)) {
-        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "validate") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "validate") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1051; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
       }
     } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
       goto __pyx_L5_argtuple_error;
@@ -23055,7 +23241,7 @@ static PyObject *__pyx_pw_5pysam_4cvcf_3VCF_84validate(PyObject *__pyx_self, PyO
   }
   goto __pyx_L4_argument_unpacking_done;
   __pyx_L5_argtuple_error:;
-  __Pyx_RaiseArgtupleInvalid("validate", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __Pyx_RaiseArgtupleInvalid("validate", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1051; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
   __pyx_L3_error:;
   __Pyx_AddTraceback("pysam.cvcf.VCF.validate", __pyx_clineno, __pyx_lineno, __pyx_filename);
   __Pyx_RefNannyFinishContext();
@@ -23077,23 +23263,23 @@ static PyObject *__pyx_pf_5pysam_4cvcf_3VCF_83validate(CYTHON_UNUSED PyObject *_
   int __pyx_clineno = 0;
   __Pyx_RefNannySetupContext("validate", 0);
 
-  /* "pysam/cvcf.pyx":1048
+  /* "pysam/cvcf.pyx":1057
  *         '''
  * 
- *         raise NotImplementedError( "needs to be checked" )             # <<<<<<<<<<<<<<
+ *         raise NotImplementedError("needs to be checked")             # <<<<<<<<<<<<<<
  * 
  *         chrom, pos = record.chrom, record.pos
  */
-  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__90, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_NotImplementedError, __pyx_tuple__90, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_1);
   __Pyx_Raise(__pyx_t_1, 0, 0, 0);
   __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
-  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":1042
+  /* "pysam/cvcf.pyx":1051
  *             parser = asVCFRecord(self))
  * 
- *     def validate( self, record ):             # <<<<<<<<<<<<<<
+ *     def validate(self, record):             # <<<<<<<<<<<<<<
  *         '''validate vcf record.
  * 
  */
@@ -23131,9 +23317,6 @@ static void __pyx_tp_dealloc_5pysam_4cvcf_VCFRecord(PyObject *o) {
   #endif
   PyObject_GC_UnTrack(o);
   Py_CLEAR(p->vcf);
-  #if CYTHON_COMPILING_IN_CPYTHON
-  if (PyType_IS_GC(Py_TYPE(o)->tp_base))
-  #endif
   PyObject_GC_Track(o);
   if (likely(__pyx_ptype_5pysam_10TabProxies_TupleProxy)) __pyx_ptype_5pysam_10TabProxies_TupleProxy->tp_dealloc(o); else __Pyx_call_next_tp_dealloc(o, __pyx_tp_dealloc_5pysam_4cvcf_VCFRecord);
 }
@@ -23322,9 +23505,6 @@ static void __pyx_tp_dealloc_5pysam_4cvcf_asVCFRecord(PyObject *o) {
   #endif
   PyObject_GC_UnTrack(o);
   Py_CLEAR(p->vcffile);
-  #if CYTHON_COMPILING_IN_CPYTHON
-  if (PyType_IS_GC(Py_TYPE(o)->tp_base))
-  #endif
   PyObject_GC_Track(o);
   if (likely(__pyx_ptype_5pysam_6ctabix_Parser)) __pyx_ptype_5pysam_6ctabix_Parser->tp_dealloc(o); else __Pyx_call_next_tp_dealloc(o, __pyx_tp_dealloc_5pysam_4cvcf_asVCFRecord);
 }
@@ -23792,6 +23972,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_alt2, __pyx_k_alt2, sizeof(__pyx_k_alt2), 0, 0, 1, 1},
   {&__pyx_n_s_append, __pyx_k_append, sizeof(__pyx_k_append), 0, 0, 1, 1},
   {&__pyx_n_s_args, __pyx_k_args, sizeof(__pyx_k_args), 0, 0, 1, 1},
+  {&__pyx_n_s_ascii, __pyx_k_ascii, sizeof(__pyx_k_ascii), 0, 0, 1, 1},
   {&__pyx_n_s_bisect, __pyx_k_bisect, sizeof(__pyx_k_bisect), 0, 0, 1, 1},
   {&__pyx_n_s_blurp, __pyx_k_blurp, sizeof(__pyx_k_blurp), 0, 0, 1, 1},
   {&__pyx_n_s_c, __pyx_k_c, sizeof(__pyx_k_c), 0, 0, 1, 1},
@@ -23818,6 +23999,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {&__pyx_n_s_dict_2, __pyx_k_dict_2, sizeof(__pyx_k_dict_2), 0, 0, 1, 1},
   {&__pyx_n_s_doc, __pyx_k_doc, sizeof(__pyx_k_doc), 0, 0, 1, 1},
   {&__pyx_n_s_elts, __pyx_k_elts, sizeof(__pyx_k_elts), 0, 0, 1, 1},
+  {&__pyx_n_s_encoding, __pyx_k_encoding, sizeof(__pyx_k_encoding), 0, 0, 1, 1},
   {&__pyx_n_s_end, __pyx_k_end, sizeof(__pyx_k_end), 0, 0, 1, 1},
   {&__pyx_n_s_endswith, __pyx_k_endswith, sizeof(__pyx_k_endswith), 0, 0, 1, 1},
   {&__pyx_n_s_enter_default_format, __pyx_k_enter_default_format, sizeof(__pyx_k_enter_default_format), 0, 0, 1, 1},
@@ -24018,16 +24200,16 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
   {0, 0, 0, 0, 0, 0, 0}
 };
 static int __Pyx_InitCachedBuiltins(void) {
-  __pyx_builtin_object = __Pyx_GetBuiltinName(__pyx_n_s_object); if (!__pyx_builtin_object) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 212; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_enumerate = __Pyx_GetBuiltinName(__pyx_n_s_enumerate); if (!__pyx_builtin_enumerate) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_map = __Pyx_GetBuiltinName(__pyx_n_s_map); if (!__pyx_builtin_map) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_min = __Pyx_GetBuiltinName(__pyx_n_s_min); if (!__pyx_builtin_min) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_zip = __Pyx_GetBuiltinName(__pyx_n_s_zip); if (!__pyx_builtin_zip) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_KeyError = __Pyx_GetBuiltinName(__pyx_n_s_KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_builtin_NotImplementedError = __Pyx_GetBuiltinName(__pyx_n_s_NotImplementedError); if (!__pyx_builtin_NotImplementedError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_object = __Pyx_GetBuiltinName(__pyx_n_s_object); if (!__pyx_builtin_object) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 79; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_enumerate = __Pyx_GetBuiltinName(__pyx_n_s_enumerate); if (!__pyx_builtin_enumerate) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 481; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_map = __Pyx_GetBuiltinName(__pyx_n_s_map); if (!__pyx_builtin_map) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 488; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_min = __Pyx_GetBuiltinName(__pyx_n_s_min); if (!__pyx_builtin_min) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_zip = __Pyx_GetBuiltinName(__pyx_n_s_zip); if (!__pyx_builtin_zip) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_StopIteration = __Pyx_GetBuiltinName(__pyx_n_s_StopIteration); if (!__pyx_builtin_StopIteration) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_KeyError = __Pyx_GetBuiltinName(__pyx_n_s_KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 985; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_builtin_NotImplementedError = __Pyx_GetBuiltinName(__pyx_n_s_NotImplementedError); if (!__pyx_builtin_NotImplementedError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   return 0;
   __pyx_L1_error:;
   return -1;
@@ -24037,1324 +24219,1327 @@ static int __Pyx_InitCachedConstants(void) {
   __Pyx_RefNannyDeclarations
   __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0);
 
-  /* "pysam/cvcf.pyx":71
+  /* "pysam/cvcf.pyx":72
  * def parse_regions( string ):
  *     result = []
  *     for r in string.split(','):             # <<<<<<<<<<<<<<
  *         elts = r.split(':')
  *         chrom, start, end = elts[0], 0, 3000000000
  */
-  __pyx_tuple__3 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__3 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__3);
   __Pyx_GIVEREF(__pyx_tuple__3);
 
-  /* "pysam/cvcf.pyx":72
+  /* "pysam/cvcf.pyx":73
  *     result = []
  *     for r in string.split(','):
  *         elts = r.split(':')             # <<<<<<<<<<<<<<
  *         chrom, start, end = elts[0], 0, 3000000000
  *         if len(elts)==1: pass
  */
-  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__5);
   __Pyx_GIVEREF(__pyx_tuple__5);
 
-  /* "pysam/cvcf.pyx":77
+  /* "pysam/cvcf.pyx":78
  *         elif len(elts)==2:
  *             if len(elts[1])>0:
  *                 ielts = elts[1].split('-')             # <<<<<<<<<<<<<<
  *                 if len(ielts) != 2: ValueError("Don't understand region string '%s'" % r)
  *                 try:    start, end = int(ielts[0])-1, int(ielts[1])
  */
-  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_s__6); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_s__6); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__7);
   __Pyx_GIVEREF(__pyx_tuple__7);
 
-  /* "pysam/cvcf.pyx":159
+  /* "pysam/cvcf.pyx":160
  *             alt = self.fields[4]
  *             if alt == ".": alt = []
  *             else: alt = alt.upper().split(',')             # <<<<<<<<<<<<<<
  *             return alt
  * 
  */
-  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 159; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 160; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__9);
   __Pyx_GIVEREF(__pyx_tuple__9);
 
-  /* "pysam/cvcf.pyx":176
+  /* "pysam/cvcf.pyx":177
  *             # postpone checking that filters exist.  Encode missing filter or no filtering as empty list
  *             if f == b"." or f == b"PASS" or f == b"0": return []
  *             else: return f.split(';')             # <<<<<<<<<<<<<<
  * 
  *     property info:
  */
-  __pyx_tuple__11 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__11 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__11);
   __Pyx_GIVEREF(__pyx_tuple__11);
 
-  /* "pysam/cvcf.pyx":184
+  /* "pysam/cvcf.pyx":185
  *             info = {}
  *             if col != b".":
  *                 for blurp in col.split(';'):             # <<<<<<<<<<<<<<
  *                     elts = blurp.split('=')
  *                     if len(elts) == 1: v = None
  */
-  __pyx_tuple__12 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 184; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__12 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__12);
   __Pyx_GIVEREF(__pyx_tuple__12);
 
-  /* "pysam/cvcf.pyx":185
+  /* "pysam/cvcf.pyx":186
  *             if col != b".":
  *                 for blurp in col.split(';'):
  *                     elts = blurp.split('=')             # <<<<<<<<<<<<<<
  *                     if len(elts) == 1: v = None
  *                     elif len(elts) == 2: v = elts[1]
  */
-  __pyx_tuple__14 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 185; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__14 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__14);
   __Pyx_GIVEREF(__pyx_tuple__14);
 
-  /* "pysam/cvcf.pyx":194
+  /* "pysam/cvcf.pyx":195
  *     property format:
  *          def __get__(self):
  *              return self.fields[8].split(':')             # <<<<<<<<<<<<<<
  * 
  *     property samples:
  */
-  __pyx_tuple__15 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__15 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 195; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__15);
   __Pyx_GIVEREF(__pyx_tuple__15);
 
-  /* "pysam/cvcf.pyx":203
+  /* "pysam/cvcf.pyx":204
  * 
  *         # parse sample columns
  *         values = self.fields[self.vcf._sample2column[key]].split(':')             # <<<<<<<<<<<<<<
  *         alt = self.alt
  *         format = self.format
  */
-  __pyx_tuple__16 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 203; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__16 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 204; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__16);
   __Pyx_GIVEREF(__pyx_tuple__16);
 
-  /* "pysam/cvcf.pyx":320
- *     def __init__(self, _copy=None, reference=None, regions=None, lines=None, leftalign=False):
+  /* "pysam/cvcf.pyx":323
  *         # make error identifiers accessible by name
- *         for id in self._errors.keys(): self.__dict__[self._errors[id].split(':')[0]] = id             # <<<<<<<<<<<<<<
+ *         for id in self._errors.keys():
+ *             self.__dict__[self._errors[id].split(':')[0]] = id             # <<<<<<<<<<<<<<
  *         if _copy != None:
  *             self._leftalign = _copy._leftalign
  */
-  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 320; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__17);
   __Pyx_GIVEREF(__pyx_tuple__17);
 
-  /* "pysam/cvcf.pyx":323
+  /* "pysam/cvcf.pyx":326
  *         if _copy != None:
  *             self._leftalign = _copy._leftalign
  *             self._header = _copy._header[:]             # <<<<<<<<<<<<<<
  *             self._version = _copy._version
  *             self._info = copy.deepcopy(_copy._info)
  */
-  __pyx_slice__18 = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 323; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__18 = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__18);
   __Pyx_GIVEREF(__pyx_slice__18);
 
-  /* "pysam/cvcf.pyx":328
+  /* "pysam/cvcf.pyx":331
  *             self._filter = copy.deepcopy(_copy._filter)
  *             self._format = copy.deepcopy(_copy._format)
  *             self._samples = _copy._samples[:]             # <<<<<<<<<<<<<<
  *             self._sample2column = copy.deepcopy(_copy._sample2column)
  *             self._ignored_errors = copy.deepcopy(_copy._ignored_errors)
  */
-  __pyx_slice__19 = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__19 = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 331; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__19);
   __Pyx_GIVEREF(__pyx_slice__19);
 
-  /* "pysam/cvcf.pyx":341
+  /* "pysam/cvcf.pyx":345
  *     def error(self,line,error,opt=None):
  *         if error in self._ignored_errors: return
  *         errorlabel, errorstring = self._errors[error].split(':')             # <<<<<<<<<<<<<<
  *         if opt: errorstring = errorstring % opt
  *         errwarn = ["Error","Warning"][error in self._warn_errors]
  */
-  __pyx_tuple__20 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__20 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 345; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__20);
   __Pyx_GIVEREF(__pyx_tuple__20);
 
-  /* "pysam/cvcf.pyx":350
+  /* "pysam/cvcf.pyx":354
  *     def parse_format(self,line,format,filter=False):
  *         if self._version == 40:
  *             if not format.startswith('<'):             # <<<<<<<<<<<<<<
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format = "<"+format
  */
-  __pyx_tuple__22 = PyTuple_Pack(1, __pyx_kp_s__21); if (unlikely(!__pyx_tuple__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__22 = PyTuple_Pack(1, __pyx_kp_s__21); if (unlikely(!__pyx_tuple__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 354; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__22);
   __Pyx_GIVEREF(__pyx_tuple__22);
 
-  /* "pysam/cvcf.pyx":353
+  /* "pysam/cvcf.pyx":357
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format = "<"+format
  *             if not format.endswith('>'):             # <<<<<<<<<<<<<<
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format += ">"
  */
-  __pyx_tuple__24 = PyTuple_Pack(1, __pyx_kp_s__23); if (unlikely(!__pyx_tuple__24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__24 = PyTuple_Pack(1, __pyx_kp_s__23); if (unlikely(!__pyx_tuple__24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 357; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__24);
   __Pyx_GIVEREF(__pyx_tuple__24);
 
-  /* "pysam/cvcf.pyx":356
+  /* "pysam/cvcf.pyx":360
  *                 self.error(line,self.V40_MISSING_ANGLE_BRACKETS)
  *                 format += ">"
  *             format = format[1:-1]             # <<<<<<<<<<<<<<
  *         data = {'id':None,'number':None,'type':None,'descr':None}
  *         idx = 0
  */
-  __pyx_slice__25 = PySlice_New(__pyx_int_1, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__25 = PySlice_New(__pyx_int_1, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__25);
   __Pyx_GIVEREF(__pyx_slice__25);
 
-  /* "pysam/cvcf.pyx":360
+  /* "pysam/cvcf.pyx":364
  *         idx = 0
  *         while len(format.strip())>0:
  *             elts = format.strip().split(',')             # <<<<<<<<<<<<<<
  *             first, rest = elts[0], ','.join(elts[1:])
  *             if first.find('=') == -1 or (first.find('"')>=0 and first.find('=') > first.find('"')):
  */
-  __pyx_tuple__26 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 360; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__26 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 364; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__26);
   __Pyx_GIVEREF(__pyx_tuple__26);
 
-  /* "pysam/cvcf.pyx":361
+  /* "pysam/cvcf.pyx":365
  *         while len(format.strip())>0:
  *             elts = format.strip().split(',')
  *             first, rest = elts[0], ','.join(elts[1:])             # <<<<<<<<<<<<<<
  *             if first.find('=') == -1 or (first.find('"')>=0 and first.find('=') > first.find('"')):
  *                 if self._version == 40: self.error(line,self.V40_FORMAT_MUST_HAVE_NAMED_FIELDS)
  */
-  __pyx_slice__27 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__27)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__27 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__27)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 365; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__27);
   __Pyx_GIVEREF(__pyx_slice__27);
 
-  /* "pysam/cvcf.pyx":362
+  /* "pysam/cvcf.pyx":366
  *             elts = format.strip().split(',')
  *             first, rest = elts[0], ','.join(elts[1:])
  *             if first.find('=') == -1 or (first.find('"')>=0 and first.find('=') > first.find('"')):             # <<<<<<<<<<<<<<
  *                 if self._version == 40: self.error(line,self.V40_FORMAT_MUST_HAVE_NAMED_FIELDS)
  *                 if idx == 4: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  */
-  __pyx_tuple__28 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__28)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__28 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__28)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__28);
   __Pyx_GIVEREF(__pyx_tuple__28);
-  __pyx_tuple__30 = PyTuple_Pack(1, __pyx_kp_s__29); if (unlikely(!__pyx_tuple__30)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__30 = PyTuple_Pack(1, __pyx_kp_s__29); if (unlikely(!__pyx_tuple__30)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__30);
   __Pyx_GIVEREF(__pyx_tuple__30);
-  __pyx_tuple__31 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__31)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__31 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__31)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__31);
   __Pyx_GIVEREF(__pyx_tuple__31);
-  __pyx_tuple__32 = PyTuple_Pack(1, __pyx_kp_s__29); if (unlikely(!__pyx_tuple__32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 362; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__32 = PyTuple_Pack(1, __pyx_kp_s__29); if (unlikely(!__pyx_tuple__32)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__32);
   __Pyx_GIVEREF(__pyx_tuple__32);
 
-  /* "pysam/cvcf.pyx":366
+  /* "pysam/cvcf.pyx":370
  *                 if idx == 4: self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  *                 first = ["ID=","Number=","Type=","Description="][idx] + first
  *             if first.startswith('ID='):            data['id'] = first.split('=')[1]             # <<<<<<<<<<<<<<
  *             elif first.startswith('Number='):      data['number'] = first.split('=')[1]
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]
  */
-  __pyx_tuple__33 = PyTuple_Pack(1, __pyx_kp_s_ID); if (unlikely(!__pyx_tuple__33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__33 = PyTuple_Pack(1, __pyx_kp_s_ID); if (unlikely(!__pyx_tuple__33)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__33);
   __Pyx_GIVEREF(__pyx_tuple__33);
-  __pyx_tuple__34 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__34)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 366; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__34 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__34)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__34);
   __Pyx_GIVEREF(__pyx_tuple__34);
 
-  /* "pysam/cvcf.pyx":367
+  /* "pysam/cvcf.pyx":371
  *                 first = ["ID=","Number=","Type=","Description="][idx] + first
  *             if first.startswith('ID='):            data['id'] = first.split('=')[1]
  *             elif first.startswith('Number='):      data['number'] = first.split('=')[1]             # <<<<<<<<<<<<<<
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]
  *             elif first.startswith('Description='):
  */
-  __pyx_tuple__35 = PyTuple_Pack(1, __pyx_kp_s_Number); if (unlikely(!__pyx_tuple__35)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__35 = PyTuple_Pack(1, __pyx_kp_s_Number); if (unlikely(!__pyx_tuple__35)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__35);
   __Pyx_GIVEREF(__pyx_tuple__35);
-  __pyx_tuple__36 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__36)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 367; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__36 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__36)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 371; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__36);
   __Pyx_GIVEREF(__pyx_tuple__36);
 
-  /* "pysam/cvcf.pyx":368
+  /* "pysam/cvcf.pyx":372
  *             if first.startswith('ID='):            data['id'] = first.split('=')[1]
  *             elif first.startswith('Number='):      data['number'] = first.split('=')[1]
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]             # <<<<<<<<<<<<<<
  *             elif first.startswith('Description='):
  *                 elts = format.split('"')
  */
-  __pyx_tuple__37 = PyTuple_Pack(1, __pyx_kp_s_Type); if (unlikely(!__pyx_tuple__37)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__37 = PyTuple_Pack(1, __pyx_kp_s_Type); if (unlikely(!__pyx_tuple__37)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__37);
   __Pyx_GIVEREF(__pyx_tuple__37);
-  __pyx_tuple__38 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__38)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 368; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__38 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__38)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 372; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__38);
   __Pyx_GIVEREF(__pyx_tuple__38);
 
-  /* "pysam/cvcf.pyx":369
+  /* "pysam/cvcf.pyx":373
  *             elif first.startswith('Number='):      data['number'] = first.split('=')[1]
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]
  *             elif first.startswith('Description='):             # <<<<<<<<<<<<<<
  *                 elts = format.split('"')
  *                 if len(elts)<3:
  */
-  __pyx_tuple__39 = PyTuple_Pack(1, __pyx_kp_s_Description); if (unlikely(!__pyx_tuple__39)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 369; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__39 = PyTuple_Pack(1, __pyx_kp_s_Description); if (unlikely(!__pyx_tuple__39)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__39);
   __Pyx_GIVEREF(__pyx_tuple__39);
 
-  /* "pysam/cvcf.pyx":370
+  /* "pysam/cvcf.pyx":374
  *             elif first.startswith('Type='):        data['type'] = first.split('=')[1]
  *             elif first.startswith('Description='):
  *                 elts = format.split('"')             # <<<<<<<<<<<<<<
  *                 if len(elts)<3:
  *                     self.error(line,self.FORMAT_MISSING_QUOTES)
  */
-  __pyx_tuple__40 = PyTuple_Pack(1, __pyx_kp_s__29); if (unlikely(!__pyx_tuple__40)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 370; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__40 = PyTuple_Pack(1, __pyx_kp_s__29); if (unlikely(!__pyx_tuple__40)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 374; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__40);
   __Pyx_GIVEREF(__pyx_tuple__40);
 
-  /* "pysam/cvcf.pyx":373
+  /* "pysam/cvcf.pyx":377
  *                 if len(elts)<3:
  *                     self.error(line,self.FORMAT_MISSING_QUOTES)
  *                     elts = first.split('=') + [rest]             # <<<<<<<<<<<<<<
  *                 data['descr'] = elts[1]
  *                 rest = '"'.join(elts[2:])
  */
-  __pyx_tuple__41 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__41)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 373; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__41 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__41)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 377; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__41);
   __Pyx_GIVEREF(__pyx_tuple__41);
 
-  /* "pysam/cvcf.pyx":375
+  /* "pysam/cvcf.pyx":379
  *                     elts = first.split('=') + [rest]
  *                 data['descr'] = elts[1]
  *                 rest = '"'.join(elts[2:])             # <<<<<<<<<<<<<<
  *                 if rest.startswith(','): rest = rest[1:]
  *             else:
  */
-  __pyx_slice__42 = PySlice_New(__pyx_int_2, Py_None, Py_None); if (unlikely(!__pyx_slice__42)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 375; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__42 = PySlice_New(__pyx_int_2, Py_None, Py_None); if (unlikely(!__pyx_slice__42)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 379; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__42);
   __Pyx_GIVEREF(__pyx_slice__42);
 
-  /* "pysam/cvcf.pyx":376
+  /* "pysam/cvcf.pyx":380
  *                 data['descr'] = elts[1]
  *                 rest = '"'.join(elts[2:])
  *                 if rest.startswith(','): rest = rest[1:]             # <<<<<<<<<<<<<<
  *             else:
  *                 self.error(line,self.BADLY_FORMATTED_FORMAT_STRING)
  */
-  __pyx_tuple__43 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__43)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 376; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__43 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__43)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 380; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__43);
   __Pyx_GIVEREF(__pyx_tuple__43);
 
-  /* "pysam/cvcf.pyx":496
+  /* "pysam/cvcf.pyx":500
  *         # snip off trailing missing data
  *         while len(output) > 1:
  *             last = output[-1].replace(',','').replace('.','')             # <<<<<<<<<<<<<<
  *             if len(last)>0: break
  *             output = output[:-1]
  */
-  __pyx_tuple__44 = PyTuple_Pack(2, __pyx_kp_s__2, __pyx_kp_s_); if (unlikely(!__pyx_tuple__44)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__44 = PyTuple_Pack(2, __pyx_kp_s__2, __pyx_kp_s_); if (unlikely(!__pyx_tuple__44)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__44);
   __Pyx_GIVEREF(__pyx_tuple__44);
-  __pyx_tuple__45 = PyTuple_Pack(2, __pyx_kp_s__8, __pyx_kp_s_); if (unlikely(!__pyx_tuple__45)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__45 = PyTuple_Pack(2, __pyx_kp_s__8, __pyx_kp_s_); if (unlikely(!__pyx_tuple__45)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__45);
   __Pyx_GIVEREF(__pyx_tuple__45);
 
-  /* "pysam/cvcf.pyx":522
+  /* "pysam/cvcf.pyx":527
+ *     def parse_header(self, line):
  * 
- *     def parse_header( self, line ):
  *         assert line.startswith('##')             # <<<<<<<<<<<<<<
  *         elts = line[2:].split('=')
  *         key = elts[0].strip()
  */
-  __pyx_tuple__47 = PyTuple_Pack(1, __pyx_kp_s__46); if (unlikely(!__pyx_tuple__47)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 522; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__47 = PyTuple_Pack(1, __pyx_kp_s__46); if (unlikely(!__pyx_tuple__47)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__47);
   __Pyx_GIVEREF(__pyx_tuple__47);
 
-  /* "pysam/cvcf.pyx":523
- *     def parse_header( self, line ):
+  /* "pysam/cvcf.pyx":528
+ * 
  *         assert line.startswith('##')
  *         elts = line[2:].split('=')             # <<<<<<<<<<<<<<
  *         key = elts[0].strip()
  *         value = '='.join(elts[1:]).strip()
  */
-  __pyx_slice__48 = PySlice_New(__pyx_int_2, Py_None, Py_None); if (unlikely(!__pyx_slice__48)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__48 = PySlice_New(__pyx_int_2, Py_None, Py_None); if (unlikely(!__pyx_slice__48)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__48);
   __Pyx_GIVEREF(__pyx_slice__48);
-  __pyx_tuple__49 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__49)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 523; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__49 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__49)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__49);
   __Pyx_GIVEREF(__pyx_tuple__49);
 
-  /* "pysam/cvcf.pyx":525
+  /* "pysam/cvcf.pyx":530
  *         elts = line[2:].split('=')
  *         key = elts[0].strip()
  *         value = '='.join(elts[1:]).strip()             # <<<<<<<<<<<<<<
  *         if key == "fileformat":
  *             if value == "VCFv3.3":
  */
-  __pyx_slice__50 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__50)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__50 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__50)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 530; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__50);
   __Pyx_GIVEREF(__pyx_slice__50);
 
-  /* "pysam/cvcf.pyx":558
+  /* "pysam/cvcf.pyx":563
  * 
  *     def parse_heading( self, line ):
  *         assert line.startswith('#')             # <<<<<<<<<<<<<<
  *         assert not line.startswith('##')
  *         headings = line[1:].split('\t')
  */
-  __pyx_tuple__52 = PyTuple_Pack(1, __pyx_kp_s__51); if (unlikely(!__pyx_tuple__52)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 558; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__52 = PyTuple_Pack(1, __pyx_kp_s__51); if (unlikely(!__pyx_tuple__52)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__52);
   __Pyx_GIVEREF(__pyx_tuple__52);
 
-  /* "pysam/cvcf.pyx":559
+  /* "pysam/cvcf.pyx":564
  *     def parse_heading( self, line ):
  *         assert line.startswith('#')
  *         assert not line.startswith('##')             # <<<<<<<<<<<<<<
  *         headings = line[1:].split('\t')
  *         # test for 8, as FORMAT field might be missing
  */
-  __pyx_tuple__53 = PyTuple_Pack(1, __pyx_kp_s__46); if (unlikely(!__pyx_tuple__53)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 559; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__53 = PyTuple_Pack(1, __pyx_kp_s__46); if (unlikely(!__pyx_tuple__53)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__53);
   __Pyx_GIVEREF(__pyx_tuple__53);
 
-  /* "pysam/cvcf.pyx":560
+  /* "pysam/cvcf.pyx":565
  *         assert line.startswith('#')
  *         assert not line.startswith('##')
  *         headings = line[1:].split('\t')             # <<<<<<<<<<<<<<
  *         # test for 8, as FORMAT field might be missing
  *         if len(headings)==1 and len(line[1:].split()) >= 8:
  */
-  __pyx_slice__54 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__54)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__54 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__54)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__54);
   __Pyx_GIVEREF(__pyx_slice__54);
-  __pyx_tuple__56 = PyTuple_Pack(1, __pyx_kp_s__55); if (unlikely(!__pyx_tuple__56)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__56 = PyTuple_Pack(1, __pyx_kp_s__55); if (unlikely(!__pyx_tuple__56)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__56);
   __Pyx_GIVEREF(__pyx_tuple__56);
 
-  /* "pysam/cvcf.pyx":562
+  /* "pysam/cvcf.pyx":567
  *         headings = line[1:].split('\t')
  *         # test for 8, as FORMAT field might be missing
  *         if len(headings)==1 and len(line[1:].split()) >= 8:             # <<<<<<<<<<<<<<
  *             self.error(line,self.HEADING_NOT_SEPARATED_BY_TABS)
  *             headings = line[1:].split()
  */
-  __pyx_slice__57 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__57)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__57 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__57)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 567; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__57);
   __Pyx_GIVEREF(__pyx_slice__57);
 
-  /* "pysam/cvcf.pyx":564
+  /* "pysam/cvcf.pyx":569
  *         if len(headings)==1 and len(line[1:].split()) >= 8:
  *             self.error(line,self.HEADING_NOT_SEPARATED_BY_TABS)
  *             headings = line[1:].split()             # <<<<<<<<<<<<<<
  * 
  *         for i,s in enumerate(self._required):
  */
-  __pyx_slice__58 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__58)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 564; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__58 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__58)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 569; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__58);
   __Pyx_GIVEREF(__pyx_slice__58);
 
-  /* "pysam/cvcf.pyx":582
+  /* "pysam/cvcf.pyx":587
  *                     self.error(line,self.BADLY_FORMATTED_HEADING,err)
  * 
  *         self._samples = headings[9:]             # <<<<<<<<<<<<<<
  *         self._sample2column = dict( [(y,x+9) for x,y in enumerate( self._samples ) ] )
  * 
  */
-  __pyx_slice__59 = PySlice_New(__pyx_int_9, Py_None, Py_None); if (unlikely(!__pyx_slice__59)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 582; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__59 = PySlice_New(__pyx_int_9, Py_None, Py_None); if (unlikely(!__pyx_slice__59)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 587; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__59);
   __Pyx_GIVEREF(__pyx_slice__59);
 
-  /* "pysam/cvcf.pyx":612
+  /* "pysam/cvcf.pyx":617
  *             if value is not None: self.error(line,self.ERROR_FLAG_HAS_VALUE)
  *             return []
  *         values = value.split(',')             # <<<<<<<<<<<<<<
  *         # deal with trailing data in some early VCF files
  *         if f.type in ["Float","Integer"] and len(values)>0 and values[-1].find(';') > -1:
  */
-  __pyx_tuple__62 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__62)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 612; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__62 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__62)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 617; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__62);
   __Pyx_GIVEREF(__pyx_tuple__62);
 
-  /* "pysam/cvcf.pyx":614
+  /* "pysam/cvcf.pyx":619
  *         values = value.split(',')
  *         # deal with trailing data in some early VCF files
  *         if f.type in ["Float","Integer"] and len(values)>0 and values[-1].find(';') > -1:             # <<<<<<<<<<<<<<
  *             self.error(line,self.ERROR_TRAILING_DATA,values[-1])
  *             values[-1] = values[-1].split(';')[0]
  */
-  __pyx_tuple__63 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__63)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 614; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__63 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__63)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__63);
   __Pyx_GIVEREF(__pyx_tuple__63);
 
-  /* "pysam/cvcf.pyx":616
+  /* "pysam/cvcf.pyx":621
  *         if f.type in ["Float","Integer"] and len(values)>0 and values[-1].find(';') > -1:
  *             self.error(line,self.ERROR_TRAILING_DATA,values[-1])
  *             values[-1] = values[-1].split(';')[0]             # <<<<<<<<<<<<<<
  *         if f.type == "Integer":
  *             for idx,v in enumerate(values):
  */
-  __pyx_tuple__64 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__64)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 616; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__64 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__64)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 621; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__64);
   __Pyx_GIVEREF(__pyx_tuple__64);
 
-  /* "pysam/cvcf.pyx":652
+  /* "pysam/cvcf.pyx":657
  * 
  *     def parse_data( self, line, lineparse=False ):
  *         cols = line.split('\t')             # <<<<<<<<<<<<<<
  *         if len(cols) != len(self._samples)+9:
  *             # gracefully deal with absent FORMAT column
  */
-  __pyx_tuple__65 = PyTuple_Pack(1, __pyx_kp_s__55); if (unlikely(!__pyx_tuple__65)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 652; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__65 = PyTuple_Pack(1, __pyx_kp_s__55); if (unlikely(!__pyx_tuple__65)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 657; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__65);
   __Pyx_GIVEREF(__pyx_tuple__65);
 
-  /* "pysam/cvcf.pyx":698
+  /* "pysam/cvcf.pyx":703
  *         # convert v3.3 to v4.0 alleles below
  *         if cols[4] == ".": alt = []
  *         else: alt = cols[4].upper().split(',')             # <<<<<<<<<<<<<<
  * 
  *         if cols[5] == ".": qual = -1
  */
-  __pyx_tuple__66 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__66)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__66 = PyTuple_Pack(1, __pyx_kp_s__2); if (unlikely(!__pyx_tuple__66)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 703; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__66);
   __Pyx_GIVEREF(__pyx_tuple__66);
 
-  /* "pysam/cvcf.pyx":707
+  /* "pysam/cvcf.pyx":712
  *         # postpone checking that filters exist.  Encode missing filter or no filtering as empty list
  *         if cols[6] == "." or cols[6] == "PASS" or cols[6] == "0": filter = []
  *         else: filter = cols[6].split(';')             # <<<<<<<<<<<<<<
  * 
  *         # dictionary of keys, and list of values
  */
-  __pyx_tuple__67 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__67)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 707; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__67 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__67)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__67);
   __Pyx_GIVEREF(__pyx_tuple__67);
 
-  /* "pysam/cvcf.pyx":712
+  /* "pysam/cvcf.pyx":717
  *         info = {}
  *         if cols[7] != ".":
  *             for blurp in cols[7].split(';'):             # <<<<<<<<<<<<<<
  *                 elts = blurp.split('=')
  *                 if len(elts) == 1: v = None
  */
-  __pyx_tuple__68 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__68)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 712; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__68 = PyTuple_Pack(1, __pyx_kp_s__10); if (unlikely(!__pyx_tuple__68)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__68);
   __Pyx_GIVEREF(__pyx_tuple__68);
 
-  /* "pysam/cvcf.pyx":713
+  /* "pysam/cvcf.pyx":718
  *         if cols[7] != ".":
  *             for blurp in cols[7].split(';'):
  *                 elts = blurp.split('=')             # <<<<<<<<<<<<<<
  *                 if len(elts) == 1: v = None
  *                 elif len(elts) == 2: v = elts[1]
  */
-  __pyx_tuple__69 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__69)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__69 = PyTuple_Pack(1, __pyx_kp_s__13); if (unlikely(!__pyx_tuple__69)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__69);
   __Pyx_GIVEREF(__pyx_tuple__69);
 
-  /* "pysam/cvcf.pyx":724
+  /* "pysam/cvcf.pyx":729
  *         # Gracefully deal with absent FORMAT column
  *         if cols[8] == "": format = []
  *         else: format = cols[8].split(':')             # <<<<<<<<<<<<<<
  * 
  *         # check: all filters are defined
  */
-  __pyx_tuple__70 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__70)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__70 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__70)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 729; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__70);
   __Pyx_GIVEREF(__pyx_tuple__70);
 
-  /* "pysam/cvcf.pyx":741
+  /* "pysam/cvcf.pyx":746
  *             have_deletions = False
  *             for a in alt:
  *                 if len(a) == 1: a = a + ref[1:]                       # SNP; add trailing reference             # <<<<<<<<<<<<<<
  *                 elif a.startswith('I'): a = ref[0] + a[1:] + ref[1:]  # insertion just beyond pos; add first and trailing reference
  *                 elif a.startswith('D'): # allow D<seq> and D<num>
  */
-  __pyx_slice__71 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__71)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 741; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__71 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__71)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 746; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__71);
   __Pyx_GIVEREF(__pyx_slice__71);
 
-  /* "pysam/cvcf.pyx":742
+  /* "pysam/cvcf.pyx":747
  *             for a in alt:
  *                 if len(a) == 1: a = a + ref[1:]                       # SNP; add trailing reference
  *                 elif a.startswith('I'): a = ref[0] + a[1:] + ref[1:]  # insertion just beyond pos; add first and trailing reference             # <<<<<<<<<<<<<<
  *                 elif a.startswith('D'): # allow D<seq> and D<num>
  *                     have_deletions = True
  */
-  __pyx_tuple__72 = PyTuple_Pack(1, __pyx_n_s_I); if (unlikely(!__pyx_tuple__72)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__72 = PyTuple_Pack(1, __pyx_n_s_I); if (unlikely(!__pyx_tuple__72)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__72);
   __Pyx_GIVEREF(__pyx_tuple__72);
-  __pyx_slice__73 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__73)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__73 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__73)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__73);
   __Pyx_GIVEREF(__pyx_slice__73);
-  __pyx_slice__74 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__74)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__74 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__74)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__74);
   __Pyx_GIVEREF(__pyx_slice__74);
 
-  /* "pysam/cvcf.pyx":743
+  /* "pysam/cvcf.pyx":748
  *                 if len(a) == 1: a = a + ref[1:]                       # SNP; add trailing reference
  *                 elif a.startswith('I'): a = ref[0] + a[1:] + ref[1:]  # insertion just beyond pos; add first and trailing reference
  *                 elif a.startswith('D'): # allow D<seq> and D<num>             # <<<<<<<<<<<<<<
  *                     have_deletions = True
  *                     try:
  */
-  __pyx_tuple__75 = PyTuple_Pack(1, __pyx_n_s_D); if (unlikely(!__pyx_tuple__75)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__75 = PyTuple_Pack(1, __pyx_n_s_D); if (unlikely(!__pyx_tuple__75)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__75);
   __Pyx_GIVEREF(__pyx_tuple__75);
 
-  /* "pysam/cvcf.pyx":746
+  /* "pysam/cvcf.pyx":751
  *                     have_deletions = True
  *                     try:
  *                         l = int(a[1:])          # throws ValueError if sequence             # <<<<<<<<<<<<<<
  *                         if len(ref) < l:        # add to reference if necessary
  *                             addns = get_sequence(chrom,pos+len(ref),pos+l,self._reference)
  */
-  __pyx_slice__76 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__76)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 746; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__76 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__76)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 751; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__76);
   __Pyx_GIVEREF(__pyx_slice__76);
 
-  /* "pysam/cvcf.pyx":753
+  /* "pysam/cvcf.pyx":758
  *                         a = ref[l:]             # new deletion, deleting pos...pos+l
  *                     except ValueError:
  *                         s = a[1:]             # <<<<<<<<<<<<<<
  *                         if len(ref) < len(s):   # add Ns to reference if necessary
  *                             addns = get_sequence(chrom,pos+len(ref),pos+len(s),self._reference)
  */
-  __pyx_slice__77 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__77)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 753; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__77 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_slice__77)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 758; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__77);
   __Pyx_GIVEREF(__pyx_slice__77);
 
-  /* "pysam/cvcf.pyx":815
+  /* "pysam/cvcf.pyx":820
  *                 if not movable:
  *                     break
  *                 ref = ref[:-1]             # <<<<<<<<<<<<<<
  *                 alt = [allele[:-1] for allele in alt]
  *                 if min([len(allele) for allele in alt]) == 0 or len(ref) == 0:
  */
-  __pyx_slice__78 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__78)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 815; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__78 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__78)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__78);
   __Pyx_GIVEREF(__pyx_slice__78);
 
-  /* "pysam/cvcf.pyx":816
+  /* "pysam/cvcf.pyx":821
  *                     break
  *                 ref = ref[:-1]
  *                 alt = [allele[:-1] for allele in alt]             # <<<<<<<<<<<<<<
  *                 if min([len(allele) for allele in alt]) == 0 or len(ref) == 0:
  *                     ref = faref_leftflank[pos-left-1] + ref
  */
-  __pyx_slice__79 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__79)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__79 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__79)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__79);
   __Pyx_GIVEREF(__pyx_slice__79);
 
-  /* "pysam/cvcf.pyx":824
+  /* "pysam/cvcf.pyx":829
  *         # parse sample columns
  *         samples = []
  *         for sample in cols[9:]:             # <<<<<<<<<<<<<<
  *             dict = {}
  *             values = sample.split(':')
  */
-  __pyx_slice__80 = PySlice_New(__pyx_int_9, Py_None, Py_None); if (unlikely(!__pyx_slice__80)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__80 = PySlice_New(__pyx_int_9, Py_None, Py_None); if (unlikely(!__pyx_slice__80)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__80);
   __Pyx_GIVEREF(__pyx_slice__80);
 
-  /* "pysam/cvcf.pyx":826
+  /* "pysam/cvcf.pyx":831
  *         for sample in cols[9:]:
  *             dict = {}
  *             values = sample.split(':')             # <<<<<<<<<<<<<<
  *             if len(values) > len(format):
  *                 self.error(line,self.BAD_NUMBER_OF_VALUES,"(found %s values in element %s; expected %s)" % (len(values),sample,len(format)))
  */
-  __pyx_tuple__81 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__81)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__81 = PyTuple_Pack(1, __pyx_kp_s__4); if (unlikely(!__pyx_tuple__81)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__81);
   __Pyx_GIVEREF(__pyx_tuple__81);
 
-  /* "pysam/cvcf.pyx":897
- *             line = ctabix._force_str(line)
+  /* "pysam/cvcf.pyx":905
+ *             line = ctabix._force_str(line, self.encoding)
  *             self._lineno += 1
  *             if line.startswith('##'):             # <<<<<<<<<<<<<<
- *                 self.parse_header( line.strip() )
+ *                 self.parse_header(line.strip())
  *             elif line.startswith('#'):
  */
-  __pyx_tuple__82 = PyTuple_Pack(1, __pyx_kp_s__46); if (unlikely(!__pyx_tuple__82)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 897; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__82 = PyTuple_Pack(1, __pyx_kp_s__46); if (unlikely(!__pyx_tuple__82)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 905; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__82);
   __Pyx_GIVEREF(__pyx_tuple__82);
 
-  /* "pysam/cvcf.pyx":899
+  /* "pysam/cvcf.pyx":907
  *             if line.startswith('##'):
- *                 self.parse_header( line.strip() )
+ *                 self.parse_header(line.strip())
  *             elif line.startswith('#'):             # <<<<<<<<<<<<<<
- *                 self.parse_heading( line.strip() )
+ *                 self.parse_heading(line.strip())
  *                 self.enter_default_format()
  */
-  __pyx_tuple__83 = PyTuple_Pack(1, __pyx_kp_s__51); if (unlikely(!__pyx_tuple__83)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 899; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__83 = PyTuple_Pack(1, __pyx_kp_s__51); if (unlikely(!__pyx_tuple__83)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 907; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__83);
   __Pyx_GIVEREF(__pyx_tuple__83);
 
-  /* "pysam/cvcf.pyx":908
+  /* "pysam/cvcf.pyx":916
  *     def _parse(self, line, stream):
  *         # deal with files with header only
  *         if line.startswith("##"): return             # <<<<<<<<<<<<<<
  *         if len(line.strip()) > 0:
  *             d = self.parse_data( line.strip() )
  */
-  __pyx_tuple__84 = PyTuple_Pack(1, __pyx_kp_s__46); if (unlikely(!__pyx_tuple__84)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 908; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__84 = PyTuple_Pack(1, __pyx_kp_s__46); if (unlikely(!__pyx_tuple__84)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 916; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__84);
   __Pyx_GIVEREF(__pyx_tuple__84);
 
-  /* "pysam/cvcf.pyx":965
+  /* "pysam/cvcf.pyx":973
  * 
  *     def setversion(self, version):
  *         if version != 33 and version != 40: raise ValueError("Can only handle v3.3 and v4.0 VCF files")             # <<<<<<<<<<<<<<
  *         self._version = version
  * 
  */
-  __pyx_tuple__85 = PyTuple_Pack(1, __pyx_kp_s_Can_only_handle_v3_3_and_v4_0_VC); if (unlikely(!__pyx_tuple__85)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 965; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__85 = PyTuple_Pack(1, __pyx_kp_s_Can_only_handle_v3_3_and_v4_0_VC); if (unlikely(!__pyx_tuple__85)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 973; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__85);
   __Pyx_GIVEREF(__pyx_tuple__85);
 
-  /* "pysam/cvcf.pyx":1010
+  /* "pysam/cvcf.pyx":1018
  *         # only when the reference bases are not Ns
  *         while len(ref1)>0 and len(alt1)>0 and ref1[-1] == alt1[-1]:
  *             ref1 = ref1[:-1]             # <<<<<<<<<<<<<<
  *             alt1 = alt1[:-1]
  *         while len(ref2)>0 and len(alt2)>0 and ref2[-1] == alt2[-1]:
  */
-  __pyx_slice__86 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__86)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1010; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__86 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__86)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__86);
   __Pyx_GIVEREF(__pyx_slice__86);
 
-  /* "pysam/cvcf.pyx":1011
+  /* "pysam/cvcf.pyx":1019
  *         while len(ref1)>0 and len(alt1)>0 and ref1[-1] == alt1[-1]:
  *             ref1 = ref1[:-1]
  *             alt1 = alt1[:-1]             # <<<<<<<<<<<<<<
  *         while len(ref2)>0 and len(alt2)>0 and ref2[-1] == alt2[-1]:
  *             ref2 = ref2[:-1]
  */
-  __pyx_slice__87 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__87)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__87 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__87)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1019; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__87);
   __Pyx_GIVEREF(__pyx_slice__87);
 
-  /* "pysam/cvcf.pyx":1013
+  /* "pysam/cvcf.pyx":1021
  *             alt1 = alt1[:-1]
  *         while len(ref2)>0 and len(alt2)>0 and ref2[-1] == alt2[-1]:
  *             ref2 = ref2[:-1]             # <<<<<<<<<<<<<<
  *             alt2 = alt2[:-1]
  *         # now, the alternative alleles must be identical
  */
-  __pyx_slice__88 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__88)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1013; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__88 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__88)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1021; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__88);
   __Pyx_GIVEREF(__pyx_slice__88);
 
-  /* "pysam/cvcf.pyx":1014
+  /* "pysam/cvcf.pyx":1022
  *         while len(ref2)>0 and len(alt2)>0 and ref2[-1] == alt2[-1]:
  *             ref2 = ref2[:-1]
  *             alt2 = alt2[:-1]             # <<<<<<<<<<<<<<
  *         # now, the alternative alleles must be identical
  *         return alt1 == alt2
  */
-  __pyx_slice__89 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__89)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_slice__89 = PySlice_New(Py_None, __pyx_int_neg_1, Py_None); if (unlikely(!__pyx_slice__89)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1022; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_slice__89);
   __Pyx_GIVEREF(__pyx_slice__89);
 
-  /* "pysam/cvcf.pyx":1048
+  /* "pysam/cvcf.pyx":1057
  *         '''
  * 
- *         raise NotImplementedError( "needs to be checked" )             # <<<<<<<<<<<<<<
+ *         raise NotImplementedError("needs to be checked")             # <<<<<<<<<<<<<<
  * 
  *         chrom, pos = record.chrom, record.pos
  */
-  __pyx_tuple__90 = PyTuple_Pack(1, __pyx_kp_s_needs_to_be_checked); if (unlikely(!__pyx_tuple__90)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__90 = PyTuple_Pack(1, __pyx_kp_s_needs_to_be_checked); if (unlikely(!__pyx_tuple__90)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__90);
   __Pyx_GIVEREF(__pyx_tuple__90);
 
-  /* "pysam/cvcf.pyx":55
+  /* "pysam/cvcf.pyx":56
  * import pysam
  * 
  * gtsRegEx = re.compile("[|/\\\\]")             # <<<<<<<<<<<<<<
  * alleleRegEx = re.compile('^[ACGTN]+$')
  * 
  */
-  __pyx_tuple__92 = PyTuple_Pack(1, __pyx_kp_s__91); if (unlikely(!__pyx_tuple__92)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__92 = PyTuple_Pack(1, __pyx_kp_s__91); if (unlikely(!__pyx_tuple__92)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__92);
   __Pyx_GIVEREF(__pyx_tuple__92);
 
-  /* "pysam/cvcf.pyx":56
+  /* "pysam/cvcf.pyx":57
  * 
  * gtsRegEx = re.compile("[|/\\\\]")
  * alleleRegEx = re.compile('^[ACGTN]+$')             # <<<<<<<<<<<<<<
  * 
  * # Utility function.  Uses 0-based coordinates
  */
-  __pyx_tuple__93 = PyTuple_Pack(1, __pyx_kp_s_ACGTN_2); if (unlikely(!__pyx_tuple__93)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__93 = PyTuple_Pack(1, __pyx_kp_s_ACGTN_2); if (unlikely(!__pyx_tuple__93)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__93);
   __Pyx_GIVEREF(__pyx_tuple__93);
 
-  /* "pysam/cvcf.pyx":59
+  /* "pysam/cvcf.pyx":60
  * 
  * # Utility function.  Uses 0-based coordinates
  * def get_sequence(chrom, start, end, fa):             # <<<<<<<<<<<<<<
  *     # obtain sequence from .fa file, without truncation
  *     if end<=start: return ""
  */
-  __pyx_tuple__94 = PyTuple_Pack(5, __pyx_n_s_chrom, __pyx_n_s_start, __pyx_n_s_end, __pyx_n_s_fa, __pyx_n_s_sequence); if (unlikely(!__pyx_tuple__94)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__94 = PyTuple_Pack(5, __pyx_n_s_chrom, __pyx_n_s_start, __pyx_n_s_end, __pyx_n_s_fa, __pyx_n_s_sequence); if (unlikely(!__pyx_tuple__94)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__94);
   __Pyx_GIVEREF(__pyx_tuple__94);
-  __pyx_codeobj__95 = (PyObject*)__Pyx_PyCode_New(4, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__94, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_get_sequence, 59, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__95)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__95 = (PyObject*)__Pyx_PyCode_New(4, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__94, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_get_sequence, 60, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__95)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":69
+  /* "pysam/cvcf.pyx":70
  * 
  * # Utility function.  Parses a region string
  * def parse_regions( string ):             # <<<<<<<<<<<<<<
  *     result = []
  *     for r in string.split(','):
  */
-  __pyx_tuple__96 = PyTuple_Pack(8, __pyx_n_s_string, __pyx_n_s_result, __pyx_n_s_r, __pyx_n_s_elts, __pyx_n_s_chrom, __pyx_n_s_start, __pyx_n_s_end, __pyx_n_s_ielts); if (unlikely(!__pyx_tuple__96)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__96 = PyTuple_Pack(8, __pyx_n_s_string, __pyx_n_s_result, __pyx_n_s_r, __pyx_n_s_elts, __pyx_n_s_chrom, __pyx_n_s_start, __pyx_n_s_end, __pyx_n_s_ielts); if (unlikely(!__pyx_tuple__96)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 70; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__96);
   __Pyx_GIVEREF(__pyx_tuple__96);
-  __pyx_codeobj__97 = (PyObject*)__Pyx_PyCode_New(1, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__96, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_regions, 69, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__97)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__97 = (PyObject*)__Pyx_PyCode_New(1, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__96, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_regions, 70, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__97)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 70; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":87
+  /* "pysam/cvcf.pyx":88
  * 
  * 
  * FORMAT = namedtuple('FORMAT','id numbertype number type description missingvalue')             # <<<<<<<<<<<<<<
  * 
  * ###########################################################################################################
  */
-  __pyx_tuple__98 = PyTuple_Pack(2, __pyx_n_s_FORMAT, __pyx_kp_s_id_numbertype_number_type_descri); if (unlikely(!__pyx_tuple__98)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__98 = PyTuple_Pack(2, __pyx_n_s_FORMAT, __pyx_kp_s_id_numbertype_number_type_descri); if (unlikely(!__pyx_tuple__98)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__98);
   __Pyx_GIVEREF(__pyx_tuple__98);
 
-  /* "pysam/cvcf.pyx":318
+  /* "pysam/cvcf.pyx":319
  *     _lines = None
  * 
- *     def __init__(self, _copy=None, reference=None, regions=None, lines=None, leftalign=False):             # <<<<<<<<<<<<<<
+ *     def __init__(self, _copy=None, reference=None, regions=None,             # <<<<<<<<<<<<<<
+ *                  lines=None, leftalign=False):
  *         # make error identifiers accessible by name
- *         for id in self._errors.keys(): self.__dict__[self._errors[id].split(':')[0]] = id
  */
-  __pyx_tuple__99 = PyTuple_Pack(7, __pyx_n_s_self, __pyx_n_s_copy, __pyx_n_s_reference, __pyx_n_s_regions, __pyx_n_s_lines, __pyx_n_s_leftalign, __pyx_n_s_id); if (unlikely(!__pyx_tuple__99)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__99 = PyTuple_Pack(7, __pyx_n_s_self, __pyx_n_s_copy, __pyx_n_s_reference, __pyx_n_s_regions, __pyx_n_s_lines, __pyx_n_s_leftalign, __pyx_n_s_id); if (unlikely(!__pyx_tuple__99)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__99);
   __Pyx_GIVEREF(__pyx_tuple__99);
-  __pyx_codeobj__100 = (PyObject*)__Pyx_PyCode_New(6, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__99, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_init, 318, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__100)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_tuple__101 = PyTuple_Pack(5, ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_False)); if (unlikely(!__pyx_tuple__101)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__100 = (PyObject*)__Pyx_PyCode_New(6, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__99, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_init, 319, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__100)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__101 = PyTuple_Pack(5, ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_False)); if (unlikely(!__pyx_tuple__101)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__101);
   __Pyx_GIVEREF(__pyx_tuple__101);
 
-  /* "pysam/cvcf.pyx":339
- *         self._lines = lines
+  /* "pysam/cvcf.pyx":343
+ *         self.encoding = "ascii"
  * 
  *     def error(self,line,error,opt=None):             # <<<<<<<<<<<<<<
  *         if error in self._ignored_errors: return
  *         errorlabel, errorstring = self._errors[error].split(':')
  */
-  __pyx_tuple__102 = PyTuple_Pack(7, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_error, __pyx_n_s_opt, __pyx_n_s_errorlabel, __pyx_n_s_errorstring, __pyx_n_s_errwarn); if (unlikely(!__pyx_tuple__102)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__102 = PyTuple_Pack(7, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_error, __pyx_n_s_opt, __pyx_n_s_errorlabel, __pyx_n_s_errorstring, __pyx_n_s_errwarn); if (unlikely(!__pyx_tuple__102)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__102);
   __Pyx_GIVEREF(__pyx_tuple__102);
-  __pyx_codeobj__103 = (PyObject*)__Pyx_PyCode_New(4, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__102, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_error, 339, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__103)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_tuple__104 = PyTuple_Pack(1, ((PyObject *)Py_None)); if (unlikely(!__pyx_tuple__104)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__103 = (PyObject*)__Pyx_PyCode_New(4, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__102, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_error, 343, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__103)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__104 = PyTuple_Pack(1, ((PyObject *)Py_None)); if (unlikely(!__pyx_tuple__104)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__104);
   __Pyx_GIVEREF(__pyx_tuple__104);
 
-  /* "pysam/cvcf.pyx":348
+  /* "pysam/cvcf.pyx":352
  *         raise ValueError(errorstring)
  * 
  *     def parse_format(self,line,format,filter=False):             # <<<<<<<<<<<<<<
  *         if self._version == 40:
  *             if not format.startswith('<'):
  */
-  __pyx_tuple__105 = PyTuple_Pack(11, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_format, __pyx_n_s_filter_2, __pyx_n_s_data, __pyx_n_s_idx, __pyx_n_s_elts, __pyx_n_s_first, __pyx_n_s_rest, __pyx_n_s_n, __pyx_n_s_t); if (unlikely(!__pyx_tuple__105)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__105 = PyTuple_Pack(11, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_format, __pyx_n_s_filter_2, __pyx_n_s_data, __pyx_n_s_idx, __pyx_n_s_elts, __pyx_n_s_first, __pyx_n_s_rest, __pyx_n_s_n, __pyx_n_s_t); if (unlikely(!__pyx_tuple__105)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__105);
   __Pyx_GIVEREF(__pyx_tuple__105);
-  __pyx_codeobj__106 = (PyObject*)__Pyx_PyCode_New(4, 0, 11, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__105, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_format, 348, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__106)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_tuple__107 = PyTuple_Pack(1, ((PyObject *)Py_False)); if (unlikely(!__pyx_tuple__107)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__106 = (PyObject*)__Pyx_PyCode_New(4, 0, 11, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__105, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_format, 352, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__106)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__107 = PyTuple_Pack(1, ((PyObject *)Py_False)); if (unlikely(!__pyx_tuple__107)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__107);
   __Pyx_GIVEREF(__pyx_tuple__107);
 
-  /* "pysam/cvcf.pyx":420
+  /* "pysam/cvcf.pyx":424
  *         return FORMAT(data['id'],t,n,data['type'],data['descr'],data['missing'])
  * 
  *     def format_format( self, fmt, filter=False ):             # <<<<<<<<<<<<<<
  *         values = [('ID',fmt.id)]
  *         if fmt.number != None and not filter:
  */
-  __pyx_tuple__108 = PyTuple_Pack(8, __pyx_n_s_self, __pyx_n_s_fmt, __pyx_n_s_filter_2, __pyx_n_s_values, __pyx_n_s_nmb, __pyx_n_s_format, __pyx_n_s_k, __pyx_n_s_v); if (unlikely(!__pyx_tuple__108)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__108 = PyTuple_Pack(8, __pyx_n_s_self, __pyx_n_s_fmt, __pyx_n_s_filter_2, __pyx_n_s_values, __pyx_n_s_nmb, __pyx_n_s_format, __pyx_n_s_k, __pyx_n_s_v); if (unlikely(!__pyx_tuple__108)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__108);
   __Pyx_GIVEREF(__pyx_tuple__108);
-  __pyx_codeobj__109 = (PyObject*)__Pyx_PyCode_New(3, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__108, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_format_format, 420, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__109)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_tuple__110 = PyTuple_Pack(1, ((PyObject *)Py_False)); if (unlikely(!__pyx_tuple__110)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__109 = (PyObject*)__Pyx_PyCode_New(3, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__108, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_format_format, 424, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__109)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__110 = PyTuple_Pack(1, ((PyObject *)Py_False)); if (unlikely(!__pyx_tuple__110)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__110);
   __Pyx_GIVEREF(__pyx_tuple__110);
 
-  /* "pysam/cvcf.pyx":440
+  /* "pysam/cvcf.pyx":444
  *         return format
  * 
  *     def get_expected(self, format, formatdict, alt):             # <<<<<<<<<<<<<<
  *         fmt = formatdict[format]
  *         if fmt.numbertype == self.NT_UNKNOWN: return -1
  */
-  __pyx_tuple__111 = PyTuple_Pack(5, __pyx_n_s_self, __pyx_n_s_format, __pyx_n_s_formatdict, __pyx_n_s_alt, __pyx_n_s_fmt); if (unlikely(!__pyx_tuple__111)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__111 = PyTuple_Pack(5, __pyx_n_s_self, __pyx_n_s_format, __pyx_n_s_formatdict, __pyx_n_s_alt, __pyx_n_s_fmt); if (unlikely(!__pyx_tuple__111)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__111);
   __Pyx_GIVEREF(__pyx_tuple__111);
-  __pyx_codeobj__112 = (PyObject*)__Pyx_PyCode_New(4, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__111, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_get_expected, 440, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__112)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__112 = (PyObject*)__Pyx_PyCode_New(4, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__111, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_get_expected, 444, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__112)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":451
+  /* "pysam/cvcf.pyx":455
  * 
  * 
  *     def _add_definition(self, formatdict, key, data, line ):             # <<<<<<<<<<<<<<
  *         if key in formatdict: return
  *         self.error(line,self.ERROR_UNKNOWN_KEY,key)
  */
-  __pyx_tuple__113 = PyTuple_Pack(5, __pyx_n_s_self, __pyx_n_s_formatdict, __pyx_n_s_key, __pyx_n_s_data, __pyx_n_s_line); if (unlikely(!__pyx_tuple__113)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__113 = PyTuple_Pack(5, __pyx_n_s_self, __pyx_n_s_formatdict, __pyx_n_s_key, __pyx_n_s_data, __pyx_n_s_line); if (unlikely(!__pyx_tuple__113)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__113);
   __Pyx_GIVEREF(__pyx_tuple__113);
-  __pyx_codeobj__114 = (PyObject*)__Pyx_PyCode_New(5, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__113, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_add_definition, 451, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__114)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__114 = (PyObject*)__Pyx_PyCode_New(5, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__113, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_add_definition, 455, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__114)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":468
+  /* "pysam/cvcf.pyx":472
  * 
  *     # todo: trim trailing missing values
  *     def format_formatdata( self, data, format, key=True, value=True, separator=":" ):             # <<<<<<<<<<<<<<
  *         output, sdata = [], []
  *         if type(data) == type([]): # for FORMAT field, make data with dummy values
  */
-  __pyx_tuple__115 = PyTuple_Pack(13, __pyx_n_s_self, __pyx_n_s_data, __pyx_n_s_format, __pyx_n_s_key, __pyx_n_s_value, __pyx_n_s_separator, __pyx_n_s_output_2, __pyx_n_s_sdata, __pyx_n_s_d, __pyx_n_s_k, __pyx_n_s_idx, __pyx_n_s_v, __pyx_n_s_last); if (unlikely(!__pyx_tuple__115)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__115 = PyTuple_Pack(13, __pyx_n_s_self, __pyx_n_s_data, __pyx_n_s_format, __pyx_n_s_key, __pyx_n_s_value, __pyx_n_s_separator, __pyx_n_s_output_2, __pyx_n_s_sdata, __pyx_n_s_d, __pyx_n_s_k, __pyx_n_s_idx, __pyx_n_s_v, __pyx_n_s_last); if (unlikely(!__pyx_tuple__115)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__115);
   __Pyx_GIVEREF(__pyx_tuple__115);
-  __pyx_codeobj__116 = (PyObject*)__Pyx_PyCode_New(6, 0, 13, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__115, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_format_formatdata, 468, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__116)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_tuple__117 = PyTuple_Pack(3, ((PyObject *)Py_True), ((PyObject *)Py_True), ((PyObject*)__pyx_kp_s__4)); if (unlikely(!__pyx_tuple__117)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__116 = (PyObject*)__Pyx_PyCode_New(6, 0, 13, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__115, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_format_formatdata, 472, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__116)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__117 = PyTuple_Pack(3, ((PyObject *)Py_True), ((PyObject *)Py_True), ((PyObject*)__pyx_kp_s__4)); if (unlikely(!__pyx_tuple__117)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__117);
   __Pyx_GIVEREF(__pyx_tuple__117);
 
-  /* "pysam/cvcf.pyx":502
+  /* "pysam/cvcf.pyx":506
  * 
  * 
  *     def enter_default_format(self):             # <<<<<<<<<<<<<<
  *         for f in [FORMAT('GT',self.NT_NUMBER,1,'String','Genotype','.'),
  *                   FORMAT('DP',self.NT_NUMBER,1,'Integer','Read depth at this position for this sample',-1),
  */
-  __pyx_tuple__118 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_f); if (unlikely(!__pyx_tuple__118)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__118 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_f); if (unlikely(!__pyx_tuple__118)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__118);
   __Pyx_GIVEREF(__pyx_tuple__118);
-  __pyx_codeobj__119 = (PyObject*)__Pyx_PyCode_New(1, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__118, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_enter_default_format, 502, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__119)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__119 = (PyObject*)__Pyx_PyCode_New(1, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__118, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_enter_default_format, 506, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__119)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":521
+  /* "pysam/cvcf.pyx":525
  *                 self._format[f.id] = f
  * 
- *     def parse_header( self, line ):             # <<<<<<<<<<<<<<
+ *     def parse_header(self, line):             # <<<<<<<<<<<<<<
+ * 
  *         assert line.startswith('##')
- *         elts = line[2:].split('=')
  */
-  __pyx_tuple__120 = PyTuple_Pack(6, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_elts, __pyx_n_s_key, __pyx_n_s_value, __pyx_n_s_f); if (unlikely(!__pyx_tuple__120)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__120 = PyTuple_Pack(6, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_elts, __pyx_n_s_key, __pyx_n_s_value, __pyx_n_s_f); if (unlikely(!__pyx_tuple__120)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__120);
   __Pyx_GIVEREF(__pyx_tuple__120);
-  __pyx_codeobj__121 = (PyObject*)__Pyx_PyCode_New(2, 0, 6, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__120, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_header, 521, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__121)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__121 = (PyObject*)__Pyx_PyCode_New(2, 0, 6, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__120, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_header, 525, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__121)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":550
+  /* "pysam/cvcf.pyx":555
  * 
  * 
  *     def write_header( self, stream ):             # <<<<<<<<<<<<<<
  *         stream.write("##fileformat=VCFv%s.%s\n" % (self._version // 10, self._version % 10))
  *         for key,value in self._header: stream.write("##%s=%s\n" % (key,value))
  */
-  __pyx_tuple__122 = PyTuple_Pack(7, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_key, __pyx_n_s_value, __pyx_n_s_var, __pyx_n_s_label, __pyx_n_s_f); if (unlikely(!__pyx_tuple__122)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__122 = PyTuple_Pack(7, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_key, __pyx_n_s_value, __pyx_n_s_var, __pyx_n_s_label, __pyx_n_s_f); if (unlikely(!__pyx_tuple__122)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__122);
   __Pyx_GIVEREF(__pyx_tuple__122);
-  __pyx_codeobj__123 = (PyObject*)__Pyx_PyCode_New(2, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__122, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_write_header, 550, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__123)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__123 = (PyObject*)__Pyx_PyCode_New(2, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__122, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_write_header, 555, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__123)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":557
+  /* "pysam/cvcf.pyx":562
  * 
  * 
  *     def parse_heading( self, line ):             # <<<<<<<<<<<<<<
  *         assert line.startswith('#')
  *         assert not line.startswith('##')
  */
-  __pyx_tuple__124 = PyTuple_Pack(8, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_headings, __pyx_n_s_i, __pyx_n_s_s, __pyx_n_s_err, __pyx_n_s_x, __pyx_n_s_y); if (unlikely(!__pyx_tuple__124)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__124 = PyTuple_Pack(8, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_headings, __pyx_n_s_i, __pyx_n_s_s, __pyx_n_s_err, __pyx_n_s_x, __pyx_n_s_y); if (unlikely(!__pyx_tuple__124)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__124);
   __Pyx_GIVEREF(__pyx_tuple__124);
-  __pyx_codeobj__125 = (PyObject*)__Pyx_PyCode_New(2, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__124, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_heading, 557, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__125)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__125 = (PyObject*)__Pyx_PyCode_New(2, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__124, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_heading, 562, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__125)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":585
+  /* "pysam/cvcf.pyx":590
  *         self._sample2column = dict( [(y,x+9) for x,y in enumerate( self._samples ) ] )
  * 
  *     def write_heading( self, stream ):             # <<<<<<<<<<<<<<
  *         stream.write("#" + "\t".join(self._required + self._samples) + "\n")
  * 
  */
-  __pyx_tuple__126 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_stream); if (unlikely(!__pyx_tuple__126)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__126 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_stream); if (unlikely(!__pyx_tuple__126)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__126);
   __Pyx_GIVEREF(__pyx_tuple__126);
-  __pyx_codeobj__127 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__126, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_write_heading, 585, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__127)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__127 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__126, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_write_heading, 590, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__127)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":588
+  /* "pysam/cvcf.pyx":593
  *         stream.write("#" + "\t".join(self._required + self._samples) + "\n")
  * 
  *     def convertGT(self, GTstring):             # <<<<<<<<<<<<<<
  *         if GTstring == ".": return ["."]
  *         try:
  */
-  __pyx_tuple__128 = PyTuple_Pack(3, __pyx_n_s_self, __pyx_n_s_GTstring, __pyx_n_s_gts); if (unlikely(!__pyx_tuple__128)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__128 = PyTuple_Pack(3, __pyx_n_s_self, __pyx_n_s_GTstring, __pyx_n_s_gts); if (unlikely(!__pyx_tuple__128)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__128);
   __Pyx_GIVEREF(__pyx_tuple__128);
-  __pyx_codeobj__129 = (PyObject*)__Pyx_PyCode_New(2, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__128, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_convertGT, 588, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__129)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__129 = (PyObject*)__Pyx_PyCode_New(2, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__128, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_convertGT, 593, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__129)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":600
+  /* "pysam/cvcf.pyx":605
  *             return [".","|","."]
  * 
  *     def convertGTback(self, GTdata):             # <<<<<<<<<<<<<<
  *         return ''.join(map(str,GTdata))
  * 
  */
-  __pyx_tuple__130 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_GTdata); if (unlikely(!__pyx_tuple__130)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__130 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_GTdata); if (unlikely(!__pyx_tuple__130)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__130);
   __Pyx_GIVEREF(__pyx_tuple__130);
-  __pyx_codeobj__131 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__130, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_convertGTback, 600, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__131)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__131 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__130, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_convertGTback, 605, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__131)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":603
+  /* "pysam/cvcf.pyx":608
  *         return ''.join(map(str,GTdata))
  * 
  *     def parse_formatdata( self, key, value, formatdict, line ):             # <<<<<<<<<<<<<<
  *         # To do: check that the right number of values is present
  *         f = formatdict.get(key,None)
  */
-  __pyx_tuple__132 = PyTuple_Pack(9, __pyx_n_s_self, __pyx_n_s_key, __pyx_n_s_value, __pyx_n_s_formatdict, __pyx_n_s_line, __pyx_n_s_f, __pyx_n_s_values, __pyx_n_s_idx, __pyx_n_s_v); if (unlikely(!__pyx_tuple__132)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__132 = PyTuple_Pack(9, __pyx_n_s_self, __pyx_n_s_key, __pyx_n_s_value, __pyx_n_s_formatdict, __pyx_n_s_line, __pyx_n_s_f, __pyx_n_s_values, __pyx_n_s_idx, __pyx_n_s_v); if (unlikely(!__pyx_tuple__132)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__132);
   __Pyx_GIVEREF(__pyx_tuple__132);
-  __pyx_codeobj__133 = (PyObject*)__Pyx_PyCode_New(5, 0, 9, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__132, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_formatdata, 603, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__133)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__133 = (PyObject*)__Pyx_PyCode_New(5, 0, 9, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__132, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_formatdata, 608, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__133)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":645
+  /* "pysam/cvcf.pyx":650
  *             self.error(line,self.ERROR_INFO_STRING)
  * 
  *     def inregion(self, chrom, pos):             # <<<<<<<<<<<<<<
  *         if not self._regions: return True
  *         for r in self._regions:
  */
-  __pyx_tuple__134 = PyTuple_Pack(4, __pyx_n_s_self, __pyx_n_s_chrom, __pyx_n_s_pos, __pyx_n_s_r); if (unlikely(!__pyx_tuple__134)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__134 = PyTuple_Pack(4, __pyx_n_s_self, __pyx_n_s_chrom, __pyx_n_s_pos, __pyx_n_s_r); if (unlikely(!__pyx_tuple__134)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__134);
   __Pyx_GIVEREF(__pyx_tuple__134);
-  __pyx_codeobj__135 = (PyObject*)__Pyx_PyCode_New(3, 0, 4, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__134, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_inregion, 645, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__135)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__135 = (PyObject*)__Pyx_PyCode_New(3, 0, 4, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__134, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_inregion, 650, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__135)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":651
+  /* "pysam/cvcf.pyx":656
  *         return False
  * 
  *     def parse_data( self, line, lineparse=False ):             # <<<<<<<<<<<<<<
  *         cols = line.split('\t')
  *         if len(cols) != len(self._samples)+9:
  */
-  __pyx_tuple__136 = PyTuple_Pack(43, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_lineparse, __pyx_n_s_cols, __pyx_n_s_chrom, __pyx_n_s_pos, __pyx_n_s_id, __pyx_n_s_ref, __pyx_n_s_c, __pyx_n_s_left, __pyx_n_s_faref_leftflank, __pyx_n_s_faref, __pyx_n_s_alt, __pyx_n_s_qual, __pyx_n_s_filter_2, __pyx_n_s_info_2, __pyx_n_s_blurp, __pyx_n_s_elts, __pyx_n_s_v, __pyx_n_s_format, __pyx_n_s_f, __pyx_n_s_newalts, __pyx_n_s_have_deletions, __pyx_n_s_a, __pyx_n_s_l, __pyx_n_s_addns, __pyx_n_s_i, __py [...]
+  __pyx_tuple__136 = PyTuple_Pack(43, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_lineparse, __pyx_n_s_cols, __pyx_n_s_chrom, __pyx_n_s_pos, __pyx_n_s_id, __pyx_n_s_ref, __pyx_n_s_c, __pyx_n_s_left, __pyx_n_s_faref_leftflank, __pyx_n_s_faref, __pyx_n_s_alt, __pyx_n_s_qual, __pyx_n_s_filter_2, __pyx_n_s_info_2, __pyx_n_s_blurp, __pyx_n_s_elts, __pyx_n_s_v, __pyx_n_s_format, __pyx_n_s_f, __pyx_n_s_newalts, __pyx_n_s_have_deletions, __pyx_n_s_a, __pyx_n_s_l, __pyx_n_s_addns, __pyx_n_s_i, __py [...]
   __Pyx_GOTREF(__pyx_tuple__136);
   __Pyx_GIVEREF(__pyx_tuple__136);
-  __pyx_codeobj__137 = (PyObject*)__Pyx_PyCode_New(3, 0, 43, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__136, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_data, 651, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__137)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_tuple__138 = PyTuple_Pack(1, ((PyObject *)Py_False)); if (unlikely(!__pyx_tuple__138)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__137 = (PyObject*)__Pyx_PyCode_New(3, 0, 43, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__136, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_data, 656, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__137)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__138 = PyTuple_Pack(1, ((PyObject *)Py_False)); if (unlikely(!__pyx_tuple__138)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__138);
   __Pyx_GIVEREF(__pyx_tuple__138);
 
-  /* "pysam/cvcf.pyx":863
+  /* "pysam/cvcf.pyx":868
  * 
  * 
  *     def write_data(self, stream, data):             # <<<<<<<<<<<<<<
  *         required = ['chrom','pos','id','ref','alt','qual','filter','info','format'] + self._samples
  *         for k in required:
  */
-  __pyx_tuple__139 = PyTuple_Pack(10, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_data, __pyx_n_s_required_2, __pyx_n_s_k, __pyx_n_s_alt, __pyx_n_s_filter_2, __pyx_n_s_qual, __pyx_n_s_output_2, __pyx_n_s_s); if (unlikely(!__pyx_tuple__139)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__139 = PyTuple_Pack(10, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_data, __pyx_n_s_required_2, __pyx_n_s_k, __pyx_n_s_alt, __pyx_n_s_filter_2, __pyx_n_s_qual, __pyx_n_s_output_2, __pyx_n_s_s); if (unlikely(!__pyx_tuple__139)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__139);
   __Pyx_GIVEREF(__pyx_tuple__139);
-  __pyx_codeobj__140 = (PyObject*)__Pyx_PyCode_New(3, 0, 10, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__139, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_write_data, 863, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__140)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__140 = (PyObject*)__Pyx_PyCode_New(3, 0, 10, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__139, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_write_data, 868, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__140)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":892
+  /* "pysam/cvcf.pyx":900
  *         stream.write( "\t".join(output) + "\n" )
  * 
  *     def _parse_header(self, stream):             # <<<<<<<<<<<<<<
  *         self._lineno = 0
  *         for line in stream:
  */
-  __pyx_tuple__141 = PyTuple_Pack(3, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_line); if (unlikely(!__pyx_tuple__141)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__141 = PyTuple_Pack(3, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_line); if (unlikely(!__pyx_tuple__141)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__141);
   __Pyx_GIVEREF(__pyx_tuple__141);
-  __pyx_codeobj__142 = (PyObject*)__Pyx_PyCode_New(2, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__141, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_header_2, 892, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__142)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__142 = (PyObject*)__Pyx_PyCode_New(2, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__141, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_header_2, 900, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__142)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":906
+  /* "pysam/cvcf.pyx":914
  *         return line
  * 
  *     def _parse(self, line, stream):             # <<<<<<<<<<<<<<
  *         # deal with files with header only
  *         if line.startswith("##"): return
  */
-  __pyx_tuple__143 = PyTuple_Pack(4, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_stream, __pyx_n_s_d); if (unlikely(!__pyx_tuple__143)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__143 = PyTuple_Pack(4, __pyx_n_s_self, __pyx_n_s_line, __pyx_n_s_stream, __pyx_n_s_d); if (unlikely(!__pyx_tuple__143)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__143);
   __Pyx_GIVEREF(__pyx_tuple__143);
-  __pyx_codeobj__144 = (PyObject*)__Pyx_PyCode_New(3, 0, 4, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__143, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse, 906, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__144)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__144 = (PyObject*)__Pyx_PyCode_New(3, 0, 4, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__143, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse, 914, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__144)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":924
+  /* "pysam/cvcf.pyx":932
  *     ######################################################################################################
  * 
  *     def getsamples(self):             # <<<<<<<<<<<<<<
  *         """ List of samples in VCF file """
  *         return self._samples
  */
-  __pyx_tuple__145 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__145)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__145 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__145)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__145);
   __Pyx_GIVEREF(__pyx_tuple__145);
-  __pyx_codeobj__146 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__145, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getsamples, 924, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__146)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__146 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__145, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getsamples, 932, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__146)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":928
+  /* "pysam/cvcf.pyx":936
  *         return self._samples
  * 
  *     def setsamples(self,samples):             # <<<<<<<<<<<<<<
  *         """ List of samples in VCF file """
  *         self._samples = samples
  */
-  __pyx_tuple__147 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_samples_2); if (unlikely(!__pyx_tuple__147)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 928; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__147 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_samples_2); if (unlikely(!__pyx_tuple__147)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__147);
   __Pyx_GIVEREF(__pyx_tuple__147);
-  __pyx_codeobj__148 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__147, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setsamples, 928, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__148)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 928; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__148 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__147, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setsamples, 936, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__148)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":932
+  /* "pysam/cvcf.pyx":940
  *         self._samples = samples
  * 
  *     def getheader(self):             # <<<<<<<<<<<<<<
  *         """ List of header key-value pairs (strings) """
  *         return self._header
  */
-  __pyx_tuple__149 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__149)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__149 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__149)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__149);
   __Pyx_GIVEREF(__pyx_tuple__149);
-  __pyx_codeobj__150 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__149, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getheader, 932, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__150)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__150 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__149, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getheader, 940, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__150)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":936
+  /* "pysam/cvcf.pyx":944
  *         return self._header
  * 
  *     def setheader(self,header):             # <<<<<<<<<<<<<<
  *         """ List of header key-value pairs (strings) """
  *         self._header = header
  */
-  __pyx_tuple__151 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_header_2); if (unlikely(!__pyx_tuple__151)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__151 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_header_2); if (unlikely(!__pyx_tuple__151)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__151);
   __Pyx_GIVEREF(__pyx_tuple__151);
-  __pyx_codeobj__152 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__151, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setheader, 936, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__152)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__152 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__151, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setheader, 944, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__152)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":940
+  /* "pysam/cvcf.pyx":948
  *         self._header = header
  * 
  *     def getinfo(self):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##INFO tags, as VCF.FORMAT values """
  *         return self._info
  */
-  __pyx_tuple__153 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__153)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__153 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__153)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__153);
   __Pyx_GIVEREF(__pyx_tuple__153);
-  __pyx_codeobj__154 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__153, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getinfo, 940, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__154)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__154 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__153, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getinfo, 948, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__154)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":944
+  /* "pysam/cvcf.pyx":952
  *         return self._info
  * 
  *     def setinfo(self,info):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##INFO tags, as VCF.FORMAT values """
  *         self._info = info
  */
-  __pyx_tuple__155 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_info_2); if (unlikely(!__pyx_tuple__155)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__155 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_info_2); if (unlikely(!__pyx_tuple__155)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__155);
   __Pyx_GIVEREF(__pyx_tuple__155);
-  __pyx_codeobj__156 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__155, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setinfo, 944, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__156)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__156 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__155, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setinfo, 952, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__156)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":948
+  /* "pysam/cvcf.pyx":956
  *         self._info = info
  * 
  *     def getformat(self):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##FORMAT tags, as VCF.FORMAT values """
  *         return self._format
  */
-  __pyx_tuple__157 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__157)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__157 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__157)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__157);
   __Pyx_GIVEREF(__pyx_tuple__157);
-  __pyx_codeobj__158 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__157, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getformat, 948, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__158)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__158 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__157, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getformat, 956, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__158)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":952
+  /* "pysam/cvcf.pyx":960
  *         return self._format
  * 
  *     def setformat(self,format):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##FORMAT tags, as VCF.FORMAT values """
  *         self._format = format
  */
-  __pyx_tuple__159 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_format); if (unlikely(!__pyx_tuple__159)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__159 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_format); if (unlikely(!__pyx_tuple__159)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__159);
   __Pyx_GIVEREF(__pyx_tuple__159);
-  __pyx_codeobj__160 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__159, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setformat, 952, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__160)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__160 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__159, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setformat, 960, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__160)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":956
+  /* "pysam/cvcf.pyx":964
  *         self._format = format
  * 
  *     def getfilter(self):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##FILTER tags, as VCF.FORMAT values """
  *         return self._filter
  */
-  __pyx_tuple__161 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__161)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__161 = PyTuple_Pack(1, __pyx_n_s_self); if (unlikely(!__pyx_tuple__161)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__161);
   __Pyx_GIVEREF(__pyx_tuple__161);
-  __pyx_codeobj__162 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__161, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getfilter, 956, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__162)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__162 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__161, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_getfilter, 964, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__162)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":960
+  /* "pysam/cvcf.pyx":968
  *         return self._filter
  * 
  *     def setfilter(self,filter):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##FILTER tags, as VCF.FORMAT values """
  *         self._filter = filter
  */
-  __pyx_tuple__163 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_filter_2); if (unlikely(!__pyx_tuple__163)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__163 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_filter_2); if (unlikely(!__pyx_tuple__163)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__163);
   __Pyx_GIVEREF(__pyx_tuple__163);
-  __pyx_codeobj__164 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__163, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setfilter, 960, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__164)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__164 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__163, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setfilter, 968, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__164)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":964
+  /* "pysam/cvcf.pyx":972
  *         self._filter = filter
  * 
  *     def setversion(self, version):             # <<<<<<<<<<<<<<
  *         if version != 33 and version != 40: raise ValueError("Can only handle v3.3 and v4.0 VCF files")
  *         self._version = version
  */
-  __pyx_tuple__165 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_version_2); if (unlikely(!__pyx_tuple__165)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__165 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_version_2); if (unlikely(!__pyx_tuple__165)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 972; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__165);
   __Pyx_GIVEREF(__pyx_tuple__165);
-  __pyx_codeobj__166 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__165, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setversion, 964, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__166)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__166 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__165, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setversion, 972, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__166)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 972; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":968
+  /* "pysam/cvcf.pyx":976
  *         self._version = version
  * 
  *     def setregions(self, regions):             # <<<<<<<<<<<<<<
  *         self._regions = regions
  * 
  */
-  __pyx_tuple__167 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_regions); if (unlikely(!__pyx_tuple__167)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__167 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_regions); if (unlikely(!__pyx_tuple__167)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__167);
   __Pyx_GIVEREF(__pyx_tuple__167);
-  __pyx_codeobj__168 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__167, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setregions, 968, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__168)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__168 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__167, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setregions, 976, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__168)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":971
+  /* "pysam/cvcf.pyx":979
  *         self._regions = regions
  * 
  *     def setreference(self, ref):             # <<<<<<<<<<<<<<
  *         """ Provide a reference sequence; a Python class supporting a fetch(chromosome, start, end) method, e.g. PySam.FastaFile """
  *         self._reference = ref
  */
-  __pyx_tuple__169 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_ref); if (unlikely(!__pyx_tuple__169)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__169 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_ref); if (unlikely(!__pyx_tuple__169)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__169);
   __Pyx_GIVEREF(__pyx_tuple__169);
-  __pyx_codeobj__170 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__169, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setreference, 971, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__170)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__170 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__169, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_setreference, 979, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__170)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":975
+  /* "pysam/cvcf.pyx":983
  *         self._reference = ref
  * 
  *     def ignoreerror(self, errorstring):             # <<<<<<<<<<<<<<
  *         try:             self._ignored_errors.add(self.__dict__[errorstring])
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  */
-  __pyx_tuple__171 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_errorstring); if (unlikely(!__pyx_tuple__171)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__171 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_errorstring); if (unlikely(!__pyx_tuple__171)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__171);
   __Pyx_GIVEREF(__pyx_tuple__171);
-  __pyx_codeobj__172 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__171, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_ignoreerror, 975, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__172)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__172 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__171, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_ignoreerror, 983, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__172)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":979
+  /* "pysam/cvcf.pyx":987
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  * 
  *     def warnerror(self, errorstring):             # <<<<<<<<<<<<<<
  *         try:             self._warn_errors.add(self.__dict__[errorstring])
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  */
-  __pyx_tuple__173 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_errorstring); if (unlikely(!__pyx_tuple__173)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__173 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_errorstring); if (unlikely(!__pyx_tuple__173)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__173);
   __Pyx_GIVEREF(__pyx_tuple__173);
-  __pyx_codeobj__174 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__173, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_warnerror, 979, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__174)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__174 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__173, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_warnerror, 987, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__174)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":983
+  /* "pysam/cvcf.pyx":991
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  * 
  *     def parse(self, stream):             # <<<<<<<<<<<<<<
  *         """ Parse a stream of VCF-formatted lines.  Initializes class instance and return generator """
  *         last_line = self._parse_header(stream)
  */
-  __pyx_tuple__175 = PyTuple_Pack(3, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_last_line); if (unlikely(!__pyx_tuple__175)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__175 = PyTuple_Pack(3, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_last_line); if (unlikely(!__pyx_tuple__175)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__175);
   __Pyx_GIVEREF(__pyx_tuple__175);
-  __pyx_codeobj__176 = (PyObject*)__Pyx_PyCode_New(2, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__175, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_2, 983, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__176)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__176 = (PyObject*)__Pyx_PyCode_New(2, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__175, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_parse_2, 991, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__176)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":990
+  /* "pysam/cvcf.pyx":998
  *         return self._parse(last_line, stream)
  * 
  *     def write(self, stream, datagenerator):             # <<<<<<<<<<<<<<
  *         """ Writes a VCF file to a stream, using a data generator (or list) """
  *         self.write_header(stream)
  */
-  __pyx_tuple__177 = PyTuple_Pack(4, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_datagenerator, __pyx_n_s_data); if (unlikely(!__pyx_tuple__177)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__177 = PyTuple_Pack(4, __pyx_n_s_self, __pyx_n_s_stream, __pyx_n_s_datagenerator, __pyx_n_s_data); if (unlikely(!__pyx_tuple__177)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__177);
   __Pyx_GIVEREF(__pyx_tuple__177);
-  __pyx_codeobj__178 = (PyObject*)__Pyx_PyCode_New(3, 0, 4, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__177, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_write, 990, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__178)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__178 = (PyObject*)__Pyx_PyCode_New(3, 0, 4, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__177, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_write, 998, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__178)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":996
+  /* "pysam/cvcf.pyx":1004
  *         for data in datagenerator: self.write_data(stream,data)
  * 
  *     def writeheader(self, stream):             # <<<<<<<<<<<<<<
  *         """ Writes a VCF header """
  *         self.write_header(stream)
  */
-  __pyx_tuple__179 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_stream); if (unlikely(!__pyx_tuple__179)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__179 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_stream); if (unlikely(!__pyx_tuple__179)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__179);
   __Pyx_GIVEREF(__pyx_tuple__179);
-  __pyx_codeobj__180 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__179, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_writeheader, 996, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__180)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__180 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__179, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_writeheader, 1004, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__180)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":1001
+  /* "pysam/cvcf.pyx":1009
  *         self.write_heading(stream)
  * 
  *     def compare_calls(self, pos1, ref1, alt1, pos2, ref2, alt2):             # <<<<<<<<<<<<<<
  *         """ Utility function: compares two calls for equality """
  *         # a variant should always be assigned to a unique position, one base before
  */
-  __pyx_tuple__181 = PyTuple_Pack(7, __pyx_n_s_self, __pyx_n_s_pos1, __pyx_n_s_ref1, __pyx_n_s_alt1, __pyx_n_s_pos2, __pyx_n_s_ref2, __pyx_n_s_alt2); if (unlikely(!__pyx_tuple__181)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__181 = PyTuple_Pack(7, __pyx_n_s_self, __pyx_n_s_pos1, __pyx_n_s_ref1, __pyx_n_s_alt1, __pyx_n_s_pos2, __pyx_n_s_ref2, __pyx_n_s_alt2); if (unlikely(!__pyx_tuple__181)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__181);
   __Pyx_GIVEREF(__pyx_tuple__181);
-  __pyx_codeobj__182 = (PyObject*)__Pyx_PyCode_New(7, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__181, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_compare_calls, 1001, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__182)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__182 = (PyObject*)__Pyx_PyCode_New(7, 0, 7, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__181, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_compare_calls, 1009, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__182)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":1023
+  /* "pysam/cvcf.pyx":1031
  * ###########################################################################################################
  * 
- *     def connect(self, filename):             # <<<<<<<<<<<<<<
+ *     def connect(self, filename, encoding="ascii"):             # <<<<<<<<<<<<<<
  *         '''connect to tabix file.'''
- *         self.tabixfile = pysam.Tabixfile( filename )
+ *         self.encoding=encoding
  */
-  __pyx_tuple__183 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_filename); if (unlikely(!__pyx_tuple__183)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1023; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__183 = PyTuple_Pack(3, __pyx_n_s_self, __pyx_n_s_filename, __pyx_n_s_encoding); if (unlikely(!__pyx_tuple__183)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_tuple__183);
   __Pyx_GIVEREF(__pyx_tuple__183);
-  __pyx_codeobj__184 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__183, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_connect, 1023, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__184)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1023; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_codeobj__184 = (PyObject*)__Pyx_PyCode_New(3, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__183, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_connect, 1031, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__184)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__185 = PyTuple_Pack(1, ((PyObject*)__pyx_n_s_ascii)); if (unlikely(!__pyx_tuple__185)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__185);
+  __Pyx_GIVEREF(__pyx_tuple__185);
 
-  /* "pysam/cvcf.pyx":1028
+  /* "pysam/cvcf.pyx":1037
  *         self._parse_header(self.tabixfile.header)
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
- *               reference = None,
- *               start = None,
- */
-  __pyx_tuple__185 = PyTuple_Pack(5, __pyx_n_s_self, __pyx_n_s_reference, __pyx_n_s_start, __pyx_n_s_end, __pyx_n_s_region); if (unlikely(!__pyx_tuple__185)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__185);
-  __Pyx_GIVEREF(__pyx_tuple__185);
-  __pyx_codeobj__186 = (PyObject*)__Pyx_PyCode_New(5, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__185, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_fetch, 1028, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__186)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_tuple__187 = PyTuple_Pack(4, ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_None)); if (unlikely(!__pyx_tuple__187)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__187);
-  __Pyx_GIVEREF(__pyx_tuple__187);
+ *               reference=None,
+ *               start=None,
+ */
+  __pyx_tuple__186 = PyTuple_Pack(5, __pyx_n_s_self, __pyx_n_s_reference, __pyx_n_s_start, __pyx_n_s_end, __pyx_n_s_region); if (unlikely(!__pyx_tuple__186)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__186);
+  __Pyx_GIVEREF(__pyx_tuple__186);
+  __pyx_codeobj__187 = (PyObject*)__Pyx_PyCode_New(5, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__186, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_fetch, 1037, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__187)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__188 = PyTuple_Pack(4, ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_None), ((PyObject *)Py_None)); if (unlikely(!__pyx_tuple__188)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__188);
+  __Pyx_GIVEREF(__pyx_tuple__188);
 
-  /* "pysam/cvcf.pyx":1042
+  /* "pysam/cvcf.pyx":1051
  *             parser = asVCFRecord(self))
  * 
- *     def validate( self, record ):             # <<<<<<<<<<<<<<
+ *     def validate(self, record):             # <<<<<<<<<<<<<<
  *         '''validate vcf record.
  * 
  */
-  __pyx_tuple__188 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_record); if (unlikely(!__pyx_tuple__188)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __Pyx_GOTREF(__pyx_tuple__188);
-  __Pyx_GIVEREF(__pyx_tuple__188);
-  __pyx_codeobj__189 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__188, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_validate, 1042, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__189)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_tuple__189 = PyTuple_Pack(2, __pyx_n_s_self, __pyx_n_s_record); if (unlikely(!__pyx_tuple__189)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1051; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_tuple__189);
+  __Pyx_GIVEREF(__pyx_tuple__189);
+  __pyx_codeobj__190 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__189, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_andreas_devel_pysam_pysam, __pyx_n_s_validate, 1051, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__190)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1051; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_RefNannyFinishContext();
   return 0;
   __pyx_L1_error:;
@@ -25498,10 +25683,10 @@ PyMODINIT_FUNC PyInit_cvcf(void)
   __pyx_vtable_5pysam_4cvcf_VCFRecord.__pyx_base = *__pyx_vtabptr_5pysam_10TabProxies_TupleProxy;
   __pyx_vtable_5pysam_4cvcf_VCFRecord.__pyx_base.update = (PyObject *(*)(struct __pyx_obj_5pysam_10TabProxies_TupleProxy *, char *, size_t))__pyx_f_5pysam_4cvcf_9VCFRecord_update;
   __pyx_type_5pysam_4cvcf_VCFRecord.tp_base = __pyx_ptype_5pysam_10TabProxies_TupleProxy;
-  if (PyType_Ready(&__pyx_type_5pysam_4cvcf_VCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_4cvcf_VCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_4cvcf_VCFRecord.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_4cvcf_VCFRecord.tp_dict, __pyx_vtabptr_5pysam_4cvcf_VCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "VCFRecord", (PyObject *)&__pyx_type_5pysam_4cvcf_VCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_4cvcf_VCFRecord.tp_dict, __pyx_vtabptr_5pysam_4cvcf_VCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "VCFRecord", (PyObject *)&__pyx_type_5pysam_4cvcf_VCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_4cvcf_VCFRecord = &__pyx_type_5pysam_4cvcf_VCFRecord;
   __pyx_ptype_5pysam_6ctabix_Parser = __Pyx_ImportType("pysam.ctabix", "Parser", sizeof(struct __pyx_obj_5pysam_6ctabix_Parser), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_Parser)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_vtabptr_5pysam_6ctabix_Parser = (struct __pyx_vtabstruct_5pysam_6ctabix_Parser*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_Parser->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_Parser)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
@@ -25509,59 +25694,60 @@ PyMODINIT_FUNC PyInit_cvcf(void)
   __pyx_vtable_5pysam_4cvcf_asVCFRecord.__pyx_base = *__pyx_vtabptr_5pysam_6ctabix_Parser;
   __pyx_vtable_5pysam_4cvcf_asVCFRecord.__pyx_base.parse = (PyObject *(*)(struct __pyx_obj_5pysam_6ctabix_Parser *, char *, int))__pyx_f_5pysam_4cvcf_11asVCFRecord_parse;
   __pyx_type_5pysam_4cvcf_asVCFRecord.tp_base = __pyx_ptype_5pysam_6ctabix_Parser;
-  if (PyType_Ready(&__pyx_type_5pysam_4cvcf_asVCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_4cvcf_asVCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 230; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_4cvcf_asVCFRecord.tp_print = 0;
-  if (__Pyx_SetVtable(__pyx_type_5pysam_4cvcf_asVCFRecord.tp_dict, __pyx_vtabptr_5pysam_4cvcf_asVCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_SetAttrString(__pyx_m, "asVCFRecord", (PyObject *)&__pyx_type_5pysam_4cvcf_asVCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 229; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetVtable(__pyx_type_5pysam_4cvcf_asVCFRecord.tp_dict, __pyx_vtabptr_5pysam_4cvcf_asVCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 230; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_SetAttrString(__pyx_m, "asVCFRecord", (PyObject *)&__pyx_type_5pysam_4cvcf_asVCFRecord) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 230; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_ptype_5pysam_4cvcf_asVCFRecord = &__pyx_type_5pysam_4cvcf_asVCFRecord;
-  if (PyType_Ready(&__pyx_type_5pysam_4cvcf___pyx_scope_struct___parse) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyType_Ready(&__pyx_type_5pysam_4cvcf___pyx_scope_struct___parse) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __pyx_type_5pysam_4cvcf___pyx_scope_struct___parse.tp_print = 0;
   __pyx_ptype_5pysam_4cvcf___pyx_scope_struct___parse = &__pyx_type_5pysam_4cvcf___pyx_scope_struct___parse;
   /*--- Type import code ---*/
-  __pyx_ptype_5pysam_6ctabix_tabix_file_iterator = __Pyx_ImportType("pysam.ctabix", "tabix_file_iterator", sizeof(struct __pyx_obj_5pysam_6ctabix_tabix_file_iterator), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_tabix_file_iterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_tabix_file_iterator = (struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_tabix_file_iterator->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_tabix_file_iterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_Tabixfile = __Pyx_ImportType("pysam.ctabix", "Tabixfile", sizeof(struct __pyx_obj_5pysam_6ctabix_Tabixfile), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_Tabixfile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_asTuple = __Pyx_ImportType("pysam.ctabix", "asTuple", sizeof(struct __pyx_obj_5pysam_6ctabix_asTuple), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_asTuple)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_asTuple = (struct __pyx_vtabstruct_5pysam_6ctabix_asTuple*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_asTuple->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_asTuple)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_asGTF = __Pyx_ImportType("pysam.ctabix", "asGTF", sizeof(struct __pyx_obj_5pysam_6ctabix_asGTF), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_asGTF)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_asGTF = (struct __pyx_vtabstruct_5pysam_6ctabix_asGTF*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_asGTF->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_asGTF)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_asBed = __Pyx_ImportType("pysam.ctabix", "asBed", sizeof(struct __pyx_obj_5pysam_6ctabix_asBed), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_asBed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_asBed = (struct __pyx_vtabstruct_5pysam_6ctabix_asBed*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_asBed->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_asBed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_asVCF = __Pyx_ImportType("pysam.ctabix", "asVCF", sizeof(struct __pyx_obj_5pysam_6ctabix_asVCF), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_asVCF)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_asVCF = (struct __pyx_vtabstruct_5pysam_6ctabix_asVCF*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_asVCF->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_asVCF)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_TabixIterator = __Pyx_ImportType("pysam.ctabix", "TabixIterator", sizeof(struct __pyx_obj_5pysam_6ctabix_TabixIterator), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_TabixIterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_TabixIterator = (struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_TabixIterator->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_TabixIterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_TabixIteratorParsed = __Pyx_ImportType("pysam.ctabix", "TabixIteratorParsed", sizeof(struct __pyx_obj_5pysam_6ctabix_TabixIteratorParsed), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_TabixIteratorParsed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_TabixIteratorParsed = (struct __pyx_vtabstruct_5pysam_6ctabix_TabixIteratorParsed*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_TabixIteratorParsed->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_TabixIteratorParsed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_GZIterator = __Pyx_ImportType("pysam.ctabix", "GZIterator", sizeof(struct __pyx_obj_5pysam_6ctabix_GZIterator), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_GZIterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 67; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_GZIterator = (struct __pyx_vtabstruct_5pysam_6ctabix_GZIterator*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_GZIterator->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_GZIterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 67; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_GZIteratorHead = __Pyx_ImportType("pysam.ctabix", "GZIteratorHead", sizeof(struct __pyx_obj_5pysam_6ctabix_GZIteratorHead), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_GZIteratorHead)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 74; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_GZIteratorHead = (struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorHead*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_GZIteratorHead->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_GZIteratorHead)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 74; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_6ctabix_GZIteratorParsed = __Pyx_ImportType("pysam.ctabix", "GZIteratorParsed", sizeof(struct __pyx_obj_5pysam_6ctabix_GZIteratorParsed), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_GZIteratorParsed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_6ctabix_GZIteratorParsed = (struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorParsed*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_GZIteratorParsed->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_GZIteratorParsed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_10TabProxies_GTFProxy = __Pyx_ImportType("pysam.TabProxies", "GTFProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_GTFProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_GTFProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_10TabProxies_GTFProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_GTFProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_GTFProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_10TabProxies_NamedTupleProxy = __Pyx_ImportType("pysam.TabProxies", "NamedTupleProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_10TabProxies_BedProxy = __Pyx_ImportType("pysam.TabProxies", "BedProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_BedProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_BedProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_10TabProxies_BedProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_BedProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_BedProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 72; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_ptype_5pysam_10TabProxies_VCFProxy = __Pyx_ImportType("pysam.TabProxies", "VCFProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_VCFProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_VCFProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  __pyx_vtabptr_5pysam_10TabProxies_VCFProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_VCFProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_VCFProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 83; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_tabix_file_iterator = __Pyx_ImportType("pysam.ctabix", "tabix_file_iterator", sizeof(struct __pyx_obj_5pysam_6ctabix_tabix_file_iterator), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_tabix_file_iterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_tabix_file_iterator = (struct __pyx_vtabstruct_5pysam_6ctabix_tabix_file_iterator*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_tabix_file_iterator->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_tabix_file_iterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_TabixFile = __Pyx_ImportType("pysam.ctabix", "TabixFile", sizeof(struct __pyx_obj_5pysam_6ctabix_TabixFile), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_TabixFile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_asTuple = __Pyx_ImportType("pysam.ctabix", "asTuple", sizeof(struct __pyx_obj_5pysam_6ctabix_asTuple), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_asTuple)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_asTuple = (struct __pyx_vtabstruct_5pysam_6ctabix_asTuple*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_asTuple->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_asTuple)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_asGTF = __Pyx_ImportType("pysam.ctabix", "asGTF", sizeof(struct __pyx_obj_5pysam_6ctabix_asGTF), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_asGTF)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_asGTF = (struct __pyx_vtabstruct_5pysam_6ctabix_asGTF*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_asGTF->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_asGTF)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_asBed = __Pyx_ImportType("pysam.ctabix", "asBed", sizeof(struct __pyx_obj_5pysam_6ctabix_asBed), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_asBed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_asBed = (struct __pyx_vtabstruct_5pysam_6ctabix_asBed*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_asBed->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_asBed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_asVCF = __Pyx_ImportType("pysam.ctabix", "asVCF", sizeof(struct __pyx_obj_5pysam_6ctabix_asVCF), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_asVCF)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 67; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_asVCF = (struct __pyx_vtabstruct_5pysam_6ctabix_asVCF*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_asVCF->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_asVCF)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 67; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_TabixIterator = __Pyx_ImportType("pysam.ctabix", "TabixIterator", sizeof(struct __pyx_obj_5pysam_6ctabix_TabixIterator), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_TabixIterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 70; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_TabixIterator = (struct __pyx_vtabstruct_5pysam_6ctabix_TabixIterator*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_TabixIterator->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_TabixIterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 70; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_TabixIteratorParsed = __Pyx_ImportType("pysam.ctabix", "TabixIteratorParsed", sizeof(struct __pyx_obj_5pysam_6ctabix_TabixIteratorParsed), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_TabixIteratorParsed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_TabixIteratorParsed = (struct __pyx_vtabstruct_5pysam_6ctabix_TabixIteratorParsed*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_TabixIteratorParsed->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_TabixIteratorParsed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_GZIterator = __Pyx_ImportType("pysam.ctabix", "GZIterator", sizeof(struct __pyx_obj_5pysam_6ctabix_GZIterator), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_GZIterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_GZIterator = (struct __pyx_vtabstruct_5pysam_6ctabix_GZIterator*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_GZIterator->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_GZIterator)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_GZIteratorHead = __Pyx_ImportType("pysam.ctabix", "GZIteratorHead", sizeof(struct __pyx_obj_5pysam_6ctabix_GZIteratorHead), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_GZIteratorHead)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_GZIteratorHead = (struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorHead*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_GZIteratorHead->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_GZIteratorHead)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_GZIteratorParsed = __Pyx_ImportType("pysam.ctabix", "GZIteratorParsed", sizeof(struct __pyx_obj_5pysam_6ctabix_GZIteratorParsed), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_GZIteratorParsed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 91; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_6ctabix_GZIteratorParsed = (struct __pyx_vtabstruct_5pysam_6ctabix_GZIteratorParsed*)__Pyx_GetVtable(__pyx_ptype_5pysam_6ctabix_GZIteratorParsed->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_6ctabix_GZIteratorParsed)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 91; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_6ctabix_Tabixfile = __Pyx_ImportType("pysam.ctabix", "Tabixfile", sizeof(struct __pyx_obj_5pysam_6ctabix_Tabixfile), 1); if (unlikely(!__pyx_ptype_5pysam_6ctabix_Tabixfile)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_10TabProxies_GTFProxy = __Pyx_ImportType("pysam.TabProxies", "GTFProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_GTFProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_GTFProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_10TabProxies_GTFProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_GTFProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_GTFProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_GTFProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_10TabProxies_NamedTupleProxy = __Pyx_ImportType("pysam.TabProxies", "NamedTupleProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_NamedTupleProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_NamedTupleProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_NamedTupleProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_NamedTupleProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_10TabProxies_BedProxy = __Pyx_ImportType("pysam.TabProxies", "BedProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_BedProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_BedProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 76; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_10TabProxies_BedProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_BedProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_BedProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_BedProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 76; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_ptype_5pysam_10TabProxies_VCFProxy = __Pyx_ImportType("pysam.TabProxies", "VCFProxy", sizeof(struct __pyx_obj_5pysam_10TabProxies_VCFProxy), 1); if (unlikely(!__pyx_ptype_5pysam_10TabProxies_VCFProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_vtabptr_5pysam_10TabProxies_VCFProxy = (struct __pyx_vtabstruct_5pysam_10TabProxies_VCFProxy*)__Pyx_GetVtable(__pyx_ptype_5pysam_10TabProxies_VCFProxy->tp_dict); if (unlikely(!__pyx_vtabptr_5pysam_10TabProxies_VCFProxy)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   /*--- Variable import code ---*/
   /*--- Function import code ---*/
   __pyx_t_1 = __Pyx_ImportModule("pysam.ctabix"); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (__Pyx_ImportFunction(__pyx_t_1, "_force_str", (void (**)(void))&__pyx_f_5pysam_6ctabix__force_str, "PyObject *(PyObject *)") < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (__Pyx_ImportFunction(__pyx_t_1, "_force_str", (void (**)(void))&__pyx_f_5pysam_6ctabix__force_str, "PyObject *(PyObject *, struct __pyx_opt_args_5pysam_6ctabix__force_str *__pyx_optional_args)") < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   Py_DECREF(__pyx_t_1); __pyx_t_1 = 0;
   /*--- Execution code ---*/
 
-  /* "pysam/cvcf.pyx":46
+  /* "pysam/cvcf.pyx":47
  * #
  * 
  * from collections import namedtuple, defaultdict             # <<<<<<<<<<<<<<
  * from operator import itemgetter
  * import sys, re, copy, bisect
  */
-  __pyx_t_2 = PyList_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyList_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_INCREF(__pyx_n_s_namedtuple);
   PyList_SET_ITEM(__pyx_t_2, 0, __pyx_n_s_namedtuple);
@@ -25569,333 +25755,333 @@ PyMODINIT_FUNC PyInit_cvcf(void)
   __Pyx_INCREF(__pyx_n_s_defaultdict);
   PyList_SET_ITEM(__pyx_t_2, 1, __pyx_n_s_defaultdict);
   __Pyx_GIVEREF(__pyx_n_s_defaultdict);
-  __pyx_t_3 = __Pyx_Import(__pyx_n_s_collections, __pyx_t_2, -1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_Import(__pyx_n_s_collections, __pyx_t_2, -1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_ImportFrom(__pyx_t_3, __pyx_n_s_namedtuple); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_ImportFrom(__pyx_t_3, __pyx_n_s_namedtuple); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_namedtuple, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_namedtuple, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_ImportFrom(__pyx_t_3, __pyx_n_s_defaultdict); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_ImportFrom(__pyx_t_3, __pyx_n_s_defaultdict); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_defaultdict, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_defaultdict, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":47
+  /* "pysam/cvcf.pyx":48
  * 
  * from collections import namedtuple, defaultdict
  * from operator import itemgetter             # <<<<<<<<<<<<<<
  * import sys, re, copy, bisect
  * 
  */
-  __pyx_t_3 = PyList_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyList_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_n_s_itemgetter);
   PyList_SET_ITEM(__pyx_t_3, 0, __pyx_n_s_itemgetter);
   __Pyx_GIVEREF(__pyx_n_s_itemgetter);
-  __pyx_t_2 = __Pyx_Import(__pyx_n_s_operator, __pyx_t_3, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_Import(__pyx_n_s_operator, __pyx_t_3, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  __pyx_t_3 = __Pyx_ImportFrom(__pyx_t_2, __pyx_n_s_itemgetter); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_ImportFrom(__pyx_t_2, __pyx_n_s_itemgetter); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_itemgetter, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_itemgetter, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":48
+  /* "pysam/cvcf.pyx":49
  * from collections import namedtuple, defaultdict
  * from operator import itemgetter
  * import sys, re, copy, bisect             # <<<<<<<<<<<<<<
  * 
  * cimport ctabix
  */
-  __pyx_t_2 = __Pyx_Import(__pyx_n_s_sys, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_Import(__pyx_n_s_sys, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_sys, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_sys, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_Import(__pyx_n_s_re, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_Import(__pyx_n_s_re, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_re, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_re, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_Import(__pyx_n_s_copy_2, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_Import(__pyx_n_s_copy_2, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_copy_2, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_copy_2, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_Import(__pyx_n_s_bisect, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_Import(__pyx_n_s_bisect, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_bisect, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_bisect, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":53
+  /* "pysam/cvcf.pyx":54
  * cimport TabProxies
  * 
  * import pysam             # <<<<<<<<<<<<<<
  * 
  * gtsRegEx = re.compile("[|/\\\\]")
  */
-  __pyx_t_2 = __Pyx_Import(__pyx_n_s_pysam, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_Import(__pyx_n_s_pysam, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_pysam, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_pysam, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":55
+  /* "pysam/cvcf.pyx":56
  * import pysam
  * 
  * gtsRegEx = re.compile("[|/\\\\]")             # <<<<<<<<<<<<<<
  * alleleRegEx = re.compile('^[ACGTN]+$')
  * 
  */
-  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_compile); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_compile); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__92, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__92, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_gtsRegEx, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_gtsRegEx, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":56
+  /* "pysam/cvcf.pyx":57
  * 
  * gtsRegEx = re.compile("[|/\\\\]")
  * alleleRegEx = re.compile('^[ACGTN]+$')             # <<<<<<<<<<<<<<
  * 
  * # Utility function.  Uses 0-based coordinates
  */
-  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_re); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_compile); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_compile); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__93, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_tuple__93, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_alleleRegEx, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_alleleRegEx, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":59
+  /* "pysam/cvcf.pyx":60
  * 
  * # Utility function.  Uses 0-based coordinates
  * def get_sequence(chrom, start, end, fa):             # <<<<<<<<<<<<<<
  *     # obtain sequence from .fa file, without truncation
  *     if end<=start: return ""
  */
-  __pyx_t_2 = PyCFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_1get_sequence, NULL, __pyx_n_s_pysam_cvcf); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyCFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_1get_sequence, NULL, __pyx_n_s_pysam_cvcf); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_get_sequence, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_get_sequence, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":69
+  /* "pysam/cvcf.pyx":70
  * 
  * # Utility function.  Parses a region string
  * def parse_regions( string ):             # <<<<<<<<<<<<<<
  *     result = []
  *     for r in string.split(','):
  */
-  __pyx_t_2 = PyCFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3parse_regions, NULL, __pyx_n_s_pysam_cvcf); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = PyCFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3parse_regions, NULL, __pyx_n_s_pysam_cvcf); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 70; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_parse_regions, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_parse_regions, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 70; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
 
-  /* "pysam/cvcf.pyx":87
+  /* "pysam/cvcf.pyx":88
  * 
  * 
  * FORMAT = namedtuple('FORMAT','id numbertype number type description missingvalue')             # <<<<<<<<<<<<<<
  * 
  * ###########################################################################################################
  */
-  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_namedtuple); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_namedtuple); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__98, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__98, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_FORMAT, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 87; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_FORMAT, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":241
+  /* "pysam/cvcf.pyx":242
  *         return r
  * 
  * class VCF(object):             # <<<<<<<<<<<<<<
  * 
  *     # types
  */
-  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_builtin_object);
   PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_builtin_object);
   __Pyx_GIVEREF(__pyx_builtin_object);
-  __pyx_t_2 = __Pyx_CalculateMetaclass(NULL, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_2 = __Pyx_CalculateMetaclass(NULL, __pyx_t_3); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_2);
-  __pyx_t_4 = __Pyx_Py3MetaclassPrepare(__pyx_t_2, __pyx_t_3, __pyx_n_s_VCF, __pyx_n_s_VCF, (PyObject *) NULL, __pyx_n_s_pysam_cvcf, (PyObject *) NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_4 = __Pyx_Py3MetaclassPrepare(__pyx_t_2, __pyx_t_3, __pyx_n_s_VCF, __pyx_n_s_VCF, (PyObject *) NULL, __pyx_n_s_pysam_cvcf, (PyObject *) NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_4);
 
-  /* "pysam/cvcf.pyx":244
+  /* "pysam/cvcf.pyx":245
  * 
  *     # types
  *     NT_UNKNOWN = 0             # <<<<<<<<<<<<<<
  *     NT_NUMBER = 1
  *     NT_ALLELES = 2
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_UNKNOWN, __pyx_int_0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_UNKNOWN, __pyx_int_0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":245
+  /* "pysam/cvcf.pyx":246
  *     # types
  *     NT_UNKNOWN = 0
  *     NT_NUMBER = 1             # <<<<<<<<<<<<<<
  *     NT_ALLELES = 2
  *     NT_NR_ALLELES = 3
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_NUMBER, __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_NUMBER, __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":246
+  /* "pysam/cvcf.pyx":247
  *     NT_UNKNOWN = 0
  *     NT_NUMBER = 1
  *     NT_ALLELES = 2             # <<<<<<<<<<<<<<
  *     NT_NR_ALLELES = 3
  *     NT_GENOTYPES = 4
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_ALLELES, __pyx_int_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_ALLELES, __pyx_int_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":247
+  /* "pysam/cvcf.pyx":248
  *     NT_NUMBER = 1
  *     NT_ALLELES = 2
  *     NT_NR_ALLELES = 3             # <<<<<<<<<<<<<<
  *     NT_GENOTYPES = 4
  *     NT_PHASED_GENOTYPES = 5
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_NR_ALLELES, __pyx_int_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_NR_ALLELES, __pyx_int_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":248
+  /* "pysam/cvcf.pyx":249
  *     NT_ALLELES = 2
  *     NT_NR_ALLELES = 3
  *     NT_GENOTYPES = 4             # <<<<<<<<<<<<<<
  *     NT_PHASED_GENOTYPES = 5
  * 
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_GENOTYPES, __pyx_int_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_GENOTYPES, __pyx_int_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":249
+  /* "pysam/cvcf.pyx":250
  *     NT_NR_ALLELES = 3
  *     NT_GENOTYPES = 4
  *     NT_PHASED_GENOTYPES = 5             # <<<<<<<<<<<<<<
  * 
  *     _errors = { 0:"UNKNOWN_FORMAT_STRING:Unknown file format identifier",
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_PHASED_GENOTYPES, __pyx_int_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 249; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_NT_PHASED_GENOTYPES, __pyx_int_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 250; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":251
+  /* "pysam/cvcf.pyx":252
  *     NT_PHASED_GENOTYPES = 5
  * 
  *     _errors = { 0:"UNKNOWN_FORMAT_STRING:Unknown file format identifier",             # <<<<<<<<<<<<<<
  *                 1:"BADLY_FORMATTED_FORMAT_STRING:Formatting error in the format string",
  *                 2:"BADLY_FORMATTED_HEADING:Did not find 9 required headings (CHROM, POS, ..., FORMAT) %s",
  */
-  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_0, __pyx_kp_s_UNKNOWN_FORMAT_STRING_Unknown_fi) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_1, __pyx_kp_s_BADLY_FORMATTED_FORMAT_STRING_Fo) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_2, __pyx_kp_s_BADLY_FORMATTED_HEADING_Did_not) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_3, __pyx_kp_s_BAD_NUMBER_OF_COLUMNS_Wrong_numb) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_4, __pyx_kp_s_POS_NOT_NUMERICAL_Position_colum) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_5, __pyx_kp_s_UNKNOWN_CHAR_IN_REF_Unknown_char) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_6, __pyx_kp_s_V33_BAD_REF_Reference_should_be) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_7, __pyx_kp_s_V33_BAD_ALLELE_Cannot_interpret) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_8, __pyx_kp_s_POS_NOT_POSITIVE_Position_field) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_9, __pyx_kp_s_QUAL_NOT_NUMERICAL_Quality_field) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_10, __pyx_kp_s_ERROR_INFO_STRING_Error_while_pa) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_11, __pyx_kp_s_ERROR_UNKNOWN_KEY_Unknown_key_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_12, __pyx_kp_s_ERROR_FORMAT_NOT_NUMERICAL_Expec) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_13, __pyx_kp_s_ERROR_FORMAT_NOT_CHAR_Eexpected) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_14, __pyx_kp_s_FILTER_NOT_DEFINED_Identifier_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_15, __pyx_kp_s_FORMAT_NOT_DEFINED_Identifier_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_16, __pyx_kp_s_BAD_NUMBER_OF_VALUES_Found_too_m) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_17, __pyx_kp_s_BAD_NUMBER_OF_PARAMETERS_Found_u) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_18, __pyx_kp_s_BAD_GENOTYPE_Cannot_parse_genoty) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_19, __pyx_kp_s_V40_BAD_ALLELE_Bad_allele_found) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_20, __pyx_kp_s_MISSING_REF_Reference_allele_mis) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_21, __pyx_kp_s_V33_UNMATCHED_DELETION_Deleted_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_22, __pyx_kp_s_V40_MISSING_ANGLE_BRACKETS_Forma) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_23, __pyx_kp_s_FORMAT_MISSING_QUOTES_Descriptio) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_24, __pyx_kp_s_V40_FORMAT_MUST_HAVE_NAMED_FIELD_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_25, __pyx_kp_s_HEADING_NOT_SEPARATED_BY_TABS_He) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_26, __pyx_kp_s_WRONG_REF_Wrong_reference_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_27, __pyx_kp_s_ERROR_TRAILING_DATA_Numerical_fi) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_28, __pyx_kp_s_BAD_CHR_TAG_Error_calculating_ch) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_29, __pyx_kp_s_ZERO_LENGTH_ALLELE_Found_zero_le) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_30, __pyx_kp_s_MISSING_INDEL_ALLELE_REF_BASE_In) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_31, __pyx_kp_s_ZERO_FOR_NON_FLAG_FIELD_number_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_32, __pyx_kp_s_ERROR_FORMAT_NOT_INTEGER_Expecte) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyDict_SetItem(__pyx_t_5, __pyx_int_33, __pyx_kp_s_ERROR_FLAG_HAS_VALUE_Flag_fields) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_errors, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 251; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_0, __pyx_kp_s_UNKNOWN_FORMAT_STRING_Unknown_fi) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_1, __pyx_kp_s_BADLY_FORMATTED_FORMAT_STRING_Fo) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_2, __pyx_kp_s_BADLY_FORMATTED_HEADING_Did_not) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_3, __pyx_kp_s_BAD_NUMBER_OF_COLUMNS_Wrong_numb) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_4, __pyx_kp_s_POS_NOT_NUMERICAL_Position_colum) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_5, __pyx_kp_s_UNKNOWN_CHAR_IN_REF_Unknown_char) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_6, __pyx_kp_s_V33_BAD_REF_Reference_should_be) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_7, __pyx_kp_s_V33_BAD_ALLELE_Cannot_interpret) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_8, __pyx_kp_s_POS_NOT_POSITIVE_Position_field) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_9, __pyx_kp_s_QUAL_NOT_NUMERICAL_Quality_field) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_10, __pyx_kp_s_ERROR_INFO_STRING_Error_while_pa) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_11, __pyx_kp_s_ERROR_UNKNOWN_KEY_Unknown_key_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_12, __pyx_kp_s_ERROR_FORMAT_NOT_NUMERICAL_Expec) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_13, __pyx_kp_s_ERROR_FORMAT_NOT_CHAR_Eexpected) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_14, __pyx_kp_s_FILTER_NOT_DEFINED_Identifier_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_15, __pyx_kp_s_FORMAT_NOT_DEFINED_Identifier_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_16, __pyx_kp_s_BAD_NUMBER_OF_VALUES_Found_too_m) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_17, __pyx_kp_s_BAD_NUMBER_OF_PARAMETERS_Found_u) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_18, __pyx_kp_s_BAD_GENOTYPE_Cannot_parse_genoty) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_19, __pyx_kp_s_V40_BAD_ALLELE_Bad_allele_found) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_20, __pyx_kp_s_MISSING_REF_Reference_allele_mis) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_21, __pyx_kp_s_V33_UNMATCHED_DELETION_Deleted_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_22, __pyx_kp_s_V40_MISSING_ANGLE_BRACKETS_Forma) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_23, __pyx_kp_s_FORMAT_MISSING_QUOTES_Descriptio) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_24, __pyx_kp_s_V40_FORMAT_MUST_HAVE_NAMED_FIELD_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_25, __pyx_kp_s_HEADING_NOT_SEPARATED_BY_TABS_He) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_26, __pyx_kp_s_WRONG_REF_Wrong_reference_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_27, __pyx_kp_s_ERROR_TRAILING_DATA_Numerical_fi) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_28, __pyx_kp_s_BAD_CHR_TAG_Error_calculating_ch) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_29, __pyx_kp_s_ZERO_LENGTH_ALLELE_Found_zero_le) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_30, __pyx_kp_s_MISSING_INDEL_ALLELE_REF_BASE_In) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_31, __pyx_kp_s_ZERO_FOR_NON_FLAG_FIELD_number_s) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_32, __pyx_kp_s_ERROR_FORMAT_NOT_INTEGER_Expecte) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_t_5, __pyx_int_33, __pyx_kp_s_ERROR_FLAG_HAS_VALUE_Flag_fields) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_errors, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 252; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":288
+  /* "pysam/cvcf.pyx":289
  * 
  *     # tag-value pairs; tags are not unique; does not include fileformat, INFO, FILTER or FORMAT fields
  *     _header = []             # <<<<<<<<<<<<<<
  * 
  *     # version number; 33=v3.3; 40=v4.0
  */
-  __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 288; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 289; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_header, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 288; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_header, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 289; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":291
+  /* "pysam/cvcf.pyx":292
  * 
  *     # version number; 33=v3.3; 40=v4.0
  *     _version = 40             # <<<<<<<<<<<<<<
  * 
  *     # info, filter and format data
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_version, __pyx_int_40) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 291; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_version, __pyx_int_40) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 292; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":294
+  /* "pysam/cvcf.pyx":295
  * 
  *     # info, filter and format data
  *     _info = {}             # <<<<<<<<<<<<<<
  *     _filter = {}
  *     _format = {}
  */
-  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 294; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_info, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 294; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_info, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":295
+  /* "pysam/cvcf.pyx":296
  *     # info, filter and format data
  *     _info = {}
  *     _filter = {}             # <<<<<<<<<<<<<<
  *     _format = {}
  * 
  */
-  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_filter, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_filter, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":296
+  /* "pysam/cvcf.pyx":297
  *     _info = {}
  *     _filter = {}
  *     _format = {}             # <<<<<<<<<<<<<<
  * 
  *     # header; and required columns
  */
-  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_format_2, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_format_2, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":299
+  /* "pysam/cvcf.pyx":300
  * 
  *     # header; and required columns
  *     _required = ["CHROM","POS","ID","REF","ALT","QUAL","FILTER","INFO","FORMAT"]             # <<<<<<<<<<<<<<
  *     _samples = []
  * 
  */
-  __pyx_t_5 = PyList_New(9); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 299; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyList_New(9); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_INCREF(__pyx_n_s_CHROM);
   PyList_SET_ITEM(__pyx_t_5, 0, __pyx_n_s_CHROM);
@@ -25924,635 +26110,636 @@ PyMODINIT_FUNC PyInit_cvcf(void)
   __Pyx_INCREF(__pyx_n_s_FORMAT);
   PyList_SET_ITEM(__pyx_t_5, 8, __pyx_n_s_FORMAT);
   __Pyx_GIVEREF(__pyx_n_s_FORMAT);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_required, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 299; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_required, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":300
+  /* "pysam/cvcf.pyx":301
  *     # header; and required columns
  *     _required = ["CHROM","POS","ID","REF","ALT","QUAL","FILTER","INFO","FORMAT"]
  *     _samples = []             # <<<<<<<<<<<<<<
  * 
  *     # control behaviour
  */
-  __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PyList_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 301; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_samples, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 300; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_samples, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 301; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":303
+  /* "pysam/cvcf.pyx":304
  * 
  *     # control behaviour
  *     _ignored_errors = set([11,31])   # ERROR_UNKNOWN_KEY, ERROR_ZERO_FOR_NON_FLAG_FIELD             # <<<<<<<<<<<<<<
  *     _warn_errors = set([])
  *     _leftalign = False
  */
-  __pyx_t_5 = PySet_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PySet_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PySet_Add(__pyx_t_5, __pyx_int_11) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PySet_Add(__pyx_t_5, __pyx_int_31) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_ignored_errors, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 303; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PySet_Add(__pyx_t_5, __pyx_int_11) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PySet_Add(__pyx_t_5, __pyx_int_31) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_ignored_errors, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":304
+  /* "pysam/cvcf.pyx":305
  *     # control behaviour
  *     _ignored_errors = set([11,31])   # ERROR_UNKNOWN_KEY, ERROR_ZERO_FOR_NON_FLAG_FIELD
  *     _warn_errors = set([])             # <<<<<<<<<<<<<<
  *     _leftalign = False
  * 
  */
-  __pyx_t_5 = PySet_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = PySet_New(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 305; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_warn_errors, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 304; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_warn_errors, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 305; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":305
+  /* "pysam/cvcf.pyx":306
  *     _ignored_errors = set([11,31])   # ERROR_UNKNOWN_KEY, ERROR_ZERO_FOR_NON_FLAG_FIELD
  *     _warn_errors = set([])
  *     _leftalign = False             # <<<<<<<<<<<<<<
  * 
  *     # reference sequence
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_leftalign_2, Py_False) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 305; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_leftalign_2, Py_False) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 306; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":308
+  /* "pysam/cvcf.pyx":309
  * 
  *     # reference sequence
  *     _reference = None             # <<<<<<<<<<<<<<
  * 
  *     # regions to include; None includes everything
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_reference_2, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 308; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_reference_2, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 309; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":311
+  /* "pysam/cvcf.pyx":312
  * 
  *     # regions to include; None includes everything
  *     _regions = None             # <<<<<<<<<<<<<<
  * 
  *     # statefull stuff
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_regions_2, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 311; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_regions_2, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 312; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":314
+  /* "pysam/cvcf.pyx":315
  * 
  *     # statefull stuff
  *     _lineno = -1             # <<<<<<<<<<<<<<
  *     _line = None
  *     _lines = None
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_lineno, __pyx_int_neg_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 314; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_lineno, __pyx_int_neg_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 315; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":315
+  /* "pysam/cvcf.pyx":316
  *     # statefull stuff
  *     _lineno = -1
  *     _line = None             # <<<<<<<<<<<<<<
  *     _lines = None
  * 
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_line_2, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 315; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_line_2, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":316
+  /* "pysam/cvcf.pyx":317
  *     _lineno = -1
  *     _line = None
  *     _lines = None             # <<<<<<<<<<<<<<
  * 
- *     def __init__(self, _copy=None, reference=None, regions=None, lines=None, leftalign=False):
+ *     def __init__(self, _copy=None, reference=None, regions=None,
  */
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_lines_2, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 316; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_lines_2, Py_None) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 317; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
 
-  /* "pysam/cvcf.pyx":318
+  /* "pysam/cvcf.pyx":319
  *     _lines = None
  * 
- *     def __init__(self, _copy=None, reference=None, regions=None, lines=None, leftalign=False):             # <<<<<<<<<<<<<<
+ *     def __init__(self, _copy=None, reference=None, regions=None,             # <<<<<<<<<<<<<<
+ *                  lines=None, leftalign=False):
  *         # make error identifiers accessible by name
- *         for id in self._errors.keys(): self.__dict__[self._errors[id].split(':')[0]] = id
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_1__init__, 0, __pyx_n_s_VCF___init, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__100)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_1__init__, 0, __pyx_n_s_VCF___init, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__100)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_CyFunction_SetDefaultsTuple(__pyx_t_5, __pyx_tuple__101);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_init, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 318; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_init, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":339
- *         self._lines = lines
+  /* "pysam/cvcf.pyx":343
+ *         self.encoding = "ascii"
  * 
  *     def error(self,line,error,opt=None):             # <<<<<<<<<<<<<<
  *         if error in self._ignored_errors: return
  *         errorlabel, errorstring = self._errors[error].split(':')
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_3error, 0, __pyx_n_s_VCF_error, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__103)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_3error, 0, __pyx_n_s_VCF_error, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__103)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_CyFunction_SetDefaultsTuple(__pyx_t_5, __pyx_tuple__104);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_error, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_error, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":348
+  /* "pysam/cvcf.pyx":352
  *         raise ValueError(errorstring)
  * 
  *     def parse_format(self,line,format,filter=False):             # <<<<<<<<<<<<<<
  *         if self._version == 40:
  *             if not format.startswith('<'):
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_5parse_format, 0, __pyx_n_s_VCF_parse_format, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__106)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_5parse_format, 0, __pyx_n_s_VCF_parse_format, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__106)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_CyFunction_SetDefaultsTuple(__pyx_t_5, __pyx_tuple__107);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_format, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 348; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_format, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":420
+  /* "pysam/cvcf.pyx":424
  *         return FORMAT(data['id'],t,n,data['type'],data['descr'],data['missing'])
  * 
  *     def format_format( self, fmt, filter=False ):             # <<<<<<<<<<<<<<
  *         values = [('ID',fmt.id)]
  *         if fmt.number != None and not filter:
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_7format_format, 0, __pyx_n_s_VCF_format_format, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__109)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_7format_format, 0, __pyx_n_s_VCF_format_format, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__109)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_CyFunction_SetDefaultsTuple(__pyx_t_5, __pyx_tuple__110);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_format_format, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 420; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_format_format, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":440
+  /* "pysam/cvcf.pyx":444
  *         return format
  * 
  *     def get_expected(self, format, formatdict, alt):             # <<<<<<<<<<<<<<
  *         fmt = formatdict[format]
  *         if fmt.numbertype == self.NT_UNKNOWN: return -1
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_9get_expected, 0, __pyx_n_s_VCF_get_expected, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__112)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_9get_expected, 0, __pyx_n_s_VCF_get_expected, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__112)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_get_expected, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 440; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_get_expected, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 444; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":451
+  /* "pysam/cvcf.pyx":455
  * 
  * 
  *     def _add_definition(self, formatdict, key, data, line ):             # <<<<<<<<<<<<<<
  *         if key in formatdict: return
  *         self.error(line,self.ERROR_UNKNOWN_KEY,key)
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_11_add_definition, 0, __pyx_n_s_VCF__add_definition, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__114)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_11_add_definition, 0, __pyx_n_s_VCF__add_definition, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__114)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_add_definition, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 451; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_add_definition, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":468
+  /* "pysam/cvcf.pyx":472
  * 
  *     # todo: trim trailing missing values
  *     def format_formatdata( self, data, format, key=True, value=True, separator=":" ):             # <<<<<<<<<<<<<<
  *         output, sdata = [], []
  *         if type(data) == type([]): # for FORMAT field, make data with dummy values
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_13format_formatdata, 0, __pyx_n_s_VCF_format_formatdata, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__116)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_13format_formatdata, 0, __pyx_n_s_VCF_format_formatdata, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__116)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_CyFunction_SetDefaultsTuple(__pyx_t_5, __pyx_tuple__117);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_format_formatdata, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 468; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_format_formatdata, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 472; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":502
+  /* "pysam/cvcf.pyx":506
  * 
  * 
  *     def enter_default_format(self):             # <<<<<<<<<<<<<<
  *         for f in [FORMAT('GT',self.NT_NUMBER,1,'String','Genotype','.'),
  *                   FORMAT('DP',self.NT_NUMBER,1,'Integer','Read depth at this position for this sample',-1),
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_15enter_default_format, 0, __pyx_n_s_VCF_enter_default_format, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__119)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_15enter_default_format, 0, __pyx_n_s_VCF_enter_default_format, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__119)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_enter_default_format, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_enter_default_format, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":521
+  /* "pysam/cvcf.pyx":525
  *                 self._format[f.id] = f
  * 
- *     def parse_header( self, line ):             # <<<<<<<<<<<<<<
+ *     def parse_header(self, line):             # <<<<<<<<<<<<<<
+ * 
  *         assert line.startswith('##')
- *         elts = line[2:].split('=')
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_17parse_header, 0, __pyx_n_s_VCF_parse_header, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__121)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_17parse_header, 0, __pyx_n_s_VCF_parse_header, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__121)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_header, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 521; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_header, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 525; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":550
+  /* "pysam/cvcf.pyx":555
  * 
  * 
  *     def write_header( self, stream ):             # <<<<<<<<<<<<<<
  *         stream.write("##fileformat=VCFv%s.%s\n" % (self._version // 10, self._version % 10))
  *         for key,value in self._header: stream.write("##%s=%s\n" % (key,value))
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_19write_header, 0, __pyx_n_s_VCF_write_header, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__123)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_19write_header, 0, __pyx_n_s_VCF_write_header, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__123)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_write_header, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 550; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_write_header, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 555; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":557
+  /* "pysam/cvcf.pyx":562
  * 
  * 
  *     def parse_heading( self, line ):             # <<<<<<<<<<<<<<
  *         assert line.startswith('#')
  *         assert not line.startswith('##')
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_21parse_heading, 0, __pyx_n_s_VCF_parse_heading, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__125)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_21parse_heading, 0, __pyx_n_s_VCF_parse_heading, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__125)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_heading, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_heading, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 562; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":585
+  /* "pysam/cvcf.pyx":590
  *         self._sample2column = dict( [(y,x+9) for x,y in enumerate( self._samples ) ] )
  * 
  *     def write_heading( self, stream ):             # <<<<<<<<<<<<<<
  *         stream.write("#" + "\t".join(self._required + self._samples) + "\n")
  * 
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_23write_heading, 0, __pyx_n_s_VCF_write_heading, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__127)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_23write_heading, 0, __pyx_n_s_VCF_write_heading, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__127)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_write_heading, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 585; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_write_heading, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":588
+  /* "pysam/cvcf.pyx":593
  *         stream.write("#" + "\t".join(self._required + self._samples) + "\n")
  * 
  *     def convertGT(self, GTstring):             # <<<<<<<<<<<<<<
  *         if GTstring == ".": return ["."]
  *         try:
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_25convertGT, 0, __pyx_n_s_VCF_convertGT, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__129)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_25convertGT, 0, __pyx_n_s_VCF_convertGT, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__129)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_convertGT, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 588; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_convertGT, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":600
+  /* "pysam/cvcf.pyx":605
  *             return [".","|","."]
  * 
  *     def convertGTback(self, GTdata):             # <<<<<<<<<<<<<<
  *         return ''.join(map(str,GTdata))
  * 
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_27convertGTback, 0, __pyx_n_s_VCF_convertGTback, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__131)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_27convertGTback, 0, __pyx_n_s_VCF_convertGTback, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__131)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_convertGTback, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 600; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_convertGTback, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 605; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":603
+  /* "pysam/cvcf.pyx":608
  *         return ''.join(map(str,GTdata))
  * 
  *     def parse_formatdata( self, key, value, formatdict, line ):             # <<<<<<<<<<<<<<
  *         # To do: check that the right number of values is present
  *         f = formatdict.get(key,None)
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_29parse_formatdata, 0, __pyx_n_s_VCF_parse_formatdata, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__133)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_29parse_formatdata, 0, __pyx_n_s_VCF_parse_formatdata, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__133)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_formatdata, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 603; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_formatdata, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 608; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":645
+  /* "pysam/cvcf.pyx":650
  *             self.error(line,self.ERROR_INFO_STRING)
  * 
  *     def inregion(self, chrom, pos):             # <<<<<<<<<<<<<<
  *         if not self._regions: return True
  *         for r in self._regions:
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_31inregion, 0, __pyx_n_s_VCF_inregion, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__135)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_31inregion, 0, __pyx_n_s_VCF_inregion, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__135)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_inregion, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_inregion, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 650; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":651
+  /* "pysam/cvcf.pyx":656
  *         return False
  * 
  *     def parse_data( self, line, lineparse=False ):             # <<<<<<<<<<<<<<
  *         cols = line.split('\t')
  *         if len(cols) != len(self._samples)+9:
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_33parse_data, 0, __pyx_n_s_VCF_parse_data, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__137)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_33parse_data, 0, __pyx_n_s_VCF_parse_data, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__137)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
   __Pyx_CyFunction_SetDefaultsTuple(__pyx_t_5, __pyx_tuple__138);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_data, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 651; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_data, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":863
+  /* "pysam/cvcf.pyx":868
  * 
  * 
  *     def write_data(self, stream, data):             # <<<<<<<<<<<<<<
  *         required = ['chrom','pos','id','ref','alt','qual','filter','info','format'] + self._samples
  *         for k in required:
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_35write_data, 0, __pyx_n_s_VCF_write_data, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__140)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_35write_data, 0, __pyx_n_s_VCF_write_data, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__140)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_write_data, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_write_data, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":892
+  /* "pysam/cvcf.pyx":900
  *         stream.write( "\t".join(output) + "\n" )
  * 
  *     def _parse_header(self, stream):             # <<<<<<<<<<<<<<
  *         self._lineno = 0
  *         for line in stream:
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_37_parse_header, 0, __pyx_n_s_VCF__parse_header, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__142)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_37_parse_header, 0, __pyx_n_s_VCF__parse_header, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__142)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_header_2, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_header_2, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 900; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":906
+  /* "pysam/cvcf.pyx":914
  *         return line
  * 
  *     def _parse(self, line, stream):             # <<<<<<<<<<<<<<
  *         # deal with files with header only
  *         if line.startswith("##"): return
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_39_parse, 0, __pyx_n_s_VCF__parse, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__144)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_39_parse, 0, __pyx_n_s_VCF__parse, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__144)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 914; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":924
+  /* "pysam/cvcf.pyx":932
  *     ######################################################################################################
  * 
  *     def getsamples(self):             # <<<<<<<<<<<<<<
  *         """ List of samples in VCF file """
  *         return self._samples
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_42getsamples, 0, __pyx_n_s_VCF_getsamples, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__146)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_42getsamples, 0, __pyx_n_s_VCF_getsamples, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__146)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getsamples, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getsamples, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":928
+  /* "pysam/cvcf.pyx":936
  *         return self._samples
  * 
  *     def setsamples(self,samples):             # <<<<<<<<<<<<<<
  *         """ List of samples in VCF file """
  *         self._samples = samples
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_44setsamples, 0, __pyx_n_s_VCF_setsamples, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__148)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 928; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_44setsamples, 0, __pyx_n_s_VCF_setsamples, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__148)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setsamples, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 928; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setsamples, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":932
+  /* "pysam/cvcf.pyx":940
  *         self._samples = samples
  * 
  *     def getheader(self):             # <<<<<<<<<<<<<<
  *         """ List of header key-value pairs (strings) """
  *         return self._header
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_46getheader, 0, __pyx_n_s_VCF_getheader, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__150)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_46getheader, 0, __pyx_n_s_VCF_getheader, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__150)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getheader, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 932; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getheader, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":936
+  /* "pysam/cvcf.pyx":944
  *         return self._header
  * 
  *     def setheader(self,header):             # <<<<<<<<<<<<<<
  *         """ List of header key-value pairs (strings) """
  *         self._header = header
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_48setheader, 0, __pyx_n_s_VCF_setheader, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__152)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_48setheader, 0, __pyx_n_s_VCF_setheader, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__152)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setheader, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setheader, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":940
+  /* "pysam/cvcf.pyx":948
  *         self._header = header
  * 
  *     def getinfo(self):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##INFO tags, as VCF.FORMAT values """
  *         return self._info
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_50getinfo, 0, __pyx_n_s_VCF_getinfo, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__154)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_50getinfo, 0, __pyx_n_s_VCF_getinfo, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__154)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getinfo, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getinfo, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":944
+  /* "pysam/cvcf.pyx":952
  *         return self._info
  * 
  *     def setinfo(self,info):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##INFO tags, as VCF.FORMAT values """
  *         self._info = info
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_52setinfo, 0, __pyx_n_s_VCF_setinfo, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__156)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_52setinfo, 0, __pyx_n_s_VCF_setinfo, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__156)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setinfo, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setinfo, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":948
+  /* "pysam/cvcf.pyx":956
  *         self._info = info
  * 
  *     def getformat(self):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##FORMAT tags, as VCF.FORMAT values """
  *         return self._format
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_54getformat, 0, __pyx_n_s_VCF_getformat, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__158)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_54getformat, 0, __pyx_n_s_VCF_getformat, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__158)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getformat, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 948; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getformat, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":952
+  /* "pysam/cvcf.pyx":960
  *         return self._format
  * 
  *     def setformat(self,format):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##FORMAT tags, as VCF.FORMAT values """
  *         self._format = format
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_56setformat, 0, __pyx_n_s_VCF_setformat, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__160)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_56setformat, 0, __pyx_n_s_VCF_setformat, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__160)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setformat, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 952; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setformat, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":956
+  /* "pysam/cvcf.pyx":964
  *         self._format = format
  * 
  *     def getfilter(self):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##FILTER tags, as VCF.FORMAT values """
  *         return self._filter
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_58getfilter, 0, __pyx_n_s_VCF_getfilter, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__162)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_58getfilter, 0, __pyx_n_s_VCF_getfilter, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__162)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getfilter, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 956; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_getfilter, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":960
+  /* "pysam/cvcf.pyx":968
  *         return self._filter
  * 
  *     def setfilter(self,filter):             # <<<<<<<<<<<<<<
  *         """ Dictionary of ##FILTER tags, as VCF.FORMAT values """
  *         self._filter = filter
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_60setfilter, 0, __pyx_n_s_VCF_setfilter, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__164)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_60setfilter, 0, __pyx_n_s_VCF_setfilter, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__164)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setfilter, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 960; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setfilter, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":964
+  /* "pysam/cvcf.pyx":972
  *         self._filter = filter
  * 
  *     def setversion(self, version):             # <<<<<<<<<<<<<<
  *         if version != 33 and version != 40: raise ValueError("Can only handle v3.3 and v4.0 VCF files")
  *         self._version = version
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_62setversion, 0, __pyx_n_s_VCF_setversion, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__166)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_62setversion, 0, __pyx_n_s_VCF_setversion, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__166)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 972; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setversion, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 964; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setversion, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 972; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":968
+  /* "pysam/cvcf.pyx":976
  *         self._version = version
  * 
  *     def setregions(self, regions):             # <<<<<<<<<<<<<<
  *         self._regions = regions
  * 
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_64setregions, 0, __pyx_n_s_VCF_setregions, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__168)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_64setregions, 0, __pyx_n_s_VCF_setregions, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__168)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setregions, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 968; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setregions, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":971
+  /* "pysam/cvcf.pyx":979
  *         self._regions = regions
  * 
  *     def setreference(self, ref):             # <<<<<<<<<<<<<<
  *         """ Provide a reference sequence; a Python class supporting a fetch(chromosome, start, end) method, e.g. PySam.FastaFile """
  *         self._reference = ref
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_66setreference, 0, __pyx_n_s_VCF_setreference, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__170)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_66setreference, 0, __pyx_n_s_VCF_setreference, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__170)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setreference, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 971; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_setreference, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":975
+  /* "pysam/cvcf.pyx":983
  *         self._reference = ref
  * 
  *     def ignoreerror(self, errorstring):             # <<<<<<<<<<<<<<
  *         try:             self._ignored_errors.add(self.__dict__[errorstring])
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_68ignoreerror, 0, __pyx_n_s_VCF_ignoreerror, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__172)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_68ignoreerror, 0, __pyx_n_s_VCF_ignoreerror, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__172)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_ignoreerror, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 975; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_ignoreerror, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":979
+  /* "pysam/cvcf.pyx":987
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  * 
  *     def warnerror(self, errorstring):             # <<<<<<<<<<<<<<
  *         try:             self._warn_errors.add(self.__dict__[errorstring])
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_70warnerror, 0, __pyx_n_s_VCF_warnerror, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__174)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_70warnerror, 0, __pyx_n_s_VCF_warnerror, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__174)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_warnerror, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_warnerror, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 987; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":983
+  /* "pysam/cvcf.pyx":991
  *         except KeyError: raise ValueError("Invalid error string: %s" % errorstring)
  * 
  *     def parse(self, stream):             # <<<<<<<<<<<<<<
  *         """ Parse a stream of VCF-formatted lines.  Initializes class instance and return generator """
  *         last_line = self._parse_header(stream)
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_72parse, 0, __pyx_n_s_VCF_parse, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__176)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_72parse, 0, __pyx_n_s_VCF_parse, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__176)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_2, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 983; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_parse_2, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":990
+  /* "pysam/cvcf.pyx":998
  *         return self._parse(last_line, stream)
  * 
  *     def write(self, stream, datagenerator):             # <<<<<<<<<<<<<<
  *         """ Writes a VCF file to a stream, using a data generator (or list) """
  *         self.write_header(stream)
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_74write, 0, __pyx_n_s_VCF_write, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__178)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_74write, 0, __pyx_n_s_VCF_write, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__178)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_write, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 990; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_write, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":996
+  /* "pysam/cvcf.pyx":1004
  *         for data in datagenerator: self.write_data(stream,data)
  * 
  *     def writeheader(self, stream):             # <<<<<<<<<<<<<<
  *         """ Writes a VCF header """
  *         self.write_header(stream)
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_76writeheader, 0, __pyx_n_s_VCF_writeheader, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__180)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_76writeheader, 0, __pyx_n_s_VCF_writeheader, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__180)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_writeheader, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 996; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_writeheader, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":1001
+  /* "pysam/cvcf.pyx":1009
  *         self.write_heading(stream)
  * 
  *     def compare_calls(self, pos1, ref1, alt1, pos2, ref2, alt2):             # <<<<<<<<<<<<<<
  *         """ Utility function: compares two calls for equality """
  *         # a variant should always be assigned to a unique position, one base before
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_78compare_calls, 0, __pyx_n_s_VCF_compare_calls, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__182)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_78compare_calls, 0, __pyx_n_s_VCF_compare_calls, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__182)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_compare_calls, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1001; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_compare_calls, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":1023
+  /* "pysam/cvcf.pyx":1031
  * ###########################################################################################################
  * 
- *     def connect(self, filename):             # <<<<<<<<<<<<<<
+ *     def connect(self, filename, encoding="ascii"):             # <<<<<<<<<<<<<<
  *         '''connect to tabix file.'''
- *         self.tabixfile = pysam.Tabixfile( filename )
+ *         self.encoding=encoding
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_80connect, 0, __pyx_n_s_VCF_connect, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__184)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1023; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_80connect, 0, __pyx_n_s_VCF_connect, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__184)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_connect, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1023; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_CyFunction_SetDefaultsTuple(__pyx_t_5, __pyx_tuple__185);
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_connect, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1031; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":1028
+  /* "pysam/cvcf.pyx":1037
  *         self._parse_header(self.tabixfile.header)
  * 
  *     def fetch(self,             # <<<<<<<<<<<<<<
- *               reference = None,
- *               start = None,
+ *               reference=None,
+ *               start=None,
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_82fetch, 0, __pyx_n_s_VCF_fetch, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__186)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_82fetch, 0, __pyx_n_s_VCF_fetch, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__187)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  __Pyx_CyFunction_SetDefaultsTuple(__pyx_t_5, __pyx_tuple__187);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_fetch, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1028; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_CyFunction_SetDefaultsTuple(__pyx_t_5, __pyx_tuple__188);
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_fetch, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":1042
+  /* "pysam/cvcf.pyx":1051
  *             parser = asVCFRecord(self))
  * 
- *     def validate( self, record ):             # <<<<<<<<<<<<<<
+ *     def validate(self, record):             # <<<<<<<<<<<<<<
  *         '''validate vcf record.
  * 
  */
-  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_84validate, 0, __pyx_n_s_VCF_validate, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__189)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5pysam_4cvcf_3VCF_84validate, 0, __pyx_n_s_VCF_validate, NULL, __pyx_n_s_pysam_cvcf, PyModule_GetDict(__pyx_m), ((PyObject *)__pyx_codeobj__190)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1051; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_validate, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1042; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyObject_SetItem(__pyx_t_4, __pyx_n_s_validate, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1051; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
 
-  /* "pysam/cvcf.pyx":241
+  /* "pysam/cvcf.pyx":242
  *         return r
  * 
  * class VCF(object):             # <<<<<<<<<<<<<<
  * 
  *     # types
  */
-  __pyx_t_5 = __Pyx_Py3ClassCreate(__pyx_t_2, __pyx_n_s_VCF, __pyx_t_3, __pyx_t_4, NULL, 0, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_5 = __Pyx_Py3ClassCreate(__pyx_t_2, __pyx_n_s_VCF, __pyx_t_3, __pyx_t_4, NULL, 0, 1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_5);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VCF, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_VCF, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
   __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
   __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
-  /* "pysam/cvcf.pyx":1169
+  /* "pysam/cvcf.pyx":1178
  *                     pos -= 1
  * 
  * __all__ = [             # <<<<<<<<<<<<<<
  *     "VCF", "VCFRecord", ]
  * 
  */
-  __pyx_t_3 = PyList_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __pyx_t_3 = PyList_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_GOTREF(__pyx_t_3);
   __Pyx_INCREF(__pyx_n_s_VCF);
   PyList_SET_ITEM(__pyx_t_3, 0, __pyx_n_s_VCF);
@@ -26560,7 +26747,7 @@ PyMODINIT_FUNC PyInit_cvcf(void)
   __Pyx_INCREF(__pyx_n_s_VCFRecord);
   PyList_SET_ITEM(__pyx_t_3, 1, __pyx_n_s_VCFRecord);
   __Pyx_GIVEREF(__pyx_n_s_VCFRecord);
-  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  if (PyDict_SetItem(__pyx_d, __pyx_n_s_all, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
 
   /* "pysam/cvcf.pyx":1
diff --git a/pysam/cvcf.pyx b/pysam/cvcf.pyx
index 5809d06..8a6708b 100644
--- a/pysam/cvcf.pyx
+++ b/pysam/cvcf.pyx
@@ -15,29 +15,30 @@
 #
 # The sample keys are accessible through vcf.getsamples()
 #
-# A dictionary of values contains value keys (defined in ##INFO or ##FORMAT lines) which map
-# to a list, containign integers, floats, strings, or characters.  Missing values are replaced 
-# by a particular value, often -1 or .
+# A dictionary of values contains value keys (defined in ##INFO or
+# ##FORMAT lines) which map to a list, containign integers, floats,
+# strings, or characters.  Missing values are replaced by a particular
+# value, often -1 or .
 #
-# Genotypes are not stored as a string, but as a list of 1 or 3 elements (for haploid and diploid samples),
-# the first (and last) the integer representing an allele, and the second the separation character.
-# Note that there is just one genotype per sample, but for consistency the single element is stored in a list.
+# Genotypes are not stored as a string, but as a list of 1 or 3
+# elements (for haploid and diploid samples), the first (and last) the
+# integer representing an allele, and the second the separation
+# character.  Note that there is just one genotype per sample, but for
+# consistency the single element is stored in a list.
 #
-# Header lines other than ##INFO, ##FORMAT and ##FILTER are stored as (key, value) pairs and are accessible
-# through getheader()
+# Header lines other than ##INFO, ##FORMAT and ##FILTER are stored as
+# (key, value) pairs and are accessible through getheader()
 #
-# The VCF class can be instantiated with a 'regions' variable consisting of tuples (chrom,start,end) encoding
-# 0-based half-open segments.  Only variants with a position inside the segment will be parsed.  A regions
-# parser is available under parse_regions.
-#
-# When instantiated, a reference can be passed to the VCF class.  This may be any class that supports a
-# fetch(chrom, start, end) method.
-#
-#
-#
-# NOTE: the position that is returned to Python is 0-based, NOT 1-based as in the VCF file.
+# The VCF class can be instantiated with a 'regions' variable
+# consisting of tuples (chrom,start,end) encoding 0-based half-open
+# segments.  Only variants with a position inside the segment will be
+# parsed.  A regions parser is available under parse_regions.
 #
+# When instantiated, a reference can be passed to the VCF class.  This
+# may be any class that supports a fetch(chrom, start, end) method.
 #
+# NOTE: the position that is returned to Python is 0-based, NOT
+# 1-based as in the VCF file.
 #
 # TODO:
 #  only v4.0 writing is complete; alleles are not converted to v3.3 format
@@ -315,9 +316,11 @@ class VCF(object):
     _line = None
     _lines = None
 
-    def __init__(self, _copy=None, reference=None, regions=None, lines=None, leftalign=False):
+    def __init__(self, _copy=None, reference=None, regions=None,
+                 lines=None, leftalign=False):
         # make error identifiers accessible by name
-        for id in self._errors.keys(): self.__dict__[self._errors[id].split(':')[0]] = id
+        for id in self._errors.keys():
+            self.__dict__[self._errors[id].split(':')[0]] = id
         if _copy != None:
             self._leftalign = _copy._leftalign
             self._header = _copy._header[:]
@@ -335,6 +338,7 @@ class VCF(object):
         if regions: self._regions = regions
         if leftalign: self._leftalign = leftalign
         self._lines = lines
+        self.encoding = "ascii"
 
     def error(self,line,error,opt=None):
         if error in self._ignored_errors: return
@@ -518,7 +522,8 @@ class VCF(object):
             if f.id not in self._format:
                 self._format[f.id] = f
 
-    def parse_header( self, line ):
+    def parse_header(self, line):
+
         assert line.startswith('##')
         elts = line[2:].split('=')
         key = elts[0].strip()
@@ -881,23 +886,26 @@ class VCF(object):
                   alt,
                   qual,
                   filter,
-                  self.format_formatdata( data['info'], self._info, separator=";" ),
-                  self.format_formatdata( data['format'], self._format, value=False ) ]
+                  self.format_formatdata(
+                      data['info'], self._info, separator=";"),
+                  self.format_formatdata(
+                      data['format'], self._format, value=False)]
         
         for s in self._samples:
-            output.append( self.format_formatdata( data[s], self._format, key=False ) )
+            output.append(self.format_formatdata(
+                data[s], self._format, key=False))
         
         stream.write( "\t".join(output) + "\n" )
 
     def _parse_header(self, stream):
         self._lineno = 0
         for line in stream:
-            line = ctabix._force_str(line)
+            line = ctabix._force_str(line, self.encoding)
             self._lineno += 1
             if line.startswith('##'):
-                self.parse_header( line.strip() )
+                self.parse_header(line.strip())
             elif line.startswith('#'):
-                self.parse_heading( line.strip() )
+                self.parse_heading(line.strip())
                 self.enter_default_format()
             else:
                 break
@@ -1020,16 +1028,17 @@ class VCF(object):
 ## API functions added by Andreas
 ###########################################################################################################
 
-    def connect(self, filename):
+    def connect(self, filename, encoding="ascii"):
         '''connect to tabix file.'''
-        self.tabixfile = pysam.Tabixfile( filename )
+        self.encoding=encoding
+        self.tabixfile = pysam.Tabixfile(filename, encoding=encoding)
         self._parse_header(self.tabixfile.header)
         
     def fetch(self,
-              reference = None,
-              start = None, 
-              end = None, 
-              region = None ):
+              reference=None,
+              start=None, 
+              end=None, 
+              region=None ):
         """ Parse a stream of VCF-formatted lines.  
         Initializes class instance and return generator """
         return self.tabixfile.fetch(
@@ -1039,13 +1048,13 @@ class VCF(object):
             region,
             parser = asVCFRecord(self))
 
-    def validate( self, record ):
+    def validate(self, record):
         '''validate vcf record.
 
         returns a validated record.
         '''
         
-        raise NotImplementedError( "needs to be checked" )
+        raise NotImplementedError("needs to be checked")
 
         chrom, pos = record.chrom, record.pos
 
diff --git a/pysam/htslib_util.h b/pysam/htslib_util.h
index 9b65d47..3614473 100644
--- a/pysam/htslib_util.h
+++ b/pysam/htslib_util.h
@@ -48,37 +48,70 @@ unsigned char pysam_translate_sequence(const unsigned char s);
 // return byte size of type
 int aux_type2size(uint8_t type);
 
+
+//-------------------------------------------------------
 // Wrapping accessor macros in sam.h
-static inline int pysam_bam_is_rev(bam1_t * b) {return bam_is_rev(b);};
-static inline int pysam_bam_is_mrev(bam1_t * b) {return bam_is_mrev(b);}
-static inline char * pysam_bam_get_qname(bam1_t * b) {return bam_get_qname(b);}
-static inline uint32_t * pysam_bam_get_cigar(bam1_t * b) {return bam_get_cigar(b);}
-static inline uint8_t * pysam_bam_get_seq(bam1_t * b) {return bam_get_seq(b);}
-static inline uint8_t * pysam_bam_get_qual(bam1_t * b) {return bam_get_qual(b);}
-static inline uint8_t * pysam_bam_get_aux(bam1_t * b) {return bam_get_aux(b);}
-static inline int pysam_bam_get_l_aux(bam1_t * b) { return bam_get_l_aux(b); }
-static inline char pysam_bam_seqi(uint8_t * s, int i) {return bam_seqi(s,i);}
+static inline int pysam_bam_is_rev(bam1_t * b) {
+  return bam_is_rev(b);};
+
+static inline int pysam_bam_is_mrev(bam1_t * b) {
+  return bam_is_mrev(b);}
+
+static inline char * pysam_bam_get_qname(bam1_t * b) {
+  return bam_get_qname(b);}
+
+static inline uint32_t * pysam_bam_get_cigar(bam1_t * b) {
+  return bam_get_cigar(b);}
+
+static inline uint8_t * pysam_bam_get_seq(bam1_t * b) {
+  return bam_get_seq(b);}
+
+static inline uint8_t * pysam_bam_get_qual(bam1_t * b) {
+  return bam_get_qual(b);}
+
+static inline uint8_t * pysam_bam_get_aux(bam1_t * b) {
+  return bam_get_aux(b);}
+
+static inline int pysam_bam_get_l_aux(bam1_t * b) {
+  return bam_get_l_aux(b); }
+
+static inline char pysam_bam_seqi(uint8_t * s, int i) {
+  return bam_seqi(s,i);}
 
 // Wrapping bit field access in bam1_core_t
 // bit fields not supported in cython and due
 // to endian-ness it is not clear which part
 // of the bit-field is in the higher or lower bytes.
-static inline uint16_t pysam_get_bin(bam1_t * b) {return b->core.bin;}
-static inline uint8_t pysam_get_qual(bam1_t * b) {return b->core.qual;}
-static inline uint8_t pysam_get_l_qname(bam1_t * b) {return b->core.l_qname;}
-static inline uint16_t pysam_get_flag(bam1_t * b) {return b->core.flag;}
-static inline uint16_t pysam_get_n_cigar(bam1_t * b) {return b->core.n_cigar;}
+static inline uint16_t pysam_get_bin(bam1_t * b) {
+  return b->core.bin;}
+
+static inline uint8_t pysam_get_qual(bam1_t * b) {
+  return b->core.qual;}
+
+static inline uint8_t pysam_get_l_qname(bam1_t * b) {
+  return b->core.l_qname;}
+
+static inline uint16_t pysam_get_flag(bam1_t * b) {
+  return b->core.flag;}
+
+static inline uint16_t pysam_get_n_cigar(bam1_t * b) {
+  return b->core.n_cigar;}
 
 static inline void pysam_set_bin(bam1_t * b, uint16_t v) {
   b->core.bin=v;}
+
 static inline void pysam_set_qual(bam1_t * b, uint8_t v) {
   b->core.qual=v;}
+
 static inline void pysam_set_l_qname(bam1_t * b, uint8_t v) {
   b->core.l_qname=v;}
+
 static inline void pysam_set_flag(bam1_t * b, uint16_t v) {
   b->core.flag=v;}
+
 static inline void pysam_set_n_cigar(bam1_t * b, uint16_t v) {
   b->core.n_cigar=v;}
+
 static inline void pysam_update_flag(bam1_t * b, uint16_t v, uint16_t flag) {
   if (v)
     b->core.flag |= flag;
diff --git a/pysam/pysam_util.c b/pysam/pysam_util.c
index 2d8a060..9560ed0 100644
--- a/pysam/pysam_util.c
+++ b/pysam/pysam_util.c
@@ -1,10 +1,10 @@
 #include <ctype.h>
 #include <assert.h>
 #include "bam.h"
-#include "khash.h"
-#include "ksort.h"
 #include "bam_endian.h"
-#include "knetfile.h"
+#include "htslib/khash.h"
+#include "htslib/ksort.h"
+#include "htslib/knetfile.h"
 #include "pysam_util.h"
 
 // Definition of pysamerr
@@ -26,6 +26,22 @@ void pysam_unset_stderr(void)
   pysamerr = fopen("/dev/null", "w");
 }
 
+
+// dummy function - required for samtools integration
+void print_error(const char *format, ...)
+{
+}
+
+// dummy function - required for samtools integration
+void print_error_errno(const char *format, ...)
+{
+}
+
+const char *samtools_version()
+{
+}
+
+
 // pysam dispatch function to emulate the samtools
 // command line within python.
 // taken from the main function in bamtk.c
@@ -104,8 +120,8 @@ int pysam_dispatch(int argc, char *argv[] )
       fprintf(stderr, "[main] unrecognized command '%s'\n", argv[1]);
       return 1;
     }
-  fflush( stdout );
-  
+  fflush(stdout);
+
   return retval;
 }
 
diff --git a/pysam/samfile_util.c b/pysam/samfile_util.c
index 50cabae..f5724ae 100644
--- a/pysam/samfile_util.c
+++ b/pysam/samfile_util.c
@@ -1,5 +1,6 @@
 #include "samfile_util.h"
 #include "htslib/sam.h"
+
 #include "kprobaln.h"
 
 // taken from bam_md.c
@@ -183,4 +184,5 @@ int bam_prob_realn(bam1_t *b, const char *ref)
 {
 	return bam_prob_realn_core(b, ref, 1);
 }
+ 
 
diff --git a/pysam/samfile_util.h b/pysam/samfile_util.h
index ae3eb49..dd3e27a 100644
--- a/pysam/samfile_util.h
+++ b/pysam/samfile_util.h
@@ -6,6 +6,5 @@
 int bam_cap_mapQ(bam1_t *b, char *ref, int thres);
 int bam_prob_realn(bam1_t *b, const char *ref);
 
-
 #endif
 
diff --git a/pysam/version.py b/pysam/version.py
index fde7d27..13749ed 100644
--- a/pysam/version.py
+++ b/pysam/version.py
@@ -1,9 +1,7 @@
 # pysam versioning information
 
-__version__ = "0.8.0"
+__version__ = "0.8.1"
 
-__samtools_version__ = "0.1.19"
+__samtools_version__ = "1.1"
 
-__tabix_version__ = "0.2.6"
-
-__htslib_version__ = "0.0.1"
+__htslib_version__ = "1.1"
diff --git a/samtools/__init__.py b/samtools/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/samtools/bam.c.pysam.c b/samtools/bam.c.pysam.c
index cf15e9e..751a183 100644
--- a/samtools/bam.c.pysam.c
+++ b/samtools/bam.c.pysam.c
@@ -1,476 +1,192 @@
 #include "pysam.h"
 
-#include <stdio.h>
-#include <ctype.h>
-#include <errno.h>
-#include <assert.h>
-#include "bam.h"
-#include "bam_endian.h"
-#include "kstring.h"
-#include "sam_header.h"
-
-int bam_is_be = 0, bam_verbose = 2, bam_no_B = 0;
-char *bam_flag2char_table = "pPuUrR12sfd\0\0\0\0\0";
-
-/**************************
- * CIGAR related routines *
- **************************/
-
-uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
-{
-	int k, end = c->pos;
-	for (k = 0; k < c->n_cigar; ++k) {
-		int op  = bam_cigar_op(cigar[k]);
-		int len = bam_cigar_oplen(cigar[k]);
-		if (op == BAM_CBACK) { // move backward
-			int l, u, v;
-			if (k == c->n_cigar - 1) break; // skip trailing 'B'
-			for (l = k - 1, u = v = 0; l >= 0; --l) {
-				int op1  = bam_cigar_op(cigar[l]);
-				int len1 = bam_cigar_oplen(cigar[l]);
-				if (bam_cigar_type(op1)&1) { // consume query
-					if (u + len1 >= len) { // stop
-						if (bam_cigar_type(op1)&2) v += len - u;
-						break;
-					} else u += len1;
-				}
-				if (bam_cigar_type(op1)&2) v += len1;
-			}
-			end = l < 0? c->pos : end - v;
-		} else if (bam_cigar_type(op)&2) end += bam_cigar_oplen(cigar[k]);
-	}
-	return end;
-}
-
-int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar)
-{
-	uint32_t k;
-	int32_t l = 0;
-	for (k = 0; k < c->n_cigar; ++k)
-		if (bam_cigar_type(bam_cigar_op(cigar[k]))&1)
-			l += bam_cigar_oplen(cigar[k]);
-	return l;
-}
-
-/********************
- * BAM I/O routines *
- ********************/
-
-bam_header_t *bam_header_init()
-{
-	bam_is_be = bam_is_big_endian();
-	return (bam_header_t*)calloc(1, sizeof(bam_header_t));
-}
-
-void bam_header_destroy(bam_header_t *header)
-{
-	int32_t i;
-	extern void bam_destroy_header_hash(bam_header_t *header);
-	if (header == 0) return;
-	if (header->target_name) {
-		for (i = 0; i < header->n_targets; ++i)
-			free(header->target_name[i]);
-		free(header->target_name);
-		free(header->target_len);
-	}
-	free(header->text);
-	if (header->dict) sam_header_free(header->dict);
-	if (header->rg2lib) sam_tbl_destroy(header->rg2lib);
-	bam_destroy_header_hash(header);
-	free(header);
-}
-
-bam_header_t *bam_header_read(bamFile fp)
-{
-	bam_header_t *header;
-	char buf[4];
-	int magic_len;
-	int32_t i = 1, name_len;
-	// check EOF
-	i = bgzf_check_EOF(fp);
-	if (i < 0) {
-		// If the file is a pipe, checking the EOF marker will *always* fail
-		// with ESPIPE.  Suppress the error message in this case.
-		if (errno != ESPIPE) perror("[bam_header_read] bgzf_check_EOF");
-	}
-	else if (i == 0) fprintf(pysamerr, "[bam_header_read] EOF marker is absent. The input is probably truncated.\n");
-	// read "BAM1"
-	magic_len = bam_read(fp, buf, 4);
-	if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) {
-		fprintf(pysamerr, "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n");
-		return 0;
-	}
-	header = bam_header_init();
-	// read plain text and the number of reference sequences
-	bam_read(fp, &header->l_text, 4);
-	if (bam_is_be) bam_swap_endian_4p(&header->l_text);
-	header->text = (char*)calloc(header->l_text + 1, 1);
-	bam_read(fp, header->text, header->l_text);
-	bam_read(fp, &header->n_targets, 4);
-	if (bam_is_be) bam_swap_endian_4p(&header->n_targets);
-	// read reference sequence names and lengths
-	header->target_name = (char**)calloc(header->n_targets, sizeof(char*));
-	header->target_len = (uint32_t*)calloc(header->n_targets, 4);
-	for (i = 0; i != header->n_targets; ++i) {
-		bam_read(fp, &name_len, 4);
-		if (bam_is_be) bam_swap_endian_4p(&name_len);
-		header->target_name[i] = (char*)calloc(name_len, 1);
-		bam_read(fp, header->target_name[i], name_len);
-		bam_read(fp, &header->target_len[i], 4);
-		if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
-	}
-	return header;
-}
+/*  bam.c -- BAM format.
 
-int bam_header_write(bamFile fp, const bam_header_t *header)
-{
-	char buf[4];
-	int32_t i, name_len, x;
-	// write "BAM1"
-	strncpy(buf, "BAM\001", 4);
-	bam_write(fp, buf, 4);
-	// write plain text and the number of reference sequences
-	if (bam_is_be) {
-		x = bam_swap_endian_4(header->l_text);
-		bam_write(fp, &x, 4);
-		if (header->l_text) bam_write(fp, header->text, header->l_text);
-		x = bam_swap_endian_4(header->n_targets);
-		bam_write(fp, &x, 4);
-	} else {
-		bam_write(fp, &header->l_text, 4);
-		if (header->l_text) bam_write(fp, header->text, header->l_text);
-		bam_write(fp, &header->n_targets, 4);
-	}
-	// write sequence names and lengths
-	for (i = 0; i != header->n_targets; ++i) {
-		char *p = header->target_name[i];
-		name_len = strlen(p) + 1;
-		if (bam_is_be) {
-			x = bam_swap_endian_4(name_len);
-			bam_write(fp, &x, 4);
-		} else bam_write(fp, &name_len, 4);
-		bam_write(fp, p, name_len);
-		if (bam_is_be) {
-			x = bam_swap_endian_4(header->target_len[i]);
-			bam_write(fp, &x, 4);
-		} else bam_write(fp, &header->target_len[i], 4);
-	}
-	bgzf_flush(fp);
-	return 0;
-}
-
-static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data)
-{
-	uint8_t *s;
-	uint32_t i, *cigar = (uint32_t*)(data + c->l_qname);
-	s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
-	for (i = 0; i < c->n_cigar; ++i) bam_swap_endian_4p(&cigar[i]);
-	while (s < data + data_len) {
-		uint8_t type;
-		s += 2; // skip key
-		type = toupper(*s); ++s; // skip type
-		if (type == 'C' || type == 'A') ++s;
-		else if (type == 'S') { bam_swap_endian_2p(s); s += 2; }
-		else if (type == 'I' || type == 'F') { bam_swap_endian_4p(s); s += 4; }
-		else if (type == 'D') { bam_swap_endian_8p(s); s += 8; }
-		else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; }
-		else if (type == 'B') {
-			int32_t n, Bsize = bam_aux_type2size(*s);
-			memcpy(&n, s + 1, 4);
-			if (1 == Bsize) {
-			} else if (2 == Bsize) {
-				for (i = 0; i < n; i += 2)
-					bam_swap_endian_2p(s + 5 + i);
-			} else if (4 == Bsize) {
-				for (i = 0; i < n; i += 4)
-					bam_swap_endian_4p(s + 5 + i);
-			}
-			bam_swap_endian_4p(s+1); 
-		}
-	}
-}
+    Copyright (C) 2008-13 Genome Research Ltd.
+    Portions copyright (C) 2009-2012 Broad Institute.
 
-int bam_read1(bamFile fp, bam1_t *b)
-{
-	bam1_core_t *c = &b->core;
-	int32_t block_len, ret, i;
-	uint32_t x[8];
+    Author: Heng Li <lh3 at sanger.ac.uk>
 
-	assert(BAM_CORE_SIZE == 32);
-	if ((ret = bam_read(fp, &block_len, 4)) != 4) {
-		if (ret == 0) return -1; // normal end-of-file
-		else return -2; // truncated
-	}
-	if (bam_read(fp, x, BAM_CORE_SIZE) != BAM_CORE_SIZE) return -3;
-	if (bam_is_be) {
-		bam_swap_endian_4p(&block_len);
-		for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
-	}
-	c->tid = x[0]; c->pos = x[1];
-	c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff;
-	c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff;
-	c->l_qseq = x[4];
-	c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7];
-	b->data_len = block_len - BAM_CORE_SIZE;
-	if (b->m_data < b->data_len) {
-		b->m_data = b->data_len;
-		kroundup32(b->m_data);
-		b->data = (uint8_t*)realloc(b->data, b->m_data);
-	}
-	if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4;
-	b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
-	if (bam_is_be) swap_endian_data(c, b->data_len, b->data);
-	if (bam_no_B) bam_remove_B(b);
-	return 4 + block_len;
-}
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
 
-inline int bam_write1_core(bamFile fp, const bam1_core_t *c, int data_len, uint8_t *data)
-{
-	uint32_t x[8], block_len = data_len + BAM_CORE_SIZE, y;
-	int i;
-	assert(BAM_CORE_SIZE == 32);
-	x[0] = c->tid;
-	x[1] = c->pos;
-	x[2] = (uint32_t)c->bin<<16 | c->qual<<8 | c->l_qname;
-	x[3] = (uint32_t)c->flag<<16 | c->n_cigar;
-	x[4] = c->l_qseq;
-	x[5] = c->mtid;
-	x[6] = c->mpos;
-	x[7] = c->isize;
-	bgzf_flush_try(fp, 4 + block_len);
-	if (bam_is_be) {
-		for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
-		y = block_len;
-		bam_write(fp, bam_swap_endian_4p(&y), 4);
-		swap_endian_data(c, data_len, data);
-	} else bam_write(fp, &block_len, 4);
-	bam_write(fp, x, BAM_CORE_SIZE);
-	bam_write(fp, data, data_len);
-	if (bam_is_be) swap_endian_data(c, data_len, data);
-	return 4 + block_len;
-}
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
 
-int bam_write1(bamFile fp, const bam1_t *b)
-{
-	return bam_write1_core(fp, &b->core, b->data_len, b->data);
-}
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
 
-char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of)
-{
-	uint8_t *s = bam1_seq(b), *t = bam1_qual(b);
-	int i;
-	const bam1_core_t *c = &b->core;
-	kstring_t str;
-	str.l = str.m = 0; str.s = 0;
-
-	kputsn(bam1_qname(b), c->l_qname-1, &str); kputc('\t', &str);
-	if (of == BAM_OFDEC) { kputw(c->flag, &str); kputc('\t', &str); }
-	else if (of == BAM_OFHEX) ksprintf(&str, "0x%x\t", c->flag);
-	else { // BAM_OFSTR
-		for (i = 0; i < 16; ++i)
-			if ((c->flag & 1<<i) && bam_flag2char_table[i])
-				kputc(bam_flag2char_table[i], &str);
-		kputc('\t', &str);
-	}
-	if (c->tid < 0) kputsn("*\t", 2, &str);
-	else {
-		if (header) kputs(header->target_name[c->tid] , &str);
-		else kputw(c->tid, &str);
-		kputc('\t', &str);
-	}
-	kputw(c->pos + 1, &str); kputc('\t', &str); kputw(c->qual, &str); kputc('\t', &str);
-	if (c->n_cigar == 0) kputc('*', &str);
-	else {
-		uint32_t *cigar = bam1_cigar(b);
-		for (i = 0; i < c->n_cigar; ++i) {
-			kputw(bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, &str);
-			kputc(bam_cigar_opchr(cigar[i]), &str);
-		}
-	}
-	kputc('\t', &str);
-	if (c->mtid < 0) kputsn("*\t", 2, &str);
-	else if (c->mtid == c->tid) kputsn("=\t", 2, &str);
-	else {
-		if (header) kputs(header->target_name[c->mtid], &str);
-		else kputw(c->mtid, &str);
-		kputc('\t', &str);
-	}
-	kputw(c->mpos + 1, &str); kputc('\t', &str); kputw(c->isize, &str); kputc('\t', &str);
-	if (c->l_qseq) {
-		for (i = 0; i < c->l_qseq; ++i) kputc(bam_nt16_rev_table[bam1_seqi(s, i)], &str);
-		kputc('\t', &str);
-		if (t[0] == 0xff) kputc('*', &str);
-		else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str);
-	} else kputsn("*\t*", 3, &str);
-	s = bam1_aux(b);
-	while (s < b->data + b->data_len) {
-		uint8_t type, key[2];
-		key[0] = s[0]; key[1] = s[1];
-		s += 2; type = *s; ++s;
-		kputc('\t', &str); kputsn((char*)key, 2, &str); kputc(':', &str);
-		if (type == 'A') { kputsn("A:", 2, &str); kputc(*s, &str); ++s; }
-		else if (type == 'C') { kputsn("i:", 2, &str); kputw(*s, &str); ++s; }
-		else if (type == 'c') { kputsn("i:", 2, &str); kputw(*(int8_t*)s, &str); ++s; }
-		else if (type == 'S') { kputsn("i:", 2, &str); kputw(*(uint16_t*)s, &str); s += 2; }
-		else if (type == 's') { kputsn("i:", 2, &str); kputw(*(int16_t*)s, &str); s += 2; }
-		else if (type == 'I') { kputsn("i:", 2, &str); kputuw(*(uint32_t*)s, &str); s += 4; }
-		else if (type == 'i') { kputsn("i:", 2, &str); kputw(*(int32_t*)s, &str); s += 4; }
-		else if (type == 'f') { ksprintf(&str, "f:%g", *(float*)s); s += 4; }
-		else if (type == 'd') { ksprintf(&str, "d:%lg", *(double*)s); s += 8; }
-		else if (type == 'Z' || type == 'H') { kputc(type, &str); kputc(':', &str); while (*s) kputc(*s++, &str); ++s; }
-		else if (type == 'B') {
-			uint8_t sub_type = *(s++);
-			int32_t n;
-			memcpy(&n, s, 4);
-			s += 4; // no point to the start of the array
-			kputc(type, &str); kputc(':', &str); kputc(sub_type, &str); // write the typing
-			for (i = 0; i < n; ++i) {
-				kputc(',', &str);
-				if ('c' == sub_type || 'c' == sub_type) { kputw(*(int8_t*)s, &str); ++s; }
-				else if ('C' == sub_type) { kputw(*(uint8_t*)s, &str); ++s; }
-				else if ('s' == sub_type) { kputw(*(int16_t*)s, &str); s += 2; }
-				else if ('S' == sub_type) { kputw(*(uint16_t*)s, &str); s += 2; }
-				else if ('i' == sub_type) { kputw(*(int32_t*)s, &str); s += 4; }
-				else if ('I' == sub_type) { kputuw(*(uint32_t*)s, &str); s += 4; }
-				else if ('f' == sub_type) { ksprintf(&str, "%g", *(float*)s); s += 4; }
-			}
-		}
-	}
-	return str.s;
-}
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+#include "bam.h"
+#include "htslib/kstring.h"
+#include "sam_header.h"
 
 char *bam_format1(const bam_header_t *header, const bam1_t *b)
 {
-	return bam_format1_core(header, b, BAM_OFDEC);
+    kstring_t str;
+    str.l = str.m = 0; str.s = NULL;
+    sam_format1(header, b, &str);
+    return str.s;
 }
 
 void bam_view1(const bam_header_t *header, const bam1_t *b)
 {
-	char *s = bam_format1(header, b);
-	puts(s);
-	free(s);
+    char *s = bam_format1(header, b);
+    puts(s);
+    free(s);
 }
 
 int bam_validate1(const bam_header_t *header, const bam1_t *b)
 {
-	char *s;
+    char *s;
 
-	if (b->core.tid < -1 || b->core.mtid < -1) return 0;
-	if (header && (b->core.tid >= header->n_targets || b->core.mtid >= header->n_targets)) return 0;
+    if (b->core.tid < -1 || b->core.mtid < -1) return 0;
+    if (header && (b->core.tid >= header->n_targets || b->core.mtid >= header->n_targets)) return 0;
 
-	if (b->data_len < b->core.l_qname) return 0;
-	s = memchr(bam1_qname(b), '\0', b->core.l_qname);
-	if (s != &bam1_qname(b)[b->core.l_qname-1]) return 0;
+    if (b->data_len < b->core.l_qname) return 0;
+    s = memchr(bam1_qname(b), '\0', b->core.l_qname);
+    if (s != &bam1_qname(b)[b->core.l_qname-1]) return 0;
 
-	// FIXME: Other fields could also be checked, especially the auxiliary data
+    // FIXME: Other fields could also be checked, especially the auxiliary data
 
-	return 1;
+    return 1;
 }
 
 // FIXME: we should also check the LB tag associated with each alignment
 const char *bam_get_library(bam_header_t *h, const bam1_t *b)
 {
-	const uint8_t *rg;
-	if (h->dict == 0) h->dict = sam_header_parse2(h->text);
-	if (h->rg2lib == 0) h->rg2lib = sam_header2tbl(h->dict, "RG", "ID", "LB");
-	rg = bam_aux_get(b, "RG");
-	return (rg == 0)? 0 : sam_tbl_get(h->rg2lib, (const char*)(rg + 1));
+#if 0
+    const uint8_t *rg;
+    if (h->dict == 0) h->dict = sam_header_parse2(h->text);
+    if (h->rg2lib == 0) h->rg2lib = sam_header2tbl(h->dict, "RG", "ID", "LB");
+    rg = bam_aux_get(b, "RG");
+    return (rg == 0)? 0 : sam_tbl_get(h->rg2lib, (const char*)(rg + 1));
+#else
+    fprintf(pysamerr, "Samtools-htslib-API: bam_get_library() not yet implemented\n");
+    abort();
+#endif
+}
+
+int bam_fetch(bamFile fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func)
+{
+    int ret;
+    bam_iter_t iter;
+    bam1_t *b;
+    b = bam_init1();
+    iter = bam_iter_query(idx, tid, beg, end);
+    while ((ret = bam_iter_read(fp, iter, b)) >= 0) func(b, data);
+    bam_iter_destroy(iter);
+    bam_destroy1(b);
+    return (ret == -1)? 0 : ret;
 }
 
 /************
  * Remove B *
  ************/
 
+#define bam1_seq_seti(s, i, c) ( (s)[(i)>>1] = ((s)[(i)>>1] & 0xf<<(((i)&1)<<2)) | (c)<<((~(i)&1)<<2) )
+
 int bam_remove_B(bam1_t *b)
 {
-	int i, j, end_j, k, l, no_qual;
-	uint32_t *cigar, *new_cigar;
-	uint8_t *seq, *qual, *p;
-	// test if removal is necessary
-	if (b->core.flag & BAM_FUNMAP) return 0; // unmapped; do nothing
-	cigar = bam1_cigar(b);
-	for (k = 0; k < b->core.n_cigar; ++k)
-		if (bam_cigar_op(cigar[k]) == BAM_CBACK) break;
-	if (k == b->core.n_cigar) return 0; // no 'B'
-	if (bam_cigar_op(cigar[0]) == BAM_CBACK) goto rmB_err; // cannot be removed
-	// allocate memory for the new CIGAR
-	if (b->data_len + (b->core.n_cigar + 1) * 4 > b->m_data) { // not enough memory
-		b->m_data = b->data_len + b->core.n_cigar * 4;
-		kroundup32(b->m_data);
-		b->data = (uint8_t*)realloc(b->data, b->m_data);
-		cigar = bam1_cigar(b); // after realloc, cigar may be changed
-	}
-	new_cigar = (uint32_t*)(b->data + (b->m_data - b->core.n_cigar * 4)); // from the end of b->data
-	// the core loop
-	seq = bam1_seq(b); qual = bam1_qual(b);
-	no_qual = (qual[0] == 0xff); // test whether base quality is available
-	i = j = 0; end_j = -1;
-	for (k = l = 0; k < b->core.n_cigar; ++k) {
-		int op  = bam_cigar_op(cigar[k]);
-		int len = bam_cigar_oplen(cigar[k]);
-		if (op == BAM_CBACK) { // the backward operation
-			int t, u;
-			if (k == b->core.n_cigar - 1) break; // ignore 'B' at the end of CIGAR
-			if (len > j) goto rmB_err; // an excessively long backward
-			for (t = l - 1, u = 0; t >= 0; --t) { // look back
-				int op1  = bam_cigar_op(new_cigar[t]);
-				int len1 = bam_cigar_oplen(new_cigar[t]);
-				if (bam_cigar_type(op1)&1) { // consume the query
-					if (u + len1 >= len) { // stop
-						new_cigar[t] -= (len - u) << BAM_CIGAR_SHIFT;
-						break;
-					} else u += len1;
-				}
-			}
-			if (bam_cigar_oplen(new_cigar[t]) == 0) --t; // squeeze out the zero-length operation
-			l = t + 1;
-			end_j = j; j -= len;
-		} else { // other CIGAR operations
-			new_cigar[l++] = cigar[k];
-			if (bam_cigar_type(op)&1) { // consume the query
-				if (i != j) { // no need to copy if i == j
-					int u, c, c0;
-					for (u = 0; u < len; ++u) { // construct the consensus
-						c = bam1_seqi(seq, i+u);
-						if (j + u < end_j) { // in an overlap
-							c0 = bam1_seqi(seq, j+u);
-							if (c != c0) { // a mismatch; choose the better base
-								if (qual[j+u] < qual[i+u]) { // the base in the 2nd segment is better
-									bam1_seq_seti(seq, j+u, c);
-									qual[j+u] = qual[i+u] - qual[j+u];
-								} else qual[j+u] -= qual[i+u]; // the 1st is better; reduce base quality
-							} else qual[j+u] = qual[j+u] > qual[i+u]? qual[j+u] : qual[i+u];
-						} else { // not in an overlap; copy over
-							bam1_seq_seti(seq, j+u, c);
-							qual[j+u] = qual[i+u];
-						}
-					}
-				}
-				i += len, j += len;
-			}
-		}
-	}
-	if (no_qual) qual[0] = 0xff; // in very rare cases, this may be modified
-	// merge adjacent operations if possible
-	for (k = 1; k < l; ++k)
-		if (bam_cigar_op(new_cigar[k]) == bam_cigar_op(new_cigar[k-1]))
-			new_cigar[k] += new_cigar[k-1] >> BAM_CIGAR_SHIFT << BAM_CIGAR_SHIFT, new_cigar[k-1] &= 0xf;
-	// kill zero length operations
-	for (k = i = 0; k < l; ++k)
-		if (new_cigar[k] >> BAM_CIGAR_SHIFT)
-			new_cigar[i++] = new_cigar[k];
-	l = i;
-	// update b
-	memcpy(cigar, new_cigar, l * 4); // set CIGAR
-	p = b->data + b->core.l_qname + l * 4;
-	memmove(p, seq, (j+1)>>1); p += (j+1)>>1; // set SEQ
-	memmove(p, qual, j); p += j; // set QUAL
-	memmove(p, bam1_aux(b), b->l_aux); p += b->l_aux; // set optional fields
-	b->core.n_cigar = l, b->core.l_qseq = j; // update CIGAR length and query length
-	b->data_len = p - b->data; // update record length
-	return 0;
+    int i, j, end_j, k, l, no_qual;
+    uint32_t *cigar, *new_cigar;
+    uint8_t *seq, *qual, *p;
+    // test if removal is necessary
+    if (b->core.flag & BAM_FUNMAP) return 0; // unmapped; do nothing
+    cigar = bam1_cigar(b);
+    for (k = 0; k < b->core.n_cigar; ++k)
+        if (bam_cigar_op(cigar[k]) == BAM_CBACK) break;
+    if (k == b->core.n_cigar) return 0; // no 'B'
+    if (bam_cigar_op(cigar[0]) == BAM_CBACK) goto rmB_err; // cannot be removed
+    // allocate memory for the new CIGAR
+    if (b->data_len + (b->core.n_cigar + 1) * 4 > b->m_data) { // not enough memory
+        b->m_data = b->data_len + b->core.n_cigar * 4;
+        kroundup32(b->m_data);
+        b->data = (uint8_t*)realloc(b->data, b->m_data);
+        cigar = bam1_cigar(b); // after realloc, cigar may be changed
+    }
+    new_cigar = (uint32_t*)(b->data + (b->m_data - b->core.n_cigar * 4)); // from the end of b->data
+    // the core loop
+    seq = bam1_seq(b); qual = bam1_qual(b);
+    no_qual = (qual[0] == 0xff); // test whether base quality is available
+    i = j = 0; end_j = -1;
+    for (k = l = 0; k < b->core.n_cigar; ++k) {
+        int op  = bam_cigar_op(cigar[k]);
+        int len = bam_cigar_oplen(cigar[k]);
+        if (op == BAM_CBACK) { // the backward operation
+            int t, u;
+            if (k == b->core.n_cigar - 1) break; // ignore 'B' at the end of CIGAR
+            if (len > j) goto rmB_err; // an excessively long backward
+            for (t = l - 1, u = 0; t >= 0; --t) { // look back
+                int op1  = bam_cigar_op(new_cigar[t]);
+                int len1 = bam_cigar_oplen(new_cigar[t]);
+                if (bam_cigar_type(op1)&1) { // consume the query
+                    if (u + len1 >= len) { // stop
+                        new_cigar[t] -= (len - u) << BAM_CIGAR_SHIFT;
+                        break;
+                    } else u += len1;
+                }
+            }
+            if (bam_cigar_oplen(new_cigar[t]) == 0) --t; // squeeze out the zero-length operation
+            l = t + 1;
+            end_j = j; j -= len;
+        } else { // other CIGAR operations
+            new_cigar[l++] = cigar[k];
+            if (bam_cigar_type(op)&1) { // consume the query
+                if (i != j) { // no need to copy if i == j
+                    int u, c, c0;
+                    for (u = 0; u < len; ++u) { // construct the consensus
+                        c = bam1_seqi(seq, i+u);
+                        if (j + u < end_j) { // in an overlap
+                            c0 = bam1_seqi(seq, j+u);
+                            if (c != c0) { // a mismatch; choose the better base
+                                if (qual[j+u] < qual[i+u]) { // the base in the 2nd segment is better
+                                    bam1_seq_seti(seq, j+u, c);
+                                    qual[j+u] = qual[i+u] - qual[j+u];
+                                } else qual[j+u] -= qual[i+u]; // the 1st is better; reduce base quality
+                            } else qual[j+u] = qual[j+u] > qual[i+u]? qual[j+u] : qual[i+u];
+                        } else { // not in an overlap; copy over
+                            bam1_seq_seti(seq, j+u, c);
+                            qual[j+u] = qual[i+u];
+                        }
+                    }
+                }
+                i += len, j += len;
+            }
+        }
+    }
+    if (no_qual) qual[0] = 0xff; // in very rare cases, this may be modified
+    // merge adjacent operations if possible
+    for (k = 1; k < l; ++k)
+        if (bam_cigar_op(new_cigar[k]) == bam_cigar_op(new_cigar[k-1]))
+            new_cigar[k] += new_cigar[k-1] >> BAM_CIGAR_SHIFT << BAM_CIGAR_SHIFT, new_cigar[k-1] &= 0xf;
+    // kill zero length operations
+    for (k = i = 0; k < l; ++k)
+        if (new_cigar[k] >> BAM_CIGAR_SHIFT)
+            new_cigar[i++] = new_cigar[k];
+    l = i;
+    // update b
+    memcpy(cigar, new_cigar, l * 4); // set CIGAR
+    p = b->data + b->core.l_qname + l * 4;
+    memmove(p, seq, (j+1)>>1); p += (j+1)>>1; // set SEQ
+    memmove(p, qual, j); p += j; // set QUAL
+    memmove(p, bam1_aux(b), bam_get_l_aux(b)); p += bam_get_l_aux(b); // set optional fields
+    b->core.n_cigar = l, b->core.l_qseq = j; // update CIGAR length and query length
+    b->data_len = p - b->data; // update record length
+    return 0;
 
 rmB_err:
-	b->core.flag |= BAM_FUNMAP;
-	return -1;
+    b->core.flag |= BAM_FUNMAP;
+    return -1;
 }
diff --git a/samtools/bam.h b/samtools/bam.h
index a383378..e822331 100644
--- a/samtools/bam.h
+++ b/samtools/bam.h
@@ -1,29 +1,27 @@
-/* The MIT License
+/*  bam.h -- BAM API.
 
-   Copyright (c) 2008-2010 Genome Research Ltd (GRL).
+    Copyright (C) 2008-2014 Genome Research Ltd.
+    Portions copyright (C) 2010-2012 Broad Institute.
 
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
+    Author: Heng Li <lh3 at sanger.ac.uk>
 
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
 
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
 
-/* Contact: Heng Li <lh3 at sanger.ac.uk> */
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
 
 #ifndef BAM_BAM_H
 #define BAM_BAM_H
@@ -40,35 +38,23 @@
   @copyright Genome Research Ltd.
  */
 
-#define BAM_VERSION "0.1.19-44428cd"
+#define BAM_VERSION "1.1"
 
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 
-#ifndef BAM_LITE
-#define BAM_VIRTUAL_OFFSET16
-#include "bgzf.h"
+#include "htslib/bgzf.h"
+#include "htslib/sam.h"
+
 /*! @abstract BAM file handler */
 typedef BGZF *bamFile;
 #define bam_open(fn, mode) bgzf_open(fn, mode)
 #define bam_dopen(fd, mode) bgzf_fdopen(fd, mode)
 #define bam_close(fp) bgzf_close(fp)
-#define bam_read(fp, buf, size) bgzf_read(fp, buf, size)
-#define bam_write(fp, buf, size) bgzf_write(fp, buf, size)
 #define bam_tell(fp) bgzf_tell(fp)
 #define bam_seek(fp, pos, dir) bgzf_seek(fp, pos, dir)
-#else
-#define BAM_TRUE_OFFSET
-#include <zlib.h>
-typedef gzFile bamFile;
-#define bam_open(fn, mode) gzopen(fn, mode)
-#define bam_dopen(fd, mode) gzdopen(fd, mode)
-#define bam_close(fp) gzclose(fp)
-#define bam_read(fp, buf, size) gzread(fp, buf, size)
-/* no bam_write/bam_tell/bam_seek() here */
-#endif
 
 /*! @typedef
   @abstract Structure for the alignment header.
@@ -84,38 +70,9 @@ typedef gzFile bamFile;
   @discussion Field hash points to null by default. It is a private
   member.
  */
-typedef struct {
-	int32_t n_targets;
-	char **target_name;
-	uint32_t *target_len;
-	void *dict, *hash, *rg2lib;
-	uint32_t l_text, n_text;
-	char *text;
-} bam_header_t;
-
-/*! @abstract the read is paired in sequencing, no matter whether it is mapped in a pair */
-#define BAM_FPAIRED        1
-/*! @abstract the read is mapped in a proper pair */
-#define BAM_FPROPER_PAIR   2
-/*! @abstract the read itself is unmapped; conflictive with BAM_FPROPER_PAIR */
-#define BAM_FUNMAP         4
-/*! @abstract the mate is unmapped */
-#define BAM_FMUNMAP        8
-/*! @abstract the read is mapped to the reverse strand */
-#define BAM_FREVERSE      16
-/*! @abstract the mate is mapped to the reverse strand */
-#define BAM_FMREVERSE     32
-/*! @abstract this is read1 */
-#define BAM_FREAD1        64
-/*! @abstract this is read2 */
-#define BAM_FREAD2       128
-/*! @abstract not primary alignment */
-#define BAM_FSECONDARY   256
-/*! @abstract QC failure */
-#define BAM_FQCFAIL      512
-/*! @abstract optical or PCR duplicate */
-#define BAM_FDUP        1024
+typedef bam_hdr_t bam_header_t;
 
+// TODO This flag-formatting functionality does not currently exist in htslib
 #define BAM_OFDEC          0
 #define BAM_OFHEX          1
 #define BAM_OFSTR          2
@@ -123,47 +80,6 @@ typedef struct {
 /*! @abstract defautl mask for pileup */
 #define BAM_DEF_MASK (BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP)
 
-#define BAM_CORE_SIZE   sizeof(bam1_core_t)
-
-/**
- * Describing how CIGAR operation/length is packed in a 32-bit integer.
- */
-#define BAM_CIGAR_SHIFT 4
-#define BAM_CIGAR_MASK  ((1 << BAM_CIGAR_SHIFT) - 1)
-
-/*
-  CIGAR operations.
- */
-/*! @abstract CIGAR: M = match or mismatch*/
-#define BAM_CMATCH      0
-/*! @abstract CIGAR: I = insertion to the reference */
-#define BAM_CINS        1
-/*! @abstract CIGAR: D = deletion from the reference */
-#define BAM_CDEL        2
-/*! @abstract CIGAR: N = skip on the reference (e.g. spliced alignment) */
-#define BAM_CREF_SKIP   3
-/*! @abstract CIGAR: S = clip on the read with clipped sequence
-  present in qseq */
-#define BAM_CSOFT_CLIP  4
-/*! @abstract CIGAR: H = clip on the read with clipped sequence trimmed off */
-#define BAM_CHARD_CLIP  5
-/*! @abstract CIGAR: P = padding */
-#define BAM_CPAD        6
-/*! @abstract CIGAR: equals = match */
-#define BAM_CEQUAL      7
-/*! @abstract CIGAR: X = mismatch */
-#define BAM_CDIFF       8
-#define BAM_CBACK       9
-
-#define BAM_CIGAR_STR  "MIDNSHP=XB"
-#define BAM_CIGAR_TYPE 0x3C1A7
-
-#define bam_cigar_op(c) ((c)&BAM_CIGAR_MASK)
-#define bam_cigar_oplen(c) ((c)>>BAM_CIGAR_SHIFT)
-#define bam_cigar_opchr(c) (BAM_CIGAR_STR[bam_cigar_op(c)])
-#define bam_cigar_gen(l, o) ((l)<<BAM_CIGAR_SHIFT|(o))
-#define bam_cigar_type(o) (BAM_CIGAR_TYPE>>((o)<<1)&3) // bit 1: consume query; bit 2: consume reference
-
 /*! @typedef
   @abstract Structure for core alignment information.
   @field  tid     chromosome ID, defined by bam_header_t
@@ -175,16 +91,7 @@ typedef struct {
   @field  n_cigar number of CIGAR operations
   @field  l_qseq  length of the query sequence (read)
  */
-typedef struct {
-	int32_t tid;
-	int32_t pos;
-	uint32_t bin:16, qual:8, l_qname:8;
-	uint32_t flag:16, n_cigar:16;
-	int32_t l_qseq;
-	int32_t mtid;
-	int32_t mpos;
-	int32_t isize;
-} bam1_core_t;
+// typedef struct { ... } bam1_core_t;
 
 /*! @typedef
   @abstract Structure for one alignment.
@@ -195,23 +102,24 @@ typedef struct {
   @field  data       all variable-length data, concatenated; structure: qname-cigar-seq-qual-aux
 
   @discussion Notes:
- 
+
    1. qname is zero tailing and core.l_qname includes the tailing '\0'.
    2. l_qseq is calculated from the total length of an alignment block
       on reading or from CIGAR.
    3. cigar data is encoded 4 bytes per CIGAR operation.
    4. seq is nybble-encoded according to bam_nt16_table.
  */
-typedef struct {
-	bam1_core_t core;
-	int l_aux, data_len, m_data;
-	uint8_t *data;
-} bam1_t;
+// typedef struct { ... } bam1_t;
+// NOTE htslib version doesn't have l_aux; use bam_get_l_aux(b) instead
+#ifndef SAMTOOLS_HTSLIB_SUPPRESS_HACKS
+// NOTE htslib also renames data_len to l_data; this macro may help or hinder
+#define data_len l_data
+#endif
 
-typedef struct __bam_iter_t *bam_iter_t;
+typedef hts_itr_t *bam_iter_t;
 
-#define bam1_strand(b) (((b)->core.flag&BAM_FREVERSE) != 0)
-#define bam1_mstrand(b) (((b)->core.flag&BAM_FMREVERSE) != 0)
+#define bam1_strand(b) (bam_is_rev((b)))
+#define bam1_mstrand(b) (bam_is_mrev((b)))
 
 /*! @function
   @abstract  Get the CIGAR array
@@ -222,14 +130,14 @@ typedef struct __bam_iter_t *bam_iter_t;
   lower 4 bits gives a CIGAR operation and the higher 28 bits keep the
   length of a CIGAR.
  */
-#define bam1_cigar(b) ((uint32_t*)((b)->data + (b)->core.l_qname))
+#define bam1_cigar(b) (bam_get_cigar((b)))
 
 /*! @function
   @abstract  Get the name of the query
   @param  b  pointer to an alignment
   @return    pointer to the name string, null terminated
  */
-#define bam1_qname(b) ((char*)((b)->data))
+#define bam1_qname(b) (bam_get_qname((b)))
 
 /*! @function
   @abstract  Get query sequence
@@ -241,14 +149,14 @@ typedef struct __bam_iter_t *bam_iter_t;
   at the higher 4 bits having smaller coordinate on the read. It is
   recommended to use bam1_seqi() macro to get the base.
  */
-#define bam1_seq(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname)
+#define bam1_seq(b) (bam_get_seq((b)))
 
 /*! @function
   @abstract  Get query quality
   @param  b  pointer to an alignment
   @return    pointer to quality string
  */
-#define bam1_qual(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname + (((b)->core.l_qseq + 1)>>1))
+#define bam1_qual(b) (bam_get_qual((b)))
 
 /*! @function
   @abstract  Get a base on read
@@ -256,459 +164,382 @@ typedef struct __bam_iter_t *bam_iter_t;
   @param  i  The i-th position, 0-based
   @return    4-bit integer representing the base.
  */
-//#define bam1_seqi(s, i) ((s)[(i)/2] >> 4*(1-(i)%2) & 0xf)
-#define bam1_seqi(s, i) ((s)[(i)>>1] >> ((~(i)&1)<<2) & 0xf)
-
-#define bam1_seq_seti(s, i, c) ( (s)[(i)>>1] = ((s)[(i)>>1] & 0xf<<(((i)&1)<<2)) | (c)<<((~(i)&1)<<2) )
+#define bam1_seqi(s, i) (bam_seqi((s), (i)))
 
 /*! @function
-  @abstract  Get query sequence and quality
+  @abstract  Get auxiliary data
   @param  b  pointer to an alignment
   @return    pointer to the concatenated auxiliary data
  */
-#define bam1_aux(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname + (b)->core.l_qseq + ((b)->core.l_qseq + 1)/2)
-
-#ifndef kroundup32
-/*! @function
-  @abstract  Round an integer to the next closest power-2 integer.
-  @param  x  integer to be rounded (in place)
-  @discussion x will be modified.
- */
-#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
-#endif
-
-/*!
-  @abstract Whether the machine is big-endian; modified only in
-  bam_header_init().
- */
-extern int bam_is_be;
+#define bam1_aux(b) (bam_get_aux((b)))
 
 /*!
   @abstract Verbose level between 0 and 3; 0 is supposed to disable all
   debugging information, though this may not have been implemented.
  */
-extern int bam_verbose;
-
-extern int bam_no_B;
+#define bam_verbose hts_verbose
 
 /*! @abstract Table for converting a nucleotide character to the 4-bit encoding. */
-extern unsigned char bam_nt16_table[256];
+#define bam_nt16_table seq_nt16_table
 
 /*! @abstract Table for converting a 4-bit encoded nucleotide to a letter. */
-extern char *bam_nt16_rev_table;
-
-extern char bam_nt16_nt4_table[];
+#define bam_nt16_rev_table seq_nt16_str
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-	/*********************
-	 * Low-level SAM I/O *
-	 *********************/
-
-	/*! @abstract TAM file handler */
-	typedef struct __tamFile_t *tamFile;
-
-	/*!
-	  @abstract   Open a SAM file for reading, either uncompressed or compressed by gzip/zlib.
-	  @param  fn  SAM file name
-	  @return     SAM file handler
-	 */
-	tamFile sam_open(const char *fn);
-
-	/*!
-	  @abstract   Close a SAM file handler
-	  @param  fp  SAM file handler
-	 */
-	void sam_close(tamFile fp);
-
-	/*!
-	  @abstract      Read one alignment from a SAM file handler
-	  @param  fp     SAM file handler
-	  @param  header header information (ordered names of chromosomes)
-	  @param  b      read alignment; all members in b will be updated
-	  @return        0 if successful; otherwise negative
-	 */
-	int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b);
-
-	/*!
-	  @abstract       Read header information from a TAB-delimited list file.
-	  @param  fn_list file name for the list
-	  @return         a pointer to the header structure
-
-	  @discussion Each line in this file consists of chromosome name and
-	  the length of chromosome.
-	 */
-	bam_header_t *sam_header_read2(const char *fn_list);
-
-	/*!
-	  @abstract       Read header from a SAM file (if present)
-	  @param  fp      SAM file handler
-	  @return         pointer to header struct; 0 if no @SQ lines available
-	 */
-	bam_header_t *sam_header_read(tamFile fp);
-
-	/*!
-	  @abstract       Parse @SQ lines a update a header struct
-	  @param  h       pointer to the header struct to be updated
-	  @return         number of target sequences
-
-	  @discussion bam_header_t::{n_targets,target_len,target_name} will
-	  be destroyed in the first place.
-	 */
-	int sam_header_parse(bam_header_t *h);
-	int32_t bam_get_tid(const bam_header_t *header, const char *seq_name);
-
-	/*!
-	  @abstract       Parse @RG lines a update a header struct
-	  @param  h       pointer to the header struct to be updated
-	  @return         number of @RG lines
-
-	  @discussion bam_header_t::rg2lib will be destroyed in the first
-	  place.
-	 */
-	int sam_header_parse_rg(bam_header_t *h);
-
-#define sam_write1(header, b) bam_view1(header, b)
-
-
-	/********************************
-	 * APIs for string dictionaries *
-	 ********************************/
-
-	int bam_strmap_put(void *strmap, const char *rg, const char *lib);
-	const char *bam_strmap_get(const void *strmap, const char *rg);
-	void *bam_strmap_dup(const void*);
-	void *bam_strmap_init(void);
-	void bam_strmap_destroy(void *strmap);
-
-
-	/*********************
-	 * Low-level BAM I/O *
-	 *********************/
-
-	/*!
-	  @abstract Initialize a header structure.
-	  @return   the pointer to the header structure
-
-	  @discussion This function also modifies the global variable
-	  bam_is_be.
-	 */
-	bam_header_t *bam_header_init(void);
-
-	/*!
-	  @abstract        Destroy a header structure.
-	  @param  header  pointer to the header
-	 */
-	void bam_header_destroy(bam_header_t *header);
-
-	/*!
-	  @abstract   Read a header structure from BAM.
-	  @param  fp  BAM file handler, opened by bam_open()
-	  @return     pointer to the header structure
-
-	  @discussion The file position indicator must be placed at the
-	  beginning of the file. Upon success, the position indicator will
-	  be set at the start of the first alignment.
-	 */
-	bam_header_t *bam_header_read(bamFile fp);
-
-	/*!
-	  @abstract      Write a header structure to BAM.
-	  @param  fp     BAM file handler
-	  @param  header pointer to the header structure
-	  @return        always 0 currently
-	 */
-	int bam_header_write(bamFile fp, const bam_header_t *header);
-
-	/*!
-	  @abstract   Read an alignment from BAM.
-	  @param  fp  BAM file handler
-	  @param  b   read alignment; all members are updated.
-	  @return     number of bytes read from the file
-
-	  @discussion The file position indicator must be
-	  placed right before an alignment. Upon success, this function
-	  will set the position indicator to the start of the next
-	  alignment. This function is not affected by the machine
-	  endianness.
-	 */
-	int bam_read1(bamFile fp, bam1_t *b);
-
-	int bam_remove_B(bam1_t *b);
-
-	/*!
-	  @abstract Write an alignment to BAM.
-	  @param  fp       BAM file handler
-	  @param  c        pointer to the bam1_core_t structure
-	  @param  data_len total length of variable size data related to
-	                   the alignment
-	  @param  data     pointer to the concatenated data
-	  @return          number of bytes written to the file
-
-	  @discussion This function is not affected by the machine
-	  endianness.
-	 */
-	int bam_write1_core(bamFile fp, const bam1_core_t *c, int data_len, uint8_t *data);
-
-	/*!
-	  @abstract   Write an alignment to BAM.
-	  @param  fp  BAM file handler
-	  @param  b   alignment to write
-	  @return     number of bytes written to the file
-
-	  @abstract It is equivalent to:
-	    bam_write1_core(fp, &b->core, b->data_len, b->data)
-	 */
-	int bam_write1(bamFile fp, const bam1_t *b);
-
-	/*! @function
-	  @abstract  Initiate a pointer to bam1_t struct
-	 */
-#define bam_init1() ((bam1_t*)calloc(1, sizeof(bam1_t)))
-
-	/*! @function
-	  @abstract  Free the memory allocated for an alignment.
-	  @param  b  pointer to an alignment
-	 */
-#define bam_destroy1(b) do {					\
-		if (b) { free((b)->data); free(b); }	\
-	} while (0)
-
-	/*!
-	  @abstract       Format a BAM record in the SAM format
-	  @param  header  pointer to the header structure
-	  @param  b       alignment to print
-	  @return         a pointer to the SAM string
-	 */
-	char *bam_format1(const bam_header_t *header, const bam1_t *b);
-
-	char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of);
-
-	/*!
-	  @abstract       Check whether a BAM record is plausibly valid
-	  @param  header  associated header structure, or NULL if unavailable
-	  @param  b       alignment to validate
-	  @return         0 if the alignment is invalid; non-zero otherwise
-
-	  @discussion  Simple consistency check of some of the fields of the
-	  alignment record.  If the header is provided, several additional checks
-	  are made.  Not all fields are checked, so a non-zero result is not a
-	  guarantee that the record is valid.  However it is usually good enough
-	  to detect when bam_seek() has been called with a virtual file offset
-	  that is not the offset of an alignment record.
-	 */
-	int bam_validate1(const bam_header_t *header, const bam1_t *b);
-
-	const char *bam_get_library(bam_header_t *header, const bam1_t *b);
-
-
-	/***************
-	 * pileup APIs *
-	 ***************/
-
-	/*! @typedef
-	  @abstract Structure for one alignment covering the pileup position.
-	  @field  b      pointer to the alignment
-	  @field  qpos   position of the read base at the pileup site, 0-based
-	  @field  indel  indel length; 0 for no indel, positive for ins and negative for del
-	  @field  is_del 1 iff the base on the padded read is a deletion
-	  @field  level  the level of the read in the "viewer" mode
-
-	  @discussion See also bam_plbuf_push() and bam_lplbuf_push(). The
-	  difference between the two functions is that the former does not
-	  set bam_pileup1_t::level, while the later does. Level helps the
-	  implementation of alignment viewers, but calculating this has some
-	  overhead.
-	 */
-	typedef struct {
-		bam1_t *b;
-		int32_t qpos;
-		int indel, level;
-		uint32_t is_del:1, is_head:1, is_tail:1, is_refskip:1, aux:28;
-	} bam_pileup1_t;
-
-	typedef int (*bam_plp_auto_f)(void *data, bam1_t *b);
-
-	struct __bam_plp_t;
-	typedef struct __bam_plp_t *bam_plp_t;
-
-	bam_plp_t bam_plp_init(bam_plp_auto_f func, void *data);
-	int bam_plp_push(bam_plp_t iter, const bam1_t *b);
-	const bam_pileup1_t *bam_plp_next(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp);
-	const bam_pileup1_t *bam_plp_auto(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp);
-	void bam_plp_set_mask(bam_plp_t iter, int mask);
-	void bam_plp_set_maxcnt(bam_plp_t iter, int maxcnt);
-	void bam_plp_reset(bam_plp_t iter);
-	void bam_plp_destroy(bam_plp_t iter);
-
-	struct __bam_mplp_t;
-	typedef struct __bam_mplp_t *bam_mplp_t;
-
-	bam_mplp_t bam_mplp_init(int n, bam_plp_auto_f func, void **data);
-	void bam_mplp_destroy(bam_mplp_t iter);
-	void bam_mplp_set_maxcnt(bam_mplp_t iter, int maxcnt);
-	int bam_mplp_auto(bam_mplp_t iter, int *_tid, int *_pos, int *n_plp, const bam_pileup1_t **plp);
-
-	/*! @typedef
-	  @abstract    Type of function to be called by bam_plbuf_push().
-	  @param  tid  chromosome ID as is defined in the header
-	  @param  pos  start coordinate of the alignment, 0-based
-	  @param  n    number of elements in pl array
-	  @param  pl   array of alignments
-	  @param  data user provided data
-	  @discussion  See also bam_plbuf_push(), bam_plbuf_init() and bam_pileup1_t.
-	 */
-	typedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data);
-
-	typedef struct {
-		bam_plp_t iter;
-		bam_pileup_f func;
-		void *data;
-	} bam_plbuf_t;
-
-	void bam_plbuf_set_mask(bam_plbuf_t *buf, int mask);
-	void bam_plbuf_reset(bam_plbuf_t *buf);
-	bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data);
-	void bam_plbuf_destroy(bam_plbuf_t *buf);
-	int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf);
-
-	int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data);
-
-	struct __bam_lplbuf_t;
-	typedef struct __bam_lplbuf_t bam_lplbuf_t;
-
-	void bam_lplbuf_reset(bam_lplbuf_t *buf);
-
-	/*! @abstract  bam_plbuf_init() equivalent with level calculated. */
-	bam_lplbuf_t *bam_lplbuf_init(bam_pileup_f func, void *data);
-
-	/*! @abstract  bam_plbuf_destroy() equivalent with level calculated. */
-	void bam_lplbuf_destroy(bam_lplbuf_t *tv);
-
-	/*! @abstract  bam_plbuf_push() equivalent with level calculated. */
-	int bam_lplbuf_push(const bam1_t *b, bam_lplbuf_t *buf);
-
-
-	/*********************
-	 * BAM indexing APIs *
-	 *********************/
-
-	struct __bam_index_t;
-	typedef struct __bam_index_t bam_index_t;
-
-	/*!
-	  @abstract   Build index for a BAM file.
-	  @discussion Index file "fn.bai" will be created.
-	  @param  fn  name of the BAM file
-	  @return     always 0 currently
-	 */
-	int bam_index_build(const char *fn);
-
-	/*!
-	  @abstract   Load index from file "fn.bai".
-	  @param  fn  name of the BAM file (NOT the index file)
-	  @return     pointer to the index structure
-	 */
-	bam_index_t *bam_index_load(const char *fn);
-
-	/*!
-	  @abstract    Destroy an index structure.
-	  @param  idx  pointer to the index structure
-	 */
-	void bam_index_destroy(bam_index_t *idx);
-
-	/*! @typedef
-	  @abstract      Type of function to be called by bam_fetch().
-	  @param  b     the alignment
-	  @param  data  user provided data
-	 */
-	typedef int (*bam_fetch_f)(const bam1_t *b, void *data);
-
-	/*!
-	  @abstract Retrieve the alignments that are overlapped with the
-	  specified region.
-
-	  @discussion A user defined function will be called for each
-	  retrieved alignment ordered by its start position.
-
-	  @param  fp    BAM file handler
-	  @param  idx   pointer to the alignment index
-	  @param  tid   chromosome ID as is defined in the header
-	  @param  beg   start coordinate, 0-based
-	  @param  end   end coordinate, 0-based
-	  @param  data  user provided data (will be transferred to func)
-	  @param  func  user defined function
-	 */
-	int bam_fetch(bamFile fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func);
-
-	bam_iter_t bam_iter_query(const bam_index_t *idx, int tid, int beg, int end);
-	int bam_iter_read(bamFile fp, bam_iter_t iter, bam1_t *b);
-	void bam_iter_destroy(bam_iter_t iter);
-
-	/*!
-	  @abstract       Parse a region in the format: "chr2:100,000-200,000".
-	  @discussion     bam_header_t::hash will be initialized if empty.
-	  @param  header  pointer to the header structure
-	  @param  str     string to be parsed
-	  @param  ref_id  the returned chromosome ID
-	  @param  begin   the returned start coordinate
-	  @param  end     the returned end coordinate
-	  @return         0 on success; -1 on failure
-	 */
-	int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end);
-
-
-	/**************************
-	 * APIs for optional tags *
-	 **************************/
-
-	/*!
-	  @abstract       Retrieve data of a tag
-	  @param  b       pointer to an alignment struct
-	  @param  tag     two-character tag to be retrieved
-
-	  @return  pointer to the type and data. The first character is the
-	  type that can be 'iIsScCdfAZH'.
-
-	  @discussion  Use bam_aux2?() series to convert the returned data to
-	  the corresponding type.
-	*/
-	uint8_t *bam_aux_get(const bam1_t *b, const char tag[2]);
-
-	int32_t bam_aux2i(const uint8_t *s);
-	float bam_aux2f(const uint8_t *s);
-	double bam_aux2d(const uint8_t *s);
-	char bam_aux2A(const uint8_t *s);
-	char *bam_aux2Z(const uint8_t *s);
-
-	int bam_aux_del(bam1_t *b, uint8_t *s);
-	void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data);
-	uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2]); // an alias of bam_aux_get()
-
-
-	/*****************
-	 * Miscellaneous *
-	 *****************/
-
-	/*!  
-	  @abstract Calculate the rightmost coordinate of an alignment on the
-	  reference genome.
-
-	  @param  c      pointer to the bam1_core_t structure
-	  @param  cigar  the corresponding CIGAR array (from bam1_t::cigar)
-	  @return        the rightmost coordinate, 0-based
-	*/
-	uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar);
-
-	/*!
-	  @abstract      Calculate the length of the query sequence from CIGAR.
-	  @param  c      pointer to the bam1_core_t structure
-	  @param  cigar  the corresponding CIGAR array (from bam1_t::cigar)
-	  @return        length of the query sequence
-	*/
-	int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar);
+    /*********************
+     * Low-level SAM I/O *
+     *********************/
+
+    /*! @abstract TAM file handler */
+    typedef samFile *tamFile;
+
+    /*!
+      @abstract   Open a SAM file for reading, either uncompressed or compressed by gzip/zlib.
+      @param  fn  SAM file name
+      @return     SAM file handler
+     */
+    static inline tamFile samtools_sam_open(const char *fn) { return sam_open(fn, "r"); }
+    #undef  sam_open
+    #define sam_open samtools_sam_open
+
+    /*!
+      @abstract   Close a SAM file handler
+      @param  fp  SAM file handler
+     */
+    // void sam_close(tamFile fp);
+
+    /*!
+      @abstract      Read one alignment from a SAM file handler
+      @param  fp     SAM file handler
+      @param  header header information (ordered names of chromosomes)
+      @param  b      read alignment; all members in b will be updated
+      @return        0 if successful; otherwise negative
+     */
+    // int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b);
+
+    /*!
+      @abstract       Read header information from a TAB-delimited list file.
+      @param  fn_list file name for the list
+      @return         a pointer to the header structure
+
+      @discussion Each line in this file consists of chromosome name and
+      the length of chromosome.
+     */
+    bam_header_t *sam_header_read2(const char *fn_list);
+
+    /*!
+      @abstract       Read header from a SAM file (if present)
+      @param  fp      SAM file handler
+      @return         pointer to header struct; 0 if no @SQ lines available
+     */
+    static inline bam_header_t *sam_header_read(tamFile fp) { return sam_hdr_read(fp); }
+
+    // Note the distressing cast -- bam_name2id is not thread-safe
+    static inline int32_t bam_get_tid(const bam_header_t *header, const char *seq_name) { return bam_name2id((bam_header_t *)header, seq_name); }
+
+
+    /*********************
+     * Low-level BAM I/O *
+     *********************/
+
+    /*!
+      @abstract Initialize a header structure.
+      @return   the pointer to the header structure
+     */
+    static inline bam_header_t *bam_header_init(void) { return bam_hdr_init(); }
+
+    /*!
+      @abstract        Destroy a header structure.
+      @param  header  pointer to the header
+     */
+    static inline void bam_header_destroy(bam_header_t *header) { bam_hdr_destroy(header); }
+
+    /*!
+      @abstract   Read a header structure from BAM.
+      @param  fp  BAM file handler, opened by bam_open()
+      @return     pointer to the header structure
+
+      @discussion The file position indicator must be placed at the
+      beginning of the file. Upon success, the position indicator will
+      be set at the start of the first alignment.
+     */
+    static inline bam_header_t *bam_header_read(bamFile fp) { return bam_hdr_read(fp); }
+
+    /*!
+      @abstract      Write a header structure to BAM.
+      @param  fp     BAM file handler
+      @param  header pointer to the header structure
+      @return        always 0 currently
+     */
+    static inline int bam_header_write(bamFile fp, const bam_header_t *header) { return bam_hdr_write(fp, header); }
+
+    /*!
+      @abstract   Read an alignment from BAM.
+      @param  fp  BAM file handler
+      @param  b   read alignment; all members are updated.
+      @return     number of bytes read from the file
+
+      @discussion The file position indicator must be
+      placed right before an alignment. Upon success, this function
+      will set the position indicator to the start of the next
+      alignment. This function is not affected by the machine
+      endianness.
+     */
+    // int bam_read1(bamFile fp, bam1_t *b);
+
+    int bam_remove_B(bam1_t *b);
+
+    /*!
+      @abstract   Write an alignment to BAM.
+      @param  fp  BAM file handler
+      @param  b   alignment to write
+      @return     number of bytes written to the file
+     */
+    // int bam_write1(bamFile fp, const bam1_t *b);
+
+    /*! @function
+      @abstract  Initiate a pointer to bam1_t struct
+     */
+//#define bam_init1()
+
+    /*! @function
+      @abstract  Free the memory allocated for an alignment.
+      @param  b  pointer to an alignment
+     */
+//#define bam_destroy1(b)
+
+    /*!
+      @abstract       Format a BAM record in the SAM format
+      @param  header  pointer to the header structure
+      @param  b       alignment to print
+      @return         a pointer to the SAM string
+     */
+    char *bam_format1(const bam_header_t *header, const bam1_t *b);
+
+    /*! @abstract     Formats a BAM record and writes it and \n to stdout */
+    void bam_view1(const bam_header_t *header, const bam1_t *b);
+
+    /*!
+      @abstract       Check whether a BAM record is plausibly valid
+      @param  header  associated header structure, or NULL if unavailable
+      @param  b       alignment to validate
+      @return         0 if the alignment is invalid; non-zero otherwise
+
+      @discussion  Simple consistency check of some of the fields of the
+      alignment record.  If the header is provided, several additional checks
+      are made.  Not all fields are checked, so a non-zero result is not a
+      guarantee that the record is valid.  However it is usually good enough
+      to detect when bam_seek() has been called with a virtual file offset
+      that is not the offset of an alignment record.
+     */
+    int bam_validate1(const bam_header_t *header, const bam1_t *b);
+
+    // TODO Parses headers, so not yet implemented in terms of htslib
+    const char *bam_get_library(bam_header_t *header, const bam1_t *b);
+
+
+    /***************
+     * pileup APIs *
+     ***************/
+
+    /*! @typedef
+      @abstract Structure for one alignment covering the pileup position.
+      @field  b      pointer to the alignment
+      @field  qpos   position of the read base at the pileup site, 0-based
+      @field  indel  indel length; 0 for no indel, positive for ins and negative for del
+      @field  is_del 1 iff the base on the padded read is a deletion
+      @field  level  the level of the read in the "viewer" mode
+
+      @discussion See also bam_plbuf_push() and bam_lplbuf_push(). The
+      difference between the two functions is that the former does not
+      set bam_pileup1_t::level, while the later does. Level helps the
+      implementation of alignment viewers, but calculating this has some
+      overhead.
+     */
+    // typedef struct { ... } bam_pileup1_t;
+
+    // typedef int (*bam_plp_auto_f)(void *data, bam1_t *b);
+
+    // typedef struct incomplete *bam_plp_t;
+
+    // bam_plp_t bam_plp_init(bam_plp_auto_f read, void *data);
+    // int bam_plp_push(bam_plp_t iter, const bam1_t *b);
+    // const bam_pileup1_t *bam_plp_next(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp);
+    // const bam_pileup1_t *bam_plp_auto(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp);
+    // void bam_plp_set_maxcnt(bam_plp_t iter, int maxcnt);
+    // void bam_plp_reset(bam_plp_t iter);
+    // void bam_plp_destroy(bam_plp_t iter);
+
+    // typedef struct incomplete *bam_mplp_t;
+
+    // bam_mplp_t bam_mplp_init(int n, bam_plp_auto_f func,  void **data);
+    // void bam_mplp_destroy(bam_mplp_t iter);
+    // void bam_mplp_set_maxcnt(bam_mplp_t iter, int maxcnt);
+    // int bam_mplp_auto(bam_mplp_t iter, int *_tid, int *_pos, int *n_plp, const bam_pileup1_t **plp);
+
+    /*! @typedef
+      @abstract    Type of function to be called by bam_plbuf_push().
+      @param  tid  chromosome ID as is defined in the header
+      @param  pos  start coordinate of the alignment, 0-based
+      @param  n    number of elements in pl array
+      @param  pl   array of alignments
+      @param  data user provided data
+      @discussion  See also bam_plbuf_push(), bam_plbuf_init() and bam_pileup1_t.
+     */
+    typedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data);
+
+    typedef struct {
+        bam_plp_t iter;
+        bam_pileup_f func;
+        void *data;
+    } bam_plbuf_t;
+
+    void bam_plbuf_reset(bam_plbuf_t *buf);
+    bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data);
+    void bam_plbuf_destroy(bam_plbuf_t *buf);
+    int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf);
+
+    int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data);
+
+    struct __bam_lplbuf_t;
+    typedef struct __bam_lplbuf_t bam_lplbuf_t;
+
+    void bam_lplbuf_reset(bam_lplbuf_t *buf);
+
+    /*! @abstract  bam_plbuf_init() equivalent with level calculated. */
+    bam_lplbuf_t *bam_lplbuf_init(bam_pileup_f func, void *data);
+
+    /*! @abstract  bam_plbuf_destroy() equivalent with level calculated. */
+    void bam_lplbuf_destroy(bam_lplbuf_t *tv);
+
+    /*! @abstract  bam_plbuf_push() equivalent with level calculated. */
+    int bam_lplbuf_push(const bam1_t *b, bam_lplbuf_t *buf);
+
+
+    /*********************
+     * BAM indexing APIs *
+     *********************/
+
+    typedef hts_idx_t bam_index_t;
+
+    /*!
+      @abstract   Build index for a BAM file.
+      @discussion Index file "fn.bai" will be created.
+      @param  fn  name of the BAM file
+      @return     always 0 currently
+     */
+    static inline int samtools_bam_index_build(const char *fn) { return bam_index_build(fn, 0); }
+    #undef  bam_index_build
+    #define bam_index_build samtools_bam_index_build
+
+    /*!
+      @abstract   Load index from file "fn.bai".
+      @param  fn  name of the BAM file (NOT the index file)
+      @return     pointer to the index structure
+     */
+    // bam_index_t *bam_index_load(const char *fn);
+
+    /*!
+      @abstract    Destroy an index structure.
+      @param  idx  pointer to the index structure
+     */
+    static inline void bam_index_destroy(bam_index_t *idx) { hts_idx_destroy(idx); }
+
+    /*! @typedef
+      @abstract      Type of function to be called by bam_fetch().
+      @param  b     the alignment
+      @param  data  user provided data
+     */
+    typedef int (*bam_fetch_f)(const bam1_t *b, void *data);
+
+    /*!
+      @abstract Retrieve the alignments that are overlapped with the
+      specified region.
+
+      @discussion A user defined function will be called for each
+      retrieved alignment ordered by its start position.
+
+      @param  fp    BAM file handler
+      @param  idx   pointer to the alignment index
+      @param  tid   chromosome ID as is defined in the header
+      @param  beg   start coordinate, 0-based
+      @param  end   end coordinate, 0-based
+      @param  data  user provided data (will be transferred to func)
+      @param  func  user defined function
+     */
+    int bam_fetch(bamFile fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func);
+
+    static inline bam_iter_t bam_iter_query(const bam_index_t *idx, int tid, int beg, int end) { return bam_itr_queryi(idx, tid, beg, end); }
+    static inline int bam_iter_read(bamFile fp, bam_iter_t iter, bam1_t *b) { return iter? hts_itr_next(fp, iter, b, 0) : bam_read1(fp, b); }
+    static inline void bam_iter_destroy(bam_iter_t iter) { bam_itr_destroy(iter); }
+
+    /*!
+      @abstract       Parse a region in the format: "chr2:100,000-200,000".
+      @discussion     bam_header_t::hash will be initialized if empty.
+      @param  header  pointer to the header structure
+      @param  str     string to be parsed
+      @param  ref_id  the returned chromosome ID
+      @param  begin   the returned start coordinate
+      @param  end     the returned end coordinate
+      @return         0 on success; -1 on failure
+     */
+    int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end);
+
+
+    /**************************
+     * APIs for optional tags *
+     **************************/
+
+    /*!
+      @abstract       Retrieve data of a tag
+      @param  b       pointer to an alignment struct
+      @param  tag     two-character tag to be retrieved
+
+      @return  pointer to the type and data. The first character is the
+      type that can be 'iIsScCdfAZH'.
+
+      @discussion  Use bam_aux2?() series to convert the returned data to
+      the corresponding type.
+    */
+    // uint8_t *bam_aux_get(const bam1_t *b, const char tag[2]);
+
+    // int32_t bam_aux2i(const uint8_t *s);
+    // float bam_aux2f(const uint8_t *s);
+    #define bam_aux2d(s) (bam_aux2f((s)))
+    // char bam_aux2A(const uint8_t *s);
+    // char *bam_aux2Z(const uint8_t *s);
+
+    // int bam_aux_del(bam1_t *b, uint8_t *s);
+    // void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data);
+    static inline uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2]) { return bam_aux_get(b, tag); } // an alias of bam_aux_get()
+
+
+    /*****************
+     * Miscellaneous *
+     *****************/
+
+    /*!
+      @abstract Calculate the rightmost coordinate of an alignment on the
+      reference genome.
+
+      @param  c      pointer to the bam1_core_t structure
+      @param  cigar  the corresponding CIGAR array (from bam1_t::cigar)
+      @return        the rightmost coordinate, 0-based
+    */
+    static inline uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar) { return c->pos + (c->n_cigar? bam_cigar2rlen(c->n_cigar, cigar) : 1); }
+
+    /*!
+      @abstract      Calculate the length of the query sequence from CIGAR.
+      @param  c      pointer to the bam1_core_t structure
+      @param  cigar  the corresponding CIGAR array (from bam1_t::cigar)
+      @return        length of the query sequence
+    */
+    static inline int32_t samtools_bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar) { return bam_cigar2qlen(c->n_cigar, cigar); }
+    #undef  bam_cigar2qlen
+    #define bam_cigar2qlen samtools_bam_cigar2qlen
 
 #ifdef __cplusplus
 }
@@ -722,13 +553,7 @@ extern "C" {
  */
 static inline int bam_reg2bin(uint32_t beg, uint32_t end)
 {
-	--end;
-	if (beg>>14 == end>>14) return 4681 + (beg>>14);
-	if (beg>>17 == end>>17) return  585 + (beg>>17);
-	if (beg>>20 == end>>20) return   73 + (beg>>20);
-	if (beg>>23 == end>>23) return    9 + (beg>>23);
-	if (beg>>26 == end>>26) return    1 + (beg>>26);
-	return 0;
+    return hts_reg2bin(beg, end, 14, 5);
 }
 
 /*!
@@ -737,57 +562,13 @@ static inline int bam_reg2bin(uint32_t beg, uint32_t end)
   @param  bsrc  source alignment struct
   @return       pointer to the destination alignment struct
  */
-static inline bam1_t *bam_copy1(bam1_t *bdst, const bam1_t *bsrc)
-{
-	uint8_t *data = bdst->data;
-	int m_data = bdst->m_data;   // backup data and m_data
-	if (m_data < bsrc->data_len) { // double the capacity
-		m_data = bsrc->data_len; kroundup32(m_data);
-		data = (uint8_t*)realloc(data, m_data);
-	}
-	memcpy(data, bsrc->data, bsrc->data_len); // copy var-len data
-	*bdst = *bsrc; // copy the rest
-	// restore the backup
-	bdst->m_data = m_data;
-	bdst->data = data;
-	return bdst;
-}
+// bam1_t *bam_copy1(bam1_t *bdst, const bam1_t *bsrc)
 
 /*!
   @abstract     Duplicate an alignment
   @param  src   source alignment struct
   @return       pointer to the destination alignment struct
  */
-static inline bam1_t *bam_dup1(const bam1_t *src)
-{
-	bam1_t *b;
-	b = bam_init1();
-	*b = *src;
-	b->m_data = b->data_len;
-	b->data = (uint8_t*)calloc(b->data_len, 1);
-	memcpy(b->data, src->data, b->data_len);
-	return b;
-}
-
-static inline int bam_aux_type2size(int x)
-{
-	if (x == 'C' || x == 'c' || x == 'A') return 1;
-	else if (x == 'S' || x == 's') return 2;
-	else if (x == 'I' || x == 'i' || x == 'f' || x == 'F') return 4;
-	else return 0;
-}
-
-/*********************************
- *** Compatibility with htslib ***
- *********************************/
-
-typedef bam_header_t bam_hdr_t;
-
-#define bam_get_qname(b) bam1_qname(b)
-#define bam_get_cigar(b) bam1_cigar(b)
-
-#define bam_hdr_read(fp) bam_header_read(fp)
-#define bam_hdr_write(fp, h) bam_header_write(fp, h)
-#define bam_hdr_destroy(fp) bam_header_destroy(fp)
+// bam1_t *bam_dup1(const bam1_t *src)
 
 #endif
diff --git a/samtools/bam2bcf.c.pysam.c b/samtools/bam2bcf.c.pysam.c
index 9faed79..4e127ba 100644
--- a/samtools/bam2bcf.c.pysam.c
+++ b/samtools/bam2bcf.c.pysam.c
@@ -1,280 +1,496 @@
 #include "pysam.h"
 
+/*  bam2bcf.c -- variant calling.
+
+    Copyright (C) 2010-2012 Broad Institute.
+    Copyright (C) 2012-2014 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <math.h>
 #include <stdint.h>
 #include <assert.h>
-#include "bam.h"
-#include "kstring.h"
+#include <float.h>
+#include <htslib/sam.h>
+#include <htslib/kstring.h>
+#include <htslib/kfunc.h>
 #include "bam2bcf.h"
 #include "errmod.h"
-#include "bcftools/bcf.h"
 
-extern	void ks_introsort_uint32_t(size_t n, uint32_t a[]);
+extern  void ks_introsort_uint32_t(size_t n, uint32_t a[]);
+extern const char bam_nt16_nt4_table[];
 
-#define CALL_ETA 0.03f
-#define CALL_MAX 256
-#define CALL_DEFTHETA 0.83f
+#define CALL_DEFTHETA 0.83
 #define DEF_MAPQ 20
 
 #define CAP_DIST 25
 
 bcf_callaux_t *bcf_call_init(double theta, int min_baseQ)
 {
-	bcf_callaux_t *bca;
-	if (theta <= 0.) theta = CALL_DEFTHETA;
-	bca = calloc(1, sizeof(bcf_callaux_t));
-	bca->capQ = 60;
-	bca->openQ = 40; bca->extQ = 20; bca->tandemQ = 100;
-	bca->min_baseQ = min_baseQ;
-	bca->e = errmod_init(1. - theta);
-	bca->min_frac = 0.002;
-	bca->min_support = 1;
+    bcf_callaux_t *bca;
+    if (theta <= 0.) theta = CALL_DEFTHETA;
+    bca = calloc(1, sizeof(bcf_callaux_t));
+    bca->capQ = 60;
+    bca->openQ = 40; bca->extQ = 20; bca->tandemQ = 100;
+    bca->min_baseQ = min_baseQ;
+    bca->e = errmod_init(1. - theta);
+    bca->min_frac = 0.002;
+    bca->min_support = 1;
     bca->per_sample_flt = 0;
     bca->npos = 100;
-    bca->ref_pos = calloc(bca->npos, sizeof(int));
-    bca->alt_pos = calloc(bca->npos, sizeof(int));
- 	return bca;
+    bca->ref_pos = malloc(bca->npos*sizeof(int));
+    bca->alt_pos = malloc(bca->npos*sizeof(int));
+    bca->nqual = 60;
+    bca->ref_mq  = malloc(bca->nqual*sizeof(int));
+    bca->alt_mq  = malloc(bca->nqual*sizeof(int));
+    bca->ref_bq  = malloc(bca->nqual*sizeof(int));
+    bca->alt_bq  = malloc(bca->nqual*sizeof(int));
+    bca->fwd_mqs = malloc(bca->nqual*sizeof(int));
+    bca->rev_mqs = malloc(bca->nqual*sizeof(int));
+    return bca;
 }
 
+void bcf_call_destroy(bcf_callaux_t *bca)
+{
+    if (bca == 0) return;
+    errmod_destroy(bca->e);
+    if (bca->npos) { free(bca->ref_pos); free(bca->alt_pos); bca->npos = 0; }
+    free(bca->ref_mq); free(bca->alt_mq); free(bca->ref_bq); free(bca->alt_bq);
+    free(bca->fwd_mqs); free(bca->rev_mqs);
+    bca->nqual = 0;
+    free(bca->bases); free(bca->inscns); free(bca);
+}
 
+// position in the sequence with respect to the aligned part of the read
 static int get_position(const bam_pileup1_t *p, int *len)
 {
     int icig, n_tot_bases = 0, iread = 0, edist = p->qpos + 1;
-    for (icig=0; icig<p->b->core.n_cigar; icig++) 
+    for (icig=0; icig<p->b->core.n_cigar; icig++)
     {
-        // Conversion from uint32_t to MIDNSHP
-        //  0123456
-        //  MIDNSHP
-        int cig  = bam1_cigar(p->b)[icig] & BAM_CIGAR_MASK;
-        int ncig = bam1_cigar(p->b)[icig] >> BAM_CIGAR_SHIFT;
-        if ( cig==0 )
+        int cig  = bam_get_cigar(p->b)[icig] & BAM_CIGAR_MASK;
+        int ncig = bam_get_cigar(p->b)[icig] >> BAM_CIGAR_SHIFT;
+        if ( cig==BAM_CMATCH || cig==BAM_CEQUAL || cig==BAM_CDIFF )
         {
             n_tot_bases += ncig;
             iread += ncig;
+            continue;
         }
-        else if ( cig==1 )
+        if ( cig==BAM_CINS )
         {
             n_tot_bases += ncig;
             iread += ncig;
+            continue;
         }
-        else if ( cig==4 )
+        if ( cig==BAM_CSOFT_CLIP )
         {
             iread += ncig;
             if ( iread<=p->qpos ) edist -= ncig;
+            continue;
         }
+        if ( cig==BAM_CDEL ) continue;
+        if ( cig==BAM_CHARD_CLIP ) continue;
+        if ( cig==BAM_CPAD ) continue;
+        if ( cig==BAM_CREF_SKIP ) continue;
+        fprintf(pysamerr,"todo: cigar %d\n", cig);
+        assert(0);
     }
     *len = n_tot_bases;
     return edist;
 }
 
-void bcf_call_destroy(bcf_callaux_t *bca)
+void bcf_callaux_clean(bcf_callaux_t *bca, bcf_call_t *call)
 {
-	if (bca == 0) return;
-	errmod_destroy(bca->e);
-    if (bca->npos) { free(bca->ref_pos); free(bca->alt_pos); bca->npos = 0; }
-	free(bca->bases); free(bca->inscns); free(bca);
+    memset(bca->ref_pos,0,sizeof(int)*bca->npos);
+    memset(bca->alt_pos,0,sizeof(int)*bca->npos);
+    memset(bca->ref_mq,0,sizeof(int)*bca->nqual);
+    memset(bca->alt_mq,0,sizeof(int)*bca->nqual);
+    memset(bca->ref_bq,0,sizeof(int)*bca->nqual);
+    memset(bca->alt_bq,0,sizeof(int)*bca->nqual);
+    memset(bca->fwd_mqs,0,sizeof(int)*bca->nqual);
+    memset(bca->rev_mqs,0,sizeof(int)*bca->nqual);
+    if ( call->DPR ) memset(call->DPR,0,sizeof(int32_t)*(call->n+1)*B2B_MAX_ALLELES);
 }
-/* ref_base is the 4-bit representation of the reference base. It is
- * negative if we are looking at an indel. */
+
+/*
+    Notes:
+    - Called from bam_plcmd.c by mpileup. Amongst other things, sets the bcf_callret1_t.qsum frequencies
+        which are carried over via bcf_call_combine and bcf_call2bcf to the output BCF as the QS annotation.
+        Later it's used for multiallelic calling by bcftools -m
+    - ref_base is the 4-bit representation of the reference base. It is negative if we are looking at an indel.
+ */
+/*
+ * This function is called once for each sample.
+ * _n is number of pilesups pl contributing reads to this sample
+ * pl is pointer to array of _n pileups (one pileup per read)
+ * ref_base is the 4-bit representation of the reference base. It is negative if we are looking at an indel.
+ * bca is the settings to perform calls across all samples
+ * r is the returned value of the call
+ */
 int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r)
 {
-	int i, n, ref4, is_indel, ori_depth = 0;
-	memset(r, 0, sizeof(bcf_callret1_t));
-	if (ref_base >= 0) {
-		ref4 = bam_nt16_nt4_table[ref_base];
-		is_indel = 0;
-	} else ref4 = 4, is_indel = 1;
-	if (_n == 0) return -1;
-	// enlarge the bases array if necessary
-	if (bca->max_bases < _n) {
-		bca->max_bases = _n;
-		kroundup32(bca->max_bases);
-		bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
-	}
-	// fill the bases array
-	for (i = n = r->n_supp = 0; i < _n; ++i) {
-		const bam_pileup1_t *p = pl + i;
-		int q, b, mapQ, baseQ, is_diff, min_dist, seqQ;
-		// set base
-		if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
-		++ori_depth;
-		baseQ = q = is_indel? p->aux&0xff : (int)bam1_qual(p->b)[p->qpos]; // base/indel quality
-		seqQ = is_indel? (p->aux>>8&0xff) : 99;
-		if (q < bca->min_baseQ) continue;
-		if (q > seqQ) q = seqQ;
-		mapQ = p->b->core.qual < 255? p->b->core.qual : DEF_MAPQ; // special case for mapQ==255
-		mapQ = mapQ < bca->capQ? mapQ : bca->capQ;
-		if (q > mapQ) q = mapQ;
-		if (q > 63) q = 63;
-		if (q < 4) q = 4;
-		if (!is_indel) {
-			b = bam1_seqi(bam1_seq(p->b), p->qpos); // base
-			b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
-			is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
-		} else {
-			b = p->aux>>16&0x3f;
-			is_diff = (b != 0);
-		}
-		if (is_diff) ++r->n_supp;
-		bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
-		// collect annotations
-		if (b < 4) r->qsum[b] += q;
-		++r->anno[0<<2|is_diff<<1|bam1_strand(p->b)];
-		min_dist = p->b->core.l_qseq - 1 - p->qpos;
-		if (min_dist > p->qpos) min_dist = p->qpos;
-		if (min_dist > CAP_DIST) min_dist = CAP_DIST;
-		r->anno[1<<2|is_diff<<1|0] += baseQ;
-		r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
-		r->anno[2<<2|is_diff<<1|0] += mapQ;
-		r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
-		r->anno[3<<2|is_diff<<1|0] += min_dist;
-		r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
-
-        // collect read positions for ReadPosBias
+    int i, n, ref4, is_indel, ori_depth = 0;
+
+    // clean from previous run
+    r->ori_depth = 0;
+    r->mq0 = 0;
+    memset(r->qsum,0,sizeof(float)*4);
+    memset(r->anno,0,sizeof(double)*16);
+    memset(r->p,0,sizeof(float)*25);
+
+    if (ref_base >= 0) {
+        ref4 = bam_nt16_nt4_table[ref_base];
+        is_indel = 0;
+    } else ref4 = 4, is_indel = 1;
+    if (_n == 0) return -1;
+    // enlarge the bases array if necessary
+    if (bca->max_bases < _n) {
+        bca->max_bases = _n;
+        kroundup32(bca->max_bases);
+        bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
+    }
+    // fill the bases array
+    for (i = n = 0; i < _n; ++i) {
+        const bam_pileup1_t *p = pl + i;
+        int q, b, mapQ, baseQ, is_diff, min_dist, seqQ;
+        // set base
+        if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
+        ++ori_depth;
+        mapQ  = p->b->core.qual < 255? p->b->core.qual : DEF_MAPQ; // special case for mapQ==255
+        if ( !mapQ ) r->mq0++;
+        baseQ = q = is_indel? p->aux&0xff : (int)bam_get_qual(p->b)[p->qpos]; // base/indel quality
+        seqQ = is_indel? (p->aux>>8&0xff) : 99;
+        if (q < bca->min_baseQ) continue;
+        if (q > seqQ) q = seqQ;
+        mapQ = mapQ < bca->capQ? mapQ : bca->capQ;
+        if (q > mapQ) q = mapQ;
+        if (q > 63) q = 63;
+        if (q < 4) q = 4;       // MQ=0 reads count as BQ=4
+        if (!is_indel) {
+            b = bam_seqi(bam_get_seq(p->b), p->qpos); // base
+            b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
+            is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
+        } else {
+            b = p->aux>>16&0x3f;
+            is_diff = (b != 0);
+        }
+        bca->bases[n++] = q<<5 | (int)bam_is_rev(p->b)<<4 | b;
+        // collect annotations
+        if (b < 4)
+        {
+            r->qsum[b] += q;
+            if ( r->DPR ) r->DPR[b]++;
+        }
+        ++r->anno[0<<2|is_diff<<1|bam_is_rev(p->b)];
+        min_dist = p->b->core.l_qseq - 1 - p->qpos;
+        if (min_dist > p->qpos) min_dist = p->qpos;
+        if (min_dist > CAP_DIST) min_dist = CAP_DIST;
+        r->anno[1<<2|is_diff<<1|0] += baseQ;
+        r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
+        r->anno[2<<2|is_diff<<1|0] += mapQ;
+        r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
+        r->anno[3<<2|is_diff<<1|0] += min_dist;
+        r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
+
+        // collect for bias tests
+        if ( baseQ > 59 ) baseQ = 59;
+        if ( mapQ > 59 ) mapQ = 59;
         int len, pos = get_position(p, &len);
         int epos = (double)pos/(len+1) * bca->npos;
-        if ( bam1_seqi(bam1_seq(p->b),p->qpos) == ref_base )
+        int ibq  = baseQ/60. * bca->nqual;
+        int imq  = mapQ/60. * bca->nqual;
+        if ( bam_is_rev(p->b) ) bca->rev_mqs[imq]++;
+        else bca->fwd_mqs[imq]++;
+        if ( bam_seqi(bam_get_seq(p->b),p->qpos) == ref_base )
+        {
             bca->ref_pos[epos]++;
+            bca->ref_bq[ibq]++;
+            bca->ref_mq[imq]++;
+        }
         else
+        {
             bca->alt_pos[epos]++;
-	}
-	r->depth = n; r->ori_depth = ori_depth;
-	// glfgen
-	errmod_cal(bca->e, n, 5, bca->bases, r->p);
-	return r->depth;
+            bca->alt_bq[ibq]++;
+            bca->alt_mq[imq]++;
+        }
+    }
+    r->ori_depth = ori_depth;
+    // glfgen
+    errmod_cal(bca->e, n, 5, bca->bases, r->p); // calculate PL of each genotype
+    return n;
 }
 
-double mann_whitney_1947(int n, int m, int U)
-{
-    if (U<0) return 0;
-    if (n==0||m==0) return U==0 ? 1 : 0;
-    return (double)n/(n+m)*mann_whitney_1947(n-1,m,U-m) + (double)m/(n+m)*mann_whitney_1947(n,m-1,U);
-}
 
-void calc_ReadPosBias(bcf_callaux_t *bca, bcf_call_t *call)
+/*
+ *  calc_vdb() - returns value between zero (most biased) and one (no bias)
+ *               on success, or HUGE_VAL when VDB cannot be calculated because
+ *               of insufficient depth (<2x)
+ *
+ *  Variant Distance Bias tests if the variant bases are positioned within the
+ *  reads with sufficient randomness. Unlike other tests, it looks only at
+ *  variant reads and therefore gives different kind of information than Read
+ *  Position Bias for instance. VDB was developed for detecting artefacts in
+ *  RNA-seq calls where reads from spliced transcripts span splice site
+ *  boundaries.  The current implementation differs somewhat from the original
+ *  version described in supplementary material of PMID:22524474, but the idea
+ *  remains the same. (Here the random variable tested is the average distance
+ *  from the averaged position, not the average pairwise distance.)
+ *
+ *  For coverage of 2x, the calculation is exact but is approximated for the
+ *  rest. The result is most accurate between 4-200x. For 3x or >200x, the
+ *  reported values are slightly more favourable than those of a true random
+ *  distribution.
+ */
+double calc_vdb(int *pos, int npos)
 {
-    int i, nref = 0, nalt = 0;
-    unsigned long int U = 0;
-    for (i=0; i<bca->npos; i++) 
+    // Note well: the parameters were obtained by fitting to simulated data of
+    // 100bp reads. This assumes rescaling to 100bp in bcf_call_glfgen().
+    const int readlen = 100;
+    assert( npos==readlen );
+
+    #define nparam 15
+    const float param[nparam][3] = { {3,0.079,18}, {4,0.09,19.8}, {5,0.1,20.5}, {6,0.11,21.5},
+        {7,0.125,21.6}, {8,0.135,22}, {9,0.14,22.2}, {10,0.153,22.3}, {15,0.19,22.8},
+        {20,0.22,23.2}, {30,0.26,23.4}, {40,0.29,23.5}, {50,0.35,23.65}, {100,0.5,23.7},
+        {200,0.7,23.7} };
+
+    int i, dp = 0;
+    float mean_pos = 0, mean_diff = 0;
+    for (i=0; i<npos; i++)
     {
-        nref += bca->ref_pos[i];
-        nalt += bca->alt_pos[i];
-        U += nref*bca->alt_pos[i];
-        bca->ref_pos[i] = 0;
-        bca->alt_pos[i] = 0;
+        if ( !pos[i] ) continue;
+        dp += pos[i];
+        mean_pos += pos[i]*i;
     }
-#if 0
-//todo
-    double var = 0, avg = (double)(nref+nalt)/bca->npos;
-    for (i=0; i<bca->npos; i++) 
+    if ( dp<2 ) return HUGE_VAL;     // one or zero reads can be placed anywhere
+
+    mean_pos /= dp;
+    for (i=0; i<npos; i++)
     {
-        double ediff = bca->ref_pos[i] + bca->alt_pos[i] - avg;
-        var += ediff*ediff;
-        bca->ref_pos[i] = 0;
-        bca->alt_pos[i] = 0;
+        if ( !pos[i] ) continue;
+        mean_diff += pos[i] * fabs(i - mean_pos);
     }
-    call->read_pos.avg = avg;
-    call->read_pos.var = sqrt(var/bca->npos);
-    call->read_pos.dp  = nref+nalt;
-#endif
-    if ( !nref || !nalt )
+    mean_diff /= dp;
+
+    int ipos = mean_diff;   // tuned for float-to-int implicit conversion
+    if ( dp==2 )
+        return (2*readlen-2*(ipos+1)-1)*(ipos+1)/(readlen-1)/(readlen*0.5);
+
+    if ( dp>=200 )
+        i = nparam; // shortcut for big depths
+    else
     {
-        call->read_pos_bias = -1;
-        return;
+        for (i=0; i<nparam; i++)
+            if ( param[i][0]>=dp ) break;
     }
-
-    if ( nref>=8 || nalt>=8 )
+    float pshift, pscale;
+    if ( i==nparam )
+    {
+        // the depth is too high, go with 200x
+        pscale = param[nparam-1][1];
+        pshift = param[nparam-1][2];
+    }
+    else if ( i>0 && param[i][0]!=dp )
     {
-        // normal approximation
-        double mean = ((double)nref*nalt+1.0)/2.0;
-        double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
-        double z    = (U-mean)/sqrt(var2);
-        call->read_pos_bias = z;
-        //fprintf(pysamerr,"nref=%d  nalt=%d  U=%ld  mean=%e  var=%e  zval=%e\n", nref,nalt,U,mean,sqrt(var2),call->read_pos_bias);
+        // linear interpolation of parameters
+        pscale = (param[i-1][1] + param[i][1])*0.5;
+        pshift = (param[i-1][2] + param[i][2])*0.5;
     }
     else
     {
-        double p = mann_whitney_1947(nalt,nref,U);
-        // biased form claimed by GATK to behave better empirically
-        // double var2 = (1.0+1.0/(nref+nalt+1.0))*(double)nref*nalt*(nref+nalt+1.0)/12.0;
-        double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
-        double z;
-        if ( p >= 1./sqrt(var2*2*M_PI) ) z = 0;   // equal to mean
-        else
-        {
-            if ( U >= nref*nalt/2. ) z = sqrt(-2*log(sqrt(var2*2*M_PI)*p));
-            else z = -sqrt(-2*log(sqrt(var2*2*M_PI)*p));
-        }
-        call->read_pos_bias = z;
-        //fprintf(pysamerr,"nref=%d  nalt=%d  U=%ld  p=%e var2=%e  zval=%e\n", nref,nalt,U, p,var2,call->read_pos_bias);
+        pscale = param[i][1];
+        pshift = param[i][2];
     }
+    return 0.5*kf_erfc(-(mean_diff-pshift)*pscale);
 }
 
-float mean_diff_to_prob(float mdiff, int dp, int readlen)
+double calc_chisq_bias(int *a, int *b, int n)
 {
-    if ( dp==2 )
+    int na = 0, nb = 0, i, ndf = n;
+    for (i=0; i<n; i++) na += a[i];
+    for (i=0; i<n; i++) nb += b[i];
+    if ( !na || !nb ) return HUGE_VAL;
+
+    double chisq = 0;
+    for (i=0; i<n; i++)
     {
-        if ( mdiff==0 )
-            return (2.0*readlen + 4.0*(readlen-1.0))/((float)readlen*readlen);
+        if ( !a[i] && !b[i] ) ndf--;
         else
-            return 8.0*(readlen - 4.0*mdiff)/((float)readlen*readlen);
+        {
+            double tmp = a[i] - b[i];
+            chisq += tmp*tmp/(a[i]+b[i]);
+        }
     }
+    /*
+        kf_gammq: incomplete gamma function Q(a,x) = 1 - P(a,x) = Gamma(a,x)/Gamma(a)
+        1 if the distributions are identical, 0 if very different
+    */
+    double prob = kf_gammaq(0.5*ndf, 0.5*chisq);
+    return prob;
+}
+
+double mann_whitney_1947(int n, int m, int U)
+{
+    if (U<0) return 0;
+    if (n==0||m==0) return U==0 ? 1 : 0;
+    return (double)n/(n+m)*mann_whitney_1947(n-1,m,U-m) + (double)m/(n+m)*mann_whitney_1947(n,m-1,U);
+}
 
-    // This is crude empirical approximation and is not very accurate for
-    // shorter read lengths (<100bp). There certainly is a room for
-    // improvement.
-    const float mv[24][2] = { {0,0}, {0,0}, {0,0},
-        { 9.108, 4.934}, { 9.999, 3.991}, {10.273, 3.485}, {10.579, 3.160},
-        {10.828, 2.889}, {11.014, 2.703}, {11.028, 2.546}, {11.244, 2.391},
-        {11.231, 2.320}, {11.323, 2.138}, {11.403, 2.123}, {11.394, 1.994},
-        {11.451, 1.928}, {11.445, 1.862}, {11.516, 1.815}, {11.560, 1.761},
-        {11.544, 1.728}, {11.605, 1.674}, {11.592, 1.652}, {11.674, 1.613},
-        {11.641, 1.570} };
-
-    float m, v;
-    if ( dp>=24 )
+double mann_whitney_1947_cdf(int n, int m, int U)
+{
+    int i;
+    double sum = 0;
+    for (i=0; i<=U; i++)
+        sum += mann_whitney_1947(n,m,i);
+    return sum;
+}
+
+double calc_mwu_bias_cdf(int *a, int *b, int n)
+{
+    int na = 0, nb = 0, i;
+    double U = 0, ties = 0;
+    for (i=0; i<n; i++)
     {
-        m = readlen/8.;
-        if (dp>100) dp = 100;
-        v = 1.476/(0.182*pow(dp,0.514));
-        v = v*(readlen/100.);
+        na += a[i];
+        U  += a[i] * (nb + b[i]*0.5);
+        nb += b[i];
+        if ( a[i] && b[i] )
+        {
+            double tie = a[i] + b[i];
+            ties += (tie*tie-1)*tie;
+        }
     }
-    else
+    if ( !na || !nb ) return HUGE_VAL;
+
+    // Always work with the smaller U
+    double U_min = ((double)na * nb) - U;
+    if ( U < U_min ) U_min = U;
+
+    if ( na==1 ) return 2.0 * (floor(U_min)+1) / (nb+1);
+    if ( nb==1 ) return 2.0 * (floor(U_min)+1) / (na+1);
+
+    // Normal approximation, very good for na>=8 && nb>=8 and reasonable if na<8 or nb<8
+    if ( na>=8 || nb>=8 )
     {
-        m = mv[dp][0];
-        v = mv[dp][1];
-        m = m*readlen/100.;
-        v = v*readlen/100.;
-        v *= 1.2;   // allow more variability
+        double mean = ((double)na*nb)*0.5;
+        // Correction for ties:
+        //      double N = na+nb;
+        //      double var2 = (N*N-1)*N-ties;
+        //      if ( var2==0 ) return 1.0;
+        //      var2 *= ((double)na*nb)/N/(N-1)/12.0;
+        // No correction for ties:
+        double var2 = ((double)na*nb)*(na+nb+1)/12.0;
+        double z = (U_min - mean)/sqrt(2*var2);   // z is N(0,1)
+        return 2.0 - kf_erfc(z);  // which is 1 + erf(z)
     }
-    return 1.0/(v*sqrt(2*M_PI)) * exp(-0.5*((mdiff-m)/v)*((mdiff-m)/v));
+
+    // Exact calculation
+    double pval = 2*mann_whitney_1947_cdf(na,nb,U_min);
+    return pval>1 ? 1 : pval;
 }
 
-void calc_vdb(bcf_callaux_t *bca, bcf_call_t *call)
+double calc_mwu_bias(int *a, int *b, int n)
 {
-    int i, dp = 0;
-    float mean_pos = 0, mean_diff = 0;
-    for (i=0; i<bca->npos; i++)
+    int na = 0, nb = 0, i;
+    double U = 0, ties = 0;
+    for (i=0; i<n; i++)
     {
-        if ( !bca->alt_pos[i] ) continue;
-        dp += bca->alt_pos[i];
-        int j = i<bca->npos/2 ? i : bca->npos - i;
-        mean_pos += bca->alt_pos[i]*j;
+        na += a[i];
+        U  += a[i] * (nb + b[i]*0.5);
+        nb += b[i];
+        if ( a[i] && b[i] )
+        {
+            double tie = a[i] + b[i];
+            ties += (tie*tie-1)*tie;
+        }
     }
-    if ( dp<2 )
+    if ( !na || !nb ) return HUGE_VAL;
+    if ( na==1 || nb==1 ) return 1.0;       // Flat probability, all U values are equally likely
+
+    double mean = ((double)na*nb)*0.5;
+    if ( na==2 || nb==2 )
     {
-        call->vdb = -1;
-        return;
+        // Linear approximation
+        return U>mean ? (2.0*mean-U)/mean : U/mean;
     }
-    mean_pos /= dp;
-    for (i=0; i<bca->npos; i++)
+    // Correction for ties:
+    //      double N = na+nb;
+    //      double var2 = (N*N-1)*N-ties;
+    //      if ( var2==0 ) return 1.0;
+    //      var2 *= ((double)na*nb)/N/(N-1)/12.0;
+    // No correction for ties:
+    double var2 = ((double)na*nb)*(na+nb+1)/12.0;
+    if ( na>=8 || nb>=8 )
     {
-        if ( !bca->alt_pos[i] ) continue;
-        int j = i<bca->npos/2 ? i : bca->npos - i;
-        mean_diff += bca->alt_pos[i] * fabs(j - mean_pos);
+        // Normal approximation, very good for na>=8 && nb>=8 and reasonable if na<8 or nb<8
+        return exp(-0.5*(U-mean)*(U-mean)/var2);
     }
-    mean_diff /= dp;
-    call->vdb = mean_diff_to_prob(mean_diff, dp, bca->npos);
+
+    // Exact calculation
+    return mann_whitney_1947(na,nb,U) * sqrt(2*M_PI*var2);
+}
+
+static inline double logsumexp2(double a, double b)
+{
+    if ( a>b )
+        return log(1 + exp(b-a)) + a;
+    else
+        return log(1 + exp(a-b)) + b;
+}
+
+void calc_SegBias(const bcf_callret1_t *bcr, bcf_call_t *call)
+{
+    call->seg_bias = HUGE_VAL;
+    if ( !bcr ) return;
+
+    int nr = call->anno[2] + call->anno[3]; // number of observed non-reference reads
+    if ( !nr ) return;
+
+    int avg_dp = (call->anno[0] + call->anno[1] + nr) / call->n;    // average depth
+    double M   = floor((double)nr / avg_dp + 0.5);   // an approximate number of variants samples in the population
+    if ( M>call->n ) M = call->n;       // clamp M at the number of samples
+    else if ( M==0 ) M = 1;
+    double f = M / 2. / call->n;        // allele frequency
+    double p = (double) nr / call->n;   // number of variant reads per sample expected if variant not real (poisson)
+    double q = (double) nr / M;         // number of variant reads per sample expected if variant is real (poisson)
+    double sum = 0;
+    const double log2 = log(2.0);
+
+    // fprintf(pysamerr,"M=%.1f  p=%e q=%e f=%f  dp=%d\n",M,p,q,f,avg_dp);
+    int i;
+    for (i=0; i<call->n; i++)
+    {
+        int oi = bcr[i].anno[2] + bcr[i].anno[3];       // observed number of non-ref reads
+        double tmp;
+        if ( oi )
+        {
+            // tmp = log(f) + oi*log(q/p) - q + log(2*(1-f) + f*pow(2,oi)*exp(-q)) + p; // this can under/overflow
+            tmp = logsumexp2(log(2*(1-f)), log(f) + oi*log2 - q);
+            tmp += log(f) + oi*log(q/p) - q + p;
+        }
+        else
+            tmp = log(2*f*(1-f)*exp(-q) + f*f*exp(-2*q) + (1-f)*(1-f)) + p;
+        sum += tmp;
+        // fprintf(pysamerr,"oi=%d %e\n", oi,tmp);
+    }
+    call->seg_bias = sum;
 }
 
 /**
@@ -284,186 +500,307 @@ void calc_vdb(bcf_callaux_t *bca, bcf_call_t *call)
  *  @bca:       auxiliary data structure for holding temporary values
  *  @ref_base:  the reference base
  *  @call:      filled with the annotations
+ *
+ *  Combines calls across the various samples being studied
+ *  1. For each allele at each base across all samples the quality is summed so
+ *     you end up with a set of quality sums for each allele present 2. The quality
+ *     sums are sorted.
+ *  3. Using the sorted quality sums we now create the allele ordering array
+ *     A\subN. This is done by doing the following:
+ *     a) If the reference allele is known it always comes first, otherwise N
+ *        comes first.
+ *     b) Then the rest of the alleles are output in descending order of quality
+ *        sum (which we already know the qsum array was sorted).  Any allelles with
+ *        qsum 0 will be excluded.
+ *  4. Using the allele ordering array we create the genotype ordering array.
+ *     In the worst case with an unknown reference this will be:  A0/A0 A1/A0 A1/A1
+ *     A2/A0 A2/A1 A2/A2 A3/A0 A3/A1 A3/A2 A3/A3 A4/A0 A4/A1 A4/A2 A4/A3 A4/A4
+ *  5. The genotype ordering array is then used to extract data from the error
+ *     model 5*5 matrix and is used to produce a Phread likelihood array for each
+ *     sample.
  */
 int bcf_call_combine(int n, const bcf_callret1_t *calls, bcf_callaux_t *bca, int ref_base /*4-bit*/, bcf_call_t *call)
 {
-	int ref4, i, j, qsum[4];
-	int64_t tmp;
-	if (ref_base >= 0) {
-		call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
-		if (ref4 > 4) ref4 = 4;
-	} else call->ori_ref = -1, ref4 = 0;
-	// calculate qsum
-	memset(qsum, 0, 4 * sizeof(int));
-	for (i = 0; i < n; ++i)
-		for (j = 0; j < 4; ++j)
-			qsum[j] += calls[i].qsum[j];
-    int qsum_tot=0;
-    for (j=0; j<4; j++) { qsum_tot += qsum[j]; call->qsum[j] = 0; }
-	for (j = 0; j < 4; ++j) qsum[j] = qsum[j] << 2 | j;
-	// find the top 2 alleles
-	for (i = 1; i < 4; ++i) // insertion sort
-		for (j = i; j > 0 && qsum[j] < qsum[j-1]; --j)
-			tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
-	// set the reference allele and alternative allele(s)
-	for (i = 0; i < 5; ++i) call->a[i] = -1;
-	call->unseen = -1;
-	call->a[0] = ref4;
-	for (i = 3, j = 1; i >= 0; --i) {
-		if ((qsum[i]&3) != ref4) {
-			if (qsum[i]>>2 != 0) 
+    int ref4, i, j;
+    float qsum[5] = {0,0,0,0,0};
+    if (ref_base >= 0) {
+        call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
+        if (ref4 > 4) ref4 = 4;
+    } else call->ori_ref = -1, ref4 = 0;
+
+    // calculate qsum, this is done by summing normalized qsum across all samples,
+    // to account for differences in coverage
+    for (i = 0; i < n; ++i)
+    {
+        float sum = 0;
+        for (j = 0; j < 4; ++j) sum += calls[i].qsum[j];
+        if ( sum )
+            for (j = 0; j < 4; j++) qsum[j] += calls[i].qsum[j] / sum;
+    }
+
+    // sort qsum in ascending order (insertion sort)
+    float *ptr[5], *tmp;
+    for (i=0; i<5; i++) ptr[i] = &qsum[i];
+    for (i=1; i<4; i++)
+        for (j=i; j>0 && *ptr[j] < *ptr[j-1]; j--)
+            tmp = ptr[j], ptr[j] = ptr[j-1], ptr[j-1] = tmp;
+
+    // Set the reference allele and alternative allele(s)
+    for (i=0; i<5; i++) call->a[i] = -1;
+    for (i=0; i<5; i++) call->qsum[i] = 0;
+    call->unseen = -1;
+    call->a[0] = ref4;
+    for (i=3, j=1; i>=0; i--)   // i: alleles sorted by QS; j, a[j]: output allele ordering
+    {
+        int ipos = ptr[i] - qsum;   // position in sorted qsum array
+        if ( ipos==ref4 )
+            call->qsum[0] = qsum[ipos];    // REF's qsum
+        else
+        {
+            if ( !qsum[ipos] ) break;       // qsum is 0, this and consequent alleles are not seen in the pileup
+            call->qsum[j] = qsum[ipos];
+            call->a[j++]  = ipos;
+        }
+    }
+    if (ref_base >= 0)
+    {
+        // for SNPs, find the "unseen" base
+        if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
+            call->unseen = j, call->a[j++] = ptr[i] - qsum;
+        call->n_alleles = j;
+    }
+    else
+    {
+        call->n_alleles = j;
+        if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
+    }
+    /*
+     * Set the phread likelihood array (call->PL) This array is 15 entries long
+     * for each sample because that is size of an upper or lower triangle of a
+     * worst case 5x5 matrix of possible genotypes. This worst case matrix will
+     * occur when all 4 possible alleles are present and the reference allele
+     * is unknown.  The sides of the matrix will correspond to the reference
+     * allele (if known) followed by the alleles present in descending order of
+     * quality sum
+     */
+    {
+        int x, g[15], z;
+        double sum_min = 0.;
+        x = call->n_alleles * (call->n_alleles + 1) / 2;
+        // get the possible genotypes
+        // this is done by creating an ordered list of locations g for call (allele a, allele b) in the genotype likelihood matrix
+        for (i = z = 0; i < call->n_alleles; ++i) {
+            for (j = 0; j <= i; ++j) {
+                g[z++] = call->a[j] * 5 + call->a[i];
+            }
+        }
+        // for each sample calculate the PL
+        for (i = 0; i < n; ++i)
+        {
+            int32_t *PL = call->PL + x * i;
+            const bcf_callret1_t *r = calls + i;
+            float min = FLT_MAX;
+            for (j = 0; j < x; ++j) {
+                if (min > r->p[g[j]]) min = r->p[g[j]];
+            }
+            sum_min += min;
+            for (j = 0; j < x; ++j) {
+                int y;
+                y = (int)(r->p[g[j]] - min + .499);
+                if (y > 255) y = 255;
+                PL[j] = y;
+            }
+        }
+        if ( call->DP4 )
+        {
+            for (i=0; i<n; i++)
+            {
+                call->DP4[4*i]   = calls[i].anno[0];
+                call->DP4[4*i+1] = calls[i].anno[1];
+                call->DP4[4*i+2] = calls[i].anno[2];
+                call->DP4[4*i+3] = calls[i].anno[3];
+            }
+        }
+        if ( call->DPR )
+        {
+            assert( call->n_alleles<=B2B_MAX_ALLELES );   // this is always true for SNPs and so far for indels as well
+
+            int32_t tmp[B2B_MAX_ALLELES];
+            int32_t *dpr = call->DPR + B2B_MAX_ALLELES, *dpr_out = call->DPR + B2B_MAX_ALLELES;
+            int32_t *dpr_tot = call->DPR;
+            for (i=0; i<n; i++)
             {
-                if ( j<4 ) call->qsum[j] = (float)(qsum[i]>>2)/qsum_tot; // ref N can make j>=4
-                call->a[j++]  = qsum[i]&3;
+                for (j=0; j<call->n_alleles; j++)
+                {
+                    tmp[j] = dpr[ call->a[j] ];
+                    dpr_tot[j] += tmp[j];
+                }
+                for (j=0; j<call->n_alleles; j++) dpr_out[j] = tmp[j];
+                dpr_out += call->n_alleles;
+                dpr += B2B_MAX_ALLELES;
             }
-			else break;
-		}
-        else 
-            call->qsum[0] = (float)(qsum[i]>>2)/qsum_tot;
-	}
-	if (ref_base >= 0) { // for SNPs, find the "unseen" base
-		if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
-			call->unseen = j, call->a[j++] = qsum[i]&3;
-		call->n_alleles = j;
-	} else {
-		call->n_alleles = j;
-		if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
-	}
-	// set the PL array
-	if (call->n < n) {
-		call->n = n;
-		call->PL = realloc(call->PL, 15 * n);
-	}
-	{
-		int x, g[15], z;
-		double sum_min = 0.;
-		x = call->n_alleles * (call->n_alleles + 1) / 2;
-		// get the possible genotypes
-		for (i = z = 0; i < call->n_alleles; ++i)
-			for (j = 0; j <= i; ++j)
-				g[z++] = call->a[j] * 5 + call->a[i];
-		for (i = 0; i < n; ++i) {
-			uint8_t *PL = call->PL + x * i;
-			const bcf_callret1_t *r = calls + i;
-			float min = 1e37;
-			for (j = 0; j < x; ++j)
-				if (min > r->p[g[j]]) min = r->p[g[j]];
-			sum_min += min;
-			for (j = 0; j < x; ++j) {
-				int y;
-				y = (int)(r->p[g[j]] - min + .499);
-				if (y > 255) y = 255;
-				PL[j] = y;
-			}
-		}
-//		if (ref_base < 0) fprintf(pysamerr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
-		call->shift = (int)(sum_min + .499);
-	}
-	// combine annotations
-	memset(call->anno, 0, 16 * sizeof(int));
-	for (i = call->depth = call->ori_depth = 0, tmp = 0; i < n; ++i) {
-		call->depth += calls[i].depth;
-		call->ori_depth += calls[i].ori_depth;
-		for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
-	}
-
-    calc_vdb(bca, call);
-    calc_ReadPosBias(bca, call);
-
-	return 0;
+        }
+
+//      if (ref_base < 0) fprintf(pysamerr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
+        call->shift = (int)(sum_min + .499);
+    }
+    // combine annotations
+    memset(call->anno, 0, 16 * sizeof(double));
+    call->ori_depth = 0;
+    call->depth     = 0;
+    call->mq0       = 0;
+    for (i = 0; i < n; ++i) {
+        call->depth += calls[i].anno[0] + calls[i].anno[1] + calls[i].anno[2] + calls[i].anno[3];
+        call->ori_depth += calls[i].ori_depth;
+        call->mq0 += calls[i].mq0;
+        for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
+    }
+
+    calc_SegBias(calls, call);
+
+    // calc_chisq_bias("XPOS", call->bcf_hdr->id[BCF_DT_CTG][call->tid].key, call->pos, bca->ref_pos, bca->alt_pos, bca->npos);
+    // calc_chisq_bias("XMQ", call->bcf_hdr->id[BCF_DT_CTG][call->tid].key, call->pos, bca->ref_mq, bca->alt_mq, bca->nqual);
+    // calc_chisq_bias("XBQ", call->bcf_hdr->id[BCF_DT_CTG][call->tid].key, call->pos, bca->ref_bq, bca->alt_bq, bca->nqual);
+
+    call->mwu_pos = calc_mwu_bias(bca->ref_pos, bca->alt_pos, bca->npos);
+    call->mwu_mq  = calc_mwu_bias(bca->ref_mq,  bca->alt_mq,  bca->nqual);
+    call->mwu_bq  = calc_mwu_bias(bca->ref_bq,  bca->alt_bq,  bca->nqual);
+    call->mwu_mqs = calc_mwu_bias(bca->fwd_mqs, bca->rev_mqs, bca->nqual);
+
+#if CDF_MWU_TESTS
+    call->mwu_pos_cdf = calc_mwu_bias_cdf(bca->ref_pos, bca->alt_pos, bca->npos);
+    call->mwu_mq_cdf  = calc_mwu_bias_cdf(bca->ref_mq,  bca->alt_mq,  bca->nqual);
+    call->mwu_bq_cdf  = calc_mwu_bias_cdf(bca->ref_bq,  bca->alt_bq,  bca->nqual);
+    call->mwu_mqs_cdf = calc_mwu_bias_cdf(bca->fwd_mqs, bca->rev_mqs, bca->nqual);
+#endif
+
+    call->vdb = calc_vdb(bca->alt_pos, bca->npos);
+
+    return 0;
 }
 
-int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int fmt_flag,
-				 const bcf_callaux_t *bca, const char *ref)
+int bcf_call2bcf(bcf_call_t *bc, bcf1_t *rec, bcf_callret1_t *bcr, int fmt_flag, const bcf_callaux_t *bca, const char *ref)
 {
-	extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
-	kstring_t s;
-	int i, j;
-	b->n_smpl = bc->n;
-	b->tid = tid; b->pos = pos; b->qual = 0;
-	s.s = b->str; s.m = b->m_str; s.l = 0;
-	kputc('\0', &s);
-	if (bc->ori_ref < 0) { // an indel
-		// write REF
-		kputc(ref[pos], &s);
-		for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
-		kputc('\0', &s);
-		// write ALT
-		kputc(ref[pos], &s);
-		for (i = 1; i < 4; ++i) {
-			if (bc->a[i] < 0) break;
-			if (i > 1) {
-				kputc(',', &s); kputc(ref[pos], &s);
-			}
-			if (bca->indel_types[bc->a[i]] < 0) { // deletion
-				for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++j)
-					kputc(ref[pos+1+j], &s);
-			} else { // insertion; cannot be a reference unless a bug
-				char *inscns = &bca->inscns[bc->a[i] * bca->maxins];
-				for (j = 0; j < bca->indel_types[bc->a[i]]; ++j)
-					kputc("ACGTN"[(int)inscns[j]], &s);
-				for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
-			}
-		}
-		kputc('\0', &s);
-	} else { // a SNP
-		kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s);
-		for (i = 1; i < 5; ++i) {
-			if (bc->a[i] < 0) break;
-			if (i > 1) kputc(',', &s);
-			kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s);
-		}
-		kputc('\0', &s);
-	}
-	kputc('\0', &s);
-	// INFO
-	if (bc->ori_ref < 0) ksprintf(&s,"INDEL;IS=%d,%f;", bca->max_support, bca->max_frac);
-	kputs("DP=", &s); kputw(bc->ori_depth, &s); kputs(";I16=", &s);
-	for (i = 0; i < 16; ++i) {
-		if (i) kputc(',', &s);
-		kputw(bc->anno[i], &s);
-	}
-    //ksprintf(&s,";RPS=%d,%f,%f", bc->read_pos.dp,bc->read_pos.avg,bc->read_pos.var);
-    ksprintf(&s,";QS=%f,%f,%f,%f", bc->qsum[0],bc->qsum[1],bc->qsum[2],bc->qsum[3]);
-    if (bc->vdb != -1)
-        ksprintf(&s, ";VDB=%e", bc->vdb);
-    if (bc->read_pos_bias != -1 )
-        ksprintf(&s, ";RPB=%e", bc->read_pos_bias);
-	kputc('\0', &s);
-	// FMT
-	kputs("PL", &s);
-	if (bcr && fmt_flag) {
-		if (fmt_flag & B2B_FMT_DP) kputs(":DP", &s);
-		if (fmt_flag & B2B_FMT_DV) kputs(":DV", &s);
-		if (fmt_flag & B2B_FMT_SP) kputs(":SP", &s);
-	}
-	kputc('\0', &s);
-	b->m_str = s.m; b->str = s.s; b->l_str = s.l;
-	bcf_sync(b);
-	memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n);
-	if (bcr && fmt_flag) {
-		uint16_t *dp = (fmt_flag & B2B_FMT_DP)? b->gi[1].data : 0;
-		uint16_t *dv = (fmt_flag & B2B_FMT_DV)? b->gi[1 + ((fmt_flag & B2B_FMT_DP) != 0)].data : 0;
-		int32_t  *sp = (fmt_flag & B2B_FMT_SP)? b->gi[1 + ((fmt_flag & B2B_FMT_DP) != 0) + ((fmt_flag & B2B_FMT_DV) != 0)].data : 0;
-		for (i = 0; i < bc->n; ++i) {
-			bcf_callret1_t *p = bcr + i;
-			if (dp) dp[i] = p->depth  < 0xffff? p->depth  : 0xffff;
-			if (dv) dv[i] = p->n_supp < 0xffff? p->n_supp : 0xffff;
-			if (sp) {
-				if (p->anno[0] + p->anno[1] < 2 || p->anno[2] + p->anno[3] < 2
-					|| p->anno[0] + p->anno[2] < 2 || p->anno[1] + p->anno[3] < 2)
-				{
-					sp[i] = 0;
-				} else {
-					double left, right, two;
-					int x;
-					kt_fisher_exact(p->anno[0], p->anno[1], p->anno[2], p->anno[3], &left, &right, &two);
-					x = (int)(-4.343 * log(two) + .499);
-					if (x > 255) x = 255;
-					sp[i] = x;
-				}
-			}
-		}
-	}
-	return 0;
+    extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
+    int i, j, nals = 1;
+
+    bcf_hdr_t *hdr = bc->bcf_hdr;
+    rec->rid  = bc->tid;
+    rec->pos  = bc->pos;
+    rec->qual = 0;
+
+    bc->tmp.l = 0;
+    if (bc->ori_ref < 0)    // indel
+    {
+        // REF
+        kputc(ref[bc->pos], &bc->tmp);
+        for (j = 0; j < bca->indelreg; ++j) kputc(ref[bc->pos+1+j], &bc->tmp);
+
+        // ALT
+        for (i=1; i<4; i++)
+        {
+            if (bc->a[i] < 0) break;
+            kputc(',', &bc->tmp); kputc(ref[bc->pos], &bc->tmp);
+
+            if (bca->indel_types[bc->a[i]] < 0) { // deletion
+                for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++j)
+                    kputc(ref[bc->pos+1+j], &bc->tmp);
+            } else { // insertion; cannot be a reference unless a bug
+                char *inscns = &bca->inscns[bc->a[i] * bca->maxins];
+                for (j = 0; j < bca->indel_types[bc->a[i]]; ++j)
+                    kputc("ACGTN"[(int)inscns[j]], &bc->tmp);
+                for (j = 0; j < bca->indelreg; ++j) kputc(ref[bc->pos+1+j], &bc->tmp);
+            }
+            nals++;
+        }
+    }
+    else    // SNP
+    {
+        kputc("ACGTN"[bc->ori_ref], &bc->tmp);
+        for (i=1; i<5; i++)
+        {
+            if (bc->a[i] < 0) break;
+            kputc(',', &bc->tmp);
+            if ( bc->unseen==i ) kputs("<X>", &bc->tmp);
+            else kputc("ACGT"[bc->a[i]], &bc->tmp);
+            nals++;
+        }
+    }
+    bcf_update_alleles_str(hdr, rec, bc->tmp.s);
+
+    bc->tmp.l = 0;
+
+    // INFO
+    if (bc->ori_ref < 0)
+    {
+        bcf_update_info_flag(hdr, rec, "INDEL", NULL, 1);
+        bcf_update_info_int32(hdr, rec, "IDV", &bca->max_support, 1);
+        bcf_update_info_float(hdr, rec, "IMF", &bca->max_frac, 1);
+    }
+    bcf_update_info_int32(hdr, rec, "DP", &bc->ori_depth, 1);
+
+    float tmpf[16];
+    for (i=0; i<16; i++) tmpf[i] = bc->anno[i];
+    bcf_update_info_float(hdr, rec, "I16", tmpf, 16);
+    bcf_update_info_float(hdr, rec, "QS", bc->qsum, nals);
+
+    if ( bc->vdb != HUGE_VAL )      bcf_update_info_float(hdr, rec, "VDB", &bc->vdb, 1);
+    if ( bc->seg_bias != HUGE_VAL ) bcf_update_info_float(hdr, rec, "SGB", &bc->seg_bias, 1);
+    if ( bc->mwu_pos != HUGE_VAL )  bcf_update_info_float(hdr, rec, "RPB", &bc->mwu_pos, 1);
+    if ( bc->mwu_mq != HUGE_VAL )   bcf_update_info_float(hdr, rec, "MQB", &bc->mwu_mq, 1);
+    if ( bc->mwu_mqs != HUGE_VAL )  bcf_update_info_float(hdr, rec, "MQSB", &bc->mwu_mqs, 1);
+    if ( bc->mwu_bq != HUGE_VAL )   bcf_update_info_float(hdr, rec, "BQB", &bc->mwu_bq, 1);
+#if CDF_MWU_TESTS
+    if ( bc->mwu_pos_cdf != HUGE_VAL )  bcf_update_info_float(hdr, rec, "RPB2", &bc->mwu_pos_cdf, 1);
+    if ( bc->mwu_mq_cdf != HUGE_VAL )   bcf_update_info_float(hdr, rec, "MQB2", &bc->mwu_mq_cdf, 1);
+    if ( bc->mwu_mqs_cdf != HUGE_VAL )  bcf_update_info_float(hdr, rec, "MQSB2", &bc->mwu_mqs_cdf, 1);
+    if ( bc->mwu_bq_cdf != HUGE_VAL )   bcf_update_info_float(hdr, rec, "BQB2", &bc->mwu_bq_cdf, 1);
+#endif
+    tmpf[0] = bc->ori_depth ? (float)bc->mq0/bc->ori_depth : 0;
+    bcf_update_info_float(hdr, rec, "MQ0F", tmpf, 1);
+
+    // FORMAT
+    rec->n_sample = bc->n;
+    bcf_update_format_int32(hdr, rec, "PL", bc->PL, nals*(nals+1)/2 * rec->n_sample);
+    if ( fmt_flag&B2B_FMT_DP )
+    {
+        int32_t *ptr = (int32_t*) bc->fmt_arr;
+        for (i=0; i<bc->n; i++)
+            ptr[i] = bc->DP4[4*i] + bc->DP4[4*i+1] + bc->DP4[4*i+2] + bc->DP4[4*i+3];
+        bcf_update_format_int32(hdr, rec, "DP", bc->fmt_arr, rec->n_sample);
+    }
+    if ( fmt_flag&B2B_FMT_DV )
+    {
+        int32_t *ptr = (int32_t*) bc->fmt_arr;
+        for (i=0; i<bc->n; i++)
+            ptr[i] = bc->DP4[4*i+2] + bc->DP4[4*i+3];
+        bcf_update_format_int32(hdr, rec, "DV", bc->fmt_arr, rec->n_sample);
+    }
+    if ( fmt_flag&B2B_FMT_SP )
+    {
+        int32_t *ptr = (int32_t*) bc->fmt_arr;
+        for (i=0; i<bc->n; i++)
+        {
+            int fwd_ref = bc->DP4[4*i], rev_ref =  bc->DP4[4*i+1], fwd_alt = bc->DP4[4*i+2], rev_alt = bc->DP4[4*i+3];
+            if ( fwd_ref+rev_ref<2 || fwd_alt+rev_alt<2 || fwd_ref+fwd_alt<2 || rev_ref+rev_alt<2 )
+                ptr[i] = 0;
+            else
+            {
+                double left, right, two;
+                kt_fisher_exact(fwd_ref, rev_ref, fwd_alt, rev_alt, &left, &right, &two);
+                int32_t x = (int)(-4.343 * log(two) + .499);
+                if (x > 255) x = 255;
+                ptr[i] = x;
+            }
+        }
+        bcf_update_format_int32(hdr, rec, "SP", bc->fmt_arr, rec->n_sample);
+    }
+    if ( fmt_flag&B2B_FMT_DP4 )
+        bcf_update_format_int32(hdr, rec, "DP4", bc->DP4, rec->n_sample*4);
+    if ( fmt_flag&B2B_FMT_DPR )
+        bcf_update_format_int32(hdr, rec, "DPR", bc->DPR+B2B_MAX_ALLELES, rec->n_sample*rec->n_allele);
+    if ( fmt_flag&B2B_INFO_DPR )
+        bcf_update_info_int32(hdr, rec, "DPR", bc->DPR, rec->n_allele);
+
+    return 0;
 }
diff --git a/samtools/bam2bcf.h b/samtools/bam2bcf.h
index b2b1825..3d532aa 100644
--- a/samtools/bam2bcf.h
+++ b/samtools/bam2bcf.h
@@ -1,64 +1,129 @@
+/*  bam2bcf.h -- variant calling.
+
+    Copyright (C) 2010-2012 Broad Institute.
+    Copyright (C) 2012-2014 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #ifndef BAM2BCF_H
 #define BAM2BCF_H
 
 #include <stdint.h>
+#include <htslib/vcf.h>
 #include "errmod.h"
-#include "bcftools/bcf.h"
+
+/**
+ *  A simplified version of Mann-Whitney U-test is calculated
+ *  by default (no CDF) because it is faster and seems to work
+ *  better in machine learning filtering. When enabled by setting
+ *  CDF_MWU_TESTS, additional annotations will appear on mpileup's
+ *  output (RPB2 in addition to RPB, etc.).
+ */
+#ifndef CDF_MWU_TESTS
+#define CDF_MWU_TESTS 0
+#endif
 
 #define B2B_INDEL_NULL 10000
 
-#define B2B_FMT_DP 0x1
-#define B2B_FMT_SP 0x2
-#define B2B_FMT_DV 0x4
+#define B2B_FMT_DP      (1<<0)
+#define B2B_FMT_SP      (1<<1)
+#define B2B_FMT_DV      (1<<2)
+#define B2B_FMT_DP4     (1<<3)
+#define B2B_FMT_DPR     (1<<4)
+#define B2B_INFO_DPR    (1<<5)
+
+#define B2B_MAX_ALLELES 5
 
 typedef struct __bcf_callaux_t {
-	int capQ, min_baseQ;
-	int openQ, extQ, tandemQ; // for indels
-	int min_support, max_support; // for collecting indel candidates
-	double min_frac, max_frac; // for collecting indel candidates
+    int capQ, min_baseQ;
+    int openQ, extQ, tandemQ; // for indels
+    uint32_t min_support, max_support; // for collecting indel candidates
+    float min_frac, max_frac; // for collecting indel candidates
     int per_sample_flt; // indel filtering strategy
-    int *ref_pos, *alt_pos, npos; // for ReadPosBias
-	// for internal uses
-	int max_bases;
-	int indel_types[4];
-	int maxins, indelreg;
+    int *ref_pos, *alt_pos, npos, *ref_mq, *alt_mq, *ref_bq, *alt_bq, *fwd_mqs, *rev_mqs, nqual; // for bias tests
+    // for internal uses
+    int max_bases;
+    int indel_types[4];     // indel lengths
+    int maxins, indelreg;
     int read_len;
-	char *inscns;
-	uint16_t *bases;
-	errmod_t *e;
-	void *rghash;
+    char *inscns;
+    uint16_t *bases;        // 5bit: unused, 6:quality, 1:is_rev, 4:2-bit base or indel allele (index to bcf_callaux_t.indel_types)
+    errmod_t *e;
+    void *rghash;
 } bcf_callaux_t;
 
 typedef struct {
-	int depth, n_supp, ori_depth, qsum[4];
-	unsigned int anno[16];
-	float p[25];
+    uint32_t ori_depth;
+    unsigned int mq0;
+    int32_t *DPR;
+    float qsum[4];
+    // The fields are:
+    //      depth fwd   .. ref (0) and non-ref (2)
+    //      depth rev   .. ref (1) and non-ref (3)
+    //      baseQ       .. ref (4) and non-ref (6)
+    //      baseQ^2     .. ref (5) and non-ref (7)
+    //      mapQ        .. ref (8) and non-ref (10)
+    //      mapQ^2      .. ref (9) and non-ref (11)
+    //      minDist     .. ref (12) and non-ref (14)
+    //      minDist^2   .. ref (13) and non-ref (15)
+    // Note that this probably needs a more thorough fix: int types in
+    // bcf_call_t do overflow with high-coverage data, such as exomes, and
+    // BCFv2 supports only floats which may not suffice.
+    double anno[16];
+    float p[25];        // phred-scaled likelihood of each genotype
 } bcf_callret1_t;
 
 typedef struct {
-	int a[5]; // alleles: ref, alt, alt2, alt3
-    float qsum[4];
-	int n, n_alleles, shift, ori_ref, unseen;
-	int n_supp; // number of supporting non-reference reads
-	unsigned int anno[16], depth, ori_depth;
-	uint8_t *PL;
+    int tid, pos;
+    bcf_hdr_t *bcf_hdr;
+    int a[5]; // alleles: ref, alt, alt2, alt3
+    float qsum[5];  // for the QS tag
+    int n, n_alleles, shift, ori_ref, unseen;
+    int n_supp; // number of supporting non-reference reads
+    double anno[16];
+    unsigned int depth, ori_depth, mq0;
+    int32_t *PL, *DP4, *DPR;
+    uint8_t *fmt_arr;
     float vdb; // variant distance bias
-    float read_pos_bias;
-    struct { float avg, var; int dp; } read_pos;
+    float mwu_pos, mwu_mq, mwu_bq, mwu_mqs;
+#if CDF_MWU_TESTS
+    float mwu_pos_cdf, mwu_mq_cdf, mwu_bq_cdf, mwu_mqs_cdf;
+#endif
+    float seg_bias;
+    kstring_t tmp;
 } bcf_call_t;
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-	bcf_callaux_t *bcf_call_init(double theta, int min_baseQ);
-	void bcf_call_destroy(bcf_callaux_t *bca);
-	int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r);
-	int bcf_call_combine(int n, const bcf_callret1_t *calls, bcf_callaux_t *bca, int ref_base /*4-bit*/, bcf_call_t *call);
-	int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int fmt_flag,
-					 const bcf_callaux_t *bca, const char *ref);
-	int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref,
-						  const void *rghash);
+    bcf_callaux_t *bcf_call_init(double theta, int min_baseQ);
+    void bcf_call_destroy(bcf_callaux_t *bca);
+    int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r);
+    int bcf_call_combine(int n, const bcf_callret1_t *calls, bcf_callaux_t *bca, int ref_base /*4-bit*/, bcf_call_t *call);
+    int bcf_call2bcf(bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int fmt_flag,
+                     const bcf_callaux_t *bca, const char *ref);
+    int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref,
+                          const void *rghash);
+    void bcf_callaux_clean(bcf_callaux_t *bca, bcf_call_t *call);
 
 #ifdef __cplusplus
 }
diff --git a/samtools/bam2bcf_indel.c.pysam.c b/samtools/bam2bcf_indel.c.pysam.c
index cdf16d0..480481b 100644
--- a/samtools/bam2bcf_indel.c.pysam.c
+++ b/samtools/bam2bcf_indel.c.pysam.c
@@ -1,5 +1,30 @@
 #include "pysam.h"
 
+/*  bam2bcf_indel.c -- indel caller.
+
+    Copyright (C) 2010, 2011 Broad Institute.
+    Copyright (C) 2012, 2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <assert.h>
 #include <ctype.h>
 #include <string.h>
@@ -7,494 +32,504 @@
 #include "bam2bcf.h"
 #include "kaln.h"
 #include "kprobaln.h"
-#include "khash.h"
+#include "htslib/khash.h"
 KHASH_SET_INIT_STR(rg)
 
-#include "ksort.h"
+#include "htslib/ksort.h"
 KSORT_INIT_GENERIC(uint32_t)
 
 #define MINUS_CONST 0x10000000
 #define INDEL_WINDOW_SIZE 50
 
+extern const char bam_nt16_nt4_table[];
+
 void *bcf_call_add_rg(void *_hash, const char *hdtext, const char *list)
 {
-	const char *s, *p, *q, *r, *t;
-	khash_t(rg) *hash;
-	if (list == 0 || hdtext == 0) return _hash;
-	if (_hash == 0) _hash = kh_init(rg);
-	hash = (khash_t(rg)*)_hash;
-	if ((s = strstr(hdtext, "@RG\t")) == 0) return hash;
-	do {
-		t = strstr(s + 4, "@RG\t"); // the next @RG
-		if ((p = strstr(s, "\tID:")) != 0) p += 4;
-		if ((q = strstr(s, "\tPL:")) != 0) q += 4;
-		if (p && q && (t == 0 || (p < t && q < t))) { // ID and PL are both present
-			int lp, lq;
-			char *x;
-			for (r = p; *r && *r != '\t' && *r != '\n'; ++r); lp = r - p;
-			for (r = q; *r && *r != '\t' && *r != '\n'; ++r); lq = r - q;
-			x = calloc((lp > lq? lp : lq) + 1, 1);
-			for (r = q; *r && *r != '\t' && *r != '\n'; ++r) x[r-q] = *r;
-			if (strstr(list, x)) { // insert ID to the hash table
-				khint_t k;
-				int ret;
-				for (r = p; *r && *r != '\t' && *r != '\n'; ++r) x[r-p] = *r;
-				x[r-p] = 0;
-				k = kh_get(rg, hash, x);
-				if (k == kh_end(hash)) k = kh_put(rg, hash, x, &ret);
-				else free(x);
-			} else free(x);
-		}
-		s = t;
-	} while (s);
-	return hash;
+    const char *s, *p, *q, *r, *t;
+    khash_t(rg) *hash;
+    if (list == 0 || hdtext == 0) return _hash;
+    if (_hash == 0) _hash = kh_init(rg);
+    hash = (khash_t(rg)*)_hash;
+    if ((s = strstr(hdtext, "@RG\t")) == 0) return hash;
+    do {
+        t = strstr(s + 4, "@RG\t"); // the next @RG
+        if ((p = strstr(s, "\tID:")) != 0) p += 4;
+        if ((q = strstr(s, "\tPL:")) != 0) q += 4;
+        if (p && q && (t == 0 || (p < t && q < t))) { // ID and PL are both present
+            int lp, lq;
+            char *x;
+            for (r = p; *r && *r != '\t' && *r != '\n'; ++r) { }
+            lp = r - p;
+            for (r = q; *r && *r != '\t' && *r != '\n'; ++r) { }
+            lq = r - q;
+            x = calloc((lp > lq? lp : lq) + 1, 1);
+            for (r = q; *r && *r != '\t' && *r != '\n'; ++r) x[r-q] = *r;
+            if (strstr(list, x)) { // insert ID to the hash table
+                khint_t k;
+                int ret;
+                for (r = p; *r && *r != '\t' && *r != '\n'; ++r) x[r-p] = *r;
+                x[r-p] = 0;
+                k = kh_get(rg, hash, x);
+                if (k == kh_end(hash)) k = kh_put(rg, hash, x, &ret);
+                else free(x);
+            } else free(x);
+        }
+        s = t;
+    } while (s);
+    return hash;
 }
 
 void bcf_call_del_rghash(void *_hash)
 {
-	khint_t k;
-	khash_t(rg) *hash = (khash_t(rg)*)_hash;
-	if (hash == 0) return;
-	for (k = kh_begin(hash); k < kh_end(hash); ++k)
-		if (kh_exist(hash, k))
-			free((char*)kh_key(hash, k));
-	kh_destroy(rg, hash);
+    khint_t k;
+    khash_t(rg) *hash = (khash_t(rg)*)_hash;
+    if (hash == 0) return;
+    for (k = kh_begin(hash); k < kh_end(hash); ++k)
+        if (kh_exist(hash, k))
+            free((char*)kh_key(hash, k));
+    kh_destroy(rg, hash);
 }
 
 static int tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
 {
-	int k, x = c->pos, y = 0, last_y = 0;
-	*_tpos = c->pos;
-	for (k = 0; k < c->n_cigar; ++k) {
-		int op = cigar[k] & BAM_CIGAR_MASK;
-		int l = cigar[k] >> BAM_CIGAR_SHIFT;
-		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-			if (c->pos > tpos) return y;
-			if (x + l > tpos) {
-				*_tpos = tpos;
-				return y + (tpos - x);
-			}
-			x += l; y += l;
-			last_y = y;
-		} else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
-		else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
-			if (x + l > tpos) {
-				*_tpos = is_left? x : x + l;
-				return y;
-			}
-			x += l;
-		}
-	}
-	*_tpos = x;
-	return last_y;
+    int k, x = c->pos, y = 0, last_y = 0;
+    *_tpos = c->pos;
+    for (k = 0; k < c->n_cigar; ++k) {
+        int op = cigar[k] & BAM_CIGAR_MASK;
+        int l = cigar[k] >> BAM_CIGAR_SHIFT;
+        if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+            if (c->pos > tpos) return y;
+            if (x + l > tpos) {
+                *_tpos = tpos;
+                return y + (tpos - x);
+            }
+            x += l; y += l;
+            last_y = y;
+        } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
+        else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
+            if (x + l > tpos) {
+                *_tpos = is_left? x : x + l;
+                return y;
+            }
+            x += l;
+        }
+    }
+    *_tpos = x;
+    return last_y;
 }
 // FIXME: check if the inserted sequence is consistent with the homopolymer run
 // l is the relative gap length and l_run is the length of the homopolymer on the reference
 static inline int est_seqQ(const bcf_callaux_t *bca, int l, int l_run)
 {
-	int q, qh;
-	q = bca->openQ + bca->extQ * (abs(l) - 1);
-	qh = l_run >= 3? (int)(bca->tandemQ * (double)abs(l) / l_run + .499) : 1000;
-	return q < qh? q : qh;
+    int q, qh;
+    q = bca->openQ + bca->extQ * (abs(l) - 1);
+    qh = l_run >= 3? (int)(bca->tandemQ * (double)abs(l) / l_run + .499) : 1000;
+    return q < qh? q : qh;
 }
 
 static inline int est_indelreg(int pos, const char *ref, int l, char *ins4)
 {
-	int i, j, max = 0, max_i = pos, score = 0;
-	l = abs(l);
-	for (i = pos + 1, j = 0; ref[i]; ++i, ++j) {
-		if (ins4) score += (toupper(ref[i]) != "ACGTN"[(int)ins4[j%l]])? -10 : 1;
-		else score += (toupper(ref[i]) != toupper(ref[pos+1+j%l]))? -10 : 1;
-		if (score < 0) break;
-		if (max < score) max = score, max_i = i;
-	}
-	return max_i - pos;
+    int i, j, max = 0, max_i = pos, score = 0;
+    l = abs(l);
+    for (i = pos + 1, j = 0; ref[i]; ++i, ++j) {
+        if (ins4) score += (toupper(ref[i]) != "ACGTN"[(int)ins4[j%l]])? -10 : 1;
+        else score += (toupper(ref[i]) != toupper(ref[pos+1+j%l]))? -10 : 1;
+        if (score < 0) break;
+        if (max < score) max = score, max_i = i;
+    }
+    return max_i - pos;
 }
 
 /*
- *  @n:  number of samples
+    notes:
+        - n .. number of samples
+        - the routine sets bam_pileup1_t.aux of each read as follows:
+            - 6: unused
+            - 6: the call; index to bcf_callaux_t.indel_types   .. (aux>>16)&0x3f
+            - 8: estimated sequence quality                     .. (aux>>8)&0xff
+            - 8: indel quality                                  .. aux&0xff
  */
 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref,
-					  const void *rghash)
+                      const void *rghash)
 {
-	int i, s, j, k, t, n_types, *types, max_rd_len, left, right, max_ins, *score1, *score2, max_ref2;
-	int N, K, l_run, ref_type, n_alt;
-	char *inscns = 0, *ref2, *query, **ref_sample;
-	khash_t(rg) *hash = (khash_t(rg)*)rghash;
-	if (ref == 0 || bca == 0) return -1;
-	// mark filtered reads
-	if (rghash) {
-		N = 0;
-		for (s = N = 0; s < n; ++s) {
-			for (i = 0; i < n_plp[s]; ++i) {
-				bam_pileup1_t *p = plp[s] + i;
-				const uint8_t *rg = bam_aux_get(p->b, "RG");
-				p->aux = 1; // filtered by default
-				if (rg) {
-					khint_t k = kh_get(rg, hash, (const char*)(rg + 1));
-					if (k != kh_end(hash)) p->aux = 0, ++N; // not filtered
-				}
-			}
-		}
-		if (N == 0) return -1; // no reads left
-	}
-	// determine if there is a gap
-	for (s = N = 0; s < n; ++s) {
-		for (i = 0; i < n_plp[s]; ++i)
-			if (plp[s][i].indel != 0) break;
-		if (i < n_plp[s]) break;
-	}
-	if (s == n) return -1; // there is no indel at this position.
-	for (s = N = 0; s < n; ++s) N += n_plp[s]; // N is the total number of reads
-	{ // find out how many types of indels are present
+    int i, s, j, k, t, n_types, *types, max_rd_len, left, right, max_ins, *score1, *score2, max_ref2;
+    int N, K, l_run, ref_type, n_alt;
+    char *inscns = 0, *ref2, *query, **ref_sample;
+    khash_t(rg) *hash = (khash_t(rg)*)rghash;
+    if (ref == 0 || bca == 0) return -1;
+    // mark filtered reads
+    if (rghash) {
+        N = 0;
+        for (s = N = 0; s < n; ++s) {
+            for (i = 0; i < n_plp[s]; ++i) {
+                bam_pileup1_t *p = plp[s] + i;
+                const uint8_t *rg = bam_aux_get(p->b, "RG");
+                p->aux = 1; // filtered by default
+                if (rg) {
+                    khint_t k = kh_get(rg, hash, (const char*)(rg + 1));
+                    if (k != kh_end(hash)) p->aux = 0, ++N; // not filtered
+                }
+            }
+        }
+        if (N == 0) return -1; // no reads left
+    }
+    // determine if there is a gap
+    for (s = N = 0; s < n; ++s) {
+        for (i = 0; i < n_plp[s]; ++i)
+            if (plp[s][i].indel != 0) break;
+        if (i < n_plp[s]) break;
+    }
+    if (s == n) return -1; // there is no indel at this position.
+    for (s = N = 0; s < n; ++s) N += n_plp[s]; // N is the total number of reads
+    { // find out how many types of indels are present
         bca->max_support = bca->max_frac = 0;
-		int m, n_alt = 0, n_tot = 0, indel_support_ok = 0;
-		uint32_t *aux;
-		aux = calloc(N + 1, 4);
-		m = max_rd_len = 0;
-		aux[m++] = MINUS_CONST; // zero indel is always a type
-		for (s = 0; s < n; ++s) {
-            int na = 0, nt = 0; 
-			for (i = 0; i < n_plp[s]; ++i) {
-				const bam_pileup1_t *p = plp[s] + i;
-				if (rghash == 0 || p->aux == 0) {
-					++nt;
-					if (p->indel != 0) {
-						++na;
-						aux[m++] = MINUS_CONST + p->indel;
-					}
-				}
-				j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
-				if (j > max_rd_len) max_rd_len = j;
-			}
+        int m, n_alt = 0, n_tot = 0, indel_support_ok = 0;
+        uint32_t *aux;
+        aux = calloc(N + 1, 4);
+        m = max_rd_len = 0;
+        aux[m++] = MINUS_CONST; // zero indel is always a type
+        for (s = 0; s < n; ++s) {
+            int na = 0, nt = 0;
+            for (i = 0; i < n_plp[s]; ++i) {
+                const bam_pileup1_t *p = plp[s] + i;
+                if (rghash == 0 || p->aux == 0) {
+                    ++nt;
+                    if (p->indel != 0) {
+                        ++na;
+                        aux[m++] = MINUS_CONST + p->indel;
+                    }
+                }
+                j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
+                if (j > max_rd_len) max_rd_len = j;
+            }
             float frac = (float)na/nt;
             if ( !indel_support_ok && na >= bca->min_support && frac >= bca->min_frac )
                 indel_support_ok = 1;
             if ( na > bca->max_support && frac > 0 ) bca->max_support = na, bca->max_frac = frac;
-            n_alt += na; 
+            n_alt += na;
             n_tot += nt;
-		}
+        }
         // To prevent long stretches of N's to be mistaken for indels (sometimes thousands of bases),
         //  check the number of N's in the sequence and skip places where half or more reference bases are Ns.
         int nN=0; for (i=pos; i-pos<max_rd_len && ref[i]; i++) if ( ref[i]=='N' ) nN++;
-        if ( nN*2>i ) { free(aux); return -1; }
+        if ( nN*2>(i-pos) ) { free(aux); return -1; }
 
-		ks_introsort(uint32_t, m, aux);
-		// squeeze out identical types
-		for (i = 1, n_types = 1; i < m; ++i)
-			if (aux[i] != aux[i-1]) ++n_types;
+        ks_introsort(uint32_t, m, aux);
+        // squeeze out identical types
+        for (i = 1, n_types = 1; i < m; ++i)
+            if (aux[i] != aux[i-1]) ++n_types;
         // Taking totals makes it hard to call rare indels
         if ( !bca->per_sample_flt )
             indel_support_ok = ( (float)n_alt / n_tot < bca->min_frac || n_alt < bca->min_support ) ? 0 : 1;
         if ( n_types == 1 || !indel_support_ok ) { // then skip
             free(aux); return -1;
         }
-		if (n_types >= 64) {
-			free(aux);
-			if (bam_verbose >= 2) 
-				fprintf(pysamerr, "[%s] excessive INDEL alleles at position %d. Skip the position.\n", __func__, pos + 1);
-			return -1;
-		}
-		types = (int*)calloc(n_types, sizeof(int));
-		t = 0;
-		types[t++] = aux[0] - MINUS_CONST; 
-		for (i = 1; i < m; ++i)
-			if (aux[i] != aux[i-1])
-				types[t++] = aux[i] - MINUS_CONST;
-		free(aux);
-		for (t = 0; t < n_types; ++t)
-			if (types[t] == 0) break;
-		ref_type = t; // the index of the reference type (0)
-	}
-	{ // calculate left and right boundary
-		left = pos > INDEL_WINDOW_SIZE? pos - INDEL_WINDOW_SIZE : 0;
-		right = pos + INDEL_WINDOW_SIZE;
-		if (types[0] < 0) right -= types[0];
-		// in case the alignments stand out the reference
-		for (i = pos; i < right; ++i)
-			if (ref[i] == 0) break;
-		right = i;
-	}
-	/* The following block fixes a long-existing flaw in the INDEL
-	 * calling model: the interference of nearby SNPs. However, it also
-	 * reduces the power because sometimes, substitutions caused by
-	 * indels are not distinguishable from true mutations. Multiple
-	 * sequence realignment helps to increase the power.
+        if (n_types >= 64) {
+            free(aux);
+            if (bam_verbose >= 2)
+                fprintf(pysamerr, "[%s] excessive INDEL alleles at position %d. Skip the position.\n", __func__, pos + 1);
+            return -1;
+        }
+        types = (int*)calloc(n_types, sizeof(int));
+        t = 0;
+        types[t++] = aux[0] - MINUS_CONST;
+        for (i = 1; i < m; ++i)
+            if (aux[i] != aux[i-1])
+                types[t++] = aux[i] - MINUS_CONST;
+        free(aux);
+        for (t = 0; t < n_types; ++t)
+            if (types[t] == 0) break;
+        ref_type = t; // the index of the reference type (0)
+    }
+    { // calculate left and right boundary
+        left = pos > INDEL_WINDOW_SIZE? pos - INDEL_WINDOW_SIZE : 0;
+        right = pos + INDEL_WINDOW_SIZE;
+        if (types[0] < 0) right -= types[0];
+        // in case the alignments stand out the reference
+        for (i = pos; i < right; ++i)
+            if (ref[i] == 0) break;
+        right = i;
+    }
+    /* The following block fixes a long-existing flaw in the INDEL
+     * calling model: the interference of nearby SNPs. However, it also
+     * reduces the power because sometimes, substitutions caused by
+     * indels are not distinguishable from true mutations. Multiple
+     * sequence realignment helps to increase the power.
      *
      * Masks mismatches present in at least 70% of the reads with 'N'.
-	 */
-	{ // construct per-sample consensus
-		int L = right - left + 1, max_i, max2_i;
-		uint32_t *cns, max, max2;
-		char *ref0, *r;
-		ref_sample = calloc(n, sizeof(void*));
-		cns = calloc(L, 4);
-		ref0 = calloc(L, 1);
-		for (i = 0; i < right - left; ++i)
-			ref0[i] = bam_nt16_table[(int)ref[i+left]];
-		for (s = 0; s < n; ++s) {
-			r = ref_sample[s] = calloc(L, 1);
-			memset(cns, 0, sizeof(int) * L);
-			// collect ref and non-ref counts
-			for (i = 0; i < n_plp[s]; ++i) {
-				bam_pileup1_t *p = plp[s] + i;
-				bam1_t *b = p->b;
-				uint32_t *cigar = bam1_cigar(b);
-				uint8_t *seq = bam1_seq(b);
-				int x = b->core.pos, y = 0;
-				for (k = 0; k < b->core.n_cigar; ++k) {
-					int op = cigar[k]&0xf;
-					int j, l = cigar[k]>>4;
-					if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-						for (j = 0; j < l; ++j)
-							if (x + j >= left && x + j < right)
-								cns[x+j-left] += (bam1_seqi(seq, y+j) == ref0[x+j-left])? 1 : 0x10000;
-						x += l; y += l;
-					} else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
-					else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
-				}
-			}
-			// determine the consensus
-			for (i = 0; i < right - left; ++i) r[i] = ref0[i];
-			max = max2 = 0; max_i = max2_i = -1;
-			for (i = 0; i < right - left; ++i) {
-				if (cns[i]>>16 >= max>>16) max2 = max, max2_i = max_i, max = cns[i], max_i = i;
-				else if (cns[i]>>16 >= max2>>16) max2 = cns[i], max2_i = i;
-			}
-			if ((double)(max&0xffff) / ((max&0xffff) + (max>>16)) >= 0.7) max_i = -1;
-			if ((double)(max2&0xffff) / ((max2&0xffff) + (max2>>16)) >= 0.7) max2_i = -1;
-			if (max_i >= 0) r[max_i] = 15;
-			if (max2_i >= 0) r[max2_i] = 15;
-			//for (i = 0; i < right - left; ++i) fputc("=ACMGRSVTWYHKDBN"[(int)r[i]], pysamerr); fputc('\n', pysamerr);
-		}
-		free(ref0); free(cns);
-	}
-	{ // the length of the homopolymer run around the current position
-		int c = bam_nt16_table[(int)ref[pos + 1]];
-		if (c == 15) l_run = 1;
-		else {
-			for (i = pos + 2; ref[i]; ++i)
-				if (bam_nt16_table[(int)ref[i]] != c) break;
-			l_run = i;
-			for (i = pos; i >= 0; --i)
-				if (bam_nt16_table[(int)ref[i]] != c) break;
-			l_run -= i + 1;
-		}
-	}
-	// construct the consensus sequence
-	max_ins = types[n_types - 1];   // max_ins is at least 0
-	if (max_ins > 0) {
-		int *inscns_aux = calloc(5 * n_types * max_ins, sizeof(int));
-		// count the number of occurrences of each base at each position for each type of insertion
-		for (t = 0; t < n_types; ++t) {
-			if (types[t] > 0) {
-				for (s = 0; s < n; ++s) {
-					for (i = 0; i < n_plp[s]; ++i) {
-						bam_pileup1_t *p = plp[s] + i;
-						if (p->indel == types[t]) {
-							uint8_t *seq = bam1_seq(p->b);
-							for (k = 1; k <= p->indel; ++k) {
-								int c = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos + k)];
+     */
+    { // construct per-sample consensus
+        int L = right - left + 1, max_i, max2_i;
+        uint32_t *cns, max, max2;
+        char *ref0, *r;
+        ref_sample = calloc(n, sizeof(char*));
+        cns = calloc(L, 4);
+        ref0 = calloc(L, 1);
+        for (i = 0; i < right - left; ++i)
+            ref0[i] = bam_nt16_table[(int)ref[i+left]];
+        for (s = 0; s < n; ++s) {
+            r = ref_sample[s] = calloc(L, 1);
+            memset(cns, 0, sizeof(int) * L);
+            // collect ref and non-ref counts
+            for (i = 0; i < n_plp[s]; ++i) {
+                bam_pileup1_t *p = plp[s] + i;
+                bam1_t *b = p->b;
+                uint32_t *cigar = bam1_cigar(b);
+                uint8_t *seq = bam1_seq(b);
+                int x = b->core.pos, y = 0;
+                for (k = 0; k < b->core.n_cigar; ++k) {
+                    int op = cigar[k]&0xf;
+                    int j, l = cigar[k]>>4;
+                    if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+                        for (j = 0; j < l; ++j)
+                            if (x + j >= left && x + j < right)
+                                cns[x+j-left] += (bam1_seqi(seq, y+j) == ref0[x+j-left])? 1 : 0x10000;
+                        x += l; y += l;
+                    } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
+                    else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
+                }
+            }
+            // determine the consensus
+            for (i = 0; i < right - left; ++i) r[i] = ref0[i];
+            max = max2 = 0; max_i = max2_i = -1;
+            for (i = 0; i < right - left; ++i) {
+                if (cns[i]>>16 >= max>>16) max2 = max, max2_i = max_i, max = cns[i], max_i = i;
+                else if (cns[i]>>16 >= max2>>16) max2 = cns[i], max2_i = i;
+            }
+            if ((double)(max&0xffff) / ((max&0xffff) + (max>>16)) >= 0.7) max_i = -1;
+            if ((double)(max2&0xffff) / ((max2&0xffff) + (max2>>16)) >= 0.7) max2_i = -1;
+            if (max_i >= 0) r[max_i] = 15;
+            if (max2_i >= 0) r[max2_i] = 15;
+            //for (i = 0; i < right - left; ++i) fputc("=ACMGRSVTWYHKDBN"[(int)r[i]], pysamerr); fputc('\n', pysamerr);
+        }
+        free(ref0); free(cns);
+    }
+    { // the length of the homopolymer run around the current position
+        int c = bam_nt16_table[(int)ref[pos + 1]];
+        if (c == 15) l_run = 1;
+        else {
+            for (i = pos + 2; ref[i]; ++i)
+                if (bam_nt16_table[(int)ref[i]] != c) break;
+            l_run = i;
+            for (i = pos; i >= 0; --i)
+                if (bam_nt16_table[(int)ref[i]] != c) break;
+            l_run -= i + 1;
+        }
+    }
+    // construct the consensus sequence
+    max_ins = types[n_types - 1];   // max_ins is at least 0
+    if (max_ins > 0) {
+        int *inscns_aux = calloc(5 * n_types * max_ins, sizeof(int));
+        // count the number of occurrences of each base at each position for each type of insertion
+        for (t = 0; t < n_types; ++t) {
+            if (types[t] > 0) {
+                for (s = 0; s < n; ++s) {
+                    for (i = 0; i < n_plp[s]; ++i) {
+                        bam_pileup1_t *p = plp[s] + i;
+                        if (p->indel == types[t]) {
+                            uint8_t *seq = bam1_seq(p->b);
+                            for (k = 1; k <= p->indel; ++k) {
+                                int c = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos + k)];
                                 assert(c<5);
-								++inscns_aux[(t*max_ins+(k-1))*5 + c];
-							}
-						}
-					}
-				}
-			}
-		}
-		// use the majority rule to construct the consensus
-		inscns = calloc(n_types * max_ins, 1);
-		for (t = 0; t < n_types; ++t) {
-			for (j = 0; j < types[t]; ++j) {
-				int max = 0, max_k = -1, *ia = &inscns_aux[(t*max_ins+j)*5];
-				for (k = 0; k < 5; ++k)
-					if (ia[k] > max)
-						max = ia[k], max_k = k;
-				inscns[t*max_ins + j] = max? max_k : 4;
+                                ++inscns_aux[(t*max_ins+(k-1))*5 + c];
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        // use the majority rule to construct the consensus
+        inscns = calloc(n_types * max_ins, 1);
+        for (t = 0; t < n_types; ++t) {
+            for (j = 0; j < types[t]; ++j) {
+                int max = 0, max_k = -1, *ia = &inscns_aux[(t*max_ins+j)*5];
+                for (k = 0; k < 5; ++k)
+                    if (ia[k] > max)
+                        max = ia[k], max_k = k;
+                inscns[t*max_ins + j] = max? max_k : 4;
                 if ( max_k==4 ) { types[t] = 0; break; } // discard insertions which contain N's
-			}
-		}
-		free(inscns_aux);
-	}
-	// compute the likelihood given each type of indel for each read
-	max_ref2 = right - left + 2 + 2 * (max_ins > -types[0]? max_ins : -types[0]);
-	ref2  = calloc(max_ref2, 1);
-	query = calloc(right - left + max_rd_len + max_ins + 2, 1);
-	score1 = calloc(N * n_types, sizeof(int));
-	score2 = calloc(N * n_types, sizeof(int));
-	bca->indelreg = 0;
-	for (t = 0; t < n_types; ++t) {
-		int l, ir;
-		kpa_par_t apf1 = { 1e-4, 1e-2, 10 }, apf2 = { 1e-6, 1e-3, 10 };
-		apf1.bw = apf2.bw = abs(types[t]) + 3;
-		// compute indelreg
-		if (types[t] == 0) ir = 0;
-		else if (types[t] > 0) ir = est_indelreg(pos, ref, types[t], &inscns[t*max_ins]);
-		else ir = est_indelreg(pos, ref, -types[t], 0);
-		if (ir > bca->indelreg) bca->indelreg = ir;
-//		fprintf(pysamerr, "%d, %d, %d\n", pos, types[t], ir);
-		// realignment
-		for (s = K = 0; s < n; ++s) {
-			// write ref2
-			for (k = 0, j = left; j <= pos; ++j)
-				ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
-			if (types[t] <= 0) j += -types[t];
-			else for (l = 0; l < types[t]; ++l)
-					 ref2[k++] = inscns[t*max_ins + l];
-			for (; j < right && ref[j]; ++j)
-				ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
-			for (; k < max_ref2; ++k) ref2[k] = 4;
-			if (j < right) right = j;
-			// align each read to ref2
-			for (i = 0; i < n_plp[s]; ++i, ++K) {
-				bam_pileup1_t *p = plp[s] + i;
-				int qbeg, qend, tbeg, tend, sc, kk;
-				uint8_t *seq = bam1_seq(p->b);
-				uint32_t *cigar = bam1_cigar(p->b);
-				if (p->b->core.flag&4) continue; // unmapped reads
-				// FIXME: the following loop should be better moved outside; nonetheless, realignment should be much slower anyway.
-				for (kk = 0; kk < p->b->core.n_cigar; ++kk)
-					if ((cigar[kk]&BAM_CIGAR_MASK) == BAM_CREF_SKIP) break;
-				if (kk < p->b->core.n_cigar) continue;
-				// FIXME: the following skips soft clips, but using them may be more sensitive.
-				// determine the start and end of sequences for alignment
-				qbeg = tpos2qpos(&p->b->core, bam1_cigar(p->b), left,  0, &tbeg);
-				qend = tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
-				if (types[t] < 0) {
-					int l = -types[t];
-					tbeg = tbeg - l > left?  tbeg - l : left;
-				}
-				// write the query sequence
-				for (l = qbeg; l < qend; ++l)
-					query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
-				{ // do realignment; this is the bottleneck
-					const uint8_t *qual = bam1_qual(p->b), *bq;
-					uint8_t *qq;
-					qq = calloc(qend - qbeg, 1);
-					bq = (uint8_t*)bam_aux_get(p->b, "ZQ");
-					if (bq) ++bq; // skip type
-					for (l = qbeg; l < qend; ++l) {
-						qq[l - qbeg] = bq? qual[l] + (bq[l] - 64) : qual[l];
-						if (qq[l - qbeg] > 30) qq[l - qbeg] = 30;
-						if (qq[l - qbeg] < 7) qq[l - qbeg] = 7;
-					}
-					sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
-									(uint8_t*)query, qend - qbeg, qq, &apf1, 0, 0);
-					l = (int)(100. * sc / (qend - qbeg) + .499); // used for adjusting indelQ below
-					if (l > 255) l = 255;
-					score1[K*n_types + t] = score2[K*n_types + t] = sc<<8 | l;
-					if (sc > 5) {
-						sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
-										(uint8_t*)query, qend - qbeg, qq, &apf2, 0, 0);
-						l = (int)(100. * sc / (qend - qbeg) + .499);
-						if (l > 255) l = 255;
-						score2[K*n_types + t] = sc<<8 | l;
-					}
-					free(qq);
-				}
+            }
+        }
+        free(inscns_aux);
+    }
+    // compute the likelihood given each type of indel for each read
+    max_ref2 = right - left + 2 + 2 * (max_ins > -types[0]? max_ins : -types[0]);
+    ref2  = calloc(max_ref2, 1);
+    query = calloc(right - left + max_rd_len + max_ins + 2, 1);
+    score1 = calloc(N * n_types, sizeof(int));
+    score2 = calloc(N * n_types, sizeof(int));
+    bca->indelreg = 0;
+    for (t = 0; t < n_types; ++t) {
+        int l, ir;
+        kpa_par_t apf1 = { 1e-4, 1e-2, 10 }, apf2 = { 1e-6, 1e-3, 10 };
+        apf1.bw = apf2.bw = abs(types[t]) + 3;
+        // compute indelreg
+        if (types[t] == 0) ir = 0;
+        else if (types[t] > 0) ir = est_indelreg(pos, ref, types[t], &inscns[t*max_ins]);
+        else ir = est_indelreg(pos, ref, -types[t], 0);
+        if (ir > bca->indelreg) bca->indelreg = ir;
+//      fprintf(pysamerr, "%d, %d, %d\n", pos, types[t], ir);
+        // realignment
+        for (s = K = 0; s < n; ++s) {
+            // write ref2
+            for (k = 0, j = left; j <= pos; ++j)
+                ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
+            if (types[t] <= 0) j += -types[t];
+            else for (l = 0; l < types[t]; ++l)
+                     ref2[k++] = inscns[t*max_ins + l];
+            for (; j < right && ref[j]; ++j)
+                ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
+            for (; k < max_ref2; ++k) ref2[k] = 4;
+            if (j < right) right = j;
+            // align each read to ref2
+            for (i = 0; i < n_plp[s]; ++i, ++K) {
+                bam_pileup1_t *p = plp[s] + i;
+                int qbeg, qend, tbeg, tend, sc, kk;
+                uint8_t *seq = bam1_seq(p->b);
+                uint32_t *cigar = bam1_cigar(p->b);
+                if (p->b->core.flag&4) continue; // unmapped reads
+                // FIXME: the following loop should be better moved outside; nonetheless, realignment should be much slower anyway.
+                for (kk = 0; kk < p->b->core.n_cigar; ++kk)
+                    if ((cigar[kk]&BAM_CIGAR_MASK) == BAM_CREF_SKIP) break;
+                if (kk < p->b->core.n_cigar) continue;
+                // FIXME: the following skips soft clips, but using them may be more sensitive.
+                // determine the start and end of sequences for alignment
+                qbeg = tpos2qpos(&p->b->core, bam1_cigar(p->b), left,  0, &tbeg);
+                qend = tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
+                if (types[t] < 0) {
+                    int l = -types[t];
+                    tbeg = tbeg - l > left?  tbeg - l : left;
+                }
+                // write the query sequence
+                for (l = qbeg; l < qend; ++l)
+                    query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
+                { // do realignment; this is the bottleneck
+                    const uint8_t *qual = bam1_qual(p->b), *bq;
+                    uint8_t *qq;
+                    qq = calloc(qend - qbeg, 1);
+                    bq = (uint8_t*)bam_aux_get(p->b, "ZQ");
+                    if (bq) ++bq; // skip type
+                    for (l = qbeg; l < qend; ++l) {
+                        qq[l - qbeg] = bq? qual[l] + (bq[l] - 64) : qual[l];
+                        if (qq[l - qbeg] > 30) qq[l - qbeg] = 30;
+                        if (qq[l - qbeg] < 7) qq[l - qbeg] = 7;
+                    }
+                    sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
+                                    (uint8_t*)query, qend - qbeg, qq, &apf1, 0, 0);
+                    l = (int)(100. * sc / (qend - qbeg) + .499); // used for adjusting indelQ below
+                    if (l > 255) l = 255;
+                    score1[K*n_types + t] = score2[K*n_types + t] = sc<<8 | l;
+                    if (sc > 5) {
+                        sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
+                                        (uint8_t*)query, qend - qbeg, qq, &apf2, 0, 0);
+                        l = (int)(100. * sc / (qend - qbeg) + .499);
+                        if (l > 255) l = 255;
+                        score2[K*n_types + t] = sc<<8 | l;
+                    }
+                    free(qq);
+                }
 /*
-				for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
-					fputc("ACGTN"[(int)ref2[tbeg-left+l]], pysamerr);
-				fputc('\n', pysamerr);
-				for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], pysamerr);
-				fputc('\n', pysamerr);
-				fprintf(pysamerr, "pos=%d type=%d read=%d:%d name=%s qbeg=%d tbeg=%d score=%d\n", pos, types[t], s, i, bam1_qname(p->b), qbeg, tbeg, sc);
+                for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
+                    fputc("ACGTN"[(int)ref2[tbeg-left+l]], pysamerr);
+                fputc('\n', pysamerr);
+                for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], pysamerr);
+                fputc('\n', pysamerr);
+                fprintf(pysamerr, "pos=%d type=%d read=%d:%d name=%s qbeg=%d tbeg=%d score=%d\n", pos, types[t], s, i, bam1_qname(p->b), qbeg, tbeg, sc);
 */
-			}
-		}
-	}
-	free(ref2); free(query);
-	{ // compute indelQ
-		int *sc, tmp, *sumq;
-		sc   = alloca(n_types * sizeof(int));
-		sumq = alloca(n_types * sizeof(int));
-		memset(sumq, 0, sizeof(int) * n_types);
-		for (s = K = 0; s < n; ++s) {
-			for (i = 0; i < n_plp[s]; ++i, ++K) {
-				bam_pileup1_t *p = plp[s] + i;
-				int *sct = &score1[K*n_types], indelQ1, indelQ2, seqQ, indelQ;
-				for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
-				for (t = 1; t < n_types; ++t) // insertion sort
-					for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
-						tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
-				/* errmod_cal() assumes that if the call is wrong, the
-				 * likelihoods of other events are equal. This is about
-				 * right for substitutions, but is not desired for
-				 * indels. To reuse errmod_cal(), I have to make
-				 * compromise for multi-allelic indels.
-				 */
-				if ((sc[0]&0x3f) == ref_type) {
-					indelQ1 = (sc[1]>>14) - (sc[0]>>14);
-					seqQ = est_seqQ(bca, types[sc[1]&0x3f], l_run);
-				} else {
-					for (t = 0; t < n_types; ++t) // look for the reference type
-						if ((sc[t]&0x3f) == ref_type) break;
-					indelQ1 = (sc[t]>>14) - (sc[0]>>14);
-					seqQ = est_seqQ(bca, types[sc[0]&0x3f], l_run);
-				}
-				tmp = sc[0]>>6 & 0xff;
-				indelQ1 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ1 + .499); // reduce indelQ
-				sct = &score2[K*n_types];
-				for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
-				for (t = 1; t < n_types; ++t) // insertion sort
-					for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
-						tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
-				if ((sc[0]&0x3f) == ref_type) {
-					indelQ2 = (sc[1]>>14) - (sc[0]>>14);
-				} else {
-					for (t = 0; t < n_types; ++t) // look for the reference type
-						if ((sc[t]&0x3f) == ref_type) break;
-					indelQ2 = (sc[t]>>14) - (sc[0]>>14);
-				}
-				tmp = sc[0]>>6 & 0xff;
-				indelQ2 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ2 + .499);
-				// pick the smaller between indelQ1 and indelQ2
-				indelQ = indelQ1 < indelQ2? indelQ1 : indelQ2;
-				if (indelQ > 255) indelQ = 255;
-				if (seqQ > 255) seqQ = 255;
-				p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ; // use 22 bits in total
-				sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ;
-//				fprintf(pysamerr, "pos=%d read=%d:%d name=%s call=%d indelQ=%d seqQ=%d\n", pos, s, i, bam1_qname(p->b), types[sc[0]&0x3f], indelQ, seqQ);
-			}
-		}
-		// determine bca->indel_types[] and bca->inscns
-		bca->maxins = max_ins;
-		bca->inscns = realloc(bca->inscns, bca->maxins * 4);
-		for (t = 0; t < n_types; ++t)
-			sumq[t] = sumq[t]<<6 | t;
-		for (t = 1; t < n_types; ++t) // insertion sort
-			for (j = t; j > 0 && sumq[j] > sumq[j-1]; --j)
-				tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
-		for (t = 0; t < n_types; ++t) // look for the reference type
-			if ((sumq[t]&0x3f) == ref_type) break;
-		if (t) { // then move the reference type to the first
-			tmp = sumq[t];
-			for (; t > 0; --t) sumq[t] = sumq[t-1];
-			sumq[0] = tmp;
-		}
-		for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
-		for (t = 0; t < 4 && t < n_types; ++t) {
-			bca->indel_types[t] = types[sumq[t]&0x3f];
-			memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
-		}
-		// update p->aux
-		for (s = n_alt = 0; s < n; ++s) {
-			for (i = 0; i < n_plp[s]; ++i) {
-				bam_pileup1_t *p = plp[s] + i;
-				int x = types[p->aux>>16&0x3f];
-				for (j = 0; j < 4; ++j)
-					if (x == bca->indel_types[j]) break;
-				p->aux = j<<16 | (j == 4? 0 : (p->aux&0xffff));
-				if ((p->aux>>16&0x3f) > 0) ++n_alt;
-//				fprintf(pysamerr, "X pos=%d read=%d:%d name=%s call=%d type=%d q=%d seqQ=%d\n", pos, s, i, bam1_qname(p->b), p->aux>>16&63, bca->indel_types[p->aux>>16&63], p->aux&0xff, p->aux>>8&0xff);
-			}
-		}		
-	}
-	free(score1); free(score2);
-	// free
-	for (i = 0; i < n; ++i) free(ref_sample[i]);
-	free(ref_sample);
-	free(types); free(inscns);
-	return n_alt > 0? 0 : -1;
+            }
+        }
+    }
+    free(ref2); free(query);
+    { // compute indelQ
+        int *sc, tmp, *sumq;
+        sc   = alloca(n_types * sizeof(int));
+        sumq = alloca(n_types * sizeof(int));
+        memset(sumq, 0, sizeof(int) * n_types);
+        for (s = K = 0; s < n; ++s) {
+            for (i = 0; i < n_plp[s]; ++i, ++K) {
+                bam_pileup1_t *p = plp[s] + i;
+                int *sct = &score1[K*n_types], indelQ1, indelQ2, seqQ, indelQ;
+                for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
+                for (t = 1; t < n_types; ++t) // insertion sort
+                    for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
+                        tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
+                /* errmod_cal() assumes that if the call is wrong, the
+                 * likelihoods of other events are equal. This is about
+                 * right for substitutions, but is not desired for
+                 * indels. To reuse errmod_cal(), I have to make
+                 * compromise for multi-allelic indels.
+                 */
+                if ((sc[0]&0x3f) == ref_type) {
+                    indelQ1 = (sc[1]>>14) - (sc[0]>>14);
+                    seqQ = est_seqQ(bca, types[sc[1]&0x3f], l_run);
+                } else {
+                    for (t = 0; t < n_types; ++t) // look for the reference type
+                        if ((sc[t]&0x3f) == ref_type) break;
+                    indelQ1 = (sc[t]>>14) - (sc[0]>>14);
+                    seqQ = est_seqQ(bca, types[sc[0]&0x3f], l_run);
+                }
+                tmp = sc[0]>>6 & 0xff;
+                indelQ1 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ1 + .499); // reduce indelQ
+                sct = &score2[K*n_types];
+                for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
+                for (t = 1; t < n_types; ++t) // insertion sort
+                    for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
+                        tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
+                if ((sc[0]&0x3f) == ref_type) {
+                    indelQ2 = (sc[1]>>14) - (sc[0]>>14);
+                } else {
+                    for (t = 0; t < n_types; ++t) // look for the reference type
+                        if ((sc[t]&0x3f) == ref_type) break;
+                    indelQ2 = (sc[t]>>14) - (sc[0]>>14);
+                }
+                tmp = sc[0]>>6 & 0xff;
+                indelQ2 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ2 + .499);
+                // pick the smaller between indelQ1 and indelQ2
+                indelQ = indelQ1 < indelQ2? indelQ1 : indelQ2;
+                if (indelQ > 255) indelQ = 255;
+                if (seqQ > 255) seqQ = 255;
+                p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ; // use 22 bits in total
+                sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ;
+//              fprintf(pysamerr, "pos=%d read=%d:%d name=%s call=%d indelQ=%d seqQ=%d\n", pos, s, i, bam1_qname(p->b), types[sc[0]&0x3f], indelQ, seqQ);
+            }
+        }
+        // determine bca->indel_types[] and bca->inscns
+        bca->maxins = max_ins;
+        bca->inscns = realloc(bca->inscns, bca->maxins * 4);
+        for (t = 0; t < n_types; ++t)
+            sumq[t] = sumq[t]<<6 | t;
+        for (t = 1; t < n_types; ++t) // insertion sort
+            for (j = t; j > 0 && sumq[j] > sumq[j-1]; --j)
+                tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
+        for (t = 0; t < n_types; ++t) // look for the reference type
+            if ((sumq[t]&0x3f) == ref_type) break;
+        if (t) { // then move the reference type to the first
+            tmp = sumq[t];
+            for (; t > 0; --t) sumq[t] = sumq[t-1];
+            sumq[0] = tmp;
+        }
+        for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
+        for (t = 0; t < 4 && t < n_types; ++t) {
+            bca->indel_types[t] = types[sumq[t]&0x3f];
+            memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
+        }
+        // update p->aux
+        for (s = n_alt = 0; s < n; ++s) {
+            for (i = 0; i < n_plp[s]; ++i) {
+                bam_pileup1_t *p = plp[s] + i;
+                int x = types[p->aux>>16&0x3f];
+                for (j = 0; j < 4; ++j)
+                    if (x == bca->indel_types[j]) break;
+                p->aux = j<<16 | (j == 4? 0 : (p->aux&0xffff));
+                if ((p->aux>>16&0x3f) > 0) ++n_alt;
+                //fprintf(pysamerr, "X pos=%d read=%d:%d name=%s call=%d type=%d seqQ=%d indelQ=%d\n", pos, s, i, bam1_qname(p->b), (p->aux>>16)&0x3f, bca->indel_types[(p->aux>>16)&0x3f], (p->aux>>8)&0xff, p->aux&0xff);
+            }
+        }
+    }
+    free(score1); free(score2);
+    // free
+    for (i = 0; i < n; ++i) free(ref_sample[i]);
+    free(ref_sample);
+    free(types); free(inscns);
+    return n_alt > 0? 0 : -1;
 }
diff --git a/samtools/bam2depth.c.pysam.c b/samtools/bam2depth.c.pysam.c
index a27e8ba..c991e08 100644
--- a/samtools/bam2depth.c.pysam.c
+++ b/samtools/bam2depth.c.pysam.c
@@ -1,21 +1,49 @@
 #include "pysam.h"
 
+/*  bam2depth.c -- depth subcommand.
+
+    Copyright (C) 2011, 2012 Broad Institute.
+    Copyright (C) 2012-2014 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 /* This program demonstrates how to generate pileup from multiple BAMs
  * simutaneously, to achieve random access and to use the BED interface.
  * To compile this program separately, you may:
  *
- *   gcc -g -O2 -Wall -o bam2depth -D_MAIN_BAM2DEPTH bam2depth.c -L. -lbam -lz
+ *   gcc -g -O2 -Wall -o bam2depth -D_MAIN_BAM2DEPTH bam2depth.c -lhts -lz
  */
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <unistd.h>
-#include "bam.h"
+#include "htslib/sam.h"
+#include "samtools.h"
 
 typedef struct {     // auxiliary data structure
-	bamFile fp;      // the file handler
-	bam_iter_t iter; // NULL if a region not specified
-	int min_mapQ, min_len; // mapQ filter; length filter
+    samFile *fp;     // the file handle
+    bam_hdr_t *hdr;  // the file header
+    hts_itr_t *iter; // NULL if a region not specified
+    int min_mapQ, min_len; // mapQ filter; length filter
 } aux_t;
 
 void *bed_read(const char *fn); // read a BED or position list file
@@ -25,44 +53,48 @@ int bed_overlap(const void *_h, const char *chr, int beg, int end); // test if c
 // This function reads a BAM alignment from one BAM file.
 static int read_bam(void *data, bam1_t *b) // read level filters better go here to avoid pileup
 {
-	aux_t *aux = (aux_t*)data; // data in fact is a pointer to an auxiliary structure
-	int ret = aux->iter? bam_iter_read(aux->fp, aux->iter, b) : bam_read1(aux->fp, b);
-	if (!(b->core.flag&BAM_FUNMAP)) {
-		if ((int)b->core.qual < aux->min_mapQ) b->core.flag |= BAM_FUNMAP;
-		else if (aux->min_len && bam_cigar2qlen(&b->core, bam1_cigar(b)) < aux->min_len) b->core.flag |= BAM_FUNMAP;
-	}
-	return ret;
+    aux_t *aux = (aux_t*)data; // data in fact is a pointer to an auxiliary structure
+    int ret;
+    while (1)
+    {
+        ret = aux->iter? sam_itr_next(aux->fp, aux->iter, b) : sam_read1(aux->fp, aux->hdr, b);
+        if ( ret<0 ) break;
+        if ( b->core.flag & (BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP) ) continue;
+        if ( (int)b->core.qual < aux->min_mapQ ) continue;
+        if ( aux->min_len && bam_cigar2qlen(b->core.n_cigar, bam_get_cigar(b)) < aux->min_len ) continue;
+        break;
+    }
+    return ret;
 }
 
 int read_file_list(const char *file_list,int *n,char **argv[]);
 
-#ifdef _MAIN_BAM2DEPTH
-int main(int argc, char *argv[])
-#else
 int main_depth(int argc, char *argv[])
-#endif
 {
-	int i, n, tid, beg, end, pos, *n_plp, baseQ = 0, mapQ = 0, min_len = 0, nfiles;
-	const bam_pileup1_t **plp;
-	char *reg = 0; // specified region
-	void *bed = 0; // BED data structure
+    int i, n, tid, beg, end, pos, *n_plp, baseQ = 0, mapQ = 0, min_len = 0, status = EXIT_SUCCESS, nfiles;
+    const bam_pileup1_t **plp;
+    char *reg = 0; // specified region
+    void *bed = 0; // BED data structure
     char *file_list = NULL, **fn = NULL;
-	bam_header_t *h = 0; // BAM header of the 1st input
-	aux_t **data;
-	bam_mplp_t mplp;
-
-	// parse the command line
-	while ((n = getopt(argc, argv, "r:b:q:Q:l:f:")) >= 0) {
-		switch (n) {
-			case 'l': min_len = atoi(optarg); break; // minimum query length
-			case 'r': reg = strdup(optarg); break;   // parsing a region requires a BAM header
-			case 'b': bed = bed_read(optarg); break; // BED or position list file can be parsed now
-			case 'q': baseQ = atoi(optarg); break;   // base quality threshold
-			case 'Q': mapQ = atoi(optarg); break;    // mapping quality threshold
-			case 'f': file_list = optarg; break;
-		}
-	}
-	if (optind == argc && !file_list) {
+    bam_hdr_t *h = NULL; // BAM header of the 1st input
+    aux_t **data;
+    bam_mplp_t mplp;
+
+    // parse the command line
+    while ((n = getopt(argc, argv, "r:b:q:Q:l:f:")) >= 0) {
+        switch (n) {
+            case 'l': min_len = atoi(optarg); break; // minimum query length
+            case 'r': reg = strdup(optarg); break;   // parsing a region requires a BAM header
+            case 'b':
+                bed = bed_read(optarg); // BED or position list file can be parsed now
+                if (!bed) { print_error_errno("Could not read file \"%s\"", optarg); return 1; }
+                break;
+            case 'q': baseQ = atoi(optarg); break;   // base quality threshold
+            case 'Q': mapQ = atoi(optarg); break;    // mapping quality threshold
+            case 'f': file_list = optarg; break;
+        }
+    }
+    if (optind == argc && !file_list) {
         fprintf(pysamerr, "\n");
         fprintf(pysamerr, "Usage: samtools depth [options] in1.bam [in2.bam [...]]\n");
         fprintf(pysamerr, "Options:\n");
@@ -73,11 +105,11 @@ int main_depth(int argc, char *argv[])
         fprintf(pysamerr, "   -Q <int>            mapping quality threshold\n");
         fprintf(pysamerr, "   -r <chr:from-to>    region\n");
         fprintf(pysamerr, "\n");
-		return 1;
-	}
+        return 1;
+    }
 
-	// initialize the auxiliary data structures
-    if (file_list) 
+    // initialize the auxiliary data structures
+    if (file_list)
     {
         if ( read_file_list(file_list,&nfiles,&fn) ) return 1;
         n = nfiles;
@@ -86,60 +118,84 @@ int main_depth(int argc, char *argv[])
     }
     else
         n = argc - optind; // the number of BAMs on the command line
-	data = calloc(n, sizeof(void*)); // data[i] for the i-th input
-	beg = 0; end = 1<<30; tid = -1;  // set the default region
-	for (i = 0; i < n; ++i) {
-		bam_header_t *htmp;
-		data[i] = calloc(1, sizeof(aux_t));
-		data[i]->fp = bam_open(argv[optind+i], "r"); // open BAM
-		data[i]->min_mapQ = mapQ;                    // set the mapQ filter
-		data[i]->min_len  = min_len;                 // set the qlen filter
-		htmp = bam_header_read(data[i]->fp);         // read the BAM header
-		if (i == 0) {
-			h = htmp; // keep the header of the 1st BAM
-			if (reg) bam_parse_region(h, reg, &tid, &beg, &end); // also parse the region
-		} else bam_header_destroy(htmp); // if not the 1st BAM, trash the header
-		if (tid >= 0) { // if a region is specified and parsed successfully
-			bam_index_t *idx = bam_index_load(argv[optind+i]);  // load the index
-			data[i]->iter = bam_iter_query(idx, tid, beg, end); // set the iterator
-			bam_index_destroy(idx); // the index is not needed any more; phase out of the memory
-		}
-	}
-
-	// the core multi-pileup loop
-	mplp = bam_mplp_init(n, read_bam, (void**)data); // initialization
-	n_plp = calloc(n, sizeof(int)); // n_plp[i] is the number of covering reads from the i-th BAM
-	plp = calloc(n, sizeof(void*)); // plp[i] points to the array of covering reads (internal in mplp)
-	while (bam_mplp_auto(mplp, &tid, &pos, n_plp, plp) > 0) { // come to the next covered position
-		if (pos < beg || pos >= end) continue; // out of range; skip
-		if (bed && bed_overlap(bed, h->target_name[tid], pos, pos + 1) == 0) continue; // not in BED; skip
-		fputs(h->target_name[tid], stdout); printf("\t%d", pos+1); // a customized printf() would be faster
-		for (i = 0; i < n; ++i) { // base level filters have to go here
-			int j, m = 0;
-			for (j = 0; j < n_plp[i]; ++j) {
-				const bam_pileup1_t *p = plp[i] + j; // DON'T modfity plp[][] unless you really know
-				if (p->is_del || p->is_refskip) ++m; // having dels or refskips at tid:pos
-				else if (bam1_qual(p->b)[p->qpos] < baseQ) ++m; // low base quality
-			}
-			printf("\t%d", n_plp[i] - m); // this the depth to output
-		}
-		putchar('\n');
-	}
-	free(n_plp); free(plp);
-	bam_mplp_destroy(mplp);
-
-	bam_header_destroy(h);
-	for (i = 0; i < n; ++i) {
-		bam_close(data[i]->fp);
-		if (data[i]->iter) bam_iter_destroy(data[i]->iter);
-		free(data[i]);
-	}
-	free(data); free(reg);
-	if (bed) bed_destroy(bed);
+    data = calloc(n, sizeof(aux_t*)); // data[i] for the i-th input
+    beg = 0; end = 1<<30;  // set the default region
+    for (i = 0; i < n; ++i) {
+        data[i] = calloc(1, sizeof(aux_t));
+        data[i]->fp = sam_open(argv[optind+i], "r"); // open BAM
+        if (data[i]->fp == NULL) {
+            print_error_errno("Could not open \"%s\"", argv[optind+i]);
+            status = EXIT_FAILURE;
+            goto depth_end;
+        }
+        data[i]->min_mapQ = mapQ;                    // set the mapQ filter
+        data[i]->min_len  = min_len;                 // set the qlen filter
+        data[i]->hdr = sam_hdr_read(data[i]->fp);    // read the BAM header
+        if (reg) { // if a region is specified
+            hts_idx_t *idx = sam_index_load(data[i]->fp, argv[optind+i]);  // load the index
+            if (idx == NULL) {
+                print_error("can't load index for \"%s\"", argv[optind+i]);
+                status = EXIT_FAILURE;
+                goto depth_end;
+            }
+            data[i]->iter = sam_itr_querys(idx, data[i]->hdr, reg); // set the iterator
+            hts_idx_destroy(idx); // the index is not needed any more; free the memory
+            if (data[i]->iter == NULL) {
+                print_error("can't parse region \"%s\"", reg);
+                status = EXIT_FAILURE;
+                goto depth_end;
+            }
+        }
+    }
+
+    h = data[0]->hdr; // easy access to the header of the 1st BAM
+    if (reg) {
+        beg = data[0]->iter->beg; // and to the parsed region coordinates
+        end = data[0]->iter->end;
+    }
+
+    // the core multi-pileup loop
+    mplp = bam_mplp_init(n, read_bam, (void**)data); // initialization
+    n_plp = calloc(n, sizeof(int)); // n_plp[i] is the number of covering reads from the i-th BAM
+    plp = calloc(n, sizeof(bam_pileup1_t*)); // plp[i] points to the array of covering reads (internal in mplp)
+    while (bam_mplp_auto(mplp, &tid, &pos, n_plp, plp) > 0) { // come to the next covered position
+        if (pos < beg || pos >= end) continue; // out of range; skip
+        if (bed && bed_overlap(bed, h->target_name[tid], pos, pos + 1) == 0) continue; // not in BED; skip
+        fputs(h->target_name[tid], stdout); printf("\t%d", pos+1); // a customized printf() would be faster
+        for (i = 0; i < n; ++i) { // base level filters have to go here
+            int j, m = 0;
+            for (j = 0; j < n_plp[i]; ++j) {
+                const bam_pileup1_t *p = plp[i] + j; // DON'T modfity plp[][] unless you really know
+                if (p->is_del || p->is_refskip) ++m; // having dels or refskips at tid:pos
+                else if (bam_get_qual(p->b)[p->qpos] < baseQ) ++m; // low base quality
+            }
+            printf("\t%d", n_plp[i] - m); // this the depth to output
+        }
+        putchar('\n');
+    }
+    free(n_plp); free(plp);
+    bam_mplp_destroy(mplp);
+
+depth_end:
+    for (i = 0; i < n && data[i]; ++i) {
+        bam_hdr_destroy(data[i]->hdr);
+        sam_close(data[i]->fp);
+        if (data[i]->iter) hts_itr_destroy(data[i]->iter);
+        free(data[i]);
+    }
+    free(data); free(reg);
+    if (bed) bed_destroy(bed);
     if ( file_list )
     {
         for (i=0; i<n; i++) free(fn[i]);
         free(fn);
     }
-	return 0;
+    return status;
 }
+
+#ifdef _MAIN_BAM2DEPTH
+int main(int argc, char *argv[])
+{
+    return main_depth(argc, argv);
+}
+#endif
diff --git a/samtools/bam_aux.c.pysam.c b/samtools/bam_aux.c.pysam.c
index 258a705..89e2dfa 100644
--- a/samtools/bam_aux.c.pysam.c
+++ b/samtools/bam_aux.c.pysam.c
@@ -1,219 +1,73 @@
 #include "pysam.h"
 
-#include <ctype.h>
-#include "bam.h"
-#include "khash.h"
-typedef char *str_p;
-KHASH_MAP_INIT_STR(s, int)
-KHASH_MAP_INIT_STR(r2l, str_p)
-
-void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data)
-{
-	int ori_len = b->data_len;
-	b->data_len += 3 + len;
-	b->l_aux += 3 + len;
-	if (b->m_data < b->data_len) {
-		b->m_data = b->data_len;
-		kroundup32(b->m_data);
-		b->data = (uint8_t*)realloc(b->data, b->m_data);
-	}
-	b->data[ori_len] = tag[0]; b->data[ori_len + 1] = tag[1];
-	b->data[ori_len + 2] = type;
-	memcpy(b->data + ori_len + 3, data, len);
-}
-
-uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2])
-{
-	return bam_aux_get(b, tag);
-}
-
-#define __skip_tag(s) do { \
-		int type = toupper(*(s)); \
-		++(s); \
-		if (type == 'Z' || type == 'H') { while (*(s)) ++(s); ++(s); } \
-		else if (type == 'B') (s) += 5 + bam_aux_type2size(*(s)) * (*(int32_t*)((s)+1)); \
-		else (s) += bam_aux_type2size(type); \
-	} while(0)
-
-uint8_t *bam_aux_get(const bam1_t *b, const char tag[2])
-{
-	uint8_t *s;
-	int y = tag[0]<<8 | tag[1];
-	s = bam1_aux(b);
-	while (s < b->data + b->data_len) {
-		int x = (int)s[0]<<8 | s[1];
-		s += 2;
-		if (x == y) return s;
-		__skip_tag(s);
-	}
-	return 0;
-}
-// s MUST BE returned by bam_aux_get()
-int bam_aux_del(bam1_t *b, uint8_t *s)
-{
-	uint8_t *p, *aux;
-	aux = bam1_aux(b);
-	p = s - 2;
-	__skip_tag(s);
-	memmove(p, s, b->l_aux - (s - aux));
-	b->data_len -= s - p;
-	b->l_aux -= s - p;
-	return 0;
-}
-
-int bam_aux_drop_other(bam1_t *b, uint8_t *s)
-{
-	if (s) {
-		uint8_t *p, *aux;
-		aux = bam1_aux(b);
-		p = s - 2;
-		__skip_tag(s);
-		memmove(aux, p, s - p);
-		b->data_len -= b->l_aux - (s - p);
-		b->l_aux = s - p;
-	} else {
-		b->data_len -= b->l_aux;
-		b->l_aux = 0;
-	}
-	return 0;
-}
-
-void bam_init_header_hash(bam_header_t *header)
-{
-	if (header->hash == 0) {
-		int ret, i;
-		khiter_t iter;
-		khash_t(s) *h;
-		header->hash = h = kh_init(s);
-		for (i = 0; i < header->n_targets; ++i) {
-			iter = kh_put(s, h, header->target_name[i], &ret);
-			kh_value(h, iter) = i;
-		}
-	}
-}
+/*  bam_aux.c -- remaining aux field handling.
 
-void bam_destroy_header_hash(bam_header_t *header)
-{
-	if (header->hash)
-		kh_destroy(s, (khash_t(s)*)header->hash);
-}
+    Copyright (C) 2008-2010, 2013 Genome Research Ltd.
+    Portions copyright (C) 2011 Broad Institute.
 
-int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
-{
-	khint_t k;
-	khash_t(s) *h = (khash_t(s)*)header->hash;
-	k = kh_get(s, h, seq_name);
-	return k == kh_end(h)? -1 : kh_value(h, k);
-}
+    Author: Heng Li <lh3 at sanger.ac.uk>
 
-int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *beg, int *end)
-{
-	char *s;
-	int i, l, k, name_end;
-	khiter_t iter;
-	khash_t(s) *h;
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
 
-	bam_init_header_hash(header);
-	h = (khash_t(s)*)header->hash;
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
 
-	*ref_id = *beg = *end = -1;
-	name_end = l = strlen(str);
-	s = (char*)malloc(l+1);
-	// remove space
-	for (i = k = 0; i < l; ++i)
-		if (!isspace(str[i])) s[k++] = str[i];
-	s[k] = 0; l = k;
-	// determine the sequence name
-	for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
-	if (i >= 0) name_end = i;
-	if (name_end < l) { // check if this is really the end
-		int n_hyphen = 0;
-		for (i = name_end + 1; i < l; ++i) {
-			if (s[i] == '-') ++n_hyphen;
-			else if (!isdigit(s[i]) && s[i] != ',') break;
-		}
-		if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
-		s[name_end] = 0;
-		iter = kh_get(s, h, s);
-		if (iter == kh_end(h)) { // cannot find the sequence name
-			iter = kh_get(s, h, str); // try str as the name
-			if (iter == kh_end(h)) {
-				if (bam_verbose >= 2) fprintf(pysamerr, "[%s] fail to determine the sequence name.\n", __func__);
-				free(s); return -1;
-			} else s[name_end] = ':', name_end = l;
-		}
-	} else iter = kh_get(s, h, str);
-        if (iter == kh_end(h)) {
-          free(s); 
-          return -1;
-        }
-	*ref_id = kh_val(h, iter);
-	// parse the interval
-	if (name_end < l) {
-		for (i = k = name_end + 1; i < l; ++i)
-			if (s[i] != ',') s[k++] = s[i];
-		s[k] = 0;
-		*beg = atoi(s + name_end + 1);
-		for (i = name_end + 1; i != k; ++i) if (s[i] == '-') break;
-		*end = i < k? atoi(s + i + 1) : 1<<29;
-		if (*beg > 0) --*beg;
-	} else *beg = 0, *end = 1<<29;
-	free(s);
-	return *beg <= *end? 0 : -1;
-}
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
 
-int32_t bam_aux2i(const uint8_t *s)
-{
-	int type;
-	if (s == 0) return 0;
-	type = *s++;
-	if (type == 'c') return (int32_t)*(int8_t*)s;
-	else if (type == 'C') return (int32_t)*(uint8_t*)s;
-	else if (type == 's') return (int32_t)*(int16_t*)s;
-	else if (type == 'S') return (int32_t)*(uint16_t*)s;
-	else if (type == 'i' || type == 'I') return *(int32_t*)s;
-	else return 0;
-}
+#include <ctype.h>
+#include "bam.h"
 
-float bam_aux2f(const uint8_t *s)
+static inline int bam_aux_type2size(int x)
 {
-	int type;
-	type = *s++;
-	if (s == 0) return 0.0;
-	if (type == 'f') return *(float*)s;
-	else return 0.0;
+    if (x == 'C' || x == 'c' || x == 'A') return 1;
+    else if (x == 'S' || x == 's') return 2;
+    else if (x == 'I' || x == 'i' || x == 'f' || x == 'F') return 4;
+    else return 0;
 }
 
-double bam_aux2d(const uint8_t *s)
-{
-	int type;
-	type = *s++;
-	if (s == 0) return 0.0;
-	if (type == 'd') return *(double*)s;
-	else return 0.0;
-}
+#define __skip_tag(s) do { \
+        int type = toupper(*(s)); \
+        ++(s); \
+        if (type == 'Z' || type == 'H') { while (*(s)) ++(s); ++(s); } \
+        else if (type == 'B') (s) += 5 + bam_aux_type2size(*(s)) * (*(int32_t*)((s)+1)); \
+        else (s) += bam_aux_type2size(type); \
+    } while(0)
 
-char bam_aux2A(const uint8_t *s)
-{
-	int type;
-	type = *s++;
-	if (s == 0) return 0;
-	if (type == 'A') return *(char*)s;
-	else return 0;
-}
 
-char *bam_aux2Z(const uint8_t *s)
+int bam_aux_drop_other(bam1_t *b, uint8_t *s)
 {
-	int type;
-	type = *s++;
-	if (s == 0) return 0;
-	if (type == 'Z' || type == 'H') return (char*)s;
-	else return 0;
+    if (s) {
+        uint8_t *p, *aux;
+        aux = bam1_aux(b);
+        p = s - 2;
+        __skip_tag(s);
+        memmove(aux, p, s - p);
+        b->data_len -= bam_get_l_aux(b) - (s - p);
+    } else {
+        b->data_len -= bam_get_l_aux(b);
+    }
+    return 0;
 }
 
-#ifdef _WIN32
-double drand48()
+int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *beg, int *end)
 {
-	return (double)rand() / RAND_MAX;
+    const char *name_lim = hts_parse_reg(str, beg, end);
+    char *name = malloc(name_lim - str + 1);
+    memcpy(name, str, name_lim - str);
+    name[name_lim - str] = '\0';
+    *ref_id = bam_name2id(header, name);
+    free(name);
+    if (*ref_id == -1) return -1;
+    return *beg <= *end? 0 : -1;
 }
-#endif
diff --git a/samtools/bam_cat.c.pysam.c b/samtools/bam_cat.c.pysam.c
index 5faefc0..b9a40e3 100644
--- a/samtools/bam_cat.c.pysam.c
+++ b/samtools/bam_cat.c.pysam.c
@@ -1,187 +1,158 @@
 #include "pysam.h"
 
-/*
-
-bam_cat -- efficiently concatenates bam files
-
-bam_cat can be used to concatenate BAM files. Under special
-circumstances, it can be used as an alternative to 'samtools merge' to
-concatenate multiple sorted files into a single sorted file. For this
-to work each file must be sorted, and the sorted files must be given
-as command line arguments in order such that the final read in file i
-is less than or equal to the first read in file i+1.
-
-This code is derived from the bam_reheader function in samtools 0.1.8
-and modified to perform concatenation by Chris Saunders on behalf of
-Illumina.
-
-
-########## License:
-
-The MIT License
-
-Original SAMtools work copyright (c) 2008-2009 Genome Research Ltd.
-Modified SAMtools work copyright (c) 2010 Illumina, Inc.
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-
-*/
-
-
-/*
-makefile:
-"""
-CC=gcc
-CFLAGS+=-g -Wall -O2 -D_FILE_OFFSET_BITS=64 -D_USE_KNETFILE -I$(SAMTOOLS_DIR)
-LDFLAGS+=-L$(SAMTOOLS_DIR)
-LDLIBS+=-lbam -lz
-
-all:bam_cat
-"""
-*/
-
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include "knetfile.h"
-#include "bgzf.h"
-#include "bam.h"
-
-#define BUF_SIZE 0x10000
-
-#define GZIPID1 31
-#define GZIPID2 139
-
-#define BGZF_EMPTY_BLOCK_SIZE 28
-
-
-int bam_cat(int nfn, char * const *fn, const bam_header_t *h, const char* outbam)
-{
-    BGZF *fp;
-    FILE* fp_file;
-    uint8_t *buf;
-    uint8_t ebuf[BGZF_EMPTY_BLOCK_SIZE];
-    const int es=BGZF_EMPTY_BLOCK_SIZE;
-    int i;
-    
-    fp = strcmp(outbam, "-")? bgzf_open(outbam, "w") : bgzf_fdopen(fileno(stdout), "w");
-    if (fp == 0) {
-        fprintf(pysamerr, "[%s] ERROR: fail to open output file '%s'.\n", __func__, outbam);
-        return 1;
-    }
-    if (h) bam_header_write(fp, h);
-    
-    buf = (uint8_t*) malloc(BUF_SIZE);
-    for(i = 0; i < nfn; ++i){
-        BGZF *in;
-        bam_header_t *old;
-        int len,j;
-        
-        in = strcmp(fn[i], "-")? bam_open(fn[i], "r") : bam_dopen(fileno(stdin), "r");
-        if (in == 0) {
-            fprintf(pysamerr, "[%s] ERROR: fail to open file '%s'.\n", __func__, fn[i]);
-            return -1;
-        }
-        if (in->is_write) return -1;
-        
-        old = bam_header_read(in);
-		if (h == 0 && i == 0) bam_header_write(fp, old);
-        
-        if (in->block_offset < in->block_length) {
-            bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);
-            bgzf_flush(fp);
-        }
-        
-        j=0;
-#ifdef _USE_KNETFILE
-        fp_file = fp->fp;
-        while ((len = knet_read(in->fp, buf, BUF_SIZE)) > 0) {
-#else  
-        fp_file = fp->fp;
-        while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0) {
-#endif
-            if(len<es){
-                int diff=es-len;
-                if(j==0) {
-                    fprintf(pysamerr, "[%s] ERROR: truncated file?: '%s'.\n", __func__, fn[i]);
-                    return -1;
-                }
-                fwrite(ebuf, 1, len, fp_file);
-                memcpy(ebuf,ebuf+len,diff);
-                memcpy(ebuf+diff,buf,len);
-            } else {
-                if(j!=0) fwrite(ebuf, 1, es, fp_file);
-                len-= es;
-                memcpy(ebuf,buf+len,es);
-                fwrite(buf, 1, len, fp_file);
-            }
-            j=1;
-        }
-
-        /* check final gzip block */
-        {
-            const uint8_t gzip1=ebuf[0];
-            const uint8_t gzip2=ebuf[1];
-            const uint32_t isize=*((uint32_t*)(ebuf+es-4));
-            if(((gzip1!=GZIPID1) || (gzip2!=GZIPID2)) || (isize!=0)) {
-                fprintf(pysamerr, "[%s] WARNING: Unexpected block structure in file '%s'.", __func__, fn[i]);
-                fprintf(pysamerr, " Possible output corruption.\n");
-                fwrite(ebuf, 1, es, fp_file);
-            }
-        }
-        bam_header_destroy(old);
-        bgzf_close(in);
-    }
-    free(buf);
-    bgzf_close(fp);
-    return 0;
-}
-
-
-
-int main_cat(int argc, char *argv[])
-{
-    bam_header_t *h = 0;
-	char *outfn = 0;
-	int c, ret;
-	while ((c = getopt(argc, argv, "h:o:")) >= 0) {
-		switch (c) {
-			case 'h': {
-        		tamFile fph = sam_open(optarg);
-		        if (fph == 0) {
-    		        fprintf(pysamerr, "[%s] ERROR: fail to read the header from '%s'.\n", __func__, argv[1]);
-        		    return 1;
-	        	}
-	    	    h = sam_header_read(fph);
-    	    	sam_close(fph);
-				break;
-			}
-			case 'o': outfn = strdup(optarg); break;
-		}
-	}
-	if (argc - optind < 2) {
-        fprintf(pysamerr, "Usage: samtools cat [-h header.sam] [-o out.bam] <in1.bam> <in2.bam> [...]\n");
-        return 1;
-    }
-    ret = bam_cat(argc - optind, argv + optind, h, outfn? outfn : "-");
-	free(outfn);
-	return ret;
-}
+/*  bam_cat.c -- efficiently concatenates bam files.
+
+    Copyright (C) 2008-2009, 2011-2013 Genome Research Ltd.
+    Modified SAMtools work copyright (C) 2010 Illumina, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notices and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+/*
+bam_cat can be used to concatenate BAM files. Under special
+circumstances, it can be used as an alternative to 'samtools merge' to
+concatenate multiple sorted files into a single sorted file. For this
+to work each file must be sorted, and the sorted files must be given
+as command line arguments in order such that the final read in file i
+is less than or equal to the first read in file i+1.
+
+This code is derived from the bam_reheader function in samtools 0.1.8
+and modified to perform concatenation by Chris Saunders on behalf of
+Illumina.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "htslib/bgzf.h"
+#include "bam.h"
+
+#define BUF_SIZE 0x10000
+
+#define GZIPID1 31
+#define GZIPID2 139
+
+#define BGZF_EMPTY_BLOCK_SIZE 28
+
+
+int bam_cat(int nfn, char * const *fn, const bam_header_t *h, const char* outbam)
+{
+    BGZF *fp;
+    uint8_t *buf;
+    uint8_t ebuf[BGZF_EMPTY_BLOCK_SIZE];
+    const int es=BGZF_EMPTY_BLOCK_SIZE;
+    int i;
+
+    fp = strcmp(outbam, "-")? bgzf_open(outbam, "w") : bgzf_fdopen(fileno(stdout), "w");
+    if (fp == 0) {
+        fprintf(pysamerr, "[%s] ERROR: fail to open output file '%s'.\n", __func__, outbam);
+        return 1;
+    }
+    if (h) bam_header_write(fp, h);
+
+    buf = (uint8_t*) malloc(BUF_SIZE);
+    for(i = 0; i < nfn; ++i){
+        BGZF *in;
+        bam_header_t *old;
+        int len,j;
+
+        in = strcmp(fn[i], "-")? bgzf_open(fn[i], "r") : bgzf_fdopen(fileno(stdin), "r");
+        if (in == 0) {
+            fprintf(pysamerr, "[%s] ERROR: fail to open file '%s'.\n", __func__, fn[i]);
+            return -1;
+        }
+        if (in->is_write) return -1;
+
+        old = bam_header_read(in);
+        if (h == 0 && i == 0) bam_header_write(fp, old);
+
+        if (in->block_offset < in->block_length) {
+            bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);
+            bgzf_flush(fp);
+        }
+
+        j=0;
+        while ((len = bgzf_raw_read(in, buf, BUF_SIZE)) > 0) {
+            if(len<es){
+                int diff=es-len;
+                if(j==0) {
+                    fprintf(pysamerr, "[%s] ERROR: truncated file?: '%s'.\n", __func__, fn[i]);
+                    return -1;
+                }
+                bgzf_raw_write(fp, ebuf, len);
+                memcpy(ebuf,ebuf+len,diff);
+                memcpy(ebuf+diff,buf,len);
+            } else {
+                if(j!=0) bgzf_raw_write(fp, ebuf, es);
+                len-= es;
+                memcpy(ebuf,buf+len,es);
+                bgzf_raw_write(fp, buf, len);
+            }
+            j=1;
+        }
+
+        /* check final gzip block */
+        {
+            const uint8_t gzip1=ebuf[0];
+            const uint8_t gzip2=ebuf[1];
+            const uint32_t isize=*((uint32_t*)(ebuf+es-4));
+            if(((gzip1!=GZIPID1) || (gzip2!=GZIPID2)) || (isize!=0)) {
+                fprintf(pysamerr, "[%s] WARNING: Unexpected block structure in file '%s'.", __func__, fn[i]);
+                fprintf(pysamerr, " Possible output corruption.\n");
+                bgzf_raw_write(fp, ebuf, es);
+            }
+        }
+        bam_header_destroy(old);
+        bgzf_close(in);
+    }
+    free(buf);
+    bgzf_close(fp);
+    return 0;
+}
+
+
+
+int main_cat(int argc, char *argv[])
+{
+    bam_header_t *h = 0;
+    char *outfn = 0;
+    int c, ret;
+    while ((c = getopt(argc, argv, "h:o:")) >= 0) {
+        switch (c) {
+            case 'h': {
+                tamFile fph = sam_open(optarg);
+                if (fph == 0) {
+                    fprintf(pysamerr, "[%s] ERROR: fail to read the header from '%s'.\n", __func__, argv[1]);
+                    return 1;
+                }
+                h = sam_header_read(fph);
+                sam_close(fph);
+                break;
+            }
+            case 'o': outfn = strdup(optarg); break;
+        }
+    }
+    if (argc - optind < 2) {
+        fprintf(pysamerr, "Usage: samtools cat [-h header.sam] [-o out.bam] <in1.bam> <in2.bam> [...]\n");
+        return 1;
+    }
+    ret = bam_cat(argc - optind, argv + optind, h, outfn? outfn : "-");
+    free(outfn);
+    return ret;
+}
diff --git a/samtools/bam_color.c.pysam.c b/samtools/bam_color.c.pysam.c
index 2e8ac13..78d8510 100644
--- a/samtools/bam_color.c.pysam.c
+++ b/samtools/bam_color.c.pysam.c
@@ -1,5 +1,29 @@
 #include "pysam.h"
 
+/*  bam_color.c -- color-space support.
+
+    Copyright (C) 2009, 2012 University of California - Los Angeles.
+
+    Author: Nils Homer <nilshomer at gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <ctype.h>
 #include "bam.h"
 
@@ -13,23 +37,23 @@
  */
 char bam_aux_getCSi(bam1_t *b, int i)
 {
-	uint8_t *c = bam_aux_get(b, "CS");
-	char *cs = NULL;
-
-	// return the base if the tag was not found
-	if(0 == c) return 0;
-
-	cs = bam_aux2Z(c);
-	// adjust for strandedness and leading adaptor
-	if(bam1_strand(b)) {
-	    i = strlen(cs) - 1 - i;
-	    // adjust for leading hard clip
-	    uint32_t cigar = bam1_cigar(b)[0];
-	    if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
-		i -= cigar >> BAM_CIGAR_SHIFT;
-	    }
-	} else { i++; }
-	return cs[i];
+    uint8_t *c = bam_aux_get(b, "CS");
+    char *cs = NULL;
+
+    // return the base if the tag was not found
+    if(0 == c) return 0;
+
+    cs = bam_aux2Z(c);
+    // adjust for strandedness and leading adaptor
+    if(bam1_strand(b)) {
+        i = strlen(cs) - 1 - i;
+        // adjust for leading hard clip
+        uint32_t cigar = bam1_cigar(b)[0];
+        if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
+        i -= cigar >> BAM_CIGAR_SHIFT;
+        }
+    } else { i++; }
+    return cs[i];
 }
 
 /*!
@@ -42,56 +66,56 @@ char bam_aux_getCSi(bam1_t *b, int i)
  */
 char bam_aux_getCQi(bam1_t *b, int i)
 {
-	uint8_t *c = bam_aux_get(b, "CQ");
-	char *cq = NULL;
-	
-	// return the base if the tag was not found
-	if(0 == c) return 0;
-
-	cq = bam_aux2Z(c);
-	// adjust for strandedness
-	if(bam1_strand(b)) {
-	    i = strlen(cq) - 1 - i;
-	    // adjust for leading hard clip
-	    uint32_t cigar = bam1_cigar(b)[0];
-	    if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
-		i -= (cigar >> BAM_CIGAR_SHIFT);
-	    }
-	}
-	return cq[i];
+    uint8_t *c = bam_aux_get(b, "CQ");
+    char *cq = NULL;
+
+    // return the base if the tag was not found
+    if(0 == c) return 0;
+
+    cq = bam_aux2Z(c);
+    // adjust for strandedness
+    if(bam1_strand(b)) {
+        i = strlen(cq) - 1 - i;
+        // adjust for leading hard clip
+        uint32_t cigar = bam1_cigar(b)[0];
+        if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
+        i -= (cigar >> BAM_CIGAR_SHIFT);
+        }
+    }
+    return cq[i];
 }
 
 char bam_aux_nt2int(char a)
 {
-	switch(toupper(a)) {
-		case 'A':
-			return 0;
-			break;
-		case 'C':
-			return 1;
-			break;
-		case 'G':
-			return 2;
-			break;
-		case 'T':
-			return 3;
-			break;
-		default:
-			return 4;
-			break;
-	}
+    switch(toupper(a)) {
+        case 'A':
+            return 0;
+            break;
+        case 'C':
+            return 1;
+            break;
+        case 'G':
+            return 2;
+            break;
+        case 'T':
+            return 3;
+            break;
+        default:
+            return 4;
+            break;
+    }
 }
 
 char bam_aux_ntnt2cs(char a, char b)
 {
-	a = bam_aux_nt2int(a);
-	b = bam_aux_nt2int(b);
-	if(4 == a || 4 == b) return '4';
-	return "0123"[(int)(a ^ b)];
+    a = bam_aux_nt2int(a);
+    b = bam_aux_nt2int(b);
+    if(4 == a || 4 == b) return '4';
+    return "0123"[(int)(a ^ b)];
 }
 
 /*!
- @abstract     Get the color error profile at the give position    
+ @abstract     Get the color error profile at the give position
  @param b      pointer to an alignment
  @return       the original color if the color was an error, '-' (dash) otherwise
 
@@ -99,49 +123,49 @@ char bam_aux_ntnt2cs(char a, char b)
  */
 char bam_aux_getCEi(bam1_t *b, int i)
 {
-	int cs_i;
-	uint8_t *c = bam_aux_get(b, "CS");
-	char *cs = NULL;
-	char prev_b, cur_b;
-	char cur_color, cor_color;
-
-	// return the base if the tag was not found
-	if(0 == c) return 0;
-	
-	cs = bam_aux2Z(c);
-
-	// adjust for strandedness and leading adaptor
-	if(bam1_strand(b)) { //reverse strand
-		cs_i = strlen(cs) - 1 - i;
-		// adjust for leading hard clip
-		uint32_t cigar = bam1_cigar(b)[0];
-		if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
-			cs_i -= cigar >> BAM_CIGAR_SHIFT;
-		}
-		// get current color
-		cur_color = cs[cs_i];
-		// get previous base.  Note: must rc adaptor
-		prev_b = (cs_i == 1) ? "TGCAN"[(int)bam_aux_nt2int(cs[0])] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i+1)];
-		// get current base
-		cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)]; 
-	}
-	else {
-		cs_i=i+1;
-		// get current color
-		cur_color = cs[cs_i];
-		// get previous base
-		prev_b = (0 == i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i-1)];
-		// get current base
-		cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
-	}
-
-	// corrected color
-	cor_color = bam_aux_ntnt2cs(prev_b, cur_b);
-
-	if(cur_color == cor_color) { 
-		return '-';
-	}
-	else {
-		return cur_color;
-	}
+    int cs_i;
+    uint8_t *c = bam_aux_get(b, "CS");
+    char *cs = NULL;
+    char prev_b, cur_b;
+    char cur_color, cor_color;
+
+    // return the base if the tag was not found
+    if(0 == c) return 0;
+
+    cs = bam_aux2Z(c);
+
+    // adjust for strandedness and leading adaptor
+    if(bam1_strand(b)) { //reverse strand
+        cs_i = strlen(cs) - 1 - i;
+        // adjust for leading hard clip
+        uint32_t cigar = bam1_cigar(b)[0];
+        if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
+            cs_i -= cigar >> BAM_CIGAR_SHIFT;
+        }
+        // get current color
+        cur_color = cs[cs_i];
+        // get previous base.  Note: must rc adaptor
+        prev_b = (cs_i == 1) ? "TGCAN"[(int)bam_aux_nt2int(cs[0])] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i+1)];
+        // get current base
+        cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
+    }
+    else {
+        cs_i=i+1;
+        // get current color
+        cur_color = cs[cs_i];
+        // get previous base
+        prev_b = (0 == i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i-1)];
+        // get current base
+        cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
+    }
+
+    // corrected color
+    cor_color = bam_aux_ntnt2cs(prev_b, cur_b);
+
+    if(cur_color == cor_color) {
+        return '-';
+    }
+    else {
+        return cur_color;
+    }
 }
diff --git a/samtools/bam_endian.h b/samtools/bam_endian.h
index 4919016..d870ca5 100644
--- a/samtools/bam_endian.h
+++ b/samtools/bam_endian.h
@@ -1,42 +1,66 @@
+/*  bam_endian.h -- endianness conversion functions.
+
+    Copyright (C) 2008 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #ifndef BAM_ENDIAN_H
 #define BAM_ENDIAN_H
 
 #include <stdint.h>
 
-static inline int bam_is_big_endian(void)
+static inline int bam_is_big_endian()
 {
-	long one= 1;
-	return !(*((char *)(&one)));
+    long one= 1;
+    return !(*((char *)(&one)));
 }
 static inline uint16_t bam_swap_endian_2(uint16_t v)
 {
-	return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8));
+    return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8));
 }
 static inline void *bam_swap_endian_2p(void *x)
 {
-	*(uint16_t*)x = bam_swap_endian_2(*(uint16_t*)x);
-	return x;
+    *(uint16_t*)x = bam_swap_endian_2(*(uint16_t*)x);
+    return x;
 }
 static inline uint32_t bam_swap_endian_4(uint32_t v)
 {
-	v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
-	return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
+    v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
+    return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
 }
 static inline void *bam_swap_endian_4p(void *x)
 {
-	*(uint32_t*)x = bam_swap_endian_4(*(uint32_t*)x);
-	return x;
+    *(uint32_t*)x = bam_swap_endian_4(*(uint32_t*)x);
+    return x;
 }
 static inline uint64_t bam_swap_endian_8(uint64_t v)
 {
-	v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
-	v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
-	return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
+    v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
+    v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
+    return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
 }
 static inline void *bam_swap_endian_8p(void *x)
 {
-	*(uint64_t*)x = bam_swap_endian_8(*(uint64_t*)x);
-	return x;
+    *(uint64_t*)x = bam_swap_endian_8(*(uint64_t*)x);
+    return x;
 }
 
 #endif
diff --git a/samtools/bam_flags.c.pysam.c b/samtools/bam_flags.c.pysam.c
new file mode 100644
index 0000000..f4df057
--- /dev/null
+++ b/samtools/bam_flags.c.pysam.c
@@ -0,0 +1,70 @@
+#include "pysam.h"
+
+/*  bam_flags.c -- flags subcommand.
+
+    Copyright (C) 2013-2014 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <ctype.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <htslib/sam.h>
+
+static void usage(void)
+{
+    fprintf(pysamerr, "\n");
+    fprintf(pysamerr, "About: Convert between textual and numeric flag representation\n");
+    fprintf(pysamerr, "Usage: samtools flags INT|STR[,...]\n");
+    fprintf(pysamerr, "\n");
+    fprintf(pysamerr, "Flags:\n");
+    fprintf(pysamerr, "\t0x%x\tPAIRED        .. paired-end (or multiple-segment) sequencing technology\n", BAM_FPAIRED);
+    fprintf(pysamerr, "\t0x%x\tPROPER_PAIR   .. each segment properly aligned according to the aligner\n", BAM_FPROPER_PAIR);
+    fprintf(pysamerr, "\t0x%x\tUNMAP         .. segment unmapped\n", BAM_FUNMAP);
+    fprintf(pysamerr, "\t0x%x\tMUNMAP        .. next segment in the template unmapped\n", BAM_FMUNMAP);
+    fprintf(pysamerr, "\t0x%x\tREVERSE       .. SEQ is reverse complemented\n", BAM_FREVERSE);
+    fprintf(pysamerr, "\t0x%x\tMREVERSE      .. SEQ of the next segment in the template is reversed\n", BAM_FMREVERSE);
+    fprintf(pysamerr, "\t0x%x\tREAD1         .. the first segment in the template\n", BAM_FREAD1);
+    fprintf(pysamerr, "\t0x%x\tREAD2         .. the last segment in the template\n", BAM_FREAD2);
+    fprintf(pysamerr, "\t0x%x\tSECONDARY     .. secondary alignment\n", BAM_FSECONDARY);
+    fprintf(pysamerr, "\t0x%x\tQCFAIL        .. not passing quality controls\n", BAM_FQCFAIL);
+    fprintf(pysamerr, "\t0x%x\tDUP           .. PCR or optical duplicate\n", BAM_FDUP);
+    fprintf(pysamerr, "\t0x%x\tSUPPLEMENTARY .. supplementary alignment\n", BAM_FSUPPLEMENTARY);
+    fprintf(pysamerr, "\n");
+}
+
+
+int main_flags(int argc, char *argv[])
+{
+    if ( argc!=2 ) usage();
+    else
+    {
+        int mask = bam_str2flag(argv[1]);
+        if ( mask<0 ) { fprintf(pysamerr,"Error: Could not parse \"%s\"\n", argv[1]); usage(); return 1; }
+        printf("0x%x\t%d\t%s\n", mask, mask, bam_flag2str(mask));
+    }
+    return 0;
+}
+
diff --git a/samtools/bam_import.c.pysam.c b/samtools/bam_import.c.pysam.c
index afdfb2f..c2854f4 100644
--- a/samtools/bam_import.c.pysam.c
+++ b/samtools/bam_import.c.pysam.c
@@ -1,491 +1,65 @@
 #include "pysam.h"
 
-#include <zlib.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <assert.h>
-#ifdef _WIN32
-#include <fcntl.h>
-#endif
-#include "kstring.h"
-#include "bam.h"
-#include "sam_header.h"
-#include "kseq.h"
-#include "khash.h"
+/*  bam_import.c -- SAM format parsing.
 
-KSTREAM_INIT(gzFile, gzread, 16384)
-KHASH_MAP_INIT_STR(ref, uint64_t)
+    Copyright (C) 2008-2013 Genome Research Ltd.
 
-void bam_init_header_hash(bam_header_t *header);
-void bam_destroy_header_hash(bam_header_t *header);
-int32_t bam_get_tid(const bam_header_t *header, const char *seq_name);
+    Author: Heng Li <lh3 at sanger.ac.uk>
 
-unsigned char bam_nt16_table[256] = {
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	 1, 2, 4, 8, 15,15,15,15, 15,15,15,15, 15, 0 /*=*/,15,15,
-	15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
-	15,15, 5, 6,  8,15, 7, 9, 15,10,15,15, 15,15,15,15,
-	15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
-	15,15, 5, 6,  8,15, 7, 9, 15,10,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
-	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15
-};
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
 
-unsigned short bam_char2flag_table[256] = {
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,BAM_FREAD1,BAM_FREAD2,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	BAM_FPROPER_PAIR,0,BAM_FMREVERSE,0, 0,BAM_FMUNMAP,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, BAM_FDUP,0,BAM_FQCFAIL,0, 0,0,0,0, 0,0,0,0,
-	BAM_FPAIRED,0,BAM_FREVERSE,BAM_FSECONDARY, 0,BAM_FUNMAP,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
-};
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
 
-char *bam_nt16_rev_table = "=ACMGRSVTWYHKDBN";
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
 
-struct __tamFile_t {
-	gzFile fp;
-	kstream_t *ks;
-	kstring_t *str;
-	uint64_t n_lines;
-	int is_first;
-};
+#include <zlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include "htslib/kstring.h"
+#include "bam.h"
+#include "htslib/kseq.h"
 
-char **__bam_get_lines(const char *fn, int *_n) // for bam_plcmd.c only
-{
-	char **list = 0, *s;
-	int n = 0, dret, m = 0;
-	gzFile fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
-	kstream_t *ks;
-	kstring_t *str;
-	str = (kstring_t*)calloc(1, sizeof(kstring_t));
-	ks = ks_init(fp);
-	while (ks_getuntil(ks, '\n', str, &dret) > 0) {
-		if (n == m) {
-			m = m? m << 1 : 16;
-			list = (char**)realloc(list, m * sizeof(char*));
-		}
-		if (str->s[str->l-1] == '\r')
-			str->s[--str->l] = '\0';
-		s = list[n++] = (char*)calloc(str->l + 1, 1);
-		strcpy(s, str->s);
-	}
-	ks_destroy(ks);
-	gzclose(fp);
-	free(str->s); free(str);
-	*_n = n;
-	return list;
-}
+KSTREAM_INIT(gzFile, gzread, 16384)
 
-static bam_header_t *hash2header(const kh_ref_t *hash)
-{
-	bam_header_t *header;
-	khiter_t k;
-	header = bam_header_init();
-	header->n_targets = kh_size(hash);
-	header->target_name = (char**)calloc(kh_size(hash), sizeof(char*));
-	header->target_len = (uint32_t*)calloc(kh_size(hash), 4);
-	for (k = kh_begin(hash); k != kh_end(hash); ++k) {
-		if (kh_exist(hash, k)) {
-			int i = (int)kh_value(hash, k);
-			header->target_name[i] = (char*)kh_key(hash, k);
-			header->target_len[i] = kh_value(hash, k)>>32;
-		}
-	}
-	bam_init_header_hash(header);
-	return header;
-}
 bam_header_t *sam_header_read2(const char *fn)
 {
-	bam_header_t *header;
-	int c, dret, ret, error = 0;
-	gzFile fp;
-	kstream_t *ks;
-	kstring_t *str;
-	kh_ref_t *hash;
-	khiter_t k;
-	if (fn == 0) return 0;
-	fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
-	if (fp == 0) return 0;
-	hash = kh_init(ref);
-	ks = ks_init(fp);
-	str = (kstring_t*)calloc(1, sizeof(kstring_t));
-	while (ks_getuntil(ks, 0, str, &dret) > 0) {
-		char *s = strdup(str->s);
-		int len, i;
-		i = kh_size(hash);
-		ks_getuntil(ks, 0, str, &dret);
-		len = atoi(str->s);
-		k = kh_put(ref, hash, s, &ret);
-		if (ret == 0) {
-			fprintf(pysamerr, "[sam_header_read2] duplicated sequence name: %s\n", s);
-			error = 1;
-		}
-		kh_value(hash, k) = (uint64_t)len<<32 | i;
-		if (dret != '\n')
-			while ((c = ks_getc(ks)) != '\n' && c != -1);
-	}
-	ks_destroy(ks);
-	gzclose(fp);
-	free(str->s); free(str);
-	fprintf(pysamerr, "[sam_header_read2] %d sequences loaded.\n", kh_size(hash));
-	if (error) return 0;
-	header = hash2header(hash);
-	kh_destroy(ref, hash);
-	return header;
-}
-static inline uint8_t *alloc_data(bam1_t *b, int size)
-{
-	if (b->m_data < size) {
-		b->m_data = size;
-		kroundup32(b->m_data);
-		b->data = (uint8_t*)realloc(b->data, b->m_data);
-	}
-	return b->data;
-}
-static inline void parse_error(int64_t n_lines, const char * __restrict msg)
-{
-	fprintf(pysamerr, "Parse error at line %lld: %s\n", (long long)n_lines, msg);
-	abort();
-}
-static inline void append_text(bam_header_t *header, kstring_t *str)
-{
-	size_t x = header->l_text, y = header->l_text + str->l + 2; // 2 = 1 byte dret + 1 byte null
-	kroundup32(x); kroundup32(y);
-	if (x < y) 
-    {
-        header->n_text = y;
-        header->text = (char*)realloc(header->text, y);
-        if ( !header->text ) 
-        {
-            fprintf(pysamerr,"realloc failed to alloc %ld bytes\n", y);
-            abort();
-        }
-    }
-    // Sanity check
-    if ( header->l_text+str->l+1 >= header->n_text )
-    {
-        fprintf(pysamerr,"append_text FIXME: %ld>=%ld, x=%ld,y=%ld\n",  header->l_text+str->l+1,(long)header->n_text,x,y);
-        abort();
+    bam_header_t *header;
+    int c, dret, n_targets = 0;
+    gzFile fp;
+    kstream_t *ks;
+    kstring_t *str;
+    kstring_t samstr = { 0, 0, NULL };
+    if (fn == 0) return 0;
+    fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
+    if (fp == 0) return 0;
+    ks = ks_init(fp);
+    str = (kstring_t*)calloc(1, sizeof(kstring_t));
+    while (ks_getuntil(ks, 0, str, &dret) > 0) {
+        ksprintf(&samstr, "@SQ\tSN:%s", str->s);
+        ks_getuntil(ks, 0, str, &dret);
+        ksprintf(&samstr, "\tLN:%d\n", atoi(str->s));
+        n_targets++;
+        if (dret != '\n')
+            while ((c = ks_getc(ks)) != '\n' && c != -1);
     }
-	strncpy(header->text + header->l_text, str->s, str->l+1); // we cannot use strcpy() here.
-	header->l_text += str->l + 1;
-	header->text[header->l_text] = 0;
-}
-
-int sam_header_parse(bam_header_t *h)
-{
-	char **tmp;
-	int i;
-	free(h->target_len); free(h->target_name);
-	h->n_targets = 0; h->target_len = 0; h->target_name = 0;
-	if (h->l_text < 3) return 0;
-	if (h->dict == 0) h->dict = sam_header_parse2(h->text);
-	tmp = sam_header2list(h->dict, "SQ", "SN", &h->n_targets);
-	if (h->n_targets == 0) return 0;
-	h->target_name = calloc(h->n_targets, sizeof(void*));
-	for (i = 0; i < h->n_targets; ++i)
-		h->target_name[i] = strdup(tmp[i]);
-	free(tmp);
-	tmp = sam_header2list(h->dict, "SQ", "LN", &h->n_targets);
-	h->target_len = calloc(h->n_targets, 4);
-	for (i = 0; i < h->n_targets; ++i)
-		h->target_len[i] = atoi(tmp[i]);
-	free(tmp);
-	return h->n_targets;
-}
-
-bam_header_t *sam_header_read(tamFile fp)
-{
-	int ret, dret;
-	bam_header_t *header = bam_header_init();
-	kstring_t *str = fp->str;
-	while ((ret = ks_getuntil(fp->ks, KS_SEP_TAB, str, &dret)) >= 0 && str->s[0] == '@') { // skip header
-		str->s[str->l] = dret; // note that str->s is NOT null terminated!!
-		append_text(header, str);
-		if (dret != '\n') {
-			ret = ks_getuntil(fp->ks, '\n', str, &dret);
-			str->s[str->l] = '\n'; // NOT null terminated!!
-			append_text(header, str);
-		}
-		++fp->n_lines;
-	}
-	sam_header_parse(header);
-	bam_init_header_hash(header);
-	fp->is_first = 1;
-	return header;
-}
-
-int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b)
-{
-	int ret, doff, doff0, dret, z = 0;
-	bam1_core_t *c = &b->core;
-	kstring_t *str = fp->str;
-	kstream_t *ks = fp->ks;
-
-	if (fp->is_first) {
-		fp->is_first = 0;
-		ret = str->l;
-	} else {
-		do { // special consideration for empty lines
-			ret = ks_getuntil(fp->ks, KS_SEP_TAB, str, &dret);
-			if (ret >= 0) z += str->l + 1;
-		} while (ret == 0);
-	}
-	if (ret < 0) return -1;
-	++fp->n_lines;
-	doff = 0;
-
-	{ // name
-		c->l_qname = strlen(str->s) + 1;
-		memcpy(alloc_data(b, doff + c->l_qname) + doff, str->s, c->l_qname);
-		doff += c->l_qname;
-	}
-	{ // flag
-		long flag;
-		char *s;
-		ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
-		flag = strtol((char*)str->s, &s, 0);
-		if (*s) { // not the end of the string
-			flag = 0;
-			for (s = str->s; *s; ++s)
-				flag |= bam_char2flag_table[(int)*s];
-		}
-		c->flag = flag;
-	}
-	{ // tid, pos, qual
-		ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->tid = bam_get_tid(header, str->s);
-		if (c->tid < 0 && strcmp(str->s, "*")) {
-			if (header->n_targets == 0) {
-				fprintf(pysamerr, "[sam_read1] missing header? Abort!\n");
-				exit(1);
-			} else fprintf(pysamerr, "[sam_read1] reference '%s' is recognized as '*'.\n", str->s);
-		}
-		ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->pos = isdigit(str->s[0])? atoi(str->s) - 1 : -1;
-		ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->qual = isdigit(str->s[0])? atoi(str->s) : 0;
-		if (ret < 0) return -2;
-	}
-	{ // cigar
-		char *s, *t;
-		int i, op;
-		long x;
-		c->n_cigar = 0;
-		if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -3;
-		z += str->l + 1;
-		if (str->s[0] != '*') {
-			uint32_t *cigar;
-			for (s = str->s; *s; ++s) {
-				if ((isalpha(*s)) || (*s=='=')) ++c->n_cigar;
-				else if (!isdigit(*s)) parse_error(fp->n_lines, "invalid CIGAR character");
-			}
-			b->data = alloc_data(b, doff + c->n_cigar * 4);
-			cigar = bam1_cigar(b);
-			for (i = 0, s = str->s; i != c->n_cigar; ++i) {
-				x = strtol(s, &t, 10);
-				op = toupper(*t);
-				if (op == 'M') op = BAM_CMATCH;
-				else if (op == 'I') op = BAM_CINS;
-				else if (op == 'D') op = BAM_CDEL;
-				else if (op == 'N') op = BAM_CREF_SKIP;
-				else if (op == 'S') op = BAM_CSOFT_CLIP;
-				else if (op == 'H') op = BAM_CHARD_CLIP;
-				else if (op == 'P') op = BAM_CPAD;
-				else if (op == '=') op = BAM_CEQUAL;
-				else if (op == 'X') op = BAM_CDIFF;
-				else if (op == 'B') op = BAM_CBACK;
-				else parse_error(fp->n_lines, "invalid CIGAR operation");
-				s = t + 1;
-				cigar[i] = bam_cigar_gen(x, op);
-			}
-			if (*s) parse_error(fp->n_lines, "unmatched CIGAR operation");
-			c->bin = bam_reg2bin(c->pos, bam_calend(c, cigar));
-			doff += c->n_cigar * 4;
-		} else {
-			if (!(c->flag&BAM_FUNMAP)) {
-				fprintf(pysamerr, "Parse warning at line %lld: mapped sequence without CIGAR\n", (long long)fp->n_lines);
-				c->flag |= BAM_FUNMAP;
-			}
-			c->bin = bam_reg2bin(c->pos, c->pos + 1);
-		}
-	}
-	{ // mtid, mpos, isize
-		ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
-		c->mtid = strcmp(str->s, "=")? bam_get_tid(header, str->s) : c->tid;
-		ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
-		c->mpos = isdigit(str->s[0])? atoi(str->s) - 1 : -1;
-		ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
-		c->isize = (str->s[0] == '-' || isdigit(str->s[0]))? atoi(str->s) : 0;
-		if (ret < 0) return -4;
-	}
-	{ // seq and qual
-		int i;
-		uint8_t *p = 0;
-		if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -5; // seq
-		z += str->l + 1;
-		if (strcmp(str->s, "*")) {
-			c->l_qseq = strlen(str->s);
-			if (c->n_cigar && c->l_qseq != (int32_t)bam_cigar2qlen(c, bam1_cigar(b))) {
-				fprintf(pysamerr, "Line %ld, sequence length %i vs %i from CIGAR\n",
-						(long)fp->n_lines, c->l_qseq, (int32_t)bam_cigar2qlen(c, bam1_cigar(b)));
-				parse_error(fp->n_lines, "CIGAR and sequence length are inconsistent");
-			}
-			p = (uint8_t*)alloc_data(b, doff + c->l_qseq + (c->l_qseq+1)/2) + doff;
-			memset(p, 0, (c->l_qseq+1)/2);
-			for (i = 0; i < c->l_qseq; ++i)
-				p[i/2] |= bam_nt16_table[(int)str->s[i]] << 4*(1-i%2);
-		} else c->l_qseq = 0;
-		if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -6; // qual
-		z += str->l + 1;
-		if (strcmp(str->s, "*") && c->l_qseq != strlen(str->s))
-			parse_error(fp->n_lines, "sequence and quality are inconsistent");
-		p += (c->l_qseq+1)/2;
-		if (strcmp(str->s, "*") == 0) for (i = 0; i < c->l_qseq; ++i) p[i] = 0xff;
-		else for (i = 0; i < c->l_qseq; ++i) p[i] = str->s[i] - 33;
-		doff += c->l_qseq + (c->l_qseq+1)/2;
-	}
-	doff0 = doff;
-	if (dret != '\n' && dret != '\r') { // aux
-		while (ks_getuntil(ks, KS_SEP_TAB, str, &dret) >= 0) {
-			uint8_t *s, type, key[2];
-			z += str->l + 1;
-			if (str->l < 6 || str->s[2] != ':' || str->s[4] != ':')
-				parse_error(fp->n_lines, "missing colon in auxiliary data");
-			key[0] = str->s[0]; key[1] = str->s[1];
-			type = str->s[3];
-			s = alloc_data(b, doff + 3) + doff;
-			s[0] = key[0]; s[1] = key[1]; s += 2; doff += 2;
-			if (type == 'A' || type == 'a' || type == 'c' || type == 'C') { // c and C for backward compatibility
-				s = alloc_data(b, doff + 2) + doff;
-				*s++ = 'A'; *s = str->s[5];
-				doff += 2;
-			} else if (type == 'I' || type == 'i') {
-				long long x;
-				s = alloc_data(b, doff + 5) + doff;
-				x = (long long)atoll(str->s + 5);
-				if (x < 0) {
-					if (x >= -127) {
-						*s++ = 'c'; *(int8_t*)s = (int8_t)x;
-						s += 1; doff += 2;
-					} else if (x >= -32767) {
-						*s++ = 's'; *(int16_t*)s = (int16_t)x;
-						s += 2; doff += 3;
-					} else {
-						*s++ = 'i'; *(int32_t*)s = (int32_t)x;
-						s += 4; doff += 5;
-						if (x < -2147483648ll)
-							fprintf(pysamerr, "Parse warning at line %lld: integer %lld is out of range.",
-									(long long)fp->n_lines, x);
-					}
-				} else {
-					if (x <= 255) {
-						*s++ = 'C'; *s++ = (uint8_t)x;
-						doff += 2;
-					} else if (x <= 65535) {
-						*s++ = 'S'; *(uint16_t*)s = (uint16_t)x;
-						s += 2; doff += 3;
-					} else {
-						*s++ = 'I'; *(uint32_t*)s = (uint32_t)x;
-						s += 4; doff += 5;
-						if (x > 4294967295ll)
-							fprintf(pysamerr, "Parse warning at line %lld: integer %lld is out of range.",
-									(long long)fp->n_lines, x);
-					}
-				}
-			} else if (type == 'f') {
-				s = alloc_data(b, doff + 5) + doff;
-				*s++ = 'f';
-				*(float*)s = (float)atof(str->s + 5);
-				s += 4; doff += 5;
-			} else if (type == 'd') {
-				s = alloc_data(b, doff + 9) + doff;
-				*s++ = 'd';
-				*(float*)s = (float)atof(str->s + 9);
-				s += 8; doff += 9;
-			} else if (type == 'Z' || type == 'H') {
-				int size = 1 + (str->l - 5) + 1;
-				if (type == 'H') { // check whether the hex string is valid
-					int i;
-					if ((str->l - 5) % 2 == 1) parse_error(fp->n_lines, "length of the hex string not even");
-					for (i = 0; i < str->l - 5; ++i) {
-						int c = toupper(str->s[5 + i]);
-						if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')))
-							parse_error(fp->n_lines, "invalid hex character");
-					}
-				}
-				s = alloc_data(b, doff + size) + doff;
-				*s++ = type;
-				memcpy(s, str->s + 5, str->l - 5);
-				s[str->l - 5] = 0;
-				doff += size;
-			} else if (type == 'B') {
-				int32_t n = 0, Bsize, k = 0, size;
-				char *p;
-				if (str->l < 8) parse_error(fp->n_lines, "too few values in aux type B");
-				Bsize = bam_aux_type2size(str->s[5]); // the size of each element
-				for (p = (char*)str->s + 6; *p; ++p) // count the number of elements in the array
-					if (*p == ',') ++n;
-				p = str->s + 7; // now p points to the first number in the array
-				size = 6 + Bsize * n; // total number of bytes allocated to this tag
-				s = alloc_data(b, doff + 6 * Bsize * n) + doff; // allocate memory
-				*s++ = 'B'; *s++ = str->s[5];
-				memcpy(s, &n, 4); s += 4; // write the number of elements
-				if (str->s[5] == 'c')      while (p < str->s + str->l) ((int8_t*)s)[k++]   = (int8_t)strtol(p, &p, 0),   ++p;
-				else if (str->s[5] == 'C') while (p < str->s + str->l) ((uint8_t*)s)[k++]  = (uint8_t)strtol(p, &p, 0),  ++p;
-				else if (str->s[5] == 's') while (p < str->s + str->l) ((int16_t*)s)[k++]  = (int16_t)strtol(p, &p, 0),  ++p; // FIXME: avoid unaligned memory
-				else if (str->s[5] == 'S') while (p < str->s + str->l) ((uint16_t*)s)[k++] = (uint16_t)strtol(p, &p, 0), ++p;
-				else if (str->s[5] == 'i') while (p < str->s + str->l) ((int32_t*)s)[k++]  = (int32_t)strtol(p, &p, 0),  ++p;
-				else if (str->s[5] == 'I') while (p < str->s + str->l) ((uint32_t*)s)[k++] = (uint32_t)strtol(p, &p, 0), ++p;
-				else if (str->s[5] == 'f') while (p < str->s + str->l) ((float*)s)[k++]    = (float)strtod(p, &p),       ++p;
-				else parse_error(fp->n_lines, "unrecognized array type");
-				s += Bsize * n; doff += size;
-			} else parse_error(fp->n_lines, "unrecognized type");
-			if (dret == '\n' || dret == '\r') break;
-		}
-	}
-	b->l_aux = doff - doff0;
-	b->data_len = doff;
-	if (bam_no_B) bam_remove_B(b);
-	return z;
-}
-
-tamFile sam_open(const char *fn)
-{
-	tamFile fp;
-	gzFile gzfp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "rb") : gzopen(fn, "rb");
-	if (gzfp == 0) return 0;
-	fp = (tamFile)calloc(1, sizeof(struct __tamFile_t));
-	fp->str = (kstring_t*)calloc(1, sizeof(kstring_t));
-	fp->fp = gzfp;
-	fp->ks = ks_init(fp->fp);
-	return fp;
-}
-
-void sam_close(tamFile fp)
-{
-	if (fp) {
-		ks_destroy(fp->ks);
-		gzclose(fp->fp);
-		free(fp->str->s); free(fp->str);
-		free(fp);
-	}
+    ks_destroy(ks);
+    gzclose(fp);
+    free(str->s); free(str);
+    header = sam_hdr_parse(samstr.l, samstr.s? samstr.s : "");
+    free(samstr.s);
+    fprintf(pysamerr, "[sam_header_read2] %d sequences loaded.\n", n_targets);
+    return header;
 }
diff --git a/samtools/bam_index.c.pysam.c b/samtools/bam_index.c.pysam.c
index 284f191..7fd7a76 100644
--- a/samtools/bam_index.c.pysam.c
+++ b/samtools/bam_index.c.pysam.c
@@ -1,728 +1,112 @@
 #include "pysam.h"
 
-#include <ctype.h>
-#include <assert.h>
-#include "bam.h"
-#include "khash.h"
-#include "ksort.h"
-#include "bam_endian.h"
-#ifdef _USE_KNETFILE
-#include "knetfile.h"
-#endif
+/*  bam_index.c -- index and idxstats subcommands.
+
+    Copyright (C) 2008-2011, 2013, 2014 Genome Research Ltd.
+    Portions copyright (C) 2010 Broad Institute.
+    Portions copyright (C) 2013 Peter Cock, The James Hutton Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notices and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <htslib/hts.h>
+#include <htslib/sam.h>
+#include <htslib/khash.h>
+#include <stdlib.h>
+#include <stdio.h>
+#define __STDC_FORMAT_MACROS
+#include <inttypes.h>
+#include <unistd.h>
 
-/*!
-  @header
-
-  Alignment indexing. Before indexing, BAM must be sorted based on the
-  leftmost coordinate of alignments. In indexing, BAM uses two indices:
-  a UCSC binning index and a simple linear index. The binning index is
-  efficient for alignments spanning long distance, while the auxiliary
-  linear index helps to reduce unnecessary seek calls especially for
-  short alignments.
-
-  The UCSC binning scheme was suggested by Richard Durbin and Lincoln
-  Stein and is explained by Kent et al. (2002). In this scheme, each bin
-  represents a contiguous genomic region which can be fully contained in
-  another bin; each alignment is associated with a bin which represents
-  the smallest region containing the entire alignment. The binning
-  scheme is essentially another representation of R-tree. A distinct bin
-  uniquely corresponds to a distinct internal node in a R-tree. Bin A is
-  a child of Bin B if region A is contained in B.
-
-  In BAM, each bin may span 2^29, 2^26, 2^23, 2^20, 2^17 or 2^14 bp. Bin
-  0 spans a 512Mbp region, bins 1-8 span 64Mbp, 9-72 8Mbp, 73-584 1Mbp,
-  585-4680 128Kbp and bins 4681-37449 span 16Kbp regions. If we want to
-  find the alignments overlapped with a region [rbeg,rend), we need to
-  calculate the list of bins that may be overlapped the region and test
-  the alignments in the bins to confirm the overlaps. If the specified
-  region is short, typically only a few alignments in six bins need to
-  be retrieved. The overlapping alignments can be quickly fetched.
-
- */
-
-#define BAM_MIN_CHUNK_GAP 32768
-// 1<<14 is the size of minimum bin.
 #define BAM_LIDX_SHIFT    14
 
-#define BAM_MAX_BIN 37450 // =(8^6-1)/7+1
-
-typedef struct {
-	uint64_t u, v;
-} pair64_t;
-
-#define pair64_lt(a,b) ((a).u < (b).u)
-KSORT_INIT(off, pair64_t, pair64_lt)
-
-typedef struct {
-	uint32_t m, n;
-	pair64_t *list;
-} bam_binlist_t;
-
-typedef struct {
-	int32_t n, m;
-	uint64_t *offset;
-} bam_lidx_t;
-
-KHASH_MAP_INIT_INT(i, bam_binlist_t)
-
-struct __bam_index_t {
-	int32_t n;
-	uint64_t n_no_coor; // unmapped reads without coordinate
-	khash_t(i) **index;
-	bam_lidx_t *index2;
-};
-
-// requirement: len <= LEN_MASK
-static inline void insert_offset(khash_t(i) *h, int bin, uint64_t beg, uint64_t end)
-{
-	khint_t k;
-	bam_binlist_t *l;
-	int ret;
-	k = kh_put(i, h, bin, &ret);
-	l = &kh_value(h, k);
-	if (ret) { // not present
-		l->m = 1; l->n = 0;
-		l->list = (pair64_t*)calloc(l->m, 16);
-	}
-	if (l->n == l->m) {
-		l->m <<= 1;
-		l->list = (pair64_t*)realloc(l->list, l->m * 16);
-	}
-	l->list[l->n].u = beg; l->list[l->n++].v = end;
-}
-
-static inline void insert_offset2(bam_lidx_t *index2, bam1_t *b, uint64_t offset)
-{
-	int i, beg, end;
-	beg = b->core.pos >> BAM_LIDX_SHIFT;
-	end = (bam_calend(&b->core, bam1_cigar(b)) - 1) >> BAM_LIDX_SHIFT;
-	if (index2->m < end + 1) {
-		int old_m = index2->m;
-		index2->m = end + 1;
-		kroundup32(index2->m);
-		index2->offset = (uint64_t*)realloc(index2->offset, index2->m * 8);
-		memset(index2->offset + old_m, 0, 8 * (index2->m - old_m));
-	}
-	if (beg == end) {
-		if (index2->offset[beg] == 0) index2->offset[beg] = offset;
-	} else {
-		for (i = beg; i <= end; ++i)
-			if (index2->offset[i] == 0) index2->offset[i] = offset;
-	}
-	index2->n = end + 1;
-}
-
-static void merge_chunks(bam_index_t *idx)
-{
-#if defined(BAM_TRUE_OFFSET) || defined(BAM_VIRTUAL_OFFSET16)
-	khash_t(i) *index;
-	int i, l, m;
-	khint_t k;
-	for (i = 0; i < idx->n; ++i) {
-		index = idx->index[i];
-		for (k = kh_begin(index); k != kh_end(index); ++k) {
-			bam_binlist_t *p;
-			if (!kh_exist(index, k) || kh_key(index, k) == BAM_MAX_BIN) continue;
-			p = &kh_value(index, k);
-			m = 0;
-			for (l = 1; l < p->n; ++l) {
-#ifdef BAM_TRUE_OFFSET
-				if (p->list[m].v + BAM_MIN_CHUNK_GAP > p->list[l].u) p->list[m].v = p->list[l].v;
-#else
-				if (p->list[m].v>>16 == p->list[l].u>>16) p->list[m].v = p->list[l].v;
-#endif
-				else p->list[++m] = p->list[l];
-			} // ~for(l)
-			p->n = m + 1;
-		} // ~for(k)
-	} // ~for(i)
-#endif // defined(BAM_TRUE_OFFSET) || defined(BAM_BGZF)
-}
-
-static void fill_missing(bam_index_t *idx)
-{
-	int i, j;
-	for (i = 0; i < idx->n; ++i) {
-		bam_lidx_t *idx2 = &idx->index2[i];
-		for (j = 1; j < idx2->n; ++j)
-			if (idx2->offset[j] == 0)
-				idx2->offset[j] = idx2->offset[j-1];
-	}
-}
-
-bam_index_t *bam_index_core(bamFile fp)
-{
-	bam1_t *b;
-	bam_header_t *h;
-	int i, ret;
-	bam_index_t *idx;
-	uint32_t last_bin, save_bin;
-	int32_t last_coor, last_tid, save_tid;
-	bam1_core_t *c;
-	uint64_t save_off, last_off, n_mapped, n_unmapped, off_beg, off_end, n_no_coor;
-
-	h = bam_header_read(fp);
-	if(h == 0) {
-	    fprintf(pysamerr, "[bam_index_core] Invalid BAM header.");
-	    return NULL;
-	}
-
-	idx = (bam_index_t*)calloc(1, sizeof(bam_index_t));
-	b = (bam1_t*)calloc(1, sizeof(bam1_t));
-	c = &b->core;
-
-	idx->n = h->n_targets;
-	bam_header_destroy(h);
-	idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*));
-	for (i = 0; i < idx->n; ++i) idx->index[i] = kh_init(i);
-	idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t));
-
-	save_bin = save_tid = last_tid = last_bin = 0xffffffffu;
-	save_off = last_off = bam_tell(fp); last_coor = 0xffffffffu;
-	n_mapped = n_unmapped = n_no_coor = off_end = 0;
-	off_beg = off_end = bam_tell(fp);
-	while ((ret = bam_read1(fp, b)) >= 0) {
-		if (c->tid < 0) ++n_no_coor;
-		if (last_tid < c->tid || (last_tid >= 0 && c->tid < 0)) { // change of chromosomes
-			last_tid = c->tid;
-			last_bin = 0xffffffffu;
-		} else if ((uint32_t)last_tid > (uint32_t)c->tid) {
-			fprintf(pysamerr, "[bam_index_core] the alignment is not sorted (%s): %d-th chr > %d-th chr\n",
-					bam1_qname(b), last_tid+1, c->tid+1);
-			return NULL;
-		} else if ((int32_t)c->tid >= 0 && last_coor > c->pos) {
-			fprintf(pysamerr, "[bam_index_core] the alignment is not sorted (%s): %u > %u in %d-th chr\n",
-					bam1_qname(b), last_coor, c->pos, c->tid+1);
-			return NULL;
-		}
-		if (c->tid >= 0 && !(c->flag & BAM_FUNMAP)) insert_offset2(&idx->index2[b->core.tid], b, last_off);
-		if (c->bin != last_bin) { // then possibly write the binning index
-			if (save_bin != 0xffffffffu) // save_bin==0xffffffffu only happens to the first record
-				insert_offset(idx->index[save_tid], save_bin, save_off, last_off);
-			if (last_bin == 0xffffffffu && save_tid != 0xffffffffu) { // write the meta element
-				off_end = last_off;
-				insert_offset(idx->index[save_tid], BAM_MAX_BIN, off_beg, off_end);
-				insert_offset(idx->index[save_tid], BAM_MAX_BIN, n_mapped, n_unmapped);
-				n_mapped = n_unmapped = 0;
-				off_beg = off_end;
-			}
-			save_off = last_off;
-			save_bin = last_bin = c->bin;
-			save_tid = c->tid;
-			if (save_tid < 0) break;
-		}
-		if (bam_tell(fp) <= last_off) {
-			fprintf(pysamerr, "[bam_index_core] bug in BGZF/RAZF: %llx < %llx\n",
-					(unsigned long long)bam_tell(fp), (unsigned long long)last_off);
-			return NULL;
-		}
-		if (c->flag & BAM_FUNMAP) ++n_unmapped;
-		else ++n_mapped;
-		last_off = bam_tell(fp);
-		last_coor = b->core.pos;
-	}
-	if (save_tid >= 0) {
-		insert_offset(idx->index[save_tid], save_bin, save_off, bam_tell(fp));
-		insert_offset(idx->index[save_tid], BAM_MAX_BIN, off_beg, bam_tell(fp));
-		insert_offset(idx->index[save_tid], BAM_MAX_BIN, n_mapped, n_unmapped);
-	}
-	merge_chunks(idx);
-	fill_missing(idx);
-	if (ret >= 0) {
-		while ((ret = bam_read1(fp, b)) >= 0) {
-			++n_no_coor;
-			if (c->tid >= 0 && n_no_coor) {
-				fprintf(pysamerr, "[bam_index_core] the alignment is not sorted: reads without coordinates prior to reads with coordinates.\n");
-				return NULL;
-			}
-		}
-	}
-	if (ret < -1) fprintf(pysamerr, "[bam_index_core] truncated file? Continue anyway. (%d)\n", ret);
-	free(b->data); free(b);
-	idx->n_no_coor = n_no_coor;
-	return idx;
-}
-
-void bam_index_destroy(bam_index_t *idx)
-{
-	khint_t k;
-	int i;
-	if (idx == 0) return;
-	for (i = 0; i < idx->n; ++i) {
-		khash_t(i) *index = idx->index[i];
-		bam_lidx_t *index2 = idx->index2 + i;
-		for (k = kh_begin(index); k != kh_end(index); ++k) {
-			if (kh_exist(index, k))
-				free(kh_value(index, k).list);
-		}
-		kh_destroy(i, index);
-		free(index2->offset);
-	}
-	free(idx->index); free(idx->index2);
-	free(idx);
-}
-
-void bam_index_save(const bam_index_t *idx, FILE *fp)
-{
-	int32_t i, size;
-	khint_t k;
-	fwrite("BAI\1", 1, 4, fp);
-	if (bam_is_be) {
-		uint32_t x = idx->n;
-		fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
-	} else fwrite(&idx->n, 4, 1, fp);
-	for (i = 0; i < idx->n; ++i) {
-		khash_t(i) *index = idx->index[i];
-		bam_lidx_t *index2 = idx->index2 + i;
-		// write binning index
-		size = kh_size(index);
-		if (bam_is_be) { // big endian
-			uint32_t x = size;
-			fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
-		} else fwrite(&size, 4, 1, fp);
-		for (k = kh_begin(index); k != kh_end(index); ++k) {
-			if (kh_exist(index, k)) {
-				bam_binlist_t *p = &kh_value(index, k);
-				if (bam_is_be) { // big endian
-					uint32_t x;
-					x = kh_key(index, k); fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
-					x = p->n; fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
-					for (x = 0; (int)x < p->n; ++x) {
-						bam_swap_endian_8p(&p->list[x].u);
-						bam_swap_endian_8p(&p->list[x].v);
-					}
-					fwrite(p->list, 16, p->n, fp);
-					for (x = 0; (int)x < p->n; ++x) {
-						bam_swap_endian_8p(&p->list[x].u);
-						bam_swap_endian_8p(&p->list[x].v);
-					}
-				} else {
-					fwrite(&kh_key(index, k), 4, 1, fp);
-					fwrite(&p->n, 4, 1, fp);
-					fwrite(p->list, 16, p->n, fp);
-				}
-			}
-		}
-		// write linear index (index2)
-		if (bam_is_be) {
-			int x = index2->n;
-			fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
-		} else fwrite(&index2->n, 4, 1, fp);
-		if (bam_is_be) { // big endian
-			int x;
-			for (x = 0; (int)x < index2->n; ++x)
-				bam_swap_endian_8p(&index2->offset[x]);
-			fwrite(index2->offset, 8, index2->n, fp);
-			for (x = 0; (int)x < index2->n; ++x)
-				bam_swap_endian_8p(&index2->offset[x]);
-		} else fwrite(index2->offset, 8, index2->n, fp);
-	}
-	{ // write the number of reads coor-less records.
-		uint64_t x = idx->n_no_coor;
-		if (bam_is_be) bam_swap_endian_8p(&x);
-		fwrite(&x, 8, 1, fp);
-	}
-	fflush(fp);
-}
-
-static bam_index_t *bam_index_load_core(FILE *fp)
-{
-	int i;
-	char magic[4];
-	bam_index_t *idx;
-	if (fp == 0) {
-		fprintf(pysamerr, "[bam_index_load_core] fail to load index.\n");
-		return 0;
-	}
-	fread(magic, 1, 4, fp);
-	if (strncmp(magic, "BAI\1", 4)) {
-		fprintf(pysamerr, "[bam_index_load] wrong magic number.\n");
-		fclose(fp);
-		return 0;
-	}
-	idx = (bam_index_t*)calloc(1, sizeof(bam_index_t));	
-	fread(&idx->n, 4, 1, fp);
-	if (bam_is_be) bam_swap_endian_4p(&idx->n);
-	idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*));
-	idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t));
-	for (i = 0; i < idx->n; ++i) {
-		khash_t(i) *index;
-		bam_lidx_t *index2 = idx->index2 + i;
-		uint32_t key, size;
-		khint_t k;
-		int j, ret;
-		bam_binlist_t *p;
-		index = idx->index[i] = kh_init(i);
-		// load binning index
-		fread(&size, 4, 1, fp);
-		if (bam_is_be) bam_swap_endian_4p(&size);
-		for (j = 0; j < (int)size; ++j) {
-			fread(&key, 4, 1, fp);
-			if (bam_is_be) bam_swap_endian_4p(&key);
-			k = kh_put(i, index, key, &ret);
-			p = &kh_value(index, k);
-			fread(&p->n, 4, 1, fp);
-			if (bam_is_be) bam_swap_endian_4p(&p->n);
-			p->m = p->n;
-			p->list = (pair64_t*)malloc(p->m * 16);
-			fread(p->list, 16, p->n, fp);
-			if (bam_is_be) {
-				int x;
-				for (x = 0; x < p->n; ++x) {
-					bam_swap_endian_8p(&p->list[x].u);
-					bam_swap_endian_8p(&p->list[x].v);
-				}
-			}
-		}
-		// load linear index
-		fread(&index2->n, 4, 1, fp);
-		if (bam_is_be) bam_swap_endian_4p(&index2->n);
-		index2->m = index2->n;
-		index2->offset = (uint64_t*)calloc(index2->m, 8);
-		fread(index2->offset, index2->n, 8, fp);
-		if (bam_is_be)
-			for (j = 0; j < index2->n; ++j) bam_swap_endian_8p(&index2->offset[j]);
-	}
-	if (fread(&idx->n_no_coor, 8, 1, fp) == 0) idx->n_no_coor = 0;
-	if (bam_is_be) bam_swap_endian_8p(&idx->n_no_coor);
-	return idx;
-}
-
-bam_index_t *bam_index_load_local(const char *_fn)
-{
-	FILE *fp;
-	char *fnidx, *fn;
-
-	if (strstr(_fn, "ftp://") == _fn || strstr(_fn, "http://") == _fn) {
-		const char *p;
-		int l = strlen(_fn);
-		for (p = _fn + l - 1; p >= _fn; --p)
-			if (*p == '/') break;
-		fn = strdup(p + 1);
-	} else fn = strdup(_fn);
-	fnidx = (char*)calloc(strlen(fn) + 5, 1);
-	strcpy(fnidx, fn); strcat(fnidx, ".bai");
-	fp = fopen(fnidx, "rb");
-	if (fp == 0) { // try "{base}.bai"
-		char *s = strstr(fn, "bam");
-		if (s == fn + strlen(fn) - 3) {
-			strcpy(fnidx, fn);
-			fnidx[strlen(fn)-1] = 'i';
-			fp = fopen(fnidx, "rb");
-		}
-	}
-	free(fnidx); free(fn);
-	if (fp) {
-		bam_index_t *idx = bam_index_load_core(fp);
-		fclose(fp);
-		return idx;
-	} else return 0;
-}
-
-#ifdef _USE_KNETFILE
-static void download_from_remote(const char *url)
-{
-	const int buf_size = 1 * 1024 * 1024;
-	char *fn;
-	FILE *fp;
-	uint8_t *buf;
-	knetFile *fp_remote;
-	int l;
-	if (strstr(url, "ftp://") != url && strstr(url, "http://") != url) return;
-	l = strlen(url);
-	for (fn = (char*)url + l - 1; fn >= url; --fn)
-		if (*fn == '/') break;
-	++fn; // fn now points to the file name
-	fp_remote = knet_open(url, "r");
-	if (fp_remote == 0) {
-		fprintf(pysamerr, "[download_from_remote] fail to open remote file.\n");
-		return;
-	}
-	if ((fp = fopen(fn, "wb")) == 0) {
-		fprintf(pysamerr, "[download_from_remote] fail to create file in the working directory.\n");
-		knet_close(fp_remote);
-		return;
-	}
-	buf = (uint8_t*)calloc(buf_size, 1);
-	while ((l = knet_read(fp_remote, buf, buf_size)) != 0)
-		fwrite(buf, 1, l, fp);
-	free(buf);
-	fclose(fp);
-	knet_close(fp_remote);
-}
-#else
-static void download_from_remote(const char *url)
-{
-	return;
-}
-#endif
-
-bam_index_t *bam_index_load(const char *fn)
-{
-	bam_index_t *idx;
-	idx = bam_index_load_local(fn);
-	if (idx == 0 && (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn)) {
-		char *fnidx = calloc(strlen(fn) + 5, 1);
-		strcat(strcpy(fnidx, fn), ".bai");
-		fprintf(pysamerr, "[bam_index_load] attempting to download the remote index file.\n");
-		download_from_remote(fnidx);
-        free(fnidx);
-		idx = bam_index_load_local(fn);
-	}
-	if (idx == 0) fprintf(pysamerr, "[bam_index_load] fail to load BAM index.\n");
-	return idx;
-}
-
 int bam_index_build2(const char *fn, const char *_fnidx)
 {
-	char *fnidx;
-	FILE *fpidx;
-	bamFile fp;
-	bam_index_t *idx;
-	if ((fp = bam_open(fn, "r")) == 0) {
-		fprintf(pysamerr, "[bam_index_build2] fail to open the BAM file.\n");
-		return -1;
-	}
-	idx = bam_index_core(fp);
-	bam_close(fp);
-	if(idx == 0) {
-		fprintf(pysamerr, "[bam_index_build2] fail to index the BAM file.\n");
-		return -1;
-	}
-	if (_fnidx == 0) {
-		fnidx = (char*)calloc(strlen(fn) + 5, 1);
-		strcpy(fnidx, fn); strcat(fnidx, ".bai");
-	} else fnidx = strdup(_fnidx);
-	fpidx = fopen(fnidx, "wb");
-	if (fpidx == 0) {
-		fprintf(pysamerr, "[bam_index_build2] fail to create the index file.\n");
-		free(fnidx);
-        bam_index_destroy(idx);
-		return -1;
-	}
-	bam_index_save(idx, fpidx);
-	bam_index_destroy(idx);
-	fclose(fpidx);
-	free(fnidx);
-	return 0;
+    fprintf(pysamerr, "Samtools-htslib-API: bam_index_build2() not yet implemented\n");
+    abort();
 }
 
-int bam_index_build(const char *fn)
+static void index_usage(FILE *fp)
 {
-	return bam_index_build2(fn, 0);
+    fprintf(fp,
+"Usage: samtools index [-bc] [-m INT] <in.bam> [out.index]\n"
+"Options:\n"
+"  -b       Generate BAI-format index for BAM files [default]\n"
+"  -c       Generate CSI-format index for BAM files\n"
+"  -m INT   Set minimum interval size for CSI indices to 2^INT [%d]\n", BAM_LIDX_SHIFT);
 }
 
 int bam_index(int argc, char *argv[])
 {
-	if (argc < 2) {
-		fprintf(pysamerr, "Usage: samtools index <in.bam> [out.index]\n");
-		return 1;
-	}
-	if (argc >= 3) bam_index_build2(argv[1], argv[2]);
-	else bam_index_build(argv[1]);
-	return 0;
+    int csi = 0;
+    int min_shift = BAM_LIDX_SHIFT;
+    int c;
+
+    while ((c = getopt(argc, argv, "bcm:")) >= 0)
+        switch (c) {
+        case 'b': csi = 0; break;
+        case 'c': csi = 1; break;
+        case 'm': csi = 1; min_shift = atoi(optarg); break;
+        default:
+            index_usage(pysamerr);
+            return 1;
+        }
+
+    if (optind == argc) {
+        index_usage(stdout);
+        return 1;
+    }
+    if (argc - optind > 1) bam_index_build2(argv[optind], argv[optind+1]);
+    else bam_index_build(argv[optind], csi? min_shift : 0);
+    return 0;
 }
 
 int bam_idxstats(int argc, char *argv[])
 {
-	bam_index_t *idx;
-	bam_header_t *header;
-	bamFile fp;
-	int i;
-	if (argc < 2) {
-		fprintf(pysamerr, "Usage: samtools idxstats <in.bam>\n");
-		return 1;
-	}
-	fp = bam_open(argv[1], "r");
-	if (fp == 0) { fprintf(pysamerr, "[%s] fail to open BAM.\n", __func__); return 1; }
-	header = bam_header_read(fp);
-	bam_close(fp);
-	idx = bam_index_load(argv[1]);
-	if (idx == 0) { fprintf(pysamerr, "[%s] fail to load the index.\n", __func__); return 1; }
-	for (i = 0; i < idx->n; ++i) {
-		khint_t k;
-		khash_t(i) *h = idx->index[i];
-		printf("%s\t%d", header->target_name[i], header->target_len[i]);
-		k = kh_get(i, h, BAM_MAX_BIN);
-		if (k != kh_end(h))
-			printf("\t%llu\t%llu", (long long)kh_val(h, k).list[1].u, (long long)kh_val(h, k).list[1].v);
-		else printf("\t0\t0");
-		putchar('\n');
-	}
-	printf("*\t0\t0\t%llu\n", (long long)idx->n_no_coor);
-	bam_header_destroy(header);
-	bam_index_destroy(idx);
-	return 0;
-}
-
-static inline int reg2bins(uint32_t beg, uint32_t end, uint16_t list[BAM_MAX_BIN])
-{
-	int i = 0, k;
-	if (beg >= end) return 0;
-	if (end >= 1u<<29) end = 1u<<29;
-	--end;
-	list[i++] = 0;
-	for (k =    1 + (beg>>26); k <=    1 + (end>>26); ++k) list[i++] = k;
-	for (k =    9 + (beg>>23); k <=    9 + (end>>23); ++k) list[i++] = k;
-	for (k =   73 + (beg>>20); k <=   73 + (end>>20); ++k) list[i++] = k;
-	for (k =  585 + (beg>>17); k <=  585 + (end>>17); ++k) list[i++] = k;
-	for (k = 4681 + (beg>>14); k <= 4681 + (end>>14); ++k) list[i++] = k;
-	return i;
-}
-
-static inline int is_overlap(uint32_t beg, uint32_t end, const bam1_t *b)
-{
-	uint32_t rbeg = b->core.pos;
-	uint32_t rend = b->core.n_cigar? bam_calend(&b->core, bam1_cigar(b)) : b->core.pos + 1;
-	return (rend > beg && rbeg < end);
-}
-
-struct __bam_iter_t {
-	int from_first; // read from the first record; no random access
-	int tid, beg, end, n_off, i, finished;
-	uint64_t curr_off;
-	pair64_t *off;
-};
-
-// bam_fetch helper function retrieves 
-bam_iter_t bam_iter_query(const bam_index_t *idx, int tid, int beg, int end)
-{
-	uint16_t *bins;
-	int i, n_bins, n_off;
-	pair64_t *off;
-	khint_t k;
-	khash_t(i) *index;
-	uint64_t min_off;
-	bam_iter_t iter = 0;
-
-	if (beg < 0) beg = 0;
-	if (end < beg) return 0;
-	// initialize iter
-	iter = calloc(1, sizeof(struct __bam_iter_t));
-	iter->tid = tid, iter->beg = beg, iter->end = end; iter->i = -1;
-	//
-	bins = (uint16_t*)calloc(BAM_MAX_BIN, 2);
-	n_bins = reg2bins(beg, end, bins);
-	index = idx->index[tid];
-	if (idx->index2[tid].n > 0) {
-		min_off = (beg>>BAM_LIDX_SHIFT >= idx->index2[tid].n)? idx->index2[tid].offset[idx->index2[tid].n-1]
-			: idx->index2[tid].offset[beg>>BAM_LIDX_SHIFT];
-		if (min_off == 0) { // improvement for index files built by tabix prior to 0.1.4
-			int n = beg>>BAM_LIDX_SHIFT;
-			if (n > idx->index2[tid].n) n = idx->index2[tid].n;
-			for (i = n - 1; i >= 0; --i)
-				if (idx->index2[tid].offset[i] != 0) break;
-			if (i >= 0) min_off = idx->index2[tid].offset[i];
-		}
-	} else min_off = 0; // tabix 0.1.2 may produce such index files
-	for (i = n_off = 0; i < n_bins; ++i) {
-		if ((k = kh_get(i, index, bins[i])) != kh_end(index))
-			n_off += kh_value(index, k).n;
-	}
-	if (n_off == 0) {
-		free(bins); return iter;
-	}
-	off = (pair64_t*)calloc(n_off, 16);
-	for (i = n_off = 0; i < n_bins; ++i) {
-		if ((k = kh_get(i, index, bins[i])) != kh_end(index)) {
-			int j;
-			bam_binlist_t *p = &kh_value(index, k);
-			for (j = 0; j < p->n; ++j)
-				if (p->list[j].v > min_off) off[n_off++] = p->list[j];
-		}
-	}
-	free(bins);
-	if (n_off == 0) {
-		free(off); return iter;
-	}
-	{
-		bam1_t *b = (bam1_t*)calloc(1, sizeof(bam1_t));
-		int l;
-		ks_introsort(off, n_off, off);
-		// resolve completely contained adjacent blocks
-		for (i = 1, l = 0; i < n_off; ++i)
-			if (off[l].v < off[i].v)
-				off[++l] = off[i];
-		n_off = l + 1;
-		// resolve overlaps between adjacent blocks; this may happen due to the merge in indexing
-		for (i = 1; i < n_off; ++i)
-			if (off[i-1].v >= off[i].u) off[i-1].v = off[i].u;
-		{ // merge adjacent blocks
-#if defined(BAM_TRUE_OFFSET) || defined(BAM_VIRTUAL_OFFSET16)
-			for (i = 1, l = 0; i < n_off; ++i) {
-#ifdef BAM_TRUE_OFFSET
-				if (off[l].v + BAM_MIN_CHUNK_GAP > off[i].u) off[l].v = off[i].v;
-#else
-				if (off[l].v>>16 == off[i].u>>16) off[l].v = off[i].v;
-#endif
-				else off[++l] = off[i];
-			}
-			n_off = l + 1;
-#endif
-		}
-		bam_destroy1(b);
-	}
-	iter->n_off = n_off; iter->off = off;
-	return iter;
-}
-
-pair64_t *get_chunk_coordinates(const bam_index_t *idx, int tid, int beg, int end, int *cnt_off)
-{ // for pysam compatibility
-	bam_iter_t iter;
-	pair64_t *off;
-	iter = bam_iter_query(idx, tid, beg, end);
-	off = iter->off; *cnt_off = iter->n_off;
-	free(iter);
-	return off;
-}
-
-void bam_iter_destroy(bam_iter_t iter)
-{
-	if (iter) { free(iter->off); free(iter); }
-}
-
-int bam_iter_read(bamFile fp, bam_iter_t iter, bam1_t *b)
-{
-	int ret;
-	if (iter && iter->finished) return -1;
-	if (iter == 0 || iter->from_first) {
-		ret = bam_read1(fp, b);
-		if (ret < 0 && iter) iter->finished = 1;
-		return ret;
-	}
-	if (iter->off == 0) return -1;
-	for (;;) {
-		if (iter->curr_off == 0 || iter->curr_off >= iter->off[iter->i].v) { // then jump to the next chunk
-			if (iter->i == iter->n_off - 1) { ret = -1; break; } // no more chunks
-			if (iter->i >= 0) assert(iter->curr_off == iter->off[iter->i].v); // otherwise bug
-			if (iter->i < 0 || iter->off[iter->i].v != iter->off[iter->i+1].u) { // not adjacent chunks; then seek
-				bam_seek(fp, iter->off[iter->i+1].u, SEEK_SET);
-				iter->curr_off = bam_tell(fp);
-			}
-			++iter->i;
-		}
-		if ((ret = bam_read1(fp, b)) >= 0) {
-			iter->curr_off = bam_tell(fp);
-			if (b->core.tid != iter->tid || b->core.pos >= iter->end) { // no need to proceed
-				ret = bam_validate1(NULL, b)? -1 : -5; // determine whether end of region or error
-				break;
-			}
-			else if (is_overlap(iter->beg, iter->end, b)) return ret;
-		} else break; // end of file or error
-	}
-	iter->finished = 1;
-	return ret;
-}
-
-int bam_fetch(bamFile fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func)
-{
-	int ret;
-	bam_iter_t iter;
-	bam1_t *b;
-	b = bam_init1();
-	iter = bam_iter_query(idx, tid, beg, end);
-	while ((ret = bam_iter_read(fp, iter, b)) >= 0) func(b, data);
-	bam_iter_destroy(iter);
-	bam_destroy1(b);
-	return (ret == -1)? 0 : ret;
+    hts_idx_t* idx;
+    bam_hdr_t* header;
+    samFile* fp;
+
+    if (argc < 2) {
+        fprintf(pysamerr, "Usage: samtools idxstats <in.bam>\n");
+        return 1;
+    }
+    fp = sam_open(argv[1], "r");
+    if (fp == NULL) { fprintf(pysamerr, "[%s] fail to open BAM.\n", __func__); return 1; }
+    header = sam_hdr_read(fp);
+    idx = sam_index_load(fp, argv[1]);
+    if (idx == NULL) { fprintf(pysamerr, "[%s] fail to load the index.\n", __func__); return 1; }
+
+    int i;
+    for (i = 0; i < header->n_targets; ++i) {
+        // Print out contig name and length
+        printf("%s\t%d", header->target_name[i], header->target_len[i]);
+        // Now fetch info about it from the meta bin
+        uint64_t u, v;
+        hts_idx_get_stat(idx, i, &u, &v);
+        printf("\t%" PRIu64 "\t%" PRIu64 "\n", u, v);
+    }
+    // Dump information about unmapped reads
+    printf("*\t0\t0\t%" PRIu64 "\n", hts_idx_get_n_no_coor(idx));
+    bam_hdr_destroy(header);
+    hts_idx_destroy(idx);
+    sam_close(fp);
+    return 0;
 }
diff --git a/samtools/bam_lpileup.c.pysam.c b/samtools/bam_lpileup.c.pysam.c
index e3c2785..bdf4348 100644
--- a/samtools/bam_lpileup.c.pysam.c
+++ b/samtools/bam_lpileup.c.pysam.c
@@ -1,16 +1,41 @@
 #include "pysam.h"
 
+/*  bam_lpileup.c -- lplbuf routines.
+
+    Copyright (C) 2008, 2009, 2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
-#include "bam.h"
-#include "ksort.h"
+#include "bam_plbuf.h"
+#include "bam_lpileup.h"
+#include <htslib/ksort.h>
 
 #define TV_GAP 2
 
 typedef struct __freenode_t {
-	uint32_t level:28, cnt:4;
-	struct __freenode_t *next;
+    uint32_t level:28, cnt:4;
+    struct __freenode_t *next;
 } freenode_t, *freenode_p;
 
 #define freenode_lt(a,b) ((a)->cnt < (b)->cnt || ((a)->cnt == (b)->cnt && (a)->level < (b)->level))
@@ -18,183 +43,183 @@ KSORT_INIT(node, freenode_p, freenode_lt)
 
 /* Memory pool, similar to the one in bam_pileup.c */
 typedef struct {
-	int cnt, n, max;
-	freenode_t **buf;
+    int cnt, n, max;
+    freenode_t **buf;
 } mempool_t;
 
 static mempool_t *mp_init(void)
 {
-	return (mempool_t*)calloc(1, sizeof(mempool_t));
+    return (mempool_t*)calloc(1, sizeof(mempool_t));
 }
 static void mp_destroy(mempool_t *mp)
 {
-	int k;
-	for (k = 0; k < mp->n; ++k) free(mp->buf[k]);
-	free(mp->buf); free(mp);
+    int k;
+    for (k = 0; k < mp->n; ++k) free(mp->buf[k]);
+    free(mp->buf); free(mp);
 }
 static inline freenode_t *mp_alloc(mempool_t *mp)
 {
-	++mp->cnt;
-	if (mp->n == 0) return (freenode_t*)calloc(1, sizeof(freenode_t));
-	else return mp->buf[--mp->n];
+    ++mp->cnt;
+    if (mp->n == 0) return (freenode_t*)calloc(1, sizeof(freenode_t));
+    else return mp->buf[--mp->n];
 }
 static inline void mp_free(mempool_t *mp, freenode_t *p)
 {
-	--mp->cnt; p->next = 0; p->cnt = TV_GAP;
-	if (mp->n == mp->max) {
-		mp->max = mp->max? mp->max<<1 : 256;
-		mp->buf = (freenode_t**)realloc(mp->buf, sizeof(freenode_t*) * mp->max);
-	}
-	mp->buf[mp->n++] = p;
+    --mp->cnt; p->next = 0; p->cnt = TV_GAP;
+    if (mp->n == mp->max) {
+        mp->max = mp->max? mp->max<<1 : 256;
+        mp->buf = (freenode_t**)realloc(mp->buf, sizeof(freenode_t*) * mp->max);
+    }
+    mp->buf[mp->n++] = p;
 }
 
 /* core part */
 struct __bam_lplbuf_t {
-	int max, n_cur, n_pre;
-	int max_level, *cur_level, *pre_level;
-	mempool_t *mp;
-	freenode_t **aux, *head, *tail;
-	int n_nodes, m_aux;
-	bam_pileup_f func;
-	void *user_data;
-	bam_plbuf_t *plbuf;
+    int max, n_cur, n_pre;
+    int max_level, *cur_level, *pre_level;
+    mempool_t *mp;
+    freenode_t **aux, *head, *tail;
+    int n_nodes, m_aux;
+    bam_pileup_f func;
+    void *user_data;
+    bam_plbuf_t *plbuf;
 };
 
 void bam_lplbuf_reset(bam_lplbuf_t *buf)
 {
-	freenode_t *p, *q;
-	bam_plbuf_reset(buf->plbuf);
-	for (p = buf->head; p->next;) {
-		q = p->next;
-		mp_free(buf->mp, p);
-		p = q;
-	}
-	buf->head = buf->tail;
-	buf->max_level = 0;
-	buf->n_cur = buf->n_pre = 0;
-	buf->n_nodes = 0;
+    freenode_t *p, *q;
+    bam_plbuf_reset(buf->plbuf);
+    for (p = buf->head; p->next;) {
+        q = p->next;
+        mp_free(buf->mp, p);
+        p = q;
+    }
+    buf->head = buf->tail;
+    buf->max_level = 0;
+    buf->n_cur = buf->n_pre = 0;
+    buf->n_nodes = 0;
 }
 
 static int tview_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
 {
-	bam_lplbuf_t *tv = (bam_lplbuf_t*)data;
-	freenode_t *p;
-	int i, l, max_level;
-	// allocate memory if necessary
-	if (tv->max < n) { // enlarge
-		tv->max = n;
-		kroundup32(tv->max);
-		tv->cur_level = (int*)realloc(tv->cur_level, sizeof(int) * tv->max);
-		tv->pre_level = (int*)realloc(tv->pre_level, sizeof(int) * tv->max);
-	}
-	tv->n_cur = n;
-	// update cnt
-	for (p = tv->head; p->next; p = p->next)
-		if (p->cnt > 0) --p->cnt;
-	// calculate cur_level[]
-	max_level = 0;
-	for (i = l = 0; i < n; ++i) {
-		const bam_pileup1_t *p = pl + i;
-		if (p->is_head) {
-			if (tv->head->next && tv->head->cnt == 0) { // then take a free slot
-				freenode_t *p = tv->head->next;
-				tv->cur_level[i] = tv->head->level;
-				mp_free(tv->mp, tv->head);
-				tv->head = p;
-				--tv->n_nodes;
-			} else tv->cur_level[i] = ++tv->max_level;
-		} else {
-			tv->cur_level[i] = tv->pre_level[l++];
-			if (p->is_tail) { // then return a free slot
-				tv->tail->level = tv->cur_level[i];
-				tv->tail->next = mp_alloc(tv->mp);
-				tv->tail = tv->tail->next;
-				++tv->n_nodes;
-			}
-		}
-		if (tv->cur_level[i] > max_level) max_level = tv->cur_level[i];
-		((bam_pileup1_t*)p)->level = tv->cur_level[i];
-	}
-	assert(l == tv->n_pre);
-	tv->func(tid, pos, n, pl, tv->user_data);
-	// sort the linked list
-	if (tv->n_nodes) {
-		freenode_t *q;
-		if (tv->n_nodes + 1 > tv->m_aux) { // enlarge
-			tv->m_aux = tv->n_nodes + 1;
-			kroundup32(tv->m_aux);
-			tv->aux = (freenode_t**)realloc(tv->aux, sizeof(void*) * tv->m_aux);
-		}
-		for (p = tv->head, i = l = 0; p->next;) {
-			if (p->level > max_level) { // then discard this entry
-				q = p->next;
-				mp_free(tv->mp, p);
-				p = q;
-			} else {
-				tv->aux[i++] = p;
-				p = p->next;
-			}
-		}
-		tv->aux[i] = tv->tail; // add a proper tail for the loop below
-		tv->n_nodes = i;
-		if (tv->n_nodes) {
-			ks_introsort(node, tv->n_nodes, tv->aux);
-			for (i = 0; i < tv->n_nodes; ++i) tv->aux[i]->next = tv->aux[i+1];
-			tv->head = tv->aux[0];
-		} else tv->head = tv->tail;
-	}
-	// clean up
-	tv->max_level = max_level;
-	memcpy(tv->pre_level, tv->cur_level, tv->n_cur * 4);
-	// squeeze out terminated levels
-	for (i = l = 0; i < n; ++i) {
-		const bam_pileup1_t *p = pl + i;
-		if (!p->is_tail)
-			tv->pre_level[l++] = tv->pre_level[i];
-	}
-	tv->n_pre = l;
+    bam_lplbuf_t *tv = (bam_lplbuf_t*)data;
+    freenode_t *p;
+    int i, l, max_level;
+    // allocate memory if necessary
+    if (tv->max < n) { // enlarge
+        tv->max = n;
+        kroundup32(tv->max);
+        tv->cur_level = (int*)realloc(tv->cur_level, sizeof(int) * tv->max);
+        tv->pre_level = (int*)realloc(tv->pre_level, sizeof(int) * tv->max);
+    }
+    tv->n_cur = n;
+    // update cnt
+    for (p = tv->head; p->next; p = p->next)
+        if (p->cnt > 0) --p->cnt;
+    // calculate cur_level[]
+    max_level = 0;
+    for (i = l = 0; i < n; ++i) {
+        const bam_pileup1_t *p = pl + i;
+        if (p->is_head) {
+            if (tv->head->next && tv->head->cnt == 0) { // then take a free slot
+                freenode_t *p = tv->head->next;
+                tv->cur_level[i] = tv->head->level;
+                mp_free(tv->mp, tv->head);
+                tv->head = p;
+                --tv->n_nodes;
+            } else tv->cur_level[i] = ++tv->max_level;
+        } else {
+            tv->cur_level[i] = tv->pre_level[l++];
+            if (p->is_tail) { // then return a free slot
+                tv->tail->level = tv->cur_level[i];
+                tv->tail->next = mp_alloc(tv->mp);
+                tv->tail = tv->tail->next;
+                ++tv->n_nodes;
+            }
+        }
+        if (tv->cur_level[i] > max_level) max_level = tv->cur_level[i];
+        ((bam_pileup1_t*)p)->level = tv->cur_level[i];
+    }
+    assert(l == tv->n_pre);
+    tv->func(tid, pos, n, pl, tv->user_data);
+    // sort the linked list
+    if (tv->n_nodes) {
+        freenode_t *q;
+        if (tv->n_nodes + 1 > tv->m_aux) { // enlarge
+            tv->m_aux = tv->n_nodes + 1;
+            kroundup32(tv->m_aux);
+            tv->aux = (freenode_t**)realloc(tv->aux, sizeof(freenode_t*) * tv->m_aux);
+        }
+        for (p = tv->head, i = l = 0; p->next;) {
+            if (p->level > max_level) { // then discard this entry
+                q = p->next;
+                mp_free(tv->mp, p);
+                p = q;
+            } else {
+                tv->aux[i++] = p;
+                p = p->next;
+            }
+        }
+        tv->aux[i] = tv->tail; // add a proper tail for the loop below
+        tv->n_nodes = i;
+        if (tv->n_nodes) {
+            ks_introsort(node, tv->n_nodes, tv->aux);
+            for (i = 0; i < tv->n_nodes; ++i) tv->aux[i]->next = tv->aux[i+1];
+            tv->head = tv->aux[0];
+        } else tv->head = tv->tail;
+    }
+    // clean up
+    tv->max_level = max_level;
+    memcpy(tv->pre_level, tv->cur_level, tv->n_cur * 4);
+    // squeeze out terminated levels
+    for (i = l = 0; i < n; ++i) {
+        const bam_pileup1_t *p = pl + i;
+        if (!p->is_tail)
+            tv->pre_level[l++] = tv->pre_level[i];
+    }
+    tv->n_pre = l;
 /*
-	fprintf(pysamerr, "%d\t", pos+1);
-	for (i = 0; i < n; ++i) {
-		const bam_pileup1_t *p = pl + i;
-		if (p->is_head) fprintf(pysamerr, "^");
-		if (p->is_tail) fprintf(pysamerr, "$");
-		fprintf(pysamerr, "%d,", p->level);
-	}
-	fprintf(pysamerr, "\n");
+    fprintf(pysamerr, "%d\t", pos+1);
+    for (i = 0; i < n; ++i) {
+        const bam_pileup1_t *p = pl + i;
+        if (p->is_head) fprintf(pysamerr, "^");
+        if (p->is_tail) fprintf(pysamerr, "$");
+        fprintf(pysamerr, "%d,", p->level);
+    }
+    fprintf(pysamerr, "\n");
 */
-	return 0;
+    return 0;
 }
 
 bam_lplbuf_t *bam_lplbuf_init(bam_pileup_f func, void *data)
 {
-	bam_lplbuf_t *tv;
-	tv = (bam_lplbuf_t*)calloc(1, sizeof(bam_lplbuf_t));
-	tv->mp = mp_init();
-	tv->head = tv->tail = mp_alloc(tv->mp);
-	tv->func = func;
-	tv->user_data = data;
-	tv->plbuf = bam_plbuf_init(tview_func, tv);
-	return (bam_lplbuf_t*)tv;
+    bam_lplbuf_t *tv;
+    tv = (bam_lplbuf_t*)calloc(1, sizeof(bam_lplbuf_t));
+    tv->mp = mp_init();
+    tv->head = tv->tail = mp_alloc(tv->mp);
+    tv->func = func;
+    tv->user_data = data;
+    tv->plbuf = bam_plbuf_init(tview_func, tv);
+    return (bam_lplbuf_t*)tv;
 }
 
 void bam_lplbuf_destroy(bam_lplbuf_t *tv)
 {
-	freenode_t *p, *q;
-	free(tv->cur_level); free(tv->pre_level);
-	bam_plbuf_destroy(tv->plbuf);
-	free(tv->aux);
-	for (p = tv->head; p->next;) {
-		q = p->next;
-		mp_free(tv->mp, p); p = q;
-	}
-	mp_free(tv->mp, p);
-	assert(tv->mp->cnt == 0);
-	mp_destroy(tv->mp);
-	free(tv);
+    freenode_t *p, *q;
+    free(tv->cur_level); free(tv->pre_level);
+    bam_plbuf_destroy(tv->plbuf);
+    free(tv->aux);
+    for (p = tv->head; p->next;) {
+        q = p->next;
+        mp_free(tv->mp, p); p = q;
+    }
+    mp_free(tv->mp, p);
+    assert(tv->mp->cnt == 0);
+    mp_destroy(tv->mp);
+    free(tv);
 }
 
 int bam_lplbuf_push(const bam1_t *b, bam_lplbuf_t *tv)
 {
-	return bam_plbuf_push(b, tv->plbuf);
+    return bam_plbuf_push(b, tv->plbuf);
 }
diff --git a/samtools/bam_lpileup.h b/samtools/bam_lpileup.h
new file mode 100644
index 0000000..24acd2b
--- /dev/null
+++ b/samtools/bam_lpileup.h
@@ -0,0 +1,57 @@
+/*  bam_lpileup.h -- lplbuf routines (declarations copied from bam.h).
+
+    Copyright (C) 2008, 2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#ifndef BAM_LPILEUP_H
+#define BAM_LPILEUP_H
+
+
+#include <htslib/sam.h>
+
+struct __bam_lplbuf_t;
+typedef struct __bam_lplbuf_t bam_lplbuf_t;
+
+#ifndef BAM_PILEUP_F_DEFINED
+#define BAM_PILEUP_F_DEFINED
+typedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data);
+#endif //BAM_PILEUP_F_DEFINED
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+    void bam_lplbuf_reset(bam_lplbuf_t *buf);
+
+    /*! @abstract  bam_plbuf_init() equivalent with level calculated. */
+    bam_lplbuf_t *bam_lplbuf_init(bam_pileup_f func, void *data);
+
+    /*! @abstract  bam_plbuf_destroy() equivalent with level calculated. */
+    void bam_lplbuf_destroy(bam_lplbuf_t *tv);
+
+    /*! @abstract  bam_plbuf_push() equivalent with level calculated. */
+    int bam_lplbuf_push(const bam1_t *b, bam_lplbuf_t *buf);
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAM_LPILEUP_H
diff --git a/samtools/bam_mate.c.pysam.c b/samtools/bam_mate.c.pysam.c
index bdeb885..210e6ea 100644
--- a/samtools/bam_mate.c.pysam.c
+++ b/samtools/bam_mate.c.pysam.c
@@ -1,130 +1,349 @@
 #include "pysam.h"
 
+/*  bam_mate.c -- fix mate pairing information and clean up flags.
+
+    Copyright (C) 2009, 2011-2014 Genome Research Ltd.
+    Portions copyright (C) 2011 Broad Institute.
+    Portions copyright (C) 2012 Peter Cock, The James Hutton Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notices and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <assert.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "kstring.h"
-#include "bam.h"
+#include "htslib/kstring.h"
+#include "htslib/sam.h"
+
+/*
+ * This function calculates ct tag for two bams, it assumes they are from the same template and
+ * writes the tag to the first read in position terms.
+ */
+static void bam_template_cigar(bam1_t *b1, bam1_t *b2, kstring_t *str)
+{
+    bam1_t *swap;
+    int i, end;
+    uint32_t *cigar;
+    str->l = 0;
+    if (b1->core.tid != b2->core.tid || b1->core.tid < 0 || b1->core.pos < 0 || b2->core.pos < 0 || b1->core.flag&BAM_FUNMAP || b2->core.flag&BAM_FUNMAP) return; // coordinateless or not on the same chr; skip
+    if (b1->core.pos > b2->core.pos) swap = b1, b1 = b2, b2 = swap; // make sure b1 has a smaller coordinate
+    kputc((b1->core.flag & BAM_FREAD1)? '1' : '2', str); // segment index
+    kputc((b1->core.flag & BAM_FREVERSE)? 'R' : 'F', str); // strand
+    for (i = 0, cigar = bam_get_cigar(b1); i < b1->core.n_cigar; ++i) {
+        kputw(bam_cigar_oplen(cigar[i]), str);
+        kputc(bam_cigar_opchr(cigar[i]), str);
+    }
+    end = bam_endpos(b1);
+    kputw(b2->core.pos - end, str);
+    kputc('T', str);
+    kputc((b2->core.flag & BAM_FREAD1)? '1' : '2', str); // segment index
+    kputc((b2->core.flag & BAM_FREVERSE)? 'R' : 'F', str); // strand
+    for (i = 0, cigar = bam_get_cigar(b2); i < b2->core.n_cigar; ++i) {
+        kputw(bam_cigar_oplen(cigar[i]), str);
+        kputc(bam_cigar_opchr(cigar[i]), str);
+    }
+
+    uint8_t* data;
+    if ((data = bam_aux_get(b1,"ct")) != NULL) bam_aux_del(b1, data);
+    if ((data = bam_aux_get(b2,"ct")) != NULL) bam_aux_del(b2, data);
+
+    bam_aux_append(b1, "ct", 'Z', str->l+1, (uint8_t*)str->s);
+}
+
+/*
+ * What This Program is Supposed To Do:
+ * Fill in mate coordinates, ISIZE and mate related flags from a name-sorted
+ * alignment.
+ *
+ * How We Handle Input
+ *
+ * Secondary Reads:
+ * -write to output unchanged
+ * All Reads:
+ * -if pos == 0 (1 based), tid == -1 set UNMAPPED flag
+ * single Reads:
+ * -if pos == 0 (1 based), tid == -1, or UNMAPPED then set UNMAPPED, pos = 0,
+ *  tid = -1
+ * -clear bad flags (PAIRED, MREVERSE, PROPER_PAIR)
+ * -set mpos = 0 (1 based), mtid = -1 and isize = 0
+ * -write to output
+ * Paired Reads:
+ * -if read is unmapped and mate is not, set pos and tid to equal that of mate
+ * -sync mate flags (MREVERSE, MUNMAPPED), mpos, mtid
+ * -recalculate ISIZE if possible, otherwise set it to 0
+ * -optionally clear PROPER_PAIR flag from reads where mapping or orientation
+ *  indicate this is not possible (Illumina orientation only)
+ * -calculate ct and apply to lowest positioned read
+ * -write to output
+ * Limitations
+ * -Does not handle tandem reads
+ * Notes
+ * -CT definition appears to be something else in spec, this was in here before
+ *  I started tampering with it, anyone know what is going on here? To work
+ *  around this I have demoted the CT this tool generates to ct.
+ */
+
+static void sync_unmapped_pos_inner(bam1_t* src, bam1_t* dest) {
+    if ((dest->core.flag & BAM_FUNMAP) && !(src->core.flag & BAM_FUNMAP)) {
+        // Set unmapped read's RNAME and POS to those of its mapped mate
+        // (recommended best practice, ensures if coord sort will be together)
+        dest->core.tid = src->core.tid;
+        dest->core.pos = src->core.pos;
+    }
+}
+
+static void sync_mate_inner(bam1_t* src, bam1_t* dest)
+{
+    // sync mate pos information
+    dest->core.mtid = src->core.tid; dest->core.mpos = src->core.pos;
+    // sync flag info
+    if (src->core.flag&BAM_FREVERSE)
+        dest->core.flag |= BAM_FMREVERSE;
+    else
+        dest->core.flag &= ~BAM_FMREVERSE;
+    if (src->core.flag & BAM_FUNMAP) {
+        dest->core.flag |= BAM_FMUNMAP;
+    }
+}
+
+// Is it plausible that these reads are properly paired?
+// Can't really give definitive answer without checking isize
+static bool plausibly_properly_paired(bam1_t* a, bam1_t* b)
+{
+    if ((a->core.flag & BAM_FUNMAP) || (b->core.flag & BAM_FUNMAP)) return false;
+    assert(a->core.tid >= 0); // This should never happen if FUNMAP is set correctly
+
+    if (a->core.tid != b->core.tid) return false;
+
+    bam1_t* first = a;
+    bam1_t* second = b;
+    int32_t a_pos = a->core.flag&BAM_FREVERSE ? bam_endpos(a) : a->core.pos;
+    int32_t b_pos = b->core.flag&BAM_FREVERSE ? bam_endpos(b) : b->core.pos;
+    if (a_pos > b_pos) {
+        first = b;
+        second = a;
+    } else {
+        first = a;
+        second = b;
+    }
+
+    if (!(first->core.flag&BAM_FREVERSE) && (second->core.flag&BAM_FREVERSE))
+        return true;
+    else
+        return false;
+}
 
-void bam_template_cigar(bam1_t *b1, bam1_t *b2, kstring_t *str)
+static void sync_mq(bam1_t* src, bam1_t* dest)
 {
-	bam1_t *swap;
-	int i, end;
-	uint32_t *cigar;
-	str->l = 0;
-	if (b1->core.tid != b2->core.tid || b1->core.tid < 0) return; // coordinateless or not on the same chr; skip
-	if (b1->core.pos > b2->core.pos) swap = b1, b1 = b2, b2 = swap; // make sure b1 has a smaller coordinate
-	kputc((b1->core.flag & BAM_FREAD1)? '1' : '2', str); // segment index
-	kputc((b1->core.flag & BAM_FREVERSE)? 'R' : 'F', str); // strand
-	for (i = 0, cigar = bam1_cigar(b1); i < b1->core.n_cigar; ++i) {
-		kputw(bam_cigar_oplen(cigar[i]), str);
-		kputc(bam_cigar_opchr(cigar[i]), str);
-	}
-	end = bam_calend(&b1->core, cigar);
-	kputw(b2->core.pos - end, str);
-	kputc('T', str);
-	kputc((b2->core.flag & BAM_FREAD1)? '1' : '2', str); // segment index
-	kputc((b2->core.flag & BAM_FREVERSE)? 'R' : 'F', str); // strand
-	for (i = 0, cigar = bam1_cigar(b2); i < b2->core.n_cigar; ++i) {
-		kputw(bam_cigar_oplen(cigar[i]), str);
-		kputc(bam_cigar_opchr(cigar[i]), str);
-	}
-	bam_aux_append(b1, "CT", 'Z', str->l+1, (uint8_t*)str->s); 
+    if ( (src->core.flag & BAM_FUNMAP) == 0 ) { // If mapped
+        uint32_t mq = src->core.qual;
+        uint8_t* data;
+        if ((data = bam_aux_get(dest,"MQ")) != NULL) {
+            bam_aux_del(dest, data);
+        }
+
+        bam_aux_append(dest, "MQ", 'i', sizeof(uint32_t), (uint8_t*)&mq);
+    }
+}
+
+// copy flags
+static void sync_mate(bam1_t* a, bam1_t* b)
+{
+    sync_unmapped_pos_inner(a,b);
+    sync_unmapped_pos_inner(b,a);
+    sync_mate_inner(a,b);
+    sync_mate_inner(b,a);
+    sync_mq(a,b);
+    sync_mq(b,a);
 }
 
 // currently, this function ONLY works if each read has one hit
-void bam_mating_core(bamFile in, bamFile out, int remove_reads)
+static void bam_mating_core(samFile* in, samFile* out, int remove_reads, int proper_pair_check, int add_ct)
 {
-	bam_header_t *header;
-	bam1_t *b[2];
-	int curr, has_prev, pre_end = 0, cur_end;
-	kstring_t str;
-
-	str.l = str.m = 0; str.s = 0;
-	header = bam_header_read(in);
-	bam_header_write(out, header);
-
-	b[0] = bam_init1();
-	b[1] = bam_init1();
-	curr = 0; has_prev = 0;
-	while (bam_read1(in, b[curr]) >= 0) {
-		bam1_t *cur = b[curr], *pre = b[1-curr];
-		if (cur->core.tid < 0) 
-        {
-            if ( !remove_reads ) bam_write1(out, cur);
-            continue;
+    bam_hdr_t *header;
+    bam1_t *b[2];
+    int curr, has_prev, pre_end = 0, cur_end = 0;
+    kstring_t str;
+
+    str.l = str.m = 0; str.s = 0;
+    header = sam_hdr_read(in);
+    // Accept unknown, unsorted, or queryname sort order, but error on coordinate sorted.
+    if ((header->l_text > 3) && (strncmp(header->text, "@HD", 3) == 0)) {
+        char *p, *q;
+        p = strstr(header->text, "\tSO:coordinate");
+        q = strchr(header->text, '\n');
+        // Looking for SO:coordinate within the @HD line only
+        // (e.g. must ignore in a @CO comment line later in header)
+        if ((p != 0) && (p < q)) {
+            fprintf(pysamerr, "[bam_mating_core] ERROR: Coordinate sorted, require grouped/sorted by queryname.\n");
+            exit(1);
         }
-		cur_end = bam_calend(&cur->core, bam1_cigar(cur));
-		if (cur_end > (int)header->target_len[cur->core.tid]) cur->core.flag |= BAM_FUNMAP;
-		if (cur->core.flag & BAM_FSECONDARY) 
+    }
+    sam_hdr_write(out, header);
+
+    b[0] = bam_init1();
+    b[1] = bam_init1();
+    curr = 0; has_prev = 0;
+    while (sam_read1(in, header, b[curr]) >= 0) {
+        bam1_t *cur = b[curr], *pre = b[1-curr];
+        if (cur->core.flag & BAM_FSECONDARY)
         {
-            if ( !remove_reads ) bam_write1(out, cur);
+            if ( !remove_reads ) sam_write1(out, header, cur);
             continue; // skip secondary alignments
         }
-		if (has_prev) {
-			if (strcmp(bam1_qname(cur), bam1_qname(pre)) == 0) { // identical pair name
-				cur->core.mtid = pre->core.tid; cur->core.mpos = pre->core.pos;
-				pre->core.mtid = cur->core.tid; pre->core.mpos = cur->core.pos;
-				if (pre->core.tid == cur->core.tid && !(cur->core.flag&(BAM_FUNMAP|BAM_FMUNMAP))
-					&& !(pre->core.flag&(BAM_FUNMAP|BAM_FMUNMAP))) // set TLEN/ISIZE
-				{
-					uint32_t cur5, pre5;
-					cur5 = (cur->core.flag&BAM_FREVERSE)? cur_end : cur->core.pos;
-					pre5 = (pre->core.flag&BAM_FREVERSE)? pre_end : pre->core.pos;
-					cur->core.isize = pre5 - cur5; pre->core.isize = cur5 - pre5;
-				} else cur->core.isize = pre->core.isize = 0;
-				if (pre->core.flag&BAM_FREVERSE) cur->core.flag |= BAM_FMREVERSE;
-				else cur->core.flag &= ~BAM_FMREVERSE;
-				if (cur->core.flag&BAM_FREVERSE) pre->core.flag |= BAM_FMREVERSE;
-				else pre->core.flag &= ~BAM_FMREVERSE;
-				if (cur->core.flag & BAM_FUNMAP) { pre->core.flag |= BAM_FMUNMAP; pre->core.flag &= ~BAM_FPROPER_PAIR; }
-				if (pre->core.flag & BAM_FUNMAP) { cur->core.flag |= BAM_FMUNMAP; cur->core.flag &= ~BAM_FPROPER_PAIR; }
-				bam_template_cigar(pre, cur, &str);
-				bam_write1(out, pre);
-				bam_write1(out, cur);
-				has_prev = 0;
-			} else { // unpaired or singleton
-				pre->core.mtid = -1; pre->core.mpos = -1; pre->core.isize = 0;
-				if (pre->core.flag & BAM_FPAIRED) {
-					pre->core.flag |= BAM_FMUNMAP;
-					pre->core.flag &= ~BAM_FMREVERSE & ~BAM_FPROPER_PAIR;
-				}
-				bam_write1(out, pre);
-			}
-		} else has_prev = 1;
-		curr = 1 - curr;
-		pre_end = cur_end;
-	}
-	if (has_prev) bam_write1(out, b[1-curr]);
-	bam_header_destroy(header);
-	bam_destroy1(b[0]);
-	bam_destroy1(b[1]);
-	free(str.s);
+        if (cur->core.flag & BAM_FSUPPLEMENTARY)
+        {
+            sam_write1(out, header, cur);
+            continue; // pass supplementary alignments through unchanged (TODO:make them match read they came from)
+        }
+        if (cur->core.tid < 0 || cur->core.pos < 0) // If unmapped set the flag
+        {
+            cur->core.flag |= BAM_FUNMAP;
+        }
+        if ((cur->core.flag&BAM_FUNMAP) == 0) // If mapped calculate end
+        {
+            cur_end = bam_endpos(cur);
+
+            // Check cur_end isn't past the end of the contig we're on, if it is set the UNMAP'd flag
+            if (cur_end > (int)header->target_len[cur->core.tid]) cur->core.flag |= BAM_FUNMAP;
+        }
+        if (has_prev) { // do we have a pair of reads to examine?
+            if (strcmp(bam_get_qname(cur), bam_get_qname(pre)) == 0) { // identical pair name
+                pre->core.flag |= BAM_FPAIRED;
+                cur->core.flag |= BAM_FPAIRED;
+                sync_mate(pre, cur);
+
+                if (pre->core.tid == cur->core.tid && !(cur->core.flag&(BAM_FUNMAP|BAM_FMUNMAP))
+                    && !(pre->core.flag&(BAM_FUNMAP|BAM_FMUNMAP))) // if safe set TLEN/ISIZE
+                {
+                    uint32_t cur5, pre5;
+                    cur5 = (cur->core.flag&BAM_FREVERSE)? cur_end : cur->core.pos;
+                    pre5 = (pre->core.flag&BAM_FREVERSE)? pre_end : pre->core.pos;
+                    cur->core.isize = pre5 - cur5; pre->core.isize = cur5 - pre5;
+                } else cur->core.isize = pre->core.isize = 0;
+                if (add_ct) bam_template_cigar(pre, cur, &str);
+                // TODO: Add code to properly check if read is in a proper pair based on ISIZE distribution
+                if (proper_pair_check && !plausibly_properly_paired(pre,cur)) {
+                    pre->core.flag &= ~BAM_FPROPER_PAIR;
+                    cur->core.flag &= ~BAM_FPROPER_PAIR;
+                }
+
+                // Write out result
+                if ( !remove_reads ) {
+                    sam_write1(out, header, pre);
+                    sam_write1(out, header, cur);
+                } else {
+                    // If we have to remove reads make sure we do it in a way that doesn't create orphans with bad flags
+                    if(pre->core.flag&BAM_FUNMAP) cur->core.flag &= ~(BAM_FPAIRED|BAM_FMREVERSE|BAM_FPROPER_PAIR);
+                    if(cur->core.flag&BAM_FUNMAP) pre->core.flag &= ~(BAM_FPAIRED|BAM_FMREVERSE|BAM_FPROPER_PAIR);
+                    if(!(pre->core.flag&BAM_FUNMAP)) sam_write1(out, header, pre);
+                    if(!(cur->core.flag&BAM_FUNMAP)) sam_write1(out, header, cur);
+                }
+                has_prev = 0;
+            } else { // unpaired?  clear bad info and write it out
+                if (pre->core.tid < 0 || pre->core.pos < 0 || pre->core.flag&BAM_FUNMAP) { // If unmapped
+                    pre->core.flag |= BAM_FUNMAP;
+                    pre->core.tid = -1;
+                    pre->core.pos = -1;
+                }
+                pre->core.mtid = -1; pre->core.mpos = -1; pre->core.isize = 0;
+                pre->core.flag &= ~(BAM_FPAIRED|BAM_FMREVERSE|BAM_FPROPER_PAIR);
+                if ( !remove_reads || !(pre->core.flag&BAM_FUNMAP) ) sam_write1(out, header, pre);
+            }
+        } else has_prev = 1;
+        curr = 1 - curr;
+        pre_end = cur_end;
+    }
+    if (has_prev && !remove_reads) { // If we still have a BAM in the buffer it must be unpaired
+        bam1_t *pre = b[1-curr];
+        if (pre->core.tid < 0 || pre->core.pos < 0 || pre->core.flag&BAM_FUNMAP) { // If unmapped
+            pre->core.flag |= BAM_FUNMAP;
+            pre->core.tid = -1;
+            pre->core.pos = -1;
+        }
+        pre->core.mtid = -1; pre->core.mpos = -1; pre->core.isize = 0;
+        pre->core.flag &= ~(BAM_FPAIRED|BAM_FMREVERSE|BAM_FPROPER_PAIR);
+
+        sam_write1(out, header, pre);
+    }
+    bam_hdr_destroy(header);
+    bam_destroy1(b[0]);
+    bam_destroy1(b[1]);
+    free(str.s);
 }
 
-void usage(void)
+void usage(FILE* where)
 {
-    fprintf(pysamerr,"Usage: samtools fixmate <in.nameSrt.bam> <out.nameSrt.bam>\n");
-    fprintf(pysamerr,"Options:\n");
-    fprintf(pysamerr,"       -r    remove unmapped reads and secondary alignments\n");
-    exit(1);
+    fprintf(where,"Usage: samtools fixmate <in.nameSrt.bam> <out.nameSrt.bam>\n\n");
+    fprintf(where,"Options:\n");
+    fprintf(pysamerr,"  -r         Remove unmapped reads and secondary alignments\n");
+    fprintf(pysamerr,"  -p         Disable FR proper pair check\n");
+    fprintf(pysamerr,"  -c         Add template cigar ct tag\n");
+    fprintf(pysamerr,"  -O FORMAT  Write output as FORMAT ('sam'/'bam'/'cram')\n");
+    fprintf(pysamerr,"As elsewhere in samtools, use '-' as the filename for stdin/stdout. The input\n");
+    fprintf(pysamerr,"file must be grouped by read name (e.g. sorted by name). Coordinated sorted\n");
+    fprintf(pysamerr,"input is not accepted.\n");
 }
 
 int bam_mating(int argc, char *argv[])
 {
-	bamFile in, out;
-    int c, remove_reads=0;
-    while ((c = getopt(argc, argv, "r")) >= 0) {
+    samFile *in, *out;
+    int c, remove_reads = 0, proper_pair_check = 1, add_ct = 0;
+    char* fmtout = NULL;
+    char modeout[12];
+    // parse args
+    if (argc == 1) { usage(stdout); return 0; }
+    while ((c = getopt(argc, argv, "rpcO:")) >= 0) {
         switch (c) {
-            case 'r': remove_reads=1; break;
+            case 'r': remove_reads = 1; break;
+            case 'p': proper_pair_check = 0; break;
+            case 'c': add_ct = 1; break;
+            case 'O': fmtout = optarg; break;
+            default: usage(pysamerr); return 1;
         }
     }
-    if (optind+1 >= argc) usage();
-	in = (strcmp(argv[optind], "-") == 0)? bam_dopen(fileno(stdin), "r") : bam_open(argv[optind], "r");
-    out = (strcmp(argv[optind+1], "-") == 0)? bam_dopen(fileno(stdout), "w") : bam_open(argv[optind+1], "w");
-	bam_mating_core(in, out, remove_reads);
-	bam_close(in); bam_close(out);
-	return 0;
+    if (optind+1 >= argc) { usage(pysamerr); return 1; }
+    strcpy(modeout, "w");
+    if (sam_open_mode(&modeout[1], argv[optind+1], fmtout) < 0) {
+        if (fmtout) fprintf(pysamerr, "[bam_mating] cannot parse output format \"%s\"\n", fmtout);
+        else fprintf(pysamerr, "[bam_mating] cannot determine output format\n");
+        return 1;
+    }
+
+    // init
+    if ((in = sam_open(argv[optind], "r")) == NULL) {
+        fprintf(pysamerr, "[bam_mating] cannot open input file\n");
+        return 1;
+    }
+    if ((out = sam_open(argv[optind+1], modeout)) == NULL) {
+        fprintf(pysamerr, "[bam_mating] cannot open output file\n");
+        return 1;
+    }
+
+    // run
+    bam_mating_core(in, out, remove_reads, proper_pair_check, add_ct);
+    // cleanup
+    sam_close(in); sam_close(out);
+    return 0;
 }
 
 
diff --git a/samtools/bam_md.c.pysam.c b/samtools/bam_md.c.pysam.c
index e0cf67b..840d774 100644
--- a/samtools/bam_md.c.pysam.c
+++ b/samtools/bam_md.c.pysam.c
@@ -1,13 +1,37 @@
 #include "pysam.h"
 
+/*  bam_md.c -- calmd subcommand.
+
+    Copyright (C) 2009-2011 Genome Research Ltd.
+    Portions copyright (C) 2009-2011 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <unistd.h>
-#include <assert.h>
 #include <string.h>
 #include <ctype.h>
 #include <math.h>
-#include "faidx.h"
+#include "htslib/faidx.h"
 #include "sam.h"
-#include "kstring.h"
+#include "htslib/kstring.h"
 #include "kaln.h"
 #include "kprobaln.h"
 
@@ -18,374 +42,374 @@
 #define UPDATE_MD 16
 #define HASH_QNM  32
 
-char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 };
+const char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 };
 
 int bam_aux_drop_other(bam1_t *b, uint8_t *s);
 
 void bam_fillmd1_core(bam1_t *b, char *ref, int flag, int max_nm)
 {
-	uint8_t *seq = bam1_seq(b);
-	uint32_t *cigar = bam1_cigar(b);
-	bam1_core_t *c = &b->core;
-	int i, x, y, u = 0;
-	kstring_t *str;
-	int32_t old_nm_i = -1, nm = 0;
+    uint8_t *seq = bam1_seq(b);
+    uint32_t *cigar = bam1_cigar(b);
+    bam1_core_t *c = &b->core;
+    int i, x, y, u = 0;
+    kstring_t *str;
+    int32_t old_nm_i = -1, nm = 0;
+
+    str = (kstring_t*)calloc(1, sizeof(kstring_t));
+    for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
+        int j, l = cigar[i]>>4, op = cigar[i]&0xf;
+        if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+            for (j = 0; j < l; ++j) {
+                int z = y + j;
+                int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
+                if (ref[x+j] == 0) break; // out of boundary
+                if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
+                    if (flag&USE_EQUAL) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
+                    ++u;
+                } else {
+                    kputw(u, str); kputc(ref[x+j], str);
+                    u = 0; ++nm;
+                }
+            }
+            if (j < l) break;
+            x += l; y += l;
+        } else if (op == BAM_CDEL) {
+            kputw(u, str); kputc('^', str);
+            for (j = 0; j < l; ++j) {
+                if (ref[x+j] == 0) break;
+                kputc(ref[x+j], str);
+            }
+            u = 0;
+            if (j < l) break;
+            x += l; nm += l;
+        } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
+            y += l;
+            if (op == BAM_CINS) nm += l;
+        } else if (op == BAM_CREF_SKIP) {
+            x += l;
+        }
+    }
+    kputw(u, str);
+    // apply max_nm
+    if (max_nm > 0 && nm >= max_nm) {
+        for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
+            int j, l = cigar[i]>>4, op = cigar[i]&0xf;
+            if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+                for (j = 0; j < l; ++j) {
+                    int z = y + j;
+                    int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
+                    if (ref[x+j] == 0) break; // out of boundary
+                    if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
+                        seq[z/2] |= (z&1)? 0x0f : 0xf0;
+                        bam1_qual(b)[z] = 0;
+                    }
+                }
+                if (j < l) break;
+                x += l; y += l;
+            } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
+            else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
+        }
+    }
+    // update NM
+    if ((flag & UPDATE_NM) && !(c->flag & BAM_FUNMAP)) {
+        uint8_t *old_nm = bam_aux_get(b, "NM");
+        if (old_nm) old_nm_i = bam_aux2i(old_nm);
+        if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
+        else if (nm != old_nm_i) {
+            fprintf(pysamerr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm);
+            bam_aux_del(b, old_nm);
+            bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
+        }
+    }
+    // update MD
+    if ((flag & UPDATE_MD) && !(c->flag & BAM_FUNMAP)) {
+        uint8_t *old_md = bam_aux_get(b, "MD");
+        if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
+        else {
+            int is_diff = 0;
+            if (strlen((char*)old_md+1) == str->l) {
+                for (i = 0; i < str->l; ++i)
+                    if (toupper(old_md[i+1]) != toupper(str->s[i]))
+                        break;
+                if (i < str->l) is_diff = 1;
+            } else is_diff = 1;
+            if (is_diff) {
+                fprintf(pysamerr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
+                bam_aux_del(b, old_md);
+                bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
+            }
+        }
+    }
+
+    // drop all tags but RG
+    if (flag&DROP_TAG) {
+        uint8_t *q = bam_aux_get(b, "RG");
+        bam_aux_drop_other(b, q);
+    }
+    // reduce the resolution of base quality
+    if (flag&BIN_QUAL) {
+        uint8_t *qual = bam1_qual(b);
+        for (i = 0; i < b->core.l_qseq; ++i)
+            if (qual[i] >= 3) qual[i] = qual[i]/10*10 + 7;
+    }
 
-	str = (kstring_t*)calloc(1, sizeof(kstring_t));
-	for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
-		int j, l = cigar[i]>>4, op = cigar[i]&0xf;
-		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-			for (j = 0; j < l; ++j) {
-				int z = y + j;
-				int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
-				if (ref[x+j] == 0) break; // out of boundary
-				if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
-					if (flag&USE_EQUAL) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
-					++u;
-				} else {
-					kputw(u, str); kputc(ref[x+j], str);
-					u = 0; ++nm;
-				}
-			}
-			if (j < l) break;
-			x += l; y += l;
-		} else if (op == BAM_CDEL) {
-			kputw(u, str); kputc('^', str);
-			for (j = 0; j < l; ++j) {
-				if (ref[x+j] == 0) break;
-				kputc(ref[x+j], str);
-			}
-			u = 0;
-			if (j < l) break;
-			x += l; nm += l;
-		} else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
-			y += l;
-			if (op == BAM_CINS) nm += l;
-		} else if (op == BAM_CREF_SKIP) {
-			x += l;
-		}
-	}
-	kputw(u, str);
-	// apply max_nm
-	if (max_nm > 0 && nm >= max_nm) {
-		for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
-			int j, l = cigar[i]>>4, op = cigar[i]&0xf;
-			if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-				for (j = 0; j < l; ++j) {
-					int z = y + j;
-					int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
-					if (ref[x+j] == 0) break; // out of boundary
-					if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
-						seq[z/2] |= (z&1)? 0x0f : 0xf0;
-						bam1_qual(b)[z] = 0;
-					}
-				}
-				if (j < l) break;
-				x += l; y += l;
-			} else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
-			else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
-		}
-	}
-	// update NM
-	if (flag & UPDATE_NM) {
-		uint8_t *old_nm = bam_aux_get(b, "NM");
-		if (c->flag & BAM_FUNMAP) return;
-		if (old_nm) old_nm_i = bam_aux2i(old_nm);
-		if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
-		else if (nm != old_nm_i) {
-			fprintf(pysamerr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm);
-			bam_aux_del(b, old_nm);
-			bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
-		}
-	}
-	// update MD
-	if (flag & UPDATE_MD) {
-		uint8_t *old_md = bam_aux_get(b, "MD");
-		if (c->flag & BAM_FUNMAP) return;
-		if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
-		else {
-			int is_diff = 0;
-			if (strlen((char*)old_md+1) == str->l) {
-				for (i = 0; i < str->l; ++i)
-					if (toupper(old_md[i+1]) != toupper(str->s[i]))
-						break;
-				if (i < str->l) is_diff = 1;
-			} else is_diff = 1;
-			if (is_diff) {
-				fprintf(pysamerr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
-				bam_aux_del(b, old_md);
-				bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
-			}
-		}
-	}
-	// drop all tags but RG
-	if (flag&DROP_TAG) {
-		uint8_t *q = bam_aux_get(b, "RG");
-		bam_aux_drop_other(b, q);
-	}
-	// reduce the resolution of base quality
-	if (flag&BIN_QUAL) {
-		uint8_t *qual = bam1_qual(b);
-		for (i = 0; i < b->core.l_qseq; ++i)
-			if (qual[i] >= 3) qual[i] = qual[i]/10*10 + 7;
-	}
-	free(str->s); free(str);
+    free(str->s); free(str);
 }
 
 void bam_fillmd1(bam1_t *b, char *ref, int flag)
 {
-	bam_fillmd1_core(b, ref, flag, 0);
+    bam_fillmd1_core(b, ref, flag, 0);
 }
 
 int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
 {
-	uint8_t *seq = bam1_seq(b), *qual = bam1_qual(b);
-	uint32_t *cigar = bam1_cigar(b);
-	bam1_core_t *c = &b->core;
-	int i, x, y, mm, q, len, clip_l, clip_q;
-	double t;
-	if (thres < 0) thres = 40; // set the default
-	mm = q = len = clip_l = clip_q = 0;
-	for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
-		int j, l = cigar[i]>>4, op = cigar[i]&0xf;
-		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-			for (j = 0; j < l; ++j) {
-				int z = y + j;
-				int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
-				if (ref[x+j] == 0) break; // out of boundary
-				if (c2 != 15 && c1 != 15 && qual[z] >= 13) { // not ambiguous
-					++len;
-					if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch
-						++mm;
-						q += qual[z] > 33? 33 : qual[z];
-					}
-				}
-			}
-			if (j < l) break;
-			x += l; y += l; len += l;
-		} else if (op == BAM_CDEL) {
-			for (j = 0; j < l; ++j)
-				if (ref[x+j] == 0) break;
-			if (j < l) break;
-			x += l;
-		} else if (op == BAM_CSOFT_CLIP) {
-			for (j = 0; j < l; ++j) clip_q += qual[y+j];
-			clip_l += l;
-			y += l;
-		} else if (op == BAM_CHARD_CLIP) {
-			clip_q += 13 * l;
-			clip_l += l;
-		} else if (op == BAM_CINS) y += l;
-		else if (op == BAM_CREF_SKIP) x += l;
-	}
-	for (i = 0, t = 1; i < mm; ++i)
-		t *= (double)len / (i+1);
-	t = q - 4.343 * log(t) + clip_q / 5.;
-	if (t > thres) return -1;
-	if (t < 0) t = 0;
-	t = sqrt((thres - t) / thres) * thres;
-//	fprintf(pysamerr, "%s %lf %d\n", bam1_qname(b), t, q);
-	return (int)(t + .499);
+    uint8_t *seq = bam1_seq(b), *qual = bam1_qual(b);
+    uint32_t *cigar = bam1_cigar(b);
+    bam1_core_t *c = &b->core;
+    int i, x, y, mm, q, len, clip_l, clip_q;
+    double t;
+    if (thres < 0) thres = 40; // set the default
+    mm = q = len = clip_l = clip_q = 0;
+    for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
+        int j, l = cigar[i]>>4, op = cigar[i]&0xf;
+        if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+            for (j = 0; j < l; ++j) {
+                int z = y + j;
+                int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
+                if (ref[x+j] == 0) break; // out of boundary
+                if (c2 != 15 && c1 != 15 && qual[z] >= 13) { // not ambiguous
+                    ++len;
+                    if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch
+                        ++mm;
+                        q += qual[z] > 33? 33 : qual[z];
+                    }
+                }
+            }
+            if (j < l) break;
+            x += l; y += l; len += l;
+        } else if (op == BAM_CDEL) {
+            for (j = 0; j < l; ++j)
+                if (ref[x+j] == 0) break;
+            if (j < l) break;
+            x += l;
+        } else if (op == BAM_CSOFT_CLIP) {
+            for (j = 0; j < l; ++j) clip_q += qual[y+j];
+            clip_l += l;
+            y += l;
+        } else if (op == BAM_CHARD_CLIP) {
+            clip_q += 13 * l;
+            clip_l += l;
+        } else if (op == BAM_CINS) y += l;
+        else if (op == BAM_CREF_SKIP) x += l;
+    }
+    for (i = 0, t = 1; i < mm; ++i)
+        t *= (double)len / (i+1);
+    t = q - 4.343 * log(t) + clip_q / 5.;
+    if (t > thres) return -1;
+    if (t < 0) t = 0;
+    t = sqrt((thres - t) / thres) * thres;
+//  fprintf(pysamerr, "%s %lf %d\n", bam1_qname(b), t, q);
+    return (int)(t + .499);
 }
 
 int bam_prob_realn_core(bam1_t *b, const char *ref, int flag)
 {
-	int k, i, bw, x, y, yb, ye, xb, xe, apply_baq = flag&1, extend_baq = flag>>1&1, redo_baq = flag&4;
-	uint32_t *cigar = bam1_cigar(b);
-	bam1_core_t *c = &b->core;
-	kpa_par_t conf = kpa_par_def;
-	uint8_t *bq = 0, *zq = 0, *qual = bam1_qual(b);
-	if ((c->flag & BAM_FUNMAP) || b->core.l_qseq == 0) return -1; // do nothing
-	// test if BQ or ZQ is present
-	if ((bq = bam_aux_get(b, "BQ")) != 0) ++bq;
-	if ((zq = bam_aux_get(b, "ZQ")) != 0 && *zq == 'Z') ++zq;
-	if (bq && redo_baq)
-	{
-	    bam_aux_del(b, bq-1);
-	    bq = 0;
-	}
-	if (bq && zq) { // remove the ZQ tag
-		bam_aux_del(b, zq-1);
-		zq = 0;
-	}
-	if (bq || zq) {
-		if ((apply_baq && zq) || (!apply_baq && bq)) return -3; // in both cases, do nothing
-		if (bq && apply_baq) { // then convert BQ to ZQ
-			for (i = 0; i < c->l_qseq; ++i)
-				qual[i] = qual[i] + 64 < bq[i]? 0 : qual[i] - ((int)bq[i] - 64);
-			*(bq - 3) = 'Z';
-		} else if (zq && !apply_baq) { // then convert ZQ to BQ
-			for (i = 0; i < c->l_qseq; ++i)
-				qual[i] += (int)zq[i] - 64;
-			*(zq - 3) = 'B';
-		}
-		return 0;
-	}
-	// find the start and end of the alignment	
-	x = c->pos, y = 0, yb = ye = xb = xe = -1;
-	for (k = 0; k < c->n_cigar; ++k) {
-		int op, l;
-		op = cigar[k]&0xf; l = cigar[k]>>4;
-		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-			if (yb < 0) yb = y;
-			if (xb < 0) xb = x;
-			ye = y + l; xe = x + l;
-			x += l; y += l;
-		} else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
-		else if (op == BAM_CDEL) x += l;
-		else if (op == BAM_CREF_SKIP) return -1; // do nothing if there is a reference skip
-	}
-	// set bandwidth and the start and the end
-	bw = 7;
-	if (abs((xe - xb) - (ye - yb)) > bw)
-		bw = abs((xe - xb) - (ye - yb)) + 3;
-	conf.bw = bw;
-	xb -= yb + bw/2; if (xb < 0) xb = 0;
-	xe += c->l_qseq - ye + bw/2;
-	if (xe - xb - c->l_qseq > bw)
-		xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2;
-	{ // glocal
-		uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq;
-		int *state;
-		bq = calloc(c->l_qseq + 1, 1);
-		memcpy(bq, qual, c->l_qseq);
-		s = calloc(c->l_qseq, 1);
-		for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)];
-		r = calloc(xe - xb, 1);
-		for (i = xb; i < xe; ++i) {
-			if (ref[i] == 0) { xe = i; break; }
-			r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]];
-		}
-		state = calloc(c->l_qseq, sizeof(int));
-		q = calloc(c->l_qseq, 1);
-		kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q);
-		if (!extend_baq) { // in this block, bq[] is capped by base quality qual[]
-			for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
-				int op = cigar[k]&0xf, l = cigar[k]>>4;
-				if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-					for (i = y; i < y + l; ++i) {
-						if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) bq[i] = 0;
-						else bq[i] = bq[i] < q[i]? bq[i] : q[i];
-					}
-					x += l; y += l;
-				} else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
-				else if (op == BAM_CDEL) x += l;
-			}
-			for (i = 0; i < c->l_qseq; ++i) bq[i] = qual[i] - bq[i] + 64; // finalize BQ
-		} else { // in this block, bq[] is BAQ that can be larger than qual[] (different from the above!)
-			uint8_t *left, *rght;
-			left = calloc(c->l_qseq, 1); rght = calloc(c->l_qseq, 1);
-			for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
-				int op = cigar[k]&0xf, l = cigar[k]>>4;
-				if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-					for (i = y; i < y + l; ++i)
-						bq[i] = ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y))? 0 : q[i];
-					for (left[y] = bq[y], i = y + 1; i < y + l; ++i)
-						left[i] = bq[i] > left[i-1]? bq[i] : left[i-1];
-					for (rght[y+l-1] = bq[y+l-1], i = y + l - 2; i >= y; --i)
-						rght[i] = bq[i] > rght[i+1]? bq[i] : rght[i+1];
-					for (i = y; i < y + l; ++i)
-						bq[i] = left[i] < rght[i]? left[i] : rght[i];
-					x += l; y += l;
-				} else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
-				else if (op == BAM_CDEL) x += l;
-			}
-			for (i = 0; i < c->l_qseq; ++i) bq[i] = 64 + (qual[i] <= bq[i]? 0 : qual[i] - bq[i]); // finalize BQ
-			free(left); free(rght);
-		}
-		if (apply_baq) {
-			for (i = 0; i < c->l_qseq; ++i) qual[i] -= bq[i] - 64; // modify qual
-			bam_aux_append(b, "ZQ", 'Z', c->l_qseq + 1, bq);
-		} else bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq);
-		free(bq); free(s); free(r); free(q); free(state);
-	}
-	return 0;
+    int k, i, bw, x, y, yb, ye, xb, xe, apply_baq = flag&1, extend_baq = flag>>1&1, redo_baq = flag&4;
+    uint32_t *cigar = bam1_cigar(b);
+    bam1_core_t *c = &b->core;
+    kpa_par_t conf = kpa_par_def;
+    uint8_t *bq = 0, *zq = 0, *qual = bam1_qual(b);
+    if ((c->flag & BAM_FUNMAP) || b->core.l_qseq == 0) return -1; // do nothing
+    // test if BQ or ZQ is present
+    if ((bq = bam_aux_get(b, "BQ")) != 0) ++bq;
+    if ((zq = bam_aux_get(b, "ZQ")) != 0 && *zq == 'Z') ++zq;
+    if (bq && redo_baq)
+    {
+        bam_aux_del(b, bq-1);
+        bq = 0;
+    }
+    if (bq && zq) { // remove the ZQ tag
+        bam_aux_del(b, zq-1);
+        zq = 0;
+    }
+    if (bq || zq) {
+        if ((apply_baq && zq) || (!apply_baq && bq)) return -3; // in both cases, do nothing
+        if (bq && apply_baq) { // then convert BQ to ZQ
+            for (i = 0; i < c->l_qseq; ++i)
+                qual[i] = qual[i] + 64 < bq[i]? 0 : qual[i] - ((int)bq[i] - 64);
+            *(bq - 3) = 'Z';
+        } else if (zq && !apply_baq) { // then convert ZQ to BQ
+            for (i = 0; i < c->l_qseq; ++i)
+                qual[i] += (int)zq[i] - 64;
+            *(zq - 3) = 'B';
+        }
+        return 0;
+    }
+    // find the start and end of the alignment
+    x = c->pos, y = 0, yb = ye = xb = xe = -1;
+    for (k = 0; k < c->n_cigar; ++k) {
+        int op, l;
+        op = cigar[k]&0xf; l = cigar[k]>>4;
+        if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+            if (yb < 0) yb = y;
+            if (xb < 0) xb = x;
+            ye = y + l; xe = x + l;
+            x += l; y += l;
+        } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
+        else if (op == BAM_CDEL) x += l;
+        else if (op == BAM_CREF_SKIP) return -1; // do nothing if there is a reference skip
+    }
+    // set bandwidth and the start and the end
+    bw = 7;
+    if (abs((xe - xb) - (ye - yb)) > bw)
+        bw = abs((xe - xb) - (ye - yb)) + 3;
+    conf.bw = bw;
+    xb -= yb + bw/2; if (xb < 0) xb = 0;
+    xe += c->l_qseq - ye + bw/2;
+    if (xe - xb - c->l_qseq > bw)
+        xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2;
+    { // glocal
+        uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq;
+        int *state;
+        bq = calloc(c->l_qseq + 1, 1);
+        memcpy(bq, qual, c->l_qseq);
+        s = calloc(c->l_qseq, 1);
+        for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)];
+        r = calloc(xe - xb, 1);
+        for (i = xb; i < xe; ++i) {
+            if (ref[i] == 0) { xe = i; break; }
+            r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]];
+        }
+        state = calloc(c->l_qseq, sizeof(int));
+        q = calloc(c->l_qseq, 1);
+        kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q);
+        if (!extend_baq) { // in this block, bq[] is capped by base quality qual[]
+            for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
+                int op = cigar[k]&0xf, l = cigar[k]>>4;
+                if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+                    for (i = y; i < y + l; ++i) {
+                        if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) bq[i] = 0;
+                        else bq[i] = bq[i] < q[i]? bq[i] : q[i];
+                    }
+                    x += l; y += l;
+                } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
+                else if (op == BAM_CDEL) x += l;
+            }
+            for (i = 0; i < c->l_qseq; ++i) bq[i] = qual[i] - bq[i] + 64; // finalize BQ
+        } else { // in this block, bq[] is BAQ that can be larger than qual[] (different from the above!)
+            uint8_t *left, *rght;
+            left = calloc(c->l_qseq, 1); rght = calloc(c->l_qseq, 1);
+            for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
+                int op = cigar[k]&0xf, l = cigar[k]>>4;
+                if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+                    for (i = y; i < y + l; ++i)
+                        bq[i] = ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y))? 0 : q[i];
+                    for (left[y] = bq[y], i = y + 1; i < y + l; ++i)
+                        left[i] = bq[i] > left[i-1]? bq[i] : left[i-1];
+                    for (rght[y+l-1] = bq[y+l-1], i = y + l - 2; i >= y; --i)
+                        rght[i] = bq[i] > rght[i+1]? bq[i] : rght[i+1];
+                    for (i = y; i < y + l; ++i)
+                        bq[i] = left[i] < rght[i]? left[i] : rght[i];
+                    x += l; y += l;
+                } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
+                else if (op == BAM_CDEL) x += l;
+            }
+            for (i = 0; i < c->l_qseq; ++i) bq[i] = 64 + (qual[i] <= bq[i]? 0 : qual[i] - bq[i]); // finalize BQ
+            free(left); free(rght);
+        }
+        if (apply_baq) {
+            for (i = 0; i < c->l_qseq; ++i) qual[i] -= bq[i] - 64; // modify qual
+            bam_aux_append(b, "ZQ", 'Z', c->l_qseq + 1, bq);
+        } else bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq);
+        free(bq); free(s); free(r); free(q); free(state);
+    }
+    return 0;
 }
 
 int bam_prob_realn(bam1_t *b, const char *ref)
 {
-	return bam_prob_realn_core(b, ref, 1);
+    return bam_prob_realn_core(b, ref, 1);
 }
 
 int bam_fillmd(int argc, char *argv[])
 {
-	int c, flt_flag, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, baq_flag;
-	samfile_t *fp, *fpout = 0;
-	faidx_t *fai;
-	char *ref = 0, mode_w[8], mode_r[8];
-	bam1_t *b;
+    int c, flt_flag, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, baq_flag;
+    samfile_t *fp, *fpout = 0;
+    faidx_t *fai;
+    char *ref = 0, mode_w[8], mode_r[8];
+    bam1_t *b;
 
-	flt_flag = UPDATE_NM | UPDATE_MD;
-	is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = capQ = baq_flag = 0;
-	mode_w[0] = mode_r[0] = 0;
-	strcpy(mode_r, "r"); strcpy(mode_w, "w");
-	while ((c = getopt(argc, argv, "EqreuNhbSC:n:Ad")) >= 0) {
-		switch (c) {
-		case 'r': is_realn = 1; break;
-		case 'e': flt_flag |= USE_EQUAL; break;
-		case 'd': flt_flag |= DROP_TAG; break;
-		case 'q': flt_flag |= BIN_QUAL; break;
-		case 'h': flt_flag |= HASH_QNM; break;
-		case 'N': flt_flag &= ~(UPDATE_MD|UPDATE_NM); break;
-		case 'b': is_bam_out = 1; break;
-		case 'u': is_uncompressed = is_bam_out = 1; break;
-		case 'S': is_sam_in = 1; break;
-		case 'n': max_nm = atoi(optarg); break;
-		case 'C': capQ = atoi(optarg); break;
-		case 'A': baq_flag |= 1; break;
-		case 'E': baq_flag |= 2; break;
-		default: fprintf(pysamerr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
-		}
-	}
-	if (!is_sam_in) strcat(mode_r, "b");
-	if (is_bam_out) strcat(mode_w, "b");
-	else strcat(mode_w, "h");
-	if (is_uncompressed) strcat(mode_w, "u");
-	if (optind + 1 >= argc) {
-		fprintf(pysamerr, "\n");
-		fprintf(pysamerr, "Usage:   samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
-		fprintf(pysamerr, "Options: -e       change identical bases to '='\n");
-		fprintf(pysamerr, "         -u       uncompressed BAM output (for piping)\n");
-		fprintf(pysamerr, "         -b       compressed BAM output\n");
-		fprintf(pysamerr, "         -S       the input is SAM with header\n");
-		fprintf(pysamerr, "         -A       modify the quality string\n");
-		fprintf(pysamerr, "         -r       compute the BQ tag (without -A) or cap baseQ by BAQ (with -A)\n");
-		fprintf(pysamerr, "         -E       extended BAQ for better sensitivity but lower specificity\n\n");
-		return 1;
-	}
-	fp = samopen(argv[optind], mode_r, 0);
-	if (fp == 0) return 1;
-	if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
-		fprintf(pysamerr, "[bam_fillmd] input SAM does not have header. Abort!\n");
-		return 1;
-	}
-	fpout = samopen("-", mode_w, fp->header);
-	fai = fai_load(argv[optind+1]);
+    flt_flag = UPDATE_NM | UPDATE_MD;
+    is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = capQ = baq_flag = 0;
+    mode_w[0] = mode_r[0] = 0;
+    strcpy(mode_r, "r"); strcpy(mode_w, "w");
+    while ((c = getopt(argc, argv, "EqreuNhbSC:n:Ad")) >= 0) {
+        switch (c) {
+        case 'r': is_realn = 1; break;
+        case 'e': flt_flag |= USE_EQUAL; break;
+        case 'd': flt_flag |= DROP_TAG; break;
+        case 'q': flt_flag |= BIN_QUAL; break;
+        case 'h': flt_flag |= HASH_QNM; break;
+        case 'N': flt_flag &= ~(UPDATE_MD|UPDATE_NM); break;
+        case 'b': is_bam_out = 1; break;
+        case 'u': is_uncompressed = is_bam_out = 1; break;
+        case 'S': is_sam_in = 1; break;
+        case 'n': max_nm = atoi(optarg); break;
+        case 'C': capQ = atoi(optarg); break;
+        case 'A': baq_flag |= 1; break;
+        case 'E': baq_flag |= 2; break;
+        default: fprintf(pysamerr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
+        }
+    }
+    if (!is_sam_in) strcat(mode_r, "b");
+    if (is_bam_out) strcat(mode_w, "b");
+    else strcat(mode_w, "h");
+    if (is_uncompressed) strcat(mode_w, "u");
+    if (optind + 1 >= argc) {
+        fprintf(pysamerr, "\n");
+        fprintf(pysamerr, "Usage:   samtools calmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
+        fprintf(pysamerr, "Options: -e       change identical bases to '='\n");
+        fprintf(pysamerr, "         -u       uncompressed BAM output (for piping)\n");
+        fprintf(pysamerr, "         -b       compressed BAM output\n");
+        fprintf(pysamerr, "         -S       the input is SAM with header\n");
+        fprintf(pysamerr, "         -A       modify the quality string\n");
+        fprintf(pysamerr, "         -r       compute the BQ tag (without -A) or cap baseQ by BAQ (with -A)\n");
+        fprintf(pysamerr, "         -E       extended BAQ for better sensitivity but lower specificity\n\n");
+        return 1;
+    }
+    fp = samopen(argv[optind], mode_r, 0);
+    if (fp == 0) return 1;
+    if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
+        fprintf(pysamerr, "[bam_fillmd] input SAM does not have header. Abort!\n");
+        return 1;
+    }
+    fpout = samopen("-", mode_w, fp->header);
+    fai = fai_load(argv[optind+1]);
 
-	b = bam_init1();
-	while ((ret = samread(fp, b)) >= 0) {
-		if (b->core.tid >= 0) {
-			if (tid != b->core.tid) {
-				free(ref);
-				ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
-				tid = b->core.tid;
-				if (ref == 0)
-					fprintf(pysamerr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
-							fp->header->target_name[tid]);
-			}
-			if (is_realn) bam_prob_realn_core(b, ref, baq_flag);
-			if (capQ > 10) {
-				int q = bam_cap_mapQ(b, ref, capQ);
-				if (b->core.qual > q) b->core.qual = q;
-			}
-			if (ref) bam_fillmd1_core(b, ref, flt_flag, max_nm);
-		}
-		samwrite(fpout, b);
-	}
-	bam_destroy1(b);
+    b = bam_init1();
+    while ((ret = samread(fp, b)) >= 0) {
+        if (b->core.tid >= 0) {
+            if (tid != b->core.tid) {
+                free(ref);
+                ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
+                tid = b->core.tid;
+                if (ref == 0)
+                    fprintf(pysamerr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
+                            fp->header->target_name[tid]);
+            }
+            if (is_realn) bam_prob_realn_core(b, ref, baq_flag);
+            if (capQ > 10) {
+                int q = bam_cap_mapQ(b, ref, capQ);
+                if (b->core.qual > q) b->core.qual = q;
+            }
+            if (ref) bam_fillmd1_core(b, ref, flt_flag, max_nm);
+        }
+        samwrite(fpout, b);
+    }
+    bam_destroy1(b);
 
-	free(ref);
-	fai_destroy(fai);
-	samclose(fp); samclose(fpout);
-	return 0;
+    free(ref);
+    fai_destroy(fai);
+    samclose(fp); samclose(fpout);
+    return 0;
 }
diff --git a/samtools/bam_pileup.c.pysam.c b/samtools/bam_pileup.c.pysam.c
deleted file mode 100644
index 3d9a3b7..0000000
--- a/samtools/bam_pileup.c.pysam.c
+++ /dev/null
@@ -1,439 +0,0 @@
-#include "pysam.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <assert.h>
-#include "sam.h"
-
-typedef struct {
-	int k, x, y, end;
-} cstate_t;
-
-static cstate_t g_cstate_null = { -1, 0, 0, 0 };
-
-typedef struct __linkbuf_t {
-	bam1_t b;
-	uint32_t beg, end;
-	cstate_t s;
-	struct __linkbuf_t *next;
-} lbnode_t;
-
-/* --- BEGIN: Memory pool */
-
-typedef struct {
-	int cnt, n, max;
-	lbnode_t **buf;
-} mempool_t;
-
-static mempool_t *mp_init(void)
-{
-	mempool_t *mp;
-	mp = (mempool_t*)calloc(1, sizeof(mempool_t));
-	return mp;
-}
-static void mp_destroy(mempool_t *mp)
-{
-	int k;
-	for (k = 0; k < mp->n; ++k) {
-		free(mp->buf[k]->b.data);
-		free(mp->buf[k]);
-	}
-	free(mp->buf);
-	free(mp);
-}
-static inline lbnode_t *mp_alloc(mempool_t *mp)
-{
-	++mp->cnt;
-	if (mp->n == 0) return (lbnode_t*)calloc(1, sizeof(lbnode_t));
-	else return mp->buf[--mp->n];
-}
-static inline void mp_free(mempool_t *mp, lbnode_t *p)
-{
-	--mp->cnt; p->next = 0; // clear lbnode_t::next here
-	if (mp->n == mp->max) {
-		mp->max = mp->max? mp->max<<1 : 256;
-		mp->buf = (lbnode_t**)realloc(mp->buf, sizeof(lbnode_t*) * mp->max);
-	}
-	mp->buf[mp->n++] = p;
-}
-
-/* --- END: Memory pool */
-
-/* --- BEGIN: Auxiliary functions */
-
-/* s->k: the index of the CIGAR operator that has just been processed.
-   s->x: the reference coordinate of the start of s->k
-   s->y: the query coordiante of the start of s->k
- */
-static inline int resolve_cigar2(bam_pileup1_t *p, uint32_t pos, cstate_t *s)
-{
-#define _cop(c) ((c)&BAM_CIGAR_MASK)
-#define _cln(c) ((c)>>BAM_CIGAR_SHIFT)
-
-	bam1_t *b = p->b;
-	bam1_core_t *c = &b->core;
-	uint32_t *cigar = bam1_cigar(b);
-	int k, is_head = 0;
-	// determine the current CIGAR operation
-//	fprintf(pysamerr, "%s\tpos=%d\tend=%d\t(%d,%d,%d)\n", bam1_qname(b), pos, s->end, s->k, s->x, s->y);
-	if (s->k == -1) { // never processed
-		is_head = 1;
-		if (c->n_cigar == 1) { // just one operation, save a loop
-		  if (_cop(cigar[0]) == BAM_CMATCH || _cop(cigar[0]) == BAM_CEQUAL || _cop(cigar[0]) == BAM_CDIFF) s->k = 0, s->x = c->pos, s->y = 0;
-		} else { // find the first match or deletion
-			for (k = 0, s->x = c->pos, s->y = 0; k < c->n_cigar; ++k) {
-				int op = _cop(cigar[k]);
-				int l = _cln(cigar[k]);
-				if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CEQUAL || op == BAM_CDIFF) break;
-				else if (op == BAM_CREF_SKIP) s->x += l;
-				else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) s->y += l;
-			}
-			assert(k < c->n_cigar);
-			s->k = k;
-		}
-	} else { // the read has been processed before
-		int op, l = _cln(cigar[s->k]);
-		if (pos - s->x >= l) { // jump to the next operation
-			assert(s->k < c->n_cigar); // otherwise a bug: this function should not be called in this case
-			op = _cop(cigar[s->k+1]);
-			if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP || op == BAM_CEQUAL || op == BAM_CDIFF) { // jump to the next without a loop
-			  if (_cop(cigar[s->k]) == BAM_CMATCH|| _cop(cigar[s->k]) == BAM_CEQUAL || _cop(cigar[s->k]) == BAM_CDIFF) s->y += l;
-				s->x += l;
-				++s->k;
-			} else { // find the next M/D/N/=/X
-			  if (_cop(cigar[s->k]) == BAM_CMATCH|| _cop(cigar[s->k]) == BAM_CEQUAL || _cop(cigar[s->k]) == BAM_CDIFF) s->y += l;
-				s->x += l;
-				for (k = s->k + 1; k < c->n_cigar; ++k) {
-					op = _cop(cigar[k]), l = _cln(cigar[k]);
-					if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP || op == BAM_CEQUAL || op == BAM_CDIFF) break;
-					else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) s->y += l;
-				}
-				s->k = k;
-			}
-			assert(s->k < c->n_cigar); // otherwise a bug
-		} // else, do nothing
-	}
-	{ // collect pileup information
-		int op, l;
-		op = _cop(cigar[s->k]); l = _cln(cigar[s->k]);
-		p->is_del = p->indel = p->is_refskip = 0;
-		if (s->x + l - 1 == pos && s->k + 1 < c->n_cigar) { // peek the next operation
-			int op2 = _cop(cigar[s->k+1]);
-			int l2 = _cln(cigar[s->k+1]);
-			if (op2 == BAM_CDEL) p->indel = -(int)l2;
-			else if (op2 == BAM_CINS) p->indel = l2;
-			else if (op2 == BAM_CPAD && s->k + 2 < c->n_cigar) { // no working for adjacent padding
-				int l3 = 0;
-				for (k = s->k + 2; k < c->n_cigar; ++k) {
-					op2 = _cop(cigar[k]); l2 = _cln(cigar[k]);
-					if (op2 == BAM_CINS) l3 += l2;
-					else if (op2 == BAM_CDEL || op2 == BAM_CMATCH || op2 == BAM_CREF_SKIP || op2 == BAM_CEQUAL || op2 == BAM_CDIFF) break;
-				}
-				if (l3 > 0) p->indel = l3;
-			}
-		}
-		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-			p->qpos = s->y + (pos - s->x);
-		} else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
-			p->is_del = 1; p->qpos = s->y; // FIXME: distinguish D and N!!!!!
-			p->is_refskip = (op == BAM_CREF_SKIP);
-		} // cannot be other operations; otherwise a bug
-		p->is_head = (pos == c->pos); p->is_tail = (pos == s->end);
-	}
-	return 1;
-}
-
-/* --- END: Auxiliary functions */
-
-/*******************
- * pileup iterator *
- *******************/
-
-struct __bam_plp_t {
-	mempool_t *mp;
-	lbnode_t *head, *tail, *dummy;
-	int32_t tid, pos, max_tid, max_pos;
-	int is_eof, flag_mask, max_plp, error, maxcnt;
-	bam_pileup1_t *plp;
-	// for the "auto" interface only
-	bam1_t *b;
-	bam_plp_auto_f func;
-	void *data;
-};
-
-bam_plp_t bam_plp_init(bam_plp_auto_f func, void *data)
-{
-	bam_plp_t iter;
-	iter = calloc(1, sizeof(struct __bam_plp_t));
-	iter->mp = mp_init();
-	iter->head = iter->tail = mp_alloc(iter->mp);
-	iter->dummy = mp_alloc(iter->mp);
-	iter->max_tid = iter->max_pos = -1;
-	iter->flag_mask = BAM_DEF_MASK;
-	iter->maxcnt = 8000;
-	if (func) {
-		iter->func = func;
-		iter->data = data;
-		iter->b = bam_init1();
-	}
-	return iter;
-}
-
-void bam_plp_destroy(bam_plp_t iter)
-{
-	mp_free(iter->mp, iter->dummy);
-	mp_free(iter->mp, iter->head);
-	if (iter->mp->cnt != 0)
-		fprintf(pysamerr, "[bam_plp_destroy] memory leak: %d. Continue anyway.\n", iter->mp->cnt);
-	mp_destroy(iter->mp);
-	if (iter->b) bam_destroy1(iter->b);
-	free(iter->plp);
-	free(iter);
-}
-
-const bam_pileup1_t *bam_plp_next(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp)
-{
-	if (iter->error) { *_n_plp = -1; return 0; }
-	*_n_plp = 0;
-	if (iter->is_eof && iter->head->next == 0) return 0;
-	while (iter->is_eof || iter->max_tid > iter->tid || (iter->max_tid == iter->tid && iter->max_pos > iter->pos)) {
-		int n_plp = 0;
-		lbnode_t *p, *q;
-		// write iter->plp at iter->pos
-		iter->dummy->next = iter->head;
-		for (p = iter->head, q = iter->dummy; p->next; q = p, p = p->next) {
-			if (p->b.core.tid < iter->tid || (p->b.core.tid == iter->tid && p->end <= iter->pos)) { // then remove
-				q->next = p->next; mp_free(iter->mp, p); p = q;
-			} else if (p->b.core.tid == iter->tid && p->beg <= iter->pos) { // here: p->end > pos; then add to pileup
-				if (n_plp == iter->max_plp) { // then double the capacity
-					iter->max_plp = iter->max_plp? iter->max_plp<<1 : 256;
-					iter->plp = (bam_pileup1_t*)realloc(iter->plp, sizeof(bam_pileup1_t) * iter->max_plp);
-				}
-				iter->plp[n_plp].b = &p->b;
-				if (resolve_cigar2(iter->plp + n_plp, iter->pos, &p->s)) ++n_plp; // actually always true...
-			}
-		}
-		iter->head = iter->dummy->next; // dummy->next may be changed
-		*_n_plp = n_plp; *_tid = iter->tid; *_pos = iter->pos;
-		// update iter->tid and iter->pos
-		if (iter->head->next) {
-			if (iter->tid > iter->head->b.core.tid) {
-				fprintf(pysamerr, "[%s] unsorted input. Pileup aborts.\n", __func__);
-				iter->error = 1;
-				*_n_plp = -1;
-				return 0;
-			}
-		}
-		if (iter->tid < iter->head->b.core.tid) { // come to a new reference sequence
-			iter->tid = iter->head->b.core.tid; iter->pos = iter->head->beg; // jump to the next reference
-		} else if (iter->pos < iter->head->beg) { // here: tid == head->b.core.tid
-			iter->pos = iter->head->beg; // jump to the next position
-		} else ++iter->pos; // scan contiguously
-		// return
-		if (n_plp) return iter->plp;
-		if (iter->is_eof && iter->head->next == 0) break;
-	}
-	return 0;
-}
-
-int bam_plp_push(bam_plp_t iter, const bam1_t *b)
-{
-	if (iter->error) return -1;
-	if (b) {
-		if (b->core.tid < 0) return 0;
-		if (b->core.flag & iter->flag_mask) return 0;
-		if (iter->tid == b->core.tid && iter->pos == b->core.pos && iter->mp->cnt > iter->maxcnt) return 0;
-		bam_copy1(&iter->tail->b, b);
-		iter->tail->beg = b->core.pos; iter->tail->end = bam_calend(&b->core, bam1_cigar(b));
-		iter->tail->s = g_cstate_null; iter->tail->s.end = iter->tail->end - 1; // initialize cstate_t
-		if (b->core.tid < iter->max_tid) {
-			fprintf(pysamerr, "[bam_pileup_core] the input is not sorted (chromosomes out of order)\n");
-			iter->error = 1;
-			return -1;
-		}
-		if ((b->core.tid == iter->max_tid) && (iter->tail->beg < iter->max_pos)) {
-			fprintf(pysamerr, "[bam_pileup_core] the input is not sorted (reads out of order)\n");
-			iter->error = 1;
-			return -1;
-		}
-		iter->max_tid = b->core.tid; iter->max_pos = iter->tail->beg;
-		if (iter->tail->end > iter->pos || iter->tail->b.core.tid > iter->tid) {
-			iter->tail->next = mp_alloc(iter->mp);
-			iter->tail = iter->tail->next;
-		}
-	} else iter->is_eof = 1;
-	return 0;
-}
-
-const bam_pileup1_t *bam_plp_auto(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp)
-{
-	const bam_pileup1_t *plp;
-	if (iter->func == 0 || iter->error) { *_n_plp = -1; return 0; }
-	if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp;
-	else { // no pileup line can be obtained; read alignments
-		*_n_plp = 0;
-		if (iter->is_eof) return 0;
-		while (iter->func(iter->data, iter->b) >= 0) {
-			if (bam_plp_push(iter, iter->b) < 0) {
-				*_n_plp = -1;
-				return 0;
-			}
-			if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp;
-			// otherwise no pileup line can be returned; read the next alignment.
-		}
-		bam_plp_push(iter, 0);
-		if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp;
-		return 0;
-	}
-}
-
-void bam_plp_reset(bam_plp_t iter)
-{
-	lbnode_t *p, *q;
-	iter->max_tid = iter->max_pos = -1;
-	iter->tid = iter->pos = 0;
-	iter->is_eof = 0;
-	for (p = iter->head; p->next;) {
-		q = p->next;
-		mp_free(iter->mp, p);
-		p = q;
-	}
-	iter->head = iter->tail;
-}
-
-void bam_plp_set_mask(bam_plp_t iter, int mask)
-{
-	iter->flag_mask = mask < 0? BAM_DEF_MASK : (BAM_FUNMAP | mask);
-}
-
-void bam_plp_set_maxcnt(bam_plp_t iter, int maxcnt)
-{
-	iter->maxcnt = maxcnt;
-}
-
-/*****************
- * callback APIs *
- *****************/
-
-int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data)
-{
-	bam_plbuf_t *buf;
-	int ret;
-	bam1_t *b;
-	b = bam_init1();
-	buf = bam_plbuf_init(func, func_data);
-	bam_plbuf_set_mask(buf, mask);
-	while ((ret = bam_read1(fp, b)) >= 0)
-		bam_plbuf_push(b, buf);
-	bam_plbuf_push(0, buf);
-	bam_plbuf_destroy(buf);
-	bam_destroy1(b);
-	return 0;
-}
-
-void bam_plbuf_set_mask(bam_plbuf_t *buf, int mask)
-{
-	bam_plp_set_mask(buf->iter, mask);
-}
-
-void bam_plbuf_reset(bam_plbuf_t *buf)
-{
-	bam_plp_reset(buf->iter);
-}
-
-bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data)
-{
-	bam_plbuf_t *buf;
-	buf = calloc(1, sizeof(bam_plbuf_t));
-	buf->iter = bam_plp_init(0, 0);
-	buf->func = func;
-	buf->data = data;
-	return buf;
-}
-
-void bam_plbuf_destroy(bam_plbuf_t *buf)
-{
-	bam_plp_destroy(buf->iter);
-	free(buf);
-}
-
-int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
-{
-	int ret, n_plp, tid, pos;
-	const bam_pileup1_t *plp;
-	ret = bam_plp_push(buf->iter, b);
-	if (ret < 0) return ret;
-	while ((plp = bam_plp_next(buf->iter, &tid, &pos, &n_plp)) != 0)
-		buf->func(tid, pos, n_plp, plp, buf->data);
-	return 0;
-}
-
-/***********
- * mpileup *
- ***********/
-
-struct __bam_mplp_t {
-	int n;
-	uint64_t min, *pos;
-	bam_plp_t *iter;
-	int *n_plp;
-	const bam_pileup1_t **plp;
-};
-
-bam_mplp_t bam_mplp_init(int n, bam_plp_auto_f func, void **data)
-{
-	int i;
-	bam_mplp_t iter;
-	iter = calloc(1, sizeof(struct __bam_mplp_t));
-	iter->pos = calloc(n, 8);
-	iter->n_plp = calloc(n, sizeof(int));
-	iter->plp = calloc(n, sizeof(void*));
-	iter->iter = calloc(n, sizeof(void*));
-	iter->n = n;
-	iter->min = (uint64_t)-1;
-	for (i = 0; i < n; ++i) {
-		iter->iter[i] = bam_plp_init(func, data[i]);
-		iter->pos[i] = iter->min;
-	}
-	return iter;
-}
-
-void bam_mplp_set_maxcnt(bam_mplp_t iter, int maxcnt)
-{
-	int i;
-	for (i = 0; i < iter->n; ++i)
-		iter->iter[i]->maxcnt = maxcnt;
-}
-
-void bam_mplp_destroy(bam_mplp_t iter)
-{
-	int i;
-	for (i = 0; i < iter->n; ++i) bam_plp_destroy(iter->iter[i]);
-	free(iter->iter); free(iter->pos); free(iter->n_plp); free(iter->plp);
-	free(iter);
-}
-
-int bam_mplp_auto(bam_mplp_t iter, int *_tid, int *_pos, int *n_plp, const bam_pileup1_t **plp)
-{
-	int i, ret = 0;
-	uint64_t new_min = (uint64_t)-1;
-	for (i = 0; i < iter->n; ++i) {
-		if (iter->pos[i] == iter->min) {
-			int tid, pos;
-			iter->plp[i] = bam_plp_auto(iter->iter[i], &tid, &pos, &iter->n_plp[i]);
-			iter->pos[i] = (uint64_t)tid<<32 | pos;
-		}
-		if (iter->plp[i] && iter->pos[i] < new_min) new_min = iter->pos[i];
-	}
-	iter->min = new_min;
-	if (new_min == (uint64_t)-1) return 0;
-	*_tid = new_min>>32; *_pos = (uint32_t)new_min;
-	for (i = 0; i < iter->n; ++i) {
-		if (iter->pos[i] == iter->min) { // FIXME: valgrind reports "uninitialised value(s) at this line"
-			n_plp[i] = iter->n_plp[i], plp[i] = iter->plp[i];
-			++ret;
-		} else n_plp[i] = 0, plp[i] = 0;
-	}
-	return ret;
-}
diff --git a/samtools/bam_plbuf.c.pysam.c b/samtools/bam_plbuf.c.pysam.c
new file mode 100644
index 0000000..5b8dda0
--- /dev/null
+++ b/samtools/bam_plbuf.c.pysam.c
@@ -0,0 +1,68 @@
+#include "pysam.h"
+
+/*  bam_plbuf.c -- plbuf routines (previously in bam_pileup.c).
+
+    Copyright (C) 2008-2010, 2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <htslib/hts.h>
+#include <htslib/sam.h>
+#include "bam_plbuf.h"
+
+/*****************
+ * callback APIs *
+ *****************/
+
+void bam_plbuf_reset(bam_plbuf_t *buf)
+{
+    bam_plp_reset(buf->iter);
+}
+
+bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data)
+{
+    bam_plbuf_t *buf;
+    buf = calloc(1, sizeof(bam_plbuf_t));
+    buf->iter = bam_plp_init(0, 0);
+    buf->func = func;
+    buf->data = data;
+    return buf;
+}
+
+void bam_plbuf_destroy(bam_plbuf_t *buf)
+{
+    bam_plp_destroy(buf->iter);
+    free(buf);
+}
+
+int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
+{
+    int ret, n_plp, tid, pos;
+    const bam_pileup1_t *plp;
+    ret = bam_plp_push(buf->iter, b);
+    if (ret < 0) return ret;
+    while ((plp = bam_plp_next(buf->iter, &tid, &pos, &n_plp)) != 0)
+        buf->func(tid, pos, n_plp, plp, buf->data);
+    return 0;
+}
diff --git a/htslib/htslib/hts_defs.h b/samtools/bam_plbuf.h
similarity index 52%
copy from htslib/htslib/hts_defs.h
copy to samtools/bam_plbuf.h
index efc4f6c..1534551 100644
--- a/htslib/htslib/hts_defs.h
+++ b/samtools/bam_plbuf.h
@@ -1,8 +1,8 @@
-/*  hts_defs.h -- Miscellaneous definitions.
+/*  bam_plbuf.h -- plbuf routines (declarations copied from bam.h).
 
-    Copyright (C) 2013-2014 Genome Research Ltd.
+    Copyright (C) 2008, 2013 Genome Research Ltd.
 
-    Author: John Marshall <jm18 at sanger.ac.uk>
+    Author: Heng Li <lh3 at sanger.ac.uk>
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
@@ -11,7 +11,7 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
-The above copyright notices and this permission notice shall be included in
+The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -22,26 +22,34 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 DEALINGS IN THE SOFTWARE.  */
 
-#ifndef HTSLIB_HTS_DEFS_H
-#define HTSLIB_HTS_DEFS_H
+#ifndef BAM_PLBUF_H
+#define BAM_PLBUF_H
 
-#if __clang__major__ >= 2 || __GNUC__ >= 3
-#define HTS_NORETURN __attribute__ ((__noreturn__))
-#else
-#define HTS_NORETURN
-#endif
+#include <htslib/sam.h>
 
-#if (defined __clang__ && __clang_major__ >= 3) || \
-    (defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__==4 && __GNUC_MINOR__ >= 5)))
-#define HTS_RESULT_USED __attribute__ ((__warn_unused_result__))
-#else
-#define HTS_RESULT_USED
-#endif
+#ifndef BAM_PILEUP_F_DEFINED
+#define BAM_PILEUP_F_DEFINED
+typedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data);
+#endif //BAM_PILEUP_F_DEFINED
+
+typedef struct {
+    bam_plp_t iter;
+    bam_pileup_f func;
+    void *data;
+} bam_plbuf_t;
 
-#if defined __clang__ || defined __GNUC__
-#define HTS_UNUSED __attribute__ ((__unused__))
-#else
-#define HTS_UNUSED
+#ifdef __cplusplus
+extern "C" {
 #endif
+    void bam_plbuf_reset(bam_plbuf_t *buf);
 
+    bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data);
+
+    void bam_plbuf_destroy(bam_plbuf_t *buf);
+
+    int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf);
+#ifdef __cplusplus
+}
 #endif
+
+#endif // BAM_PLBUF_H
diff --git a/samtools/bam_plcmd.c.pysam.c b/samtools/bam_plcmd.c.pysam.c
index 79917c9..2151a1b 100644
--- a/samtools/bam_plcmd.c.pysam.c
+++ b/samtools/bam_plcmd.c.pysam.c
@@ -1,5 +1,30 @@
 #include "pysam.h"
 
+/*  bam_plcmd.c -- mpileup subcommand.
+
+    Copyright (C) 2008-2014 Genome Research Ltd.
+    Portions copyright (C) 2009-2012 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <math.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -8,415 +33,526 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include <getopt.h>
-#include "sam.h"
-#include "faidx.h"
-#include "kstring.h"
+#include <htslib/sam.h>
+#include <htslib/faidx.h>
+#include <htslib/kstring.h>
+#include <htslib/khash_str2int.h>
 #include "sam_header.h"
+#include "samtools.h"
 
 static inline int printw(int c, FILE *fp)
 {
-	char buf[16];
-	int l, x;
-	if (c == 0) return fputc('0', fp);
-	for (l = 0, x = c < 0? -c : c; x > 0; x /= 10) buf[l++] = x%10 + '0';
-	if (c < 0) buf[l++] = '-';
-	buf[l] = 0;
-	for (x = 0; x < l/2; ++x) {
-		int y = buf[x]; buf[x] = buf[l-1-x]; buf[l-1-x] = y;
-	}
-	fputs(buf, fp);
-	return 0;
+    char buf[16];
+    int l, x;
+    if (c == 0) return fputc('0', fp);
+    for (l = 0, x = c < 0? -c : c; x > 0; x /= 10) buf[l++] = x%10 + '0';
+    if (c < 0) buf[l++] = '-';
+    buf[l] = 0;
+    for (x = 0; x < l/2; ++x) {
+        int y = buf[x]; buf[x] = buf[l-1-x]; buf[l-1-x] = y;
+    }
+    fputs(buf, fp);
+    return 0;
 }
 
-static inline void pileup_seq(const bam_pileup1_t *p, int pos, int ref_len, const char *ref)
+static inline void pileup_seq(FILE *fp, const bam_pileup1_t *p, int pos, int ref_len, const char *ref)
 {
-	int j;
-	if (p->is_head) {
-		putchar('^');
-		putchar(p->b->core.qual > 93? 126 : p->b->core.qual + 33);
-	}
-	if (!p->is_del) {
-		int c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
-		if (ref) {
-			int rb = pos < ref_len? ref[pos] : 'N';
-			if (c == '=' || bam_nt16_table[c] == bam_nt16_table[rb]) c = bam1_strand(p->b)? ',' : '.';
-			else c = bam1_strand(p->b)? tolower(c) : toupper(c);
-		} else {
-			if (c == '=') c = bam1_strand(p->b)? ',' : '.';
-			else c = bam1_strand(p->b)? tolower(c) : toupper(c);
-		}
-		putchar(c);
-	} else putchar(p->is_refskip? (bam1_strand(p->b)? '<' : '>') : '*');
-	if (p->indel > 0) {
-		putchar('+'); printw(p->indel, stdout);
-		for (j = 1; j <= p->indel; ++j) {
-			int c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
-			putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
-		}
-	} else if (p->indel < 0) {
-		printw(p->indel, stdout);
-		for (j = 1; j <= -p->indel; ++j) {
-			int c = (ref && (int)pos+j < ref_len)? ref[pos+j] : 'N';
-			putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
-		}
-	}
-	if (p->is_tail) putchar('$');
+    int j;
+    if (p->is_head) {
+        putc('^', fp);
+        putc(p->b->core.qual > 93? 126 : p->b->core.qual + 33, fp);
+    }
+    if (!p->is_del) {
+        int c = seq_nt16_str[bam_seqi(bam_get_seq(p->b), p->qpos)];
+        if (ref) {
+            int rb = pos < ref_len? ref[pos] : 'N';
+            if (c == '=' || seq_nt16_table[c] == seq_nt16_table[rb]) c = bam_is_rev(p->b)? ',' : '.';
+            else c = bam_is_rev(p->b)? tolower(c) : toupper(c);
+        } else {
+            if (c == '=') c = bam_is_rev(p->b)? ',' : '.';
+            else c = bam_is_rev(p->b)? tolower(c) : toupper(c);
+        }
+        putc(c, fp);
+    } else putc(p->is_refskip? (bam_is_rev(p->b)? '<' : '>') : '*', fp);
+    if (p->indel > 0) {
+        putc('+', fp); printw(p->indel, fp);
+        for (j = 1; j <= p->indel; ++j) {
+            int c = seq_nt16_str[bam_seqi(bam_get_seq(p->b), p->qpos + j)];
+            putc(bam_is_rev(p->b)? tolower(c) : toupper(c), fp);
+        }
+    } else if (p->indel < 0) {
+        printw(p->indel, fp);
+        for (j = 1; j <= -p->indel; ++j) {
+            int c = (ref && (int)pos+j < ref_len)? ref[pos+j] : 'N';
+            putc(bam_is_rev(p->b)? tolower(c) : toupper(c), fp);
+        }
+    }
+    if (p->is_tail) putc('$', fp);
 }
 
 #include <assert.h>
 #include "bam2bcf.h"
 #include "sample.h"
 
-#define MPLP_GLF   0x10
-#define MPLP_NO_COMP 0x20
-#define MPLP_NO_ORPHAN 0x40
-#define MPLP_REALN   0x80
-#define MPLP_NO_INDEL 0x400
-#define MPLP_REDO_BAQ 0x800
-#define MPLP_ILLUMINA13 0x1000
-#define MPLP_IGNORE_RG 0x2000
-#define MPLP_PRINT_POS 0x4000
-#define MPLP_PRINT_MAPQ 0x8000
-#define MPLP_PER_SAMPLE 0x10000
+#define MPLP_BCF        1
+#define MPLP_VCF        (1<<1)
+#define MPLP_NO_COMP    (1<<2)
+#define MPLP_NO_ORPHAN  (1<<3)
+#define MPLP_REALN      (1<<4)
+#define MPLP_NO_INDEL   (1<<5)
+#define MPLP_REDO_BAQ   (1<<6)
+#define MPLP_ILLUMINA13 (1<<7)
+#define MPLP_IGNORE_RG  (1<<8)
+#define MPLP_PRINT_POS  (1<<9)
+#define MPLP_PRINT_MAPQ (1<<10)
+#define MPLP_PER_SAMPLE (1<<11)
+#define MPLP_SMART_OVERLAPS (1<<12)
 
 void *bed_read(const char *fn);
 void bed_destroy(void *_h);
 int bed_overlap(const void *_h, const char *chr, int beg, int end);
 
 typedef struct {
-	int max_mq, min_mq, flag, min_baseQ, capQ_thres, max_depth, max_indel_depth, fmt_flag;
+    int min_mq, flag, min_baseQ, capQ_thres, max_depth, max_indel_depth, fmt_flag;
     int rflag_require, rflag_filter;
-	int openQ, extQ, tandemQ, min_support; // for indels
-	double min_frac; // for indels
-	char *reg, *pl_list, *fai_fname;
-	faidx_t *fai;
-	void *bed, *rghash;
+    int openQ, extQ, tandemQ, min_support; // for indels
+    double min_frac; // for indels
+    char *reg, *pl_list, *fai_fname, *output_fname;
+    faidx_t *fai;
+    void *bed, *rghash;
+    int argc;
+    char **argv;
 } mplp_conf_t;
 
 typedef struct {
-	bamFile fp;
-	bam_iter_t iter;
-	bam_header_t *h;
-	int ref_id;
-	char *ref;
-	const mplp_conf_t *conf;
+    samFile *fp;
+    hts_itr_t *iter;
+    bam_hdr_t *h;
+    int ref_id;
+    char *ref;
+    const mplp_conf_t *conf;
 } mplp_aux_t;
 
 typedef struct {
-	int n;
-	int *n_plp, *m_plp;
-	bam_pileup1_t **plp;
+    int n;
+    int *n_plp, *m_plp;
+    bam_pileup1_t **plp;
 } mplp_pileup_t;
 
 static int mplp_func(void *data, bam1_t *b)
 {
-	extern int bam_realn(bam1_t *b, const char *ref);
-	extern int bam_prob_realn_core(bam1_t *b, const char *ref, int);
-	extern int bam_cap_mapQ(bam1_t *b, char *ref, int thres);
-	mplp_aux_t *ma = (mplp_aux_t*)data;
-	int ret, skip = 0;
-	do {
-		int has_ref;
-		ret = ma->iter? bam_iter_read(ma->fp, ma->iter, b) : bam_read1(ma->fp, b);
-		if (ret < 0) break;
-		if (b->core.tid < 0 || (b->core.flag&BAM_FUNMAP)) { // exclude unmapped reads
-			skip = 1;
-			continue;
-		}
+    extern int bam_realn(bam1_t *b, const char *ref);
+    extern int bam_prob_realn_core(bam1_t *b, const char *ref, int);
+    extern int bam_cap_mapQ(bam1_t *b, char *ref, int thres);
+    mplp_aux_t *ma = (mplp_aux_t*)data;
+    int ret, skip = 0;
+    do {
+        int has_ref;
+        ret = ma->iter? sam_itr_next(ma->fp, ma->iter, b) : sam_read1(ma->fp, ma->h, b);
+        if (ret < 0) break;
+        // The 'B' cigar operation is not part of the specification, considering as obsolete.
+        //  bam_remove_B(b);
+        if (b->core.tid < 0 || (b->core.flag&BAM_FUNMAP)) { // exclude unmapped reads
+            skip = 1;
+            continue;
+        }
         if (ma->conf->rflag_require && !(ma->conf->rflag_require&b->core.flag)) { skip = 1; continue; }
         if (ma->conf->rflag_filter && ma->conf->rflag_filter&b->core.flag) { skip = 1; continue; }
-		if (ma->conf->bed) { // test overlap
-			skip = !bed_overlap(ma->conf->bed, ma->h->target_name[b->core.tid], b->core.pos, bam_calend(&b->core, bam1_cigar(b)));
-			if (skip) continue;
-		}
-		if (ma->conf->rghash) { // exclude read groups
-			uint8_t *rg = bam_aux_get(b, "RG");
-			skip = (rg && bcf_str2id(ma->conf->rghash, (const char*)(rg+1)) >= 0);
-			if (skip) continue;
-		}
-		if (ma->conf->flag & MPLP_ILLUMINA13) {
-			int i;
-			uint8_t *qual = bam1_qual(b);
-			for (i = 0; i < b->core.l_qseq; ++i)
-				qual[i] = qual[i] > 31? qual[i] - 31 : 0;
-		}
-		has_ref = (ma->ref && ma->ref_id == b->core.tid)? 1 : 0;
-		skip = 0;
-		if (has_ref && (ma->conf->flag&MPLP_REALN)) bam_prob_realn_core(b, ma->ref, (ma->conf->flag & MPLP_REDO_BAQ)? 7 : 3);
-		if (has_ref && ma->conf->capQ_thres > 10) {
-			int q = bam_cap_mapQ(b, ma->ref, ma->conf->capQ_thres);
-			if (q < 0) skip = 1;
-			else if (b->core.qual > q) b->core.qual = q;
-		}
-		else if (b->core.qual < ma->conf->min_mq) skip = 1; 
-		else if ((ma->conf->flag&MPLP_NO_ORPHAN) && (b->core.flag&1) && !(b->core.flag&2)) skip = 1;
-	} while (skip);
-	return ret;
+        if (ma->conf->bed) { // test overlap
+            skip = !bed_overlap(ma->conf->bed, ma->h->target_name[b->core.tid], b->core.pos, bam_endpos(b));
+            if (skip) continue;
+        }
+        if (ma->conf->rghash) { // exclude read groups
+            uint8_t *rg = bam_aux_get(b, "RG");
+            skip = (rg && khash_str2int_get(ma->conf->rghash, (const char*)(rg+1), NULL)==0);
+            if (skip) continue;
+        }
+        if (ma->conf->flag & MPLP_ILLUMINA13) {
+            int i;
+            uint8_t *qual = bam_get_qual(b);
+            for (i = 0; i < b->core.l_qseq; ++i)
+                qual[i] = qual[i] > 31? qual[i] - 31 : 0;
+        }
+        has_ref = (ma->ref && ma->ref_id == b->core.tid)? 1 : 0;
+        skip = 0;
+        if (has_ref && (ma->conf->flag&MPLP_REALN)) bam_prob_realn_core(b, ma->ref, (ma->conf->flag & MPLP_REDO_BAQ)? 7 : 3);
+        if (has_ref && ma->conf->capQ_thres > 10) {
+            int q = bam_cap_mapQ(b, ma->ref, ma->conf->capQ_thres);
+            if (q < 0) skip = 1;
+            else if (b->core.qual > q) b->core.qual = q;
+        }
+        if (b->core.qual < ma->conf->min_mq) skip = 1;
+        else if ((ma->conf->flag&MPLP_NO_ORPHAN) && (b->core.flag&BAM_FPAIRED) && !(b->core.flag&BAM_FPROPER_PAIR)) skip = 1;
+    } while (skip);
+    return ret;
 }
 
 static void group_smpl(mplp_pileup_t *m, bam_sample_t *sm, kstring_t *buf,
-					   int n, char *const*fn, int *n_plp, const bam_pileup1_t **plp, int ignore_rg)
+                       int n, char *const*fn, int *n_plp, const bam_pileup1_t **plp, int ignore_rg)
 {
-	int i, j;
-	memset(m->n_plp, 0, m->n * sizeof(int));
-	for (i = 0; i < n; ++i) {
-		for (j = 0; j < n_plp[i]; ++j) {
-			const bam_pileup1_t *p = plp[i] + j;
-			uint8_t *q;
-			int id = -1;
-			q = ignore_rg? 0 : bam_aux_get(p->b, "RG");
-			if (q) id = bam_smpl_rg2smid(sm, fn[i], (char*)q+1, buf);
-			if (id < 0) id = bam_smpl_rg2smid(sm, fn[i], 0, buf);
-			if (id < 0 || id >= m->n) {
-				assert(q); // otherwise a bug
-				fprintf(pysamerr, "[%s] Read group %s used in file %s but absent from the header or an alignment missing read group.\n", __func__, (char*)q+1, fn[i]);
-				exit(1);
-			}
-			if (m->n_plp[id] == m->m_plp[id]) {
-				m->m_plp[id] = m->m_plp[id]? m->m_plp[id]<<1 : 8;
-				m->plp[id] = realloc(m->plp[id], sizeof(bam_pileup1_t) * m->m_plp[id]);
-			}
-			m->plp[id][m->n_plp[id]++] = *p;
-		}
-	}
+    int i, j;
+    memset(m->n_plp, 0, m->n * sizeof(int));
+    for (i = 0; i < n; ++i) {
+        for (j = 0; j < n_plp[i]; ++j) {
+            const bam_pileup1_t *p = plp[i] + j;
+            uint8_t *q;
+            int id = -1;
+            q = ignore_rg? 0 : bam_aux_get(p->b, "RG");
+            if (q) id = bam_smpl_rg2smid(sm, fn[i], (char*)q+1, buf);
+            if (id < 0) id = bam_smpl_rg2smid(sm, fn[i], 0, buf);
+            if (id < 0 || id >= m->n) {
+                assert(q); // otherwise a bug
+                fprintf(pysamerr, "[%s] Read group %s used in file %s but absent from the header or an alignment missing read group.\n", __func__, (char*)q+1, fn[i]);
+                exit(1);
+            }
+            if (m->n_plp[id] == m->m_plp[id]) {
+                m->m_plp[id] = m->m_plp[id]? m->m_plp[id]<<1 : 8;
+                m->plp[id] = realloc(m->plp[id], sizeof(bam_pileup1_t) * m->m_plp[id]);
+            }
+            m->plp[id][m->n_plp[id]++] = *p;
+        }
+    }
 }
 
+/*
+ * Performs pileup
+ * @param conf configuration for this pileup
+ * @param n number of files specified in fn
+ * @param fn filenames
+ */
 static int mpileup(mplp_conf_t *conf, int n, char **fn)
 {
-	extern void *bcf_call_add_rg(void *rghash, const char *hdtext, const char *list);
-	extern void bcf_call_del_rghash(void *rghash);
-	mplp_aux_t **data;
-	int i, tid, pos, *n_plp, tid0 = -1, beg0 = 0, end0 = 1u<<29, ref_len, ref_tid = -1, max_depth, max_indel_depth;
-	const bam_pileup1_t **plp;
-	bam_mplp_t iter;
-	bam_header_t *h = 0;
-	char *ref;
-	void *rghash = 0;
-
-	bcf_callaux_t *bca = 0;
-	bcf_callret1_t *bcr = 0;
-	bcf_call_t bc;
-	bcf_t *bp = 0;
-	bcf_hdr_t *bh = 0;
-
-	bam_sample_t *sm = 0;
-	kstring_t buf;
-	mplp_pileup_t gplp;
-
-	memset(&gplp, 0, sizeof(mplp_pileup_t));
-	memset(&buf, 0, sizeof(kstring_t));
-	memset(&bc, 0, sizeof(bcf_call_t));
-	data = calloc(n, sizeof(void*));
-	plp = calloc(n, sizeof(void*));
-	n_plp = calloc(n, sizeof(int*));
-	sm = bam_smpl_init();
-
-	// read the header and initialize data
-	for (i = 0; i < n; ++i) {
-		bam_header_t *h_tmp;
-		data[i] = calloc(1, sizeof(mplp_aux_t));
-		data[i]->fp = strcmp(fn[i], "-") == 0? bam_dopen(fileno(stdin), "r") : bam_open(fn[i], "r");
+    extern void *bcf_call_add_rg(void *rghash, const char *hdtext, const char *list);
+    extern void bcf_call_del_rghash(void *rghash);
+    mplp_aux_t **data;
+    int i, tid, pos, *n_plp, tid0 = -1, beg0 = 0, end0 = 1u<<29, ref_len, ref_tid = -1, max_depth, max_indel_depth;
+    const bam_pileup1_t **plp;
+    bam_mplp_t iter;
+    bam_hdr_t *h = NULL; /* header of first file in input list */
+    char *ref;
+    void *rghash = NULL;
+    FILE *pileup_fp = NULL;
+
+    bcf_callaux_t *bca = NULL;
+    bcf_callret1_t *bcr = NULL;
+    bcf_call_t bc;
+    htsFile *bcf_fp = NULL;
+    bcf_hdr_t *bcf_hdr = NULL;
+
+    bam_sample_t *sm = NULL;
+    kstring_t buf;
+    mplp_pileup_t gplp;
+
+    memset(&gplp, 0, sizeof(mplp_pileup_t));
+    memset(&buf, 0, sizeof(kstring_t));
+    memset(&bc, 0, sizeof(bcf_call_t));
+    data = calloc(n, sizeof(mplp_aux_t*));
+    plp = calloc(n, sizeof(bam_pileup1_t*));
+    n_plp = calloc(n, sizeof(int));
+    sm = bam_smpl_init();
+
+    if (n == 0) {
+        fprintf(pysamerr,"[%s] no input file/data given\n", __func__);
+        exit(1);
+    }
+
+    // read the header of each file in the list and initialize data
+    for (i = 0; i < n; ++i) {
+        bam_hdr_t *h_tmp;
+        data[i] = calloc(1, sizeof(mplp_aux_t));
+        data[i]->fp = sam_open(fn[i], "rb");
         if ( !data[i]->fp )
         {
             fprintf(pysamerr, "[%s] failed to open %s: %s\n", __func__, fn[i], strerror(errno));
             exit(1);
         }
-		data[i]->conf = conf;
-		h_tmp = bam_header_read(data[i]->fp);
+        hts_set_fai_filename(data[i]->fp, conf->fai_fname);
+        data[i]->conf = conf;
+        h_tmp = sam_hdr_read(data[i]->fp);
         if ( !h_tmp ) {
             fprintf(pysamerr,"[%s] fail to read the header of %s\n", __func__, fn[i]);
             exit(1);
         }
-		data[i]->h = i? h : h_tmp; // for i==0, "h" has not been set yet
-		bam_smpl_add(sm, fn[i], (conf->flag&MPLP_IGNORE_RG)? 0 : h_tmp->text);
-		rghash = bcf_call_add_rg(rghash, h_tmp->text, conf->pl_list);
-		if (conf->reg) {
-			int beg, end;
-			bam_index_t *idx;
-			idx = bam_index_load(fn[i]);
-			if (idx == 0) {
-				fprintf(pysamerr, "[%s] fail to load index for %s\n", __func__, fn[i]);
-				exit(1);
-			}
-			if (bam_parse_region(h_tmp, conf->reg, &tid, &beg, &end) < 0) {
-				fprintf(pysamerr, "[%s] malformatted region or wrong seqname for %s\n", __func__, fn[i]);
-				exit(1);
-			}
-			if (i == 0) tid0 = tid, beg0 = beg, end0 = end;
-			data[i]->iter = bam_iter_query(idx, tid, beg, end);
-			bam_index_destroy(idx);
-		}
-		if (i == 0) h = h_tmp;
-		else {
-			// FIXME: to check consistency
-			bam_header_destroy(h_tmp);
-		}
-	}
-	gplp.n = sm->n;
-	gplp.n_plp = calloc(sm->n, sizeof(int));
-	gplp.m_plp = calloc(sm->n, sizeof(int));
-	gplp.plp = calloc(sm->n, sizeof(void*));
-
-	fprintf(pysamerr, "[%s] %d samples in %d input files\n", __func__, sm->n, n);
-	// write the VCF header
-	if (conf->flag & MPLP_GLF) {
-		kstring_t s;
-		bh = calloc(1, sizeof(bcf_hdr_t));
-		s.l = s.m = 0; s.s = 0;
-		bp = bcf_open("-", (conf->flag&MPLP_NO_COMP)? "wu" : "w");
-		for (i = 0; i < h->n_targets; ++i) {
-			kputs(h->target_name[i], &s);
-			kputc('\0', &s);
-		}
-		bh->l_nm = s.l;
-		bh->name = malloc(s.l);
-		memcpy(bh->name, s.s, s.l);
-		s.l = 0;
-		for (i = 0; i < sm->n; ++i) {
-			kputs(sm->smpl[i], &s); kputc('\0', &s);
-		}
-		bh->l_smpl = s.l;
-		bh->sname = malloc(s.l);
-		memcpy(bh->sname, s.s, s.l);
-        s.l = 0;
-        ksprintf(&s, "##samtoolsVersion=%s\n", BAM_VERSION);
-        if (conf->fai_fname) ksprintf(&s, "##reference=file://%s\n", conf->fai_fname);
-        h->dict = sam_header_parse2(h->text);
-        int nseq;
-        const char *tags[] = {"SN","LN","UR","M5",NULL};
-        char **tbl = sam_header2tbl_n(h->dict, "SQ", tags, &nseq);
-        for (i=0; i<nseq; i++)
+        data[i]->h = i? h : h_tmp; // for i==0, "h" has not been set yet
+        bam_smpl_add(sm, fn[i], (conf->flag&MPLP_IGNORE_RG)? 0 : h_tmp->text);
+        // Collect read group IDs with PL (platform) listed in pl_list (note: fragile, strstr search)
+        rghash = bcf_call_add_rg(rghash, h_tmp->text, conf->pl_list);
+        if (conf->reg) {
+            hts_idx_t *idx = sam_index_load(data[i]->fp, fn[i]);
+            if (idx == 0) {
+                fprintf(pysamerr, "[%s] fail to load index for %s\n", __func__, fn[i]);
+                exit(1);
+            }
+            if ( (data[i]->iter=sam_itr_querys(idx, data[i]->h, conf->reg)) == 0) {
+                fprintf(pysamerr, "[E::%s] fail to parse region '%s'\n", __func__, conf->reg);
+                exit(1);
+            }
+            if (i == 0) tid0 = data[i]->iter->tid, beg0 = data[i]->iter->beg, end0 = data[i]->iter->end;
+            hts_idx_destroy(idx);
+        }
+        if (i == 0) h = h_tmp; /* save the header of first file in list */
+        else {
+            // FIXME: to check consistency
+            bam_hdr_destroy(h_tmp);
+        }
+    }
+    // allocate data storage proportionate to number of samples being studied sm->n
+    gplp.n = sm->n;
+    gplp.n_plp = calloc(sm->n, sizeof(int));
+    gplp.m_plp = calloc(sm->n, sizeof(int));
+    gplp.plp = calloc(sm->n, sizeof(bam_pileup1_t*));
+
+    fprintf(pysamerr, "[%s] %d samples in %d input files\n", __func__, sm->n, n);
+    // write the VCF header
+    if (conf->flag & MPLP_BCF)
+    {
+        const char *mode;
+        if ( conf->flag & MPLP_VCF )
+            mode = (conf->flag&MPLP_NO_COMP)? "wu" : "wz";   // uncompressed VCF or compressed VCF
+        else
+            mode = (conf->flag&MPLP_NO_COMP)? "wub" : "wb";  // uncompressed BCF or compressed BCF
+
+        bcf_fp = bcf_open(conf->output_fname? conf->output_fname : "-", mode);
+        if (bcf_fp == NULL) {
+            fprintf(pysamerr, "[%s] failed to write to %s: %s\n", __func__, conf->output_fname? conf->output_fname : "standard output", strerror(errno));
+            exit(1);
+        }
+
+        bcf_hdr = bcf_hdr_init("w");
+        kstring_t str = {0,0,0};
+
+        ksprintf(&str, "##samtoolsVersion=%s+htslib-%s\n",samtools_version(),hts_version());
+        bcf_hdr_append(bcf_hdr, str.s);
+
+        str.l = 0;
+        ksprintf(&str, "##samtoolsCommand=samtools mpileup");
+        for (i=1; i<conf->argc; i++) ksprintf(&str, " %s", conf->argv[i]);
+        kputc('\n', &str);
+        bcf_hdr_append(bcf_hdr, str.s);
+
+        if (conf->fai_fname)
         {
-            ksprintf(&s, "##contig=<ID=%s", tbl[4*i]);
-            if ( tbl[4*i+1] ) ksprintf(&s, ",length=%s", tbl[4*i+1]);
-            if ( tbl[4*i+2] ) ksprintf(&s, ",URL=%s", tbl[4*i+2]);
-            if ( tbl[4*i+3] ) ksprintf(&s, ",md5=%s", tbl[4*i+3]);
-            kputs(">\n", &s);
+            str.l = 0;
+            ksprintf(&str, "##reference=file://%s\n", conf->fai_fname);
+            bcf_hdr_append(bcf_hdr, str.s);
         }
-        if (tbl) free(tbl);
-		bh->txt = s.s;
-		bh->l_txt = 1 + s.l;
-		bcf_hdr_sync(bh);
-		bcf_hdr_write(bp, bh);
-		bca = bcf_call_init(-1., conf->min_baseQ);
-		bcr = calloc(sm->n, sizeof(bcf_callret1_t));
-		bca->rghash = rghash;
-		bca->openQ = conf->openQ, bca->extQ = conf->extQ, bca->tandemQ = conf->tandemQ;
-		bca->min_frac = conf->min_frac;
-		bca->min_support = conf->min_support;
+
+        // todo: use/write new BAM header manipulation routines, fill also UR, M5
+        for (i=0; i<h->n_targets; i++)
+        {
+            str.l = 0;
+            ksprintf(&str, "##contig=<ID=%s,length=%d>", h->target_name[i], h->target_len[i]);
+            bcf_hdr_append(bcf_hdr, str.s);
+        }
+        free(str.s);
+        bcf_hdr_append(bcf_hdr,"##ALT=<ID=X,Description=\"Represents allele(s) other than observed.\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=INDEL,Number=0,Type=Flag,Description=\"Indicates that the variant is an INDEL.\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=IDV,Number=1,Type=Integer,Description=\"Maximum number of reads supporting an indel\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=IMF,Number=1,Type=Float,Description=\"Maximum fraction of reads supporting an indel\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=DP,Number=1,Type=Integer,Description=\"Raw read depth\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=VDB,Number=1,Type=Float,Description=\"Variant Distance Bias for filtering splice-site artefacts in RNA-seq data (bigger is better)\",Version=\"3\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=RPB,Number=1,Type=Float,Description=\"Mann-Whitney U test of Read Position Bias (bigger is better)\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=MQB,Number=1,Type=Float,Description=\"Mann-Whitney U test of Mapping Quality Bias (bigger is better)\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=BQB,Number=1,Type=Float,Description=\"Mann-Whitney U test of Base Quality Bias (bigger is better)\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=MQSB,Number=1,Type=Float,Description=\"Mann-Whitney U test of Mapping Quality vs Strand Bias (bigger is better)\">");
+#if CDF_MWU_TESTS
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=RPB2,Number=1,Type=Float,Description=\"Mann-Whitney U test of Read Position Bias [CDF] (bigger is better)\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=MQB2,Number=1,Type=Float,Description=\"Mann-Whitney U test of Mapping Quality Bias [CDF] (bigger is better)\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=BQB2,Number=1,Type=Float,Description=\"Mann-Whitney U test of Base Quality Bias [CDF] (bigger is better)\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=MQSB2,Number=1,Type=Float,Description=\"Mann-Whitney U test of Mapping Quality vs Strand Bias [CDF] (bigger is better)\">");
+#endif
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=SGB,Number=1,Type=Float,Description=\"Segregation based metric.\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=MQ0F,Number=1,Type=Float,Description=\"Fraction of MQ0 reads (smaller is better)\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=I16,Number=16,Type=Float,Description=\"Auxiliary tag used for calling, see description of bcf_callret1_t in bam2bcf.h\">");
+        bcf_hdr_append(bcf_hdr,"##INFO=<ID=QS,Number=R,Type=Float,Description=\"Auxiliary tag used for calling\">");
+        bcf_hdr_append(bcf_hdr,"##FORMAT=<ID=PL,Number=G,Type=Integer,Description=\"List of Phred-scaled genotype likelihoods\">");
+        if ( conf->fmt_flag&B2B_FMT_DP )
+            bcf_hdr_append(bcf_hdr,"##FORMAT=<ID=DP,Number=1,Type=Integer,Description=\"Number of high-quality bases\">");
+        if ( conf->fmt_flag&B2B_FMT_DV )
+            bcf_hdr_append(bcf_hdr,"##FORMAT=<ID=DV,Number=1,Type=Integer,Description=\"Number of high-quality non-reference bases\">");
+        if ( conf->fmt_flag&B2B_FMT_DPR )
+            bcf_hdr_append(bcf_hdr,"##FORMAT=<ID=DPR,Number=R,Type=Integer,Description=\"Number of high-quality bases observed for each allele\">");
+        if ( conf->fmt_flag&B2B_INFO_DPR )
+            bcf_hdr_append(bcf_hdr,"##INFO=<ID=DPR,Number=R,Type=Integer,Description=\"Number of high-quality bases observed for each allele\">");
+        if ( conf->fmt_flag&B2B_FMT_DP4 )
+            bcf_hdr_append(bcf_hdr,"##FORMAT=<ID=DP4,Number=4,Type=Integer,Description=\"Number of high-quality ref-fwd, ref-reverse, alt-fwd and alt-reverse bases\">");
+        if ( conf->fmt_flag&B2B_FMT_SP )
+            bcf_hdr_append(bcf_hdr,"##FORMAT=<ID=SP,Number=1,Type=Integer,Description=\"Phred-scaled strand bias P-value\">");
+
+        for (i=0; i<sm->n; i++)
+            bcf_hdr_add_sample(bcf_hdr, sm->smpl[i]);
+        bcf_hdr_add_sample(bcf_hdr, NULL);
+        bcf_hdr_write(bcf_fp, bcf_hdr);
+
+        bca = bcf_call_init(-1., conf->min_baseQ);
+        bcr = calloc(sm->n, sizeof(bcf_callret1_t));
+        bca->rghash = rghash;
+        bca->openQ = conf->openQ, bca->extQ = conf->extQ, bca->tandemQ = conf->tandemQ;
+        bca->min_frac = conf->min_frac;
+        bca->min_support = conf->min_support;
         bca->per_sample_flt = conf->flag & MPLP_PER_SAMPLE;
-	}
-	if (tid0 >= 0 && conf->fai) { // region is set
-		ref = faidx_fetch_seq(conf->fai, h->target_name[tid0], 0, 0x7fffffff, &ref_len);
-		ref_tid = tid0;
-		for (i = 0; i < n; ++i) data[i]->ref = ref, data[i]->ref_id = tid0;
-	} else ref_tid = -1, ref = 0;
-	iter = bam_mplp_init(n, mplp_func, (void**)data);
-	max_depth = conf->max_depth;
-	if (max_depth * sm->n > 1<<20)
-		fprintf(pysamerr, "(%s) Max depth is above 1M. Potential memory hog!\n", __func__);
-	if (max_depth * sm->n < 8000) {
-		max_depth = 8000 / sm->n;
-		fprintf(pysamerr, "<%s> Set max per-file depth to %d\n", __func__, max_depth);
-	}
-	max_indel_depth = conf->max_indel_depth * sm->n;
-	bam_mplp_set_maxcnt(iter, max_depth);
-	while (bam_mplp_auto(iter, &tid, &pos, n_plp, plp) > 0) {
-		if (conf->reg && (pos < beg0 || pos >= end0)) continue; // out of the region requested
-		if (conf->bed && tid >= 0 && !bed_overlap(conf->bed, h->target_name[tid], pos, pos+1)) continue;
-		if (tid != ref_tid) {
-			free(ref); ref = 0;
-			if (conf->fai) ref = faidx_fetch_seq(conf->fai, h->target_name[tid], 0, 0x7fffffff, &ref_len);
-			for (i = 0; i < n; ++i) data[i]->ref = ref, data[i]->ref_id = tid;
-			ref_tid = tid;
-		}
-		if (conf->flag & MPLP_GLF) {
-			int total_depth, _ref0, ref16;
-			bcf1_t *b = calloc(1, sizeof(bcf1_t));
-			for (i = total_depth = 0; i < n; ++i) total_depth += n_plp[i];
-			group_smpl(&gplp, sm, &buf, n, fn, n_plp, plp, conf->flag & MPLP_IGNORE_RG);
-			_ref0 = (ref && pos < ref_len)? ref[pos] : 'N';
-			ref16 = bam_nt16_table[_ref0];
-			for (i = 0; i < gplp.n; ++i)
-				bcf_call_glfgen(gplp.n_plp[i], gplp.plp[i], ref16, bca, bcr + i);
-			bcf_call_combine(gplp.n, bcr, bca, ref16, &bc);
-			bcf_call2bcf(tid, pos, &bc, b, bcr, conf->fmt_flag, 0, 0);
-			bcf_write(bp, bh, b);
-			bcf_destroy(b);
-			// call indels
-			if (!(conf->flag&MPLP_NO_INDEL) && total_depth < max_indel_depth && bcf_call_gap_prep(gplp.n, gplp.n_plp, gplp.plp, pos, bca, ref, rghash) >= 0) {
-				for (i = 0; i < gplp.n; ++i)
-					bcf_call_glfgen(gplp.n_plp[i], gplp.plp[i], -1, bca, bcr + i);
-				if (bcf_call_combine(gplp.n, bcr, bca, -1, &bc) >= 0) {
-					b = calloc(1, sizeof(bcf1_t));
-					bcf_call2bcf(tid, pos, &bc, b, bcr, conf->fmt_flag, bca, ref);
-					bcf_write(bp, bh, b);
-					bcf_destroy(b);
-				}
-			}
-		} else {
-			printf("%s\t%d\t%c", h->target_name[tid], pos + 1, (ref && pos < ref_len)? ref[pos] : 'N');
-			for (i = 0; i < n; ++i) {
-				int j, cnt;
-				for (j = cnt = 0; j < n_plp[i]; ++j) {
-					const bam_pileup1_t *p = plp[i] + j;
-					if (bam1_qual(p->b)[p->qpos] >= conf->min_baseQ) ++cnt;
-				}
-				printf("\t%d\t", cnt);
-				if (n_plp[i] == 0) {
-					printf("*\t*"); // FIXME: printf() is very slow...
-					if (conf->flag & MPLP_PRINT_POS) printf("\t*");
-				} else {
-					for (j = 0; j < n_plp[i]; ++j) {
-						const bam_pileup1_t *p = plp[i] + j;
-						if (bam1_qual(p->b)[p->qpos] >= conf->min_baseQ)
-							pileup_seq(plp[i] + j, pos, ref_len, ref);
-					}
-					putchar('\t');
-					for (j = 0; j < n_plp[i]; ++j) {
-						const bam_pileup1_t *p = plp[i] + j;
-						int c = bam1_qual(p->b)[p->qpos];
-						if (c >= conf->min_baseQ) {
-							c = c + 33 < 126? c + 33 : 126;
-							putchar(c);
-						}
-					}
-					if (conf->flag & MPLP_PRINT_MAPQ) {
-						putchar('\t');
-						for (j = 0; j < n_plp[i]; ++j) {
-							int c = plp[i][j].b->core.qual + 33;
-							if (c > 126) c = 126;
-							putchar(c);
-						}
-					}
-					if (conf->flag & MPLP_PRINT_POS) {
-						putchar('\t');
-						for (j = 0; j < n_plp[i]; ++j) {
-							if (j > 0) putchar(',');
-							printf("%d", plp[i][j].qpos + 1); // FIXME: printf() is very slow...
-						}
-					}
-				}
-			}
-			putchar('\n');
-		}
-	}
-
-	bcf_close(bp);
-	bam_smpl_destroy(sm); free(buf.s);
-	for (i = 0; i < gplp.n; ++i) free(gplp.plp[i]);
-	free(gplp.plp); free(gplp.n_plp); free(gplp.m_plp);
-	bcf_call_del_rghash(rghash);
-	bcf_hdr_destroy(bh); bcf_call_destroy(bca); free(bc.PL); free(bcr);
-	bam_mplp_destroy(iter);
-	bam_header_destroy(h);
-	for (i = 0; i < n; ++i) {
-		bam_close(data[i]->fp);
-		if (data[i]->iter) bam_iter_destroy(data[i]->iter);
-		free(data[i]);
-	}
-	free(data); free(plp); free(ref); free(n_plp);
-	return 0;
+
+        bc.bcf_hdr = bcf_hdr;
+        bc.n = sm->n;
+        bc.PL = malloc(15 * sm->n * sizeof(*bc.PL));
+        if (conf->fmt_flag)
+        {
+            assert( sizeof(float)==sizeof(int32_t) );
+            bc.DP4 = malloc(sm->n * sizeof(int32_t) * 4);
+            bc.fmt_arr = malloc(sm->n * sizeof(float)); // all fmt_flag fields
+            if ( conf->fmt_flag&(B2B_INFO_DPR|B2B_FMT_DPR) )
+            {
+                // first B2B_MAX_ALLELES fields for total numbers, the rest per-sample
+                bc.DPR = malloc((sm->n+1)*B2B_MAX_ALLELES*sizeof(int32_t));
+                for (i=0; i<sm->n; i++)
+                    bcr[i].DPR = bc.DPR + (i+1)*B2B_MAX_ALLELES;
+            }
+        }
+    }
+    else {
+        pileup_fp = conf->output_fname? fopen(conf->output_fname, "w") : stdout;
+
+        if (pileup_fp == NULL) {
+            fprintf(pysamerr, "[%s] failed to write to %s: %s\n", __func__, conf->output_fname, strerror(errno));
+            exit(1);
+        }
+    }
+
+    if (tid0 >= 0 && conf->fai) { // region is set
+        ref = faidx_fetch_seq(conf->fai, h->target_name[tid0], 0, 0x7fffffff, &ref_len);
+        ref_tid = tid0;
+        for (i = 0; i < n; ++i) data[i]->ref = ref, data[i]->ref_id = tid0;
+    } else ref_tid = -1, ref = 0;
+
+    // begin pileup
+    iter = bam_mplp_init(n, mplp_func, (void**)data);
+    if ( conf->flag & MPLP_SMART_OVERLAPS ) bam_mplp_init_overlaps(iter);
+    max_depth = conf->max_depth;
+    if (max_depth * sm->n > 1<<20)
+        fprintf(pysamerr, "(%s) Max depth is above 1M. Potential memory hog!\n", __func__);
+    if (max_depth * sm->n < 8000) {
+        max_depth = 8000 / sm->n;
+        fprintf(pysamerr, "<%s> Set max per-file depth to %d\n", __func__, max_depth);
+    }
+    max_indel_depth = conf->max_indel_depth * sm->n;
+    bam_mplp_set_maxcnt(iter, max_depth);
+    bcf1_t *bcf_rec = bcf_init1();
+    int ret;
+    while ( (ret=bam_mplp_auto(iter, &tid, &pos, n_plp, plp)) > 0) {
+        if (conf->reg && (pos < beg0 || pos >= end0)) continue; // out of the region requested
+        if (conf->bed && tid >= 0 && !bed_overlap(conf->bed, h->target_name[tid], pos, pos+1)) continue;
+        if (tid != ref_tid) {
+            free(ref); ref = 0;
+            if (conf->fai) ref = faidx_fetch_seq(conf->fai, h->target_name[tid], 0, 0x7fffffff, &ref_len);
+            for (i = 0; i < n; ++i) data[i]->ref = ref, data[i]->ref_id = tid;
+            ref_tid = tid;
+        }
+        if (conf->flag & MPLP_BCF) {
+            int total_depth, _ref0, ref16;
+            for (i = total_depth = 0; i < n; ++i) total_depth += n_plp[i];
+            group_smpl(&gplp, sm, &buf, n, fn, n_plp, plp, conf->flag & MPLP_IGNORE_RG);
+            _ref0 = (ref && pos < ref_len)? ref[pos] : 'N';
+            ref16 = seq_nt16_table[_ref0];
+            bcf_callaux_clean(bca, &bc);
+            for (i = 0; i < gplp.n; ++i)
+                bcf_call_glfgen(gplp.n_plp[i], gplp.plp[i], ref16, bca, bcr + i);
+            bc.tid = tid; bc.pos = pos;
+            bcf_call_combine(gplp.n, bcr, bca, ref16, &bc);
+            bcf_clear1(bcf_rec);
+            bcf_call2bcf(&bc, bcf_rec, bcr, conf->fmt_flag, 0, 0);
+            bcf_write1(bcf_fp, bcf_hdr, bcf_rec);
+            // call indels; todo: subsampling with total_depth>max_indel_depth instead of ignoring?
+            if (!(conf->flag&MPLP_NO_INDEL) && total_depth < max_indel_depth && bcf_call_gap_prep(gplp.n, gplp.n_plp, gplp.plp, pos, bca, ref, rghash) >= 0)
+            {
+                bcf_callaux_clean(bca, &bc);
+                for (i = 0; i < gplp.n; ++i)
+                    bcf_call_glfgen(gplp.n_plp[i], gplp.plp[i], -1, bca, bcr + i);
+                if (bcf_call_combine(gplp.n, bcr, bca, -1, &bc) >= 0) {
+                    bcf_clear1(bcf_rec);
+                    bcf_call2bcf(&bc, bcf_rec, bcr, conf->fmt_flag, bca, ref);
+                    bcf_write1(bcf_fp, bcf_hdr, bcf_rec);
+                }
+            }
+        } else {
+            fprintf(pileup_fp, "%s\t%d\t%c", h->target_name[tid], pos + 1, (ref && pos < ref_len)? ref[pos] : 'N');
+            for (i = 0; i < n; ++i) {
+                int j, cnt;
+                for (j = cnt = 0; j < n_plp[i]; ++j) {
+                    const bam_pileup1_t *p = plp[i] + j;
+                    if (bam_get_qual(p->b)[p->qpos] >= conf->min_baseQ) ++cnt;
+                }
+                fprintf(pileup_fp, "\t%d\t", cnt);
+                if (n_plp[i] == 0) {
+                    fputs("*\t*", pileup_fp);
+                    if (conf->flag & MPLP_PRINT_MAPQ) fputs("\t*", pileup_fp);
+                    if (conf->flag & MPLP_PRINT_POS) fputs("\t*", pileup_fp);
+                } else {
+                    for (j = 0; j < n_plp[i]; ++j) {
+                        const bam_pileup1_t *p = plp[i] + j;
+                        if (bam_get_qual(p->b)[p->qpos] >= conf->min_baseQ)
+                            pileup_seq(pileup_fp, plp[i] + j, pos, ref_len, ref);
+                    }
+                    putc('\t', pileup_fp);
+                    for (j = 0; j < n_plp[i]; ++j) {
+                        const bam_pileup1_t *p = plp[i] + j;
+                        int c = bam_get_qual(p->b)[p->qpos];
+                        if (c >= conf->min_baseQ) {
+                            c = c + 33 < 126? c + 33 : 126;
+                            putc(c, pileup_fp);
+                        }
+                    }
+                    if (conf->flag & MPLP_PRINT_MAPQ) {
+                        putc('\t', pileup_fp);
+                        for (j = 0; j < n_plp[i]; ++j) {
+                            const bam_pileup1_t *p = plp[i] + j;
+                            int c = bam_get_qual(p->b)[p->qpos];
+                            if ( c < conf->min_baseQ ) continue;
+                            c = plp[i][j].b->core.qual + 33;
+                            if (c > 126) c = 126;
+                            putc(c, pileup_fp);
+                        }
+                    }
+                    if (conf->flag & MPLP_PRINT_POS) {
+                        putc('\t', pileup_fp);
+                        for (j = 0; j < n_plp[i]; ++j) {
+                            if (j > 0) putc(',', pileup_fp);
+                            fprintf(pileup_fp, "%d", plp[i][j].qpos + 1); // FIXME: printf() is very slow...
+                        }
+                    }
+                }
+            }
+            putc('\n', pileup_fp);
+        }
+    }
+
+    // clean up
+    free(bc.tmp.s);
+    bcf_destroy1(bcf_rec);
+    if (bcf_fp)
+    {
+        hts_close(bcf_fp);
+        bcf_hdr_destroy(bcf_hdr);
+        bcf_call_destroy(bca);
+        free(bc.PL);
+        free(bc.DP4);
+        free(bc.DPR);
+        free(bc.fmt_arr);
+        free(bcr);
+    }
+    if (pileup_fp && conf->output_fname) fclose(pileup_fp);
+    bam_smpl_destroy(sm); free(buf.s);
+    for (i = 0; i < gplp.n; ++i) free(gplp.plp[i]);
+    free(gplp.plp); free(gplp.n_plp); free(gplp.m_plp);
+    bcf_call_del_rghash(rghash);
+    bam_mplp_destroy(iter);
+    bam_hdr_destroy(h);
+    for (i = 0; i < n; ++i) {
+        sam_close(data[i]->fp);
+        if (data[i]->iter) hts_itr_destroy(data[i]->iter);
+        free(data[i]);
+    }
+    free(data); free(plp); free(ref); free(n_plp);
+    return ret;
 }
 
 #define MAX_PATH_LEN 1024
@@ -439,7 +575,7 @@ int read_file_list(const char *file_list,int *n,char **argv[])
 
     files = calloc(nfiles,sizeof(char*));
     nfiles = 0;
-    while ( fgets(buf,MAX_PATH_LEN,fh) ) 
+    while ( fgets(buf,MAX_PATH_LEN,fh) )
     {
         // allow empty lines and trailing spaces
         len = strlen(buf);
@@ -453,7 +589,7 @@ int read_file_list(const char *file_list,int *n,char **argv[])
             // no such file, check if it is safe to print its name
             int i, safe_to_print = 1;
             for (i=0; i<len; i++)
-                if (!isprint(buf[i])) { safe_to_print = 0; break; } 
+                if (!isprint(buf[i])) { safe_to_print = 0; break; }
             if ( safe_to_print )
                 fprintf(pysamerr,"The file list \"%s\" appears broken, could not locate: %s\n", file_list,buf);
             else
@@ -477,132 +613,271 @@ int read_file_list(const char *file_list,int *n,char **argv[])
 }
 #undef MAX_PATH_LEN
 
+int parse_format_flag(const char *str)
+{
+    int i, flag = 0, n_tags;
+    char **tags = hts_readlist(str, 0, &n_tags);
+    for(i=0; i<n_tags; i++)
+    {
+        if ( !strcasecmp(tags[i],"DP") ) flag |= B2B_FMT_DP;
+        else if ( !strcasecmp(tags[i],"DV") ) flag |= B2B_FMT_DV;
+        else if ( !strcasecmp(tags[i],"SP") ) flag |= B2B_FMT_SP;
+        else if ( !strcasecmp(tags[i],"DP4") ) flag |= B2B_FMT_DP4;
+        else if ( !strcasecmp(tags[i],"DPR") ) flag |= B2B_FMT_DPR;
+        else if ( !strcasecmp(tags[i],"INFO/DPR") ) flag |= B2B_INFO_DPR;
+        else
+        {
+            fprintf(pysamerr,"Could not parse tag \"%s\" in \"%s\"\n", tags[i], str);
+            exit(1);
+        }
+        free(tags[i]);
+    }
+    if (n_tags) free(tags);
+    return flag;
+}
+
+static void print_usage(FILE *fp, const mplp_conf_t *mplp)
+{
+    char *tmp_require = bam_flag2str(mplp->rflag_require);
+    char *tmp_filter  = bam_flag2str(mplp->rflag_filter);
+
+    // Display usage information, formatted for the standard 80 columns.
+    // (The unusual string formatting here aids the readability of this
+    // source code in 80 columns, to the extent that's possible.)
+
+    fprintf(fp,
+"\n"
+"Usage: samtools mpileup [options] in1.bam [in2.bam [...]]\n"
+"\n"
+"Input options:\n"
+"  -6, --illumina1.3+      quality is in the Illumina-1.3+ encoding\n"
+"  -A, --count-orphans     do not discard anomalous read pairs\n"
+"  -b, --bam-list FILE     list of input BAM filenames, one per line\n"
+"  -B, --no-BAQ            disable BAQ (per-Base Alignment Quality)\n"
+"  -C, --adjust-MQ INT     adjust mapping quality; recommended:50, disable:0 [0]\n"
+"  -d, --max-depth INT     max per-BAM depth; avoids excessive memory usage [%d]\n", mplp->max_depth);
+    fprintf(fp,
+"  -E, --redo-BAQ          recalculate BAQ on the fly, ignore existing BQs\n"
+"  -f, --fasta-ref FILE    faidx indexed reference sequence file\n"
+"  -G, --exclude-RG FILE   exclude read groups listed in FILE\n"
+"  -l, --positions FILE    skip unlisted positions (chr pos) or regions (BED)\n"
+"  -q, --min-MQ INT        skip alignments with mapQ smaller than INT [%d]\n", mplp->min_mq);
+    fprintf(fp,
+"  -Q, --min-BQ INT        skip bases with baseQ/BAQ smaller than INT [%d]\n", mplp->min_baseQ);
+    fprintf(fp,
+"  -r, --region REG        region in which pileup is generated\n"
+"  -R, --ignore-RG         ignore RG tags (one BAM = one sample)\n"
+"  --rf, --incl-flags STR|INT  required flags: skip reads with mask bits unset [%s]\n", tmp_require);
+    fprintf(fp,
+"  --ff, --excl-flags STR|INT  filter flags: skip reads with mask bits set\n"
+"                                            [%s]\n", tmp_filter);
+    fprintf(fp,
+"  -x, --ignore-overlaps   disable read-pair overlap detection\n"
+"\n"
+"Output options:\n"
+"  -o, --output FILE       write output to FILE [standard output]\n"
+"  -g, --BCF               generate genotype likelihoods in BCF format\n"
+"  -v, --VCF               generate genotype likelihoods in VCF format\n"
+"\n"
+"Output options for mpileup format (without -g/-v):\n"
+"  -O, --output-BP         output base positions on reads\n"
+"  -s, --output-MQ         output mapping quality\n"
+"\n"
+"Output options for genotype likelihoods (when -g/-v is used):\n"
+"  -t, --output-tags LIST  optional tags to output: DP,DPR,DV,DP4,INFO/DPR,SP []\n"
+"  -u, --uncompressed      generate uncompressed VCF/BCF output\n"
+"\n"
+"SNP/INDEL genotype likelihoods options (effective with -g/-v):\n"
+"  -e, --ext-prob INT      Phred-scaled gap extension seq error probability [%d]\n", mplp->extQ);
+    fprintf(fp,
+"  -F, --gap-frac FLOAT    minimum fraction of gapped reads [%g]\n", mplp->min_frac);
+    fprintf(fp,
+"  -h, --tandem-qual INT   coefficient for homopolymer errors [%d]\n", mplp->tandemQ);
+    fprintf(fp,
+"  -I, --skip-indels       do not perform indel calling\n"
+"  -L, --max-idepth INT    maximum per-sample depth for INDEL calling [%d]\n", mplp->max_indel_depth);
+    fprintf(fp,
+"  -m, --min-ireads INT    minimum number gapped reads for indel candidates [%d]\n", mplp->min_support);
+    fprintf(fp,
+"  -o, --open-prob INT     Phred-scaled gap open seq error probability [%d]\n", mplp->openQ);
+    fprintf(fp,
+"  -p, --per-sample-mF     apply -m and -F per-sample for increased sensitivity\n"
+"  -P, --platforms STR     comma separated list of platforms for indels [all]\n"
+"\n"
+"Notes: Assuming diploid individuals.\n");
+
+    free(tmp_require);
+    free(tmp_filter);
+}
+
 int bam_mpileup(int argc, char *argv[])
 {
-	int c;
+    int c;
     const char *file_list = NULL;
     char **fn = NULL;
     int nfiles = 0, use_orphan = 0;
-	mplp_conf_t mplp;
-	memset(&mplp, 0, sizeof(mplp_conf_t));
-	mplp.max_mq = 60;
-	mplp.min_baseQ = 13;
-	mplp.capQ_thres = 0;
-	mplp.max_depth = 250; mplp.max_indel_depth = 250;
-	mplp.openQ = 40; mplp.extQ = 20; mplp.tandemQ = 100;
-	mplp.min_frac = 0.002; mplp.min_support = 1;
-	mplp.flag = MPLP_NO_ORPHAN | MPLP_REALN;
-    static struct option lopts[] = 
+    mplp_conf_t mplp;
+    memset(&mplp, 0, sizeof(mplp_conf_t));
+    mplp.min_baseQ = 13;
+    mplp.capQ_thres = 0;
+    mplp.max_depth = 250; mplp.max_indel_depth = 250;
+    mplp.openQ = 40; mplp.extQ = 20; mplp.tandemQ = 100;
+    mplp.min_frac = 0.002; mplp.min_support = 1;
+    mplp.flag = MPLP_NO_ORPHAN | MPLP_REALN | MPLP_SMART_OVERLAPS;
+    mplp.argc = argc; mplp.argv = argv;
+    mplp.rflag_filter = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP;
+    mplp.output_fname = NULL;
+    static const struct option lopts[] =
     {
-        {"rf",1,0,1},   // require flag
-        {"ff",1,0,2},   // filter flag
-        {0,0,0,0}
+        {"rf", required_argument, NULL, 1},   // require flag
+        {"ff", required_argument, NULL, 2},   // filter flag
+        {"incl-flags", required_argument, NULL, 1},
+        {"excl-flags", required_argument, NULL, 2},
+        {"output", required_argument, NULL, 3},
+        {"open-prob", required_argument, NULL, 4},
+        {"illumina1.3+", no_argument, NULL, '6'},
+        {"count-orphans", no_argument, NULL, 'A'},
+        {"bam-list", required_argument, NULL, 'b'},
+        {"no-BAQ", no_argument, NULL, 'B'},
+        {"no-baq", no_argument, NULL, 'B'},
+        {"adjust-MQ", required_argument, NULL, 'C'},
+        {"adjust-mq", required_argument, NULL, 'C'},
+        {"max-depth", required_argument, NULL, 'd'},
+        {"redo-BAQ", no_argument, NULL, 'E'},
+        {"redo-baq", no_argument, NULL, 'E'},
+        {"fasta-ref", required_argument, NULL, 'f'},
+        {"exclude-RG", required_argument, NULL, 'G'},
+        {"exclude-rg", required_argument, NULL, 'G'},
+        {"positions", required_argument, NULL, 'l'},
+        {"region", required_argument, NULL, 'r'},
+        {"ignore-RG", no_argument, NULL, 'R'},
+        {"ignore-rg", no_argument, NULL, 'R'},
+        {"min-MQ", required_argument, NULL, 'q'},
+        {"min-mq", required_argument, NULL, 'q'},
+        {"min-BQ", required_argument, NULL, 'Q'},
+        {"min-bq", required_argument, NULL, 'Q'},
+        {"ignore-overlaps", no_argument, NULL, 'x'},
+        {"BCF", no_argument, NULL, 'g'},
+        {"bcf", no_argument, NULL, 'g'},
+        {"VCF", no_argument, NULL, 'v'},
+        {"vcf", no_argument, NULL, 'v'},
+        {"output-BP", no_argument, NULL, 'O'},
+        {"output-bp", no_argument, NULL, 'O'},
+        {"output-MQ", no_argument, NULL, 's'},
+        {"output-mq", no_argument, NULL, 's'},
+        {"output-tags", required_argument, NULL, 't'},
+        {"uncompressed", no_argument, NULL, 'u'},
+        {"ext-prob", required_argument, NULL, 'e'},
+        {"gap-frac", required_argument, NULL, 'F'},
+        {"tandem-qual", required_argument, NULL, 'h'},
+        {"skip-indels", no_argument, NULL, 'I'},
+        {"max-idepth", required_argument, NULL, 'L'},
+        {"min-ireads ", required_argument, NULL, 'm'},
+        {"per-sample-mF", no_argument, NULL, 'p'},
+        {"per-sample-mf", no_argument, NULL, 'p'},
+        {"platforms", required_argument, NULL, 'P'},
+        {NULL, 0, NULL, 0}
     };
-	while ((c = getopt_long(argc, argv, "Agf:r:l:M:q:Q:uaRC:BDSd:L:b:P:po:e:h:Im:F:EG:6OsV1:2:",lopts,NULL)) >= 0) {
-		switch (c) {
-        case  1 : mplp.rflag_require = strtol(optarg,0,0); break;
-        case  2 : mplp.rflag_filter  = strtol(optarg,0,0); break;
-		case 'f':
-			mplp.fai = fai_load(optarg);
-			if (mplp.fai == 0) return 1;
+    while ((c = getopt_long(argc, argv, "Agf:r:l:q:Q:uRC:BDSd:L:b:P:po:e:h:Im:F:EG:6OsVvxt:",lopts,NULL)) >= 0) {
+        switch (c) {
+        case 'x': mplp.flag &= ~MPLP_SMART_OVERLAPS; break;
+        case  1 :
+            mplp.rflag_require = bam_str2flag(optarg);
+            if ( mplp.rflag_require<0 ) { fprintf(pysamerr,"Could not parse --rf %s\n", optarg); return 1; }
+            break;
+        case  2 :
+            mplp.rflag_filter = bam_str2flag(optarg);
+            if ( mplp.rflag_filter<0 ) { fprintf(pysamerr,"Could not parse --ff %s\n", optarg); return 1; }
+            break;
+        case  3 : mplp.output_fname = optarg; break;
+        case  4 : mplp.openQ = atoi(optarg); break;
+        case 'f':
+            mplp.fai = fai_load(optarg);
+            if (mplp.fai == 0) return 1;
             mplp.fai_fname = optarg;
-			break;
-		case 'd': mplp.max_depth = atoi(optarg); break;
-		case 'r': mplp.reg = strdup(optarg); break;
-		case 'l': mplp.bed = bed_read(optarg); break;
-		case 'P': mplp.pl_list = strdup(optarg); break;
-		case 'p': mplp.flag |= MPLP_PER_SAMPLE; break;
-		case 'g': mplp.flag |= MPLP_GLF; break;
-		case 'u': mplp.flag |= MPLP_NO_COMP | MPLP_GLF; break;
-		case 'a': mplp.flag |= MPLP_NO_ORPHAN | MPLP_REALN; break;
-		case 'B': mplp.flag &= ~MPLP_REALN; break;
-		case 'D': mplp.fmt_flag |= B2B_FMT_DP; break;
-		case 'S': mplp.fmt_flag |= B2B_FMT_SP; break;
-		case 'V': mplp.fmt_flag |= B2B_FMT_DV; break;
-		case 'I': mplp.flag |= MPLP_NO_INDEL; break;
-		case 'E': mplp.flag |= MPLP_REDO_BAQ; break;
-		case '6': mplp.flag |= MPLP_ILLUMINA13; break;
-		case 'R': mplp.flag |= MPLP_IGNORE_RG; break;
-		case 's': mplp.flag |= MPLP_PRINT_MAPQ; break;
-		case 'O': mplp.flag |= MPLP_PRINT_POS; break;
-		case 'C': mplp.capQ_thres = atoi(optarg); break;
-		case 'M': mplp.max_mq = atoi(optarg); break;
-		case 'q': mplp.min_mq = atoi(optarg); break;
-		case 'Q': mplp.min_baseQ = atoi(optarg); break;
+            break;
+        case 'd': mplp.max_depth = atoi(optarg); break;
+        case 'r': mplp.reg = strdup(optarg); break;
+        case 'l':
+                  // In the original version the whole BAM was streamed which is inefficient
+                  //  with few BED intervals and big BAMs. Todo: devise a heuristic to determine
+                  //  best strategy, that is streaming or jumping.
+                  mplp.bed = bed_read(optarg);
+                  if (!mplp.bed) { print_error_errno("Could not read file \"%s\"", optarg); return 1; }
+                  break;
+        case 'P': mplp.pl_list = strdup(optarg); break;
+        case 'p': mplp.flag |= MPLP_PER_SAMPLE; break;
+        case 'g': mplp.flag |= MPLP_BCF; break;
+        case 'v': mplp.flag |= MPLP_BCF | MPLP_VCF; break;
+        case 'u': mplp.flag |= MPLP_NO_COMP | MPLP_BCF; break;
+        case 'B': mplp.flag &= ~MPLP_REALN; break;
+        case 'D': mplp.fmt_flag |= B2B_FMT_DP; fprintf(pysamerr, "[warning] samtools mpileup option `-D` is functional, but deprecated. Please switch to `-t DP` in future.\n"); break;
+        case 'S': mplp.fmt_flag |= B2B_FMT_SP; fprintf(pysamerr, "[warning] samtools mpileup option `-S` is functional, but deprecated. Please switch to `-t SP` in future.\n"); break;
+        case 'V': mplp.fmt_flag |= B2B_FMT_DV; fprintf(pysamerr, "[warning] samtools mpileup option `-V` is functional, but deprecated. Please switch to `-t DV` in future.\n"); break;
+        case 'I': mplp.flag |= MPLP_NO_INDEL; break;
+        case 'E': mplp.flag |= MPLP_REDO_BAQ; break;
+        case '6': mplp.flag |= MPLP_ILLUMINA13; break;
+        case 'R': mplp.flag |= MPLP_IGNORE_RG; break;
+        case 's': mplp.flag |= MPLP_PRINT_MAPQ; break;
+        case 'O': mplp.flag |= MPLP_PRINT_POS; break;
+        case 'C': mplp.capQ_thres = atoi(optarg); break;
+        case 'q': mplp.min_mq = atoi(optarg); break;
+        case 'Q': mplp.min_baseQ = atoi(optarg); break;
         case 'b': file_list = optarg; break;
-		case 'o': mplp.openQ = atoi(optarg); break;
-		case 'e': mplp.extQ = atoi(optarg); break;
-		case 'h': mplp.tandemQ = atoi(optarg); break;
-		case 'A': use_orphan = 1; break;
-		case 'F': mplp.min_frac = atof(optarg); break;
-		case 'm': mplp.min_support = atoi(optarg); break;
-		case 'L': mplp.max_indel_depth = atoi(optarg); break;
-		case 'G': {
-				FILE *fp_rg;
-				char buf[1024];
-				mplp.rghash = bcf_str2id_init();
-				if ((fp_rg = fopen(optarg, "r")) == 0)
-					fprintf(pysamerr, "(%s) Fail to open file %s. Continue anyway.\n", __func__, optarg);
-				while (!feof(fp_rg) && fscanf(fp_rg, "%s", buf) > 0) // this is not a good style, but forgive me...
-					bcf_str2id_add(mplp.rghash, strdup(buf));
-				fclose(fp_rg);
-			}
-			break;
-		}
-	}
-	if (use_orphan) mplp.flag &= ~MPLP_NO_ORPHAN;
-	if (argc == 1) {
-		fprintf(pysamerr, "\n");
-		fprintf(pysamerr, "Usage: samtools mpileup [options] in1.bam [in2.bam [...]]\n\n");
-		fprintf(pysamerr, "Input options:\n\n");
-		fprintf(pysamerr, "       -6           assume the quality is in the Illumina-1.3+ encoding\n");
-		fprintf(pysamerr, "       -A           count anomalous read pairs\n");
-		fprintf(pysamerr, "       -B           disable BAQ computation\n");
-		fprintf(pysamerr, "       -b FILE      list of input BAM filenames, one per line [null]\n");
-		fprintf(pysamerr, "       -C INT       parameter for adjusting mapQ; 0 to disable [0]\n");
-		fprintf(pysamerr, "       -d INT       max per-BAM depth to avoid excessive memory usage [%d]\n", mplp.max_depth);
-		fprintf(pysamerr, "       -E           recalculate extended BAQ on the fly thus ignoring existing BQs\n");
-		fprintf(pysamerr, "       -f FILE      faidx indexed reference sequence file [null]\n");
-		fprintf(pysamerr, "       -G FILE      exclude read groups listed in FILE [null]\n");
-		fprintf(pysamerr, "       -l FILE      list of positions (chr pos) or regions (BED) [null]\n");
-		fprintf(pysamerr, "       -M INT       cap mapping quality at INT [%d]\n", mplp.max_mq);
-		fprintf(pysamerr, "       -r STR       region in which pileup is generated [null]\n");
-		fprintf(pysamerr, "       -R           ignore RG tags\n");
-		fprintf(pysamerr, "       -q INT       skip alignments with mapQ smaller than INT [%d]\n", mplp.min_mq);
-		fprintf(pysamerr, "       -Q INT       skip bases with baseQ/BAQ smaller than INT [%d]\n", mplp.min_baseQ);
-		fprintf(pysamerr, "       --rf INT     required flags: skip reads with mask bits unset []\n");
-		fprintf(pysamerr, "       --ff INT     filter flags: skip reads with mask bits set []\n");
-		fprintf(pysamerr, "\nOutput options:\n\n");
-		fprintf(pysamerr, "       -D           output per-sample DP in BCF (require -g/-u)\n");
-		fprintf(pysamerr, "       -g           generate BCF output (genotype likelihoods)\n");
-		fprintf(pysamerr, "       -O           output base positions on reads (disabled by -g/-u)\n");
-		fprintf(pysamerr, "       -s           output mapping quality (disabled by -g/-u)\n");
-		fprintf(pysamerr, "       -S           output per-sample strand bias P-value in BCF (require -g/-u)\n");
-		fprintf(pysamerr, "       -u           generate uncompress BCF output\n");
-		fprintf(pysamerr, "\nSNP/INDEL genotype likelihoods options (effective with `-g' or `-u'):\n\n");
-		fprintf(pysamerr, "       -e INT       Phred-scaled gap extension seq error probability [%d]\n", mplp.extQ);
-		fprintf(pysamerr, "       -F FLOAT     minimum fraction of gapped reads for candidates [%g]\n", mplp.min_frac);
-		fprintf(pysamerr, "       -h INT       coefficient for homopolymer errors [%d]\n", mplp.tandemQ);
-		fprintf(pysamerr, "       -I           do not perform indel calling\n");
-		fprintf(pysamerr, "       -L INT       max per-sample depth for INDEL calling [%d]\n", mplp.max_indel_depth);
-		fprintf(pysamerr, "       -m INT       minimum gapped reads for indel candidates [%d]\n", mplp.min_support);
-		fprintf(pysamerr, "       -o INT       Phred-scaled gap open sequencing error probability [%d]\n", mplp.openQ);
-		fprintf(pysamerr, "       -p           apply -m and -F per-sample to increase sensitivity\n");
-		fprintf(pysamerr, "       -P STR       comma separated list of platforms for indels [all]\n");
-		fprintf(pysamerr, "\n");
-		fprintf(pysamerr, "Notes: Assuming diploid individuals.\n\n");
-		return 1;
-	}
-	bam_no_B = 1;
+        case 'o': {
+                char *end;
+                long value = strtol(optarg, &end, 10);
+                // Distinguish between -o INT and -o FILE (a bit of a hack!)
+                if (*end == '\0') mplp.openQ = value;
+                else mplp.output_fname = optarg;
+            }
+            break;
+        case 'e': mplp.extQ = atoi(optarg); break;
+        case 'h': mplp.tandemQ = atoi(optarg); break;
+        case 'A': use_orphan = 1; break;
+        case 'F': mplp.min_frac = atof(optarg); break;
+        case 'm': mplp.min_support = atoi(optarg); break;
+        case 'L': mplp.max_indel_depth = atoi(optarg); break;
+        case 'G': {
+                FILE *fp_rg;
+                char buf[1024];
+                mplp.rghash = khash_str2int_init();
+                if ((fp_rg = fopen(optarg, "r")) == 0)
+                    fprintf(pysamerr, "(%s) Fail to open file %s. Continue anyway.\n", __func__, optarg);
+                while (!feof(fp_rg) && fscanf(fp_rg, "%s", buf) > 0) // this is not a good style, but forgive me...
+                    khash_str2int_inc(mplp.rghash, strdup(buf));
+                fclose(fp_rg);
+            }
+            break;
+        case 't': mplp.fmt_flag |= parse_format_flag(optarg); break;
+        default:
+            fprintf(pysamerr,"Invalid option: '%c'\n", c);
+            return 1;
+        }
+    }
+    if ( !(mplp.flag&MPLP_REALN) && mplp.flag&MPLP_REDO_BAQ )
+    {
+        fprintf(pysamerr,"Error: The -B option cannot be combined with -E\n");
+        return 1;
+    }
+    if (use_orphan) mplp.flag &= ~MPLP_NO_ORPHAN;
+    if (argc == 1)
+    {
+        print_usage(pysamerr, &mplp);
+        return 1;
+    }
+    int ret;
     if (file_list) {
         if ( read_file_list(file_list,&nfiles,&fn) ) return 1;
-        mpileup(&mplp,nfiles,fn);
+        ret = mpileup(&mplp,nfiles,fn);
         for (c=0; c<nfiles; c++) free(fn[c]);
         free(fn);
-    } else mpileup(&mplp, argc - optind, argv + optind);
-	if (mplp.rghash) bcf_str2id_thorough_destroy(mplp.rghash);
-	free(mplp.reg); free(mplp.pl_list);
-	if (mplp.fai) fai_destroy(mplp.fai);
-	if (mplp.bed) bed_destroy(mplp.bed);
-	return 0;
+    }
+    else
+        ret = mpileup(&mplp, argc - optind, argv + optind);
+    if (mplp.rghash) khash_str2int_destroy_free(mplp.rghash);
+    free(mplp.reg); free(mplp.pl_list);
+    if (mplp.fai) fai_destroy(mplp.fai);
+    if (mplp.bed) bed_destroy(mplp.bed);
+    return ret;
 }
diff --git a/samtools/bam_reheader.c.pysam.c b/samtools/bam_reheader.c.pysam.c
index dca0c42..3fcafbd 100644
--- a/samtools/bam_reheader.c.pysam.c
+++ b/samtools/bam_reheader.c.pysam.c
@@ -1,64 +1,83 @@
 #include "pysam.h"
 
+/*  bam_reheader.c -- reheader subcommand.
+
+    Copyright (C) 2010 Broad Institute.
+    Copyright (C) 2012, 2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdio.h>
 #include <stdlib.h>
-#include "knetfile.h"
-#include "bgzf.h"
+#include "htslib/bgzf.h"
 #include "bam.h"
 
 #define BUF_SIZE 0x10000
 
 int bam_reheader(BGZF *in, const bam_header_t *h, int fd)
 {
-	BGZF *fp;
-	bam_header_t *old;
-	int len;
-	uint8_t *buf;
-	if (in->is_write) return -1;
-	buf = malloc(BUF_SIZE);
-	old = bam_header_read(in);
-	fp = bgzf_fdopen(fd, "w");
-	bam_header_write(fp, h);
-	if (in->block_offset < in->block_length) {
-		bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);
-		bgzf_flush(fp);
-	}
-#ifdef _USE_KNETFILE
-	while ((len = knet_read(in->fp, buf, BUF_SIZE)) > 0)
-		fwrite(buf, 1, len, fp->fp);
-#else
-	while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0)
-		fwrite(buf, 1, len, fp->file);
-#endif
-	free(buf);
-	fp->block_offset = in->block_offset = 0;
-	bgzf_close(fp);
-	return 0;
+    BGZF *fp;
+    bam_header_t *old;
+    ssize_t len;
+    uint8_t *buf;
+    if (in->is_write) return -1;
+    buf = malloc(BUF_SIZE);
+    old = bam_header_read(in);
+    fp = bgzf_fdopen(fd, "w");
+    bam_header_write(fp, h);
+    if (in->block_offset < in->block_length) {
+        bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);
+        bgzf_flush(fp);
+    }
+    while ((len = bgzf_raw_read(in, buf, BUF_SIZE)) > 0)
+        bgzf_raw_write(fp, buf, len);
+    free(buf);
+    fp->block_offset = in->block_offset = 0;
+    bgzf_close(fp);
+    return 0;
 }
 
 int main_reheader(int argc, char *argv[])
 {
-	bam_header_t *h;
-	BGZF *in;
-	if (argc != 3) {
-		fprintf(pysamerr, "Usage: samtools reheader <in.header.sam> <in.bam>\n");
-		return 1;
-	}
-	{ // read the header
-		tamFile fph = sam_open(argv[1]);
-		if (fph == 0) {
-			fprintf(pysamerr, "[%s] fail to read the header from %s.\n", __func__, argv[1]);
-			return 1;
-		}
-		h = sam_header_read(fph);
-		sam_close(fph);
-	}
-	in = strcmp(argv[2], "-")? bam_open(argv[2], "r") : bam_dopen(fileno(stdin), "r");
-	if (in == 0) {
-		fprintf(pysamerr, "[%s] fail to open file %s.\n", __func__, argv[2]);
-		return 1;
-	}
-	bam_reheader(in, h, fileno(stdout));
-	bgzf_close(in);
-	return 0;
+    bam_header_t *h;
+    BGZF *in;
+    if (argc != 3) {
+        fprintf(pysamerr, "Usage: samtools reheader <in.header.sam> <in.bam>\n");
+        return 1;
+    }
+    { // read the header
+        tamFile fph = sam_open(argv[1]);
+        if (fph == 0) {
+            fprintf(pysamerr, "[%s] fail to read the header from %s.\n", __func__, argv[1]);
+            return 1;
+        }
+        h = sam_header_read(fph);
+        sam_close(fph);
+    }
+    in = strcmp(argv[2], "-")? bgzf_open(argv[2], "r") : bgzf_fdopen(fileno(stdin), "r");
+    if (in == 0) {
+        fprintf(pysamerr, "[%s] fail to open file %s.\n", __func__, argv[2]);
+        return 1;
+    }
+    bam_reheader(in, h, fileno(stdout));
+    bgzf_close(in);
+    return 0;
 }
diff --git a/samtools/bam_rmdup.c.pysam.c b/samtools/bam_rmdup.c.pysam.c
index 2d5a718..e53b065 100644
--- a/samtools/bam_rmdup.c.pysam.c
+++ b/samtools/bam_rmdup.c.pysam.c
@@ -1,5 +1,30 @@
 #include "pysam.h"
 
+/*  bam_rmdup.c -- duplicate read detection.
+
+    Copyright (C) 2009 Genome Research Ltd.
+    Portions copyright (C) 2009 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -9,200 +34,200 @@
 
 typedef bam1_t *bam1_p;
 
-#include "khash.h"
+#include "htslib/khash.h"
 KHASH_SET_INIT_STR(name)
 KHASH_MAP_INIT_INT64(pos, bam1_p)
 
 #define BUFFER_SIZE 0x40000
 
 typedef struct {
-	uint64_t n_checked, n_removed;
-	khash_t(pos) *best_hash;
+    uint64_t n_checked, n_removed;
+    khash_t(pos) *best_hash;
 } lib_aux_t;
 KHASH_MAP_INIT_STR(lib, lib_aux_t)
 
 typedef struct {
-	int n, max;
-	bam1_t **a;
+    int n, max;
+    bam1_t **a;
 } tmp_stack_t;
 
 static inline void stack_insert(tmp_stack_t *stack, bam1_t *b)
 {
-	if (stack->n == stack->max) {
-		stack->max = stack->max? stack->max<<1 : 0x10000;
-		stack->a = (bam1_t**)realloc(stack->a, sizeof(bam1_t*) * stack->max);
-	}
-	stack->a[stack->n++] = b;
+    if (stack->n == stack->max) {
+        stack->max = stack->max? stack->max<<1 : 0x10000;
+        stack->a = (bam1_t**)realloc(stack->a, sizeof(bam1_t*) * stack->max);
+    }
+    stack->a[stack->n++] = b;
 }
 
 static inline void dump_best(tmp_stack_t *stack, samfile_t *out)
 {
-	int i;
-	for (i = 0; i != stack->n; ++i) {
-		samwrite(out, stack->a[i]);
-		bam_destroy1(stack->a[i]);
-	}
-	stack->n = 0;
+    int i;
+    for (i = 0; i != stack->n; ++i) {
+        samwrite(out, stack->a[i]);
+        bam_destroy1(stack->a[i]);
+    }
+    stack->n = 0;
 }
 
 static void clear_del_set(khash_t(name) *del_set)
 {
-	khint_t k;
-	for (k = kh_begin(del_set); k < kh_end(del_set); ++k)
-		if (kh_exist(del_set, k))
-			free((char*)kh_key(del_set, k));
-	kh_clear(name, del_set);
+    khint_t k;
+    for (k = kh_begin(del_set); k < kh_end(del_set); ++k)
+        if (kh_exist(del_set, k))
+            free((char*)kh_key(del_set, k));
+    kh_clear(name, del_set);
 }
 
 static lib_aux_t *get_aux(khash_t(lib) *aux, const char *lib)
 {
-	khint_t k = kh_get(lib, aux, lib);
-	if (k == kh_end(aux)) {
-		int ret;
-		char *p = strdup(lib);
-		lib_aux_t *q;
-		k = kh_put(lib, aux, p, &ret);
-		q = &kh_val(aux, k);
-		q->n_checked = q->n_removed = 0;
-		q->best_hash = kh_init(pos);
-		return q;
-	} else return &kh_val(aux, k);
+    khint_t k = kh_get(lib, aux, lib);
+    if (k == kh_end(aux)) {
+        int ret;
+        char *p = strdup(lib);
+        lib_aux_t *q;
+        k = kh_put(lib, aux, p, &ret);
+        q = &kh_val(aux, k);
+        q->n_checked = q->n_removed = 0;
+        q->best_hash = kh_init(pos);
+        return q;
+    } else return &kh_val(aux, k);
 }
 
 static void clear_best(khash_t(lib) *aux, int max)
 {
-	khint_t k;
-	for (k = kh_begin(aux); k != kh_end(aux); ++k) {
-		if (kh_exist(aux, k)) {
-			lib_aux_t *q = &kh_val(aux, k);
-			if (kh_size(q->best_hash) >= max)
-				kh_clear(pos, q->best_hash);
-		}
-	}
+    khint_t k;
+    for (k = kh_begin(aux); k != kh_end(aux); ++k) {
+        if (kh_exist(aux, k)) {
+            lib_aux_t *q = &kh_val(aux, k);
+            if (kh_size(q->best_hash) >= max)
+                kh_clear(pos, q->best_hash);
+        }
+    }
 }
 
 static inline int sum_qual(const bam1_t *b)
 {
-	int i, q;
-	uint8_t *qual = bam1_qual(b);
-	for (i = q = 0; i < b->core.l_qseq; ++i) q += qual[i];
-	return q;
+    int i, q;
+    uint8_t *qual = bam1_qual(b);
+    for (i = q = 0; i < b->core.l_qseq; ++i) q += qual[i];
+    return q;
 }
 
 void bam_rmdup_core(samfile_t *in, samfile_t *out)
 {
-	bam1_t *b;
-	int last_tid = -1, last_pos = -1;
-	tmp_stack_t stack;
-	khint_t k;
-	khash_t(lib) *aux;
-	khash_t(name) *del_set;
-	
-	aux = kh_init(lib);
-	del_set = kh_init(name);
-	b = bam_init1();
-	memset(&stack, 0, sizeof(tmp_stack_t));
-
-	kh_resize(name, del_set, 4 * BUFFER_SIZE);
-	while (samread(in, b) >= 0) {
-		bam1_core_t *c = &b->core;
-		if (c->tid != last_tid || last_pos != c->pos) {
-			dump_best(&stack, out); // write the result
-			clear_best(aux, BUFFER_SIZE);
-			if (c->tid != last_tid) {
-				clear_best(aux, 0);
-				if (kh_size(del_set)) { // check
-					fprintf(pysamerr, "[bam_rmdup_core] %llu unmatched pairs\n", (long long)kh_size(del_set));
-					clear_del_set(del_set);
-				}
-				if ((int)c->tid == -1) { // append unmapped reads
-					samwrite(out, b);
-					while (samread(in, b) >= 0) samwrite(out, b);
-					break;
-				}
-				last_tid = c->tid;
-				fprintf(pysamerr, "[bam_rmdup_core] processing reference %s...\n", in->header->target_name[c->tid]);
-			}
-		}
-		if (!(c->flag&BAM_FPAIRED) || (c->flag&(BAM_FUNMAP|BAM_FMUNMAP)) || (c->mtid >= 0 && c->tid != c->mtid)) {
-			samwrite(out, b);
-		} else if (c->isize > 0) { // paired, head
-			uint64_t key = (uint64_t)c->pos<<32 | c->isize;
-			const char *lib;
-			lib_aux_t *q;
-			int ret;
-			lib = bam_get_library(in->header, b);
-			q = lib? get_aux(aux, lib) : get_aux(aux, "\t");
-			++q->n_checked;
-			k = kh_put(pos, q->best_hash, key, &ret);
-			if (ret == 0) { // found in best_hash
-				bam1_t *p = kh_val(q->best_hash, k);
-				++q->n_removed;
-				if (sum_qual(p) < sum_qual(b)) { // the current alignment is better; this can be accelerated in principle
-					kh_put(name, del_set, strdup(bam1_qname(p)), &ret); // p will be removed
-					bam_copy1(p, b); // replaced as b
-				} else kh_put(name, del_set, strdup(bam1_qname(b)), &ret); // b will be removed
-				if (ret == 0)
-					fprintf(pysamerr, "[bam_rmdup_core] inconsistent BAM file for pair '%s'. Continue anyway.\n", bam1_qname(b));
-			} else { // not found in best_hash
-				kh_val(q->best_hash, k) = bam_dup1(b);
-				stack_insert(&stack, kh_val(q->best_hash, k));
-			}
-		} else { // paired, tail
-			k = kh_get(name, del_set, bam1_qname(b));
-			if (k != kh_end(del_set)) {
-				free((char*)kh_key(del_set, k));
-				kh_del(name, del_set, k);
-			} else samwrite(out, b);
-		}
-		last_pos = c->pos;
-	}
-
-	for (k = kh_begin(aux); k != kh_end(aux); ++k) {
-		if (kh_exist(aux, k)) {
-			lib_aux_t *q = &kh_val(aux, k);			
-			dump_best(&stack, out);
-			fprintf(pysamerr, "[bam_rmdup_core] %lld / %lld = %.4lf in library '%s'\n", (long long)q->n_removed,
-					(long long)q->n_checked, (double)q->n_removed/q->n_checked, kh_key(aux, k));
-			kh_destroy(pos, q->best_hash);
-			free((char*)kh_key(aux, k));
-		}
-	}
-	kh_destroy(lib, aux);
-
-	clear_del_set(del_set);
-	kh_destroy(name, del_set);
-	free(stack.a);
-	bam_destroy1(b);
+    bam1_t *b;
+    int last_tid = -1, last_pos = -1;
+    tmp_stack_t stack;
+    khint_t k;
+    khash_t(lib) *aux;
+    khash_t(name) *del_set;
+
+    aux = kh_init(lib);
+    del_set = kh_init(name);
+    b = bam_init1();
+    memset(&stack, 0, sizeof(tmp_stack_t));
+
+    kh_resize(name, del_set, 4 * BUFFER_SIZE);
+    while (samread(in, b) >= 0) {
+        bam1_core_t *c = &b->core;
+        if (c->tid != last_tid || last_pos != c->pos) {
+            dump_best(&stack, out); // write the result
+            clear_best(aux, BUFFER_SIZE);
+            if (c->tid != last_tid) {
+                clear_best(aux, 0);
+                if (kh_size(del_set)) { // check
+                    fprintf(pysamerr, "[bam_rmdup_core] %llu unmatched pairs\n", (long long)kh_size(del_set));
+                    clear_del_set(del_set);
+                }
+                if ((int)c->tid == -1) { // append unmapped reads
+                    samwrite(out, b);
+                    while (samread(in, b) >= 0) samwrite(out, b);
+                    break;
+                }
+                last_tid = c->tid;
+                fprintf(pysamerr, "[bam_rmdup_core] processing reference %s...\n", in->header->target_name[c->tid]);
+            }
+        }
+        if (!(c->flag&BAM_FPAIRED) || (c->flag&(BAM_FUNMAP|BAM_FMUNMAP)) || (c->mtid >= 0 && c->tid != c->mtid)) {
+            samwrite(out, b);
+        } else if (c->isize > 0) { // paired, head
+            uint64_t key = (uint64_t)c->pos<<32 | c->isize;
+            const char *lib;
+            lib_aux_t *q;
+            int ret;
+            lib = bam_get_library(in->header, b);
+            q = lib? get_aux(aux, lib) : get_aux(aux, "\t");
+            ++q->n_checked;
+            k = kh_put(pos, q->best_hash, key, &ret);
+            if (ret == 0) { // found in best_hash
+                bam1_t *p = kh_val(q->best_hash, k);
+                ++q->n_removed;
+                if (sum_qual(p) < sum_qual(b)) { // the current alignment is better; this can be accelerated in principle
+                    kh_put(name, del_set, strdup(bam1_qname(p)), &ret); // p will be removed
+                    bam_copy1(p, b); // replaced as b
+                } else kh_put(name, del_set, strdup(bam1_qname(b)), &ret); // b will be removed
+                if (ret == 0)
+                    fprintf(pysamerr, "[bam_rmdup_core] inconsistent BAM file for pair '%s'. Continue anyway.\n", bam1_qname(b));
+            } else { // not found in best_hash
+                kh_val(q->best_hash, k) = bam_dup1(b);
+                stack_insert(&stack, kh_val(q->best_hash, k));
+            }
+        } else { // paired, tail
+            k = kh_get(name, del_set, bam1_qname(b));
+            if (k != kh_end(del_set)) {
+                free((char*)kh_key(del_set, k));
+                kh_del(name, del_set, k);
+            } else samwrite(out, b);
+        }
+        last_pos = c->pos;
+    }
+
+    for (k = kh_begin(aux); k != kh_end(aux); ++k) {
+        if (kh_exist(aux, k)) {
+            lib_aux_t *q = &kh_val(aux, k);
+            dump_best(&stack, out);
+            fprintf(pysamerr, "[bam_rmdup_core] %lld / %lld = %.4lf in library '%s'\n", (long long)q->n_removed,
+                    (long long)q->n_checked, (double)q->n_removed/q->n_checked, kh_key(aux, k));
+            kh_destroy(pos, q->best_hash);
+            free((char*)kh_key(aux, k));
+        }
+    }
+    kh_destroy(lib, aux);
+
+    clear_del_set(del_set);
+    kh_destroy(name, del_set);
+    free(stack.a);
+    bam_destroy1(b);
 }
 
 void bam_rmdupse_core(samfile_t *in, samfile_t *out, int force_se);
 
 int bam_rmdup(int argc, char *argv[])
 {
-	int c, is_se = 0, force_se = 0;
-	samfile_t *in, *out;
-	while ((c = getopt(argc, argv, "sS")) >= 0) {
-		switch (c) {
-		case 's': is_se = 1; break;
-		case 'S': force_se = is_se = 1; break;
-		}
-	}
-	if (optind + 2 > argc) {
-		fprintf(pysamerr, "\n");
-		fprintf(pysamerr, "Usage:  samtools rmdup [-sS] <input.srt.bam> <output.bam>\n\n");
-		fprintf(pysamerr, "Option: -s    rmdup for SE reads\n");
-		fprintf(pysamerr, "        -S    treat PE reads as SE in rmdup (force -s)\n\n");
-		return 1;
-	}
-	in = samopen(argv[optind], "rb", 0);
-	out = samopen(argv[optind+1], "wb", in->header);
-	if (in == 0 || out == 0) {
-		fprintf(pysamerr, "[bam_rmdup] fail to read/write input files\n");
-		return 1;
-	}
-	if (is_se) bam_rmdupse_core(in, out, force_se);
-	else bam_rmdup_core(in, out);
-	samclose(in); samclose(out);
-	return 0;
+    int c, is_se = 0, force_se = 0;
+    samfile_t *in, *out;
+    while ((c = getopt(argc, argv, "sS")) >= 0) {
+        switch (c) {
+        case 's': is_se = 1; break;
+        case 'S': force_se = is_se = 1; break;
+        }
+    }
+    if (optind + 2 > argc) {
+        fprintf(pysamerr, "\n");
+        fprintf(pysamerr, "Usage:  samtools rmdup [-sS] <input.srt.bam> <output.bam>\n\n");
+        fprintf(pysamerr, "Option: -s    rmdup for SE reads\n");
+        fprintf(pysamerr, "        -S    treat PE reads as SE in rmdup (force -s)\n\n");
+        return 1;
+    }
+    in = samopen(argv[optind], "rb", 0);
+    out = samopen(argv[optind+1], "wb", in->header);
+    if (in == 0 || out == 0) {
+        fprintf(pysamerr, "[bam_rmdup] fail to read/write input files\n");
+        return 1;
+    }
+    if (is_se) bam_rmdupse_core(in, out, force_se);
+    else bam_rmdup_core(in, out);
+    samclose(in); samclose(out);
+    return 0;
 }
diff --git a/samtools/bam_rmdupse.c.pysam.c b/samtools/bam_rmdupse.c.pysam.c
index f2cd8b7..31adc67 100644
--- a/samtools/bam_rmdupse.c.pysam.c
+++ b/samtools/bam_rmdupse.c.pysam.c
@@ -1,17 +1,42 @@
 #include "pysam.h"
 
+/*  bam_rmdupse.c -- duplicate read detection for unpaired reads.
+
+    Copyright (C) 2009 Genome Research Ltd.
+    Portions copyright (C) 2009 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <math.h>
 #include "sam.h"
-#include "khash.h"
-#include "klist.h"
+#include "htslib/khash.h"
+#include "htslib/klist.h"
 
 #define QUEUE_CLEAR_SIZE 0x100000
 #define MAX_POS 0x7fffffff
 
 typedef struct {
-	int endpos;
-	uint32_t score:31, discarded:1;
-	bam1_t *b;
+    int endpos;
+    uint32_t score:31, discarded:1;
+    bam1_t *b;
 } elem_t, *elem_p;
 #define __free_elem(p) bam_destroy1((p)->data.b)
 KLIST_INIT(q, elem_t, __free_elem)
@@ -21,141 +46,141 @@ KHASH_MAP_INIT_INT(best, elem_p)
 typedef khash_t(best) besthash_t;
 
 typedef struct {
-	uint64_t n_checked, n_removed;
-	besthash_t *left, *rght;
+    uint64_t n_checked, n_removed;
+    besthash_t *left, *rght;
 } lib_aux_t;
 KHASH_MAP_INIT_STR(lib, lib_aux_t)
 
 static lib_aux_t *get_aux(khash_t(lib) *aux, const char *lib)
 {
-	khint_t k = kh_get(lib, aux, lib);
-	if (k == kh_end(aux)) {
-		int ret;
-		char *p = strdup(lib);
-		lib_aux_t *q;
-		k = kh_put(lib, aux, p, &ret);
-		q = &kh_val(aux, k);
-		q->left = kh_init(best);
-		q->rght = kh_init(best);
-		q->n_checked = q->n_removed = 0;
-		return q;
-	} else return &kh_val(aux, k);
+    khint_t k = kh_get(lib, aux, lib);
+    if (k == kh_end(aux)) {
+        int ret;
+        char *p = strdup(lib);
+        lib_aux_t *q;
+        k = kh_put(lib, aux, p, &ret);
+        q = &kh_val(aux, k);
+        q->left = kh_init(best);
+        q->rght = kh_init(best);
+        q->n_checked = q->n_removed = 0;
+        return q;
+    } else return &kh_val(aux, k);
 }
 
 static inline int sum_qual(const bam1_t *b)
 {
-	int i, q;
-	uint8_t *qual = bam1_qual(b);
-	for (i = q = 0; i < b->core.l_qseq; ++i) q += qual[i];
-	return q;
+    int i, q;
+    uint8_t *qual = bam1_qual(b);
+    for (i = q = 0; i < b->core.l_qseq; ++i) q += qual[i];
+    return q;
 }
 
 static inline elem_t *push_queue(queue_t *queue, const bam1_t *b, int endpos, int score)
 {
-	elem_t *p = kl_pushp(q, queue);
-	p->discarded = 0;
-	p->endpos = endpos; p->score = score;
-	if (p->b == 0) p->b = bam_init1();
-	bam_copy1(p->b, b);
-	return p;
+    elem_t *p = kl_pushp(q, queue);
+    p->discarded = 0;
+    p->endpos = endpos; p->score = score;
+    if (p->b == 0) p->b = bam_init1();
+    bam_copy1(p->b, b);
+    return p;
 }
 
 static void clear_besthash(besthash_t *h, int32_t pos)
 {
-	khint_t k;
-	for (k = kh_begin(h); k != kh_end(h); ++k)
-		if (kh_exist(h, k) && kh_val(h, k)->endpos <= pos)
-			kh_del(best, h, k);
+    khint_t k;
+    for (k = kh_begin(h); k != kh_end(h); ++k)
+        if (kh_exist(h, k) && kh_val(h, k)->endpos <= pos)
+            kh_del(best, h, k);
 }
 
 static void dump_alignment(samfile_t *out, queue_t *queue, int32_t pos, khash_t(lib) *h)
 {
-	if (queue->size > QUEUE_CLEAR_SIZE || pos == MAX_POS) {
-		khint_t k;
-		while (1) {
-			elem_t *q;
-			if (queue->head == queue->tail) break;
-			q = &kl_val(queue->head);
-			if (q->discarded) {
-				q->b->data_len = 0;
-				kl_shift(q, queue, 0);
-				continue;
-			}
-			if ((q->b->core.flag&BAM_FREVERSE) && q->endpos > pos) break;
-			samwrite(out, q->b);
-			q->b->data_len = 0;
-			kl_shift(q, queue, 0);
-		}
-		for (k = kh_begin(h); k != kh_end(h); ++k) {
-			if (kh_exist(h, k)) {
-				clear_besthash(kh_val(h, k).left, pos);
-				clear_besthash(kh_val(h, k).rght, pos);
-			}
-		}
-	}
+    if (queue->size > QUEUE_CLEAR_SIZE || pos == MAX_POS) {
+        khint_t k;
+        while (1) {
+            elem_t *q;
+            if (queue->head == queue->tail) break;
+            q = &kl_val(queue->head);
+            if (q->discarded) {
+                q->b->data_len = 0;
+                kl_shift(q, queue, 0);
+                continue;
+            }
+            if ((q->b->core.flag&BAM_FREVERSE) && q->endpos > pos) break;
+            samwrite(out, q->b);
+            q->b->data_len = 0;
+            kl_shift(q, queue, 0);
+        }
+        for (k = kh_begin(h); k != kh_end(h); ++k) {
+            if (kh_exist(h, k)) {
+                clear_besthash(kh_val(h, k).left, pos);
+                clear_besthash(kh_val(h, k).rght, pos);
+            }
+        }
+    }
 }
 
 void bam_rmdupse_core(samfile_t *in, samfile_t *out, int force_se)
 {
-	bam1_t *b;
-	queue_t *queue;
-	khint_t k;
-	int last_tid = -2;
-	khash_t(lib) *aux;
-
-	aux = kh_init(lib);
-	b = bam_init1();
-	queue = kl_init(q);
-	while (samread(in, b) >= 0) {
-		bam1_core_t *c = &b->core;
-		int endpos = bam_calend(c, bam1_cigar(b));
-		int score = sum_qual(b);
-		
-		if (last_tid != c->tid) {
-			if (last_tid >= 0) dump_alignment(out, queue, MAX_POS, aux);
-			last_tid = c->tid;
-		} else dump_alignment(out, queue, c->pos, aux);
-		if ((c->flag&BAM_FUNMAP) || ((c->flag&BAM_FPAIRED) && !force_se)) {
-			push_queue(queue, b, endpos, score);
-		} else {
-			const char *lib;
-			lib_aux_t *q;
-			besthash_t *h;
-			uint32_t key;
-			int ret;
-			lib = bam_get_library(in->header, b);
-			q = lib? get_aux(aux, lib) : get_aux(aux, "\t");
-			++q->n_checked;
-			h = (c->flag&BAM_FREVERSE)? q->rght : q->left;
-			key = (c->flag&BAM_FREVERSE)? endpos : c->pos;
-			k = kh_put(best, h, key, &ret);
-			if (ret == 0) { // in the hash table
-				elem_t *p = kh_val(h, k);
-				++q->n_removed;
-				if (p->score < score) {
-					if (c->flag&BAM_FREVERSE) { // mark "discarded" and push the queue
-						p->discarded = 1;
-						kh_val(h, k) = push_queue(queue, b, endpos, score);
-					} else { // replace
-						p->score = score; p->endpos = endpos;
-						bam_copy1(p->b, b);
-					}
-				} // otherwise, discard the alignment
-			} else kh_val(h, k) = push_queue(queue, b, endpos, score);
-		}
-	}
-	dump_alignment(out, queue, MAX_POS, aux);
-
-	for (k = kh_begin(aux); k != kh_end(aux); ++k) {
-		if (kh_exist(aux, k)) {
-			lib_aux_t *q = &kh_val(aux, k);
-			fprintf(pysamerr, "[bam_rmdupse_core] %lld / %lld = %.4lf in library '%s'\n", (long long)q->n_removed,
-					(long long)q->n_checked, (double)q->n_removed/q->n_checked, kh_key(aux, k));
-			kh_destroy(best, q->left); kh_destroy(best, q->rght);
-			free((char*)kh_key(aux, k));
-		}
-	}
-	kh_destroy(lib, aux);
-	bam_destroy1(b);
-	kl_destroy(q, queue);
+    bam1_t *b;
+    queue_t *queue;
+    khint_t k;
+    int last_tid = -2;
+    khash_t(lib) *aux;
+
+    aux = kh_init(lib);
+    b = bam_init1();
+    queue = kl_init(q);
+    while (samread(in, b) >= 0) {
+        bam1_core_t *c = &b->core;
+        int endpos = bam_calend(c, bam1_cigar(b));
+        int score = sum_qual(b);
+
+        if (last_tid != c->tid) {
+            if (last_tid >= 0) dump_alignment(out, queue, MAX_POS, aux);
+            last_tid = c->tid;
+        } else dump_alignment(out, queue, c->pos, aux);
+        if ((c->flag&BAM_FUNMAP) || ((c->flag&BAM_FPAIRED) && !force_se)) {
+            push_queue(queue, b, endpos, score);
+        } else {
+            const char *lib;
+            lib_aux_t *q;
+            besthash_t *h;
+            uint32_t key;
+            int ret;
+            lib = bam_get_library(in->header, b);
+            q = lib? get_aux(aux, lib) : get_aux(aux, "\t");
+            ++q->n_checked;
+            h = (c->flag&BAM_FREVERSE)? q->rght : q->left;
+            key = (c->flag&BAM_FREVERSE)? endpos : c->pos;
+            k = kh_put(best, h, key, &ret);
+            if (ret == 0) { // in the hash table
+                elem_t *p = kh_val(h, k);
+                ++q->n_removed;
+                if (p->score < score) {
+                    if (c->flag&BAM_FREVERSE) { // mark "discarded" and push the queue
+                        p->discarded = 1;
+                        kh_val(h, k) = push_queue(queue, b, endpos, score);
+                    } else { // replace
+                        p->score = score; p->endpos = endpos;
+                        bam_copy1(p->b, b);
+                    }
+                } // otherwise, discard the alignment
+            } else kh_val(h, k) = push_queue(queue, b, endpos, score);
+        }
+    }
+    dump_alignment(out, queue, MAX_POS, aux);
+
+    for (k = kh_begin(aux); k != kh_end(aux); ++k) {
+        if (kh_exist(aux, k)) {
+            lib_aux_t *q = &kh_val(aux, k);
+            fprintf(pysamerr, "[bam_rmdupse_core] %lld / %lld = %.4lf in library '%s'\n", (long long)q->n_removed,
+                    (long long)q->n_checked, (double)q->n_removed/q->n_checked, kh_key(aux, k));
+            kh_destroy(best, q->left); kh_destroy(best, q->rght);
+            free((char*)kh_key(aux, k));
+        }
+    }
+    kh_destroy(lib, aux);
+    bam_destroy1(b);
+    kl_destroy(q, queue);
 }
diff --git a/samtools/bam_sort.c.pysam.c b/samtools/bam_sort.c.pysam.c
index 9d86d48..630eb26 100644
--- a/samtools/bam_sort.c.pysam.c
+++ b/samtools/bam_sort.c.pysam.c
@@ -1,316 +1,870 @@
 #include "pysam.h"
 
+/*  bam_sort.c -- sorting and merging.
+
+    Copyright (C) 2008-2014 Genome Research Ltd.
+    Portions copyright (C) 2009-2012 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <stdbool.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
+#include <regex.h>
+#include <time.h>
 #include <unistd.h>
-#include "bam.h"
-#include "ksort.h"
+#include "htslib/ksort.h"
+#include "htslib/khash.h"
+#include "htslib/klist.h"
+#include "htslib/kstring.h"
+#include "htslib/sam.h"
+
+#if !defined(__DARWIN_C_LEVEL) || __DARWIN_C_LEVEL < 900000L
+#define NEED_MEMSET_PATTERN4
+#endif
+
+#ifdef NEED_MEMSET_PATTERN4
+void memset_pattern4(void *target, const void *pattern, size_t size) {
+    uint32_t* target_iter = target;
+    size_t loops = size/4;
+    size_t i;
+    for (i = 0; i < loops; ++i) {
+        memcpy(target_iter, pattern, 4);
+        ++target_iter;
+    }
+    if (size%4 != 0)
+        memcpy(target_iter, pattern, size%4);
+}
+#endif
+
+KHASH_INIT(c2c, char*, char*, 1, kh_str_hash_func, kh_str_hash_equal)
+KHASH_MAP_INIT_STR(c2i, int)
+
+#define __free_char(p)
+KLIST_INIT(hdrln, char*, __free_char)
 
 static int g_is_by_qname = 0;
 
 static int strnum_cmp(const char *_a, const char *_b)
 {
-	const unsigned char *a = (const unsigned char*)_a, *b = (const unsigned char*)_b;
-	const unsigned char *pa = a, *pb = b;
-	while (*pa && *pb) {
-		if (isdigit(*pa) && isdigit(*pb)) {
-			while (*pa == '0') ++pa;
-			while (*pb == '0') ++pb;
-			while (isdigit(*pa) && isdigit(*pb) && *pa == *pb) ++pa, ++pb;
-			if (isdigit(*pa) && isdigit(*pb)) {
-				int i = 0;
-				while (isdigit(pa[i]) && isdigit(pb[i])) ++i;
-				return isdigit(pa[i])? 1 : isdigit(pb[i])? -1 : (int)*pa - (int)*pb;
-			} else if (isdigit(*pa)) return 1;
-			else if (isdigit(*pb)) return -1;
-			else if (pa - a != pb - b) return pa - a < pb - b? 1 : -1;
-		} else {
-			if (*pa != *pb) return (int)*pa - (int)*pb;
-			++pa; ++pb;
-		}
-	}
-	return *pa? 1 : *pb? -1 : 0;
+    const unsigned char *a = (const unsigned char*)_a, *b = (const unsigned char*)_b;
+    const unsigned char *pa = a, *pb = b;
+    while (*pa && *pb) {
+        if (isdigit(*pa) && isdigit(*pb)) {
+            while (*pa == '0') ++pa;
+            while (*pb == '0') ++pb;
+            while (isdigit(*pa) && isdigit(*pb) && *pa == *pb) ++pa, ++pb;
+            if (isdigit(*pa) && isdigit(*pb)) {
+                int i = 0;
+                while (isdigit(pa[i]) && isdigit(pb[i])) ++i;
+                return isdigit(pa[i])? 1 : isdigit(pb[i])? -1 : (int)*pa - (int)*pb;
+            } else if (isdigit(*pa)) return 1;
+            else if (isdigit(*pb)) return -1;
+            else if (pa - a != pb - b) return pa - a < pb - b? 1 : -1;
+        } else {
+            if (*pa != *pb) return (int)*pa - (int)*pb;
+            ++pa; ++pb;
+        }
+    }
+    return *pa? 1 : *pb? -1 : 0;
 }
 
-#define HEAP_EMPTY 0xffffffffffffffffull
+#define HEAP_EMPTY UINT64_MAX
 
 typedef struct {
-	int i;
-	uint64_t pos, idx;
-	bam1_t *b;
+    int i;
+    uint64_t pos, idx;
+    bam1_t *b;
 } heap1_t;
 
 #define __pos_cmp(a, b) ((a).pos > (b).pos || ((a).pos == (b).pos && ((a).i > (b).i || ((a).i == (b).i && (a).idx > (b).idx))))
 
+// Function to compare reads in the heap and determine which one is < the other
 static inline int heap_lt(const heap1_t a, const heap1_t b)
 {
-	if (g_is_by_qname) {
-		int t;
-		if (a.b == 0 || b.b == 0) return a.b == 0? 1 : 0;
-		t = strnum_cmp(bam1_qname(a.b), bam1_qname(b.b));
-		return (t > 0 || (t == 0 && (a.b->core.flag&0xc0) > (b.b->core.flag&0xc0)));
-	} else return __pos_cmp(a, b);
+    if (g_is_by_qname) {
+        int t;
+        if (a.b == NULL || b.b == NULL) return a.b == NULL? 1 : 0;
+        t = strnum_cmp(bam_get_qname(a.b), bam_get_qname(b.b));
+        return (t > 0 || (t == 0 && (a.b->core.flag&0xc0) > (b.b->core.flag&0xc0)));
+    } else return __pos_cmp(a, b);
 }
 
 KSORT_INIT(heap, heap1_t, heap_lt)
 
-static void swap_header_targets(bam_header_t *h1, bam_header_t *h2)
+typedef struct trans_tbl {
+    int32_t n_targets;
+    int* tid_trans;
+    kh_c2c_t* rg_trans;
+    kh_c2c_t* pg_trans;
+    bool lost_coord_sort;
+} trans_tbl_t;
+
+static void trans_tbl_destroy(trans_tbl_t *tbl) {
+    free(tbl->tid_trans);
+    khiter_t iter;
+    for (iter = kh_begin(tbl->rg_trans); iter != kh_end(tbl->rg_trans); ++iter) {
+        if (kh_exist(tbl->rg_trans, iter)) {
+            free(kh_value(tbl->rg_trans, iter));
+            free(kh_key(tbl->rg_trans, iter));
+        }
+    }
+    for (iter = kh_begin(tbl->pg_trans); iter != kh_end(tbl->pg_trans); ++iter) {
+        if (kh_exist(tbl->pg_trans, iter)) {
+            free(kh_value(tbl->pg_trans, iter));
+            free(kh_key(tbl->pg_trans, iter));
+        }
+    }
+
+    kh_destroy(c2c,tbl->rg_trans);
+    kh_destroy(c2c,tbl->pg_trans);
+}
+
+// Takes in existing header and rewrites it in the usual order HD, SQ, RG, PG CO, other
+static void pretty_header(char** text_in_out, int32_t text_len)
 {
-	bam_header_t t;
-	t.n_targets = h1->n_targets, h1->n_targets = h2->n_targets, h2->n_targets = t.n_targets;
-	t.target_name = h1->target_name, h1->target_name = h2->target_name, h2->target_name = t.target_name;
-	t.target_len = h1->target_len, h1->target_len = h2->target_len, h2->target_len = t.target_len;
+    char* output, *output_pointer;
+    output = output_pointer = (char*)calloc(1,text_len+1);
+    output[text_len] = '\0';
+
+    // Read @HD and write
+    regex_t hd_regex, sq_regex, pg_regex, rg_regex, co_regex, other_regex;
+    regmatch_t matches[1];
+    if (regcomp( &hd_regex, "^@HD.*$", REG_EXTENDED|REG_NEWLINE ))
+        abort();
+    if (regexec( &hd_regex, *text_in_out, 1, &matches[0], 0 ) == 0) {
+        size_t match_size = matches[0].rm_eo - matches[0].rm_so;
+        memcpy(output_pointer, *text_in_out+matches[0].rm_so, match_size);
+        output_pointer[match_size] = '\n';
+        output_pointer += match_size + 1;
+    }
+    regfree(&hd_regex);
+
+    // Read @SQ's and write
+    if (regcomp( &sq_regex, "^@SQ.*$", REG_EXTENDED|REG_NEWLINE )) abort();
+    char* sq_pointer = *text_in_out;
+    while (*text_in_out+text_len > sq_pointer && regexec( &sq_regex, sq_pointer, 1, &matches[0], 0) == 0) {
+        size_t match_size = matches[0].rm_eo - matches[0].rm_so;
+        memcpy(output_pointer, sq_pointer+matches[0].rm_so, match_size);
+        output_pointer[match_size] = '\n';
+        output_pointer += match_size + 1;
+        sq_pointer += matches[0].rm_eo + 1;
+    }
+    regfree(&sq_regex);
+
+    // Read @RG's and write
+    if (regcomp( &rg_regex, "^@RG.*$", REG_EXTENDED|REG_NEWLINE )) abort();
+    char* rg_pointer = *text_in_out;
+    while (*text_in_out+text_len > rg_pointer && regexec( &rg_regex, rg_pointer, 1, &matches[0], 0) == 0) {
+        size_t match_size = matches[0].rm_eo - matches[0].rm_so;
+        memcpy(output_pointer, rg_pointer+matches[0].rm_so, match_size);
+        output_pointer[match_size] = '\n';
+        output_pointer += match_size + 1;
+        rg_pointer += matches[0].rm_eo + 1;
+    }
+    regfree(&rg_regex);
+
+    // Read @PG's and write
+    if (regcomp( &pg_regex, "^@PG.*$", REG_EXTENDED|REG_NEWLINE )) abort();
+    char* pg_pointer = *text_in_out;
+    while (*text_in_out+text_len > pg_pointer && regexec( &pg_regex, pg_pointer, 1, &matches[0], 0) == 0) {
+        size_t match_size = matches[0].rm_eo - matches[0].rm_so;
+        memcpy(output_pointer, pg_pointer+matches[0].rm_so, match_size);
+        output_pointer[match_size] = '\n';
+        output_pointer += match_size + 1;
+        pg_pointer += matches[0].rm_eo + 1;
+    }
+    regfree(&pg_regex);
+
+    // Read @CO's and write
+    if (regcomp( &co_regex, "^@CO.*$", REG_EXTENDED|REG_NEWLINE )) abort();
+    char* co_pointer = *text_in_out;
+    while (*text_in_out+text_len > co_pointer && regexec( &co_regex, co_pointer, 1, &matches[0], 0) == 0) {
+        size_t match_size = matches[0].rm_eo - matches[0].rm_so;
+        memcpy(output_pointer, co_pointer+matches[0].rm_so, match_size);
+        output_pointer[match_size] = '\n';
+        output_pointer += match_size + 1;
+        co_pointer += matches[0].rm_eo + 1;
+    }
+    regfree(&co_regex);
+
+    // Read any other not HD,SQ,RG,PG,CO tags and write
+    if (regcomp( &other_regex, "^@([^HSCPR]|H[^D]|S[^Q]|[PR][^G]|C[^O]).*$", REG_EXTENDED|REG_NEWLINE )) abort();
+    char* other_pointer = *text_in_out;
+    while (*text_in_out+text_len > other_pointer && regexec( &other_regex, other_pointer, 1, &matches[0], 0) == 0) {
+        size_t match_size = matches[0].rm_eo - matches[0].rm_so;
+        memcpy(output_pointer, other_pointer+matches[0].rm_so, match_size);
+        output_pointer[match_size] = '\n';
+        output_pointer += match_size + 1;
+        other_pointer += matches[0].rm_eo + 1;
+    }
+    regfree(&other_regex);
+
+    // Safety check, make sure we copied it all, if we didn't something is wrong with the header
+    if ( output+text_len != output_pointer ) {
+        fprintf(pysamerr, "[pretty_header] invalid header\n");
+        exit(1);
+    }
+    free(*text_in_out);
+    *text_in_out = output;
 }
 
-static void swap_header_text(bam_header_t *h1, bam_header_t *h2)
+static void trans_tbl_init(bam_hdr_t* out, bam_hdr_t* translate, trans_tbl_t* tbl, bool merge_rg, bool merge_pg)
 {
-	int tempi;
-	char *temps;
-	tempi = h1->l_text, h1->l_text = h2->l_text, h2->l_text = tempi;
-	temps = h1->text, h1->text = h2->text, h2->text = temps;
+    // No need to translate header into itself
+    if (out == translate) { merge_rg = merge_pg = true; }
+
+    tbl->n_targets = translate->n_targets;
+    tbl->tid_trans = (int*)calloc(translate->n_targets, sizeof(int));
+    tbl->rg_trans = kh_init(c2c);
+    tbl->pg_trans = kh_init(c2c);
+    if (!tbl->tid_trans || !tbl->rg_trans || !tbl->pg_trans) { perror("out of memory"); exit(-1); }
+
+    int32_t out_len = out->l_text;
+    while (out_len > 0 && out->text[out_len-1] == '\n') {--out_len; } // strip trailing \n's
+    kstring_t out_text = { 0, 0, NULL };
+    kputsn(out->text, out_len, &out_text);
+
+    int i, min_tid = -1;
+    tbl->lost_coord_sort = false;
+
+    khash_t(c2i) *out_tid = kh_init(c2i);
+    for (i = 0; i < out->n_targets; ++i) {
+        int ret;
+        khiter_t iter = kh_put(c2i, out_tid, out->target_name[i], &ret);
+        if (ret <= 0) abort();
+        kh_value(out_tid, iter) = i;
+    }
+
+    for (i = 0; i < translate->n_targets; ++i) {
+        khiter_t iter = kh_get(c2i, out_tid, translate->target_name[i]);
+
+        if (iter == kh_end(out_tid)) { // Append missing entries to out
+            tbl->tid_trans[i] = out->n_targets++;
+            out->target_name = (char**)realloc(out->target_name, sizeof(char*)*out->n_targets);
+            out->target_name[out->n_targets-1] = strdup(translate->target_name[i]);
+            out->target_len = (uint32_t*)realloc(out->target_len, sizeof(uint32_t)*out->n_targets);
+            out->target_len[out->n_targets-1] = translate->target_len[i];
+            // grep line with regex '^@SQ.*\tSN:%s(\t.*$|$)', translate->target_name[i]
+            // from translate->text
+            regex_t sq_id;
+            regmatch_t* matches = (regmatch_t*)calloc(2, sizeof(regmatch_t));
+            if (matches == NULL) { perror("out of memory"); exit(-1); }
+            kstring_t seq_regex = { 0, 0, NULL };
+            ksprintf(&seq_regex, "^@SQ.*\tSN:%s(\t.*$|$)", translate->target_name[i]);
+            regcomp(&sq_id, seq_regex.s, REG_EXTENDED|REG_NEWLINE);
+            free(seq_regex.s);
+            if (regexec(&sq_id, translate->text, 1, matches, 0) != 0)
+            {
+                fprintf(pysamerr, "[trans_tbl_init] @SQ SN (%s) found in binary header but not text header.\n",translate->target_name[i]);
+                exit(1);
+            }
+            regfree(&sq_id);
+
+            // Produce our output line and append it to out_text
+            kputc('\n', &out_text);
+            kputsn(translate->text+matches[0].rm_so, matches[0].rm_eo-matches[0].rm_so, &out_text);
+
+            free(matches);
+        } else {
+            tbl->tid_trans[i] = kh_value(out_tid, iter);
+        }
+        if (tbl->tid_trans[i] > min_tid) {
+            min_tid = tbl->tid_trans[i];
+        } else {
+            tbl->lost_coord_sort = true;
+        }
+    }
+    kh_destroy(c2i, out_tid);
+
+    // grep @RG id's
+    regex_t rg_id;
+    regmatch_t* matches = (regmatch_t*)calloc(2, sizeof(regmatch_t));
+    if (matches == NULL) { perror("out of memory"); exit(-1); }
+    regcomp(&rg_id, "^@RG.*\tID:([!-)+-<>-~][ !-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE);
+    char* text = translate->text;
+    klist_t(hdrln) *rg_list = kl_init(hdrln);
+    while(1) { //   foreach rg id in translate's header
+        if (regexec(&rg_id, text, 2, matches, 0) != 0) break;
+        // matches[0] is the whole @RG line; matches[1] is the ID field value
+        kstring_t match_id = { 0, 0, NULL };
+        kputsn(text+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &match_id);
+
+        // is our matched ID in our output list already
+        regex_t rg_id_search;
+        kstring_t rg_regex = { 0, 0, NULL };
+        ksprintf(&rg_regex, "^@RG.*\tID:%s(\t.*$|$)", match_id.s);
+        regcomp(&rg_id_search, rg_regex.s, REG_EXTENDED|REG_NEWLINE|REG_NOSUB);
+        free(rg_regex.s);
+        kstring_t transformed_id = { 0, 0, NULL };
+        bool transformed_equals_match;
+        if (regexec(&rg_id_search, out->text, 0, NULL, 0) != 0  || merge_rg) {
+            // Not in there so can add it as 1-1 mapping
+            kputs(match_id.s, &transformed_id);
+            transformed_equals_match = true;
+        } else {
+            // It's in there so we need to transform it by appending random number to id
+            ksprintf(&transformed_id, "%s-%0lX", match_id.s, lrand48());
+            transformed_equals_match = false;
+        }
+        regfree(&rg_id_search);
+
+        // Insert it into our translation map
+        int in_there = 0;
+        khiter_t iter = kh_put(c2c, tbl->rg_trans, ks_release(&match_id), &in_there);
+        char *transformed_id_s = ks_release(&transformed_id);
+        kh_value(tbl->rg_trans,iter) = transformed_id_s;
+        // take matched line and replace ID with transformed_id
+        kstring_t transformed_line = { 0, 0, NULL };
+        if (transformed_equals_match) {
+            kputsn(text+matches[0].rm_so, matches[0].rm_eo-matches[0].rm_so, &transformed_line);
+        } else {
+            kputsn(text+matches[0].rm_so, matches[1].rm_so-matches[0].rm_so, &transformed_line);
+            kputs(transformed_id_s, &transformed_line);
+            kputsn(text+matches[1].rm_eo, matches[0].rm_eo-matches[1].rm_eo, &transformed_line);
+        }
+
+        if (!(transformed_equals_match && merge_rg)) {
+            // append line to linked list for PG processing
+            char** ln = kl_pushp(hdrln, rg_list);
+            *ln = ks_release(&transformed_line);  // Give away to linked list
+        }
+        else free(transformed_line.s);
+
+        text += matches[0].rm_eo; // next!
+    }
+    regfree(&rg_id);
+
+    // Do same for PG id's
+    regex_t pg_id;
+    regcomp(&pg_id, "^@PG.*\tID:([!-)+-<>-~][ !-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE);
+    text = translate->text;
+    klist_t(hdrln) *pg_list = kl_init(hdrln);
+    while(1) { //   foreach pg id in translate's header
+        if (regexec(&pg_id, text, 2, matches, 0) != 0) break;
+        kstring_t match_id = { 0, 0, NULL };
+        kputsn(text+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &match_id);
+
+        // is our matched ID in our output list already
+        regex_t pg_id_search;
+        kstring_t pg_regex = { 0, 0, NULL };
+        ksprintf(&pg_regex, "^@PG.*\tID:%s(\t.*$|$)", match_id.s);
+        regcomp(&pg_id_search, pg_regex.s, REG_EXTENDED|REG_NEWLINE|REG_NOSUB);
+        free(pg_regex.s);
+        kstring_t transformed_id = { 0, 0, NULL };
+        bool transformed_equals_match;
+        if (regexec(&pg_id_search, out->text, 0, NULL, 0) != 0 || merge_pg) {
+            // Not in there so can add it as 1-1 mapping
+            kputs(match_id.s, &transformed_id);
+            transformed_equals_match = true;
+        } else {
+            // It's in there so we need to transform it by appending random number to id
+            ksprintf(&transformed_id, "%s-%0lX", match_id.s, lrand48());
+            transformed_equals_match = false;
+        }
+        regfree(&pg_id_search);
+
+        // Insert it into our translation map
+        int in_there = 0;
+        khiter_t iter = kh_put(c2c, tbl->pg_trans, ks_release(&match_id), &in_there);
+        char *transformed_id_s = ks_release(&transformed_id);
+        kh_value(tbl->pg_trans,iter) = transformed_id_s;
+        // take matched line and replace ID with transformed_id
+        kstring_t transformed_line = { 0, 0, NULL };
+        if (transformed_equals_match) {
+            kputsn(text+matches[0].rm_so, matches[0].rm_eo-matches[0].rm_so, &transformed_line);
+        } else {
+            kputsn(text+matches[0].rm_so, matches[1].rm_so-matches[0].rm_so, &transformed_line);
+            kputs(transformed_id_s, &transformed_line);
+            kputsn(text+matches[1].rm_eo, matches[0].rm_eo-matches[1].rm_eo, &transformed_line);
+        }
+
+        if (!(transformed_equals_match && merge_pg)) {
+            // append line to linked list for PP processing
+            char** ln = kl_pushp(hdrln, pg_list);
+            *ln = ks_release(&transformed_line);  // Give away to linked list
+        }
+        else free(transformed_line.s);
+        text += matches[0].rm_eo; // next!
+    }
+    regfree(&pg_id);
+    // need to translate PP's on the fly in second pass because they may not be in correct order and need complete tbl->pg_trans to do this
+    // for each line {
+    // with ID replaced with tranformed_id and PP's transformed using the translation table
+    // }
+    regex_t pg_pp;
+    regcomp(&pg_pp, "^@PG.*\tPP:([!-)+-<>-~][!-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE);
+    kliter_t(hdrln) *iter = kl_begin(pg_list);
+    while (iter != kl_end(pg_list)) {
+        char* data = kl_val(iter);
+
+        kstring_t transformed_line = { 0, 0, NULL };
+        // Find PP tag
+        if (regexec(&pg_pp, data, 2, matches, 0) == 0) {
+            // Lookup in hash table
+            kstring_t pp_id = { 0, 0, NULL };
+            kputsn(data+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &pp_id);
+
+            khiter_t k = kh_get(c2c, tbl->pg_trans, pp_id.s);
+            free(pp_id.s);
+            char* transformed_id = kh_value(tbl->pg_trans,k);
+            // Replace
+            kputsn(data, matches[1].rm_so-matches[0].rm_so, &transformed_line);
+            kputs(transformed_id, &transformed_line);
+            kputsn(data+matches[1].rm_eo, matches[0].rm_eo-matches[1].rm_eo, &transformed_line);
+        } else { kputs(data, &transformed_line); }
+        // Produce our output line and append it to out_text
+        kputc('\n', &out_text);
+        kputsn(transformed_line.s, transformed_line.l, &out_text);
+
+        free(transformed_line.s);
+        free(data);
+        iter = kl_next(iter);
+    }
+    regfree(&pg_pp);
+
+    // Need to also translate @RG PG's on the fly too
+    regex_t rg_pg;
+    regcomp(&rg_pg, "^@RG.*\tPG:([!-)+-<>-~][!-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE);
+    kliter_t(hdrln) *rg_iter = kl_begin(rg_list);
+    while (rg_iter != kl_end(rg_list)) {
+        char* data = kl_val(rg_iter);
+
+        kstring_t transformed_line = { 0, 0, NULL };
+        // Find PG tag
+        if (regexec(&rg_pg, data, 2, matches, 0) == 0) {
+            // Lookup in hash table
+            kstring_t pg_id = { 0, 0, NULL };
+            kputsn(data+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &pg_id);
+
+            khiter_t k = kh_get(c2c, tbl->pg_trans, pg_id.s);
+            free(pg_id.s);
+            char* transformed_id = kh_value(tbl->pg_trans,k);
+            // Replace
+            kputsn(data, matches[1].rm_so-matches[0].rm_so, &transformed_line);
+            kputs(transformed_id, &transformed_line);
+            kputsn(data+matches[1].rm_eo, matches[0].rm_eo-matches[1].rm_eo, &transformed_line);
+        } else { kputs(data, &transformed_line); }
+        // Produce our output line and append it to out_text
+        kputc('\n', &out_text);
+        kputsn(transformed_line.s, transformed_line.l, &out_text);
+
+        free(transformed_line.s);
+        free(data);
+        rg_iter = kl_next(rg_iter);
+    }
+
+    regfree(&rg_pg);
+    kl_destroy(hdrln,pg_list);
+    kl_destroy(hdrln,rg_list);
+    free(matches);
+
+    // Add trailing \n and write back to header
+    free(out->text);
+    kputc('\n', &out_text);
+    out->l_text = out_text.l;
+    out->text = ks_release(&out_text);
+}
+
+static void bam_translate(bam1_t* b, trans_tbl_t* tbl)
+{
+    // Update target id if not unmapped tid
+    if ( b->core.tid >= 0 ) { b->core.tid = tbl->tid_trans[b->core.tid]; }
+    if ( b->core.mtid >= 0 ) { b->core.mtid = tbl->tid_trans[b->core.mtid]; }
+
+    // If we have a RG update it
+    uint8_t *rg = bam_aux_get(b, "RG");
+    if (rg) {
+        char* decoded_rg = bam_aux2Z(rg);
+        khiter_t k = kh_get(c2c, tbl->rg_trans, decoded_rg);
+        if (k != kh_end(tbl->rg_trans)) {
+            char* translate_rg = kh_value(tbl->rg_trans,k);
+            bam_aux_del(b, rg);
+            bam_aux_append(b, "RG", 'Z', strlen(translate_rg) + 1, (uint8_t*)translate_rg);
+        } else {
+            fprintf(pysamerr, "[bam_translate] RG tag \"%s\" on read \"%s\" encountered with no corresponding entry in header, tag lost\n",decoded_rg, bam_get_qname(b));
+            bam_aux_del(b, rg);
+        }
+    }
+
+    // If we have a PG update it
+    uint8_t *pg = bam_aux_get(b, "PG");
+    if (pg) {
+        char* decoded_pg = bam_aux2Z(pg);
+        khiter_t k = kh_get(c2c, tbl->pg_trans, decoded_pg);
+        if (k != kh_end(tbl->pg_trans)) {
+            char* translate_pg = kh_value(tbl->pg_trans,k);
+            bam_aux_del(b, pg);
+            bam_aux_append(b, "PG", 'Z', strlen(translate_pg) + 1, (uint8_t*)translate_pg);
+        } else {
+            fprintf(pysamerr, "[bam_translate] PG tag \"%s\" on read \"%s\" encountered with no corresponding entry in header, tag lost\n",decoded_pg, bam_get_qname(b));
+            bam_aux_del(b, pg);
+        }
+    }
+}
+
+int* rtrans_build(int n, int n_targets, trans_tbl_t* translation_tbl)
+{
+    // Create reverse translation table for tids
+    int* rtrans = (int*)malloc(sizeof(int32_t)*n*n_targets);
+    const int32_t NOTID = INT32_MIN;
+    memset_pattern4((void*)rtrans, &NOTID, sizeof(int32_t)*n*n_targets);
+    int i;
+    for (i = 0; i < n; ++i) {
+        int j;
+        for (j = 0; j < (translation_tbl+i)->n_targets; ++j) {
+            if ((translation_tbl+i)->tid_trans[j] != -1) {
+                rtrans[i*n_targets + (translation_tbl+i)->tid_trans[j]] = j;
+            }
+        }
+    }
+
+    return rtrans;
 }
 
-#define MERGE_RG     1
-#define MERGE_UNCOMP 2
-#define MERGE_LEVEL1 4
-#define MERGE_FORCE  8
+#define MERGE_RG          1 // Attach RG tag based on filename
+#define MERGE_UNCOMP      2 // Generate uncompressed BAM
+#define MERGE_LEVEL1      4 // Compress the BAM at level 1 (fast) mode
+#define MERGE_FORCE       8 // Overwrite output BAM if it exists
+#define MERGE_COMBINE_RG 16 // Combine RG tags frather than redefining them
+#define MERGE_COMBINE_PG 32 // Combine PG tags frather than redefining them
+
+/*
+ * How merging is handled
+ *
+ * If a hheader is defined use we will use that as our output header
+ * otherwise we use the first header from the first input file.
+ *
+ * Now go through each file and create a translation table for that file for:
+ * -RG
+ * -tid
+ * -PG tags
+ *
+ * Then whenever we read a record from a bam we translate that read before
+ * stashing it in the hash.
+ *
+ * In the actual merge, a read is read from each input file, translated and
+ * stashed in the hash. This assumes that all input files are sorted in the
+ * same way.  Next we just extract the next position ordered read from the
+ * hash, and replace it if there are still reads left in it's source input
+ * file. Finally we write our chosen read it to the output file.
+ */
 
 /*!
   @abstract    Merge multiple sorted BAM.
   @param  is_by_qname whether to sort by query name
-  @param  out  output BAM file name
-  @param  headers  name of SAM file from which to copy '@' header lines,
-                   or NULL to copy them from the first file to be merged
-  @param  n    number of files to be merged
-  @param  fn   names of files to be merged
-
+  @param  out         output BAM file name
+  @param  mode        sam_open() mode to be used to create the final output file
+                      (overrides level settings from UNCOMP and LEVEL1 flags)
+  @param  headers     name of SAM file from which to copy '@' header lines,
+                      or NULL to copy them from the first file to be merged
+  @param  n           number of files to be merged
+  @param  fn          names of files to be merged
+  @param  flag        flags that control how the merge is undertaken
+  @param  reg         region to merge
+  @param  n_threads   number of threads to use (passed to htslib)
   @discussion Padding information may NOT correctly maintained. This
   function is NOT thread safe.
  */
-int bam_merge_core2(int by_qname, const char *out, const char *headers, int n, char * const *fn, int flag, const char *reg, int n_threads, int level)
+int bam_merge_core2(int by_qname, const char *out, const char *mode, const char *headers, int n, char * const *fn, int flag, const char *reg, int n_threads)
 {
-	bamFile fpout, *fp;
-	heap1_t *heap;
-	bam_header_t *hout = 0;
-	bam_header_t *hheaders = NULL;
-	int i, j, *RG_len = 0;
-	uint64_t idx = 0;
-	char **RG = 0, mode[8];
-	bam_iter_t *iter = 0;
-
-	if (headers) {
-		tamFile fpheaders = sam_open(headers);
-		if (fpheaders == 0) {
-			const char *message = strerror(errno);
-			fprintf(pysamerr, "[bam_merge_core] cannot open '%s': %s\n", headers, message);
-			return -1;
-		}
-		hheaders = sam_header_read(fpheaders);
-		sam_close(fpheaders);
-	}
-
-	g_is_by_qname = by_qname;
-	fp = (bamFile*)calloc(n, sizeof(bamFile));
-	heap = (heap1_t*)calloc(n, sizeof(heap1_t));
-	iter = (bam_iter_t*)calloc(n, sizeof(bam_iter_t));
-	// prepare RG tag
-	if (flag & MERGE_RG) {
-		RG = (char**)calloc(n, sizeof(void*));
-		RG_len = (int*)calloc(n, sizeof(int));
-		for (i = 0; i != n; ++i) {
-			int l = strlen(fn[i]);
-			const char *s = fn[i];
-			if (l > 4 && strcmp(s + l - 4, ".bam") == 0) l -= 4;
-			for (j = l - 1; j >= 0; --j) if (s[j] == '/') break;
-			++j; l -= j;
-			RG[i] = calloc(l + 1, 1);
-			RG_len[i] = l;
-			strncpy(RG[i], s + j, l);
-		}
-	}
-	// read the first
-	for (i = 0; i != n; ++i) {
-		bam_header_t *hin;
-		fp[i] = bam_open(fn[i], "r");
-		if (fp[i] == 0) {
-			int j;
-			fprintf(pysamerr, "[bam_merge_core] fail to open file %s\n", fn[i]);
-			for (j = 0; j < i; ++j) bam_close(fp[j]);
-			free(fp); free(heap);
-			// FIXME: possible memory leak
-			return -1;
-		}
-		hin = bam_header_read(fp[i]);
-		if (i == 0) { // the first BAM
-			hout = hin;
-		} else { // validate multiple baf
-			int min_n_targets = hout->n_targets;
-			if (hin->n_targets < min_n_targets) min_n_targets = hin->n_targets;
-
-			for (j = 0; j < min_n_targets; ++j)
-				if (strcmp(hout->target_name[j], hin->target_name[j]) != 0) {
-					fprintf(pysamerr, "[bam_merge_core] different target sequence name: '%s' != '%s' in file '%s'\n",
-							hout->target_name[j], hin->target_name[j], fn[i]);
-					return -1;
-				}
-
-			// If this input file has additional target reference sequences,
-			// add them to the headers to be output
-			if (hin->n_targets > hout->n_targets) {
-				swap_header_targets(hout, hin);
-				// FIXME Possibly we should also create @SQ text headers
-				// for the newly added reference sequences
-			}
-
-			bam_header_destroy(hin);
-		}
-	}
-
-	if (hheaders) {
-		// If the text headers to be swapped in include any @SQ headers,
-		// check that they are consistent with the existing binary list
-		// of reference information.
-		if (hheaders->n_targets > 0) {
-			if (hout->n_targets != hheaders->n_targets) {
-				fprintf(pysamerr, "[bam_merge_core] number of @SQ headers in '%s' differs from number of target sequences\n", headers);
-				if (!reg) return -1;
-			}
-			for (j = 0; j < hout->n_targets; ++j)
-				if (strcmp(hout->target_name[j], hheaders->target_name[j]) != 0) {
-					fprintf(pysamerr, "[bam_merge_core] @SQ header '%s' in '%s' differs from target sequence\n", hheaders->target_name[j], headers);
-					if (!reg) return -1;
-				}
-		}
-
-		swap_header_text(hout, hheaders);
-		bam_header_destroy(hheaders);
-	}
-
-	if (reg) {
-		int tid, beg, end;
-		if (bam_parse_region(hout, reg, &tid, &beg, &end) < 0) {
-			fprintf(pysamerr, "[%s] Malformated region string or undefined reference name\n", __func__);
-			return -1;
-		}
-		for (i = 0; i < n; ++i) {
-			bam_index_t *idx;
-			idx = bam_index_load(fn[i]);
-			iter[i] = bam_iter_query(idx, tid, beg, end);
-			bam_index_destroy(idx);
-		}
-	}
-
-	for (i = 0; i < n; ++i) {
-		heap1_t *h = heap + i;
-		h->i = i;
-		h->b = (bam1_t*)calloc(1, sizeof(bam1_t));
-		if (bam_iter_read(fp[i], iter[i], h->b) >= 0) {
-			h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)((int32_t)h->b->core.pos+1)<<1 | bam1_strand(h->b);
-			h->idx = idx++;
-		}
-		else h->pos = HEAP_EMPTY;
-	}
-	if (flag & MERGE_UNCOMP) level = 0;
-	else if (flag & MERGE_LEVEL1) level = 1;
-	strcpy(mode, "w");
-	if (level >= 0) sprintf(mode + 1, "%d", level < 9? level : 9);
-	if ((fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w")) == 0) {
-		fprintf(pysamerr, "[%s] fail to create the output file.\n", __func__);
-		return -1;
-	}
-	bam_header_write(fpout, hout);
-	bam_header_destroy(hout);
-	if (!(flag & MERGE_UNCOMP)) bgzf_mt(fpout, n_threads, 256);
-
-	ks_heapmake(heap, n, heap);
-	while (heap->pos != HEAP_EMPTY) {
-		bam1_t *b = heap->b;
-		if (flag & MERGE_RG) {
-			uint8_t *rg = bam_aux_get(b, "RG");
-			if (rg) bam_aux_del(b, rg);
-			bam_aux_append(b, "RG", 'Z', RG_len[heap->i] + 1, (uint8_t*)RG[heap->i]);
-		}
-		bam_write1_core(fpout, &b->core, b->data_len, b->data);
-		if ((j = bam_iter_read(fp[heap->i], iter[heap->i], b)) >= 0) {
-			heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)((int)b->core.pos+1)<<1 | bam1_strand(b);
-			heap->idx = idx++;
-		} else if (j == -1) {
-			heap->pos = HEAP_EMPTY;
-			free(heap->b->data); free(heap->b);
-			heap->b = 0;
-		} else fprintf(pysamerr, "[bam_merge_core] '%s' is truncated. Continue anyway.\n", fn[heap->i]);
-		ks_heapadjust(heap, 0, n, heap);
-	}
-
-	if (flag & MERGE_RG) {
-		for (i = 0; i != n; ++i) free(RG[i]);
-		free(RG); free(RG_len);
-	}
-	for (i = 0; i != n; ++i) {
-		bam_iter_destroy(iter[i]);
-		bam_close(fp[i]);
-	}
-	bam_close(fpout);
-	free(fp); free(heap); free(iter);
-	return 0;
+    samFile *fpout, **fp;
+    heap1_t *heap;
+    bam_hdr_t *hout = NULL;
+    int i, j, *RG_len = NULL;
+    uint64_t idx = 0;
+    char **RG = NULL;
+    hts_itr_t **iter = NULL;
+    trans_tbl_t *translation_tbl = NULL;
+
+    // Is there a specified pre-prepared header to use for output?
+    if (headers) {
+        samFile* fpheaders = sam_open(headers, "r");
+        if (fpheaders == NULL) {
+            const char *message = strerror(errno);
+            fprintf(pysamerr, "[bam_merge_core] cannot open '%s': %s\n", headers, message);
+            return -1;
+        }
+        hout = sam_hdr_read(fpheaders);
+        sam_close(fpheaders);
+    }
+
+    g_is_by_qname = by_qname;
+    fp = (samFile**)calloc(n, sizeof(samFile*));
+    heap = (heap1_t*)calloc(n, sizeof(heap1_t));
+    iter = (hts_itr_t**)calloc(n, sizeof(hts_itr_t*));
+    translation_tbl = (trans_tbl_t*)calloc(n, sizeof(trans_tbl_t));
+    // prepare RG tag from file names
+    if (flag & MERGE_RG) {
+        RG = (char**)calloc(n, sizeof(char*));
+        RG_len = (int*)calloc(n, sizeof(int));
+        for (i = 0; i != n; ++i) {
+            int l = strlen(fn[i]);
+            const char *s = fn[i];
+            if (l > 4 && strcmp(s + l - 4, ".bam") == 0) l -= 4;
+            for (j = l - 1; j >= 0; --j) if (s[j] == '/') break;
+            ++j; l -= j;
+            RG[i] = (char*)calloc(l + 1, 1);
+            RG_len[i] = l;
+            strncpy(RG[i], s + j, l);
+        }
+    }
+    // open and read the header from each file
+    for (i = 0; i < n; ++i) {
+        bam_hdr_t *hin;
+        fp[i] = sam_open(fn[i], "r");
+        if (fp[i] == NULL) {
+            int j;
+            fprintf(pysamerr, "[bam_merge_core] fail to open file %s\n", fn[i]);
+            for (j = 0; j < i; ++j) sam_close(fp[j]);
+            free(fp); free(heap);
+            // FIXME: possible memory leak
+            return -1;
+        }
+        hin = sam_hdr_read(fp[i]);
+        if (hout == NULL) hout = hin;
+        trans_tbl_init(hout, hin, translation_tbl+i, flag & MERGE_COMBINE_RG, flag & MERGE_COMBINE_PG);
+        if (hin != hout) bam_hdr_destroy(hin);
+        if ((translation_tbl+i)->lost_coord_sort && !by_qname) {
+            fprintf(pysamerr, "[bam_merge_core] Order of targets in file %s caused coordinate sort to be lost\n", fn[i]);
+        }
+    }
+
+    // Transform the header into standard form
+    pretty_header(&hout->text,hout->l_text);
+
+    // If we're only merging a specified region move our iters to start at that point
+    if (reg) {
+        int* rtrans = rtrans_build(n, hout->n_targets, translation_tbl);
+
+        int tid, beg, end;
+        const char *name_lim = hts_parse_reg(reg, &beg, &end);
+        char *name = malloc(name_lim - reg + 1);
+        memcpy(name, reg, name_lim - reg);
+        name[name_lim - reg] = '\0';
+        tid = bam_name2id(hout, name);
+        free(name);
+        if (tid < 0) {
+            fprintf(pysamerr, "[%s] Malformated region string or undefined reference name\n", __func__);
+            return -1;
+        }
+        for (i = 0; i < n; ++i) {
+            hts_idx_t *idx = sam_index_load(fp[i], fn[i]);
+            // (rtrans[i*n+tid]) Look up what hout tid translates to in input tid space
+            int mapped_tid = rtrans[i*hout->n_targets+tid];
+            if (mapped_tid != INT32_MIN) {
+                iter[i] = sam_itr_queryi(idx, mapped_tid, beg, end);
+            } else {
+                iter[i] = sam_itr_queryi(idx, HTS_IDX_NONE, 0, 0);
+            }
+            hts_idx_destroy(idx);
+        }
+        free(rtrans);
+    } else {
+        for (i = 0; i < n; ++i) {
+            iter[i] = sam_itr_queryi(NULL, HTS_IDX_REST, 0, 0);
+            if (iter[i] == NULL) {
+                fprintf(pysamerr, "[%s] Memory allocation failed\n", __func__);
+                return -1;
+            }
+        }
+    }
+
+    // Load the first read from each file into the heap
+    for (i = 0; i < n; ++i) {
+        heap1_t *h = heap + i;
+        h->i = i;
+        h->b = (bam1_t*)calloc(1, sizeof(bam1_t));
+        if (sam_itr_next(fp[i], iter[i], h->b) >= 0) {
+            bam_translate(h->b, translation_tbl + i);
+            h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)((int32_t)h->b->core.pos+1)<<1 | bam_is_rev(h->b);
+            h->idx = idx++;
+        }
+        else {
+            h->pos = HEAP_EMPTY;
+        }
+    }
+
+    // Open output file and write header
+    if ((fpout = sam_open(out, mode)) == 0) {
+        fprintf(pysamerr, "[%s] fail to create the output file.\n", __func__);
+        return -1;
+    }
+    sam_hdr_write(fpout, hout);
+    if (!(flag & MERGE_UNCOMP)) hts_set_threads(fpout, n_threads);
+
+    // Begin the actual merge
+    ks_heapmake(heap, n, heap);
+    while (heap->pos != HEAP_EMPTY) {
+        bam1_t *b = heap->b;
+        if (flag & MERGE_RG) {
+            uint8_t *rg = bam_aux_get(b, "RG");
+            if (rg) bam_aux_del(b, rg);
+            bam_aux_append(b, "RG", 'Z', RG_len[heap->i] + 1, (uint8_t*)RG[heap->i]);
+        }
+        sam_write1(fpout, hout, b);
+        if ((j = sam_itr_next(fp[heap->i], iter[heap->i], b)) >= 0) {
+            bam_translate(b, translation_tbl + heap->i);
+            heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)((int)b->core.pos+1)<<1 | bam_is_rev(b);
+            heap->idx = idx++;
+        } else if (j == -1) {
+            heap->pos = HEAP_EMPTY;
+            free(heap->b->data); free(heap->b);
+            heap->b = NULL;
+        } else fprintf(pysamerr, "[bam_merge_core] '%s' is truncated. Continue anyway.\n", fn[heap->i]);
+        ks_heapadjust(heap, 0, n, heap);
+    }
+
+    // Clean up and close
+    if (flag & MERGE_RG) {
+        for (i = 0; i != n; ++i) free(RG[i]);
+        free(RG); free(RG_len);
+    }
+    for (i = 0; i < n; ++i) {
+        trans_tbl_destroy(translation_tbl + i);
+        hts_itr_destroy(iter[i]);
+        sam_close(fp[i]);
+    }
+    bam_hdr_destroy(hout);
+    sam_close(fpout);
+    free(translation_tbl); free(fp); free(heap); free(iter);
+    return 0;
 }
 
 int bam_merge_core(int by_qname, const char *out, const char *headers, int n, char * const *fn, int flag, const char *reg)
 {
-	return bam_merge_core2(by_qname, out, headers, n, fn, flag, reg, 0, -1);
+    char mode[12];
+    strcpy(mode, "wb");
+    if (flag & MERGE_UNCOMP) strcat(mode, "0");
+    else if (flag & MERGE_LEVEL1) strcat(mode, "1");
+    return bam_merge_core2(by_qname, out, mode, headers, n, fn, flag, reg, 0);
+}
+
+static void merge_usage(FILE *to)
+{
+    fprintf(to, "Usage:   samtools merge [-nurlf] [-h inh.sam] [-b <bamlist.fofn>] <out.bam> <in1.bam> <in2.bam> [<in3.bam> ... <inN.bam>]\n\n");
+    fprintf(to, "Options: -n       sort by read names\n");
+    fprintf(to, "         -r       attach RG tag (inferred from file names)\n");
+    fprintf(to, "         -u       uncompressed BAM output\n");
+    fprintf(to, "         -f       overwrite the output BAM if exist\n");
+    fprintf(to, "         -1       compress level 1\n");
+    fprintf(to, "         -l INT   compression level, from 0 to 9 [-1]\n");
+    fprintf(to, "         -@ INT   number of BAM compression threads [0]\n");
+    fprintf(to, "         -R STR   merge file in the specified region STR [all]\n");
+    fprintf(to, "         -h FILE  copy the header in FILE to <out.bam> [in1.bam]\n");
+    fprintf(to, "         -c       combine RG tags with colliding IDs rather than amending them\n");
+    fprintf(to, "         -p       combine PG tags with colliding IDs rather than amending them\n");
+    fprintf(to, "         -s VALUE override random seed\n");
+    fprintf(to, "         -b FILE  list of input BAM filenames, one per line [null]\n\n");
 }
 
 int bam_merge(int argc, char *argv[])
 {
-	int c, is_by_qname = 0, flag = 0, ret = 0, n_threads = 0, level = -1;
-	char *fn_headers = NULL, *reg = 0;
-
-	while ((c = getopt(argc, argv, "h:nru1R:f@:l:")) >= 0) {
-		switch (c) {
-		case 'r': flag |= MERGE_RG; break;
-		case 'f': flag |= MERGE_FORCE; break;
-		case 'h': fn_headers = strdup(optarg); break;
-		case 'n': is_by_qname = 1; break;
-		case '1': flag |= MERGE_LEVEL1; break;
-		case 'u': flag |= MERGE_UNCOMP; break;
-		case 'R': reg = strdup(optarg); break;
-		case 'l': level = atoi(optarg); break;
-		case '@': n_threads = atoi(optarg); break;
-		}
-	}
-	if (optind + 2 >= argc) {
-		fprintf(pysamerr, "\n");
-		fprintf(pysamerr, "Usage:   samtools merge [-nr] [-h inh.sam] <out.bam> <in1.bam> <in2.bam> [...]\n\n");
-		fprintf(pysamerr, "Options: -n       sort by read names\n");
-		fprintf(pysamerr, "         -r       attach RG tag (inferred from file names)\n");
-		fprintf(pysamerr, "         -u       uncompressed BAM output\n");
-		fprintf(pysamerr, "         -f       overwrite the output BAM if exist\n");
-		fprintf(pysamerr, "         -1       compress level 1\n");
-		fprintf(pysamerr, "         -l INT   compression level, from 0 to 9 [-1]\n");
-		fprintf(pysamerr, "         -@ INT   number of BAM compression threads [0]\n");
-		fprintf(pysamerr, "         -R STR   merge file in the specified region STR [all]\n");
-		fprintf(pysamerr, "         -h FILE  copy the header in FILE to <out.bam> [in1.bam]\n\n");
-		fprintf(pysamerr, "Note: Samtools' merge does not reconstruct the @RG dictionary in the header. Users\n");
-		fprintf(pysamerr, "      must provide the correct header with -h, or uses Picard which properly maintains\n");
-		fprintf(pysamerr, "      the header dictionary in merging.\n\n");
-		return 1;
-	}
-	if (!(flag & MERGE_FORCE) && strcmp(argv[optind], "-")) {
-		FILE *fp = fopen(argv[optind], "rb");
-		if (fp != NULL) {
-			fclose(fp);
-			fprintf(pysamerr, "[%s] File '%s' exists. Please apply '-f' to overwrite. Abort.\n", __func__, argv[optind]);
-			return 1;
-		}
-	}
-	if (bam_merge_core2(is_by_qname, argv[optind], fn_headers, argc - optind - 1, argv + optind + 1, flag, reg, n_threads, level) < 0) ret = 1;
-	free(reg);
-	free(fn_headers);
-	return ret;
+    int c, is_by_qname = 0, flag = 0, ret = 0, n_threads = 0, level = -1;
+    char *fn_headers = NULL, *reg = NULL, mode[12];
+    long random_seed = (long)time(NULL);
+    char** fn = NULL;
+    int fn_size = 0;
+
+    if (argc == 1) {
+        merge_usage(stdout);
+        return 0;
+    }
+
+    while ((c = getopt(argc, argv, "h:nru1R:f@:l:cps:b:")) >= 0) {
+        switch (c) {
+        case 'r': flag |= MERGE_RG; break;
+        case 'f': flag |= MERGE_FORCE; break;
+        case 'h': fn_headers = strdup(optarg); break;
+        case 'n': is_by_qname = 1; break;
+        case '1': flag |= MERGE_LEVEL1; level = 1; break;
+        case 'u': flag |= MERGE_UNCOMP; level = 0; break;
+        case 'R': reg = strdup(optarg); break;
+        case 'l': level = atoi(optarg); break;
+        case '@': n_threads = atoi(optarg); break;
+        case 'c': flag |= MERGE_COMBINE_RG; break;
+        case 'p': flag |= MERGE_COMBINE_PG; break;
+        case 's': random_seed = atol(optarg); break;
+        case 'b': {
+            // load the list of files to read
+            int nfiles;
+            char **fn_read = hts_readlines(optarg, &nfiles);
+            if (fn_read) {
+                // Append to end of array
+                fn = realloc(fn, (fn_size+nfiles) * sizeof(char*));
+                if (fn == NULL) { ret = 1; goto end; }
+                memcpy(fn+fn_size, fn_read, nfiles * sizeof(char*));
+                fn_size += nfiles;
+            }
+            else {
+                fprintf(pysamerr, "[%s] Invalid file list \"%s\"\n", __func__, optarg);
+                ret = 1;
+            }
+            break;
+        }
+        }
+    }
+    if ( argc - optind < 1 ) {
+        fprintf(pysamerr, "You must at least specify the output file.\n");
+        merge_usage(pysamerr);
+        return 1;
+    }
+
+    srand48(random_seed);
+    if (!(flag & MERGE_FORCE) && strcmp(argv[optind], "-")) {
+        FILE *fp = fopen(argv[optind], "rb");
+        if (fp != NULL) {
+            fclose(fp);
+            fprintf(pysamerr, "[%s] File '%s' exists. Please apply '-f' to overwrite. Abort.\n", __func__, argv[optind]);
+            return 1;
+        }
+    }
+
+    int nargcfiles = argc - (optind+1);
+    if (nargcfiles > 0) {
+        // Add argc files to end of array
+        fn = realloc(fn, (fn_size+nargcfiles) * sizeof(char*));
+        if (fn == NULL) { ret = 1; goto end; }
+        memcpy(fn+fn_size, argv + (optind+1), nargcfiles * sizeof(char*));
+    }
+    if (fn_size+nargcfiles < 2) {
+        fprintf(pysamerr, "You must specify at least 2 input files.\n");
+        merge_usage(pysamerr);
+        return 1;
+    }
+    strcpy(mode, "wb");
+    if (level >= 0) sprintf(strchr(mode, '\0'), "%d", level < 9? level : 9);
+    if (bam_merge_core2(is_by_qname, argv[optind], mode, fn_headers, fn_size+nargcfiles, fn, flag, reg, n_threads) < 0) ret = 1;
+end:
+    if (fn_size > 0) {
+        int i;
+        for (i=0; i<fn_size; i++) free(fn[i]);
+        free(fn);
+    }
+    free(reg);
+    free(fn_headers);
+    return ret;
 }
 
 /***************
@@ -321,110 +875,111 @@ int bam_merge(int argc, char *argv[])
 
 typedef bam1_t *bam1_p;
 
-static int change_SO(bam_header_t *h, const char *so)
+static int change_SO(bam_hdr_t *h, const char *so)
 {
-	char *p, *q, *beg = 0, *end = 0, *newtext;
-	if (h->l_text > 3) {
-		if (strncmp(h->text, "@HD", 3) == 0) {
-			if ((p = strchr(h->text, '\n')) == 0) return -1;
-			*p = '\0';
-			if ((q = strstr(h->text, "\tSO:")) != 0) {
-				*p = '\n'; // change back
-				if (strncmp(q + 4, so, p - q - 4) != 0) {
-					beg = q;
-					for (q += 4; *q != '\n' && *q != '\t'; ++q);
-					end = q;
-				} else return 0; // no need to change
-			} else beg = end = p, *p = '\n';
-		}
-	}
-	if (beg == 0) { // no @HD
-		h->l_text += strlen(so) + 15;
-		newtext = malloc(h->l_text + 1);
-		sprintf(newtext, "@HD\tVN:1.3\tSO:%s\n", so);
-		strcat(newtext, h->text);
-	} else { // has @HD but different or no SO
-		h->l_text = (beg - h->text) + (4 + strlen(so)) + (h->text + h->l_text - end);
-		newtext = malloc(h->l_text + 1);
-		strncpy(newtext, h->text, beg - h->text);
-		sprintf(newtext + (beg - h->text), "\tSO:%s", so);
-		strcat(newtext, end);
-	}
-	free(h->text);
-	h->text = newtext;
-	return 0;
+    char *p, *q, *beg = NULL, *end = NULL, *newtext;
+    if (h->l_text > 3) {
+        if (strncmp(h->text, "@HD", 3) == 0) {
+            if ((p = strchr(h->text, '\n')) == 0) return -1;
+            *p = '\0';
+            if ((q = strstr(h->text, "\tSO:")) != 0) {
+                *p = '\n'; // change back
+                if (strncmp(q + 4, so, p - q - 4) != 0) {
+                    beg = q;
+                    for (q += 4; *q != '\n' && *q != '\t'; ++q);
+                    end = q;
+                } else return 0; // no need to change
+            } else beg = end = p, *p = '\n';
+        }
+    }
+    if (beg == NULL) { // no @HD
+        h->l_text += strlen(so) + 15;
+        newtext = (char*)malloc(h->l_text + 1);
+        sprintf(newtext, "@HD\tVN:1.3\tSO:%s\n", so);
+        strcat(newtext, h->text);
+    } else { // has @HD but different or no SO
+        h->l_text = (beg - h->text) + (4 + strlen(so)) + (h->text + h->l_text - end);
+        newtext = (char*)malloc(h->l_text + 1);
+        strncpy(newtext, h->text, beg - h->text);
+        sprintf(newtext + (beg - h->text), "\tSO:%s", so);
+        strcat(newtext, end);
+    }
+    free(h->text);
+    h->text = newtext;
+    return 0;
 }
 
+// Function to compare reads and determine which one is < the other
 static inline int bam1_lt(const bam1_p a, const bam1_p b)
 {
-	if (g_is_by_qname) {
-		int t = strnum_cmp(bam1_qname(a), bam1_qname(b));
-		return (t < 0 || (t == 0 && (a->core.flag&0xc0) < (b->core.flag&0xc0)));
-	} else return (((uint64_t)a->core.tid<<32|(a->core.pos+1)<<1|bam1_strand(a)) < ((uint64_t)b->core.tid<<32|(b->core.pos+1)<<1|bam1_strand(b)));
+    if (g_is_by_qname) {
+        int t = strnum_cmp(bam_get_qname(a), bam_get_qname(b));
+        return (t < 0 || (t == 0 && (a->core.flag&0xc0) < (b->core.flag&0xc0)));
+    } else return (((uint64_t)a->core.tid<<32|(a->core.pos+1)<<1|bam_is_rev(a)) < ((uint64_t)b->core.tid<<32|(b->core.pos+1)<<1|bam_is_rev(b)));
 }
 KSORT_INIT(sort, bam1_p, bam1_lt)
 
 typedef struct {
-	size_t buf_len;
-	const char *prefix;
-	bam1_p *buf;
-	const bam_header_t *h;
-	int index;
+    size_t buf_len;
+    const char *prefix;
+    bam1_p *buf;
+    const bam_hdr_t *h;
+    int index;
 } worker_t;
 
-static void write_buffer(const char *fn, const char *mode, size_t l, bam1_p *buf, const bam_header_t *h, int n_threads)
+static void write_buffer(const char *fn, const char *mode, size_t l, bam1_p *buf, const bam_hdr_t *h, int n_threads)
 {
-	size_t i;
-	bamFile fp;
-	fp = strcmp(fn, "-")? bam_open(fn, mode) : bam_dopen(fileno(stdout), mode);
-	if (fp == 0) return;
-	bam_header_write(fp, h);
-	if (n_threads > 1) bgzf_mt(fp, n_threads, 256);
-	for (i = 0; i < l; ++i)
-		bam_write1_core(fp, &buf[i]->core, buf[i]->data_len, buf[i]->data);
-	bam_close(fp);
+    size_t i;
+    samFile* fp;
+    fp = sam_open(fn, mode);
+    if (fp == NULL) return;
+    sam_hdr_write(fp, h);
+    if (n_threads > 1) hts_set_threads(fp, n_threads);
+    for (i = 0; i < l; ++i)
+        sam_write1(fp, h, buf[i]);
+    sam_close(fp);
 }
 
 static void *worker(void *data)
 {
-	worker_t *w = (worker_t*)data;
-	char *name;
-	ks_mergesort(sort, w->buf_len, w->buf, 0);
-	name = (char*)calloc(strlen(w->prefix) + 20, 1);
-	sprintf(name, "%s.%.4d.bam", w->prefix, w->index);
-	write_buffer(name, "w1", w->buf_len, w->buf, w->h, 0);
-	free(name);
-	return 0;
+    worker_t *w = (worker_t*)data;
+    char *name;
+    ks_mergesort(sort, w->buf_len, w->buf, 0);
+    name = (char*)calloc(strlen(w->prefix) + 20, 1);
+    sprintf(name, "%s.%.4d.bam", w->prefix, w->index);
+    write_buffer(name, "wb1", w->buf_len, w->buf, w->h, 0);
+    free(name);
+    return 0;
 }
 
-static int sort_blocks(int n_files, size_t k, bam1_p *buf, const char *prefix, const bam_header_t *h, int n_threads)
+static int sort_blocks(int n_files, size_t k, bam1_p *buf, const char *prefix, const bam_hdr_t *h, int n_threads)
 {
-	int i;
-	size_t rest;
-	bam1_p *b;
-	pthread_t *tid;
-	pthread_attr_t attr;
-	worker_t *w;
-
-	if (n_threads < 1) n_threads = 1;
-	if (k < n_threads * 64) n_threads = 1; // use a single thread if we only sort a small batch of records
-	pthread_attr_init(&attr);
-	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
-	w = calloc(n_threads, sizeof(worker_t));
-	tid = calloc(n_threads, sizeof(pthread_t));
-	b = buf; rest = k;
-	for (i = 0; i < n_threads; ++i) {
-		w[i].buf_len = rest / (n_threads - i);
-		w[i].buf = b;
-		w[i].prefix = prefix;
-		w[i].h = h;
-		w[i].index = n_files + i;
-		b += w[i].buf_len; rest -= w[i].buf_len;
-		pthread_create(&tid[i], &attr, worker, &w[i]);
-	}
-	for (i = 0; i < n_threads; ++i) pthread_join(tid[i], 0);
-	free(tid); free(w);
-	return n_files + n_threads;
+    int i;
+    size_t rest;
+    bam1_p *b;
+    pthread_t *tid;
+    pthread_attr_t attr;
+    worker_t *w;
+
+    if (n_threads < 1) n_threads = 1;
+    if (k < n_threads * 64) n_threads = 1; // use a single thread if we only sort a small batch of records
+    pthread_attr_init(&attr);
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
+    w = (worker_t*)calloc(n_threads, sizeof(worker_t));
+    tid = (pthread_t*)calloc(n_threads, sizeof(pthread_t));
+    b = buf; rest = k;
+    for (i = 0; i < n_threads; ++i) {
+        w[i].buf_len = rest / (n_threads - i);
+        w[i].buf = b;
+        w[i].prefix = prefix;
+        w[i].h = h;
+        w[i].index = n_files + i;
+        b += w[i].buf_len; rest -= w[i].buf_len;
+        pthread_create(&tid[i], &attr, worker, &w[i]);
+    }
+    for (i = 0; i < n_threads; ++i) pthread_join(tid[i], 0);
+    free(tid); free(w);
+    return n_files + n_threads;
 }
 
 /*!
@@ -433,141 +988,196 @@ static int sort_blocks(int n_files, size_t k, bam1_p *buf, const char *prefix, c
 
   @param  is_by_qname whether to sort by query name
   @param  fn       name of the file to be sorted
-  @param  prefix   prefix of the output and the temporary files; upon
-	                   sucessess, prefix.bam will be written.
+  @param  prefix   prefix of the temporary files (prefix.NNNN.bam are written)
+  @param  fnout    name of the final output file to be written
+  @param  modeout  sam_open() mode to be used to create the final output file
   @param  max_mem  approxiate maximum memory (very inaccurate)
-  @param full_path the given output path is the full path and not just the prefix
+  @return 0 for successful sorting, negative on errors
 
   @discussion It may create multiple temporary subalignment files
   and then merge them by calling bam_merge_core(). This function is
   NOT thread safe.
  */
-void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size_t _max_mem, int is_stdout, int n_threads, int level, int full_path)
+int bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, const char *fnout, const char *modeout, size_t _max_mem, int n_threads)
+{
+    int ret, i, n_files = 0;
+    size_t mem, max_k, k, max_mem;
+    bam_hdr_t *header;
+    samFile *fp;
+    bam1_t *b, **buf;
+
+    if (n_threads < 2) n_threads = 1;
+    g_is_by_qname = is_by_qname;
+    max_k = k = 0; mem = 0;
+    max_mem = _max_mem * n_threads;
+    buf = NULL;
+    fp = sam_open(fn, "r");
+    if (fp == NULL) {
+        fprintf(pysamerr, "[bam_sort_core] fail to open file %s\n", fn);
+        return -1;
+    }
+    header = sam_hdr_read(fp);
+    if (is_by_qname) change_SO(header, "queryname");
+    else change_SO(header, "coordinate");
+    // write sub files
+    for (;;) {
+        if (k == max_k) {
+            size_t old_max = max_k;
+            max_k = max_k? max_k<<1 : 0x10000;
+            buf = (bam1_t**)realloc(buf, max_k * sizeof(bam1_t*));
+            memset(buf + old_max, 0, sizeof(bam1_t*) * (max_k - old_max));
+        }
+        if (buf[k] == NULL) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t));
+        b = buf[k];
+        if ((ret = sam_read1(fp, header, b)) < 0) break;
+        if (b->l_data < b->m_data>>2) { // shrink
+            b->m_data = b->l_data;
+            kroundup32(b->m_data);
+            b->data = (uint8_t*)realloc(b->data, b->m_data);
+        }
+        mem += sizeof(bam1_t) + b->m_data + sizeof(void*) + sizeof(void*); // two sizeof(void*) for the data allocated to pointer arrays
+        ++k;
+        if (mem >= max_mem) {
+            n_files = sort_blocks(n_files, k, buf, prefix, header, n_threads);
+            mem = k = 0;
+        }
+    }
+    if (ret != -1)
+        fprintf(pysamerr, "[bam_sort_core] truncated file. Continue anyway.\n");
+    // write the final output
+    if (n_files == 0) { // a single block
+        ks_mergesort(sort, k, buf, 0);
+        write_buffer(fnout, modeout, k, buf, header, n_threads);
+    } else { // then merge
+        char **fns;
+        n_files = sort_blocks(n_files, k, buf, prefix, header, n_threads);
+        fprintf(pysamerr, "[bam_sort_core] merging from %d files...\n", n_files);
+        fns = (char**)calloc(n_files, sizeof(char*));
+        for (i = 0; i < n_files; ++i) {
+            fns[i] = (char*)calloc(strlen(prefix) + 20, 1);
+            sprintf(fns[i], "%s.%.4d.bam", prefix, i);
+        }
+        if (bam_merge_core2(is_by_qname, fnout, modeout, NULL, n_files, fns, MERGE_COMBINE_RG|MERGE_COMBINE_PG, NULL, n_threads) < 0) {
+            // Propagate bam_merge_core2() failure; it has already emitted a
+            // message explaining the failure, so no further message is needed.
+            return -1;
+        }
+        for (i = 0; i < n_files; ++i) {
+            unlink(fns[i]);
+            free(fns[i]);
+        }
+        free(fns);
+    }
+    // free
+    for (k = 0; k < max_k; ++k) {
+        if (!buf[k]) continue;
+        free(buf[k]->data);
+        free(buf[k]);
+    }
+    free(buf);
+    bam_hdr_destroy(header);
+    sam_close(fp);
+    return 0;
+}
+
+int bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t max_mem)
 {
-	int ret, i, n_files = 0;
-	size_t mem, max_k, k, max_mem;
-	bam_header_t *header;
-	bamFile fp;
-	bam1_t *b, **buf;
-	char *fnout = 0;
-	char const *suffix = ".bam";
-	if (full_path) suffix += 4;
-
-	if (n_threads < 2) n_threads = 1;
-	g_is_by_qname = is_by_qname;
-	max_k = k = 0; mem = 0;
-	max_mem = _max_mem * n_threads;
-	buf = 0;
-	fp = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r");
-	if (fp == 0) {
-		fprintf(pysamerr, "[bam_sort_core] fail to open file %s\n", fn);
-		return;
-	}
-	header = bam_header_read(fp);
-	if (is_by_qname) change_SO(header, "queryname");
-	else change_SO(header, "coordinate");
-	// write sub files
-	for (;;) {
-		if (k == max_k) {
-			size_t old_max = max_k;
-			max_k = max_k? max_k<<1 : 0x10000;
-			buf = realloc(buf, max_k * sizeof(void*));
-			memset(buf + old_max, 0, sizeof(void*) * (max_k - old_max));
-		}
-		if (buf[k] == 0) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t));
-		b = buf[k];
-		if ((ret = bam_read1(fp, b)) < 0) break;
-		if (b->data_len < b->m_data>>2) { // shrink
-			b->m_data = b->data_len;
-			kroundup32(b->m_data);
-			b->data = realloc(b->data, b->m_data);
-		}
-		mem += sizeof(bam1_t) + b->m_data + sizeof(void*) + sizeof(void*); // two sizeof(void*) for the data allocated to pointer arrays
-		++k;
-		if (mem >= max_mem) {
-			n_files = sort_blocks(n_files, k, buf, prefix, header, n_threads);
-			mem = k = 0;
-		}
-	}
-	if (ret != -1)
-		fprintf(pysamerr, "[bam_sort_core] truncated file. Continue anyway.\n");
-	// output file name
-	fnout = calloc(strlen(prefix) + 20, 1);
-	if (is_stdout) sprintf(fnout, "-");
-	else sprintf(fnout, "%s%s", prefix, suffix);
-	// write the final output
-	if (n_files == 0) { // a single block
-		char mode[8];
-		strcpy(mode, "w");
-		if (level >= 0) sprintf(mode + 1, "%d", level < 9? level : 9);
-		ks_mergesort(sort, k, buf, 0);
-		write_buffer(fnout, mode, k, buf, header, n_threads);
-	} else { // then merge
-		char **fns;
-		n_files = sort_blocks(n_files, k, buf, prefix, header, n_threads);
-		fprintf(pysamerr, "[bam_sort_core] merging from %d files...\n", n_files);
-		fns = (char**)calloc(n_files, sizeof(char*));
-		for (i = 0; i < n_files; ++i) {
-			fns[i] = (char*)calloc(strlen(prefix) + 20, 1);
-			sprintf(fns[i], "%s.%.4d%s", prefix, i, suffix);
-		}
-		bam_merge_core2(is_by_qname, fnout, 0, n_files, fns, 0, 0, n_threads, level);
-		for (i = 0; i < n_files; ++i) {
-			unlink(fns[i]);
-			free(fns[i]);
-		}
-		free(fns);
-	}
-	free(fnout);
-	// free
-	for (k = 0; k < max_k; ++k) {
-		if (!buf[k]) continue;
-		free(buf[k]->data);
-		free(buf[k]);
-	}
-	free(buf);
-	bam_header_destroy(header);
-	bam_close(fp);
+    int ret;
+    char *fnout = calloc(strlen(prefix) + 4 + 1, 1);
+    sprintf(fnout, "%s.bam", prefix);
+    ret = bam_sort_core_ext(is_by_qname, fn, prefix, fnout, "wb", max_mem, 0);
+    free(fnout);
+    return ret;
 }
 
-void bam_sort_core(int is_by_qname, const char *fn, const char *prefix, size_t max_mem)
+static int sort_usage(FILE *fp, int status)
 {
-	bam_sort_core_ext(is_by_qname, fn, prefix, max_mem, 0, 0, -1, 0);
+    fprintf(fp,
+"Usage: samtools sort [options...] [in.bam]\n"
+"Options:\n"
+"  -l INT     Set compression level, from 0 (uncompressed) to 9 (best)\n"
+"  -m INT     Set maximum memory per thread; suffix K/M/G recognized [768M]\n"
+"  -n         Sort by read name\n"
+"  -o FILE    Write final output to FILE rather than standard output\n"
+"  -O FORMAT  Write output as FORMAT ('sam'/'bam'/'cram')   (either -O or\n"
+"  -T PREFIX  Write temporary files to PREFIX.nnnn.bam       -T is required)\n"
+"  -@ INT     Set number of sorting and compression threads [1]\n"
+"\n"
+"Legacy usage: samtools sort [options...] <in.bam> <out.prefix>\n"
+"Options:\n"
+"  -f         Use <out.prefix> as full final filename rather than prefix\n"
+"  -o         Write final output to stdout rather than <out.prefix>.bam\n"
+"  -l,m,n,@   Similar to corresponding options above\n");
+    return status;
 }
 
 int bam_sort(int argc, char *argv[])
 {
-	size_t max_mem = 768<<20; // 512MB
-	int c, is_by_qname = 0, is_stdout = 0, n_threads = 0, level = -1, full_path = 0;
-	while ((c = getopt(argc, argv, "fnom:@:l:")) >= 0) {
-		switch (c) {
-		case 'f': full_path = 1; break;
-		case 'o': is_stdout = 1; break;
-		case 'n': is_by_qname = 1; break;
-		case 'm': {
-				char *q;
-				max_mem = strtol(optarg, &q, 0);
-				if (*q == 'k' || *q == 'K') max_mem <<= 10;
-				else if (*q == 'm' || *q == 'M') max_mem <<= 20;
-				else if (*q == 'g' || *q == 'G') max_mem <<= 30;
-				break;
-			}
-		case '@': n_threads = atoi(optarg); break;
-		case 'l': level = atoi(optarg); break;
-		}
-	}
-	if (optind + 2 > argc) {
-		fprintf(pysamerr, "\n");
-		fprintf(pysamerr, "Usage:   samtools sort [options] <in.bam> <out.prefix>\n\n");
-		fprintf(pysamerr, "Options: -n        sort by read name\n");
-		fprintf(pysamerr, "         -f        use <out.prefix> as full file name instead of prefix\n");
-		fprintf(pysamerr, "         -o        final output to stdout\n");
-		fprintf(pysamerr, "         -l INT    compression level, from 0 to 9 [-1]\n");
-		fprintf(pysamerr, "         -@ INT    number of sorting and compression threads [1]\n");
-		fprintf(pysamerr, "         -m INT    max memory per thread; suffix K/M/G recognized [768M]\n");
-		fprintf(pysamerr, "\n");
-		return 1;
-	}
-	bam_sort_core_ext(is_by_qname, argv[optind], argv[optind+1], max_mem, is_stdout, n_threads, level, full_path);
-	return 0;
+    size_t max_mem = 768<<20; // 512MB
+    int c, i, modern, nargs, is_by_qname = 0, is_stdout = 0, ret = EXIT_SUCCESS, n_threads = 0, level = -1, full_path = 0;
+    char *fnout = "-", *fmtout = NULL, modeout[12], *tmpprefix = NULL;
+    kstring_t fnout_buffer = { 0, 0, NULL };
+
+    modern = 0;
+    for (i = 1; i < argc; ++i)
+        if (argv[i][0] == '-' && strpbrk(argv[i], "OT")) { modern = 1; break; }
+
+    while ((c = getopt(argc, argv, modern? "l:m:no:O:T:@:" : "fnom:@:l:")) >= 0) {
+        switch (c) {
+        case 'f': full_path = 1; break;
+        case 'o': if (modern) fnout = optarg; else is_stdout = 1; break;
+        case 'n': is_by_qname = 1; break;
+        case 'm': {
+                char *q;
+                max_mem = strtol(optarg, &q, 0);
+                if (*q == 'k' || *q == 'K') max_mem <<= 10;
+                else if (*q == 'm' || *q == 'M') max_mem <<= 20;
+                else if (*q == 'g' || *q == 'G') max_mem <<= 30;
+                break;
+            }
+        case 'O': fmtout = optarg; break;
+        case 'T': tmpprefix = optarg; break;
+        case '@': n_threads = atoi(optarg); break;
+        case 'l': level = atoi(optarg); break;
+        default: return sort_usage(pysamerr, EXIT_FAILURE);
+        }
+    }
+
+    nargs = argc - optind;
+    if (argc == 1)
+        return sort_usage(stdout, EXIT_SUCCESS);
+    else if (modern? (nargs > 1) : (nargs != 2))
+        return sort_usage(pysamerr, EXIT_FAILURE);
+
+    if (!modern) {
+        fmtout = "bam";
+        if (is_stdout) fnout = "-";
+        else if (full_path) fnout = argv[optind+1];
+        else {
+            ksprintf(&fnout_buffer, "%s.%s", argv[optind+1], fmtout);
+            fnout = fnout_buffer.s;
+        }
+        tmpprefix = argv[optind+1];
+    }
+
+    strcpy(modeout, "w");
+    if (sam_open_mode(&modeout[1], fnout, fmtout) < 0) {
+        if (fmtout) fprintf(pysamerr, "[bam_sort] can't parse output format \"%s\"\n", fmtout);
+        else fprintf(pysamerr, "[bam_sort] can't determine output format\n");
+        ret = EXIT_FAILURE;
+        goto sort_end;
+    }
+    if (level >= 0) sprintf(strchr(modeout, '\0'), "%d", level < 9? level : 9);
+
+    if (tmpprefix == NULL) {
+        fprintf(pysamerr, "[bam_sort] no prefix specified for temporary files (use -T option)\n");
+        ret = EXIT_FAILURE;
+        goto sort_end;
+    }
+
+    if (bam_sort_core_ext(is_by_qname, (nargs > 0)? argv[optind] : "-", tmpprefix, fnout, modeout, max_mem, n_threads) < 0) ret = EXIT_FAILURE;
+
+sort_end:
+    free(fnout_buffer.s);
+    return ret;
 }
diff --git a/samtools/bam_split.c.pysam.c b/samtools/bam_split.c.pysam.c
new file mode 100644
index 0000000..ba03c3d
--- /dev/null
+++ b/samtools/bam_split.c.pysam.c
@@ -0,0 +1,510 @@
+#include "pysam.h"
+
+/*  bam_split.c -- split subcommand.
+
+    Copyright (C) 2013, 2014 Genome Research Ltd.
+
+    Author: Martin Pollard <mp15 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <htslib/sam.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <limits.h>
+#include <unistd.h>
+#include <regex.h>
+#include <htslib/khash.h>
+#include <htslib/kstring.h>
+
+
+KHASH_MAP_INIT_STR(c2i, int)
+
+struct parsed_opts {
+    char* merged_input_name;
+    char* unaccounted_header_name;
+    char* unaccounted_name;
+    char* output_format_string;
+    bool verbose;
+};
+
+typedef struct parsed_opts parsed_opts_t;
+
+struct state {
+    samFile* merged_input_file;
+    bam_hdr_t* merged_input_header;
+    samFile* unaccounted_file;
+    bam_hdr_t* unaccounted_header;
+    size_t output_count;
+    char** rg_id;
+    samFile** rg_output_file;
+    bam_hdr_t** rg_output_header;
+    kh_c2i_t* rg_hash;
+};
+
+typedef struct state state_t;
+
+static void cleanup_state(state_t* status);
+static void cleanup_opts(parsed_opts_t* opts);
+
+static void usage(FILE *write_to)
+{
+    fprintf(write_to,
+"Usage: samtools split [-u <unaccounted.bam>[:<unaccounted_header.sam>]]\n"
+"                      [-f <format_string>] [-v] <merged.bam>\n"
+"Options:\n"
+"  -f STRING       output filename format string [\"%%*_%%#.bam\"]\n"
+"  -u FILE1        put reads with no RG tag or an unrecognised RG tag in FILE1\n"
+"  -u FILE1:FILE2  ...and override the header with FILE2\n"
+"  -v              verbose output\n"
+"\n"
+"Format string expansions:\n"
+"  %%%%     %%\n"
+"  %%*     basename\n"
+"  %%#     @RG index\n"
+"  %%!     @RG ID\n"
+      );
+}
+
+// Takes the command line options and turns them into something we can understand
+static parsed_opts_t* parse_args(int argc, char** argv)
+{
+    if (argc == 1) { usage(stdout); return NULL; }
+
+    const char* optstring = "vf:u:";
+    char* delim;
+
+    parsed_opts_t* retval = calloc(sizeof(parsed_opts_t), 1);
+    if (! retval ) { perror("cannot allocate option parsing memory"); return NULL; }
+
+    int opt;
+    while ((opt = getopt(argc, argv, optstring)) != -1) {
+        switch (opt) {
+        case 'f':
+            retval->output_format_string = strdup(optarg);
+            if (! retval->output_format_string ) { perror("cannot allocate output format string memory"); return NULL; }
+            break;
+        case 'v':
+            retval->verbose = true;
+            break;
+        case 'u':
+            retval->unaccounted_name = strdup(optarg);
+            if (! retval->unaccounted_name ) { perror("cannot allocate string memory"); return NULL; }
+            if ((delim = strchr(retval->unaccounted_name, ':')) != NULL) {
+                *delim = '\0';
+                retval->unaccounted_header_name = strdup(delim+1);
+                if (! retval->unaccounted_header_name ) { perror("cannot allocate string memory"); return NULL; }
+            }
+            break;
+        default:
+            usage(stdout);
+            free(retval);
+            return NULL;
+        }
+    }
+
+    if (retval->output_format_string == NULL) retval->output_format_string = strdup("%*_%#.bam");
+
+    argc -= optind;
+    argv += optind;
+
+    if (argc != 1) {
+        fprintf(pysamerr, "Invalid number of arguments: %d\n", argc);
+        usage(pysamerr);
+        free(retval);
+        return NULL;
+    }
+
+    retval->merged_input_name = strdup(argv[0]);
+    if (! retval->merged_input_name ) { perror("cannot allocate string memory"); return NULL; }
+
+    return retval;
+}
+
+// Expands a output filename format string
+static char* expand_format_string(const char* format_string, const char* basename, const char* rg_id, const int rg_idx)
+{
+    kstring_t str = { 0, 0, NULL };
+    const char* pointer = format_string;
+    const char* next;
+    while ((next = strchr(pointer, '%')) != NULL) {
+        kputsn(pointer, next-pointer, &str);
+        ++next;
+        switch (*next) {
+            case '%':
+                kputc('%', &str);
+                break;
+            case '*':
+                kputs(basename, &str);
+                break;
+            case '#':
+                kputl(rg_idx, &str);
+                break;
+            case '!':
+                kputs(rg_id, &str);
+                break;
+            case '\0':
+                // Error is: fprintf(pysamerr, "bad format string, trailing %%\n");
+                free(str.s);
+                return NULL;
+            default:
+                // Error is: fprintf(pysamerr, "bad format string, unknown format specifier\n");
+                free(str.s);
+                return NULL;
+        }
+        pointer = next + 1;
+    }
+    kputs(pointer, &str);
+    return ks_release(&str);
+}
+
+// Parse the header, count the number of RG tags and return a list of their names
+static bool count_RG(bam_hdr_t* hdr, size_t* count, char*** output_name)
+{
+    if (hdr->l_text < 3 ) {
+        *count = 0;
+        *output_name = NULL;
+        return true;
+    }
+    kstring_t input = { 0, 0, NULL };
+    kputsn(hdr->text, hdr->l_text, &input);
+
+    //////////////////////////////////////////
+    // First stage count number of @RG tags //
+    //////////////////////////////////////////
+    char* pointer = ks_str(&input);
+    size_t n_rg = 0;
+    // Guard against rare case where @RG is first header line
+    // This shouldn't happen but could where @HD is omitted
+    if (pointer[0] == '@' && pointer[1] == 'R' && pointer[2] == 'G' ) {
+        ++n_rg;
+        pointer += 3;
+    }
+    char* line;
+    while ((line = strstr(pointer, "\n at RG")) != NULL) {
+        ++n_rg;
+        pointer = line + 1;
+    }
+
+    //////////////////////////////////
+    // Second stage locate @RG ID's //
+    //////////////////////////////////
+    char** names = (char**)calloc(sizeof(char*), n_rg);
+    size_t next = 0;
+
+    regex_t rg_finder;
+    if (regcomp(&rg_finder, "^@RG.*\tID:([!-)+-<>-~][ !-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE) != 0) {
+        free(input.s);
+        free(names);
+        return false;
+    }
+    regmatch_t* matches = (regmatch_t*)calloc(sizeof(regmatch_t),2);
+    int error;
+    char* begin = ks_str(&input);
+
+    while ((error = regexec(&rg_finder, begin, 2, matches, 0)) == 0) {
+        kstring_t str = { 0, 0, NULL };
+        kputsn(begin+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &str);
+        names[next++] = ks_release(&str);
+        begin += matches[0].rm_eo;
+    }
+
+    if (error != REG_NOMATCH) {
+        // cleanup
+        regfree(&rg_finder);
+        free(matches);
+        free(names);
+        free(input.s);
+        return false;
+    }
+    free(matches);
+
+    // return results
+    *count = n_rg;
+    *output_name = names;
+    regfree(&rg_finder);
+    free(input.s);
+    return true;
+}
+
+// Filters a header of @RG lines where ID != id_keep
+// TODO: strip @PG's descended from other RGs and their descendants
+static bool filter_header_rg(bam_hdr_t* hdr, const char* id_keep)
+{
+    kstring_t str = {0, 0, NULL};
+
+    regex_t rg_finder;
+
+    if (regcomp(&rg_finder, "^@RG.*\tID:([!-)+-<>-~][ !-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE) != 0) {
+        return false;
+    }
+
+    // regex vars
+    char* header = hdr->text;
+    regmatch_t* matches = (regmatch_t*)calloc(sizeof(regmatch_t),2);
+    kstring_t found_id = { 0, 0, NULL };
+    int error;
+
+    while ((error = regexec(&rg_finder, header, 2, matches, 0)) == 0) {
+        kputsn(header, matches[0].rm_so, &str); // copy header up until the found RG line
+
+        found_id.l = 0;
+        kputsn(header+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &found_id); // extract ID
+        // if it matches keep keep it, else we can just ignore it
+        if (strcmp(ks_str(&found_id), id_keep) == 0) {
+            kputsn(header+matches[0].rm_so, (matches[0].rm_eo+1)-matches[0].rm_so, &str);
+        }
+        // move pointer forward
+        header += matches[0].rm_eo+1;
+    }
+    // cleanup
+    free(found_id.s);
+    free(matches);
+    regfree(&rg_finder);
+    // Did we leave loop because of an error?
+    if (error != REG_NOMATCH) {
+        return false;
+    }
+
+    // Write remainder of string
+    kputs(header, &str);
+
+    // Modify header
+    hdr->l_text = ks_len(&str);
+    free(hdr->text);
+    hdr->text = ks_release(&str);
+
+    return true;
+}
+
+// Set the initial state
+static state_t* init(parsed_opts_t* opts)
+{
+    state_t* retval = calloc(sizeof(state_t), 1);
+    if (!retval) {
+        fprintf(pysamerr, "Out of memory");
+        return NULL;
+    }
+
+    retval->merged_input_file = sam_open(opts->merged_input_name, "rb");
+    if (!retval->merged_input_file) {
+        fprintf(pysamerr, "Could not open input file (%s)\n", opts->merged_input_name);
+        free(retval);
+        return NULL;
+    }
+    retval->merged_input_header = sam_hdr_read(retval->merged_input_file);
+
+    if (opts->unaccounted_name) {
+        if (opts->unaccounted_header_name) {
+            samFile* hdr_load = sam_open(opts->unaccounted_header_name, "r");
+            if (!hdr_load) {
+                fprintf(pysamerr, "Could not open unaccounted header file (%s)\n", opts->unaccounted_header_name);
+                cleanup_state(retval);
+                return NULL;
+            }
+            retval->unaccounted_header = sam_hdr_read(hdr_load);
+            sam_close(hdr_load);
+        } else {
+            retval->unaccounted_header = bam_hdr_dup(retval->merged_input_header);
+        }
+
+        retval->unaccounted_file = sam_open(opts->unaccounted_name, "wb");
+        if (retval->unaccounted_file == NULL) {
+            fprintf(pysamerr, "Could not open unaccounted output file: %s\n", opts->unaccounted_name);
+            cleanup_state(retval);
+            return NULL;
+        }
+    }
+
+    // Open output files for RGs
+    if (!count_RG(retval->merged_input_header, &retval->output_count, &retval->rg_id)) return NULL;
+    if (opts->verbose) fprintf(pysamerr, "@RG's found %zu\n",retval->output_count);
+
+    retval->rg_output_file = (samFile**)calloc(retval->output_count, sizeof(samFile*));
+    retval->rg_output_header = (bam_hdr_t**)calloc(retval->output_count, sizeof(bam_hdr_t*));
+    retval->rg_hash = kh_init_c2i();
+    if (!retval->rg_output_file || !retval->rg_output_header) {
+        fprintf(pysamerr, "Could not allocate memory for output file array. Out of memory?");
+        cleanup_state(retval);
+        return NULL;
+    }
+
+    char* dirsep = strrchr(opts->merged_input_name, '/');
+    char* input_base_name = strdup(dirsep? dirsep+1 : opts->merged_input_name);
+    if (!input_base_name) {
+        fprintf(pysamerr, "Out of memory\n");
+        cleanup_state(retval);
+        return NULL;
+    }
+    char* extension = strrchr(input_base_name, '.');
+    if (extension) *extension = '\0';
+
+    size_t i;
+    for (i = 0; i < retval->output_count; i++) {
+        char* output_filename = NULL;
+
+        if ( ( output_filename = expand_format_string(opts->output_format_string, input_base_name, retval->rg_id[i], i) ) == NULL) {
+            fprintf(pysamerr, "Error expanding output filename format string.\r\n");
+            cleanup_state(retval);
+            free(input_base_name);
+            return NULL;
+        }
+
+        retval->rg_output_file[i] = sam_open(output_filename, "wb");
+        if (retval->rg_output_file[i] == NULL) {
+            fprintf(pysamerr, "Could not open output file: %s\r\n", output_filename);
+            cleanup_state(retval);
+            free(input_base_name);
+            return NULL;
+        }
+
+        // Record index in hash
+        int ret;
+        khiter_t iter = kh_put_c2i(retval->rg_hash, retval->rg_id[i], &ret);
+        kh_val(retval->rg_hash,iter) = i;
+
+        // Set and edit header
+        retval->rg_output_header[i] = bam_hdr_dup(retval->merged_input_header);
+        if ( !filter_header_rg(retval->rg_output_header[i], retval->rg_id[i]) ) {
+            fprintf(pysamerr, "Could not rewrite header for file: %s\r\n", output_filename);
+            cleanup_state(retval);
+            free(output_filename);
+            free(input_base_name);
+            return NULL;
+        }
+        free(output_filename);
+    }
+
+    free(input_base_name);
+
+    return retval;
+}
+
+static bool split(state_t* state)
+{
+    if (state->unaccounted_file && sam_hdr_write(state->unaccounted_file, state->unaccounted_header) != 0) {
+        fprintf(pysamerr, "Could not write output file header\n");
+        return false;
+    }
+    size_t i;
+    for (i = 0; i < state->output_count; i++) {
+        if (sam_hdr_write(state->rg_output_file[i], state->rg_output_header[i]) != 0) {
+            fprintf(pysamerr, "Could not write output file header\n");
+            return false;
+        }
+    }
+
+    bam1_t* file_read = bam_init1();
+    // Read the first record
+    if (sam_read1(state->merged_input_file, state->merged_input_header, file_read) < 0) {
+        // Nothing more to read?  Ignore this file
+        bam_destroy1(file_read);
+        file_read = NULL;
+    }
+
+    while (file_read != NULL) {
+        // Get RG tag from read and look it up in hash to find file to output it to
+        uint8_t* tag = bam_aux_get(file_read, "RG");
+        khiter_t iter;
+        if ( tag != NULL ) {
+            char* rg = bam_aux2Z(tag);
+            iter = kh_get_c2i(state->rg_hash, rg);
+        } else {
+            iter = kh_end(state->rg_hash);
+        }
+
+        // Write the read out to correct file
+        if (iter != kh_end(state->rg_hash)) {
+            // if found write to the appropriate untangled bam
+            int i = kh_val(state->rg_hash,iter);
+            sam_write1(state->rg_output_file[i], state->rg_output_header[i], file_read);
+        } else {
+            // otherwise write to the unaccounted bam if there is one or fail
+            if (state->unaccounted_file == NULL) {
+                if (tag) {
+                    fprintf(pysamerr, "Read \"%s\" with unaccounted for tag \"%s\".\n", bam_get_qname(file_read), bam_aux2Z(tag));
+                } else {
+                    fprintf(pysamerr, "Read \"%s\" has no RG tag.\n", bam_get_qname(file_read));
+                }
+                bam_destroy1(file_read);
+                return false;
+            } else {
+                sam_write1(state->unaccounted_file, state->unaccounted_header, file_read);
+            }
+        }
+
+        // Replace written read with the next one to process
+        if (sam_read1(state->merged_input_file, state->merged_input_header, file_read) < 0) {
+            // Nothing more to read?  Ignore this file in future
+            bam_destroy1(file_read);
+            file_read = NULL;
+        }
+    }
+
+    return true;
+}
+
+static void cleanup_state(state_t* status)
+{
+    if (!status) return;
+    if (status->unaccounted_header) bam_hdr_destroy(status->unaccounted_header);
+    if (status->unaccounted_file) sam_close(status->unaccounted_file);
+    sam_close(status->merged_input_file);
+    size_t i;
+    for (i = 0; i < status->output_count; i++) {
+        bam_hdr_destroy(status->rg_output_header[i]);
+        sam_close(status->rg_output_file[i]);
+        free(status->rg_id[i]);
+    }
+    bam_hdr_destroy(status->merged_input_header);
+    free(status->rg_output_header);
+    free(status->rg_output_file);
+    kh_destroy_c2i(status->rg_hash);
+    free(status->rg_id);
+    free(status);
+}
+
+static void cleanup_opts(parsed_opts_t* opts)
+{
+    if (!opts) return;
+    free(opts->merged_input_name);
+    free(opts->unaccounted_header_name);
+    free(opts->unaccounted_name);
+    free(opts->output_format_string);
+    free(opts);
+}
+
+int main_split(int argc, char** argv)
+{
+    int ret = 1;
+    parsed_opts_t* opts = parse_args(argc, argv);
+    if (!opts ) goto cleanup_opts;
+    state_t* status = init(opts);
+    if (!status) goto cleanup_opts;
+
+    if (split(status)) ret = 0;
+
+    cleanup_state(status);
+cleanup_opts:
+    cleanup_opts(opts);
+
+    return ret;
+}
diff --git a/samtools/bam_stat.c.pysam.c b/samtools/bam_stat.c.pysam.c
index 4751782..a07d32d 100644
--- a/samtools/bam_stat.c.pysam.c
+++ b/samtools/bam_stat.c.pysam.c
@@ -1,79 +1,113 @@
 #include "pysam.h"
 
+/*  bam_stat.c -- flagstat subcommand.
+
+    Copyright (C) 2009, 2011, 2013, 2014 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <unistd.h>
-#include <assert.h>
 #include "bam.h"
+#include "samtools.h"
 
 typedef struct {
-	long long n_reads[2], n_mapped[2], n_pair_all[2], n_pair_map[2], n_pair_good[2];
-	long long n_sgltn[2], n_read1[2], n_read2[2];
-	long long n_dup[2];
-	long long n_diffchr[2], n_diffhigh[2];
+    long long n_reads[2], n_mapped[2], n_pair_all[2], n_pair_map[2], n_pair_good[2];
+    long long n_sgltn[2], n_read1[2], n_read2[2];
+    long long n_dup[2];
+    long long n_diffchr[2], n_diffhigh[2];
+    long long n_secondary[2], n_supp[2];
 } bam_flagstat_t;
 
-#define flagstat_loop(s, c) do {										\
-		int w = ((c)->flag & BAM_FQCFAIL)? 1 : 0;	 					\
-		++(s)->n_reads[w];												\
-		if ((c)->flag & BAM_FPAIRED) {									\
-			++(s)->n_pair_all[w];										\
-			if ((c)->flag & BAM_FPROPER_PAIR) ++(s)->n_pair_good[w];	\
-			if ((c)->flag & BAM_FREAD1) ++(s)->n_read1[w];				\
-			if ((c)->flag & BAM_FREAD2) ++(s)->n_read2[w];				\
-			if (((c)->flag & BAM_FMUNMAP) && !((c)->flag & BAM_FUNMAP)) ++(s)->n_sgltn[w];	\
-			if (!((c)->flag & BAM_FUNMAP) && !((c)->flag & BAM_FMUNMAP)) { \
-				++(s)->n_pair_map[w];									\
-				if ((c)->mtid != (c)->tid) {							\
-					++(s)->n_diffchr[w];								\
-					if ((c)->qual >= 5) ++(s)->n_diffhigh[w];			\
-				}														\
-			}															\
-		}																\
-		if (!((c)->flag & BAM_FUNMAP)) ++(s)->n_mapped[w];				\
-		if ((c)->flag & BAM_FDUP) ++(s)->n_dup[w];						\
-	} while (0)
+#define flagstat_loop(s, c) do {                                        \
+        int w = ((c)->flag & BAM_FQCFAIL)? 1 : 0;                       \
+        ++(s)->n_reads[w];                                              \
+        if ((c)->flag & BAM_FSECONDARY ) {                              \
+            ++(s)->n_secondary[w];                                      \
+        } else if ((c)->flag & BAM_FSUPPLEMENTARY ) {                   \
+            ++(s)->n_supp[w];                                           \
+        } else if ((c)->flag & BAM_FPAIRED) {                           \
+            ++(s)->n_pair_all[w];                                       \
+            if (((c)->flag & BAM_FPROPER_PAIR) && !((c)->flag & BAM_FUNMAP) ) ++(s)->n_pair_good[w];    \
+            if ((c)->flag & BAM_FREAD1) ++(s)->n_read1[w];              \
+            if ((c)->flag & BAM_FREAD2) ++(s)->n_read2[w];              \
+            if (((c)->flag & BAM_FMUNMAP) && !((c)->flag & BAM_FUNMAP)) ++(s)->n_sgltn[w];  \
+            if (!((c)->flag & BAM_FUNMAP) && !((c)->flag & BAM_FMUNMAP)) { \
+                ++(s)->n_pair_map[w];                                   \
+                if ((c)->mtid != (c)->tid) {                            \
+                    ++(s)->n_diffchr[w];                                \
+                    if ((c)->qual >= 5) ++(s)->n_diffhigh[w];           \
+                }                                                       \
+            }                                                           \
+        }                                                               \
+        if (!((c)->flag & BAM_FUNMAP)) ++(s)->n_mapped[w];              \
+        if ((c)->flag & BAM_FDUP) ++(s)->n_dup[w];                      \
+    } while (0)
 
 bam_flagstat_t *bam_flagstat_core(bamFile fp)
 {
-	bam_flagstat_t *s;
-	bam1_t *b;
-	bam1_core_t *c;
-	int ret;
-	s = (bam_flagstat_t*)calloc(1, sizeof(bam_flagstat_t));
-	b = bam_init1();
-	c = &b->core;
-	while ((ret = bam_read1(fp, b)) >= 0)
-		flagstat_loop(s, c);
-	bam_destroy1(b);
-	if (ret != -1)
-		fprintf(pysamerr, "[bam_flagstat_core] Truncated file? Continue anyway.\n");
-	return s;
+    bam_flagstat_t *s;
+    bam1_t *b;
+    bam1_core_t *c;
+    int ret;
+    s = (bam_flagstat_t*)calloc(1, sizeof(bam_flagstat_t));
+    b = bam_init1();
+    c = &b->core;
+    while ((ret = bam_read1(fp, b)) >= 0)
+        flagstat_loop(s, c);
+    bam_destroy1(b);
+    if (ret != -1)
+        fprintf(pysamerr, "[bam_flagstat_core] Truncated file? Continue anyway.\n");
+    return s;
 }
 int bam_flagstat(int argc, char *argv[])
 {
-	bamFile fp;
-	bam_header_t *header;
-	bam_flagstat_t *s;
-	if (argc == optind) {
-		fprintf(pysamerr, "Usage: samtools flagstat <in.bam>\n");
-		return 1;
-	}
-	fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
-	assert(fp);
-	header = bam_header_read(fp);
-	s = bam_flagstat_core(fp);
-	printf("%lld + %lld in total (QC-passed reads + QC-failed reads)\n", s->n_reads[0], s->n_reads[1]);
-	printf("%lld + %lld duplicates\n", s->n_dup[0], s->n_dup[1]);
-	printf("%lld + %lld mapped (%.2f%%:%.2f%%)\n", s->n_mapped[0], s->n_mapped[1], (float)s->n_mapped[0] / s->n_reads[0] * 100.0, (float)s->n_mapped[1] / s->n_reads[1] * 100.0);
-	printf("%lld + %lld paired in sequencing\n", s->n_pair_all[0], s->n_pair_all[1]);
-	printf("%lld + %lld read1\n", s->n_read1[0], s->n_read1[1]);
-	printf("%lld + %lld read2\n", s->n_read2[0], s->n_read2[1]);
-	printf("%lld + %lld properly paired (%.2f%%:%.2f%%)\n", s->n_pair_good[0], s->n_pair_good[1], (float)s->n_pair_good[0] / s->n_pair_all[0] * 100.0, (float)s->n_pair_good[1] / s->n_pair_all[1] * 100.0);
-	printf("%lld + %lld with itself and mate mapped\n", s->n_pair_map[0], s->n_pair_map[1]);
-	printf("%lld + %lld singletons (%.2f%%:%.2f%%)\n", s->n_sgltn[0], s->n_sgltn[1], (float)s->n_sgltn[0] / s->n_pair_all[0] * 100.0, (float)s->n_sgltn[1] / s->n_pair_all[1] * 100.0);
-	printf("%lld + %lld with mate mapped to a different chr\n", s->n_diffchr[0], s->n_diffchr[1]);
-	printf("%lld + %lld with mate mapped to a different chr (mapQ>=5)\n", s->n_diffhigh[0], s->n_diffhigh[1]);
-	free(s);
-	bam_header_destroy(header);
-	bam_close(fp);
-	return 0;
+    bamFile fp;
+    bam_header_t *header;
+    bam_flagstat_t *s;
+    if (argc == optind) {
+        fprintf(pysamerr, "Usage: samtools flagstat <in.bam>\n");
+        return 1;
+    }
+    fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(STDIN_FILENO, "r");
+    if (fp == NULL) {
+        print_error_errno("Cannot open input file \"%s\"", argv[optind]);
+        return 1;
+    }
+    header = bam_header_read(fp);
+    s = bam_flagstat_core(fp);
+    printf("%lld + %lld in total (QC-passed reads + QC-failed reads)\n", s->n_reads[0], s->n_reads[1]);
+    printf("%lld + %lld secondary\n", s->n_secondary[0], s->n_secondary[1]);
+    printf("%lld + %lld supplimentary\n", s->n_supp[0], s->n_supp[1]);
+    printf("%lld + %lld duplicates\n", s->n_dup[0], s->n_dup[1]);
+    printf("%lld + %lld mapped (%.2f%%:%.2f%%)\n", s->n_mapped[0], s->n_mapped[1], (float)s->n_mapped[0] / s->n_reads[0] * 100.0, (float)s->n_mapped[1] / s->n_reads[1] * 100.0);
+    printf("%lld + %lld paired in sequencing\n", s->n_pair_all[0], s->n_pair_all[1]);
+    printf("%lld + %lld read1\n", s->n_read1[0], s->n_read1[1]);
+    printf("%lld + %lld read2\n", s->n_read2[0], s->n_read2[1]);
+    printf("%lld + %lld properly paired (%.2f%%:%.2f%%)\n", s->n_pair_good[0], s->n_pair_good[1], (float)s->n_pair_good[0] / s->n_pair_all[0] * 100.0, (float)s->n_pair_good[1] / s->n_pair_all[1] * 100.0);
+    printf("%lld + %lld with itself and mate mapped\n", s->n_pair_map[0], s->n_pair_map[1]);
+    printf("%lld + %lld singletons (%.2f%%:%.2f%%)\n", s->n_sgltn[0], s->n_sgltn[1], (float)s->n_sgltn[0] / s->n_pair_all[0] * 100.0, (float)s->n_sgltn[1] / s->n_pair_all[1] * 100.0);
+    printf("%lld + %lld with mate mapped to a different chr\n", s->n_diffchr[0], s->n_diffchr[1]);
+    printf("%lld + %lld with mate mapped to a different chr (mapQ>=5)\n", s->n_diffhigh[0], s->n_diffhigh[1]);
+    free(s);
+    bam_header_destroy(header);
+    bam_close(fp);
+    return 0;
 }
diff --git a/samtools/bam_tview.c.pysam.c b/samtools/bam_tview.c.pysam.c
index 26679eb..82be6e3 100644
--- a/samtools/bam_tview.c.pysam.c
+++ b/samtools/bam_tview.c.pysam.c
@@ -1,220 +1,274 @@
 #include "pysam.h"
 
+/*  bam_tview.c -- tview subcommand.
+
+    Copyright (C) 2008-2014 Genome Research Ltd.
+    Portions copyright (C) 2013 Pierre Lindenbaum, Institut du Thorax, INSERM U1087, Université de Nantes.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notices and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <regex.h>
 #include <assert.h>
 #include "bam_tview.h"
+#include <htslib/faidx.h>
+#include <htslib/sam.h>
+#include <htslib/bgzf.h>
+
+/*! @typedef
+ @abstract      Type of function to be called by sam_fetch().
+ @param  b     the alignment
+ @param  data  user provided data
+ */
+typedef int (*sam_fetch_f)(const bam1_t *b, void *data);
+
+int sam_fetch(samFile *fp, const hts_idx_t *idx, int tid, int beg, int end, void *data, sam_fetch_f func)
+{
+    int ret;
+    hts_itr_t* iter;
+    bam1_t* b = bam_init1();
+    iter = sam_itr_queryi(idx, tid, beg, end);
+    while ((ret = sam_itr_next(fp, iter, b)) >= 0) func(b, data);
+    hts_itr_destroy(iter);
+    bam_destroy1(b);
+    return (ret == -1)? 0 : ret;
+}
 
-int base_tv_init(tview_t* tv,const char *fn, const char *fn_fa, const char *samples)
-	{
-	assert(tv!=NULL);
-	assert(fn!=NULL);
-	tv->mrow = 24; tv->mcol = 80;
-	tv->color_for = TV_COLOR_MAPQ;
-	tv->is_dot = 1;
-	
-	tv->fp = bam_open(fn, "r");
-	if(tv->fp==0)
-		{
-		fprintf(pysamerr,"bam_open %s. %s\n", fn,fn_fa);
-            	exit(EXIT_FAILURE);
-		}
-	bgzf_set_cache_size(tv->fp, 8 * 1024 *1024);
-	assert(tv->fp);
-	
-	tv->header = bam_header_read(tv->fp);
-	if(tv->header==0)
-		{
-		fprintf(pysamerr,"Cannot read '%s'.\n", fn);
-            	exit(EXIT_FAILURE);
-		}
-	tv->idx = bam_index_load(fn);
-	if (tv->idx == 0)
-		{
-		fprintf(pysamerr,"Cannot read index for '%s'.\n", fn);
-		exit(EXIT_FAILURE);
-		}
-	tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
-	if (fn_fa) tv->fai = fai_load(fn_fa);
-	tv->bca = bcf_call_init(0.83, 13);
-	tv->ins = 1;
-
-    if ( samples ) 
+
+
+khash_t(kh_rg)* get_rg_sample(const char* header, const char* sample)
+{
+    khash_t(kh_rg)* rg_hash = kh_init(kh_rg);
+    // given sample id return all the RD ID's
+    const char rg_regex[] = "^@RG.*\tID:([!-)+-<>-~][ !-~]*)(\t.*$|$)";
+
+    regex_t rg_id;
+    regmatch_t* matches = (regmatch_t*)calloc(2, sizeof(regmatch_t));
+    if (matches == NULL) { perror("out of memory"); exit(-1); }
+    regcomp(&rg_id, rg_regex, REG_EXTENDED|REG_NEWLINE);
+    char* text = strdup(header);
+    char* end = text + strlen(header);
+    char* tofree = text;
+    while (end > text && regexec(&rg_id, text, 2, matches, 0) == 0) { //    foreach rg id in  header
+        int ret;
+        text[matches[1].rm_eo] = '\0';
+        kh_put(kh_rg, rg_hash, strdup(text+matches[1].rm_so), &ret); // Add the RG to the list
+        text += matches[0].rm_eo + 1; // Move search pointer forward
+    }
+    free(tofree);
+    return rg_hash;
+}
+
+int base_tv_init(tview_t* tv, const char *fn, const char *fn_fa, const char *samples)
+{
+    assert(tv!=NULL);
+    assert(fn!=NULL);
+    tv->mrow = 24; tv->mcol = 80;
+    tv->color_for = TV_COLOR_MAPQ;
+    tv->is_dot = 1;
+
+    tv->fp = sam_open(fn, "r");
+    if(tv->fp == NULL)
     {
-        if ( !tv->header->dict ) tv->header->dict = sam_header_parse2(tv->header->text);
-        void *iter = tv->header->dict;
-        const char *key, *val;
-        int n = 0;
-        tv->rg_hash = kh_init(kh_rg);
-        while ( (iter = sam_header2key_val(iter, "RG","ID","SM", &key, &val)) )
-        {
-            if ( !strcmp(samples,key) || (val && !strcmp(samples,val)) )
-            {
-                khiter_t k = kh_get(kh_rg, tv->rg_hash, key);
-                if ( k != kh_end(tv->rg_hash) ) continue;
-                int ret;
-                k = kh_put(kh_rg, tv->rg_hash, key, &ret);
-                kh_value(tv->rg_hash, k) = val;
-                n++;
-            }
-        }
-        if ( !n )
-        {
-            fprintf(pysamerr,"The sample or read group \"%s\" not present.\n", samples);
-            exit(EXIT_FAILURE);
-        }
+        fprintf(pysamerr,"sam_open %s. %s\n", fn,fn_fa);
+        exit(EXIT_FAILURE);
+    }
+    // TODO bgzf_set_cache_size(tv->fp->fp.bgzf, 8 * 1024 *1024);
+    assert(tv->fp);
+
+    tv->header = sam_hdr_read(tv->fp);
+    if(tv->header == NULL)
+    {
+        fprintf(pysamerr,"Cannot read '%s'.\n", fn);
+        exit(EXIT_FAILURE);
+    }
+    tv->idx = sam_index_load(tv->fp, fn);
+    if (tv->idx == NULL)
+    {
+        fprintf(pysamerr,"Cannot read index for '%s'.\n", fn);
+        exit(EXIT_FAILURE);
     }
+    tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
+    if (fn_fa) tv->fai = fai_load(fn_fa);
+    tv->bca = bcf_call_init(0.83, 13);
+    tv->ins = 1;
 
-	return 0;
-	}
+    // If the user has asked for specific samples find out create a list of readgroups make up these samples
+    if ( samples )
+    {
+        tv->rg_hash = get_rg_sample(tv->header->text, samples); // Init the list of rg's
+    }
+
+    return 0;
+}
 
 
 void base_tv_destroy(tview_t* tv)
-	{
-	bam_lplbuf_destroy(tv->lplbuf);
-	bcf_call_destroy(tv->bca);
-	bam_index_destroy(tv->idx);
-	if (tv->fai) fai_destroy(tv->fai);
-	free(tv->ref);
-	bam_header_destroy(tv->header);
-	bam_close(tv->fp);
-	}
+{
+    bam_lplbuf_destroy(tv->lplbuf);
+    bcf_call_destroy(tv->bca);
+    hts_idx_destroy(tv->idx);
+    if (tv->fai) fai_destroy(tv->fai);
+    free(tv->ref);
+    bam_hdr_destroy(tv->header);
+    sam_close(tv->fp);
+}
 
 
 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
 {
-	extern unsigned char bam_nt16_table[256];
-	tview_t *tv = (tview_t*)data;
-	int i, j, c, rb, attr, max_ins = 0;
-	uint32_t call = 0;
-	if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
-	// print referece
-	rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
-	for (i = tv->last_pos + 1; i < pos; ++i) {
-		if (i%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", i+1);
-		c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
-		tv->my_mvaddch(tv,1, tv->ccol++, c);
-	}
-	if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
-	{ // call consensus
-		bcf_callret1_t bcr;
-		int qsum[4], a1, a2, tmp;
-		double p[3], prior = 30;
-		bcf_call_glfgen(n, pl, bam_nt16_table[rb], tv->bca, &bcr);
-		for (i = 0; i < 4; ++i) qsum[i] = bcr.qsum[i]<<2 | i;
-		for (i = 1; i < 4; ++i) // insertion sort
-			for (j = i; j > 0 && qsum[j] > qsum[j-1]; --j)
-				tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
-		a1 = qsum[0]&3; a2 = qsum[1]&3;
-		p[0] = bcr.p[a1*5+a1]; p[1] = bcr.p[a1*5+a2] + prior; p[2] = bcr.p[a2*5+a2];
-		if ("ACGT"[a1] != toupper(rb)) p[0] += prior + 3;
-		if ("ACGT"[a2] != toupper(rb)) p[2] += prior + 3;
-		if (p[0] < p[1] && p[0] < p[2]) call = (1<<a1)<<16 | (int)((p[1]<p[2]?p[1]:p[2]) - p[0] + .499);
-		else if (p[2] < p[1] && p[2] < p[0]) call = (1<<a2)<<16 | (int)((p[0]<p[1]?p[0]:p[1]) - p[2] + .499);
-		else call = (1<<a1|1<<a2)<<16 | (int)((p[0]<p[2]?p[0]:p[2]) - p[1] + .499);
-	}
-	attr = tv->my_underline(tv);
-	c = ",ACMGRSVTWYHKDBN"[call>>16&0xf];
-	i = (call&0xffff)/10+1;
-	if (i > 4) i = 4;
-	attr |= tv->my_colorpair(tv,i);
-	if (c == toupper(rb)) c = '.';
-	tv->my_attron(tv,attr);
-	tv->my_mvaddch(tv,2, tv->ccol, c);
-	tv->my_attroff(tv,attr);
-	if(tv->ins) {
-		// calculate maximum insert
-		for (i = 0; i < n; ++i) {
-			const bam_pileup1_t *p = pl + i;
-			if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
-		}
-	}
-	// core loop
-	for (j = 0; j <= max_ins; ++j) {
-		for (i = 0; i < n; ++i) {
-			const bam_pileup1_t *p = pl + i;
-			int row = TV_MIN_ALNROW + p->level - tv->row_shift;
-			if (j == 0) {
-				if (!p->is_del) {
-					if (tv->base_for == TV_BASE_COLOR_SPACE && 
-							(c = bam_aux_getCSi(p->b, p->qpos))) {
-						// assume that if we found one color, we will be able to get the color error
-						if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos)) c = bam1_strand(p->b)? ',' : '.';
-					} else {
-						if (tv->show_name) {
-							char *name = bam1_qname(p->b);
-							c = (p->qpos + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos];
-						} else {
-							c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
-							if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
-						}
-					}
-				} else c = p->is_refskip? (bam1_strand(p->b)? '<' : '>') : '*';
-			} else { // padding
-				if (j > p->indel) c = '*';
-				else { // insertion
-					if (tv->base_for ==  TV_BASE_NUCL) {
-						if (tv->show_name) {
-							char *name = bam1_qname(p->b);
-							c = (p->qpos + j + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos + j];
-						} else {
-							c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
-							if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
-						}
-					} else {
-						c = bam_aux_getCSi(p->b, p->qpos + j);
-						if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam1_strand(p->b)? ',' : '.';
-					}
-				}
-			}
-			if (row > TV_MIN_ALNROW && row < tv->mrow) {
-				int x;
-				attr = 0;
-				if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
-						|| (p->b->core.flag & BAM_FSECONDARY)) attr |= tv->my_underline(tv);
-				if (tv->color_for == TV_COLOR_BASEQ) {
-					x = bam1_qual(p->b)[p->qpos]/10 + 1;
-					if (x > 4) x = 4;
-					attr |= tv->my_colorpair(tv,x);
-				} else if (tv->color_for == TV_COLOR_MAPQ) {
-					x = p->b->core.qual/10 + 1;
-					if (x > 4) x = 4;
-					attr |= tv->my_colorpair(tv,x);
-				} else if (tv->color_for == TV_COLOR_NUCL) {
-					x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
-					attr |= tv->my_colorpair(tv,x);
-				} else if(tv->color_for == TV_COLOR_COL) {
-					x = 0;
-					switch(bam_aux_getCSi(p->b, p->qpos)) {
-						case '0': x = 0; break;
-						case '1': x = 1; break;
-						case '2': x = 2; break;
-						case '3': x = 3; break;
-						case '4': x = 4; break;
-						default: x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; break;
-					}
-					x+=5;
-					attr |= tv->my_colorpair(tv,x);
-				} else if(tv->color_for == TV_COLOR_COLQ) {
-					x = bam_aux_getCQi(p->b, p->qpos);
-					if(0 == x) x = bam1_qual(p->b)[p->qpos];
-					x = x/10 + 1;
-					if (x > 4) x = 4;
-					attr |= tv->my_colorpair(tv,x);
-				}
-				tv->my_attron(tv,attr);
-				tv->my_mvaddch(tv,row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
-				tv->my_attroff(tv,attr);
-			}
-		}
-		c = j? '*' : rb;
-		if (c == '*') {
-			attr = tv->my_colorpair(tv,8);
-			tv->my_attron(tv,attr);
-			tv->my_mvaddch(tv,1, tv->ccol++, c);
-			tv->my_attroff(tv,attr);
-		} else tv->my_mvaddch(tv,1, tv->ccol++, c);
-	}
-	tv->last_pos = pos;
-	return 0;
+    extern const char bam_nt16_nt4_table[];
+    tview_t *tv = (tview_t*)data;
+    int i, j, c, rb, attr, max_ins = 0;
+    uint32_t call = 0;
+    if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
+    // print reference
+    rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
+    for (i = tv->last_pos + 1; i < pos; ++i) {
+        if (i%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", i+1);
+        c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
+        tv->my_mvaddch(tv,1, tv->ccol++, c);
+    }
+    if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
+    { // call consensus
+        bcf_callret1_t bcr;
+        memset(&bcr, 0, sizeof bcr);
+        int qsum[4], a1, a2, tmp;
+        double p[3], prior = 30;
+        bcf_call_glfgen(n, pl, seq_nt16_table[rb], tv->bca, &bcr);
+        for (i = 0; i < 4; ++i) qsum[i] = ((int)bcr.qsum[i])<<2 | i;
+        for (i = 1; i < 4; ++i) // insertion sort
+            for (j = i; j > 0 && qsum[j] > qsum[j-1]; --j)
+                tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
+        a1 = qsum[0]&3; a2 = qsum[1]&3;
+        p[0] = bcr.p[a1*5+a1]; p[1] = bcr.p[a1*5+a2] + prior; p[2] = bcr.p[a2*5+a2];
+        if ("ACGT"[a1] != toupper(rb)) p[0] += prior + 3;
+        if ("ACGT"[a2] != toupper(rb)) p[2] += prior + 3;
+        if (p[0] < p[1] && p[0] < p[2]) call = (1<<a1)<<16 | (int)((p[1]<p[2]?p[1]:p[2]) - p[0] + .499);
+        else if (p[2] < p[1] && p[2] < p[0]) call = (1<<a2)<<16 | (int)((p[0]<p[1]?p[0]:p[1]) - p[2] + .499);
+        else call = (1<<a1|1<<a2)<<16 | (int)((p[0]<p[2]?p[0]:p[2]) - p[1] + .499);
+    }
+    attr = tv->my_underline(tv);
+    c = ",ACMGRSVTWYHKDBN"[call>>16&0xf];
+    i = (call&0xffff)/10+1;
+    if (i > 4) i = 4;
+    attr |= tv->my_colorpair(tv,i);
+    if (c == toupper(rb)) c = '.';
+    tv->my_attron(tv,attr);
+    tv->my_mvaddch(tv,2, tv->ccol, c);
+    tv->my_attroff(tv,attr);
+    if(tv->ins) {
+        // calculate maximum insert
+        for (i = 0; i < n; ++i) {
+            const bam_pileup1_t *p = pl + i;
+            if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
+        }
+    }
+    // core loop
+    for (j = 0; j <= max_ins; ++j) {
+        for (i = 0; i < n; ++i) {
+            const bam_pileup1_t *p = pl + i;
+            int row = TV_MIN_ALNROW + p->level - tv->row_shift;
+            if (j == 0) {
+                if (!p->is_del) {
+                    if (tv->base_for == TV_BASE_COLOR_SPACE &&
+                            (c = bam_aux_getCSi(p->b, p->qpos))) {
+                        // assume that if we found one color, we will be able to get the color error
+                        if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos)) c = bam_is_rev(p->b)? ',' : '.';
+                    } else {
+                        if (tv->show_name) {
+                            char *name = bam_get_qname(p->b);
+                            c = (p->qpos + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos];
+                        } else {
+                            c = seq_nt16_str[bam_seqi(bam_get_seq(p->b), p->qpos)];
+                            if (tv->is_dot && toupper(c) == toupper(rb)) c = bam_is_rev(p->b)? ',' : '.';
+                        }
+                    }
+                } else c = p->is_refskip? (bam_is_rev(p->b)? '<' : '>') : '*';
+            } else { // padding
+                if (j > p->indel) c = '*';
+                else { // insertion
+                    if (tv->base_for ==  TV_BASE_NUCL) {
+                        if (tv->show_name) {
+                            char *name = bam_get_qname(p->b);
+                            c = (p->qpos + j + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos + j];
+                        } else {
+                            c = seq_nt16_str[bam_seqi(bam_get_seq(p->b), p->qpos + j)];
+                            if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam_is_rev(p->b)? ',' : '.';
+                        }
+                    } else {
+                        c = bam_aux_getCSi(p->b, p->qpos + j);
+                        if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam_is_rev(p->b)? ',' : '.';
+                    }
+                }
+            }
+            if (row > TV_MIN_ALNROW && row < tv->mrow) {
+                int x;
+                attr = 0;
+                if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
+                        || (p->b->core.flag & BAM_FSECONDARY)) attr |= tv->my_underline(tv);
+                if (tv->color_for == TV_COLOR_BASEQ) {
+                    x = bam_get_qual(p->b)[p->qpos]/10 + 1;
+                    if (x > 4) x = 4;
+                    attr |= tv->my_colorpair(tv,x);
+                } else if (tv->color_for == TV_COLOR_MAPQ) {
+                    x = p->b->core.qual/10 + 1;
+                    if (x > 4) x = 4;
+                    attr |= tv->my_colorpair(tv,x);
+                } else if (tv->color_for == TV_COLOR_NUCL) {
+                    x = bam_nt16_nt4_table[bam_seqi(bam_get_seq(p->b), p->qpos)] + 5;
+                    attr |= tv->my_colorpair(tv,x);
+                } else if(tv->color_for == TV_COLOR_COL) {
+                    x = 0;
+                    switch(bam_aux_getCSi(p->b, p->qpos)) {
+                        case '0': x = 0; break;
+                        case '1': x = 1; break;
+                        case '2': x = 2; break;
+                        case '3': x = 3; break;
+                        case '4': x = 4; break;
+                        default: x = bam_nt16_nt4_table[bam_seqi(bam_get_seq(p->b), p->qpos)]; break;
+                    }
+                    x+=5;
+                    attr |= tv->my_colorpair(tv,x);
+                } else if(tv->color_for == TV_COLOR_COLQ) {
+                    x = bam_aux_getCQi(p->b, p->qpos);
+                    if(0 == x) x = bam_get_qual(p->b)[p->qpos];
+                    x = x/10 + 1;
+                    if (x > 4) x = 4;
+                    attr |= tv->my_colorpair(tv,x);
+                }
+                tv->my_attron(tv,attr);
+                tv->my_mvaddch(tv,row, tv->ccol, bam_is_rev(p->b)? tolower(c) : toupper(c));
+                tv->my_attroff(tv,attr);
+            }
+        }
+        c = j? '*' : rb;
+        if (c == '*') {
+            attr = tv->my_colorpair(tv,8);
+            tv->my_attron(tv,attr);
+            tv->my_mvaddch(tv,1, tv->ccol++, c);
+            tv->my_attroff(tv,attr);
+        } else tv->my_mvaddch(tv,1, tv->ccol++, c);
+    }
+    tv->last_pos = pos;
+    return 0;
 }
 
 
@@ -222,58 +276,64 @@ int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void
 
 int tv_fetch_func(const bam1_t *b, void *data)
 {
-	tview_t *tv = (tview_t*)data;
+    tview_t *tv = (tview_t*)data;
+    /* If we are restricted to specific readgroups check RG is in the list */
     if ( tv->rg_hash )
     {
         const uint8_t *rg = bam_aux_get(b, "RG");
-        if ( !rg ) return 0; 
+        if ( !rg ) return 0; // If we don't have an RG tag exclude read
         khiter_t k = kh_get(kh_rg, tv->rg_hash, (const char*)(rg + 1));
-        if ( k == kh_end(tv->rg_hash) ) return 0;
+        if ( k == kh_end(tv->rg_hash) ) return 0; // if RG tag is not in list of allowed tags exclude read
     }
-	if (tv->no_skip) {
-		uint32_t *cigar = bam1_cigar(b); // this is cheating...
-		int i;
-		for (i = 0; i <b->core.n_cigar; ++i) {
-			if ((cigar[i]&0xf) == BAM_CREF_SKIP)
-				cigar[i] = cigar[i]>>4<<4 | BAM_CDEL;
-		}
-	}
-	bam_lplbuf_push(b, tv->lplbuf);
-	return 0;
+    if (tv->no_skip) {
+        uint32_t *cigar = bam_get_cigar(b); // this is cheating...
+        int i;
+        for (i = 0; i <b->core.n_cigar; ++i) {
+            if ((cigar[i]&0xf) == BAM_CREF_SKIP)
+                cigar[i] = cigar[i]>>4<<4 | BAM_CDEL;
+        }
+    }
+    bam_lplbuf_push(b, tv->lplbuf);
+    return 0;
 }
 
 int base_draw_aln(tview_t *tv, int tid, int pos)
-	{
-	assert(tv!=NULL);
-	// reset
-	tv->my_clear(tv);
-	tv->curr_tid = tid; tv->left_pos = pos;
-	tv->last_pos = tv->left_pos - 1;
-	tv->ccol = 0;
-	// print ref and consensus
-	if (tv->fai) {
-		char *str;
-		if (tv->ref) free(tv->ref);
-		assert(tv->curr_tid>=0);
-		
-		str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
-		assert(str!=NULL);
-		sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
-		tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
-		free(str);
-	}
-	// draw aln
-	bam_lplbuf_reset(tv->lplbuf);
-	bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
-	bam_lplbuf_push(0, tv->lplbuf);
-
-	while (tv->ccol < tv->mcol) {
-		int pos = tv->last_pos + 1;
-		if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
-		tv->my_mvaddch(tv,1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N');
-		++tv->last_pos;
-	}
-	return 0;
+{
+    assert(tv!=NULL);
+    // reset
+    tv->my_clear(tv);
+    tv->curr_tid = tid; tv->left_pos = pos;
+    tv->last_pos = tv->left_pos - 1;
+    tv->ccol = 0;
+    // print ref and consensus
+    if (tv->fai) {
+        char *str;
+        if (tv->ref) free(tv->ref);
+        assert(tv->curr_tid>=0);
+
+        str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
+        assert(str!=NULL);
+        sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
+        tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
+        free(str);
+        if ( !tv->ref )
+        {
+            fprintf(pysamerr,"Could not read the reference sequence. Is it seekable (plain text or compressed + .gzi indexed with bgzip)?\n");
+            exit(1);
+        }
+    }
+    // draw aln
+    bam_lplbuf_reset(tv->lplbuf);
+    sam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
+    bam_lplbuf_push(0, tv->lplbuf);
+
+    while (tv->ccol < tv->mcol) {
+        int pos = tv->last_pos + 1;
+        if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
+        tv->my_mvaddch(tv,1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N');
+        ++tv->last_pos;
+    }
+    return 0;
 }
 
 
@@ -283,13 +343,12 @@ static void error(const char *format, ...)
 {
     if ( !format )
     {
-        fprintf(pysamerr, "\n");
-        fprintf(pysamerr, "Usage: bamtk tview [options] <aln.bam> [ref.fasta]\n");
-        fprintf(pysamerr, "Options:\n");
-        fprintf(pysamerr, "   -d display      output as (H)tml or (C)urses or (T)ext \n");
-        fprintf(pysamerr, "   -p chr:pos      go directly to this position\n");
-        fprintf(pysamerr, "   -s STR          display only reads from this sample or group\n");
-        fprintf(pysamerr, "\n\n");
+        fprintf(pysamerr,
+"Usage: samtools tview [options] <aln.bam> [ref.fasta]\n"
+"Options:\n"
+"   -d display      output as (H)tml or (C)urses or (T)ext \n"
+"   -p chr:pos      go directly to this position\n"
+"   -s STR          display only reads from this sample or group\n");
     }
     else
     {
@@ -307,9 +366,9 @@ extern tview_t* html_tv_init(const char *fn, const char *fn_fa, const char *samp
 extern tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples);
 
 int bam_tview_main(int argc, char *argv[])
-	{
-	int view_mode=display_ncurses;
-	tview_t* tv=NULL;
+{
+    int view_mode=display_ncurses;
+    tview_t* tv=NULL;
     char *samples=NULL, *position=NULL;
     int c;
     while ((c = getopt(argc, argv, "s:p:d:")) >= 0) {
@@ -317,54 +376,70 @@ int bam_tview_main(int argc, char *argv[])
             case 's': samples=optarg; break;
             case 'p': position=optarg; break;
             case 'd':
-            	{
-            	switch(optarg[0])
-            		{
-            		case 'H': case 'h': view_mode=display_html;break;
-            		case 'T': case 't': view_mode=display_text;break;
-            		case 'C': case 'c': view_mode=display_ncurses;break;
-			default: view_mode=display_ncurses;break;
-			}
-            	break;
-            	}
+            {
+                switch(optarg[0])
+                {
+                    case 'H': case 'h': view_mode=display_html;break;
+                    case 'T': case 't': view_mode=display_text;break;
+                    case 'C': case 'c': view_mode=display_ncurses;break;
+                    default: view_mode=display_ncurses;break;
+                }
+                break;
+            }
             default: error(NULL);
         }
     }
-	if (argc==optind) error(NULL);
-	
-	switch(view_mode)
-		{
-		case display_ncurses:
-			{
-			tv = curses_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
-			break;
-			}
-		case display_text:
-			{
-			tv = text_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
-			break;
-			}
-		case display_html:
-			{
-			tv = html_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
-			break;
-			}
-		}
-	if(tv==NULL)
-		{
-		error("cannot create view");
-		return EXIT_FAILURE;
-		}
-	
-	if ( position )
-	   	 {
-		int _tid = -1, _beg, _end;
-		bam_parse_region(tv->header, position, &_tid, &_beg, &_end);
-		if (_tid >= 0) { tv->curr_tid = _tid; tv->left_pos = _beg; }
-	    	}
-	tv->my_drawaln(tv, tv->curr_tid, tv->left_pos);
-	tv->my_loop(tv);
-	tv->my_destroy(tv);
-	
-	return EXIT_SUCCESS;
-	}
+    if (argc==optind) error(NULL);
+
+    switch(view_mode)
+    {
+        case display_ncurses:
+            {
+            tv = curses_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
+            break;
+            }
+        case display_text:
+            {
+            tv = text_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
+            break;
+            }
+        case display_html:
+            {
+            tv = html_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
+            break;
+            }
+    }
+    if (tv==NULL)
+    {
+        error("cannot create view");
+        return EXIT_FAILURE;
+    }
+
+    if ( position )
+    {
+        int tid, beg, end;
+        *(char *)hts_parse_reg(position, &beg, &end) = '\0';
+        tid = bam_name2id(tv->header, position);
+        if (tid >= 0) { tv->curr_tid = tid; tv->left_pos = beg; }
+    }
+    else if ( tv->fai )
+    {
+        // find the first sequence present in both BAM and the reference file
+        int i;
+        for (i=0; i<tv->header->n_targets; i++)
+        {
+            if ( faidx_has_seq(tv->fai, tv->header->target_name[i]) ) break;
+        }
+        if ( i==tv->header->n_targets )
+        {
+            fprintf(pysamerr,"None of the BAM sequence names present in the fasta file\n");
+            exit(EXIT_FAILURE);
+        }
+        tv->curr_tid = i;
+    }
+    tv->my_drawaln(tv, tv->curr_tid, tv->left_pos);
+    tv->my_loop(tv);
+    tv->my_destroy(tv);
+
+    return EXIT_SUCCESS;
+}
diff --git a/samtools/bam_tview.h b/samtools/bam_tview.h
index 80f0464..305e69f 100644
--- a/samtools/bam_tview.h
+++ b/samtools/bam_tview.h
@@ -1,33 +1,61 @@
+/*  bam_tview.h -- tview subcommand.
+
+    Copyright (C) 2008, 2013 Genome Research Ltd.
+    Portions copyright (C) 2013 Pierre Lindenbaum, Institut du Thorax, INSERM U1087, Université de Nantes.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notices and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #ifndef BAM_TVIEW_H
 #define BAM_TVIEW_H
 
 #include <ctype.h>
-#include <assert.h>
 #include <string.h>
 #include <math.h>
 #include <unistd.h>
 #include <stdarg.h>
-#include "bam.h"
-#include "faidx.h"
+#include <htslib/sam.h>
 #include "bam2bcf.h"
-#include "sam_header.h"
-#include "khash.h"
+#include <htslib/khash.h>
+#include <htslib/hts.h>
+#include <htslib/faidx.h>
+#include "bam_lpileup.h"
+
 
 KHASH_MAP_INIT_STR(kh_rg, const char *)
 
+/* Holds state of Tview */
 typedef struct AbstractTview {
-	int mrow, mcol;
-	
-	bam_index_t *idx;
-	bam_lplbuf_t *lplbuf;
-	bam_header_t *header;
-	bamFile fp;
-	int curr_tid, left_pos;
-	faidx_t *fai;
-	bcf_callaux_t *bca;
-
-	int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins, no_skip, show_name;
-	char *ref;
+    int mrow, mcol;
+
+    hts_idx_t* idx;
+    bam_lplbuf_t* lplbuf;
+    bam_hdr_t* header;
+    samFile* fp;
+    int curr_tid, left_pos;
+    faidx_t* fai;
+    bcf_callaux_t* bca;
+
+    int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins, no_skip, show_name;
+    char *ref;
+    /* maps @RG ID => SM (sample), in practice only used to determine whether a particular RG is in the list of allowed ones */
     khash_t(kh_rg) *rg_hash;
     /* callbacks */
     void (*my_destroy)(struct AbstractTview* );
@@ -66,10 +94,10 @@ void base_tv_destroy(tview_t*);
 int base_draw_aln(tview_t *tv, int tid, int pos);
 
 typedef struct Tixel
-	{
-	int ch;
-	int attributes;
-	}tixel_t;
+    {
+    int ch;
+    int attributes;
+    }tixel_t;
 
 #endif
 
diff --git a/samtools/bam_tview_curses.c.pysam.c b/samtools/bam_tview_curses.c.pysam.c
index fb14594..90f3673 100644
--- a/samtools/bam_tview_curses.c.pysam.c
+++ b/samtools/bam_tview_curses.c.pysam.c
@@ -1,5 +1,30 @@
 #include "pysam.h"
 
+/*  bam_tview_curses.c -- curses tview implementation.
+
+    Copyright (C) 2008-2013 Genome Research Ltd.
+    Portions copyright (C) 2013 Pierre Lindenbaum, Institut du Thorax, INSERM U1087, Université de Nantes.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notices and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #undef _HAVE_CURSES
 
 #if _CURSES_LIB == 0
@@ -25,9 +50,9 @@
 
 
 typedef struct CursesTview {
-	tview_t view;
-	WINDOW *wgoto, *whelp;
-	} curses_tview_t;
+    tview_t view;
+    WINDOW *wgoto, *whelp;
+    } curses_tview_t;
 
 
 
@@ -35,17 +60,17 @@ typedef struct CursesTview {
 #define FROM_TV(ptr) ((curses_tview_t*)ptr)
 
 static void curses_destroy(tview_t* base)
-	{
-	curses_tview_t* tv=(curses_tview_t*)base;
+    {
+    curses_tview_t* tv=(curses_tview_t*)base;
+
 
-	
-	delwin(tv->wgoto); delwin(tv->whelp);
-	endwin();
+    delwin(tv->wgoto); delwin(tv->whelp);
+    endwin();
 
-	base_tv_destroy(base);
-	
-	free(tv);
-	}
+    base_tv_destroy(base);
+
+    free(tv);
+    }
 
 /*
  void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...);
@@ -57,23 +82,23 @@ static void curses_destroy(tview_t* base)
 */
 
 static void curses_mvprintw(struct AbstractTview* tv,int y ,int x,const char* fmt,...)
-	{
-	unsigned int size=tv->mcol+2;
-	char* str=malloc(size);
-	if(str==0) exit(EXIT_FAILURE);
-	va_list argptr;
-  	va_start(argptr, fmt);
-	vsnprintf(str,size, fmt, argptr);
-	va_end(argptr);
-	mvprintw(y,x,str);
-	free(str);
-	}
+    {
+    unsigned int size=tv->mcol+2;
+    char* str=malloc(size);
+    if(str==0) exit(EXIT_FAILURE);
+    va_list argptr;
+    va_start(argptr, fmt);
+    vsnprintf(str,size, fmt, argptr);
+    va_end(argptr);
+    mvprintw(y,x,str);
+    free(str);
+    }
 
 static void curses_mvaddch(struct AbstractTview* tv,int y,int x,int ch)
-	{
-	mvaddch(y,x,ch);
-	}
-    	
+    {
+    mvaddch(y,x,ch);
+    }
+
 static void curses_attron(struct AbstractTview* tv,int flag)
     {
     attron(flag);
@@ -86,7 +111,7 @@ static void curses_clear(struct AbstractTview* tv)
     {
     clear();
     }
-    
+
 static int curses_colorpair(struct AbstractTview* tv,int flag)
     {
     return COLOR_PAIR(flag);
@@ -100,188 +125,198 @@ static int curses_drawaln(struct AbstractTview* tv, int tid, int pos)
 
 
 static void tv_win_goto(curses_tview_t *tv, int *tid, int *pos)
-	{
-	char str[256], *p;
-	int i, l = 0;
-	tview_t *base=(tview_t*)tv;
-	wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
-	mvwprintw(tv->wgoto, 1, 2, "Goto: ");
-	for (;;) {
-		int c = wgetch(tv->wgoto);
-		wrefresh(tv->wgoto);
-		if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
-			if(l > 0) --l;
-		} else if (c == KEY_ENTER || c == '\012' || c == '\015') {
-			int _tid = -1, _beg, _end;
-			if (str[0] == '=') {
-				_beg = strtol(str+1, &p, 10) - 1;
-				if (_beg > 0) {
-					*pos = _beg;
-					return;
-				}
-			} else {
-				bam_parse_region(base->header, str, &_tid, &_beg, &_end);
-				if (_tid >= 0) {
-					*tid = _tid; *pos = _beg;
-					return;
-				}
-			}
-		} else if (isgraph(c)) {
-			if (l < TV_MAX_GOTO) str[l++] = c;
-		} else if (c == '\027') l = 0;
-		else if (c == '\033') return;
-		str[l] = '\0';
-		for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
-		mvwprintw(tv->wgoto, 1, 8, "%s", str);
-	}
+    {
+    char str[256], *p;
+    int i, l = 0;
+    tview_t *base=(tview_t*)tv;
+    wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
+    mvwprintw(tv->wgoto, 1, 2, "Goto: ");
+    for (;;) {
+        int invalid = 0;
+        int c = wgetch(tv->wgoto);
+        wrefresh(tv->wgoto);
+        if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
+            if(l > 0) --l;
+        } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
+            int _tid = -1, _beg, _end;
+            if (str[0] == '=') {
+                _beg = strtol(str+1, &p, 10) - 1;
+                if (_beg > 0) {
+                    *pos = _beg;
+                    return;
+                }
+            } else {
+                char *name_lim = (char *) hts_parse_reg(str, &_beg, &_end);
+                char name_terminator = *name_lim;
+                *name_lim = '\0';
+                _tid = bam_name2id(base->header, str);
+                *name_lim = name_terminator;
+
+                if (_tid >= 0) {
+                    *tid = _tid; *pos = _beg;
+                    return;
+                }
+            }
+
+            // If we get here, the region string is invalid
+            invalid = 1;
+        } else if (isgraph(c)) {
+            if (l < TV_MAX_GOTO) str[l++] = c;
+        } else if (c == '\027') l = 0;
+        else if (c == '\033') return;
+        str[l] = '\0';
+        for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
+        if (invalid) mvwprintw(tv->wgoto, 1, TV_MAX_GOTO - 1, "[Invalid]");
+        mvwprintw(tv->wgoto, 1, 8, "%s", str);
+    }
 }
 
 
 
 
 static void tv_win_help(curses_tview_t *tv) {
-	int r = 1;
-	tview_t* base=(tview_t*)base;
-	WINDOW *win = tv->whelp;
-	wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
-	mvwprintw(win, r++, 2, "        -=-    Help    -=- ");
-	r++;
-	mvwprintw(win, r++, 2, "?          This window");
-	mvwprintw(win, r++, 2, "Arrows     Small scroll movement");
-	mvwprintw(win, r++, 2, "h,j,k,l    Small scroll movement");
-	mvwprintw(win, r++, 2, "H,J,K,L    Large scroll movement");
-	mvwprintw(win, r++, 2, "ctrl-H     Scroll 1k left");
-	mvwprintw(win, r++, 2, "ctrl-L     Scroll 1k right");
-	mvwprintw(win, r++, 2, "space      Scroll one screen");
-	mvwprintw(win, r++, 2, "backspace  Scroll back one screen");
-	mvwprintw(win, r++, 2, "g          Go to specific location");
-	mvwprintw(win, r++, 2, "m          Color for mapping qual");
-	mvwprintw(win, r++, 2, "n          Color for nucleotide");
-	mvwprintw(win, r++, 2, "b          Color for base quality");
-	mvwprintw(win, r++, 2, "c          Color for cs color");
-	mvwprintw(win, r++, 2, "z          Color for cs qual");
-	mvwprintw(win, r++, 2, ".          Toggle on/off dot view");
-	mvwprintw(win, r++, 2, "s          Toggle on/off ref skip");
-	mvwprintw(win, r++, 2, "r          Toggle on/off rd name");
-	mvwprintw(win, r++, 2, "N          Turn on nt view");
-	mvwprintw(win, r++, 2, "C          Turn on cs view");
-	mvwprintw(win, r++, 2, "i          Toggle on/off ins");
-	mvwprintw(win, r++, 2, "q          Exit");
-	r++;
-	mvwprintw(win, r++, 2, "Underline:      Secondary or orphan");
-	mvwprintw(win, r++, 2, "Blue:    0-9    Green: 10-19");
-	mvwprintw(win, r++, 2, "Yellow: 20-29   White: >=30");
-	wrefresh(win);
-	wgetch(win);
+    int r = 1;
+    tview_t* base=(tview_t*)base;
+    WINDOW *win = tv->whelp;
+    wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
+    mvwprintw(win, r++, 2, "        -=-    Help    -=- ");
+    r++;
+    mvwprintw(win, r++, 2, "?          This window");
+    mvwprintw(win, r++, 2, "Arrows     Small scroll movement");
+    mvwprintw(win, r++, 2, "h,j,k,l    Small scroll movement");
+    mvwprintw(win, r++, 2, "H,J,K,L    Large scroll movement");
+    mvwprintw(win, r++, 2, "ctrl-H     Scroll 1k left");
+    mvwprintw(win, r++, 2, "ctrl-L     Scroll 1k right");
+    mvwprintw(win, r++, 2, "space      Scroll one screen");
+    mvwprintw(win, r++, 2, "backspace  Scroll back one screen");
+    mvwprintw(win, r++, 2, "g          Go to specific location");
+    mvwprintw(win, r++, 2, "m          Color for mapping qual");
+    mvwprintw(win, r++, 2, "n          Color for nucleotide");
+    mvwprintw(win, r++, 2, "b          Color for base quality");
+    mvwprintw(win, r++, 2, "c          Color for cs color");
+    mvwprintw(win, r++, 2, "z          Color for cs qual");
+    mvwprintw(win, r++, 2, ".          Toggle on/off dot view");
+    mvwprintw(win, r++, 2, "s          Toggle on/off ref skip");
+    mvwprintw(win, r++, 2, "r          Toggle on/off rd name");
+    mvwprintw(win, r++, 2, "N          Turn on nt view");
+    mvwprintw(win, r++, 2, "C          Turn on cs view");
+    mvwprintw(win, r++, 2, "i          Toggle on/off ins");
+    mvwprintw(win, r++, 2, "q          Exit");
+    r++;
+    mvwprintw(win, r++, 2, "Underline:      Secondary or orphan");
+    mvwprintw(win, r++, 2, "Blue:    0-9    Green: 10-19");
+    mvwprintw(win, r++, 2, "Yellow: 20-29   White: >=30");
+    wrefresh(win);
+    wgetch(win);
 }
 
 static int curses_underline(tview_t* tv)
-	{
-	return A_UNDERLINE;
-	}
-	
+    {
+    return A_UNDERLINE;
+    }
+
 static int curses_loop(tview_t* tv)
-	{
-	int tid, pos;
-	curses_tview_t *CTV=(curses_tview_t *)tv;
-	tid = tv->curr_tid; pos = tv->left_pos;
-	while (1) {
-		int c = getch();
-		switch (c) {
-			case '?': tv_win_help(CTV); break;
-			case '\033':
-			case 'q': goto end_loop;
-			case '/': 
-			case 'g': tv_win_goto(CTV, &tid, &pos); break;
-			case 'm': tv->color_for = TV_COLOR_MAPQ; break;
-			case 'b': tv->color_for = TV_COLOR_BASEQ; break;
-			case 'n': tv->color_for = TV_COLOR_NUCL; break;
-			case 'c': tv->color_for = TV_COLOR_COL; break;
-			case 'z': tv->color_for = TV_COLOR_COLQ; break;
-			case 's': tv->no_skip = !tv->no_skip; break;
-			case 'r': tv->show_name = !tv->show_name; break;
-			case KEY_LEFT:
-			case 'h': --pos; break;
-			case KEY_RIGHT:
-			case 'l': ++pos; break;
-			case KEY_SLEFT:
-			case 'H': pos -= 20; break;
-			case KEY_SRIGHT:
-			case 'L': pos += 20; break;
-			case '.': tv->is_dot = !tv->is_dot; break;
-			case 'N': tv->base_for = TV_BASE_NUCL; break;
-			case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
-			case 'i': tv->ins = !tv->ins; break;
-			case '\010': pos -= 1000; break;
-			case '\014': pos += 1000; break;
-			case ' ': pos += tv->mcol; break;
-			case KEY_UP:
-			case 'j': --tv->row_shift; break;
-			case KEY_DOWN:
-			case 'k': ++tv->row_shift; break;
-			case KEY_BACKSPACE:
-			case '\177': pos -= tv->mcol; break;
-			case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
-			default: continue;
-		}
-		if (pos < 0) pos = 0;
-		if (tv->row_shift < 0) tv->row_shift = 0;
-		tv->my_drawaln(tv, tid, pos);
-	}
+    {
+    int tid, pos;
+    curses_tview_t *CTV=(curses_tview_t *)tv;
+    tid = tv->curr_tid; pos = tv->left_pos;
+    while (1) {
+        int c = getch();
+        switch (c) {
+            case '?': tv_win_help(CTV); break;
+            case '\033':
+            case 'q': goto end_loop;
+            case '/':
+            case 'g': tv_win_goto(CTV, &tid, &pos); break;
+            case 'm': tv->color_for = TV_COLOR_MAPQ; break;
+            case 'b': tv->color_for = TV_COLOR_BASEQ; break;
+            case 'n': tv->color_for = TV_COLOR_NUCL; break;
+            case 'c': tv->color_for = TV_COLOR_COL; break;
+            case 'z': tv->color_for = TV_COLOR_COLQ; break;
+            case 's': tv->no_skip = !tv->no_skip; break;
+            case 'r': tv->show_name = !tv->show_name; break;
+            case KEY_LEFT:
+            case 'h': --pos; break;
+            case KEY_RIGHT:
+            case 'l': ++pos; break;
+            case KEY_SLEFT:
+            case 'H': pos -= 20; break;
+            case KEY_SRIGHT:
+            case 'L': pos += 20; break;
+            case '.': tv->is_dot = !tv->is_dot; break;
+            case 'N': tv->base_for = TV_BASE_NUCL; break;
+            case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
+            case 'i': tv->ins = !tv->ins; break;
+            case '\010': pos -= 1000; break;
+            case '\014': pos += 1000; break;
+            case ' ': pos += tv->mcol; break;
+            case KEY_UP:
+            case 'j': --tv->row_shift; break;
+            case KEY_DOWN:
+            case 'k': ++tv->row_shift; break;
+            case KEY_BACKSPACE:
+            case '\177': pos -= tv->mcol; break;
+            case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
+            default: continue;
+        }
+        if (pos < 0) pos = 0;
+        if (tv->row_shift < 0) tv->row_shift = 0;
+        tv->my_drawaln(tv, tid, pos);
+    }
 end_loop:
-	return 0;
+    return 0;
 }
 
 
 
 
 tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples)
-	{
-	curses_tview_t *tv = (curses_tview_t*)calloc(1, sizeof(curses_tview_t));
-	tview_t* base=(tview_t*)tv;
-	if(tv==0)
-		{
-		fprintf(pysamerr,"Calloc failed\n");
-		return 0;
-		}
-	
-	base_tv_init(base,fn,fn_fa,samples);
-	/* initialize callbacks */
+    {
+    curses_tview_t *tv = (curses_tview_t*)calloc(1, sizeof(curses_tview_t));
+    tview_t* base=(tview_t*)tv;
+    if(tv==0)
+        {
+        fprintf(pysamerr,"Calloc failed\n");
+        return 0;
+        }
+
+    base_tv_init(base,fn,fn_fa,samples);
+    /* initialize callbacks */
 #define SET_CALLBACK(fun) base->my_##fun=curses_##fun;
-	SET_CALLBACK(destroy);
-	SET_CALLBACK(mvprintw);
-	SET_CALLBACK(mvaddch);
-	SET_CALLBACK(attron);
-	SET_CALLBACK(attroff);
-	SET_CALLBACK(clear);
-	SET_CALLBACK(colorpair);
-	SET_CALLBACK(drawaln);
-	SET_CALLBACK(loop);
-	SET_CALLBACK(underline);
+    SET_CALLBACK(destroy);
+    SET_CALLBACK(mvprintw);
+    SET_CALLBACK(mvaddch);
+    SET_CALLBACK(attron);
+    SET_CALLBACK(attroff);
+    SET_CALLBACK(clear);
+    SET_CALLBACK(colorpair);
+    SET_CALLBACK(drawaln);
+    SET_CALLBACK(loop);
+    SET_CALLBACK(underline);
 #undef SET_CALLBACK
 
-	initscr();
-	keypad(stdscr, TRUE);
-	clear();
-	noecho();
-	cbreak();
-	
-	getmaxyx(stdscr, base->mrow, base->mcol);
-	tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
-	tv->whelp = newwin(29, 40, 5, 5);
-	
-	start_color();
-	init_pair(1, COLOR_BLUE, COLOR_BLACK);
-	init_pair(2, COLOR_GREEN, COLOR_BLACK);
-	init_pair(3, COLOR_YELLOW, COLOR_BLACK);
-	init_pair(4, COLOR_WHITE, COLOR_BLACK);
-	init_pair(5, COLOR_GREEN, COLOR_BLACK);
-	init_pair(6, COLOR_CYAN, COLOR_BLACK);
-	init_pair(7, COLOR_YELLOW, COLOR_BLACK);
-	init_pair(8, COLOR_RED, COLOR_BLACK);
-	init_pair(9, COLOR_BLUE, COLOR_BLACK);
-	return base;
-	}
+    initscr();
+    keypad(stdscr, TRUE);
+    clear();
+    noecho();
+    cbreak();
+
+    getmaxyx(stdscr, base->mrow, base->mcol);
+    tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
+    tv->whelp = newwin(29, 40, 5, 5);
+
+    start_color();
+    init_pair(1, COLOR_BLUE, COLOR_BLACK);
+    init_pair(2, COLOR_GREEN, COLOR_BLACK);
+    init_pair(3, COLOR_YELLOW, COLOR_BLACK);
+    init_pair(4, COLOR_WHITE, COLOR_BLACK);
+    init_pair(5, COLOR_GREEN, COLOR_BLACK);
+    init_pair(6, COLOR_CYAN, COLOR_BLACK);
+    init_pair(7, COLOR_YELLOW, COLOR_BLACK);
+    init_pair(8, COLOR_RED, COLOR_BLACK);
+    init_pair(9, COLOR_BLUE, COLOR_BLACK);
+    return base;
+    }
 
 
 #else // #ifdef _HAVE_CURSES
@@ -291,9 +326,9 @@ tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples)
 extern tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples);
 
 tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples)
-	{
-	return text_tv_init(fn,fn_fa,samples);
-	}
+    {
+    return text_tv_init(fn,fn_fa,samples);
+    }
 #endif // #ifdef _HAVE_CURSES
 
-	
+
diff --git a/samtools/bam_tview_html.c.pysam.c b/samtools/bam_tview_html.c.pysam.c
index 95dadd7..e4529a5 100644
--- a/samtools/bam_tview_html.c.pysam.c
+++ b/samtools/bam_tview_html.c.pysam.c
@@ -1,32 +1,56 @@
 #include "pysam.h"
 
+/*  bam_tview_html.c -- HTML tview output.
+
+    Copyright (C) 2013 Pierre Lindenbaum, Institut du Thorax, INSERM U1087, Université de Nantes.
+
+    Author: Pierre Lindenbaum <plindenbaum at yahoo.fr>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <unistd.h>
 #include "bam_tview.h"
 
 #define UNDERLINE_FLAG 10
 
 typedef struct HtmlTview {
-	tview_t view;
-	int row_count;
-	tixel_t** screen;
-	FILE* out;
-	int attributes;/* color... */
-	} html_tview_t;
+    tview_t view;
+    int row_count;
+    tixel_t** screen;
+    FILE* out;
+    int attributes;/* color... */
+    } html_tview_t;
 
 #define FROM_TV(ptr) ((html_tview_t*)ptr)
 
 static void html_destroy(tview_t* base)
-	{
-	int i;
-	html_tview_t* tv=(html_tview_t*)base;
-	if(tv->screen!=NULL)
-		{
-		for(i=0;i< tv->row_count;++i) free(tv->screen[i]);
-		free(tv->screen);
-		}
-	base_tv_destroy(base);
-	free(tv);
-	}
+    {
+    int i;
+    html_tview_t* tv=(html_tview_t*)base;
+    if(tv->screen!=NULL)
+        {
+        for(i=0;i< tv->row_count;++i) free(tv->screen[i]);
+        free(tv->screen);
+        }
+    base_tv_destroy(base);
+    free(tv);
+    }
 
 /*
  void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...);
@@ -38,42 +62,42 @@ static void html_destroy(tview_t* base)
 */
 
 static void html_mvprintw(struct AbstractTview* tv,int y ,int x,const char* fmt,...)
-	{
-	int i,nchars=0;
-	unsigned int size=tv->mcol+2;
-	char* str=malloc(size);
-	if(str==0) exit(EXIT_FAILURE);
-	va_list argptr;
-  	va_start(argptr, fmt);
-	nchars=vsnprintf(str,size, fmt, argptr);
-	va_end(argptr);
-	
-	for(i=0;i< nchars;++i)
-		{
-		tv->my_mvaddch(tv,y,x+i,str[i]);
-		}
-	free(str);
-	}
+    {
+    int i,nchars=0;
+    unsigned int size=tv->mcol+2;
+    char* str=malloc(size);
+    if(str==0) exit(EXIT_FAILURE);
+    va_list argptr;
+    va_start(argptr, fmt);
+    nchars=vsnprintf(str,size, fmt, argptr);
+    va_end(argptr);
+
+    for(i=0;i< nchars;++i)
+        {
+        tv->my_mvaddch(tv,y,x+i,str[i]);
+        }
+    free(str);
+    }
 
 static void html_mvaddch(struct AbstractTview* tv,int y,int x,int ch)
-	{
-	tixel_t* row=NULL;
-	html_tview_t* ptr=FROM_TV(tv);
-	if( x >= tv->mcol ) return; //out of screen
-	while(ptr->row_count<=y)
-		{
-		int x;
-		row=(tixel_t*)calloc(tv->mcol,sizeof(tixel_t));
-		if(row==0)  exit(EXIT_FAILURE);
-		for(x=0;x<tv->mcol;++x) {row[x].ch=' ';row[x].attributes=0;}
-		ptr->screen=(tixel_t**)realloc(ptr->screen,sizeof(tixel_t*)*(ptr->row_count+1));
-		ptr->screen[ptr->row_count++]=row;
-		}
-	row=ptr->screen[y];
-	row[x].ch=ch;
-	row[x].attributes=ptr->attributes;
-	}
-    	
+    {
+    tixel_t* row=NULL;
+    html_tview_t* ptr=FROM_TV(tv);
+    if( x >= tv->mcol ) return; //out of screen
+    while(ptr->row_count<=y)
+        {
+        int x;
+        row=(tixel_t*)calloc(tv->mcol,sizeof(tixel_t));
+        if(row==0)  exit(EXIT_FAILURE);
+        for(x=0;x<tv->mcol;++x) {row[x].ch=' ';row[x].attributes=0;}
+        ptr->screen=(tixel_t**)realloc(ptr->screen,sizeof(tixel_t*)*(ptr->row_count+1));
+        ptr->screen[ptr->row_count++]=row;
+        }
+    row=ptr->screen[y];
+    row[x].ch=ch;
+    row[x].attributes=ptr->attributes;
+    }
+
 static void html_attron(struct AbstractTview* tv,int flag)
     {
     html_tview_t* ptr=FROM_TV(tv);
@@ -81,27 +105,27 @@ static void html_attron(struct AbstractTview* tv,int flag)
 
 
     }
-   
+
 static void html_attroff(struct AbstractTview* tv,int flag)
     {
     html_tview_t* ptr=FROM_TV(tv);
     ptr->attributes &= ~(flag);
     }
-    
+
 static void html_clear(struct AbstractTview* tv)
     {
     html_tview_t* ptr=FROM_TV(tv);
     if(ptr->screen!=NULL)
-	{
-	int i;
-	for(i=0;i< ptr->row_count;++i) free(ptr->screen[i]);
-	free(ptr->screen);
-	ptr->screen=NULL;
-	}
+    {
+    int i;
+    for(i=0;i< ptr->row_count;++i) free(ptr->screen[i]);
+    free(ptr->screen);
+    ptr->screen=NULL;
+    }
     ptr->row_count=0;
     ptr->attributes=0;
     }
-    
+
 static int html_colorpair(struct AbstractTview* tv,int flag)
     {
     return (1 << (flag));
@@ -115,83 +139,83 @@ static int html_drawaln(struct AbstractTview* tv, int tid, int pos)
     base_draw_aln(tv,  tid, pos);
     fputs("<html><head>",ptr->out);
     fprintf(ptr->out,"<title>%s:%d</title>",
-    	tv->header->target_name[tid],
-    	pos+1
-    	);
+        tv->header->target_name[tid],
+        pos+1
+        );
     //style
-   
+
     fputs("<style type='text/css'>\n",ptr->out);
     fputs(".tviewbody { margin:5px; background-color:white;text-align:center;}\n",ptr->out);
     fputs(".tviewtitle {text-align:center;}\n",ptr->out);
     fputs(".tviewpre { margin:5px; background-color:white;}\n",ptr->out);
     #define CSS(id,col) fprintf(ptr->out,".tviewc%d {color:%s;}\n.tviewcu%d {color:%s;text-decoration:underline;}\n",id,col,id,col);
         CSS(0, "black");
-    	CSS(1, "blue");
-	CSS(2, "green");
-	CSS(3, "yellow");
-	CSS(4, "black");
-	CSS(5, "green");
-	CSS(6, "cyan");
-	CSS(7, "yellow");
-	CSS(8, "red");
-	CSS(9, "blue");
+        CSS(1, "blue");
+    CSS(2, "green");
+    CSS(3, "yellow");
+    CSS(4, "black");
+    CSS(5, "green");
+    CSS(6, "cyan");
+    CSS(7, "yellow");
+    CSS(8, "red");
+    CSS(9, "blue");
     #undef CSS
     fputs("</style>",ptr->out);
-    
+
     fputs("</head><body>",ptr->out);
-    
+
       fprintf(ptr->out,"<div class='tviewbody'><div class='tviewtitle'>%s:%d</div>",
-    	tv->header->target_name[tid],
-    	pos+1
-    	);
-    
+        tv->header->target_name[tid],
+        pos+1
+        );
+
     fputs("<pre class='tviewpre'>",ptr->out);
     for(y=0;y< ptr->row_count;++y)
-    	{
-    	
-    	for(x=0;x< tv->mcol;++x)
-	    	{
-	    	
-		
-		if(x== 0 || ptr->screen[y][x].attributes != ptr->screen[y][x-1].attributes)
-	    		{
-	    		int css=0;
-			fprintf(ptr->out,"<span");
-	    		while(css<32)
-	    			{
-	    			//if(y>1) fprintf(pysamerr,"css=%d pow2=%d vs %d\n",css,(1 << (css)),ptr->screen[y][x].attributes);
-	    			if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
-	    				{
-	    				
-	    				fprintf(ptr->out," class='tviewc%s%d'",
-	    					(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)) )!=0?"u":""),
-	    					css);
-	    				break;
-	    				}
-	    			++css;
-	    			}
-
-
-	    		fputs(">",ptr->out);
-	    		}
-		
-		int ch=ptr->screen[y][x].ch;
-		switch(ch)
-			{
-			case '<': fputs("<",ptr->out);break;
-			case '>': fputs(">",ptr->out);break;
-			case '&': fputs("&",ptr->out);break;
-			default: fputc(ch,ptr->out); break;
-			}
-	    	
-	    	
-	    	if(x+1 == tv->mcol  || ptr->screen[y][x].attributes!=ptr->screen[y][x+1].attributes)
-	    		{
-	    		fputs("</span>",ptr->out);
-	    		}
-	    	}
-    	if(y+1 < ptr->row_count) fputs("<br/>",ptr->out);
-    	}
+        {
+
+        for(x=0;x< tv->mcol;++x)
+            {
+
+
+        if(x== 0 || ptr->screen[y][x].attributes != ptr->screen[y][x-1].attributes)
+                {
+                int css=0;
+            fprintf(ptr->out,"<span");
+                while(css<32)
+                    {
+                    //if(y>1) fprintf(pysamerr,"css=%d pow2=%d vs %d\n",css,(1 << (css)),ptr->screen[y][x].attributes);
+                    if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
+                        {
+
+                        fprintf(ptr->out," class='tviewc%s%d'",
+                            (( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)) )!=0?"u":""),
+                            css);
+                        break;
+                        }
+                    ++css;
+                    }
+
+
+                fputs(">",ptr->out);
+                }
+
+        int ch=ptr->screen[y][x].ch;
+        switch(ch)
+            {
+            case '<': fputs("<",ptr->out);break;
+            case '>': fputs(">",ptr->out);break;
+            case '&': fputs("&",ptr->out);break;
+            default: fputc(ch,ptr->out); break;
+            }
+
+
+            if(x+1 == tv->mcol  || ptr->screen[y][x].attributes!=ptr->screen[y][x+1].attributes)
+                {
+                fputs("</span>",ptr->out);
+                }
+            }
+        if(y+1 < ptr->row_count) fputs("<br/>",ptr->out);
+        }
     fputs("</pre></div></body></html>",ptr->out);
     return 0;
     }
@@ -214,138 +238,138 @@ static int text_drawaln(struct AbstractTview* tv, int tid, int pos)
     int y,x;
     html_tview_t* ptr=FROM_TV(tv);
     html_clear(tv);
-    base_draw_aln(tv,  tid, pos); 
+    base_draw_aln(tv,  tid, pos);
     int is_term= isatty(fileno(ptr->out));
-    
+
     for(y=0;y< ptr->row_count;++y)
-    	{
-    	for(x=0;x< tv->mcol;++x)
-	    	{
-	    	if(is_term)
-	    		{
-	    		int css=0;
-	    		while(css<32)
-	    			{
-	    			if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
-	    				{
-	    				break;
-	    				}
-	    			++css;
-	    			}
-    			switch(css)
-    				{
-    				//CSS(0, "black");
-			    	case 1: fputs(ANSI_COLOR_BLUE,ptr->out); break;
-				case 2: fputs(ANSI_COLOR_GREEN,ptr->out); break;
-				case 3: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
-				//CSS(4, "black");
-				case 5: fputs(ANSI_COLOR_GREEN,ptr->out); break;
-				case 6: fputs(ANSI_COLOR_CYAN,ptr->out); break;
-				case 7: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
-				case 8: fputs(ANSI_COLOR_RED,ptr->out); break;
-				case 9: fputs(ANSI_COLOR_BLUE,ptr->out); break;
-				default:break;
-    				}
-    			if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
-    				{
-    				fputs(ANSI_UNDERLINE_SET,ptr->out);
-    				}
-    			
-	    		}
-	    	
-	    	
-	    	int ch=ptr->screen[y][x].ch;
-
-	    	fputc(ch,ptr->out);
-	    	if(is_term)
-	    		{
-	    		fputs(ANSI_COLOR_RESET,ptr->out);
-	    		if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
-    				{
-    				fputs(ANSI_UNDERLINE_UNSET,ptr->out);
-    				}
-	    		}
-	    	}
-    	fputc('\n',ptr->out);
-    	}
+        {
+        for(x=0;x< tv->mcol;++x)
+            {
+            if(is_term)
+                {
+                int css=0;
+                while(css<32)
+                    {
+                    if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
+                        {
+                        break;
+                        }
+                    ++css;
+                    }
+                switch(css)
+                    {
+                    //CSS(0, "black");
+                    case 1: fputs(ANSI_COLOR_BLUE,ptr->out); break;
+                case 2: fputs(ANSI_COLOR_GREEN,ptr->out); break;
+                case 3: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
+                //CSS(4, "black");
+                case 5: fputs(ANSI_COLOR_GREEN,ptr->out); break;
+                case 6: fputs(ANSI_COLOR_CYAN,ptr->out); break;
+                case 7: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
+                case 8: fputs(ANSI_COLOR_RED,ptr->out); break;
+                case 9: fputs(ANSI_COLOR_BLUE,ptr->out); break;
+                default:break;
+                    }
+                if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
+                    {
+                    fputs(ANSI_UNDERLINE_SET,ptr->out);
+                    }
+
+                }
+
+
+            int ch=ptr->screen[y][x].ch;
+
+            fputc(ch,ptr->out);
+            if(is_term)
+                {
+                fputs(ANSI_COLOR_RESET,ptr->out);
+                if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
+                    {
+                    fputs(ANSI_UNDERLINE_UNSET,ptr->out);
+                    }
+                }
+            }
+        fputc('\n',ptr->out);
+        }
     return 0;
     }
 
 
 static int html_loop(tview_t* tv)
-	{
-	//tv->my_drawaln(tv, tv->curr_tid, tv->left_pos);
-	return 0;	
-	}
+    {
+    //tv->my_drawaln(tv, tv->curr_tid, tv->left_pos);
+    return 0;
+    }
 
 static int html_underline(tview_t* tv)
-	{
-	return (1 << UNDERLINE_FLAG);	
-	}
+    {
+    return (1 << UNDERLINE_FLAG);
+    }
 
 /*
 static void init_pair(html_tview_t *tv,int id_ge_1, const char* pen, const char* paper)
-	{
-	
-	}
+    {
+
+    }
 */
 
 tview_t* html_tv_init(const char *fn, const char *fn_fa, const char *samples)
-	{
-	char* colstr=getenv("COLUMNS");
-	html_tview_t *tv = (html_tview_t*)calloc(1, sizeof(html_tview_t));
-	tview_t* base=(tview_t*)tv;
-	if(tv==0)
-		{
-		fprintf(pysamerr,"Calloc failed\n");
-		return 0;
-		}
-	tv->row_count=0;
-	tv->screen=NULL;
-	tv->out=stdout;
-	tv->attributes=0;
-	base_tv_init(base,fn,fn_fa,samples);
-	/* initialize callbacks */
+    {
+    char* colstr=getenv("COLUMNS");
+    html_tview_t *tv = (html_tview_t*)calloc(1, sizeof(html_tview_t));
+    tview_t* base=(tview_t*)tv;
+    if(tv==0)
+        {
+        fprintf(pysamerr,"Calloc failed\n");
+        return 0;
+        }
+    tv->row_count=0;
+    tv->screen=NULL;
+    tv->out=stdout;
+    tv->attributes=0;
+    base_tv_init(base,fn,fn_fa,samples);
+    /* initialize callbacks */
 #define SET_CALLBACK(fun) base->my_##fun=html_##fun;
-	SET_CALLBACK(destroy);
-	SET_CALLBACK(mvprintw);
-	SET_CALLBACK(mvaddch);
-	SET_CALLBACK(attron);
-	SET_CALLBACK(attroff);
-	SET_CALLBACK(clear);
-	SET_CALLBACK(colorpair);
-	SET_CALLBACK(drawaln);
-	SET_CALLBACK(loop);
-	SET_CALLBACK(underline);
+    SET_CALLBACK(destroy);
+    SET_CALLBACK(mvprintw);
+    SET_CALLBACK(mvaddch);
+    SET_CALLBACK(attron);
+    SET_CALLBACK(attroff);
+    SET_CALLBACK(clear);
+    SET_CALLBACK(colorpair);
+    SET_CALLBACK(drawaln);
+    SET_CALLBACK(loop);
+    SET_CALLBACK(underline);
 #undef SET_CALLBACK
 
-	
-	if(colstr!=0)
-		{
-		base->mcol=atoi(colstr);
-		if(base->mcol<10) base->mcol=80;
-		}
-	base->mrow=99999;
-	
+
+    if(colstr!=0)
+        {
+        base->mcol=atoi(colstr);
+        if(base->mcol<10) base->mcol=80;
+        }
+    base->mrow=99999;
+
 /*
-	init_pair(tv,1, "blue", "white");
-	init_pair(tv,2, "green", "white");
-	init_pair(tv,3, "yellow", "white");
-	init_pair(tv,4, "white", "white");
-	init_pair(tv,5, "green", "white");
-	init_pair(tv,6, "cyan", "white");
-	init_pair(tv,7, "yellow", "white");
-	init_pair(tv,8, "red", "white");
-	init_pair(tv,9, "blue", "white");
-	*/
-	return base;
-	}
+    init_pair(tv,1, "blue", "white");
+    init_pair(tv,2, "green", "white");
+    init_pair(tv,3, "yellow", "white");
+    init_pair(tv,4, "white", "white");
+    init_pair(tv,5, "green", "white");
+    init_pair(tv,6, "cyan", "white");
+    init_pair(tv,7, "yellow", "white");
+    init_pair(tv,8, "red", "white");
+    init_pair(tv,9, "blue", "white");
+    */
+    return base;
+    }
 
 
 tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples)
-	{
-	tview_t* tv=html_tv_init(fn,fn_fa,samples);
-	tv->my_drawaln=text_drawaln;
-	return tv;
-	}
+    {
+    tview_t* tv=html_tv_init(fn,fn_fa,samples);
+    tv->my_drawaln=text_drawaln;
+    return tv;
+    }
 
diff --git a/samtools/bamshuf.c.pysam.c b/samtools/bamshuf.c.pysam.c
index b526408..ccc7bb1 100644
--- a/samtools/bamshuf.c.pysam.c
+++ b/samtools/bamshuf.c.pysam.c
@@ -1,12 +1,39 @@
 #include "pysam.h"
 
+/*  bamshuf.c -- bamshuf subcommand.
+
+    Copyright (C) 2012 Broad Institute.
+    Copyright (C) 2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
-#include "sam.h"
-#include "ksort.h"
+#include "htslib/sam.h"
+#include "htslib/bgzf.h"
+#include "htslib/ksort.h"
+#include "samtools.h"
 
 #define DEF_CLEVEL 1
 
@@ -23,127 +50,136 @@ static inline unsigned hash_Wang(unsigned key)
 
 static inline unsigned hash_X31_Wang(const char *s)
 {
-	unsigned h = *s;
-	if (h) {
-		for (++s ; *s; ++s) h = (h << 5) - h + *s;
-		return hash_Wang(h);
-	} else return 0;
+    unsigned h = *s;
+    if (h) {
+        for (++s ; *s; ++s) h = (h << 5) - h + *s;
+        return hash_Wang(h);
+    } else return 0;
 }
 
 typedef struct {
-	unsigned key;
-	bam1_t *b;
+    unsigned key;
+    bam1_t *b;
 } elem_t;
 
 static inline int elem_lt(elem_t x, elem_t y)
 {
-	if (x.key < y.key) return 1;
-	if (x.key == y.key) {
-		int t;
-		t = strcmp(bam_get_qname(x.b), bam_get_qname(y.b));
-		if (t < 0) return 1;
-		return (t == 0 && ((x.b->core.flag>>6&3) < (y.b->core.flag>>6&3)));
-	} else return 0;
+    if (x.key < y.key) return 1;
+    if (x.key == y.key) {
+        int t;
+        t = strcmp(bam_get_qname(x.b), bam_get_qname(y.b));
+        if (t < 0) return 1;
+        return (t == 0 && ((x.b->core.flag>>6&3) < (y.b->core.flag>>6&3)));
+    } else return 0;
 }
 
 KSORT_INIT(bamshuf, elem_t, elem_lt)
 
-static void bamshuf(const char *fn, int n_files, const char *pre, int clevel, int is_stdout)
+static int bamshuf(const char *fn, int n_files, const char *pre, int clevel, int is_stdout)
 {
-	BGZF *fp, *fpw, **fpt;
-	char **fnt, modew[8];
-	bam1_t *b;
-	int i, l;
-	bam_hdr_t *h;
-	int64_t *cnt;
-
-	// split
-	fp = strcmp(fn, "-")? bgzf_open(fn, "r") : bgzf_dopen(fileno(stdin), "r");
-	assert(fp);
-	h = bam_hdr_read(fp);
-	fnt = (char**)calloc(n_files, sizeof(void*));
-	fpt = (BGZF**)calloc(n_files, sizeof(void*));
-	cnt = (int64_t*)calloc(n_files, 8);
-	l = strlen(pre);
-	for (i = 0; i < n_files; ++i) {
-		fnt[i] = (char*)calloc(l + 10, 1);
-		sprintf(fnt[i], "%s.%.4d.bam", pre, i);
-		fpt[i] = bgzf_open(fnt[i], "w1");
-		bam_hdr_write(fpt[i], h);
-	}
-	b = bam_init1();
-	while (bam_read1(fp, b) >= 0) {
-		uint32_t x;
-		x = hash_X31_Wang(bam_get_qname(b)) % n_files;
-		bam_write1(fpt[x], b);
-		++cnt[x];
-	}
-	bam_destroy1(b);
-	for (i = 0; i < n_files; ++i) bgzf_close(fpt[i]);
-	free(fpt);
-	bgzf_close(fp);
-	// merge
-	sprintf(modew, "w%d", (clevel >= 0 && clevel <= 9)? clevel : DEF_CLEVEL);
-	if (!is_stdout) { // output to a file
-		char *fnw = (char*)calloc(l + 5, 1);
-		sprintf(fnw, "%s.bam", pre);
-		fpw = bgzf_open(fnw, modew);
-		free(fnw);
-	} else fpw = bgzf_dopen(fileno(stdout), modew); // output to stdout
-	bam_hdr_write(fpw, h);
-	bam_hdr_destroy(h);
-	for (i = 0; i < n_files; ++i) {
-		int64_t j, c = cnt[i];
-		elem_t *a;
-		fp = bgzf_open(fnt[i], "r");
-		bam_hdr_destroy(bam_hdr_read(fp));
-		a = (elem_t*)calloc(c, sizeof(elem_t));
-		for (j = 0; j < c; ++j) {
-			a[j].b = bam_init1();
-			// added by pysam to prevent seg-fault
-			// was: assert( bam_read1(fp, a[j].b );
-			// Assume that assertion was optimized out
-			// and a[j].b not set.
-			int _l = 0;
-			_l = bam_read1(fp, a[j].b);
-			assert( _l >= 0 );
-			a[j].key = hash_X31_Wang(bam_get_qname(a[j].b));
-		}
-		bgzf_close(fp);
-		unlink(fnt[i]);
-		free(fnt[i]);
-		ks_introsort(bamshuf, c, a);
-		for (j = 0; j < c; ++j) {
-			bam_write1(fpw, a[j].b);
-			bam_destroy1(a[j].b);
-		}
-		free(a);
-	}
-	bgzf_close(fpw);
-	free(fnt); free(cnt);
+    BGZF *fp, *fpw, **fpt;
+    char **fnt, modew[8];
+    bam1_t *b;
+    int i, l;
+    bam_hdr_t *h;
+    int64_t *cnt;
+
+    // split
+    fp = strcmp(fn, "-")? bgzf_open(fn, "r") : bgzf_dopen(fileno(stdin), "r");
+    if (fp == NULL) {
+        print_error_errno("Cannot open input file \"%s\"", fn);
+        return 1;
+    }
+
+    h = bam_hdr_read(fp);
+    fnt = (char**)calloc(n_files, sizeof(char*));
+    fpt = (BGZF**)calloc(n_files, sizeof(BGZF*));
+    cnt = (int64_t*)calloc(n_files, 8);
+    l = strlen(pre);
+    for (i = 0; i < n_files; ++i) {
+        fnt[i] = (char*)calloc(l + 10, 1);
+        sprintf(fnt[i], "%s.%.4d.bam", pre, i);
+        fpt[i] = bgzf_open(fnt[i], "w1");
+        if (fpt[i] == NULL) {
+            print_error_errno("Cannot open intermediate file \"%s\"", fnt[i]);
+            return 1;
+        }
+        bam_hdr_write(fpt[i], h);
+    }
+    b = bam_init1();
+    while (bam_read1(fp, b) >= 0) {
+        uint32_t x;
+        x = hash_X31_Wang(bam_get_qname(b)) % n_files;
+        bam_write1(fpt[x], b);
+        ++cnt[x];
+    }
+    bam_destroy1(b);
+    for (i = 0; i < n_files; ++i) bgzf_close(fpt[i]);
+    free(fpt);
+    bgzf_close(fp);
+    // merge
+    sprintf(modew, "w%d", (clevel >= 0 && clevel <= 9)? clevel : DEF_CLEVEL);
+    if (!is_stdout) { // output to a file
+        char *fnw = (char*)calloc(l + 5, 1);
+        sprintf(fnw, "%s.bam", pre);
+        fpw = bgzf_open(fnw, modew);
+        free(fnw);
+    } else fpw = bgzf_dopen(fileno(stdout), modew); // output to stdout
+    if (fpw == NULL) {
+        if (is_stdout) print_error_errno("Cannot open standard output");
+        else print_error_errno("Cannot open output file \"%s.bam\"", pre);
+        return 1;
+    }
+
+    bam_hdr_write(fpw, h);
+    bam_hdr_destroy(h);
+    for (i = 0; i < n_files; ++i) {
+        int64_t j, c = cnt[i];
+        elem_t *a;
+        fp = bgzf_open(fnt[i], "r");
+        bam_hdr_destroy(bam_hdr_read(fp));
+        a = (elem_t*)calloc(c, sizeof(elem_t));
+        for (j = 0; j < c; ++j) {
+            a[j].b = bam_init1();
+            assert(bam_read1(fp, a[j].b) >= 0);
+            a[j].key = hash_X31_Wang(bam_get_qname(a[j].b));
+        }
+        bgzf_close(fp);
+        unlink(fnt[i]);
+        free(fnt[i]);
+        ks_introsort(bamshuf, c, a);
+        for (j = 0; j < c; ++j) {
+            bam_write1(fpw, a[j].b);
+            bam_destroy1(a[j].b);
+        }
+        free(a);
+    }
+    bgzf_close(fpw);
+    free(fnt); free(cnt);
+    return 0;
 }
 
 int main_bamshuf(int argc, char *argv[])
 {
-	int c, n_files = 64, clevel = DEF_CLEVEL, is_stdout = 0, is_un = 0;
-	while ((c = getopt(argc, argv, "n:l:uO")) >= 0) {
-		switch (c) {
-		case 'n': n_files = atoi(optarg); break;
-		case 'l': clevel = atoi(optarg); break;
-		case 'u': is_un = 1; break;
-		case 'O': is_stdout = 1; break;
-		}
-	}
-	if (is_un) clevel = 0;
-	if (optind + 2 > argc) {
-		fprintf(pysamerr, "\nUsage:   bamshuf [-Ou] [-n nFiles] [-c cLevel] <in.bam> <out.prefix>\n\n");
-		fprintf(pysamerr, "Options: -O      output to stdout\n");
-		fprintf(pysamerr, "         -u      uncompressed BAM output\n");
-		fprintf(pysamerr, "         -l INT  compression level [%d]\n", DEF_CLEVEL);
-		fprintf(pysamerr, "         -n INT  number of temporary files [%d]\n", n_files);
-		fprintf(pysamerr, "\n");
-		return 1;
-	}
-	bamshuf(argv[optind], n_files, argv[optind+1], clevel, is_stdout);
-	return 0;
+    int c, n_files = 64, clevel = DEF_CLEVEL, is_stdout = 0, is_un = 0;
+    while ((c = getopt(argc, argv, "n:l:uO")) >= 0) {
+        switch (c) {
+        case 'n': n_files = atoi(optarg); break;
+        case 'l': clevel = atoi(optarg); break;
+        case 'u': is_un = 1; break;
+        case 'O': is_stdout = 1; break;
+        }
+    }
+    if (is_un) clevel = 0;
+    if (optind + 2 > argc) {
+        fprintf(pysamerr,
+"Usage:   samtools bamshuf [-Ou] [-n nFiles] [-c cLevel] <in.bam> <out.prefix>\n\n"
+"Options: -O      output to stdout\n"
+"         -u      uncompressed BAM output\n"
+"         -l INT  compression level [%d]\n" // DEF_CLEVEL
+"         -n INT  number of temporary files [%d]\n", // n_files
+                DEF_CLEVEL, n_files);
+        return 1;
+    }
+    return bamshuf(argv[optind], n_files, argv[optind+1], clevel, is_stdout);
 }
diff --git a/samtools/bcftools/__init__.py b/samtools/bcftools/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/samtools/bcftools/bcf.c.pysam.c b/samtools/bcftools/bcf.c.pysam.c
deleted file mode 100644
index dcde831..0000000
--- a/samtools/bcftools/bcf.c.pysam.c
+++ /dev/null
@@ -1,398 +0,0 @@
-#include "pysam.h"
-
-#include <string.h>
-#include <ctype.h>
-#include <stdio.h>
-#include "kstring.h"
-#include "bcf.h"
-
-bcf_t *bcf_open(const char *fn, const char *mode)
-{
-	bcf_t *b;
-	b = calloc(1, sizeof(bcf_t));
-	if (strchr(mode, 'w')) {
-		b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdout), mode);
-	} else {
-		b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdin), mode);
-	}
-	return b;
-}
-
-int bcf_close(bcf_t *b)
-{
-	int ret;
-	if (b == 0) return 0;
-	ret = bgzf_close(b->fp);
-	free(b);
-	return ret;
-}
-
-int bcf_hdr_write(bcf_t *b, const bcf_hdr_t *h)
-{
-	if (b == 0 || h == 0) return -1;
-	bgzf_write(b->fp, "BCF\4", 4);
-	bgzf_write(b->fp, &h->l_nm, 4);
-	bgzf_write(b->fp, h->name, h->l_nm);
-	bgzf_write(b->fp, &h->l_smpl, 4);
-	bgzf_write(b->fp, h->sname, h->l_smpl);
-	bgzf_write(b->fp, &h->l_txt, 4);
-	bgzf_write(b->fp, h->txt, h->l_txt);
-	bgzf_flush(b->fp);
-	return 16 + h->l_nm + h->l_smpl + h->l_txt;
-}
-
-bcf_hdr_t *bcf_hdr_read(bcf_t *b)
-{
-	uint8_t magic[4];
-	bcf_hdr_t *h;
-	if (b == 0) return 0;
-	h = calloc(1, sizeof(bcf_hdr_t));
-	bgzf_read(b->fp, magic, 4);
-	bgzf_read(b->fp, &h->l_nm, 4);
-	h->name = malloc(h->l_nm);
-	bgzf_read(b->fp, h->name, h->l_nm);
-	bgzf_read(b->fp, &h->l_smpl, 4);
-	h->sname = malloc(h->l_smpl);
-	bgzf_read(b->fp, h->sname, h->l_smpl);
-	bgzf_read(b->fp, &h->l_txt, 4);
-	h->txt = malloc(h->l_txt);
-	bgzf_read(b->fp, h->txt, h->l_txt);
-	bcf_hdr_sync(h);
-	return h;
-}
-
-void bcf_hdr_destroy(bcf_hdr_t *h)
-{
-	if (h == 0) return;
-	free(h->name); free(h->sname); free(h->txt); free(h->ns); free(h->sns);
-	free(h);
-}
-
-static inline char **cnt_null(int l, char *str, int *_n)
-{
-	int n = 0;
-	char *p, **list;
-	*_n = 0;
-	if (l == 0 || str == 0) return 0;
-	for (p = str; p != str + l; ++p)
-		if (*p == 0) ++n;
-	*_n = n;
-	list = calloc(n, sizeof(void*));
-	list[0] = str;
-	for (p = str, n = 1; p < str + l - 1; ++p)
-		if (*p == 0) list[n++] = p + 1;
-	return list;
-}
-
-int bcf_hdr_sync(bcf_hdr_t *b)
-{
-	if (b == 0) return -1;
-	if (b->ns) free(b->ns);
-	if (b->sns) free(b->sns);
-	if (b->l_nm) b->ns = cnt_null(b->l_nm, b->name, &b->n_ref);
-	else b->ns = 0, b->n_ref = 0;
-	b->sns = cnt_null(b->l_smpl, b->sname, &b->n_smpl);
-	return 0;
-}
-
-int bcf_sync(bcf1_t *b)
-{
-	char *p, *tmp[5];
-	int i, n, n_smpl = b->n_smpl;
-	ks_tokaux_t aux;
-	// set ref, alt, flt, info, fmt
-	b->ref = b->alt = b->flt = b->info = b->fmt = 0;
-	for (p = b->str, n = 0; p < b->str + b->l_str; ++p) {
-		if (*p == 0 && p+1 != b->str + b->l_str) {
-			if (n == 5) {
-				++n;
-				break;
-			} else tmp[n++] = p + 1;
-		}
-	}
-	if (n != 5) {
-		fprintf(pysamerr, "[%s] incorrect number of fields (%d != 5) at %d:%d\n", __func__, n, b->tid, b->pos);
-		return -1;
-	}
-	b->ref = tmp[0]; b->alt = tmp[1]; b->flt = tmp[2]; b->info = tmp[3]; b->fmt = tmp[4];
-	// set n_alleles
-	if (*b->alt == 0) b->n_alleles = 1;
-	else {
-		for (p = b->alt, n = 1; *p; ++p)
-			if (*p == ',') ++n;
-		b->n_alleles = n + 1;
-	}
-	// set n_gi and gi[i].fmt
-	for (p = b->fmt, n = 1; *p; ++p)
-		if (*p == ':') ++n;
-	if (n > b->m_gi) {
-		int old_m = b->m_gi;
-		b->m_gi = n;
-		kroundup32(b->m_gi);
-		b->gi = realloc(b->gi, b->m_gi * sizeof(bcf_ginfo_t));
-		memset(b->gi + old_m, 0, (b->m_gi - old_m) * sizeof(bcf_ginfo_t));
-	}
-	b->n_gi = n;
-	for (p = kstrtok(b->fmt, ":", &aux), n = 0; p; p = kstrtok(0, 0, &aux))
-		b->gi[n++].fmt = bcf_str2int(p, aux.p - p);
-	// set gi[i].len
-	for (i = 0; i < b->n_gi; ++i) {
-		if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
-			b->gi[i].len = b->n_alleles * (b->n_alleles + 1) / 2;
-		} else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("HQ", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
-			b->gi[i].len = 2;
-		} else if (b->gi[i].fmt == bcf_str2int("GQ", 2) || b->gi[i].fmt == bcf_str2int("GT", 2)) {
-			b->gi[i].len = 1;
-		} else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
-			b->gi[i].len = 4;
-		} else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
-			b->gi[i].len = b->n_alleles * (b->n_alleles + 1) / 2 * 4;
-		}
-		b->gi[i].data = realloc(b->gi[i].data, n_smpl * b->gi[i].len);
-	}
-	return 0;
-}
-
-int bcf_write(bcf_t *bp, const bcf_hdr_t *h, const bcf1_t *b)
-{
-	int i, l = 0;
-	if (b == 0) return -1;
-	bgzf_write(bp->fp, &b->tid, 4);
-	bgzf_write(bp->fp, &b->pos, 4);
-	bgzf_write(bp->fp, &b->qual, 4);
-	bgzf_write(bp->fp, &b->l_str, 4);
-	bgzf_write(bp->fp, b->str, b->l_str);
-	l = 12 + b->l_str;
-	for (i = 0; i < b->n_gi; ++i) {
-		bgzf_write(bp->fp, b->gi[i].data, b->gi[i].len * h->n_smpl);
-		l += b->gi[i].len * h->n_smpl;
-	}
-	return l;
-}
-
-int bcf_read(bcf_t *bp, const bcf_hdr_t *h, bcf1_t *b)
-{
-	int i, l = 0;
-	if (b == 0) return -1;
-	if (bgzf_read(bp->fp, &b->tid, 4) == 0) return -1;
-	b->n_smpl = h->n_smpl;
-	bgzf_read(bp->fp, &b->pos, 4);
-	bgzf_read(bp->fp, &b->qual, 4);
-	bgzf_read(bp->fp, &b->l_str, 4);
-	if (b->l_str > b->m_str) {
-		b->m_str = b->l_str;
-		kroundup32(b->m_str);
-		b->str = realloc(b->str, b->m_str);
-	}
-	bgzf_read(bp->fp, b->str, b->l_str);
-	l = 12 + b->l_str;
-	if (bcf_sync(b) < 0) return -2;
-	for (i = 0; i < b->n_gi; ++i) {
-		bgzf_read(bp->fp, b->gi[i].data, b->gi[i].len * h->n_smpl);
-		l += b->gi[i].len * h->n_smpl;
-	}
-	return l;
-}
-
-int bcf_destroy(bcf1_t *b)
-{
-	int i;
-	if (b == 0) return -1;
-	free(b->str);
-	for (i = 0; i < b->m_gi; ++i)
-		free(b->gi[i].data);
-	free(b->gi);
-	free(b);
-	return 0;
-}
-
-static inline void fmt_str(const char *p, kstring_t *s)
-{
-	if (*p == 0) kputc('.', s);
-	else kputs(p, s);
-}
-
-void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s)
-{
-	int i, j, x;
-	s->l = 0;
-	if (h->n_ref) kputs(h->ns[b->tid], s);
-	else kputw(b->tid, s);
-	kputc('\t', s);
-	kputw(b->pos + 1, s); kputc('\t', s);
-	fmt_str(b->str, s); kputc('\t', s);
-	fmt_str(b->ref, s); kputc('\t', s);
-	fmt_str(b->alt, s); kputc('\t', s);
-	ksprintf(s, "%.3g", b->qual); kputc('\t', s);
-	fmt_str(b->flt, s); kputc('\t', s);
-	fmt_str(b->info, s);
-	if (b->fmt[0]) {
-		kputc('\t', s);
-		fmt_str(b->fmt, s);
-	}
-	x = b->n_alleles * (b->n_alleles + 1) / 2;
-	if (b->n_gi == 0) return;
-    int iPL = -1;
-    if ( b->n_alleles > 2 ) {
-        for (i=0; i<b->n_gi; i++) {
-            if ( b->gi[i].fmt == bcf_str2int("PL", 2) ) {
-                iPL = i;
-                break;
-            }
-        }
-    }
-	for (j = 0; j < h->n_smpl; ++j) {
-        int ploidy = b->ploidy ? b->ploidy[j] : 2;
-		kputc('\t', s);
-		for (i = 0; i < b->n_gi; ++i) {
-			if (i) kputc(':', s);
-			if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
-				uint8_t *d = (uint8_t*)b->gi[i].data + j * x;
-				int k;
-                if ( ploidy==1 )
-                    for (k=0; k<b->n_alleles; k++)
-                    {
-                        if (k>0) kputc(',', s);
-                        kputw(d[(k+1)*(k+2)/2-1], s);
-                    }
-                else
-                    for (k = 0; k < x; ++k) {
-                        if (k > 0) kputc(',', s);
-                        kputw(d[k], s);
-                    }
-			} else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
-				kputw(((uint16_t*)b->gi[i].data)[j], s);
-			} else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
-				kputw(((uint8_t*)b->gi[i].data)[j], s);
-			} else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
-				kputw(((int32_t*)b->gi[i].data)[j], s);
-			} else if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
-                int y = ((uint8_t*)b->gi[i].data)[j];
-                if ( ploidy==1 )
-                {
-                    if ( y>>7&1 )
-                        kputc('.', s);
-                    else 
-                        kputc('0' + (y>>3&7), s);
-                }
-                else
-                {
-                    if ( y>>7&1 )
-                        kputsn("./.", 3, s);
-                    else { 
-                        kputc('0' + (y>>3&7), s);
-                        kputc("/|"[y>>6&1], s);
-                        kputc('0' + (y&7), s);
-                    }
-                }
-			} else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
-				float *d = (float*)b->gi[i].data + j * x;
-				int k;
-				//printf("- %lx\n", d);
-				for (k = 0; k < x; ++k) {
-					if (k > 0) kputc(',', s);
-					ksprintf(s, "%.2f", d[k]);
-				}
-			} else kputc('.', s); // custom fields
-		}
-	}
-}
-
-char *bcf_fmt(const bcf_hdr_t *h, bcf1_t *b)
-{
-	kstring_t s;
-	s.l = s.m = 0; s.s = 0;
-	bcf_fmt_core(h, b, &s);
-	return s.s;
-}
-
-int bcf_append_info(bcf1_t *b, const char *info, int l)
-{
-	int shift = b->fmt - b->str;
-	int l_fmt = b->l_str - shift;
-	char *ori = b->str;
-	if (b->l_str + l > b->m_str) { // enlarge if necessary
-		b->m_str = b->l_str + l;
-		kroundup32(b->m_str);
-		b->str = realloc(b->str, b->m_str);
-	}
-	memmove(b->str + shift + l, b->str + shift, l_fmt); // move the FORMAT field
-	memcpy(b->str + shift - 1, info, l); // append to the INFO field
-	b->str[shift + l - 1] = '\0';
-	b->fmt = b->str + shift + l;
-	b->l_str += l;
-	if (ori != b->str) bcf_sync(b); // synchronize when realloc changes the pointer
-	return 0;
-}
-
-int remove_tag(char *str, const char *tag, char delim)
-{
-    char *tmp = str, *p;
-    int len_diff = 0, ori_len = strlen(str);
-    while ( *tmp && (p = strstr(tmp,tag)) )
-    {
-        if ( p>str )
-        {
-            if ( *(p-1)!=delim ) { tmp=p+1; continue; } // shared substring
-            p--;
-        }
-        char *q=p+1;
-        while ( *q && *q!=delim ) q++;
-        if ( p==str && *q ) q++;        // the tag is first, don't move the delim char
-        len_diff += q-p;
-        if ( ! *q ) { *p = 0; break; }  // the tag was last, no delim follows
-        else
-            memmove(p,q,ori_len-(int)(p-str)-(int)(q-p));  // *q==delim
-    }
-    if ( len_diff==ori_len )
-        str[0]='.', str[1]=0, len_diff--;
-
-    return len_diff;
-}
-
-
-void rm_info(kstring_t *s, const char *key)
-{
-    char *p = s->s; 
-    int n = 0;
-    while ( n<4 )
-    {
-        if ( !*p ) n++;
-        p++;
-    }
-    char *q = p+1; 
-    while ( *q && q-s->s<s->l ) q++;
-
-    int nrm = remove_tag(p, key, ';');
-    if ( nrm )
-        memmove(q-nrm, q, s->s+s->l-q+1);
-    s->l -= nrm;
-}
-
-int bcf_cpy(bcf1_t *r, const bcf1_t *b)
-{
-	char *t1 = r->str;
-	bcf_ginfo_t *t2 = r->gi;
-	int i, t3 = r->m_str, t4 = r->m_gi;
-	*r = *b;
-	r->str = t1; r->gi = t2; r->m_str = t3; r->m_gi = t4;
-	if (r->m_str < b->m_str) {
-		r->m_str = b->m_str;
-		r->str = realloc(r->str, r->m_str);
-	}
-	memcpy(r->str, b->str, r->m_str);
-	bcf_sync(r); // calling bcf_sync() is simple but inefficient
-	for (i = 0; i < r->n_gi; ++i)
-		memcpy(r->gi[i].data, b->gi[i].data, r->n_smpl * r->gi[i].len);
-	return 0;
-}
-
-int bcf_is_indel(const bcf1_t *b)
-{
-	char *p;
-	if (strlen(b->ref) > 1) return 1;
-	for (p = b->alt; *p; ++p)
-		if (*p != ',' && p[1] != ',' && p[1] != '\0')
-			return 1;
-	return 0;
-}
diff --git a/samtools/bcftools/bcf.h b/samtools/bcftools/bcf.h
deleted file mode 100644
index 1cc13c9..0000000
--- a/samtools/bcftools/bcf.h
+++ /dev/null
@@ -1,197 +0,0 @@
-/* The MIT License
-
-   Copyright (c) 2010 Broad Institute
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-/* Contact: Heng Li <lh3 at live.co.uk> */
-
-#ifndef BCF_H
-#define BCF_H
-
-#define BCF_VERSION "0.1.19-44428cd"
-
-#include <stdint.h>
-#include <zlib.h>
-
-#ifndef BCF_LITE
-#include "bgzf.h"
-typedef BGZF *bcfFile;
-#else
-typedef gzFile bcfFile;
-#define bgzf_open(fn, mode) gzopen(fn, mode)
-#define bgzf_fdopen(fd, mode) gzdopen(fd, mode)
-#define bgzf_close(fp) gzclose(fp)
-#define bgzf_read(fp, buf, len) gzread(fp, buf, len)
-#define bgzf_write(fp, buf, len)
-#define bgzf_flush(fp)
-#endif
-
-/*
-  A member in the structs below is said to "primary" if its content
-  cannot be inferred from other members in any of structs below; a
-  member is said to be "derived" if its content can be derived from
-  other members. For example, bcf1_t::str is primary as this comes from
-  the input data, while bcf1_t::info is derived as it can always be
-  correctly set if we know bcf1_t::str. Derived members are for quick
-  access to the content and must be synchronized with the primary data.
- */
-
-typedef struct {
-	uint32_t fmt; // format of the block, set by bcf_str2int(). 
-	int len; // length of data for each individual
-	void *data; // concatenated data
-	// derived info: fmt, len (<-bcf1_t::fmt)
-} bcf_ginfo_t;
-
-typedef struct {
-	int32_t tid, pos; // refID and 0-based position
-	int32_t l_str, m_str; // length and the allocated size of ->str
-	float qual; // SNP quality
-	char *str; // concatenated string of variable length strings in VCF (from col.2 to col.7)
-	char *ref, *alt, *flt, *info, *fmt; // they all point to ->str; no memory allocation
-	int n_gi, m_gi; // number and the allocated size of geno fields
-	bcf_ginfo_t *gi; // array of geno fields
-	int n_alleles, n_smpl; // number of alleles and samples
-	// derived info: ref, alt, flt, info, fmt (<-str), n_gi (<-fmt), n_alleles (<-alt), n_smpl (<-bcf_hdr_t::n_smpl)
-    uint8_t *ploidy;    // ploidy of all samples; if NULL, ploidy of 2 is assumed.
-} bcf1_t;
-
-typedef struct {
-	int32_t n_ref, n_smpl; // number of reference sequences and samples
-	int32_t l_nm; // length of concatenated sequence names; 0 padded
-	int32_t l_smpl; // length of concatenated sample names; 0 padded
-	int32_t l_txt; // length of header text (lines started with ##)
-	char *name, *sname, *txt; // concatenated sequence names, sample names and header text
-	char **ns, **sns; // array of sequence and sample names; point to name and sname, respectively
-	// derived info: n_ref (<-name), n_smpl (<-sname), ns (<-name), sns (<-sname)
-} bcf_hdr_t;
-
-typedef struct {
-	int is_vcf; // if the file in operation is a VCF
-	void *v; // auxillary data structure for VCF
-	bcfFile fp; // file handler for BCF
-} bcf_t;
-
-struct __bcf_idx_t;
-typedef struct __bcf_idx_t bcf_idx_t;
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-	// open a BCF file; for BCF file only
-	bcf_t *bcf_open(const char *fn, const char *mode);
-	// close file
-	int bcf_close(bcf_t *b);
-	// read one record from BCF; return -1 on end-of-file, and <-1 for errors
-	int bcf_read(bcf_t *bp, const bcf_hdr_t *h, bcf1_t *b);
-	// call this function if b->str is changed
-	int bcf_sync(bcf1_t *b);
-	// write a BCF record
-	int bcf_write(bcf_t *bp, const bcf_hdr_t *h, const bcf1_t *b);
-	// read the BCF header; BCF only
-	bcf_hdr_t *bcf_hdr_read(bcf_t *b);
-	// write the BCF header
-	int bcf_hdr_write(bcf_t *b, const bcf_hdr_t *h);
-	// set bcf_hdr_t::ns and bcf_hdr_t::sns
-	int bcf_hdr_sync(bcf_hdr_t *b);
-	// destroy the header
-	void bcf_hdr_destroy(bcf_hdr_t *h);
-	// destroy a record
-	int bcf_destroy(bcf1_t *b);
-	// BCF->VCF conversion
-	char *bcf_fmt(const bcf_hdr_t *h, bcf1_t *b);
-	// append more info
-	int bcf_append_info(bcf1_t *b, const char *info, int l);
-    // remove tag
-    int remove_tag(char *string, const char *tag, char delim);
-    // remove info tag, string is the kstring holder of bcf1_t.str
-    void rm_info(kstring_t *string, const char *key);
-	// copy
-	int bcf_cpy(bcf1_t *r, const bcf1_t *b);
-
-	// open a VCF or BCF file if "b" is set in "mode"
-	bcf_t *vcf_open(const char *fn, const char *mode);
-	// close a VCF/BCF file
-	int vcf_close(bcf_t *bp);
-	// read the VCF/BCF header
-	bcf_hdr_t *vcf_hdr_read(bcf_t *bp);
-	// read the sequence dictionary from a separate file; required for VCF->BCF conversion
-	int vcf_dictread(bcf_t *bp, bcf_hdr_t *h, const char *fn);
-	// read a VCF/BCF record; return -1 on end-of-file and <-1 for errors
-	int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b);
-	// write the VCF header
-	int vcf_hdr_write(bcf_t *bp, const bcf_hdr_t *h);
-	// write a VCF record
-	int vcf_write(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b);
-
-	// keep the first n alleles and discard the rest
-	int bcf_shrink_alt(bcf1_t *b, int n);
-    // keep the masked alleles and discard the rest
-	void bcf_fit_alt(bcf1_t *b, int mask);
-	// convert GL to PL
-	int bcf_gl2pl(bcf1_t *b);
-	// if the site is an indel
-	int bcf_is_indel(const bcf1_t *b);
-	bcf_hdr_t *bcf_hdr_subsam(const bcf_hdr_t *h0, int n, char *const* samples, int *list);
-	int bcf_subsam(int n_smpl, int *list, bcf1_t *b);
-	// move GT to the first FORMAT field
-	int bcf_fix_gt(bcf1_t *b);
-	// update PL generated by old samtools
-	int bcf_fix_pl(bcf1_t *b);
-	// convert PL to GLF-like 10-likelihood GL
-	int bcf_gl10(const bcf1_t *b, uint8_t *gl);
-	// convert up to 4 INDEL alleles to GLF-like 10-likelihood GL
-	int bcf_gl10_indel(const bcf1_t *b, uint8_t *gl);
-
-	// string hash table
-	void *bcf_build_refhash(bcf_hdr_t *h);
-	void bcf_str2id_destroy(void *_hash);
-	void bcf_str2id_thorough_destroy(void *_hash);
-	int bcf_str2id_add(void *_hash, const char *str);
-	int bcf_str2id(void *_hash, const char *str);
-	void *bcf_str2id_init(void);
-
-	// indexing related functions
-	int bcf_idx_build(const char *fn);
-	uint64_t bcf_idx_query(const bcf_idx_t *idx, int tid, int beg);
-	int bcf_parse_region(void *str2id, const char *str, int *tid, int *begin, int *end);
-	bcf_idx_t *bcf_idx_load(const char *fn);
-	void bcf_idx_destroy(bcf_idx_t *idx);
-
-#ifdef __cplusplus
-}
-#endif
-
-static inline uint32_t bcf_str2int(const char *str, int l)
-{
-	int i;
-	uint32_t x = 0;
-	for (i = 0; i < l && i < 4; ++i) {
-		if (str[i] == 0) return x;
-		x = x<<8 | str[i];
-	}
-	return x;
-}
-
-#endif
diff --git a/samtools/bcftools/bcf2qcall.c.pysam.c b/samtools/bcftools/bcf2qcall.c.pysam.c
deleted file mode 100644
index 6dc7e0c..0000000
--- a/samtools/bcftools/bcf2qcall.c.pysam.c
+++ /dev/null
@@ -1,93 +0,0 @@
-#include "pysam.h"
-
-#include <errno.h>
-#include <math.h>
-#include <string.h>
-#include <stdlib.h>
-#include "bcf.h"
-
-static int8_t nt4_table[256] = {
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4 /*'-'*/, 4, 4,
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 0, 4, 1,  4, 4, 4, 2,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  3, 4, 4, 4, -1, 4, 4, 4,  4, 4, 4, 4, 
-	4, 0, 4, 1,  4, 4, 4, 2,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  3, 4, 4, 4, -1, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4
-};
-
-static int read_I16(bcf1_t *b, int anno[16])
-{
-	char *p;
-	int i;
-	if ((p = strstr(b->info, "I16=")) == 0) return -1;
-	p += 4;
-	for (i = 0; i < 16; ++i) {
-		anno[i] = strtol(p, &p, 10);
-		if (anno[i] == 0 && (errno == EINVAL || errno == ERANGE)) return -2;
-		++p;
-	}
-	return 0;
-}
-
-int bcf_2qcall(bcf_hdr_t *h, bcf1_t *b)
-{
-	int a[4], k, g[10], l, map[4], k1, j, i, i0, anno[16], dp, mq, d_rest;
-	char *s;
-	if (b->ref[1] != 0 || b->n_alleles > 4) return -1; // ref is not a single base
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
-	if (i == b->n_gi) return -1; // no PL
-	if (read_I16(b, anno) != 0) return -1; // no I16; FIXME: can be improved
-	d_rest = dp = anno[0] + anno[1] + anno[2] + anno[3];
-	if (dp == 0) return -1; // depth is zero
-	mq = (int)(sqrt((double)(anno[9] + anno[11]) / dp) + .499);
-	i0 = i;
-	a[0] = nt4_table[(int)b->ref[0]];
-	if (a[0] > 3) return -1; // ref is not A/C/G/T
-	a[1] = a[2] = a[3] = -2; // -1 has a special meaning
-	if (b->alt[0] == 0) return -1; // no alternate allele
-	map[0] = map[1] = map[2] = map[3] = -2;
-	map[a[0]] = 0;
-	for (k = 0, s = b->alt, k1 = -1; k < 3 && *s; ++k, s += 2) {
-		if (s[1] != ',' && s[1] != 0) return -1; // ALT is not single base
-		a[k+1] = nt4_table[(int)*s];
-		if (a[k+1] >= 0) map[a[k+1]] = k+1;
-		else k1 = k+1;
-		if (s[1] == 0) break;
-	}
-	for (k = 0; k < 4; ++k)
-		if (map[k] < 0) map[k] = k1;
-	for (i = 0; i < h->n_smpl; ++i) {
-		int d;
-		uint8_t *p = b->gi[i0].data + i * b->gi[i0].len;
-		for (j = 0; j < b->gi[i0].len; ++j)
-			if (p[j]) break;
-		d = (int)((double)d_rest / (h->n_smpl - i) + .499);
-		if (d == 0) d = 1;
-		if (j == b->gi[i0].len) d = 0;
-		d_rest -= d;
-		for (k = j = 0; k < 4; ++k) {
-			for (l = k; l < 4; ++l) {
-				int t, x = map[k], y = map[l];
-				if (x > y) t = x, x = y, y = t; // swap
-				g[j++] = p[y * (y+1) / 2 + x];
-			}
-		}
-		printf("%s\t%d\t%c", h->ns[b->tid], b->pos+1, *b->ref);
-		printf("\t%d\t%d\t0", d, mq);
-		for (j = 0; j < 10; ++j)
-			printf("\t%d", g[j]);
-		printf("\t%s\n", h->sns[i]);
-	}
-	return 0;
-}
diff --git a/samtools/bcftools/bcfutils.c.pysam.c b/samtools/bcftools/bcfutils.c.pysam.c
deleted file mode 100644
index 9bcea7d..0000000
--- a/samtools/bcftools/bcfutils.c.pysam.c
+++ /dev/null
@@ -1,506 +0,0 @@
-#include "pysam.h"
-
-#include <string.h>
-#include <math.h>
-#include <assert.h>
-#include "bcf.h"
-#include "kstring.h"
-#include "khash.h"
-KHASH_MAP_INIT_STR(str2id, int)
-
-#ifdef _WIN32
-#define srand48(x) srand(x)
-#define drand48() ((double)rand() / RAND_MAX)
-#endif
-
-// FIXME: valgrind report a memory leak in this function. Probably it does not get deallocated...
-void *bcf_build_refhash(bcf_hdr_t *h)
-{
-	khash_t(str2id) *hash;
-	int i, ret;
-	hash = kh_init(str2id);
-	for (i = 0; i < h->n_ref; ++i) {
-		khint_t k;
-		k = kh_put(str2id, hash, h->ns[i], &ret); // FIXME: check ret
-		kh_val(hash, k) = i;
-	}
-	return hash;
-}
-
-void *bcf_str2id_init()
-{
-	return kh_init(str2id);
-}
-
-void bcf_str2id_destroy(void *_hash)
-{
-	khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
-	if (hash) kh_destroy(str2id, hash); // Note that strings are not freed.
-}
-
-void bcf_str2id_thorough_destroy(void *_hash)
-{
-	khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
-	khint_t k;
-	if (hash == 0) return;
-	for (k = 0; k < kh_end(hash); ++k)
-		if (kh_exist(hash, k)) free((char*)kh_key(hash, k));
-	kh_destroy(str2id, hash);
-}
-
-int bcf_str2id(void *_hash, const char *str)
-{
-	khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
-	khint_t k;
-	if (!hash) return -1;
-	k = kh_get(str2id, hash, str);
-	return k == kh_end(hash)? -1 : kh_val(hash, k);
-}
-
-int bcf_str2id_add(void *_hash, const char *str)
-{
-	khint_t k;
-	int ret;
-	khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
-	if (!hash) return -1;
-	k = kh_put(str2id, hash, str, &ret);
-	if (ret == 0) return kh_val(hash, k);
-	kh_val(hash, k) = kh_size(hash) - 1;
-	return kh_val(hash, k);
-}
-
-void bcf_fit_alt(bcf1_t *b, int mask)
-{
-    mask |= 1; // REF must be always present
-
-    int i,j,nals=0;
-    for (i=0; i<sizeof(int); i++)
-        if ( mask&1<<i) nals++;
-    if ( b->n_alleles <= nals ) return;
-
-    // update ALT, in principle any of the alleles can be removed
-    char *p;
-    if ( nals>1 ) 
-    {
-        char *dst, *src;
-        int n=0, nalts=nals-1;
-        for (src=dst=p=b->alt, i=1; *p; p++)
-        {
-            if ( *p!=',' ) continue;
-
-            if ( mask&1<<i )
-            {
-                n++;
-                if ( src!=dst )
-                {
-                    memmove(dst,src,p-src);
-                    dst += p-src;
-                }
-                else dst = p;
-                if ( n<nalts ) { *dst=','; dst++; }
-            }
-            i++;
-
-            if ( n>=nalts ) { *dst=0; break; }
-            src = p+1;
-        }
-        if ( n<nalts )
-        {
-            memmove(dst,src,p-src);
-            dst += p-src;
-            *dst = 0;
-        }
-        p = dst;
-    }
-    else p = b->alt, *p = '\0';
-    p++;
-    memmove(p, b->flt, b->str + b->l_str - b->flt);
-    b->l_str -= b->flt - p;
-
-    // update PL and GT
-    int ipl=-1, igt=-1;
-    for (i = 0; i < b->n_gi; ++i) 
-    {
-        bcf_ginfo_t *g = b->gi + i;
-        if (g->fmt == bcf_str2int("PL", 2)) ipl = i;
-        if (g->fmt == bcf_str2int("GT", 2)) igt = i;
-    }
-
-    // .. create mapping between old and new indexes
-    int npl = nals * (nals+1) / 2;
-    int *map = malloc(sizeof(int)*(npl>b->n_alleles ? npl : b->n_alleles));
-    int kori=0,knew=0;
-    for (i=0; i<b->n_alleles; i++)
-    {
-        for (j=0; j<=i; j++)
-        {
-            int skip=0;
-            if ( i && !(mask&1<<i) ) skip=1;
-            if ( j && !(mask&1<<j) ) skip=1;
-            if ( !skip ) { map[knew++] = kori; }
-            kori++;
-        }
-    }
-    // .. apply to all samples
-    int n_smpl = b->n_smpl;
-    for (i = 0; i < b->n_gi; ++i) 
-    {
-        bcf_ginfo_t *g = b->gi + i;
-        if (g->fmt == bcf_str2int("PL", 2)) 
-        {
-            g->len = npl;
-            uint8_t *d = (uint8_t*)g->data;
-            int ismpl, npl_ori = b->n_alleles * (b->n_alleles + 1) / 2;
-            for (knew=ismpl=0; ismpl<n_smpl; ismpl++)
-            {
-                uint8_t *dl = d + ismpl * npl_ori;
-                for (j=0; j<npl; j++) d[knew++] = dl[map[j]];
-            }
-        } // FIXME: to add GL
-    }
-    // update GTs
-    map[0] = 0;
-    for (i=1, knew=0; i<b->n_alleles; i++)
-        map[i] = mask&1<<i ? ++knew : -1;
-    for (i=0; i<n_smpl; i++)
-    {
-        uint8_t gt = ((uint8_t*)b->gi[igt].data)[i];
-        int a1 = (gt>>3)&7;
-        int a2 = gt&7;
-        assert( map[a1]>=0 && map[a2]>=0 );
-        ((uint8_t*)b->gi[igt].data)[i] = ((1<<7|1<<6)&gt) | map[a1]<<3 | map[a2];
-    }
-    free(map);
-    b->n_alleles = nals;
-    bcf_sync(b);
-}
-
-int bcf_shrink_alt(bcf1_t *b, int n)
-{
-	char *p;
-	int i, j, k, n_smpl = b->n_smpl;
-	if (b->n_alleles <= n) return -1;
-	// update ALT
-	if (n > 1) {
-		for (p = b->alt, k = 1; *p; ++p)
-			if (*p == ',' && ++k == n) break;
-		*p = '\0';
-	} else p = b->alt, *p = '\0';
-	++p;
-	memmove(p, b->flt, b->str + b->l_str - b->flt);
-	b->l_str -= b->flt - p;
-	// update PL
-	for (i = 0; i < b->n_gi; ++i) {
-		bcf_ginfo_t *g = b->gi + i;
-		if (g->fmt == bcf_str2int("PL", 2)) {
-			int l, x = b->n_alleles * (b->n_alleles + 1) / 2;
-			uint8_t *d = (uint8_t*)g->data;
-			g->len = n * (n + 1) / 2;
-			for (l = k = 0; l < n_smpl; ++l) {
-				uint8_t *dl = d + l * x;
-				for (j = 0; j < g->len; ++j) d[k++] = dl[j];
-			}
-		} // FIXME: to add GL
-	}
-	b->n_alleles = n;
-	bcf_sync(b);
-	return 0;
-}
-
-int bcf_gl2pl(bcf1_t *b)
-{
-	char *p;
-	int i, n_smpl = b->n_smpl;
-	bcf_ginfo_t *g;
-	float *d0;
-	uint8_t *d1;
-	if (strstr(b->fmt, "PL")) return -1;
-	if ((p = strstr(b->fmt, "GL")) == 0) return -1;
-	*p = 'P';
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == bcf_str2int("GL", 2))
-			break;
-	g = b->gi + i;
-	g->fmt = bcf_str2int("PL", 2);
-	g->len /= 4; // 4 == sizeof(float)
-	d0 = (float*)g->data; d1 = (uint8_t*)g->data;
-	for (i = 0; i < n_smpl * g->len; ++i) {
-		int x = (int)(-10. * d0[i] + .499);
-		if (x > 255) x = 255;
-		if (x < 0) x = 0;
-		d1[i] = x;
-	}
-	return 0;
-}
-/* FIXME: this function will fail given AB:GTX:GT. BCFtools never
- * produces such FMT, but others may do. */
-int bcf_fix_gt(bcf1_t *b)
-{
-	char *s;
-	int i;
-	uint32_t tmp;
-	bcf_ginfo_t gt;
-	// check the presence of the GT FMT
-	if ((s = strstr(b->fmt, ":GT")) == 0) return 0; // no GT or GT is already the first
-	assert(s[3] == '\0' || s[3] == ':'); // :GTX in fact
-	tmp = bcf_str2int("GT", 2);
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == tmp) break;
-	if (i == b->n_gi) return 0; // no GT in b->gi; probably a bug...
-	gt = b->gi[i];
-	// move GT to the first
-	for (; i > 0; --i) b->gi[i] = b->gi[i-1];
-	b->gi[0] = gt;
-    if ( s[3]==0 )
-        memmove(b->fmt + 3, b->fmt, s - b->fmt);        // :GT
-    else
-        memmove(b->fmt + 3, b->fmt, s - b->fmt + 1);    // :GT:
-	b->fmt[0] = 'G'; b->fmt[1] = 'T'; b->fmt[2] = ':';
-	return 0;
-}
-
-int bcf_fix_pl(bcf1_t *b)
-{
-	int i;
-	uint32_t tmp;
-	uint8_t *PL, *swap;
-	bcf_ginfo_t *gi;
-	// pinpoint PL
-	tmp = bcf_str2int("PL", 2);
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == tmp) break;
-	if (i == b->n_gi) return 0;
-	// prepare
-	gi = b->gi + i;
-	PL = (uint8_t*)gi->data;
-	swap = alloca(gi->len);
-	// loop through individuals
-	for (i = 0; i < b->n_smpl; ++i) {
-		int k, l, x;
-		uint8_t *PLi = PL + i * gi->len;
-		memcpy(swap, PLi, gi->len);
-		for (k = x = 0; k < b->n_alleles; ++k)
-			for (l = k; l < b->n_alleles; ++l)
-				PLi[l*(l+1)/2 + k] = swap[x++];
-	}
-	return 0;
-}
-
-int bcf_smpl_covered(const bcf1_t *b)
-{
-	int i, j, n = 0;
-	uint32_t tmp;
-	bcf_ginfo_t *gi;
-	// pinpoint PL
-	tmp = bcf_str2int("PL", 2);
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == tmp) break;
-	if (i == b->n_gi) return 0;
-	// count how many samples having PL!=[0..0]
-	gi = b->gi + i;
-	for (i = 0; i < b->n_smpl; ++i) {
-		uint8_t *PLi = ((uint8_t*)gi->data) + i * gi->len;
-		for (j = 0; j < gi->len; ++j)
-			if (PLi[j]) break;
-		if (j < gi->len) ++n;
-	}
-	return n;
-}
-
-static void *locate_field(const bcf1_t *b, const char *fmt, int l)
-{
-	int i;
-	uint32_t tmp;
-	tmp = bcf_str2int(fmt, l);
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == tmp) break;
-	return i == b->n_gi? 0 : b->gi[i].data;
-}
-
-int bcf_anno_max(bcf1_t *b)
-{
-	int k, max_gq, max_sp, n_het;
-	kstring_t str;
-	uint8_t *gt, *gq;
-	int32_t *sp;
-	max_gq = max_sp = n_het = 0;
-	gt = locate_field(b, "GT", 2);
-	if (gt == 0) return -1;
-	gq = locate_field(b, "GQ", 2);
-	sp = locate_field(b, "SP", 2);
-	if (sp)
-		for (k = 0; k < b->n_smpl; ++k)
-			if (gt[k]&0x3f)
-				max_sp = max_sp > (int)sp[k]? max_sp : sp[k];
-	if (gq)
-		for (k = 0; k < b->n_smpl; ++k)
-			if (gt[k]&0x3f)
-				max_gq = max_gq > (int)gq[k]? max_gq : gq[k];
-	for (k = 0; k < b->n_smpl; ++k) {
-		int a1, a2;
-		a1 = gt[k]&7; a2 = gt[k]>>3&7;
-		if ((!a1 && a2) || (!a2 && a1)) { // a het
-			if (gq == 0) ++n_het;
-			else if (gq[k] >= 20) ++n_het;
-		}
-	}
-	if (n_het) max_sp -= (int)(4.343 * log(n_het) + .499);
-	if (max_sp < 0) max_sp = 0;
-	memset(&str, 0, sizeof(kstring_t));
-	if (*b->info) kputc(';', &str);
-	ksprintf(&str, "MXSP=%d;MXGQ=%d", max_sp, max_gq);
-	bcf_append_info(b, str.s, str.l);
-	free(str.s);
-	return 0;
-}
-
-// FIXME: only data are shuffled; the header is NOT
-int bcf_shuffle(bcf1_t *b, int seed)
-{
-	int i, j, *a;
-	if (seed > 0) srand48(seed);
-	a = malloc(b->n_smpl * sizeof(int));
-	for (i = 0; i < b->n_smpl; ++i) a[i] = i;
-	for (i = b->n_smpl; i > 1; --i) {
-		int tmp;
-		j = (int)(drand48() * i);
-		tmp = a[j]; a[j] = a[i-1]; a[i-1] = tmp;
-	}
-	for (j = 0; j < b->n_gi; ++j) {
-		bcf_ginfo_t *gi = b->gi + j;
-		uint8_t *swap, *data = (uint8_t*)gi->data;
-		swap = malloc(gi->len * b->n_smpl);
-		for (i = 0; i < b->n_smpl; ++i)
-			memcpy(swap + gi->len * a[i], data + gi->len * i, gi->len);
-		free(gi->data);
-		gi->data = swap;
-	}
-	free(a);
-	return 0;
-}
-
-bcf_hdr_t *bcf_hdr_subsam(const bcf_hdr_t *h0, int n, char *const* samples, int *list)
-{
-	int i, ret, j;
-	khint_t k;
-	bcf_hdr_t *h;
-	khash_t(str2id) *hash;
-	kstring_t s;
-	s.l = s.m = 0; s.s = 0;
-	hash = kh_init(str2id);
-	for (i = 0; i < h0->n_smpl; ++i) {
-		k = kh_put(str2id, hash, h0->sns[i], &ret);
-		kh_val(hash, k) = i;
-	}
-	for (i = j = 0; i < n; ++i) {
-		k = kh_get(str2id, hash, samples[i]);
-		if (k != kh_end(hash)) {
-			list[j++] = kh_val(hash, k);
-			kputs(samples[i], &s); kputc('\0', &s);
-		}
-	}
-	if (j < n) 
-    {
-        fprintf(pysamerr, "<%s> %d samples in the list but not in BCF.", __func__, n - j);
-        exit(1);
-    }
-	kh_destroy(str2id, hash);
-	h = calloc(1, sizeof(bcf_hdr_t));
-	*h = *h0;
-	h->ns = 0; h->sns = 0;
-	h->name = malloc(h->l_nm); memcpy(h->name, h0->name, h->l_nm);
-	h->txt = calloc(1, h->l_txt + 1); memcpy(h->txt, h0->txt, h->l_txt);
-	h->l_smpl = s.l; h->sname = s.s;
-	bcf_hdr_sync(h);
-	return h;
-}
-
-int bcf_subsam(int n_smpl, int *list, bcf1_t *b)
-{
-	int i, j;
-	for (j = 0; j < b->n_gi; ++j) {
-		bcf_ginfo_t *gi = b->gi + j;
-		uint8_t *swap;
-		swap = malloc(gi->len * b->n_smpl);
-		for (i = 0; i < n_smpl; ++i)
-			memcpy(swap + i * gi->len, (uint8_t*)gi->data + list[i] * gi->len, gi->len);
-		free(gi->data);
-		gi->data = swap;
-	}
-	b->n_smpl = n_smpl;
-	return 0;
-}
-
-static int8_t nt4_table[128] = {
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4 /*'-'*/, 4, 4,
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 0, 4, 1,  4, 4, 4, 2,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  3, 4, 4, 4, -1, 4, 4, 4,  4, 4, 4, 4, 
-	4, 0, 4, 1,  4, 4, 4, 2,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  3, 4, 4, 4, -1, 4, 4, 4,  4, 4, 4, 4
-};
-
-int bcf_gl10(const bcf1_t *b, uint8_t *gl)
-{
-	int a[4], k, l, map[4], k1, j, i;
-	const bcf_ginfo_t *PL;
-	char *s;
-	if (b->ref[1] != 0 || b->n_alleles > 4) return -1; // ref is not a single base or >4 alleles
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
-	if (i == b->n_gi) return -1; // no PL
-	PL = b->gi + i;
-	a[0] = nt4_table[(int)b->ref[0]];
-	if (a[0] > 3 || a[0] < 0) return -1; // ref is not A/C/G/T
-	a[1] = a[2] = a[3] = -2; // -1 has a special meaning
-	if (b->alt[0] == 0) return -1; // no alternate allele
-	map[0] = map[1] = map[2] = map[3] = -2;
-	map[a[0]] = 0;
-	for (k = 0, s = b->alt, k1 = -1; k < 3 && *s; ++k, s += 2) {
-		if (s[1] != ',' && s[1] != 0) return -1; // ALT is not single base
-		a[k+1] = nt4_table[(int)*s];
-		if (a[k+1] >= 0) map[a[k+1]] = k+1;
-		else k1 = k + 1;
-		if (s[1] == 0) break; // the end of the ALT string
-	}
-	for (k = 0; k < 4; ++k)
-		if (map[k] < 0) map[k] = k1;
-	for (i = 0; i < b->n_smpl; ++i) {
-		const uint8_t *p = PL->data + i * PL->len; // the PL for the i-th individual
-		uint8_t *g = gl + 10 * i;
-		for (k = j = 0; k < 4; ++k) {
-			for (l = k; l < 4; ++l) {
-				int t, x = map[k], y = map[l];
-				if (x > y) t = x, x = y, y = t; // make sure x is the smaller
-				g[j++] = p[y * (y+1) / 2 + x];
-			}
-		}
-	}
-	return 0;
-}
-
-int bcf_gl10_indel(const bcf1_t *b, uint8_t *gl)
-{
-	int k, l, j, i;
-	const bcf_ginfo_t *PL;
-	if (b->alt[0] == 0) return -1; // no alternate allele
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
-	if (i == b->n_gi) return -1; // no PL
-	PL = b->gi + i;
-	for (i = 0; i < b->n_smpl; ++i) {
-		const uint8_t *p = PL->data + i * PL->len; // the PL for the i-th individual
-		uint8_t *g = gl + 10 * i;
-		for (k = j = 0; k < 4; ++k) {
-			for (l = k; l < 4; ++l) {
-				int t, x = k, y = l;
-				if (x > y) t = x, x = y, y = t; // make sure x is the smaller
-				x = y * (y+1) / 2 + x;
-				g[j++] = x < PL->len? p[x] : 255;
-			}
-		}
-	}
-	return 0;
-}
diff --git a/samtools/bcftools/call1.c.pysam.c b/samtools/bcftools/call1.c.pysam.c
deleted file mode 100644
index 2e5e5e0..0000000
--- a/samtools/bcftools/call1.c.pysam.c
+++ /dev/null
@@ -1,635 +0,0 @@
-#include "pysam.h"
-
-#include <unistd.h>
-#include <stdlib.h>
-#include <math.h>
-#include <zlib.h>
-#include <errno.h>
-#include "bcf.h"
-#include "prob1.h"
-#include "kstring.h"
-#include "time.h"
-
-#ifdef _WIN32
-#define srand48(x) srand(x)
-#define lrand48() rand()
-#endif
-
-#include "kseq.h"
-KSTREAM_INIT(gzFile, gzread, 16384)
-
-#define VC_NO_GENO 2
-#define VC_BCFOUT  4
-#define VC_CALL    8
-#define VC_VARONLY 16
-#define VC_VCFIN   32
-#define VC_UNCOMP  64
-#define VC_KEEPALT 256
-#define VC_ACGT_ONLY 512
-#define VC_QCALL   1024
-#define VC_CALL_GT 2048
-#define VC_ADJLD   4096
-#define VC_NO_INDEL 8192
-#define VC_ANNO_MAX 16384
-#define VC_FIX_PL   32768
-#define VC_EM       0x10000
-#define VC_PAIRCALL 0x20000
-#define VC_QCNT     0x40000
-#define VC_INDEL_ONLY 0x80000
-
-typedef struct {
-	int flag, prior_type, n1, n_sub, *sublist, n_perm;
-	uint32_t *trio_aux;
-	char *prior_file, **subsam, *fn_dict;
-	uint8_t *ploidy;
-	double theta, pref, indel_frac, min_perm_p, min_smpl_frac, min_lrt, min_ma_lrt;
-	void *bed;
-} viewconf_t;
-
-void *bed_read(const char *fn);
-void bed_destroy(void *_h);
-int bed_overlap(const void *_h, const char *chr, int beg, int end);
-
-static double ttest(int n1, int n2, int a[4])
-{
-	extern double kf_betai(double a, double b, double x);
-	double t, v, u1, u2;
-	if (n1 == 0 || n2 == 0 || n1 + n2 < 3) return 1.0;
-	u1 = (double)a[0] / n1; u2 = (double)a[2] / n2;
-	if (u1 <= u2) return 1.;
-	t = (u1 - u2) / sqrt(((a[1] - n1 * u1 * u1) + (a[3] - n2 * u2 * u2)) / (n1 + n2 - 2) * (1./n1 + 1./n2));
-	v = n1 + n2 - 2;
-//	printf("%d,%d,%d,%d,%lf,%lf,%lf\n", a[0], a[1], a[2], a[3], t, u1, u2);
-	return t < 0.? 1. : .5 * kf_betai(.5*v, .5, v/(v+t*t));
-}
-
-static int test16_core(int anno[16], anno16_t *a)
-{
-	extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
-	double left, right;
-	int i;
-	a->p[0] = a->p[1] = a->p[2] = a->p[3] = 1.;
-	memcpy(a->d, anno, 4 * sizeof(int));
-	a->depth = anno[0] + anno[1] + anno[2] + anno[3];
-	a->is_tested = (anno[0] + anno[1] > 0 && anno[2] + anno[3] > 0);
-	if (a->depth == 0) return -1;
-	a->mq = (int)(sqrt((anno[9] + anno[11]) / a->depth) + .499);
-	kt_fisher_exact(anno[0], anno[1], anno[2], anno[3], &left, &right, &a->p[0]);
-	for (i = 1; i < 4; ++i)
-		a->p[i] = ttest(anno[0] + anno[1], anno[2] + anno[3], anno+4*i);
-	return 0;
-}
-
-int test16(bcf1_t *b, anno16_t *a)
-{
-	char *p;
-	int i, anno[16];
-	a->p[0] = a->p[1] = a->p[2] = a->p[3] = 1.;
-	a->d[0] = a->d[1] = a->d[2] = a->d[3] = 0.;
-	a->mq = a->depth = a->is_tested = 0;
-	if ((p = strstr(b->info, "I16=")) == 0) return -1;
-	p += 4;
-	for (i = 0; i < 16; ++i) {
-		errno = 0; anno[i] = strtol(p, &p, 10);
-		if (anno[i] == 0 && (errno == EINVAL || errno == ERANGE)) return -2;
-		++p;
-	}
-	return test16_core(anno, a);
-}
-
-static int update_bcf1(bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p1rst_t *pr, double pref, int flag, double em[10], int cons_llr, int64_t cons_gt)
-{
-	kstring_t s;
-	int has_I16, is_var;
-	double fq, r;
-	anno16_t a;
-
-	has_I16 = test16(b, &a) >= 0? 1 : 0;
-	//rm_info(b, "I16="); // FIXME: probably this function has a bug. If I move it below, I16 will not be removed!
-
-	memset(&s, 0, sizeof(kstring_t));
-	kputc('\0', &s); kputs(b->ref, &s); kputc('\0', &s);
-	kputs(b->alt, &s); kputc('\0', &s); kputc('\0', &s);
-	kputs(b->info, &s);
-	if (b->info[0]) kputc(';', &s);
-	{ // print EM
-		if (em[0] >= 0) ksprintf(&s, "AF1=%.4g", 1 - em[0]);
-		if (em[4] >= 0 && em[4] <= 0.05) ksprintf(&s, ";G3=%.4g,%.4g,%.4g;HWE=%.3g", em[3], em[2], em[1], em[4]);
-		if (em[5] >= 0 && em[6] >= 0) ksprintf(&s, ";AF2=%.4g,%.4g", 1 - em[5], 1 - em[6]);
-		if (em[7] >= 0) ksprintf(&s, ";LRT=%.3g", em[7]);
-		if (em[8] >= 0) ksprintf(&s, ";LRT2=%.3g", em[8]);
-	}
-	if (cons_llr > 0) {
-		ksprintf(&s, ";CLR=%d", cons_llr);
-		if (cons_gt > 0)
-			ksprintf(&s, ";UGT=%c%c%c;CGT=%c%c%c", cons_gt&0xff, cons_gt>>8&0xff, cons_gt>>16&0xff,
-				     cons_gt>>32&0xff, cons_gt>>40&0xff, cons_gt>>48&0xff);
-	}
-	if (pr == 0) { // if pr is unset, return
-		kputc('\0', &s); kputs(b->fmt, &s); kputc('\0', &s);
-		free(b->str);
-		b->m_str = s.m; b->l_str = s.l; b->str = s.s;
-		bcf_sync(b);
-		return 1;
-	}
-
-	is_var = (pr->p_ref < pref);
-	r = is_var? pr->p_ref : pr->p_var;
-
-//	ksprintf(&s, ";CI95=%.4g,%.4g", pr->cil, pr->cih); // FIXME: when EM is not used, ";" should be omitted!
-	ksprintf(&s, ";AC1=%d", pr->ac);
-	if (has_I16) ksprintf(&s, ";DP4=%d,%d,%d,%d;MQ=%d", a.d[0], a.d[1], a.d[2], a.d[3], a.mq);
-	fq = pr->p_ref_folded < 0.5? -4.343 * log(pr->p_ref_folded) : 4.343 * log(pr->p_var_folded);
-	if (fq < -999) fq = -999;
-	if (fq > 999) fq = 999;
-	ksprintf(&s, ";FQ=%.3g", fq);
-	if (pr->cmp[0] >= 0.) { // two sample groups
-		int i, q[3];
-		for (i = 1; i < 3; ++i) {
-			double x = pr->cmp[i] + pr->cmp[0]/2.;
-			q[i] = x == 0? 255 : (int)(-4.343 * log(x) + .499);
-			if (q[i] > 255) q[i] = 255;
-		}
-		if (pr->perm_rank >= 0) ksprintf(&s, ";PR=%d", pr->perm_rank);
-		// ksprintf(&s, ";LRT3=%.3g", pr->lrt);
-		ksprintf(&s, ";PCHI2=%.3g;PC2=%d,%d", q[1], q[2], pr->p_chi2);
-	}
-	if (has_I16 && a.is_tested) ksprintf(&s, ";PV4=%.2g,%.2g,%.2g,%.2g", a.p[0], a.p[1], a.p[2], a.p[3]);
-	kputc('\0', &s);
-    rm_info(&s, "QS=");
-    rm_info(&s, "I16=");
-	kputs(b->fmt, &s); kputc('\0', &s);
-	free(b->str);
-	b->m_str = s.m; b->l_str = s.l; b->str = s.s;
-	b->qual = r < 1e-100? 999 : -4.343 * log(r);
-	if (b->qual > 999) b->qual = 999;
-	bcf_sync(b);
-	if (!is_var) bcf_shrink_alt(b, 1);
-	else if (!(flag&VC_KEEPALT))
-		bcf_shrink_alt(b, pr->rank0 < 2? 2 : pr->rank0+1);
-	if (is_var && (flag&VC_CALL_GT)) { // call individual genotype
-		int i, x, old_n_gi = b->n_gi;
-		s.m = b->m_str; s.l = b->l_str - 1; s.s = b->str;
-		kputs(":GT:GQ", &s); kputc('\0', &s);
-		b->m_str = s.m; b->l_str = s.l; b->str = s.s;
-		bcf_sync(b);
-		for (i = 0; i < b->n_smpl; ++i) {
-			x = bcf_p1_call_gt(pa, pr->f_exp, i);
-			((uint8_t*)b->gi[old_n_gi].data)[i] = (x&3) == 0? 1<<3|1 : (x&3) == 1? 1 : 0;
-			((uint8_t*)b->gi[old_n_gi+1].data)[i] = x>>2;
-		}
-	}
-	return is_var;
-}
-
-static char **read_samples(const char *fn, int *_n)
-{
-	gzFile fp;
-	kstream_t *ks;
-	kstring_t s;
-	int dret, n = 0, max = 0;
-	char **sam = 0;
-	*_n = 0;
-	s.l = s.m = 0; s.s = 0;
-	fp = gzopen(fn, "r");
-	if (fp == 0) 
-    {
-        // interpret as sample names, not as a file name
-        const char *t = fn, *p = t;
-        while (*t)
-        {
-            t++;
-            if ( *t==',' || !*t )
-            { 
-                sam = realloc(sam, sizeof(void*)*(n+1));
-                sam[n] = (char*) malloc(sizeof(char)*(t-p+2));
-                memcpy(sam[n], p, t-p);
-                sam[n][t-p]   = 0;
-                sam[n][t-p+1] = 2;    // assume diploid
-                p = t+1;
-                n++; 
-            }
-        }
-        *_n = n;
-        return sam; // fail to open file
-    }
-	ks = ks_init(fp);
-	while (ks_getuntil(ks, 0, &s, &dret) >= 0) {
-		int l;
-		if (max == n) {
-			max = max? max<<1 : 4;
-			sam = realloc(sam, sizeof(void*)*max);
-		}
-		l = s.l;
-		sam[n] = malloc(s.l + 2);
-		strcpy(sam[n], s.s);
-		sam[n][l+1] = 2; // by default, diploid
-		if (dret != '\n') {
-			if (ks_getuntil(ks, 0, &s, &dret) >= 0) { // read ploidy, 1 or 2
-				int x = (int)s.s[0] - '0';
-				if (x == 1 || x == 2) sam[n][l+1] = x;
-				else fprintf(pysamerr, "(%s) ploidy can only be 1 or 2; assume diploid\n", __func__);
-			}
-			if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret);
-		}
-		++n;
-	}
-	ks_destroy(ks);
-	gzclose(fp);
-	free(s.s);
-	*_n = n;
-	return sam;
-}
-
-static void write_header(bcf_hdr_t *h)
-{
-	kstring_t str;
-	str.l = h->l_txt? h->l_txt - 1 : 0;
-	str.m = str.l + 1; str.s = h->txt;
-	if (!strstr(str.s, "##INFO=<ID=DP,"))
-		kputs("##INFO=<ID=DP,Number=1,Type=Integer,Description=\"Raw read depth\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=DP4,"))
-		kputs("##INFO=<ID=DP4,Number=4,Type=Integer,Description=\"# high-quality ref-forward bases, ref-reverse, alt-forward and alt-reverse bases\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=MQ,"))
-		kputs("##INFO=<ID=MQ,Number=1,Type=Integer,Description=\"Root-mean-square mapping quality of covering reads\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=FQ,"))
-		kputs("##INFO=<ID=FQ,Number=1,Type=Float,Description=\"Phred probability of all samples being the same\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=AF1,"))
-		kputs("##INFO=<ID=AF1,Number=1,Type=Float,Description=\"Max-likelihood estimate of the first ALT allele frequency (assuming HWE)\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=AC1,"))
-		kputs("##INFO=<ID=AC1,Number=1,Type=Float,Description=\"Max-likelihood estimate of the first ALT allele count (no HWE assumption)\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=AN,"))
-		kputs("##INFO=<ID=AN,Number=1,Type=Integer,Description=\"Total number of alleles in called genotypes\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=IS,"))
-		kputs("##INFO=<ID=IS,Number=2,Type=Float,Description=\"Maximum number of reads supporting an indel and fraction of indel reads\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=AC,"))
-		kputs("##INFO=<ID=AC,Number=A,Type=Integer,Description=\"Allele count in genotypes for each ALT allele, in the same order as listed\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=G3,"))
-		kputs("##INFO=<ID=G3,Number=3,Type=Float,Description=\"ML estimate of genotype frequencies\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=HWE,"))
-		kputs("##INFO=<ID=HWE,Number=1,Type=Float,Description=\"Chi^2 based HWE test P-value based on G3\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=CLR,"))
-		kputs("##INFO=<ID=CLR,Number=1,Type=Integer,Description=\"Log ratio of genotype likelihoods with and without the constraint\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=UGT,"))
-		kputs("##INFO=<ID=UGT,Number=1,Type=String,Description=\"The most probable unconstrained genotype configuration in the trio\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=CGT,"))
-		kputs("##INFO=<ID=CGT,Number=1,Type=String,Description=\"The most probable constrained genotype configuration in the trio\">\n", &str);
-//	if (!strstr(str.s, "##INFO=<ID=CI95,"))
-//		kputs("##INFO=<ID=CI95,Number=2,Type=Float,Description=\"Equal-tail Bayesian credible interval of the site allele frequency at the 95% level\">\n", &str);
-	if (!strstr(str.s, "##INFO=<ID=PV4,"))
-		kputs("##INFO=<ID=PV4,Number=4,Type=Float,Description=\"P-values for strand bias, baseQ bias, mapQ bias and tail distance bias\">\n", &str);
-    if (!strstr(str.s, "##INFO=<ID=INDEL,"))
-        kputs("##INFO=<ID=INDEL,Number=0,Type=Flag,Description=\"Indicates that the variant is an INDEL.\">\n", &str);
-    if (!strstr(str.s, "##INFO=<ID=PC2,"))
-        kputs("##INFO=<ID=PC2,Number=2,Type=Integer,Description=\"Phred probability of the nonRef allele frequency in group1 samples being larger (,smaller) than in group2.\">\n", &str);
-    if (!strstr(str.s, "##INFO=<ID=PCHI2,"))
-        kputs("##INFO=<ID=PCHI2,Number=1,Type=Float,Description=\"Posterior weighted chi^2 P-value for testing the association between group1 and group2 samples.\">\n", &str);
-    if (!strstr(str.s, "##INFO=<ID=QCHI2,"))
-        kputs("##INFO=<ID=QCHI2,Number=1,Type=Integer,Description=\"Phred scaled PCHI2.\">\n", &str);
-    if (!strstr(str.s, "##INFO=<ID=RP,"))
-        kputs("##INFO=<ID=PR,Number=1,Type=Integer,Description=\"# permutations yielding a smaller PCHI2.\">\n", &str);
-    if (!strstr(str.s, "##INFO=<ID=QBD,"))
-        kputs("##INFO=<ID=QBD,Number=1,Type=Float,Description=\"Quality by Depth: QUAL/#reads\">\n", &str);
-    //if (!strstr(str.s, "##INFO=<ID=RPS,"))
-    //    kputs("##INFO=<ID=RPS,Number=3,Type=Float,Description=\"Read Position Stats: depth, average, stddev\">\n", &str);
-    if (!strstr(str.s, "##INFO=<ID=RPB,"))
-        kputs("##INFO=<ID=RPB,Number=1,Type=Float,Description=\"Read Position Bias\">\n", &str);
-    if (!strstr(str.s, "##INFO=<ID=MDV,"))
-        kputs("##INFO=<ID=MDV,Number=1,Type=Integer,Description=\"Maximum number of high-quality nonRef reads in samples\">\n", &str);
-    if (!strstr(str.s, "##INFO=<ID=VDB,"))
-        kputs("##INFO=<ID=VDB,Number=1,Type=Float,Description=\"Variant Distance Bias (v2) for filtering splice-site artefacts in RNA-seq data. Note: this version may be broken.\">\n", &str);
-    if (!strstr(str.s, "##FORMAT=<ID=GT,"))
-        kputs("##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">\n", &str);
-    if (!strstr(str.s, "##FORMAT=<ID=GQ,"))
-        kputs("##FORMAT=<ID=GQ,Number=1,Type=Integer,Description=\"Genotype Quality\">\n", &str);
-    if (!strstr(str.s, "##FORMAT=<ID=GL,"))
-        kputs("##FORMAT=<ID=GL,Number=3,Type=Float,Description=\"Likelihoods for RR,RA,AA genotypes (R=ref,A=alt)\">\n", &str);
-	if (!strstr(str.s, "##FORMAT=<ID=DP,"))
-		kputs("##FORMAT=<ID=DP,Number=1,Type=Integer,Description=\"# high-quality bases\">\n", &str);
-	if (!strstr(str.s, "##FORMAT=<ID=DV,"))
-		kputs("##FORMAT=<ID=DV,Number=1,Type=Integer,Description=\"# high-quality non-reference bases\">\n", &str);
-	if (!strstr(str.s, "##FORMAT=<ID=SP,"))
-		kputs("##FORMAT=<ID=SP,Number=1,Type=Integer,Description=\"Phred-scaled strand bias P-value\">\n", &str);
-	if (!strstr(str.s, "##FORMAT=<ID=PL,"))
-		kputs("##FORMAT=<ID=PL,Number=G,Type=Integer,Description=\"List of Phred-scaled genotype likelihoods\">\n", &str);
-	h->l_txt = str.l + 1; h->txt = str.s;
-}
-
-double bcf_pair_freq(const bcf1_t *b0, const bcf1_t *b1, double f[4]);
-
-int bcfview(int argc, char *argv[])
-{
-	extern int bcf_2qcall(bcf_hdr_t *h, bcf1_t *b);
-	extern void bcf_p1_indel_prior(bcf_p1aux_t *ma, double x);
-	extern int bcf_fix_gt(bcf1_t *b);
-	extern int bcf_anno_max(bcf1_t *b);
-	extern int bcf_shuffle(bcf1_t *b, int seed);
-	extern uint32_t *bcf_trio_prep(int is_x, int is_son);
-	extern int bcf_trio_call(uint32_t *prep, const bcf1_t *b, int *llr, int64_t *gt);
-	extern int bcf_pair_call(const bcf1_t *b);
-	extern int bcf_min_diff(const bcf1_t *b);
-	extern int bcf_p1_get_M(bcf_p1aux_t *b);
-
-	extern gzFile bcf_p1_fp_lk;
-
-	bcf_t *bp, *bout = 0;
-	bcf1_t *b, *blast;
-	int c, *seeds = 0;
-	uint64_t n_processed = 0, qcnt[256];
-	viewconf_t vc;
-	bcf_p1aux_t *p1 = 0;
-	bcf_hdr_t *hin, *hout;
-	int tid, begin, end;
-	char moder[4], modew[4];
-
-	tid = begin = end = -1;
-	memset(&vc, 0, sizeof(viewconf_t));
-	vc.prior_type = vc.n1 = -1; vc.theta = 1e-3; vc.pref = 0.5; vc.indel_frac = -1.; vc.n_perm = 0; vc.min_perm_p = 0.01; vc.min_smpl_frac = 0; vc.min_lrt = 1; vc.min_ma_lrt = -1;
-	memset(qcnt, 0, 8 * 256);
-	while ((c = getopt(argc, argv, "FN1:l:cC:eHAGvbSuP:t:p:QgLi:IMs:D:U:X:d:T:Ywm:K:")) >= 0) {
-		switch (c) {
-		case '1': vc.n1 = atoi(optarg); break;
-		case 'l': vc.bed = bed_read(optarg); if (!vc.bed) { fprintf(pysamerr,"Could not read \"%s\"\n", optarg); return 1; } break;
-		case 'D': vc.fn_dict = strdup(optarg); break;
-		case 'F': vc.flag |= VC_FIX_PL; break;
-		case 'N': vc.flag |= VC_ACGT_ONLY; break;
-		case 'G': vc.flag |= VC_NO_GENO; break;
-		case 'A': vc.flag |= VC_KEEPALT; break;
-		case 'b': vc.flag |= VC_BCFOUT; break;
-		case 'S': vc.flag |= VC_VCFIN; break;
-		case 'c': vc.flag |= VC_CALL; break;
-		case 'e': vc.flag |= VC_EM; break;
-		case 'v': vc.flag |= VC_VARONLY | VC_CALL; break;
-		case 'u': vc.flag |= VC_UNCOMP | VC_BCFOUT; break;
-		case 'g': vc.flag |= VC_CALL_GT | VC_CALL; break;
-		case 'I': vc.flag |= VC_NO_INDEL; break;
-		case 'w': vc.flag |= VC_INDEL_ONLY; break;
-		case 'M': vc.flag |= VC_ANNO_MAX; break;
-		case 'Y': vc.flag |= VC_QCNT; break;
-        case 'm': vc.min_ma_lrt = atof(optarg); break;
-		case 't': vc.theta = atof(optarg); break;
-		case 'p': vc.pref = atof(optarg); break;
-		case 'i': vc.indel_frac = atof(optarg); break;
-		case 'Q': vc.flag |= VC_QCALL; break;
-		case 'L': vc.flag |= VC_ADJLD; break;
-		case 'U': vc.n_perm = atoi(optarg); break;
-		case 'C': vc.min_lrt = atof(optarg); break;
-		case 'X': vc.min_perm_p = atof(optarg); break;
-		case 'd': vc.min_smpl_frac = atof(optarg); break;
-		case 'K': bcf_p1_fp_lk = gzopen(optarg, "w"); break;
-		case 's': vc.subsam = read_samples(optarg, &vc.n_sub);
-			vc.ploidy = calloc(vc.n_sub + 1, 1);
-			for (tid = 0; tid < vc.n_sub; ++tid) vc.ploidy[tid] = vc.subsam[tid][strlen(vc.subsam[tid]) + 1];
-			tid = -1;
-			break;
-		case 'T':
-			if (strcmp(optarg, "trioauto") == 0) vc.trio_aux = bcf_trio_prep(0, 0);
-			else if (strcmp(optarg, "trioxd") == 0) vc.trio_aux = bcf_trio_prep(1, 0);
-			else if (strcmp(optarg, "trioxs") == 0) vc.trio_aux = bcf_trio_prep(1, 1);
-			else if (strcmp(optarg, "pair") == 0) vc.flag |= VC_PAIRCALL;
-			else {
-				fprintf(pysamerr, "[%s] Option '-T' can only take value trioauto, trioxd or trioxs.\n", __func__);
-				return 1;
-			}
-			break;
-		case 'P':
-			if (strcmp(optarg, "full") == 0) vc.prior_type = MC_PTYPE_FULL;
-			else if (strcmp(optarg, "cond2") == 0) vc.prior_type = MC_PTYPE_COND2;
-			else if (strcmp(optarg, "flat") == 0) vc.prior_type = MC_PTYPE_FLAT;
-			else vc.prior_file = strdup(optarg);
-			break;
-		}
-	}
-	if (argc == optind) {
-		fprintf(pysamerr, "\n");
-		fprintf(pysamerr, "Usage: bcftools view [options] <in.bcf> [reg]\n\n");
-		fprintf(pysamerr, "Input/output options:\n\n");
-		fprintf(pysamerr, "       -A        keep all possible alternate alleles at variant sites\n");
-		fprintf(pysamerr, "       -b        output BCF instead of VCF\n");
-		fprintf(pysamerr, "       -D FILE   sequence dictionary for VCF->BCF conversion [null]\n");
-		fprintf(pysamerr, "       -F        PL generated by r921 or before (which generate old ordering)\n");
-		fprintf(pysamerr, "       -G        suppress all individual genotype information\n");
-		fprintf(pysamerr, "       -l FILE   list of sites (chr pos) or regions (BED) to output [all sites]\n");
-		fprintf(pysamerr, "       -L        calculate LD for adjacent sites\n");
-		fprintf(pysamerr, "       -N        skip sites where REF is not A/C/G/T\n");
-		fprintf(pysamerr, "       -Q        output the QCALL likelihood format\n");
-		fprintf(pysamerr, "       -s FILE   list of samples to use [all samples]\n");
-		fprintf(pysamerr, "       -S        input is VCF\n");
-		fprintf(pysamerr, "       -u        uncompressed BCF output (force -b)\n");
-		fprintf(pysamerr, "\nConsensus/variant calling options:\n\n");
-		fprintf(pysamerr, "       -c        SNP calling (force -e)\n");
-		fprintf(pysamerr, "       -d FLOAT  skip loci where less than FLOAT fraction of samples covered [0]\n");
-		fprintf(pysamerr, "       -e        likelihood based analyses\n");
-		fprintf(pysamerr, "       -g        call genotypes at variant sites (force -c)\n");
-		fprintf(pysamerr, "       -i FLOAT  indel-to-substitution ratio [%.4g]\n", vc.indel_frac);
-		fprintf(pysamerr, "       -I        skip indels\n");
-		fprintf(pysamerr, "       -m FLOAT  alternative model for multiallelic and rare-variant calling, include if P(chi^2)>=FLOAT\n");
-		fprintf(pysamerr, "       -p FLOAT  variant if P(ref|D)<FLOAT [%.3g]\n", vc.pref);
-		fprintf(pysamerr, "       -P STR    type of prior: full, cond2, flat [full]\n");
-		fprintf(pysamerr, "       -t FLOAT  scaled substitution mutation rate [%.4g]\n", vc.theta);
-		fprintf(pysamerr, "       -T STR    constrained calling; STR can be: pair, trioauto, trioxd and trioxs (see manual) [null]\n");
-		fprintf(pysamerr, "       -v        output potential variant sites only (force -c)\n");
-		fprintf(pysamerr, "\nContrast calling and association test options:\n\n");
-		fprintf(pysamerr, "       -1 INT    number of group-1 samples [0]\n");
-		fprintf(pysamerr, "       -C FLOAT  posterior constrast for LRT<FLOAT and P(ref|D)<0.5 [%g]\n", vc.min_lrt);
-		fprintf(pysamerr, "       -U INT    number of permutations for association testing (effective with -1) [0]\n");
-		fprintf(pysamerr, "       -X FLOAT  only perform permutations for P(chi^2)<FLOAT [%g]\n", vc.min_perm_p);
-		fprintf(pysamerr, "\n");
-		return 1;
-	}
-
-	if (vc.flag & VC_CALL) vc.flag |= VC_EM;
-	if ((vc.flag & VC_VCFIN) && (vc.flag & VC_BCFOUT) && vc.fn_dict == 0) {
-		fprintf(pysamerr, "[%s] For VCF->BCF conversion please specify the sequence dictionary with -D\n", __func__);
-		return 1;
-	}
-	if (vc.n1 <= 0) vc.n_perm = 0; // TODO: give a warning here!
-	if (vc.n_perm > 0) {
-		seeds = malloc(vc.n_perm * sizeof(int));
-		srand48(time(0));
-		for (c = 0; c < vc.n_perm; ++c) seeds[c] = lrand48();
-	}
-	b = calloc(1, sizeof(bcf1_t));
-	blast = calloc(1, sizeof(bcf1_t));
-	strcpy(moder, "r");
-	if (!(vc.flag & VC_VCFIN)) strcat(moder, "b");
-	strcpy(modew, "w");
-	if (vc.flag & VC_BCFOUT) strcat(modew, "b");
-	if (vc.flag & VC_UNCOMP) strcat(modew, "u");
-	bp = vcf_open(argv[optind], moder);
-	hin = hout = vcf_hdr_read(bp);
-	if (vc.fn_dict && (vc.flag & VC_VCFIN))
-		vcf_dictread(bp, hin, vc.fn_dict);
-	bout = vcf_open("-", modew);
-	if (!(vc.flag & VC_QCALL)) {
-		if (vc.n_sub) {
-			vc.sublist = calloc(vc.n_sub, sizeof(int));
-			hout = bcf_hdr_subsam(hin, vc.n_sub, vc.subsam, vc.sublist);
-		}
-		write_header(hout); // always print the header
-		vcf_hdr_write(bout, hout);
-	}
-	if (vc.flag & VC_CALL) {
-		p1 = bcf_p1_init(hout->n_smpl, vc.ploidy);
-		if (vc.prior_file) {
-			if (bcf_p1_read_prior(p1, vc.prior_file) < 0) {
-				fprintf(pysamerr, "[%s] fail to read the prior AFS.\n", __func__);
-				return 1;
-			}
-		} else bcf_p1_init_prior(p1, vc.prior_type, vc.theta);
-		if (vc.n1 > 0 && vc.min_lrt > 0.) { // set n1
-			bcf_p1_set_n1(p1, vc.n1);
-			bcf_p1_init_subprior(p1, vc.prior_type, vc.theta);
-		}
-		if (vc.indel_frac > 0.) bcf_p1_indel_prior(p1, vc.indel_frac); // otherwise use the default indel_frac
-	}
-	if (optind + 1 < argc && !(vc.flag&VC_VCFIN)) {
-		void *str2id = bcf_build_refhash(hout);
-		if (bcf_parse_region(str2id, argv[optind+1], &tid, &begin, &end) >= 0) {
-			bcf_idx_t *idx;
-			idx = bcf_idx_load(argv[optind]);
-			if (idx) {
-				uint64_t off;
-				off = bcf_idx_query(idx, tid, begin);
-				if (off == 0) {
-					fprintf(pysamerr, "[%s] no records in the query region.\n", __func__);
-					return 1; // FIXME: a lot of memory leaks...
-				}
-				bgzf_seek(bp->fp, off, SEEK_SET);
-				bcf_idx_destroy(idx);
-			}
-		}
-	}
-	if (bcf_p1_fp_lk && p1) {
-		int32_t M = bcf_p1_get_M(p1);
-		gzwrite(bcf_p1_fp_lk, &M, 4);
-	}
-	while (vcf_read(bp, hin, b) > 0) {
-		int is_indel, cons_llr = -1;
-		int64_t cons_gt = -1;
-		double em[10];
-		if ((vc.flag & VC_VARONLY) && strcmp(b->alt, "X") == 0) continue;
-		if ((vc.flag & VC_VARONLY) && vc.min_smpl_frac > 0.) {
-			extern int bcf_smpl_covered(const bcf1_t *b);
-			int n = bcf_smpl_covered(b);
-			if ((double)n / b->n_smpl < vc.min_smpl_frac) continue;
-		}
-		if (vc.n_sub) bcf_subsam(vc.n_sub, vc.sublist, b);
-		if (vc.flag & VC_FIX_PL) bcf_fix_pl(b);
-		is_indel = bcf_is_indel(b);
-		if ((vc.flag & VC_NO_INDEL) && is_indel) continue;
-		if ((vc.flag & VC_INDEL_ONLY) && !is_indel) continue;
-		if ((vc.flag & VC_ACGT_ONLY) && !is_indel) {
-			int x;
-			if (b->ref[0] == 0 || b->ref[1] != 0) continue;
-			x = toupper(b->ref[0]);
-			if (x != 'A' && x != 'C' && x != 'G' && x != 'T') continue;
-		}
-		if (vc.bed && !bed_overlap(vc.bed, hin->ns[b->tid], b->pos, b->pos + strlen(b->ref))) continue;
-		if (tid >= 0) {
-			int l = strlen(b->ref);
-			l = b->pos + (l > 0? l : 1);
-			if (b->tid != tid || b->pos >= end) break;
-			if (!(l > begin && end > b->pos)) continue;
-		}
-		++n_processed;
-		if ((vc.flag & VC_QCNT) && !is_indel) { // summarize the difference
-			int x = bcf_min_diff(b);
-			if (x > 255) x = 255;
-			if (x >= 0) ++qcnt[x];
-		}
-		if (vc.flag & VC_QCALL) { // output QCALL format; STOP here
-			bcf_2qcall(hout, b);
-			continue;
-		}
-		if (vc.trio_aux) // do trio calling
-			bcf_trio_call(vc.trio_aux, b, &cons_llr, &cons_gt);
-		else if (vc.flag & VC_PAIRCALL)
-			cons_llr = bcf_pair_call(b);
-		if (vc.flag & (VC_CALL|VC_ADJLD|VC_EM)) bcf_gl2pl(b);
-		if (vc.flag & VC_EM) bcf_em1(b, vc.n1, 0x1ff, em);
-		else {
-			int i;
-			for (i = 0; i < 9; ++i) em[i] = -1.;
-		}
-        if ( !(vc.flag&VC_KEEPALT) && (vc.flag&VC_CALL) && vc.min_ma_lrt>=0 )
-        {
-            bcf_p1_set_ploidy(b, p1); // could be improved: do this per site to allow pseudo-autosomal regions
-            int gts = call_multiallelic_gt(b, p1, vc.min_ma_lrt, vc.flag&VC_VARONLY);
-            if ( gts<=1 && vc.flag & VC_VARONLY ) continue;
-        }
-		else if (vc.flag & VC_CALL) { // call variants
-			bcf_p1rst_t pr;
-			int calret;
-			gzwrite(bcf_p1_fp_lk, &b->tid, 4);
-			gzwrite(bcf_p1_fp_lk, &b->pos, 4);
-			gzwrite(bcf_p1_fp_lk, &em[0], sizeof(double));
-			calret = bcf_p1_cal(b, (em[7] >= 0 && em[7] < vc.min_lrt), p1, &pr);
-			if (n_processed % 100000 == 0) {
-				fprintf(pysamerr, "[%s] %ld sites processed.\n", __func__, (long)n_processed);
-				bcf_p1_dump_afs(p1);
-			}
-			if (pr.p_ref >= vc.pref && (vc.flag & VC_VARONLY)) continue;
-			if (vc.n_perm && vc.n1 > 0 && pr.p_chi2 < vc.min_perm_p) { // permutation test
-				bcf_p1rst_t r;
-				int i, n = 0;
-				for (i = 0; i < vc.n_perm; ++i) {
-#ifdef BCF_PERM_LRT // LRT based permutation is much faster but less robust to artifacts
-					double x[10];
-					bcf_shuffle(b, seeds[i]);
-					bcf_em1(b, vc.n1, 1<<7, x);
-					if (x[7] < em[7]) ++n;
-#else
-					bcf_shuffle(b, seeds[i]);
-					bcf_p1_cal(b, 1, p1, &r);
-					if (pr.p_chi2 >= r.p_chi2) ++n;
-#endif
-				}
-				pr.perm_rank = n;
-			}
-			if (calret >= 0) update_bcf1(b, p1, &pr, vc.pref, vc.flag, em, cons_llr, cons_gt);
-		} else if (vc.flag & VC_EM) update_bcf1(b, 0, 0, 0, vc.flag, em, cons_llr, cons_gt);
-		if (vc.flag & VC_ADJLD) { // compute LD
-			double f[4], r2;
-			if ((r2 = bcf_pair_freq(blast, b, f)) >= 0) {
-				kstring_t s;
-				s.m = s.l = 0; s.s = 0;
-				if (*b->info) kputc(';', &s);
-				ksprintf(&s, "NEIR=%.3f;NEIF4=%.3f,%.3f,%.3f,%.3f", r2, f[0], f[1], f[2], f[3]);
-				bcf_append_info(b, s.s, s.l);
-				free(s.s);
-			}
-			bcf_cpy(blast, b);
-		}
-		if (vc.flag & VC_ANNO_MAX) bcf_anno_max(b);
-		if (vc.flag & VC_NO_GENO) { // do not output GENO fields
-			b->n_gi = 0;
-			b->fmt[0] = '\0';
-			b->l_str = b->fmt - b->str + 1;
-		} else bcf_fix_gt(b);
-		vcf_write(bout, hout, b);
-	}
-
-	if (bcf_p1_fp_lk) gzclose(bcf_p1_fp_lk);
-	if (vc.prior_file) free(vc.prior_file);
-	if (vc.flag & VC_CALL) bcf_p1_dump_afs(p1);
-	if (hin != hout) bcf_hdr_destroy(hout);
-	bcf_hdr_destroy(hin);
-	bcf_destroy(b); bcf_destroy(blast);
-	vcf_close(bp); vcf_close(bout);
-	if (vc.fn_dict) free(vc.fn_dict);
-	if (vc.ploidy) free(vc.ploidy);
-	if (vc.trio_aux) free(vc.trio_aux);
-	if (vc.n_sub) {
-		int i;
-		for (i = 0; i < vc.n_sub; ++i) free(vc.subsam[i]);
-		free(vc.subsam); free(vc.sublist);
-	}
-	if (vc.bed) bed_destroy(vc.bed);
-	if (vc.flag & VC_QCNT)
-		for (c = 0; c < 256; ++c)
-			fprintf(pysamerr, "QT\t%d\t%lld\n", c, (long long)qcnt[c]);
-	if (seeds) free(seeds);
-	if (p1) bcf_p1_destroy(p1);
-	return 0;
-}
diff --git a/samtools/bcftools/em.c.pysam.c b/samtools/bcftools/em.c.pysam.c
deleted file mode 100644
index 94b6d75..0000000
--- a/samtools/bcftools/em.c.pysam.c
+++ /dev/null
@@ -1,312 +0,0 @@
-#include "pysam.h"
-
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include "bcf.h"
-#include "kmin.h"
-
-static double g_q2p[256];
-
-#define ITER_MAX 50
-#define ITER_TRY 10
-#define EPS 1e-5
-
-extern double kf_gammaq(double, double);
-
-/*
-	Generic routines
- */
-// get the 3 genotype likelihoods
-static double *get_pdg3(const bcf1_t *b)
-{
-	double *pdg;
-	const uint8_t *PL = 0;
-	int i, PL_len = 0;
-	// initialize g_q2p if necessary
-	if (g_q2p[0] == 0.)
-		for (i = 0; i < 256; ++i)
-			g_q2p[i] = pow(10., -i / 10.);
-	// set PL and PL_len
-	for (i = 0; i < b->n_gi; ++i) {
-		if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
-			PL = (const uint8_t*)b->gi[i].data;
-			PL_len = b->gi[i].len;
-			break;
-		}
-	}
-	if (i == b->n_gi) return 0; // no PL
-	// fill pdg
-	pdg = malloc(3 * b->n_smpl * sizeof(double));
-	for (i = 0; i < b->n_smpl; ++i) {
-		const uint8_t *pi = PL + i * PL_len;
-		double *p = pdg + i * 3;
-		p[0] = g_q2p[pi[2]]; p[1] = g_q2p[pi[1]]; p[2] = g_q2p[pi[0]];
-	}
-	return pdg;
-}
-
-// estimate site allele frequency in a very naive and inaccurate way
-static double est_freq(int n, const double *pdg)
-{
-	int i, gcnt[3], tmp1;
-	// get a rough estimate of the genotype frequency
-	gcnt[0] = gcnt[1] = gcnt[2] = 0;
-	for (i = 0; i < n; ++i) {
-		const double *p = pdg + i * 3;
-		if (p[0] != 1. || p[1] != 1. || p[2] != 1.) {
-			int which = p[0] > p[1]? 0 : 1;
-			which = p[which] > p[2]? which : 2;
-			++gcnt[which];
-		}
-	}
-	tmp1 = gcnt[0] + gcnt[1] + gcnt[2];
-	return (tmp1 == 0)? -1.0 : (.5 * gcnt[1] + gcnt[2]) / tmp1;
-}
-
-/*
-	Single-locus EM
- */
-
-typedef struct {
-	int beg, end;
-	const double *pdg;
-} minaux1_t;
-
-static double prob1(double f, void *data)
-{
-	minaux1_t *a = (minaux1_t*)data;
-	double p = 1., l = 0., f3[3];
-	int i;
-//	printf("brent %lg\n", f);
-	if (f < 0 || f > 1) return 1e300;
-	f3[0] = (1.-f)*(1.-f); f3[1] = 2.*f*(1.-f); f3[2] = f*f;
-	for (i = a->beg; i < a->end; ++i) {
-		const double *pdg = a->pdg + i * 3;
-		p *= pdg[0] * f3[0] + pdg[1] * f3[1] + pdg[2] * f3[2];
-		if (p < 1e-200) l -= log(p), p = 1.;
-	}
-	return l - log(p);
-}
-
-// one EM iteration for allele frequency estimate
-static double freq_iter(double *f, const double *_pdg, int beg, int end)
-{
-	double f0 = *f, f3[3], err;
-	int i;
-//	printf("em %lg\n", *f);
-	f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
-	for (i = beg, f0 = 0.; i < end; ++i) {
-		const double *pdg = _pdg + i * 3;
-		f0 += (pdg[1] * f3[1] + 2. * pdg[2] * f3[2])
-			/ (pdg[0] * f3[0] + pdg[1] * f3[1] + pdg[2] * f3[2]);
-	}
-	f0 /= (end - beg) * 2;
-	err = fabs(f0 - *f);
-	*f = f0;
-	return err;
-}
-
-/* The following function combines EM and Brent's method. When the signal from
- * the data is strong, EM is faster but sometimes, EM may converge very slowly.
- * When this happens, we switch to Brent's method. The idea is learned from
- * Rasmus Nielsen.
- */
-static double freqml(double f0, int beg, int end, const double *pdg)
-{
-	int i;
-	double f;
-	for (i = 0, f = f0; i < ITER_TRY; ++i)
-		if (freq_iter(&f, pdg, beg, end) < EPS) break;
-	if (i == ITER_TRY) { // haven't converged yet; try Brent's method
-		minaux1_t a;
-		a.beg = beg; a.end = end; a.pdg = pdg;
-		kmin_brent(prob1, f0 == f? .5*f0 : f0, f, (void*)&a, EPS, &f);
-	}
-	return f;
-}
-
-// one EM iteration for genotype frequency estimate
-static double g3_iter(double g[3], const double *_pdg, int beg, int end)
-{
-	double err, gg[3];
-	int i;
-	gg[0] = gg[1] = gg[2] = 0.;
-//	printf("%lg,%lg,%lg\n", g[0], g[1], g[2]);
-	for (i = beg; i < end; ++i) {
-		double sum, tmp[3];
-		const double *pdg = _pdg + i * 3;
-		tmp[0] = pdg[0] * g[0]; tmp[1] = pdg[1] * g[1]; tmp[2] = pdg[2] * g[2];
-		sum = (tmp[0] + tmp[1] + tmp[2]) * (end - beg);
-		gg[0] += tmp[0] / sum; gg[1] += tmp[1] / sum; gg[2] += tmp[2] / sum;
-	}
-	err = fabs(gg[0] - g[0]) > fabs(gg[1] - g[1])? fabs(gg[0] - g[0]) : fabs(gg[1] - g[1]);
-	err = err > fabs(gg[2] - g[2])? err : fabs(gg[2] - g[2]);
-	g[0] = gg[0]; g[1] = gg[1]; g[2] = gg[2];
-	return err;
-}
-
-// perform likelihood ratio test
-static double lk_ratio_test(int n, int n1, const double *pdg, double f3[3][3])
-{
-	double r;
-	int i;
-	for (i = 0, r = 1.; i < n1; ++i) {
-		const double *p = pdg + i * 3;
-		r *= (p[0] * f3[1][0] + p[1] * f3[1][1] + p[2] * f3[1][2])
-			/ (p[0] * f3[0][0] + p[1] * f3[0][1] + p[2] * f3[0][2]);
-	}
-	for (; i < n; ++i) {
-		const double *p = pdg + i * 3;
-		r *= (p[0] * f3[2][0] + p[1] * f3[2][1] + p[2] * f3[2][2])
-			/ (p[0] * f3[0][0] + p[1] * f3[0][1] + p[2] * f3[0][2]);
-	}
-	return r;
-}
-
-// x[0]: ref frequency
-// x[1..3]: alt-alt, alt-ref, ref-ref frequenc
-// x[4]: HWE P-value
-// x[5..6]: group1 freq, group2 freq
-// x[7]: 1-degree P-value
-// x[8]: 2-degree P-value
-int bcf_em1(const bcf1_t *b, int n1, int flag, double x[10])
-{
-	double *pdg;
-	int i, n, n2;
-	if (b->n_alleles < 2) return -1; // one allele only
-	// initialization
-	if (n1 < 0 || n1 > b->n_smpl) n1 = 0;
-	if (flag & 1<<7) flag |= 7<<5; // compute group freq if LRT is required
-	if (flag & 0xf<<1) flag |= 0xf<<1;
-	n = b->n_smpl; n2 = n - n1;
-	pdg = get_pdg3(b);
-	if (pdg == 0) return -1;
-	for (i = 0; i < 10; ++i) x[i] = -1.; // set to negative
-	{
-		if ((x[0] = est_freq(n, pdg)) < 0.) {
-			free(pdg);
-			return -1; // no data
-		}
-		x[0] = freqml(x[0], 0, n, pdg);
-	}
-	if (flag & (0xf<<1|3<<8)) { // estimate the genotype frequency and test HWE
-		double *g = x + 1, f3[3], r;
-		f3[0] = g[0] = (1 - x[0]) * (1 - x[0]);
-		f3[1] = g[1] = 2 * x[0] * (1 - x[0]);
-		f3[2] = g[2] = x[0] * x[0];
-		for (i = 0; i < ITER_MAX; ++i)
-			if (g3_iter(g, pdg, 0, n) < EPS) break;
-		// Hardy-Weinberg equilibrium (HWE)
-		for (i = 0, r = 1.; i < n; ++i) {
-			double *p = pdg + i * 3;
-			r *= (p[0] * g[0] + p[1] * g[1] + p[2] * g[2]) / (p[0] * f3[0] + p[1] * f3[1] + p[2] * f3[2]);
-		}
-		x[4] = kf_gammaq(.5, log(r));
-	}
-	if ((flag & 7<<5) && n1 > 0 && n1 < n) { // group frequency
-		x[5] = freqml(x[0], 0, n1, pdg);
-		x[6] = freqml(x[0], n1, n, pdg);
-	}
-	if ((flag & 1<<7) && n1 > 0 && n1 < n) { // 1-degree P-value
-		double f[3], f3[3][3], tmp;
-		f[0] = x[0]; f[1] = x[5]; f[2] = x[6];
-		for (i = 0; i < 3; ++i)
-			f3[i][0] = (1-f[i])*(1-f[i]), f3[i][1] = 2*f[i]*(1-f[i]), f3[i][2] = f[i]*f[i];
-		tmp = log(lk_ratio_test(n, n1, pdg, f3));
-		if (tmp < 0) tmp = 0;
-		x[7] = kf_gammaq(.5, tmp);
-	}
-	if ((flag & 3<<8) && n1 > 0 && n1 < n) { // 2-degree P-value
-		double g[3][3], tmp;
-		for (i = 0; i < 3; ++i) memcpy(g[i], x + 1, 3 * sizeof(double));
-		for (i = 0; i < ITER_MAX; ++i)
-			if (g3_iter(g[1], pdg, 0, n1) < EPS) break;
-		for (i = 0; i < ITER_MAX; ++i)
-			if (g3_iter(g[2], pdg, n1, n) < EPS) break;
-		tmp = log(lk_ratio_test(n, n1, pdg, g));
-		if (tmp < 0) tmp = 0;
-		x[8] = kf_gammaq(1., tmp);
-	}
-	// free
-	free(pdg);
-	return 0;
-}
-
-/*
-	Two-locus EM (LD)
- */
-
-#define _G1(h, k) ((h>>1&1) + (k>>1&1))
-#define _G2(h, k) ((h&1) + (k&1))
-
-// 0: the previous site; 1: the current site
-static int pair_freq_iter(int n, double *pdg[2], double f[4])
-{
-	double ff[4];
-	int i, k, h;
-//	printf("%lf,%lf,%lf,%lf\n", f[0], f[1], f[2], f[3]);
-	memset(ff, 0, 4 * sizeof(double));
-	for (i = 0; i < n; ++i) {
-		double *p[2], sum, tmp;
-		p[0] = pdg[0] + i * 3; p[1] = pdg[1] + i * 3;
-		for (k = 0, sum = 0.; k < 4; ++k)
-			for (h = 0; h < 4; ++h)
-				sum += f[k] * f[h] * p[0][_G1(k,h)] * p[1][_G2(k,h)];
-		for (k = 0; k < 4; ++k) {
-			tmp = f[0] * (p[0][_G1(0,k)] * p[1][_G2(0,k)] + p[0][_G1(k,0)] * p[1][_G2(k,0)])
-				+ f[1] * (p[0][_G1(1,k)] * p[1][_G2(1,k)] + p[0][_G1(k,1)] * p[1][_G2(k,1)])
-				+ f[2] * (p[0][_G1(2,k)] * p[1][_G2(2,k)] + p[0][_G1(k,2)] * p[1][_G2(k,2)])
-				+ f[3] * (p[0][_G1(3,k)] * p[1][_G2(3,k)] + p[0][_G1(k,3)] * p[1][_G2(k,3)]);
-			ff[k] += f[k] * tmp / sum;
-		}
-	}
-	for (k = 0; k < 4; ++k) f[k] = ff[k] / (2 * n);
-	return 0;
-}
-
-double bcf_pair_freq(const bcf1_t *b0, const bcf1_t *b1, double f[4])
-{
-	const bcf1_t *b[2];
-	int i, j, n_smpl;
-	double *pdg[2], flast[4], r, f0[2];
-	// initialize others
-	if (b0->n_smpl != b1->n_smpl) return -1; // different number of samples
-	n_smpl = b0->n_smpl;
-	b[0] = b0; b[1] = b1;
-	f[0] = f[1] = f[2] = f[3] = -1.;
-	if (b[0]->n_alleles < 2 || b[1]->n_alleles < 2) return -1; // one allele only
-	pdg[0] = get_pdg3(b0); pdg[1] = get_pdg3(b1);
-	if (pdg[0] == 0 || pdg[1] == 0) {
-		free(pdg[0]); free(pdg[1]);
-		return -1;
-	}
-	// set the initial value
-	f0[0] = est_freq(n_smpl, pdg[0]);
-	f0[1] = est_freq(n_smpl, pdg[1]);
-	f[0] = (1 - f0[0]) * (1 - f0[1]); f[3] = f0[0] * f0[1];
-	f[1] = (1 - f0[0]) * f0[1]; f[2] = f0[0] * (1 - f0[1]);
-	// iteration
-	for (j = 0; j < ITER_MAX; ++j) {
-		double eps = 0;
-		memcpy(flast, f, 4 * sizeof(double));
-		pair_freq_iter(n_smpl, pdg, f);
-		for (i = 0; i < 4; ++i) {
-			double x = fabs(f[i] - flast[i]);
-			if (x > eps) eps = x;
-		}
-		if (eps < EPS) break;
-	}
-	// free
-	free(pdg[0]); free(pdg[1]);
-	{ // calculate r^2
-		double p[2], q[2], D;
-		p[0] = f[0] + f[1]; q[0] = 1 - p[0];
-		p[1] = f[0] + f[2]; q[1] = 1 - p[1];
-		D = f[0] * f[3] - f[1] * f[2];
-		r = sqrt(D * D / (p[0] * p[1] * q[0] * q[1]));
-//		printf("R(%lf,%lf,%lf,%lf)=%lf\n", f[0], f[1], f[2], f[3], r);
-		if (isnan(r)) r = -1.;
-	}
-	return r;
-}
diff --git a/samtools/bcftools/fet.c.pysam.c b/samtools/bcftools/fet.c.pysam.c
deleted file mode 100644
index cbd3585..0000000
--- a/samtools/bcftools/fet.c.pysam.c
+++ /dev/null
@@ -1,114 +0,0 @@
-#include "pysam.h"
-
-#include <math.h>
-#include <stdlib.h>
-
-/* This program is implemented with ideas from this web page:
- *
- *   http://www.langsrud.com/fisher.htm
- */
-
-// log\binom{n}{k}
-static double lbinom(int n, int k)
-{
-	if (k == 0 || n == k) return 0;
-	return lgamma(n+1) - lgamma(k+1) - lgamma(n-k+1);
-}
-
-// n11  n12  | n1_
-// n21  n22  | n2_
-//-----------+----
-// n_1  n_2  | n
-
-// hypergeometric distribution
-static double hypergeo(int n11, int n1_, int n_1, int n)
-{
-	return exp(lbinom(n1_, n11) + lbinom(n-n1_, n_1-n11) - lbinom(n, n_1));
-}
-
-typedef struct {
-	int n11, n1_, n_1, n;
-	double p;
-} hgacc_t;
-
-// incremental version of hypergenometric distribution
-static double hypergeo_acc(int n11, int n1_, int n_1, int n, hgacc_t *aux)
-{
-	if (n1_ || n_1 || n) {
-		aux->n11 = n11; aux->n1_ = n1_; aux->n_1 = n_1; aux->n = n;
-	} else { // then only n11 changed; the rest fixed
-		if (n11%11 && n11 + aux->n - aux->n1_ - aux->n_1) {
-			if (n11 == aux->n11 + 1) { // incremental
-				aux->p *= (double)(aux->n1_ - aux->n11) / n11
-					* (aux->n_1 - aux->n11) / (n11 + aux->n - aux->n1_ - aux->n_1);
-				aux->n11 = n11;
-				return aux->p;
-			}
-			if (n11 == aux->n11 - 1) { // incremental
-				aux->p *= (double)aux->n11 / (aux->n1_ - n11)
-					* (aux->n11 + aux->n - aux->n1_ - aux->n_1) / (aux->n_1 - n11);
-				aux->n11 = n11;
-				return aux->p;
-			}
-		}
-		aux->n11 = n11;
-	}
-	aux->p = hypergeo(aux->n11, aux->n1_, aux->n_1, aux->n);
-	return aux->p;
-}
-
-double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two)
-{
-	int i, j, max, min;
-	double p, q, left, right;
-	hgacc_t aux;
-	int n1_, n_1, n;
-
-	n1_ = n11 + n12; n_1 = n11 + n21; n = n11 + n12 + n21 + n22; // calculate n1_, n_1 and n
-	max = (n_1 < n1_) ? n_1 : n1_; // max n11, for right tail
-	min = n1_ + n_1 - n;
-	if (min < 0) min = 0; // min n11, for left tail
-	*two = *_left = *_right = 1.;
-	if (min == max) return 1.; // no need to do test
-	q = hypergeo_acc(n11, n1_, n_1, n, &aux); // the probability of the current table
-	// left tail
-	p = hypergeo_acc(min, 0, 0, 0, &aux);
-	for (left = 0., i = min + 1; p < 0.99999999 * q; ++i) // loop until underflow
-		left += p, p = hypergeo_acc(i, 0, 0, 0, &aux);
-	--i;
-	if (p < 1.00000001 * q) left += p;
-	else --i;
-	// right tail
-	p = hypergeo_acc(max, 0, 0, 0, &aux);
-	for (right = 0., j = max - 1; p < 0.99999999 * q; --j) // loop until underflow
-		right += p, p = hypergeo_acc(j, 0, 0, 0, &aux);
-	++j;
-	if (p < 1.00000001 * q) right += p;
-	else ++j;
-	// two-tail
-	*two = left + right;
-	if (*two > 1.) *two = 1.;
-	// adjust left and right
-	if (abs(i - n11) < abs(j - n11)) right = 1. - left + q;
-	else left = 1.0 - right + q;
-	*_left = left; *_right = right;
-	return q;
-}
-
-#ifdef FET_MAIN
-#include <stdio.h>
-
-int main(int argc, char *argv[])
-{
-	char id[1024];
-	int n11, n12, n21, n22;
-	double left, right, twotail, prob;
-
-	while (scanf("%s%d%d%d%d", id, &n11, &n12, &n21, &n22) == 5) {
-		prob = kt_fisher_exact(n11, n12, n21, n22, &left, &right, &twotail);
-		printf("%s\t%d\t%d\t%d\t%d\t%.6g\t%.6g\t%.6g\t%.6g\n", id, n11, n12, n21, n22,
-				prob, left, right, twotail);
-	}
-	return 0;
-}
-#endif
diff --git a/samtools/bcftools/index.c.pysam.c b/samtools/bcftools/index.c.pysam.c
deleted file mode 100644
index a713aaa..0000000
--- a/samtools/bcftools/index.c.pysam.c
+++ /dev/null
@@ -1,338 +0,0 @@
-#include "pysam.h"
-
-#include <assert.h>
-#include <ctype.h>
-#include <sys/stat.h>
-#include "bam_endian.h"
-#include "kstring.h"
-#include "bcf.h"
-#ifdef _USE_KNETFILE
-#include "knetfile.h"
-#endif
-
-#define TAD_LIDX_SHIFT 13
-
-typedef struct {
-	int32_t n, m;
-	uint64_t *offset;
-} bcf_lidx_t;
-
-struct __bcf_idx_t {
-	int32_t n;
-	bcf_lidx_t *index2;
-};
-
-/************
- * indexing *
- ************/
-
-static inline void insert_offset2(bcf_lidx_t *index2, int _beg, int _end, uint64_t offset)
-{
-	int i, beg, end;
-	beg = _beg >> TAD_LIDX_SHIFT;
-	end = (_end - 1) >> TAD_LIDX_SHIFT;
-	if (index2->m < end + 1) {
-		int old_m = index2->m;
-		index2->m = end + 1;
-		kroundup32(index2->m);
-		index2->offset = (uint64_t*)realloc(index2->offset, index2->m * 8);
-		memset(index2->offset + old_m, 0, 8 * (index2->m - old_m));
-	}
-	if (beg == end) {
-		if (index2->offset[beg] == 0) index2->offset[beg] = offset;
-	} else {
-		for (i = beg; i <= end; ++i)
-			if (index2->offset[i] == 0) index2->offset[i] = offset;
-	}
-	if (index2->n < end + 1) index2->n = end + 1;
-}
-
-bcf_idx_t *bcf_idx_core(bcf_t *bp, bcf_hdr_t *h)
-{
-	bcf_idx_t *idx;
-	int32_t last_coor, last_tid;
-	uint64_t last_off;
-	kstring_t *str;
-	BGZF *fp = bp->fp;
-	bcf1_t *b;
-	int ret;
-
-	b = calloc(1, sizeof(bcf1_t));
-	str = calloc(1, sizeof(kstring_t));
-	idx = (bcf_idx_t*)calloc(1, sizeof(bcf_idx_t));
-	idx->n = h->n_ref;
-	idx->index2 = calloc(h->n_ref, sizeof(bcf_lidx_t));
-
-	last_tid = 0xffffffffu;
-	last_off = bgzf_tell(fp); last_coor = 0xffffffffu;
-	while ((ret = bcf_read(bp, h, b)) > 0) {
-		int end, tmp;
-		if (last_tid != b->tid) { // change of chromosomes
-			last_tid = b->tid;
-		} else if (last_coor > b->pos) {
-			fprintf(pysamerr, "[bcf_idx_core] the input is out of order\n");
-			free(str->s); free(str); free(idx); bcf_destroy(b);
-			return 0;
-		}
-		tmp = strlen(b->ref);
-		end = b->pos + (tmp > 0? tmp : 1);
-		insert_offset2(&idx->index2[b->tid], b->pos, end, last_off);
-		last_off = bgzf_tell(fp);
-		last_coor = b->pos;
-	}
-	free(str->s); free(str); bcf_destroy(b);
-	return idx;
-}
-
-void bcf_idx_destroy(bcf_idx_t *idx)
-{
-	int i;
-	if (idx == 0) return;
-	for (i = 0; i < idx->n; ++i) free(idx->index2[i].offset);
-	free(idx->index2);
-	free(idx);
-}
-
-/******************
- * index file I/O *
- ******************/
-
-void bcf_idx_save(const bcf_idx_t *idx, BGZF *fp)
-{
-	int32_t i, ti_is_be;
-	ti_is_be = bam_is_big_endian();
-	bgzf_write(fp, "BCI\4", 4);
-	if (ti_is_be) {
-		uint32_t x = idx->n;
-		bgzf_write(fp, bam_swap_endian_4p(&x), 4);
-	} else bgzf_write(fp, &idx->n, 4);
-	for (i = 0; i < idx->n; ++i) {
-		bcf_lidx_t *index2 = idx->index2 + i;
-		// write linear index (index2)
-		if (ti_is_be) {
-			int x = index2->n;
-			bgzf_write(fp, bam_swap_endian_4p(&x), 4);
-		} else bgzf_write(fp, &index2->n, 4);
-		if (ti_is_be) { // big endian
-			int x;
-			for (x = 0; (int)x < index2->n; ++x)
-				bam_swap_endian_8p(&index2->offset[x]);
-			bgzf_write(fp, index2->offset, 8 * index2->n);
-			for (x = 0; (int)x < index2->n; ++x)
-				bam_swap_endian_8p(&index2->offset[x]);
-		} else bgzf_write(fp, index2->offset, 8 * index2->n);
-	}
-}
-
-static bcf_idx_t *bcf_idx_load_core(BGZF *fp)
-{
-	int i, ti_is_be;
-	char magic[4];
-	bcf_idx_t *idx;
-	ti_is_be = bam_is_big_endian();
-	if (fp == 0) {
-		fprintf(pysamerr, "[%s] fail to load index.\n", __func__);
-		return 0;
-	}
-	bgzf_read(fp, magic, 4);
-	if (strncmp(magic, "BCI\4", 4)) {
-		fprintf(pysamerr, "[%s] wrong magic number.\n", __func__);
-		return 0;
-	}
-	idx = (bcf_idx_t*)calloc(1, sizeof(bcf_idx_t));	
-	bgzf_read(fp, &idx->n, 4);
-	if (ti_is_be) bam_swap_endian_4p(&idx->n);
-	idx->index2 = (bcf_lidx_t*)calloc(idx->n, sizeof(bcf_lidx_t));
-	for (i = 0; i < idx->n; ++i) {
-		bcf_lidx_t *index2 = idx->index2 + i;
-		int j;
-		bgzf_read(fp, &index2->n, 4);
-		if (ti_is_be) bam_swap_endian_4p(&index2->n);
-		index2->m = index2->n;
-		index2->offset = (uint64_t*)calloc(index2->m, 8);
-		bgzf_read(fp, index2->offset, index2->n * 8);
-		if (ti_is_be)
-			for (j = 0; j < index2->n; ++j) bam_swap_endian_8p(&index2->offset[j]);
-	}
-	return idx;
-}
-
-bcf_idx_t *bcf_idx_load_local(const char *fnidx)
-{
-	BGZF *fp;
-	fp = bgzf_open(fnidx, "r");
-	if (fp) {
-		bcf_idx_t *idx = bcf_idx_load_core(fp);
-		bgzf_close(fp);
-		return idx;
-	} else return 0;
-}
-
-#ifdef _USE_KNETFILE
-static void download_from_remote(const char *url)
-{
-	const int buf_size = 1 * 1024 * 1024;
-	char *fn;
-	FILE *fp;
-	uint8_t *buf;
-	knetFile *fp_remote;
-	int l;
-	if (strstr(url, "ftp://") != url && strstr(url, "http://") != url) return;
-	l = strlen(url);
-	for (fn = (char*)url + l - 1; fn >= url; --fn)
-		if (*fn == '/') break;
-	++fn; // fn now points to the file name
-	fp_remote = knet_open(url, "r");
-	if (fp_remote == 0) {
-		fprintf(pysamerr, "[download_from_remote] fail to open remote file.\n");
-		return;
-	}
-	if ((fp = fopen(fn, "w")) == 0) {
-		fprintf(pysamerr, "[download_from_remote] fail to create file in the working directory.\n");
-		knet_close(fp_remote);
-		return;
-	}
-	buf = (uint8_t*)calloc(buf_size, 1);
-	while ((l = knet_read(fp_remote, buf, buf_size)) != 0)
-		fwrite(buf, 1, l, fp);
-	free(buf);
-	fclose(fp);
-	knet_close(fp_remote);
-}
-#else
-static void download_from_remote(const char *url)
-{
-	return;
-}
-#endif
-
-static char *get_local_version(const char *fn)
-{
-    struct stat sbuf;
-	char *fnidx = (char*)calloc(strlen(fn) + 5, 1);
-	strcat(strcpy(fnidx, fn), ".bci");
-	if ((strstr(fnidx, "ftp://") == fnidx || strstr(fnidx, "http://") == fnidx)) {
-		char *p, *url;
-		int l = strlen(fnidx);
-		for (p = fnidx + l - 1; p >= fnidx; --p)
-			if (*p == '/') break;
-		url = fnidx; fnidx = strdup(p + 1);
-		if (stat(fnidx, &sbuf) == 0) {
-			free(url);
-			return fnidx;
-		}
-		fprintf(pysamerr, "[%s] downloading the index file...\n", __func__);
-		download_from_remote(url);
-		free(url);
-	}
-    if (stat(fnidx, &sbuf) == 0) return fnidx;
-	free(fnidx); return 0;
-}
-
-bcf_idx_t *bcf_idx_load(const char *fn)
-{
-	bcf_idx_t *idx;
-    char *fname = get_local_version(fn);
-	if (fname == 0) return 0;
-	idx = bcf_idx_load_local(fname);
-    free(fname);
-	return idx;
-}
-
-int bcf_idx_build2(const char *fn, const char *_fnidx)
-{
-	char *fnidx;
-	BGZF *fpidx;
-	bcf_t *bp;
-	bcf_idx_t *idx;
-	bcf_hdr_t *h;
-	if ((bp = bcf_open(fn, "r")) == 0) {
-		fprintf(pysamerr, "[bcf_idx_build2] fail to open the BAM file.\n");
-		return -1;
-	}
-	h = bcf_hdr_read(bp);
-	idx = bcf_idx_core(bp, h);
-	bcf_close(bp);
-	if (_fnidx == 0) {
-		fnidx = (char*)calloc(strlen(fn) + 5, 1);
-		strcpy(fnidx, fn); strcat(fnidx, ".bci");
-	} else fnidx = strdup(_fnidx);
-	fpidx = bgzf_open(fnidx, "w");
-	if (fpidx == 0) {
-		fprintf(pysamerr, "[bcf_idx_build2] fail to create the index file.\n");
-		free(fnidx);
-        bcf_idx_destroy(idx);
-		return -1;
-	}
-	bcf_idx_save(idx, fpidx);
-	bcf_idx_destroy(idx);
-	bgzf_close(fpidx);
-	free(fnidx);
-	return 0;
-}
-
-int bcf_idx_build(const char *fn)
-{
-	return bcf_idx_build2(fn, 0);
-}
-
-/********************************************
- * parse a region in the format chr:beg-end *
- ********************************************/
-
-int bcf_parse_region(void *str2id, const char *str, int *tid, int *begin, int *end)
-{
-	char *s, *p;
-	int i, l, k;
-	l = strlen(str);
-	p = s = (char*)malloc(l+1);
-	/* squeeze out "," */
-	for (i = k = 0; i != l; ++i)
-		if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
-	s[k] = 0;
-	for (i = 0; i != k; ++i) if (s[i] == ':') break;
-	s[i] = 0;
-	if ((*tid = bcf_str2id(str2id, s)) < 0) {
-		free(s);
-		return -1;
-	}
-	if (i == k) { /* dump the whole sequence */
-		*begin = 0; *end = 1<<29; free(s);
-		return 0;
-	}
-	for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
-	*begin = atoi(p);
-	if (i < k) {
-		p = s + i + 1;
-		*end = atoi(p);
-	} else *end = 1<<29;
-	if (*begin > 0) --*begin;
-	free(s);
-	if (*begin > *end) return -1;
-	return 0;
-}
-
-/*******************************
- * retrieve a specified region *
- *******************************/
-
-uint64_t bcf_idx_query(const bcf_idx_t *idx, int tid, int beg)
-{
-	uint64_t min_off, *offset;
-	int i;
-	if (beg < 0) beg = 0;
-	offset = idx->index2[tid].offset;
-	for (i = beg>>TAD_LIDX_SHIFT; i < idx->index2[tid].n && offset[i] == 0; ++i);
-	min_off = (i == idx->index2[tid].n)? offset[idx->index2[tid].n-1] : offset[i];
-	return min_off;
-}
-
-int bcf_main_index(int argc, char *argv[])
-{
-	if (argc == 1) {
-		fprintf(pysamerr, "Usage: bcftools index <in.bcf>\n");
-		return 1;
-	}
-	bcf_idx_build(argv[1]);
-	return 0;
-}
diff --git a/samtools/bcftools/kfunc.c.pysam.c b/samtools/bcftools/kfunc.c.pysam.c
deleted file mode 100644
index 4de836e..0000000
--- a/samtools/bcftools/kfunc.c.pysam.c
+++ /dev/null
@@ -1,164 +0,0 @@
-#include "pysam.h"
-
-#include <math.h>
-
-
-/* Log gamma function
- * \log{\Gamma(z)}
- * AS245, 2nd algorithm, http://lib.stat.cmu.edu/apstat/245
- */
-double kf_lgamma(double z)
-{
-	double x = 0;
-	x += 0.1659470187408462e-06 / (z+7);
-	x += 0.9934937113930748e-05 / (z+6);
-	x -= 0.1385710331296526     / (z+5);
-	x += 12.50734324009056      / (z+4);
-	x -= 176.6150291498386      / (z+3);
-	x += 771.3234287757674      / (z+2);
-	x -= 1259.139216722289      / (z+1);
-	x += 676.5203681218835      / z;
-	x += 0.9999999999995183;
-	return log(x) - 5.58106146679532777 - z + (z-0.5) * log(z+6.5);
-}
-
-/* complementary error function
- * \frac{2}{\sqrt{\pi}} \int_x^{\infty} e^{-t^2} dt
- * AS66, 2nd algorithm, http://lib.stat.cmu.edu/apstat/66
- */
-double kf_erfc(double x)
-{
-	const double p0 = 220.2068679123761;
-	const double p1 = 221.2135961699311;
-	const double p2 = 112.0792914978709;
-	const double p3 = 33.912866078383;
-	const double p4 = 6.37396220353165;
-	const double p5 = .7003830644436881;
-	const double p6 = .03526249659989109;
-	const double q0 = 440.4137358247522;
-	const double q1 = 793.8265125199484;
-	const double q2 = 637.3336333788311;
-	const double q3 = 296.5642487796737;
-	const double q4 = 86.78073220294608;
-	const double q5 = 16.06417757920695;
-	const double q6 = 1.755667163182642;
-	const double q7 = .08838834764831844;
-	double expntl, z, p;
-	z = fabs(x) * M_SQRT2;
-	if (z > 37.) return x > 0.? 0. : 2.;
-	expntl = exp(z * z * - .5);
-	if (z < 10. / M_SQRT2) // for small z
-	    p = expntl * ((((((p6 * z + p5) * z + p4) * z + p3) * z + p2) * z + p1) * z + p0)
-			/ (((((((q7 * z + q6) * z + q5) * z + q4) * z + q3) * z + q2) * z + q1) * z + q0);
-	else p = expntl / 2.506628274631001 / (z + 1. / (z + 2. / (z + 3. / (z + 4. / (z + .65)))));
-	return x > 0.? 2. * p : 2. * (1. - p);
-}
-
-/* The following computes regularized incomplete gamma functions.
- * Formulas are taken from Wiki, with additional input from Numerical
- * Recipes in C (for modified Lentz's algorithm) and AS245
- * (http://lib.stat.cmu.edu/apstat/245).
- *
- * A good online calculator is available at:
- *
- *   http://www.danielsoper.com/statcalc/calc23.aspx
- *
- * It calculates upper incomplete gamma function, which equals
- * kf_gammaq(s,z)*tgamma(s).
- */
-
-#define KF_GAMMA_EPS 1e-14
-#define KF_TINY 1e-290
-
-// regularized lower incomplete gamma function, by series expansion
-static double _kf_gammap(double s, double z)
-{
-	double sum, x;
-	int k;
-	for (k = 1, sum = x = 1.; k < 100; ++k) {
-		sum += (x *= z / (s + k));
-		if (x / sum < KF_GAMMA_EPS) break;
-	}
-	return exp(s * log(z) - z - kf_lgamma(s + 1.) + log(sum));
-}
-// regularized upper incomplete gamma function, by continued fraction
-static double _kf_gammaq(double s, double z)
-{
-	int j;
-	double C, D, f;
-	f = 1. + z - s; C = f; D = 0.;
-	// Modified Lentz's algorithm for computing continued fraction
-	// See Numerical Recipes in C, 2nd edition, section 5.2
-	for (j = 1; j < 100; ++j) {
-		double a = j * (s - j), b = (j<<1) + 1 + z - s, d;
-		D = b + a * D;
-		if (D < KF_TINY) D = KF_TINY;
-		C = b + a / C;
-		if (C < KF_TINY) C = KF_TINY;
-		D = 1. / D;
-		d = C * D;
-		f *= d;
-		if (fabs(d - 1.) < KF_GAMMA_EPS) break;
-	}
-	return exp(s * log(z) - z - kf_lgamma(s) - log(f));
-}
-
-double kf_gammap(double s, double z)
-{
-	return z <= 1. || z < s? _kf_gammap(s, z) : 1. - _kf_gammaq(s, z);
-}
-
-double kf_gammaq(double s, double z)
-{
-	return z <= 1. || z < s? 1. - _kf_gammap(s, z) : _kf_gammaq(s, z);
-}
-
-/* Regularized incomplete beta function. The method is taken from
- * Numerical Recipe in C, 2nd edition, section 6.4. The following web
- * page calculates the incomplete beta function, which equals
- * kf_betai(a,b,x) * gamma(a) * gamma(b) / gamma(a+b):
- *
- *   http://www.danielsoper.com/statcalc/calc36.aspx
- */
-static double kf_betai_aux(double a, double b, double x)
-{
-	double C, D, f;
-	int j;
-	if (x == 0.) return 0.;
-	if (x == 1.) return 1.;
-	f = 1.; C = f; D = 0.;
-	// Modified Lentz's algorithm for computing continued fraction
-	for (j = 1; j < 200; ++j) {
-		double aa, d;
-		int m = j>>1;
-		aa = (j&1)? -(a + m) * (a + b + m) * x / ((a + 2*m) * (a + 2*m + 1))
-			: m * (b - m) * x / ((a + 2*m - 1) * (a + 2*m));
-		D = 1. + aa * D;
-		if (D < KF_TINY) D = KF_TINY;
-		C = 1. + aa / C;
-		if (C < KF_TINY) C = KF_TINY;
-		D = 1. / D;
-		d = C * D;
-		f *= d;
-		if (fabs(d - 1.) < KF_GAMMA_EPS) break;
-	}
-	return exp(kf_lgamma(a+b) - kf_lgamma(a) - kf_lgamma(b) + a * log(x) + b * log(1.-x)) / a / f;
-}
-double kf_betai(double a, double b, double x)
-{
-	return x < (a + 1.) / (a + b + 2.)? kf_betai_aux(a, b, x) : 1. - kf_betai_aux(b, a, 1. - x);
-}
-
-#ifdef KF_MAIN
-#include <stdio.h>
-int main(int argc, char *argv[])
-{
-	double x = 5.5, y = 3;
-	double a, b;
-	printf("erfc(%lg): %lg, %lg\n", x, erfc(x), kf_erfc(x));
-	printf("upper-gamma(%lg,%lg): %lg\n", x, y, kf_gammaq(y, x)*tgamma(y));
-	a = 2; b = 2; x = 0.5;
-	printf("incomplete-beta(%lg,%lg,%lg): %lg\n", a, b, x, kf_betai(a, b, x) / exp(kf_lgamma(a+b) - kf_lgamma(a) - kf_lgamma(b)));
-	return 0;
-}
-#endif
diff --git a/samtools/bcftools/kmin.c.pysam.c b/samtools/bcftools/kmin.c.pysam.c
deleted file mode 100644
index ee7b512..0000000
--- a/samtools/bcftools/kmin.c.pysam.c
+++ /dev/null
@@ -1,211 +0,0 @@
-#include "pysam.h"
-
-/* The MIT License
-
-   Copyright (c) 2008, 2010 by Attractive Chaos <attractor at live.co.uk>
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-/* Hooke-Jeeves algorithm for nonlinear minimization
- 
-   Based on the pseudocodes by Bell and Pike (CACM 9(9):684-685), and
-   the revision by Tomlin and Smith (CACM 12(11):637-638). Both of the
-   papers are comments on Kaupe's Algorithm 178 "Direct Search" (ACM
-   6(6):313-314). The original algorithm was designed by Hooke and
-   Jeeves (ACM 8:212-229). This program is further revised according to
-   Johnson's implementation at Netlib (opt/hooke.c).
- 
-   Hooke-Jeeves algorithm is very simple and it works quite well on a
-   few examples. However, it might fail to converge due to its heuristic
-   nature. A possible improvement, as is suggested by Johnson, may be to
-   choose a small r at the beginning to quickly approach to the minimum
-   and a large r at later step to hit the minimum.
- */
-
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include "kmin.h"
-
-static double __kmin_hj_aux(kmin_f func, int n, double *x1, void *data, double fx1, double *dx, int *n_calls)
-{
-	int k, j = *n_calls;
-	double ftmp;
-	for (k = 0; k != n; ++k) {
-		x1[k] += dx[k];
-		ftmp = func(n, x1, data); ++j;
-		if (ftmp < fx1) fx1 = ftmp;
-		else { /* search the opposite direction */
-			dx[k] = 0.0 - dx[k];
-			x1[k] += dx[k] + dx[k];
-			ftmp = func(n, x1, data); ++j;
-			if (ftmp < fx1) fx1 = ftmp;
-			else x1[k] -= dx[k]; /* back to the original x[k] */
-		}
-	}
-	*n_calls = j;
-	return fx1; /* here: fx1=f(n,x1) */
-}
-
-double kmin_hj(kmin_f func, int n, double *x, void *data, double r, double eps, int max_calls)
-{
-	double fx, fx1, *x1, *dx, radius;
-	int k, n_calls = 0;
-	x1 = (double*)calloc(n, sizeof(double));
-	dx = (double*)calloc(n, sizeof(double));
-	for (k = 0; k != n; ++k) { /* initial directions, based on MGJ */
-		dx[k] = fabs(x[k]) * r;
-		if (dx[k] == 0) dx[k] = r;
-	}
-	radius = r;
-	fx1 = fx = func(n, x, data); ++n_calls;
-	for (;;) {
-		memcpy(x1, x, n * sizeof(double)); /* x1 = x */
-		fx1 = __kmin_hj_aux(func, n, x1, data, fx, dx, &n_calls);
-		while (fx1 < fx) {
-			for (k = 0; k != n; ++k) {
-				double t = x[k];
-				dx[k] = x1[k] > x[k]? fabs(dx[k]) : 0.0 - fabs(dx[k]);
-				x[k] = x1[k];
-				x1[k] = x1[k] + x1[k] - t;
-			}
-			fx = fx1;
-			if (n_calls >= max_calls) break;
-			fx1 = func(n, x1, data); ++n_calls;
-			fx1 = __kmin_hj_aux(func, n, x1, data, fx1, dx, &n_calls);
-			if (fx1 >= fx) break;
-			for (k = 0; k != n; ++k)
-				if (fabs(x1[k] - x[k]) > .5 * fabs(dx[k])) break;
-			if (k == n) break;
-		}
-		if (radius >= eps) {
-			if (n_calls >= max_calls) break;
-			radius *= r;
-			for (k = 0; k != n; ++k) dx[k] *= r;
-		} else break; /* converge */
-	}
-	free(x1); free(dx);
-	return fx1;
-}
-
-// I copied this function somewhere several years ago with some of my modifications, but I forgot the source.
-double kmin_brent(kmin1_f func, double a, double b, void *data, double tol, double *xmin)
-{
-	double bound, u, r, q, fu, tmp, fa, fb, fc, c;
-	const double gold1 = 1.6180339887;
-	const double gold2 = 0.3819660113;
-	const double tiny = 1e-20;
-	const int max_iter = 100;
-
-	double e, d, w, v, mid, tol1, tol2, p, eold, fv, fw;
-	int iter;
-
-	fa = func(a, data); fb = func(b, data);
-	if (fb > fa) { // swap, such that f(a) > f(b)
-		tmp = a; a = b; b = tmp;
-		tmp = fa; fa = fb; fb = tmp;
-	}
-	c = b + gold1 * (b - a), fc = func(c, data); // golden section extrapolation
-	while (fb > fc) {
-		bound = b + 100.0 * (c - b); // the farthest point where we want to go
-		r = (b - a) * (fb - fc);
-		q = (b - c) * (fb - fa);
-		if (fabs(q - r) < tiny) { // avoid 0 denominator
-			tmp = q > r? tiny : 0.0 - tiny;
-		} else tmp = q - r;
-		u = b - ((b - c) * q - (b - a) * r) / (2.0 * tmp); // u is the parabolic extrapolation point
-		if ((b > u && u > c) || (b < u && u < c)) { // u lies between b and c
-			fu = func(u, data);
-			if (fu < fc) { // (b,u,c) bracket the minimum
-				a = b; b = u; fa = fb; fb = fu;
-				break;
-			} else if (fu > fb) { // (a,b,u) bracket the minimum
-				c = u; fc = fu;
-				break;
-			}
-			u = c + gold1 * (c - b); fu = func(u, data); // golden section extrapolation
-		} else if ((c > u && u > bound) || (c < u && u < bound)) { // u lies between c and bound
-			fu = func(u, data);
-			if (fu < fc) { // fb > fc > fu
-				b = c; c = u; u = c + gold1 * (c - b);
-				fb = fc; fc = fu; fu = func(u, data);
-			} else { // (b,c,u) bracket the minimum
-				a = b; b = c; c = u;
-				fa = fb; fb = fc; fc = fu;
-				break;
-			}
-		} else if ((u > bound && bound > c) || (u < bound && bound < c)) { // u goes beyond the bound
-			u = bound; fu = func(u, data);
-		} else { // u goes the other way around, use golden section extrapolation
-			u = c + gold1 * (c - b); fu = func(u, data);
-		}
-		a = b; b = c; c = u;
-		fa = fb; fb = fc; fc = fu;
-	}
-	if (a > c) u = a, a = c, c = u; // swap
-
-	// now, a<b<c, fa>fb and fb<fc, move on to Brent's algorithm
-	e = d = 0.0;
-	w = v = b; fv = fw = fb;
-	for (iter = 0; iter != max_iter; ++iter) {
-		mid = 0.5 * (a + c);
-		tol2 = 2.0 * (tol1 = tol * fabs(b) + tiny);
-		if (fabs(b - mid) <= (tol2 - 0.5 * (c - a))) {
-			*xmin = b; return fb; // found
-		}
-		if (fabs(e) > tol1) {
-			// related to parabolic interpolation
-			r = (b - w) * (fb - fv);
-			q = (b - v) * (fb - fw);
-			p = (b - v) * q - (b - w) * r;
-			q = 2.0 * (q - r);
-			if (q > 0.0) p = 0.0 - p;
-			else q = 0.0 - q;
-			eold = e; e = d;
-			if (fabs(p) >= fabs(0.5 * q * eold) || p <= q * (a - b) || p >= q * (c - b)) {
-				d = gold2 * (e = (b >= mid ? a - b : c - b));
-			} else {
-				d = p / q; u = b + d; // actual parabolic interpolation happens here
-				if (u - a < tol2 || c - u < tol2)
-					d = (mid > b)? tol1 : 0.0 - tol1;
-			}
-		} else d = gold2 * (e = (b >= mid ? a - b : c - b)); // golden section interpolation
-		u = fabs(d) >= tol1 ? b + d : b + (d > 0.0? tol1 : -tol1);
-		fu = func(u, data);
-		if (fu <= fb) { // u is the minimum point so far
-			if (u >= b) a = b;
-			else c = b;
-			v = w; w = b; b = u; fv = fw; fw = fb; fb = fu;
-		} else { // adjust (a,c) and (u,v,w)
-			if (u < b) a = u;
-			else c = u;
-			if (fu <= fw || w == b) {
-				v = w; w = u;
-				fv = fw; fw = fu;
-			} else if (fu <= fv || v == b || v == w) {
-				v = u; fv = fu;
-			}
-		}
-	}
-	*xmin = b;
-	return fb;
-}
diff --git a/samtools/bcftools/kmin.h b/samtools/bcftools/kmin.h
deleted file mode 100644
index 6feba45..0000000
--- a/samtools/bcftools/kmin.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
-   Copyright (c) 2008, 2010 by Attractive Chaos <attractor at live.co.uk>
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-#ifndef KMIN_H
-#define KMIN_H
-
-#define KMIN_RADIUS  0.5
-#define KMIN_EPS     1e-7
-#define KMIN_MAXCALL 50000
-
-typedef double (*kmin_f)(int, double*, void*);
-typedef double (*kmin1_f)(double, void*);
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-	double kmin_hj(kmin_f func, int n, double *x, void *data, double r, double eps, int max_calls);
-	double kmin_brent(kmin1_f func, double a, double b, void *data, double tol, double *xmin);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/samtools/bcftools/mut.c.pysam.c b/samtools/bcftools/mut.c.pysam.c
deleted file mode 100644
index 59ffb60..0000000
--- a/samtools/bcftools/mut.c.pysam.c
+++ /dev/null
@@ -1,129 +0,0 @@
-#include "pysam.h"
-
-#include <stdlib.h>
-#include <stdint.h>
-#include "bcf.h"
-
-#define MAX_GENO 359
-
-int8_t seq_bitcnt[] = { 4, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
-char *seq_nt16rev = "XACMGRSVTWYHKDBN";
-
-uint32_t *bcf_trio_prep(int is_x, int is_son)
-{
-	int i, j, k, n, map[10];
-	uint32_t *ret;
-	ret = calloc(MAX_GENO, 4);
-	for (i = 0, k = 0; i < 4; ++i)
-		for (j = i; j < 4; ++j)
-			map[k++] = 1<<i|1<<j;
-	for (i = 0, n = 1; i < 10; ++i) { // father
-		if (is_x && seq_bitcnt[map[i]] != 1) continue;
-		if (is_x && is_son) {
-			for (j = 0; j < 10; ++j) // mother
-				for (k = 0; k < 10; ++k) // child
-					if (seq_bitcnt[map[k]] == 1 && (map[j]&map[k]))
-						ret[n++] = j<<16 | i<<8 | k;
-		} else {
-			for (j = 0; j < 10; ++j) // mother
-				for (k = 0; k < 10; ++k) // child
-					if ((map[i]&map[k]) && (map[j]&map[k]) && ((map[i]|map[j])&map[k]) == map[k])
-						ret[n++] = j<<16 | i<<8 | k;
-		}
-	}
-	ret[0] = n - 1;
-	return ret;
-}
-
-
-int bcf_trio_call(const uint32_t *prep, const bcf1_t *b, int *llr, int64_t *gt)
-{
-	int i, j, k;
-	const bcf_ginfo_t *PL;
-	uint8_t *gl10;
-	int map[10];
-	if (b->n_smpl != 3) return -1; // not a trio
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
-	if (i == b->n_gi) return -1; // no PL
-	gl10 = alloca(10 * b->n_smpl);
-	if (bcf_gl10(b, gl10) < 0) {
-		if (bcf_gl10_indel(b, gl10) < 0) return -1;
-	}
-	PL = b->gi + i;
-	for (i = 0, k = 0; i < 4; ++i)
-		for (j = i; j < 4; ++j)
-			map[k++] = seq_nt16rev[1<<i|1<<j];
-	for (j = 0; j < 3; ++j) // check if ref hom is the most probable in all members
-		if (((uint8_t*)PL->data)[j * PL->len] != 0) break;
-	if (j < 3) { // we need to go through the complex procedure
-		uint8_t *g[3];
-		int minc = 1<<30, minc_j = -1, minf = 0, gtf = 0, gtc = 0;
-		g[0] = gl10;
-		g[1] = gl10 + 10;
-		g[2] = gl10 + 20;
-		for (j = 1; j <= (int)prep[0]; ++j) { // compute LK with constraint
-			int sum = g[0][prep[j]&0xff] + g[1][prep[j]>>8&0xff] + g[2][prep[j]>>16&0xff];
-			if (sum < minc) minc = sum, minc_j = j;
-		}
-		gtc |= map[prep[minc_j]&0xff]; gtc |= map[prep[minc_j]>>8&0xff]<<8; gtc |= map[prep[minc_j]>>16]<<16;
-		for (j = 0; j < 3; ++j) { // compute LK without constraint
-			int min = 1<<30, min_k = -1;
-			for (k = 0; k < 10; ++k)
-				if (g[j][k] < min) min = g[j][k], min_k = k;
-			gtf |= map[min_k]<<(j*8);
-			minf += min;
-		}
-		*llr = minc - minf; *gt = (int64_t)gtc<<32 | gtf;
-	} else *llr = 0, *gt = -1;
-	return 0;
-}
-
-int bcf_pair_call(const bcf1_t *b)
-{
-	int i, j, k;
-	const bcf_ginfo_t *PL;
-	if (b->n_smpl != 2) return -1; // not a pair
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
-	if (i == b->n_gi) return -1; // no PL
-	PL = b->gi + i;
-	for (j = 0; j < 2; ++j) // check if ref hom is the most probable in all members
-		if (((uint8_t*)PL->data)[j * PL->len] != 0) break;
-	if (j < 2) { // we need to go through the complex procedure
-		uint8_t *g[2];
-		int minc = 1<<30, minf = 0;
-		g[0] = PL->data;
-		g[1] = (uint8_t*)PL->data + PL->len;
-		for (j = 0; j < PL->len; ++j) // compute LK with constraint
-			minc = minc < g[0][j] + g[1][j]? minc : g[0][j] + g[1][j];
-		for (j = 0; j < 2; ++j) { // compute LK without constraint
-			int min = 1<<30;
-			for (k = 0; k < PL->len; ++k)
-				min = min < g[j][k]? min : g[j][k];
-			minf += min;
-		}
-		return minc - minf;
-	} else return 0;
-}
-
-int bcf_min_diff(const bcf1_t *b)
-{
-	int i, min = 1<<30;
-	const bcf_ginfo_t *PL;
-	for (i = 0; i < b->n_gi; ++i)
-		if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
-	if (i == b->n_gi) return -1; // no PL
-	PL = b->gi + i;
-	for (i = 0; i < b->n_smpl; ++i) {
-		int m1, m2, j;
-		const uint8_t *p = (uint8_t*)PL->data;
-		m1 = m2 = 1<<30;
-		for (j = 0; j < PL->len; ++j) {
-			if ((int)p[j] < m1) m2 = m1, m1 = p[j];
-			else if ((int)p[j] < m2) m2 = p[j];
-		}
-		min = min < m2 - m1? min : m2 - m1;
-	}
-	return min;
-}
diff --git a/samtools/bcftools/prob1.c.pysam.c b/samtools/bcftools/prob1.c.pysam.c
deleted file mode 100644
index 235c83b..0000000
--- a/samtools/bcftools/prob1.c.pysam.c
+++ /dev/null
@@ -1,990 +0,0 @@
-#include "pysam.h"
-
-#include <math.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-#include <errno.h>
-#include <assert.h>
-#include <limits.h>
-#include <zlib.h>
-#include "prob1.h"
-#include "kstring.h"
-
-#include "kseq.h"
-KSTREAM_INIT(gzFile, gzread, 16384)
-
-#define MC_MAX_EM_ITER 16
-#define MC_EM_EPS 1e-5
-#define MC_DEF_INDEL 0.15
-
-gzFile bcf_p1_fp_lk;
-
-unsigned char seq_nt4_table[256] = {
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4 /*'-'*/, 4, 4,
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 0, 4, 1,  4, 4, 4, 2,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  3, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 0, 4, 1,  4, 4, 4, 2,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  3, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
-	4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4
-};
-
-struct __bcf_p1aux_t {
-	int n, M, n1, is_indel;
-	uint8_t *ploidy; // haploid or diploid ONLY
-	double *q2p, *pdg; // pdg -> P(D|g)
-	double *phi, *phi_indel;
-	double *z, *zswap; // aux for afs
-	double *z1, *z2, *phi1, *phi2; // only calculated when n1 is set
-	double **hg; // hypergeometric distribution
-	double *lf; // log factorial
-	double t, t1, t2;
-	double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution
-	const uint8_t *PL; // point to PL
-	int PL_len;
-};
-
-void bcf_p1_indel_prior(bcf_p1aux_t *ma, double x)
-{
-	int i;
-	for (i = 0; i < ma->M; ++i)
-		ma->phi_indel[i] = ma->phi[i] * x;
-	ma->phi_indel[ma->M] = 1. - ma->phi[ma->M] * x;
-}
-
-static void init_prior(int type, double theta, int M, double *phi)
-{
-	int i;
-	if (type == MC_PTYPE_COND2) {
-		for (i = 0; i <= M; ++i)
-			phi[i] = 2. * (i + 1) / (M + 1) / (M + 2);
-	} else if (type == MC_PTYPE_FLAT) {
-		for (i = 0; i <= M; ++i)
-			phi[i] = 1. / (M + 1);
-	} else {
-		double sum;
-		for (i = 0, sum = 0.; i < M; ++i)
-			sum += (phi[i] = theta / (M - i));
-		phi[M] = 1. - sum;
-	}
-}
-
-void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta)
-{
-	init_prior(type, theta, ma->M, ma->phi);
-	bcf_p1_indel_prior(ma, MC_DEF_INDEL);
-}
-
-void bcf_p1_init_subprior(bcf_p1aux_t *ma, int type, double theta)
-{
-	if (ma->n1 <= 0 || ma->n1 >= ma->M) return;
-	init_prior(type, theta, 2*ma->n1, ma->phi1);
-	init_prior(type, theta, 2*(ma->n - ma->n1), ma->phi2);
-}
-
-int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn)
-{
-	gzFile fp;
-	kstring_t s;
-	kstream_t *ks;
-	long double sum;
-	int dret, k;
-	memset(&s, 0, sizeof(kstring_t));
-	fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
-	ks = ks_init(fp);
-	memset(ma->phi, 0, sizeof(double) * (ma->M + 1));
-	while (ks_getuntil(ks, '\n', &s, &dret) >= 0) {
-		if (strstr(s.s, "[afs] ") == s.s) {
-			char *p = s.s + 6;
-			for (k = 0; k <= ma->M; ++k) {
-				int x;
-				double y;
-				x = strtol(p, &p, 10);
-				if (x != k && (errno == EINVAL || errno == ERANGE)) return -1;
-				++p;
-				y = strtod(p, &p);
-				if (y == 0. && (errno == EINVAL || errno == ERANGE)) return -1;
-				ma->phi[ma->M - k] += y;
-			}
-		}
-	}
-	ks_destroy(ks);
-	gzclose(fp);
-	free(s.s);
-	for (sum = 0., k = 0; k <= ma->M; ++k) sum += ma->phi[k];
-	fprintf(pysamerr, "[prior]");
-	for (k = 0; k <= ma->M; ++k) ma->phi[k] /= sum;
-	for (k = 0; k <= ma->M; ++k) fprintf(pysamerr, " %d:%.3lg", k, ma->phi[ma->M - k]);
-	fputc('\n', pysamerr);
-	for (sum = 0., k = 1; k < ma->M; ++k) sum += ma->phi[ma->M - k] * (2.* k * (ma->M - k) / ma->M / (ma->M - 1));
-	fprintf(pysamerr, "[%s] heterozygosity=%lf, ", __func__, (double)sum);
-	for (sum = 0., k = 1; k <= ma->M; ++k) sum += k * ma->phi[ma->M - k] / ma->M;
-	fprintf(pysamerr, "theta=%lf\n", (double)sum);
-	bcf_p1_indel_prior(ma, MC_DEF_INDEL);
-	return 0;
-}
-
-bcf_p1aux_t *bcf_p1_init(int n, uint8_t *ploidy)
-{
-	bcf_p1aux_t *ma;
-	int i;
-	ma = calloc(1, sizeof(bcf_p1aux_t));
-	ma->n1 = -1;
-	ma->n = n; ma->M = 2 * n;
-	if (ploidy) {
-		ma->ploidy = malloc(n);
-		memcpy(ma->ploidy, ploidy, n);
-		for (i = 0, ma->M = 0; i < n; ++i) ma->M += ploidy[i];
-		if (ma->M == 2 * n) {
-			free(ma->ploidy);
-			ma->ploidy = 0;
-		}
-	}
-	ma->q2p = calloc(256, sizeof(double));
-	ma->pdg = calloc(3 * ma->n, sizeof(double));
-	ma->phi = calloc(ma->M + 1, sizeof(double));
-	ma->phi_indel = calloc(ma->M + 1, sizeof(double));
-	ma->phi1 = calloc(ma->M + 1, sizeof(double));
-	ma->phi2 = calloc(ma->M + 1, sizeof(double));
-	ma->z = calloc(ma->M + 1, sizeof(double));
-	ma->zswap = calloc(ma->M + 1, sizeof(double));
-	ma->z1 = calloc(ma->M + 1, sizeof(double)); // actually we do not need this large
-	ma->z2 = calloc(ma->M + 1, sizeof(double));
-	ma->afs = calloc(ma->M + 1, sizeof(double));
-	ma->afs1 = calloc(ma->M + 1, sizeof(double));
-	ma->lf = calloc(ma->M + 1, sizeof(double));
-	for (i = 0; i < 256; ++i)
-		ma->q2p[i] = pow(10., -i / 10.);
-	for (i = 0; i <= ma->M; ++i) ma->lf[i] = lgamma(i + 1);
-	bcf_p1_init_prior(ma, MC_PTYPE_FULL, 1e-3); // the simplest prior
-	return ma;
-}
-
-int bcf_p1_get_M(bcf_p1aux_t *b) { return b->M; }
-
-int bcf_p1_set_n1(bcf_p1aux_t *b, int n1)
-{
-	if (n1 == 0 || n1 >= b->n) return -1;
-	if (b->M != b->n * 2) {
-		fprintf(pysamerr, "[%s] unable to set `n1' when there are haploid samples.\n", __func__);
-		return -1;
-	}
-	b->n1 = n1;
-	return 0;
-}
-
-void bcf_p1_set_ploidy(bcf1_t *b, bcf_p1aux_t *ma)
-{
-    // bcf_p1aux_t fields are not visible outside of prob1.c, hence this wrapper.
-    // Ideally, this should set ploidy per site to allow pseudo-autosomal regions
-    b->ploidy = ma->ploidy;
-}
-
-void bcf_p1_destroy(bcf_p1aux_t *ma)
-{
-	if (ma) {
-		int k;
-		free(ma->lf);
-		if (ma->hg && ma->n1 > 0) {
-			for (k = 0; k <= 2*ma->n1; ++k) free(ma->hg[k]);
-			free(ma->hg);
-		}
-		free(ma->ploidy); free(ma->q2p); free(ma->pdg);
-		free(ma->phi); free(ma->phi_indel); free(ma->phi1); free(ma->phi2);
-		free(ma->z); free(ma->zswap); free(ma->z1); free(ma->z2);
-		free(ma->afs); free(ma->afs1);
-		free(ma);
-	}
-}
-
-extern double kf_gammap(double s, double z);
-int test16(bcf1_t *b, anno16_t *a);
-
-// Wigginton 2005, PMID: 15789306
-// written by Jan Wigginton
-double calc_hwe(int obs_hom1, int obs_hom2, int obs_hets)
-{
-    if (obs_hom1 + obs_hom2 + obs_hets == 0 ) return 1;
-
-    assert(obs_hom1 >= 0 && obs_hom2 >= 0 && obs_hets >= 0);
-
-    int obs_homc = obs_hom1 < obs_hom2 ? obs_hom2 : obs_hom1;
-    int obs_homr = obs_hom1 < obs_hom2 ? obs_hom1 : obs_hom2;
-
-    int rare_copies = 2 * obs_homr + obs_hets;
-    int genotypes   = obs_hets + obs_homc + obs_homr;
-
-    double *het_probs = (double*) calloc(rare_copies+1, sizeof(double));
-
-    /* start at midpoint */
-    int mid = rare_copies * (2 * genotypes - rare_copies) / (2 * genotypes);
-
-    /* check to ensure that midpoint and rare alleles have same parity */
-    if ((rare_copies & 1) ^ (mid & 1)) mid++;
-
-    int curr_hets = mid;
-    int curr_homr = (rare_copies - mid) / 2;
-    int curr_homc = genotypes - curr_hets - curr_homr;
-
-    het_probs[mid] = 1.0;
-    double sum = het_probs[mid];
-    for (curr_hets = mid; curr_hets > 1; curr_hets -= 2)
-    {
-        het_probs[curr_hets - 2] = het_probs[curr_hets] * curr_hets * (curr_hets - 1.0) / (4.0 * (curr_homr + 1.0) * (curr_homc + 1.0));
-        sum += het_probs[curr_hets - 2];
-
-        /* 2 fewer heterozygotes for next iteration -> add one rare, one common homozygote */
-        curr_homr++;
-        curr_homc++;
-    }
-
-    curr_hets = mid;
-    curr_homr = (rare_copies - mid) / 2;
-    curr_homc = genotypes - curr_hets - curr_homr;
-    for (curr_hets = mid; curr_hets <= rare_copies - 2; curr_hets += 2)
-    {
-        het_probs[curr_hets + 2] = het_probs[curr_hets] * 4.0 * curr_homr * curr_homc /((curr_hets + 2.0) * (curr_hets + 1.0));
-        sum += het_probs[curr_hets + 2];
-
-        /* add 2 heterozygotes for next iteration -> subtract one rare, one common homozygote */
-        curr_homr--;
-        curr_homc--;
-    }
-    int i;
-    for (i = 0; i <= rare_copies; i++) het_probs[i] /= sum;
-
-    /*  p-value calculation for p_hwe  */
-    double p_hwe = 0.0;
-    for (i = 0; i <= rare_copies; i++)
-    {
-        if (het_probs[i] > het_probs[obs_hets])
-            continue;
-        p_hwe += het_probs[i];
-    }
-
-    p_hwe = p_hwe > 1.0 ? 1.0 : p_hwe;
-    free(het_probs);
-    return p_hwe;
-
-}
-
-
-static void _bcf1_set_ref(bcf1_t *b, int idp)
-{
-    kstring_t s;
-    int old_n_gi = b->n_gi;
-    s.m = b->m_str; s.l = b->l_str - 1; s.s = b->str;
-    kputs(":GT", &s); kputc('\0', &s);
-    b->m_str = s.m; b->l_str = s.l; b->str = s.s;
-    bcf_sync(b);
-
-    // Call GTs
-    int isample, an = 0;
-    for (isample = 0; isample < b->n_smpl; isample++) 
-    {
-        if ( idp>=0 && ((uint16_t*)b->gi[idp].data)[isample]==0 )
-            ((uint8_t*)b->gi[old_n_gi].data)[isample] = 1<<7;
-        else
-        {
-            ((uint8_t*)b->gi[old_n_gi].data)[isample] = 0;
-            an += b->ploidy ? b->ploidy[isample] : 2;
-        }
-    }
-    bcf_fit_alt(b,1);
-    b->qual = 999;
-
-    // Prepare BCF for output: ref, alt, filter, info, format
-    memset(&s, 0, sizeof(kstring_t)); kputc('\0', &s); 
-    kputs(b->ref, &s); kputc('\0', &s);
-    kputs(b->alt, &s); kputc('\0', &s); kputc('\0', &s);
-    {
-        ksprintf(&s, "AN=%d;", an);
-        kputs(b->info, &s); 
-        anno16_t a;
-        int has_I16 = test16(b, &a) >= 0? 1 : 0;
-        if (has_I16 )
-        {
-            if ( a.is_tested) ksprintf(&s, ";PV4=%.2g,%.2g,%.2g,%.2g", a.p[0], a.p[1], a.p[2], a.p[3]);
-            ksprintf(&s, ";DP4=%d,%d,%d,%d;MQ=%d", a.d[0], a.d[1], a.d[2], a.d[3], a.mq);
-        }
-        kputc('\0', &s);
-        rm_info(&s, "I16=");
-        rm_info(&s, "QS=");
-    }
-    kputs(b->fmt, &s); kputc('\0', &s);
-    free(b->str);
-    b->m_str = s.m; b->l_str = s.l; b->str = s.s;
-    bcf_sync(b);
-}
-
-int call_multiallelic_gt(bcf1_t *b, bcf_p1aux_t *ma, double threshold, int var_only)
-{
-    int nals = 1;
-    char *p;
-    for (p=b->alt; *p; p++)
-    {
-        if ( *p=='X' || p[0]=='.' ) break;
-        if ( p[0]==',' ) nals++;
-    }
-    if ( b->alt[0] && !*p ) nals++;
-
-    if ( nals>4 )
-    {
-        if ( *b->ref=='N' ) return 0;
-        fprintf(pysamerr,"Not ready for this, more than 4 alleles at %d: %s, %s\n", b->pos+1, b->ref,b->alt); 
-        exit(1);
-    }
-
-    // find PL, DV and DP FORMAT indexes
-    uint8_t *pl = NULL;
-    int i, npl = 0, idp = -1, idv = -1;
-    for (i = 0; i < b->n_gi; ++i) 
-    {
-        if (b->gi[i].fmt == bcf_str2int("PL", 2)) 
-        {
-            pl  = (uint8_t*)b->gi[i].data;
-            npl = b->gi[i].len;
-        }
-        else if (b->gi[i].fmt == bcf_str2int("DP", 2))  idp=i;
-        else if (b->gi[i].fmt == bcf_str2int("DV", 2))  idv=i;
-    }
-    if ( nals==1 ) 
-    {
-        if ( !var_only ) _bcf1_set_ref(b, idp);
-        return 1;
-    }
-    if ( !pl ) return -1;
-
-    assert(ma->q2p[0] == 1);
-
-    // Init P(D|G)
-    int npdg = nals*(nals+1)/2;
-    double *pdg,*_pdg;
-    _pdg = pdg = malloc(sizeof(double)*ma->n*npdg);
-    for (i=0; i<ma->n; i++)
-    {
-        int j; 
-        double sum = 0;
-        for (j=0; j<npdg; j++)
-        {
-            //_pdg[j] = pow(10,-0.1*pl[j]); 
-            _pdg[j] = ma->q2p[pl[j]];
-            sum += _pdg[j];
-        }
-        if ( sum )
-            for (j=0; j<npdg; j++) _pdg[j] /= sum;
-        _pdg += npdg;
-        pl += npl;
-    }
-
-    if ((p = strstr(b->info, "QS=")) == 0) { fprintf(pysamerr,"INFO/QS is required with -m, exiting\n"); exit(1); }
-    double qsum[4];
-    if ( sscanf(p+3,"%lf,%lf,%lf,%lf",&qsum[0],&qsum[1],&qsum[2],&qsum[3])!=4 ) { fprintf(pysamerr,"Could not parse %s\n",p); exit(1); }
-
-
-    // Calculate the most likely combination of alleles, remembering the most and second most likely set
-    int ia,ib,ic, max_als=0, max_als2=0;
-    double ref_lk = 0, max_lk = INT_MIN, max_lk2 = INT_MIN, lk_sum = INT_MIN, lk_sums[3];
-    for (ia=0; ia<nals; ia++)
-    {
-        double lk_tot = 0;
-        int iaa = (ia+1)*(ia+2)/2-1;
-        int isample;
-        for (isample=0; isample<ma->n; isample++)
-        {
-            double *p = pdg + isample*npdg;
-            // assert( log(p[iaa]) <= 0 );
-            lk_tot += log(p[iaa]);
-        }
-        if ( ia==0 ) ref_lk = lk_tot;
-        if ( max_lk<lk_tot ) { max_lk2 = max_lk; max_als2 = max_als; max_lk = lk_tot; max_als = 1<<ia; }
-        else if ( max_lk2<lk_tot ) { max_lk2 = lk_tot; max_als2 = 1<<ia; }
-        lk_sum = lk_tot>lk_sum ? lk_tot + log(1+exp(lk_sum-lk_tot)) : lk_sum + log(1+exp(lk_tot-lk_sum));
-    }
-    lk_sums[0] = lk_sum;
-    if ( nals>1 )
-    {
-        for (ia=0; ia<nals; ia++)
-        {
-            if ( qsum[ia]==0 ) continue;
-            int iaa = (ia+1)*(ia+2)/2-1;
-            for (ib=0; ib<ia; ib++)
-            {
-                if ( qsum[ib]==0 ) continue;
-                double lk_tot = 0;
-                double fa  = qsum[ia]/(qsum[ia]+qsum[ib]);
-                double fb  = qsum[ib]/(qsum[ia]+qsum[ib]);
-                double fab = 2*fa*fb; fa *= fa; fb *= fb;
-                int isample, ibb = (ib+1)*(ib+2)/2-1, iab = iaa - ia + ib;
-                for (isample=0; isample<ma->n; isample++)
-                {
-                    double *p = pdg + isample*npdg;
-                    //assert( log(fa*p[iaa] + fb*p[ibb] + fab*p[iab]) <= 0 );
-                    if ( b->ploidy && b->ploidy[isample]==1 )
-                        lk_tot +=  log(fa*p[iaa] + fb*p[ibb]);
-                    else 
-                        lk_tot +=  log(fa*p[iaa] + fb*p[ibb] + fab*p[iab]);
-                }
-                if ( max_lk<lk_tot ) { max_lk2 = max_lk; max_als2 = max_als; max_lk = lk_tot; max_als = 1<<ia|1<<ib; }
-                else if ( max_lk2<lk_tot ) { max_lk2 = lk_tot; max_als2 = 1<<ia|1<<ib; }
-                lk_sum = lk_tot>lk_sum ? lk_tot + log(1+exp(lk_sum-lk_tot)) : lk_sum + log(1+exp(lk_tot-lk_sum));
-            }
-        }
-        lk_sums[1] = lk_sum;
-    }
-    if ( nals>2 )
-    {
-        for (ia=0; ia<nals; ia++)
-        {
-            if ( qsum[ia]==0 ) continue;
-            int iaa = (ia+1)*(ia+2)/2-1;
-            for (ib=0; ib<ia; ib++)
-            {
-                if ( qsum[ib]==0 ) continue;
-                int ibb = (ib+1)*(ib+2)/2-1; 
-                int iab = iaa - ia + ib;
-                for (ic=0; ic<ib; ic++)
-                {
-                    if ( qsum[ic]==0 ) continue;
-                    double lk_tot = 0;
-                    double fa  = qsum[ia]/(qsum[ia]+qsum[ib]+qsum[ic]);
-                    double fb  = qsum[ib]/(qsum[ia]+qsum[ib]+qsum[ic]);
-                    double fc  = qsum[ic]/(qsum[ia]+qsum[ib]+qsum[ic]);
-                    double fab = 2*fa*fb, fac = 2*fa*fc, fbc = 2*fb*fc; fa *= fa; fb *= fb; fc *= fc;
-                    int isample, icc = (ic+1)*(ic+2)/2-1;
-                    int iac = iaa - ia + ic, ibc = ibb - ib + ic;
-                    for (isample=0; isample<ma->n; isample++)
-                    {
-                        double *p = pdg + isample*npdg;
-                        //assert( log(fa*p[iaa] + fb*p[ibb] + fc*p[icc] + fab*p[iab] + fac*p[iac] + fbc*p[ibc]) <= 0 );
-                        if ( b->ploidy && b->ploidy[isample]==1 ) 
-                            lk_tot += log(fa*p[iaa] + fb*p[ibb] + fc*p[icc]);
-                        else
-                            lk_tot += log(fa*p[iaa] + fb*p[ibb] + fc*p[icc] + fab*p[iab] + fac*p[iac] + fbc*p[ibc]);
-                    }
-                    if ( max_lk<lk_tot ) { max_lk2 = max_lk; max_als2 = max_als; max_lk = lk_tot; max_als = 1<<ia|1<<ib|1<<ic; }
-                    else if ( max_lk2<lk_tot ) { max_lk2 = lk_tot; max_als2 = 1<<ia|1<<ib|1<<ic; }
-                    lk_sum = lk_tot>lk_sum ? lk_tot + log(1+exp(lk_sum-lk_tot)) : lk_sum + log(1+exp(lk_tot-lk_sum));
-                }
-            }
-        }
-        lk_sums[2] = lk_sum;
-    }
-
-    // Should we add another allele, does it increase the likelihood significantly?
-    int n1=0, n2=0;
-    for (i=0; i<nals; i++) if ( max_als&1<<i) n1++;
-    for (i=0; i<nals; i++) if ( max_als2&1<<i) n2++;
-    if ( n2<n1 && kf_gammap(1,2.0*(max_lk-max_lk2))<threshold )
-    {
-        // the threshold not exceeded, use the second most likely set with fewer alleles
-        max_lk  = max_lk2;
-        max_als = max_als2;
-        n1 = n2;
-    }
-    lk_sum = lk_sums[n1-1];
-
-    // Get the BCF record ready for GT and GQ
-    kstring_t s;
-    int old_n_gi = b->n_gi;
-    s.m = b->m_str; s.l = b->l_str - 1; s.s = b->str;
-    kputs(":GT:GQ", &s); kputc('\0', &s);
-    b->m_str = s.m; b->l_str = s.l; b->str = s.s;
-    bcf_sync(b);
-
-    // Call GTs
-    int isample, gts=0, ac[4] = {0,0,0,0};
-    int nRR = 0, nAA = 0, nRA = 0, max_dv = 0;
-    for (isample = 0; isample < b->n_smpl; isample++) 
-    {
-        int ploidy = b->ploidy ? b->ploidy[isample] : 2;
-        double *p = pdg + isample*npdg;
-        int ia, als = 0;
-        double lk = 0, lk_s = 0;
-        for (ia=0; ia<nals; ia++)
-        {
-            if ( !(max_als&1<<ia) ) continue;
-            int iaa = (ia+1)*(ia+2)/2-1;
-            double _lk = p[iaa]*qsum[ia]*qsum[ia];
-            if ( _lk > lk ) { lk = _lk; als = ia<<3 | ia; }
-            lk_s += _lk;
-        }
-        if ( ploidy==2 ) 
-        {
-            for (ia=0; ia<nals; ia++)
-            {
-                if ( !(max_als&1<<ia) ) continue;
-                int iaa = (ia+1)*(ia+2)/2-1;
-                for (ib=0; ib<ia; ib++)
-                {
-                    if ( !(max_als&1<<ib) ) continue;
-                    int iab = iaa - ia + ib;
-                    double _lk = 2*qsum[ia]*qsum[ib]*p[iab];
-                    if ( _lk > lk ) { lk = _lk; als = ib<<3 | ia; }
-                    lk_s += _lk;
-                }
-            }
-        }
-        lk = -log(1-lk/lk_s)/0.2302585;
-        int dp = 0;
-        if ( idp>=0 && (dp=((uint16_t*)b->gi[idp].data)[isample])==0 )
-        {
-            // no coverage
-            ((uint8_t*)b->gi[old_n_gi].data)[isample]   = 1<<7;
-            ((uint8_t*)b->gi[old_n_gi+1].data)[isample] = 0;
-            continue;
-        }
-        if ( lk>99 ) lk = 99;
-        ((uint8_t*)b->gi[old_n_gi].data)[isample]   = als;
-        ((uint8_t*)b->gi[old_n_gi+1].data)[isample] = (int)lk;
-
-        // For MDV annotation
-        int dv;
-        if ( als && idv>=0 && (dv=((uint16_t*)b->gi[idv].data)[isample]) )
-        {
-            if ( max_dv < dv ) max_dv = dv;
-        }
-
-        // For HWE annotation; multiple ALT alleles treated as one
-        if ( !als ) nRR++;
-        else if ( !(als>>3&7) || !(als&7) ) nRA++;
-        else nAA++;
-
-        gts |= 1<<(als>>3&7) | 1<<(als&7);
-        ac[ als>>3&7 ]++;
-        ac[ als&7 ]++;
-    }
-    free(pdg);
-    bcf_fit_alt(b,max_als);
-
-    // The VCF spec is ambiguous about QUAL: is it the probability of anything else
-    //  (that is QUAL(non-ref) = P(ref)+P(any non-ref other than ALT)) or is it
-    //  QUAL(non-ref)=P(ref) and QUAL(ref)=1-P(ref)? Assuming the latter.
-    b->qual = gts>1 ? -4.343*(ref_lk - lk_sum) : -4.343*log(1-exp(ref_lk - lk_sum));
-    if ( b->qual>999 ) b->qual = 999;
-
-    // Prepare BCF for output: ref, alt, filter, info, format
-    memset(&s, 0, sizeof(kstring_t)); kputc('\0', &s); 
-    kputs(b->ref, &s); kputc('\0', &s);
-    kputs(b->alt, &s); kputc('\0', &s); kputc('\0', &s);
-    {
-        int an=0, nalts=0;
-        for (i=0; i<nals; i++)
-        {
-            an += ac[i];
-            if ( i>0 && ac[i] ) nalts++;
-        }
-        ksprintf(&s, "AN=%d;", an);
-        if ( nalts )
-        {
-            kputs("AC=", &s);
-            for (i=1; i<nals; i++)
-            {
-                if ( !(gts&1<<i) ) continue;
-                nalts--;
-                ksprintf(&s,"%d", ac[i]);
-                if ( nalts>0 ) kputc(',', &s);
-            }
-            kputc(';', &s);
-        }
-        kputs(b->info, &s); 
-        anno16_t a;
-        int has_I16 = test16(b, &a) >= 0? 1 : 0;
-        if (has_I16 )
-        {
-            if ( a.is_tested) ksprintf(&s, ";PV4=%.2g,%.2g,%.2g,%.2g", a.p[0], a.p[1], a.p[2], a.p[3]);
-            ksprintf(&s, ";DP4=%d,%d,%d,%d;MQ=%d", a.d[0], a.d[1], a.d[2], a.d[3], a.mq);
-            ksprintf(&s, ";QBD=%e", b->qual/(a.d[0] + a.d[1] + a.d[2] + a.d[3]));
-            if ( max_dv ) ksprintf(&s, ";MDV=%d", max_dv);
-        }
-        if ( nAA+nRA )
-        {
-            double hwe = calc_hwe(nAA, nRR, nRA);
-            ksprintf(&s, ";HWE=%e", hwe);
-        }
-        kputc('\0', &s);
-        rm_info(&s, "I16=");
-        rm_info(&s, "QS=");
-    }
-    kputs(b->fmt, &s); kputc('\0', &s);
-    free(b->str);
-    b->m_str = s.m; b->l_str = s.l; b->str = s.s;
-    bcf_sync(b);
-
-    return gts;
-}
-
-static int cal_pdg(const bcf1_t *b, bcf_p1aux_t *ma)
-{
-    int i, j;
-    long *p, tmp;
-    p = alloca(b->n_alleles * sizeof(long));
-    memset(p, 0, sizeof(long) * b->n_alleles);
-    for (j = 0; j < ma->n; ++j) {
-        const uint8_t *pi = ma->PL + j * ma->PL_len;
-        double *pdg = ma->pdg + j * 3;
-        pdg[0] = ma->q2p[pi[2]]; pdg[1] = ma->q2p[pi[1]]; pdg[2] = ma->q2p[pi[0]];
-        for (i = 0; i < b->n_alleles; ++i)
-            p[i] += (int)pi[(i+1)*(i+2)/2-1];
-    }
-    for (i = 0; i < b->n_alleles; ++i) p[i] = p[i]<<4 | i;
-    for (i = 1; i < b->n_alleles; ++i) // insertion sort
-        for (j = i; j > 0 && p[j] < p[j-1]; --j)
-            tmp = p[j], p[j] = p[j-1], p[j-1] = tmp;
-    for (i = b->n_alleles - 1; i >= 0; --i)
-        if ((p[i]&0xf) == 0) break;
-    return i;
-}
-
-
-int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k)
-{
-	double sum, g[3];
-	double max, f3[3], *pdg = ma->pdg + k * 3;
-	int q, i, max_i, ploidy;
-	ploidy = ma->ploidy? ma->ploidy[k] : 2;
-	if (ploidy == 2) {
-		f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
-	} else {
-		f3[0] = 1. - f0; f3[1] = 0; f3[2] = f0;
-	}
-	for (i = 0, sum = 0.; i < 3; ++i)
-		sum += (g[i] = pdg[i] * f3[i]);
-	for (i = 0, max = -1., max_i = 0; i < 3; ++i) {
-		g[i] /= sum;
-		if (g[i] > max) max = g[i], max_i = i;
-	}
-	max = 1. - max;
-	if (max < 1e-308) max = 1e-308;
-	q = (int)(-4.343 * log(max) + .499);
-	if (q > 99) q = 99;
-	return q<<2|max_i;
-}
-
-#define TINY 1e-20
-
-static void mc_cal_y_core(bcf_p1aux_t *ma, int beg)
-{
-	double *z[2], *tmp, *pdg;
-	int _j, last_min, last_max;
-	assert(beg == 0 || ma->M == ma->n*2);
-	z[0] = ma->z;
-	z[1] = ma->zswap;
-	pdg = ma->pdg;
-	memset(z[0], 0, sizeof(double) * (ma->M + 1));
-	memset(z[1], 0, sizeof(double) * (ma->M + 1));
-	z[0][0] = 1.;
-	last_min = last_max = 0;
-	ma->t = 0.;
-	if (ma->M == ma->n * 2) {
-		int M = 0;
-		for (_j = beg; _j < ma->n; ++_j) {
-			int k, j = _j - beg, _min = last_min, _max = last_max, M0;
-			double p[3], sum;
-			M0 = M; M += 2;
-			pdg = ma->pdg + _j * 3;
-			p[0] = pdg[0]; p[1] = 2. * pdg[1]; p[2] = pdg[2];
-			for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
-			for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
-			_max += 2;
-			if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
-			if (_min <= 1) k = 1, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1];
-			for (k = _min < 2? 2 : _min; k <= _max; ++k)
-				z[1][k] = (M0-k+1)*(M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1] + k*(k-1)* p[2] * z[0][k-2];
-			for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
-			ma->t += log(sum / (M * (M - 1.)));
-			for (k = _min; k <= _max; ++k) z[1][k] /= sum;
-			if (_min >= 1) z[1][_min-1] = 0.;
-			if (_min >= 2) z[1][_min-2] = 0.;
-			if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
-			if (_j == ma->n1 - 1) { // set pop1; ma->n1==-1 when unset
-				ma->t1 = ma->t;
-				memcpy(ma->z1, z[1], sizeof(double) * (ma->n1 * 2 + 1));
-			}
-			tmp = z[0]; z[0] = z[1]; z[1] = tmp;
-			last_min = _min; last_max = _max;
-		}
-		//for (_j = 0; _j < last_min; ++_j) z[0][_j] = 0.; // TODO: are these necessary?
-		//for (_j = last_max + 1; _j < ma->M; ++_j) z[0][_j] = 0.;
-	} else { // this block is very similar to the block above; these two might be merged in future
-		int j, M = 0;
-		for (j = 0; j < ma->n; ++j) {
-			int k, M0, _min = last_min, _max = last_max;
-			double p[3], sum;
-			pdg = ma->pdg + j * 3;
-			for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
-			for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
-			M0 = M;
-			M += ma->ploidy[j];
-			if (ma->ploidy[j] == 1) {
-				p[0] = pdg[0]; p[1] = pdg[2];
-				_max++;
-				if (_min == 0) k = 0, z[1][k] = (M0+1-k) * p[0] * z[0][k];
-				for (k = _min < 1? 1 : _min; k <= _max; ++k)
-					z[1][k] = (M0+1-k) * p[0] * z[0][k] + k * p[1] * z[0][k-1];
-				for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
-				ma->t += log(sum / M);
-				for (k = _min; k <= _max; ++k) z[1][k] /= sum;
-				if (_min >= 1) z[1][_min-1] = 0.;
-				if (j < ma->n - 1) z[1][_max+1] = 0.;
-			} else if (ma->ploidy[j] == 2) {
-				p[0] = pdg[0]; p[1] = 2 * pdg[1]; p[2] = pdg[2];
-				_max += 2;
-				if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
-				if (_min <= 1) k = 1, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1];
-				for (k = _min < 2? 2 : _min; k <= _max; ++k)
-					z[1][k] = (M0-k+1)*(M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1] + k*(k-1)* p[2] * z[0][k-2];
-				for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
-				ma->t += log(sum / (M * (M - 1.)));
-				for (k = _min; k <= _max; ++k) z[1][k] /= sum;
-				if (_min >= 1) z[1][_min-1] = 0.;
-				if (_min >= 2) z[1][_min-2] = 0.;
-				if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
-			}
-			tmp = z[0]; z[0] = z[1]; z[1] = tmp;
-			last_min = _min; last_max = _max;
-		}
-	}
-	if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1));
-	if (bcf_p1_fp_lk)
-		gzwrite(bcf_p1_fp_lk, ma->z, sizeof(double) * (ma->M + 1));
-}
-
-static void mc_cal_y(bcf_p1aux_t *ma)
-{
-	if (ma->n1 > 0 && ma->n1 < ma->n && ma->M == ma->n * 2) { // NB: ma->n1 is ineffective when there are haploid samples
-		int k;
-		long double x;
-		memset(ma->z1, 0, sizeof(double) * (2 * ma->n1 + 1));
-		memset(ma->z2, 0, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
-		ma->t1 = ma->t2 = 0.;
-		mc_cal_y_core(ma, ma->n1);
-		ma->t2 = ma->t;
-		memcpy(ma->z2, ma->z, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
-		mc_cal_y_core(ma, 0);
-		// rescale z
-		x = expl(ma->t - (ma->t1 + ma->t2));
-		for (k = 0; k <= ma->M; ++k) ma->z[k] *= x;
-	} else mc_cal_y_core(ma, 0);
-}
-
-#define CONTRAST_TINY 1e-30
-
-extern double kf_gammaq(double s, double z); // incomplete gamma function for chi^2 test
-
-static inline double chi2_test(int a, int b, int c, int d)
-{
-	double x, z;
-	x = (double)(a+b) * (c+d) * (b+d) * (a+c);
-	if (x == 0.) return 1;
-	z = a * d - b * c;
-	return kf_gammaq(.5, .5 * z * z * (a+b+c+d) / x);
-}
-
-// chi2=(a+b+c+d)(ad-bc)^2/[(a+b)(c+d)(a+c)(b+d)]
-static inline double contrast2_aux(const bcf_p1aux_t *p1, double sum, int k1, int k2, double x[3])
-{
-	double p = p1->phi[k1+k2] * p1->z1[k1] * p1->z2[k2] / sum * p1->hg[k1][k2];
-	int n1 = p1->n1, n2 = p1->n - p1->n1;
-	if (p < CONTRAST_TINY) return -1;
-	if (.5*k1/n1 < .5*k2/n2) x[1] += p;
-	else if (.5*k1/n1 > .5*k2/n2) x[2] += p;
-	else x[0] += p;
-	return p * chi2_test(k1, k2, (n1<<1) - k1, (n2<<1) - k2);
-}
-
-static double contrast2(bcf_p1aux_t *p1, double ret[3])
-{
-	int k, k1, k2, k10, k20, n1, n2;
-	double sum;
-	// get n1 and n2
-	n1 = p1->n1; n2 = p1->n - p1->n1;
-	if (n1 <= 0 || n2 <= 0) return 0.;
-	if (p1->hg == 0) { // initialize the hypergeometric distribution
-		/* NB: the hg matrix may take a lot of memory when there are many samples. There is a way
-		   to avoid precomputing this matrix, but it is slower and quite intricate. The following
-		   computation in this block can be accelerated with a similar strategy, but perhaps this
-		   is not a serious concern for now. */
-		double tmp = lgamma(2*(n1+n2)+1) - (lgamma(2*n1+1) + lgamma(2*n2+1));
-		p1->hg = calloc(2*n1+1, sizeof(void*));
-		for (k1 = 0; k1 <= 2*n1; ++k1) {
-			p1->hg[k1] = calloc(2*n2+1, sizeof(double));
-			for (k2 = 0; k2 <= 2*n2; ++k2)
-				p1->hg[k1][k2] = exp(lgamma(k1+k2+1) + lgamma(p1->M-k1-k2+1) - (lgamma(k1+1) + lgamma(k2+1) + lgamma(2*n1-k1+1) + lgamma(2*n2-k2+1) + tmp));
-		}
-	}
-	{ // compute
-		long double suml = 0;
-		for (k = 0; k <= p1->M; ++k) suml += p1->phi[k] * p1->z[k];
-		sum = suml;
-	}
-	{ // get the max k1 and k2
-		double max;
-		int max_k;
-		for (k = 0, max = 0, max_k = -1; k <= 2*n1; ++k) {
-			double x = p1->phi1[k] * p1->z1[k];
-			if (x > max) max = x, max_k = k;
-		}
-		k10 = max_k;
-		for (k = 0, max = 0, max_k = -1; k <= 2*n2; ++k) {
-			double x = p1->phi2[k] * p1->z2[k];
-			if (x > max) max = x, max_k = k;
-		}
-		k20 = max_k;
-	}
-	{ // We can do the following with one nested loop, but that is an O(N^2) thing. The following code block is much faster for large N.
-		double x[3], y;
-		long double z = 0., L[2];
-		x[0] = x[1] = x[2] = 0; L[0] = L[1] = 0;
-		for (k1 = k10; k1 >= 0; --k1) {
-			for (k2 = k20; k2 >= 0; --k2) {
-				if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
-				else z += y;
-			}
-			for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
-				if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
-				else z += y;
-			}
-		}
-		ret[0] = x[0]; ret[1] = x[1]; ret[2] = x[2];
-		x[0] = x[1] = x[2] = 0;
-		for (k1 = k10 + 1; k1 <= 2*n1; ++k1) {
-			for (k2 = k20; k2 >= 0; --k2) {
-				if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
-				else z += y;
-			}
-			for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
-				if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
-				else z += y;
-			}
-		}
-		ret[0] += x[0]; ret[1] += x[1]; ret[2] += x[2];
-		if (ret[0] + ret[1] + ret[2] < 0.95) { // in case of bad things happened
-			ret[0] = ret[1] = ret[2] = 0; L[0] = L[1] = 0;
-			for (k1 = 0, z = 0.; k1 <= 2*n1; ++k1)
-				for (k2 = 0; k2 <= 2*n2; ++k2)
-					if ((y = contrast2_aux(p1, sum, k1, k2, ret)) >= 0) z += y;
-			if (ret[0] + ret[1] + ret[2] < 0.95) // It seems that this may be caused by floating point errors. I do not really understand why...
-				z = 1.0, ret[0] = ret[1] = ret[2] = 1./3;
-		}
-		return (double)z;
-	}
-}
-
-static double mc_cal_afs(bcf_p1aux_t *ma, double *p_ref_folded, double *p_var_folded)
-{
-	int k;
-	long double sum = 0., sum2;
-	double *phi = ma->is_indel? ma->phi_indel : ma->phi;
-	memset(ma->afs1, 0, sizeof(double) * (ma->M + 1));
-	mc_cal_y(ma);
-	// compute AFS
-	for (k = 0, sum = 0.; k <= ma->M; ++k)
-		sum += (long double)phi[k] * ma->z[k];
-	for (k = 0; k <= ma->M; ++k) {
-		ma->afs1[k] = phi[k] * ma->z[k] / sum;
-		if (isnan(ma->afs1[k]) || isinf(ma->afs1[k])) return -1.;
-	}
-	// compute folded variant probability
-	for (k = 0, sum = 0.; k <= ma->M; ++k)
-		sum += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
-	for (k = 1, sum2 = 0.; k < ma->M; ++k)
-		sum2 += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
-	*p_var_folded = sum2 / sum;
-	*p_ref_folded = (phi[k] + phi[ma->M - k]) / 2. * (ma->z[ma->M] + ma->z[0]) / sum;
-	// the expected frequency
-	for (k = 0, sum = 0.; k <= ma->M; ++k) {
-		ma->afs[k] += ma->afs1[k];
-		sum += k * ma->afs1[k];
-	}
-	return sum / ma->M;
-}
-
-int bcf_p1_cal(const bcf1_t *b, int do_contrast, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
-{
-	int i, k;
-	long double sum = 0.;
-	ma->is_indel = bcf_is_indel(b);
-	rst->perm_rank = -1;
-	// set PL and PL_len
-	for (i = 0; i < b->n_gi; ++i) {
-		if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
-			ma->PL = (uint8_t*)b->gi[i].data;
-			ma->PL_len = b->gi[i].len;
-			break;
-		}
-	}
-	if (i == b->n_gi) return -1; // no PL
-	if (b->n_alleles < 2) return -1; // FIXME: find a better solution
-	// 
-	rst->rank0 = cal_pdg(b, ma);
-	rst->f_exp = mc_cal_afs(ma, &rst->p_ref_folded, &rst->p_var_folded);
-	rst->p_ref = ma->afs1[ma->M];
-	for (k = 0, sum = 0.; k < ma->M; ++k)
-		sum += ma->afs1[k];
-	rst->p_var = (double)sum;
-	{ // compute the allele count
-		double max = -1;
-		rst->ac = -1;
-		for (k = 0; k <= ma->M; ++k)
-			if (max < ma->z[k]) max = ma->z[k], rst->ac = k;
-		rst->ac = ma->M - rst->ac;
-	}
-	// calculate f_flat and f_em
-	for (k = 0, sum = 0.; k <= ma->M; ++k)
-		sum += (long double)ma->z[k];
-	rst->f_flat = 0.;
-	for (k = 0; k <= ma->M; ++k) {
-		double p = ma->z[k] / sum;
-		rst->f_flat += k * p;
-	}
-	rst->f_flat /= ma->M;
-	{ // estimate equal-tail credible interval (95% level)
-		int l, h;
-		double p;
-		for (i = 0, p = 0.; i <= ma->M; ++i)
-			if (p + ma->afs1[i] > 0.025) break;
-			else p += ma->afs1[i];
-		l = i;
-		for (i = ma->M, p = 0.; i >= 0; --i)
-			if (p + ma->afs1[i] > 0.025) break;
-			else p += ma->afs1[i];
-		h = i;
-		rst->cil = (double)(ma->M - h) / ma->M; rst->cih = (double)(ma->M - l) / ma->M;
-	}
-	if (ma->n1 > 0) { // compute LRT
-		double max0, max1, max2;
-		for (k = 0, max0 = -1; k <= ma->M; ++k)
-			if (max0 < ma->z[k]) max0 = ma->z[k];
-		for (k = 0, max1 = -1; k <= ma->n1 * 2; ++k)
-			if (max1 < ma->z1[k]) max1 = ma->z1[k];
-		for (k = 0, max2 = -1; k <= ma->M - ma->n1 * 2; ++k)
-			if (max2 < ma->z2[k]) max2 = ma->z2[k];
-		rst->lrt = log(max1 * max2 / max0);
-		rst->lrt = rst->lrt < 0? 1 : kf_gammaq(.5, rst->lrt);
-	} else rst->lrt = -1.0;
-	rst->cmp[0] = rst->cmp[1] = rst->cmp[2] = rst->p_chi2 = -1.0;
-	if (do_contrast && rst->p_var > 0.5) // skip contrast2() if the locus is a strong non-variant
-		rst->p_chi2 = contrast2(ma, rst->cmp);
-	return 0;
-}
-
-void bcf_p1_dump_afs(bcf_p1aux_t *ma)
-{
-	int k;
-	fprintf(pysamerr, "[afs]");
-	for (k = 0; k <= ma->M; ++k)
-		fprintf(pysamerr, " %d:%.3lf", k, ma->afs[ma->M - k]);
-	fprintf(pysamerr, "\n");
-	memset(ma->afs, 0, sizeof(double) * (ma->M + 1));
-}
diff --git a/samtools/bcftools/prob1.h b/samtools/bcftools/prob1.h
deleted file mode 100644
index 6f93155..0000000
--- a/samtools/bcftools/prob1.h
+++ /dev/null
@@ -1,49 +0,0 @@
-#ifndef BCF_PROB1_H
-#define BCF_PROB1_H
-
-#include "bcf.h"
-
-struct __bcf_p1aux_t;
-typedef struct __bcf_p1aux_t bcf_p1aux_t;
-
-typedef struct {
-	int rank0, perm_rank; // NB: perm_rank is always set to -1 by bcf_p1_cal()
-	int ac; // ML alternative allele count
-	double f_exp, f_flat, p_ref_folded, p_ref, p_var_folded, p_var;
-	double cil, cih;
-	double cmp[3], p_chi2, lrt; // used by contrast2()
-} bcf_p1rst_t;
-
-typedef struct {
-    double p[4];
-    int mq, depth, is_tested, d[4];
-} anno16_t;
-
-#define MC_PTYPE_FULL  1
-#define MC_PTYPE_COND2 2
-#define MC_PTYPE_FLAT  3
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-	bcf_p1aux_t *bcf_p1_init(int n, uint8_t *ploidy);
-	void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta);
-	void bcf_p1_init_subprior(bcf_p1aux_t *ma, int type, double theta);
-	void bcf_p1_destroy(bcf_p1aux_t *ma);
-    void bcf_p1_set_ploidy(bcf1_t *b, bcf_p1aux_t *ma);
-	int bcf_p1_cal(const bcf1_t *b, int do_contrast, bcf_p1aux_t *ma, bcf_p1rst_t *rst);
-    int call_multiallelic_gt(bcf1_t *b, bcf_p1aux_t *ma, double threshold, int var_only);
-	int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k);
-	void bcf_p1_dump_afs(bcf_p1aux_t *ma);
-	int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn);
-	int bcf_p1_set_n1(bcf_p1aux_t *b, int n1);
-	void bcf_p1_set_folded(bcf_p1aux_t *p1a); // only effective when set_n1() is not called
-
-	int bcf_em1(const bcf1_t *b, int n1, int flag, double x[10]);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/samtools/bcftools/vcf.c.pysam.c b/samtools/bcftools/vcf.c.pysam.c
deleted file mode 100644
index b67e915..0000000
--- a/samtools/bcftools/vcf.c.pysam.c
+++ /dev/null
@@ -1,251 +0,0 @@
-#include "pysam.h"
-
-#include <zlib.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include "bcf.h"
-#include "kstring.h"
-#include "kseq.h"
-KSTREAM_INIT(gzFile, gzread, 4096)
-
-typedef struct {
-	gzFile fp;
-	FILE *fpout;
-	kstream_t *ks;
-	void *refhash;
-	kstring_t line;
-	int max_ref;
-} vcf_t;
-
-bcf_hdr_t *vcf_hdr_read(bcf_t *bp)
-{
-	kstring_t meta, smpl;
-	int dret;
-	vcf_t *v;
-	bcf_hdr_t *h;
-	if (!bp->is_vcf) return bcf_hdr_read(bp);
-	h = calloc(1, sizeof(bcf_hdr_t));
-	v = (vcf_t*)bp->v;
-	v->line.l = 0;
-	memset(&meta, 0, sizeof(kstring_t));
-	memset(&smpl, 0, sizeof(kstring_t));
-	while (ks_getuntil(v->ks, '\n', &v->line, &dret) >= 0) {
-		if (v->line.l < 2) continue;
-		if (v->line.s[0] != '#') {
-            free(meta.s);
-            free(smpl.s);
-            free(h);
-            return 0; // no sample line
-        }
-		if (v->line.s[0] == '#' && v->line.s[1] == '#') {
-			kputsn(v->line.s, v->line.l, &meta); kputc('\n', &meta);
-		} else if (v->line.s[0] == '#') {
-			int k;
-			ks_tokaux_t aux;
-			char *p;
-			for (p = kstrtok(v->line.s, "\t\n", &aux), k = 0; p; p = kstrtok(0, 0, &aux), ++k) {
-				if (k >= 9) {
-					kputsn(p, aux.p - p, &smpl);
-					kputc('\0', &smpl);
-				}
-			}
-			break;
-		}
-	}
-	kputc('\0', &meta);
-	h->name = 0;
-	h->sname = smpl.s; h->l_smpl = smpl.l;
-	h->txt = meta.s; h->l_txt = meta.l;
-	bcf_hdr_sync(h);
-	return h;
-}
-
-bcf_t *vcf_open(const char *fn, const char *mode)
-{
-	bcf_t *bp;
-	vcf_t *v;
-	if (strchr(mode, 'b')) return bcf_open(fn, mode);
-	bp = calloc(1, sizeof(bcf_t));
-	v = calloc(1, sizeof(vcf_t));
-	bp->is_vcf = 1;
-	bp->v = v;
-	v->refhash = bcf_str2id_init();
-	if (strchr(mode, 'r')) {
-		v->fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
-		v->ks = ks_init(v->fp);
-	} else if (strchr(mode, 'w'))
-		v->fpout = strcmp(fn, "-")? fopen(fn, "w") : stdout;
-	return bp;
-}
-
-int vcf_dictread(bcf_t *bp, bcf_hdr_t *h, const char *fn)
-{
-	vcf_t *v;
-	gzFile fp;
-	kstream_t *ks;
-	kstring_t s, rn;
-	int dret;
-	if (bp == 0) return -1;
-	if (!bp->is_vcf) return 0;
-	s.l = s.m = 0; s.s = 0;
-	rn.m = rn.l = h->l_nm; rn.s = h->name;
-	v = (vcf_t*)bp->v;
-	fp = gzopen(fn, "r");
-	ks = ks_init(fp);
-	while (ks_getuntil(ks, 0, &s, &dret) >= 0) {
-		bcf_str2id_add(v->refhash, strdup(s.s));
-		kputs(s.s, &rn); kputc('\0', &rn);
-		if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret);
-	}
-	ks_destroy(ks);
-	gzclose(fp);
-	h->l_nm = rn.l; h->name = rn.s;
-	bcf_hdr_sync(h);
-	free(s.s);
-	return 0;
-}
-
-int vcf_close(bcf_t *bp)
-{
-	vcf_t *v;
-	if (bp == 0) return -1;
-	if (!bp->is_vcf) return bcf_close(bp);
-	v = (vcf_t*)bp->v;
-	if (v->fp) {
-		ks_destroy(v->ks);
-		gzclose(v->fp);
-	}
-	if (v->fpout) fclose(v->fpout);
-	free(v->line.s);
-	bcf_str2id_thorough_destroy(v->refhash);
-	free(v);
-	free(bp);
-	return 0;
-}
-
-int vcf_hdr_write(bcf_t *bp, const bcf_hdr_t *h)
-{
-	vcf_t *v = (vcf_t*)bp->v;
-	int i, has_ver = 0;
-	if (!bp->is_vcf) return bcf_hdr_write(bp, h);
-	if (h->l_txt > 0) {
-		if (strstr(h->txt, "##fileformat=")) has_ver = 1;
-		if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.1\n");
-		fwrite(h->txt, 1, h->l_txt - 1, v->fpout);
-	}
-	if (h->l_txt == 0) fprintf(v->fpout, "##fileformat=VCFv4.1\n");
-	fprintf(v->fpout, "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT");
-	for (i = 0; i < h->n_smpl; ++i)
-		fprintf(v->fpout, "\t%s", h->sns[i]);
-	fputc('\n', v->fpout);
-	return 0;
-}
-
-int vcf_write(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
-{
-	vcf_t *v = (vcf_t*)bp->v;
-	extern void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s);
-	if (!bp->is_vcf) return bcf_write(bp, h, b);
-	bcf_fmt_core(h, b, &v->line);
-	fwrite(v->line.s, 1, v->line.l, v->fpout);
-	fputc('\n', v->fpout);
-	return v->line.l + 1;
-}
-
-int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
-{
-	int dret, k, i, sync = 0;
-	vcf_t *v = (vcf_t*)bp->v;
-	char *p, *q;
-	kstring_t str, rn;
-	ks_tokaux_t aux, a2;
-	if (!bp->is_vcf) return bcf_read(bp, h, b);
-	v->line.l = 0;
-	str.l = 0; str.m = b->m_str; str.s = b->str;
-	rn.l = rn.m = h->l_nm; rn.s = h->name;
-	if (ks_getuntil(v->ks, '\n', &v->line, &dret) < 0) return -1;
-	b->n_smpl = h->n_smpl;
-	for (p = kstrtok(v->line.s, "\t", &aux), k = 0; p; p = kstrtok(0, 0, &aux), ++k) {
-		*(char*)aux.p = 0;
-		if (k == 0) { // ref
-			int tid = bcf_str2id(v->refhash, p);
-			if (tid < 0) {
-				tid = bcf_str2id_add(v->refhash, strdup(p));
-				kputs(p, &rn); kputc('\0', &rn);
-				sync = 1;
-			}
-			b->tid = tid;
-		} else if (k == 1) { // pos
-			b->pos = atoi(p) - 1;
-		} else if (k == 5) { // qual
-			b->qual = (p[0] >= '0' && p[0] <= '9')? atof(p) : 0;
-		} else if (k <= 8) { // variable length strings
-			kputs(p, &str); kputc('\0', &str);
-			b->l_str = str.l; b->m_str = str.m; b->str = str.s;
-			if (k == 8) bcf_sync(b);
-		} else { // k > 9
-			if (strncmp(p, "./.", 3) == 0) {
-				for (i = 0; i < b->n_gi; ++i) {
-					if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
-						((uint8_t*)b->gi[i].data)[k-9] = 1<<7;
-					} else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
-						((uint8_t*)b->gi[i].data)[k-9] = 0;
-					} else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
-						((int32_t*)b->gi[i].data)[k-9] = 0;
-					} else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
-						((uint16_t*)b->gi[i].data)[k-9] = 0;
-					} else if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
-						int y = b->n_alleles * (b->n_alleles + 1) / 2;
-						memset((uint8_t*)b->gi[i].data + (k - 9) * y, 0, y);
-					} else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
-						int y = b->n_alleles * (b->n_alleles + 1) / 2;
-						memset((float*)b->gi[i].data + (k - 9) * y, 0, y * 4);
-					}
-				}
-				goto endblock;
-			}
-			for (q = kstrtok(p, ":", &a2), i = 0; q && i < b->n_gi; q = kstrtok(0, 0, &a2), ++i) {
-				if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
-					((uint8_t*)b->gi[i].data)[k-9] = (q[0] - '0')<<3 | (q[2] - '0') | (q[1] == '/'? 0 : 1) << 6;
-				} else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
-					double _x = strtod(q, &q);
-					int x = (int)(_x + .499);
-					if (x > 255) x = 255;
-					((uint8_t*)b->gi[i].data)[k-9] = x;
-				} else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
-					int x = strtol(q, &q, 10);
-					if (x > 0xffff) x = 0xffff;
-					((uint32_t*)b->gi[i].data)[k-9] = x;
-				} else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
-					int x = strtol(q, &q, 10);
-					if (x > 0xffff) x = 0xffff;
-					((uint16_t*)b->gi[i].data)[k-9] = x;
-				} else if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
-					int x, y, j;
-					uint8_t *data = (uint8_t*)b->gi[i].data;
-					y = b->n_alleles * (b->n_alleles + 1) / 2;
-					for (j = 0; j < y; ++j) {
-						x = strtol(q, &q, 10);
-						if (x > 255) x = 255;
-						data[(k-9) * y + j] = x;
-						++q;
-					}
-				} else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
-					int j, y;
-					float x, *data = (float*)b->gi[i].data;
-					y = b->n_alleles * (b->n_alleles + 1) / 2;
-					for (j = 0; j < y; ++j) {
-						x = strtod(q, &q);
-						data[(k-9) * y + j] = x > 0? -x/10. : x;
-						++q;
-					}
-				}
-			}
-		endblock: i = i;
-		}
-	}
-	h->l_nm = rn.l; h->name = rn.s;
-	if (sync) bcf_hdr_sync(h);
-	return v->line.l + 1;
-}
diff --git a/samtools/bedcov.c.pysam.c b/samtools/bedcov.c.pysam.c
index ea495a0..6d20b17 100644
--- a/samtools/bedcov.c.pysam.c
+++ b/samtools/bedcov.c.pysam.c
@@ -1,129 +1,158 @@
 #include "pysam.h"
 
+/*  bedcov.c -- bedcov subcommand.
+
+    Copyright (C) 2012 Broad Institute.
+    Copyright (C) 2013-2014 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <zlib.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "kstring.h"
-#include "bgzf.h"
-#include "bam.h"
+#include "htslib/kstring.h"
+#include "htslib/sam.h"
 
-#include "kseq.h"
+#include "htslib/kseq.h"
 KSTREAM_INIT(gzFile, gzread, 16384)
 
 typedef struct {
-	bamFile fp;
-	bam_iter_t iter;
-	int min_mapQ;
+    htsFile *fp;
+    bam_hdr_t *header;
+    hts_itr_t *iter;
+    int min_mapQ;
 } aux_t;
 
 static int read_bam(void *data, bam1_t *b)
 {
-	aux_t *aux = (aux_t*)data;
-	int ret = bam_iter_read(aux->fp, aux->iter, b);
-	if ((int)b->core.qual < aux->min_mapQ) b->core.flag |= BAM_FUNMAP;
-	return ret;
+    aux_t *aux = (aux_t*)data; // data in fact is a pointer to an auxiliary structure
+    int ret;
+    while (1)
+    {
+        ret = aux->iter? sam_itr_next(aux->fp, aux->iter, b) : sam_read1(aux->fp, aux->header, b);
+        if ( ret<0 ) break;
+        if ( b->core.flag & (BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP) ) continue;
+        if ( (int)b->core.qual < aux->min_mapQ ) continue;
+        break;
+    }
+    return ret;
 }
 
 int main_bedcov(int argc, char *argv[])
 {
-	extern void bam_init_header_hash(bam_header_t*);
-	gzFile fp;
-	kstring_t str;
-	kstream_t *ks;
-	bam_index_t **idx;
-	bam_header_t *h = 0;
-	aux_t **aux;
-	int *n_plp, dret, i, n, c, min_mapQ = 0;
-	int64_t *cnt;
-	const bam_pileup1_t **plp;
-
-	while ((c = getopt(argc, argv, "Q:")) >= 0) {
-		switch (c) {
-		case 'Q': min_mapQ = atoi(optarg); break;
-		}
-	}
-	if (optind + 2 > argc) {
-		fprintf(pysamerr, "Usage: samtools bedcov <in.bed> <in1.bam> [...]\n");
-		return 1;
-	}
-	memset(&str, 0, sizeof(kstring_t));
-	n = argc - optind - 1;
-	aux = calloc(n, sizeof(void*));
-	idx = calloc(n, sizeof(void*));
-	for (i = 0; i < n; ++i) {
-		aux[i] = calloc(1, sizeof(aux_t));
-		aux[i]->min_mapQ = min_mapQ;
-		aux[i]->fp = bam_open(argv[i+optind+1], "r");
-		idx[i] = bam_index_load(argv[i+optind+1]);
-		if (aux[i]->fp == 0 || idx[i] == 0) {
-			fprintf(pysamerr, "ERROR: fail to open index BAM file '%s'\n", argv[i+optind+1]);
-			return 2;
-		}
-		bgzf_set_cache_size(aux[i]->fp, 20);
-		if (i == 0) h = bam_header_read(aux[0]->fp);
-	}
-	bam_init_header_hash(h);
-	cnt = calloc(n, 8);
-
-	fp = gzopen(argv[optind], "rb");
-	ks = ks_init(fp);
-	n_plp = calloc(n, sizeof(int));
-	plp = calloc(n, sizeof(void*));
-	while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) {
-		char *p, *q;
-		int tid, beg, end, pos;
-		bam_mplp_t mplp;
-
-		for (p = q = str.s; *p && *p != '\t'; ++p);
-		if (*p != '\t') goto bed_error;
-		*p = 0; tid = bam_get_tid(h, q); *p = '\t';
-		if (tid < 0) goto bed_error;
-		for (q = p = p + 1; isdigit(*p); ++p);
-		if (*p != '\t') goto bed_error;
-		*p = 0; beg = atoi(q); *p = '\t';
-		for (q = p = p + 1; isdigit(*p); ++p);
-		if (*p == '\t' || *p == 0) {
-			int c = *p;
-			*p = 0; end = atoi(q); *p = c;
-		} else goto bed_error;
-
-		for (i = 0; i < n; ++i) {
-			if (aux[i]->iter) bam_iter_destroy(aux[i]->iter);
-			aux[i]->iter = bam_iter_query(idx[i], tid, beg, end);
-		}
-		mplp = bam_mplp_init(n, read_bam, (void**)aux);
-		bam_mplp_set_maxcnt(mplp, 64000);
-		memset(cnt, 0, 8 * n);
-		while (bam_mplp_auto(mplp, &tid, &pos, n_plp, plp) > 0)
-			if (pos >= beg && pos < end)
-				for (i = 0; i < n; ++i) cnt[i] += n_plp[i];
-		for (i = 0; i < n; ++i) {
-			kputc('\t', &str);
-			kputl(cnt[i], &str);
-		}
-		puts(str.s);
-		bam_mplp_destroy(mplp);
-		continue;
+    gzFile fp;
+    kstring_t str;
+    kstream_t *ks;
+    hts_idx_t **idx;
+    aux_t **aux;
+    int *n_plp, dret, i, n, c, min_mapQ = 0;
+    int64_t *cnt;
+    const bam_pileup1_t **plp;
+
+    while ((c = getopt(argc, argv, "Q:")) >= 0) {
+        switch (c) {
+        case 'Q': min_mapQ = atoi(optarg); break;
+        }
+    }
+    if (optind + 2 > argc) {
+        fprintf(pysamerr, "Usage: samtools bedcov <in.bed> <in1.bam> [...]\n");
+        return 1;
+    }
+    memset(&str, 0, sizeof(kstring_t));
+    n = argc - optind - 1;
+    aux = calloc(n, sizeof(aux_t*));
+    idx = calloc(n, sizeof(hts_idx_t*));
+    for (i = 0; i < n; ++i) {
+        aux[i] = calloc(1, sizeof(aux_t));
+        aux[i]->min_mapQ = min_mapQ;
+        aux[i]->fp = sam_open(argv[i+optind+1], "r");
+        idx[i] = sam_index_load(aux[i]->fp, argv[i+optind+1]);
+        if (aux[i]->fp == 0 || idx[i] == 0) {
+            fprintf(pysamerr, "ERROR: fail to open index BAM file '%s'\n", argv[i+optind+1]);
+            return 2;
+        }
+        // TODO bgzf_set_cache_size(aux[i]->fp, 20);
+        aux[i]->header = sam_hdr_read(aux[i]->fp);
+    }
+    cnt = calloc(n, 8);
+
+    fp = gzopen(argv[optind], "rb");
+    ks = ks_init(fp);
+    n_plp = calloc(n, sizeof(int));
+    plp = calloc(n, sizeof(bam_pileup1_t*));
+    while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) {
+        char *p, *q;
+        int tid, beg, end, pos;
+        bam_mplp_t mplp;
+
+        for (p = q = str.s; *p && *p != '\t'; ++p);
+        if (*p != '\t') goto bed_error;
+        *p = 0; tid = bam_name2id(aux[0]->header, q); *p = '\t';
+        if (tid < 0) goto bed_error;
+        for (q = p = p + 1; isdigit(*p); ++p);
+        if (*p != '\t') goto bed_error;
+        *p = 0; beg = atoi(q); *p = '\t';
+        for (q = p = p + 1; isdigit(*p); ++p);
+        if (*p == '\t' || *p == 0) {
+            int c = *p;
+            *p = 0; end = atoi(q); *p = c;
+        } else goto bed_error;
+
+        for (i = 0; i < n; ++i) {
+            if (aux[i]->iter) hts_itr_destroy(aux[i]->iter);
+            aux[i]->iter = sam_itr_queryi(idx[i], tid, beg, end);
+        }
+        mplp = bam_mplp_init(n, read_bam, (void**)aux);
+        bam_mplp_set_maxcnt(mplp, 64000);
+        memset(cnt, 0, 8 * n);
+        while (bam_mplp_auto(mplp, &tid, &pos, n_plp, plp) > 0)
+            if (pos >= beg && pos < end)
+                for (i = 0; i < n; ++i) cnt[i] += n_plp[i];
+        for (i = 0; i < n; ++i) {
+            kputc('\t', &str);
+            kputl(cnt[i], &str);
+        }
+        puts(str.s);
+        bam_mplp_destroy(mplp);
+        continue;
 
 bed_error:
-		fprintf(pysamerr, "Errors in BED line '%s'\n", str.s);
-	}
-	free(n_plp); free(plp);
-	ks_destroy(ks);
-	gzclose(fp);
-
-	free(cnt);
-	for (i = 0; i < n; ++i) {
-		if (aux[i]->iter) bam_iter_destroy(aux[i]->iter);
-		bam_index_destroy(idx[i]);
-		bam_close(aux[i]->fp);
-		free(aux[i]);
-	}
-	bam_header_destroy(h);
-	free(aux); free(idx);
-	free(str.s);
-	return 0;
+        fprintf(pysamerr, "Errors in BED line '%s'\n", str.s);
+    }
+    free(n_plp); free(plp);
+    ks_destroy(ks);
+    gzclose(fp);
+
+    free(cnt);
+    for (i = 0; i < n; ++i) {
+        if (aux[i]->iter) hts_itr_destroy(aux[i]->iter);
+        hts_idx_destroy(idx[i]);
+        bam_hdr_destroy(aux[i]->header);
+        sam_close(aux[i]->fp);
+        free(aux[i]);
+    }
+    free(aux); free(idx);
+    free(str.s);
+    return 0;
 }
diff --git a/samtools/bedidx.c.pysam.c b/samtools/bedidx.c.pysam.c
index 2bbb05f..716aee5 100644
--- a/samtools/bedidx.c.pysam.c
+++ b/samtools/bedidx.c.pysam.c
@@ -1,164 +1,260 @@
 #include "pysam.h"
 
+/*  bedidx.c -- BED file indexing.
+
+    Copyright (C) 2011 Broad Institute.
+    Copyright (C) 2014 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
 #include <stdio.h>
+#include <errno.h>
 #include <zlib.h>
 
 #ifdef _WIN32
 #define drand48() ((double)rand() / RAND_MAX)
 #endif
 
-#include "ksort.h"
+#include "htslib/ksort.h"
 KSORT_INIT_GENERIC(uint64_t)
 
-#include "kseq.h"
+#include "htslib/kseq.h"
 KSTREAM_INIT(gzFile, gzread, 8192)
 
 typedef struct {
-	int n, m;
-	uint64_t *a;
-	int *idx;
+    int n, m;
+    uint64_t *a;
+    int *idx;
 } bed_reglist_t;
 
-#include "khash.h"
+#include "htslib/khash.h"
 KHASH_MAP_INIT_STR(reg, bed_reglist_t)
 
 #define LIDX_SHIFT 13
 
 typedef kh_reg_t reghash_t;
 
+void bed_destroy(void *_h);
+
+
 int *bed_index_core(int n, uint64_t *a, int *n_idx)
 {
-	int i, j, m, *idx;
-	m = *n_idx = 0; idx = 0;
-	for (i = 0; i < n; ++i) {
-		int beg, end;
-		beg = a[i]>>32 >> LIDX_SHIFT; end = ((uint32_t)a[i]) >> LIDX_SHIFT;
-		if (m < end + 1) {
-			int oldm = m;
-			m = end + 1;
-			kroundup32(m);
-			idx = realloc(idx, m * sizeof(int));
-			for (j = oldm; j < m; ++j) idx[j] = -1;
-		}
-		if (beg == end) {
-			if (idx[beg] < 0) idx[beg] = i;
-		} else {
-			for (j = beg; j <= end; ++j)
-				if (idx[j] < 0) idx[j] = i;
-		}
-		*n_idx = end + 1;
-	}
-	return idx;
+    int i, j, m, *idx;
+    m = *n_idx = 0; idx = 0;
+    for (i = 0; i < n; ++i) {
+        int beg, end;
+        beg = a[i]>>32 >> LIDX_SHIFT; end = ((uint32_t)a[i]) >> LIDX_SHIFT;
+        if (m < end + 1) {
+            int oldm = m;
+            m = end + 1;
+            kroundup32(m);
+            idx = realloc(idx, m * sizeof(int));
+            for (j = oldm; j < m; ++j) idx[j] = -1;
+        }
+        if (beg == end) {
+            if (idx[beg] < 0) idx[beg] = i;
+        } else {
+            for (j = beg; j <= end; ++j)
+                if (idx[j] < 0) idx[j] = i;
+        }
+        *n_idx = end + 1;
+    }
+    return idx;
 }
 
 void bed_index(void *_h)
 {
-	reghash_t *h = (reghash_t*)_h;
-	khint_t k;
-	for (k = 0; k < kh_end(h); ++k) {
-		if (kh_exist(h, k)) {
-			bed_reglist_t *p = &kh_val(h, k);
-			if (p->idx) free(p->idx);
-			ks_introsort(uint64_t, p->n, p->a);
-			p->idx = bed_index_core(p->n, p->a, &p->m);
-		}
-	}
+    reghash_t *h = (reghash_t*)_h;
+    khint_t k;
+    for (k = 0; k < kh_end(h); ++k) {
+        if (kh_exist(h, k)) {
+            bed_reglist_t *p = &kh_val(h, k);
+            if (p->idx) free(p->idx);
+            ks_introsort(uint64_t, p->n, p->a);
+            p->idx = bed_index_core(p->n, p->a, &p->m);
+        }
+    }
 }
 
 int bed_overlap_core(const bed_reglist_t *p, int beg, int end)
 {
-	int i, min_off;
-	if (p->n == 0) return 0;
-	min_off = (beg>>LIDX_SHIFT >= p->n)? p->idx[p->n-1] : p->idx[beg>>LIDX_SHIFT];
-	if (min_off < 0) { // TODO: this block can be improved, but speed should not matter too much here
-		int n = beg>>LIDX_SHIFT;
-		if (n > p->n) n = p->n;
-		for (i = n - 1; i >= 0; --i)
-			if (p->idx[i] >= 0) break;
-		min_off = i >= 0? p->idx[i] : 0;
-	}
-	for (i = min_off; i < p->n; ++i) {
-		if ((int)(p->a[i]>>32) >= end) break; // out of range; no need to proceed
-		if ((int32_t)p->a[i] > beg && (int32_t)(p->a[i]>>32) < end)
-			return 1; // find the overlap; return
-	}
-	return 0;
+    int i, min_off;
+    if (p->n == 0) return 0;
+    min_off = (beg>>LIDX_SHIFT >= p->n)? p->idx[p->n-1] : p->idx[beg>>LIDX_SHIFT];
+    if (min_off < 0) { // TODO: this block can be improved, but speed should not matter too much here
+        int n = beg>>LIDX_SHIFT;
+        if (n > p->n) n = p->n;
+        for (i = n - 1; i >= 0; --i)
+            if (p->idx[i] >= 0) break;
+        min_off = i >= 0? p->idx[i] : 0;
+    }
+    for (i = min_off; i < p->n; ++i) {
+        if ((int)(p->a[i]>>32) >= end) break; // out of range; no need to proceed
+        if ((int32_t)p->a[i] > beg && (int32_t)(p->a[i]>>32) < end)
+            return 1; // find the overlap; return
+    }
+    return 0;
 }
 
 int bed_overlap(const void *_h, const char *chr, int beg, int end)
 {
-	const reghash_t *h = (const reghash_t*)_h;
-	khint_t k;
-	if (!h) return 0;
-	k = kh_get(reg, h, chr);
-	if (k == kh_end(h)) return 0;
-	return bed_overlap_core(&kh_val(h, k), beg, end);
+    const reghash_t *h = (const reghash_t*)_h;
+    khint_t k;
+    if (!h) return 0;
+    k = kh_get(reg, h, chr);
+    if (k == kh_end(h)) return 0;
+    return bed_overlap_core(&kh_val(h, k), beg, end);
 }
 
+/* "BED" file reader, which actually reads two different formats.
+
+   BED files contain between three and nine fields per line, of which
+   only the first three (reference, start, end) are of interest to us.
+   BED counts positions from base 0, and the end is the base after the
+   region of interest.  While not properly documented in the specification,
+   it is also possible to have 'browser' and 'track' lines in BED files that
+   do not follow the standard format and should be ignored.  Examination
+   of the BED file reading code in
+   http://genome-source.cse.ucsc.edu/gitweb/?p=kent.git shows that BED
+   files can also have comment lines starting with '#', leading whitespace
+   is stripped, and that fields are separated by one or more consecutive
+   whitespace characters.
+
+   The alternative format was originally for reading positions in VCF
+   format.  This expects two columns, which indicate the reference and
+   a position.  The position corresponds to a single base, and unlike
+   BED counts from 1.
+
+   Which format is in use is determined based on whether one or two
+   numbers can be decoded on the line.  As this choice is made line-by-line
+   in this implementation, it is possible (but probably a bad idea) to mix
+   both formats in the same file.  If trying to read a VCF file by this
+   method, it would be important to ensure that the third column (ID) does
+   not contain any entries that start with a digit, to avoid the line
+   erroneously being parsed as a BED file entry.
+
+   The BED specification is at http://www.genome.ucsc.edu/FAQ/FAQformat.html
+   The VCF specification is at https://github.com/samtools/hts-specs
+ */
+
 void *bed_read(const char *fn)
 {
-	reghash_t *h = kh_init(reg);
-	gzFile fp;
-	kstream_t *ks;
-	int dret;
-	kstring_t *str;
-	// read the list
-	fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
-	if (fp == 0) return 0;
-	str = calloc(1, sizeof(kstring_t));
-	ks = ks_init(fp);
-	while (ks_getuntil(ks, 0, str, &dret) >= 0) { // read the chr name
-		int beg = -1, end = -1;
-		bed_reglist_t *p;
-		khint_t k = kh_get(reg, h, str->s);
-		if (k == kh_end(h)) { // absent from the hash table
-			int ret;
-			char *s = strdup(str->s);
-			k = kh_put(reg, h, s, &ret);
-			memset(&kh_val(h, k), 0, sizeof(bed_reglist_t));
-		}
-		p = &kh_val(h, k);
-		if (dret != '\n') { // if the lines has other characters
-			if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0])) {
-				beg = atoi(str->s); // begin
-				if (dret != '\n') {
-					if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0])) {
-						end = atoi(str->s); // end
-						if (end < beg) end = -1;
-					}
-				}
-			}
-		}
-		if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n'); // skip the rest of the line
-		if (end < 0 && beg > 0) end = beg, beg = beg - 1; // if there is only one column
-		if (beg >= 0 && end > beg) {
-			if (p->n == p->m) {
-				p->m = p->m? p->m<<1 : 4;
-				p->a = realloc(p->a, p->m * 8);
-			}
-			p->a[p->n++] = (uint64_t)beg<<32 | end;
-		}
-	}
-	ks_destroy(ks);
-	gzclose(fp);
-	free(str->s); free(str);
-	bed_index(h);
-	return h;
+    reghash_t *h = kh_init(reg);
+    gzFile fp;
+    kstream_t *ks = NULL;
+    int dret;
+    unsigned int line = 0;
+    kstring_t str = { 0, 0, NULL };
+
+    if (NULL == h) return NULL;
+    // read the list
+    fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
+    if (fp == 0) return 0;
+    ks = ks_init(fp);
+    if (NULL == ks) goto fail;  // In case ks_init ever gets error checking...
+    while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) > 0) { // read a line
+        char *ref = str.s, *ref_end;
+        unsigned int beg = 0, end = 0;
+        int num = 0;
+        khint_t k;
+        bed_reglist_t *p;
+
+        line++;
+        while (*ref && isspace(*ref)) ref++;
+        if ('\0' == *ref) continue;  // Skip blank lines
+        if ('#'  == *ref) continue;  // Skip BED file comments
+        ref_end = ref;   // look for the end of the reference name
+        while (*ref_end && !isspace(*ref_end)) ref_end++;
+        if ('\0' != *ref_end) {
+            *ref_end = '\0';  // terminate ref and look for start, end
+            num = sscanf(ref_end + 1, "%u %u", &beg, &end);
+        }
+        if (1 == num) {  // VCF-style format
+            end = beg--; // Counts from 1 instead of 0 for BED files
+        }
+        if (num < 1 || end < beg) {
+            // These two are special lines that can occur in BED files.
+            // Check for them here instead of earlier in case someone really
+            // has called their reference "browser" or "track".
+            if (0 == strcmp(ref, "browser")) continue;
+            if (0 == strcmp(ref, "track")) continue;
+            fprintf(pysamerr, "[bed_read] Parse error reading %s at line %u\n",
+                    fn, line);
+            goto fail_no_msg;
+        }
+
+        // Put reg in the hash table if not already there
+        k = kh_get(reg, h, ref);
+        if (k == kh_end(h)) { // absent from the hash table
+            int ret;
+            char *s = strdup(ref);
+            if (NULL == s) goto fail;
+            k = kh_put(reg, h, s, &ret);
+            if (-1 == ret) {
+                free(s);
+                goto fail;
+            }
+            memset(&kh_val(h, k), 0, sizeof(bed_reglist_t));
+        }
+        p = &kh_val(h, k);
+
+        // Add begin,end to the list
+        if (p->n == p->m) {
+            p->m = p->m? p->m<<1 : 4;
+            p->a = realloc(p->a, p->m * 8);
+            if (NULL == p->a) goto fail;
+        }
+        p->a[p->n++] = (uint64_t)beg<<32 | end;
+    }
+    // FIXME: Need to check for errors in ks_getuntil.  At the moment it
+    // doesn't look like it can return one.  Possibly use gzgets instead?
+
+    ks_destroy(ks);
+    gzclose(fp);
+    free(str.s);
+    bed_index(h);
+    return h;
+ fail:
+    fprintf(pysamerr, "[bed_read] Error reading %s : %s\n", fn, strerror(errno));
+ fail_no_msg:
+    if (ks) ks_destroy(ks);
+    if (fp) gzclose(fp);
+    free(str.s);
+    bed_destroy(h);
+    return NULL;
 }
 
 void bed_destroy(void *_h)
 {
-	reghash_t *h = (reghash_t*)_h;
-	khint_t k;
-	for (k = 0; k < kh_end(h); ++k) {
-		if (kh_exist(h, k)) {
-			free(kh_val(h, k).a);
-			free(kh_val(h, k).idx);
-			free((char*)kh_key(h, k));
-		}
-	}
-	kh_destroy(reg, h);
+    reghash_t *h = (reghash_t*)_h;
+    khint_t k;
+    for (k = 0; k < kh_end(h); ++k) {
+        if (kh_exist(h, k)) {
+            free(kh_val(h, k).a);
+            free(kh_val(h, k).idx);
+            free((char*)kh_key(h, k));
+        }
+    }
+    kh_destroy(reg, h);
 }
diff --git a/samtools/bgzf.c.pysam.c b/samtools/bgzf.c.pysam.c
deleted file mode 100644
index a7543c5..0000000
--- a/samtools/bgzf.c.pysam.c
+++ /dev/null
@@ -1,696 +0,0 @@
-#include "pysam.h"
-
-/* The MIT License
-
-   Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
-                 2011 Attractive Chaos <attractor at live.co.uk>
-
-   Permission is hereby granted, free of charge, to any person obtaining a copy
-   of this software and associated documentation files (the "Software"), to deal
-   in the Software without restriction, including without limitation the rights
-   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-   copies of the Software, and to permit persons to whom the Software is
-   furnished to do so, subject to the following conditions:
-
-   The above copyright notice and this permission notice shall be included in
-   all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-   THE SOFTWARE.
-*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <assert.h>
-#include <pthread.h>
-#include <sys/types.h>
-#include "bgzf.h"
-
-#ifdef _USE_KNETFILE
-#include "knetfile.h"
-typedef knetFile *_bgzf_file_t;
-#define _bgzf_open(fn, mode) knet_open(fn, mode)
-#define _bgzf_dopen(fp, mode) knet_dopen(fp, mode)
-#define _bgzf_close(fp) knet_close(fp)
-#define _bgzf_fileno(fp) ((fp)->fd)
-#define _bgzf_tell(fp) knet_tell(fp)
-#define _bgzf_seek(fp, offset, whence) knet_seek(fp, offset, whence)
-#define _bgzf_read(fp, buf, len) knet_read(fp, buf, len)
-#define _bgzf_write(fp, buf, len) knet_write(fp, buf, len)
-#else // ~defined(_USE_KNETFILE)
-#if defined(_WIN32) || defined(_MSC_VER)
-#define ftello(fp) ftell(fp)
-#define fseeko(fp, offset, whence) fseek(fp, offset, whence)
-#else // ~defined(_WIN32)
-extern off_t ftello(FILE *stream);
-extern int fseeko(FILE *stream, off_t offset, int whence);
-#endif // ~defined(_WIN32)
-typedef FILE *_bgzf_file_t;
-#define _bgzf_open(fn, mode) fopen(fn, mode)
-#define _bgzf_dopen(fp, mode) fdopen(fp, mode)
-#define _bgzf_close(fp) fclose(fp)
-#define _bgzf_fileno(fp) fileno(fp)
-#define _bgzf_tell(fp) ftello(fp)
-#define _bgzf_seek(fp, offset, whence) fseeko(fp, offset, whence)
-#define _bgzf_read(fp, buf, len) fread(buf, 1, len, fp)
-#define _bgzf_write(fp, buf, len) fwrite(buf, 1, len, fp)
-#endif // ~define(_USE_KNETFILE)
-
-#define BLOCK_HEADER_LENGTH 18
-#define BLOCK_FOOTER_LENGTH 8
-
-
-/* BGZF/GZIP header (speciallized from RFC 1952; little endian):
- +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
- | 31|139|  8|  4|              0|  0|255|      6| 66| 67|      2|BLK_LEN|
- +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
-*/
-static const uint8_t g_magic[19] = "\037\213\010\4\0\0\0\0\0\377\6\0\102\103\2\0\0\0";
-
-#ifdef BGZF_CACHE
-typedef struct {
-	int size;
-	uint8_t *block;
-	int64_t end_offset;
-} cache_t;
-#include "khash.h"
-KHASH_MAP_INIT_INT64(cache, cache_t)
-#endif
-
-static inline void packInt16(uint8_t *buffer, uint16_t value)
-{
-	buffer[0] = value;
-	buffer[1] = value >> 8;
-}
-
-static inline int unpackInt16(const uint8_t *buffer)
-{
-	return buffer[0] | buffer[1] << 8;
-}
-
-static inline void packInt32(uint8_t *buffer, uint32_t value)
-{
-	buffer[0] = value;
-	buffer[1] = value >> 8;
-	buffer[2] = value >> 16;
-	buffer[3] = value >> 24;
-}
-
-static BGZF *bgzf_read_init(void)
-{
-	BGZF *fp;
-	fp = calloc(1, sizeof(BGZF));
-	fp->is_write = 0;
-	fp->uncompressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
-	fp->compressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
-#ifdef BGZF_CACHE
-	fp->cache = kh_init(cache);
-#endif
-	return fp;
-}
-
-static BGZF *bgzf_write_init(int compress_level) // compress_level==-1 for the default level
-{
-	BGZF *fp;
-	fp = calloc(1, sizeof(BGZF));
-	fp->is_write = 1;
-	fp->uncompressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
-	fp->compressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
-	fp->compress_level = compress_level < 0? Z_DEFAULT_COMPRESSION : compress_level; // Z_DEFAULT_COMPRESSION==-1
-	if (fp->compress_level > 9) fp->compress_level = Z_DEFAULT_COMPRESSION;
-	return fp;
-}
-// get the compress level from the mode string
-static int mode2level(const char *__restrict mode)
-{
-	int i, compress_level = -1;
-	for (i = 0; mode[i]; ++i)
-		if (mode[i] >= '0' && mode[i] <= '9') break;
-	if (mode[i]) compress_level = (int)mode[i] - '0';
-	if (strchr(mode, 'u')) compress_level = 0;
-	return compress_level;
-}
-
-BGZF *bgzf_open(const char *path, const char *mode)
-{
-	BGZF *fp = 0;
-	assert(compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE);
-	if (strchr(mode, 'r') || strchr(mode, 'R')) {
-		_bgzf_file_t fpr;
-		if ((fpr = _bgzf_open(path, "r")) == 0) return 0;
-		fp = bgzf_read_init();
-		fp->fp = fpr;
-	} else if (strchr(mode, 'w') || strchr(mode, 'W')) {
-		FILE *fpw;
-		if ((fpw = fopen(path, "w")) == 0) return 0;
-		fp = bgzf_write_init(mode2level(mode));
-		fp->fp = fpw;
-	}
-	return fp;
-}
-
-BGZF *bgzf_dopen(int fd, const char *mode)
-{
-	BGZF *fp = 0;
-	assert(compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE);
-	if (strchr(mode, 'r') || strchr(mode, 'R')) {
-		_bgzf_file_t fpr;
-		if ((fpr = _bgzf_dopen(fd, "r")) == 0) return 0;
-		fp = bgzf_read_init();
-		fp->fp = fpr;
-	} else if (strchr(mode, 'w') || strchr(mode, 'W')) {
-		FILE *fpw;
-		if ((fpw = fdopen(fd, "w")) == 0) return 0;
-		fp = bgzf_write_init(mode2level(mode));
-		fp->fp = fpw;
-	}
-	return fp;
-}
-
-static int bgzf_compress(void *_dst, int *dlen, void *src, int slen, int level)
-{
-	uint32_t crc;
-	z_stream zs;
-	uint8_t *dst = (uint8_t*)_dst;
-
-	// compress the body
-	zs.zalloc = NULL; zs.zfree = NULL;
-	zs.next_in  = src;
-	zs.avail_in = slen;
-	zs.next_out = dst + BLOCK_HEADER_LENGTH;
-	zs.avail_out = *dlen - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;
-	if (deflateInit2(&zs, level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1; // -15 to disable zlib header/footer
-	if (deflate(&zs, Z_FINISH) != Z_STREAM_END) return -1;
-	if (deflateEnd(&zs) != Z_OK) return -1;
-	*dlen = zs.total_out + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;
-	// write the header
-	memcpy(dst, g_magic, BLOCK_HEADER_LENGTH); // the last two bytes are a place holder for the length of the block
-	packInt16(&dst[16], *dlen - 1); // write the compressed length; -1 to fit 2 bytes
-	// write the footer
-	crc = crc32(crc32(0L, NULL, 0L), src, slen);
-	packInt32((uint8_t*)&dst[*dlen - 8], crc);
-	packInt32((uint8_t*)&dst[*dlen - 4], slen);
-	return 0;
-}
-
-// Deflate the block in fp->uncompressed_block into fp->compressed_block. Also adds an extra field that stores the compressed block length.
-static int deflate_block(BGZF *fp, int block_length)
-{
-	int comp_size = BGZF_MAX_BLOCK_SIZE;
-	if (bgzf_compress(fp->compressed_block, &comp_size, fp->uncompressed_block, block_length, fp->compress_level) != 0) {
-		fp->errcode |= BGZF_ERR_ZLIB;
-		return -1;
-	}
-	fp->block_offset = 0;
-	return comp_size;
-}
-
-// Inflate the block in fp->compressed_block into fp->uncompressed_block
-static int inflate_block(BGZF* fp, int block_length)
-{
-	z_stream zs;
-	zs.zalloc = NULL;
-	zs.zfree = NULL;
-	zs.next_in = fp->compressed_block + 18;
-	zs.avail_in = block_length - 16;
-	zs.next_out = fp->uncompressed_block;
-	zs.avail_out = BGZF_MAX_BLOCK_SIZE;
-
-	if (inflateInit2(&zs, -15) != Z_OK) {
-		fp->errcode |= BGZF_ERR_ZLIB;
-		return -1;
-	}
-	if (inflate(&zs, Z_FINISH) != Z_STREAM_END) {
-		inflateEnd(&zs);
-		fp->errcode |= BGZF_ERR_ZLIB;
-		return -1;
-	}
-	if (inflateEnd(&zs) != Z_OK) {
-		fp->errcode |= BGZF_ERR_ZLIB;
-		return -1;
-	}
-	return zs.total_out;
-}
-
-static int check_header(const uint8_t *header)
-{
-	return (header[0] == 31 && header[1] == 139 && header[2] == 8 && (header[3] & 4) != 0
-			&& unpackInt16((uint8_t*)&header[10]) == 6
-			&& header[12] == 'B' && header[13] == 'C'
-			&& unpackInt16((uint8_t*)&header[14]) == 2);
-}
-
-#ifdef BGZF_CACHE
-static void free_cache(BGZF *fp)
-{
-	khint_t k;
-	khash_t(cache) *h = (khash_t(cache)*)fp->cache;
-	if (fp->is_write) return;
-	for (k = kh_begin(h); k < kh_end(h); ++k)
-		if (kh_exist(h, k)) free(kh_val(h, k).block);
-	kh_destroy(cache, h);
-}
-
-static int load_block_from_cache(BGZF *fp, int64_t block_address)
-{
-	khint_t k;
-	cache_t *p;
-	khash_t(cache) *h = (khash_t(cache)*)fp->cache;
-	k = kh_get(cache, h, block_address);
-	if (k == kh_end(h)) return 0;
-	p = &kh_val(h, k);
-	if (fp->block_length != 0) fp->block_offset = 0;
-	fp->block_address = block_address;
-	fp->block_length = p->size;
-	memcpy(fp->uncompressed_block, p->block, BGZF_MAX_BLOCK_SIZE);
-	_bgzf_seek((_bgzf_file_t)fp->fp, p->end_offset, SEEK_SET);
-	return p->size;
-}
-
-static void cache_block(BGZF *fp, int size)
-{
-	int ret;
-	khint_t k;
-	cache_t *p;
-	khash_t(cache) *h = (khash_t(cache)*)fp->cache;
-	if (BGZF_MAX_BLOCK_SIZE >= fp->cache_size) return;
-	if ((kh_size(h) + 1) * BGZF_MAX_BLOCK_SIZE > fp->cache_size) {
-		/* A better way would be to remove the oldest block in the
-		 * cache, but here we remove a random one for simplicity. This
-		 * should not have a big impact on performance. */
-		for (k = kh_begin(h); k < kh_end(h); ++k)
-			if (kh_exist(h, k)) break;
-		if (k < kh_end(h)) {
-			free(kh_val(h, k).block);
-			kh_del(cache, h, k);
-		}
-	}
-	k = kh_put(cache, h, fp->block_address, &ret);
-	if (ret == 0) return; // if this happens, a bug!
-	p = &kh_val(h, k);
-	p->size = fp->block_length;
-	p->end_offset = fp->block_address + size;
-	p->block = malloc(BGZF_MAX_BLOCK_SIZE);
-	memcpy(kh_val(h, k).block, fp->uncompressed_block, BGZF_MAX_BLOCK_SIZE);
-}
-#else
-static void free_cache(BGZF *fp) {}
-static int load_block_from_cache(BGZF *fp, int64_t block_address) {return 0;}
-static void cache_block(BGZF *fp, int size) {}
-#endif
-
-int bgzf_read_block(BGZF *fp)
-{
-	uint8_t header[BLOCK_HEADER_LENGTH], *compressed_block;
-	int count, size = 0, block_length, remaining;
-	int64_t block_address;
-	block_address = _bgzf_tell((_bgzf_file_t)fp->fp);
-	if (fp->cache_size && load_block_from_cache(fp, block_address)) return 0;
-	count = _bgzf_read(fp->fp, header, sizeof(header));
-	if (count == 0) { // no data read
-		fp->block_length = 0;
-		return 0;
-	}
-	if (count != sizeof(header) || !check_header(header)) {
-		fp->errcode |= BGZF_ERR_HEADER;
-		return -1;
-	}
-	size = count;
-	block_length = unpackInt16((uint8_t*)&header[16]) + 1; // +1 because when writing this number, we used "-1"
-	compressed_block = (uint8_t*)fp->compressed_block;
-	memcpy(compressed_block, header, BLOCK_HEADER_LENGTH);
-	remaining = block_length - BLOCK_HEADER_LENGTH;
-	count = _bgzf_read(fp->fp, &compressed_block[BLOCK_HEADER_LENGTH], remaining);
-	if (count != remaining) {
-		fp->errcode |= BGZF_ERR_IO;
-		return -1;
-	}
-	size += count;
-	if ((count = inflate_block(fp, block_length)) < 0) return -1;
-	if (fp->block_length != 0) fp->block_offset = 0; // Do not reset offset if this read follows a seek.
-	fp->block_address = block_address;
-	fp->block_length = count;
-	cache_block(fp, size);
-	return 0;
-}
-
-ssize_t bgzf_read(BGZF *fp, void *data, ssize_t length)
-{
-	ssize_t bytes_read = 0;
-	uint8_t *output = data;
-	if (length <= 0) return 0;
-	assert(fp->is_write == 0);
-	while (bytes_read < length) {
-		int copy_length, available = fp->block_length - fp->block_offset;
-		uint8_t *buffer;
-		if (available <= 0) {
-			if (bgzf_read_block(fp) != 0) return -1;
-			available = fp->block_length - fp->block_offset;
-			if (available <= 0) break;
-		}
-		copy_length = length - bytes_read < available? length - bytes_read : available;
-		buffer = fp->uncompressed_block;
-		memcpy(output, buffer + fp->block_offset, copy_length);
-		fp->block_offset += copy_length;
-		output += copy_length;
-		bytes_read += copy_length;
-	}
-	if (fp->block_offset == fp->block_length) {
-		fp->block_address = _bgzf_tell((_bgzf_file_t)fp->fp);
-		fp->block_offset = fp->block_length = 0;
-	}
-	return bytes_read;
-}
-
-/***** BEGIN: multi-threading *****/
-
-typedef struct {
-	BGZF *fp;
-	struct mtaux_t *mt;
-	void *buf;
-	int i, errcode, toproc;
-} worker_t;
-
-typedef struct mtaux_t {
-	int n_threads, n_blks, curr, done;
-	volatile int proc_cnt;
-	void **blk;
-	int *len;
-	worker_t *w;
-	pthread_t *tid;
-	pthread_mutex_t lock;
-	pthread_cond_t cv;
-} mtaux_t;
-
-static int worker_aux(worker_t *w)
-{
-	int i, tmp, stop = 0;
-	// wait for condition: to process or all done
-	pthread_mutex_lock(&w->mt->lock);
-	while (!w->toproc && !w->mt->done)
-		pthread_cond_wait(&w->mt->cv, &w->mt->lock);
-	if (w->mt->done) stop = 1;
-	w->toproc = 0;
-	pthread_mutex_unlock(&w->mt->lock);
-	if (stop) return 1; // to quit the thread
-	w->errcode = 0;
-	for (i = w->i; i < w->mt->curr; i += w->mt->n_threads) {
-		int clen = BGZF_MAX_BLOCK_SIZE;
-		if (bgzf_compress(w->buf, &clen, w->mt->blk[i], w->mt->len[i], w->fp->compress_level) != 0)
-			w->errcode |= BGZF_ERR_ZLIB;
-		memcpy(w->mt->blk[i], w->buf, clen);
-		w->mt->len[i] = clen;
-	}
-	tmp = __sync_fetch_and_add(&w->mt->proc_cnt, 1);
-	return 0;
-}
-
-static void *mt_worker(void *data)
-{
-	while (worker_aux(data) == 0);
-	return 0;
-}
-
-int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks)
-{
-	int i;
-	mtaux_t *mt;
-	pthread_attr_t attr;
-	if (!fp->is_write || fp->mt || n_threads <= 1) return -1;
-	mt = calloc(1, sizeof(mtaux_t));
-	mt->n_threads = n_threads;
-	mt->n_blks = n_threads * n_sub_blks;
-	mt->len = calloc(mt->n_blks, sizeof(int));
-	mt->blk = calloc(mt->n_blks, sizeof(void*));
-	for (i = 0; i < mt->n_blks; ++i)
-		mt->blk[i] = malloc(BGZF_MAX_BLOCK_SIZE);
-	mt->tid = calloc(mt->n_threads, sizeof(pthread_t)); // tid[0] is not used, as the worker 0 is launched by the master
-	mt->w = calloc(mt->n_threads, sizeof(worker_t));
-	for (i = 0; i < mt->n_threads; ++i) {
-		mt->w[i].i = i;
-		mt->w[i].mt = mt;
-		mt->w[i].fp = fp;
-		mt->w[i].buf = malloc(BGZF_MAX_BLOCK_SIZE);
-	}
-	pthread_attr_init(&attr);
-	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
-	pthread_mutex_init(&mt->lock, 0);
-	pthread_cond_init(&mt->cv, 0);
-	for (i = 1; i < mt->n_threads; ++i) // worker 0 is effectively launched by the master thread
-		pthread_create(&mt->tid[i], &attr, mt_worker, &mt->w[i]);
-	fp->mt = mt;
-	return 0;
-}
-
-static void mt_destroy(mtaux_t *mt)
-{
-	int i;
-	// signal all workers to quit
-	pthread_mutex_lock(&mt->lock);
-	mt->done = 1; mt->proc_cnt = 0;
-	pthread_cond_broadcast(&mt->cv);
-	pthread_mutex_unlock(&mt->lock);
-	for (i = 1; i < mt->n_threads; ++i) pthread_join(mt->tid[i], 0); // worker 0 is effectively launched by the master thread
-	// free other data allocated on heap
-	for (i = 0; i < mt->n_blks; ++i) free(mt->blk[i]);
-	for (i = 0; i < mt->n_threads; ++i) free(mt->w[i].buf);
-	free(mt->blk); free(mt->len); free(mt->w); free(mt->tid);
-	pthread_cond_destroy(&mt->cv);
-	pthread_mutex_destroy(&mt->lock);
-	free(mt);
-}
-
-static void mt_queue(BGZF *fp)
-{
-	mtaux_t *mt = (mtaux_t*)fp->mt;
-	assert(mt->curr < mt->n_blks); // guaranteed by the caller
-	memcpy(mt->blk[mt->curr], fp->uncompressed_block, fp->block_offset);
-	mt->len[mt->curr] = fp->block_offset;
-	fp->block_offset = 0;
-	++mt->curr;
-}
-
-static int mt_flush(BGZF *fp)
-{
-	int i;
-	mtaux_t *mt = (mtaux_t*)fp->mt;
-	if (fp->block_offset) mt_queue(fp); // guaranteed that assertion does not fail
-	// signal all the workers to compress
-	pthread_mutex_lock(&mt->lock);
-	for (i = 0; i < mt->n_threads; ++i) mt->w[i].toproc = 1;
-	mt->proc_cnt = 0;
-	pthread_cond_broadcast(&mt->cv);
-	pthread_mutex_unlock(&mt->lock);
-	// worker 0 is doing things here
-	worker_aux(&mt->w[0]);
-	// wait for all the threads to complete
-	while (mt->proc_cnt < mt->n_threads);
-	// dump data to disk
-	for (i = 0; i < mt->n_threads; ++i) fp->errcode |= mt->w[i].errcode;
-	for (i = 0; i < mt->curr; ++i)
-		if (fwrite(mt->blk[i], 1, mt->len[i], fp->fp) != mt->len[i])
-			fp->errcode |= BGZF_ERR_IO;
-	mt->curr = 0;
-	return 0;
-}
-
-static int mt_lazy_flush(BGZF *fp)
-{
-	mtaux_t *mt = (mtaux_t*)fp->mt;
-	if (fp->block_offset) mt_queue(fp);
-	if (mt->curr == mt->n_blks)
-		return mt_flush(fp);
-	return -1;
-}
-
-static ssize_t mt_write(BGZF *fp, const void *data, ssize_t length)
-{
-	const uint8_t *input = data;
-	ssize_t rest = length;
-	while (rest) {
-		int copy_length = BGZF_BLOCK_SIZE - fp->block_offset < rest? BGZF_BLOCK_SIZE - fp->block_offset : rest;
-		memcpy(fp->uncompressed_block + fp->block_offset, input, copy_length);
-		fp->block_offset += copy_length; input += copy_length; rest -= copy_length;
-		if (fp->block_offset == BGZF_BLOCK_SIZE) mt_lazy_flush(fp);
-	}
-	return length - rest;
-}
-
-/***** END: multi-threading *****/
-
-int bgzf_flush(BGZF *fp)
-{
-	if (!fp->is_write) return 0;
-	if (fp->mt) return mt_flush(fp);
-	while (fp->block_offset > 0) {
-		int block_length;
-		block_length = deflate_block(fp, fp->block_offset);
-		if (block_length < 0) return -1;
-		if (fwrite(fp->compressed_block, 1, block_length, fp->fp) != block_length) {
-			fp->errcode |= BGZF_ERR_IO; // possibly truncated file
-			return -1;
-		}
-		fp->block_address += block_length;
-	}
-	return 0;
-}
-
-int bgzf_flush_try(BGZF *fp, ssize_t size)
-{
-	if (fp->block_offset + size > BGZF_BLOCK_SIZE) {
-		if (fp->mt) return mt_lazy_flush(fp);
-		else return bgzf_flush(fp);
-	}
-	return -1;
-}
-
-ssize_t bgzf_write(BGZF *fp, const void *data, ssize_t length)
-{
-	const uint8_t *input = data;
-	int block_length = BGZF_BLOCK_SIZE, bytes_written = 0;
-	assert(fp->is_write);
-	if (fp->mt) return mt_write(fp, data, length);
-	while (bytes_written < length) {
-		uint8_t* buffer = fp->uncompressed_block;
-		int copy_length = block_length - fp->block_offset < length - bytes_written? block_length - fp->block_offset : length - bytes_written;
-		memcpy(buffer + fp->block_offset, input, copy_length);
-		fp->block_offset += copy_length;
-		input += copy_length;
-		bytes_written += copy_length;
-		if (fp->block_offset == block_length && bgzf_flush(fp)) break;
-	}
-	return bytes_written;
-}
-
-int bgzf_close(BGZF* fp)
-{
-	int ret, count, block_length;
-	if (fp == 0) return -1;
-	if (fp->is_write) {
-		if (bgzf_flush(fp) != 0) return -1;
-		fp->compress_level = -1;
-		block_length = deflate_block(fp, 0); // write an empty block
-		count = fwrite(fp->compressed_block, 1, block_length, fp->fp);
-		if (fflush(fp->fp) != 0) {
-			fp->errcode |= BGZF_ERR_IO;
-			return -1;
-		}
-		if (fp->mt) mt_destroy(fp->mt);
-	}
-	ret = fp->is_write? fclose(fp->fp) : _bgzf_close(fp->fp);
-	if (ret != 0) return -1;
-	free(fp->uncompressed_block);
-	free(fp->compressed_block);
-	free_cache(fp);
-	free(fp);
-	return 0;
-}
-
-void bgzf_set_cache_size(BGZF *fp, int cache_size)
-{
-	if (fp) fp->cache_size = cache_size;
-}
-
-int bgzf_check_EOF(BGZF *fp)
-{
-	static uint8_t magic[28] = "\037\213\010\4\0\0\0\0\0\377\6\0\102\103\2\0\033\0\3\0\0\0\0\0\0\0\0\0";
-	uint8_t buf[28];
-	off_t offset;
-	offset = _bgzf_tell((_bgzf_file_t)fp->fp);
-	if (_bgzf_seek(fp->fp, -28, SEEK_END) < 0) return 0;
-	_bgzf_read(fp->fp, buf, 28);
-	_bgzf_seek(fp->fp, offset, SEEK_SET);
-	return (memcmp(magic, buf, 28) == 0)? 1 : 0;
-}
-
-int64_t bgzf_seek(BGZF* fp, int64_t pos, int where)
-{
-	int block_offset;
-	int64_t block_address;
-
-	if (fp->is_write || where != SEEK_SET) {
-		fp->errcode |= BGZF_ERR_MISUSE;
-		return -1;
-	}
-	block_offset = pos & 0xFFFF;
-	block_address = pos >> 16;
-	if (_bgzf_seek(fp->fp, block_address, SEEK_SET) < 0) {
-		fp->errcode |= BGZF_ERR_IO;
-		return -1;
-	}
-	fp->block_length = 0;  // indicates current block has not been loaded
-	fp->block_address = block_address;
-	fp->block_offset = block_offset;
-	return 0;
-}
-
-int bgzf_is_bgzf(const char *fn)
-{
-	uint8_t buf[16];
-	int n;
-	_bgzf_file_t fp;
-	if ((fp = _bgzf_open(fn, "r")) == 0) return 0;
-	n = _bgzf_read(fp, buf, 16);
-	_bgzf_close(fp);
-	if (n != 16) return 0;
-	return memcmp(g_magic, buf, 16) == 0? 1 : 0;
-}
-
-int bgzf_getc(BGZF *fp)
-{
-	int c;
-	if (fp->block_offset >= fp->block_length) {
-		if (bgzf_read_block(fp) != 0) return -2; /* error */
-		if (fp->block_length == 0) return -1; /* end-of-file */
-	}
-	c = ((unsigned char*)fp->uncompressed_block)[fp->block_offset++];
-    if (fp->block_offset == fp->block_length) {
-        fp->block_address = _bgzf_tell((_bgzf_file_t)fp->fp);
-        fp->block_offset = 0;
-        fp->block_length = 0;
-    }
-	return c;
-}
-
-#ifndef kroundup32
-#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
-#endif
-
-int bgzf_getline(BGZF *fp, int delim, kstring_t *str)
-{
-	int l, state = 0;
-	unsigned char *buf = (unsigned char*)fp->uncompressed_block;
-	str->l = 0;
-	do {
-		if (fp->block_offset >= fp->block_length) {
-			if (bgzf_read_block(fp) != 0) { state = -2; break; }
-			if (fp->block_length == 0) { state = -1; break; }
-		}
-		for (l = fp->block_offset; l < fp->block_length && buf[l] != delim; ++l);
-		if (l < fp->block_length) state = 1;
-		l -= fp->block_offset;
-		if (str->l + l + 1 >= str->m) {
-			str->m = str->l + l + 2;
-			kroundup32(str->m);
-			str->s = (char*)realloc(str->s, str->m);
-		}
-		memcpy(str->s + str->l, buf + fp->block_offset, l);
-		str->l += l;
-		fp->block_offset += l + 1;
-		if (fp->block_offset >= fp->block_length) {
-			fp->block_address = _bgzf_tell((_bgzf_file_t)fp->fp);
-			fp->block_offset = 0;
-			fp->block_length = 0;
-		} 
-	} while (state == 0);
-	if (str->l == 0 && state < 0) return state;
-	str->s[str->l] = 0;
-	return str->l;
-}
diff --git a/samtools/bgzf.h b/samtools/bgzf.h
deleted file mode 100644
index cb67681..0000000
--- a/samtools/bgzf.h
+++ /dev/null
@@ -1,207 +0,0 @@
-/* The MIT License
-
-   Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
-                 2011, 2012 Attractive Chaos <attractor at live.co.uk>
-
-   Permission is hereby granted, free of charge, to any person obtaining a copy
-   of this software and associated documentation files (the "Software"), to deal
-   in the Software without restriction, including without limitation the rights
-   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-   copies of the Software, and to permit persons to whom the Software is
-   furnished to do so, subject to the following conditions:
-
-   The above copyright notice and this permission notice shall be included in
-   all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-   THE SOFTWARE.
-*/
-
-/* The BGZF library was originally written by Bob Handsaker from the Broad
- * Institute. It was later improved by the SAMtools developers. */
-
-#ifndef __BGZF_H
-#define __BGZF_H
-
-#include <stdint.h>
-#include <stdio.h>
-#include <zlib.h>
-#include <sys/types.h>
-
-#define BGZF_BLOCK_SIZE     0xff00 // make sure compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE
-#define BGZF_MAX_BLOCK_SIZE 0x10000
-
-#define BGZF_ERR_ZLIB   1
-#define BGZF_ERR_HEADER 2
-#define BGZF_ERR_IO     4
-#define BGZF_ERR_MISUSE 8
-
-typedef struct {
-	int errcode:16, is_write:2, compress_level:14;
-	int cache_size;
-    int block_length, block_offset;
-    int64_t block_address;
-    void *uncompressed_block, *compressed_block;
-	void *cache; // a pointer to a hash table
-	void *fp; // actual file handler; FILE* on writing; FILE* or knetFile* on reading
-	void *mt; // only used for multi-threading
-} BGZF;
-
-#ifndef KSTRING_T
-#define KSTRING_T kstring_t
-typedef struct __kstring_t {
-	size_t l, m;
-	char *s;
-} kstring_t;
-#endif
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-	/******************
-	 * Basic routines *
-	 ******************/
-
-	/**
-	 * Open an existing file descriptor for reading or writing.
-	 *
-	 * @param fd    file descriptor
-	 * @param mode  mode matching /[rwu0-9]+/: 'r' for reading, 'w' for writing and a digit specifies
-	 *              the zlib compression level; if both 'r' and 'w' are present, 'w' is ignored.
-	 * @return      BGZF file handler; 0 on error
-	 */
-	BGZF* bgzf_dopen(int fd, const char *mode);
-
-	#define bgzf_fdopen(fd, mode) bgzf_dopen((fd), (mode)) // for backward compatibility
-
-	/**
-	 * Open the specified file for reading or writing.
-	 */
-	BGZF* bgzf_open(const char* path, const char *mode);
-
-	/**
-	 * Close the BGZF and free all associated resources.
-	 *
-	 * @param fp    BGZF file handler
-	 * @return      0 on success and -1 on error
-	 */
-	int bgzf_close(BGZF *fp);
-
-	/**
-	 * Read up to _length_ bytes from the file storing into _data_.
-	 *
-	 * @param fp     BGZF file handler
-	 * @param data   data array to read into
-	 * @param length size of data to read
-	 * @return       number of bytes actually read; 0 on end-of-file and -1 on error
-	 */
-	ssize_t bgzf_read(BGZF *fp, void *data, ssize_t length);
-
-	/**
-	 * Write _length_ bytes from _data_ to the file.
-	 *
-	 * @param fp     BGZF file handler
-	 * @param data   data array to write
-	 * @param length size of data to write
-	 * @return       number of bytes actually written; -1 on error
-	 */
-	ssize_t bgzf_write(BGZF *fp, const void *data, ssize_t length);
-
-	/**
-	 * Write the data in the buffer to the file.
-	 */
-	int bgzf_flush(BGZF *fp);
-
-	/**
-	 * Return a virtual file pointer to the current location in the file.
-	 * No interpetation of the value should be made, other than a subsequent
-	 * call to bgzf_seek can be used to position the file at the same point.
-	 * Return value is non-negative on success.
-	 */
-	#define bgzf_tell(fp) ((fp->block_address << 16) | (fp->block_offset & 0xFFFF))
-
-	/**
-	 * Set the file to read from the location specified by _pos_.
-	 *
-	 * @param fp     BGZF file handler
-	 * @param pos    virtual file offset returned by bgzf_tell()
-	 * @param whence must be SEEK_SET
-	 * @return       0 on success and -1 on error
-	 */
-	int64_t bgzf_seek(BGZF *fp, int64_t pos, int whence);
-
-	/**
-	 * Check if the BGZF end-of-file (EOF) marker is present
-	 *
-	 * @param fp    BGZF file handler opened for reading
-	 * @return      1 if EOF is present; 0 if not or on I/O error
-	 */
-	int bgzf_check_EOF(BGZF *fp);
-
-	/**
-	 * Check if a file is in the BGZF format
-	 *
-	 * @param fn    file name
-	 * @return      1 if _fn_ is BGZF; 0 if not or on I/O error
-	 */
-	 int bgzf_is_bgzf(const char *fn);
-
-	/*********************
-	 * Advanced routines *
-	 *********************/
-
-	/**
-	 * Set the cache size. Only effective when compiled with -DBGZF_CACHE.
-	 *
-	 * @param fp    BGZF file handler
-	 * @param size  size of cache in bytes; 0 to disable caching (default)
-	 */
-	void bgzf_set_cache_size(BGZF *fp, int size);
-
-	/**
-	 * Flush the file if the remaining buffer size is smaller than _size_ 
-	 */
-	int bgzf_flush_try(BGZF *fp, ssize_t size);
-
-	/**
-	 * Read one byte from a BGZF file. It is faster than bgzf_read()
-	 * @param fp     BGZF file handler
-	 * @return       byte read; -1 on end-of-file or error
-	 */
-	int bgzf_getc(BGZF *fp);
-
-	/**
-	 * Read one line from a BGZF file. It is faster than bgzf_getc()
-	 *
-	 * @param fp     BGZF file handler
-	 * @param delim  delimitor
-	 * @param str    string to write to; must be initialized
-	 * @return       length of the string; 0 on end-of-file; negative on error
-	 */
-	int bgzf_getline(BGZF *fp, int delim, kstring_t *str);
-
-	/**
-	 * Read the next BGZF block.
-	 */
-	int bgzf_read_block(BGZF *fp);
-
-	/**
-	 * Enable multi-threading (only effective on writing)
-	 *
-	 * @param fp          BGZF file handler; must be opened for writing
-	 * @param n_threads   #threads used for writing
-	 * @param n_sub_blks  #blocks processed by each thread; a value 64-256 is recommended
-	 */
-	int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/samtools/cut_target.c.pysam.c b/samtools/cut_target.c.pysam.c
index 13cf2d2..de6b17c 100644
--- a/samtools/cut_target.c.pysam.c
+++ b/samtools/cut_target.c.pysam.c
@@ -1,16 +1,41 @@
 #include "pysam.h"
 
+/*  cut_target.c -- targetcut subcommand.
+
+    Copyright (C) 2011 Broad Institute.
+    Copyright (C) 2012-2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include "bam.h"
 #include "errmod.h"
-#include "faidx.h"
+#include "htslib/faidx.h"
 
-#define ERR_DEP 0.83f
+#define ERR_DEP 0.83
 
 typedef struct {
-	int e[2][3], p[2][2];
+    int e[2][3], p[2][2];
 } score_param_t;
 
 /* Note that although the two matrics have 10 parameters in total, only 4
@@ -19,177 +44,183 @@ typedef struct {
 static score_param_t g_param = { {{0,0,0},{-4,1,6}}, {{0,-14000}, {0,0}} };
 
 typedef struct {
-	int min_baseQ, tid, max_bases;
-	uint16_t *bases;
-	bamFile fp;
-	bam_header_t *h;
-	char *ref;
-	faidx_t *fai;
-	errmod_t *em;
+    int min_baseQ, tid, max_bases;
+    uint16_t *bases;
+    bamFile fp;
+    bam_header_t *h;
+    char *ref;
+    faidx_t *fai;
+    errmod_t *em;
 } ct_t;
 
 static uint16_t gencns(ct_t *g, int n, const bam_pileup1_t *plp)
 {
-	int i, j, ret, tmp, k, sum[4], qual;
-	float q[16];
-	if (n > g->max_bases) { // enlarge g->bases
-		g->max_bases = n;
-		kroundup32(g->max_bases);
-		g->bases = realloc(g->bases, g->max_bases * 2);
-	}
-	for (i = k = 0; i < n; ++i) {
-		const bam_pileup1_t *p = plp + i;
-		uint8_t *seq;
-		int q, baseQ, b;
-		if (p->is_refskip || p->is_del) continue;
-		baseQ = bam1_qual(p->b)[p->qpos];
-		if (baseQ < g->min_baseQ) continue;
-		seq = bam1_seq(p->b);
-		b = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos)];
-		if (b > 3) continue;
-		q = baseQ < p->b->core.qual? baseQ : p->b->core.qual;
-		if (q < 4) q = 4;
-		if (q > 63) q = 63;
-		g->bases[k++] = q<<5 | bam1_strand(p->b)<<4 | b;
-	}
-	if (k == 0) return 0;
-	errmod_cal(g->em, k, 4, g->bases, q);
-	for (i = 0; i < 4; ++i) sum[i] = (int)(q[i<<2|i] + .499) << 2 | i;
-	for (i = 1; i < 4; ++i) // insertion sort
-		for (j = i; j > 0 && sum[j] < sum[j-1]; --j)
-			tmp = sum[j], sum[j] = sum[j-1], sum[j-1] = tmp;
-	qual = (sum[1]>>2) - (sum[0]>>2);
-	k = k < 256? k : 255;
-	ret = (qual < 63? qual : 63) << 2 | (sum[0]&3);
-	return ret<<8|k;
+    extern const char bam_nt16_nt4_table[];
+    int i, j, ret, tmp, k, sum[4], qual;
+    float q[16];
+    if (n > g->max_bases) { // enlarge g->bases
+        g->max_bases = n;
+        kroundup32(g->max_bases);
+        g->bases = realloc(g->bases, g->max_bases * 2);
+    }
+    for (i = k = 0; i < n; ++i) {
+        const bam_pileup1_t *p = plp + i;
+        uint8_t *seq;
+        int q, baseQ, b;
+        if (p->is_refskip || p->is_del) continue;
+        baseQ = bam1_qual(p->b)[p->qpos];
+        if (baseQ < g->min_baseQ) continue;
+        seq = bam1_seq(p->b);
+        b = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos)];
+        if (b > 3) continue;
+        q = baseQ < p->b->core.qual? baseQ : p->b->core.qual;
+        if (q < 4) q = 4;
+        if (q > 63) q = 63;
+        g->bases[k++] = q<<5 | bam1_strand(p->b)<<4 | b;
+    }
+    if (k == 0) return 0;
+    errmod_cal(g->em, k, 4, g->bases, q);
+    for (i = 0; i < 4; ++i) sum[i] = (int)(q[i<<2|i] + .499) << 2 | i;
+    for (i = 1; i < 4; ++i) // insertion sort
+        for (j = i; j > 0 && sum[j] < sum[j-1]; --j)
+            tmp = sum[j], sum[j] = sum[j-1], sum[j-1] = tmp;
+    qual = (sum[1]>>2) - (sum[0]>>2);
+    k = k < 256? k : 255;
+    ret = (qual < 63? qual : 63) << 2 | (sum[0]&3);
+    return ret<<8|k;
 }
 
 static void process_cns(bam_header_t *h, int tid, int l, uint16_t *cns)
 {
-	int i, f[2][2], *prev, *curr, *swap_tmp, s;
-	uint8_t *b; // backtrack array
-	b = calloc(l, 1);
-	f[0][0] = f[0][1] = 0;
-	prev = f[0]; curr = f[1];
-	// fill the backtrack matrix
-	for (i = 0; i < l; ++i) {
-		int c = (cns[i] == 0)? 0 : (cns[i]>>8 == 0)? 1 : 2;
-		int tmp0, tmp1;
-		// compute f[0]
-		tmp0 = prev[0] + g_param.e[0][c] + g_param.p[0][0]; // (s[i+1],s[i])=(0,0)
-		tmp1 = prev[1] + g_param.e[0][c] + g_param.p[1][0]; // (0,1)
-		if (tmp0 > tmp1) curr[0] = tmp0, b[i] = 0;
-		else curr[0] = tmp1, b[i] = 1;
-		// compute f[1]
-		tmp0 = prev[0] + g_param.e[1][c] + g_param.p[0][1]; // (s[i+1],s[i])=(1,0)
-		tmp1 = prev[1] + g_param.e[1][c] + g_param.p[1][1]; // (1,1)
-		if (tmp0 > tmp1) curr[1] = tmp0, b[i] |= 0<<1;
-		else curr[1] = tmp1, b[i] |= 1<<1;
-		// swap
-		swap_tmp = prev; prev = curr; curr = swap_tmp;
-	}
-	// backtrack
-	s = prev[0] > prev[1]? 0 : 1;
-	for (i = l - 1; i > 0; --i) {
-		b[i] |= s<<2;
-		s = b[i]>>s&1;
-	}
-	// print
-	for (i = 0, s = -1; i <= l; ++i) {
-		if (i == l || ((b[i]>>2&3) == 0 && s >= 0)) {
-			if (s >= 0) {
-				int j;
-				printf("%s:%d-%d\t0\t%s\t%d\t60\t%dM\t*\t0\t0\t", h->target_name[tid], s+1, i, h->target_name[tid], s+1, i-s);
-				for (j = s; j < i; ++j) {
-					int c = cns[j]>>8;
-					if (c == 0) putchar('N');
-					else putchar("ACGT"[c&3]);
-				}
-				putchar('\t');
-				for (j = s; j < i; ++j)
-					putchar(33 + (cns[j]>>8>>2));
-				putchar('\n');
-			}
-			//if (s >= 0) printf("%s\t%d\t%d\t%d\n", h->target_name[tid], s, i, i - s);
-			s = -1;
-		} else if ((b[i]>>2&3) && s < 0) s = i;
-	}
-	free(b);
+    int i, f[2][2], *prev, *curr, *swap_tmp, s;
+    uint8_t *b; // backtrack array
+    b = calloc(l, 1);
+    f[0][0] = f[0][1] = 0;
+    prev = f[0]; curr = f[1];
+    // fill the backtrack matrix
+    for (i = 0; i < l; ++i) {
+        int c = (cns[i] == 0)? 0 : (cns[i]>>8 == 0)? 1 : 2;
+        int tmp0, tmp1;
+        // compute f[0]
+        tmp0 = prev[0] + g_param.e[0][c] + g_param.p[0][0]; // (s[i+1],s[i])=(0,0)
+        tmp1 = prev[1] + g_param.e[0][c] + g_param.p[1][0]; // (0,1)
+        if (tmp0 > tmp1) curr[0] = tmp0, b[i] = 0;
+        else curr[0] = tmp1, b[i] = 1;
+        // compute f[1]
+        tmp0 = prev[0] + g_param.e[1][c] + g_param.p[0][1]; // (s[i+1],s[i])=(1,0)
+        tmp1 = prev[1] + g_param.e[1][c] + g_param.p[1][1]; // (1,1)
+        if (tmp0 > tmp1) curr[1] = tmp0, b[i] |= 0<<1;
+        else curr[1] = tmp1, b[i] |= 1<<1;
+        // swap
+        swap_tmp = prev; prev = curr; curr = swap_tmp;
+    }
+    // backtrack
+    s = prev[0] > prev[1]? 0 : 1;
+    for (i = l - 1; i > 0; --i) {
+        b[i] |= s<<2;
+        s = b[i]>>s&1;
+    }
+    // print
+    for (i = 0, s = -1; i <= l; ++i) {
+        if (i == l || ((b[i]>>2&3) == 0 && s >= 0)) {
+            if (s >= 0) {
+                int j;
+                printf("%s:%d-%d\t0\t%s\t%d\t60\t%dM\t*\t0\t0\t", h->target_name[tid], s+1, i, h->target_name[tid], s+1, i-s);
+                for (j = s; j < i; ++j) {
+                    int c = cns[j]>>8;
+                    if (c == 0) putchar('N');
+                    else putchar("ACGT"[c&3]);
+                }
+                putchar('\t');
+                for (j = s; j < i; ++j)
+                    putchar(33 + (cns[j]>>8>>2));
+                putchar('\n');
+            }
+            //if (s >= 0) printf("%s\t%d\t%d\t%d\n", h->target_name[tid], s, i, i - s);
+            s = -1;
+        } else if ((b[i]>>2&3) && s < 0) s = i;
+    }
+    free(b);
 }
 
 static int read_aln(void *data, bam1_t *b)
 {
-	extern int bam_prob_realn_core(bam1_t *b, const char *ref, int flag);
-	ct_t *g = (ct_t*)data;
-	int ret, len;
-	ret = bam_read1(g->fp, b);
-	if (ret >= 0 && g->fai && b->core.tid >= 0 && (b->core.flag&4) == 0) {
-		if (b->core.tid != g->tid) { // then load the sequence
-			free(g->ref);
-			g->ref = fai_fetch(g->fai, g->h->target_name[b->core.tid], &len);
-			g->tid = b->core.tid;
-		}
-		bam_prob_realn_core(b, g->ref, 1<<1|1);
-	}
-	return ret;
+    extern int bam_prob_realn_core(bam1_t *b, const char *ref, int flag);
+    ct_t *g = (ct_t*)data;
+    int ret, len;
+    while (1)
+    {
+        ret = bam_read1(g->fp, b);
+        if ( ret<0 ) break;
+        if ( b->core.flag & (BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP) ) continue;
+        if ( g->fai && b->core.tid >= 0 ) {
+            if (b->core.tid != g->tid) { // then load the sequence
+                free(g->ref);
+                g->ref = fai_fetch(g->fai, g->h->target_name[b->core.tid], &len);
+                g->tid = b->core.tid;
+            }
+            bam_prob_realn_core(b, g->ref, 1<<1|1);
+        }
+        break;
+    }
+    return ret;
 }
 
 int main_cut_target(int argc, char *argv[])
 {
-	int c, tid, pos, n, lasttid = -1, lastpos = -1, l, max_l;
-	const bam_pileup1_t *p;
-	bam_plp_t plp;
-	uint16_t *cns;
-	ct_t g;
-
-	memset(&g, 0, sizeof(ct_t));
-	g.min_baseQ = 13; g.tid = -1;
-	while ((c = getopt(argc, argv, "f:Q:i:o:0:1:2:")) >= 0) {
-		switch (c) {
-			case 'Q': g.min_baseQ = atoi(optarg); break; // quality cutoff
-			case 'i': g_param.p[0][1] = -atoi(optarg); break; // 0->1 transition (in) PENALTY
-			case '0': g_param.e[1][0] = atoi(optarg); break; // emission SCORE
-			case '1': g_param.e[1][1] = atoi(optarg); break;
-			case '2': g_param.e[1][2] = atoi(optarg); break;
-			case 'f': g.fai = fai_load(optarg);
-				if (g.fai == 0) fprintf(pysamerr, "[%s] fail to load the fasta index.\n", __func__);
-				break;
-		}
-	}
-	if (argc == optind) {
-		fprintf(pysamerr, "Usage: samtools targetcut [-Q minQ] [-i inPen] [-0 em0] [-1 em1] [-2 em2] [-f ref] <in.bam>\n");
-		return 1;
-	}
-	l = max_l = 0; cns = 0;
-	g.fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
-	g.h = bam_header_read(g.fp);
-	g.em = errmod_init(1 - ERR_DEP);
-	plp = bam_plp_init(read_aln, &g);
-	while ((p = bam_plp_auto(plp, &tid, &pos, &n)) != 0) {
-		if (tid < 0) break;
-		if (tid != lasttid) { // change of chromosome
-			if (cns) process_cns(g.h, lasttid, l, cns);
-			if (max_l < g.h->target_len[tid]) {
-				max_l = g.h->target_len[tid];
-				kroundup32(max_l);
-				cns = realloc(cns, max_l * 2);
-			}
-			l = g.h->target_len[tid];
-			memset(cns, 0, max_l * 2);
-			lasttid = tid;
-		}
-		cns[pos] = gencns(&g, n, p);
-		lastpos = pos;
-	}
-	process_cns(g.h, lasttid, l, cns);
-	free(cns);
-	bam_header_destroy(g.h);
-	bam_plp_destroy(plp);
-	bam_close(g.fp);
-	if (g.fai) {
-		fai_destroy(g.fai); free(g.ref);
-	}
-	errmod_destroy(g.em);
-	free(g.bases);
-	return 0;
+    int c, tid, pos, n, lasttid = -1, l, max_l;
+    const bam_pileup1_t *p;
+    bam_plp_t plp;
+    uint16_t *cns;
+    ct_t g;
+
+    memset(&g, 0, sizeof(ct_t));
+    g.min_baseQ = 13; g.tid = -1;
+    while ((c = getopt(argc, argv, "f:Q:i:o:0:1:2:")) >= 0) {
+        switch (c) {
+            case 'Q': g.min_baseQ = atoi(optarg); break; // quality cutoff
+            case 'i': g_param.p[0][1] = -atoi(optarg); break; // 0->1 transition (in) PENALTY
+            case '0': g_param.e[1][0] = atoi(optarg); break; // emission SCORE
+            case '1': g_param.e[1][1] = atoi(optarg); break;
+            case '2': g_param.e[1][2] = atoi(optarg); break;
+            case 'f': g.fai = fai_load(optarg);
+                if (g.fai == 0) fprintf(pysamerr, "[%s] fail to load the fasta index.\n", __func__);
+                break;
+        }
+    }
+    if (argc == optind) {
+        fprintf(pysamerr, "Usage: samtools targetcut [-Q minQ] [-i inPen] [-0 em0] [-1 em1] [-2 em2] [-f ref] <in.bam>\n");
+        return 1;
+    }
+    l = max_l = 0; cns = 0;
+    g.fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
+    g.h = bam_header_read(g.fp);
+    g.em = errmod_init(1. - ERR_DEP);
+    plp = bam_plp_init(read_aln, &g);
+    while ((p = bam_plp_auto(plp, &tid, &pos, &n)) != 0) {
+        if (tid < 0) break;
+        if (tid != lasttid) { // change of chromosome
+            if (cns) process_cns(g.h, lasttid, l, cns);
+            if (max_l < g.h->target_len[tid]) {
+                max_l = g.h->target_len[tid];
+                kroundup32(max_l);
+                cns = realloc(cns, max_l * 2);
+            }
+            l = g.h->target_len[tid];
+            memset(cns, 0, max_l * 2);
+            lasttid = tid;
+        }
+        cns[pos] = gencns(&g, n, p);
+    }
+    process_cns(g.h, lasttid, l, cns);
+    free(cns);
+    bam_header_destroy(g.h);
+    bam_plp_destroy(plp);
+    bam_close(g.fp);
+    if (g.fai) {
+        fai_destroy(g.fai); free(g.ref);
+    }
+    errmod_destroy(g.em);
+    free(g.bases);
+    return 0;
 }
diff --git a/samtools/errmod.c.pysam.c b/samtools/errmod.c.pysam.c
index bf62418..c19a201 100644
--- a/samtools/errmod.c.pysam.c
+++ b/samtools/errmod.c.pysam.c
@@ -1,132 +1,195 @@
 #include "pysam.h"
 
+/*  errmod.c -- revised MAQ error model.
+
+    Copyright (C) 2010 Broad Institute.
+    Copyright (C) 2012, 2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <math.h>
 #include "errmod.h"
-#include "ksort.h"
+#include "htslib/ksort.h"
 KSORT_INIT_GENERIC(uint16_t)
 
+/* table of constants generated for given depcorr and eta */
 typedef struct __errmod_coef_t {
-	double *fk, *beta, *lhet;
+    double *fk, *beta, *lhet;
 } errmod_coef_t;
 
 typedef struct {
-	double fsum[16], bsum[16];
-	uint32_t c[16];
+    double fsum[16], bsum[16];
+    uint32_t c[16];
 } call_aux_t;
 
+/* \Gamma(n) = (n-1)! */
+#define lfact(n) lgamma(n+1)
+
+/* generates a success * trials table of bionomial probability densities (log transformed) */
+static double* logbinomial_table( const int n_size )
+{
+    /* prob distribution for binom var is p(k) = {n! \over k! (n-k)! } p^k (1-p)^{n-k} */
+    /* this calcs p(k) = {log(n!) - log(k!) - log((n-k)!) */
+    int k, n;
+    double *logbinom = (double*)calloc(n_size * n_size, sizeof(double));
+    for (n = 1; n < n_size; ++n) {
+        double lfn = lfact(n);
+        for (k = 1; k <= n; ++k)
+            logbinom[n<<8|k] = lfn - lfact(k) - lfact(n-k);
+    }
+    return logbinom;
+}
+
 static errmod_coef_t *cal_coef(double depcorr, double eta)
 {
-	int k, n, q;
-	long double sum, sum1;
-	double *lC;
-	errmod_coef_t *ec;
-
-	ec = calloc(1, sizeof(errmod_coef_t));
-	// initialize ->fk
-	ec->fk = (double*)calloc(256, sizeof(double));
-	ec->fk[0] = 1.0;
-	for (n = 1; n != 256; ++n)
-		ec->fk[n] = pow(1. - depcorr, n) * (1.0 - eta) + eta;
-	// initialize ->coef
-	ec->beta = (double*)calloc(256 * 256 * 64, sizeof(double));
-	lC = (double*)calloc(256 * 256, sizeof(double));
-	for (n = 1; n != 256; ++n) {
-		double lgn = lgamma(n+1);
-		for (k = 1; k <= n; ++k)
-			lC[n<<8|k] = lgn - lgamma(k+1) - lgamma(n-k+1);
-	}
-	for (q = 1; q != 64; ++q) {
-		double e = pow(10.0, -q/10.0);
-		double le = log(e);
-		double le1 = log(1.0 - e);
-		for (n = 1; n <= 255; ++n) {
-			double *beta = ec->beta + (q<<16|n<<8);
-			sum1 = sum = 0.0;
-			for (k = n; k >= 0; --k, sum1 = sum) {
-				sum = sum1 + expl(lC[n<<8|k] + k*le + (n-k)*le1);
-				beta[k] = -10. / M_LN10 * logl(sum1 / sum);
-			}
-		}
-	}
-	// initialize ->lhet
-	ec->lhet = (double*)calloc(256 * 256, sizeof(double));
-	for (n = 0; n < 256; ++n)
-		for (k = 0; k < 256; ++k)
-			ec->lhet[n<<8|k] = lC[n<<8|k] - M_LN2 * n;
-	free(lC);
-	return ec;
+    int k, n, q;
+    long double sum, sum1;
+    double *lC;
+    errmod_coef_t *ec;
+
+    ec = calloc(1, sizeof(errmod_coef_t));
+    // initialize ->fk
+    ec->fk = (double*)calloc(256, sizeof(double));
+    ec->fk[0] = 1.0;
+    for (n = 1; n != 256; ++n)
+        ec->fk[n] = pow(1. - depcorr, n) * (1.0 - eta) + eta;
+    // initialize ->coef
+    ec->beta = (double*)calloc(256 * 256 * 64, sizeof(double));
+
+    lC = logbinomial_table( 256 );
+
+    for (q = 1; q != 64; ++q) {
+        double e = pow(10.0, -q/10.0);
+        double le = log(e);
+        double le1 = log(1.0 - e);
+        for (n = 1; n <= 255; ++n) {
+            double *beta = ec->beta + (q<<16|n<<8);
+            sum1 = sum = 0.0;
+            for (k = n; k >= 0; --k, sum1 = sum) {
+                sum = sum1 + expl(lC[n<<8|k] + k*le + (n-k)*le1);
+                beta[k] = -10. / M_LN10 * logl(sum1 / sum);
+            }
+        }
+    }
+    // initialize ->lhet
+    ec->lhet = (double*)calloc(256 * 256, sizeof(double));
+    for (n = 0; n < 256; ++n)
+        for (k = 0; k < 256; ++k)
+            ec->lhet[n<<8|k] = lC[n<<8|k] - M_LN2 * n;
+    free(lC);
+    return ec;
 }
 
-errmod_t *errmod_init(float depcorr)
+/**
+ * Create errmod_t object with obj.depcorr set to depcorr and initialise
+ */
+errmod_t *errmod_init(double depcorr)
 {
-	errmod_t *em;
-	em = (errmod_t*)calloc(1, sizeof(errmod_t));
-	em->depcorr = depcorr;
-	em->coef = cal_coef(depcorr, 0.03);
-	return em;
+    errmod_t *em;
+    em = (errmod_t*)calloc(1, sizeof(errmod_t));
+    em->depcorr = depcorr;
+    em->coef = cal_coef(depcorr, 0.03);
+    return em;
 }
 
+/**
+ * Deallocate an errmod_t object
+ */
 void errmod_destroy(errmod_t *em)
 {
-	if (em == 0) return;
-	free(em->coef->lhet); free(em->coef->fk); free(em->coef->beta);
-	free(em->coef); free(em);
+    if (em == 0) return;
+    free(em->coef->lhet); free(em->coef->fk); free(em->coef->beta);
+    free(em->coef); free(em);
 }
-// qual:6, strand:1, base:4
+
+//
+// em: error model to fit to data
+// m: number of alleles across all samples
+// n: number of bases observed in sample
+// bases[i]: bases observed in pileup [6 bit quality|1 bit strand|4 bit base]
+// q[i*m+j]: (Output) phred-scaled likelihood of each genotype (i,j)
 int errmod_cal(const errmod_t *em, int n, int m, uint16_t *bases, float *q)
 {
-	call_aux_t aux;
-	int i, j, k, w[32];
-
-	if (m > m) return -1;
-	memset(q, 0, m * m * sizeof(float));
-	if (n == 0) return 0;
-	// calculate aux.esum and aux.fsum
-	if (n > 255) { // then sample 255 bases
-		ks_shuffle(uint16_t, n, bases);
-		n = 255;
-	}
-	ks_introsort(uint16_t, n, bases);
-	memset(w, 0, 32 * sizeof(int));
-	memset(&aux, 0, sizeof(call_aux_t));
-	for (j = n - 1; j >= 0; --j) { // calculate esum and fsum
-		uint16_t b = bases[j];
-		int q = b>>5 < 4? 4 : b>>5;
-		if (q > 63) q = 63;
-		k = b&0x1f;
-		aux.fsum[k&0xf] += em->coef->fk[w[k]];
-		aux.bsum[k&0xf] += em->coef->fk[w[k]] * em->coef->beta[q<<16|n<<8|aux.c[k&0xf]];
-		++aux.c[k&0xf];
-		++w[k];
-	}
-	// generate likelihood
-	for (j = 0; j != m; ++j) {
-		float tmp1, tmp3;
-		int tmp2, bar_e;
-		// homozygous
-		for (k = 0, tmp1 = tmp3 = 0.0, tmp2 = 0; k != m; ++k) {
-			if (k == j) continue;
-			tmp1 += aux.bsum[k]; tmp2 += aux.c[k]; tmp3 += aux.fsum[k];
-		}
-		if (tmp2) {
-			bar_e = (int)(tmp1 / tmp3 + 0.499);
-			if (bar_e > 63) bar_e = 63;
-			q[j*m+j] = tmp1;
-		}
-		// heterozygous
-		for (k = j + 1; k < m; ++k) {
-			int cjk = aux.c[j] + aux.c[k];
-			for (i = 0, tmp2 = 0, tmp1 = tmp3 = 0.0; i < m; ++i) {
-				if (i == j || i == k) continue;
-				tmp1 += aux.bsum[i]; tmp2 += aux.c[i]; tmp3 += aux.fsum[i];
-			}
-			if (tmp2) {
-				bar_e = (int)(tmp1 / tmp3 + 0.499);
-				if (bar_e > 63) bar_e = 63;
-				q[j*m+k] = q[k*m+j] = -4.343 * em->coef->lhet[cjk<<8|aux.c[k]] + tmp1;
-			} else q[j*m+k] = q[k*m+j] = -4.343 * em->coef->lhet[cjk<<8|aux.c[k]]; // all the bases are either j or k
-		}
-		for (k = 0; k != m; ++k) if (q[j*m+k] < 0.0) q[j*m+k] = 0.0;
-	}
-	return 0;
+    // Aux
+    // aux.c is total count of each base observed (ignoring strand)
+    call_aux_t aux;
+    // Loop variables
+    int i, j, k;
+    // The total count of each base observed per strand
+    int w[32];
+
+        /* zero out q */
+    memset(q, 0, m * m * sizeof(float));
+    if (n == 0) return 0;
+    // calculate aux.esum and aux.fsum
+    if (n > 255) { // then sample 255 bases
+        ks_shuffle(uint16_t, n, bases);
+        n = 255;
+    }
+    ks_introsort(uint16_t, n, bases);
+    /* zero out w and aux */
+    memset(w, 0, 32 * sizeof(int));
+    memset(&aux, 0, sizeof(call_aux_t));
+
+    for (j = n - 1; j >= 0; --j) { // calculate esum and fsum
+        uint16_t b = bases[j];
+        /* extract quality and cap at 63 */
+        int qual = b>>5 < 4? 4 : b>>5;
+        if (qual > 63) qual = 63;
+        /* extract base ORed with strand */
+        int basestrand = b&0x1f;
+        /* extract base */
+        int base = b&0xf;
+        aux.fsum[base] += em->coef->fk[w[basestrand]];
+        aux.bsum[base] += em->coef->fk[w[basestrand]] * em->coef->beta[qual<<16|n<<8|aux.c[base]];
+        ++aux.c[base];
+        ++w[basestrand];
+    }
+
+    // generate likelihood
+    for (j = 0; j < m; ++j) {
+        float tmp1, tmp3;
+        int tmp2;
+        // homozygous
+        for (k = 0, tmp1 = tmp3 = 0.0, tmp2 = 0; k < m; ++k) {
+            if (k == j) continue;
+            tmp1 += aux.bsum[k]; tmp2 += aux.c[k]; tmp3 += aux.fsum[k];
+        }
+        if (tmp2) {
+            q[j*m+j] = tmp1;
+        }
+        // heterozygous
+        for (k = j + 1; k < m; ++k) {
+            int cjk = aux.c[j] + aux.c[k];
+            for (i = 0, tmp2 = 0, tmp1 = tmp3 = 0.0; i < m; ++i) {
+                if (i == j || i == k) continue;
+                tmp1 += aux.bsum[i]; tmp2 += aux.c[i]; tmp3 += aux.fsum[i];
+            }
+            if (tmp2) {
+                q[j*m+k] = q[k*m+j] = -4.343 * em->coef->lhet[cjk<<8|aux.c[k]] + tmp1;
+            } else q[j*m+k] = q[k*m+j] = -4.343 * em->coef->lhet[cjk<<8|aux.c[k]]; // all the bases are either j or k
+        }
+        /* clamp to greater than 0 */
+        for (k = 0; k < m; ++k) if (q[j*m+k] < 0.0) q[j*m+k] = 0.0;
+    }
+
+    return 0;
 }
diff --git a/samtools/errmod.h b/samtools/errmod.h
index 32c07b6..6db46f4 100644
--- a/samtools/errmod.h
+++ b/samtools/errmod.h
@@ -1,3 +1,28 @@
+/*  errmod.h -- revised MAQ error model.
+
+    Copyright (C) 2010 Broad Institute.
+    Copyright (C) 2012 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #ifndef ERRMOD_H
 #define ERRMOD_H
 
@@ -6,18 +31,18 @@
 struct __errmod_coef_t;
 
 typedef struct {
-	double depcorr;
-	struct __errmod_coef_t *coef;
+    double depcorr;
+    struct __errmod_coef_t *coef;
 } errmod_t;
 
-errmod_t *errmod_init(float depcorr);
+errmod_t *errmod_init(double depcorr);
 void errmod_destroy(errmod_t *em);
 
 /*
-	n: number of bases
-	m: maximum base
-	bases[i]: qual:6, strand:1, base:4
-	q[i*m+j]: phred-scaled likelihood of (i,j)
+    n: number of bases
+    m: maximum base
+    bases[i]: qual:6, strand:1, base:4
+    q[i*m+j]: phred-scaled likelihood of (i,j)
  */
 int errmod_cal(const errmod_t *em, int n, int m, uint16_t *bases, float *q);
 
diff --git a/samtools/faidx.c.pysam.c b/samtools/faidx.c.pysam.c
index c011756..971db3b 100644
--- a/samtools/faidx.c.pysam.c
+++ b/samtools/faidx.c.pysam.c
@@ -1,439 +1,97 @@
 #include "pysam.h"
 
-#include <ctype.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.h>
-#include "faidx.h"
-#include "khash.h"
+/*  faidx.c -- faidx subcommand.
 
-typedef struct {
-	int32_t line_len, line_blen;
-	int64_t len;
-	uint64_t offset;
-} faidx1_t;
-KHASH_MAP_INIT_STR(s, faidx1_t)
+    Copyright (C) 2008, 2009, 2013 Genome Research Ltd.
+    Portions copyright (C) 2011 Broad Institute.
 
-#ifndef _NO_RAZF
-#include "razf.h"
-#else
-#ifdef _WIN32
-#define ftello(fp) ftell(fp)
-#define fseeko(fp, offset, whence) fseek(fp, offset, whence)
-#else
-extern off_t ftello(FILE *stream);
-extern int fseeko(FILE *stream, off_t offset, int whence);
-#endif
-#define RAZF FILE
-#define razf_read(fp, buf, size) fread(buf, 1, size, fp)
-#define razf_open(fn, mode) fopen(fn, mode)
-#define razf_close(fp) fclose(fp)
-#define razf_seek(fp, offset, whence) fseeko(fp, offset, whence)
-#define razf_tell(fp) ftello(fp)
-#endif
-#ifdef _USE_KNETFILE
-#include "knetfile.h"
-#endif
+    Author: Heng Li <lh3 at sanger.ac.uk>
 
-struct __faidx_t {
-	RAZF *rz;
-	int n, m;
-	char **name;
-	khash_t(s) *hash;
-};
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
 
-#ifndef kroundup32
-#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
-#endif
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
 
-static inline void fai_insert_index(faidx_t *idx, const char *name, int len, int line_len, int line_blen, uint64_t offset)
-{
-	khint_t k;
-	int ret;
-	faidx1_t t;
-	if (idx->n == idx->m) {
-		idx->m = idx->m? idx->m<<1 : 16;
-		idx->name = (char**)realloc(idx->name, sizeof(void*) * idx->m);
-	}
-	idx->name[idx->n] = strdup(name);
-	k = kh_put(s, idx->hash, idx->name[idx->n], &ret);
-	t.len = len; t.line_len = line_len; t.line_blen = line_blen; t.offset = offset;
-	kh_value(idx->hash, k) = t;
-	++idx->n;
-}
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
 
-faidx_t *fai_build_core(RAZF *rz)
-{
-	char c, *name;
-	int l_name, m_name, ret;
-	int line_len, line_blen, state;
-	int l1, l2;
-	faidx_t *idx;
-	uint64_t offset;
-	int64_t len;
-
-	idx = (faidx_t*)calloc(1, sizeof(faidx_t));
-	idx->hash = kh_init(s);
-	name = 0; l_name = m_name = 0;
-	len = line_len = line_blen = -1; state = 0; l1 = l2 = -1; offset = 0;
-	while (razf_read(rz, &c, 1)) {
-		if (c == '\n') { // an empty line
-			if (state == 1) {
-				offset = razf_tell(rz);
-				continue;
-			} else if ((state == 0 && len < 0) || state == 2) continue;
-		}
-		if (c == '>') { // fasta header
-			if (len >= 0)
-				fai_insert_index(idx, name, len, line_len, line_blen, offset);
-			l_name = 0;
-			while ((ret = razf_read(rz, &c, 1)) != 0 && !isspace(c)) {
-				if (m_name < l_name + 2) {
-					m_name = l_name + 2;
-					kroundup32(m_name);
-					name = (char*)realloc(name, m_name);
-				}
-				name[l_name++] = c;
-			}
-			name[l_name] = '\0';
-			if (ret == 0) {
-				fprintf(pysamerr, "[fai_build_core] the last entry has no sequence\n");
-				free(name); fai_destroy(idx);
-				return 0;
-			}
-			if (c != '\n') while (razf_read(rz, &c, 1) && c != '\n');
-			state = 1; len = 0;
-			offset = razf_tell(rz);
-		} else {
-			if (state == 3) {
-				fprintf(pysamerr, "[fai_build_core] inlined empty line is not allowed in sequence '%s'.\n", name);
-				free(name); fai_destroy(idx);
-				return 0;
-			}
-			if (state == 2) state = 3;
-			l1 = l2 = 0;
-			do {
-				++l1;
-				if (isgraph(c)) ++l2;
-			} while ((ret = razf_read(rz, &c, 1)) && c != '\n');
-			if (state == 3 && l2) {
-				fprintf(pysamerr, "[fai_build_core] different line length in sequence '%s'.\n", name);
-				free(name); fai_destroy(idx);
-				return 0;
-			}
-			++l1; len += l2;
-			if (state == 1) line_len = l1, line_blen = l2, state = 0;
-			else if (state == 0) {
-				if (l1 != line_len || l2 != line_blen) state = 2;
-			}
-		}
-	}
-	fai_insert_index(idx, name, len, line_len, line_blen, offset);
-	free(name);
-	return idx;
-}
-
-void fai_save(const faidx_t *fai, FILE *fp)
-{
-	khint_t k;
-	int i;
-	for (i = 0; i < fai->n; ++i) {
-		faidx1_t x;
-		k = kh_get(s, fai->hash, fai->name[i]);
-		x = kh_value(fai->hash, k);
-#ifdef _WIN32
-		fprintf(fp, "%s\t%d\t%ld\t%d\t%d\n", fai->name[i], (int)x.len, (long)x.offset, (int)x.line_blen, (int)x.line_len);
-#else
-		fprintf(fp, "%s\t%d\t%lld\t%d\t%d\n", fai->name[i], (int)x.len, (long long)x.offset, (int)x.line_blen, (int)x.line_len);
-#endif
-	}
-}
-
-faidx_t *fai_read(FILE *fp)
-{
-	faidx_t *fai;
-	char *buf, *p;
-	int len, line_len, line_blen;
-#ifdef _WIN32
-	long offset;
-#else
-	long long offset;
-#endif
-	fai = (faidx_t*)calloc(1, sizeof(faidx_t));
-	fai->hash = kh_init(s);
-	buf = (char*)calloc(0x10000, 1);
-	while (!feof(fp) && fgets(buf, 0x10000, fp)) {
-		for (p = buf; *p && isgraph(*p); ++p);
-		*p = 0; ++p;
-#ifdef _WIN32
-		sscanf(p, "%d%ld%d%d", &len, &offset, &line_blen, &line_len);
-#else
-		sscanf(p, "%d%lld%d%d", &len, &offset, &line_blen, &line_len);
-#endif
-		fai_insert_index(fai, buf, len, line_len, line_blen, offset);
-	}
-	free(buf);
-	return fai;
-}
+#include <ctype.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <htslib/faidx.h>
 
-void fai_destroy(faidx_t *fai)
+static void error(const char *format, ...)
 {
-	int i;
-	for (i = 0; i < fai->n; ++i) free(fai->name[i]);
-	free(fai->name);
-	kh_destroy(s, fai->hash);
-	if (fai->rz) razf_close(fai->rz);
-	free(fai);
+    if ( format )
+    {
+        va_list ap;
+        va_start(ap, format);
+        vfprintf(pysamerr, format, ap);
+        va_end(ap);
+    }
+    else
+    {
+        fprintf(pysamerr, "\n");
+        fprintf(pysamerr, "Usage:   samtools faidx <file.fa|file.fa.gz> [<reg> [...]]\n");
+        fprintf(pysamerr, "\n");
+    }
+    exit(-1);
 }
 
-int fai_build(const char *fn)
-{
-	char *str;
-	RAZF *rz;
-	FILE *fp;
-	faidx_t *fai;
-	str = (char*)calloc(strlen(fn) + 5, 1);
-	sprintf(str, "%s.fai", fn);
-	rz = razf_open(fn, "r");
-	if (rz == 0) {
-		fprintf(pysamerr, "[fai_build] fail to open the FASTA file %s\n",fn);
-		free(str);
-		return -1;
-	}
-	fai = fai_build_core(rz);
-	razf_close(rz);
-	fp = fopen(str, "wb");
-	if (fp == 0) {
-		fprintf(pysamerr, "[fai_build] fail to write FASTA index %s\n",str);
-		fai_destroy(fai); free(str);
-		return -1;
-	}
-	fai_save(fai, fp);
-	fclose(fp);
-	free(str);
-	fai_destroy(fai);
-	return 0;
-}
 
-#ifdef _USE_KNETFILE
-FILE *download_and_open(const char *fn)
+int faidx_main(int argc, char *argv[])
 {
-    const int buf_size = 1 * 1024 * 1024;
-    uint8_t *buf;
-    FILE *fp;
-    knetFile *fp_remote;
-    const char *url = fn;
-    const char *p;
-    int l = strlen(fn);
-    for (p = fn + l - 1; p >= fn; --p)
-        if (*p == '/') break;
-    fn = p + 1;
-
-    // First try to open a local copy
-    fp = fopen(fn, "r");
-    if (fp)
-        return fp;
-
-    // If failed, download from remote and open
-    fp_remote = knet_open(url, "rb");
-    if (fp_remote == 0) {
-        fprintf(pysamerr, "[download_from_remote] fail to open remote file %s\n",url);
-        return NULL;
+    int c;
+    while((c  = getopt(argc, argv, "h")) >= 0)
+    {
+        switch(c)
+        {
+            case 'h':
+            default:
+                error(NULL);
+        }
     }
-    if ((fp = fopen(fn, "wb")) == 0) {
-        fprintf(pysamerr, "[download_from_remote] fail to create file in the working directory %s\n",fn);
-        knet_close(fp_remote);
-        return NULL;
+    if ( argc==optind )
+        error(NULL);
+    if ( argc==2 )
+    {
+        fai_build(argv[optind]);
+        return 0;
     }
-    buf = (uint8_t*)calloc(buf_size, 1);
-    while ((l = knet_read(fp_remote, buf, buf_size)) != 0)
-        fwrite(buf, 1, l, fp);
-    free(buf);
-    fclose(fp);
-    knet_close(fp_remote);
-
-    return fopen(fn, "r");
-}
-#endif
 
-faidx_t *fai_load(const char *fn)
-{
-	char *str;
-	FILE *fp;
-	faidx_t *fai;
-	str = (char*)calloc(strlen(fn) + 5, 1);
-	sprintf(str, "%s.fai", fn);
+    faidx_t *fai = fai_load(argv[optind]);
+    if ( !fai ) error("Could not load fai index of %s\n", argv[optind]);
 
-#ifdef _USE_KNETFILE
-    if (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn)
+    while ( ++optind<argc )
     {
-        fp = download_and_open(str);
-        if ( !fp )
+        printf(">%s\n", argv[optind]);
+        int i, j, seq_len;
+        char *seq = fai_fetch(fai, argv[optind], &seq_len);
+        if ( seq_len < 0 ) error("Failed to fetch sequence in %s\n", argv[optind]);
+        for (i=0; i<seq_len; i+=60)
         {
-            fprintf(pysamerr, "[fai_load] failed to open remote FASTA index %s\n", str);
-            free(str);
-            return 0;
+            for (j=0; j<60 && i+j<seq_len; j++)
+                putchar(seq[i+j]);
+            putchar('\n');
         }
+        free(seq);
     }
-    else
-#endif
-        fp = fopen(str, "rb");
-	if (fp == 0) {
-		fprintf(pysamerr, "[fai_load] build FASTA index.\n");
-		fai_build(fn);
-		fp = fopen(str, "rb");
-		if (fp == 0) {
-			fprintf(pysamerr, "[fai_load] fail to open FASTA index.\n");
-			free(str);
-			return 0;
-		}
-	}
-
-	fai = fai_read(fp);
-	fclose(fp);
-
-	fai->rz = razf_open(fn, "rb");
-	free(str);
-	if (fai->rz == 0) {
-		fprintf(pysamerr, "[fai_load] fail to open FASTA file.\n");
-		return 0;
-	}
-	return fai;
-}
-
-char *fai_fetch(const faidx_t *fai, const char *str, int *len)
-{
-	char *s, c;
-	int i, l, k, name_end;
-	khiter_t iter;
-	faidx1_t val;
-	khash_t(s) *h;
-	int beg, end;
-
-	beg = end = -1;
-	h = fai->hash;
-	name_end = l = strlen(str);
-	s = (char*)malloc(l+1);
-	// remove space
-	for (i = k = 0; i < l; ++i)
-		if (!isspace(str[i])) s[k++] = str[i];
-	s[k] = 0; l = k;
-	// determine the sequence name
-	for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
-	if (i >= 0) name_end = i;
-	if (name_end < l) { // check if this is really the end
-		int n_hyphen = 0;
-		for (i = name_end + 1; i < l; ++i) {
-			if (s[i] == '-') ++n_hyphen;
-			else if (!isdigit(s[i]) && s[i] != ',') break;
-		}
-		if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
-		s[name_end] = 0;
-		iter = kh_get(s, h, s);
-		if (iter == kh_end(h)) { // cannot find the sequence name
-			iter = kh_get(s, h, str); // try str as the name
-			if (iter == kh_end(h)) {
-				*len = 0;
-			free(s); return 0;
-			} else s[name_end] = ':', name_end = l;
-		}
-	} else iter = kh_get(s, h, str);
-	if(iter == kh_end(h)) {
-		fprintf(pysamerr, "[fai_fetch] Warning - Reference %s not found in FASTA file, returning empty sequence\n", str);
-		free(s);
-		return 0;
-	};
-	val = kh_value(h, iter);
-	// parse the interval
-	if (name_end < l) {
-		for (i = k = name_end + 1; i < l; ++i)
-			if (s[i] != ',') s[k++] = s[i];
-		s[k] = 0;
-		beg = atoi(s + name_end + 1);
-		for (i = name_end + 1; i != k; ++i) if (s[i] == '-') break;
-		end = i < k? atoi(s + i + 1) : val.len;
-		if (beg > 0) --beg;
-	} else beg = 0, end = val.len;
-	if (beg >= val.len) beg = val.len;
-	if (end >= val.len) end = val.len;
-	if (beg > end) beg = end;
-	free(s);
-
-	// now retrieve the sequence
-	l = 0;
-	s = (char*)malloc(end - beg + 2);
-	razf_seek(fai->rz, val.offset + beg / val.line_blen * val.line_len + beg % val.line_blen, SEEK_SET);
-	while (razf_read(fai->rz, &c, 1) == 1 && l < end - beg && !fai->rz->z_err)
-		if (isgraph(c)) s[l++] = c;
-	s[l] = '\0';
-	*len = l;
-	return s;
-}
-
-int faidx_main(int argc, char *argv[])
-{
-	if (argc == 1) {
-		fprintf(pysamerr, "Usage: faidx <in.fasta> [<reg> [...]]\n");
-		return 1;
-	} else {
-		if (argc == 2) fai_build(argv[1]);
-		else {
-			int i, j, k, l;
-			char *s;
-			faidx_t *fai;
-			fai = fai_load(argv[1]);
-			if (fai == 0) return 1;
-			for (i = 2; i != argc; ++i) {
-				printf(">%s\n", argv[i]);
-				s = fai_fetch(fai, argv[i], &l);
-				for (j = 0; j < l; j += 60) {
-					for (k = 0; k < 60 && k < l - j; ++k)
-						putchar(s[j + k]);
-					putchar('\n');
-				}
-				free(s);
-			}
-			fai_destroy(fai);
-		}
-	}
-	return 0;
-}
-
-int faidx_fetch_nseq(const faidx_t *fai) 
-{
-	return fai->n;
-}
-
-char *faidx_fetch_seq(const faidx_t *fai, char *c_name, int p_beg_i, int p_end_i, int *len)
-{
-	int l;
-	char c;
-    khiter_t iter;
-    faidx1_t val;
-	char *seq=NULL;
-
-    // Adjust position
-    iter = kh_get(s, fai->hash, c_name);
-    if(iter == kh_end(fai->hash)) return 0;
-    val = kh_value(fai->hash, iter);
-	if(p_end_i < p_beg_i) p_beg_i = p_end_i;
-    if(p_beg_i < 0) p_beg_i = 0;
-    else if(val.len <= p_beg_i) p_beg_i = val.len - 1;
-    if(p_end_i < 0) p_end_i = 0;
-    else if(val.len <= p_end_i) p_end_i = val.len - 1;
+    fai_destroy(fai);
 
-    // Now retrieve the sequence 
-	l = 0;
-	seq = (char*)malloc(p_end_i - p_beg_i + 2);
-	razf_seek(fai->rz, val.offset + p_beg_i / val.line_blen * val.line_len + p_beg_i % val.line_blen, SEEK_SET);
-	while (razf_read(fai->rz, &c, 1) == 1 && l < p_end_i - p_beg_i + 1)
-		if (isgraph(c)) seq[l++] = c;
-	seq[l] = '\0';
-	*len = l;
-	return seq;
+    return 0;
 }
 
-#ifdef FAIDX_MAIN
-int main(int argc, char *argv[]) { return faidx_main(argc, argv); }
-#endif
diff --git a/samtools/faidx.h b/samtools/faidx.h
deleted file mode 100644
index 1fb1b1f..0000000
--- a/samtools/faidx.h
+++ /dev/null
@@ -1,103 +0,0 @@
-/* The MIT License
-
-   Copyright (c) 2008 Genome Research Ltd (GRL).
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-/* Contact: Heng Li <lh3 at sanger.ac.uk> */
-
-#ifndef FAIDX_H
-#define FAIDX_H
-
-/*!
-  @header
-
-  Index FASTA files and extract subsequence.
-
-  @copyright The Wellcome Trust Sanger Institute.
- */
-
-struct __faidx_t;
-typedef struct __faidx_t faidx_t;
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-	/*!
-	  @abstract   Build index for a FASTA or razip compressed FASTA file.
-	  @param  fn  FASTA file name
-	  @return     0 on success; or -1 on failure
-	  @discussion File "fn.fai" will be generated.
-	 */
-	int fai_build(const char *fn);
-
-	/*!
-	  @abstract    Distroy a faidx_t struct.
-	  @param  fai  Pointer to the struct to be destroyed
-	 */
-	void fai_destroy(faidx_t *fai);
-
-	/*!
-	  @abstract   Load index from "fn.fai".
-	  @param  fn  File name of the FASTA file
-	 */
-	faidx_t *fai_load(const char *fn);
-
-	/*!
-	  @abstract    Fetch the sequence in a region.
-	  @param  fai  Pointer to the faidx_t struct
-	  @param  reg  Region in the format "chr2:20,000-30,000"
-	  @param  len  Length of the region
-	  @return      Pointer to the sequence; null on failure
-
-	  @discussion The returned sequence is allocated by malloc family
-	  and should be destroyed by end users by calling free() on it.
-	 */
-	char *fai_fetch(const faidx_t *fai, const char *reg, int *len);
-
-	/*!
-	  @abstract	   Fetch the number of sequences. 
-	  @param  fai  Pointer to the faidx_t struct
-	  @return	   The number of sequences
-	 */
-	int faidx_fetch_nseq(const faidx_t *fai);
-
-	/*!
-	  @abstract    Fetch the sequence in a region.
-	  @param  fai  Pointer to the faidx_t struct
-	  @param  c_name Region name
-	  @param  p_beg_i  Beginning position number (zero-based)
-	  @param  p_end_i  End position number (zero-based)
-	  @param  len  Length of the region
-	  @return      Pointer to the sequence; null on failure
-
-	  @discussion The returned sequence is allocated by malloc family
-	  and should be destroyed by end users by calling free() on it.
-	 */
-	char *faidx_fetch_seq(const faidx_t *fai, char *c_name, int p_beg_i, int p_end_i, int *len);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/samtools/kaln.c.pysam.c b/samtools/kaln.c.pysam.c
index 953a214..1922cc1 100644
--- a/samtools/kaln.c.pysam.c
+++ b/samtools/kaln.c.pysam.c
@@ -2,7 +2,7 @@
 
 /* The MIT License
 
-   Copyright (c) 2003-2006, 2008, 2009, by Heng Li <lh3lh3 at gmail.com>
+   Copyright (C) 2003-2006, 2008-2010 by Heng Li <lh3lh3 at gmail.com>
 
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
diff --git a/samtools/kaln.h b/samtools/kaln.h
index 1ece132..8f4a2c6 100644
--- a/samtools/kaln.h
+++ b/samtools/kaln.h
@@ -1,6 +1,6 @@
 /* The MIT License
 
-   Copyright (c) 2003-2006, 2008, 2009 by Heng Li <lh3 at live.co.uk>
+   Copyright (C) 2003-2006, 2008-2010 by Heng Li <lh3 at live.co.uk>
 
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
diff --git a/samtools/khash.h b/samtools/khash.h
deleted file mode 100644
index e2a9d7a..0000000
--- a/samtools/khash.h
+++ /dev/null
@@ -1,528 +0,0 @@
-/* The MIT License
-
-   Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor at live.co.uk>
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-/*
-  An example:
-
-#include "khash.h"
-KHASH_MAP_INIT_INT(32, char)
-int main() {
-	int ret, is_missing;
-	khiter_t k;
-	khash_t(32) *h = kh_init(32);
-	k = kh_put(32, h, 5, &ret);
-	if (!ret) kh_del(32, h, k);
-	kh_value(h, k) = 10;
-	k = kh_get(32, h, 10);
-	is_missing = (k == kh_end(h));
-	k = kh_get(32, h, 5);
-	kh_del(32, h, k);
-	for (k = kh_begin(h); k != kh_end(h); ++k)
-		if (kh_exist(h, k)) kh_value(h, k) = 1;
-	kh_destroy(32, h);
-	return 0;
-}
-*/
-
-/*
-  2011-02-14 (0.2.5):
-
-    * Allow to declare global functions.
-
-  2009-09-26 (0.2.4):
-
-    * Improve portability
-
-  2008-09-19 (0.2.3):
-
-	* Corrected the example
-	* Improved interfaces
-
-  2008-09-11 (0.2.2):
-
-	* Improved speed a little in kh_put()
-
-  2008-09-10 (0.2.1):
-
-	* Added kh_clear()
-	* Fixed a compiling error
-
-  2008-09-02 (0.2.0):
-
-	* Changed to token concatenation which increases flexibility.
-
-  2008-08-31 (0.1.2):
-
-	* Fixed a bug in kh_get(), which has not been tested previously.
-
-  2008-08-31 (0.1.1):
-
-	* Added destructor
-*/
-
-
-#ifndef __AC_KHASH_H
-#define __AC_KHASH_H
-
-/*!
-  @header
-
-  Generic hash table library.
-
-  @copyright Heng Li
- */
-
-#define AC_VERSION_KHASH_H "0.2.5"
-
-#include <stdlib.h>
-#include <string.h>
-#include <limits.h>
-
-/* compipler specific configuration */
-
-#if UINT_MAX == 0xffffffffu
-typedef unsigned int khint32_t;
-#elif ULONG_MAX == 0xffffffffu
-typedef unsigned long khint32_t;
-#endif
-
-#if ULONG_MAX == ULLONG_MAX
-typedef unsigned long khint64_t;
-#else
-typedef unsigned long long khint64_t;
-#endif
-
-#ifdef _MSC_VER
-#define inline __inline
-#endif
-
-typedef khint32_t khint_t;
-typedef khint_t khiter_t;
-
-#define __ac_HASH_PRIME_SIZE 32
-static const khint32_t __ac_prime_list[__ac_HASH_PRIME_SIZE] =
-{
-  0ul,          3ul,          11ul,         23ul,         53ul,
-  97ul,         193ul,        389ul,        769ul,        1543ul,
-  3079ul,       6151ul,       12289ul,      24593ul,      49157ul,
-  98317ul,      196613ul,     393241ul,     786433ul,     1572869ul,
-  3145739ul,    6291469ul,    12582917ul,   25165843ul,   50331653ul,
-  100663319ul,  201326611ul,  402653189ul,  805306457ul,  1610612741ul,
-  3221225473ul, 4294967291ul
-};
-
-#define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
-#define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
-#define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
-#define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
-#define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
-#define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
-#define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
-
-static const double __ac_HASH_UPPER = 0.77;
-
-#define KHASH_DECLARE(name, khkey_t, khval_t)		 					\
-	typedef struct {													\
-		khint_t n_buckets, size, n_occupied, upper_bound;				\
-		khint32_t *flags;												\
-		khkey_t *keys;													\
-		khval_t *vals;													\
-	} kh_##name##_t;													\
-	extern kh_##name##_t *kh_init_##name(void);								\
-	extern void kh_destroy_##name(kh_##name##_t *h);					\
-	extern void kh_clear_##name(kh_##name##_t *h);						\
-	extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); 	\
-	extern void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
-	extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
-	extern void kh_del_##name(kh_##name##_t *h, khint_t x);
-
-#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
-	typedef struct {													\
-		khint_t n_buckets, size, n_occupied, upper_bound;				\
-		khint32_t *flags;												\
-		khkey_t *keys;													\
-		khval_t *vals;													\
-	} kh_##name##_t;													\
-	SCOPE kh_##name##_t *kh_init_##name(void) {								\
-		return (kh_##name##_t*)calloc(1, sizeof(kh_##name##_t));		\
-	}																	\
-	SCOPE void kh_destroy_##name(kh_##name##_t *h)						\
-	{																	\
-		if (h) {														\
-			free(h->keys); free(h->flags);								\
-			free(h->vals);												\
-			free(h);													\
-		}																\
-	}																	\
-	SCOPE void kh_clear_##name(kh_##name##_t *h)						\
-	{																	\
-		if (h && h->flags) {											\
-			memset(h->flags, 0xaa, ((h->n_buckets>>4) + 1) * sizeof(khint32_t)); \
-			h->size = h->n_occupied = 0;								\
-		}																\
-	}																	\
-	SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) 	\
-	{																	\
-		if (h->n_buckets) {												\
-			khint_t inc, k, i, last;									\
-			k = __hash_func(key); i = k % h->n_buckets;					\
-			inc = 1 + k % (h->n_buckets - 1); last = i;					\
-			while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
-				if (i + inc >= h->n_buckets) i = i + inc - h->n_buckets; \
-				else i += inc;											\
-				if (i == last) return h->n_buckets;						\
-			}															\
-			return __ac_iseither(h->flags, i)? h->n_buckets : i;		\
-		} else return 0;												\
-	}																	\
-	SCOPE void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
-	{																	\
-		khint32_t *new_flags = 0;										\
-		khint_t j = 1;													\
-		{																\
-			khint_t t = __ac_HASH_PRIME_SIZE - 1;						\
-			while (__ac_prime_list[t] > new_n_buckets) --t;				\
-			new_n_buckets = __ac_prime_list[t+1];						\
-			if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0;	\
-			else {														\
-				new_flags = (khint32_t*)malloc(((new_n_buckets>>4) + 1) * sizeof(khint32_t));	\
-				memset(new_flags, 0xaa, ((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \
-				if (h->n_buckets < new_n_buckets) {						\
-					h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
-					if (kh_is_map)										\
-						h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
-				}														\
-			}															\
-		}																\
-		if (j) {														\
-			for (j = 0; j != h->n_buckets; ++j) {						\
-				if (__ac_iseither(h->flags, j) == 0) {					\
-					khkey_t key = h->keys[j];							\
-					khval_t val;										\
-					if (kh_is_map) val = h->vals[j];					\
-					__ac_set_isdel_true(h->flags, j);					\
-					while (1) {											\
-						khint_t inc, k, i;								\
-						k = __hash_func(key);							\
-						i = k % new_n_buckets;							\
-						inc = 1 + k % (new_n_buckets - 1);				\
-						while (!__ac_isempty(new_flags, i)) {			\
-							if (i + inc >= new_n_buckets) i = i + inc - new_n_buckets; \
-							else i += inc;								\
-						}												\
-						__ac_set_isempty_false(new_flags, i);			\
-						if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { \
-							{ khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
-							if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
-							__ac_set_isdel_true(h->flags, i);			\
-						} else {										\
-							h->keys[i] = key;							\
-							if (kh_is_map) h->vals[i] = val;			\
-							break;										\
-						}												\
-					}													\
-				}														\
-			}															\
-			if (h->n_buckets > new_n_buckets) {							\
-				h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
-				if (kh_is_map)											\
-					h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
-			}															\
-			free(h->flags);												\
-			h->flags = new_flags;										\
-			h->n_buckets = new_n_buckets;								\
-			h->n_occupied = h->size;									\
-			h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
-		}																\
-	}																	\
-	SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
-	{																	\
-		khint_t x;														\
-		if (h->n_occupied >= h->upper_bound) {							\
-			if (h->n_buckets > (h->size<<1)) kh_resize_##name(h, h->n_buckets - 1); \
-			else kh_resize_##name(h, h->n_buckets + 1);					\
-		}																\
-		{																\
-			khint_t inc, k, i, site, last;								\
-			x = site = h->n_buckets; k = __hash_func(key); i = k % h->n_buckets; \
-			if (__ac_isempty(h->flags, i)) x = i;						\
-			else {														\
-				inc = 1 + k % (h->n_buckets - 1); last = i;				\
-				while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
-					if (__ac_isdel(h->flags, i)) site = i;				\
-					if (i + inc >= h->n_buckets) i = i + inc - h->n_buckets; \
-					else i += inc;										\
-					if (i == last) { x = site; break; }					\
-				}														\
-				if (x == h->n_buckets) {								\
-					if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
-					else x = i;											\
-				}														\
-			}															\
-		}																\
-		if (__ac_isempty(h->flags, x)) {								\
-			h->keys[x] = key;											\
-			__ac_set_isboth_false(h->flags, x);							\
-			++h->size; ++h->n_occupied;									\
-			*ret = 1;													\
-		} else if (__ac_isdel(h->flags, x)) {							\
-			h->keys[x] = key;											\
-			__ac_set_isboth_false(h->flags, x);							\
-			++h->size;													\
-			*ret = 2;													\
-		} else *ret = 0;												\
-		return x;														\
-	}																	\
-	SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x)				\
-	{																	\
-		if (x != h->n_buckets && !__ac_iseither(h->flags, x)) {			\
-			__ac_set_isdel_true(h->flags, x);							\
-			--h->size;													\
-		}																\
-	}
-
-#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
-	KHASH_INIT2(name, static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
-
-/* --- BEGIN OF HASH FUNCTIONS --- */
-
-/*! @function
-  @abstract     Integer hash function
-  @param  key   The integer [khint32_t]
-  @return       The hash value [khint_t]
- */
-#define kh_int_hash_func(key) (khint32_t)(key)
-/*! @function
-  @abstract     Integer comparison function
- */
-#define kh_int_hash_equal(a, b) ((a) == (b))
-/*! @function
-  @abstract     64-bit integer hash function
-  @param  key   The integer [khint64_t]
-  @return       The hash value [khint_t]
- */
-#define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11)
-/*! @function
-  @abstract     64-bit integer comparison function
- */
-#define kh_int64_hash_equal(a, b) ((a) == (b))
-/*! @function
-  @abstract     const char* hash function
-  @param  s     Pointer to a null terminated string
-  @return       The hash value
- */
-static inline khint_t __ac_X31_hash_string(const char *s)
-{
-	khint_t h = *s;
-	if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
-	return h;
-}
-/*! @function
-  @abstract     Another interface to const char* hash function
-  @param  key   Pointer to a null terminated string [const char*]
-  @return       The hash value [khint_t]
- */
-#define kh_str_hash_func(key) __ac_X31_hash_string(key)
-/*! @function
-  @abstract     Const char* comparison function
- */
-#define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
-
-/* --- END OF HASH FUNCTIONS --- */
-
-/* Other necessary macros... */
-
-/*!
-  @abstract Type of the hash table.
-  @param  name  Name of the hash table [symbol]
- */
-#define khash_t(name) kh_##name##_t
-
-/*! @function
-  @abstract     Initiate a hash table.
-  @param  name  Name of the hash table [symbol]
-  @return       Pointer to the hash table [khash_t(name)*]
- */
-#define kh_init(name) kh_init_##name()
-
-/*! @function
-  @abstract     Destroy a hash table.
-  @param  name  Name of the hash table [symbol]
-  @param  h     Pointer to the hash table [khash_t(name)*]
- */
-#define kh_destroy(name, h) kh_destroy_##name(h)
-
-/*! @function
-  @abstract     Reset a hash table without deallocating memory.
-  @param  name  Name of the hash table [symbol]
-  @param  h     Pointer to the hash table [khash_t(name)*]
- */
-#define kh_clear(name, h) kh_clear_##name(h)
-
-/*! @function
-  @abstract     Resize a hash table.
-  @param  name  Name of the hash table [symbol]
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @param  s     New size [khint_t]
- */
-#define kh_resize(name, h, s) kh_resize_##name(h, s)
-
-/*! @function
-  @abstract     Insert a key to the hash table.
-  @param  name  Name of the hash table [symbol]
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @param  k     Key [type of keys]
-  @param  r     Extra return code: 0 if the key is present in the hash table;
-                1 if the bucket is empty (never used); 2 if the element in
-				the bucket has been deleted [int*]
-  @return       Iterator to the inserted element [khint_t]
- */
-#define kh_put(name, h, k, r) kh_put_##name(h, k, r)
-
-/*! @function
-  @abstract     Retrieve a key from the hash table.
-  @param  name  Name of the hash table [symbol]
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @param  k     Key [type of keys]
-  @return       Iterator to the found element, or kh_end(h) is the element is absent [khint_t]
- */
-#define kh_get(name, h, k) kh_get_##name(h, k)
-
-/*! @function
-  @abstract     Remove a key from the hash table.
-  @param  name  Name of the hash table [symbol]
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @param  k     Iterator to the element to be deleted [khint_t]
- */
-#define kh_del(name, h, k) kh_del_##name(h, k)
-
-
-/*! @function
-  @abstract     Test whether a bucket contains data.
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @param  x     Iterator to the bucket [khint_t]
-  @return       1 if containing data; 0 otherwise [int]
- */
-#define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
-
-/*! @function
-  @abstract     Get key given an iterator
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @param  x     Iterator to the bucket [khint_t]
-  @return       Key [type of keys]
- */
-#define kh_key(h, x) ((h)->keys[x])
-
-/*! @function
-  @abstract     Get value given an iterator
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @param  x     Iterator to the bucket [khint_t]
-  @return       Value [type of values]
-  @discussion   For hash sets, calling this results in segfault.
- */
-#define kh_val(h, x) ((h)->vals[x])
-
-/*! @function
-  @abstract     Alias of kh_val()
- */
-#define kh_value(h, x) ((h)->vals[x])
-
-/*! @function
-  @abstract     Get the start iterator
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @return       The start iterator [khint_t]
- */
-#define kh_begin(h) (khint_t)(0)
-
-/*! @function
-  @abstract     Get the end iterator
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @return       The end iterator [khint_t]
- */
-#define kh_end(h) ((h)->n_buckets)
-
-/*! @function
-  @abstract     Get the number of elements in the hash table
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @return       Number of elements in the hash table [khint_t]
- */
-#define kh_size(h) ((h)->size)
-
-/*! @function
-  @abstract     Get the number of buckets in the hash table
-  @param  h     Pointer to the hash table [khash_t(name)*]
-  @return       Number of buckets in the hash table [khint_t]
- */
-#define kh_n_buckets(h) ((h)->n_buckets)
-
-/* More conenient interfaces */
-
-/*! @function
-  @abstract     Instantiate a hash set containing integer keys
-  @param  name  Name of the hash table [symbol]
- */
-#define KHASH_SET_INIT_INT(name)										\
-	KHASH_INIT(name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal)
-
-/*! @function
-  @abstract     Instantiate a hash map containing integer keys
-  @param  name  Name of the hash table [symbol]
-  @param  khval_t  Type of values [type]
- */
-#define KHASH_MAP_INIT_INT(name, khval_t)								\
-	KHASH_INIT(name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal)
-
-/*! @function
-  @abstract     Instantiate a hash map containing 64-bit integer keys
-  @param  name  Name of the hash table [symbol]
- */
-#define KHASH_SET_INIT_INT64(name)										\
-	KHASH_INIT(name, khint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal)
-
-/*! @function
-  @abstract     Instantiate a hash map containing 64-bit integer keys
-  @param  name  Name of the hash table [symbol]
-  @param  khval_t  Type of values [type]
- */
-#define KHASH_MAP_INIT_INT64(name, khval_t)								\
-	KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal)
-
-typedef const char *kh_cstr_t;
-/*! @function
-  @abstract     Instantiate a hash map containing const char* keys
-  @param  name  Name of the hash table [symbol]
- */
-#define KHASH_SET_INIT_STR(name)										\
-	KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal)
-
-/*! @function
-  @abstract     Instantiate a hash map containing const char* keys
-  @param  name  Name of the hash table [symbol]
-  @param  khval_t  Type of values [type]
- */
-#define KHASH_MAP_INIT_STR(name, khval_t)								\
-	KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal)
-
-#endif /* __AC_KHASH_H */
diff --git a/samtools/klist.h b/samtools/klist.h
deleted file mode 100644
index 71ef19a..0000000
--- a/samtools/klist.h
+++ /dev/null
@@ -1,96 +0,0 @@
-#ifndef _LH3_KLIST_H
-#define _LH3_KLIST_H
-
-#include <stdlib.h>
-
-#define KMEMPOOL_INIT(name, kmptype_t, kmpfree_f)						\
-	typedef struct {													\
-		size_t cnt, n, max;												\
-		kmptype_t **buf;												\
-	} kmp_##name##_t;													\
-	static inline kmp_##name##_t *kmp_init_##name(void) {					\
-		return calloc(1, sizeof(kmp_##name##_t));						\
-	}																	\
-	static inline void kmp_destroy_##name(kmp_##name##_t *mp) {			\
-		size_t k;														\
-		for (k = 0; k < mp->n; ++k) {									\
-			kmpfree_f(mp->buf[k]); free(mp->buf[k]);					\
-		}																\
-		free(mp->buf); free(mp);										\
-	}																	\
-	static inline kmptype_t *kmp_alloc_##name(kmp_##name##_t *mp) {		\
-		++mp->cnt;														\
-		if (mp->n == 0) return calloc(1, sizeof(kmptype_t));			\
-		return mp->buf[--mp->n];										\
-	}																	\
-	static inline void kmp_free_##name(kmp_##name##_t *mp, kmptype_t *p) { \
-		--mp->cnt;														\
-		if (mp->n == mp->max) {											\
-			mp->max = mp->max? mp->max<<1 : 16;							\
-			mp->buf = realloc(mp->buf, sizeof(void*) * mp->max);		\
-		}																\
-		mp->buf[mp->n++] = p;											\
-	}
-
-#define kmempool_t(name) kmp_##name##_t
-#define kmp_init(name) kmp_init_##name()
-#define kmp_destroy(name, mp) kmp_destroy_##name(mp)
-#define kmp_alloc(name, mp) kmp_alloc_##name(mp)
-#define kmp_free(name, mp, p) kmp_free_##name(mp, p)
-
-#define KLIST_INIT(name, kltype_t, kmpfree_t)							\
-	struct __kl1_##name {												\
-		kltype_t data;													\
-		struct __kl1_##name *next;										\
-	};																	\
-	typedef struct __kl1_##name kl1_##name;								\
-	KMEMPOOL_INIT(name, kl1_##name, kmpfree_t)							\
-	typedef struct {													\
-		kl1_##name *head, *tail;										\
-		kmp_##name##_t *mp;												\
-		size_t size;													\
-	} kl_##name##_t;													\
-	static inline kl_##name##_t *kl_init_##name(void) {						\
-		kl_##name##_t *kl = calloc(1, sizeof(kl_##name##_t));			\
-		kl->mp = kmp_init(name);										\
-		kl->head = kl->tail = kmp_alloc(name, kl->mp);					\
-		kl->head->next = 0;												\
-		return kl;														\
-	}																	\
-	static inline void kl_destroy_##name(kl_##name##_t *kl) {			\
-		kl1_##name *p;													\
-		for (p = kl->head; p != kl->tail; p = p->next)					\
-			kmp_free(name, kl->mp, p);									\
-		kmp_free(name, kl->mp, p);										\
-		kmp_destroy(name, kl->mp);										\
-		free(kl);														\
-	}																	\
-	static inline kltype_t *kl_pushp_##name(kl_##name##_t *kl) {		\
-		kl1_##name *q, *p = kmp_alloc(name, kl->mp);					\
-		q = kl->tail; p->next = 0; kl->tail->next = p; kl->tail = p;	\
-		++kl->size;														\
-		return &q->data;												\
-	}																	\
-	static inline int kl_shift_##name(kl_##name##_t *kl, kltype_t *d) { \
-		kl1_##name *p;													\
-		if (kl->head->next == 0) return -1;								\
-		--kl->size;														\
-		p = kl->head; kl->head = kl->head->next;						\
-		if (d) *d = p->data;											\
-		kmp_free(name, kl->mp, p);										\
-		return 0;														\
-	}
-
-#define kliter_t(name) kl1_##name
-#define klist_t(name) kl_##name##_t
-#define kl_val(iter) ((iter)->data)
-#define kl_next(iter) ((iter)->next)
-#define kl_begin(kl) ((kl)->head)
-#define kl_end(kl) ((kl)->tail)
-
-#define kl_init(name) kl_init_##name()
-#define kl_destroy(name, kl) kl_destroy_##name(kl)
-#define kl_pushp(name, kl) kl_pushp_##name(kl)
-#define kl_shift(name, kl, d) kl_shift_##name(kl, d)
-
-#endif
diff --git a/samtools/knetfile.c.pysam.c b/samtools/knetfile.c.pysam.c
deleted file mode 100644
index 25bbafe..0000000
--- a/samtools/knetfile.c.pysam.c
+++ /dev/null
@@ -1,634 +0,0 @@
-#include "pysam.h"
-
-/* The MIT License
-
-   Copyright (c) 2008 by Genome Research Ltd (GRL).
-                 2010 by Attractive Chaos <attractor at live.co.uk>
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-/* Probably I will not do socket programming in the next few years and
-   therefore I decide to heavily annotate this file, for Linux and
-   Windows as well.  -ac */
-
-#include <time.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/types.h>
-
-#ifndef _WIN32
-#include <netdb.h>
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#endif
-
-#include "knetfile.h"
-
-/* In winsock.h, the type of a socket is SOCKET, which is: "typedef
- * u_int SOCKET". An invalid SOCKET is: "(SOCKET)(~0)", or signed
- * integer -1. In knetfile.c, I use "int" for socket type
- * throughout. This should be improved to avoid confusion.
- *
- * In Linux/Mac, recv() and read() do almost the same thing. You can see
- * in the header file that netread() is simply an alias of read(). In
- * Windows, however, they are different and using recv() is mandatory.
- */
-
-/* This function tests if the file handler is ready for reading (or
- * writing if is_read==0). */
-static int socket_wait(int fd, int is_read)
-{
-	fd_set fds, *fdr = 0, *fdw = 0;
-	struct timeval tv;
-	int ret;
-	tv.tv_sec = 5; tv.tv_usec = 0; // 5 seconds time out
-	FD_ZERO(&fds);
-	FD_SET(fd, &fds);
-	if (is_read) fdr = &fds;
-	else fdw = &fds;
-	ret = select(fd+1, fdr, fdw, 0, &tv);
-#ifndef _WIN32
-	if (ret == -1) perror("select");
-#else
-	if (ret == 0)
-		fprintf(pysamerr, "select time-out\n");
-	else if (ret == SOCKET_ERROR)
-		fprintf(pysamerr, "select: %d\n", WSAGetLastError());
-#endif
-	return ret;
-}
-
-#ifndef _WIN32
-/* This function does not work with Windows due to the lack of
- * getaddrinfo() in winsock. It is addapted from an example in "Beej's
- * Guide to Network Programming" (http://beej.us/guide/bgnet/). */
-static int socket_connect(const char *host, const char *port)
-{
-#define __err_connect(func) do { perror(func); freeaddrinfo(res); return -1; } while (0)
-
-	int on = 1, fd;
-	struct linger lng = { 0, 0 };
-	struct addrinfo hints, *res = 0;
-	memset(&hints, 0, sizeof(struct addrinfo));
-	hints.ai_family = AF_UNSPEC;
-	hints.ai_socktype = SOCK_STREAM;
-	/* In Unix/Mac, getaddrinfo() is the most convenient way to get
-	 * server information. */
-	if (getaddrinfo(host, port, &hints, &res) != 0) __err_connect("getaddrinfo");
-	if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) __err_connect("socket");
-	/* The following two setsockopt() are used by ftplib
-	 * (http://nbpfaus.net/~pfau/ftplib/). I am not sure if they
-	 * necessary. */
-	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) __err_connect("setsockopt");
-	if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &lng, sizeof(lng)) == -1) __err_connect("setsockopt");
-	if (connect(fd, res->ai_addr, res->ai_addrlen) != 0) __err_connect("connect");
-	freeaddrinfo(res);
-	return fd;
-}
-#else
-/* MinGW's printf has problem with "%lld" */
-char *int64tostr(char *buf, int64_t x)
-{
-	int cnt;
-	int i = 0;
-	do {
-		buf[i++] = '0' + x % 10;
-		x /= 10;
-	} while (x);
-	buf[i] = 0;
-	for (cnt = i, i = 0; i < cnt/2; ++i) {
-		int c = buf[i]; buf[i] = buf[cnt-i-1]; buf[cnt-i-1] = c;
-	}
-	return buf;
-}
-
-int64_t strtoint64(const char *buf)
-{
-	int64_t x;
-	for (x = 0; *buf != '\0'; ++buf)
-		x = x * 10 + ((int64_t) *buf - 48);
-	return x;
-}
-/* In windows, the first thing is to establish the TCP connection. */
-int knet_win32_init()
-{
-	WSADATA wsaData;
-	return WSAStartup(MAKEWORD(2, 2), &wsaData);
-}
-void knet_win32_destroy()
-{
-	WSACleanup();
-}
-/* A slightly modfied version of the following function also works on
- * Mac (and presummably Linux). However, this function is not stable on
- * my Mac. It sometimes works fine but sometimes does not. Therefore for
- * non-Windows OS, I do not use this one. */
-static SOCKET socket_connect(const char *host, const char *port)
-{
-#define __err_connect(func)										\
-	do {														\
-		fprintf(pysamerr, "%s: %d\n", func, WSAGetLastError());	\
-		return -1;												\
-	} while (0)
-
-	int on = 1;
-	SOCKET fd;
-	struct linger lng = { 0, 0 };
-	struct sockaddr_in server;
-	struct hostent *hp = 0;
-	// open socket
-	if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) __err_connect("socket");
-	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) == -1) __err_connect("setsockopt");
-	if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&lng, sizeof(lng)) == -1) __err_connect("setsockopt");
-	// get host info
-	if (isalpha(host[0])) hp = gethostbyname(host);
-	else {
-		struct in_addr addr;
-		addr.s_addr = inet_addr(host);
-		hp = gethostbyaddr((char*)&addr, 4, AF_INET);
-	}
-	if (hp == 0) __err_connect("gethost");
-	// connect
-	server.sin_addr.s_addr = *((unsigned long*)hp->h_addr);
-	server.sin_family= AF_INET;
-	server.sin_port = htons(atoi(port));
-	if (connect(fd, (struct sockaddr*)&server, sizeof(server)) != 0) __err_connect("connect");
-	// freehostent(hp); // strangely in MSDN, hp is NOT freed (memory leak?!)
-	return fd;
-}
-#endif
-
-static off_t my_netread(int fd, void *buf, off_t len)
-{
-	off_t rest = len, curr, l = 0;
-	/* recv() and read() may not read the required length of data with
-	 * one call. They have to be called repeatedly. */
-	while (rest) {
-		if (socket_wait(fd, 1) <= 0) break; // socket is not ready for reading
-		curr = netread(fd, buf + l, rest);
-		/* According to the glibc manual, section 13.2, a zero returned
-		 * value indicates end-of-file (EOF), which should mean that
-		 * read() will not return zero if EOF has not been met but data
-		 * are not immediately available. */
-		if (curr == 0) break;
-		l += curr; rest -= curr;
-	}
-	return l;
-}
-
-/*************************
- * FTP specific routines *
- *************************/
-
-static int kftp_get_response(knetFile *ftp)
-{
-#ifndef _WIN32
-	unsigned char c;
-#else
-	char c;
-#endif
-	int n = 0;
-	char *p;
-	if (socket_wait(ftp->ctrl_fd, 1) <= 0) return 0;
-	while (netread(ftp->ctrl_fd, &c, 1)) { // FIXME: this is *VERY BAD* for unbuffered I/O
-		//fputc(c, pysamerr);
-		if (n >= ftp->max_response) {
-			ftp->max_response = ftp->max_response? ftp->max_response<<1 : 256;
-			ftp->response = realloc(ftp->response, ftp->max_response);
-		}
-		ftp->response[n++] = c;
-		if (c == '\n') {
-			if (n >= 4 && isdigit(ftp->response[0]) && isdigit(ftp->response[1]) && isdigit(ftp->response[2])
-				&& ftp->response[3] != '-') break;
-			n = 0;
-			continue;
-		}
-	}
-	if (n < 2) return -1;
-	ftp->response[n-2] = 0;
-	return strtol(ftp->response, &p, 0);
-}
-
-static int kftp_send_cmd(knetFile *ftp, const char *cmd, int is_get)
-{
-	if (socket_wait(ftp->ctrl_fd, 0) <= 0) return -1; // socket is not ready for writing
-	netwrite(ftp->ctrl_fd, cmd, strlen(cmd));
-	return is_get? kftp_get_response(ftp) : 0;
-}
-
-static int kftp_pasv_prep(knetFile *ftp)
-{
-	char *p;
-	int v[6];
-	kftp_send_cmd(ftp, "PASV\r\n", 1);
-	for (p = ftp->response; *p && *p != '('; ++p);
-	if (*p != '(') return -1;
-	++p;
-	sscanf(p, "%d,%d,%d,%d,%d,%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5]);
-	memcpy(ftp->pasv_ip, v, 4 * sizeof(int));
-	ftp->pasv_port = (v[4]<<8&0xff00) + v[5];
-	return 0;
-}
-
-
-static int kftp_pasv_connect(knetFile *ftp)
-{
-	char host[80], port[10];
-	if (ftp->pasv_port == 0) {
-		fprintf(pysamerr, "[kftp_pasv_connect] kftp_pasv_prep() is not called before hand.\n");
-		return -1;
-	}
-	sprintf(host, "%d.%d.%d.%d", ftp->pasv_ip[0], ftp->pasv_ip[1], ftp->pasv_ip[2], ftp->pasv_ip[3]);
-	sprintf(port, "%d", ftp->pasv_port);
-	ftp->fd = socket_connect(host, port);
-	if (ftp->fd == -1) return -1;
-	return 0;
-}
-
-int kftp_connect(knetFile *ftp)
-{
-	ftp->ctrl_fd = socket_connect(ftp->host, ftp->port);
-	if (ftp->ctrl_fd == -1) return -1;
-	kftp_get_response(ftp);
-	kftp_send_cmd(ftp, "USER anonymous\r\n", 1);
-	kftp_send_cmd(ftp, "PASS kftp@\r\n", 1);
-	kftp_send_cmd(ftp, "TYPE I\r\n", 1);
-	return 0;
-}
-
-int kftp_reconnect(knetFile *ftp)
-{
-	if (ftp->ctrl_fd != -1) {
-		netclose(ftp->ctrl_fd);
-		ftp->ctrl_fd = -1;
-	}
-	netclose(ftp->fd);
-	ftp->fd = -1;
-	return kftp_connect(ftp);
-}
-
-// initialize ->type, ->host, ->retr and ->size
-knetFile *kftp_parse_url(const char *fn, const char *mode)
-{
-	knetFile *fp;
-	char *p;
-	int l;
-	if (strstr(fn, "ftp://") != fn) return 0;
-	for (p = (char*)fn + 6; *p && *p != '/'; ++p);
-	if (*p != '/') return 0;
-	l = p - fn - 6;
-	fp = calloc(1, sizeof(knetFile));
-	fp->type = KNF_TYPE_FTP;
-	fp->fd = -1;
-	/* the Linux/Mac version of socket_connect() also recognizes a port
-	 * like "ftp", but the Windows version does not. */
-	fp->port = strdup("21");
-	fp->host = calloc(l + 1, 1);
-	if (strchr(mode, 'c')) fp->no_reconnect = 1;
-	strncpy(fp->host, fn + 6, l);
-	fp->retr = calloc(strlen(p) + 8, 1);
-	sprintf(fp->retr, "RETR %s\r\n", p);
-    fp->size_cmd = calloc(strlen(p) + 8, 1);
-    sprintf(fp->size_cmd, "SIZE %s\r\n", p);
-	fp->seek_offset = 0;
-	return fp;
-}
-// place ->fd at offset off
-int kftp_connect_file(knetFile *fp)
-{
-	int ret;
-	long long file_size;
-	if (fp->fd != -1) {
-		netclose(fp->fd);
-		if (fp->no_reconnect) kftp_get_response(fp);
-	}
-	kftp_pasv_prep(fp);
-    kftp_send_cmd(fp, fp->size_cmd, 1);
-#ifndef _WIN32
-    if ( sscanf(fp->response,"%*d %lld", &file_size) != 1 )
-    {
-        fprintf(pysamerr,"[kftp_connect_file] %s\n", fp->response);
-        return -1;
-    }
-#else
-	const char *p = fp->response;
-	while (*p != ' ') ++p;
-	while (*p < '0' || *p > '9') ++p;
-	file_size = strtoint64(p);
-#endif
-	fp->file_size = file_size;
-	if (fp->offset>=0) {
-		char tmp[32];
-#ifndef _WIN32
-		sprintf(tmp, "REST %lld\r\n", (long long)fp->offset);
-#else
-		strcpy(tmp, "REST ");
-		int64tostr(tmp + 5, fp->offset);
-		strcat(tmp, "\r\n");
-#endif
-		kftp_send_cmd(fp, tmp, 1);
-	}
-	kftp_send_cmd(fp, fp->retr, 0);
-	kftp_pasv_connect(fp);
-	ret = kftp_get_response(fp);
-	if (ret != 150) {
-		fprintf(pysamerr, "[kftp_connect_file] %s\n", fp->response);
-		netclose(fp->fd);
-		fp->fd = -1;
-		return -1;
-	}
-	fp->is_ready = 1;
-	return 0;
-}
-
-
-/**************************
- * HTTP specific routines *
- **************************/
-
-knetFile *khttp_parse_url(const char *fn, const char *mode)
-{
-	knetFile *fp;
-	char *p, *proxy, *q;
-	int l;
-	if (strstr(fn, "http://") != fn) return 0;
-	// set ->http_host
-	for (p = (char*)fn + 7; *p && *p != '/'; ++p);
-	l = p - fn - 7;
-	fp = calloc(1, sizeof(knetFile));
-	fp->http_host = calloc(l + 1, 1);
-	strncpy(fp->http_host, fn + 7, l);
-	fp->http_host[l] = 0;
-	for (q = fp->http_host; *q && *q != ':'; ++q);
-	if (*q == ':') *q++ = 0;
-	// get http_proxy
-	proxy = getenv("http_proxy");
-	// set ->host, ->port and ->path
-	if (proxy == 0) {
-		fp->host = strdup(fp->http_host); // when there is no proxy, server name is identical to http_host name.
-		fp->port = strdup(*q? q : "80");
-		fp->path = strdup(*p? p : "/");
-	} else {
-		fp->host = (strstr(proxy, "http://") == proxy)? strdup(proxy + 7) : strdup(proxy);
-		for (q = fp->host; *q && *q != ':'; ++q);
-		if (*q == ':') *q++ = 0; 
-		fp->port = strdup(*q? q : "80");
-		fp->path = strdup(fn);
-	}
-	fp->type = KNF_TYPE_HTTP;
-	fp->ctrl_fd = fp->fd = -1;
-	fp->seek_offset = 0;
-	return fp;
-}
-
-int khttp_connect_file(knetFile *fp)
-{
-	int ret, l = 0;
-	char *buf, *p;
-	if (fp->fd != -1) netclose(fp->fd);
-	fp->fd = socket_connect(fp->host, fp->port);
-	buf = calloc(0x10000, 1); // FIXME: I am lazy... But in principle, 64KB should be large enough.
-	l += sprintf(buf + l, "GET %s HTTP/1.0\r\nHost: %s\r\n", fp->path, fp->http_host);
-    l += sprintf(buf + l, "Range: bytes=%lld-\r\n", (long long)fp->offset);
-	l += sprintf(buf + l, "\r\n");
-	netwrite(fp->fd, buf, l);
-	l = 0;
-	while (netread(fp->fd, buf + l, 1)) { // read HTTP header; FIXME: bad efficiency
-		if (buf[l] == '\n' && l >= 3)
-			if (strncmp(buf + l - 3, "\r\n\r\n", 4) == 0) break;
-		++l;
-	}
-	buf[l] = 0;
-	if (l < 14) { // prematured header
-		netclose(fp->fd);
-		fp->fd = -1;
-		return -1;
-	}
-	ret = strtol(buf + 8, &p, 0); // HTTP return code
-	if (ret == 200 && fp->offset>0) { // 200 (complete result); then skip beginning of the file
-		off_t rest = fp->offset;
-		while (rest) {
-			off_t l = rest < 0x10000? rest : 0x10000;
-			rest -= my_netread(fp->fd, buf, l);
-		}
-	} else if (ret != 206 && ret != 200) {
-		free(buf);
-		fprintf(pysamerr, "[khttp_connect_file] fail to open file (HTTP code: %d).\n", ret);
-		netclose(fp->fd);
-		fp->fd = -1;
-		return -1;
-	}
-	free(buf);
-	fp->is_ready = 1;
-	return 0;
-}
-
-/********************
- * Generic routines *
- ********************/
-
-knetFile *knet_open(const char *fn, const char *mode)
-{
-	knetFile *fp = 0;
-	if (mode[0] != 'r') {
-		fprintf(pysamerr, "[kftp_open] only mode \"r\" is supported.\n");
-		return 0;
-	}
-	if (strstr(fn, "ftp://") == fn) {
-		fp = kftp_parse_url(fn, mode);
-		if (fp == 0) return 0;
-		if (kftp_connect(fp) == -1) {
-			knet_close(fp);
-			return 0;
-		}
-		kftp_connect_file(fp);
-	} else if (strstr(fn, "http://") == fn) {
-		fp = khttp_parse_url(fn, mode);
-		if (fp == 0) return 0;
-		khttp_connect_file(fp);
-	} else { // local file
-#ifdef _WIN32
-		/* In windows, O_BINARY is necessary. In Linux/Mac, O_BINARY may
-		 * be undefined on some systems, although it is defined on my
-		 * Mac and the Linux I have tested on. */
-		int fd = open(fn, O_RDONLY | O_BINARY);
-#else		
-		int fd = open(fn, O_RDONLY);
-#endif
-		if (fd == -1) {
-			perror("open");
-			return 0;
-		}
-		fp = (knetFile*)calloc(1, sizeof(knetFile));
-		fp->type = KNF_TYPE_LOCAL;
-		fp->fd = fd;
-		fp->ctrl_fd = -1;
-	}
-	if (fp && fp->fd == -1) {
-		knet_close(fp);
-		return 0;
-	}
-	return fp;
-}
-
-knetFile *knet_dopen(int fd, const char *mode)
-{
-	knetFile *fp = (knetFile*)calloc(1, sizeof(knetFile));
-	fp->type = KNF_TYPE_LOCAL;
-	fp->fd = fd;
-	return fp;
-}
-
-off_t knet_read(knetFile *fp, void *buf, off_t len)
-{
-	off_t l = 0;
-	if (fp->fd == -1) return 0;
-	if (fp->type == KNF_TYPE_FTP) {
-		if (fp->is_ready == 0) {
-			if (!fp->no_reconnect) kftp_reconnect(fp);
-			kftp_connect_file(fp);
-		}
-	} else if (fp->type == KNF_TYPE_HTTP) {
-		if (fp->is_ready == 0)
-			khttp_connect_file(fp);
-	}
-	if (fp->type == KNF_TYPE_LOCAL) { // on Windows, the following block is necessary; not on UNIX
-		off_t rest = len, curr;
-		while (rest) {
-			do {
-				curr = read(fp->fd, buf + l, rest);
-			} while (curr < 0 && EINTR == errno);
-			if (curr < 0) return -1;
-			if (curr == 0) break;
-			l += curr; rest -= curr;
-		}
-	} else l = my_netread(fp->fd, buf, len);
-	fp->offset += l;
-	return l;
-}
-
-off_t knet_seek(knetFile *fp, int64_t off, int whence)
-{
-	if (whence == SEEK_SET && off == fp->offset) return 0;
-	if (fp->type == KNF_TYPE_LOCAL) {
-		/* Be aware that lseek() returns the offset after seeking,
-		 * while fseek() returns zero on success. */
-		off_t offset = lseek(fp->fd, off, whence);
-		if (offset == -1) {
-            // Be silent, it is OK for knet_seek to fail when the file is streamed
-            // fprintf(pysamerr,"[knet_seek] %s\n", strerror(errno));
-			return -1;
-		}
-		fp->offset = offset;
-		return 0;
-	}
-    else if (fp->type == KNF_TYPE_FTP) 
-    {
-        if (whence==SEEK_CUR)
-            fp->offset += off;
-        else if (whence==SEEK_SET)
-            fp->offset = off;
-        else if ( whence==SEEK_END)
-            fp->offset = fp->file_size+off;
-		fp->is_ready = 0;
-		return 0;
-	} 
-    else if (fp->type == KNF_TYPE_HTTP) 
-    {
-		if (whence == SEEK_END) { // FIXME: can we allow SEEK_END in future?
-			fprintf(pysamerr, "[knet_seek] SEEK_END is not supported for HTTP. Offset is unchanged.\n");
-			errno = ESPIPE;
-			return -1;
-		}
-        if (whence==SEEK_CUR)
-            fp->offset += off;
-        else if (whence==SEEK_SET)
-            fp->offset = off;
-		fp->is_ready = 0;
-		return 0;
-	}
-	errno = EINVAL;
-    fprintf(pysamerr,"[knet_seek] %s\n", strerror(errno));
-	return -1;
-}
-
-int knet_close(knetFile *fp)
-{
-	if (fp == 0) return 0;
-	if (fp->ctrl_fd != -1) netclose(fp->ctrl_fd); // FTP specific
-	if (fp->fd != -1) {
-		/* On Linux/Mac, netclose() is an alias of close(), but on
-		 * Windows, it is an alias of closesocket(). */
-		if (fp->type == KNF_TYPE_LOCAL) close(fp->fd);
-		else netclose(fp->fd);
-	}
-	free(fp->host); free(fp->port);
-	free(fp->response); free(fp->retr); // FTP specific
-	free(fp->path); free(fp->http_host); // HTTP specific
-	free(fp);
-	return 0;
-}
-
-#ifdef KNETFILE_MAIN
-int main(void)
-{
-	char *buf;
-	knetFile *fp;
-	int type = 4, l;
-#ifdef _WIN32
-	knet_win32_init();
-#endif
-	buf = calloc(0x100000, 1);
-	if (type == 0) {
-		fp = knet_open("knetfile.c", "r");
-		knet_seek(fp, 1000, SEEK_SET);
-	} else if (type == 1) { // NCBI FTP, large file
-		fp = knet_open("ftp://ftp.ncbi.nih.gov/1000genomes/ftp/data/NA12878/alignment/NA12878.chrom6.SLX.SRP000032.2009_06.bam", "r");
-		knet_seek(fp, 2500000000ll, SEEK_SET);
-		l = knet_read(fp, buf, 255);
-	} else if (type == 2) {
-		fp = knet_open("ftp://ftp.sanger.ac.uk/pub4/treefam/tmp/index.shtml", "r");
-		knet_seek(fp, 1000, SEEK_SET);
-	} else if (type == 3) {
-		fp = knet_open("http://www.sanger.ac.uk/Users/lh3/index.shtml", "r");
-		knet_seek(fp, 1000, SEEK_SET);
-	} else if (type == 4) {
-		fp = knet_open("http://www.sanger.ac.uk/Users/lh3/ex1.bam", "r");
-		knet_read(fp, buf, 10000);
-		knet_seek(fp, 20000, SEEK_SET);
-		knet_seek(fp, 10000, SEEK_SET);
-		l = knet_read(fp, buf+10000, 10000000) + 10000;
-	}
-	if (type != 4 && type != 1) {
-		knet_read(fp, buf, 255);
-		buf[255] = 0;
-		printf("%s\n", buf);
-	} else write(fileno(stdout), buf, l);
-	knet_close(fp);
-	free(buf);
-	return 0;
-}
-#endif
diff --git a/samtools/knetfile.h b/samtools/knetfile.h
deleted file mode 100644
index 0a0e66f..0000000
--- a/samtools/knetfile.h
+++ /dev/null
@@ -1,75 +0,0 @@
-#ifndef KNETFILE_H
-#define KNETFILE_H
-
-#include <stdint.h>
-#include <fcntl.h>
-
-#ifndef _WIN32
-#define netread(fd, ptr, len) read(fd, ptr, len)
-#define netwrite(fd, ptr, len) write(fd, ptr, len)
-#define netclose(fd) close(fd)
-#else
-#include <winsock2.h>
-#define netread(fd, ptr, len) recv(fd, ptr, len, 0)
-#define netwrite(fd, ptr, len) send(fd, ptr, len, 0)
-#define netclose(fd) closesocket(fd)
-#endif
-
-// FIXME: currently I/O is unbuffered
-
-#define KNF_TYPE_LOCAL 1
-#define KNF_TYPE_FTP   2
-#define KNF_TYPE_HTTP  3
-
-typedef struct knetFile_s {
-	int type, fd;
-	int64_t offset;
-	char *host, *port;
-
-	// the following are for FTP only
-	int ctrl_fd, pasv_ip[4], pasv_port, max_response, no_reconnect, is_ready;
-	char *response, *retr, *size_cmd;
-	int64_t seek_offset; // for lazy seek
-    int64_t file_size;
-
-	// the following are for HTTP only
-	char *path, *http_host;
-} knetFile;
-
-#define knet_tell(fp) ((fp)->offset)
-#define knet_fileno(fp) ((fp)->fd)
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ifdef _WIN32
-	int knet_win32_init();
-	void knet_win32_destroy();
-#endif
-
-	knetFile *knet_open(const char *fn, const char *mode);
-
-	/* 
-	   This only works with local files.
-	 */
-	knetFile *knet_dopen(int fd, const char *mode);
-
-	/*
-	  If ->is_ready==0, this routine updates ->fd; otherwise, it simply
-	  reads from ->fd.
-	 */
-	off_t knet_read(knetFile *fp, void *buf, off_t len);
-
-	/*
-	  This routine only sets ->offset and ->is_ready=0. It does not
-	  communicate with the FTP server.
-	 */
-	off_t knet_seek(knetFile *fp, int64_t off, int whence);
-	int knet_close(knetFile *fp);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/samtools/kprobaln.c b/samtools/kprobaln.c
index 04e526a..c746803 100644
--- a/samtools/kprobaln.c
+++ b/samtools/kprobaln.c
@@ -1,6 +1,6 @@
 /* The MIT License
 
-   Copyright (c) 2003-2006, 2008-2010, by Heng Li <lh3lh3 at live.co.uk>
+   Copyright (C) 2003-2006, 2008-2010 by Heng Li <lh3lh3 at live.co.uk>
 
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
@@ -87,8 +87,8 @@ int kpa_glocal(const uint8_t *_ref, int l_ref, const uint8_t *_query, int l_quer
 	if (bw < abs(l_ref - l_query)) bw = abs(l_ref - l_query);
 	bw2 = bw * 2 + 1;
 	// allocate the forward and backward matrices f[][] and b[][] and the scaling array s[]
-	f = calloc(l_query+1, sizeof(void*));
-	if (is_backward) b = calloc(l_query+1, sizeof(void*));
+	f = calloc(l_query+1, sizeof(double*));
+	if (is_backward) b = calloc(l_query+1, sizeof(double*));
 	for (i = 0; i <= l_query; ++i) {    // FIXME: this will lead in segfault for l_query==0
 		f[i] = calloc(bw2 * 3 + 6, sizeof(double)); // FIXME: this is over-allocated for very short seqs
 		if (is_backward) b[i] = calloc(bw2 * 3 + 6, sizeof(double));
diff --git a/samtools/kprobaln.c.pysam.c b/samtools/kprobaln.c.pysam.c
index 045054a..63dad4c 100644
--- a/samtools/kprobaln.c.pysam.c
+++ b/samtools/kprobaln.c.pysam.c
@@ -2,7 +2,7 @@
 
 /* The MIT License
 
-   Copyright (c) 2003-2006, 2008-2010, by Heng Li <lh3lh3 at live.co.uk>
+   Copyright (C) 2003-2006, 2008-2010 by Heng Li <lh3lh3 at live.co.uk>
 
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
@@ -89,8 +89,8 @@ int kpa_glocal(const uint8_t *_ref, int l_ref, const uint8_t *_query, int l_quer
 	if (bw < abs(l_ref - l_query)) bw = abs(l_ref - l_query);
 	bw2 = bw * 2 + 1;
 	// allocate the forward and backward matrices f[][] and b[][] and the scaling array s[]
-	f = calloc(l_query+1, sizeof(void*));
-	if (is_backward) b = calloc(l_query+1, sizeof(void*));
+	f = calloc(l_query+1, sizeof(double*));
+	if (is_backward) b = calloc(l_query+1, sizeof(double*));
 	for (i = 0; i <= l_query; ++i) {    // FIXME: this will lead in segfault for l_query==0
 		f[i] = calloc(bw2 * 3 + 6, sizeof(double)); // FIXME: this is over-allocated for very short seqs
 		if (is_backward) b[i] = calloc(bw2 * 3 + 6, sizeof(double));
diff --git a/samtools/kprobaln.h b/samtools/kprobaln.h
index 0357dcc..50ae77b 100644
--- a/samtools/kprobaln.h
+++ b/samtools/kprobaln.h
@@ -1,6 +1,6 @@
 /* The MIT License
 
-   Copyright (c) 2003-2006, 2008, 2009 by Heng Li <lh3 at live.co.uk>
+   Copyright (C) 2003-2006, 2008-2010 by Heng Li <lh3 at live.co.uk>
 
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
diff --git a/samtools/kseq.h b/samtools/kseq.h
deleted file mode 100644
index a5cec7c..0000000
--- a/samtools/kseq.h
+++ /dev/null
@@ -1,235 +0,0 @@
-/* The MIT License
-
-   Copyright (c) 2008, 2009, 2011 Attractive Chaos <attractor at live.co.uk>
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-/* Last Modified: 05MAR2012 */
-
-#ifndef AC_KSEQ_H
-#define AC_KSEQ_H
-
-#include <ctype.h>
-#include <string.h>
-#include <stdlib.h>
-
-#define KS_SEP_SPACE 0 // isspace(): \t, \n, \v, \f, \r
-#define KS_SEP_TAB   1 // isspace() && !' '
-#define KS_SEP_LINE  2 // line separator: "\n" (Unix) or "\r\n" (Windows)
-#define KS_SEP_MAX   2
-
-#define __KS_TYPE(type_t)						\
-	typedef struct __kstream_t {				\
-		unsigned char *buf;						\
-		int begin, end, is_eof;					\
-		type_t f;								\
-	} kstream_t;
-
-#define ks_eof(ks) ((ks)->is_eof && (ks)->begin >= (ks)->end)
-#define ks_rewind(ks) ((ks)->is_eof = (ks)->begin = (ks)->end = 0)
-
-#define __KS_BASIC(type_t, __bufsize)								\
-	static inline kstream_t *ks_init(type_t f)						\
-	{																\
-		kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t));	\
-		ks->f = f;													\
-		ks->buf = (unsigned char*)malloc(__bufsize);				\
-		return ks;													\
-	}																\
-	static inline void ks_destroy(kstream_t *ks)					\
-	{																\
-		if (ks) {													\
-			free(ks->buf);											\
-			free(ks);												\
-		}															\
-	}
-
-#define __KS_GETC(__read, __bufsize)						\
-	static inline int ks_getc(kstream_t *ks)				\
-	{														\
-		if (ks->is_eof && ks->begin >= ks->end) return -1;	\
-		if (ks->begin >= ks->end) {							\
-			ks->begin = 0;									\
-			ks->end = __read(ks->f, ks->buf, __bufsize);	\
-			if (ks->end < __bufsize) ks->is_eof = 1;		\
-			if (ks->end == 0) return -1;					\
-		}													\
-		return (int)ks->buf[ks->begin++];					\
-	}
-
-#ifndef KSTRING_T
-#define KSTRING_T kstring_t
-typedef struct __kstring_t {
-	size_t l, m;
-	char *s;
-} kstring_t;
-#endif
-
-#ifndef kroundup32
-#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
-#endif
-
-#define __KS_GETUNTIL(__read, __bufsize)								\
-	static int ks_getuntil2(kstream_t *ks, int delimiter, kstring_t *str, int *dret, int append) \
-	{																	\
-		if (dret) *dret = 0;											\
-		str->l = append? str->l : 0;									\
-		if (ks->begin >= ks->end && ks->is_eof) return -1;				\
-		for (;;) {														\
-			int i;														\
-			if (ks->begin >= ks->end) {									\
-				if (!ks->is_eof) {										\
-					ks->begin = 0;										\
-					ks->end = __read(ks->f, ks->buf, __bufsize);		\
-					if (ks->end < __bufsize) ks->is_eof = 1;			\
-					if (ks->end == 0) break;							\
-				} else break;											\
-			}															\
-			if (delimiter == KS_SEP_LINE) { \
-				for (i = ks->begin; i < ks->end; ++i) \
-					if (ks->buf[i] == '\n') break; \
-			} else if (delimiter > KS_SEP_MAX) {						\
-				for (i = ks->begin; i < ks->end; ++i)					\
-					if (ks->buf[i] == delimiter) break;					\
-			} else if (delimiter == KS_SEP_SPACE) {						\
-				for (i = ks->begin; i < ks->end; ++i)					\
-					if (isspace(ks->buf[i])) break;						\
-			} else if (delimiter == KS_SEP_TAB) {						\
-				for (i = ks->begin; i < ks->end; ++i)					\
-					if (isspace(ks->buf[i]) && ks->buf[i] != ' ') break; \
-			} else i = 0; /* never come to here! */						\
-			if (str->m - str->l < (size_t)(i - ks->begin + 1)) {		\
-				str->m = str->l + (i - ks->begin) + 1;					\
-				kroundup32(str->m);										\
-				str->s = (char*)realloc(str->s, str->m);				\
-			}															\
-			memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin); \
-			str->l = str->l + (i - ks->begin);							\
-			ks->begin = i + 1;											\
-			if (i < ks->end) {											\
-				if (dret) *dret = ks->buf[i];							\
-				break;													\
-			}															\
-		}																\
-		if (str->s == 0) {												\
-			str->m = 1;													\
-			str->s = (char*)calloc(1, 1);								\
-		} else if (delimiter == KS_SEP_LINE && str->l > 1 && str->s[str->l-1] == '\r') --str->l; \
-		str->s[str->l] = '\0';											\
-		return str->l;													\
-	} \
-	static inline int ks_getuntil(kstream_t *ks, int delimiter, kstring_t *str, int *dret) \
-	{ return ks_getuntil2(ks, delimiter, str, dret, 0); }
-
-#define KSTREAM_INIT(type_t, __read, __bufsize) \
-	__KS_TYPE(type_t)							\
-	__KS_BASIC(type_t, __bufsize)				\
-	__KS_GETC(__read, __bufsize)				\
-	__KS_GETUNTIL(__read, __bufsize)
-
-#define kseq_rewind(ks) ((ks)->last_char = (ks)->f->is_eof = (ks)->f->begin = (ks)->f->end = 0)
-
-#define __KSEQ_BASIC(SCOPE, type_t)										\
-	SCOPE kseq_t *kseq_init(type_t fd)									\
-	{																	\
-		kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t));					\
-		s->f = ks_init(fd);												\
-		return s;														\
-	}																	\
-	SCOPE void kseq_destroy(kseq_t *ks)									\
-	{																	\
-		if (!ks) return;												\
-		free(ks->name.s); free(ks->comment.s); free(ks->seq.s);	free(ks->qual.s); \
-		ks_destroy(ks->f);												\
-		free(ks);														\
-	}
-
-/* Return value:
-   >=0  length of the sequence (normal)
-   -1   end-of-file
-   -2   truncated quality string
- */
-#define __KSEQ_READ(SCOPE) \
-	SCOPE int kseq_read(kseq_t *seq) \
-	{ \
-		int c; \
-		kstream_t *ks = seq->f; \
-		if (seq->last_char == 0) { /* then jump to the next header line */ \
-			while ((c = ks_getc(ks)) != -1 && c != '>' && c != '@'); \
-			if (c == -1) return -1; /* end of file */ \
-			seq->last_char = c; \
-		} /* else: the first header char has been read in the previous call */ \
-		seq->comment.l = seq->seq.l = seq->qual.l = 0; /* reset all members */ \
-		if (ks_getuntil(ks, 0, &seq->name, &c) < 0) return -1; /* normal exit: EOF */ \
-		if (c != '\n') ks_getuntil(ks, KS_SEP_LINE, &seq->comment, 0); /* read FASTA/Q comment */ \
-		if (seq->seq.s == 0) { /* we can do this in the loop below, but that is slower */ \
-			seq->seq.m = 256; \
-			seq->seq.s = (char*)malloc(seq->seq.m); \
-		} \
-		while ((c = ks_getc(ks)) != -1 && c != '>' && c != '+' && c != '@') { \
-			if (c == '\n') continue; /* skip empty lines */ \
-			seq->seq.s[seq->seq.l++] = c; /* this is safe: we always have enough space for 1 char */ \
-			ks_getuntil2(ks, KS_SEP_LINE, &seq->seq, 0, 1); /* read the rest of the line */ \
-		} \
-		if (c == '>' || c == '@') seq->last_char = c; /* the first header char has been read */	\
-		if (seq->seq.l + 1 >= seq->seq.m) { /* seq->seq.s[seq->seq.l] below may be out of boundary */ \
-			seq->seq.m = seq->seq.l + 2; \
-			kroundup32(seq->seq.m); /* rounded to the next closest 2^k */ \
-			seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m); \
-		} \
-		seq->seq.s[seq->seq.l] = 0;	/* null terminated string */ \
-		if (c != '+') return seq->seq.l; /* FASTA */ \
-		if (seq->qual.m < seq->seq.m) {	/* allocate memory for qual in case insufficient */ \
-			seq->qual.m = seq->seq.m; \
-			seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m); \
-		} \
-		while ((c = ks_getc(ks)) != -1 && c != '\n'); /* skip the rest of '+' line */ \
-		if (c == -1) return -2; /* error: no quality string */ \
-		while (ks_getuntil2(ks, KS_SEP_LINE, &seq->qual, 0, 1) >= 0 && seq->qual.l < seq->seq.l); \
-		seq->last_char = 0;	/* we have not come to the next header line */ \
-		if (seq->seq.l != seq->qual.l) return -2; /* error: qual string is of a different length */ \
-		return seq->seq.l; \
-	}
-
-#define __KSEQ_TYPE(type_t)						\
-	typedef struct {							\
-		kstring_t name, comment, seq, qual;		\
-		int last_char;							\
-		kstream_t *f;							\
-	} kseq_t;
-
-#define KSEQ_INIT2(SCOPE, type_t, __read)		\
-	KSTREAM_INIT(type_t, __read, 16384)			\
-	__KSEQ_TYPE(type_t)							\
-	__KSEQ_BASIC(SCOPE, type_t)					\
-	__KSEQ_READ(SCOPE)
-
-#define KSEQ_INIT(type_t, __read) KSEQ_INIT2(static, type_t, __read)
-
-#define KSEQ_DECLARE(type_t) \
-	__KS_TYPE(type_t) \
-	__KSEQ_TYPE(type_t) \
-	extern kseq_t *kseq_init(type_t fd); \
-	void kseq_destroy(kseq_t *ks); \
-	int kseq_read(kseq_t *seq);
-
-#endif
diff --git a/samtools/ksort.h b/samtools/ksort.h
deleted file mode 100644
index aa0bb93..0000000
--- a/samtools/ksort.h
+++ /dev/null
@@ -1,285 +0,0 @@
-/* The MIT License
-
-   Copyright (c) 2008 Genome Research Ltd (GRL).
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-/* Contact: Heng Li <lh3 at sanger.ac.uk> */
-
-/*
-  2012-12-11 (0.1.4):
-
-    * Defined __ks_insertsort_##name as static to compile with C99.
-
-  2008-11-16 (0.1.4):
-
-    * Fixed a bug in introsort() that happens in rare cases.
-
-  2008-11-05 (0.1.3):
-
-    * Fixed a bug in introsort() for complex comparisons.
-
-	* Fixed a bug in mergesort(). The previous version is not stable.
-
-  2008-09-15 (0.1.2):
-
-	* Accelerated introsort. On my Mac (not on another Linux machine),
-	  my implementation is as fast as std::sort on random input.
-
-	* Added combsort and in introsort, switch to combsort if the
-	  recursion is too deep.
-
-  2008-09-13 (0.1.1):
-
-	* Added k-small algorithm
-
-  2008-09-05 (0.1.0):
-
-	* Initial version
-
-*/
-
-#ifndef AC_KSORT_H
-#define AC_KSORT_H
-
-#include <stdlib.h>
-#include <string.h>
-
-typedef struct {
-	void *left, *right;
-	int depth;
-} ks_isort_stack_t;
-
-#define KSORT_SWAP(type_t, a, b) { register type_t t=(a); (a)=(b); (b)=t; }
-
-#define KSORT_INIT(name, type_t, __sort_lt)								\
-	void ks_mergesort_##name(size_t n, type_t array[], type_t temp[])	\
-	{																	\
-		type_t *a2[2], *a, *b;											\
-		int curr, shift;												\
-																		\
-		a2[0] = array;													\
-		a2[1] = temp? temp : (type_t*)malloc(sizeof(type_t) * n);		\
-		for (curr = 0, shift = 0; (1ul<<shift) < n; ++shift) {			\
-			a = a2[curr]; b = a2[1-curr];								\
-			if (shift == 0) {											\
-				type_t *p = b, *i, *eb = a + n;							\
-				for (i = a; i < eb; i += 2) {							\
-					if (i == eb - 1) *p++ = *i;							\
-					else {												\
-						if (__sort_lt(*(i+1), *i)) {					\
-							*p++ = *(i+1); *p++ = *i;					\
-						} else {										\
-							*p++ = *i; *p++ = *(i+1);					\
-						}												\
-					}													\
-				}														\
-			} else {													\
-				size_t i, step = 1ul<<shift;							\
-				for (i = 0; i < n; i += step<<1) {						\
-					type_t *p, *j, *k, *ea, *eb;						\
-					if (n < i + step) {									\
-						ea = a + n; eb = a;								\
-					} else {											\
-						ea = a + i + step;								\
-						eb = a + (n < i + (step<<1)? n : i + (step<<1)); \
-					}													\
-					j = a + i; k = a + i + step; p = b + i;				\
-					while (j < ea && k < eb) {							\
-						if (__sort_lt(*k, *j)) *p++ = *k++;				\
-						else *p++ = *j++;								\
-					}													\
-					while (j < ea) *p++ = *j++;							\
-					while (k < eb) *p++ = *k++;							\
-				}														\
-			}															\
-			curr = 1 - curr;											\
-		}																\
-		if (curr == 1) {												\
-			type_t *p = a2[0], *i = a2[1], *eb = array + n;				\
-			for (; p < eb; ++i) *p++ = *i;								\
-		}																\
-		if (temp == 0) free(a2[1]);										\
-	}																	\
-	void ks_heapadjust_##name(size_t i, size_t n, type_t l[])			\
-	{																	\
-		size_t k = i;													\
-		type_t tmp = l[i];												\
-		while ((k = (k << 1) + 1) < n) {								\
-			if (k != n - 1 && __sort_lt(l[k], l[k+1])) ++k;				\
-			if (__sort_lt(l[k], tmp)) break;							\
-			l[i] = l[k]; i = k;											\
-		}																\
-		l[i] = tmp;														\
-	}																	\
-	void ks_heapmake_##name(size_t lsize, type_t l[])					\
-	{																	\
-		size_t i;														\
-		for (i = (lsize >> 1) - 1; i != (size_t)(-1); --i)				\
-			ks_heapadjust_##name(i, lsize, l);							\
-	}																	\
-	void ks_heapsort_##name(size_t lsize, type_t l[])					\
-	{																	\
-		size_t i;														\
-		for (i = lsize - 1; i > 0; --i) {								\
-			type_t tmp;													\
-			tmp = *l; *l = l[i]; l[i] = tmp; ks_heapadjust_##name(0, i, l); \
-		}																\
-	}																	\
-	static inline void __ks_insertsort_##name(type_t *s, type_t *t)		\
-	{																	\
-		type_t *i, *j, swap_tmp;										\
-		for (i = s + 1; i < t; ++i)										\
-			for (j = i; j > s && __sort_lt(*j, *(j-1)); --j) {			\
-				swap_tmp = *j; *j = *(j-1); *(j-1) = swap_tmp;			\
-			}															\
-	}																	\
-	void ks_combsort_##name(size_t n, type_t a[])						\
-	{																	\
-		const double shrink_factor = 1.2473309501039786540366528676643; \
-		int do_swap;													\
-		size_t gap = n;													\
-		type_t tmp, *i, *j;												\
-		do {															\
-			if (gap > 2) {												\
-				gap = (size_t)(gap / shrink_factor);					\
-				if (gap == 9 || gap == 10) gap = 11;					\
-			}															\
-			do_swap = 0;												\
-			for (i = a; i < a + n - gap; ++i) {							\
-				j = i + gap;											\
-				if (__sort_lt(*j, *i)) {								\
-					tmp = *i; *i = *j; *j = tmp;						\
-					do_swap = 1;										\
-				}														\
-			}															\
-		} while (do_swap || gap > 2);									\
-		if (gap != 1) __ks_insertsort_##name(a, a + n);					\
-	}																	\
-	void ks_introsort_##name(size_t n, type_t a[])						\
-	{																	\
-		int d;															\
-		ks_isort_stack_t *top, *stack;									\
-		type_t rp, swap_tmp;											\
-		type_t *s, *t, *i, *j, *k;										\
-																		\
-		if (n < 1) return;												\
-		else if (n == 2) {												\
-			if (__sort_lt(a[1], a[0])) { swap_tmp = a[0]; a[0] = a[1]; a[1] = swap_tmp; } \
-			return;														\
-		}																\
-		for (d = 2; 1ul<<d < n; ++d);									\
-		stack = (ks_isort_stack_t*)malloc(sizeof(ks_isort_stack_t) * ((sizeof(size_t)*d)+2)); \
-		top = stack; s = a; t = a + (n-1); d <<= 1;						\
-		while (1) {														\
-			if (s < t) {												\
-				if (--d == 0) {											\
-					ks_combsort_##name(t - s + 1, s);					\
-					t = s;												\
-					continue;											\
-				}														\
-				i = s; j = t; k = i + ((j-i)>>1) + 1;					\
-				if (__sort_lt(*k, *i)) {								\
-					if (__sort_lt(*k, *j)) k = j;						\
-				} else k = __sort_lt(*j, *i)? i : j;					\
-				rp = *k;												\
-				if (k != t) { swap_tmp = *k; *k = *t; *t = swap_tmp; }	\
-				for (;;) {												\
-					do ++i; while (__sort_lt(*i, rp));					\
-					do --j; while (i <= j && __sort_lt(rp, *j));		\
-					if (j <= i) break;									\
-					swap_tmp = *i; *i = *j; *j = swap_tmp;				\
-				}														\
-				swap_tmp = *i; *i = *t; *t = swap_tmp;					\
-				if (i-s > t-i) {										\
-					if (i-s > 16) { top->left = s; top->right = i-1; top->depth = d; ++top; } \
-					s = t-i > 16? i+1 : t;								\
-				} else {												\
-					if (t-i > 16) { top->left = i+1; top->right = t; top->depth = d; ++top; } \
-					t = i-s > 16? i-1 : s;								\
-				}														\
-			} else {													\
-				if (top == stack) {										\
-					free(stack);										\
-					__ks_insertsort_##name(a, a+n);						\
-					return;												\
-				} else { --top; s = (type_t*)top->left; t = (type_t*)top->right; d = top->depth; } \
-			}															\
-		}																\
-	}																	\
-	/* This function is adapted from: http://ndevilla.free.fr/median/ */ \
-	/* 0 <= kk < n */													\
-	type_t ks_ksmall_##name(size_t n, type_t arr[], size_t kk)			\
-	{																	\
-		type_t *low, *high, *k, *ll, *hh, *mid;							\
-		low = arr; high = arr + n - 1; k = arr + kk;					\
-		for (;;) {														\
-			if (high <= low) return *k;									\
-			if (high == low + 1) {										\
-				if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \
-				return *k;												\
-			}															\
-			mid = low + (high - low) / 2;								\
-			if (__sort_lt(*high, *mid)) KSORT_SWAP(type_t, *mid, *high); \
-			if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \
-			if (__sort_lt(*low, *mid)) KSORT_SWAP(type_t, *mid, *low);	\
-			KSORT_SWAP(type_t, *mid, *(low+1));							\
-			ll = low + 1; hh = high;									\
-			for (;;) {													\
-				do ++ll; while (__sort_lt(*ll, *low));					\
-				do --hh; while (__sort_lt(*low, *hh));					\
-				if (hh < ll) break;										\
-				KSORT_SWAP(type_t, *ll, *hh);							\
-			}															\
-			KSORT_SWAP(type_t, *low, *hh);								\
-			if (hh <= k) low = ll;										\
-			if (hh >= k) high = hh - 1;									\
-		}																\
-	}																	\
-	void ks_shuffle_##name(size_t n, type_t a[])						\
-	{																	\
-		int i, j;														\
-		for (i = n; i > 1; --i) {										\
-			type_t tmp;													\
-			j = (int)(drand48() * i);									\
-			tmp = a[j]; a[j] = a[i-1]; a[i-1] = tmp;					\
-		}																\
-	}
-
-#define ks_mergesort(name, n, a, t) ks_mergesort_##name(n, a, t)
-#define ks_introsort(name, n, a) ks_introsort_##name(n, a)
-#define ks_combsort(name, n, a) ks_combsort_##name(n, a)
-#define ks_heapsort(name, n, a) ks_heapsort_##name(n, a)
-#define ks_heapmake(name, n, a) ks_heapmake_##name(n, a)
-#define ks_heapadjust(name, i, n, a) ks_heapadjust_##name(i, n, a)
-#define ks_ksmall(name, n, a, k) ks_ksmall_##name(n, a, k)
-#define ks_shuffle(name, n, a) ks_shuffle_##name(n, a)
-
-#define ks_lt_generic(a, b) ((a) < (b))
-#define ks_lt_str(a, b) (strcmp((a), (b)) < 0)
-
-typedef const char *ksstr_t;
-
-#define KSORT_INIT_GENERIC(type_t) KSORT_INIT(type_t, type_t, ks_lt_generic)
-#define KSORT_INIT_STR KSORT_INIT(str, ksstr_t, ks_lt_str)
-
-#endif
diff --git a/samtools/kstring.c.pysam.c b/samtools/kstring.c.pysam.c
deleted file mode 100644
index ba37801..0000000
--- a/samtools/kstring.c.pysam.c
+++ /dev/null
@@ -1,214 +0,0 @@
-#include "pysam.h"
-
-#include <stdarg.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <string.h>
-#include <stdint.h>
-#include "kstring.h"
-
-int ksprintf(kstring_t *s, const char *fmt, ...)
-{
-	va_list ap;
-	int l;
-	va_start(ap, fmt);
-	l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap); // This line does not work with glibc 2.0. See `man snprintf'.
-	va_end(ap);
-	if (l + 1 > s->m - s->l) {
-		s->m = s->l + l + 2;
-		kroundup32(s->m);
-		s->s = (char*)realloc(s->s, s->m);
-		va_start(ap, fmt);
-		l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
-	}
-	va_end(ap);
-	s->l += l;
-	return l;
-}
-
-char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux)
-{
-	const char *p, *start;
-	if (sep) { // set up the table
-		if (str == 0 && (aux->tab[0]&1)) return 0; // no need to set up if we have finished
-		aux->finished = 0;
-		if (sep[1]) {
-			aux->sep = -1;
-			aux->tab[0] = aux->tab[1] = aux->tab[2] = aux->tab[3] = 0;
-			for (p = sep; *p; ++p) aux->tab[*p>>6] |= 1ull<<(*p&0x3f);
-		} else aux->sep = sep[0];
-	}
-	if (aux->finished) return 0;
-	else if (str) aux->p = str - 1, aux->finished = 0;
-	if (aux->sep < 0) {
-		for (p = start = aux->p + 1; *p; ++p)
-			if (aux->tab[*p>>6]>>(*p&0x3f)&1) break;
-	} else {
-		for (p = start = aux->p + 1; *p; ++p)
-			if (*p == aux->sep) break;
-	}
-	aux->p = p; // end of token
-	if (*p == 0) aux->finished = 1; // no more tokens
-	return (char*)start;
-}
-
-// s MUST BE a null terminated string; l = strlen(s)
-int ksplit_core(char *s, int delimiter, int *_max, int **_offsets)
-{
-	int i, n, max, last_char, last_start, *offsets, l;
-	n = 0; max = *_max; offsets = *_offsets;
-	l = strlen(s);
-	
-#define __ksplit_aux do {												\
-		if (_offsets) {													\
-			s[i] = 0;													\
-			if (n == max) {												\
-				max = max? max<<1 : 2;									\
-				offsets = (int*)realloc(offsets, sizeof(int) * max);	\
-			}															\
-			offsets[n++] = last_start;									\
-		} else ++n;														\
-	} while (0)
-
-	for (i = 0, last_char = last_start = 0; i <= l; ++i) {
-		if (delimiter == 0) {
-			if (isspace(s[i]) || s[i] == 0) {
-				if (isgraph(last_char)) __ksplit_aux; // the end of a field
-			} else {
-				if (isspace(last_char) || last_char == 0) last_start = i;
-			}
-		} else {
-			if (s[i] == delimiter || s[i] == 0) {
-				if (last_char != 0 && last_char != delimiter) __ksplit_aux; // the end of a field
-			} else {
-				if (last_char == delimiter || last_char == 0) last_start = i;
-			}
-		}
-		last_char = s[i];
-	}
-	*_max = max; *_offsets = offsets;
-	return n;
-}
-
-/**********************
- * Boyer-Moore search *
- **********************/
-
-typedef unsigned char ubyte_t;
-
-// reference: http://www-igm.univ-mlv.fr/~lecroq/string/node14.html
-static int *ksBM_prep(const ubyte_t *pat, int m)
-{
-	int i, *suff, *prep, *bmGs, *bmBc;
-	prep = (int*)calloc(m + 256, sizeof(int));
-	bmGs = prep; bmBc = prep + m;
-	{ // preBmBc()
-		for (i = 0; i < 256; ++i) bmBc[i] = m;
-		for (i = 0; i < m - 1; ++i) bmBc[pat[i]] = m - i - 1;
-	}
-	suff = (int*)calloc(m, sizeof(int));
-	{ // suffixes()
-		int f = 0, g;
-		suff[m - 1] = m;
-		g = m - 1;
-		for (i = m - 2; i >= 0; --i) {
-			if (i > g && suff[i + m - 1 - f] < i - g)
-				suff[i] = suff[i + m - 1 - f];
-			else {
-				if (i < g) g = i;
-				f = i;
-				while (g >= 0 && pat[g] == pat[g + m - 1 - f]) --g;
-				suff[i] = f - g;
-			}
-		}
-	}
-	{ // preBmGs()
-		int j = 0;
-		for (i = 0; i < m; ++i) bmGs[i] = m;
-		for (i = m - 1; i >= 0; --i)
-			if (suff[i] == i + 1)
-				for (; j < m - 1 - i; ++j)
-					if (bmGs[j] == m)
-						bmGs[j] = m - 1 - i;
-		for (i = 0; i <= m - 2; ++i)
-			bmGs[m - 1 - suff[i]] = m - 1 - i;
-	}
-	free(suff);
-	return prep;
-}
-
-void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep)
-{
-	int i, j, *prep = 0, *bmGs, *bmBc;
-	const ubyte_t *str, *pat;
-	str = (const ubyte_t*)_str; pat = (const ubyte_t*)_pat;
-	prep = (_prep == 0 || *_prep == 0)? ksBM_prep(pat, m) : *_prep;
-	if (_prep && *_prep == 0) *_prep = prep;
-	bmGs = prep; bmBc = prep + m;
-	j = 0;
-	while (j <= n - m) {
-		for (i = m - 1; i >= 0 && pat[i] == str[i+j]; --i);
-		if (i >= 0) {
-			int max = bmBc[str[i+j]] - m + 1 + i;
-			if (max < bmGs[i]) max = bmGs[i];
-			j += max;
-		} else return (void*)(str + j);
-	}
-	if (_prep == 0) free(prep);
-	return 0;
-}
-
-char *kstrstr(const char *str, const char *pat, int **_prep)
-{
-	return (char*)kmemmem(str, strlen(str), pat, strlen(pat), _prep);
-}
-
-char *kstrnstr(const char *str, const char *pat, int n, int **_prep)
-{
-	return (char*)kmemmem(str, n, pat, strlen(pat), _prep);
-}
-
-/***********************
- * The main() function *
- ***********************/
-
-#ifdef KSTRING_MAIN
-#include <stdio.h>
-int main()
-{
-	kstring_t *s;
-	int *fields, n, i;
-	ks_tokaux_t aux;
-	char *p;
-	s = (kstring_t*)calloc(1, sizeof(kstring_t));
-	// test ksprintf()
-	ksprintf(s, " abcdefg:    %d ", 100);
-	printf("'%s'\n", s->s);
-	// test ksplit()
-	fields = ksplit(s, 0, &n);
-	for (i = 0; i < n; ++i)
-		printf("field[%d] = '%s'\n", i, s->s + fields[i]);
-	// test kstrtok()
-	s->l = 0;
-	for (p = kstrtok("ab:cde:fg/hij::k", ":/", &aux); p; p = kstrtok(0, 0, &aux)) {
-		kputsn(p, aux.p - p, s);
-		kputc('\n', s);
-	}
-	printf("%s", s->s);
-	// free
-	free(s->s); free(s); free(fields);
-
-	{
-		static char *str = "abcdefgcdgcagtcakcdcd";
-		static char *pat = "cd";
-		char *ret, *s = str;
-		int *prep = 0;
-		while ((ret = kstrstr(s, pat, &prep)) != 0) {
-			printf("match: %s\n", ret);
-			s = ret + prep[0];
-		}
-		free(prep);
-	}
-	return 0;
-}
-#endif
diff --git a/samtools/kstring.h b/samtools/kstring.h
deleted file mode 100644
index abd8236..0000000
--- a/samtools/kstring.h
+++ /dev/null
@@ -1,169 +0,0 @@
-/* The MIT License
-
-   Copyright (c) by Attractive Chaos <attractor at live.co.uk> 
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-   SOFTWARE.
-*/
-
-#ifndef KSTRING_H
-#define KSTRING_H
-
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-
-#ifndef kroundup32
-#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
-#endif
-
-#ifndef KSTRING_T
-#define KSTRING_T kstring_t
-typedef struct __kstring_t {
-	size_t l, m;
-	char *s;
-} kstring_t;
-#endif
-
-typedef struct {
-	uint64_t tab[4];
-	int sep, finished;
-	const char *p; // end of the current token
-} ks_tokaux_t;
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-	int ksprintf(kstring_t *s, const char *fmt, ...);
-	int ksplit_core(char *s, int delimiter, int *_max, int **_offsets);
-	char *kstrstr(const char *str, const char *pat, int **_prep);
-	char *kstrnstr(const char *str, const char *pat, int n, int **_prep);
-	void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep);
-
-	/* kstrtok() is similar to strtok_r() except that str is not
-	 * modified and both str and sep can be NULL. For efficiency, it is
-	 * actually recommended to set both to NULL in the subsequent calls
-	 * if sep is not changed. */
-	char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux);
-
-#ifdef __cplusplus
-}
-#endif
-
-static inline void ks_resize(kstring_t *s, size_t size)
-{
-	if (s->m < size) {
-		s->m = size;
-		kroundup32(s->m);
-		s->s = (char*)realloc(s->s, s->m);
-	}
-}
-
-static inline int kputsn(const char *p, int l, kstring_t *s)
-{
-	if (s->l + l + 1 >= s->m) {
-		s->m = s->l + l + 2;
-		kroundup32(s->m);
-		s->s = (char*)realloc(s->s, s->m);
-	}
-	memcpy(s->s + s->l, p, l);
-	s->l += l;
-	s->s[s->l] = 0;
-	return l;
-}
-
-static inline int kputs(const char *p, kstring_t *s)
-{
-	return kputsn(p, strlen(p), s);
-}
-
-static inline int kputc(int c, kstring_t *s)
-{
-	if (s->l + 1 >= s->m) {
-		s->m = s->l + 2;
-		kroundup32(s->m);
-		s->s = (char*)realloc(s->s, s->m);
-	}
-	s->s[s->l++] = c;
-	s->s[s->l] = 0;
-	return c;
-}
-
-static inline int kputw(int c, kstring_t *s)
-{
-	char buf[16];
-	int l, x;
-	if (c == 0) return kputc('0', s);
-        if(c < 0) for (l = 0, x = c; x < 0; x /= 10) buf[l++] = '0' - (x%10);
-        else for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0';
-	if (c < 0) buf[l++] = '-';
-	if (s->l + l + 1 >= s->m) {
-		s->m = s->l + l + 2;
-		kroundup32(s->m);
-		s->s = (char*)realloc(s->s, s->m);
-	}
-	for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x];
-	s->s[s->l] = 0;
-	return 0;
-}
-
-static inline int kputuw(unsigned c, kstring_t *s)
-{
-	char buf[16];
-	int l, i;
-	unsigned x;
-	if (c == 0) return kputc('0', s);
-	for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0';
-	if (s->l + l + 1 >= s->m) {
-		s->m = s->l + l + 2;
-		kroundup32(s->m);
-		s->s = (char*)realloc(s->s, s->m);
-	}
-	for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
-	s->s[s->l] = 0;
-	return 0;
-}
-
-static inline int kputl(long c, kstring_t *s)
-{
-	char buf[32];
-	long l, x;
-	if (c == 0) return kputc('0', s);
-	for (l = 0, x = c < 0? -c : c; x > 0; x /= 10) buf[l++] = x%10 + '0';
-	if (c < 0) buf[l++] = '-';
-	if (s->l + l + 1 >= s->m) {
-		s->m = s->l + l + 2;
-		kroundup32(s->m);
-		s->s = (char*)realloc(s->s, s->m);
-	}
-	for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x];
-	s->s[s->l] = 0;
-	return 0;
-}
-
-static inline int *ksplit(kstring_t *s, int delimiter, int *n)
-{
-	int max = 0, *offsets = 0;
-	*n = ksplit_core(s->s, delimiter, &max, &offsets);
-	return offsets;
-}
-
-#endif
diff --git a/samtools/misc/ace2sam.c.pysam.c b/samtools/misc/ace2sam.c.pysam.c
index a518c67..53c82dc 100644
--- a/samtools/misc/ace2sam.c.pysam.c
+++ b/samtools/misc/ace2sam.c.pysam.c
@@ -30,8 +30,8 @@
 #include <string.h>
 #include <unistd.h>
 #include <zlib.h>
-#include "kstring.h"
-#include "kseq.h"
+#include "htslib/kstring.h"
+#include "htslib/kseq.h"
 KSTREAM_INIT(gzFile, gzread, 16384)
 
 #define N_TMPSTR 5
@@ -39,213 +39,213 @@ KSTREAM_INIT(gzFile, gzread, 16384)
 
 // append a CIGAR operation plus length
 #define write_cigar(_c, _n, _m, _v) do { \
-		if (_n == _m) { \
-			_m = _m? _m<<1 : 4; \
-			_c = realloc(_c, _m * sizeof(unsigned)); \
-		} \
-		_c[_n++] = (_v); \
-	} while (0)
+        if (_n == _m) { \
+            _m = _m? _m<<1 : 4; \
+            _c = realloc(_c, _m * sizeof(unsigned)); \
+        } \
+        _c[_n++] = (_v); \
+    } while (0)
 
 // a fatal error
 static void fatal(const char *msg)
 {
-	fprintf(pysamerr, "E %s\n", msg);
-	exit(1);
+    fprintf(pysamerr, "E %s\n", msg);
+    exit(1);
 }
 // remove pads
 static void remove_pads(const kstring_t *src, kstring_t *dst)
 {
-	int i, j;
-	dst->l = 0;
-	kputsn(src->s, src->l, dst);
-	for (i = j = 0; i < dst->l; ++i)
-		if (dst->s[i] != '*') dst->s[j++] = dst->s[i];
-	dst->s[j] = 0;
-	dst->l = j;
+    int i, j;
+    dst->l = 0;
+    kputsn(src->s, src->l, dst);
+    for (i = j = 0; i < dst->l; ++i)
+        if (dst->s[i] != '*') dst->s[j++] = dst->s[i];
+    dst->s[j] = 0;
+    dst->l = j;
 }
 
 int main(int argc, char *argv[])
 {
-	gzFile fp;
-	kstream_t *ks;
-	kstring_t s, t[N_TMPSTR];
-	int dret, i, k, af_n, af_max, af_i, c, is_padded = 0, write_cns = 0, *p2u = 0;
-	long m_cigar = 0, n_cigar = 0;
-	unsigned *af, *cigar = 0;
+    gzFile fp;
+    kstream_t *ks;
+    kstring_t s, t[N_TMPSTR];
+    int dret, i, k, af_n, af_max, af_i, c, is_padded = 0, write_cns = 0, *p2u = 0;
+    long m_cigar = 0, n_cigar = 0;
+    unsigned *af, *cigar = 0;
 
-	while ((c = getopt(argc, argv, "pc")) >= 0) {
-		switch (c) {
-			case 'p': is_padded = 1; break;
-			case 'c': write_cns = 1; break;
-		}
-	}
-	if (argc == optind) {
-		fprintf(pysamerr, "\nUsage:   ace2sam [-pc] <in.ace>\n\n");
-		fprintf(pysamerr, "Options: -p     output padded SAM\n");
-		fprintf(pysamerr, "         -c     write the contig sequence in SAM\n\n");
-		fprintf(pysamerr, "Notes: 1. Fields must appear in the following order: (CO->[BQ]->(AF)->(RD->QA))\n");
-		fprintf(pysamerr, "       2. The order of reads in AF and in RD must be identical\n");
-		fprintf(pysamerr, "       3. Except in BQ, words and numbers must be separated by a single SPACE or TAB\n");
-		fprintf(pysamerr, "       4. This program writes the headerless SAM to stdout and header to pysamerr\n\n");
-		return 1;
-	}
+    while ((c = getopt(argc, argv, "pc")) >= 0) {
+        switch (c) {
+            case 'p': is_padded = 1; break;
+            case 'c': write_cns = 1; break;
+        }
+    }
+    if (argc == optind) {
+        fprintf(pysamerr, "\nUsage:   ace2sam [-pc] <in.ace>\n\n");
+        fprintf(pysamerr, "Options: -p     output padded SAM\n");
+        fprintf(pysamerr, "         -c     write the contig sequence in SAM\n\n");
+        fprintf(pysamerr, "Notes: 1. Fields must appear in the following order: (CO->[BQ]->(AF)->(RD->QA))\n");
+        fprintf(pysamerr, "       2. The order of reads in AF and in RD must be identical\n");
+        fprintf(pysamerr, "       3. Except in BQ, words and numbers must be separated by a single SPACE or TAB\n");
+        fprintf(pysamerr, "       4. This program writes the headerless SAM to stdout and header to pysamerr\n\n");
+        return 1;
+    }
 
-	s.l = s.m = 0; s.s = 0;
-	af_n = af_max = af_i = 0; af = 0;
-	for (i = 0; i < N_TMPSTR; ++i) t[i].l = t[i].m = 0, t[i].s = 0;
-	fp = strcmp(argv[1], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
-	ks = ks_init(fp);
-	while (ks_getuntil(ks, 0, &s, &dret) >= 0) {
-		if (strcmp(s.s, "CO") == 0) { // contig sequence
-			kstring_t *cns;
-			t[0].l = t[1].l = t[2].l = t[3].l = t[4].l = 0; // 0: name; 1: padded ctg; 2: unpadded ctg/padded read; 3: unpadded read; 4: SAM line
-			af_n = af_i = 0; // reset the af array
-			ks_getuntil(ks, 0, &s, &dret); kputs(s.s, &t[0]); // contig name
-			ks_getuntil(ks, '\n', &s, &dret); // read the whole line
-			while (ks_getuntil(ks, '\n', &s, &dret) >= 0 && s.l > 0) kputsn(s.s, s.l, &t[1]); // read the padded consensus sequence
-			remove_pads(&t[1], &t[2]); // construct the unpadded sequence
-			// compute the array for mapping padded positions to unpadded positions
-			p2u = realloc(p2u, t[1].m * sizeof(int));
-			for (i = k = 0; i < t[1].l; ++i) {
-				p2u[i] = k;
-				if (t[1].s[i] != '*') ++k;
-			}
-			// write out the SAM header and contig sequences
-			fprintf(pysamerr, "H @SQ\tSN:%s\tLN:%ld\n", t[0].s, t[is_padded?1:2].l); // The SAM header line
-			cns = &t[is_padded?1:2];
-			fprintf(pysamerr, "S >%s\n", t[0].s);
-			for (i = 0; i < cns->l; i += LINE_LEN) {
-				fputs("S ", pysamerr);
-				for (k = 0; k < LINE_LEN && i + k < cns->l; ++k)
-					fputc(cns->s[i + k], pysamerr);
-				fputc('\n', pysamerr);
-			}
+    s.l = s.m = 0; s.s = 0;
+    af_n = af_max = af_i = 0; af = 0;
+    for (i = 0; i < N_TMPSTR; ++i) t[i].l = t[i].m = 0, t[i].s = 0;
+    fp = strcmp(argv[1], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
+    ks = ks_init(fp);
+    while (ks_getuntil(ks, 0, &s, &dret) >= 0) {
+        if (strcmp(s.s, "CO") == 0) { // contig sequence
+            kstring_t *cns;
+            t[0].l = t[1].l = t[2].l = t[3].l = t[4].l = 0; // 0: name; 1: padded ctg; 2: unpadded ctg/padded read; 3: unpadded read; 4: SAM line
+            af_n = af_i = 0; // reset the af array
+            ks_getuntil(ks, 0, &s, &dret); kputs(s.s, &t[0]); // contig name
+            ks_getuntil(ks, '\n', &s, &dret); // read the whole line
+            while (ks_getuntil(ks, '\n', &s, &dret) >= 0 && s.l > 0) kputsn(s.s, s.l, &t[1]); // read the padded consensus sequence
+            remove_pads(&t[1], &t[2]); // construct the unpadded sequence
+            // compute the array for mapping padded positions to unpadded positions
+            p2u = realloc(p2u, t[1].m * sizeof(int));
+            for (i = k = 0; i < t[1].l; ++i) {
+                p2u[i] = k;
+                if (t[1].s[i] != '*') ++k;
+            }
+            // write out the SAM header and contig sequences
+            fprintf(pysamerr, "H @SQ\tSN:%s\tLN:%ld\n", t[0].s, t[is_padded?1:2].l); // The SAM header line
+            cns = &t[is_padded?1:2];
+            fprintf(pysamerr, "S >%s\n", t[0].s);
+            for (i = 0; i < cns->l; i += LINE_LEN) {
+                fputs("S ", pysamerr);
+                for (k = 0; k < LINE_LEN && i + k < cns->l; ++k)
+                    fputc(cns->s[i + k], pysamerr);
+                fputc('\n', pysamerr);
+            }
 
 #define __padded2cigar(sp) do { \
-		int i, l_M = 0, l_D = 0; \
-		for (i = 0; i < sp.l; ++i) { \
-			if (sp.s[i] == '*') { \
-				if (l_M) write_cigar(cigar, n_cigar, m_cigar, l_M<<4); \
-				++l_D; l_M = 0; \
-			} else { \
-				if (l_D) write_cigar(cigar, n_cigar, m_cigar, l_D<<4 | 2); \
-				++l_M; l_D = 0; \
-			} \
-		} \
-		if (l_M) write_cigar(cigar, n_cigar, m_cigar, l_M<<4); \
-		else write_cigar(cigar, n_cigar, m_cigar, l_D<<4 | 2); \
-	} while (0)
+        int i, l_M = 0, l_D = 0; \
+        for (i = 0; i < sp.l; ++i) { \
+            if (sp.s[i] == '*') { \
+                if (l_M) write_cigar(cigar, n_cigar, m_cigar, l_M<<4); \
+                ++l_D; l_M = 0; \
+            } else { \
+                if (l_D) write_cigar(cigar, n_cigar, m_cigar, l_D<<4 | 2); \
+                ++l_M; l_D = 0; \
+            } \
+        } \
+        if (l_M) write_cigar(cigar, n_cigar, m_cigar, l_M<<4); \
+        else write_cigar(cigar, n_cigar, m_cigar, l_D<<4 | 2); \
+    } while (0)
 
-			if (write_cns) { // write the consensus SAM line (dummy read)
-				n_cigar = 0;
-				if (is_padded) __padded2cigar(t[1]);
-				else write_cigar(cigar, n_cigar, m_cigar, t[2].l<<4);
-				kputsn(t[0].s, t[0].l, &t[4]); kputs("\t516\t", &t[4]); kputsn(t[0].s, t[0].l, &t[4]); kputs("\t1\t60\t", &t[4]);
-				for (i = 0; i < n_cigar; ++i) {
-					kputw(cigar[i]>>4, &t[4]); kputc("MIDNSHP=X"[cigar[i]&0xf], &t[4]);
-				}
-				kputs("\t*\t0\t0\t", &t[4]); kputsn(t[2].s, t[2].l, &t[4]); kputs("\t*", &t[4]);
-			}
-		} else if (strcmp(s.s, "BQ") == 0) { // contig quality
-			if (t[0].l == 0) fatal("come to 'BQ' before reading 'CO'");
-			if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret); // read the entire "BQ" line
-			if (write_cns) t[4].s[--t[4].l] = 0; // remove the trailing "*"
-			for (i = 0; i < t[2].l; ++i) { // read the consensus quality
-				int q;
-				if (ks_getuntil(ks, 0, &s, &dret) < 0) fprintf(pysamerr, "E truncated contig quality\n");
-				if (s.l) {
-					q = atoi(s.s) + 33;
-					if (q > 126) q = 126;
-					if (write_cns) kputc(q, &t[4]);
-				} else --i;
-			}
-			if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret);
-			ks_getuntil(ks, '\n', &s, &dret); // skip the empty line
-			if (write_cns) puts(t[4].s); t[4].l = 0;
-		} else if (strcmp(s.s, "AF") == 0) { // padded read position
-			int reversed, neg, pos;
-			if (t[0].l == 0) fatal("come to 'AF' before reading 'CO'");
-			if (write_cns) {
-				if (t[4].l) puts(t[4].s);
-				t[4].l = 0;
-			}
-			ks_getuntil(ks, 0, &s, &dret); // read name
-			ks_getuntil(ks, 0, &s, &dret); reversed = s.s[0] == 'C'? 1 : 0; // strand
-			ks_getuntil(ks, 0, &s, &dret); pos = atoi(s.s); neg = pos < 0? 1 : 0; pos = pos < 0? -pos : pos; // position
-			if (af_n == af_max) { // double the af array
-				af_max = af_max? af_max<<1 : 4;
-				af = realloc(af, af_max * sizeof(unsigned));
-			}
-			af[af_n++] = pos << 2 | neg << 1 | reversed; // keep the placement information
-		} else if (strcmp(s.s, "RD") == 0) { // read sequence
-			if (af_i >= af_n) fatal("more 'RD' records than 'AF'");
-			t[2].l = t[3].l = t[4].l = 0;
-			ks_getuntil(ks, 0, &t[4], &dret); // QNAME
-			if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret); // read the entire RD line
-			while (ks_getuntil(ks, '\n', &s, &dret) >= 0 && s.l > 0) kputs(s.s, &t[2]); // read the read sequence
-		} else if (strcmp(s.s, "QA") == 0) { // clipping
-			if (af_i >= af_n) fatal("more 'QA' records than 'AF'");
-			int beg, end, pos, op;
-			ks_getuntil(ks, 0, &s, &dret); ks_getuntil(ks, 0, &s, &dret); // skip quality clipping
-			ks_getuntil(ks, 0, &s, &dret); beg = atoi(s.s) - 1; // align clipping start
-			ks_getuntil(ks, 0, &s, &dret); end = atoi(s.s); // clipping end
-			if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret);
-			// compute 1-based POS
-			pos = af[af_i]>>2; // retrieve the position information
-			if (af[af_i]>>1&1) pos = -pos;
-			pos += beg; // now pos is the true padded position
-			// generate CIGAR
-			remove_pads(&t[2], &t[3]); // backup the unpadded read sequence
-			n_cigar = 0;
-			if (beg) write_cigar(cigar, n_cigar, m_cigar, beg<<4|4);
-			if (is_padded) {
-				__padded2cigar(t[2]);
-				if (beg && n_cigar > 1) cigar[1] -= beg<<4; // fix the left-hand CIGAR 
-				if (end < t[2].l && n_cigar) cigar[n_cigar-1] -= (t[2].l - end)<<4; // fix the right-hand CIGAR
-			} else {
-				// generate flattened CIGAR string
-				for (i = beg, k = pos - 1; i < end; ++i, ++k)
-					t[2].s[i] = t[2].s[i] != '*'? (t[1].s[k] != '*'? 0 : 1) : (t[1].s[k] != '*'? 2 : 6);
-				// generate the proper CIGAR
-				for (i = beg + 1, k = 1, op = t[2].s[beg]; i < end; ++i) {
-					if (op != t[2].s[i]) {
-						write_cigar(cigar, n_cigar, m_cigar, k<<4|op);
-						op = t[2].s[i]; k = 1;
-					} else ++k;
-				}
-				write_cigar(cigar, n_cigar, m_cigar, k<<4|op);
-				// remove unnecessary "P" and possibly merge adjacent operations
-				for (i = 2; i < n_cigar; ++i) {
-					if ((cigar[i]&0xf) != 1 && (cigar[i-1]&0xf) == 6 && (cigar[i-2]&0xf) != 1) {
-						cigar[i-1] = 0;
-						if ((cigar[i]&0xf) == (cigar[i-2]&0xf)) // merge operations
-							cigar[i] += cigar[i-2], cigar[i-2] = 0;
-					}
-				}
-				for (i = k = 0; i < n_cigar; ++i) // squeeze out dumb operations
-					if (cigar[i]) cigar[k++] = cigar[i];
-				n_cigar = k;
-			}
-			if (end < t[2].l) write_cigar(cigar, n_cigar, m_cigar, (t[2].l - end)<<4|4);
-			// write the SAM line for the read
-			kputc('\t', &t[4]); // QNAME has already been written
-			kputw((af[af_i]&1)? 16 : 0, &t[4]); kputc('\t', &t[4]); // FLAG
-			kputsn(t[0].s, t[0].l, &t[4]); kputc('\t', &t[4]); // RNAME
-			kputw(is_padded? pos : p2u[pos-1]+1, &t[4]); // POS
-			kputs("\t60\t", &t[4]); // MAPQ
-			for (i = 0; i < n_cigar; ++i) { // CIGAR
-				kputw(cigar[i]>>4, &t[4]); kputc("MIDNSHP=X"[cigar[i]&0xf], &t[4]);
-			}
-			kputs("\t*\t0\t0\t", &t[4]); // empty MRNM, MPOS and TLEN
-			kputsn(t[3].s, t[3].l, &t[4]); // unpadded SEQ
-			kputs("\t*", &t[4]); // QUAL
-			puts(t[4].s); // print to stdout
-			++af_i;
-		} else if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret);
-	}
-	ks_destroy(ks);
-	gzclose(fp);
-	free(af); free(s.s); free(cigar); free(p2u);
-	for (i = 0; i < N_TMPSTR; ++i) free(t[i].s);
-	return 0;
+            if (write_cns) { // write the consensus SAM line (dummy read)
+                n_cigar = 0;
+                if (is_padded) __padded2cigar(t[1]);
+                else write_cigar(cigar, n_cigar, m_cigar, t[2].l<<4);
+                kputsn(t[0].s, t[0].l, &t[4]); kputs("\t516\t", &t[4]); kputsn(t[0].s, t[0].l, &t[4]); kputs("\t1\t60\t", &t[4]);
+                for (i = 0; i < n_cigar; ++i) {
+                    kputw(cigar[i]>>4, &t[4]); kputc("MIDNSHP=X"[cigar[i]&0xf], &t[4]);
+                }
+                kputs("\t*\t0\t0\t", &t[4]); kputsn(t[2].s, t[2].l, &t[4]); kputs("\t*", &t[4]);
+            }
+        } else if (strcmp(s.s, "BQ") == 0) { // contig quality
+            if (t[0].l == 0) fatal("come to 'BQ' before reading 'CO'");
+            if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret); // read the entire "BQ" line
+            if (write_cns) t[4].s[--t[4].l] = 0; // remove the trailing "*"
+            for (i = 0; i < t[2].l; ++i) { // read the consensus quality
+                int q;
+                if (ks_getuntil(ks, 0, &s, &dret) < 0) fprintf(pysamerr, "E truncated contig quality\n");
+                if (s.l) {
+                    q = atoi(s.s) + 33;
+                    if (q > 126) q = 126;
+                    if (write_cns) kputc(q, &t[4]);
+                } else --i;
+            }
+            if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret);
+            ks_getuntil(ks, '\n', &s, &dret); // skip the empty line
+            if (write_cns) puts(t[4].s); t[4].l = 0;
+        } else if (strcmp(s.s, "AF") == 0) { // padded read position
+            int reversed, neg, pos;
+            if (t[0].l == 0) fatal("come to 'AF' before reading 'CO'");
+            if (write_cns) {
+                if (t[4].l) puts(t[4].s);
+                t[4].l = 0;
+            }
+            ks_getuntil(ks, 0, &s, &dret); // read name
+            ks_getuntil(ks, 0, &s, &dret); reversed = s.s[0] == 'C'? 1 : 0; // strand
+            ks_getuntil(ks, 0, &s, &dret); pos = atoi(s.s); neg = pos < 0? 1 : 0; pos = pos < 0? -pos : pos; // position
+            if (af_n == af_max) { // double the af array
+                af_max = af_max? af_max<<1 : 4;
+                af = realloc(af, af_max * sizeof(unsigned));
+            }
+            af[af_n++] = pos << 2 | neg << 1 | reversed; // keep the placement information
+        } else if (strcmp(s.s, "RD") == 0) { // read sequence
+            if (af_i >= af_n) fatal("more 'RD' records than 'AF'");
+            t[2].l = t[3].l = t[4].l = 0;
+            ks_getuntil(ks, 0, &t[4], &dret); // QNAME
+            if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret); // read the entire RD line
+            while (ks_getuntil(ks, '\n', &s, &dret) >= 0 && s.l > 0) kputs(s.s, &t[2]); // read the read sequence
+        } else if (strcmp(s.s, "QA") == 0) { // clipping
+            if (af_i >= af_n) fatal("more 'QA' records than 'AF'");
+            int beg, end, pos, op;
+            ks_getuntil(ks, 0, &s, &dret); ks_getuntil(ks, 0, &s, &dret); // skip quality clipping
+            ks_getuntil(ks, 0, &s, &dret); beg = atoi(s.s) - 1; // align clipping start
+            ks_getuntil(ks, 0, &s, &dret); end = atoi(s.s); // clipping end
+            if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret);
+            // compute 1-based POS
+            pos = af[af_i]>>2; // retrieve the position information
+            if (af[af_i]>>1&1) pos = -pos;
+            pos += beg; // now pos is the true padded position
+            // generate CIGAR
+            remove_pads(&t[2], &t[3]); // backup the unpadded read sequence
+            n_cigar = 0;
+            if (beg) write_cigar(cigar, n_cigar, m_cigar, beg<<4|4);
+            if (is_padded) {
+                __padded2cigar(t[2]);
+                if (beg && n_cigar > 1) cigar[1] -= beg<<4; // fix the left-hand CIGAR
+                if (end < t[2].l && n_cigar) cigar[n_cigar-1] -= (t[2].l - end)<<4; // fix the right-hand CIGAR
+            } else {
+                // generate flattened CIGAR string
+                for (i = beg, k = pos - 1; i < end; ++i, ++k)
+                    t[2].s[i] = t[2].s[i] != '*'? (t[1].s[k] != '*'? 0 : 1) : (t[1].s[k] != '*'? 2 : 6);
+                // generate the proper CIGAR
+                for (i = beg + 1, k = 1, op = t[2].s[beg]; i < end; ++i) {
+                    if (op != t[2].s[i]) {
+                        write_cigar(cigar, n_cigar, m_cigar, k<<4|op);
+                        op = t[2].s[i]; k = 1;
+                    } else ++k;
+                }
+                write_cigar(cigar, n_cigar, m_cigar, k<<4|op);
+                // remove unnecessary "P" and possibly merge adjacent operations
+                for (i = 2; i < n_cigar; ++i) {
+                    if ((cigar[i]&0xf) != 1 && (cigar[i-1]&0xf) == 6 && (cigar[i-2]&0xf) != 1) {
+                        cigar[i-1] = 0;
+                        if ((cigar[i]&0xf) == (cigar[i-2]&0xf)) // merge operations
+                            cigar[i] += cigar[i-2], cigar[i-2] = 0;
+                    }
+                }
+                for (i = k = 0; i < n_cigar; ++i) // squeeze out dumb operations
+                    if (cigar[i]) cigar[k++] = cigar[i];
+                n_cigar = k;
+            }
+            if (end < t[2].l) write_cigar(cigar, n_cigar, m_cigar, (t[2].l - end)<<4|4);
+            // write the SAM line for the read
+            kputc('\t', &t[4]); // QNAME has already been written
+            kputw((af[af_i]&1)? 16 : 0, &t[4]); kputc('\t', &t[4]); // FLAG
+            kputsn(t[0].s, t[0].l, &t[4]); kputc('\t', &t[4]); // RNAME
+            kputw(is_padded? pos : p2u[pos-1]+1, &t[4]); // POS
+            kputs("\t60\t", &t[4]); // MAPQ
+            for (i = 0; i < n_cigar; ++i) { // CIGAR
+                kputw(cigar[i]>>4, &t[4]); kputc("MIDNSHP=X"[cigar[i]&0xf], &t[4]);
+            }
+            kputs("\t*\t0\t0\t", &t[4]); // empty MRNM, MPOS and TLEN
+            kputsn(t[3].s, t[3].l, &t[4]); // unpadded SEQ
+            kputs("\t*", &t[4]); // QUAL
+            puts(t[4].s); // print to stdout
+            ++af_i;
+        } else if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret);
+    }
+    ks_destroy(ks);
+    gzclose(fp);
+    free(af); free(s.s); free(cigar); free(p2u);
+    for (i = 0; i < N_TMPSTR; ++i) free(t[i].s);
+    return 0;
 }
diff --git a/samtools/misc/md5.c.pysam.c b/samtools/misc/md5.c.pysam.c
index 7710844..10469fc 100644
--- a/samtools/misc/md5.c.pysam.c
+++ b/samtools/misc/md5.c.pysam.c
@@ -30,6 +30,8 @@
 /*
  * Note: this code is harmless on little-endian machines.
  */
+void byteReverse(unsigned char *buf, unsigned longs);
+
 void byteReverse(buf, longs)
     unsigned char *buf; unsigned longs;
 {
@@ -154,7 +156,7 @@ void MD5Final(digest, ctx)
     MD5Transform(ctx->buf, (uint32_t *) ctx->in);
     byteReverse((unsigned char *) ctx->buf, 4);
     memcpy(digest, ctx->buf, 16);
-    memset(ctx, 0, sizeof(ctx));        /* In case it's sensitive */
+    memset(ctx, 0, sizeof(struct MD5Context));        /* In case it's sensitive */
 }
 
 
diff --git a/samtools/padding.c.pysam.c b/samtools/padding.c.pysam.c
index b736dcd..25ed0f4 100644
--- a/samtools/padding.c.pysam.c
+++ b/samtools/padding.c.pysam.c
@@ -1,481 +1,515 @@
 #include "pysam.h"
 
+/*  padding.c -- depad subcommand.
+
+    Copyright (C) 2011, 2012 Broad Institute.
+    Copyright (C) 2014 Genome Research Ltd.
+    Portions copyright (C) 2012, 2013 Peter Cock, The James Hutton Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <string.h>
 #include <assert.h>
 #include <unistd.h>
-#include "kstring.h"
+#include "htslib/kstring.h"
 #include "sam_header.h"
 #include "sam.h"
 #include "bam.h"
-#include "faidx.h"
+#include "htslib/faidx.h"
 
 bam_header_t *bam_header_dup(const bam_header_t *h0); /*in sam.c*/
 
 static void replace_cigar(bam1_t *b, int n, uint32_t *cigar)
 {
-	if (n != b->core.n_cigar) {
-		int o = b->core.l_qname + b->core.n_cigar * 4;
-		if (b->data_len + (n - b->core.n_cigar) * 4 > b->m_data) {
-			b->m_data = b->data_len + (n - b->core.n_cigar) * 4;
-			kroundup32(b->m_data);
-			b->data = (uint8_t*)realloc(b->data, b->m_data);
-		}
-		memmove(b->data + b->core.l_qname + n * 4, b->data + o, b->data_len - o);
-		memcpy(b->data + b->core.l_qname, cigar, n * 4);
-		b->data_len += (n - b->core.n_cigar) * 4;
-		b->core.n_cigar = n;
-	} else memcpy(b->data + b->core.l_qname, cigar, n * 4);
+    if (n != b->core.n_cigar) {
+        int o = b->core.l_qname + b->core.n_cigar * 4;
+        if (b->data_len + (n - b->core.n_cigar) * 4 > b->m_data) {
+            b->m_data = b->data_len + (n - b->core.n_cigar) * 4;
+            kroundup32(b->m_data);
+            b->data = (uint8_t*)realloc(b->data, b->m_data);
+        }
+        memmove(b->data + b->core.l_qname + n * 4, b->data + o, b->data_len - o);
+        memcpy(b->data + b->core.l_qname, cigar, n * 4);
+        b->data_len += (n - b->core.n_cigar) * 4;
+        b->core.n_cigar = n;
+    } else memcpy(b->data + b->core.l_qname, cigar, n * 4);
 }
 
 #define write_cigar(_c, _n, _m, _v) do { \
-		if (_n == _m) { \
-			_m = _m? _m<<1 : 4; \
-			_c = (uint32_t*)realloc(_c, _m * 4); \
-		} \
-		_c[_n++] = (_v); \
-	} while (0)
+        if (_n == _m) { \
+            _m = _m? _m<<1 : 4; \
+            _c = (uint32_t*)realloc(_c, _m * 4); \
+        } \
+        _c[_n++] = (_v); \
+    } while (0)
 
 static void unpad_seq(bam1_t *b, kstring_t *s)
 {
-	int k, j, i;
-	int length;
-	uint32_t *cigar = bam1_cigar(b);
-	uint8_t *seq = bam1_seq(b);
-	// b->core.l_qseq gives length of the SEQ entry (including soft clips, S)
-	// We need the padded length after alignment from the CIGAR (excluding
-	// soft clips S, but including pads from CIGAR D operations)
-	length = 0;
-	for (k = 0; k < b->core.n_cigar; ++k) {
-		int op, ol;
-		op= bam_cigar_op(cigar[k]);
-		ol = bam_cigar_oplen(cigar[k]);
-		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF || op == BAM_CDEL)
-			length += ol;
-	}
-	ks_resize(s, length);
-	for (k = 0, s->l = 0, j = 0; k < b->core.n_cigar; ++k) {
-		int op, ol;
-		op = bam_cigar_op(cigar[k]);
-		ol = bam_cigar_oplen(cigar[k]);
-		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
-			for (i = 0; i < ol; ++i, ++j) s->s[s->l++] = bam1_seqi(seq, j);
-		} else if (op == BAM_CSOFT_CLIP) {
-			j += ol;
-		} else if (op == BAM_CHARD_CLIP) {
-			/* do nothing */
-		} else if (op == BAM_CDEL) {
-			for (i = 0; i < ol; ++i) s->s[s->l++] = 0;
-                } else {
-			fprintf(pysamerr, "[depad] ERROR: Didn't expect CIGAR op %c in read %s\n", BAM_CIGAR_STR[op], bam1_qname(b));
-                        assert(-1);
-		}
-	}
-	assert(length == s->l);
+    int k, j, i;
+    int length;
+    uint32_t *cigar = bam1_cigar(b);
+    uint8_t *seq = bam1_seq(b);
+    // b->core.l_qseq gives length of the SEQ entry (including soft clips, S)
+    // We need the padded length after alignment from the CIGAR (excluding
+    // soft clips S, but including pads from CIGAR D operations)
+    length = 0;
+    for (k = 0; k < b->core.n_cigar; ++k) {
+        int op, ol;
+        op= bam_cigar_op(cigar[k]);
+        ol = bam_cigar_oplen(cigar[k]);
+        if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF || op == BAM_CDEL)
+            length += ol;
+    }
+    ks_resize(s, length);
+    for (k = 0, s->l = 0, j = 0; k < b->core.n_cigar; ++k) {
+        int op, ol;
+        op = bam_cigar_op(cigar[k]);
+        ol = bam_cigar_oplen(cigar[k]);
+        if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
+            for (i = 0; i < ol; ++i, ++j) s->s[s->l++] = bam1_seqi(seq, j);
+        } else if (op == BAM_CSOFT_CLIP) {
+            j += ol;
+        } else if (op == BAM_CHARD_CLIP) {
+            /* do nothing */
+        } else if (op == BAM_CDEL) {
+            for (i = 0; i < ol; ++i) s->s[s->l++] = 0;
+        } else {
+            fprintf(pysamerr, "[depad] ERROR: Didn't expect CIGAR op %c in read %s\n", BAM_CIGAR_STR[op], bam1_qname(b));
+            exit(1);
+        }
+    }
+    assert(length == s->l);
 }
 
 int load_unpadded_ref(faidx_t *fai, char *ref_name, int ref_len, kstring_t *seq)
 {
-	char base;
-	char *fai_ref = 0;
-	int fai_ref_len = 0, k;
+    char base;
+    char *fai_ref = 0;
+    int fai_ref_len = 0, k;
 
-	fai_ref = fai_fetch(fai, ref_name, &fai_ref_len);
-	if (fai_ref_len != ref_len) {
-		fprintf(pysamerr, "[depad] ERROR: FASTA sequence %s length %i, expected %i\n", ref_name, fai_ref_len, ref_len);
-		free(fai_ref);
-		return -1;
-	}
-	ks_resize(seq, ref_len);
-	seq->l = 0;
-	for (k = 0; k < ref_len; ++k) {
-		base = fai_ref[k];
-		if (base == '-' || base == '*') {
-			// Map gaps to null to match unpad_seq function
-			seq->s[seq->l++] = 0;
-		} else {
-			int i = bam_nt16_table[(int)base];
-			if (i == 0 || i==16) { // Equals maps to 0, anything unexpected to 16
-				fprintf(pysamerr, "[depad] ERROR: Invalid character %c (ASCII %i) in FASTA sequence %s\n", base, (int)base, ref_name);
-				free(fai_ref);
-				return -1;
-			}
-			seq->s[seq->l++] = i;
-		}
-	}
-	assert(ref_len == seq->l);
-	free(fai_ref);
-	return 0;
+    fai_ref = fai_fetch(fai, ref_name, &fai_ref_len);
+    if (fai_ref_len != ref_len) {
+        fprintf(pysamerr, "[depad] ERROR: FASTA sequence %s length %i, expected %i\n", ref_name, fai_ref_len, ref_len);
+        free(fai_ref);
+        return -1;
+    }
+    ks_resize(seq, ref_len);
+    seq->l = 0;
+    for (k = 0; k < ref_len; ++k) {
+        base = fai_ref[k];
+        if (base == '-' || base == '*') {
+            // Map gaps to null to match unpad_seq function
+            seq->s[seq->l++] = 0;
+        } else {
+            int i = bam_nt16_table[(int)base];
+            if (i == 0 || i==16) { // Equals maps to 0, anything unexpected to 16
+                fprintf(pysamerr, "[depad] ERROR: Invalid character %c (ASCII %i) in FASTA sequence %s\n", base, (int)base, ref_name);
+                free(fai_ref);
+                return -1;
+            }
+            seq->s[seq->l++] = i;
+        }
+    }
+    assert(ref_len == seq->l);
+    free(fai_ref);
+    return 0;
 }
 
 int get_unpadded_len(faidx_t *fai, char *ref_name, int padded_len)
 {
-	char base;
-	char *fai_ref = 0;
-	int fai_ref_len = 0, k;
-	int bases=0, gaps=0;
+    char base;
+    char *fai_ref = 0;
+    int fai_ref_len = 0, k;
+    int bases=0, gaps=0;
 
-	fai_ref = fai_fetch(fai, ref_name, &fai_ref_len);
-	if (fai_ref_len != padded_len) {
-		fprintf(pysamerr, "[depad] ERROR: FASTA sequence '%s' length %i, expected %i\n", ref_name, fai_ref_len, padded_len);
-		free(fai_ref);
-		return -1;
-	}
-	for (k = 0; k < padded_len; ++k) {
-		//fprintf(pysamerr, "[depad] checking base %i of %i or %i\n", k+1, ref_len, strlen(fai_ref));
-		base = fai_ref[k];
-		if (base == '-' || base == '*') {
-			gaps += 1;
-		} else {
-			int i = bam_nt16_table[(int)base];
-			if (i == 0 || i==16) { // Equals maps to 0, anything unexpected to 16
-				fprintf(pysamerr, "[depad] ERROR: Invalid character %c (ASCII %i) in FASTA sequence '%s'\n", base, (int)base, ref_name);
-				free(fai_ref);
-				return -1;
-			}
-			bases += 1;
-		}
-	}
-	free(fai_ref);
-	assert (padded_len == bases + gaps);
-	return bases;
+    fai_ref = fai_fetch(fai, ref_name, &fai_ref_len);
+    if (fai_ref_len != padded_len) {
+        fprintf(pysamerr, "[depad] ERROR: FASTA sequence '%s' length %i, expected %i\n", ref_name, fai_ref_len, padded_len);
+        free(fai_ref);
+        return -1;
+    }
+    for (k = 0; k < padded_len; ++k) {
+        //fprintf(pysamerr, "[depad] checking base %i of %i or %i\n", k+1, ref_len, strlen(fai_ref));
+        base = fai_ref[k];
+        if (base == '-' || base == '*') {
+            gaps += 1;
+        } else {
+            int i = bam_nt16_table[(int)base];
+            if (i == 0 || i==16) { // Equals maps to 0, anything unexpected to 16
+                fprintf(pysamerr, "[depad] ERROR: Invalid character %c (ASCII %i) in FASTA sequence '%s'\n", base, (int)base, ref_name);
+                free(fai_ref);
+                return -1;
+            }
+            bases += 1;
+        }
+    }
+    free(fai_ref);
+    assert (padded_len == bases + gaps);
+    return bases;
 }
 
-inline int * update_posmap(int *posmap, kstring_t ref)
+static inline int * update_posmap(int *posmap, kstring_t ref)
 {
-	int i, k;
-	posmap = realloc(posmap, ref.m * sizeof(int));
-	for (i = k = 0; i < ref.l; ++i) {
-		posmap[i] = k;
-		if (ref.s[i]) ++k;
-	}
-	return posmap;
+    int i, k;
+    posmap = realloc(posmap, ref.m * sizeof(int));
+    for (i = k = 0; i < ref.l; ++i) {
+        posmap[i] = k;
+        if (ref.s[i]) ++k;
+    }
+    return posmap;
 }
 
 int bam_pad2unpad(samfile_t *in, samfile_t *out, faidx_t *fai)
 {
-	bam_header_t *h = 0;
-	bam1_t *b = 0;
-	kstring_t r, q;
-	int r_tid = -1;
-	uint32_t *cigar2 = 0;
-	int ret = 0, n2 = 0, m2 = 0, *posmap = 0;
+    bam_header_t *h = 0;
+    bam1_t *b = 0;
+    kstring_t r, q;
+    int r_tid = -1;
+    uint32_t *cigar2 = 0;
+    int ret = 0, n2 = 0, m2 = 0, *posmap = 0;
 
-	b = bam_init1();
-	r.l = r.m = q.l = q.m = 0; r.s = q.s = 0;
-	int read_ret;
-	h = in->header;
-	while ((read_ret = samread(in, b)) >= 0) { // read one alignment from `in'
-		uint32_t *cigar = bam1_cigar(b);
-		n2 = 0;
-		if (b->core.pos == 0 && b->core.tid >= 0 && strcmp(bam1_qname(b), h->target_name[b->core.tid]) == 0) {
-			// fprintf(pysamerr, "[depad] Found embedded reference '%s'\n", bam1_qname(b));
-			r_tid = b->core.tid;
-			unpad_seq(b, &r);
-			if (h->target_len[r_tid] != r.l) {
-				fprintf(pysamerr, "[depad] ERROR: (Padded) length of '%s' is %d in BAM header, but %ld in embedded reference\n", bam1_qname(b), h->target_len[r_tid], r.l);
-				return -1;
-			}
-			if (fai) {
-				// Check the embedded reference matches the FASTA file
-				if (load_unpadded_ref(fai, h->target_name[b->core.tid], h->target_len[b->core.tid], &q)) {
-					fprintf(pysamerr, "[depad] ERROR: Failed to load embedded reference '%s' from FASTA\n", h->target_name[b->core.tid]);
-					return -1;
-				}
-				assert(r.l == q.l);
-				int i;
-				for (i = 0; i < r.l; ++i) {
-					if (r.s[i] != q.s[i]) {
-						// Show gaps as ASCII 45
-						fprintf(pysamerr, "[depad] ERROR: Embedded sequence and reference FASTA don't match for %s base %i, '%c' vs '%c'\n",
-							h->target_name[b->core.tid], i+1,
-							r.s[i] ? bam_nt16_rev_table[(int)r.s[i]] : 45,
-							q.s[i] ? bam_nt16_rev_table[(int)q.s[i]] : 45);
-						return -1;
-					}
-				}
-			}
-			write_cigar(cigar2, n2, m2, bam_cigar_gen(b->core.l_qseq, BAM_CMATCH));
-			replace_cigar(b, n2, cigar2);
-			posmap = update_posmap(posmap, r);
-		} else if (b->core.n_cigar > 0) {
-			int i, k, op;
-			if (b->core.tid < 0) {
-				fprintf(pysamerr, "[depad] ERROR: Read '%s' has CIGAR but no RNAME\n", bam1_qname(b));
-				return -1;
-			} else if (b->core.tid == r_tid) {
-				; // good case, reference available
-				//fprintf(pysamerr, "[depad] Have ref '%s' for read '%s'\n", h->target_name[b->core.tid], bam1_qname(b));
-			} else if (fai) {
-				if (load_unpadded_ref(fai, h->target_name[b->core.tid], h->target_len[b->core.tid], &r)) {
-					fprintf(pysamerr, "[depad] ERROR: Failed to load '%s' from reference FASTA\n", h->target_name[b->core.tid]);
-					return -1;
-				}
-				posmap = update_posmap(posmap, r);
-				r_tid = b->core.tid;
-				// fprintf(pysamerr, "[depad] Loaded %s from FASTA file\n", h->target_name[b->core.tid]);
-			} else {				
-				fprintf(pysamerr, "[depad] ERROR: Missing %s embedded reference sequence (and no FASTA file)\n", h->target_name[b->core.tid]);
-				return -1;
-			}
-			unpad_seq(b, &q);
-			if (bam_cigar_op(cigar[0]) == BAM_CSOFT_CLIP) {
-				write_cigar(cigar2, n2, m2, cigar[0]);
-			} else if (bam_cigar_op(cigar[0]) == BAM_CHARD_CLIP) {
-				write_cigar(cigar2, n2, m2, cigar[0]);
-				if (b->core.n_cigar > 2 && bam_cigar_op(cigar[1]) == BAM_CSOFT_CLIP) {
-					write_cigar(cigar2, n2, m2, cigar[1]);
-				}
-			}
-			/* Determine CIGAR operator for each base in the aligned read */
-			for (i = 0, k = b->core.pos; i < q.l; ++i, ++k)
-				q.s[i] = q.s[i]? (r.s[k]? BAM_CMATCH : BAM_CINS) : (r.s[k]? BAM_CDEL : BAM_CPAD);
-			/* Include any pads if starts with an insert */
-			if (q.s[0] == BAM_CINS) {
-				for (k = 0; k+1 < b->core.pos && !r.s[b->core.pos - k - 1]; ++k);
-				if (k) write_cigar(cigar2, n2, m2, bam_cigar_gen(k, BAM_CPAD));
-			}
-			/* Count consecutive CIGAR operators to turn into a CIGAR string */
-			for (i = k = 1, op = q.s[0]; i < q.l; ++i) {
-				if (op != q.s[i]) {
-					write_cigar(cigar2, n2, m2, bam_cigar_gen(k, op));
-					op = q.s[i]; k = 1;
-				} else ++k;
-			}
-			write_cigar(cigar2, n2, m2, bam_cigar_gen(k, op));
-			if (bam_cigar_op(cigar[b->core.n_cigar-1]) == BAM_CSOFT_CLIP) {
-				write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-1]);
-                        } else if (bam_cigar_op(cigar[b->core.n_cigar-1]) == BAM_CHARD_CLIP) {
-				if (b->core.n_cigar > 2 && bam_cigar_op(cigar[b->core.n_cigar-2]) == BAM_CSOFT_CLIP) {
-					write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-2]);
-			  	}
-				write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-1]);
-			}
-			/* Remove redundant P operators between M/X/=/D operators, e.g. 5M2P10M -> 15M */
-			int pre_op, post_op;
-			for (i = 2; i < n2; ++i)
-				if (bam_cigar_op(cigar2[i-1]) == BAM_CPAD) {
-					pre_op = bam_cigar_op(cigar2[i-2]);
-					post_op = bam_cigar_op(cigar2[i]);
-					/* Note don't need to check for X/= as code above will use M only */
-					if ((pre_op == BAM_CMATCH || pre_op == BAM_CDEL) && (post_op == BAM_CMATCH || post_op == BAM_CDEL)) {
-						/* This is a redundant P operator */
-						cigar2[i-1] = 0; // i.e. 0M
-						/* If had same operator either side, combine them in post_op */
-						if (pre_op == post_op) {
-							/* If CIGAR M, could treat as simple integers since BAM_CMATCH is zero*/
-							cigar2[i] = bam_cigar_gen(bam_cigar_oplen(cigar2[i-2]) + bam_cigar_oplen(cigar2[i]), post_op);
-							cigar2[i-2] = 0; // i.e. 0M
-						}
-					}
-				}
-			/* Remove the zero'd operators (0M) */
-			for (i = k = 0; i < n2; ++i)
-				if (cigar2[i]) cigar2[k++] = cigar2[i];
-			n2 = k;
-			replace_cigar(b, n2, cigar2);
-			b->core.pos = posmap[b->core.pos];
-			if (b->core.mtid < 0 || b->core.mpos < 0) {
-				/* Nice case, no mate to worry about*/
-				// fprintf(pysamerr, "[depad] Read '%s' mate not mapped\n", bam1_qname(b));
-				/* TODO - Warning if FLAG says mate should be mapped? */
-				/* Clean up funny input where mate position is given but mate reference is missing: */
-				b->core.mtid = -1;
-				b->core.mpos = -1;
-			} else if (b->core.mtid == b->core.tid) {
-				/* Nice case, same reference */
-				// fprintf(pysamerr, "[depad] Read '%s' mate mapped to same ref\n", bam1_qname(b));
-				b->core.mpos = posmap[b->core.mpos];
-			} else {
-				/* Nasty case, Must load alternative posmap */
-				// fprintf(pysamerr, "[depad] Loading reference '%s' temporarily\n", h->target_name[b->core.mtid]);
-				if (!fai) {
-					fprintf(pysamerr, "[depad] ERROR: Needed reference %s sequence for mate (and no FASTA file)\n", h->target_name[b->core.mtid]);
-					return -1;
-				}
-				/* Temporarily load the other reference sequence */
-				if (load_unpadded_ref(fai, h->target_name[b->core.mtid], h->target_len[b->core.mtid], &r)) {
-					fprintf(pysamerr, "[depad] ERROR: Failed to load '%s' from reference FASTA\n", h->target_name[b->core.mtid]);
-					return -1;
-				}
-				posmap = update_posmap(posmap, r);
-				b->core.mpos = posmap[b->core.mpos];
-				/* Restore the reference and posmap*/
-				if (load_unpadded_ref(fai, h->target_name[b->core.tid], h->target_len[b->core.tid], &r)) {
-					fprintf(pysamerr, "[depad] ERROR: Failed to load '%s' from reference FASTA\n", h->target_name[b->core.tid]);
-					return -1;
-				}
-				posmap = update_posmap(posmap, r);
-			}
-		}
-		samwrite(out, b);
-	}
-	if (read_ret < -1) {
-		fprintf(pysamerr, "[depad] truncated file.\n");
-		ret = 1;
-	}
-	free(r.s); free(q.s); free(posmap);
-	bam_destroy1(b);
-	return ret;
+    b = bam_init1();
+    r.l = r.m = q.l = q.m = 0; r.s = q.s = 0;
+    int read_ret;
+    h = in->header;
+    while ((read_ret = samread(in, b)) >= 0) { // read one alignment from `in'
+        uint32_t *cigar = bam1_cigar(b);
+        n2 = 0;
+        if (b->core.pos == 0 && b->core.tid >= 0 && strcmp(bam1_qname(b), h->target_name[b->core.tid]) == 0) {
+            // fprintf(pysamerr, "[depad] Found embedded reference '%s'\n", bam1_qname(b));
+            r_tid = b->core.tid;
+            unpad_seq(b, &r);
+            if (h->target_len[r_tid] != r.l) {
+                fprintf(pysamerr, "[depad] ERROR: (Padded) length of '%s' is %d in BAM header, but %ld in embedded reference\n", bam1_qname(b), h->target_len[r_tid], r.l);
+                return -1;
+            }
+            if (fai) {
+                // Check the embedded reference matches the FASTA file
+                if (load_unpadded_ref(fai, h->target_name[b->core.tid], h->target_len[b->core.tid], &q)) {
+                    fprintf(pysamerr, "[depad] ERROR: Failed to load embedded reference '%s' from FASTA\n", h->target_name[b->core.tid]);
+                    return -1;
+                }
+                assert(r.l == q.l);
+                int i;
+                for (i = 0; i < r.l; ++i) {
+                    if (r.s[i] != q.s[i]) {
+                        // Show gaps as ASCII 45
+                        fprintf(pysamerr, "[depad] ERROR: Embedded sequence and reference FASTA don't match for %s base %i, '%c' vs '%c'\n",
+                            h->target_name[b->core.tid], i+1,
+                            r.s[i] ? bam_nt16_rev_table[(int)r.s[i]] : 45,
+                            q.s[i] ? bam_nt16_rev_table[(int)q.s[i]] : 45);
+                        return -1;
+                    }
+                }
+            }
+            write_cigar(cigar2, n2, m2, bam_cigar_gen(b->core.l_qseq, BAM_CMATCH));
+            replace_cigar(b, n2, cigar2);
+            posmap = update_posmap(posmap, r);
+        } else if (b->core.n_cigar > 0) {
+            int i, k, op;
+            if (b->core.tid < 0) {
+                fprintf(pysamerr, "[depad] ERROR: Read '%s' has CIGAR but no RNAME\n", bam1_qname(b));
+                return -1;
+            } else if (b->core.tid == r_tid) {
+                ; // good case, reference available
+                //fprintf(pysamerr, "[depad] Have ref '%s' for read '%s'\n", h->target_name[b->core.tid], bam1_qname(b));
+            } else if (fai) {
+                if (load_unpadded_ref(fai, h->target_name[b->core.tid], h->target_len[b->core.tid], &r)) {
+                    fprintf(pysamerr, "[depad] ERROR: Failed to load '%s' from reference FASTA\n", h->target_name[b->core.tid]);
+                    return -1;
+                }
+                posmap = update_posmap(posmap, r);
+                r_tid = b->core.tid;
+                // fprintf(pysamerr, "[depad] Loaded %s from FASTA file\n", h->target_name[b->core.tid]);
+            } else {
+                fprintf(pysamerr, "[depad] ERROR: Missing %s embedded reference sequence (and no FASTA file)\n", h->target_name[b->core.tid]);
+                return -1;
+            }
+            unpad_seq(b, &q);
+            if (bam_cigar_op(cigar[0]) == BAM_CSOFT_CLIP) {
+                write_cigar(cigar2, n2, m2, cigar[0]);
+            } else if (bam_cigar_op(cigar[0]) == BAM_CHARD_CLIP) {
+                write_cigar(cigar2, n2, m2, cigar[0]);
+                if (b->core.n_cigar > 2 && bam_cigar_op(cigar[1]) == BAM_CSOFT_CLIP) {
+                    write_cigar(cigar2, n2, m2, cigar[1]);
+                }
+            }
+            /* Determine CIGAR operator for each base in the aligned read */
+            for (i = 0, k = b->core.pos; i < q.l; ++i, ++k)
+                q.s[i] = q.s[i]? (r.s[k]? BAM_CMATCH : BAM_CINS) : (r.s[k]? BAM_CDEL : BAM_CPAD);
+            /* Include any pads if starts with an insert */
+            if (q.s[0] == BAM_CINS) {
+                for (k = 0; k+1 < b->core.pos && !r.s[b->core.pos - k - 1]; ++k);
+                if (k) write_cigar(cigar2, n2, m2, bam_cigar_gen(k, BAM_CPAD));
+            }
+            /* Count consecutive CIGAR operators to turn into a CIGAR string */
+            for (i = k = 1, op = q.s[0]; i < q.l; ++i) {
+                if (op != q.s[i]) {
+                    write_cigar(cigar2, n2, m2, bam_cigar_gen(k, op));
+                    op = q.s[i]; k = 1;
+                } else ++k;
+            }
+            write_cigar(cigar2, n2, m2, bam_cigar_gen(k, op));
+            if (bam_cigar_op(cigar[b->core.n_cigar-1]) == BAM_CSOFT_CLIP) {
+                write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-1]);
+            } else if (bam_cigar_op(cigar[b->core.n_cigar-1]) == BAM_CHARD_CLIP) {
+                if (b->core.n_cigar > 2 && bam_cigar_op(cigar[b->core.n_cigar-2]) == BAM_CSOFT_CLIP) {
+                    write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-2]);
+                }
+                write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-1]);
+            }
+            /* Remove redundant P operators between M/X/=/D operators, e.g. 5M2P10M -> 15M */
+            int pre_op, post_op;
+            for (i = 2; i < n2; ++i)
+                if (bam_cigar_op(cigar2[i-1]) == BAM_CPAD) {
+                    pre_op = bam_cigar_op(cigar2[i-2]);
+                    post_op = bam_cigar_op(cigar2[i]);
+                    /* Note don't need to check for X/= as code above will use M only */
+                    if ((pre_op == BAM_CMATCH || pre_op == BAM_CDEL) && (post_op == BAM_CMATCH || post_op == BAM_CDEL)) {
+                        /* This is a redundant P operator */
+                        cigar2[i-1] = 0; // i.e. 0M
+                        /* If had same operator either side, combine them in post_op */
+                        if (pre_op == post_op) {
+                            /* If CIGAR M, could treat as simple integers since BAM_CMATCH is zero*/
+                            cigar2[i] = bam_cigar_gen(bam_cigar_oplen(cigar2[i-2]) + bam_cigar_oplen(cigar2[i]), post_op);
+                            cigar2[i-2] = 0; // i.e. 0M
+                        }
+                    }
+                }
+            /* Remove the zero'd operators (0M) */
+            for (i = k = 0; i < n2; ++i)
+                if (cigar2[i]) cigar2[k++] = cigar2[i];
+            n2 = k;
+            replace_cigar(b, n2, cigar2);
+        }
+        /* Even unmapped reads can have a POS value, e.g. if their mate was mapped */
+        if (b->core.pos != -1) b->core.pos = posmap[b->core.pos];
+        if (b->core.mtid < 0 || b->core.mpos < 0) {
+            /* Nice case, no mate to worry about*/
+            // fprintf(pysamerr, "[depad] Read '%s' mate not mapped\n", bam1_qname(b));
+            /* TODO - Warning if FLAG says mate should be mapped? */
+            /* Clean up funny input where mate position is given but mate reference is missing: */
+            b->core.mtid = -1;
+            b->core.mpos = -1;
+        } else if (b->core.mtid == b->core.tid) {
+            /* Nice case, same reference */
+            // fprintf(pysamerr, "[depad] Read '%s' mate mapped to same ref\n", bam1_qname(b));
+            b->core.mpos = posmap[b->core.mpos];
+        } else {
+            /* Nasty case, Must load alternative posmap */
+            // fprintf(pysamerr, "[depad] Loading reference '%s' temporarily\n", h->target_name[b->core.mtid]);
+            if (!fai) {
+                fprintf(pysamerr, "[depad] ERROR: Needed reference %s sequence for mate (and no FASTA file)\n", h->target_name[b->core.mtid]);
+                return -1;
+            }
+            /* Temporarily load the other reference sequence */
+            if (load_unpadded_ref(fai, h->target_name[b->core.mtid], h->target_len[b->core.mtid], &r)) {
+                fprintf(pysamerr, "[depad] ERROR: Failed to load '%s' from reference FASTA\n", h->target_name[b->core.mtid]);
+                return -1;
+            }
+            posmap = update_posmap(posmap, r);
+            b->core.mpos = posmap[b->core.mpos];
+            /* Restore the reference and posmap*/
+            if (load_unpadded_ref(fai, h->target_name[b->core.tid], h->target_len[b->core.tid], &r)) {
+                fprintf(pysamerr, "[depad] ERROR: Failed to load '%s' from reference FASTA\n", h->target_name[b->core.tid]);
+                return -1;
+            }
+            posmap = update_posmap(posmap, r);
+        }
+        /* Most reads will have been moved so safest to always recalculate the BIN value */
+        b->core.bin = bam_reg2bin(b->core.pos, bam_calend(&b->core, bam1_cigar(b)));
+        samwrite(out, b);
+    }
+    if (read_ret < -1) {
+        fprintf(pysamerr, "[depad] truncated file.\n");
+        ret = 1;
+    }
+    free(r.s); free(q.s); free(posmap);
+    bam_destroy1(b);
+    return ret;
 }
 
 bam_header_t * fix_header(bam_header_t *old, faidx_t *fai)
 {
-	int i = 0, unpadded_len = 0;
-	bam_header_t *header = 0 ;
+#if 0
+    int i = 0, unpadded_len = 0;
+    bam_header_t *header = 0 ;
 
-	header = bam_header_dup(old);
-	for (i = 0; i < old->n_targets; ++i) {
-		unpadded_len = get_unpadded_len(fai, old->target_name[i], old->target_len[i]);
-		if (unpadded_len < 0) {
-			fprintf(pysamerr, "[depad] ERROR getting unpadded length of '%s', padded length %i\n", old->target_name[i], old->target_len[i]);
-		} else {
-			header->target_len[i] = unpadded_len;
-			//fprintf(pysamerr, "[depad] Recalculating '%s' length %i -> %i\n", old->target_name[i], old->target_len[i], header->target_len[i]);
-		}
-	}
-	/* Duplicating the header allocated new buffer for header string */
-	/* After modifying the @SQ lines it will only get smaller, since */
-	/* the LN entries will be the same or shorter, and we'll remove */
-	/* any MD entries (MD5 checksums). */
-	assert(strlen(old->text) == strlen(header->text));
-	assert (0==strcmp(old->text, header->text));
-	const char *text;
-	text = old->text;
-	header->text[0] = '\0'; /* Resuse the allocated buffer */
-	char * newtext = header->text;
-	char * end=NULL;
-	while (text[0]=='@') {
-		end = strchr(text, '\n');
-		assert(end != 0);
-		if (text[1]=='S' && text[2]=='Q' && text[3]=='\t') {
-			/* TODO - edit the @SQ line here to remove MD and fix LN. */
-			/* For now just remove the @SQ line, and samtools will */
-			/* automatically generate a minimal replacement with LN. */
-			/* However, that discards any other tags like AS, SP, UR. */
-			//fprintf(pysamerr, "[depad] Removing @SQ line\n");
-		} else {
-			/* Copy this line to the new header */
-			strncat(newtext, text, end - text + 1);
-		}
-		text = end + 1;
-	}
-	assert (text[0]=='\0');
-	/* Check we didn't overflow the buffer */
-	assert (strlen(header->text) <= strlen(old->text));
-	if (strlen(header->text) < header->l_text) {
-		//fprintf(pysamerr, "[depad] Reallocating header buffer\n");
-		assert (newtext == header->text);
-		newtext = malloc(strlen(header->text) + 1);
-		strcpy(newtext, header->text);
-		free(header->text);
-		header->text = newtext;
-		header->l_text = strlen(newtext);
-	}
-	//fprintf(pysamerr, "[depad] Here is the new header (pending @SQ lines),\n\n%s\n(end)\n", header->text);
-	return header;
+    header = bam_header_dup(old);
+    for (i = 0; i < old->n_targets; ++i) {
+        unpadded_len = get_unpadded_len(fai, old->target_name[i], old->target_len[i]);
+        if (unpadded_len < 0) {
+            fprintf(pysamerr, "[depad] ERROR getting unpadded length of '%s', padded length %i\n", old->target_name[i], old->target_len[i]);
+        } else {
+            header->target_len[i] = unpadded_len;
+            //fprintf(pysamerr, "[depad] Recalculating '%s' length %i -> %i\n", old->target_name[i], old->target_len[i], header->target_len[i]);
+        }
+    }
+    /* Duplicating the header allocated new buffer for header string */
+    /* After modifying the @SQ lines it will only get smaller, since */
+    /* the LN entries will be the same or shorter, and we'll remove */
+    /* any MD entries (MD5 checksums). */
+    assert(strlen(old->text) == strlen(header->text));
+    assert (0==strcmp(old->text, header->text));
+    const char *text;
+    text = old->text;
+    header->text[0] = '\0'; /* Resuse the allocated buffer */
+    char * newtext = header->text;
+    char * end=NULL;
+    while (text[0]=='@') {
+        end = strchr(text, '\n');
+        assert(end != 0);
+        if (text[1]=='S' && text[2]=='Q' && text[3]=='\t') {
+            /* TODO - edit the @SQ line here to remove MD and fix LN. */
+            /* For now just remove the @SQ line, and samtools will */
+            /* automatically generate a minimal replacement with LN. */
+            /* However, that discards any other tags like AS, SP, UR. */
+            //fprintf(pysamerr, "[depad] Removing @SQ line\n");
+        } else {
+            /* Copy this line to the new header */
+            strncat(newtext, text, end - text + 1);
+        }
+        text = end + 1;
+    }
+    assert (text[0]=='\0');
+    /* Check we didn't overflow the buffer */
+    assert (strlen(header->text) <= strlen(old->text));
+    if (strlen(header->text) < header->l_text) {
+        //fprintf(pysamerr, "[depad] Reallocating header buffer\n");
+        assert (newtext == header->text);
+        newtext = malloc(strlen(header->text) + 1);
+        strcpy(newtext, header->text);
+        free(header->text);
+        header->text = newtext;
+        header->l_text = strlen(newtext);
+    }
+    //fprintf(pysamerr, "[depad] Here is the new header (pending @SQ lines),\n\n%s\n(end)\n", header->text);
+    return header;
+#else
+    fprintf(pysamerr, "Samtools-htslib: fix_header() header parsing not yet implemented\n");
+    abort();
+#endif
 }
 
 static int usage(int is_long_help);
 
 int main_pad2unpad(int argc, char *argv[])
 {
-	samfile_t *in = 0, *out = 0;
-        bam_header_t *h = 0;
-	faidx_t *fai = 0;
-	int c, is_bamin = 1, compress_level = -1, is_bamout = 1, is_long_help = 0;
-	char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0;
-        int ret=0;
+    samfile_t *in = 0, *out = 0;
+    bam_header_t *h = 0;
+    faidx_t *fai = 0;
+    int c, is_bamin = 1, compress_level = -1, is_bamout = 1, is_long_help = 0;
+    char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0;
+    int ret=0;
 
-	/* parse command-line options */
-	strcpy(in_mode, "r"); strcpy(out_mode, "w");
-	while ((c = getopt(argc, argv, "Sso:u1T:?")) >= 0) {
-		switch (c) {
-		case 'S': is_bamin = 0; break;
-		case 's': assert(compress_level == -1); is_bamout = 0; break;
-		case 'o': fn_out = strdup(optarg); break;
-		case 'u': assert(is_bamout == 1); compress_level = 0; break;
-		case '1': assert(is_bamout == 1); compress_level = 1; break;
-		case 'T': fn_ref = strdup(optarg); break;
-                case '?': is_long_help = 1; break;
-		default: return usage(is_long_help);
-		}
+    /* parse command-line options */
+    strcpy(in_mode, "r"); strcpy(out_mode, "w");
+    while ((c = getopt(argc, argv, "Sso:u1T:?")) >= 0) {
+        switch (c) {
+        case 'S': is_bamin = 0; break;
+        case 's': assert(compress_level == -1); is_bamout = 0; break;
+        case 'o': fn_out = strdup(optarg); break;
+        case 'u': assert(is_bamout == 1); compress_level = 0; break;
+        case '1': assert(is_bamout == 1); compress_level = 1; break;
+        case 'T': fn_ref = strdup(optarg); break;
+        case '?': is_long_help = 1; break;
+        default: return usage(is_long_help);
         }
-	if (argc == optind) return usage(is_long_help);
+    }
+    if (argc == optind) return usage(is_long_help);
 
-	if (is_bamin) strcat(in_mode, "b");
-	if (is_bamout) strcat(out_mode, "b");
-	strcat(out_mode, "h");
-	if (compress_level >= 0) {
-		char tmp[2];
-		tmp[0] = compress_level + '0'; tmp[1] = '\0';
-		strcat(out_mode, tmp);
-	}
+    if (is_bamin) strcat(in_mode, "b");
+    if (is_bamout) strcat(out_mode, "b");
+    strcat(out_mode, "h");
+    if (compress_level >= 0) {
+        char tmp[2];
+        tmp[0] = compress_level + '0'; tmp[1] = '\0';
+        strcat(out_mode, tmp);
+    }
 
-	// Load FASTA reference (also needed for SAM -> BAM if missing header)
-	if (fn_ref) {
-		fn_list = samfaipath(fn_ref);
-		fai = fai_load(fn_ref);
-	}
-	// open file handlers
-	if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) {
-		fprintf(pysamerr, "[depad] failed to open \"%s\" for reading.\n", argv[optind]);
-		ret = 1;
-		goto depad_end;
-	}
-	if (in->header == 0) {
-		fprintf(pysamerr, "[depad] failed to read the header from \"%s\".\n", argv[optind]);
-		ret = 1;
-		goto depad_end;
-	}
-	if (in->header->text == 0 || in->header->l_text == 0) {
-		fprintf(pysamerr, "[depad] Warning - failed to read any header text from \"%s\".\n", argv[optind]);
-		assert (0 == in->header->l_text);
-		assert (0 == in->header->text);
-	}
-	if (fn_ref) {
-		h = fix_header(in->header, fai);
-	} else {
-		fprintf(pysamerr, "[depad] Warning - reference lengths will not be corrected without FASTA reference\n");
-		h = in->header;
-	}
-	if ((out = samopen(fn_out? fn_out : "-", out_mode, h)) == 0) {
-		fprintf(pysamerr, "[depad] failed to open \"%s\" for writing.\n", fn_out? fn_out : "standard output");
-		ret = 1;
-		goto depad_end;
-	}
+    // Load FASTA reference (also needed for SAM -> BAM if missing header)
+    if (fn_ref) {
+        fn_list = samfaipath(fn_ref);
+        fai = fai_load(fn_ref);
+    }
+    // open file handlers
+    if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) {
+        fprintf(pysamerr, "[depad] failed to open \"%s\" for reading.\n", argv[optind]);
+        ret = 1;
+        goto depad_end;
+    }
+    if (in->header == 0) {
+        fprintf(pysamerr, "[depad] failed to read the header from \"%s\".\n", argv[optind]);
+        ret = 1;
+        goto depad_end;
+    }
+    if (in->header->text == 0 || in->header->l_text == 0) {
+        fprintf(pysamerr, "[depad] Warning - failed to read any header text from \"%s\".\n", argv[optind]);
+        assert (0 == in->header->l_text);
+        assert (0 == in->header->text);
+    }
+    if (fn_ref) {
+        h = fix_header(in->header, fai);
+    } else {
+        fprintf(pysamerr, "[depad] Warning - reference lengths will not be corrected without FASTA reference\n");
+        h = in->header;
+    }
+    if ((out = samopen(fn_out? fn_out : "-", out_mode, h)) == 0) {
+        fprintf(pysamerr, "[depad] failed to open \"%s\" for writing.\n", fn_out? fn_out : "standard output");
+        ret = 1;
+        goto depad_end;
+    }
 
-	// Do the depad
-	ret = bam_pad2unpad(in, out, fai);
+    // Do the depad
+    ret = bam_pad2unpad(in, out, fai);
 
 depad_end:
-	// close files, free and return
-	if (fai) fai_destroy(fai);
-	if (h != in->header) bam_header_destroy(h);
-	samclose(in);
-	samclose(out);
-	free(fn_list); free(fn_out);
-	return ret;
+    // close files, free and return
+    if (fai) fai_destroy(fai);
+    if (! (in && h == in->header)) bam_header_destroy(h);
+    samclose(in);
+    samclose(out);
+    free(fn_list); free(fn_out);
+    return ret;
 }
 
 static int usage(int is_long_help)
 {
-	fprintf(pysamerr, "\n");
-	fprintf(pysamerr, "Usage:   samtools depad <in.bam>\n\n");
-	fprintf(pysamerr, "Options: -s       output is SAM (default is BAM)\n");
-	fprintf(pysamerr, "         -S       input is SAM (default is BAM)\n");
-	fprintf(pysamerr, "         -u       uncompressed BAM output (can't use with -s)\n");
-	fprintf(pysamerr, "         -1       fast compression BAM output (can't use with -s)\n");
-	fprintf(pysamerr, "         -T FILE  reference sequence file [null]\n");
-	fprintf(pysamerr, "         -o FILE  output file name [stdout]\n");
-	fprintf(pysamerr, "         -?       longer help\n");
-	fprintf(pysamerr, "\n");
-	if (is_long_help)
-		fprintf(pysamerr, "Notes:\n\
+    fprintf(pysamerr, "\n");
+    fprintf(pysamerr, "Usage:   samtools depad <in.bam>\n\n");
+    fprintf(pysamerr, "Options: -s       output is SAM (default is BAM)\n");
+    fprintf(pysamerr, "         -S       input is SAM (default is BAM)\n");
+    fprintf(pysamerr, "         -u       uncompressed BAM output (can't use with -s)\n");
+    fprintf(pysamerr, "         -1       fast compression BAM output (can't use with -s)\n");
+    fprintf(pysamerr, "         -T FILE  padded reference sequence file [null]\n");
+    fprintf(pysamerr, "         -o FILE  output file name [stdout]\n");
+    fprintf(pysamerr, "         -?       longer help\n");
+    fprintf(pysamerr, "\n");
+    if (is_long_help)
+        fprintf(pysamerr, "Notes:\n\
 \n\
   1. Requires embedded reference sequences (before the reads for that reference),\n\
-     with the future aim to also support a FASTA padded reference sequence file.\n\
+     or ideally a FASTA file of the padded reference sequences (via the -T argument).\n\
 \n\
   2. The input padded alignment read's CIGAR strings must not use P or I operators.\n\
 \n");
-        return 1;
+    return 1;
 }
diff --git a/samtools/phase.c.pysam.c b/samtools/phase.c.pysam.c
index d142538..420f759 100644
--- a/samtools/phase.c.pysam.c
+++ b/samtools/phase.c.pysam.c
@@ -1,15 +1,40 @@
 #include "pysam.h"
 
+/*  phase.c -- phase subcommand.
+
+    Copyright (C) 2011 Broad Institute.
+    Copyright (C) 2013, 2014 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdint.h>
 #include <math.h>
 #include <zlib.h>
-#include "bam.h"
+#include "htslib/sam.h"
 #include "errmod.h"
 
-#include "kseq.h"
+#include "htslib/kseq.h"
 KSTREAM_INIT(gzFile, gzread, 16384)
 
 #define MAX_VARS 256
@@ -22,668 +47,678 @@ KSTREAM_INIT(gzFile, gzread, 16384)
 #define FLAG_DROP_AMBI   0x8
 
 typedef struct {
-	// configurations, initialized in the main function
-	int flag, k, min_baseQ, min_varLOD, max_depth;
-	// other global variables
-	int vpos_shift;
-	bamFile fp;
-	char *pre;
-	bamFile out[3];
-	// alignment queue
-	int n, m;
-	bam1_t **b;
+    // configurations, initialized in the main function
+    int flag, k, min_baseQ, min_varLOD, max_depth;
+    // other global variables
+    int vpos_shift;
+    samFile* fp;
+    bam_hdr_t* fp_hdr;
+    char *pre;
+    samFile* out[3];
+    bam_hdr_t* out_hdr[3];
+    // alignment queue
+    int n, m;
+    bam1_t **b;
 } phaseg_t;
 
 typedef struct {
-	int8_t seq[MAX_VARS]; // TODO: change to dynamic memory allocation!
-	int vpos, beg, end;
-	uint32_t vlen:16, single:1, flip:1, phase:1, phased:1, ambig:1;
-	uint32_t in:16, out:16; // in-phase and out-phase
+    int8_t seq[MAX_VARS]; // TODO: change to dynamic memory allocation!
+    int vpos, beg, end;
+    uint32_t vlen:16, single:1, flip:1, phase:1, phased:1, ambig:1;
+    uint32_t in:16, out:16; // in-phase and out-phase
 } frag_t, *frag_p;
 
 #define rseq_lt(a,b) ((a)->vpos < (b)->vpos)
 
-#include "khash.h"
+#include "htslib/khash.h"
 KHASH_SET_INIT_INT64(set64)
 KHASH_MAP_INIT_INT64(64, frag_t)
 
 typedef khash_t(64) nseq_t;
 
-#include "ksort.h"
+#include "htslib/ksort.h"
 KSORT_INIT(rseq, frag_p, rseq_lt)
 
-static char nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 };
+extern const char bam_nt16_nt4_table[];
 
 static inline uint64_t X31_hash_string(const char *s)
 {
-	uint64_t h = *s;
-	if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
-	return h;
+    uint64_t h = *s;
+    if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
+    return h;
 }
 
 static void count1(int l, const uint8_t *seq, int *cnt)
 {
-	int i, j, n_ambi;
-	uint32_t z, x;
-	if (seq[l-1] == 0) return; // do nothing is the last base is ambiguous
-	for (i = n_ambi = 0; i < l; ++i) // collect ambiguous bases
-		if (seq[i] == 0) ++n_ambi;
-	if (l - n_ambi <= 1) return; // only one SNP
-	for (x = 0; x < 1u<<n_ambi; ++x) { // count
-		for (i = j = 0, z = 0; i < l; ++i) {
-			int c;
-			if (seq[i]) c = seq[i] - 1;
-			else {
-				c = x>>j&1;
-				++j;
-			}
-			z = z<<1 | c;
-		}
-		++cnt[z];
-	}
+    int i, j, n_ambi;
+    uint32_t z, x;
+    if (seq[l-1] == 0) return; // do nothing is the last base is ambiguous
+    for (i = n_ambi = 0; i < l; ++i) // collect ambiguous bases
+        if (seq[i] == 0) ++n_ambi;
+    if (l - n_ambi <= 1) return; // only one SNP
+    for (x = 0; x < 1u<<n_ambi; ++x) { // count
+        for (i = j = 0, z = 0; i < l; ++i) {
+            int c;
+            if (seq[i]) c = seq[i] - 1;
+            else {
+                c = x>>j&1;
+                ++j;
+            }
+            z = z<<1 | c;
+        }
+        ++cnt[z];
+    }
 }
 
 static int **count_all(int l, int vpos, nseq_t *hash)
 {
-	khint_t k;
-	int i, j, **cnt;
-	uint8_t *seq;
-	seq = calloc(l, 1);
-	cnt = calloc(vpos, sizeof(void*));
-	for (i = 0; i < vpos; ++i) cnt[i] = calloc(1<<l, sizeof(int));
-	for (k = 0; k < kh_end(hash); ++k) {
-		if (kh_exist(hash, k)) {
-			frag_t *f = &kh_val(hash, k);
-			if (f->vpos >= vpos || f->single) continue; // out of region; or singleton
-			if (f->vlen == 1) { // such reads should be flagged as deleted previously if everything is right
-				f->single = 1;
-				continue;
-			}
-			for (j = 1; j < f->vlen; ++j) {
-				for (i = 0; i < l; ++i)
-					seq[i] = j < l - 1 - i? 0 : f->seq[j - (l - 1 - i)];
-				count1(l, seq, cnt[f->vpos + j]);
-			}
-		}
-	}
-	free(seq);
-	return cnt;
+    khint_t k;
+    int i, j, **cnt;
+    uint8_t *seq;
+    seq = calloc(l, 1);
+    cnt = calloc(vpos, sizeof(int*));
+    for (i = 0; i < vpos; ++i) cnt[i] = calloc(1<<l, sizeof(int));
+    for (k = 0; k < kh_end(hash); ++k) {
+        if (kh_exist(hash, k)) {
+            frag_t *f = &kh_val(hash, k);
+            if (f->vpos >= vpos || f->single) continue; // out of region; or singleton
+            if (f->vlen == 1) { // such reads should be flagged as deleted previously if everything is right
+                f->single = 1;
+                continue;
+            }
+            for (j = 1; j < f->vlen; ++j) {
+                for (i = 0; i < l; ++i)
+                    seq[i] = j < l - 1 - i? 0 : f->seq[j - (l - 1 - i)];
+                count1(l, seq, cnt[f->vpos + j]);
+            }
+        }
+    }
+    free(seq);
+    return cnt;
 }
 
 // phasing
 static int8_t *dynaprog(int l, int vpos, int **w)
 {
-	int *f[2], *curr, *prev, max, i;
-	int8_t **b, *h = 0;
-	uint32_t x, z = 1u<<(l-1), mask = (1u<<l) - 1;
-	f[0] = calloc(z, sizeof(int));
-	f[1] = calloc(z, sizeof(int));
-	b = calloc(vpos, sizeof(void*));
-	prev = f[0]; curr = f[1];
-	// fill the backtrack matrix
-	for (i = 0; i < vpos; ++i) {
-		int *wi = w[i], *tmp;
-		int8_t *bi;
-		bi = b[i] = calloc(z, 1);
-		/* In the following, x is the current state, which is the
-		 * lexicographically smaller local haplotype. xc is the complement of
-		 * x, or the larger local haplotype; y0 and y1 are the two predecessors
-		 * of x. */
-		for (x = 0; x < z; ++x) { // x0 is the smaller 
-			uint32_t y0, y1, xc;
-			int c0, c1;
-			xc = ~x&mask; y0 = x>>1; y1 = xc>>1;
-			c0 = prev[y0] + wi[x] + wi[xc];
-			c1 = prev[y1] + wi[x] + wi[xc];
-			if (c0 > c1) bi[x] = 0, curr[x] = c0;
-			else bi[x] = 1, curr[x] = c1;
-		}
-		tmp = prev; prev = curr; curr = tmp; // swap
-	}
-	{ // backtrack
-		uint32_t max_x = 0;
-		int which = 0;
-		h = calloc(vpos, 1);
-		for (x = 0, max = 0, max_x = 0; x < z; ++x)
-			if (prev[x] > max) max = prev[x], max_x = x;
-		for (i = vpos - 1, x = max_x; i >= 0; --i) {
-			h[i] = which? (~x&1) : (x&1);
-			which = b[i][x]? !which : which;
-			x = b[i][x]? (~x&mask)>>1 : x>>1;
-		}
-	}
-	// free
-	for (i = 0; i < vpos; ++i) free(b[i]);
-	free(f[0]); free(f[1]); free(b);
-	return h;
+    int *f[2], *curr, *prev, max, i;
+    int8_t **b, *h = 0;
+    uint32_t x, z = 1u<<(l-1), mask = (1u<<l) - 1;
+    f[0] = calloc(z, sizeof(int));
+    f[1] = calloc(z, sizeof(int));
+    b = calloc(vpos, sizeof(int8_t*));
+    prev = f[0]; curr = f[1];
+    // fill the backtrack matrix
+    for (i = 0; i < vpos; ++i) {
+        int *wi = w[i], *tmp;
+        int8_t *bi;
+        bi = b[i] = calloc(z, 1);
+        /* In the following, x is the current state, which is the
+         * lexicographically smaller local haplotype. xc is the complement of
+         * x, or the larger local haplotype; y0 and y1 are the two predecessors
+         * of x. */
+        for (x = 0; x < z; ++x) { // x0 is the smaller
+            uint32_t y0, y1, xc;
+            int c0, c1;
+            xc = ~x&mask; y0 = x>>1; y1 = xc>>1;
+            c0 = prev[y0] + wi[x] + wi[xc];
+            c1 = prev[y1] + wi[x] + wi[xc];
+            if (c0 > c1) bi[x] = 0, curr[x] = c0;
+            else bi[x] = 1, curr[x] = c1;
+        }
+        tmp = prev; prev = curr; curr = tmp; // swap
+    }
+    { // backtrack
+        uint32_t max_x = 0;
+        int which = 0;
+        h = calloc(vpos, 1);
+        for (x = 0, max = 0, max_x = 0; x < z; ++x)
+            if (prev[x] > max) max = prev[x], max_x = x;
+        for (i = vpos - 1, x = max_x; i >= 0; --i) {
+            h[i] = which? (~x&1) : (x&1);
+            which = b[i][x]? !which : which;
+            x = b[i][x]? (~x&mask)>>1 : x>>1;
+        }
+    }
+    // free
+    for (i = 0; i < vpos; ++i) free(b[i]);
+    free(f[0]); free(f[1]); free(b);
+    return h;
 }
 
 // phase each fragment
 static uint64_t *fragphase(int vpos, const int8_t *path, nseq_t *hash, int flip)
 {
-	khint_t k;
-	uint64_t *pcnt;
-	uint32_t *left, *rght, max;
-	left = rght = 0; max = 0;
-	pcnt = calloc(vpos, 8);
-	for (k = 0; k < kh_end(hash); ++k) {
-		if (kh_exist(hash, k)) {
-			int i, c[2];
-			frag_t *f = &kh_val(hash, k);
-			if (f->vpos >= vpos) continue;
-			// get the phase
-			c[0] = c[1] = 0;
-			for (i = 0; i < f->vlen; ++i) {
-				if (f->seq[i] == 0) continue;
-				++c[f->seq[i] == path[f->vpos + i] + 1? 0 : 1];
-			}
-			f->phase = c[0] > c[1]? 0 : 1;
-			f->in = c[f->phase]; f->out = c[1 - f->phase];
-			f->phased = f->in == f->out? 0 : 1;
-			f->ambig = (f->in && f->out && f->out < 3 && f->in <= f->out + 1)? 1 : 0;
-			// fix chimera
-			f->flip = 0;
-			if (flip && c[0] >= 3 && c[1] >= 3) {
-				int sum[2], m, mi, md;
-				if (f->vlen > max) { // enlarge the array
-					max = f->vlen;
-					kroundup32(max);
-					left = realloc(left, max * 4);
-					rght = realloc(rght, max * 4);
-				}
-				for (i = 0, sum[0] = sum[1] = 0; i < f->vlen; ++i) { // get left counts
-					if (f->seq[i]) {
-						int c = f->phase? 2 - f->seq[i] : f->seq[i] - 1;
-						++sum[c == path[f->vpos + i]? 0 : 1];
-					}
-					left[i] = sum[1]<<16 | sum[0];
-				}
-				for (i = f->vlen - 1, sum[0] = sum[1] = 0; i >= 0; --i) { // get right counts
-					if (f->seq[i]) {
-						int c = f->phase? 2 - f->seq[i] : f->seq[i] - 1;
-						++sum[c == path[f->vpos + i]? 0 : 1];
-					}
-					rght[i] = sum[1]<<16 | sum[0];
-				}
-				// find the best flip point
-				for (i = m = 0, mi = -1, md = -1; i < f->vlen - 1; ++i) {
-					int a[2];
-					a[0] = (left[i]&0xffff) + (rght[i+1]>>16&0xffff) - (rght[i+1]&0xffff) * FLIP_PENALTY;
-					a[1] = (left[i]>>16&0xffff) + (rght[i+1]&0xffff) - (rght[i+1]>>16&0xffff) * FLIP_PENALTY;
-					if (a[0] > a[1]) {
-						if (a[0] > m) m = a[0], md = 0, mi = i;
-					} else {
-						if (a[1] > m) m = a[1], md = 1, mi = i;
-					}
-				}
-				if (m - c[0] >= FLIP_THRES && m - c[1] >= FLIP_THRES) { // then flip
-					f->flip = 1;
-					if (md == 0) { // flip the tail
-						for (i = mi + 1; i < f->vlen; ++i)
-							if (f->seq[i] == 1) f->seq[i] = 2;
-							else if (f->seq[i] == 2) f->seq[i] = 1;
-					} else { // flip the head
-						for (i = 0; i <= mi; ++i)
-							if (f->seq[i] == 1) f->seq[i] = 2;
-							else if (f->seq[i] == 2) f->seq[i] = 1;
-					}
-				}
-			}
-			// update pcnt[]
-			if (!f->single) {
-				for (i = 0; i < f->vlen; ++i) {
-					int c;
-					if (f->seq[i] == 0) continue;
-					c = f->phase? 2 - f->seq[i] : f->seq[i] - 1;
-					if (c == path[f->vpos + i]) {
-						if (f->phase == 0) ++pcnt[f->vpos + i];
-						else pcnt[f->vpos + i] += 1ull<<32;
-					} else {
-						if (f->phase == 0) pcnt[f->vpos + i] += 1<<16;
-						else pcnt[f->vpos + i] += 1ull<<48;
-					}
-				}
-			}
-		}
-	}
-	free(left); free(rght);
-	return pcnt;
+    khint_t k;
+    uint64_t *pcnt;
+    uint32_t *left, *rght, max;
+    left = rght = 0; max = 0;
+    pcnt = calloc(vpos, 8);
+    for (k = 0; k < kh_end(hash); ++k) {
+        if (kh_exist(hash, k)) {
+            int i, c[2];
+            frag_t *f = &kh_val(hash, k);
+            if (f->vpos >= vpos) continue;
+            // get the phase
+            c[0] = c[1] = 0;
+            for (i = 0; i < f->vlen; ++i) {
+                if (f->seq[i] == 0) continue;
+                ++c[f->seq[i] == path[f->vpos + i] + 1? 0 : 1];
+            }
+            f->phase = c[0] > c[1]? 0 : 1;
+            f->in = c[f->phase]; f->out = c[1 - f->phase];
+            f->phased = f->in == f->out? 0 : 1;
+            f->ambig = (f->in && f->out && f->out < 3 && f->in <= f->out + 1)? 1 : 0;
+            // fix chimera
+            f->flip = 0;
+            if (flip && c[0] >= 3 && c[1] >= 3) {
+                int sum[2], m, mi, md;
+                if (f->vlen > max) { // enlarge the array
+                    max = f->vlen;
+                    kroundup32(max);
+                    left = realloc(left, max * 4);
+                    rght = realloc(rght, max * 4);
+                }
+                for (i = 0, sum[0] = sum[1] = 0; i < f->vlen; ++i) { // get left counts
+                    if (f->seq[i]) {
+                        int c = f->phase? 2 - f->seq[i] : f->seq[i] - 1;
+                        ++sum[c == path[f->vpos + i]? 0 : 1];
+                    }
+                    left[i] = sum[1]<<16 | sum[0];
+                }
+                for (i = f->vlen - 1, sum[0] = sum[1] = 0; i >= 0; --i) { // get right counts
+                    if (f->seq[i]) {
+                        int c = f->phase? 2 - f->seq[i] : f->seq[i] - 1;
+                        ++sum[c == path[f->vpos + i]? 0 : 1];
+                    }
+                    rght[i] = sum[1]<<16 | sum[0];
+                }
+                // find the best flip point
+                for (i = m = 0, mi = -1, md = -1; i < f->vlen - 1; ++i) {
+                    int a[2];
+                    a[0] = (left[i]&0xffff) + (rght[i+1]>>16&0xffff) - (rght[i+1]&0xffff) * FLIP_PENALTY;
+                    a[1] = (left[i]>>16&0xffff) + (rght[i+1]&0xffff) - (rght[i+1]>>16&0xffff) * FLIP_PENALTY;
+                    if (a[0] > a[1]) {
+                        if (a[0] > m) m = a[0], md = 0, mi = i;
+                    } else {
+                        if (a[1] > m) m = a[1], md = 1, mi = i;
+                    }
+                }
+                if (m - c[0] >= FLIP_THRES && m - c[1] >= FLIP_THRES) { // then flip
+                    f->flip = 1;
+                    if (md == 0) { // flip the tail
+                        for (i = mi + 1; i < f->vlen; ++i)
+                            if (f->seq[i] == 1) f->seq[i] = 2;
+                            else if (f->seq[i] == 2) f->seq[i] = 1;
+                    } else { // flip the head
+                        for (i = 0; i <= mi; ++i)
+                            if (f->seq[i] == 1) f->seq[i] = 2;
+                            else if (f->seq[i] == 2) f->seq[i] = 1;
+                    }
+                }
+            }
+            // update pcnt[]
+            if (!f->single) {
+                for (i = 0; i < f->vlen; ++i) {
+                    int c;
+                    if (f->seq[i] == 0) continue;
+                    c = f->phase? 2 - f->seq[i] : f->seq[i] - 1;
+                    if (c == path[f->vpos + i]) {
+                        if (f->phase == 0) ++pcnt[f->vpos + i];
+                        else pcnt[f->vpos + i] += 1ull<<32;
+                    } else {
+                        if (f->phase == 0) pcnt[f->vpos + i] += 1<<16;
+                        else pcnt[f->vpos + i] += 1ull<<48;
+                    }
+                }
+            }
+        }
+    }
+    free(left); free(rght);
+    return pcnt;
 }
 
 static uint64_t *genmask(int vpos, const uint64_t *pcnt, int *_n)
 {
-	int i, max = 0, max_i = -1, m = 0, n = 0, beg = 0, score = 0;
-	uint64_t *list = 0;
-	for (i = 0; i < vpos; ++i) {
-		uint64_t x = pcnt[i];
-		int c[4], pre = score, s;
-		c[0] = x&0xffff; c[1] = x>>16&0xffff; c[2] = x>>32&0xffff; c[3] = x>>48&0xffff;
-		s = (c[1] + c[3] == 0)? -(c[0] + c[2]) : (c[1] + c[3] - 1);
-		if (c[3] > c[2]) s += c[3] - c[2];
-		if (c[1] > c[0]) s += c[1] - c[0];
-		score += s;
-		if (score < 0) score = 0;
-		if (pre == 0 && score > 0) beg = i; // change from zero to non-zero
-		if ((i == vpos - 1 || score == 0) && max >= MASK_THRES) {
-			if (n == m) {
-				m = m? m<<1 : 4;
-				list = realloc(list, m * 8);
-			}
-			list[n++] = (uint64_t)beg<<32 | max_i;
-			i = max_i; // reset i to max_i
-			score = 0;
-		} else if (score > max) max = score, max_i = i;
-		if (score == 0) max = 0;
-	}
-	*_n = n;
-	return list;
+    int i, max = 0, max_i = -1, m = 0, n = 0, beg = 0, score = 0;
+    uint64_t *list = 0;
+    for (i = 0; i < vpos; ++i) {
+        uint64_t x = pcnt[i];
+        int c[4], pre = score, s;
+        c[0] = x&0xffff; c[1] = x>>16&0xffff; c[2] = x>>32&0xffff; c[3] = x>>48&0xffff;
+        s = (c[1] + c[3] == 0)? -(c[0] + c[2]) : (c[1] + c[3] - 1);
+        if (c[3] > c[2]) s += c[3] - c[2];
+        if (c[1] > c[0]) s += c[1] - c[0];
+        score += s;
+        if (score < 0) score = 0;
+        if (pre == 0 && score > 0) beg = i; // change from zero to non-zero
+        if ((i == vpos - 1 || score == 0) && max >= MASK_THRES) {
+            if (n == m) {
+                m = m? m<<1 : 4;
+                list = realloc(list, m * 8);
+            }
+            list[n++] = (uint64_t)beg<<32 | max_i;
+            i = max_i; // reset i to max_i
+            score = 0;
+        } else if (score > max) max = score, max_i = i;
+        if (score == 0) max = 0;
+    }
+    *_n = n;
+    return list;
 }
 
 // trim heading and tailing ambiguous bases; mark deleted and remove sequence
 static int clean_seqs(int vpos, nseq_t *hash)
 {
-	khint_t k;
-	int ret = 0;
-	for (k = 0; k < kh_end(hash); ++k) {
-		if (kh_exist(hash, k)) {
-			frag_t *f = &kh_val(hash, k);
-			int beg, end, i;
-			if (f->vpos >= vpos) {
-				ret = 1;
-				continue;
-			}
-			for (i = 0; i < f->vlen; ++i)
-				if (f->seq[i] != 0) break;
-			beg = i;
-			for (i = f->vlen - 1; i >= 0; --i)
-				if (f->seq[i] != 0) break;
-			end = i + 1;
-			if (end - beg <= 0) kh_del(64, hash, k);
-			else {
-				if (beg != 0) memmove(f->seq, f->seq + beg, end - beg);
-				f->vpos += beg; f->vlen = end - beg;
-				f->single = f->vlen == 1? 1 : 0;
-			}
-		}
-	}
-	return ret;
+    khint_t k;
+    int ret = 0;
+    for (k = 0; k < kh_end(hash); ++k) {
+        if (kh_exist(hash, k)) {
+            frag_t *f = &kh_val(hash, k);
+            int beg, end, i;
+            if (f->vpos >= vpos) {
+                ret = 1;
+                continue;
+            }
+            for (i = 0; i < f->vlen; ++i)
+                if (f->seq[i] != 0) break;
+            beg = i;
+            for (i = f->vlen - 1; i >= 0; --i)
+                if (f->seq[i] != 0) break;
+            end = i + 1;
+            if (end - beg <= 0) kh_del(64, hash, k);
+            else {
+                if (beg != 0) memmove(f->seq, f->seq + beg, end - beg);
+                f->vpos += beg; f->vlen = end - beg;
+                f->single = f->vlen == 1? 1 : 0;
+            }
+        }
+    }
+    return ret;
 }
 
 static void dump_aln(phaseg_t *g, int min_pos, const nseq_t *hash)
 {
-	int i, is_flip, drop_ambi;
-	drop_ambi = g->flag & FLAG_DROP_AMBI;
-	is_flip = (drand48() < 0.5);
-	for (i = 0; i < g->n; ++i) {
-		int end, which;
-		uint64_t key;
-		khint_t k;
-		bam1_t *b = g->b[i];
-		key = X31_hash_string(bam1_qname(b));
-		end = bam_calend(&b->core, bam1_cigar(b));
-		if (end > min_pos) break;
-		k = kh_get(64, hash, key);
-		if (k == kh_end(hash)) which = 3;
-		else {
-			frag_t *f = &kh_val(hash, k);
-			if (f->ambig) which = drop_ambi? 2 : 3;
-			else if (f->phased && f->flip) which = 2;
-			else if (f->phased == 0) which = 3;
-			else { // phased and not flipped
-				char c = 'Y';
-				which = f->phase;
-				bam_aux_append(b, "ZP", 'A', 1, (uint8_t*)&c);
-			}
-			if (which < 2 && is_flip) which = 1 - which; // increase the randomness
-		}
-		if (which == 3) which = (drand48() < 0.5);
-		bam_write1(g->out[which], b);
-		bam_destroy1(b);
-		g->b[i] = 0;
-	}
-	memmove(g->b, g->b + i, (g->n - i) * sizeof(void*));
-	g->n -= i;
+    int i, is_flip, drop_ambi;
+    drop_ambi = g->flag & FLAG_DROP_AMBI;
+    is_flip = (drand48() < 0.5);
+    for (i = 0; i < g->n; ++i) {
+        int end, which;
+        uint64_t key;
+        khint_t k;
+        bam1_t *b = g->b[i];
+        key = X31_hash_string(bam_get_qname(b));
+        end = bam_endpos(b);
+        if (end > min_pos) break;
+        k = kh_get(64, hash, key);
+        if (k == kh_end(hash)) which = 3;
+        else {
+            frag_t *f = &kh_val(hash, k);
+            if (f->ambig) which = drop_ambi? 2 : 3;
+            else if (f->phased && f->flip) which = 2;
+            else if (f->phased == 0) which = 3;
+            else { // phased and not flipped
+                char c = 'Y';
+                which = f->phase;
+                bam_aux_append(b, "ZP", 'A', 1, (uint8_t*)&c);
+            }
+            if (which < 2 && is_flip) which = 1 - which; // increase the randomness
+        }
+        if (which == 3) which = (drand48() < 0.5);
+        sam_write1(g->out[which], g->out_hdr[which], b);
+        bam_destroy1(b);
+        g->b[i] = 0;
+    }
+    memmove(g->b, g->b + i, (g->n - i) * sizeof(void*));
+    g->n -= i;
 }
 
 static int phase(phaseg_t *g, const char *chr, int vpos, uint64_t *cns, nseq_t *hash)
 {
-	int i, j, n_seqs = kh_size(hash), n_masked = 0, min_pos;
-	khint_t k;
-	frag_t **seqs;
-	int8_t *path, *sitemask;
-	uint64_t *pcnt, *regmask;
-
-	if (vpos == 0) return 0;
-	i = clean_seqs(vpos, hash); // i is true if hash has an element with its vpos >= vpos
-	min_pos = i? cns[vpos]>>32 : 0x7fffffff;
-	if (vpos == 1) {
-		printf("PS\t%s\t%d\t%d\n", chr, (int)(cns[0]>>32) + 1, (int)(cns[0]>>32) + 1);
-		printf("M0\t%s\t%d\t%d\t%c\t%c\t%d\t0\t0\t0\t0\n//\n", chr, (int)(cns[0]>>32) + 1, (int)(cns[0]>>32) + 1,
-			"ACGTX"[cns[0]&3], "ACGTX"[cns[0]>>16&3], g->vpos_shift + 1);
-		for (k = 0; k < kh_end(hash); ++k) {
-			if (kh_exist(hash, k)) {
-				frag_t *f = &kh_val(hash, k);
-				if (f->vpos) continue;
-				f->flip = 0;
-				if (f->seq[0] == 0) f->phased = 0;
-				else f->phased = 1, f->phase = f->seq[0] - 1;
-			}
-		}
-		dump_aln(g, min_pos, hash);
-		++g->vpos_shift;
-		return 1;
-	}
-	{ // phase
-		int **cnt;
-		uint64_t *mask;
-		printf("PS\t%s\t%d\t%d\n", chr, (int)(cns[0]>>32) + 1, (int)(cns[vpos-1]>>32) + 1);
-		sitemask = calloc(vpos, 1);
-		cnt = count_all(g->k, vpos, hash);
-		path = dynaprog(g->k, vpos, cnt);
-		for (i = 0; i < vpos; ++i) free(cnt[i]);
-		free(cnt);
-		pcnt = fragphase(vpos, path, hash, 0); // do not fix chimeras when masking
-		mask = genmask(vpos, pcnt, &n_masked);
-		regmask = calloc(n_masked, 8);
-		for (i = 0; i < n_masked; ++i) {
-			regmask[i] = cns[mask[i]>>32]>>32<<32 | cns[(uint32_t)mask[i]]>>32;
-			for (j = mask[i]>>32; j <= (int32_t)mask[i]; ++j)
-				sitemask[j] = 1;
-		}
-		free(mask);
-		if (g->flag & FLAG_FIX_CHIMERA) {
-			free(pcnt);
-			pcnt = fragphase(vpos, path, hash, 1);
-		}
-	}
-	for (i = 0; i < n_masked; ++i)
-		printf("FL\t%s\t%d\t%d\n", chr, (int)(regmask[i]>>32) + 1, (int)regmask[i] + 1);
-	for (i = 0; i < vpos; ++i) {
-		uint64_t x = pcnt[i];
-		int8_t c[2];
-		c[0] = (cns[i]&0xffff)>>2 == 0? 4 : (cns[i]&3);
-		c[1] = (cns[i]>>16&0xffff)>>2 == 0? 4 : (cns[i]>>16&3);
-		printf("M%d\t%s\t%d\t%d\t%c\t%c\t%d\t%d\t%d\t%d\t%d\n", sitemask[i]+1, chr, (int)(cns[0]>>32) + 1, (int)(cns[i]>>32) + 1, "ACGTX"[c[path[i]]], "ACGTX"[c[1-path[i]]],
-			i + g->vpos_shift + 1, (int)(x&0xffff), (int)(x>>16&0xffff), (int)(x>>32&0xffff), (int)(x>>48&0xffff));
-	}
-	free(path); free(pcnt); free(regmask); free(sitemask);
-	seqs = calloc(n_seqs, sizeof(void*));
-	for (k = 0, i = 0; k < kh_end(hash); ++k) 
-		if (kh_exist(hash, k) && kh_val(hash, k).vpos < vpos && !kh_val(hash, k).single)
-			seqs[i++] = &kh_val(hash, k);
-	n_seqs = i;
-	ks_introsort_rseq(n_seqs, seqs);
-	for (i = 0; i < n_seqs; ++i) {
-		frag_t *f = seqs[i];
-		printf("EV\t0\t%s\t%d\t40\t%dM\t*\t0\t0\t", chr, f->vpos + 1 + g->vpos_shift, f->vlen);
-		for (j = 0; j < f->vlen; ++j) {
-			uint32_t c = cns[f->vpos + j];
-			if (f->seq[j] == 0) putchar('N');
-			else putchar("ACGT"[f->seq[j] == 1? (c&3) : (c>>16&3)]);
-		}
-		printf("\t*\tYP:i:%d\tYF:i:%d\tYI:i:%d\tYO:i:%d\tYS:i:%d\n", f->phase, f->flip, f->in, f->out, f->beg+1);
-	}
-	free(seqs);
-	printf("//\n");
-	fflush(stdout);
-	g->vpos_shift += vpos;
-	dump_aln(g, min_pos, hash);
-	return vpos;
+    int i, j, n_seqs = kh_size(hash), n_masked = 0, min_pos;
+    khint_t k;
+    frag_t **seqs;
+    int8_t *path, *sitemask;
+    uint64_t *pcnt, *regmask;
+
+    if (vpos == 0) return 0;
+    i = clean_seqs(vpos, hash); // i is true if hash has an element with its vpos >= vpos
+    min_pos = i? cns[vpos]>>32 : 0x7fffffff;
+    if (vpos == 1) {
+        printf("PS\t%s\t%d\t%d\n", chr, (int)(cns[0]>>32) + 1, (int)(cns[0]>>32) + 1);
+        printf("M0\t%s\t%d\t%d\t%c\t%c\t%d\t0\t0\t0\t0\n//\n", chr, (int)(cns[0]>>32) + 1, (int)(cns[0]>>32) + 1,
+            "ACGTX"[cns[0]&3], "ACGTX"[cns[0]>>16&3], g->vpos_shift + 1);
+        for (k = 0; k < kh_end(hash); ++k) {
+            if (kh_exist(hash, k)) {
+                frag_t *f = &kh_val(hash, k);
+                if (f->vpos) continue;
+                f->flip = 0;
+                if (f->seq[0] == 0) f->phased = 0;
+                else f->phased = 1, f->phase = f->seq[0] - 1;
+            }
+        }
+        dump_aln(g, min_pos, hash);
+        ++g->vpos_shift;
+        return 1;
+    }
+    { // phase
+        int **cnt;
+        uint64_t *mask;
+        printf("PS\t%s\t%d\t%d\n", chr, (int)(cns[0]>>32) + 1, (int)(cns[vpos-1]>>32) + 1);
+        sitemask = calloc(vpos, 1);
+        cnt = count_all(g->k, vpos, hash);
+        path = dynaprog(g->k, vpos, cnt);
+        for (i = 0; i < vpos; ++i) free(cnt[i]);
+        free(cnt);
+        pcnt = fragphase(vpos, path, hash, 0); // do not fix chimeras when masking
+        mask = genmask(vpos, pcnt, &n_masked);
+        regmask = calloc(n_masked, 8);
+        for (i = 0; i < n_masked; ++i) {
+            regmask[i] = cns[mask[i]>>32]>>32<<32 | cns[(uint32_t)mask[i]]>>32;
+            for (j = mask[i]>>32; j <= (int32_t)mask[i]; ++j)
+                sitemask[j] = 1;
+        }
+        free(mask);
+        if (g->flag & FLAG_FIX_CHIMERA) {
+            free(pcnt);
+            pcnt = fragphase(vpos, path, hash, 1);
+        }
+    }
+    for (i = 0; i < n_masked; ++i)
+        printf("FL\t%s\t%d\t%d\n", chr, (int)(regmask[i]>>32) + 1, (int)regmask[i] + 1);
+    for (i = 0; i < vpos; ++i) {
+        uint64_t x = pcnt[i];
+        int8_t c[2];
+        c[0] = (cns[i]&0xffff)>>2 == 0? 4 : (cns[i]&3);
+        c[1] = (cns[i]>>16&0xffff)>>2 == 0? 4 : (cns[i]>>16&3);
+        printf("M%d\t%s\t%d\t%d\t%c\t%c\t%d\t%d\t%d\t%d\t%d\n", sitemask[i]+1, chr, (int)(cns[0]>>32) + 1, (int)(cns[i]>>32) + 1, "ACGTX"[c[path[i]]], "ACGTX"[c[1-path[i]]],
+            i + g->vpos_shift + 1, (int)(x&0xffff), (int)(x>>16&0xffff), (int)(x>>32&0xffff), (int)(x>>48&0xffff));
+    }
+    free(path); free(pcnt); free(regmask); free(sitemask);
+    seqs = calloc(n_seqs, sizeof(frag_t*));
+    for (k = 0, i = 0; k < kh_end(hash); ++k)
+        if (kh_exist(hash, k) && kh_val(hash, k).vpos < vpos && !kh_val(hash, k).single)
+            seqs[i++] = &kh_val(hash, k);
+    n_seqs = i;
+    ks_introsort_rseq(n_seqs, seqs);
+    for (i = 0; i < n_seqs; ++i) {
+        frag_t *f = seqs[i];
+        printf("EV\t0\t%s\t%d\t40\t%dM\t*\t0\t0\t", chr, f->vpos + 1 + g->vpos_shift, f->vlen);
+        for (j = 0; j < f->vlen; ++j) {
+            uint32_t c = cns[f->vpos + j];
+            if (f->seq[j] == 0) putchar('N');
+            else putchar("ACGT"[f->seq[j] == 1? (c&3) : (c>>16&3)]);
+        }
+        printf("\t*\tYP:i:%d\tYF:i:%d\tYI:i:%d\tYO:i:%d\tYS:i:%d\n", f->phase, f->flip, f->in, f->out, f->beg+1);
+    }
+    free(seqs);
+    printf("//\n");
+    fflush(stdout);
+    g->vpos_shift += vpos;
+    dump_aln(g, min_pos, hash);
+    return vpos;
 }
 
 static void update_vpos(int vpos, nseq_t *hash)
 {
-	khint_t k;
-	for (k = 0; k < kh_end(hash); ++k) {
-		if (kh_exist(hash, k)) {
-			frag_t *f = &kh_val(hash, k);
-			if (f->vpos < vpos) kh_del(64, hash, k); // TODO: if frag_t::seq is allocated dynamically, free it
-			else f->vpos -= vpos;
-		}
-	}
+    khint_t k;
+    for (k = 0; k < kh_end(hash); ++k) {
+        if (kh_exist(hash, k)) {
+            frag_t *f = &kh_val(hash, k);
+            if (f->vpos < vpos) kh_del(64, hash, k); // TODO: if frag_t::seq is allocated dynamically, free it
+            else f->vpos -= vpos;
+        }
+    }
 }
 
 static nseq_t *shrink_hash(nseq_t *hash) // TODO: to implement
 {
-	return hash;
+    return hash;
 }
 
 static int readaln(void *data, bam1_t *b)
 {
-	phaseg_t *g = (phaseg_t*)data;
-	int ret;
-	ret = bam_read1(g->fp, b);
-	if (ret < 0) return ret;
-	if (!(b->core.flag & (BAM_FUNMAP|BAM_FSECONDARY|BAM_FQCFAIL|BAM_FDUP)) && g->pre) {
-		if (g->n == g->m) {
-			g->m = g->m? g->m<<1 : 16;
-			g->b = realloc(g->b, g->m * sizeof(void*));
-		}
-		g->b[g->n++] = bam_dup1(b);
-	}
-	return ret;
+    phaseg_t *g = (phaseg_t*)data;
+    int ret;
+    while (1)
+    {
+        ret = sam_read1(g->fp, g->fp_hdr, b);
+        if (ret < 0) break;
+        if ( b->core.flag & (BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP) ) continue;
+        if ( g->pre ) {
+            if (g->n == g->m) {
+                g->m = g->m? g->m<<1 : 16;
+                g->b = realloc(g->b, g->m * sizeof(bam1_t*));
+            }
+            g->b[g->n++] = bam_dup1(b);
+        }
+        break;
+    }
+    return ret;
 }
 
-static khash_t(set64) *loadpos(const char *fn, bam_header_t *h)
+static khash_t(set64) *loadpos(const char *fn, bam_hdr_t *h)
 {
-	gzFile fp;
-	kstream_t *ks;
-	int ret, dret;
-	kstring_t *str;
-	khash_t(set64) *hash;
-
-	hash = kh_init(set64);
-	str = calloc(1, sizeof(kstring_t));
-	fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
-	ks = ks_init(fp);
-	while (ks_getuntil(ks, 0, str, &dret) >= 0) {
-		int tid = bam_get_tid(h, str->s);
-		if (tid >= 0 && dret != '\n') {
-			if (ks_getuntil(ks, 0, str, &dret) >= 0) {
-				uint64_t x = (uint64_t)tid<<32 | (atoi(str->s) - 1);
-				kh_put(set64, hash, x, &ret);
-			} else break;
-		}
-		if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n');
-		if (dret < 0) break;
-	}
-	ks_destroy(ks);
-	gzclose(fp);
-	free(str->s); free(str);
-	return hash;
+    gzFile fp;
+    kstream_t *ks;
+    int ret, dret;
+    kstring_t *str;
+    khash_t(set64) *hash;
+
+    hash = kh_init(set64);
+    str = calloc(1, sizeof(kstring_t));
+    fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
+    ks = ks_init(fp);
+    while (ks_getuntil(ks, 0, str, &dret) >= 0) {
+        int tid = bam_name2id(h, str->s);
+        if (tid >= 0 && dret != '\n') {
+            if (ks_getuntil(ks, 0, str, &dret) >= 0) {
+                uint64_t x = (uint64_t)tid<<32 | (atoi(str->s) - 1);
+                kh_put(set64, hash, x, &ret);
+            } else break;
+        }
+        if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n');
+        if (dret < 0) break;
+    }
+    ks_destroy(ks);
+    gzclose(fp);
+    free(str->s); free(str);
+    return hash;
 }
 
 static int gl2cns(float q[16])
 {
-	int i, j, min_ij;
-	float min, min2;
-	min = min2 = 1e30; min_ij = -1;
-	for (i = 0; i < 4; ++i) {
-		for (j = i; j < 4; ++j) {
-			if (q[i<<2|j] < min) min_ij = i<<2|j, min2 = min, min = q[i<<2|j];
-			else if (q[i<<2|j] < min2) min2 = q[i<<2|j];
-		}
-	}
-	return (min_ij>>2&3) == (min_ij&3)? 0 : 1<<18 | (min_ij>>2&3)<<16 | (min_ij&3) | (int)(min2 - min + .499) << 2;
+    int i, j, min_ij;
+    float min, min2;
+    min = min2 = 1e30; min_ij = -1;
+    for (i = 0; i < 4; ++i) {
+        for (j = i; j < 4; ++j) {
+            if (q[i<<2|j] < min) min_ij = i<<2|j, min2 = min, min = q[i<<2|j];
+            else if (q[i<<2|j] < min2) min2 = q[i<<2|j];
+        }
+    }
+    return (min_ij>>2&3) == (min_ij&3)? 0 : 1<<18 | (min_ij>>2&3)<<16 | (min_ij&3) | (int)(min2 - min + .499) << 2;
 }
 
 int main_phase(int argc, char *argv[])
 {
-	extern void bam_init_header_hash(bam_header_t *header);
-	int c, tid, pos, vpos = 0, n, lasttid = -1, max_vpos = 0;
-	const bam_pileup1_t *plp;
-	bam_plp_t iter;
-	bam_header_t *h;
-	nseq_t *seqs;
-	uint64_t *cns = 0;
-	phaseg_t g;
-	char *fn_list = 0;
-	khash_t(set64) *set = 0;
-	errmod_t *em;
-	uint16_t *bases;
-
-	memset(&g, 0, sizeof(phaseg_t));
-	g.flag = FLAG_FIX_CHIMERA;
-	g.min_varLOD = 37; g.k = 13; g.min_baseQ = 13; g.max_depth = 256;
-	while ((c = getopt(argc, argv, "Q:eFq:k:b:l:D:A:")) >= 0) {
-		switch (c) {
-			case 'D': g.max_depth = atoi(optarg); break;
-			case 'q': g.min_varLOD = atoi(optarg); break;
-			case 'Q': g.min_baseQ = atoi(optarg); break;
-			case 'k': g.k = atoi(optarg); break;
-			case 'F': g.flag &= ~FLAG_FIX_CHIMERA; break;
-			case 'e': g.flag |= FLAG_LIST_EXCL; break;
-			case 'A': g.flag |= FLAG_DROP_AMBI; break;
-			case 'b': g.pre = strdup(optarg); break;
-			case 'l': fn_list = strdup(optarg); break;
-		}
-	}
-	if (argc == optind) {
-		fprintf(pysamerr, "\n");
-		fprintf(pysamerr, "Usage:   samtools phase [options] <in.bam>\n\n");
-		fprintf(pysamerr, "Options: -k INT    block length [%d]\n", g.k);
-		fprintf(pysamerr, "         -b STR    prefix of BAMs to output [null]\n");
-		fprintf(pysamerr, "         -q INT    min het phred-LOD [%d]\n", g.min_varLOD);
-		fprintf(pysamerr, "         -Q INT    min base quality in het calling [%d]\n", g.min_baseQ);
-		fprintf(pysamerr, "         -D INT    max read depth [%d]\n", g.max_depth);
-//		fprintf(pysamerr, "         -l FILE   list of sites to phase [null]\n");
-		fprintf(pysamerr, "         -F        do not attempt to fix chimeras\n");
-		fprintf(pysamerr, "         -A        drop reads with ambiguous phase\n");
-//		fprintf(pysamerr, "         -e        do not discover SNPs (effective with -l)\n");
-		fprintf(pysamerr, "\n");
-		return 1;
-	}
-	g.fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
-	h = bam_header_read(g.fp);
-	if (fn_list) { // read the list of sites to phase
-		bam_init_header_hash(h);
-		set = loadpos(fn_list, h);
-		free(fn_list);
-	} else g.flag &= ~FLAG_LIST_EXCL;
-	if (g.pre) { // open BAMs to write
-		char *s = malloc(strlen(g.pre) + 20);
-		strcpy(s, g.pre); strcat(s, ".0.bam"); g.out[0] = bam_open(s, "w");
-		strcpy(s, g.pre); strcat(s, ".1.bam"); g.out[1] = bam_open(s, "w");
-		strcpy(s, g.pre); strcat(s, ".chimera.bam"); g.out[2] = bam_open(s, "w");
-		for (c = 0; c <= 2; ++c) bam_header_write(g.out[c], h);
-		free(s);
-	}
-
-	iter = bam_plp_init(readaln, &g);
-	g.vpos_shift = 0;
-	seqs = kh_init(64);
-	em = errmod_init(1. - 0.83);
-	bases = calloc(g.max_depth, 2);
-	printf("CC\n");
-	printf("CC\tDescriptions:\nCC\n");
-	printf("CC\t  CC      comments\n");
-	printf("CC\t  PS      start of a phase set\n");
-	printf("CC\t  FL      filtered region\n");
-	printf("CC\t  M[012]  markers; 0 for singletons, 1 for phased and 2 for filtered\n");
-	printf("CC\t  EV      supporting reads; SAM format\n");
-	printf("CC\t  //      end of a phase set\nCC\n");
-	printf("CC\tFormats of PS, FL and M[012] lines (1-based coordinates):\nCC\n");
-	printf("CC\t  PS  chr  phaseSetStart  phaseSetEnd\n");
-	printf("CC\t  FL  chr  filterStart    filterEnd\n");
-	printf("CC\t  M?  chr  PS  pos  allele0  allele1  hetIndex  #supports0  #errors0  #supp1  #err1\n");
-	printf("CC\nCC\n");
-	fflush(stdout);
-	while ((plp = bam_plp_auto(iter, &tid, &pos, &n)) != 0) {
-		int i, k, c, tmp, dophase = 1, in_set = 0;
-		float q[16];
-		if (tid < 0) break;
-		if (tid != lasttid) { // change of chromosome
-			g.vpos_shift = 0;
-			if (lasttid >= 0) {
-				seqs = shrink_hash(seqs);
-				phase(&g, h->target_name[lasttid], vpos, cns, seqs);
-				update_vpos(0x7fffffff, seqs);
-			}
-			lasttid = tid;
-			vpos = 0;
-		}
-		if (set && kh_get(set64, set, (uint64_t)tid<<32 | pos) != kh_end(set)) in_set = 1;
-		if (n > g.max_depth) continue; // do not proceed if the depth is too high
-		// fill the bases array and check if there is a variant
-		for (i = k = 0; i < n; ++i) {
-			const bam_pileup1_t *p = plp + i;
-			uint8_t *seq;
-			int q, baseQ, b;
-			if (p->is_del || p->is_refskip) continue;
-			baseQ = bam1_qual(p->b)[p->qpos];
-			if (baseQ < g.min_baseQ) continue;
-			seq = bam1_seq(p->b);
-			b = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos)];
-			if (b > 3) continue;
-			q = baseQ < p->b->core.qual? baseQ : p->b->core.qual;
-			if (q < 4) q = 4;
-			if (q > 63) q = 63;
-			bases[k++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
-		}
-		if (k == 0) continue;
-		errmod_cal(em, k, 4, bases, q); // compute genotype likelihood
-		c = gl2cns(q); // get the consensus
-		// tell if to proceed
-		if (set && (g.flag&FLAG_LIST_EXCL) && !in_set) continue; // not in the list
-		if (!in_set && (c&0xffff)>>2 < g.min_varLOD) continue; // not a variant
-		// add the variant
-		if (vpos == max_vpos) {
-			max_vpos = max_vpos? max_vpos<<1 : 128;
-			cns = realloc(cns, max_vpos * 8);
-		}
-		cns[vpos] = (uint64_t)pos<<32 | c;
-		for (i = 0; i < n; ++i) {
-			const bam_pileup1_t *p = plp + i;
-			uint64_t key;
-			khint_t k;
-			uint8_t *seq = bam1_seq(p->b);
-			frag_t *f;
-			if (p->is_del || p->is_refskip) continue;
-			if (p->b->core.qual == 0) continue;
-			// get the base code
-			c = nt16_nt4_table[(int)bam1_seqi(seq, p->qpos)];
-			if (c == (cns[vpos]&3)) c = 1;
-			else if (c == (cns[vpos]>>16&3)) c = 2;
-			else c = 0;
-			// write to seqs
-			key = X31_hash_string(bam1_qname(p->b));
-			k = kh_put(64, seqs, key, &tmp);
-			f = &kh_val(seqs, k);
-			if (tmp == 0) { // present in the hash table
-				if (vpos - f->vpos + 1 < MAX_VARS) {
-					f->vlen = vpos - f->vpos + 1;
-					f->seq[f->vlen-1] = c;
-					f->end = bam_calend(&p->b->core, bam1_cigar(p->b));
-				}
-				dophase = 0;
-			} else { // absent
-				memset(f->seq, 0, MAX_VARS);
-				f->beg = p->b->core.pos;
-				f->end = bam_calend(&p->b->core, bam1_cigar(p->b));
-				f->vpos = vpos, f->vlen = 1, f->seq[0] = c, f->single = f->phased = f->flip = f->ambig = 0;
-			}
-		}
-		if (dophase) {
-			seqs = shrink_hash(seqs);
-			phase(&g, h->target_name[tid], vpos, cns, seqs);
-			update_vpos(vpos, seqs);
-			cns[0] = cns[vpos];
-			vpos = 0;
-		}
-		++vpos;
-	}
-	if (tid >= 0) phase(&g, h->target_name[tid], vpos, cns, seqs);
-	bam_header_destroy(h);
-	bam_plp_destroy(iter);
-	bam_close(g.fp);
-	kh_destroy(64, seqs);
-	kh_destroy(set64, set);
-	free(cns);
-	errmod_destroy(em);
-	free(bases);
-	if (g.pre) {
-		for (c = 0; c <= 2; ++c) bam_close(g.out[c]);
-		free(g.pre); free(g.b);
-	}
-	return 0;
+    int c, tid, pos, vpos = 0, n, lasttid = -1, max_vpos = 0;
+    const bam_pileup1_t *plp;
+    bam_plp_t iter;
+    nseq_t *seqs;
+    uint64_t *cns = 0;
+    phaseg_t g;
+    char *fn_list = 0;
+    khash_t(set64) *set = 0;
+    errmod_t *em;
+    uint16_t *bases;
+
+    memset(&g, 0, sizeof(phaseg_t));
+    g.flag = FLAG_FIX_CHIMERA;
+    g.min_varLOD = 37; g.k = 13; g.min_baseQ = 13; g.max_depth = 256;
+    while ((c = getopt(argc, argv, "Q:eFq:k:b:l:D:A:")) >= 0) {
+        switch (c) {
+            case 'D': g.max_depth = atoi(optarg); break;
+            case 'q': g.min_varLOD = atoi(optarg); break;
+            case 'Q': g.min_baseQ = atoi(optarg); break;
+            case 'k': g.k = atoi(optarg); break;
+            case 'F': g.flag &= ~FLAG_FIX_CHIMERA; break;
+            case 'e': g.flag |= FLAG_LIST_EXCL; break;
+            case 'A': g.flag |= FLAG_DROP_AMBI; break;
+            case 'b': g.pre = strdup(optarg); break;
+            case 'l': fn_list = strdup(optarg); break;
+        }
+    }
+    if (argc == optind) {
+        fprintf(pysamerr, "\n");
+        fprintf(pysamerr, "Usage:   samtools phase [options] <in.bam>\n\n");
+        fprintf(pysamerr, "Options: -k INT    block length [%d]\n", g.k);
+        fprintf(pysamerr, "         -b STR    prefix of BAMs to output [null]\n");
+        fprintf(pysamerr, "         -q INT    min het phred-LOD [%d]\n", g.min_varLOD);
+        fprintf(pysamerr, "         -Q INT    min base quality in het calling [%d]\n", g.min_baseQ);
+        fprintf(pysamerr, "         -D INT    max read depth [%d]\n", g.max_depth);
+//      fprintf(pysamerr, "         -l FILE   list of sites to phase [null]\n");
+        fprintf(pysamerr, "         -F        do not attempt to fix chimeras\n");
+        fprintf(pysamerr, "         -A        drop reads with ambiguous phase\n");
+//      fprintf(pysamerr, "         -e        do not discover SNPs (effective with -l)\n");
+        fprintf(pysamerr, "\n");
+        return 1;
+    }
+    g.fp = sam_open(argv[optind], "r");
+    g.fp_hdr = sam_hdr_read(g.fp);
+    if (fn_list) { // read the list of sites to phase
+        set = loadpos(fn_list, g.fp_hdr);
+        free(fn_list);
+    } else g.flag &= ~FLAG_LIST_EXCL;
+    if (g.pre) { // open BAMs to write
+        char *s = (char*)malloc(strlen(g.pre) + 20);
+        strcpy(s, g.pre); strcat(s, ".0.bam"); g.out[0] = sam_open(s, "wb");
+        strcpy(s, g.pre); strcat(s, ".1.bam"); g.out[1] = sam_open(s, "wb");
+        strcpy(s, g.pre); strcat(s, ".chimera.bam"); g.out[2] = sam_open(s, "wb");
+        for (c = 0; c <= 2; ++c) {
+            g.out_hdr[c] = bam_hdr_dup(g.fp_hdr);
+            sam_hdr_write(g.out[c], g.out_hdr[c]);
+        }
+        free(s);
+    }
+
+    iter = bam_plp_init(readaln, &g);
+    g.vpos_shift = 0;
+    seqs = kh_init(64);
+    em = errmod_init(1. - 0.83);
+    bases = calloc(g.max_depth, 2);
+    printf("CC\n");
+    printf("CC\tDescriptions:\nCC\n");
+    printf("CC\t  CC      comments\n");
+    printf("CC\t  PS      start of a phase set\n");
+    printf("CC\t  FL      filtered region\n");
+    printf("CC\t  M[012]  markers; 0 for singletons, 1 for phased and 2 for filtered\n");
+    printf("CC\t  EV      supporting reads; SAM format\n");
+    printf("CC\t  //      end of a phase set\nCC\n");
+    printf("CC\tFormats of PS, FL and M[012] lines (1-based coordinates):\nCC\n");
+    printf("CC\t  PS  chr  phaseSetStart  phaseSetEnd\n");
+    printf("CC\t  FL  chr  filterStart    filterEnd\n");
+    printf("CC\t  M?  chr  PS  pos  allele0  allele1  hetIndex  #supports0  #errors0  #supp1  #err1\n");
+    printf("CC\nCC\n");
+    fflush(stdout);
+    while ((plp = bam_plp_auto(iter, &tid, &pos, &n)) != 0) {
+        int i, k, c, tmp, dophase = 1, in_set = 0;
+        float q[16];
+        if (tid < 0) break;
+        if (tid != lasttid) { // change of chromosome
+            g.vpos_shift = 0;
+            if (lasttid >= 0) {
+                seqs = shrink_hash(seqs);
+                phase(&g, g.fp_hdr->target_name[lasttid], vpos, cns, seqs);
+                update_vpos(0x7fffffff, seqs);
+            }
+            lasttid = tid;
+            vpos = 0;
+        }
+        if (set && kh_get(set64, set, (uint64_t)tid<<32 | pos) != kh_end(set)) in_set = 1;
+        if (n > g.max_depth) continue; // do not proceed if the depth is too high
+        // fill the bases array and check if there is a variant
+        for (i = k = 0; i < n; ++i) {
+            const bam_pileup1_t *p = plp + i;
+            uint8_t *seq;
+            int q, baseQ, b;
+            if (p->is_del || p->is_refskip) continue;
+            baseQ = bam_get_qual(p->b)[p->qpos];
+            if (baseQ < g.min_baseQ) continue;
+            seq = bam_get_seq(p->b);
+            b = bam_nt16_nt4_table[bam_seqi(seq, p->qpos)];
+            if (b > 3) continue;
+            q = baseQ < p->b->core.qual? baseQ : p->b->core.qual;
+            if (q < 4) q = 4;
+            if (q > 63) q = 63;
+            bases[k++] = q<<5 | (int)bam_is_rev(p->b)<<4 | b;
+        }
+        if (k == 0) continue;
+        errmod_cal(em, k, 4, bases, q); // compute genotype likelihood
+        c = gl2cns(q); // get the consensus
+        // tell if to proceed
+        if (set && (g.flag&FLAG_LIST_EXCL) && !in_set) continue; // not in the list
+        if (!in_set && (c&0xffff)>>2 < g.min_varLOD) continue; // not a variant
+        // add the variant
+        if (vpos == max_vpos) {
+            max_vpos = max_vpos? max_vpos<<1 : 128;
+            cns = realloc(cns, max_vpos * 8);
+        }
+        cns[vpos] = (uint64_t)pos<<32 | c;
+        for (i = 0; i < n; ++i) {
+            const bam_pileup1_t *p = plp + i;
+            uint64_t key;
+            khint_t k;
+            uint8_t *seq = bam_get_seq(p->b);
+            frag_t *f;
+            if (p->is_del || p->is_refskip) continue;
+            if (p->b->core.qual == 0) continue;
+            // get the base code
+            c = bam_nt16_nt4_table[(int)bam_seqi(seq, p->qpos)];
+            if (c == (cns[vpos]&3)) c = 1;
+            else if (c == (cns[vpos]>>16&3)) c = 2;
+            else c = 0;
+            // write to seqs
+            key = X31_hash_string(bam_get_qname(p->b));
+            k = kh_put(64, seqs, key, &tmp);
+            f = &kh_val(seqs, k);
+            if (tmp == 0) { // present in the hash table
+                if (vpos - f->vpos + 1 < MAX_VARS) {
+                    f->vlen = vpos - f->vpos + 1;
+                    f->seq[f->vlen-1] = c;
+                    f->end = bam_endpos(p->b);
+                }
+                dophase = 0;
+            } else { // absent
+                memset(f->seq, 0, MAX_VARS);
+                f->beg = p->b->core.pos;
+                f->end = bam_endpos(p->b);
+                f->vpos = vpos, f->vlen = 1, f->seq[0] = c, f->single = f->phased = f->flip = f->ambig = 0;
+            }
+        }
+        if (dophase) {
+            seqs = shrink_hash(seqs);
+            phase(&g, g.fp_hdr->target_name[tid], vpos, cns, seqs);
+            update_vpos(vpos, seqs);
+            cns[0] = cns[vpos];
+            vpos = 0;
+        }
+        ++vpos;
+    }
+    if (tid >= 0) phase(&g, g.fp_hdr->target_name[tid], vpos, cns, seqs);
+    bam_hdr_destroy(g.fp_hdr);
+    bam_plp_destroy(iter);
+    sam_close(g.fp);
+    kh_destroy(64, seqs);
+    kh_destroy(set64, set);
+    free(cns);
+    errmod_destroy(em);
+    free(bases);
+    if (g.pre) {
+        for (c = 0; c <= 2; ++c) {
+            sam_close(g.out[c]);
+            bam_hdr_destroy(g.out_hdr[c]);
+        }
+        free(g.pre); free(g.b);
+    }
+    return 0;
 }
diff --git a/samtools/razf.c.pysam.c b/samtools/razf.c.pysam.c
deleted file mode 100644
index 49ce37c..0000000
--- a/samtools/razf.c.pysam.c
+++ /dev/null
@@ -1,855 +0,0 @@
-#include "pysam.h"
-
-/*
- * RAZF : Random Access compressed(Z) File
- * Version: 1.0
- * Release Date: 2008-10-27
- *
- * Copyright 2008, Jue Ruan <ruanjue at gmail.com>, Heng Li <lh3 at sanger.ac.uk>
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _NO_RAZF
-
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include "razf.h"
-
-
-#if ZLIB_VERNUM < 0x1221
-struct _gz_header_s {
-    int     text;
-    uLong   time;
-    int     xflags;
-    int     os;
-    Bytef   *extra;
-    uInt    extra_len;
-    uInt    extra_max;
-    Bytef   *name;
-    uInt    name_max;
-    Bytef   *comment;
-    uInt    comm_max;
-    int     hcrc;
-    int     done;
-};
-#warning "zlib < 1.2.2.1; RAZF writing is disabled."
-#endif
-
-#define DEF_MEM_LEVEL 8
-
-static inline uint32_t byte_swap_4(uint32_t v){
-	v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
-	return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
-}
-
-static inline uint64_t byte_swap_8(uint64_t v){
-	v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
-	v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
-	return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
-}
-
-static inline int is_big_endian(void){
-	int x = 0x01;
-	char *c = (char*)&x;
-	return (c[0] != 0x01);
-}
-
-#ifndef _RZ_READONLY
-static void add_zindex(RAZF *rz, int64_t in, int64_t out){
-	if(rz->index->size == rz->index->cap){
-		rz->index->cap = rz->index->cap * 1.5 + 2;
-		rz->index->cell_offsets = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
-		rz->index->bin_offsets  = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
-	}
-	if(rz->index->size % RZ_BIN_SIZE == 0) rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE] = out;
-	rz->index->cell_offsets[rz->index->size] = out - rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE];
-	rz->index->size ++;
-}
-
-static void save_zindex(RAZF *rz, int fd){
-	int32_t i, v32;
-	int is_be;
-	is_be = is_big_endian();
-	if(is_be) write(fd, &rz->index->size, sizeof(int));
-	else {
-		v32 = byte_swap_4((uint32_t)rz->index->size);
-		write(fd, &v32, sizeof(uint32_t));
-	}
-	v32 = rz->index->size / RZ_BIN_SIZE + 1;
-	if(!is_be){
-		for(i=0;i<v32;i++) rz->index->bin_offsets[i]  = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
-		for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
-	}
-	write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
-	write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size);
-}
-#endif
-
-#ifdef _USE_KNETFILE
-static void load_zindex(RAZF *rz, knetFile *fp){
-#else
-static void load_zindex(RAZF *rz, int fd){
-#endif
-	int32_t i, v32;
-	int is_be;
-	if(!rz->load_index) return;
-	if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
-	is_be = is_big_endian();
-#ifdef _USE_KNETFILE
-	knet_read(fp, &rz->index->size, sizeof(int));
-#else
-	read(fd, &rz->index->size, sizeof(int));
-#endif
-	if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
-	rz->index->cap = rz->index->size;
-	v32 = rz->index->size / RZ_BIN_SIZE + 1;
-	rz->index->bin_offsets  = malloc(sizeof(int64_t) * v32);
-#ifdef _USE_KNETFILE
-	knet_read(fp, rz->index->bin_offsets, sizeof(int64_t) * v32);
-#else
-	read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
-#endif
-	rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
-#ifdef _USE_KNETFILE
-	knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size);
-#else
-	read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size);
-#endif
-	if(!is_be){
-		for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
-		for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
-	}
-}
-
-#ifdef _RZ_READONLY
-static RAZF* razf_open_w(int fd)
-{
-	fprintf(pysamerr, "[razf_open_w] Writing is not available with zlib ver < 1.2.2.1\n");
-	return 0;
-}
-#else
-static RAZF* razf_open_w(int fd){
-	RAZF *rz;
-#ifdef _WIN32
-	setmode(fd, O_BINARY);
-#endif
-	rz = calloc(1, sizeof(RAZF));
-	rz->mode = 'w';
-#ifdef _USE_KNETFILE
-    rz->x.fpw = fd;
-#else
-	rz->filedes = fd;
-#endif
-	rz->stream = calloc(sizeof(z_stream), 1);
-	rz->inbuf  = malloc(RZ_BUFFER_SIZE);
-	rz->outbuf = malloc(RZ_BUFFER_SIZE);
-	rz->index = calloc(sizeof(ZBlockIndex), 1);
-	deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
-	rz->stream->avail_out = RZ_BUFFER_SIZE;
-	rz->stream->next_out  = rz->outbuf;
-	rz->header = calloc(sizeof(gz_header), 1);
-	rz->header->os    = 0x03; //Unix
-	rz->header->text  = 0;
-	rz->header->time  = 0;
-	rz->header->extra = malloc(7);
-	strncpy((char*)rz->header->extra, "RAZF", 4);
-	rz->header->extra[4] = 1; // obsolete field
-	// block size = RZ_BLOCK_SIZE, Big-Endian
-	rz->header->extra[5] = RZ_BLOCK_SIZE >> 8;
-	rz->header->extra[6] = RZ_BLOCK_SIZE & 0xFF;
-	rz->header->extra_len = 7;
-	rz->header->name = rz->header->comment  = 0;
-	rz->header->hcrc = 0;
-	deflateSetHeader(rz->stream, rz->header);
-	rz->block_pos = rz->block_off = 0;
-	return rz;
-}
-
-static void _razf_write(RAZF* rz, const void *data, int size){
-	int tout;
-	rz->stream->avail_in = size;
-	rz->stream->next_in  = (void*)data;
-	while(1){
-		tout = rz->stream->avail_out;
-		deflate(rz->stream, Z_NO_FLUSH);
-		rz->out += tout - rz->stream->avail_out;
-		if(rz->stream->avail_out) break;
-#ifdef _USE_KNETFILE
-		write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
-#else
-		write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
-#endif
-		rz->stream->avail_out = RZ_BUFFER_SIZE;
-		rz->stream->next_out  = rz->outbuf;
-		if(rz->stream->avail_in == 0) break;
-	};
-	rz->in += size - rz->stream->avail_in;
-	rz->block_off += size - rz->stream->avail_in;
-}
-
-static void razf_flush(RAZF *rz){
-	uint32_t tout;
-	if(rz->buf_len){
-		_razf_write(rz, rz->inbuf, rz->buf_len);
-		rz->buf_off = rz->buf_len = 0;
-	}
-	if(rz->stream->avail_out){
-#ifdef _USE_KNETFILE    
-		write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
-#else        
-		write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
-#endif
-		rz->stream->avail_out = RZ_BUFFER_SIZE;
-		rz->stream->next_out  = rz->outbuf;
-	}
-	while(1){
-		tout = rz->stream->avail_out;
-		deflate(rz->stream, Z_FULL_FLUSH);
-		rz->out += tout - rz->stream->avail_out;
-		if(rz->stream->avail_out == 0){
-#ifdef _USE_KNETFILE    
-			write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
-#else            
-			write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
-#endif
-			rz->stream->avail_out = RZ_BUFFER_SIZE;
-			rz->stream->next_out  = rz->outbuf;
-		} else break;
-	}
-	rz->block_pos = rz->out;
-	rz->block_off = 0;
-}
-
-static void razf_end_flush(RAZF *rz){
-	uint32_t tout;
-	if(rz->buf_len){
-		_razf_write(rz, rz->inbuf, rz->buf_len);
-		rz->buf_off = rz->buf_len = 0;
-	}
-	while(1){
-		tout = rz->stream->avail_out;
-		deflate(rz->stream, Z_FINISH);
-		rz->out += tout - rz->stream->avail_out;
-		if(rz->stream->avail_out < RZ_BUFFER_SIZE){
-#ifdef _USE_KNETFILE        
-			write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
-#else            
-			write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
-#endif
-			rz->stream->avail_out = RZ_BUFFER_SIZE;
-			rz->stream->next_out  = rz->outbuf;
-		} else break;
-	}
-}
-
-static void _razf_buffered_write(RAZF *rz, const void *data, int size){
-	int i, n;
-	while(1){
-		if(rz->buf_len == RZ_BUFFER_SIZE){
-			_razf_write(rz, rz->inbuf, rz->buf_len);
-			rz->buf_len = 0;
-		}
-		if(size + rz->buf_len < RZ_BUFFER_SIZE){
-			for(i=0;i<size;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
-			rz->buf_len += size;
-			return;
-		} else {
-			n = RZ_BUFFER_SIZE - rz->buf_len;
-			for(i=0;i<n;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
-			size -= n;
-			data += n;
-			rz->buf_len += n;
-		}
-	}
-}
-
-int razf_write(RAZF* rz, const void *data, int size){
-	int ori_size, n;
-	int64_t next_block;
-	ori_size = size;
-	next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
-	while(rz->in + rz->buf_len + size >= next_block){
-		n = next_block - rz->in - rz->buf_len;
-		_razf_buffered_write(rz, data, n);
-		data += n;
-		size -= n;
-		razf_flush(rz);
-		add_zindex(rz, rz->in, rz->out);
-		next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
-	}
-	_razf_buffered_write(rz, data, size);
-	return ori_size;
-}
-#endif
-
-/* gzip flag byte */
-#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
-#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
-#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
-#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
-#define COMMENT      0x10 /* bit 4 set: file comment present */
-#define RESERVED     0xE0 /* bits 5..7: reserved */
-
-static int _read_gz_header(unsigned char *data, int size, int *extra_off, int *extra_len){
-	int method, flags, n, len;
-	if(size < 2) return 0;
-	if(data[0] != 0x1f || data[1] != 0x8b) return 0;
-	if(size < 4) return 0;
-	method = data[2];
-	flags  = data[3];
-	if(method != Z_DEFLATED || (flags & RESERVED)) return 0;
-	n = 4 + 6; // Skip 6 bytes
-	*extra_off = n + 2;
-	*extra_len = 0;
-	if(flags & EXTRA_FIELD){
-		if(size < n + 2) return 0;
-		len = ((int)data[n + 1] << 8) | data[n];
-		n += 2;
-		*extra_off = n;
-		while(len){
-			if(n >= size) return 0;
-			n ++;
-			len --;
-		}
-		*extra_len = n - (*extra_off);
-	}
-	if(flags & ORIG_NAME) while(n < size && data[n++]);
-	if(flags & COMMENT) while(n < size && data[n++]);
-	if(flags & HEAD_CRC){
-		if(n + 2 > size) return 0;
-		n += 2;
-	}
-	return n;
-}
-
-#ifdef _USE_KNETFILE
-static RAZF* razf_open_r(knetFile *fp, int _load_index){
-#else
-static RAZF* razf_open_r(int fd, int _load_index){
-#endif
-	RAZF *rz;
-	int ext_off, ext_len;
-	int n, is_be, ret;
-	int64_t end;
-	unsigned char c[] = "RAZF";
-	rz = calloc(1, sizeof(RAZF));
-	rz->mode = 'r';
-#ifdef _USE_KNETFILE
-    rz->x.fpr = fp;
-#else
-#ifdef _WIN32
-	setmode(fd, O_BINARY);
-#endif
-	rz->filedes = fd;
-#endif
-	rz->stream = calloc(sizeof(z_stream), 1);
-	rz->inbuf  = malloc(RZ_BUFFER_SIZE);
-	rz->outbuf = malloc(RZ_BUFFER_SIZE);
-	rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
-#ifdef _USE_KNETFILE
-    n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
-#else
-	n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
-#endif
-	ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
-	if(ret == 0){
-		PLAIN_FILE:
-		rz->in = n;
-		rz->file_type = FILE_TYPE_PLAIN;
-		memcpy(rz->outbuf, rz->inbuf, n);
-		rz->buf_len = n;
-		free(rz->stream);
-		rz->stream = NULL;
-		return rz;
-	}
-	rz->header_size = ret;
-	ret = inflateInit2(rz->stream, -WINDOW_BITS);
-	if(ret != Z_OK){ inflateEnd(rz->stream); goto PLAIN_FILE;}
-	rz->stream->avail_in = n - rz->header_size;
-	rz->stream->next_in  = rz->inbuf + rz->header_size;
-	rz->stream->avail_out = RZ_BUFFER_SIZE;
-	rz->stream->next_out  = rz->outbuf;
-	rz->file_type = FILE_TYPE_GZ;
-	rz->in = rz->header_size;
-	rz->block_pos = rz->header_size;
-	rz->next_block_pos = rz->header_size;
-	rz->block_off = 0;
-	if(ext_len < 7 || memcmp(rz->inbuf + ext_off, c, 4) != 0) return rz;
-	if(((((unsigned char*)rz->inbuf)[ext_off + 5] << 8) | ((unsigned char*)rz->inbuf)[ext_off + 6]) != RZ_BLOCK_SIZE){
-		fprintf(pysamerr, " -- WARNING: RZ_BLOCK_SIZE is not %d, treat source as gz file.  in %s -- %s:%d --\n", RZ_BLOCK_SIZE, __FUNCTION__, __FILE__, __LINE__);
-		return rz;
-	}
-	rz->load_index = _load_index;
-	rz->file_type = FILE_TYPE_RZ;
-#ifdef _USE_KNETFILE
-	if(knet_seek(fp, -16, SEEK_END) == -1){
-#else
-	if(lseek(fd, -16, SEEK_END) == -1){
-#endif
-		UNSEEKABLE:
-		rz->seekable = 0;
-		rz->index = NULL;
-		rz->src_end = rz->end = 0x7FFFFFFFFFFFFFFFLL;
-	} else {
-		is_be = is_big_endian();
-		rz->seekable = 1;
-#ifdef _USE_KNETFILE
-        knet_read(fp, &end, sizeof(int64_t));
-#else
-		read(fd, &end, sizeof(int64_t));
-#endif        
-		if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
-		else rz->src_end = end;
-
-#ifdef _USE_KNETFILE
-		knet_read(fp, &end, sizeof(int64_t));
-#else
-		read(fd, &end, sizeof(int64_t));
-#endif        
-		if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
-		else rz->end = end;
-		if(n > rz->end){
-			rz->stream->avail_in -= n - rz->end;
-			n = rz->end;
-		}
-		if(rz->end > rz->src_end){
-#ifdef _USE_KNETFILE
-            knet_seek(fp, rz->in, SEEK_SET);
-#else
-			lseek(fd, rz->in, SEEK_SET);
-#endif
-			goto UNSEEKABLE;
-		}
-#ifdef _USE_KNETFILE
-        knet_seek(fp, rz->end, SEEK_SET);
-		if(knet_tell(fp) != rz->end){
-			knet_seek(fp, rz->in, SEEK_SET);
-#else
-		if(lseek(fd, rz->end, SEEK_SET) != rz->end){
-			lseek(fd, rz->in, SEEK_SET);
-#endif
-			goto UNSEEKABLE;
-		}
-#ifdef _USE_KNETFILE
-		load_zindex(rz, fp);
-		knet_seek(fp, n, SEEK_SET);
-#else
-		load_zindex(rz, fd);
-		lseek(fd, n, SEEK_SET);
-#endif
-	}
-	return rz;
-}
-
-#ifdef _USE_KNETFILE
-RAZF* razf_dopen(int fd, const char *mode){
-    if (strstr(mode, "r")) fprintf(pysamerr,"[razf_dopen] implement me\n");
-    else if(strstr(mode, "w")) return razf_open_w(fd);
-	return NULL;
-}
-
-RAZF* razf_dopen2(int fd, const char *mode)
-{
-    fprintf(pysamerr,"[razf_dopen2] implement me\n");
-    return NULL;
-}
-#else
-RAZF* razf_dopen(int fd, const char *mode){
-	if(strstr(mode, "r")) return razf_open_r(fd, 1);
-	else if(strstr(mode, "w")) return razf_open_w(fd);
-	else return NULL;
-}
-
-RAZF* razf_dopen2(int fd, const char *mode)
-{
-	if(strstr(mode, "r")) return razf_open_r(fd, 0);
-	else if(strstr(mode, "w")) return razf_open_w(fd);
-	else return NULL;
-}
-#endif
-
-static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
-	int fd;
-	RAZF *rz;
-	if(strstr(mode, "r")){
-#ifdef _USE_KNETFILE
-        knetFile *fd = knet_open(filename, "r");
-        if (fd == 0) {
-            fprintf(pysamerr, "[_razf_open] fail to open %s\n", filename);
-            return NULL;
-        }
-#else
-#ifdef _WIN32
-		fd = open(filename, O_RDONLY | O_BINARY);
-#else
-		fd = open(filename, O_RDONLY);
-#endif
-#endif
-		if(fd < 0) return NULL;
-		rz = razf_open_r(fd, _load_index);
-	} else if(strstr(mode, "w")){
-#ifdef _WIN32
-		fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
-#else
-		fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-#endif
-		if(fd < 0) return NULL;
-		rz = razf_open_w(fd);
-	} else return NULL;
-	return rz;
-}
-
-RAZF* razf_open(const char *filename, const char *mode){
-	return _razf_open(filename, mode, 1);
-}
-
-RAZF* razf_open2(const char *filename, const char *mode){
-	return _razf_open(filename, mode, 0);
-}
-
-int razf_get_data_size(RAZF *rz, int64_t *u_size, int64_t *c_size){
-	int64_t n;
-	if(rz->mode != 'r' && rz->mode != 'R') return 0;
-	switch(rz->file_type){
-		case FILE_TYPE_PLAIN:
-			if(rz->end == 0x7fffffffffffffffLL){
-#ifdef _USE_KNETFILE
-				if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0;
-                n = knet_tell(rz->x.fpr);
-				knet_seek(rz->x.fpr, 0, SEEK_END);
-                rz->end = knet_tell(rz->x.fpr);
-				knet_seek(rz->x.fpr, n, SEEK_SET);
-#else
-				if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
-				rz->end = lseek(rz->filedes, 0, SEEK_END);
-				lseek(rz->filedes, n, SEEK_SET);
-#endif                
-			}
-			*u_size = *c_size = rz->end;
-			return 1;
-		case FILE_TYPE_GZ:
-			return 0;
-		case FILE_TYPE_RZ:
-			if(rz->src_end == rz->end) return 0;
-			*u_size = rz->src_end;
-			*c_size = rz->end;
-			return 1;
-		default:
-			return 0;
-	}
-}
-
-static int _razf_read(RAZF* rz, void *data, int size){
-	int ret, tin;
-	if(rz->z_eof || rz->z_err) return 0;
-	if (rz->file_type == FILE_TYPE_PLAIN) {
-#ifdef _USE_KNETFILE
-		ret = knet_read(rz->x.fpr, data, size);
-#else
-		ret = read(rz->filedes, data, size);
-#endif        
-		if (ret == 0) rz->z_eof = 1;
-		return ret;
-	}
-	rz->stream->avail_out = size;
-	rz->stream->next_out  = data;
-	while(rz->stream->avail_out){
-		if(rz->stream->avail_in == 0){
-			if(rz->in >= rz->end){ rz->z_eof = 1; break; }
-			if(rz->end - rz->in < RZ_BUFFER_SIZE){
-#ifdef _USE_KNETFILE
-				rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in);
-#else
-				rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
-#endif        
-			} else {
-#ifdef _USE_KNETFILE
-				rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
-#else
-				rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
-#endif        
-			}
-			if(rz->stream->avail_in == 0){
-				rz->z_eof = 1;
-				break;
-			}
-			rz->stream->next_in = rz->inbuf;
-		}
-		tin = rz->stream->avail_in;
-		ret = inflate(rz->stream, Z_BLOCK);
-		rz->in += tin - rz->stream->avail_in;
-		if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
-			fprintf(pysamerr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__);
-			rz->z_err = 1;
-			break;
-		}
-		if(ret == Z_STREAM_END){
-			rz->z_eof = 1;
-			break;
-		}
-		if ((rz->stream->data_type&128) && !(rz->stream->data_type&64)){
-			rz->buf_flush = 1;
-			rz->next_block_pos = rz->in;
-			break;
-		}
-	}
-	return size - rz->stream->avail_out;
-}
-
-int razf_read(RAZF *rz, void *data, int size){
-	int ori_size, i;
-	ori_size = size;
-	while(size > 0){
-		if(rz->buf_len){
-			if(size < rz->buf_len){
-				for(i=0;i<size;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
-				rz->buf_off += size;
-				rz->buf_len -= size;
-				data += size;
-				rz->block_off += size;
-				size = 0;
-				break;
-			} else {
-				for(i=0;i<rz->buf_len;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
-				data += rz->buf_len;
-				size -= rz->buf_len;
-				rz->block_off += rz->buf_len;
-				rz->buf_off = 0;
-				rz->buf_len = 0;
-				if(rz->buf_flush){
-					rz->block_pos = rz->next_block_pos;
-					rz->block_off = 0;
-					rz->buf_flush = 0;
-				}
-			}
-		} else if(rz->buf_flush){
-			rz->block_pos = rz->next_block_pos;
-			rz->block_off = 0;
-			rz->buf_flush = 0;
-		}
-		if(rz->buf_flush) continue;
-		rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
-		if(rz->z_eof && rz->buf_len == 0) break;
-	}
-	rz->out += ori_size - size;
-	return ori_size - size;
-}
-
-int razf_skip(RAZF* rz, int size){
-	int ori_size;
-	ori_size = size;
-	while(size > 0){
-		if(rz->buf_len){
-			if(size < rz->buf_len){
-				rz->buf_off += size;
-				rz->buf_len -= size;
-				rz->block_off += size;
-				size = 0;
-				break;
-			} else {
-				size -= rz->buf_len;
-				rz->buf_off = 0;
-				rz->buf_len = 0;
-				rz->block_off += rz->buf_len;
-				if(rz->buf_flush){
-					rz->block_pos = rz->next_block_pos;
-					rz->block_off = 0;
-					rz->buf_flush = 0;
-				}
-			}
-		} else if(rz->buf_flush){
-			rz->block_pos = rz->next_block_pos;
-			rz->block_off = 0;
-			rz->buf_flush = 0;
-		}
-		if(rz->buf_flush) continue;
-		rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
-		if(rz->z_eof || rz->z_err) break;
-	}
-	rz->out += ori_size - size;
-	return ori_size - size;
-}
-
-static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
-#ifdef _USE_KNETFILE
-	knet_seek(rz->x.fpr, in, SEEK_SET);
-#else
-	lseek(rz->filedes, in, SEEK_SET);
-#endif
-	rz->in  = in;
-	rz->out = out;
-	rz->block_pos = in;
-	rz->next_block_pos = in;
-	rz->block_off = 0;
-	rz->buf_flush = 0;
-	rz->z_eof = rz->z_err = 0;
-	inflateReset(rz->stream);
-	rz->stream->avail_in = 0;
-	rz->buf_off = rz->buf_len = 0;
-}
-
-int64_t razf_jump(RAZF *rz, int64_t block_start, int block_offset){
-	int64_t pos;
-	rz->z_eof = 0;
-	if(rz->file_type == FILE_TYPE_PLAIN){
-		rz->buf_off = rz->buf_len = 0;
-		pos = block_start + block_offset;
-#ifdef _USE_KNETFILE
-		knet_seek(rz->x.fpr, pos, SEEK_SET);
-        pos = knet_tell(rz->x.fpr);
-#else
-		pos = lseek(rz->filedes, pos, SEEK_SET);
-#endif
-		rz->out = rz->in = pos;
-		return pos;
-	}
-	if(block_start == rz->block_pos && block_offset >= rz->block_off) {
-		block_offset -= rz->block_off;
-		goto SKIP; // Needn't reset inflate
-	}
-	if(block_start  == 0) block_start = rz->header_size; // Automaticly revist wrong block_start
-	_razf_reset_read(rz, block_start, 0);
-	SKIP:
-	if(block_offset) razf_skip(rz, block_offset);
-	return rz->block_off;
-}
-
-int64_t razf_seek(RAZF* rz, int64_t pos, int where){
-	int64_t idx;
-	int64_t seek_pos, new_out;
-	rz->z_eof = 0;
-	if (where == SEEK_CUR) pos += rz->out;
-	else if (where == SEEK_END) pos += rz->src_end;
-	if(rz->file_type == FILE_TYPE_PLAIN){
-#ifdef _USE_KNETFILE
-		knet_seek(rz->x.fpr, pos, SEEK_SET);
-        seek_pos = knet_tell(rz->x.fpr);
-#else
-		seek_pos = lseek(rz->filedes, pos, SEEK_SET);
-#endif
-		rz->buf_off = rz->buf_len = 0;
-		rz->out = rz->in = seek_pos;
-		return seek_pos;
-	} else if(rz->file_type == FILE_TYPE_GZ){
-		if(pos >= rz->out) goto SKIP;
-		return rz->out;
-	}
-	if(pos == rz->out) return pos;
-	if(pos > rz->src_end) return rz->out;
-	if(!rz->seekable || !rz->load_index){
-		if(pos >= rz->out) goto SKIP;
-	}
-	idx = pos / RZ_BLOCK_SIZE - 1;
-	seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
-	new_out  = (idx + 1) * RZ_BLOCK_SIZE;
-	if(pos > rz->out && new_out <= rz->out) goto SKIP;
-	_razf_reset_read(rz, seek_pos, new_out);
-	SKIP:
-	razf_skip(rz, (int)(pos - rz->out));
-	return rz->out;
-}
-
-uint64_t razf_tell2(RAZF *rz)
-{
-	/*
-	if (rz->load_index) {
-		int64_t idx, seek_pos;
-		idx = rz->out / RZ_BLOCK_SIZE - 1;
-		seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
-		if (seek_pos != rz->block_pos || rz->out%RZ_BLOCK_SIZE != rz->block_off)
-			fprintf(pysamerr, "[razf_tell2] inconsistent block offset: (%lld, %lld) != (%lld, %lld)\n",
-					(long long)seek_pos, (long long)rz->out%RZ_BLOCK_SIZE, (long long)rz->block_pos, (long long) rz->block_off);
-	}
-	*/
-	return (uint64_t)rz->block_pos<<16 | (rz->block_off&0xffff);
-}
-
-int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where)
-{
-	if (where != SEEK_SET) return -1;
-	return razf_jump(rz, voffset>>16, voffset&0xffff);
-}
-
-void razf_close(RAZF *rz){
-	if(rz->mode == 'w'){
-#ifndef _RZ_READONLY
-		razf_end_flush(rz);
-		deflateEnd(rz->stream);
-#ifdef _USE_KNETFILE
-		save_zindex(rz, rz->x.fpw);
-		if(is_big_endian()){
-			write(rz->x.fpw, &rz->in, sizeof(int64_t));
-			write(rz->x.fpw, &rz->out, sizeof(int64_t));
-		} else {
-			uint64_t v64 = byte_swap_8((uint64_t)rz->in);
-			write(rz->x.fpw, &v64, sizeof(int64_t));
-			v64 = byte_swap_8((uint64_t)rz->out);
-			write(rz->x.fpw, &v64, sizeof(int64_t));
-		}
-#else
-		save_zindex(rz, rz->filedes);
-		if(is_big_endian()){
-			write(rz->filedes, &rz->in, sizeof(int64_t));
-			write(rz->filedes, &rz->out, sizeof(int64_t));
-		} else {
-			uint64_t v64 = byte_swap_8((uint64_t)rz->in);
-			write(rz->filedes, &v64, sizeof(int64_t));
-			v64 = byte_swap_8((uint64_t)rz->out);
-			write(rz->filedes, &v64, sizeof(int64_t));
-		}
-#endif
-#endif
-	} else if(rz->mode == 'r'){
-		if(rz->stream) inflateEnd(rz->stream);
-	}
-	if(rz->inbuf) free(rz->inbuf);
-	if(rz->outbuf) free(rz->outbuf);
-	if(rz->header){
-		free(rz->header->extra);
-		free(rz->header->name);
-		free(rz->header->comment);
-		free(rz->header);
-	}
-	if(rz->index){
-		free(rz->index->bin_offsets);
-		free(rz->index->cell_offsets);
-		free(rz->index);
-	}
-	free(rz->stream);
-#ifdef _USE_KNETFILE
-    if (rz->mode == 'r')
-        knet_close(rz->x.fpr);
-    if (rz->mode == 'w')
-        close(rz->x.fpw);
-#else
-	close(rz->filedes);
-#endif
-	free(rz);
-}
-
-#endif
diff --git a/samtools/razf.h b/samtools/razf.h
deleted file mode 100644
index 60a0c96..0000000
--- a/samtools/razf.h
+++ /dev/null
@@ -1,134 +0,0 @@
- /*-
- * RAZF : Random Access compressed(Z) File
- * Version: 1.0
- * Release Date: 2008-10-27
- *
- * Copyright 2008, Jue Ruan <ruanjue at gmail.com>, Heng Li <lh3 at sanger.ac.uk>
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-
-#ifndef __RAZF_RJ_H
-#define __RAZF_RJ_H
-
-#include <stdint.h>
-#include <stdio.h>
-#include "zlib.h"
-
-#ifdef _USE_KNETFILE
-#include "knetfile.h"
-#endif
-
-#if ZLIB_VERNUM < 0x1221
-#define _RZ_READONLY
-struct _gz_header_s;
-typedef struct _gz_header_s _gz_header;
-#define gz_header _gz_header
-#endif
-
-#define WINDOW_BITS   15
-
-#ifndef RZ_BLOCK_SIZE
-#define RZ_BLOCK_SIZE (1<<WINDOW_BITS)
-#endif
-
-#ifndef RZ_BUFFER_SIZE
-#define RZ_BUFFER_SIZE 4096
-#endif
-
-#ifndef RZ_COMPRESS_LEVEL
-#define RZ_COMPRESS_LEVEL 6
-#endif
-
-#define RZ_BIN_SIZE ((1LLU << 32) / RZ_BLOCK_SIZE)
-
-typedef struct {
-	uint32_t *cell_offsets; // i
-	int64_t  *bin_offsets; // i / BIN_SIZE
-	int size;
-	int cap;
-} ZBlockIndex;
-/* When storing index, output bytes in Big-Endian everywhere */
-
-#define FILE_TYPE_RZ	1
-#define FILE_TYPE_PLAIN	2
-#define FILE_TYPE_GZ	3
-
-typedef struct RandomAccessZFile  {
-	char mode; /* 'w' : write mode; 'r' : read mode */
-	int file_type;
-	/* plain file or rz file, razf_read support plain file as input too, in this case, razf_read work as buffered fread */
-#ifdef _USE_KNETFILE
-    union {
-        knetFile *fpr;
-        int fpw;
-    } x;
-#else
-	int filedes; /* the file descriptor */
-#endif
-	z_stream *stream;
-	ZBlockIndex *index;
-	int64_t in, out, end, src_end;
-	/* in: n bytes total in; out: n bytes total out; */
-	/* end: the end of all data blocks, while the start of index; src_end: the true end position in uncompressed file */
-	int buf_flush; // buffer should be flush, suspend inflate util buffer is empty
-	int64_t block_pos, block_off, next_block_pos;
-	/* block_pos: the start postiion of current block  in compressed file */
-	/* block_off: tell how many bytes have been read from current block */
-	void *inbuf, *outbuf;
-	int header_size;
-	gz_header *header;
-	/* header is used to transfer inflate_state->mode from HEAD to TYPE after call inflateReset */
-	int buf_off, buf_len;
-	int z_err, z_eof;
-	int seekable;
-	/* Indice where the source is seekable */
-	int load_index;
-	/* set has_index to 0 in mode 'w', then index will be discarded */
-} RAZF;
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-	RAZF* razf_dopen(int data_fd, const char *mode);
-	RAZF *razf_open(const char *fn, const char *mode);
-	int razf_write(RAZF* rz, const void *data, int size);
-	int razf_read(RAZF* rz, void *data, int size);
-	int64_t razf_seek(RAZF* rz, int64_t pos, int where);
-	void razf_close(RAZF* rz);
-
-#define razf_tell(rz) ((rz)->out)
-
-	RAZF* razf_open2(const char *filename, const char *mode);
-	RAZF* razf_dopen2(int fd, const char *mode);
-	uint64_t razf_tell2(RAZF *rz);
-	int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/samtools/sam.c.pysam.c b/samtools/sam.c.pysam.c
index a65fc0c..bfa4fc0 100644
--- a/samtools/sam.c.pysam.c
+++ b/samtools/sam.c.pysam.c
@@ -1,188 +1,110 @@
 #include "pysam.h"
 
+/*  sam.c -- format-neutral SAM/BAM API.
+
+    Copyright (C) 2009, 2012-2014 Genome Research Ltd.
+    Portions copyright (C) 2011 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <string.h>
 #include <unistd.h>
-#include "faidx.h"
+#include "htslib/faidx.h"
 #include "sam.h"
 
-#define TYPE_BAM  1
-#define TYPE_READ 2
-
-bam_header_t *bam_header_dup(const bam_header_t *h0)
-{
-	bam_header_t *h;
-	int i;
-	h = bam_header_init();
-	*h = *h0;
-	h->hash = h->dict = h->rg2lib = 0;
-	h->text = (char*)calloc(h->l_text + 1, 1);
-	memcpy(h->text, h0->text, h->l_text);
-	h->target_len = (uint32_t*)calloc(h->n_targets, 4);
-	h->target_name = (char**)calloc(h->n_targets, sizeof(void*));
-	for (i = 0; i < h->n_targets; ++i) {
-		h->target_len[i] = h0->target_len[i];
-		h->target_name[i] = strdup(h0->target_name[i]);
-	}
-	return h;
-}
-static void append_header_text(bam_header_t *header, char* text, int len)
-{
-	int x = header->l_text + 1;
-	int y = header->l_text + len + 1; // 1 byte null
-	if (text == 0) return;
-	kroundup32(x); 
-	kroundup32(y);
-	if (x < y) header->text = (char*)realloc(header->text, y);
-	strncpy(header->text + header->l_text, text, len); // we cannot use strcpy() here.
-	header->l_text += len;
-	header->text[header->l_text] = 0;
-}
-
 int samthreads(samfile_t *fp, int n_threads, int n_sub_blks)
 {
-	if (!(fp->type&1) || (fp->type&2)) return -1;
-	bgzf_mt(fp->x.bam, n_threads, n_sub_blks);
-	return 0;
+    if (!fp->file->is_bin || !fp->file->is_write) return -1;
+    bgzf_mt(fp->x.bam, n_threads, n_sub_blks);
+    return 0;
 }
 
 samfile_t *samopen(const char *fn, const char *mode, const void *aux)
 {
-	samfile_t *fp;
-	fp = (samfile_t*)calloc(1, sizeof(samfile_t));
-	if (strchr(mode, 'r')) { // read
-		fp->type |= TYPE_READ;
-		if (strchr(mode, 'b')) { // binary
-			fp->type |= TYPE_BAM;
-			fp->x.bam = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r");
-			if (fp->x.bam == 0) goto open_err_ret;
-			fp->header = bam_header_read(fp->x.bam);
-		} else { // text
-			fp->x.tamr = sam_open(fn);
-			if (fp->x.tamr == 0) goto open_err_ret;
-			fp->header = sam_header_read(fp->x.tamr);
-			if (fp->header->n_targets == 0) { // no @SQ fields
-				if (aux) { // check if aux is present
-					bam_header_t *textheader = fp->header;
-					fp->header = sam_header_read2((const char*)aux);
-					if (fp->header == 0) goto open_err_ret;
-					append_header_text(fp->header, textheader->text, textheader->l_text);
-					bam_header_destroy(textheader);
-				}
-				if (fp->header->n_targets == 0 && bam_verbose >= 1)
-					fprintf(pysamerr, "[samopen] no @SQ lines in the header.\n");
-			} else if (bam_verbose >= 2) fprintf(pysamerr, "[samopen] SAM header is present: %d sequences.\n", fp->header->n_targets);
-		}
-	} else if (strchr(mode, 'w')) { // write
-		fp->header = bam_header_dup((const bam_header_t*)aux);
-		if (strchr(mode, 'b')) { // binary
-			char bmode[3];
-			int i, compress_level = -1;
-			for (i = 0; mode[i]; ++i) if (mode[i] >= '0' && mode[i] <= '9') break;
-			if (mode[i]) compress_level = mode[i] - '0';
-			if (strchr(mode, 'u')) compress_level = 0;
-			bmode[0] = 'w'; bmode[1] = compress_level < 0? 0 : compress_level + '0'; bmode[2] = 0;
-			fp->type |= TYPE_BAM;
-			fp->x.bam = strcmp(fn, "-")? bam_open(fn, bmode) : bam_dopen(fileno(stdout), bmode);
-			if (fp->x.bam == 0) goto open_err_ret;
-			bam_header_write(fp->x.bam, fp->header);
-		} else { // text
-			// open file
-			fp->x.tamw = strcmp(fn, "-")? fopen(fn, "w") : stdout;
-			if (fp->x.tamw == 0) goto open_err_ret;
-			if (strchr(mode, 'X')) fp->type |= BAM_OFSTR<<2;
-			else if (strchr(mode, 'x')) fp->type |= BAM_OFHEX<<2;
-			else fp->type |= BAM_OFDEC<<2;
-			// write header
-			if (strchr(mode, 'h')) {
-				int i;
-				bam_header_t *alt;
-				// parse the header text 
-				alt = bam_header_init();
-				alt->l_text = fp->header->l_text; alt->text = fp->header->text;
-				sam_header_parse(alt);
-				alt->l_text = 0; alt->text = 0;
-				// check if there are @SQ lines in the header
-				fwrite(fp->header->text, 1, fp->header->l_text, fp->x.tamw); // FIXME: better to skip the trailing NULL
-				if (alt->n_targets) { // then write the header text without dumping ->target_{name,len}
-					if (alt->n_targets != fp->header->n_targets && bam_verbose >= 1)
-						fprintf(pysamerr, "[samopen] inconsistent number of target sequences. Output the text header.\n");
-				} else { // then dump ->target_{name,len}
-					for (i = 0; i < fp->header->n_targets; ++i)
-						fprintf(fp->x.tamw, "@SQ\tSN:%s\tLN:%d\n", fp->header->target_name[i], fp->header->target_len[i]);
-				}
-				bam_header_destroy(alt);
-			}
-		}
-	}
-	return fp;
+    // hts_open() is really sam_open(), except for #define games
+    samFile *hts_fp = hts_open(fn, mode);
+    if (hts_fp == NULL)  return NULL;
 
-open_err_ret:
-	free(fp);
-	return 0;
-}
+    samfile_t *fp = malloc(sizeof (samfile_t));
+    fp->file = hts_fp;
+    fp->x.bam = hts_fp->fp.bgzf;
+    if (strchr(mode, 'r')) {
+        if (aux) hts_set_fai_filename(fp->file, aux);
+        fp->header = sam_hdr_read(fp->file);  // samclose() will free this
+        if (fp->header->n_targets == 0 && bam_verbose >= 1)
+            fprintf(pysamerr, "[samopen] no @SQ lines in the header.\n");
+    }
+    else {
+        fp->header = (bam_hdr_t *)aux;  // For writing, we won't free it
+        if (fp->file->is_bin || fp->file->is_cram || strchr(mode, 'h')) sam_hdr_write(fp->file, fp->header);
+    }
 
-void samclose(samfile_t *fp)
-{
-	if (fp == 0) return;
-	if (fp->header) bam_header_destroy(fp->header);
-	if (fp->type & TYPE_BAM) bam_close(fp->x.bam);
-	else if (fp->type & TYPE_READ) sam_close(fp->x.tamr);
-	else fclose(fp->x.tamw);
-	free(fp);
+    return fp;
 }
 
-int samread(samfile_t *fp, bam1_t *b)
-{
-	if (fp == 0 || !(fp->type & TYPE_READ)) return -1; // not open for reading
-	if (fp->type & TYPE_BAM) return bam_read1(fp->x.bam, b);
-	else return sam_read1(fp->x.tamr, fp->header, b);
-}
-
-int samwrite(samfile_t *fp, const bam1_t *b)
+void samclose(samfile_t *fp)
 {
-	if (fp == 0 || (fp->type & TYPE_READ)) return -1; // not open for writing
-	if (fp->type & TYPE_BAM) return bam_write1(fp->x.bam, b);
-	else {
-		char *s = bam_format1_core(fp->header, b, fp->type>>2&3);
-		int l = strlen(s);
-		fputs(s, fp->x.tamw); fputc('\n', fp->x.tamw);
-		free(s);
-		return l + 1;
-	}
+    if (fp) {
+        if (!fp->file->is_write && fp->header) bam_hdr_destroy(fp->header);
+        sam_close(fp->file);
+        free(fp);
+    }
 }
 
 int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *func_data)
 {
-	bam_plbuf_t *buf;
-	int ret;
-	bam1_t *b;
-	b = bam_init1();
-	buf = bam_plbuf_init(func, func_data);
-	bam_plbuf_set_mask(buf, mask);
-	while ((ret = samread(fp, b)) >= 0)
-		bam_plbuf_push(b, buf);
-	bam_plbuf_push(0, buf);
-	bam_plbuf_destroy(buf);
-	bam_destroy1(b);
-	return 0;
+    bam_plbuf_t *buf;
+    int ret;
+    bam1_t *b;
+    b = bam_init1();
+    buf = bam_plbuf_init(func, func_data);
+    if (mask < 0) mask = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP;
+    else mask |= BAM_FUNMAP;
+    while ((ret = samread(fp, b)) >= 0) {
+        // bam_plp_push() itself now filters out unmapped reads only
+        if (b->core.flag & mask) b->core.flag |= BAM_FUNMAP;
+        bam_plbuf_push(b, buf);
+    }
+    bam_plbuf_push(0, buf);
+    bam_plbuf_destroy(buf);
+    bam_destroy1(b);
+    return 0;
 }
 
 char *samfaipath(const char *fn_ref)
 {
-	char *fn_list = 0;
-	if (fn_ref == 0) return 0;
-	fn_list = calloc(strlen(fn_ref) + 5, 1);
-	strcat(strcpy(fn_list, fn_ref), ".fai");
-	if (access(fn_list, R_OK) == -1) { // fn_list is unreadable
-		if (access(fn_ref, R_OK) == -1) {
-			fprintf(pysamerr, "[samfaipath] fail to read file %s.\n", fn_ref);
-		} else {
-			if (bam_verbose >= 3) fprintf(pysamerr, "[samfaipath] build FASTA index...\n");
-			if (fai_build(fn_ref) == -1) {
-				fprintf(pysamerr, "[samfaipath] fail to build FASTA index.\n");
-				free(fn_list); fn_list = 0;
-			}
-		}
-	}
-	return fn_list;
+    char *fn_list = 0;
+    if (fn_ref == 0) return 0;
+    fn_list = calloc(strlen(fn_ref) + 5, 1);
+    strcat(strcpy(fn_list, fn_ref), ".fai");
+    if (access(fn_list, R_OK) == -1) { // fn_list is unreadable
+        if (access(fn_ref, R_OK) == -1) {
+            fprintf(pysamerr, "[samfaipath] fail to read file %s.\n", fn_ref);
+        } else {
+            if (bam_verbose >= 3) fprintf(pysamerr, "[samfaipath] build FASTA index...\n");
+            if (fai_build(fn_ref) == -1) {
+                fprintf(pysamerr, "[samfaipath] fail to build FASTA index.\n");
+                free(fn_list); fn_list = 0;
+            }
+        }
+    }
+    return fn_list;
 }
diff --git a/samtools/sam.h b/samtools/sam.h
index 0495501..39da006 100644
--- a/samtools/sam.h
+++ b/samtools/sam.h
@@ -1,6 +1,31 @@
+/*  sam.h -- format-neutral SAM/BAM API.
+
+    Copyright (C) 2009, 2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #ifndef BAM_SAM_H
 #define BAM_SAM_H
 
+#include "htslib/sam.h"
 #include "bam.h"
 
 /*!
@@ -22,75 +47,71 @@
   @field  header  header struct
  */
 typedef struct {
-	int type;
-	union {
-		tamFile tamr;
-		bamFile bam;
-		FILE *tamw;
-	} x;
-	bam_header_t *header;
+    samFile *file;
+    struct { BGZF *bam; } x;  // Hack so that fp->x.bam still works
+    bam_hdr_t *header;
 } samfile_t;
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-	/*!
-	  @abstract     Open a SAM/BAM file
-
-	  @param fn SAM/BAM file name; "-" is recognized as stdin (for
-	  reading) or stdout (for writing).
-
-	  @param mode open mode /[rw](b?)(u?)(h?)([xX]?)/: 'r' for reading,
-	  'w' for writing, 'b' for BAM I/O, 'u' for uncompressed BAM output,
-	  'h' for outputing header in SAM, 'x' for HEX flag and 'X' for
-	  string flag. If 'b' present, it must immediately follow 'r' or
-	  'w'. Valid modes are "r", "w", "wh", "wx", "whx", "wX", "whX",
-	  "rb", "wb" and "wbu" exclusively.
-
-	  @param aux auxiliary data; if mode[0]=='w', aux points to
-	  bam_header_t; if strcmp(mode, "rb")!=0 and @SQ header lines in SAM
-	  are absent, aux points the file name of the list of the reference;
-	  aux is not used otherwise. If @SQ header lines are present in SAM,
-	  aux is not used, either.
-
-	  @return       SAM/BAM file handler
-	 */
-	samfile_t *samopen(const char *fn, const char *mode, const void *aux);
-
-	/*!
-	  @abstract     Close a SAM/BAM handler
-	  @param  fp    file handler to be closed
-	 */
-	void samclose(samfile_t *fp);
-
-	/*!
-	  @abstract     Read one alignment
-	  @param  fp    file handler
-	  @param  b     alignment
-	  @return       bytes read
-	 */
-	int samread(samfile_t *fp, bam1_t *b);
-
-	/*!
-	  @abstract     Write one alignment
-	  @param  fp    file handler
-	  @param  b     alignment
-	  @return       bytes written
-	 */
-	int samwrite(samfile_t *fp, const bam1_t *b);
-
-	/*!
-	  @abstract     Get the pileup for a whole alignment file
-	  @param  fp    file handler
-	  @param  mask  mask transferred to bam_plbuf_set_mask()
-	  @param  func  user defined function called in the pileup process
-	  #param  data  user provided data for func()
-	 */
-	int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *data);
-
-	char *samfaipath(const char *fn_ref);
-	int samthreads(samfile_t *fp, int n_threads, int n_sub_blks);
+    /*!
+      @abstract     Open a SAM/BAM file
+
+      @param fn SAM/BAM file name; "-" is recognized as stdin (for
+      reading) or stdout (for writing).
+
+      @param mode open mode /[rw](b?)(u?)(h?)([xX]?)/: 'r' for reading,
+      'w' for writing, 'b' for BAM I/O, 'u' for uncompressed BAM output,
+      'h' for outputing header in SAM, 'x' for HEX flag and 'X' for
+      string flag. If 'b' present, it must immediately follow 'r' or
+      'w'. Valid modes are "r", "w", "wh", "wx", "whx", "wX", "whX",
+      "rb", "wb" and "wbu" exclusively.
+
+      @param aux auxiliary data; if mode[0]=='w', aux points to
+      bam_header_t; if strcmp(mode, "rb")!=0 and @SQ header lines in SAM
+      are absent, aux points the file name of the list of the reference;
+      aux is not used otherwise. If @SQ header lines are present in SAM,
+      aux is not used, either.
+
+      @return       SAM/BAM file handler
+     */
+    samfile_t *samopen(const char *fn, const char *mode, const void *aux);
+
+    /*!
+      @abstract     Close a SAM/BAM handler
+      @param  fp    file handler to be closed
+     */
+    void samclose(samfile_t *fp);
+
+    /*!
+      @abstract     Read one alignment
+      @param  fp    file handler
+      @param  b     alignment
+      @return       bytes read
+     */
+    static inline int samread(samfile_t *fp, bam1_t *b) { return sam_read1(fp->file, fp->header, b); }
+
+    /*!
+      @abstract     Write one alignment
+      @param  fp    file handler
+      @param  b     alignment
+      @return       bytes written
+     */
+    static inline int samwrite(samfile_t *fp, const bam1_t *b) { return sam_write1(fp->file, fp->header, b); }
+
+    /*!
+      @abstract     Get the pileup for a whole alignment file
+      @param  fp    file handler
+      @param  mask  mask transferred to bam_plbuf_set_mask()
+      @param  func  user defined function called in the pileup process
+      #param  data  user provided data for func()
+     */
+    int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *data);
+
+    char *samfaipath(const char *fn_ref);
+    int samthreads(samfile_t *fp, int n_threads, int n_sub_blks);
 
 #ifdef __cplusplus
 }
diff --git a/samtools/sam_header.c.pysam.c b/samtools/sam_header.c.pysam.c
index dcfc99d..ecf937c 100644
--- a/samtools/sam_header.c.pysam.c
+++ b/samtools/sam_header.c.pysam.c
@@ -1,5 +1,29 @@
 #include "pysam.h"
 
+/*  sam_header.c -- basic SAM/BAM header API.
+
+    Copyright (C) 2009-2013 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include "sam_header.h"
 #include <stdio.h>
 #include <string.h>
@@ -7,7 +31,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 
-#include "khash.h"
+#include "htslib/khash.h"
 KHASH_MAP_INIT_STR(str, const char *)
 
 struct _HeaderList
@@ -94,7 +118,7 @@ static list_t *list_append(list_t *root, void *data)
     list_t *l = root;
     while (l && l->next)
         l = l->next;
-    if ( l ) 
+    if ( l )
     {
         l->next = malloc(sizeof(list_t));
         l = l->next;
@@ -129,7 +153,7 @@ static int tag_exists(const char *tag, const char **tags)
     if ( !tags ) return -1;
     while ( tags[itag] )
     {
-        if ( tags[itag][0]==tag[0] && tags[itag][1]==tag[1] ) return itag; 
+        if ( tags[itag][0]==tag[0] && tags[itag][1]==tag[1] ) return itag;
         itag++;
     }
     return -1;
@@ -159,20 +183,20 @@ static const char *nextline(char **lineptr, size_t *n, const char *text)
     if ( !len )
         return to;
 
-    if ( !*lineptr ) 
+    if ( !*lineptr )
     {
         *lineptr = malloc(len);
         *n = len;
     }
-    else if ( *n<len ) 
+    else if ( *n<len )
     {
         *lineptr = realloc(*lineptr, len);
         *n = len;
     }
     if ( !*lineptr ) {
-		debug("[nextline] Insufficient memory!\n");
-		return 0;
-	}
+        debug("[nextline] Insufficient memory!\n");
+        return 0;
+    }
 
     memcpy(*lineptr,text,len);
     (*lineptr)[len-1] = 0;
@@ -222,9 +246,9 @@ static int sam_header_compare_lines(HeaderLine *hline1, HeaderLine *hline2)
 
     int itype = tag_exists(hline1->type,types);
     if ( itype==-1 ) {
-		debug("[sam_header_compare_lines] Unknown type [%c%c]\n", hline1->type[0],hline1->type[1]);
-		return -1; // FIXME (lh3): error; I do not know how this will be handled in Petr's code
-	}
+        debug("[sam_header_compare_lines] Unknown type [%c%c]\n", hline1->type[0],hline1->type[1]);
+        return -1; // FIXME (lh3): error; I do not know how this will be handled in Petr's code
+    }
 
     if ( unique_tags[itype] )
     {
@@ -257,7 +281,7 @@ static int sam_header_compare_lines(HeaderLine *hline1, HeaderLine *hline2)
         {
             if ( unique_tags[itype] )
                 return 2;   // the lines have a matching unique tag but have a conflicting tag
-                    
+
             return 0;    // the lines contain conflicting tags, cannot be merged
         }
         itag++;
@@ -318,7 +342,7 @@ static int sam_header_line_merge_with(HeaderLine *out_hline, const HeaderLine *t
 
     if ( out_hline->type[0]!=tmpl_hline->type[0] || out_hline->type[1]!=tmpl_hline->type[1] )
         return 0;
-    
+
     tmpl_tags = tmpl_hline->tags;
     while (tmpl_tags)
     {
@@ -346,31 +370,31 @@ static HeaderLine *sam_header_line_parse(const char *headerLine)
     from = headerLine;
 
     if ( *from != '@' ) {
-		debug("[sam_header_line_parse] expected '@', got [%s]\n", headerLine);
-		return 0;
-	}
+        debug("[sam_header_line_parse] expected '@', got [%s]\n", headerLine);
+        return 0;
+    }
     to = ++from;
 
     while (*to && *to!='\t') to++;
     if ( to-from != 2 ) {
-		debug("[sam_header_line_parse] expected '@XY', got [%s]\nHint: The header tags must be tab-separated.\n", headerLine);
-		return 0;
-	}
-    
+        debug("[sam_header_line_parse] expected '@XY', got [%s]\nHint: The header tags must be tab-separated.\n", headerLine);
+        return 0;
+    }
+
     hline = malloc(sizeof(HeaderLine));
     hline->type[0] = from[0];
     hline->type[1] = from[1];
     hline->tags = NULL;
 
     int itype = tag_exists(hline->type, types);
-    
+
     from = to;
     while (*to && *to=='\t') to++;
     if ( to-from != 1 ) {
         debug("[sam_header_line_parse] multiple tabs on line [%s] (%d)\n", headerLine,(int)(to-from));
         free(hline);
-		return 0;
-	}
+        return 0;
+    }
     from = to;
     while (*from)
     {
@@ -385,16 +409,16 @@ static HeaderLine *sam_header_line_parse(const char *headerLine)
         else
             tag = new_tag(from,from+3,to-1);
 
-        if ( header_line_has_tag(hline,tag->key) ) 
+        if ( header_line_has_tag(hline,tag->key) )
                 debug("The tag '%c%c' present (at least) twice on line [%s]\n", tag->key[0],tag->key[1], headerLine);
         hline->tags = list_append(hline->tags, tag);
 
         from = to;
         while (*to && *to=='\t') to++;
         if ( *to && to-from != 1 ) {
-			debug("[sam_header_line_parse] multiple tabs on line [%s] (%d)\n", headerLine,(int)(to-from));
-			return 0;
-		}
+            debug("[sam_header_line_parse] multiple tabs on line [%s] (%d)\n", headerLine,(int)(to-from));
+            return 0;
+        }
 
         from = to;
     }
@@ -408,10 +432,10 @@ static int sam_header_line_validate(HeaderLine *hline)
     list_t *tags;
     HeaderTag *tag;
     int itype, itag;
-    
+
     // Is the type correct?
     itype = tag_exists(hline->type, types);
-    if ( itype==-1 ) 
+    if ( itype==-1 )
     {
         debug("The type [%c%c] not recognised.\n", hline->type[0],hline->type[1]);
         return 0;
@@ -490,7 +514,7 @@ static void sam_header_line_free(HeaderLine *hline)
 
 void sam_header_free(void *_header)
 {
-	HeaderDict *header = (HeaderDict*)_header;
+    HeaderDict *header = (HeaderDict*)_header;
     list_t *hlines = header;
     while (hlines)
     {
@@ -515,7 +539,7 @@ HeaderDict *sam_header_clone(const HeaderDict *dict)
 // Returns a newly allocated string
 char *sam_header_write(const void *_header)
 {
-	const HeaderDict *header = (const HeaderDict*)_header;
+    const HeaderDict *header = (const HeaderDict*)_header;
     char *out = NULL;
     int len=0, nout=0;
     const list_t *hlines;
@@ -572,10 +596,10 @@ void *sam_header_parse2(const char *headerText)
     const char *text;
     char *buf=NULL;
     size_t nbuf = 0;
-	int tovalidate = 0;
+    int tovalidate = 0;
 
     if ( !headerText )
-		return 0;
+        return 0;
 
     text = headerText;
     while ( (text=nextline(&buf, &nbuf, text)) )
@@ -586,8 +610,8 @@ void *sam_header_parse2(const char *headerText)
             hlines = list_append_to_end(hlines, hline);
         else
         {
-			if (hline) sam_header_line_free(hline);
-			sam_header_free(hlines);
+            if (hline) sam_header_line_free(hline);
+            sam_header_free(hlines);
             if ( buf ) free(buf);
             return NULL;
         }
@@ -599,31 +623,31 @@ void *sam_header_parse2(const char *headerText)
 
 void *sam_header2tbl(const void *_dict, char type[2], char key_tag[2], char value_tag[2])
 {
-	const HeaderDict *dict = (const HeaderDict*)_dict;
+    const HeaderDict *dict = (const HeaderDict*)_dict;
     const list_t *l   = dict;
     khash_t(str) *tbl = kh_init(str);
     khiter_t k;
     int ret;
 
-	if (_dict == 0) return tbl; // return an empty (not null) hash table
+    if (_dict == 0) return tbl; // return an empty (not null) hash table
     while (l)
     {
         HeaderLine *hline = l->data;
-        if ( hline->type[0]!=type[0] || hline->type[1]!=type[1] ) 
+        if ( hline->type[0]!=type[0] || hline->type[1]!=type[1] )
         {
             l = l->next;
             continue;
         }
-        
+
         HeaderTag *key, *value;
         key   = header_line_has_tag(hline,key_tag);
-        value = header_line_has_tag(hline,value_tag); 
+        value = header_line_has_tag(hline,value_tag);
         if ( !key || !value )
         {
             l = l->next;
             continue;
         }
-        
+
         k = kh_get(str, tbl, key->value);
         if ( k != kh_end(tbl) )
             debug("[sam_header_lookup_table] They key %s not unique.\n", key->value);
@@ -637,21 +661,21 @@ void *sam_header2tbl(const void *_dict, char type[2], char key_tag[2], char valu
 
 char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n)
 {
-	const HeaderDict *dict = (const HeaderDict*)_dict;
+    const HeaderDict *dict = (const HeaderDict*)_dict;
     const list_t *l   = dict;
     int max, n;
-	char **ret;
+    char **ret;
 
-	ret = 0; *_n = max = n = 0;
+    ret = 0; *_n = max = n = 0;
     while (l)
     {
         HeaderLine *hline = l->data;
-        if ( hline->type[0]!=type[0] || hline->type[1]!=type[1] ) 
+        if ( hline->type[0]!=type[0] || hline->type[1]!=type[1] )
         {
             l = l->next;
             continue;
         }
-        
+
         HeaderTag *key;
         key   = header_line_has_tag(hline,key_tag);
         if ( !key )
@@ -660,15 +684,15 @@ char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n
             continue;
         }
 
-		if (n == max) {
-			max = max? max<<1 : 4;
-			ret = realloc(ret, max * sizeof(void*));
-		}
-		ret[n++] = key->value;
+        if (n == max) {
+            max = max? max<<1 : 4;
+            ret = realloc(ret, max * sizeof(char*));
+        }
+        ret[n++] = key->value;
 
         l = l->next;
     }
-	*_n = n;
+    *_n = n;
     return ret;
 }
 
@@ -689,7 +713,7 @@ void *sam_header2key_val(void *iter, const char type[2], const char key_tag[2],
         HeaderTag *key, *value;
         key   = header_line_has_tag(hline,key_tag);
         value = header_line_has_tag(hline,value_tag);
-        if ( !key && !value ) 
+        if ( !key && !value )
         {
             l = l->next;
             continue;
@@ -704,27 +728,27 @@ void *sam_header2key_val(void *iter, const char type[2], const char key_tag[2],
 
 const char *sam_tbl_get(void *h, const char *key)
 {
-	khash_t(str) *tbl = (khash_t(str)*)h;
-	khint_t k;
-	k = kh_get(str, tbl, key);
-	return k == kh_end(tbl)? 0 : kh_val(tbl, k);
+    khash_t(str) *tbl = (khash_t(str)*)h;
+    khint_t k;
+    k = kh_get(str, tbl, key);
+    return k == kh_end(tbl)? 0 : kh_val(tbl, k);
 }
 
 int sam_tbl_size(void *h)
 {
-	khash_t(str) *tbl = (khash_t(str)*)h;
-	return h? kh_size(tbl) : 0;
+    khash_t(str) *tbl = (khash_t(str)*)h;
+    return h? kh_size(tbl) : 0;
 }
 
 void sam_tbl_destroy(void *h)
 {
-	khash_t(str) *tbl = (khash_t(str)*)h;
-	kh_destroy(str, tbl);
+    khash_t(str) *tbl = (khash_t(str)*)h;
+    kh_destroy(str, tbl);
 }
 
 void *sam_header_merge(int n, const void **_dicts)
 {
-	const HeaderDict **dicts = (const HeaderDict**)_dicts;
+    const HeaderDict **dicts = (const HeaderDict**)_dicts;
     HeaderDict *out_dict;
     int idict, status;
 
@@ -748,13 +772,13 @@ void *sam_header_merge(int n, const void **_dicts)
                     out_hlines = out_hlines->next;
                     continue;
                 }
-                
-                if ( status==2 ) 
+
+                if ( status==2 )
                 {
                     print_header_line(pysamerr,tmpl_hlines->data);
                     print_header_line(pysamerr,out_hlines->data);
                     debug("Conflicting lines, cannot merge the headers.\n");
-					return 0;
+                    return 0;
                 }
                 if ( status==3 )
                     sam_header_line_merge_with(out_hlines->data, tmpl_hlines->data);
@@ -796,7 +820,7 @@ char **sam_header2tbl_n(const void *dict, const char type[2], const char *tags[]
         for (i=0; i<ntags; i++)
         {
             HeaderTag *key = header_line_has_tag(hline, tags[i]);
-            if ( !key ) 
+            if ( !key )
             {
                 out[nout*ntags+i] = NULL;
                 continue;
diff --git a/samtools/sam_header.h b/samtools/sam_header.h
index 4b0cb03..8266885 100644
--- a/samtools/sam_header.h
+++ b/samtools/sam_header.h
@@ -1,3 +1,27 @@
+/*  sam_header.h -- basic SAM/BAM header API.
+
+    Copyright (C) 2009, 2012, 2013 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #ifndef __SAM_HEADER_H__
 #define __SAM_HEADER_H__
 
@@ -5,30 +29,30 @@
 extern "C" {
 #endif
 
-	void *sam_header_parse2(const char *headerText);
-	void *sam_header_merge(int n, const void **dicts);
-	void sam_header_free(void *header);
-	char *sam_header_write(const void *headerDict);   // returns a newly allocated string
+    void *sam_header_parse2(const char *headerText);
+    void *sam_header_merge(int n, const void **dicts);
+    void sam_header_free(void *header);
+    char *sam_header_write(const void *headerDict);   // returns a newly allocated string
 
     /*
-        // Usage example 
-        const char *key, *val; 
+        // Usage example
+        const char *key, *val;
         void *iter = sam_header_parse2(bam->header->text);
         while ( iter = sam_header_key_val(iter, "RG","ID","SM" &key,&val) ) printf("%s\t%s\n", key,val);
     */
     void *sam_header2key_val(void *iter, const char type[2], const char key_tag[2], const char value_tag[2], const char **key, const char **value);
-	char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n);
+    char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n);
 
     /*
         // Usage example
         int i, j, n;
-        const char *tags[] = {"SN","LN","UR","M5",NULL}; 
+        const char *tags[] = {"SN","LN","UR","M5",NULL};
         void *dict = sam_header_parse2(bam->header->text);
         char **tbl = sam_header2tbl_n(h->dict, "SQ", tags, &n);
         for (i=0; i<n; i++)
         {
-            for (j=0; j<4; j++) 
-                if ( tbl[4*i+j] ) printf("\t%s", tbl[4*i+j]); 
+            for (j=0; j<4; j++)
+                if ( tbl[4*i+j] ) printf("\t%s", tbl[4*i+j]);
                 else printf("-");
             printf("\n");
         }
@@ -36,10 +60,10 @@ extern "C" {
      */
     char **sam_header2tbl_n(const void *dict, const char type[2], const char *tags[], int *n);
 
-	void *sam_header2tbl(const void *dict, char type[2], char key_tag[2], char value_tag[2]);
-	const char *sam_tbl_get(void *h, const char *key);
-	int sam_tbl_size(void *h);
-	void sam_tbl_destroy(void *h);
+    void *sam_header2tbl(const void *dict, char type[2], char key_tag[2], char value_tag[2]);
+    const char *sam_tbl_get(void *h, const char *key);
+    int sam_tbl_size(void *h);
+    void sam_tbl_destroy(void *h);
 
 #ifdef __cplusplus
 }
diff --git a/samtools/sam_view.c.pysam.c b/samtools/sam_view.c.pysam.c
index 8622aa1..9ae5ed6 100644
--- a/samtools/sam_view.c.pysam.c
+++ b/samtools/sam_view.c.pysam.c
@@ -1,443 +1,708 @@
 #include "pysam.h"
 
+/*  sam_view.c -- SAM<->BAM<->CRAM conversion.
+
+    Copyright (C) 2009-2014 Genome Research Ltd.
+    Portions copyright (C) 2009, 2011, 2012 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notices and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <math.h>
 #include <inttypes.h>
-#include "sam_header.h"
-#include "sam.h"
-#include "faidx.h"
-#include "kstring.h"
-#include "khash.h"
+#include <stdbool.h>
+#include <assert.h>
+#include "htslib/sam.h"
+#include "htslib/faidx.h"
+#include "htslib/kstring.h"
+#include "htslib/khash.h"
+#include "samtools.h"
 KHASH_SET_INIT_STR(rg)
 
-// When counting records instead of printing them,
-// data passed to the bam_fetch callback is encapsulated in this struct.
-typedef struct {
-	bam_header_t *header;
-	int64_t *count;  // int does overflow for very big BAMs
-} count_func_data_t;
-
 typedef khash_t(rg) *rghash_t;
 
-// FIXME: we'd better use no global variables...
-static rghash_t g_rghash = 0;
-static int g_min_mapQ = 0, g_flag_on = 0, g_flag_off = 0, g_qual_scale = 0, g_min_qlen = 0;
-static uint32_t g_subsam_seed = 0;
-static double g_subsam_frac = -1.;
-static char *g_library, *g_rg;
-static void *g_bed;
+// This structure contains the settings for a samview run
+typedef struct samview_settings {
+    rghash_t rghash;
+    int min_mapQ;
+    int flag_on;
+    int flag_off;
+    int min_qlen;
+    int remove_B;
+    uint32_t subsam_seed;
+    double subsam_frac;
+    char* library;
+    void* bed;
+    size_t remove_aux_len;
+    char** remove_aux;
+} samview_settings_t;
 
+
+// TODO Add declarations of these to a viable htslib or samtools header
+extern const char *bam_get_library(bam_hdr_t *header, const bam1_t *b);
+extern int bam_remove_B(bam1_t *b);
+extern char *samfaipath(const char *fn_ref);
 void *bed_read(const char *fn);
 void bed_destroy(void *_h);
 int bed_overlap(const void *_h, const char *chr, int beg, int end);
 
-static int process_aln(const bam_header_t *h, bam1_t *b)
+// Returns 0 to indicate read should be output 1 otherwise
+static int process_aln(const bam_hdr_t *h, bam1_t *b, samview_settings_t* settings)
 {
-	if (g_qual_scale > 1) {
-		int i;
-		uint8_t *qual = bam1_qual(b);
-		for (i = 0; i < b->core.l_qseq; ++i) {
-			int c = qual[i] * g_qual_scale;
-			qual[i] = c < 93? c : 93;
-		}
-	}
-	if (g_min_qlen > 0) {
-		int k, qlen = 0;
-		uint32_t *cigar = bam1_cigar(b);
-		for (k = 0; k < b->core.n_cigar; ++k)
-			if ((bam_cigar_type(bam_cigar_op(cigar[k]))&1) || bam_cigar_op(cigar[k]) == BAM_CHARD_CLIP)
-				qlen += bam_cigar_oplen(cigar[k]);
-		if (qlen < g_min_qlen) return 1;
-	}
-	if (b->core.qual < g_min_mapQ || ((b->core.flag & g_flag_on) != g_flag_on) || (b->core.flag & g_flag_off))
-		return 1;
-	if (g_bed && b->core.tid >= 0 && !bed_overlap(g_bed, h->target_name[b->core.tid], b->core.pos, bam_calend(&b->core, bam1_cigar(b))))
-		return 1;
-	if (g_subsam_frac > 0.) {
-		uint32_t k = __ac_X31_hash_string(bam1_qname(b)) + g_subsam_seed;
-		if ((double)(k&0xffffff) / 0x1000000 >= g_subsam_frac) return 1;
-	}
-	if (g_rg || g_rghash) {
-		uint8_t *s = bam_aux_get(b, "RG");
-		if (s) {
-			if (g_rg) return (strcmp(g_rg, (char*)(s + 1)) == 0)? 0 : 1;
-			if (g_rghash) {
-				khint_t k = kh_get(rg, g_rghash, (char*)(s + 1));
-				return (k != kh_end(g_rghash))? 0 : 1;
-			}
-		}
-	}
-	if (g_library) {
-		const char *p = bam_get_library((bam_header_t*)h, b);
-		return (p && strcmp(p, g_library) == 0)? 0 : 1;
-	}
-	return 0;
+    if (settings->remove_B) bam_remove_B(b);
+    if (settings->min_qlen > 0) {
+        int k, qlen = 0;
+        uint32_t *cigar = bam_get_cigar(b);
+        for (k = 0; k < b->core.n_cigar; ++k)
+            if ((bam_cigar_type(bam_cigar_op(cigar[k]))&1) || bam_cigar_op(cigar[k]) == BAM_CHARD_CLIP)
+                qlen += bam_cigar_oplen(cigar[k]);
+        if (qlen < settings->min_qlen) return 1;
+    }
+    if (b->core.qual < settings->min_mapQ || ((b->core.flag & settings->flag_on) != settings->flag_on) || (b->core.flag & settings->flag_off))
+        return 1;
+    if (settings->bed && (b->core.tid < 0 || !bed_overlap(settings->bed, h->target_name[b->core.tid], b->core.pos, bam_endpos(b))))
+        return 1;
+    if (settings->subsam_frac > 0.) {
+        uint32_t k = __ac_Wang_hash(__ac_X31_hash_string(bam_get_qname(b)) ^ settings->subsam_seed);
+        if ((double)(k&0xffffff) / 0x1000000 >= settings->subsam_frac) return 1;
+    }
+    if (settings->rghash) {
+        uint8_t *s = bam_aux_get(b, "RG");
+        if (s) {
+            khint_t k = kh_get(rg, settings->rghash, (char*)(s + 1));
+            if (k == kh_end(settings->rghash)) return 1;
+        }
+    }
+    if (settings->library) {
+        const char *p = bam_get_library((bam_hdr_t*)h, b);
+        if (p && strcmp(p, settings->library) != 0) return 1;
+    }
+    if (settings->remove_aux_len) {
+        size_t i;
+        for (i = 0; i < settings->remove_aux_len; ++i) {
+            uint8_t *s = bam_aux_get(b, settings->remove_aux[i]);
+            if (s) {
+                bam_aux_del(b, s);
+            }
+        }
+    }
+    return 0;
 }
 
 static char *drop_rg(char *hdtxt, rghash_t h, int *len)
 {
-	char *p = hdtxt, *q, *r, *s;
-	kstring_t str;
-	memset(&str, 0, sizeof(kstring_t));
-	while (1) {
-		int toprint = 0;
-		q = strchr(p, '\n');
-		if (q == 0) q = p + strlen(p);
-		if (q - p < 3) break; // the line is too short; then stop
-		if (strncmp(p, "@RG\t", 4) == 0) {
-			int c;
-			khint_t k;
-			if ((r = strstr(p, "\tID:")) != 0) {
-				r += 4;
-				for (s = r; *s != '\0' && *s != '\n' && *s != '\t'; ++s);
-				c = *s; *s = '\0';
-				k = kh_get(rg, h, r);
-				*s = c;
-				if (k != kh_end(h)) toprint = 1;
-			}
-		} else toprint = 1;
-		if (toprint) {
-			kputsn(p, q - p, &str); kputc('\n', &str);
-		}
-		p = q + 1;
-	}
-	*len = str.l;
-	return str.s;
+    char *p = hdtxt, *q, *r, *s;
+    kstring_t str;
+    memset(&str, 0, sizeof(kstring_t));
+    while (1) {
+        int toprint = 0;
+        q = strchr(p, '\n');
+        if (q == 0) q = p + strlen(p);
+        if (q - p < 3) break; // the line is too short; then stop
+        if (strncmp(p, "@RG\t", 4) == 0) {
+            int c;
+            khint_t k;
+            if ((r = strstr(p, "\tID:")) != 0) {
+                r += 4;
+                for (s = r; *s != '\0' && *s != '\n' && *s != '\t'; ++s);
+                c = *s; *s = '\0';
+                k = kh_get(rg, h, r);
+                *s = c;
+                if (k != kh_end(h)) toprint = 1;
+            }
+        } else toprint = 1;
+        if (toprint) {
+            kputsn(p, q - p, &str); kputc('\n', &str);
+        }
+        p = q + 1;
+    }
+    *len = str.l;
+    return str.s;
+}
+
+static int usage(int is_long_help);
+
+static int add_read_group_single(samview_settings_t *settings, char *name)
+{
+    char *d = strdup(name);
+    int ret = 0;
+
+    if (d == NULL) goto err;
+
+    if (settings->rghash == NULL) {
+        settings->rghash = kh_init(rg);
+        if (settings->rghash == NULL) goto err;
+    }
+
+    kh_put(rg, settings->rghash, d, &ret);
+    if (ret == -1) goto err;
+    if (ret ==  0) free(d); /* Duplicate */
+    return 0;
+
+ err:
+    print_error("Couldn't add \"%s\" to read group list: memory exhausted?", name);
+    free(d);
+    return -1;
 }
 
-// callback function for bam_fetch() that prints nonskipped records
-static int view_func(const bam1_t *b, void *data)
+static int add_read_groups_file(samview_settings_t *settings, char *fn)
 {
-	if (!process_aln(((samfile_t*)data)->header, (bam1_t*)b))
-		samwrite((samfile_t*)data, b);
-	return 0;
+    FILE *fp;
+    char buf[1024];
+    int ret = 0;
+    if (settings->rghash == NULL) {
+        settings->rghash = kh_init(rg);
+        if (settings->rghash == NULL) {
+            perror(NULL);
+            return -1;
+        }
+    }
+
+    fp = fopen(fn, "r");
+    if (fp == NULL) {
+        print_error_errno("failed to open \"%s\" for reading", fn);
+        return -1;
+    }
+
+    while (ret != -1 && !feof(fp) && fscanf(fp, "%1023s", buf) > 0) {
+        char *d = strdup(buf);
+        if (d != NULL) {
+            kh_put(rg, settings->rghash, d, &ret);
+            if (ret == 0) free(d); /* Duplicate */
+        } else {
+            ret = -1;
+        }
+    }
+    if (ferror(fp)) ret = -1;
+    if (ret == -1) {
+        print_error_errno("failed to read \"%s\"", fn);
+    }
+    fclose(fp);
+    return (ret != -1) ? 0 : -1;
 }
 
-// callback function for bam_fetch() that counts nonskipped records
-static int count_func(const bam1_t *b, void *data)
+static inline int check_sam_write1(samFile *fp, const bam_hdr_t *h, const bam1_t *b, const char *fname, int *retp)
 {
-	if (!process_aln(((count_func_data_t*)data)->header, (bam1_t*)b)) {
-		(*((count_func_data_t*)data)->count)++;
-	}
-	return 0;
+    int r = sam_write1(fp, h, b);
+    if (r >= 0) return r;
+
+    if (fname) print_error_errno("writing to \"%s\" failed", fname);
+    else print_error_errno("writing to standard output failed");
+
+    *retp = EXIT_FAILURE;
+    return r;
 }
 
-static int usage(int is_long_help);
+static void check_sam_close(samFile *fp, const char *fname, const char *null_fname, int *retp)
+{
+    int r = sam_close(fp);
+    if (r >= 0) return;
+
+    // TODO Need error infrastructure so we can print a message instead of r
+    if (fname) print_error("error closing \"%s\": %d", fname, r);
+    else print_error("error closing %s: %d", null_fname, r);
+
+    *retp = EXIT_FAILURE;
+}
 
 int main_samview(int argc, char *argv[])
 {
-	int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, compress_level = -1, is_bamout = 0, is_count = 0;
-	int of_type = BAM_OFDEC, is_long_help = 0, n_threads = 0;
-	int64_t count = 0;
-	samfile_t *in = 0, *out = 0;
-	char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0, *fn_rg = 0, *q;
-
-	/* parse command-line options */
-	strcpy(in_mode, "r"); strcpy(out_mode, "w");
-	while ((c = getopt(argc, argv, "SbBct:h1Ho:q:f:F:ul:r:xX?T:R:L:s:Q:@:m:")) >= 0) {
-		switch (c) {
-		case 's':
-			if ((g_subsam_seed = strtol(optarg, &q, 10)) != 0) {
-				srand(g_subsam_seed);
-				g_subsam_seed = rand();
-			}
-			g_subsam_frac = strtod(q, &q);
-			break;
-		case 'm': g_min_qlen = atoi(optarg); break;
-		case 'c': is_count = 1; break;
-		case 'S': is_bamin = 0; break;
-		case 'b': is_bamout = 1; break;
-		case 't': fn_list = strdup(optarg); is_bamin = 0; break;
-		case 'h': is_header = 1; break;
-		case 'H': is_header_only = 1; break;
-		case 'o': fn_out = strdup(optarg); break;
-		case 'f': g_flag_on = strtol(optarg, 0, 0); break;
-		case 'F': g_flag_off = strtol(optarg, 0, 0); break;
-		case 'q': g_min_mapQ = atoi(optarg); break;
-		case 'u': compress_level = 0; break;
-		case '1': compress_level = 1; break;
-		case 'l': g_library = strdup(optarg); break;
-		case 'L': g_bed = bed_read(optarg); break;
-		case 'r': g_rg = strdup(optarg); break;
-		case 'R': fn_rg = strdup(optarg); break;
-		case 'x': of_type = BAM_OFHEX; break;
-		case 'X': of_type = BAM_OFSTR; break;
-		case '?': is_long_help = 1; break;
-		case 'T': fn_ref = strdup(optarg); is_bamin = 0; break;
-		case 'B': bam_no_B = 1; break;
-		case 'Q': g_qual_scale = atoi(optarg); break;
-		case '@': n_threads = strtol(optarg, 0, 0); break;
-		default: return usage(is_long_help);
-		}
-	}
-	if (compress_level >= 0) is_bamout = 1;
-	if (is_header_only) is_header = 1;
-	if (is_bamout) strcat(out_mode, "b");
-	else {
-		if (of_type == BAM_OFHEX) strcat(out_mode, "x");
-		else if (of_type == BAM_OFSTR) strcat(out_mode, "X");
-	}
-	if (is_bamin) strcat(in_mode, "b");
-	if (is_header) strcat(out_mode, "h");
-	if (compress_level >= 0) {
-		char tmp[2];
-		tmp[0] = compress_level + '0'; tmp[1] = '\0';
-		strcat(out_mode, tmp);
-	}
-	if (argc == optind) return usage(is_long_help); // potential memory leak...
-
-	// read the list of read groups
-	if (fn_rg) {
-		FILE *fp_rg;
-		char buf[1024];
-		int ret;
-		g_rghash = kh_init(rg);
-		fp_rg = fopen(fn_rg, "r");
-		while (!feof(fp_rg) && fscanf(fp_rg, "%s", buf) > 0) // this is not a good style, but bear me...
-			kh_put(rg, g_rghash, strdup(buf), &ret); // we'd better check duplicates...
-		fclose(fp_rg);
-	}
-
-	// generate the fn_list if necessary
-	if (fn_list == 0 && fn_ref) fn_list = samfaipath(fn_ref);
-	// open file handlers
-	if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) {
-		fprintf(pysamerr, "[main_samview] fail to open \"%s\" for reading.\n", argv[optind]);
-		ret = 1;
-		goto view_end;
-	}
-	if (in->header == 0) {
-		fprintf(pysamerr, "[main_samview] fail to read the header from \"%s\".\n", argv[optind]);
-		ret = 1;
-		goto view_end;
-	}
-	if (g_rghash) { // FIXME: I do not know what "bam_header_t::n_text" is for...
-		char *tmp;
-		int l;
-		tmp = drop_rg(in->header->text, g_rghash, &l);
-		free(in->header->text);
-		in->header->text = tmp;
-		in->header->l_text = l;
-	}
-	if (!is_count && (out = samopen(fn_out? fn_out : "-", out_mode, in->header)) == 0) {
-		fprintf(pysamerr, "[main_samview] fail to open \"%s\" for writing.\n", fn_out? fn_out : "standard output");
-		ret = 1;
-		goto view_end;
-	}
-	if (n_threads > 1) samthreads(out, n_threads, 256); 
-	if (is_header_only) goto view_end; // no need to print alignments
-
-	if (argc == optind + 1) { // convert/print the entire file
-		bam1_t *b = bam_init1();
-		int r;
-		while ((r = samread(in, b)) >= 0) { // read one alignment from `in'
-			if (!process_aln(in->header, b)) {
-				if (!is_count) samwrite(out, b); // write the alignment to `out'
-				count++;
-			}
-		}
-		if (r < -1) {
-			fprintf(pysamerr, "[main_samview] truncated file.\n");
-			ret = 1;
-		}
-		bam_destroy1(b);
-	} else { // retrieve alignments in specified regions
-		int i;
-		bam_index_t *idx = 0;
-		if (is_bamin) idx = bam_index_load(argv[optind]); // load BAM index
-		if (idx == 0) { // index is unavailable
-			fprintf(pysamerr, "[main_samview] random alignment retrieval only works for indexed BAM files.\n");
-			ret = 1;
-			goto view_end;
-		}
-		for (i = optind + 1; i < argc; ++i) {
-			int tid, beg, end, result;
-			bam_parse_region(in->header, argv[i], &tid, &beg, &end); // parse a region in the format like `chr2:100-200'
-			if (tid < 0) { // reference name is not found
-				fprintf(pysamerr, "[main_samview] region \"%s\" specifies an unknown reference name. Continue anyway.\n", argv[i]);
-				continue;
-			}
-			// fetch alignments
-			if (is_count) {
-				count_func_data_t count_data = { in->header, &count };
-				result = bam_fetch(in->x.bam, idx, tid, beg, end, &count_data, count_func);
-			} else
-				result = bam_fetch(in->x.bam, idx, tid, beg, end, out, view_func);
-			if (result < 0) {
-				fprintf(pysamerr, "[main_samview] retrieval of region \"%s\" failed due to truncated file or corrupt BAM index file\n", argv[i]);
-				ret = 1;
-				break;
-			}
-		}
-		bam_index_destroy(idx); // destroy the BAM index
-	}
+    int c, is_header = 0, is_header_only = 0, ret = 0, compress_level = -1, is_count = 0;
+    int is_long_help = 0, n_threads = 0;
+    int64_t count = 0;
+    samFile *in = 0, *out = 0, *un_out=0;
+    bam_hdr_t *header = NULL;
+    char out_mode[5], *out_format = "", *fn_out = 0, *fn_list = 0, *fn_ref = 0, *q, *fn_un_out = 0;
+
+    samview_settings_t settings = {
+        .rghash = NULL,
+        .min_mapQ = 0,
+        .flag_on = 0,
+        .flag_off = 0,
+        .min_qlen = 0,
+        .remove_B = 0,
+        .subsam_seed = 0,
+        .subsam_frac = -1.,
+        .library = NULL,
+        .bed = NULL,
+    };
+
+    /* parse command-line options */
+    /* TODO: convert this to getopt_long we're running out of letters */
+    strcpy(out_mode, "w");
+    while ((c = getopt(argc, argv, "SbBcCt:h1Ho:q:f:F:ul:r:?T:R:L:s:@:m:x:U:")) >= 0) {
+        switch (c) {
+        case 's':
+            if ((settings.subsam_seed = strtol(optarg, &q, 10)) != 0) {
+                srand(settings.subsam_seed);
+                settings.subsam_seed = rand();
+            }
+            settings.subsam_frac = strtod(q, &q);
+            break;
+        case 'm': settings.min_qlen = atoi(optarg); break;
+        case 'c': is_count = 1; break;
+        case 'S': break;
+        case 'b': out_format = "b"; break;
+        case 'C': out_format = "c"; break;
+        case 't': fn_list = strdup(optarg); break;
+        case 'h': is_header = 1; break;
+        case 'H': is_header_only = 1; break;
+        case 'o': fn_out = strdup(optarg); break;
+        case 'U': fn_un_out = strdup(optarg); break;
+        case 'f': settings.flag_on |= strtol(optarg, 0, 0); break;
+        case 'F': settings.flag_off |= strtol(optarg, 0, 0); break;
+        case 'q': settings.min_mapQ = atoi(optarg); break;
+        case 'u': compress_level = 0; break;
+        case '1': compress_level = 1; break;
+        case 'l': settings.library = strdup(optarg); break;
+        case 'L':
+            if ((settings.bed = bed_read(optarg)) == NULL) {
+                print_error_errno("Could not read file \"%s\"", optarg);
+                ret = 1;
+                goto view_end;
+            }
+            break;
+        case 'r':
+            if (add_read_group_single(&settings, optarg) != 0) {
+                ret = 1;
+                goto view_end;
+            }
+            break;
+        case 'R':
+            if (add_read_groups_file(&settings, optarg) != 0) {
+                ret = 1;
+                goto view_end;
+            }
+            break;
+                /* REMOVED as htslib doesn't support this
+        //case 'x': out_format = "x"; break;
+        //case 'X': out_format = "X"; break;
+                 */
+        case '?': is_long_help = 1; break;
+        case 'T': fn_ref = strdup(optarg); break;
+        case 'B': settings.remove_B = 1; break;
+        case '@': n_threads = strtol(optarg, 0, 0); break;
+        case 'x':
+            {
+                if (strlen(optarg) != 2) {
+                    fprintf(pysamerr, "main_samview: Error parsing -x auxiliary tags should be exactly two characters long.\n");
+                    return usage(is_long_help);
+                }
+                settings.remove_aux = (char**)realloc(settings.remove_aux, sizeof(char*) * (++settings.remove_aux_len));
+                settings.remove_aux[settings.remove_aux_len-1] = optarg;
+            }
+            break;
+        default: return usage(is_long_help);
+        }
+    }
+    if (compress_level >= 0) out_format = "b";
+    if (is_header_only) is_header = 1;
+    strcat(out_mode, out_format);
+    if (compress_level >= 0) {
+        char tmp[2];
+        tmp[0] = compress_level + '0'; tmp[1] = '\0';
+        strcat(out_mode, tmp);
+    }
+    if (argc == optind) return usage(is_long_help); // potential memory leak...
+
+    // generate the fn_list if necessary
+    if (fn_list == 0 && fn_ref) fn_list = samfaipath(fn_ref);
+    // open file handlers
+    if ((in = sam_open(argv[optind], "r")) == 0) {
+        print_error_errno("failed to open \"%s\" for reading", argv[optind]);
+        ret = 1;
+        goto view_end;
+    }
+    if (fn_list) hts_set_fai_filename(in, fn_list);
+    if ((header = sam_hdr_read(in)) == 0) {
+        fprintf(pysamerr, "[main_samview] fail to read the header from \"%s\".\n", argv[optind]);
+        ret = 1;
+        goto view_end;
+    }
+    if (settings.rghash) { // FIXME: I do not know what "bam_header_t::n_text" is for...
+        char *tmp;
+        int l;
+        tmp = drop_rg(header->text, settings.rghash, &l);
+        free(header->text);
+        header->text = tmp;
+        header->l_text = l;
+    }
+    if (!is_count) {
+        if ((out = sam_open(fn_out? fn_out : "-", out_mode)) == 0) {
+            print_error_errno("failed to open \"%s\" for writing", fn_out? fn_out : "standard output");
+            ret = 1;
+            goto view_end;
+        }
+        if (fn_list) hts_set_fai_filename(out, fn_list);
+        if (*out_format || is_header) sam_hdr_write(out, header);
+        if (fn_un_out) {
+            if ((un_out = sam_open(fn_un_out, out_mode)) == 0) {
+                print_error_errno("failed to open \"%s\" for writing", fn_un_out);
+                ret = 1;
+                goto view_end;
+            }
+            if (*out_format || is_header) sam_hdr_write(un_out, header);
+        }
+    }
+    if (n_threads > 1) { hts_set_threads(out, n_threads); }
+    if (is_header_only) goto view_end; // no need to print alignments
+
+    if (argc == optind + 1) { // convert/print the entire file
+        bam1_t *b = bam_init1();
+        int r;
+        while ((r = sam_read1(in, header, b)) >= 0) { // read one alignment from `in'
+            if (!process_aln(header, b, &settings)) {
+                if (!is_count) { if (check_sam_write1(out, header, b, fn_out, &ret) < 0) break; }
+                count++;
+            } else {
+                if (un_out) { if (check_sam_write1(un_out, header, b, fn_un_out, &ret) < 0) break; }
+            }
+        }
+        if (r < -1) {
+            fprintf(pysamerr, "[main_samview] truncated file.\n");
+            ret = 1;
+        }
+        bam_destroy1(b);
+    } else { // retrieve alignments in specified regions
+        int i;
+        bam1_t *b;
+        hts_idx_t *idx = sam_index_load(in, argv[optind]); // load index
+        if (idx == 0) { // index is unavailable
+            fprintf(pysamerr, "[main_samview] random alignment retrieval only works for indexed BAM or CRAM files.\n");
+            ret = 1;
+            goto view_end;
+        }
+        b = bam_init1();
+        for (i = optind + 1; i < argc; ++i) {
+            int result;
+            hts_itr_t *iter = sam_itr_querys(idx, header, argv[i]); // parse a region in the format like `chr2:100-200'
+            if (iter == NULL) { // reference name is not found
+                fprintf(pysamerr, "[main_samview] region \"%s\" specifies an unknown reference name. Continue anyway.\n", argv[i]);
+                continue;
+            }
+            // fetch alignments
+            while ((result = sam_itr_next(in, iter, b)) >= 0) {
+                if (!process_aln(header, b, &settings)) {
+                    if (!is_count) { if (check_sam_write1(out, header, b, fn_out, &ret) < 0) break; }
+                    count++;
+                } else {
+                    if (un_out) { if (check_sam_write1(un_out, header, b, fn_un_out, &ret) < 0) break; }
+                }
+            }
+            hts_itr_destroy(iter);
+            if (result < -1) {
+                fprintf(pysamerr, "[main_samview] retrieval of region \"%s\" failed due to truncated file or corrupt BAM index file\n", argv[i]);
+                ret = 1;
+                break;
+            }
+        }
+        bam_destroy1(b);
+        hts_idx_destroy(idx); // destroy the BAM index
+    }
 
 view_end:
-	if (is_count && ret == 0) 
-		printf("%" PRId64 "\n", count);
-
-	// close files, free and return
-	free(fn_list); free(fn_ref); free(fn_out); free(g_library); free(g_rg); free(fn_rg);
-	if (g_bed) bed_destroy(g_bed);
-	if (g_rghash) {
-		khint_t k;
-		for (k = 0; k < kh_end(g_rghash); ++k)
-			if (kh_exist(g_rghash, k)) free((char*)kh_key(g_rghash, k));
-		kh_destroy(rg, g_rghash);
-	}
-	samclose(in);
-	if (!is_count)
-		samclose(out);
-	return ret;
+    if (is_count && ret == 0)
+        printf("%" PRId64 "\n", count);
+
+    // close files, free and return
+    if (in) check_sam_close(in, argv[optind], "standard input", &ret);
+    if (out) check_sam_close(out, fn_out, "standard output", &ret);
+    if (un_out) check_sam_close(un_out, fn_un_out, "file", &ret);
+
+    free(fn_list); free(fn_ref); free(fn_out); free(settings.library);  free(fn_un_out);
+    if ( header ) bam_hdr_destroy(header);
+    if (settings.bed) bed_destroy(settings.bed);
+    if (settings.rghash) {
+        khint_t k;
+        for (k = 0; k < kh_end(settings.rghash); ++k)
+            if (kh_exist(settings.rghash, k)) free((char*)kh_key(settings.rghash, k));
+        kh_destroy(rg, settings.rghash);
+    }
+    if (settings.remove_aux_len) {
+        free(settings.remove_aux);
+    }
+    return ret;
 }
 
 static int usage(int is_long_help)
 {
-	fprintf(pysamerr, "\n");
-	fprintf(pysamerr, "Usage:   samtools view [options] <in.bam>|<in.sam> [region1 [...]]\n\n");
-	fprintf(pysamerr, "Options: -b       output BAM\n");
-	fprintf(pysamerr, "         -h       print header for the SAM output\n");
-	fprintf(pysamerr, "         -H       print header only (no alignments)\n");
-	fprintf(pysamerr, "         -S       input is SAM\n");
-	fprintf(pysamerr, "         -u       uncompressed BAM output (force -b)\n");
-	fprintf(pysamerr, "         -1       fast compression (force -b)\n");
-	fprintf(pysamerr, "         -x       output FLAG in HEX (samtools-C specific)\n");
-	fprintf(pysamerr, "         -X       output FLAG in string (samtools-C specific)\n");
-	fprintf(pysamerr, "         -c       print only the count of matching records\n");
-	fprintf(pysamerr, "         -B       collapse the backward CIGAR operation\n");
-	fprintf(pysamerr, "         -@ INT   number of BAM compression threads [0]\n");
-	fprintf(pysamerr, "         -L FILE  output alignments overlapping the input BED FILE [null]\n");
-	fprintf(pysamerr, "         -t FILE  list of reference names and lengths (force -S) [null]\n");
-	fprintf(pysamerr, "         -T FILE  reference sequence file (force -S) [null]\n");
-	fprintf(pysamerr, "         -o FILE  output file name [stdout]\n");
-	fprintf(pysamerr, "         -R FILE  list of read groups to be outputted [null]\n");
-	fprintf(pysamerr, "         -f INT   required flag, 0 for unset [0]\n");
-	fprintf(pysamerr, "         -F INT   filtering flag, 0 for unset [0]\n");
-	fprintf(pysamerr, "         -q INT   minimum mapping quality [0]\n");
-	fprintf(pysamerr, "         -l STR   only output reads in library STR [null]\n");
-	fprintf(pysamerr, "         -r STR   only output reads in read group STR [null]\n");
-	fprintf(pysamerr, "         -s FLOAT fraction of templates to subsample; integer part as seed [-1]\n");
-	fprintf(pysamerr, "         -?       longer help\n");
-	fprintf(pysamerr, "\n");
-	if (is_long_help)
-		fprintf(pysamerr, "Notes:\n\
+    fprintf(pysamerr, "\n");
+    fprintf(pysamerr, "Usage:   samtools view [options] <in.bam>|<in.sam>|<in.cram> [region ...]\n\n");
+    // output options
+    fprintf(pysamerr, "Options: -b       output BAM\n");
+    fprintf(pysamerr, "         -C       output CRAM (requires -T)\n");
+    fprintf(pysamerr, "         -1       use fast BAM compression (implies -b)\n");
+    fprintf(pysamerr, "         -u       uncompressed BAM output (implies -b)\n");
+    fprintf(pysamerr, "         -h       include header in SAM output\n");
+    fprintf(pysamerr, "         -H       print SAM header only (no alignments)\n");
+    fprintf(pysamerr, "         -c       print only the count of matching records\n");
+    fprintf(pysamerr, "         -o FILE  output file name [stdout]\n");
+    fprintf(pysamerr, "         -U FILE  output reads not selected by filters to FILE [null]\n");
+    // extra input
+    fprintf(pysamerr, "         -t FILE  FILE listing reference names and lengths (see long help) [null]\n");
+    fprintf(pysamerr, "         -T FILE  reference sequence FASTA FILE [null]\n");
+    // read filters
+    fprintf(pysamerr, "         -L FILE  only include reads overlapping this BED FILE [null]\n");
+    fprintf(pysamerr, "         -r STR   only include reads in read group STR [null]\n");
+    fprintf(pysamerr, "         -R FILE  only include reads with read group listed in FILE [null]\n");
+    fprintf(pysamerr, "         -q INT   only include reads with mapping quality >= INT [0]\n");
+    fprintf(pysamerr, "         -l STR   only include reads in library STR [null]\n");
+    fprintf(pysamerr, "         -m INT   only include reads with number of CIGAR operations\n");
+    fprintf(pysamerr, "                  consuming query sequence >= INT [0]\n");
+    fprintf(pysamerr, "         -f INT   only include reads with all bits set in INT set in FLAG [0]\n");
+    fprintf(pysamerr, "         -F INT   only include reads with none of the bits set in INT\n");
+    fprintf(pysamerr, "                  set in FLAG [0]\n");
+    // read processing
+    fprintf(pysamerr, "         -x STR   read tag to strip (repeatable) [null]\n");
+    fprintf(pysamerr, "         -B       collapse the backward CIGAR operation\n");
+    fprintf(pysamerr, "         -s FLOAT integer part sets seed of random number generator [0];\n");
+    fprintf(pysamerr, "                  rest sets fraction of templates to subsample [no subsampling]\n");
+    // general options
+    fprintf(pysamerr, "         -@ INT   number of BAM compression threads [0]\n");
+    fprintf(pysamerr, "         -?       print long help, including note about region specification\n");
+    fprintf(pysamerr, "         -S       ignored (input format is auto-detected)\n");
+    fprintf(pysamerr, "\n");
+    if (is_long_help)
+        fprintf(pysamerr, "Notes:\n\
+\n\
+  1. This command now auto-detects the input format (BAM/CRAM/SAM).\n\
 \n\
-  1. By default, this command assumes the file on the command line is in\n\
-     the BAM format and it prints the alignments in SAM. If `-t' is\n\
-     applied, the input file is assumed to be in the SAM format. The\n\
-     file supplied with `-t' is SPACE/TAB delimited with the first two\n\
-     fields of each line consisting of the reference name and the\n\
-     corresponding sequence length. The `.fai' file generated by `faidx'\n\
-     can be used here. This file may be empty if reads are unaligned.\n\
+  2. The file supplied with `-t' is SPACE/TAB delimited with the first\n\
+     two fields of each line consisting of the reference name and the\n\
+     corresponding sequence length. The `.fai' file generated by \n\
+     `samtools faidx' is suitable for use as this file. This may be an\n\
+     empty file if reads are unaligned.\n\
 \n\
-  2. SAM->BAM conversion: `samtools view -bT ref.fa in.sam.gz'.\n\
+  3. SAM->BAM conversion: `samtools view -bT ref.fa in.sam.gz'.\n\
 \n\
-  3. BAM->SAM conversion: `samtools view in.bam'.\n\
+  4. BAM->SAM conversion: `samtools view -h in.bam'.\n\
 \n\
-  4. A region should be presented in one of the following formats:\n\
+  5. A region should be presented in one of the following formats:\n\
      `chr1', `chr2:1,000' and `chr3:1000-2,000'. When a region is\n\
-     specified, the input alignment file must be an indexed BAM file.\n\
+     specified, the input alignment file must be a sorted and indexed\n\
+     alignment (BAM/CRAM) file.\n\
 \n\
-  5. Option `-u' is preferred over `-b' when the output is piped to\n\
+  6. Option `-u' is preferred over `-b' when the output is piped to\n\
      another samtools command.\n\
-\n\
-  6. In a string FLAG, each character represents one bit with\n\
-     p=0x1 (paired), P=0x2 (properly paired), u=0x4 (unmapped),\n\
-     U=0x8 (mate unmapped), r=0x10 (reverse), R=0x20 (mate reverse)\n\
-     1=0x40 (first), 2=0x80 (second), s=0x100 (not primary), \n\
-     f=0x200 (failure) and d=0x400 (duplicate). Note that `-x' and\n\
-     `-X' are samtools-C specific. Picard and older samtools do not\n\
-     support HEX or string flags.\n\
 \n");
-	return 1;
+    return 1;
 }
 
 int main_import(int argc, char *argv[])
 {
-	int argc2, ret;
-	char **argv2;
-	if (argc != 4) {
-		fprintf(pysamerr, "Usage: bamtk import <in.ref_list> <in.sam> <out.bam>\n");
-		return 1;
-	}
-	argc2 = 6;
-	argv2 = calloc(6, sizeof(char*));
-	argv2[0] = "import", argv2[1] = "-o", argv2[2] = argv[3], argv2[3] = "-bt", argv2[4] = argv[1], argv2[5] = argv[2];
-	ret = main_samview(argc2, argv2);
-	free(argv2);
-	return ret;
+    int argc2, ret;
+    char **argv2;
+    if (argc != 4) {
+        fprintf(pysamerr, "Usage: samtools import <in.ref_list> <in.sam> <out.bam>\n");
+        return 1;
+    }
+    argc2 = 6;
+    argv2 = calloc(6, sizeof(char*));
+    argv2[0] = "import", argv2[1] = "-o", argv2[2] = argv[3], argv2[3] = "-bt", argv2[4] = argv[1], argv2[5] = argv[2];
+    ret = main_samview(argc2, argv2);
+    free(argv2);
+    return ret;
 }
 
-int8_t seq_comp_table[16] = { 0, 8, 4, 12, 2, 10, 9, 14, 1, 6, 5, 13, 3, 11, 7, 15 };
+int8_t seq_comp_table[16] = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 };
+
+static void bam2fq_usage(FILE *to)
+{
+    fprintf(to, "\nUsage:   samtools bam2fq [-nO] [-s <outSE.fq>] <in.bam>\n\n");
+    fprintf(to, "Options: -n        don't append /1 and /2 to the read name\n");
+    fprintf(to, "         -O        output quality in the OQ tag if present\n");
+    fprintf(to, "         -s FILE   write singleton reads to FILE [assume single-end]\n");
+    fprintf(to, "\n");
+}
 
 int main_bam2fq(int argc, char *argv[])
 {
-	bamFile fp;
-	bam_header_t *h;
-	bam1_t *b;
-	int8_t *buf;
-	int max_buf, c, no12 = 0;
-	while ((c = getopt(argc, argv, "n")) > 0)
-		if (c == 'n') no12 = 1;
-	if (argc == 1) {
-		fprintf(pysamerr, "Usage: samtools bam2fq <in.bam>\n");
-		return 1;
-	}
-	fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
-	if (fp == 0) return 1;
-	h = bam_header_read(fp);
-	b = bam_init1();
-	buf = 0;
-	max_buf = 0;
-	while (bam_read1(fp, b) >= 0) {
-		int i, qlen = b->core.l_qseq;
-		uint8_t *seq;
-		putchar('@'); fputs(bam1_qname(b), stdout);
-		if (no12) putchar('\n');
-		else {
-			if ((b->core.flag & 0x40) && !(b->core.flag & 0x80)) puts("/1");
-			else if ((b->core.flag & 0x80) && !(b->core.flag & 0x40)) puts("/2");
-			else putchar('\n');
-		}
-		if (max_buf < qlen + 1) {
-			max_buf = qlen + 1;
-			kroundup32(max_buf);
-			buf = realloc(buf, max_buf);
-		}
-		buf[qlen] = 0;
-		seq = bam1_seq(b);
-		for (i = 0; i < qlen; ++i)
-			buf[i] = bam1_seqi(seq, i);
-		if (b->core.flag & 16) { // reverse complement
-			for (i = 0; i < qlen>>1; ++i) {
-				int8_t t = seq_comp_table[buf[qlen - 1 - i]];
-				buf[qlen - 1 - i] = seq_comp_table[buf[i]];
-				buf[i] = t;
-			}
-			if (qlen&1) buf[i] = seq_comp_table[buf[i]];
-		}
-		for (i = 0; i < qlen; ++i)
-			buf[i] = bam_nt16_rev_table[buf[i]];
-		puts((char*)buf);
-		puts("+");
-		seq = bam1_qual(b);
-		for (i = 0; i < qlen; ++i)
-			buf[i] = 33 + seq[i];
-		if (b->core.flag & 16) { // reverse
-			for (i = 0; i < qlen>>1; ++i) {
-				int8_t t = buf[qlen - 1 - i];
-				buf[qlen - 1 - i] = buf[i];
-				buf[i] = t;
-			}
-		}
-		puts((char*)buf);
-	}
-	free(buf);
-	bam_destroy1(b);
-	bam_header_destroy(h);
-	bam_close(fp);
-	return 0;
+    samFile *fp;
+    bam_hdr_t *h;
+    bam1_t *b;
+    int8_t *buf;
+    int status = EXIT_SUCCESS;
+    size_t max_buf;
+    FILE* fpse;
+    // Parse args
+    char* fnse = NULL;
+    bool has12 = true, use_oq = false;
+    int c;
+    while ((c = getopt(argc, argv, "nOs:")) > 0) {
+        switch (c) {
+            case 'n': has12 = false; break;
+            case 'O': use_oq = true; break;
+            case 's': fnse = optarg; break;
+            default: bam2fq_usage(pysamerr); return 1;
+        }
+    }
+
+    if ((argc - (optind)) == 0) {
+        bam2fq_usage(stdout);
+        return 0;
+    }
+
+    if ((argc - (optind)) != 1) {
+        fprintf(pysamerr, "Too many arguments.\n");
+        bam2fq_usage(pysamerr);
+        return 1;
+    }
+
+    fp = sam_open(argv[optind], "r");
+    if (fp == NULL) {
+        print_error_errno("Cannot read file \"%s\"", argv[optind]);
+        return 1;
+    }
+    fpse = NULL;
+    if (fnse) {
+        fpse = fopen(fnse,"w");
+        if (fpse == NULL) {
+            print_error_errno("Cannot write to singleton file \"%s\"", fnse);
+            return 1;
+        }
+    }
+
+    h = sam_hdr_read(fp);
+    b = bam_init1();
+    buf = NULL;
+    max_buf = 0;
+
+    int64_t n_singletons = 0, n_reads = 0;
+    char* previous = NULL;
+    kstring_t linebuf = { 0, 0, NULL };
+    kputsn("", 0, &linebuf);
+
+    while (sam_read1(fp, h, b) >= 0) {
+        if (b->core.flag&(BAM_FSECONDARY|BAM_FSUPPLEMENTARY)) continue; // skip secondary and supplementary alignments
+        ++n_reads;
+
+        int i;
+        int32_t qlen = b->core.l_qseq;
+        assert(qlen >= 0);
+        uint8_t* seq;
+        uint8_t* qual = bam_get_qual(b);
+        const uint8_t *oq = NULL;
+        if (use_oq) oq = bam_aux_get(b, "OQ");
+        bool has_qual = (qual[0] != 0xff || (use_oq && oq)); // test if there is quality
+
+        // If there was a previous readname
+        if ( fpse && previous ) {
+            if (!strcmp(bam_get_qname(b), previous ) ) {
+                fputs(linebuf.s, stdout); // Write previous read
+                free(previous);
+                previous = NULL;
+            } else { // Doesn't match it's a singleton
+                ++n_singletons;
+                fputs(linebuf.s, fpse);  // Write previous read to singletons
+                free(previous);
+                previous = strdup(bam_get_qname(b));
+            }
+        } else {
+            fputs(linebuf.s, stdout); // Write pending read
+            if (fpse) previous = strdup(bam_get_qname(b));
+        }
+
+        linebuf.l = 0;
+        // Write read name
+        kputc(!has_qual? '>' : '@', &linebuf);
+        kputs(bam_get_qname(b), &linebuf);
+        // Add the /1 /2 if requested
+        if (has12) {
+            if ((b->core.flag & BAM_FREAD1) && !(b->core.flag & BAM_FREAD2)) kputs("/1\n", &linebuf);
+            else if ((b->core.flag & BAM_FREAD2) && !(b->core.flag & BAM_FREAD1)) kputs("/2\n", &linebuf);
+            else kputc('\n', &linebuf);
+        } else {
+            kputc('\n', &linebuf);
+        }
+
+        if (max_buf < qlen + 1) {
+            max_buf = qlen + 1;
+            kroundup32(max_buf);
+            buf = realloc(buf, max_buf);
+            if (buf == NULL) {
+                fprintf(pysamerr, "Out of memory");
+                return 1;
+            }
+        }
+        buf[qlen] = '\0';
+        seq = bam_get_seq(b);
+        for (i = 0; i < qlen; ++i)
+            buf[i] = bam_seqi(seq, i);
+        if (b->core.flag & BAM_FREVERSE) { // reverse complement
+            for (i = 0; i < qlen>>1; ++i) {
+                int8_t t = seq_comp_table[buf[qlen - 1 - i]];
+                buf[qlen - 1 - i] = seq_comp_table[buf[i]];
+                buf[i] = t;
+            }
+            if (qlen&1) buf[i] = seq_comp_table[buf[i]];
+        }
+        for (i = 0; i < qlen; ++i)
+            buf[i] = seq_nt16_str[buf[i]];
+        kputs((char*)buf, &linebuf);
+        kputc('\n', &linebuf);
+
+        if (has_qual) {
+            // Write quality
+            kputs("+\n", &linebuf);
+            if (use_oq && oq) memcpy(buf, oq + 1, qlen);
+            else {
+                for (i = 0; i < qlen; ++i)
+                    buf[i] = 33 + qual[i];
+            }
+            if (b->core.flag & BAM_FREVERSE) { // reverse
+                for (i = 0; i < qlen>>1; ++i) {
+                    int8_t t = buf[qlen - 1 - i];
+                    buf[qlen - 1 - i] = buf[i];
+                    buf[i] = t;
+                }
+            }
+            kputs((char*)buf, &linebuf);
+            kputc('\n', &linebuf);
+        }
+    }
+
+    if (fpse) {
+        if ( previous ) { // Nothing left to match it's a singleton
+            ++n_singletons;
+            fputs(linebuf.s, fpse);  // Write previous read to singletons
+        } else {
+            fputs(linebuf.s, stdout); // Write previous read
+        }
+
+        fprintf(pysamerr, "[M::%s] discarded %" PRId64 " singletons\n", __func__, n_singletons);
+        fclose(fpse);
+    } else {
+        fputs(linebuf.s, stdout); // Write previous read
+    }
+    free(linebuf.s);
+    free(previous);
+
+    fprintf(pysamerr, "[M::%s] processed %" PRId64 " reads\n", __func__, n_reads);
+
+    free(buf);
+    bam_destroy1(b);
+    bam_hdr_destroy(h);
+    check_sam_close(fp, argv[optind], "file", &status);
+    return status;
 }
diff --git a/samtools/sample.c.pysam.c b/samtools/sample.c.pysam.c
index 5f3d655..73ec01f 100644
--- a/samtools/sample.c.pysam.c
+++ b/samtools/sample.c.pysam.c
@@ -1,89 +1,114 @@
 #include "pysam.h"
 
+/*  sample.c -- group data by sample.
+
+    Copyright (C) 2010, 2011 Broad Institute.
+    Copyright (C) 2013 Genome Research Ltd.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #include <stdlib.h>
 #include <string.h>
 #include "sample.h"
-#include "khash.h"
+#include "htslib/khash.h"
 KHASH_MAP_INIT_STR(sm, int)
 
 bam_sample_t *bam_smpl_init(void)
 {
-	bam_sample_t *s;
-	s = calloc(1, sizeof(bam_sample_t));
-	s->rg2smid = kh_init(sm);
-	s->sm2id = kh_init(sm);
-	return s;
+    bam_sample_t *s;
+    s = calloc(1, sizeof(bam_sample_t));
+    s->rg2smid = kh_init(sm);
+    s->sm2id = kh_init(sm);
+    return s;
 }
 
 void bam_smpl_destroy(bam_sample_t *sm)
 {
-	int i;
-	khint_t k;
-	khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
-	if (sm == 0) return;
-	for (i = 0; i < sm->n; ++i) free(sm->smpl[i]);
-	free(sm->smpl);
-	for (k = kh_begin(rg2smid); k != kh_end(rg2smid); ++k)
-		if (kh_exist(rg2smid, k)) free((char*)kh_key(rg2smid, k));
-	kh_destroy(sm, sm->rg2smid);
-	kh_destroy(sm, sm->sm2id);
-	free(sm);
+    int i;
+    khint_t k;
+    khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
+    if (sm == 0) return;
+    for (i = 0; i < sm->n; ++i) free(sm->smpl[i]);
+    free(sm->smpl);
+    for (k = kh_begin(rg2smid); k != kh_end(rg2smid); ++k)
+        if (kh_exist(rg2smid, k)) free((char*)kh_key(rg2smid, k));
+    kh_destroy(sm, sm->rg2smid);
+    kh_destroy(sm, sm->sm2id);
+    free(sm);
 }
 
 static void add_pair(bam_sample_t *sm, khash_t(sm) *sm2id, const char *key, const char *val)
 {
-	khint_t k_rg, k_sm;
-	int ret;
-	khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
-	k_rg = kh_get(sm, rg2smid, key);
-	if (k_rg != kh_end(rg2smid)) return; // duplicated @RG-ID
-	k_rg = kh_put(sm, rg2smid, strdup(key), &ret);
-	k_sm = kh_get(sm, sm2id, val);
-	if (k_sm == kh_end(sm2id)) { // absent
-		if (sm->n == sm->m) {
-			sm->m = sm->m? sm->m<<1 : 1;
-			sm->smpl = realloc(sm->smpl, sizeof(void*) * sm->m);
-		}
-		sm->smpl[sm->n] = strdup(val);
-		k_sm = kh_put(sm, sm2id, sm->smpl[sm->n], &ret);
-		kh_val(sm2id, k_sm) = sm->n++;
-	}
-	kh_val(rg2smid, k_rg) = kh_val(sm2id, k_sm);
+    khint_t k_rg, k_sm;
+    int ret;
+    khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
+    k_rg = kh_get(sm, rg2smid, key);
+    if (k_rg != kh_end(rg2smid)) return; // duplicated @RG-ID
+    k_rg = kh_put(sm, rg2smid, strdup(key), &ret);
+    k_sm = kh_get(sm, sm2id, val);
+    if (k_sm == kh_end(sm2id)) { // absent
+        if (sm->n == sm->m) {
+            sm->m = sm->m? sm->m<<1 : 1;
+            sm->smpl = realloc(sm->smpl, sizeof(char*) * sm->m);
+        }
+        sm->smpl[sm->n] = strdup(val);
+        k_sm = kh_put(sm, sm2id, sm->smpl[sm->n], &ret);
+        kh_val(sm2id, k_sm) = sm->n++;
+    }
+    kh_val(rg2smid, k_rg) = kh_val(sm2id, k_sm);
 }
 
 int bam_smpl_add(bam_sample_t *sm, const char *fn, const char *txt)
 {
-	const char *p = txt, *q, *r;
-	kstring_t buf, first_sm;
-	int n = 0;
-	khash_t(sm) *sm2id = (khash_t(sm)*)sm->sm2id;
-	if (txt == 0) {
-		add_pair(sm, sm2id, fn, fn);
-		return 0;
-	}
-	memset(&buf, 0, sizeof(kstring_t));
+    const char *p = txt, *q, *r;
+    kstring_t buf, first_sm;
+    int n = 0;
+    khash_t(sm) *sm2id = (khash_t(sm)*)sm->sm2id;
+    if (txt == 0) {
+        add_pair(sm, sm2id, fn, fn);
+        return 0;
+    }
+    memset(&buf, 0, sizeof(kstring_t));
     memset(&first_sm, 0, sizeof(kstring_t));
-	while ((q = strstr(p, "@RG")) != 0) {
-		p = q + 3;
-		r = q = 0;
-		if ((q = strstr(p, "\tID:")) != 0) q += 4;
-		if ((r = strstr(p, "\tSM:")) != 0) r += 4;
-		if (r && q) {
-			char *u, *v;
-			int oq, or;
-			for (u = (char*)q; *u && *u != '\t' && *u != '\n'; ++u);
-			for (v = (char*)r; *v && *v != '\t' && *v != '\n'; ++v);
-			oq = *u; or = *v; *u = *v = '\0';
-			buf.l = 0; kputs(fn, &buf); kputc('/', &buf); kputs(q, &buf);
-			add_pair(sm, sm2id, buf.s, r);
+    while ((q = strstr(p, "@RG")) != 0) {
+        p = q + 3;
+        r = q = 0;
+        if ((q = strstr(p, "\tID:")) != 0) q += 4;
+        if ((r = strstr(p, "\tSM:")) != 0) r += 4;
+        if (r && q) {
+            char *u, *v;
+            int oq, or;
+            for (u = (char*)q; *u && *u != '\t' && *u != '\n'; ++u);
+            for (v = (char*)r; *v && *v != '\t' && *v != '\n'; ++v);
+            oq = *u; or = *v; *u = *v = '\0';
+            buf.l = 0; kputs(fn, &buf); kputc('/', &buf); kputs(q, &buf);
+            add_pair(sm, sm2id, buf.s, r);
             if ( !first_sm.s )
-                kputs(r,&first_sm); 
-			*u = oq; *v = or;
-		} else break;
-		p = q > r? q : r;
-		++n;
-	}
-	if (n == 0) add_pair(sm, sm2id, fn, fn);
+                kputs(r,&first_sm);
+            *u = oq; *v = or;
+        } else break;
+        p = q > r? q : r;
+        ++n;
+    }
+    if (n == 0) add_pair(sm, sm2id, fn, fn);
     // If there is only one RG tag present in the header and reads are not annotated, don't refuse to work but
     //  use the tag instead.
     else if ( n==1 && first_sm.s )
@@ -91,19 +116,19 @@ int bam_smpl_add(bam_sample_t *sm, const char *fn, const char *txt)
     if ( first_sm.s )
         free(first_sm.s);
 
-//	add_pair(sm, sm2id, fn, fn);
-	free(buf.s);
-	return 0;
+//  add_pair(sm, sm2id, fn, fn);
+    free(buf.s);
+    return 0;
 }
 
 int bam_smpl_rg2smid(const bam_sample_t *sm, const char *fn, const char *rg, kstring_t *str)
 {
-	khint_t k;
-	khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
-	if (rg) {
-		str->l = 0;
-		kputs(fn, str); kputc('/', str); kputs(rg, str);
-		k = kh_get(sm, rg2smid, str->s);
-	} else k = kh_get(sm, rg2smid, fn);
-	return k == kh_end(rg2smid)? -1 : kh_val(rg2smid, k);
+    khint_t k;
+    khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
+    if (rg) {
+        str->l = 0;
+        kputs(fn, str); kputc('/', str); kputs(rg, str);
+        k = kh_get(sm, rg2smid, str->s);
+    } else k = kh_get(sm, rg2smid, fn);
+    return k == kh_end(rg2smid)? -1 : kh_val(rg2smid, k);
 }
diff --git a/samtools/sample.h b/samtools/sample.h
index 85fe499..8e8efa5 100644
--- a/samtools/sample.h
+++ b/samtools/sample.h
@@ -1,12 +1,36 @@
+/*  sample.h -- group data by sample.
+
+    Copyright (C) 2010 Broad Institute.
+
+    Author: Heng Li <lh3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
 #ifndef BAM_SAMPLE_H
 #define BAM_SAMPLE_H
 
-#include "kstring.h"
+#include "htslib/kstring.h"
 
 typedef struct {
-	int n, m;
-	char **smpl;
-	void *rg2smid, *sm2id;
+    int n, m;
+    char **smpl;
+    void *rg2smid, *sm2id;
 } bam_sample_t;
 
 bam_sample_t *bam_smpl_init(void);
diff --git a/htslib/htslib/hts_defs.h b/samtools/samtools.h
similarity index 58%
copy from htslib/htslib/hts_defs.h
copy to samtools/samtools.h
index efc4f6c..3161822 100644
--- a/htslib/htslib/hts_defs.h
+++ b/samtools/samtools.h
@@ -1,8 +1,8 @@
-/*  hts_defs.h -- Miscellaneous definitions.
+/*  samtools.h -- utility routines.
 
     Copyright (C) 2013-2014 Genome Research Ltd.
 
-    Author: John Marshall <jm18 at sanger.ac.uk>
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
@@ -11,7 +11,7 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
-The above copyright notices and this permission notice shall be included in
+The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -22,26 +22,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 DEALINGS IN THE SOFTWARE.  */
 
-#ifndef HTSLIB_HTS_DEFS_H
-#define HTSLIB_HTS_DEFS_H
+#ifndef SAMTOOLS_H
+#define SAMTOOLS_H
 
-#if __clang__major__ >= 2 || __GNUC__ >= 3
-#define HTS_NORETURN __attribute__ ((__noreturn__))
-#else
-#define HTS_NORETURN
-#endif
-
-#if (defined __clang__ && __clang_major__ >= 3) || \
-    (defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__==4 && __GNUC_MINOR__ >= 5)))
-#define HTS_RESULT_USED __attribute__ ((__warn_unused_result__))
-#else
-#define HTS_RESULT_USED
-#endif
+const char *samtools_version(void);
 
-#if defined __clang__ || defined __GNUC__
-#define HTS_UNUSED __attribute__ ((__unused__))
-#else
-#define HTS_UNUSED
-#endif
+void print_error(const char *format, ...);
+void print_error_errno(const char *format, ...);
 
 #endif
diff --git a/samtools/stats.c.pysam.c b/samtools/stats.c.pysam.c
new file mode 100644
index 0000000..507c604
--- /dev/null
+++ b/samtools/stats.c.pysam.c
@@ -0,0 +1,1540 @@
+#include "pysam.h"
+
+/*  stats.c -- This is the former bamcheck integrated into samtools/htslib.
+
+    Copyright (C) 2012-2014 Genome Research Ltd.
+
+    Author: Petr Danecek <pd3 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+/*  Assumptions, approximations and other issues:
+        - GC-depth graph does not split reads, the starting position determines which bin is incremented.
+            There are small overlaps between bins (max readlen-1). However, the bins are big (20k).
+        - coverage distribution ignores softclips and deletions
+        - some stats require sorted BAMs
+        - GC content graph can have an untidy, step-like pattern when BAM contains multiple read lengths.
+        - 'bases mapped' (stats->nbases_mapped) is calculated from read lengths given by BAM (core.l_qseq)
+        - With the -t option, the whole reads are used. Except for the number of mapped bases (cigar)
+            counts, no splicing is done, no indels or soft clips are considered, even small overlap is
+            good enough to include the read in the stats.
+        - GC content of reads not calculated for "=" sequences
+
+*/
+
+#include <unistd.h> // for isatty()
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <math.h>
+#include <ctype.h>
+#include <getopt.h>
+#include <errno.h>
+#include <assert.h>
+#include <zlib.h>   // for crc32
+#include <htslib/faidx.h>
+#include <htslib/sam.h>
+#include <htslib/hts.h>
+#include "sam_header.h"
+#include <htslib/khash_str2int.h>
+#include "samtools.h"
+#include <htslib/khash.h>
+#include "stats_isize.h"
+
+#define BWA_MIN_RDLEN 35
+// From the spec
+// If 0x4 is set, no assumptions can be made about RNAME, POS, CIGAR, MAPQ, bits 0x2, 0x10, 0x100 and 0x800, and the bit 0x20 of the previous read in the template.
+#define IS_PAIRED_AND_MAPPED(bam) (((bam)->core.flag&BAM_FPAIRED) && !((bam)->core.flag&BAM_FUNMAP) && !((bam)->core.flag&BAM_FMUNMAP))
+#define IS_PROPERLYPAIRED(bam) (((bam)->core.flag&(BAM_FPAIRED|BAM_FPROPER_PAIR)) == (BAM_FPAIRED|BAM_FPROPER_PAIR) && !((bam)->core.flag&BAM_FUNMAP))
+#define IS_UNMAPPED(bam) ((bam)->core.flag&BAM_FUNMAP)
+#define IS_REVERSE(bam) ((bam)->core.flag&BAM_FREVERSE)
+#define IS_MATE_REVERSE(bam) ((bam)->core.flag&BAM_FMREVERSE)
+#define IS_READ1(bam) ((bam)->core.flag&BAM_FREAD1)
+#define IS_READ2(bam) ((bam)->core.flag&BAM_FREAD2)
+#define IS_DUP(bam) ((bam)->core.flag&BAM_FDUP)
+
+// The GC-depth graph works as follows: split the reference sequence into
+// segments and calculate GC content and depth in each bin. Then sort
+// these segments by their GC and plot the depth distribution by means
+// of 10th, 25th, etc. depth percentiles.
+typedef struct
+{
+    float gc;
+    uint32_t depth;
+}
+gc_depth_t;
+
+// For coverage distribution, a simple pileup
+typedef struct
+{
+    int64_t pos;
+    int size, start;
+    int *buffer;
+}
+round_buffer_t;
+
+typedef struct { uint32_t from, to; } pos_t;
+typedef struct
+{
+    int npos,mpos,cpos;
+    pos_t *pos;
+}
+regions_t;
+
+typedef struct
+{
+    // Parameters
+    int trim_qual;      // bwa trim quality
+
+    // Dimensions of the quality histogram holder (quals_1st,quals_2nd), GC content holder (gc_1st,gc_2nd),
+    //  insert size histogram holder
+    int nquals;         // The number of quality bins
+    int nbases;         // The maximum sequence length the allocated array can hold
+    int nisize;         // The maximum insert size that the allocated array can hold - 0 indicates no limit
+    int ngc;            // The size of gc_1st and gc_2nd
+    int nindels;        // The maximum indel length for indel distribution
+
+    // Arrays for the histogram data
+    uint64_t *quals_1st, *quals_2nd;
+    uint64_t *gc_1st, *gc_2nd;
+    uint64_t *acgt_cycles;
+    uint64_t *read_lengths;
+    uint64_t *insertions, *deletions;
+    uint64_t *ins_cycles_1st, *ins_cycles_2nd, *del_cycles_1st, *del_cycles_2nd;
+    isize_t *isize;
+
+    // The extremes encountered
+    int max_len;            // Maximum read length
+    int max_qual;           // Maximum quality
+    float isize_main_bulk;  // There are always some unrealistically big insert sizes, report only the main part
+    int is_sorted;
+
+    // Summary numbers
+    uint64_t total_len;
+    uint64_t total_len_dup;
+    uint64_t nreads_1st;
+    uint64_t nreads_2nd;
+    uint64_t nreads_filtered;
+    uint64_t nreads_dup;
+    uint64_t nreads_unmapped;
+    uint64_t nreads_single_mapped;
+    uint64_t nreads_paired_and_mapped;
+    uint64_t nreads_properly_paired;
+    uint64_t nreads_paired_tech;
+    uint64_t nreads_anomalous;
+    uint64_t nreads_mq0;
+    uint64_t nbases_mapped;
+    uint64_t nbases_mapped_cigar;
+    uint64_t nbases_trimmed;  // bwa trimmed bases
+    uint64_t nmismatches;
+    uint64_t nreads_QCfailed, nreads_secondary;
+    struct {
+        uint32_t names, reads, quals;
+    } checksum;
+
+    // GC-depth related data
+    uint32_t ngcd, igcd;        // The maximum number of GC depth bins and index of the current bin
+    gc_depth_t *gcd;            // The GC-depth bins holder
+    int gcd_bin_size;           // The size of GC-depth bin
+    int32_t tid, gcd_pos;       // Position of the current bin
+    int32_t pos;                // Position of the last read
+
+    // Coverage distribution related data
+    int ncov;                       // The number of coverage bins
+    uint64_t *cov;                  // The coverage frequencies
+    int cov_min,cov_max,cov_step;   // Minimum, maximum coverage and size of the coverage bins
+    round_buffer_t cov_rbuf;        // Pileup round buffer
+
+    // Mismatches by read cycle
+    uint8_t *rseq_buf;              // A buffer for reference sequence to check the mismatches against
+    int mrseq_buf;                  // The size of the buffer
+    int32_t rseq_pos;               // The coordinate of the first base in the buffer
+    int32_t nrseq_buf;              // The used part of the buffer
+    uint64_t *mpc_buf;              // Mismatches per cycle
+
+    // Filters
+    int filter_readlen;
+
+    // Target regions
+    int nregions, reg_from,reg_to;
+    regions_t *regions;
+
+    // Auxiliary data
+    int flag_require, flag_filter;
+    double sum_qual;                // For calculating average quality value
+    samFile* sam;
+    bam_hdr_t* sam_header;
+    void *rg_hash;                  // Read groups to include, the array is null-terminated
+    faidx_t *fai;                   // Reference sequence for GC-depth graph
+    int argc;                       // Command line arguments to be printed on the output
+    char **argv;
+}
+stats_t;
+
+static void error(const char *format, ...);
+int is_in_regions(bam1_t *bam_line, stats_t *stats);
+void realloc_buffers(stats_t *stats, int seq_len);
+
+
+// Coverage distribution methods
+static inline int coverage_idx(int min, int max, int n, int step, int depth)
+{
+    if ( depth < min )
+        return 0;
+
+    if ( depth > max )
+        return n-1;
+
+    return 1 + (depth - min) / step;
+}
+
+static inline int round_buffer_lidx2ridx(int offset, int size, int64_t refpos, int64_t pos)
+{
+    return (offset + (pos-refpos) % size) % size;
+}
+
+void round_buffer_flush(stats_t *stats, int64_t pos)
+{
+    int ibuf,idp;
+
+    if ( pos==stats->cov_rbuf.pos )
+        return;
+
+    int64_t new_pos = pos;
+    if ( pos==-1 || pos - stats->cov_rbuf.pos >= stats->cov_rbuf.size )
+    {
+        // Flush the whole buffer, but in sequential order,
+        pos = stats->cov_rbuf.pos + stats->cov_rbuf.size - 1;
+    }
+
+    if ( pos < stats->cov_rbuf.pos )
+        error("Expected coordinates in ascending order, got %ld after %ld\n", pos,stats->cov_rbuf.pos);
+
+    int ifrom = stats->cov_rbuf.start;
+    int ito = round_buffer_lidx2ridx(stats->cov_rbuf.start,stats->cov_rbuf.size,stats->cov_rbuf.pos,pos-1);
+    if ( ifrom>ito )
+    {
+        for (ibuf=ifrom; ibuf<stats->cov_rbuf.size; ibuf++)
+        {
+            if ( !stats->cov_rbuf.buffer[ibuf] )
+                continue;
+            idp = coverage_idx(stats->cov_min,stats->cov_max,stats->ncov,stats->cov_step,stats->cov_rbuf.buffer[ibuf]);
+            stats->cov[idp]++;
+            stats->cov_rbuf.buffer[ibuf] = 0;
+        }
+        ifrom = 0;
+    }
+    for (ibuf=ifrom; ibuf<=ito; ibuf++)
+    {
+        if ( !stats->cov_rbuf.buffer[ibuf] )
+            continue;
+        idp = coverage_idx(stats->cov_min,stats->cov_max,stats->ncov,stats->cov_step,stats->cov_rbuf.buffer[ibuf]);
+        stats->cov[idp]++;
+        stats->cov_rbuf.buffer[ibuf] = 0;
+    }
+    stats->cov_rbuf.start = (new_pos==-1) ? 0 : round_buffer_lidx2ridx(stats->cov_rbuf.start,stats->cov_rbuf.size,stats->cov_rbuf.pos,pos);
+    stats->cov_rbuf.pos   = new_pos;
+}
+
+void round_buffer_insert_read(round_buffer_t *rbuf, int64_t from, int64_t to)
+{
+    if ( to-from >= rbuf->size )
+        error("The read length too big (%d), please increase the buffer length (currently %d)\n", to-from+1,rbuf->size);
+    if ( from < rbuf->pos )
+        error("The reads are not sorted (%ld comes after %ld).\n", from,rbuf->pos);
+
+    int ifrom,ito,ibuf;
+    ifrom = round_buffer_lidx2ridx(rbuf->start,rbuf->size,rbuf->pos,from);
+    ito   = round_buffer_lidx2ridx(rbuf->start,rbuf->size,rbuf->pos,to);
+    if ( ifrom>ito )
+    {
+        for (ibuf=ifrom; ibuf<rbuf->size; ibuf++)
+            rbuf->buffer[ibuf]++;
+        ifrom = 0;
+    }
+    for (ibuf=ifrom; ibuf<=ito; ibuf++)
+        rbuf->buffer[ibuf]++;
+}
+
+// Calculate the number of bases in the read trimmed by BWA
+int bwa_trim_read(int trim_qual, uint8_t *quals, int len, int reverse)
+{
+    if ( len<BWA_MIN_RDLEN ) return 0;
+
+    // Although the name implies that the read cannot be trimmed to more than BWA_MIN_RDLEN,
+    //  the calculation can in fact trim it to (BWA_MIN_RDLEN-1). (bwa_trim_read in bwa/bwaseqio.c).
+    int max_trimmed = len - BWA_MIN_RDLEN + 1;
+    int l, sum=0, max_sum=0, max_l=0;
+
+    for (l=0; l<max_trimmed; l++)
+    {
+        sum += trim_qual - quals[ reverse ? l : len-1-l ];
+        if ( sum<0 ) break;
+        if ( sum>max_sum )
+        {
+            max_sum = sum;
+            // This is the correct way, but bwa clips from some reason one base less
+            // max_l   = l+1;
+            max_l   = l;
+        }
+    }
+    return max_l;
+}
+
+
+void count_indels(stats_t *stats,bam1_t *bam_line)
+{
+    int is_fwd = IS_REVERSE(bam_line) ? 0 : 1;
+    int is_1st = IS_READ1(bam_line) ? 1 : 0;
+    int icig;
+    int icycle = 0;
+    int read_len = bam_line->core.l_qseq;
+    for (icig=0; icig<bam_line->core.n_cigar; icig++)
+    {
+        int cig  = bam_cigar_op(bam_get_cigar(bam_line)[icig]);
+        int ncig = bam_cigar_oplen(bam_get_cigar(bam_line)[icig]);
+        if ( !ncig ) continue;  // curiously, this can happen: 0D
+
+        if ( cig==BAM_CINS )
+        {
+            int idx = is_fwd ? icycle : read_len-icycle-ncig;
+            if ( idx<0 )
+                error("FIXME: read_len=%d vs icycle=%d\n", read_len,icycle);
+            if ( idx >= stats->nbases || idx<0 ) error("FIXME: %d vs %d, %s:%d %s\n", idx,stats->nbases, stats->sam_header->target_name[bam_line->core.tid],bam_line->core.pos+1,bam_get_qname(bam_line));
+            if ( is_1st )
+                stats->ins_cycles_1st[idx]++;
+            else
+                stats->ins_cycles_2nd[idx]++;
+            icycle += ncig;
+            if ( ncig<=stats->nindels )
+                stats->insertions[ncig-1]++;
+            continue;
+        }
+        if ( cig==BAM_CDEL )
+        {
+            int idx = is_fwd ? icycle-1 : read_len-icycle-1;
+            if ( idx<0 ) continue;  // discard meaningless deletions
+            if ( idx >= stats->nbases ) error("FIXME: %d vs %d\n", idx,stats->nbases);
+            if ( is_1st )
+                stats->del_cycles_1st[idx]++;
+            else
+                stats->del_cycles_2nd[idx]++;
+            if ( ncig<=stats->nindels )
+                stats->deletions[ncig-1]++;
+            continue;
+        }
+        if ( cig!=BAM_CREF_SKIP && cig!=BAM_CHARD_CLIP && cig!=BAM_CPAD )
+            icycle += ncig;
+    }
+}
+
+int unclipped_length(bam1_t *bam_line)
+{
+    int icig, read_len = bam_line->core.l_qseq;
+    for (icig=0; icig<bam_line->core.n_cigar; icig++)
+    {
+        int cig = bam_cigar_op(bam_get_cigar(bam_line)[icig]);
+        if ( cig==BAM_CHARD_CLIP )
+            read_len += bam_cigar_oplen(bam_get_cigar(bam_line)[icig]);
+    }
+    return read_len;
+}
+
+void count_mismatches_per_cycle(stats_t *stats,bam1_t *bam_line)
+{
+    int read_len = unclipped_length(bam_line);
+    if ( read_len >= stats->nbases ) realloc_buffers(stats,read_len);
+    int is_fwd = IS_REVERSE(bam_line) ? 0 : 1;
+    int icig,iread=0,icycle=0;
+    int iref = bam_line->core.pos - stats->rseq_pos;
+    uint8_t *read  = bam_get_seq(bam_line);
+    uint8_t *quals = bam_get_qual(bam_line);
+    uint64_t *mpc_buf = stats->mpc_buf;
+    for (icig=0; icig<bam_line->core.n_cigar; icig++)
+    {
+        int cig  = bam_cigar_op(bam_get_cigar(bam_line)[icig]);
+        int ncig = bam_cigar_oplen(bam_get_cigar(bam_line)[icig]);
+        if ( cig==BAM_CINS )
+        {
+            iread  += ncig;
+            icycle += ncig;
+            continue;
+        }
+        if ( cig==BAM_CDEL )
+        {
+            iref += ncig;
+            continue;
+        }
+        if ( cig==BAM_CSOFT_CLIP )
+        {
+            icycle += ncig;
+            // Soft-clips are present in the sequence, but the position of the read marks a start of the sequence after clipping
+            //   iref += ncig;
+            iread  += ncig;
+            continue;
+        }
+        if ( cig==BAM_CHARD_CLIP )
+        {
+            icycle += ncig;
+            continue;
+        }
+        // Ignore H and N CIGARs. The letter are inserted e.g. by TopHat and often require very large
+        //  chunk of refseq in memory. Not very frequent and not noticable in the stats.
+        if ( cig==BAM_CREF_SKIP || cig==BAM_CHARD_CLIP || cig==BAM_CPAD ) continue;
+        if ( cig!=BAM_CMATCH && cig!=BAM_CEQUAL && cig!=BAM_CDIFF ) // not relying on precalculated diffs
+            error("TODO: cigar %d, %s:%d %s\n", cig,stats->sam_header->target_name[bam_line->core.tid],bam_line->core.pos+1,bam_get_qname(bam_line));
+
+        if ( ncig+iref > stats->nrseq_buf )
+            error("FIXME: %d+%d > %d, %s, %s:%d\n",ncig,iref,stats->nrseq_buf, bam_get_qname(bam_line),stats->sam_header->target_name[bam_line->core.tid],bam_line->core.pos+1);
+
+        int im;
+        for (im=0; im<ncig; im++)
+        {
+            uint8_t cread = bam_seqi(read,iread);
+            uint8_t cref  = stats->rseq_buf[iref];
+
+            // ---------------15
+            // =ACMGRSVTWYHKDBN
+            if ( cread==15 )
+            {
+                int idx = is_fwd ? icycle : read_len-icycle-1;
+                if ( idx>stats->max_len )
+                    error("mpc: %d>%d\n",idx,stats->max_len);
+                idx = idx*stats->nquals;
+                if ( idx>=stats->nquals*stats->nbases )
+                    error("FIXME: mpc_buf overflow\n");
+                mpc_buf[idx]++;
+            }
+            else if ( cref && cread && cref!=cread )
+            {
+                uint8_t qual = quals[iread] + 1;
+                if ( qual>=stats->nquals )
+                    error("TODO: quality too high %d>=%d (%s %d %s)\n", qual,stats->nquals, stats->sam_header->target_name[bam_line->core.tid],bam_line->core.pos+1,bam_get_qname(bam_line));
+
+                int idx = is_fwd ? icycle : read_len-icycle-1;
+                if ( idx>stats->max_len )
+                    error("mpc: %d>%d\n",idx,stats->max_len);
+
+                idx = idx*stats->nquals + qual;
+                if ( idx>=stats->nquals*stats->nbases )
+                    error("FIXME: mpc_buf overflow\n");
+                mpc_buf[idx]++;
+            }
+
+            iref++;
+            iread++;
+            icycle++;
+        }
+    }
+}
+
+void read_ref_seq(stats_t *stats, int32_t tid, int32_t pos)
+{
+    int i, fai_ref_len;
+    char *fai_ref = faidx_fetch_seq(stats->fai, stats->sam_header->target_name[tid], pos, pos+stats->mrseq_buf-1, &fai_ref_len);
+    if ( fai_ref_len<0 ) error("Failed to fetch the sequence \"%s\"\n", stats->sam_header->target_name[tid]);
+
+    uint8_t *ptr = stats->rseq_buf;
+    for (i=0; i<fai_ref_len; i++)
+    {
+        // Conversion between uint8_t coding and ACGT
+        //      -12-4---8-------
+        //      =ACMGRSVTWYHKDBN
+        switch (fai_ref[i])
+        {
+            case 'A':
+            case 'a': *ptr = 1; break;
+            case 'C':
+            case 'c': *ptr = 2; break;
+            case 'G':
+            case 'g': *ptr = 4; break;
+            case 'T':
+            case 't': *ptr = 8; break;
+            default:  *ptr = 0; break;
+        }
+        ptr++;
+    }
+    free(fai_ref);
+
+    if ( fai_ref_len < stats->mrseq_buf ) memset(ptr,0, stats->mrseq_buf - fai_ref_len);
+    stats->nrseq_buf = fai_ref_len;
+    stats->rseq_pos  = pos;
+    stats->tid       = tid;
+}
+
+float fai_gc_content(stats_t *stats, int pos, int len)
+{
+    uint32_t gc,count,c;
+    int i = pos - stats->rseq_pos, ito = i + len;
+    assert( i>=0 );
+
+    if (  ito > stats->nrseq_buf ) ito = stats->nrseq_buf;
+
+    // Count GC content
+    gc = count = 0;
+    for (; i<ito; i++)
+    {
+        c = stats->rseq_buf[i];
+        if ( c==2 || c==4 )
+        {
+            gc++;
+            count++;
+        }
+        else if ( c==1 || c==8 )
+            count++;
+    }
+    return count ? (float)gc/count : 0;
+}
+
+void realloc_rseq_buffer(stats_t *stats)
+{
+    int n = stats->nbases*10;
+    if ( stats->gcd_bin_size > n ) n = stats->gcd_bin_size;
+    if ( stats->mrseq_buf<n )
+    {
+        stats->rseq_buf = realloc(stats->rseq_buf,sizeof(uint8_t)*n);
+        stats->mrseq_buf = n;
+    }
+}
+
+void realloc_gcd_buffer(stats_t *stats, int seq_len)
+{
+    hts_expand0(gc_depth_t,stats->igcd+1,stats->ngcd,stats->gcd);
+    realloc_rseq_buffer(stats);
+}
+
+void realloc_buffers(stats_t *stats, int seq_len)
+{
+    int n = 2*(1 + seq_len - stats->nbases) + stats->nbases;
+
+    stats->quals_1st = realloc(stats->quals_1st, n*stats->nquals*sizeof(uint64_t));
+    if ( !stats->quals_1st )
+        error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*stats->nquals*sizeof(uint64_t));
+    memset(stats->quals_1st + stats->nbases*stats->nquals, 0, (n-stats->nbases)*stats->nquals*sizeof(uint64_t));
+
+    stats->quals_2nd = realloc(stats->quals_2nd, n*stats->nquals*sizeof(uint64_t));
+    if ( !stats->quals_2nd )
+        error("Could not realloc buffers, the sequence too long: %d (2x%ld)\n", seq_len,n*stats->nquals*sizeof(uint64_t));
+    memset(stats->quals_2nd + stats->nbases*stats->nquals, 0, (n-stats->nbases)*stats->nquals*sizeof(uint64_t));
+
+    if ( stats->mpc_buf )
+    {
+        stats->mpc_buf = realloc(stats->mpc_buf, n*stats->nquals*sizeof(uint64_t));
+        if ( !stats->mpc_buf )
+            error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*stats->nquals*sizeof(uint64_t));
+        memset(stats->mpc_buf + stats->nbases*stats->nquals, 0, (n-stats->nbases)*stats->nquals*sizeof(uint64_t));
+    }
+
+    stats->acgt_cycles = realloc(stats->acgt_cycles, n*4*sizeof(uint64_t));
+    if ( !stats->acgt_cycles )
+        error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*4*sizeof(uint64_t));
+    memset(stats->acgt_cycles + stats->nbases*4, 0, (n-stats->nbases)*4*sizeof(uint64_t));
+
+    stats->read_lengths = realloc(stats->read_lengths, n*sizeof(uint64_t));
+    if ( !stats->read_lengths )
+        error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*sizeof(uint64_t));
+    memset(stats->read_lengths + stats->nbases, 0, (n-stats->nbases)*sizeof(uint64_t));
+
+    stats->insertions = realloc(stats->insertions, n*sizeof(uint64_t));
+    if ( !stats->insertions )
+        error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*sizeof(uint64_t));
+    memset(stats->insertions + stats->nbases, 0, (n-stats->nbases)*sizeof(uint64_t));
+
+    stats->deletions = realloc(stats->deletions, n*sizeof(uint64_t));
+    if ( !stats->deletions )
+        error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*sizeof(uint64_t));
+    memset(stats->deletions + stats->nbases, 0, (n-stats->nbases)*sizeof(uint64_t));
+
+    stats->ins_cycles_1st = realloc(stats->ins_cycles_1st, (n+1)*sizeof(uint64_t));
+    if ( !stats->ins_cycles_1st )
+        error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,(n+1)*sizeof(uint64_t));
+    memset(stats->ins_cycles_1st + stats->nbases + 1, 0, (n-stats->nbases)*sizeof(uint64_t));
+
+    stats->ins_cycles_2nd = realloc(stats->ins_cycles_2nd, (n+1)*sizeof(uint64_t));
+    if ( !stats->ins_cycles_2nd )
+        error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,(n+1)*sizeof(uint64_t));
+    memset(stats->ins_cycles_2nd + stats->nbases + 1, 0, (n-stats->nbases)*sizeof(uint64_t));
+
+    stats->del_cycles_1st = realloc(stats->del_cycles_1st, (n+1)*sizeof(uint64_t));
+    if ( !stats->del_cycles_1st )
+        error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,(n+1)*sizeof(uint64_t));
+    memset(stats->del_cycles_1st + stats->nbases + 1, 0, (n-stats->nbases)*sizeof(uint64_t));
+
+    stats->del_cycles_2nd = realloc(stats->del_cycles_2nd, (n+1)*sizeof(uint64_t));
+    if ( !stats->del_cycles_2nd )
+        error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,(n+1)*sizeof(uint64_t));
+    memset(stats->del_cycles_2nd + stats->nbases + 1, 0, (n-stats->nbases)*sizeof(uint64_t));
+
+    stats->nbases = n;
+
+    // Realloc the coverage distribution buffer
+    int *rbuffer = calloc(sizeof(int),seq_len*5);
+    n = stats->cov_rbuf.size-stats->cov_rbuf.start;
+    memcpy(rbuffer,stats->cov_rbuf.buffer+stats->cov_rbuf.start,n);
+    if ( stats->cov_rbuf.start>1 )
+        memcpy(rbuffer+n,stats->cov_rbuf.buffer,stats->cov_rbuf.start);
+    stats->cov_rbuf.start = 0;
+    free(stats->cov_rbuf.buffer);
+    stats->cov_rbuf.buffer = rbuffer;
+    stats->cov_rbuf.size = seq_len*5;
+
+    realloc_rseq_buffer(stats);
+}
+
+void update_checksum(bam1_t *bam_line, stats_t *stats)
+{
+    uint8_t *name = (uint8_t*) bam_get_qname(bam_line);
+    int len = 0;
+    while ( name[len] ) len++;
+    stats->checksum.names +=  crc32(0L, name, len);
+
+    int seq_len = bam_line->core.l_qseq;
+    if ( !seq_len ) return;
+
+    uint8_t *seq = bam_get_seq(bam_line);
+    stats->checksum.reads += crc32(0L, seq, (seq_len+1)/2);
+
+    uint8_t *qual = bam_get_qual(bam_line);
+    stats->checksum.quals += crc32(0L, qual, (seq_len+1)/2);
+}
+
+void collect_stats(bam1_t *bam_line, stats_t *stats)
+{
+    if ( stats->rg_hash )
+    {
+        const uint8_t *rg = bam_aux_get(bam_line, "RG");
+        if ( !rg ) return;  // certain read groups were requested but this record has none
+        if ( !khash_str2int_has_key(stats->rg_hash, (const char*)(rg + 1)) ) return;
+    }
+    if ( stats->flag_require && (bam_line->core.flag & stats->flag_require)!=stats->flag_require )
+    {
+        stats->nreads_filtered++;
+        return;
+    }
+    if ( stats->flag_filter && (bam_line->core.flag & stats->flag_filter) )
+    {
+        stats->nreads_filtered++;
+        return;
+    }
+    if ( !is_in_regions(bam_line,stats) )
+        return;
+    if ( stats->filter_readlen!=-1 && bam_line->core.l_qseq!=stats->filter_readlen )
+        return;
+
+    if ( bam_line->core.flag & BAM_FQCFAIL ) stats->nreads_QCfailed++;
+    if ( bam_line->core.flag & BAM_FSECONDARY ) stats->nreads_secondary++;
+    if ( bam_line->core.flag & BAM_FPAIRED ) stats->nreads_paired_tech++;
+
+    update_checksum(bam_line, stats);
+
+    int seq_len = bam_line->core.l_qseq;
+    if ( !seq_len ) return;
+
+    if ( seq_len >= stats->nbases )
+        realloc_buffers(stats,seq_len);
+    if ( stats->max_len<seq_len )
+        stats->max_len = seq_len;
+
+    stats->read_lengths[seq_len]++;
+
+    // Count GC and ACGT per cycle. Note that cycle is approximate, clipping is ignored
+    uint8_t base, *seq  = bam_get_seq(bam_line);
+    int gc_count  = 0;
+    int i;
+    int reverse = IS_REVERSE(bam_line);
+    for (i=0; i<seq_len; i++)
+    {
+        // Conversion from uint8_t coding to ACGT
+        //      -12-4---8------5
+        //      =ACMGRSVTWYHKDBN
+        //       01 2   3
+        base = bam_seqi(seq,i);
+        if ( base==0 ) break;   // not ready for "=" sequences
+        base /= 2;
+        if ( base==1 || base==2 ) gc_count++;
+        else if ( base>2 ) base=3;
+        if ( 4*(reverse ? seq_len-i-1 : i) + base >= stats->nbases*4 )
+            error("FIXME: acgt_cycles\n");
+        stats->acgt_cycles[ 4*(reverse ? seq_len-i-1 : i) + base ]++;
+    }
+    int gc_idx_min = gc_count*(stats->ngc-1)/seq_len;
+    int gc_idx_max = (gc_count+1)*(stats->ngc-1)/seq_len;
+    if ( gc_idx_max >= stats->ngc ) gc_idx_max = stats->ngc - 1;
+
+    // Determine which array (1st or 2nd read) will these stats go to,
+    //  trim low quality bases from end the same way BWA does,
+    //  fill GC histogram
+    uint64_t *quals;
+    uint8_t *bam_quals = bam_get_qual(bam_line);
+    if ( bam_line->core.flag&BAM_FREAD2 )
+    {
+        quals  = stats->quals_2nd;
+        stats->nreads_2nd++;
+        for (i=gc_idx_min; i<gc_idx_max; i++)
+            stats->gc_2nd[i]++;
+    }
+    else
+    {
+        quals = stats->quals_1st;
+        stats->nreads_1st++;
+        for (i=gc_idx_min; i<gc_idx_max; i++)
+            stats->gc_1st[i]++;
+    }
+    if ( stats->trim_qual>0 )
+        stats->nbases_trimmed += bwa_trim_read(stats->trim_qual, bam_quals, seq_len, reverse);
+
+    // Quality histogram and average quality. Clipping is neglected.
+    for (i=0; i<seq_len; i++)
+    {
+        uint8_t qual = bam_quals[ reverse ? seq_len-i-1 : i];
+        if ( qual>=stats->nquals )
+            error("TODO: quality too high %d>=%d (%s %d %s)\n", qual,stats->nquals,stats->sam_header->target_name[bam_line->core.tid],bam_line->core.pos+1,bam_get_qname(bam_line));
+        if ( qual>stats->max_qual )
+            stats->max_qual = qual;
+
+        quals[ i*stats->nquals+qual ]++;
+        stats->sum_qual += qual;
+    }
+
+    // Look at the flags and increment appropriate counters (mapped, paired, etc)
+    if ( IS_UNMAPPED(bam_line) )
+        stats->nreads_unmapped++;
+    else
+    {
+        if ( !bam_line->core.qual )
+            stats->nreads_mq0++;
+
+        count_indels(stats,bam_line);
+
+        if ( !IS_PAIRED_AND_MAPPED(bam_line) )
+            stats->nreads_single_mapped++;
+        else
+        {
+            stats->nreads_paired_and_mapped++;
+
+            if (IS_PROPERLYPAIRED(bam_line)) stats->nreads_properly_paired++;
+
+            if ( bam_line->core.tid!=bam_line->core.mtid )
+                stats->nreads_anomalous++;
+
+            // The insert size is tricky, because for long inserts the libraries are
+            // prepared differently and the pairs point in other direction. BWA does
+            // not set the paired flag for them.  Similar thing is true also for 454
+            // reads. Mates mapped to different chromosomes have isize==0.
+            int32_t isize = bam_line->core.isize;
+            if ( isize<0 ) isize = -isize;
+            if ( stats->nisize > 0 && isize >= stats->nisize )
+                isize = stats->nisize-1;
+            if ( isize>0 || bam_line->core.tid==bam_line->core.mtid )
+            {
+                int pos_fst = bam_line->core.mpos - bam_line->core.pos;
+                int is_fst  = IS_READ1(bam_line) ? 1 : -1;
+                int is_fwd  = IS_REVERSE(bam_line) ? -1 : 1;
+                int is_mfwd = IS_MATE_REVERSE(bam_line) ? -1 : 1;
+
+                if ( is_fwd*is_mfwd>0 )
+                    stats->isize->inc_other(stats->isize->data, isize);
+                else if ( is_fst*pos_fst>0 )
+                {
+                    if ( is_fst*is_fwd>0 )
+                        stats->isize->inc_inward(stats->isize->data, isize);
+                    else
+                        stats->isize->inc_outward(stats->isize->data, isize);
+                }
+                else if ( is_fst*pos_fst<0 )
+                {
+                    if ( is_fst*is_fwd>0 )
+                        stats->isize->inc_outward(stats->isize->data, isize);
+                    else
+                        stats->isize->inc_inward(stats->isize->data, isize);
+                }
+            }
+        }
+
+        // Number of mismatches
+        uint8_t *nm = bam_aux_get(bam_line,"NM");
+        if (nm)
+            stats->nmismatches += bam_aux2i(nm);
+
+        // Number of mapped bases from cigar
+        if ( bam_line->core.n_cigar == 0)
+            error("FIXME: mapped read with no cigar?\n");
+        int readlen=seq_len;
+        if ( stats->regions )
+        {
+            // Count only on-target bases
+            int iref = bam_line->core.pos + 1;
+            for (i=0; i<bam_line->core.n_cigar; i++)
+            {
+                int cig  = bam_cigar_op(bam_get_cigar(bam_line)[i]);
+                int ncig = bam_cigar_oplen(bam_get_cigar(bam_line)[i]);
+                if ( !ncig ) continue;  // curiously, this can happen: 0D
+                if ( cig==BAM_CDEL ) readlen += ncig;
+                else if ( cig==BAM_CMATCH )
+                {
+                    if ( iref < stats->reg_from ) ncig -= stats->reg_from-iref;
+                    else if ( iref+ncig-1 > stats->reg_to ) ncig -= iref+ncig-1 - stats->reg_to;
+                    if ( ncig<0 ) ncig = 0;
+                    stats->nbases_mapped_cigar += ncig;
+                    iref += bam_cigar_oplen(bam_get_cigar(bam_line)[i]);
+                }
+                else if ( cig==BAM_CINS )
+                {
+                    iref += ncig;
+                    if ( iref>=stats->reg_from && iref<=stats->reg_to )
+                        stats->nbases_mapped_cigar += ncig;
+                }
+            }
+        }
+        else
+        {
+            // Count the whole read
+            for (i=0; i<bam_line->core.n_cigar; i++)
+            {
+                if ( bam_cigar_op(bam_get_cigar(bam_line)[i])==BAM_CMATCH || bam_cigar_op(bam_get_cigar(bam_line)[i])==BAM_CINS )
+                    stats->nbases_mapped_cigar += bam_cigar_oplen(bam_get_cigar(bam_line)[i]);
+                if ( bam_cigar_op(bam_get_cigar(bam_line)[i])==BAM_CDEL )
+                    readlen += bam_cigar_oplen(bam_get_cigar(bam_line)[i]);
+            }
+        }
+        stats->nbases_mapped += seq_len;
+
+        if ( stats->tid==bam_line->core.tid && bam_line->core.pos<stats->pos )
+            stats->is_sorted = 0;
+        stats->pos = bam_line->core.pos;
+
+        if ( stats->is_sorted )
+        {
+            if ( stats->tid==-1 || stats->tid!=bam_line->core.tid )
+                round_buffer_flush(stats,-1);
+
+            // Mismatches per cycle and GC-depth graph. For simplicity, reads overlapping GCD bins
+            //  are not splitted which results in up to seq_len-1 overlaps. The default bin size is
+            //  20kbp, so the effect is negligible.
+            if ( stats->fai )
+            {
+                int inc_ref = 0, inc_gcd = 0;
+                // First pass or new chromosome
+                if ( stats->rseq_pos==-1 || stats->tid != bam_line->core.tid ) { inc_ref=1; inc_gcd=1; }
+                // Read goes beyond the end of the rseq buffer
+                else if ( stats->rseq_pos+stats->nrseq_buf < bam_line->core.pos+readlen ) { inc_ref=1; inc_gcd=1; }
+                // Read overlaps the next gcd bin
+                else if ( stats->gcd_pos+stats->gcd_bin_size < bam_line->core.pos+readlen )
+                {
+                    inc_gcd = 1;
+                    if ( stats->rseq_pos+stats->nrseq_buf < bam_line->core.pos+stats->gcd_bin_size ) inc_ref = 1;
+                }
+                if ( inc_gcd )
+                {
+                    stats->igcd++;
+                    if ( stats->igcd >= stats->ngcd )
+                        realloc_gcd_buffer(stats, readlen);
+                    if ( inc_ref )
+                        read_ref_seq(stats,bam_line->core.tid,bam_line->core.pos);
+                    stats->gcd_pos = bam_line->core.pos;
+                    stats->gcd[ stats->igcd ].gc = fai_gc_content(stats, stats->gcd_pos, stats->gcd_bin_size);
+                }
+
+                count_mismatches_per_cycle(stats,bam_line);
+            }
+            // No reference and first pass, new chromosome or sequence going beyond the end of the gcd bin
+            else if ( stats->gcd_pos==-1 || stats->tid != bam_line->core.tid || bam_line->core.pos - stats->gcd_pos > stats->gcd_bin_size )
+            {
+                // First pass or a new chromosome
+                stats->tid     = bam_line->core.tid;
+                stats->gcd_pos = bam_line->core.pos;
+                stats->igcd++;
+                if ( stats->igcd >= stats->ngcd )
+                    realloc_gcd_buffer(stats, readlen);
+            }
+            stats->gcd[ stats->igcd ].depth++;
+            // When no reference sequence is given, approximate the GC from the read (much shorter window, but otherwise OK)
+            if ( !stats->fai )
+                stats->gcd[ stats->igcd ].gc += (float) gc_count / seq_len;
+
+            // Coverage distribution graph
+            round_buffer_flush(stats,bam_line->core.pos);
+            round_buffer_insert_read(&(stats->cov_rbuf),bam_line->core.pos,bam_line->core.pos+seq_len-1);
+        }
+    }
+
+    stats->total_len += seq_len;
+    if ( IS_DUP(bam_line) )
+    {
+        stats->total_len_dup += seq_len;
+        stats->nreads_dup++;
+    }
+}
+
+// Sort by GC and depth
+#define GCD_t(x) ((gc_depth_t *)x)
+static int gcd_cmp(const void *a, const void *b)
+{
+    if ( GCD_t(a)->gc < GCD_t(b)->gc ) return -1;
+    if ( GCD_t(a)->gc > GCD_t(b)->gc ) return 1;
+    if ( GCD_t(a)->depth < GCD_t(b)->depth ) return -1;
+    if ( GCD_t(a)->depth > GCD_t(b)->depth ) return 1;
+    return 0;
+}
+#undef GCD_t
+
+float gcd_percentile(gc_depth_t *gcd, int N, int p)
+{
+    float n,d;
+    int k;
+
+    n = p*(N+1)/100;
+    k = n;
+    if ( k<=0 )
+        return gcd[0].depth;
+    if ( k>=N )
+        return gcd[N-1].depth;
+
+    d = n - k;
+    return gcd[k-1].depth + d*(gcd[k].depth - gcd[k-1].depth);
+}
+
+void output_stats(stats_t *stats, int sparse)
+{
+    // Calculate average insert size and standard deviation (from the main bulk data only)
+    int isize, ibulk=0;
+    uint64_t nisize=0, nisize_inward=0, nisize_outward=0, nisize_other=0;
+    for (isize=0; isize<stats->isize->nitems(stats->isize->data); isize++)
+    {
+        // Each pair was counted twice
+        stats->isize->set_inward(stats->isize->data, isize, stats->isize->inward(stats->isize->data, isize) * 0.5);
+        stats->isize->set_outward(stats->isize->data, isize, stats->isize->outward(stats->isize->data, isize) * 0.5);
+        stats->isize->set_other(stats->isize->data, isize, stats->isize->other(stats->isize->data, isize) * 0.5);
+
+        nisize_inward += stats->isize->inward(stats->isize->data, isize);
+        nisize_outward += stats->isize->outward(stats->isize->data, isize);
+        nisize_other += stats->isize->other(stats->isize->data, isize);
+        nisize += stats->isize->inward(stats->isize->data, isize) + stats->isize->outward(stats->isize->data, isize) + stats->isize->other(stats->isize->data, isize);
+    }
+
+    double bulk=0, avg_isize=0, sd_isize=0;
+    for (isize=0; isize<stats->isize->nitems(stats->isize->data); isize++)
+    {
+        bulk += stats->isize->inward(stats->isize->data, isize) +  stats->isize->outward(stats->isize->data, isize) + stats->isize->other(stats->isize->data, isize);
+        avg_isize += isize * (stats->isize->inward(stats->isize->data, isize) +  stats->isize->outward(stats->isize->data, isize) + stats->isize->other(stats->isize->data, isize));
+
+        if ( bulk/nisize > stats->isize_main_bulk )
+        {
+            ibulk  = isize+1;
+            nisize = bulk;
+            break;
+        }
+    }
+    avg_isize /= nisize ? nisize : 1;
+    for (isize=1; isize<ibulk; isize++)
+        sd_isize += (stats->isize->inward(stats->isize->data, isize) + stats->isize->outward(stats->isize->data, isize) +stats->isize->other(stats->isize->data, isize)) * (isize-avg_isize)*(isize-avg_isize) / nisize;
+    sd_isize = sqrt(sd_isize);
+
+
+    printf("# This file was produced by samtools stats (%s+htslib-%s) and can be plotted using plot-bamstats\n", samtools_version(), hts_version());
+    printf("# The command line was:  %s",stats->argv[0]);
+    int i;
+    for (i=1; i<stats->argc; i++)
+        printf(" %s",stats->argv[i]);
+    printf("\n");
+    printf("# CHK, Checksum\t[2]Read Names\t[3]Sequences\t[4]Qualities\n");
+    printf("# CHK, CRC32 of reads which passed filtering followed by addition (32bit overflow)\n");
+    printf("CHK\t%08x\t%08x\t%08x\n", stats->checksum.names,stats->checksum.reads,stats->checksum.quals);
+    printf("# Summary Numbers. Use `grep ^SN | cut -f 2-` to extract this part.\n");
+    printf("SN\traw total sequences:\t%ld\n", (long)(stats->nreads_filtered+stats->nreads_1st+stats->nreads_2nd));  // not counting excluded seqs (and none of the below)
+    printf("SN\tfiltered sequences:\t%ld\n", (long)stats->nreads_filtered);
+    printf("SN\tsequences:\t%ld\n", (long)(stats->nreads_1st+stats->nreads_2nd));
+    printf("SN\tis sorted:\t%d\n", stats->is_sorted ? 1 : 0);
+    printf("SN\t1st fragments:\t%ld\n", (long)stats->nreads_1st);
+    printf("SN\tlast fragments:\t%ld\n", (long)stats->nreads_2nd);
+    printf("SN\treads mapped:\t%ld\n", (long)(stats->nreads_paired_and_mapped+stats->nreads_single_mapped));
+    printf("SN\treads mapped and paired:\t%ld\t# paired-end technology bit set + both mates mapped\n", (long)stats->nreads_paired_and_mapped);
+    printf("SN\treads unmapped:\t%ld\n", (long)stats->nreads_unmapped);
+    printf("SN\treads properly paired:\t%ld\t# proper-pair bit set\n", (long)stats->nreads_properly_paired);
+    printf("SN\treads paired:\t%ld\t# paired-end technology bit set\n", (long)stats->nreads_paired_tech);
+    printf("SN\treads duplicated:\t%ld\t# PCR or optical duplicate bit set\n", (long)stats->nreads_dup);
+    printf("SN\treads MQ0:\t%ld\t# mapped and MQ=0\n", (long)stats->nreads_mq0);
+    printf("SN\treads QC failed:\t%ld\n", (long)stats->nreads_QCfailed);
+    printf("SN\tnon-primary alignments:\t%ld\n", (long)stats->nreads_secondary);
+    printf("SN\ttotal length:\t%ld\t# ignores clipping\n", (long)stats->total_len);
+    printf("SN\tbases mapped:\t%ld\t# ignores clipping\n", (long)stats->nbases_mapped);                 // the length of the whole read goes here, including soft-clips etc.
+    printf("SN\tbases mapped (cigar):\t%ld\t# more accurate\n", (long)stats->nbases_mapped_cigar);   // only matched and inserted bases are counted here
+    printf("SN\tbases trimmed:\t%ld\n", (long)stats->nbases_trimmed);
+    printf("SN\tbases duplicated:\t%ld\n", (long)stats->total_len_dup);
+    printf("SN\tmismatches:\t%ld\t# from NM fields\n", (long)stats->nmismatches);
+    printf("SN\terror rate:\t%e\t# mismatches / bases mapped (cigar)\n", stats->nbases_mapped_cigar ? (float)stats->nmismatches/stats->nbases_mapped_cigar : 0);
+    float avg_read_length = (stats->nreads_1st+stats->nreads_2nd)?stats->total_len/(stats->nreads_1st+stats->nreads_2nd):0;
+    printf("SN\taverage length:\t%.0f\n", avg_read_length);
+    printf("SN\tmaximum length:\t%d\n", stats->max_len);
+    printf("SN\taverage quality:\t%.1f\n", stats->total_len?stats->sum_qual/stats->total_len:0);
+    printf("SN\tinsert size average:\t%.1f\n", avg_isize);
+    printf("SN\tinsert size standard deviation:\t%.1f\n", sd_isize);
+    printf("SN\tinward oriented pairs:\t%ld\n", (long)nisize_inward);
+    printf("SN\toutward oriented pairs:\t%ld\n", (long)nisize_outward);
+    printf("SN\tpairs with other orientation:\t%ld\n", (long)nisize_other);
+    printf("SN\tpairs on different chromosomes:\t%ld\n", (long)stats->nreads_anomalous/2);
+
+    int ibase,iqual;
+    if ( stats->max_len<stats->nbases ) stats->max_len++;
+    if ( stats->max_qual+1<stats->nquals ) stats->max_qual++;
+    printf("# First Fragment Qualitites. Use `grep ^FFQ | cut -f 2-` to extract this part.\n");
+    printf("# Columns correspond to qualities and rows to cycles. First column is the cycle number.\n");
+    for (ibase=0; ibase<stats->max_len; ibase++)
+    {
+        printf("FFQ\t%d",ibase+1);
+        for (iqual=0; iqual<=stats->max_qual; iqual++)
+        {
+            printf("\t%ld", (long)stats->quals_1st[ibase*stats->nquals+iqual]);
+        }
+        printf("\n");
+    }
+    printf("# Last Fragment Qualitites. Use `grep ^LFQ | cut -f 2-` to extract this part.\n");
+    printf("# Columns correspond to qualities and rows to cycles. First column is the cycle number.\n");
+    for (ibase=0; ibase<stats->max_len; ibase++)
+    {
+        printf("LFQ\t%d",ibase+1);
+        for (iqual=0; iqual<=stats->max_qual; iqual++)
+        {
+            printf("\t%ld", (long)stats->quals_2nd[ibase*stats->nquals+iqual]);
+        }
+        printf("\n");
+    }
+    if ( stats->mpc_buf )
+    {
+        printf("# Mismatches per cycle and quality. Use `grep ^MPC | cut -f 2-` to extract this part.\n");
+        printf("# Columns correspond to qualities, rows to cycles. First column is the cycle number, second\n");
+        printf("# is the number of N's and the rest is the number of mismatches\n");
+        for (ibase=0; ibase<stats->max_len; ibase++)
+        {
+            printf("MPC\t%d",ibase+1);
+            for (iqual=0; iqual<=stats->max_qual; iqual++)
+            {
+                printf("\t%ld", (long)stats->mpc_buf[ibase*stats->nquals+iqual]);
+            }
+            printf("\n");
+        }
+    }
+    printf("# GC Content of first fragments. Use `grep ^GCF | cut -f 2-` to extract this part.\n");
+    int ibase_prev = 0;
+    for (ibase=0; ibase<stats->ngc; ibase++)
+    {
+        if ( stats->gc_1st[ibase]==stats->gc_1st[ibase_prev] ) continue;
+        printf("GCF\t%.2f\t%ld\n", (ibase+ibase_prev)*0.5*100./(stats->ngc-1), (long)stats->gc_1st[ibase_prev]);
+        ibase_prev = ibase;
+    }
+    printf("# GC Content of last fragments. Use `grep ^GCL | cut -f 2-` to extract this part.\n");
+    ibase_prev = 0;
+    for (ibase=0; ibase<stats->ngc; ibase++)
+    {
+        if ( stats->gc_2nd[ibase]==stats->gc_2nd[ibase_prev] ) continue;
+        printf("GCL\t%.2f\t%ld\n", (ibase+ibase_prev)*0.5*100./(stats->ngc-1), (long)stats->gc_2nd[ibase_prev]);
+        ibase_prev = ibase;
+    }
+    printf("# ACGT content per cycle. Use `grep ^GCC | cut -f 2-` to extract this part. The columns are: cycle, and A,C,G,T counts [%%]\n");
+    for (ibase=0; ibase<stats->max_len; ibase++)
+    {
+        uint64_t *ptr = &(stats->acgt_cycles[ibase*4]);
+        uint64_t  sum = ptr[0]+ptr[1]+ptr[2]+ptr[3];
+        if ( ! sum ) continue;
+        printf("GCC\t%d\t%.2f\t%.2f\t%.2f\t%.2f\n", ibase+1,100.*ptr[0]/sum,100.*ptr[1]/sum,100.*ptr[2]/sum,100.*ptr[3]/sum);
+    }
+    printf("# Insert sizes. Use `grep ^IS | cut -f 2-` to extract this part. The columns are: pairs total, inward oriented pairs, outward oriented pairs, other pairs\n");
+    for (isize=0; isize<ibulk; isize++) {
+        long in = (long)(stats->isize->inward(stats->isize->data, isize));
+        long out = (long)(stats->isize->outward(stats->isize->data, isize));
+        long other = (long)(stats->isize->other(stats->isize->data, isize));
+        if (!sparse || in + out + other > 0) {
+            printf("IS\t%d\t%ld\t%ld\t%ld\t%ld\n", isize,  in+out+other,
+                in , out, other);
+        }
+    }
+
+    printf("# Read lengths. Use `grep ^RL | cut -f 2-` to extract this part. The columns are: read length, count\n");
+    int ilen;
+    for (ilen=0; ilen<stats->max_len; ilen++)
+    {
+        if ( stats->read_lengths[ilen]>0 )
+            printf("RL\t%d\t%ld\n", ilen, (long)stats->read_lengths[ilen]);
+    }
+
+    printf("# Indel distribution. Use `grep ^ID | cut -f 2-` to extract this part. The columns are: length, number of insertions, number of deletions\n");
+    for (ilen=0; ilen<stats->nindels; ilen++)
+    {
+        if ( stats->insertions[ilen]>0 || stats->deletions[ilen]>0 )
+            printf("ID\t%d\t%ld\t%ld\n", ilen+1, (long)stats->insertions[ilen], (long)stats->deletions[ilen]);
+    }
+
+    printf("# Indels per cycle. Use `grep ^IC | cut -f 2-` to extract this part. The columns are: cycle, number of insertions (fwd), .. (rev) , number of deletions (fwd), .. (rev)\n");
+    for (ilen=0; ilen<=stats->nbases; ilen++)
+    {
+        // For deletions we print the index of the cycle before the deleted base (1-based) and for insertions
+        //  the index of the cycle of the first inserted base (also 1-based)
+        if ( stats->ins_cycles_1st[ilen]>0 || stats->ins_cycles_2nd[ilen]>0 || stats->del_cycles_1st[ilen]>0 || stats->del_cycles_2nd[ilen]>0 )
+            printf("IC\t%d\t%ld\t%ld\t%ld\t%ld\n", ilen+1, (long)stats->ins_cycles_1st[ilen], (long)stats->ins_cycles_2nd[ilen], (long)stats->del_cycles_1st[ilen], (long)stats->del_cycles_2nd[ilen]);
+    }
+
+    printf("# Coverage distribution. Use `grep ^COV | cut -f 2-` to extract this part.\n");
+    if  ( stats->cov[0] )
+        printf("COV\t[<%d]\t%d\t%ld\n",stats->cov_min,stats->cov_min-1, (long)stats->cov[0]);
+    int icov;
+    for (icov=1; icov<stats->ncov-1; icov++)
+        if ( stats->cov[icov] )
+            printf("COV\t[%d-%d]\t%d\t%ld\n",stats->cov_min + (icov-1)*stats->cov_step, stats->cov_min + icov*stats->cov_step-1,stats->cov_min + icov*stats->cov_step-1, (long)stats->cov[icov]);
+    if ( stats->cov[stats->ncov-1] )
+        printf("COV\t[%d<]\t%d\t%ld\n",stats->cov_min + (stats->ncov-2)*stats->cov_step-1,stats->cov_min + (stats->ncov-2)*stats->cov_step-1, (long)stats->cov[stats->ncov-1]);
+
+    // Calculate average GC content, then sort by GC and depth
+    printf("# GC-depth. Use `grep ^GCD | cut -f 2-` to extract this part. The columns are: GC%%, unique sequence percentiles, 10th, 25th, 50th, 75th and 90th depth percentile\n");
+    uint32_t igcd;
+    for (igcd=0; igcd<stats->igcd; igcd++)
+    {
+        if ( stats->fai )
+            stats->gcd[igcd].gc = rint(100. * stats->gcd[igcd].gc);
+        else
+            if ( stats->gcd[igcd].depth )
+                stats->gcd[igcd].gc = rint(100. * stats->gcd[igcd].gc / stats->gcd[igcd].depth);
+    }
+    qsort(stats->gcd, stats->igcd+1, sizeof(gc_depth_t), gcd_cmp);
+    igcd = 0;
+    while ( igcd < stats->igcd )
+    {
+        // Calculate percentiles (10,25,50,75,90th) for the current GC content and print
+        uint32_t nbins=0, itmp=igcd;
+        float gc = stats->gcd[igcd].gc;
+        while ( itmp<stats->igcd && fabs(stats->gcd[itmp].gc-gc)<0.1 )
+        {
+            nbins++;
+            itmp++;
+        }
+        printf("GCD\t%.1f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", gc, (igcd+nbins+1)*100./(stats->igcd+1),
+                gcd_percentile(&(stats->gcd[igcd]),nbins,10) *avg_read_length/stats->gcd_bin_size,
+                gcd_percentile(&(stats->gcd[igcd]),nbins,25) *avg_read_length/stats->gcd_bin_size,
+                gcd_percentile(&(stats->gcd[igcd]),nbins,50) *avg_read_length/stats->gcd_bin_size,
+                gcd_percentile(&(stats->gcd[igcd]),nbins,75) *avg_read_length/stats->gcd_bin_size,
+                gcd_percentile(&(stats->gcd[igcd]),nbins,90) *avg_read_length/stats->gcd_bin_size
+              );
+        igcd += nbins;
+    }
+}
+
+size_t mygetline(char **line, size_t *n, FILE *fp)
+{
+    if (line == NULL || n == NULL || fp == NULL)
+    {
+        errno = EINVAL;
+        return -1;
+    }
+    if (*n==0 || !*line)
+    {
+        *line = NULL;
+        *n = 0;
+    }
+
+    size_t nread=0;
+    int c;
+    while ((c=getc(fp))!= EOF && c!='\n')
+    {
+        if ( ++nread>=*n )
+        {
+            *n += 255;
+            *line = realloc(*line, sizeof(char)*(*n));
+        }
+        (*line)[nread-1] = c;
+    }
+    if ( nread>=*n )
+    {
+        *n += 255;
+        *line = realloc(*line, sizeof(char)*(*n));
+    }
+    (*line)[nread] = 0;
+    return nread>0 ? nread : -1;
+
+}
+
+void init_regions(stats_t *stats, char *file)
+{
+#if 0
+    khiter_t iter;
+    khash_t(kh_bam_tid) *header_hash;
+
+    header_hash = (khash_t(kh_bam_tid)*)stats->sam_header->hash;
+
+    FILE *fp = fopen(file,"r");
+    if ( !fp ) error("%s: %s\n",file,strerror(errno));
+
+    char *line = NULL;
+    size_t len = 0;
+    ssize_t nread;
+    int warned = 0;
+    int prev_tid=-1, prev_pos=-1;
+    while ((nread = mygetline(&line, &len, fp)) != -1)
+    {
+        if ( line[0] == '#' ) continue;
+
+        int i = 0;
+        while ( i<nread && !isspace(line[i]) ) i++;
+        if ( i>=nread ) error("Could not parse the file: %s [%s]\n", file,line);
+        line[i] = 0;
+
+        iter = kh_get(kh_bam_tid, header_hash, line);
+        int tid = kh_val(header_hash, iter);
+        if ( iter == kh_end(header_hash) )
+        {
+            if ( !warned )
+                fprintf(pysamerr,"Warning: Some sequences not present in the BAM, e.g. \"%s\". This message is printed only once.\n", line);
+            warned = 1;
+            continue;
+        }
+
+        if ( tid >= stats->nregions )
+        {
+            stats->regions = realloc(stats->regions,sizeof(regions_t)*(stats->nregions+100));
+            int j;
+            for (j=stats->nregions; j<stats->nregions+100; j++)
+            {
+                stats->regions[j].npos = stats->regions[j].mpos = stats->regions[j].cpos = 0;
+                stats->regions[j].pos = NULL;
+            }
+            stats->nregions += 100;
+        }
+        int npos = stats->regions[tid].npos;
+        if ( npos >= stats->regions[tid].mpos )
+        {
+            stats->regions[tid].mpos += 1000;
+            stats->regions[tid].pos = realloc(stats->regions[tid].pos,sizeof(pos_t)*stats->regions[tid].mpos);
+        }
+
+        if ( (sscanf(line+i+1,"%d %d",&stats->regions[tid].pos[npos].from,&stats->regions[tid].pos[npos].to))!=2 ) error("Could not parse the region [%s]\n");
+        if ( prev_tid==-1 || prev_tid!=tid )
+        {
+            prev_tid = tid;
+            prev_pos = stats->regions[tid].pos[npos].from;
+        }
+        if ( prev_pos>stats->regions[tid].pos[npos].from )
+            error("The positions are not in chromosomal order (%s:%d comes after %d)\n", line,stats->regions[tid].pos[npos].from,prev_pos);
+        stats->regions[tid].npos++;
+    }
+    if (line) free(line);
+    if ( !stats->regions ) error("Unable to map the -t sequences to the BAM sequences.\n");
+    fclose(fp);
+#else
+    fprintf(pysamerr, "Samtools-htslib: init_regions() header parsing not yet implemented\n");
+    abort();
+#endif
+}
+
+void destroy_regions(stats_t *stats)
+{
+    int i;
+    for (i=0; i<stats->nregions; i++)
+    {
+        if ( !stats->regions[i].mpos ) continue;
+        free(stats->regions[i].pos);
+    }
+    if ( stats->regions ) free(stats->regions);
+}
+
+void reset_regions(stats_t *stats)
+{
+    int i;
+    for (i=0; i<stats->nregions; i++)
+        stats->regions[i].cpos = 0;
+}
+
+int is_in_regions(bam1_t *bam_line, stats_t *stats)
+{
+    if ( !stats->regions ) return 1;
+
+    if ( bam_line->core.tid >= stats->nregions || bam_line->core.tid<0 ) return 0;
+    if ( !stats->is_sorted ) error("The BAM must be sorted in order for -t to work.\n");
+
+    regions_t *reg = &stats->regions[bam_line->core.tid];
+    if ( reg->cpos==reg->npos ) return 0;       // done for this chr
+
+    // Find a matching interval or skip this read. No splicing of reads is done, no indels or soft clips considered,
+    //  even small overlap is enough to include the read in the stats.
+    int i = reg->cpos;
+    while ( i<reg->npos && reg->pos[i].to<=bam_line->core.pos ) i++;
+    if ( i>=reg->npos ) { reg->cpos = reg->npos; return 0; }
+    if ( bam_line->core.pos + bam_line->core.l_qseq + 1 < reg->pos[i].from ) return 0;
+    reg->cpos = i;
+    stats->reg_from = reg->pos[i].from;
+    stats->reg_to   = reg->pos[i].to;
+
+    return 1;
+}
+
+void init_group_id(stats_t *stats, char *id)
+{
+#if 0
+    if ( !stats->sam_header->dict )
+        stats->sam_header->dict = sam_header_parse2(stats->sam_header->text);
+    void *iter = stats->sam_header->dict;
+    const char *key, *val;
+    int n = 0;
+    stats->rg_hash = khash_str2int_init();
+    while ( (iter = sam_header2key_val(iter, "RG","ID","SM", &key, &val)) )
+    {
+        if ( !strcmp(id,key) || (val && !strcmp(id,val)) )
+        {
+            khiter_t k = kh_get(kh_rg, stats->rg_hash, key);
+            if ( k != kh_end(stats->rg_hash) )
+                fprintf(pysamerr, "[init_group_id] The group ID not unique: \"%s\"\n", key);
+            int ret;
+            k = kh_put(kh_rg, stats->rg_hash, key, &ret);
+            kh_value(stats->rg_hash, k) = val;
+            n++;
+        }
+    }
+    if ( !n )
+        error("The sample or read group \"%s\" not present.\n", id);
+#else
+    fprintf(pysamerr, "Samtools-htslib: init_group_id() header parsing not yet implemented\n");
+    abort();
+#endif
+}
+
+
+static void error(const char *format, ...)
+{
+    if ( !format )
+    {
+        printf("About: The program collects statistics from BAM files. The output can be visualized using plot-bamstats.\n");
+        printf("Usage: samtools stats [OPTIONS] file.bam\n");
+        printf("       samtools stats [OPTIONS] file.bam chr:from-to\n");
+        printf("Options:\n");
+        printf("    -c, --coverage <int>,<int>,<int>    Coverage distribution min,max,step [1,1000,1]\n");
+        printf("    -d, --remove-dups                   Exclude from statistics reads marked as duplicates\n");
+        printf("    -f, --required-flag  <str|int>      Required flag, 0 for unset. See also `samtools flags` [0]\n");
+        printf("    -F, --filtering-flag <str|int>      Filtering flag, 0 for unset. See also `samtools flags` [0]\n");
+        printf("        --GC-depth <float>              the size of GC-depth bins (decreasing bin size increases memory requirement) [2e4]\n");
+        printf("    -h, --help                          This help message\n");
+        printf("    -i, --insert-size <int>             Maximum insert size [8000]\n");
+        printf("    -I, --id <string>                   Include only listed read group or sample name\n");
+        printf("    -l, --read-length <int>             Include in the statistics only reads with the given read length []\n");
+        printf("    -m, --most-inserts <float>          Report only the main part of inserts [0.99]\n");
+        printf("    -q, --trim-quality <int>            The BWA trimming parameter [0]\n");
+        printf("    -r, --ref-seq <file>                Reference sequence (required for GC-depth and mismatches-per-cycle calculation).\n");
+        printf("    -t, --target-regions <file>         Do stats in these regions only. Tab-delimited file chr,from,to, 1-based, inclusive.\n");
+        printf("    -s, --sam                           Input is SAM (usually auto-detected now).\n");
+        printf("    -x, --sparse                        Suppress outputting IS rows where there are no insertions.\n");
+        printf("\n");
+    }
+    else
+    {
+        va_list ap;
+        va_start(ap, format);
+        vfprintf(pysamerr, format, ap);
+        va_end(ap);
+    }
+    exit(-1);
+}
+
+void cleanup_stats(stats_t* stats)
+{
+    sam_close(stats->sam);
+    if (stats->fai) fai_destroy(stats->fai);
+    free(stats->cov_rbuf.buffer); free(stats->cov);
+    free(stats->quals_1st); free(stats->quals_2nd);
+    free(stats->gc_1st); free(stats->gc_2nd);
+    stats->isize->isize_free(stats->isize->data);
+    free(stats->isize);
+    free(stats->gcd);
+    free(stats->rseq_buf);
+    free(stats->mpc_buf);
+    free(stats->acgt_cycles);
+    free(stats->read_lengths);
+    free(stats->insertions);
+    free(stats->deletions);
+    free(stats->ins_cycles_1st);
+    free(stats->ins_cycles_2nd);
+    free(stats->del_cycles_1st);
+    free(stats->del_cycles_2nd);
+    destroy_regions(stats);
+    if ( stats->rg_hash ) khash_str2int_destroy(stats->rg_hash);
+    free(stats);
+}
+
+int main_stats(int argc, char *argv[])
+{
+    char *targets = NULL;
+    char *bam_fname = NULL;
+    char *group_id = NULL;
+    samFile* sam = NULL;
+    char in_mode[5];
+    int sparse = 0;
+
+    stats_t *stats = calloc(1,sizeof(stats_t));
+    stats->ngc    = 200;
+    stats->nquals = 256;
+    stats->nbases = 300;
+    stats->nisize = 8000;
+    stats->max_len   = 30;
+    stats->max_qual  = 40;
+    stats->isize_main_bulk = 0.99;   // There are always outliers at the far end
+    stats->gcd_bin_size = 20e3;
+    stats->rseq_pos     = -1;
+    stats->tid = stats->gcd_pos = -1;
+    stats->igcd = 0;
+    stats->is_sorted = 1;
+    stats->cov_min  = 1;
+    stats->cov_max  = 1000;
+    stats->cov_step = 1;
+    stats->argc = argc;
+    stats->argv = argv;
+    stats->filter_readlen = -1;
+    stats->nindels = stats->nbases;
+
+    strcpy(in_mode, "rb");
+
+    static const struct option loptions[] =
+    {
+        {"help", no_argument, NULL, 'h'},
+        {"remove-dups", no_argument, NULL, 'd'},
+        {"sam", no_argument, NULL, 's'},
+        {"ref-seq", required_argument, NULL, 'r'},
+        {"coverage", required_argument, NULL, 'c'},
+        {"read-length", required_argument, NULL, 'l'},
+        {"insert-size", required_argument, NULL, 'i'},
+        {"most-inserts", required_argument, NULL, 'm'},
+        {"trim-quality", required_argument, NULL, 'q'},
+        {"target-regions", required_argument, NULL, 't'},
+        {"required-flag", required_argument, NULL, 'f'},
+        {"filtering-flag", required_argument, NULL, 'F'},
+        {"id", required_argument, NULL, 'I'},
+        {"GC-depth", required_argument, NULL, 1},
+        {"sparse", no_argument, NULL, 'x'},
+        {NULL, 0, NULL, 0}
+    };
+    int opt;
+    while ( (opt=getopt_long(argc,argv,"?hdsxr:c:l:i:t:m:q:f:F:I:1:",loptions,NULL))>0 )
+    {
+        switch (opt)
+        {
+            case 'f': stats->flag_require = bam_str2flag(optarg); break;
+            case 'F': stats->flag_filter = bam_str2flag(optarg); break;
+            case 'd': stats->flag_filter |= BAM_FDUP; break;
+            case 's': strcpy(in_mode, "r"); break;
+            case 'r': stats->fai = fai_load(optarg);
+                      if (stats->fai==0)
+                          error("Could not load faidx: %s\n", optarg);
+                      break;
+            case  1 : stats->gcd_bin_size = atof(optarg); break;
+            case 'c': if ( sscanf(optarg,"%d,%d,%d",&stats->cov_min,&stats->cov_max,&stats->cov_step)!= 3 )
+                          error("Unable to parse -c %s\n", optarg);
+                      break;
+            case 'l': stats->filter_readlen = atoi(optarg); break;
+            case 'i': stats->nisize = atoi(optarg); break;
+            case 'm': stats->isize_main_bulk = atof(optarg); break;
+            case 'q': stats->trim_qual = atoi(optarg); break;
+            case 't': targets = optarg; break;
+            case 'I': group_id = optarg; break;
+            case 'x': sparse = 1; break;
+            case '?':
+            case 'h': error(NULL);
+            default: error("Unknown argument: %s\n", optarg);
+        }
+    }
+    if ( optind<argc )
+        bam_fname = argv[optind++];
+
+    if ( !bam_fname )
+    {
+        if ( isatty(STDIN_FILENO) )
+            error(NULL);
+        bam_fname = "-";
+    }
+
+    // Init structures
+    //  .. coverage bins and round buffer
+    if ( stats->cov_step > stats->cov_max - stats->cov_min + 1 )
+    {
+        stats->cov_step = stats->cov_max - stats->cov_min;
+        if ( stats->cov_step <= 0 )
+            stats->cov_step = 1;
+    }
+    stats->ncov = 3 + (stats->cov_max-stats->cov_min) / stats->cov_step;
+    stats->cov_max = stats->cov_min + ((stats->cov_max-stats->cov_min)/stats->cov_step +1)*stats->cov_step - 1;
+    stats->cov = calloc(sizeof(uint64_t),stats->ncov);
+    stats->cov_rbuf.size = stats->nbases*5;
+    stats->cov_rbuf.buffer = calloc(sizeof(int32_t),stats->cov_rbuf.size);
+    // .. bam
+    if ((sam = sam_open(bam_fname, in_mode)) == 0)
+        error("Failed to open: %s\n", bam_fname);
+    stats->sam = sam;
+    stats->sam_header = sam_hdr_read(sam);
+    if ( group_id ) init_group_id(stats, group_id);
+    bam1_t *bam_line = bam_init1();
+    // .. arrays
+    stats->quals_1st      = calloc(stats->nquals*stats->nbases,sizeof(uint64_t));
+    stats->quals_2nd      = calloc(stats->nquals*stats->nbases,sizeof(uint64_t));
+    stats->gc_1st         = calloc(stats->ngc,sizeof(uint64_t));
+    stats->gc_2nd         = calloc(stats->ngc,sizeof(uint64_t));
+    stats->isize          = init_isize_t(stats->nisize);
+    stats->gcd            = calloc(stats->ngcd,sizeof(gc_depth_t));
+    stats->mpc_buf        = stats->fai ? calloc(stats->nquals*stats->nbases,sizeof(uint64_t)) : NULL;
+    stats->acgt_cycles    = calloc(4*stats->nbases,sizeof(uint64_t));
+    stats->read_lengths   = calloc(stats->nbases,sizeof(uint64_t));
+    stats->insertions     = calloc(stats->nbases,sizeof(uint64_t));
+    stats->deletions      = calloc(stats->nbases,sizeof(uint64_t));
+    stats->ins_cycles_1st = calloc(stats->nbases+1,sizeof(uint64_t));
+    stats->ins_cycles_2nd = calloc(stats->nbases+1,sizeof(uint64_t));
+    stats->del_cycles_1st = calloc(stats->nbases+1,sizeof(uint64_t));
+    stats->del_cycles_2nd = calloc(stats->nbases+1,sizeof(uint64_t));
+    realloc_rseq_buffer(stats);
+    if ( targets )
+        init_regions(stats, targets);
+
+    // Collect statistics
+    if ( optind<argc )
+    {
+        // Collect stats in selected regions only
+        hts_idx_t *bam_idx = bam_index_load(bam_fname);
+        if (bam_idx == 0)
+            error("Random alignment retrieval only works for indexed BAM files.\n");
+
+        int i;
+        for (i=optind; i<argc; i++)
+        {
+            reset_regions(stats);
+            hts_itr_t* iter = bam_itr_querys(bam_idx, stats->sam_header, argv[i]);
+            while (sam_itr_next(sam, iter, bam_line) >= 0) {
+                collect_stats(bam_line,stats);
+            }
+            bam_itr_destroy(iter);
+        }
+        hts_idx_destroy(bam_idx);
+    }
+    else
+    {
+        // Stream through the entire BAM ignoring off-target regions if -t is given
+        while (sam_read1(sam, stats->sam_header, bam_line) >= 0)
+            collect_stats(bam_line,stats);
+    }
+    round_buffer_flush(stats,-1);
+
+    output_stats(stats, sparse);
+    bam_destroy1(bam_line);
+    bam_hdr_destroy(stats->sam_header);
+
+    cleanup_stats(stats);
+
+    return 0;
+}
diff --git a/samtools/stats_isize.c.pysam.c b/samtools/stats_isize.c.pysam.c
new file mode 100644
index 0000000..a25e4d7
--- /dev/null
+++ b/samtools/stats_isize.c.pysam.c
@@ -0,0 +1,221 @@
+#include "pysam.h"
+
+/*  stats_isize.c -- generalised insert size calculation for samtools stats.
+
+    Copyright (C) 2014 Genome Research Ltd.
+
+    Author: Nicholas Clarke <nc6 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <stdio.h>
+#include "stats_isize.h"
+#include <htslib/khash.h>
+
+typedef enum {IN,OUT,OTHER} isize_insert_t;
+
+static int max(int a, int b) {
+    if (a < b) {
+        return b;
+    } else {
+        return a;
+    }
+}
+
+static isize_sparse_record_t * sparse_get_f(isize_data_t data, int at) {
+    isize_sparse_data_t *a = data.sparse;
+    khash_t(m32) *h = a->array;
+
+    khint_t k = kh_get(m32, h, at);
+    if (k != kh_end(h)) {
+        return kh_value(h, k);
+    } else {
+        return NULL;
+    }
+}
+
+static uint64_t sparse_in_f(isize_data_t data, int at) {
+    isize_sparse_record_t* a = sparse_get_f(data, at);
+    if (a != NULL) {
+        return a->isize_inward;
+    } else {
+        return 0;
+    }
+}
+static uint64_t sparse_out_f(isize_data_t data, int at) {
+    isize_sparse_record_t* a = sparse_get_f(data, at);
+    if (a != NULL) {
+        return a->isize_outward;
+    } else {
+        return 0;
+    }
+}
+static uint64_t sparse_other_f(isize_data_t data, int at) {
+    isize_sparse_record_t* a = sparse_get_f(data, at);
+    if (a != NULL) {
+        return a->isize_other;
+    } else {
+        return 0;
+    }
+}
+
+static void sparse_set_f(isize_data_t data, int at, isize_insert_t field, uint64_t value) {
+    isize_sparse_data_t *a = data.sparse;
+    khash_t(m32) *h = a->array;
+
+    khint_t k = kh_get(m32, h, at);
+    isize_sparse_record_t *rec;
+    if (k != kh_end(h)) {
+        rec = kh_value(h, k);
+    } else if (value != 0) {
+        rec = malloc(sizeof(isize_sparse_record_t));
+        if (rec != NULL) {
+            rec->isize_inward = 0;
+            rec->isize_outward = 0;
+            rec->isize_other = 0;
+            int stupid = 0;
+            khint_t it = kh_put(m32, h, at, & stupid);
+            kh_value(h, it) = rec;
+            a->max = max(at, a->max);
+        } else {
+            fprintf(pysamerr, "%s\n", "Failed to allocate memory for isize_sparse_record_t");
+            exit(11);
+        }
+    } else {
+        return;
+    }
+    if (field == IN) {
+        rec->isize_inward = value;
+    } else if (field == OUT) {
+        rec->isize_outward = value;
+    } else {
+        rec->isize_other = value;
+    }
+
+}
+
+static void sparse_set_in_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, IN, value); }
+static void sparse_set_out_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, OUT, value); }
+static void sparse_set_other_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, OTHER, value); }
+
+static void sparse_inc_in_f(isize_data_t data, int at) { sparse_set_in_f(data, at, sparse_in_f(data, at) + 1); }
+static void sparse_inc_out_f(isize_data_t data, int at) { sparse_set_out_f(data, at, sparse_out_f(data, at) + 1); }
+static void sparse_inc_other_f(isize_data_t data, int at) { sparse_set_other_f(data, at, sparse_other_f(data, at) + 1); }
+
+static void sparse_isize_free(isize_data_t data) {
+    isize_sparse_data_t *a = data.sparse;
+    khint_t k;
+    for (k = 0; k < kh_end(a->array); ++k)
+        if (kh_exist(a->array, k)) free(kh_val(a->array, k));
+    kh_destroy(m32, a->array);
+    free(a);
+}
+
+static int sparse_nitems(isize_data_t data) {
+    isize_sparse_data_t *a = data.sparse;
+    return a->max + 1;
+}
+
+static uint64_t dense_in_f(isize_data_t data, int at) { return data.dense->isize_inward[at]; }
+static uint64_t dense_out_f(isize_data_t data, int at) { return data.dense->isize_outward[at]; }
+static uint64_t dense_other_f(isize_data_t data, int at) { return data.dense->isize_other[at]; }
+
+static void dense_set_in_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_inward[at] = value; }
+static void dense_set_out_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_outward[at] = value; }
+static void dense_set_other_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_other[at] = value; }
+
+static void dense_inc_in_f(isize_data_t data, int at) { data.dense->isize_inward[at] += 1; }
+static void dense_inc_out_f(isize_data_t data, int at) { data.dense->isize_outward[at] += 1; }
+static void dense_inc_other_f(isize_data_t data, int at) { data.dense->isize_other[at] += 1; }
+
+static void dense_isize_free(isize_data_t data) {
+    isize_dense_data_t *a = data.dense;
+    free(a->isize_inward);
+    free(a->isize_outward);
+    free(a->isize_other);
+    free(a);
+}
+
+static int dense_nitems(isize_data_t data) {
+    isize_dense_data_t *a = data.dense;
+    return a->total;
+}
+
+// Construct a relevant isize_t given the bound.
+isize_t *init_isize_t(int bound) {
+    if (bound <= 0) {
+        // Use sparse data structure.
+        isize_sparse_data_t *data = (isize_sparse_data_t *) malloc(sizeof(isize_sparse_data_t));
+
+        // Initialise
+        data->max = 0;
+        data->array = kh_init(m32);
+
+        isize_t *isize = (isize_t *)malloc(sizeof(isize_t));
+
+        isize->data.sparse = data;
+        isize->nitems = & sparse_nitems;
+
+        isize->inward = & sparse_in_f;
+        isize->outward = & sparse_out_f;
+        isize->other = & sparse_other_f;
+
+        isize->set_inward = & sparse_set_in_f;
+        isize->set_outward = & sparse_set_out_f;
+        isize->set_other = & sparse_set_other_f;
+
+        isize->inc_inward = & sparse_inc_in_f;
+        isize->inc_outward = & sparse_inc_out_f;
+        isize->inc_other = & sparse_inc_other_f;
+
+        isize->isize_free = & sparse_isize_free;
+
+        return isize;
+    } else {
+        uint64_t* in = calloc(bound,sizeof(uint64_t));
+        uint64_t* out = calloc(bound,sizeof(uint64_t));
+        uint64_t* other = calloc(bound,sizeof(uint64_t));
+        isize_dense_data_t *rec = (isize_dense_data_t *)malloc(sizeof(isize_dense_data_t));
+        rec->isize_inward = in;
+        rec->isize_outward = out;
+        rec->isize_other = other;
+        rec->total=bound;
+
+        isize_t *isize = (isize_t *)malloc(sizeof(isize_t));
+
+        isize->data.dense = rec;
+        isize->nitems = & dense_nitems;
+
+        isize->inward = & dense_in_f;
+        isize->outward = & dense_out_f;
+        isize->other = & dense_other_f;
+
+        isize->set_inward = & dense_set_in_f;
+        isize->set_outward = & dense_set_out_f;
+        isize->set_other = & dense_set_other_f;
+
+        isize->inc_inward = & dense_inc_in_f;
+        isize->inc_outward = & dense_inc_out_f;
+        isize->inc_other = & dense_inc_other_f;
+
+        isize->isize_free = & dense_isize_free;
+
+        return isize;
+    }
+}
diff --git a/samtools/stats_isize.h b/samtools/stats_isize.h
new file mode 100644
index 0000000..3243222
--- /dev/null
+++ b/samtools/stats_isize.h
@@ -0,0 +1,83 @@
+/*  stats_isize.h -- generalised insert size calculation for samtools stats.
+
+    Copyright (C) 2014 Genome Research Ltd.
+
+    Author: Nicholas Clarke <nc6 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <htslib/khash.h>
+#include <stdint.h>
+
+typedef struct
+{
+    int total;
+    uint64_t *isize_inward, *isize_outward, *isize_other;
+}
+isize_dense_data_t;
+
+typedef struct
+{
+    uint64_t isize_inward, isize_outward, isize_other;
+}
+isize_sparse_record_t;
+
+KHASH_MAP_INIT_INT(m32, isize_sparse_record_t *)
+
+typedef struct
+{
+    int max;
+    khash_t(m32) *array;
+}
+isize_sparse_data_t;
+
+typedef union {
+    isize_sparse_data_t *sparse;
+    isize_dense_data_t *dense;
+} isize_data_t;
+
+// Insert size structure
+typedef struct
+{
+    isize_data_t data;
+
+    // Maximum
+    int (*nitems)(isize_data_t);
+
+    // Fetch the number of inserts of a given size
+    uint64_t (*inward)(isize_data_t, int);
+    uint64_t (*outward)(isize_data_t, int);
+    uint64_t (*other)(isize_data_t, int);
+
+    // Set the number of inserts of a given size
+    void (*set_inward)(isize_data_t, int, uint64_t);
+    void (*set_outward)(isize_data_t, int, uint64_t);
+    void (*set_other)(isize_data_t, int, uint64_t);
+
+    // Increment the number of inserts of a given size
+    void (*inc_inward)(isize_data_t, int);
+    void (*inc_outward)(isize_data_t, int);
+    void (*inc_other)(isize_data_t, int);
+
+    // Free this structure
+    void (*isize_free)(isize_data_t);
+}
+isize_t;
+
+isize_t *init_isize_t(int bound);
diff --git a/samtools/test/test.c.pysam.c b/samtools/test/test.c.pysam.c
new file mode 100644
index 0000000..735eb7b
--- /dev/null
+++ b/samtools/test/test.c.pysam.c
@@ -0,0 +1,55 @@
+#include "pysam.h"
+
+/*  test/test.c -- test harness utility routines.
+
+    Copyright (C) 2014 Genome Research Ltd.
+
+    Author: Martin O. Pollard <mp15 at sanger.ac.uk>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.  */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <htslib/sam.h>
+
+#include "test.h"
+
+void xfreopen(const char *path, const char *mode, FILE *stream)
+{
+    if (freopen(path, mode, stream) == NULL) {
+        fprintf(pysamerr, __FILE__": error reopening %s: %s\n",
+                path, strerror(errno));
+        exit(2);
+    }
+}
+
+void dump_hdr(const bam_hdr_t* hdr)
+{
+    printf("n_targets: %d\n", hdr->n_targets);
+    printf("ignore_sam_err: %d\n", hdr->ignore_sam_err);
+    printf("l_text: %u\n", hdr->l_text);
+    printf("idx\ttarget_len\ttarget_name:\n");
+    int32_t target;
+    for (target = 0; target < hdr->n_targets; ++target) {
+        printf("%d\t%u\t\"%s\"\n", target, hdr->target_len[target], hdr->target_name[target]);
+    }
+    printf("text: \"%s\"\n", hdr->text);
+}
diff --git a/htslib/htslib/hts_defs.h b/samtools/test/test.h
similarity index 56%
copy from htslib/htslib/hts_defs.h
copy to samtools/test/test.h
index efc4f6c..610b155 100644
--- a/htslib/htslib/hts_defs.h
+++ b/samtools/test/test.h
@@ -1,8 +1,8 @@
-/*  hts_defs.h -- Miscellaneous definitions.
+/*  test/test.h -- test harness utility routines.
 
-    Copyright (C) 2013-2014 Genome Research Ltd.
+    Copyright (C) 2014 Genome Research Ltd.
 
-    Author: John Marshall <jm18 at sanger.ac.uk>
+    Author: Martin O. Pollard <mp15 at sanger.ac.uk>
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
@@ -11,7 +11,7 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
-The above copyright notices and this permission notice shall be included in
+The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -22,26 +22,14 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 DEALINGS IN THE SOFTWARE.  */
 
-#ifndef HTSLIB_HTS_DEFS_H
-#define HTSLIB_HTS_DEFS_H
+#ifndef TEST_H
+#define TEST_H
 
-#if __clang__major__ >= 2 || __GNUC__ >= 3
-#define HTS_NORETURN __attribute__ ((__noreturn__))
-#else
-#define HTS_NORETURN
-#endif
+#include <stdio.h>
+#include <htslib/sam.h>
 
-#if (defined __clang__ && __clang_major__ >= 3) || \
-    (defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__==4 && __GNUC_MINOR__ >= 5)))
-#define HTS_RESULT_USED __attribute__ ((__warn_unused_result__))
-#else
-#define HTS_RESULT_USED
-#endif
+void xfreopen(const char *path, const char *mode, FILE *stream);
 
-#if defined __clang__ || defined __GNUC__
-#define HTS_UNUSED __attribute__ ((__unused__))
-#else
-#define HTS_UNUSED
-#endif
+void dump_hdr(const bam_hdr_t* hdr);
 
 #endif
diff --git a/samtools/version.h b/samtools/version.h
new file mode 100644
index 0000000..4558007
--- /dev/null
+++ b/samtools/version.h
@@ -0,0 +1 @@
+#define SAMTOOLS_VERSION "1.1"
diff --git a/samtools/win32/__init__.py b/samtools/win32/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/setup.py b/setup.py
index 4589409..45fb8bc 100644
--- a/setup.py
+++ b/setup.py
@@ -14,50 +14,57 @@ import hashlib
 import re
 import fnmatch
 import platform
-import subprocess
 
 name = "pysam"
 
 IS_PYTHON3 = sys.version_info[0] >= 3
 
-
 # How to link against HTSLIB
 # separate: use included htslib and include in each extension
 #           module. No dependencies between modules and works
-#           with setup.py install, but wasteful in terms of 
+#           with setup.py install, but wasteful in terms of
 #           memory and compilation time.
 # shared: share chtslib across extension modules. This would be
 #         the ideal method, but currently requires
 #         LD_LIBRARY_PATH to be set correctly when using
-#         pysam. 
-# external: use shared libhts.so compiled outside of 
+#         pysam.
+# external: use shared libhts.so compiled outside of
 #           pysam
-HTSLIB = "separate"
-HTSLIB_DIR = []
+HTSLIB_MODE = "separate"
+HTSLIB_LIBRARY_DIR = os.environ.get('HTSLIB_LIBRARY_DIR', None)
+HTSLIB_INCLUDE_DIR = os.environ.get('HTSLIB_INCLUDE_DIR', None)
 
 # collect pysam version
 sys.path.insert(0, "pysam")
 import version
-
 version = version.__version__
 
+# exclude sources that contains a main function
 samtools_exclude = ("bamtk.c", "razip.c", "bgzip.c",
                     "main.c", "calDepth.c", "bam2bed.c",
                     "wgsim.c", "md5fa.c", "maq2sam.c",
                     "bamcheck.c",
-                    "chk_indel.c")
-htslib_exclude = ('htslib/tabix.c',)
-samtools_dest = os.path.abspath("samtools")
+                    "chk_indel.c",
+                    "vcf-miniview.c")
+htslib_exclude = ('htslib/tabix.c', 'htslib/bgzip.c')
 tabix_exclude = ("main.c",)
+
+# destination directories for import of samtools and tabix
+samtools_dest = os.path.abspath("samtools")
 tabix_dest = os.path.abspath("tabix")
 
-# Linking against htslib
-if HTSLIB == 'external':
+if HTSLIB_LIBRARY_DIR:
+    # linking against a shared, externally installed htslib version, no
+    # sources required for htslib
     htslib_sources = []
+    shared_htslib_sources = []
     chtslib_sources = []
-    htslib_library_dirs = HTSLIB_DIR
+    htslib_library_dirs = [HTSLIB_LIBRARY_DIR]
+    htslib_include_dirs = [HTSLIB_INCLUDE_DIR]
     htslib_libraries = ['hts']
-elif HTSLIB == 'separate':
+elif HTSLIB_MODE == 'separate':
+    # add to each pysam component a separately compiled
+    # htslib
     htslib_sources = [
         x for x in
         glob.glob(os.path.join("htslib", "*.c")) +
@@ -65,8 +72,12 @@ elif HTSLIB == 'separate':
         if x not in htslib_exclude]
     shared_htslib_sources = htslib_sources
     htslib_library_dirs = []
+    htslib_include_dirs = ['htslib']
     htslib_libraries = []
-elif HTSLIB == 'shared':
+elif HTSLIB_MODE == 'shared':
+    # link each pysam component against the same
+    # htslib built from sources included in the pysam
+    # package.
     htslib_sources = []
     shared_htslib_sources = [
         x for x in
@@ -74,9 +85,10 @@ elif HTSLIB == 'shared':
         glob.glob(os.path.join("htslib", "cram", "*.c"))
         if x not in htslib_exclude]
     htslib_library_dirs = ['pysam']
+    htslib_include_dirs = ['htslib']
     htslib_libraries = ['chtslib']
 else:
-    raise ValueError("unknown HTSLIB value '%s'" % HTSLIB)
+    raise ValueError("unknown HTSLIB value '%s'" % HTSLIB_MODE)
 
 
 def locate(pattern, root=os.curdir):
@@ -106,21 +118,33 @@ extern FILE * pysamerr;
 #endif
 """)
 
-# copy samtools source
+#################################################################
+# Importing samtools and htslib
+#
+# For htslib, simply copy the whole release tar-ball
+# into the directory "htslib" and recreate the file version.h
+#
+# rm -rf htslib
+# mv download/htslib htslib
+# git checkout -- htslib/version.h
+# Edit the file htslib/version.h to set the right version number.
+#
+# For samtools, type:
+# rm -rf samtools
+# python setup.py import download/samtools
+#
 if len(sys.argv) >= 2 and sys.argv[1] == "import":
     if len(sys.argv) < 3:
         raise ValueError("missing PATH to samtools source directory")
-    if len(sys.argv) < 4:
-        raise ValueError("missing PATH to tabix source directory")
 
     for destdir, srcdir, exclude in zip(
-            (samtools_dest, tabix_dest),
-            sys.argv[2:4],
-            (samtools_exclude, tabix_exclude)):
+            (samtools_dest,),
+            sys.argv[2:3],
+            (samtools_exclude,)):
 
         srcdir = os.path.abspath(srcdir)
         if not os.path.exists(srcdir):
-            raise IOError("samtools src dir `%s` does not exist." % srcdir)
+            raise IOError("source directory `%s` does not exist." % srcdir)
 
         cfiles = locate("*.c", srcdir)
         hfiles = locate("*.h", srcdir)
@@ -160,9 +184,9 @@ if len(sys.argv) >= 2 and sys.argv[1] == "import":
 
         sys.stdout.write(
             "installed latest source code from %s: "
-            "%i files copied" % (srcdir, ncopied))
+            "%i files copied\n" % (srcdir, ncopied))
         # redirect stderr to pysamerr and replace bam.h with a stub.
-        sys.stdout.write("applying stderr redirection")
+        sys.stdout.write("applying stderr redirection\n")
 
         _update_pysam_files(cf, destdir)
 
@@ -183,18 +207,6 @@ if len(sys.argv) >= 2 and sys.argv[1] == "refresh":
     sys.exit(0)
 
 
-# checkout latest version of htslib
-if len(sys.argv) == 2 and sys.argv[1] == "htslib":
-    if not os.path.exists("htslib"):
-        subprocess.call(["git", "clone", "git at github.com:samtools/htslib.git"])
-        with open(os.path.join("htslib", "version.h"), "w") as outfile:
-            outfile.write('#define HTS_VERSION "0.0.1"')
-    else:
-        os.chdir('htslib')
-        subprocess.call(["git", "pull"])
-
-    sys.exit(0)
-
 ###################
 # populate headers
 # mkdir pysam/include pysam/include/win32
@@ -202,12 +214,7 @@ if len(sys.argv) == 2 and sys.argv[1] == "htslib":
 # cp samtools/*.h pysam/*.h pysam/include
 # cp samtools/win32/*.h pysam/include/win32
 
-try:
-    from setuptools import Extension, setup
-except ImportError:
-    from ez_setup import use_setuptools
-    use_setuptools()
-    from setuptools import Extension, setup
+from setuptools import Extension, setup
 
 #######################################################
 #######################################################
@@ -221,6 +228,7 @@ except ImportError:
     tabix_sources = ["pysam/ctabix.c"]
     faidx_sources = ["pysam/cfaidx.c"]
     csamfile_sources = ["pysam/csamfile.c"]
+    calignmentfile_sources = ["pysam/calignmentfile.c"]
     tabproxies_sources = ["pysam/TabProxies.c"]
     cvcf_sources = ["pysam/cvcf.c"]
 else:
@@ -243,11 +251,13 @@ else:
     csamtools_sources = ["pysam/csamtools.pyx"]
     chtslib_sources = ["pysam/chtslib.pyx"]
     csamfile_sources = ["pysam/csamfile.pyx"]
+    calignmentfile_sources = ["pysam/calignmentfile.pyx"]
     tabix_sources = ["pysam/ctabix.pyx"]
     faidx_sources = ["pysam/cfaidx.pyx"]
     tabproxies_sources = ["pysam/TabProxies.pyx"]
     cvcf_sources = ["pysam/cvcf.pyx"]
 
+
 #######################################################
 classifiers = """
 Development Status :: 2 - Alpha
@@ -279,12 +289,14 @@ samtools = Extension(
         "pysam_util.c", )] +
     glob.glob(os.path.join("samtools", "*.pysam.c")) +
     os_c_files +
-    glob.glob(os.path.join("samtools", "*", "*.pysam.c")),
+    glob.glob(os.path.join("samtools", "*", "*.pysam.c")) +
+    htslib_sources,
     library_dirs=[],
-    include_dirs=["samtools", "pysam"] + include_os,
-    libraries=["z"],
+    include_dirs=["samtools", "pysam"] + include_os + htslib_include_dirs,
+    libraries=["z"] + htslib_libraries,
     language="c",
-    extra_compile_args=["-Wno-error=declaration-after-statement"],
+    extra_compile_args=["-Wno-error=declaration-after-statement",
+                        "-DSAMTOOLS=1"],
     define_macros=[('_FILE_OFFSET_BITS', '64'),
                    ('_USE_KNETFILE', '')]
 )
@@ -297,8 +309,7 @@ htslib = Extension(
     shared_htslib_sources +
     os_c_files,
     library_dirs=htslib_library_dirs,
-    include_dirs=["htslib",
-                  "pysam"] + include_os,
+    include_dirs=["pysam"] + include_os + htslib_include_dirs,
     libraries=["z"] + htslib_libraries,
     language="c",
     extra_compile_args=["-Wno-error=declaration-after-statement",
@@ -320,7 +331,30 @@ samfile = Extension(
     htslib_sources +
     os_c_files,
     library_dirs=htslib_library_dirs,
-    include_dirs=["htslib", "pysam", "samtools"] + include_os,
+    include_dirs=["pysam", "samtools"] + include_os + htslib_include_dirs,
+    libraries=["z"] + htslib_libraries,
+    language="c",
+    extra_compile_args=[
+        "-Wno-error=declaration-after-statement",
+        "-DSAMTOOLS=1"],
+    define_macros=[('_FILE_OFFSET_BITS', '64'),
+                   ('_USE_KNETFILE', '')]
+)
+
+# alignmentfile requires functions defined in bam_md.c
+# for __advance_samtools method.
+# Selected ones have been copied into samfile_utils.c
+# Needs to be devolved somehow.
+alignmentfile = Extension(
+    "pysam.calignmentfile",
+    calignmentfile_sources +
+    ["pysam/%s" % x for x in (
+        "htslib_util.c", "samfile_util.c",)] +
+    ["samtools/kprobaln.c"] +
+    htslib_sources +
+    os_c_files,
+    library_dirs=htslib_library_dirs,
+    include_dirs=["pysam", "samtools"] + include_os + htslib_include_dirs,
     libraries=["z"] + htslib_libraries,
     language="c",
     extra_compile_args=[
@@ -337,7 +371,7 @@ tabix = Extension(
     htslib_sources +
     os_c_files,
     library_dirs=["pysam"] + htslib_library_dirs,
-    include_dirs=["htslib", "pysam"] + include_os,
+    include_dirs=["pysam"] + include_os + htslib_include_dirs,
     libraries=["z"] + htslib_libraries,
     language="c",
     extra_compile_args=["-Wno-error=declaration-after-statement",
@@ -352,7 +386,7 @@ faidx = Extension(
     htslib_sources +
     os_c_files,
     library_dirs=["pysam"],
-    include_dirs=["htslib", "pysam"] + include_os,
+    include_dirs=["pysam"] + include_os + htslib_include_dirs,
     libraries=["z"] + htslib_libraries,
     language="c",
     extra_compile_args=["-Wno-error=declaration-after-statement",
@@ -375,7 +409,7 @@ cvcf = Extension(
     "pysam.cvcf",
     cvcf_sources + os_c_files,
     library_dirs=[],
-    include_dirs=["htslib"] + include_os,
+    include_dirs=["htslib"] + include_os + htslib_include_dirs,
     libraries=["z"],
     language="c",
     extra_compile_args=["-Wno-error=declaration-after-statement"],
@@ -390,20 +424,24 @@ metadata = {
     'author_email': "andreas.heger at gmail.com",
     'license': "MIT",
     'platforms': "ALL",
-    'url': "http://code.google.com/p/pysam/",
+    'url': "https://github.com/pysam-developers/pysam",
     'packages': ['pysam',
                  'pysam.include',
                  'pysam.include.htslib',
                  'pysam.include.htslib.htslib',
                  'pysam.include.samtools',
-                 'pysam.include.samtools.bcftools',
+                 # 'pysam.include.samtools.bcftools',
                  'pysam.include.samtools.win32'],
     'requires': ['cython (>=0.20.1)'],
-    'ext_modules': [samtools, htslib, samfile,
-                    tabix, tabproxies, cvcf,
+    'ext_modules': [samtools,
+                    htslib,
+                    samfile,
+                    alignmentfile,
+                    tabix,
+                    tabproxies,
+                    cvcf,
                     faidx],
     'cmdclass': cmdclass,
-    'install_requires': ['cython>=0.20.1', ],
     'package_dir': {'pysam': 'pysam',
                     'pysam.include.htslib': 'htslib',
                     'pysam.include.samtools': 'samtools'},
diff --git a/tests/example.gtf.gz.tbi b/tests/example.gtf.gz.tbi
deleted file mode 100644
index 4e2b56f..0000000
Binary files a/tests/example.gtf.gz.tbi and /dev/null differ
diff --git a/tests/pysam_test.py b/tests/pysam_test.py
deleted file mode 100644
index ae43dcf..0000000
--- a/tests/pysam_test.py
+++ /dev/null
@@ -1,1885 +0,0 @@
-#!/usr/bin/env python
-'''unit testing code for pysam.
-
-Execute in the :file:`tests` directory as it requires the Makefile
-and data files located there.
-'''
-
-import pysam
-import unittest
-import os
-import sys
-import collections
-import subprocess
-import logging
-from TestUtils import checkBinaryEqual
-
-IS_PYTHON3 = sys.version_info[0] >= 3
-
-SAMTOOLS = "samtools"
-WORKDIR = "pysam_test_work"
-DATADIR = "pysam_data"
-
-
-class BasicTestBAMFetch(unittest.TestCase):
-
-    '''basic first test - detailed testing
-    if information in file is consistent
-    with information in AlignedRead object.'''
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(
-            os.path.join(DATADIR, "ex3.bam"),
-            "rb")
-        self.reads = list(self.samfile.fetch())
-
-    def testARqname(self):
-        self.assertEqual(
-            self.reads[0].qname,
-            "read_28833_29006_6945",
-            "read name mismatch in read 1: %s != %s" % (
-                self.reads[0].qname, "read_28833_29006_6945"))
-        self.assertEqual(
-            self.reads[1].qname,
-            "read_28701_28881_323b",
-            "read name mismatch in read 2: %s != %s" % (
-                self.reads[1].qname, "read_28701_28881_323b"))
-
-    def testARflag(self):
-        self.assertEqual(
-            self.reads[0].flag, 99,
-            "flag mismatch in read 1: %s != %s" % (
-                self.reads[0].flag, 99))
-        self.assertEqual(
-            self.reads[1].flag, 147,
-            "flag mismatch in read 2: %s != %s" % (
-                self.reads[1].flag, 147))
-
-    def testARrname(self):
-        self.assertEqual(
-            self.reads[0].rname, 0,
-            "chromosome/target id mismatch in read 1: %s != %s" %
-            (self.reads[0].rname, 0))
-        self.assertEqual(
-            self.reads[1].rname, 1,
-            "chromosome/target id mismatch in read 2: %s != %s" %
-            (self.reads[1].rname, 1))
-
-    def testARpos(self):
-        self.assertEqual(
-            self.reads[0].pos, 33 - 1,
-            "mapping position mismatch in read 1: %s != %s" %
-            (self.reads[0].pos, 33 - 1))
-        self.assertEqual(
-            self.reads[1].pos, 88 - 1,
-            "mapping position mismatch in read 2: %s != %s" %
-            (self.reads[1].pos, 88 - 1))
-
-    def testARmapq(self):
-        self.assertEqual(
-            self.reads[0].mapq, 20,
-            "mapping quality mismatch in read 1: %s != %s" %
-            (self.reads[0].mapq, 20))
-        self.assertEqual(
-            self.reads[1].mapq, 30,
-            "mapping quality mismatch in read 2: %s != %s" % (
-                self.reads[1].mapq, 30))
-
-    def testARcigar(self):
-        self.assertEqual(
-            self.reads[0].cigar,
-            [(0, 10), (2, 1), (0, 25)],
-            "read name length mismatch in read 1: %s != %s" %
-            (self.reads[0].cigar, [(0, 10), (2, 1), (0, 25)]))
-        self.assertEqual(
-            self.reads[1].cigar, [(0, 35)],
-            "read name length mismatch in read 2: %s != %s" %
-            (self.reads[1].cigar, [(0, 35)]))
-
-    def testARcigarstring(self):
-        self.assertEqual(self.reads[0].cigarstring, '10M1D25M')
-        self.assertEqual(self.reads[1].cigarstring, '35M')
-
-    def testARmrnm(self):
-        self.assertEqual(
-            self.reads[0].mrnm, 0,
-            "mate reference sequence name mismatch in read 1: %s != %s" %
-            (self.reads[0].mrnm, 0))
-        self.assertEqual(
-            self.reads[1].mrnm, 1,
-            "mate reference sequence name mismatch in read 2: %s != %s" %
-            (self.reads[1].mrnm, 1))
-        self.assertEqual(
-            self.reads[0].rnext, 0,
-            "mate reference sequence name mismatch in read 1: %s != %s" %
-            (self.reads[0].rnext, 0))
-        self.assertEqual(
-            self.reads[1].rnext, 1,
-            "mate reference sequence name mismatch in read 2: %s != %s" %
-            (self.reads[1].rnext, 1))
-
-    def testARmpos(self):
-        self.assertEqual(self.reads[
-                         0].mpos, 200 - 1, "mate mapping position mismatch in read 1: %s != %s" % (self.reads[0].mpos, 200 - 1))
-        self.assertEqual(self.reads[
-                         1].mpos, 500 - 1, "mate mapping position mismatch in read 2: %s != %s" % (self.reads[1].mpos, 500 - 1))
-        self.assertEqual(self.reads[
-                         0].pnext, 200 - 1, "mate mapping position mismatch in read 1: %s != %s" % (self.reads[0].pnext, 200 - 1))
-        self.assertEqual(self.reads[
-                         1].pnext, 500 - 1, "mate mapping position mismatch in read 2: %s != %s" % (self.reads[1].pnext, 500 - 1))
-
-    def testARisize(self):
-        self.assertEqual(self.reads[0].isize, 167, "insert size mismatch in read 1: %s != %s" % (
-            self.reads[0].isize, 167))
-        self.assertEqual(self.reads[1].isize, 412, "insert size mismatch in read 2: %s != %s" % (
-            self.reads[1].isize, 412))
-        self.assertEqual(self.reads[0].tlen, 167, "insert size mismatch in read 1: %s != %s" % (
-            self.reads[0].tlen, 167))
-        self.assertEqual(self.reads[1].tlen, 412, "insert size mismatch in read 2: %s != %s" % (
-            self.reads[1].tlen, 412))
-
-    def testARseq(self):
-        self.assertEqual(self.reads[0].seq, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG", "sequence mismatch in read 1: %s != %s" % (
-            self.reads[0].seq, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG"))
-        self.assertEqual(self.reads[1].seq, b"ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA", "sequence size mismatch in read 2: %s != %s" % (
-            self.reads[1].seq, b"ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA"))
-        self.assertEqual(self.reads[3].seq, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG", "sequence mismatch in read 4: %s != %s" % (
-            self.reads[3].seq, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG"))
-
-    def testARqual(self):
-        self.assertEqual(self.reads[0].qual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<",
-                         "quality string mismatch in read 1: %s != %s" % (self.reads[0].qual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<"))
-        self.assertEqual(self.reads[1].qual, b"<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<", "quality string mismatch in read 2: %s != %s" % (
-            self.reads[1].qual, b"<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<"))
-        self.assertEqual(self.reads[3].qual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<",
-                         "quality string mismatch in read 3: %s != %s" % (self.reads[3].qual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<"))
-
-    def testARquery(self):
-        self.assertEqual(self.reads[0].query, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG", "query mismatch in read 1: %s != %s" % (
-            self.reads[0].query, b"AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG"))
-        self.assertEqual(self.reads[1].query, b"ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA", "query size mismatch in read 2: %s != %s" % (
-            self.reads[1].query, b"ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA"))
-        self.assertEqual(self.reads[3].query, b"TAGCTAGCTACCTATATCTTGGTCTT", "query mismatch in read 4: %s != %s" % (
-            self.reads[3].query, b"TAGCTAGCTACCTATATCTTGGTCTT"))
-
-    def testARqqual(self):
-        self.assertEqual(self.reads[0].qqual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<",
-                         "qquality string mismatch in read 1: %s != %s" % (self.reads[0].qqual, b"<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<"))
-        self.assertEqual(self.reads[1].qqual, b"<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<", "qquality string mismatch in read 2: %s != %s" % (
-            self.reads[1].qqual, b"<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<"))
-        self.assertEqual(self.reads[3].qqual, b"<<<<<<<<<<<<<<<<<:<9/,&,22",
-                         "qquality string mismatch in read 3: %s != %s" % (self.reads[3].qqual, b"<<<<<<<<<<<<<<<<<:<9/,&,22"))
-
-    def testPresentOptionalFields(self):
-        self.assertEqual(self.reads[0].opt(
-            'NM'), 1, "optional field mismatch in read 1, NM: %s != %s" % (self.reads[0].opt('NM'), 1))
-        self.assertEqual(self.reads[0].opt(
-            'RG'), 'L1', "optional field mismatch in read 1, RG: %s != %s" % (self.reads[0].opt('RG'), 'L1'))
-        self.assertEqual(self.reads[1].opt(
-            'RG'), 'L2', "optional field mismatch in read 2, RG: %s != %s" % (self.reads[1].opt('RG'), 'L2'))
-        self.assertEqual(self.reads[1].opt(
-            'MF'), 18, "optional field mismatch in read 2, MF: %s != %s" % (self.reads[1].opt('MF'), 18))
-
-    def testPairedBools(self):
-        self.assertEqual(self.reads[0].is_paired, True, "is paired mismatch in read 1: %s != %s" % (
-            self.reads[0].is_paired, True))
-        self.assertEqual(self.reads[1].is_paired, True, "is paired mismatch in read 2: %s != %s" % (
-            self.reads[1].is_paired, True))
-        self.assertEqual(self.reads[0].is_proper_pair, True, "is proper pair mismatch in read 1: %s != %s" % (
-            self.reads[0].is_proper_pair, True))
-        self.assertEqual(self.reads[1].is_proper_pair, True, "is proper pair mismatch in read 2: %s != %s" % (
-            self.reads[1].is_proper_pair, True))
-
-    def testTags(self):
-        self.assertEqual(self.reads[0].tags,
-                         [('NM', 1), ('RG', 'L1'),
-                          ('PG', 'P1'), ('XT', 'U')])
-        self.assertEqual(self.reads[1].tags,
-                         [('MF', 18), ('RG', 'L2'),
-                          ('PG', 'P2'), ('XT', 'R')])
-
-    def testAddTags(self):
-        self.assertEqual(sorted(self.reads[0].tags),
-                         sorted([('NM', 1), ('RG', 'L1'),
-                                 ('PG', 'P1'), ('XT', 'U')]))
-
-        self.reads[0].setTag('X1', 'C')
-        self.assertEqual(sorted(self.reads[0].tags),
-                         sorted([('X1', 'C'), ('NM', 1), ('RG', 'L1'),
-                                 ('PG', 'P1'), ('XT', 'U'), ]))
-        self.reads[0].setTag('X2', 5)
-        self.assertEqual(sorted(self.reads[0].tags),
-                         sorted([('X2', 5), ('X1', 'C'),
-                                 ('NM', 1), ('RG', 'L1'),
-                                 ('PG', 'P1'), ('XT', 'U'), ]))
-        # add with replacement
-        self.reads[0].setTag('X2', 10)
-        self.assertEqual(sorted(self.reads[0].tags),
-                         sorted([('X2', 10), ('X1', 'C'),
-                                 ('NM', 1), ('RG', 'L1'),
-                                 ('PG', 'P1'), ('XT', 'U'), ]))
-
-        # add without replacement
-        self.reads[0].setTag('X2', 5, replace=False)
-        self.assertEqual(sorted(self.reads[0].tags),
-                         sorted([('X2', 10), ('X1', 'C'),
-                                 ('X2', 5),
-                                 ('NM', 1), ('RG', 'L1'),
-                                 ('PG', 'P1'), ('XT', 'U'), ]))
-
-    def testAddTagsType(self):
-        self.reads[0].tags = None
-        self.assertEqual(self.reads[0].tags, [])
-
-        self.reads[0].setTag('X1', 5.0)
-        self.reads[0].setTag('X2', "5.0")
-        self.reads[0].setTag('X3', 5)
-
-        self.assertEqual(sorted(self.reads[0].tags),
-                         sorted([('X1', 5.0),
-                                 ('X2', "5.0"),
-                                 ('X3', 5)]))
-
-        # test setting float for int value
-        self.reads[0].setTag('X4', 5, value_type='d')
-        self.assertEqual(sorted(self.reads[0].tags),
-                         sorted([('X1', 5.0),
-                                 ('X2', "5.0"),
-                                 ('X3', 5),
-                                 ('X4', 5.0)]))
-
-        # test setting int for float value - the
-        # value will be rounded.
-        self.reads[0].setTag('X5', 5.2, value_type='i')
-        self.assertEqual(sorted(self.reads[0].tags),
-                         sorted([('X1', 5.0),
-                                 ('X2', "5.0"),
-                                 ('X3', 5),
-                                 ('X4', 5.0),
-                                 ('X5', 5)]))
-
-        # test setting invalid type code
-        self.assertRaises(ValueError, self.reads[0].setTag, 'X6', 5.2, 'g')
-
-    def testTagsUpdatingFloat(self):
-        self.assertEqual(self.reads[0].tags,
-                         [('NM', 1), ('RG', 'L1'),
-                          ('PG', 'P1'), ('XT', 'U')])
-        self.reads[0].tags += [('XC', 5.0)]
-        self.assertEqual(self.reads[0].tags,
-                         [('NM', 1), ('RG', 'L1'),
-                          ('PG', 'P1'), ('XT', 'U'), ('XC', 5.0)])
-
-    def testOpt(self):
-        self.assertEqual(self.reads[0].opt("XT"), "U")
-        self.assertEqual(self.reads[1].opt("XT"), "R")
-
-    def testMissingOpt(self):
-        self.assertRaises(KeyError, self.reads[0].opt, "XP")
-
-    def testEmptyOpt(self):
-        self.assertRaises(KeyError, self.reads[2].opt, "XT")
-
-    def tearDown(self):
-        self.samfile.close()
-
-
-class BasicTestBAMFile(BasicTestBAMFetch):
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(
-            os.path.join(DATADIR, "ex3.sam"),
-            "r")
-        self.reads = [r for r in self.samfile]
-
-
-class BasicTestSAMFile(BasicTestBAMFetch):
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(
-            os.path.join(DATADIR, "ex3.sam"),
-            "r")
-        self.reads = [r for r in self.samfile]
-
-
-class BasicTestSAMFetch(BasicTestBAMFetch):
-    def setUp(self):
-        self.samfile = pysam.Samfile(
-            os.path.join(DATADIR, "ex3.sam"),
-            "r")
-        self.reads = list(self.samfile.fetch())
-
-
-# needs to be implemented
-# class TestAlignedReadFromSamWithoutHeader(TestAlignedReadFromBam):
-#
-#     def setUp(self):
-#         self.samfile=pysam.Samfile( "ex7.sam","r" )
-#         self.reads=list(self.samfile.fetch())
-
-
-class TestIO(unittest.TestCase):
-
-    '''check if reading samfile and writing a samfile are consistent.'''
-
-    def checkEcho(self,
-                  input_filename,
-                  reference_filename,
-                  output_filename,
-                  input_mode, output_mode,
-                  use_template=True):
-        '''iterate through *input_filename* writing to *output_filename* and
-        comparing the output to *reference_filename*.
-
-        The files are opened according to the *input_mode* and *output_mode*.
-
-        If *use_template* is set, the header is copied from infile
-        using the template mechanism, otherwise target names and
-        lengths are passed explicitely.
-
-        '''
-
-        infile = pysam.Samfile(os.path.join(DATADIR, input_filename),
-                               input_mode)
-        if use_template:
-            outfile = pysam.Samfile(output_filename,
-                                    output_mode,
-                                    template=infile)
-        else:
-            outfile = pysam.Samfile(output_filename,
-                                    output_mode,
-                                    referencenames=infile.references,
-                                    referencelengths=infile.lengths,
-                                    add_sq_text=False)
-
-        iter = infile.fetch()
-
-        for x in iter:
-            outfile.write(x)
-        infile.close()
-        outfile.close()
-
-        self.assertTrue(
-            checkBinaryEqual(os.path.join(DATADIR, reference_filename),
-                             output_filename),
-            "files %s and %s are not the same" % (reference_filename,
-                                                  output_filename))
-
-    def testReadWriteBam(self):
-
-        input_filename = "ex1.bam"
-        output_filename = "pysam_ex1.bam"
-        reference_filename = "ex1.bam"
-
-        self.checkEcho(input_filename, reference_filename, output_filename,
-                       "rb", "wb", use_template=True)
-
-    def testReadWriteBamWithTargetNames(self):
-
-        input_filename = "ex1.bam"
-        output_filename = "pysam_ex1.bam"
-        reference_filename = "ex1.bam"
-
-        self.checkEcho(input_filename, reference_filename, output_filename,
-                       "rb", "wb", use_template=False)
-
-    def testReadWriteSamWithHeader(self):
-
-        input_filename = "ex2.sam"
-        output_filename = "pysam_ex2.sam"
-        reference_filename = "ex2.sam"
-
-        self.checkEcho(input_filename,
-                       reference_filename,
-                       output_filename,
-                       "r", "wh")
-
-    # Release 0.8.0
-    # no samfiles without header
-    def testReadWriteSamWithoutHeader(self):
-
-        input_filename = "ex2.sam"
-        output_filename = "pysam_ex2.sam"
-        reference_filename = "ex1.sam"
-
-        self.checkEcho(input_filename,
-                       reference_filename,
-                       output_filename,
-                       "r", "w")
-
-    def testReadSamWithoutTargetNames(self):
-        '''see issue 104.'''
-        input_filename = os.path.join(DATADIR,
-                                      "example_unmapped_reads_no_sq.sam")
-
-        # raise exception in default mode
-        self.assertRaises(ValueError, pysam.Samfile, input_filename, "r")
-
-        # raise exception if no SQ files
-        self.assertRaises(ValueError, pysam.Samfile,
-                          input_filename, "r",
-                          check_header=True)
-
-        infile = pysam.Samfile(
-            input_filename,
-            check_header=False,
-            check_sq=False)
-        
-        # TODO
-        # result = list(infile.fetch(until_eof=True))
-        # self.assertEqual(2, len(result))
-
-    def testReadBamWithoutTargetNames(self):
-        '''see issue 104.'''
-        input_filename = os.path.join(
-            DATADIR, "example_unmapped_reads_no_sq.bam")
-
-        # raise exception in default mode
-        self.assertRaises(ValueError, pysam.Samfile, input_filename, "r")
-
-        # raise exception if no SQ files
-        self.assertRaises(ValueError, pysam.Samfile, input_filename, "r",
-                          check_header=True)
-
-        infile = pysam.Samfile(
-            input_filename, check_header=False, check_sq=False)
-        result = list(infile.fetch(until_eof=True))
-
-    # TODO
-    def testReadSamWithoutHeader(self):
-        input_filename = os.path.join(DATADIR, "ex1.sam")
-
-        # reading from a samfile without header is not 
-        # implemented
-        self.assertRaises(ValueError,
-                          pysam.Samfile,
-                          input_filename,
-                          "r")
-
-        # TODO
-        # without check_header header is no read
-        # leading to segfault
-        # self.assertRaises(ValueError,
-        #                   pysam.Samfile,
-        #                   input_filename,
-        #                   "r",
-        #                   check_header=False)
-
-    # TODO
-    # def testReadUnformattedFile(self):
-    #     '''test reading from a file that is not bam/sam formatted'''
-    #     input_filename = os.path.join(DATADIR, 'Makefile')
-
-    #     # bam - file raise error
-    #     self.assertRaises(ValueError,
-    #                       pysam.Samfile,
-    #                       input_filename,
-    #                       "rb")
-
-    #     # sam - file error, but can't fetch
-    #     self.assertRaises(ValueError,
-    #                       pysam.Samfile,
-    #                       input_filename,
-    #                       "r")
-
-    #     self.assertRaises(ValueError,
-    #                       pysam.Samfile,
-    #                       input_filename,
-    #                       "r",
-    #                       check_header=False)
-
-    def testBAMWithoutAlignedReads(self):
-        '''see issue 117'''
-        input_filename = os.path.join(DATADIR, "test_unaligned.bam")
-        samfile = pysam.Samfile(input_filename, "rb", check_sq=False)
-        samfile.fetch(until_eof=True)
-
-    def testBAMWithShortBAI(self):
-        '''see issue 116'''
-        input_filename = os.path.join(DATADIR, "example_bai.bam")
-        samfile = pysam.Samfile(input_filename, "rb", check_sq=False)
-        samfile.fetch('chr2')
-
-    def testFetchFromClosedFile(self):
-
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                "rb")
-        samfile.close()
-        self.assertRaises(ValueError, samfile.fetch, 'chr1', 100, 120)
-
-    def testClosedFile(self):
-        '''test that access to a closed samfile raises ValueError.'''
-
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                "rb")
-        samfile.close()
-        self.assertRaises(ValueError, samfile.fetch, 'chr1', 100, 120)
-        self.assertRaises(ValueError, samfile.pileup, 'chr1', 100, 120)
-        self.assertRaises(ValueError, samfile.getrname, 0)
-        # TODO
-        self.assertRaises(ValueError, samfile.tell)
-        self.assertRaises(ValueError, samfile.seek, 0)
-        self.assertRaises(ValueError, getattr, samfile, "nreferences")
-        self.assertRaises(ValueError, getattr, samfile, "references")
-        self.assertRaises(ValueError, getattr, samfile, "lengths")
-        self.assertRaises(ValueError, getattr, samfile, "text")
-        self.assertRaises(ValueError, getattr, samfile, "header")
-
-        # write on closed file
-        self.assertEqual(0, samfile.write(None))
-
-    def testAutoDetection(self):
-        '''test if autodetection works.'''
-
-        # TODO
-        # samfile = pysam.Samfile(os.path.join(DATADIR, "ex3.sam"))
-        # self.assertRaises(ValueError, samfile.fetch, 'chr1')
-        # samfile.close()
-
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex3.bam"))
-        samfile.fetch('chr1')
-        samfile.close()
-
-    # TOOD
-    # def testReadingFromSamFileWithoutHeader(self):
-    #     '''read from samfile without header.
-    #     '''
-    #     samfile = pysam.Samfile(os.path.join(DATADIR, "ex7.sam"),
-    #                             check_header=False,
-    #                             check_sq=False)
-    #     self.assertRaises(NotImplementedError, samfile.__iter__)
-
-    def testReadingFromFileWithoutIndex(self):
-        '''read from bam file without index.'''
-
-        assert not os.path.exists(os.path.join(DATADIR, "ex2.bam.bai"))
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex2.bam"),
-                                "rb")
-        self.assertRaises(ValueError, samfile.fetch)
-        self.assertEqual(len(list(samfile.fetch(until_eof=True))),
-                         3270)
-
-    # def testReadingUniversalFileMode(self):
-    #     '''read from samfile without header.
-    #     '''
-
-    #     input_filename = "ex2.sam"
-    #     output_filename = "pysam_ex2.sam"
-    #     reference_filename = "ex1.sam"
-
-    #     self.checkEcho(input_filename,
-    #                    reference_filename,
-    #                    output_filename,
-    #                    "rU", "w")
-
-    def testHead(self):
-        '''test IteratorRowHead'''
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                "rb")
-        l10 = list(samfile.head(10))
-        l100 = list(samfile.head(100))
-        self.assertEqual(len(l10), 10)
-        self.assertEqual(len(l100), 100)
-        self.assertEqual(list(map(str, l10)),
-                         list(map(str, l100[:10])))
-
-
-class TestFloatTagBug(unittest.TestCase):
-
-    '''see issue 71'''
-
-    def testFloatTagBug(self):
-        '''a float tag before another exposed a parsing bug in bam_aux_get.
-
-        Fixed in 0.1.19
-        '''
-        samfile = pysam.Samfile(os.path.join(DATADIR, "tag_bug.bam"))
-        read = next(samfile.fetch(until_eof=True))
-        self.assertTrue(('XC', 1) in read.tags)
-        self.assertEqual(read.opt('XC'), 1)
-
-
-class TestLargeFieldBug(unittest.TestCase):
-
-    '''see issue 100'''
-
-    def testLargeFileBug(self):
-        '''when creating a read with a large entry in the tag field
-        causes an errror:
-            NotImplementedError: tags field too large
-        '''
-        samfile = pysam.Samfile(os.path.join(DATADIR, "issue100.bam"))
-        read = next(samfile.fetch(until_eof=True))
-        new_read = pysam.AlignedRead()
-        new_read.tags = read.tags
-        self.assertEqual(new_read.tags, read.tags)
-
-
-class TestTagParsing(unittest.TestCase):
-
-    '''tests checking the accuracy of tag setting and retrieval.'''
-
-    def makeRead(self):
-        a = pysam.AlignedRead()
-        a.qname = "read_12345"
-        a.tid = 0
-        a.seq = "ACGT" * 3
-        a.flag = 0
-        a.rname = 0
-        a.pos = 1
-        a.mapq = 20
-        a.cigar = ((0, 10), (2, 1), (0, 25))
-        a.mrnm = 0
-        a.mpos = 200
-        a.isize = 0
-        a.qual = "1234" * 3
-        # todo: create tags
-        return a
-
-    def testNegativeIntegers(self):
-        x = -2
-        aligned_read = self.makeRead()
-        aligned_read.tags = [("XD", int(x))]
-        # print (aligned_read.tags)
-
-    def testNegativeIntegers2(self):
-        x = -2
-        r = self.makeRead()
-        r.tags = [("XD", int(x))]
-        outfile = pysam.Samfile("test.bam",
-                                "wb",
-                                referencenames=("chr1",),
-                                referencelengths = (1000,))
-        outfile.write(r)
-        outfile.close()
-
-    def testCigarString(self):
-        r = self.makeRead()
-        self.assertEqual(r.cigarstring, "10M1D25M")
-        r.cigarstring = "20M10D20M"
-        self.assertEqual(r.cigar, [(0, 20), (2, 10), (0, 20)])
-        # unsetting cigar string
-        r.cigarstring = None
-        self.assertEqual(r.cigarstring, '')
-
-    def testCigar(self):
-        r = self.makeRead()
-        self.assertEqual(r.cigar, [(0, 10), (2, 1), (0, 25)])
-        # unsetting cigar string
-        r.cigar = None
-        self.assertEqual(r.cigar, [])
-
-    def testLongTags(self):
-        '''see issue 115'''
-
-        r = self.makeRead()
-        rg = 'HS2000-899_199.L3'
-        tags = [('XC', 85), ('XT', 'M'), ('NM', 5),
-                ('SM', 29), ('AM', 29), ('XM', 1),
-                ('XO', 1), ('XG', 4), ('MD', '37^ACCC29T18'),
-                ('XA', '5,+11707,36M1I48M,2;21,-48119779,46M1I38M,2;hs37d5,-10060835,40M1D45M,3;5,+11508,36M1I48M,3;hs37d5,+6743812,36M1I48M,3;19,-59118894,46M1I38M,3;4,-191044002,6M1I78M,3;')]
-
-        r.tags = tags
-        r.tags += [("RG", rg)] * 100
-        tags += [("RG", rg)] * 100
-
-        self.assertEqual(tags, r.tags)
-
-
-class TestClipping(unittest.TestCase):
-
-    def testClipping(self):
-
-        self.samfile = pysam.Samfile(os.path.join(DATADIR, "softclip.bam"),
-                                     "rb")
-        for read in self.samfile:
-
-            if read.qname == "r001":
-                self.assertEqual(read.seq, b'AAAAGATAAGGATA')
-                self.assertEqual(read.query, b'AGATAAGGATA')
-                self.assertEqual(read.qual, None)
-                self.assertEqual(read.qqual, None)
-
-            elif read.qname == "r002":
-
-                self.assertEqual(read.seq, b'GCCTAAGCTAA')
-                self.assertEqual(read.query, b'AGCTAA')
-                self.assertEqual(read.qual, b'01234567890')
-                self.assertEqual(read.qqual, b'567890')
-
-            elif read.qname == "r003":
-
-                self.assertEqual(read.seq, b'GCCTAAGCTAA')
-                self.assertEqual(read.query, b'GCCTAA')
-                self.assertEqual(read.qual, b'01234567890')
-                self.assertEqual(read.qqual, b'012345')
-
-            elif read.qname == "r004":
-
-                self.assertEqual(read.seq, b'TAGGC')
-                self.assertEqual(read.query, b'TAGGC')
-                self.assertEqual(read.qual, b'01234')
-                self.assertEqual(read.qqual, b'01234')
-
-
-class TestIteratorRow(unittest.TestCase):
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                     "rb")
-
-    def checkRange(self, rnge):
-        '''compare results from iterator with those from samtools.'''
-        ps = list(self.samfile.fetch(region=rnge))
-        sa = list(pysam.view(os.path.join(DATADIR, "ex1.bam"),
-                             rnge,
-                             raw=True))
-        self.assertEqual(len(ps), len(
-            sa), "unequal number of results for range %s: %i != %i" % (rnge, len(ps), len(sa)))
-        # check if the same reads are returned and in the same order
-        for line, (a, b) in enumerate(list(zip(ps, sa))):
-            d = b.split("\t")
-            self.assertEqual(
-                a.qname, d[0], "line %i: read id mismatch: %s != %s" % (line, a.rname, d[0]))
-            self.assertEqual(a.pos, int(d[3]) - 1, "line %i: read position mismatch: %s != %s, \n%s\n%s\n" %
-                             (line, a.pos, int(d[3]) - 1,
-                              str(a), str(d)))
-            if sys.version_info[0] < 3:
-                qual = d[10]
-            else:
-                qual = d[10].encode('ascii')
-            self.assertEqual(a.qual, qual, "line %i: quality mismatch: %s != %s, \n%s\n%s\n" %
-                             (line, a.qual, qual,
-                              str(a), str(d)))
-
-    def testIteratePerContig(self):
-        '''check random access per contig'''
-        for contig in self.samfile.references:
-            self.checkRange(contig)
-
-    def testIterateRanges(self):
-        '''check random access per range'''
-        for contig, length in zip(self.samfile.references, self.samfile.lengths):
-            for start in range(1, length, 90):
-                # this includes empty ranges
-                self.checkRange("%s:%i-%i" % (contig, start, start + 90))
-
-    def tearDown(self):
-        self.samfile.close()
-
-
-class TestIteratorRowAll(unittest.TestCase):
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                     "rb")
-
-    def testIterate(self):
-        '''compare results from iterator with those from samtools.'''
-        ps = list(self.samfile.fetch())
-        sa = list(pysam.view(os.path.join(DATADIR, "ex1.bam"),
-                             raw=True))
-        self.assertEqual(
-            len(ps), len(sa), "unequal number of results: %i != %i" % (len(ps), len(sa)))
-        # check if the same reads are returned
-        for line, pair in enumerate(list(zip(ps, sa))):
-            data = pair[1].split("\t")
-            self.assertEqual(pair[0].qname, data[
-                             0], "read id mismatch in line %i: %s != %s" % (line, pair[0].rname, data[0]))
-
-    def tearDown(self):
-        self.samfile.close()
-
-
-class TestIteratorColumn(unittest.TestCase):
-
-    '''test iterator column against contents of ex4.bam.'''
-
-    # note that samfile contains 1-based coordinates
-    # 1D means deletion with respect to reference sequence
-    #
-    mCoverages = {'chr1': [0] * 20 + [1] * 36 + [0] * (100 - 20 - 35),
-                  'chr2': [0] * 20 + [1] * 35 + [0] * (100 - 20 - 35),
-                  }
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(os.path.join(DATADIR, "ex4.bam"),
-                                     "rb")
-
-    def checkRange(self, contig, start=None, end=None, truncate=False):
-        '''compare results from iterator with those from samtools.'''
-        # check if the same reads are returned and in the same order
-        for column in self.samfile.pileup(contig, start, end, truncate=truncate):
-            if truncate:
-                self.assertGreaterEqual(column.pos, start)
-                self.assertLess(column.pos, end)
-            thiscov = len(column.pileups)
-            refcov = self.mCoverages[
-                self.samfile.getrname(column.tid)][column.pos]
-            self.assertEqual(thiscov, refcov, "wrong coverage at pos %s:%i %i should be %i" % (
-                self.samfile.getrname(column.tid), column.pos, thiscov, refcov))
-
-    def testIterateAll(self):
-        '''check random access per contig'''
-        self.checkRange(None)
-
-    def testIteratePerContig(self):
-        '''check random access per contig'''
-        for contig in self.samfile.references:
-            self.checkRange(contig)
-
-    def testIterateRanges(self):
-        '''check random access per range'''
-        for contig, length in zip(self.samfile.references, self.samfile.lengths):
-            for start in range(1, length, 90):
-                # this includes empty ranges
-                self.checkRange(contig, start, start + 90)
-
-    def testInverse(self):
-        '''test the inverse, is point-wise pileup accurate.'''
-        for contig, refseq in list(self.mCoverages.items()):
-            refcolumns = sum(refseq)
-            for pos, refcov in enumerate(refseq):
-                columns = list(self.samfile.pileup(contig, pos, pos + 1))
-                if refcov == 0:
-                    # if no read, no coverage
-                    self.assertEqual(
-                        len(columns),
-                        refcov,
-                        "wrong number of pileup columns returned for position %s:%i, %i should be %i" % (
-                            contig, pos,
-                            len(columns), refcov))
-                elif refcov == 1:
-                    # one read, all columns of the read are returned
-                    self.assertEqual(
-                        len(columns),
-                        refcolumns,
-                        "pileup incomplete at position %i: got %i, expected %i " %
-                        (pos, len(columns), refcolumns))
-
-    def testIterateTruncate(self):
-        '''check random access per range'''
-        for contig, length in zip(self.samfile.references, self.samfile.lengths):
-            for start in range(1, length, 90):
-                # this includes empty ranges
-                self.checkRange(contig, start, start + 90, truncate=True)
-
-    def tearDown(self):
-        self.samfile.close()
-
-
-class TestIteratorColumn2(unittest.TestCase):
-
-    '''test iterator column against contents of ex1.bam.'''
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                     "rb")
-
-    def testStart(self):
-        # print self.samfile.fetch().next().pos
-        # print self.samfile.pileup().next().pos
-        pass
-
-    def testTruncate(self):
-        '''see issue 107.'''
-        # note that ranges in regions start from 1
-        p = self.samfile.pileup(region='chr1:170:172', truncate=True)
-        columns = [x.pos for x in p]
-        self.assertEqual(len(columns), 3)
-        self.assertEqual(columns, [169, 170, 171])
-
-        p = self.samfile.pileup('chr1', 169, 172, truncate=True)
-        columns = [x.pos for x in p]
-
-        self.assertEqual(len(columns), 3)
-        self.assertEqual(columns, [169, 170, 171])
-
-    def testAccessOnClosedIterator(self):
-        '''see issue 131
-
-        Accessing pileup data after iterator has closed.
-        '''
-        pcolumn = self.samfile.pileup('chr1', 170, 180).__next__()
-        self.assertRaises(ValueError, getattr, pcolumn, "pileups")
-
-
-class TestHeaderSam(unittest.TestCase):
-
-    header = {'SQ': [{'LN': 1575, 'SN': 'chr1'},
-                     {'LN': 1584, 'SN': 'chr2'}],
-              'RG': [{'LB': 'SC_1', 'ID': 'L1', 'SM': 'NA12891', 'PU': 'SC_1_10', "CN": "name:with:colon"},
-                     {'LB': 'SC_2', 'ID': 'L2', 'SM': 'NA12891', 'PU': 'SC_2_12', "CN": "name:with:colon"}],
-              'PG': [{'ID': 'P1', 'VN': '1.0'}, {'ID': 'P2', 'VN': '1.1'}],
-              'HD': {'VN': '1.0'},
-              'CO': ['this is a comment', 'this is another comment'],
-              }
-
-    def compareHeaders(self, a, b):
-        '''compare two headers a and b.'''
-        for ak, av in a.items():
-            self.assertTrue(ak in b, "key '%s' not in '%s' " % (ak, b))
-            self.assertEqual(av, b[ak])
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(os.path.join(DATADIR, "ex3.sam"),
-                                     "r")
-
-    def testHeaders(self):
-        self.compareHeaders(self.header, self.samfile.header)
-        self.compareHeaders(self.samfile.header, self.header)
-
-    def testNameMapping(self):
-        for x, y in enumerate(("chr1", "chr2")):
-            tid = self.samfile.gettid(y)
-            ref = self.samfile.getrname(x)
-            self.assertEqual(tid, x)
-            self.assertEqual(ref, y)
-
-        self.assertEqual(self.samfile.gettid("chr?"), -1)
-        self.assertRaises(ValueError, self.samfile.getrname, 2)
-
-    def tearDown(self):
-        self.samfile.close()
-
-
-class TestHeaderBam(TestHeaderSam):
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(os.path.join(DATADIR, "ex3.bam"),
-                                     "rb")
-
-
-class TestHeaderFromRefs(unittest.TestCase):
-
-    '''see issue 144
-
-    reference names need to be converted to string for python 3
-    '''
-
-    # def testHeader( self ):
-    #     refs = ['chr1', 'chr2']
-    #     tmpfile = "tmp_%i" % id(self)
-    #     s = pysam.Samfile(tmpfile, 'wb',
-    #                       referencenames=refs,
-    #                       referencelengths=[100]*len(refs))
-    #     s.close()
-
-    #     self.assertTrue( checkBinaryEqual( 'issue144.bam', tmpfile ),
-    #                      'bam files differ')
-    #     os.unlink( tmpfile )
-
-
-class TestHeader1000Genomes(unittest.TestCase):
-    '''see issue 110'''
-    # bamfile = "http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/technical/phase2b_alignment/data/NA07048/exome_alignment/NA07048.unmapped.ILLUMINA.bwa.CEU.exome.20120522_p2b.bam"
-    bamfile = "http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/technical/phase3_EX_or_LC_only_alignment/data/HG00104/alignment/HG00104.chrom11.ILLUMINA.bwa.GBR.low_coverage.20130415.bam"
-
-    def testRead(self):
-
-        f = pysam.Samfile(self.bamfile, "rb")
-        data = f.header.copy()
-        self.assertTrue(data)
-
-
-class TestUnmappedReads(unittest.TestCase):
-
-    # TODO
-    # def testSAM(self):
-    #     samfile = pysam.Samfile(os.path.join(DATADIR, "ex5.sam"),
-    #                             "r")
-    #     self.assertEqual(len(list(samfile.fetch(until_eof=True))), 2)
-    #     samfile.close()
-
-    def testBAM(self):
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex5.bam"),
-                                "rb")
-        self.assertEqual(len(list(samfile.fetch(until_eof=True))), 2)
-        samfile.close()
-
-
-class TestPileupObjects(unittest.TestCase):
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                     "rb")
-
-    def testPileupColumn(self):
-        for pcolumn1 in self.samfile.pileup(region="chr1:105"):
-            if pcolumn1.pos == 104:
-                self.assertEqual(
-                    pcolumn1.tid, 0, "chromosome/target id mismatch in position 1: %s != %s" % (pcolumn1.tid, 0))
-                self.assertEqual(
-                    pcolumn1.pos, 105 - 1, "position mismatch in position 1: %s != %s" % (pcolumn1.pos, 105 - 1))
-                self.assertEqual(
-                    pcolumn1.n, 2, "# reads mismatch in position 1: %s != %s" % (pcolumn1.n, 2))
-        for pcolumn2 in self.samfile.pileup(region="chr2:1480"):
-            if pcolumn2.pos == 1479:
-                self.assertEqual(
-                    pcolumn2.tid, 1, "chromosome/target id mismatch in position 1: %s != %s" % (pcolumn2.tid, 1))
-                self.assertEqual(
-                    pcolumn2.pos, 1480 - 1, "position mismatch in position 1: %s != %s" % (pcolumn2.pos, 1480 - 1))
-                self.assertEqual(
-                    pcolumn2.n, 12, "# reads mismatch in position 1: %s != %s" % (pcolumn2.n, 12))
-
-    def testPileupRead(self):
-        for pcolumn1 in self.samfile.pileup(region="chr1:105"):
-            if pcolumn1.pos == 104:
-                self.assertEqual(
-                    len(pcolumn1.pileups), 2,
-                    "# reads aligned to column mismatch in position 1"
-                    ": %s != %s" %
-                    (len(pcolumn1.pileups), 2))
-
-
-# self.assertEqual( pcolumn1.pileups[0]  # need to test additional
-# properties here
-
-    def tearDown(self):
-        self.samfile.close()
-
-    def testIteratorOutOfScope(self):
-        '''test if exception is raised if pileup col is accessed after
-        iterator is exhausted.'''
-
-        for pileupcol in self.samfile.pileup():
-            pass
-
-        self.assertRaises(ValueError, getattr, pileupcol, "pileups")
-
-
-class TestContextManager(unittest.TestCase):
-
-    def testManager(self):
-        with pysam.Samfile(os.path.join(DATADIR, 'ex1.bam'),
-                           'rb') as samfile:
-            samfile.fetch()
-        self.assertEqual(samfile._isOpen(), False)
-
-
-class TestExceptions(unittest.TestCase):
-
-    def setUp(self):
-        self.samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                     "rb")
-
-    def testMissingFile(self):
-
-        self.assertRaises(IOError, pysam.Samfile, "exdoesntexist.bam", "rb")
-        self.assertRaises(IOError, pysam.Samfile, "exdoesntexist.sam", "r")
-        self.assertRaises(IOError, pysam.Samfile, "exdoesntexist.bam", "r")
-        self.assertRaises(IOError, pysam.Samfile, "exdoesntexist.sam", "rb")
-
-    def testBadContig(self):
-        self.assertRaises(ValueError, self.samfile.fetch, "chr88")
-
-    def testMeaninglessCrap(self):
-        self.assertRaises(ValueError, self.samfile.fetch, "skljf")
-
-    def testBackwardsOrderNewFormat(self):
-        self.assertRaises(ValueError, self.samfile.fetch, 'chr1', 100, 10)
-
-    def testBackwardsOrderOldFormat(self):
-        self.assertRaises(ValueError, self.samfile.fetch, region="chr1:100-10")
-
-    def testOutOfRangeNegativeNewFormat(self):
-        self.assertRaises(ValueError, self.samfile.fetch, "chr1", 5, -10)
-        self.assertRaises(ValueError, self.samfile.fetch, "chr1", 5, 0)
-        self.assertRaises(ValueError, self.samfile.fetch, "chr1", -5, -10)
-
-        self.assertRaises(ValueError, self.samfile.count, "chr1", 5, -10)
-        self.assertRaises(ValueError, self.samfile.count, "chr1", 5, 0)
-        self.assertRaises(ValueError, self.samfile.count, "chr1", -5, -10)
-
-    def testOutOfRangeNegativeOldFormat(self):
-        self.assertRaises(ValueError, self.samfile.fetch, region="chr1:-5-10")
-        self.assertRaises(ValueError, self.samfile.fetch, region="chr1:-5-0")
-        self.assertRaises(ValueError, self.samfile.fetch, region="chr1:-5--10")
-
-        self.assertRaises(ValueError, self.samfile.count, region="chr1:-5-10")
-        self.assertRaises(ValueError, self.samfile.count, region="chr1:-5-0")
-        self.assertRaises(ValueError, self.samfile.count, region="chr1:-5--10")
-
-    def testOutOfRangNewFormat(self):
-        self.assertRaises(
-            ValueError, self.samfile.fetch, "chr1", 9999999999, 99999999999)
-        self.assertRaises(
-            ValueError, self.samfile.count, "chr1", 9999999999, 99999999999)
-
-    def testOutOfRangeLargeNewFormat(self):
-        self.assertRaises(ValueError, self.samfile.fetch, "chr1",
-                          9999999999999999999999999999999, 9999999999999999999999999999999999999999)
-        self.assertRaises(ValueError, self.samfile.count, "chr1",
-                          9999999999999999999999999999999, 9999999999999999999999999999999999999999)
-
-    def testOutOfRangeLargeOldFormat(self):
-        self.assertRaises(
-            ValueError, self.samfile.fetch, "chr1:99999999999999999-999999999999999999")
-        self.assertRaises(
-            ValueError, self.samfile.count, "chr1:99999999999999999-999999999999999999")
-
-    def testZeroToZero(self):
-        '''see issue 44'''
-        self.assertEqual(len(list(self.samfile.fetch('chr1', 0, 0))), 0)
-
-    def tearDown(self):
-        self.samfile.close()
-
-
-class TestWrongFormat(unittest.TestCase):
-
-    '''test cases for opening files not in bam/sam format.'''
-
-    def testOpenSamAsBam(self):
-        self.assertRaises(ValueError,
-                          pysam.Samfile,
-                          os.path.join(DATADIR, 'ex1.sam'),
-                          'rb')
-
-    def testOpenBamAsSam(self):
-        # test fails, needs to be implemented.
-        # sam.fetch() fails on reading, not on opening
-        # self.assertRaises( ValueError, pysam.Samfile, 'ex1.bam', 'r' )
-        pass
-
-    def testOpenFastaAsSam(self):
-        # test fails, needs to be implemented.
-        # sam.fetch() fails on reading, not on opening
-        # self.assertRaises( ValueError, pysam.Samfile, 'ex1.fa', 'r' )
-        pass
-
-    def testOpenFastaAsBam(self):
-        self.assertRaises(ValueError,
-                          pysam.Samfile,
-                          os.path.join(DATADIR, 'ex1.fa'),
-                          'rb')
-
-
-
-class TestAlignedRead(unittest.TestCase):
-
-    '''tests to check if aligned read can be constructed
-    and manipulated.
-    '''
-
-    def checkFieldEqual(self, read1, read2, exclude=[]):
-        '''check if two reads are equal by comparing each field.'''
-
-        for x in ("qname", "seq", "flag",
-                  "rname", "pos", "mapq", "cigar",
-                  "mrnm", "mpos", "isize", "qual",
-                  "is_paired", "is_proper_pair",
-                  "is_unmapped", "mate_is_unmapped",
-                  "is_reverse", "mate_is_reverse",
-                  "is_read1", "is_read2",
-                  "is_secondary", "is_qcfail",
-                  "is_duplicate", "bin"):
-            if x in exclude:
-                continue
-            self.assertEqual(
-                getattr(read1, x),
-                getattr(read2, x),
-                "attribute mismatch for %s: %s != %s" %
-                (x, getattr(read1, x), getattr(read2, x)))
-
-    def testEmpty(self):
-        a = pysam.AlignedRead()
-        self.assertEqual(a.qname, None)
-        self.assertEqual(a.seq, None)
-        self.assertEqual(a.qual, None)
-        self.assertEqual(a.flag, 0)
-        self.assertEqual(a.rname, 0)
-        self.assertEqual(a.mapq, 0)
-        self.assertEqual(a.cigar, [])
-        self.assertEqual(a.tags, [])
-        self.assertEqual(a.mrnm, 0)
-        self.assertEqual(a.mpos, 0)
-        self.assertEqual(a.isize, 0)
-
-    def buildRead(self):
-        '''build an example read.'''
-
-        a = pysam.AlignedRead()
-        a.qname = "read_12345"
-        a.seq = "ACGT" * 10
-        a.flag = 0
-        a.rname = 0
-        a.pos = 20
-        a.mapq = 20
-        a.cigar = ((0, 10), (2, 1), (0, 9), (1, 1), (0, 20))
-        a.mrnm = 0
-        a.mpos = 200
-        a.isize = 167
-        a.qual = "1234" * 10
-        # todo: create tags
-        return a
-
-    def testUpdate(self):
-        '''check if updating fields affects other variable length data
-        '''
-        a = self.buildRead()
-        b = self.buildRead()
-
-        # check qname
-        b.qname = "read_123"
-        self.checkFieldEqual(a, b, "qname")
-        b.qname = "read_12345678"
-        self.checkFieldEqual(a, b, "qname")
-        b.qname = "read_12345"
-        self.checkFieldEqual(a, b)
-
-        # check cigar
-        b.cigar = ((0, 10), )
-        self.checkFieldEqual(a, b, "cigar")
-        b.cigar = ((0, 10), (2, 1), (0, 10))
-        self.checkFieldEqual(a, b, "cigar")
-        b.cigar = ((0, 10), (2, 1), (0, 9), (1, 1), (0, 20))
-        self.checkFieldEqual(a, b)
-
-        # check seq
-        b.seq = "ACGT"
-        self.checkFieldEqual(a, b, ("seq", "qual"))
-        b.seq = "ACGT" * 3
-        self.checkFieldEqual(a, b, ("seq", "qual"))
-        b.seq = "ACGT" * 10
-        self.checkFieldEqual(a, b, ("qual",))
-
-        # reset qual
-        b = self.buildRead()
-
-        # check flags:
-        for x in (
-                "is_paired", "is_proper_pair",
-                "is_unmapped", "mate_is_unmapped",
-                "is_reverse", "mate_is_reverse",
-                "is_read1", "is_read2",
-                "is_secondary", "is_qcfail",
-                "is_duplicate"):
-            setattr(b, x, True)
-            self.assertEqual(getattr(b, x), True)
-            self.checkFieldEqual(a, b, ("flag", x,))
-            setattr(b, x, False)
-            self.assertEqual(getattr(b, x), False)
-            self.checkFieldEqual(a, b)
-
-    def testUpdate2(self):
-        '''issue 135: inplace update of sequence and quality score.
-
-        This does not work as setting the sequence will erase
-        the quality scores.
-        '''
-        a = self.buildRead()
-        a.seq = a.seq[5:10]
-        self.assertEqual(a.qual, None)
-
-        a = self.buildRead()
-        s = a.qual
-        a.seq = a.seq[5:10]
-        a.qual = s[5:10]
-
-        self.assertEqual(a.qual, s[5:10])
-
-    def testLargeRead(self):
-        '''build an example read.'''
-
-        a = pysam.AlignedRead()
-        a.qname = "read_12345"
-        a.seq = "ACGT" * 200
-        a.flag = 0
-        a.rname = 0
-        a.pos = 20
-        a.mapq = 20
-        a.cigar = ((0, 4 * 200), )
-        a.mrnm = 0
-        a.mpos = 200
-        a.isize = 167
-        a.qual = "1234" * 200
-
-        return a
-
-    def testTagParsing(self):
-        '''test for tag parsing
-
-        see http://groups.google.com/group/pysam-user-group/browse_thread/thread/67ca204059ea465a
-        '''
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex8.bam"),
-                                "rb")
-
-        for entry in samfile:
-            before = entry.tags
-            entry.tags = entry.tags
-            after = entry.tags
-            self.assertEqual(after, before)
-
-    def testUpdateTlen(self):
-        '''check if updating tlen works'''
-        a = self.buildRead()
-        oldlen = a.tlen
-        oldlen *= 2
-        a.tlen = oldlen
-        self.assertEqual(a.tlen, oldlen)
-
-    def testPositions(self):
-        a = self.buildRead()
-        self.assertEqual(a.positions,
-                         [20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
-                          31, 32, 33, 34, 35, 36, 37, 38, 39,
-                          40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
-                          50, 51, 52, 53, 54, 55, 56, 57, 58, 59])
-
-        self.assertEqual(a.aligned_pairs,
-                         [(0, 20), (1, 21), (2, 22), (3, 23), (4, 24),
-                          (5, 25), (6, 26), (7, 27), (8, 28), (9, 29),
-                          (None, 30),
-                          (10, 31), (11, 32), (12, 33), (13, 34), (14, 35),
-                          (15, 36), (16, 37), (17, 38), (18, 39), (19, None),
-                          (20, 40), (21, 41), (22, 42), (23, 43), (24, 44),
-                          (25, 45), (26, 46), (27, 47), (28, 48), (29, 49),
-                          (30, 50), (31, 51), (32, 52), (33, 53), (34, 54),
-                          (35, 55), (36, 56), (37, 57), (38, 58), (39, 59)])
-
-        self.assertEqual(
-            a.positions,
-            [x[1] for x in a.aligned_pairs
-             if x[0] is not None and x[1] is not None])
-        # alen is the length of the aligned read in genome
-        self.assertEqual(a.alen, a.aligned_pairs[-1][0] + 1)
-        # aend points to one beyond last aligned base in ref
-        self.assertEqual(a.positions[-1], a.aend - 1)
-
-    def testBlocks(self):
-        a = self.buildRead()
-        self.assertEqual(a.blocks,
-                         [(20, 30), (31, 40), (40, 60)])
-
-    def testFancyStr(self):
-        a = self.buildRead()
-        output = a.fancy_str()
-        self.assertEqual(len(output), 9)
-
-
-class TestDeNovoConstruction(unittest.TestCase):
-
-    '''check BAM/SAM file construction using ex6.sam
-
-    (note these are +1 coordinates):
-
-    read_28833_29006_6945	99	chr1	33	20	10M1D25M	=	200	167	AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG	<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<	NM:i:1	RG:Z:L1
-    read_28701_28881_323b	147	chr2	88	30	35M	=	500	412	ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA	<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<	MF:i:18	RG:Z:L2
-    '''
-
-    header = {'HD': {'VN': '1.0'},
-              'SQ': [{'LN': 1575, 'SN': 'chr1'},
-                     {'LN': 1584, 'SN': 'chr2'}], }
-
-    bamfile = os.path.join(DATADIR, "ex6.bam")
-    samfile = os.path.join(DATADIR, "ex6.sam")
-
-    def checkFieldEqual(self, read1, read2, exclude=[]):
-        '''check if two reads are equal by comparing each field.'''
-
-        for x in ("qname", "seq", "flag",
-                  "rname", "pos", "mapq", "cigar",
-                  "mrnm", "mpos", "isize", "qual",
-                  "bin",
-                  "is_paired", "is_proper_pair",
-                  "is_unmapped", "mate_is_unmapped",
-                  "is_reverse", "mate_is_reverse",
-                  "is_read1", "is_read2",
-                  "is_secondary", "is_qcfail",
-                  "is_duplicate"):
-            if x in exclude:
-                continue
-            self.assertEqual(getattr(read1, x), getattr(read2, x), "attribute mismatch for %s: %s != %s" %
-                             (x, getattr(read1, x), getattr(read2, x)))
-
-    def setUp(self):
-
-        a = pysam.AlignedRead()
-        a.qname = "read_28833_29006_6945"
-        a.seq = "AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG"
-        a.flag = 99
-        a.rname = 0
-        a.pos = 32
-        a.mapq = 20
-        a.cigar = ((0, 10), (2, 1), (0, 25))
-        a.mrnm = 0
-        a.mpos = 199
-        a.isize = 167
-        a.qual = "<<<<<<<<<<<<<<<<<<<<<:<9/,&,22;;<<<"
-        a.tags = (("NM", 1),
-                  ("RG", "L1"))
-
-        b = pysam.AlignedRead()
-        b.qname = "read_28701_28881_323b"
-        b.seq = "ACCTATATCTTGGCCTTGGCCGATGCGGCCTTGCA"
-        b.flag = 147
-        b.rname = 1
-        b.pos = 87
-        b.mapq = 30
-        b.cigar = ((0, 35), )
-        b.mrnm = 1
-        b.mpos = 499
-        b.isize = 412
-        b.qual = "<<<<<;<<<<7;:<<<6;<<<<<<<<<<<<7<<<<"
-        b.tags = (("MF", 18),
-                  ("RG", "L2"))
-
-        self.reads = (a, b)
-
-    # TODO
-    # def testSAMWholeFile(self):
-
-    #     tmpfilename = "tmp_%i.sam" % id(self)
-
-    #     outfile = pysam.Samfile(tmpfilename,
-    #                             "wh",
-    #                             header=self.header)
-
-    #     for x in self.reads:
-    #         outfile.write(x)
-    #     outfile.close()
-    #     self.assertTrue(checkBinaryEqual(tmpfilename, self.samfile),
-    #                     "mismatch when construction SAM file, see %s %s" % (tmpfilename, self.samfile))
-
-    #     os.unlink(tmpfilename)
-
-    def testBAMPerRead(self):
-        '''check if individual reads are binary equal.'''
-        infile = pysam.Samfile(self.bamfile, "rb")
-
-        others = list(infile)
-        for denovo, other in zip(others, self.reads):
-            self.checkFieldEqual(other, denovo)
-            self.assertEqual(other.compare(denovo), 0)
-
-    # TODO
-    # def testSAMPerRead(self):
-    #     '''check if individual reads are binary equal.'''
-    #     infile = pysam.Samfile(self.samfile, "r")
-
-    #     others = list(infile)
-    #     for denovo, other in zip(others, self.reads):
-    #         self.checkFieldEqual(other, denovo)
-    #         self.assertEqual(other.compare(denovo), 0)
-
-    def testBAMWholeFile(self):
-
-        tmpfilename = "tmp_%i.bam" % id(self)
-
-        outfile = pysam.Samfile(tmpfilename, "wb", header=self.header)
-
-        for x in self.reads:
-            outfile.write(x)
-        outfile.close()
-
-        self.assertTrue(checkBinaryEqual(tmpfilename, self.bamfile),
-                        "mismatch when construction BAM file, see %s %s" % (tmpfilename, self.bamfile))
-
-        os.unlink(tmpfilename)
-
-
-class TestDeNovoConstructionUserTags(TestDeNovoConstruction):
-
-    '''test de novo construction with a header that contains lower-case tags.'''
-
-    header = {'HD': {'VN': '1.0'},
-              'SQ': [{'LN': 1575, 'SN': 'chr1'},
-                     {'LN': 1584, 'SN': 'chr2'}],
-              'x1': {'A': 2, 'B': 5},
-              'x3': {'A': 6, 'B': 5},
-              'x2': {'A': 4, 'B': 5}}
-
-    bamfile = os.path.join(DATADIR, "example_user_header.bam")
-    samfile = os.path.join(DATADIR, "example_user_header.sam")
-
-
-class TestEmptyHeader(unittest.TestCase):
-
-    '''see issue 84.'''
-
-    def testEmptyHeader(self):
-
-        s = pysam.Samfile(os.path.join(DATADIR, 'example_empty_header.bam'))
-        self.assertEqual(s.header, {'SQ': [{'LN': 1000, 'SN': 'chr1'}]})
-
-
-class TestBTagSam(unittest.TestCase):
-
-    '''see issue 81.'''
-
-    compare = [[100, 1, 91, 0, 7, 101, 0, 201, 96, 204, 0, 0, 87, 109, 0, 7, 97, 112, 1, 12, 78, 197, 0, 7, 100, 95, 101, 202, 0, 6, 0, 1, 186, 0, 84, 0, 244, 0, 0, 324, 0, 107, 195, 101, 113, 0, 102, 0, 104, 3, 0, 101, 1, 0, 212, 6, 0, 0, 1, 0, 74, 1, 11, 0, 196, 2, 197, 103, 0, 108, 98, 2, 7, 0, 1, 2, 194, 0, 180, 0, 108, 0, 203, 104, 16, 5, 205, 0, 0, 0, 1, 1, 100, 98, 0, 0, 204, 6, 0, 79, 0, 0, 101, 7, 109, 90, 265, 1, 27, 10, 109, 102, 9, 0, 292, 0, 110, 0, 0, 102, 112, 0, 0, 84, 10 [...]
-               [-100, 200, -300, -400],
-               [-100, 12],
-               [12, 15],
-               [-1.0, 5.0, 2.5]]
-
-    filename = os.path.join(DATADIR, 'example_btag.sam')
-
-    def testRead(self):
-
-        s = pysam.Samfile(self.filename)
-        for x, read in enumerate(s):
-            if x == 0:
-                self.assertEqual(read.tags, [('RG', 'QW85I'), ('PG', 'tmap'), ('MD', '140'), ('NM', 0), ('AS', 140), ('FZ', [100, 1, 91, 0, 7, 101, 0, 201, 96, 204, 0, 0, 87, 109, 0, 7, 97, 112, 1, 12, 78, 197, 0, 7, 100, 95, 101, 202, 0, 6, 0, 1, 186, 0, 84, 0, 244, 0, 0, 324, 0, 107, 195, 101, 113, 0, 102, 0, 104, 3, 0, 101, 1, 0, 212, 6, 0, 0, 1, 0, 74, 1, 11, 0, 196, 2, 197, 103, 0, 108, 98, 2, 7, 0, 1, 2, 194, 0, 180, 0, 108, 0, 203, 104, 16, 5, 205, 0, 0, 0, 1, 1, 100, 98, 0, 0, 20 [...]
-                                 )
-
-            fz = dict(read.tags)["FZ"]
-            self.assertEqual(fz, self.compare[x])
-            self.assertEqual(read.opt("FZ"), self.compare[x])
-
-    def testWrite(self):
-
-        s = pysam.Samfile(self.filename)
-        for read in s:
-            before = read.tags
-            read.tags = read.tags
-            after = read.tags
-            self.assertEqual(after, before)
-
-
-class TestBTagBam(TestBTagSam):
-    filename = os.path.join(DATADIR, 'example_btag.bam')
-
-
-class TestDoubleFetch(unittest.TestCase):
-
-    '''check if two iterators on the same bamfile are independent.'''
-
-    filename = os.path.join(DATADIR, 'ex1.bam')
-
-    def testDoubleFetch(self):
-
-        samfile1 = pysam.Samfile(self.filename, 'rb')
-
-        for a, b in zip(samfile1.fetch(), samfile1.fetch()):
-            self.assertEqual(a.compare(b), 0)
-
-    def testDoubleFetchWithRegion(self):
-
-        samfile1 = pysam.Samfile(self.filename, 'rb')
-        chr, start, stop = 'chr1', 200, 3000000
-        # just making sure the test has something to catch
-        self.assertTrue(len(list(samfile1.fetch(chr, start, stop))) > 0)
-
-        for a, b in zip(samfile1.fetch(chr, start, stop),
-                        samfile1.fetch(chr, start, stop)):
-            self.assertEqual(a.compare(b), 0)
-
-    def testDoubleFetchUntilEOF(self):
-
-        samfile1 = pysam.Samfile(self.filename, 'rb')
-
-        for a, b in zip(samfile1.fetch(until_eof=True),
-                        samfile1.fetch(until_eof=True)):
-            self.assertEqual(a.compare(b), 0)
-
-
-class TestRemoteFileFTP(unittest.TestCase):
-
-    '''test remote access.
-
-    '''
-
-    # Need to find an ftp server without password on standard
-    # port.
-
-    url = "ftp://ftp.sanger.ac.uk/pub/rd/humanSequences/CV.bam"
-    region = "1:1-1000"
-
-    def testFTPView(self):
-        return
-        result = pysam.view(self.url, self.region)
-        self.assertEqual(len(result), 36)
-
-    def testFTPFetch(self):
-        return
-        samfile = pysam.Samfile(self.url, "rb")
-        result = list(samfile.fetch(region=self.region))
-        self.assertEqual(len(result), 36)
-
-
-class TestRemoteFileHTTP(unittest.TestCase):
-
-    url = "http://genserv.anat.ox.ac.uk/downloads/pysam/test/ex1.bam"
-    region = "chr1:1-1000"
-    local = os.path.join(DATADIR, "ex1.bam")
-
-    def testView(self):
-        samfile_local = pysam.Samfile(self.local, "rb")
-        ref = list(samfile_local.fetch(region=self.region))
-
-        result = pysam.view(self.url, self.region)
-        self.assertEqual(len(result), len(ref))
-
-    def testFetch(self):
-        samfile = pysam.Samfile(self.url, "rb")
-        result = list(samfile.fetch(region=self.region))
-        samfile_local = pysam.Samfile(self.local, "rb")
-        ref = list(samfile_local.fetch(region=self.region))
-
-        self.assertEqual(len(ref), len(result))
-        for x, y in zip(result, ref):
-            self.assertEqual(x.compare(y), 0)
-
-    def testFetchAll(self):
-        samfile = pysam.Samfile(self.url, "rb")
-        result = list(samfile.fetch())
-        samfile_local = pysam.Samfile(self.local, "rb")
-        ref = list(samfile_local.fetch())
-
-        self.assertEqual(len(ref), len(result))
-        for x, y in zip(result, ref):
-            self.assertEqual(x.compare(y), 0)
-
-
-class TestLargeOptValues(unittest.TestCase):
-
-    ints = (65536, 214748, 2147484, 2147483647)
-    floats = (65536.0, 214748.0, 2147484.0)
-
-    def check(self, samfile):
-
-        i = samfile.fetch()
-        for exp in self.ints:
-            rr = next(i)
-            obs = rr.opt("ZP")
-            self.assertEqual(exp, obs, "expected %s, got %s\n%s" %
-                             (str(exp), str(obs), str(rr)))
-
-        for exp in [-x for x in self.ints]:
-            rr = next(i)
-            obs = rr.opt("ZP")
-            self.assertEqual(exp, obs, "expected %s, got %s\n%s" %
-                             (str(exp), str(obs), str(rr)))
-
-        for exp in self.floats:
-            rr = next(i)
-            obs = rr.opt("ZP")
-            self.assertEqual(exp, obs, "expected %s, got %s\n%s" %
-                             (str(exp), str(obs), str(rr)))
-
-        for exp in [-x for x in self.floats]:
-            rr = next(i)
-            obs = rr.opt("ZP")
-            self.assertEqual(exp, obs, "expected %s, got %s\n%s" %
-                             (str(exp), str(obs), str(rr)))
-
-    def testSAM(self):
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex10.sam"),
-                                "r")
-        self.check(samfile)
-
-    def testBAM(self):
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex10.bam"),
-                                "rb")
-        self.check(samfile)
-
-
-class TestPileup(unittest.TestCase):
-    '''test pileup functionality.'''
-
-    samfilename = "pysam_data/ex1.bam"
-    fastafilename = "pysam_data/ex1.fa"
-
-    def setUp(self):
-
-        self.samfile = pysam.Samfile(self.samfilename)
-        self.fastafile = pysam.Fastafile(self.fastafilename)
-
-    def checkEqual(self, references, iterator):
-
-        for x, column in enumerate(iterator):
-            (contig, pos, reference_base,
-             read_bases, read_qualities, alignment_mapping_qualities) \
-                = references[x][:-1].split("\t")
-            self.assertEqual(int(pos) - 1, column.pos)
-
-    def testSamtoolsStepper(self):
-        refs = pysam.mpileup(
-            "-f", self.fastafilename,
-            self.samfilename)
-        iterator = self.samfile.pileup(
-            stepper="samtools",
-            fastafile=self.fastafile)
-        self.checkEqual(refs, iterator)
-
-    def testAllStepper(self):
-        refs = pysam.mpileup(
-            "-f", self.fastafilename,
-            "-A", "-B",
-            self.samfilename)
-
-        iterator = self.samfile.pileup(
-            stepper="all",
-            fastafile=self.fastafile)
-        self.checkEqual(refs, iterator)
-
-
-class TestLogging(unittest.TestCase):
-
-    '''test around bug issue 42,
-
-    failed in versions < 0.4
-    '''
-
-    def check(self, bamfile, log):
-
-        if log:
-            logger = logging.getLogger('franklin')
-            logger.setLevel(logging.INFO)
-            formatter = logging.Formatter(
-                '%(asctime)s %(levelname)s %(message)s')
-            log_hand = logging.FileHandler('log.txt')
-            log_hand.setFormatter(formatter)
-            logger.addHandler(log_hand)
-
-        bam = pysam.Samfile(bamfile, 'rb')
-        cols = bam.pileup()
-        self.assertTrue(True)
-
-    def testFail1(self):
-        self.check(os.path.join(DATADIR, "ex9_fail.bam"),
-                   False)
-        self.check(os.path.join(DATADIR, "ex9_fail.bam"),
-                   True)
-
-    def testNoFail1(self):
-        self.check(os.path.join(DATADIR, "ex9_nofail.bam"),
-                   False)
-        self.check(os.path.join(DATADIR, "ex9_nofail.bam"),
-                   True)
-
-    def testNoFail2(self):
-        self.check(os.path.join(DATADIR, "ex9_nofail.bam"),
-                   True)
-        self.check(os.path.join(DATADIR, "ex9_nofail.bam"),
-                   True)
-
-# TODOS
-# 1. finish testing all properties within pileup objects
-# 2. check exceptions and bad input problems (missing files, optional fields that aren't present, etc...)
-# 3. check: presence of sequence
-
-
-class TestSamfileUtilityFunctions(unittest.TestCase):
-
-    def testCount(self):
-
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                "rb")
-
-        for contig in ("chr1", "chr2"):
-            for start in range(0, 2000, 100):
-                end = start + 1
-                self.assertEqual(
-                    len(list(samfile.fetch(contig, start, end))),
-                    samfile.count(contig, start, end),
-                    'number mismatch for %s:%i-%i %i != %i' % (
-                        contig, start, end,
-                        len(list(samfile.fetch(contig, start, end))),
-                        samfile.count(contig, start, end)))
-
-                # test empty intervals
-                self.assertEqual(
-                    len(list(samfile.fetch(contig, start, start))),
-                    samfile.count(contig, start, start),
-                    'number mismatch for %s:%i-%i %i != %i' % (
-                        contig, start, start,
-                        len(list(samfile.fetch(contig, start, start))),
-                        samfile.count(contig, start, start)))
-
-                # test half empty intervals
-                self.assertEqual(len(list(samfile.fetch(contig, start))),
-                                 samfile.count(contig, start))
-
-                self.assertEqual(
-                    len(list(samfile.fetch(contig, start))),
-                    samfile.count(contig, start),
-                    'number mismatch for %s:%i %i != %i' % (
-                        contig, start,
-                        len(list(samfile.fetch(contig, start))),
-                        samfile.count(contig, start)))
-
-    def testMate(self):
-        '''test mate access.'''
-
-        with open(os.path.join(DATADIR, "ex1.sam"), "rb") as inf:
-            readnames = [x.split(b"\t")[0] for x in inf.readlines()]
-        if sys.version_info[0] >= 3:
-            readnames = [name.decode('ascii') for name in readnames]
-
-        counts = collections.defaultdict(int)
-        for x in readnames:
-            counts[x] += 1
-
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                "rb")
-
-        for read in samfile.fetch():
-            if not read.is_paired:
-                self.assertRaises(ValueError, samfile.mate, read)
-            elif read.mate_is_unmapped:
-                self.assertRaises(ValueError, samfile.mate, read)
-            else:
-                if counts[read.qname] == 1:
-                    self.assertRaises(ValueError, samfile.mate, read)
-                else:
-                    mate = samfile.mate(read)
-                    self.assertEqual(read.qname, mate.qname)
-                    self.assertEqual(read.is_read1, mate.is_read2)
-                    self.assertEqual(read.is_read2, mate.is_read1)
-                    self.assertEqual(read.pos, mate.mpos)
-                    self.assertEqual(read.mpos, mate.pos)
-
-    def testIndexStats(self):
-        '''test if total number of mapped/unmapped reads is correct.'''
-
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                "rb")
-        self.assertEqual(samfile.mapped, 3235)
-        self.assertEqual(samfile.unmapped, 35)
-        self.assertEqual(samfile.nocoordinate, 0)
-
-
-class TestSamtoolsProxy(unittest.TestCase):
-
-    '''tests for sanity checking access to samtools functions.'''
-
-    def testIndex(self):
-        self.assertRaises(IOError, pysam.index, "missing_file")
-
-    def testView(self):
-        # note that view still echos "open: No such file or directory"
-        self.assertRaises(pysam.SamtoolsError, pysam.view, "missing_file")
-
-    def testSort(self):
-        self.assertRaises(pysam.SamtoolsError, pysam.sort, "missing_file")
-
-
-class TestSamfileIndex(unittest.TestCase):
-
-    def testIndex(self):
-        samfile = pysam.Samfile(os.path.join(DATADIR, "ex1.bam"),
-                                "rb")
-        index = pysam.IndexedReads(samfile)
-        index.build()
-        reads = collections.defaultdict(int)
-
-        for read in samfile:
-            reads[read.qname] += 1
-
-        for qname, counts in reads.items():
-            found = list(index.find(qname))
-            self.assertEqual(len(found), counts)
-            for x in found:
-                self.assertEqual(x.qname, qname)
-
-
-if __name__ == "__main__":
-    # build data files
-    print ("building data files")
-    subprocess.call("make -C %s" % DATADIR, shell=True)
-    print ("starting tests")
-    unittest.main()
-    print ("completed tests")
diff --git a/tests/tabix_test.py b/tests/tabix_test.py
deleted file mode 100644
index b3c7dc5..0000000
--- a/tests/tabix_test.py
+++ /dev/null
@@ -1,908 +0,0 @@
-#!/usr/bin/env python
-'''unit testing code for pysam.
-
-Execute in the :file:`tests` directory as it requires the Makefile
-and data files located there.
-'''
-
-import sys
-import os
-import shutil
-import gzip
-import pysam
-import unittest
-import itertools
-import subprocess
-import glob
-import re
-
-DATADIR = 'tabix_data'
-
-IS_PYTHON3 = sys.version_info[0] >= 3
-
-
-def myzip_open(infile, mode="r"):
-    '''open compressed file and decode.'''
-
-    def _convert(f):
-        for l in f:
-            yield l.decode("ascii")
-
-    if IS_PYTHON3:
-        if mode == "r":
-            return _convert(gzip.open(infile, "r"))
-    else:
-        return gzip.open(mode)
-
-
-def loadAndConvert(infile, encode=True):
-    '''load data from infile and convert all fields to bytes.
-
-    infile can be either plain or compressed (ending in .gz).
-    '''
-    data = []
-    if infile.endswith(".gz"):
-        for line in gzip.open(infile):
-            line = line.decode("ascii")
-            if line.startswith("#"):
-                continue
-            d = line.strip().split("\t")
-            if encode:
-                data.append([x.encode("ascii") for x in d])
-            else:
-                data.append(d)
-
-    else:
-        with open(infile) as f:
-            for line in f:
-                if line.startswith("#"):
-                    continue
-                d = line.strip().split("\t")
-                if encode:
-                    data.append([x.encode("ascii") for x in d])
-                else:
-                    data.append(d)
-
-    return data
-
-
-def splitToBytes(s):
-    '''split string and return list of bytes.'''
-    return [x.encode("ascii") for x in s.split("\t")]
-
-
-def checkBinaryEqual(filename1, filename2):
-    '''return true if the two files are binary equal.'''
-    if os.path.getsize(filename1) != os.path.getsize(filename2):
-        return False
-
-    infile1 = open(filename1, "rb")
-    infile2 = open(filename2, "rb")
-
-    d1, d2 = infile1.read(), infile2.read()
-    found = False
-    for c1, c2 in zip(d1, d2):
-        if c1 != c2:
-            break
-    else:
-        found = True
-
-    infile1.close()
-    infile2.close()
-    return found
-
-
-class TestIndexing(unittest.TestCase):
-    filename = os.path.join(DATADIR, "example.gtf.gz")
-    filename_idx = os.path.join(DATADIR, "example.gtf.gz.tbi")
-
-    def setUp(self):
-
-        self.tmpfilename = "tmp_%i.gtf.gz" % id(self)
-        shutil.copyfile(self.filename, self.tmpfilename)
-
-    def testIndexPreset(self):
-        '''test indexing via preset.'''
-
-        pysam.tabix_index(self.tmpfilename, preset="gff")
-        checkBinaryEqual(self.tmpfilename + ".tbi", self.filename_idx)
-
-    def tearDown(self):
-        os.unlink(self.tmpfilename)
-        os.unlink(self.tmpfilename + ".tbi")
-
-
-class TestCompression(unittest.TestCase):
-    filename = os.path.join(DATADIR, "example.gtf.gz")
-    filename_idx = os.path.join(DATADIR, "example.gtf.gz.tbi")
-    preset = "gff"
-
-    def setUp(self):
-
-        self.tmpfilename = "tmp_%i" % id(self)
-        infile = gzip.open(self.filename, "rb")
-        outfile = open(self.tmpfilename, "wb")
-        outfile.write(infile.read())
-        outfile.close()
-        infile.close()
-
-    def testCompression(self):
-        '''see also issue 106'''
-        pysam.tabix_compress(self.tmpfilename, self.tmpfilename + ".gz")
-        checkBinaryEqual(self.tmpfilename, self.tmpfilename + ".gz")
-
-    def testIndexPresetUncompressed(self):
-        '''test indexing via preset.'''
-
-        pysam.tabix_index(self.tmpfilename, preset=self.preset)
-        # check if uncompressed file has been removed
-        self.assertEqual(os.path.exists(self.tmpfilename), False)
-        checkBinaryEqual(self.tmpfilename + ".gz", self.filename)
-        checkBinaryEqual(self.tmpfilename + ".gz.tbi", self.filename_idx)
-
-    def testIndexPresetCompressed(self):
-        '''test indexing via preset.'''
-
-        pysam.tabix_compress(self.tmpfilename, self.tmpfilename + ".gz")
-        pysam.tabix_index(self.tmpfilename + ".gz", preset=self.preset)
-        checkBinaryEqual(self.tmpfilename + ".gz", self.filename)
-        checkBinaryEqual(self.tmpfilename + ".gz.tbi", self.filename_idx)
-
-    def tearDown(self):
-
-        try:
-            os.unlink(self.tmpfilename)
-            os.unlink(self.tmpfilename + ".gz")
-            os.unlink(self.tmpfilename + ".gz.tbi")
-        except OSError:
-            pass
-
-
-class TestCompressionSam(TestCompression):
-    filename = os.path.join(DATADIR, "example.sam.gz")
-    filename_index = os.path.join(DATADIR, "example.sam.gz.tbi")
-    preset = "sam"
-
-
-class TestCompressionBed(TestCompression):
-    filename = os.path.join(DATADIR, "example.bed.gz")
-    filename_index = os.path.join(DATADIR, "example.bed.gz.tbi")
-    preset = "bed"
-
-
-class TestCompressionVCF(TestCompression):
-    filename = os.path.join(DATADIR, "example.vcf.gz")
-    filename_index = os.path.join(DATADIR, "example.vcf.gz.tbi")
-    preset = "vcf"
-
-class IterationTest(unittest.TestCase):
-
-    with_comments = False
-
-    def setUp(self):
-
-        lines = []
-        inf = gzip.open(self.filename, "rb")
-        for line in inf:
-            line = line.decode('ascii')
-            if line.startswith("#"):
-                if not self.with_comments:
-                    continue
-            lines.append(line)
-        inf.close()
-
-        # creates index of contig, start, end, adds content without newline.
-        self.compare = [
-            (x[0][0], int(x[0][3]), int(x[0][4]), x[1])
-            for x in [(y.split("\t"), y[:-1]) for y in lines
-                      if not y.startswith("#")]]
-
-        self.comments = [x[:-1] for x in lines if x.startswith("#")]
-
-    def getSubset(self, contig=None, start=None, end=None):
-
-        if contig is None:
-            # all lines
-            subset = [x[3] for x in self.compare]
-        else:
-            if start is not None and end is None:
-                # until end of contig
-                subset = [x[3]
-                          for x in self.compare if x[0] == contig
-                          and x[2] > start]
-            elif start is None and end is not None:
-                # from start of contig
-                subset = [x[3]
-                          for x in self.compare if x[0] == contig
-                          and x[1] <= end]
-            elif start is None and end is None:
-                subset = [x[3] for x in self.compare if x[0] == contig]
-            else:
-                # all within interval
-                subset = [x[3] for x in self.compare if x[0] == contig
-                          and min(x[2], end) - max(x[1], start) > 0]
-
-        if self.with_comments:
-            subset.extend(self.comments)
-        
-        return subset
-    
-    def checkPairwise(self, result, ref):
-        '''check pairwise results.
-        '''
-        result.sort()
-        ref.sort()
-        a = set(result)
-        b = set(ref)
-
-        self.assertEqual(
-            len(result), len(ref),
-            "unexpected number of results: result=%i, expected ref=%i, differences are %s: %s"
-            % (len(result), len(ref),
-               a.difference(b),
-               b.difference(a)))
-
-        for x, d in enumerate(list(zip(result, ref))):
-            self.assertEqual(
-                d[0], d[1],
-                "unexpected results in pair %i:\n'%s', expected\n'%s'" %
-                (x,
-                 d[0],
-                 d[1]))
-
-
-class TestGZFile(IterationTest):
-
-    filename = os.path.join(DATADIR, "example.gtf.gz")
-    with_comments = True
-
-    def setUp(self):
-
-        IterationTest.setUp(self)
-
-        self.iter = pysam.GZIterator(self.filename)
-
-    def testAll(self):
-        result = list(self.iter)
-        ref = self.getSubset()
-        self.checkPairwise(result, ref)
-
-
-class TestIterationWithoutComments(IterationTest):
-    '''test iterating with Tabixfile.fetch() when
-    there are no comments in the file.'''
-
-    filename = os.path.join(DATADIR,
-                            "example.gtf.gz")
-
-    def setUp(self):
-        IterationTest.setUp(self)
-        self.tabix = pysam.Tabixfile(self.filename)
-
-    def testAll(self):
-        result = list(self.tabix.fetch())
-        ref = self.getSubset()
-        self.checkPairwise(result, ref)
-
-    def testPerContig(self):
-        for contig in ("chr1", "chr2", "chr1", "chr2"):
-            result = list(self.tabix.fetch(contig))
-            ref = self.getSubset(contig)
-            self.checkPairwise(result, ref)
-
-    def testPerContigToEnd(self):
-
-        end = None
-        for contig in ("chr1", "chr2", "chr1", "chr2"):
-            for start in range(0, 200000, 1000):
-                result = list(self.tabix.fetch(contig, start, end))
-                ref = self.getSubset(contig, start, end)
-                self.checkPairwise(result, ref)
-
-    def testPerContigFromStart(self):
-
-        start = None
-        for contig in ("chr1", "chr2", "chr1", "chr2"):
-            for end in range(0, 200000, 1000):
-                result = list(self.tabix.fetch(contig, start, end))
-                ref = self.getSubset(contig, start, end)
-                self.checkPairwise(result, ref)
-
-    def testPerContig(self):
-
-        start, end = None, None
-        for contig in ("chr1", "chr2", "chr1", "chr2"):
-            result = list(self.tabix.fetch(contig, start, end))
-            ref = self.getSubset(contig, start, end)
-            self.checkPairwise(result, ref)
-
-    def testPerInterval(self):
-
-        start, end = None, None
-        for contig in ("chr1", "chr2", "chr1", "chr2"):
-            for start in range(0, 200000, 2000):
-                for end in range(start, start + 2000, 500):
-                    result = list(self.tabix.fetch(contig, start, end))
-                    ref = self.getSubset(contig, start, end)
-                    self.checkPairwise(result, ref)
-
-    def testInvalidIntervals(self):
-
-        # invalid intervals (start > end)
-        self.assertRaises(ValueError, self.tabix.fetch, "chr1", 0, -10)
-        self.assertRaises(ValueError, self.tabix.fetch, "chr1", 200, 0)
-
-        # out of range intervals
-        self.assertRaises(ValueError, self.tabix.fetch, "chr1", -10, 200)
-        self.assertRaises(ValueError, self.tabix.fetch, "chr1", -10, -20)
-
-        # unknown chromosome
-        self.assertRaises(ValueError, self.tabix.fetch, "chrUn")
-
-        # out of range access
-        # to be implemented
-        # self.assertRaises( IndexError, self.tabix.fetch, "chr1", 1000000, 2000000 )
-
-        # raise no error for invalid intervals
-        self.tabix.fetch("chr1", 100, 100)
-
-    def testGetContigs(self):
-        self.assertEqual(sorted(self.tabix.contigs), [b"chr1", b"chr2"])
-        # check that contigs is read-only
-        self.assertRaises(
-            AttributeError, setattr, self.tabix, "contigs", ["chr1", "chr2"])
-
-    def testHeader(self):
-        ref = []
-        inf = gzip.open(self.filename)
-        for x in inf:
-            x = x.decode("ascii")
-            if not x.startswith("#"):
-                break
-            ref.append(x[:-1].encode('ascii'))
-        inf.close()
-
-        header = list(self.tabix.header)
-        self.assertEqual(ref, header)
-
-    def testReopening(self):
-        '''test repeated opening of the same file.'''
-        def func1():
-            # opens any tabix file
-            inf = pysam.Tabixfile(self.filename)
-            return
-
-        for i in range(10000):
-            func1()
-
-
-class TestIterationWithComments(TestIterationWithoutComments):
-    '''test iterating with Tabixfile.fetch() when
-    there are comments in the file.
-
-    
-    Tests will create plenty of warnings on stderr.
-    '''
-
-    filename = os.path.join(DATADIR, "example_comments.gtf.gz")
-
-    def setUp(self):
-        TestIterationWithoutComments.setUp(self)
-
-
-class TestParser(unittest.TestCase):
-
-    filename = os.path.join(DATADIR, "example.gtf.gz")
-
-    def setUp(self):
-
-        self.tabix = pysam.Tabixfile(self.filename)
-        self.compare = loadAndConvert(self.filename)
-
-    def testRead(self):
-
-        for x, r in enumerate(self.tabix.fetch(parser=pysam.asTuple())):
-            self.assertEqual(self.compare[x], list(r))
-            self.assertEqual(len(self.compare[x]), len(r))
-
-            # test indexing
-            for c in range(0, len(r)):
-                self.assertEqual(self.compare[x][c], r[c])
-
-            # test slicing access
-            for c in range(0, len(r) - 1):
-                for cc in range(c + 1, len(r)):
-                    self.assertEqual(self.compare[x][c:cc],
-                                     r[c:cc])
-
-    def testWrite(self):
-
-        for x, r in enumerate(self.tabix.fetch(parser=pysam.asTuple())):
-            self.assertEqual(self.compare[x], list(r))
-            c = list(r)
-            for y in range(len(r)):
-                r[y] = "test_%05i" % y
-                c[y] = "test_%05i" % y
-            self.assertEqual([x.encode("ascii") for x in c], list(r))
-            self.assertEqual("\t".join(c), str(r))
-            # check second assignment
-            for y in range(len(r)):
-                r[y] = "test_%05i" % y
-            self.assertEqual([x.encode("ascii") for x in c], list(r))
-            self.assertEqual("\t".join(c), str(r))
-
-    def testUnset(self):
-        for x, r in enumerate(self.tabix.fetch(parser=pysam.asTuple())):
-            self.assertEqual(self.compare[x], list(r))
-            c = list(r)
-            e = [x.decode('ascii') for x in r]
-            for y in range(len(r)):
-                r[y] = None
-                c[y] = None
-                e[y] = ""
-                self.assertEqual(c, list(r))
-                self.assertEqual("\t".join(e), str(r))
-
-    def testIteratorCompressed(self):
-        '''test iteration from compressed file.'''
-        with gzip.open(self.filename) as infile:
-            for x, r in enumerate(pysam.tabix_iterator(infile, pysam.asTuple())):
-                self.assertEqual(self.compare[x], list(r))
-                self.assertEqual(len(self.compare[x]), len(r))
-
-                # test indexing
-                for c in range(0, len(r)):
-                    self.assertEqual(self.compare[x][c], r[c])
-
-                # test slicing access
-                for c in range(0, len(r) - 1):
-                    for cc in range(c + 1, len(r)):
-                        self.assertEqual(self.compare[x][c:cc],
-                                         r[c:cc])
-
-    def testIteratorUncompressed(self):
-        '''test iteration from uncompressed file.'''
-        tmpfilename = 'tmp_testIteratorUncompressed'
-        infile = gzip.open(self.filename, "rb")
-        outfile = open(tmpfilename, "wb")
-        outfile.write(infile.read())
-        outfile.close()
-        infile.close()
-
-        with open(tmpfilename) as infile:
-            for x, r in enumerate(pysam.tabix_iterator(infile, pysam.asTuple())):
-                self.assertEqual(self.compare[x], list(r))
-                self.assertEqual(len(self.compare[x]), len(r))
-
-                # test indexing
-                for c in range(0, len(r)):
-                    self.assertEqual(self.compare[x][c], r[c])
-
-                # test slicing access
-                for c in range(0, len(r) - 1):
-                    for cc in range(c + 1, len(r)):
-                        self.assertEqual(self.compare[x][c:cc],
-                                         r[c:cc])
-
-        os.unlink(tmpfilename)
-
-
-class TestIterators(unittest.TestCase):
-
-    filename = os.path.join(DATADIR, "example.gtf.gz")
-
-    iterator = pysam.tabix_generic_iterator
-    parser = pysam.asTuple
-    is_compressed = False
-
-    def setUp(self):
-
-        self.tabix = pysam.Tabixfile(self.filename)
-        self.compare = loadAndConvert(self.filename)
-        self.tmpfilename_uncompressed = 'tmp_TestIterators'
-        infile = gzip.open(self.filename, "rb")
-        outfile = open(self.tmpfilename_uncompressed, "wb")
-        outfile.write(infile.read())
-        outfile.close()
-        infile.close()
-
-    def open(self):
-
-        if self.is_compressed:
-            infile = gzip.open(self.filename)
-        else:
-            infile = open(self.tmpfilename_uncompressed)
-        return infile
-
-    def testIteration(self):
-
-        infile = self.open()
-
-        for x, r in enumerate(self.iterator(infile, self.parser())):
-            self.assertEqual(self.compare[x], list(r))
-            self.assertEqual(len(self.compare[x]), len(r))
-
-            # test indexing
-            for c in range(0, len(r)):
-                self.assertEqual(self.compare[x][c], r[c])
-
-            # test slicing access
-            for c in range(0, len(r) - 1):
-                for cc in range(c + 1, len(r)):
-                    self.assertEqual(self.compare[x][c:cc],
-                                     r[c:cc])
-
-    def testClosedFile(self):
-        '''test for error when iterating from closed file.'''
-        infile = self.open()
-        infile.close()
-
-        # iterating from a closed file should raise a value error
-        self.assertRaises(ValueError, self.iterator, infile, self.parser())
-
-    def testClosedFileIteration(self):
-        '''test for error when iterating from file that has been closed'''
-
-        infile = self.open()
-
-        i = self.iterator(infile, self.parser())
-        x = i.next()
-        infile.close()
-        # Not implemented
-        # self.assertRaises(ValueError, i.next)
-
-    def tearUp(self):
-        os.unlink(self.tmpfilename_uncompressed)
-
-
-class TestIteratorsGenericCompressed(TestIterators):
-    is_compressed = True
-
-
-class TestIteratorsFileCompressed(TestIterators):
-    iterator = pysam.tabix_file_iterator
-    is_compressed = True
-
-
-class TestIteratorsFileUncompressed(TestIterators):
-    iterator = pysam.tabix_file_iterator
-    is_compressed = False
-
-
-class TestGTF(TestParser):
-
-    def testRead(self):
-
-        for x, r in enumerate(self.tabix.fetch(parser=pysam.asGTF())):
-            c = self.compare[x]
-            self.assertEqual(len(c), len(r))
-            self.assertEqual(list(c), list(r))
-            self.assertEqual(c, splitToBytes(str(r)))
-            self.assertTrue(r.gene_id.startswith("ENSG"))
-            if r.feature != b'gene':
-                self.assertTrue(r.transcript_id.startswith("ENST"))
-            self.assertEqual(c[0], r.contig)
-
-
-class TestBed(unittest.TestCase):
-    filename = os.path.join(DATADIR, "example.bed.gz")
-
-    def setUp(self):
-
-        self.tabix = pysam.Tabixfile(self.filename)
-        self.compare = loadAndConvert(self.filename)
-
-    def testRead(self):
-
-        for x, r in enumerate(self.tabix.fetch(parser=pysam.asBed())):
-            c = self.compare[x]
-            self.assertEqual(len(c), len(r))
-            self.assertEqual(c, splitToBytes(str(r)))
-            self.assertEqual(list(c), list(r))
-            self.assertEqual(c[0], r.contig)
-            self.assertEqual(int(c[1]), r.start)
-            self.assertEqual(int(c[2]), r.end)
-
-    def testWrite(self):
-
-        for x, r in enumerate(self.tabix.fetch(parser=pysam.asBed())):
-            c = self.compare[x]
-            self.assertEqual(c, splitToBytes(str(r)))
-            self.assertEqual(list(c), list(r))
-
-            r.contig = "test"
-            self.assertEqual(b"test", r.contig)
-            self.assertEqual(b"test", r[0])
-
-            r.start += 1
-            self.assertEqual(int(c[1]) + 1, r.start)
-            self.assertEqual(str(int(c[1]) + 1), r[1].decode("ascii"))
-
-            r.end += 1
-            self.assertEqual(int(c[2]) + 1, r.end)
-            self.assertEqual(str(int(c[2]) + 1), r[2].decode("ascii"))
-
-
-class TestVCF(unittest.TestCase):
-
-    filename = os.path.join(DATADIR, "example.vcf40")
-
-    def setUp(self):
-
-        self.tmpfilename = "tmp_%s.vcf" % id(self)
-        shutil.copyfile(self.filename, self.tmpfilename)
-        pysam.tabix_index(self.tmpfilename, preset="vcf")
-
-    def tearDown(self):
-        os.unlink(self.tmpfilename + ".gz")
-        if os.path.exists(self.tmpfilename + ".gz.tbi"):
-            os.unlink(self.tmpfilename + ".gz.tbi")
-
-
-class TestVCFFromTabix(TestVCF):
-
-    columns = ("contig", "pos", "id",
-               "ref", "alt", "qual",
-               "filter", "info", "format")
-
-    def setUp(self):
-
-        TestVCF.setUp(self)
-
-        self.tabix = pysam.Tabixfile(self.tmpfilename + ".gz")
-        self.compare = loadAndConvert(self.filename)
-
-    def testRead(self):
-
-        ncolumns = len(self.columns)
-
-        for x, r in enumerate(self.tabix.fetch(parser=pysam.asVCF())):
-            c = self.compare[x]
-            for y, field in enumerate(self.columns):
-                # it is ok to have a missing format column
-                if y == 8 and y == len(c):
-                    continue
-                if field == "pos":
-                    self.assertEqual(int(c[y]) - 1, getattr(r, field))
-                    self.assertEqual(int(c[y]) - 1, r.pos)
-                else:
-                    self.assertEqual(c[y], getattr(r, field),
-                                     "mismatch in field %s: %s != %s" %
-                                     (field, c[y], getattr(r, field)))
-            if len(c) == 8:
-                self.assertEqual(0, len(r))
-            else:
-                self.assertEqual(len(c), len(r) + ncolumns)
-
-            for y in range(len(c) - ncolumns):
-                self.assertEqual(c[ncolumns + y], r[y])
-
-    def testWrite(self):
-
-        ncolumns = len(self.columns)
-
-        for x, r in enumerate(self.tabix.fetch(parser=pysam.asVCF())):
-            c = self.compare[x]
-            # check unmodified string
-            cmp_string = str(r)
-            ref_string = "\t".join([x.decode() for x in c])
-
-            self.assertEqual(ref_string, cmp_string)
-
-            # set fields and compare field-wise
-            for y, field in enumerate(self.columns):
-                # it is ok to have a missing format column
-                if y == 8 and y == len(c):
-                    continue
-                if field == "pos":
-                    rpos = getattr(r, field)
-                    self.assertEqual(int(c[y]) - 1, rpos)
-                    self.assertEqual(int(c[y]) - 1, r.pos)
-                    # increment pos by 1
-                    setattr(r, field, rpos + 1)
-                    self.assertEqual(getattr(r, field), rpos + 1)
-                    c[y] = str(int(c[y]) + 1)
-                else:
-                    setattr(r, field, "test_%i" % y)
-                    c[y] = ("test_%i" % y).encode('ascii')
-                    self.assertEqual(c[y], getattr(r, field),
-                                     "mismatch in field %s: %s != %s" %
-                                     (field, c[y], getattr(r, field)))
-
-            if len(c) == 8:
-                self.assertEqual(0, len(r))
-            else:
-                self.assertEqual(len(c), len(r) + ncolumns)
-
-            for y in range(len(c) - ncolumns):
-                c[ncolumns + y] = ("test_%i" % y).encode('ascii')
-                r[y] = ("test_%i" % y).encode('ascii')
-                self.assertEqual(c[ncolumns + y], r[y])
-
-
-class TestVCFFromVCF(TestVCF):
-
-    columns = ("chrom", "pos", "id",
-               "ref", "alt", "qual",
-               "filter", "info", "format")
-
-    # tests failing while parsing
-    fail_on_parsing = (
-        (5, "Flag fields should not have a value"),
-        (9, "aouao"),
-        (13, "aoeu"),
-        (18, "Error BAD_NUMBER_OF_PARAMETERS"),
-        (24, "Error HEADING_NOT_SEPARATED_BY_TABS"))
-
-    # tests failing on opening
-    fail_on_opening = ((24, "Error HEADING_NOT_SEPARATED_BY_TABS"),
-                       )
-
-    def setUp(self):
-
-        TestVCF.setUp(self)
-
-        self.vcf = pysam.VCF()
-        self.compare = loadAndConvert(self.filename, encode=False)
-
-    def testConnecting(self):
-
-        fn = os.path.basename(self.filename)
-        for x, msg in self.fail_on_opening:
-            if "%i.vcf" % x == fn:
-                self.assertRaises(ValueError,
-                                  self.vcf.connect,
-                                  self.tmpfilename + ".gz")
-            else:
-                self.vcf.connect(self.tmpfilename + ".gz")
-
-    def testParsing(self):
-
-        fn = os.path.basename(self.filename)
-        with open(self.filename) as f:
-
-            for x, msg in self.fail_on_opening:
-                if "%i.vcf" % x == fn:
-                    self.assertRaises(ValueError, self.vcf.parse, f)
-                    return
-            else:
-                iter = self.vcf.parse(f)
-
-            for x, msg in self.fail_on_parsing:
-                if "%i.vcf" % x == fn:
-                    self.assertRaises(ValueError, list, iter)
-                    break
-                    # python 2.7
-                    # self.assertRaisesRegexp(
-                    # ValueError, re.compile(msg), self.vcf.parse, f)
-            else:
-                # do the actual parsing
-                for x, r in enumerate(iter):
-                    c = self.compare[x]
-                    for y, field in enumerate(self.columns):
-                        # it is ok to have a missing format column
-                        if y == 8 and y == len(c):
-                            continue
-
-                        val = r[field]
-                        if field == "pos":
-                            self.assertEqual(int(c[y]) - 1, val)
-                        elif field == "alt":
-                            if c[y] == ".":
-                                # convert . to empty list
-                                self.assertEqual(
-                                    [], val,
-                                    "mismatch in field %s: expected %s, got %s" %
-                                    (field, c[y], val))
-                            else:
-                                # convert to list
-                                self.assertEqual(
-                                    c[y].split(","), val,
-                                    "mismatch in field %s: expected %s, got %s" %
-                                    (field, c[y], val))
-                                
-                        elif field == "filter":
-                            if c[y] == "PASS" or c[y] == ".":
-                                # convert PASS to empty list
-                                self.assertEqual(
-                                    [], val,
-                                    "mismatch in field %s: expected %s, got %s" %
-                                    (field, c[y], val))
-                            else:
-                                # convert to list
-                                self.assertEqual(
-                                    c[y].split(";"), val,
-                                    "mismatch in field %s: expected %s, got %s" %
-                                    (field, c[y], val))
-
-                        elif field == "info":
-                            # tests for info field not implemented
-                            pass
-                        elif field == "qual" and c[y] == ".":
-                            self.assertEqual(
-                                -1, val,
-                                "mismatch in field %s: expected %s, got %s" %
-                                (field, c[y], val))
-
-                        elif field == "format":
-                            # format field converted to list
-                            self.assertEqual(
-                                c[y].split(":"), val,
-                                "mismatch in field %s: expected %s, got %s" %
-                                (field, c[y], val))
-
-                        elif type(val) in (int, float):
-                            if c[y] == ".":
-                                self.assertEqual(
-                                    None, val,
-                                    "mismatch in field %s: expected %s, got %s" %
-                                    (field, c[y], val))
-                            else:
-                                self.assertEqual(
-                                    float(c[y]), float(val),
-                                    "mismatch in field %s: expected %s, got %s" %
-                                    (field, c[y], val))
-                        else:
-                            self.assertEqual(
-                                c[y], val,
-                                "mismatch in field %s: expected %s, got %s" %
-                                (field, c[y], val))
-
-############################################################################
-# create a test class for each example vcf file.
-# Two samples are created -
-# 1. Testing pysam/tabix access
-# 2. Testing the VCF class
-vcf_files = glob.glob(os.path.join(DATADIR, "vcf", "*.vcf"))
-
-for vcf_file in vcf_files:
-    n = "VCFFromTabixTest_%s" % os.path.basename(vcf_file[:-4])
-    globals()[n] = type(n, (TestVCFFromTabix,), dict(filename=vcf_file,))
-    n = "VCFFromVCFTest_%s" % os.path.basename(vcf_file[:-4])
-    globals()[n] = type(n, (TestVCFFromVCF,), dict(filename=vcf_file,))
-
-############################################################################
-
-
-class TestRemoteFileHTTP(unittest.TestCase):
-
-    url = "http://genserv.anat.ox.ac.uk/downloads/pysam/test/example_htslib.gtf.gz"
-    region = "chr1:1-1000"
-    local = os.path.join(DATADIR, "example.gtf.gz")
-
-    def testFetchAll(self):
-        remote_file = pysam.Tabixfile(self.url, "r")
-        remote_result = list(remote_file.fetch())
-        local_file = pysam.Tabixfile(self.local, "r")
-        local_result = list(local_file.fetch())
-
-        self.assertEqual(len(remote_result), len(local_result))
-        for x, y in zip(remote_result, local_result):
-            self.assertEqual(x, y)
-
-
-class TestIndexArgument(unittest.TestCase):
-
-    filename_src = os.path.join(DATADIR, "example.vcf.gz")
-    filename_dst = "tmp_example.vcf.gz"
-    index_src = os.path.join(DATADIR, "example.vcf.gz.tbi")
-    index_dst = "tmp_index_example.vcf.gz.tbi"
-    preset = "vcf"
-
-    def testFetchAll(self):
-        shutil.copyfile(self.filename_src, self.filename_dst)
-        shutil.copyfile(self.index_src, self.index_dst)
-        same_basename_file = pysam.Tabixfile(
-            self.filename_src, "r", index=self.index_src)
-        same_basename_results = list(same_basename_file.fetch())
-        diff_index_file = pysam.Tabixfile(
-            self.filename_dst, "r", index=self.index_dst)
-        diff_index_result = list(diff_index_file.fetch())
-
-        self.assertEqual(len(same_basename_results), len(diff_index_result))
-        for x, y in zip(same_basename_results, diff_index_result):
-            self.assertEqual(x, y)
-
-if __name__ == "__main__":
-
-    unittest.main()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-pysam.git



More information about the debian-med-commit mailing list